*** empty log message ***
[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"
4c2df51b 60
c906108c
SS
61#include <fcntl.h>
62#include "gdb_string.h"
4bdf3d34 63#include "gdb_assert.h"
c906108c 64#include <sys/types.h>
233a11ab
CS
65#ifdef HAVE_ZLIB_H
66#include <zlib.h>
67#endif
dce234bc
PP
68#ifdef HAVE_MMAP
69#include <sys/mman.h>
85d9bd0e
TT
70#ifndef MAP_FAILED
71#define MAP_FAILED ((void *) -1)
72#endif
dce234bc 73#endif
d8151005 74
34eaf542
TT
75typedef struct symbol *symbolp;
76DEF_VEC_P (symbolp);
77
107d2387 78#if 0
357e46e7 79/* .debug_info header for a compilation unit
c906108c
SS
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91_COMP_UNIT_HEADER;
92#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 93#endif
c906108c 94
c906108c
SS
95/* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116_STATEMENT_PROLOGUE;
117
d97bc12b
DE
118/* When non-zero, dump DIEs after they are read in. */
119static int dwarf2_die_debug = 0;
120
dce234bc
PP
121static int pagesize;
122
df8a16a1
DJ
123/* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127static int processing_has_namespace_info;
128
6502dd73
DJ
129static const struct objfile_data *dwarf2_objfile_data_key;
130
dce234bc
PP
131struct dwarf2_section_info
132{
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
be391dca
TT
137 /* True if we have tried to read this section. */
138 int readin;
dce234bc
PP
139};
140
9291a0cd
TT
141/* All offsets in the index are of this type. It must be
142 architecture-independent. */
143typedef uint32_t offset_type;
144
145DEF_VEC_I (offset_type);
146
147/* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149struct mapped_index
150{
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
3876f04e
DE
157 /* The symbol table, implemented as a hash table. */
158 const offset_type *symbol_table;
9291a0cd 159 /* Size in slots, each slot is 2 offset_types. */
3876f04e 160 offset_type symbol_table_slots;
9291a0cd
TT
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163};
164
6502dd73
DJ
165struct dwarf2_per_objfile
166{
dce234bc
PP
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
dce234bc
PP
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
348e048f 174 struct dwarf2_section_info types;
dce234bc
PP
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
9291a0cd 177 struct dwarf2_section_info gdb_index;
ae038cb0 178
be391dca
TT
179 /* Back link. */
180 struct objfile *objfile;
181
10b3939b
DJ
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
ae038cb0
DJ
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
1fd400ff
TT
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
ae038cb0
DJ
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 198
348e048f
DE
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
72dca2f5
FR
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
9291a0cd 206
ae2de4f8
DE
207 /* True if we are using the mapped index,
208 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
209 unsigned char using_index;
210
ae2de4f8 211 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 212 struct mapped_index *index_table;
98bfdba5 213
7b9f3c50
DE
214 /* When using index_table, this keeps track of all quick_file_names entries.
215 TUs can share line table entries with CUs or other TUs, and there can be
216 a lot more TUs than unique line tables, so we maintain a separate table
217 of all line table entries to support the sharing. */
218 htab_t quick_file_names_table;
219
98bfdba5
PA
220 /* Set during partial symbol reading, to prevent queueing of full
221 symbols. */
222 int reading_partial_symbols;
673bfd45
DE
223
224 /* Table mapping type .debug_info DIE offsets to types.
225 This is NULL if not allocated yet.
226 It (currently) makes sense to allocate debug_types_type_hash lazily.
227 To keep things simple we allocate both lazily. */
228 htab_t debug_info_type_hash;
229
230 /* Table mapping type .debug_types DIE offsets to types.
231 This is NULL if not allocated yet. */
232 htab_t debug_types_type_hash;
6502dd73
DJ
233};
234
235static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
236
237/* names of the debugging sections */
238
233a11ab
CS
239/* Note that if the debugging section has been compressed, it might
240 have a name like .zdebug_info. */
241
242#define INFO_SECTION "debug_info"
243#define ABBREV_SECTION "debug_abbrev"
244#define LINE_SECTION "debug_line"
233a11ab
CS
245#define LOC_SECTION "debug_loc"
246#define MACINFO_SECTION "debug_macinfo"
247#define STR_SECTION "debug_str"
248#define RANGES_SECTION "debug_ranges"
348e048f 249#define TYPES_SECTION "debug_types"
233a11ab
CS
250#define FRAME_SECTION "debug_frame"
251#define EH_FRAME_SECTION "eh_frame"
9291a0cd 252#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
253
254/* local data types */
255
0963b4bd 256/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
257#ifndef ABBREV_HASH_SIZE
258#define ABBREV_HASH_SIZE 121
259#endif
260
107d2387
AC
261/* The data in a compilation unit header, after target2host
262 translation, looks like this. */
c906108c 263struct comp_unit_head
a738430d 264{
c764a876 265 unsigned int length;
a738430d 266 short version;
a738430d
MK
267 unsigned char addr_size;
268 unsigned char signed_addr_p;
9cbfa09e 269 unsigned int abbrev_offset;
57349743 270
a738430d
MK
271 /* Size of file offsets; either 4 or 8. */
272 unsigned int offset_size;
57349743 273
a738430d
MK
274 /* Size of the length field; either 4 or 12. */
275 unsigned int initial_length_size;
57349743 276
a738430d
MK
277 /* Offset to the first byte of this compilation unit header in the
278 .debug_info section, for resolving relative reference dies. */
279 unsigned int offset;
57349743 280
d00adf39
DE
281 /* Offset to first die in this cu from the start of the cu.
282 This will be the first byte following the compilation unit header. */
283 unsigned int first_die_offset;
a738430d 284};
c906108c 285
3da10d80
KS
286/* Type used for delaying computation of method physnames.
287 See comments for compute_delayed_physnames. */
288struct delayed_method_info
289{
290 /* The type to which the method is attached, i.e., its parent class. */
291 struct type *type;
292
293 /* The index of the method in the type's function fieldlists. */
294 int fnfield_index;
295
296 /* The index of the method in the fieldlist. */
297 int index;
298
299 /* The name of the DIE. */
300 const char *name;
301
302 /* The DIE associated with this method. */
303 struct die_info *die;
304};
305
306typedef struct delayed_method_info delayed_method_info;
307DEF_VEC_O (delayed_method_info);
308
e7c27a73
DJ
309/* Internal state when decoding a particular compilation unit. */
310struct dwarf2_cu
311{
312 /* The objfile containing this compilation unit. */
313 struct objfile *objfile;
314
d00adf39 315 /* The header of the compilation unit. */
e7c27a73 316 struct comp_unit_head header;
e142c38c 317
d00adf39
DE
318 /* Base address of this compilation unit. */
319 CORE_ADDR base_address;
320
321 /* Non-zero if base_address has been set. */
322 int base_known;
323
e142c38c
DJ
324 struct function_range *first_fn, *last_fn, *cached_fn;
325
326 /* The language we are debugging. */
327 enum language language;
328 const struct language_defn *language_defn;
329
b0f35d58
DL
330 const char *producer;
331
e142c38c
DJ
332 /* The generic symbol table building routines have separate lists for
333 file scope symbols and all all other scopes (local scopes). So
334 we need to select the right one to pass to add_symbol_to_list().
335 We do it by keeping a pointer to the correct list in list_in_scope.
336
337 FIXME: The original dwarf code just treated the file scope as the
338 first local scope, and all other local scopes as nested local
339 scopes, and worked fine. Check to see if we really need to
340 distinguish these in buildsym.c. */
341 struct pending **list_in_scope;
342
f3dd6933
DJ
343 /* DWARF abbreviation table associated with this compilation unit. */
344 struct abbrev_info **dwarf2_abbrevs;
345
346 /* Storage for the abbrev table. */
347 struct obstack abbrev_obstack;
72bf9492
DJ
348
349 /* Hash table holding all the loaded partial DIEs. */
350 htab_t partial_dies;
351
352 /* Storage for things with the same lifetime as this read-in compilation
353 unit, including partial DIEs. */
354 struct obstack comp_unit_obstack;
355
ae038cb0
DJ
356 /* When multiple dwarf2_cu structures are living in memory, this field
357 chains them all together, so that they can be released efficiently.
358 We will probably also want a generation counter so that most-recently-used
359 compilation units are cached... */
360 struct dwarf2_per_cu_data *read_in_chain;
361
362 /* Backchain to our per_cu entry if the tree has been built. */
363 struct dwarf2_per_cu_data *per_cu;
364
365 /* How many compilation units ago was this CU last referenced? */
366 int last_used;
367
10b3939b 368 /* A hash table of die offsets for following references. */
51545339 369 htab_t die_hash;
10b3939b
DJ
370
371 /* Full DIEs if read in. */
372 struct die_info *dies;
373
374 /* A set of pointers to dwarf2_per_cu_data objects for compilation
375 units referenced by this one. Only set during full symbol processing;
376 partial symbol tables do not have dependencies. */
377 htab_t dependencies;
378
cb1df416
DJ
379 /* Header data from the line table, during full symbol processing. */
380 struct line_header *line_header;
381
3da10d80
KS
382 /* A list of methods which need to have physnames computed
383 after all type information has been read. */
384 VEC (delayed_method_info) *method_list;
385
ae038cb0
DJ
386 /* Mark used when releasing cached dies. */
387 unsigned int mark : 1;
388
389 /* This flag will be set if this compilation unit might include
390 inter-compilation-unit references. */
391 unsigned int has_form_ref_addr : 1;
392
72bf9492
DJ
393 /* This flag will be set if this compilation unit includes any
394 DW_TAG_namespace DIEs. If we know that there are explicit
395 DIEs for namespaces, we don't need to try to infer them
396 from mangled names. */
397 unsigned int has_namespace_info : 1;
e7c27a73
DJ
398};
399
10b3939b
DJ
400/* Persistent data held for a compilation unit, even when not
401 processing it. We put a pointer to this structure in the
402 read_symtab_private field of the psymtab. If we encounter
403 inter-compilation-unit references, we also maintain a sorted
404 list of all compilation units. */
405
ae038cb0
DJ
406struct dwarf2_per_cu_data
407{
348e048f 408 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 409 bytes should suffice to store the length of any compilation unit
45452591
DE
410 - if it doesn't, GDB will fall over anyway.
411 NOTE: Unlike comp_unit_head.length, this length includes
412 initial_length_size. */
c764a876 413 unsigned int offset;
348e048f 414 unsigned int length : 29;
ae038cb0
DJ
415
416 /* Flag indicating this compilation unit will be read in before
417 any of the current compilation units are processed. */
c764a876 418 unsigned int queued : 1;
ae038cb0 419
5afb4e99
DJ
420 /* This flag will be set if we need to load absolutely all DIEs
421 for this compilation unit, instead of just the ones we think
422 are interesting. It gets set if we look for a DIE in the
423 hash table and don't find it. */
424 unsigned int load_all_dies : 1;
425
348e048f
DE
426 /* Non-zero if this CU is from .debug_types.
427 Otherwise it's from .debug_info. */
428 unsigned int from_debug_types : 1;
429
17ea53c3
JK
430 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
431 of the CU cache it gets reset to NULL again. */
ae038cb0 432 struct dwarf2_cu *cu;
1c379e20 433
9291a0cd
TT
434 /* The corresponding objfile. */
435 struct objfile *objfile;
436
437 /* When using partial symbol tables, the 'psymtab' field is active.
438 Otherwise the 'quick' field is active. */
439 union
440 {
441 /* The partial symbol table associated with this compilation unit,
442 or NULL for partial units (which do not have an associated
443 symtab). */
444 struct partial_symtab *psymtab;
445
446 /* Data needed by the "quick" functions. */
447 struct dwarf2_per_cu_quick_data *quick;
448 } v;
ae038cb0
DJ
449};
450
348e048f
DE
451/* Entry in the signatured_types hash table. */
452
453struct signatured_type
454{
455 ULONGEST signature;
456
457 /* Offset in .debug_types of the TU (type_unit) for this type. */
458 unsigned int offset;
459
460 /* Offset in .debug_types of the type defined by this TU. */
461 unsigned int type_offset;
462
463 /* The CU(/TU) of this type. */
464 struct dwarf2_per_cu_data per_cu;
465};
466
0963b4bd
MS
467/* Struct used to pass misc. parameters to read_die_and_children, et
468 al. which are used for both .debug_info and .debug_types dies.
469 All parameters here are unchanging for the life of the call. This
470 struct exists to abstract away the constant parameters of die
471 reading. */
93311388
DE
472
473struct die_reader_specs
474{
475 /* The bfd of this objfile. */
476 bfd* abfd;
477
478 /* The CU of the DIE we are parsing. */
479 struct dwarf2_cu *cu;
480
481 /* Pointer to start of section buffer.
482 This is either the start of .debug_info or .debug_types. */
483 const gdb_byte *buffer;
484};
485
debd256d
JB
486/* The line number information for a compilation unit (found in the
487 .debug_line section) begins with a "statement program header",
488 which contains the following information. */
489struct line_header
490{
491 unsigned int total_length;
492 unsigned short version;
493 unsigned int header_length;
494 unsigned char minimum_instruction_length;
2dc7f7b3 495 unsigned char maximum_ops_per_instruction;
debd256d
JB
496 unsigned char default_is_stmt;
497 int line_base;
498 unsigned char line_range;
499 unsigned char opcode_base;
500
501 /* standard_opcode_lengths[i] is the number of operands for the
502 standard opcode whose value is i. This means that
503 standard_opcode_lengths[0] is unused, and the last meaningful
504 element is standard_opcode_lengths[opcode_base - 1]. */
505 unsigned char *standard_opcode_lengths;
506
507 /* The include_directories table. NOTE! These strings are not
508 allocated with xmalloc; instead, they are pointers into
509 debug_line_buffer. If you try to free them, `free' will get
510 indigestion. */
511 unsigned int num_include_dirs, include_dirs_size;
512 char **include_dirs;
513
514 /* The file_names table. NOTE! These strings are not allocated
515 with xmalloc; instead, they are pointers into debug_line_buffer.
516 Don't try to free them directly. */
517 unsigned int num_file_names, file_names_size;
518 struct file_entry
c906108c 519 {
debd256d
JB
520 char *name;
521 unsigned int dir_index;
522 unsigned int mod_time;
523 unsigned int length;
aaa75496 524 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 525 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
526 } *file_names;
527
528 /* The start and end of the statement program following this
6502dd73 529 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 530 gdb_byte *statement_program_start, *statement_program_end;
debd256d 531};
c906108c
SS
532
533/* When we construct a partial symbol table entry we only
0963b4bd 534 need this much information. */
c906108c
SS
535struct partial_die_info
536 {
72bf9492 537 /* Offset of this DIE. */
c906108c 538 unsigned int offset;
72bf9492
DJ
539
540 /* DWARF-2 tag for this DIE. */
541 ENUM_BITFIELD(dwarf_tag) tag : 16;
542
72bf9492
DJ
543 /* Assorted flags describing the data found in this DIE. */
544 unsigned int has_children : 1;
545 unsigned int is_external : 1;
546 unsigned int is_declaration : 1;
547 unsigned int has_type : 1;
548 unsigned int has_specification : 1;
549 unsigned int has_pc_info : 1;
550
551 /* Flag set if the SCOPE field of this structure has been
552 computed. */
553 unsigned int scope_set : 1;
554
fa4028e9
JB
555 /* Flag set if the DIE has a byte_size attribute. */
556 unsigned int has_byte_size : 1;
557
98bfdba5
PA
558 /* Flag set if any of the DIE's children are template arguments. */
559 unsigned int has_template_arguments : 1;
560
abc72ce4
DE
561 /* Flag set if fixup_partial_die has been called on this die. */
562 unsigned int fixup_called : 1;
563
72bf9492 564 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 565 sometimes a default name for unnamed DIEs. */
c906108c 566 char *name;
72bf9492 567
abc72ce4
DE
568 /* The linkage name, if present. */
569 const char *linkage_name;
570
72bf9492
DJ
571 /* The scope to prepend to our children. This is generally
572 allocated on the comp_unit_obstack, so will disappear
573 when this compilation unit leaves the cache. */
574 char *scope;
575
576 /* The location description associated with this DIE, if any. */
577 struct dwarf_block *locdesc;
578
579 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
580 CORE_ADDR lowpc;
581 CORE_ADDR highpc;
72bf9492 582
93311388 583 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 584 DW_AT_sibling, if any. */
abc72ce4
DE
585 /* NOTE: This member isn't strictly necessary, read_partial_die could
586 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 587 gdb_byte *sibling;
72bf9492
DJ
588
589 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
590 DW_AT_specification (or DW_AT_abstract_origin or
591 DW_AT_extension). */
592 unsigned int spec_offset;
593
594 /* Pointers to this DIE's parent, first child, and next sibling,
595 if any. */
596 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
597 };
598
0963b4bd 599/* This data structure holds the information of an abbrev. */
c906108c
SS
600struct abbrev_info
601 {
602 unsigned int number; /* number identifying abbrev */
603 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
604 unsigned short has_children; /* boolean */
605 unsigned short num_attrs; /* number of attributes */
c906108c
SS
606 struct attr_abbrev *attrs; /* an array of attribute descriptions */
607 struct abbrev_info *next; /* next in chain */
608 };
609
610struct attr_abbrev
611 {
9d25dd43
DE
612 ENUM_BITFIELD(dwarf_attribute) name : 16;
613 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
614 };
615
0963b4bd 616/* Attributes have a name and a value. */
b60c80d6
DJ
617struct attribute
618 {
9d25dd43 619 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
620 ENUM_BITFIELD(dwarf_form) form : 15;
621
622 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
623 field should be in u.str (existing only for DW_STRING) but it is kept
624 here for better struct attribute alignment. */
625 unsigned int string_is_canonical : 1;
626
b60c80d6
DJ
627 union
628 {
629 char *str;
630 struct dwarf_block *blk;
43bbcdc2
PH
631 ULONGEST unsnd;
632 LONGEST snd;
b60c80d6 633 CORE_ADDR addr;
348e048f 634 struct signatured_type *signatured_type;
b60c80d6
DJ
635 }
636 u;
637 };
638
0963b4bd 639/* This data structure holds a complete die structure. */
c906108c
SS
640struct die_info
641 {
76815b17
DE
642 /* DWARF-2 tag for this DIE. */
643 ENUM_BITFIELD(dwarf_tag) tag : 16;
644
645 /* Number of attributes */
98bfdba5
PA
646 unsigned char num_attrs;
647
648 /* True if we're presently building the full type name for the
649 type derived from this DIE. */
650 unsigned char building_fullname : 1;
76815b17
DE
651
652 /* Abbrev number */
653 unsigned int abbrev;
654
93311388 655 /* Offset in .debug_info or .debug_types section. */
76815b17 656 unsigned int offset;
78ba4af6
JB
657
658 /* The dies in a compilation unit form an n-ary tree. PARENT
659 points to this die's parent; CHILD points to the first child of
660 this node; and all the children of a given node are chained
4950bc1c 661 together via their SIBLING fields. */
639d11d3
DC
662 struct die_info *child; /* Its first child, if any. */
663 struct die_info *sibling; /* Its next sibling, if any. */
664 struct die_info *parent; /* Its parent, if any. */
c906108c 665
b60c80d6
DJ
666 /* An array of attributes, with NUM_ATTRS elements. There may be
667 zero, but it's not common and zero-sized arrays are not
668 sufficiently portable C. */
669 struct attribute attrs[1];
c906108c
SS
670 };
671
5fb290d7
DJ
672struct function_range
673{
674 const char *name;
675 CORE_ADDR lowpc, highpc;
676 int seen_line;
677 struct function_range *next;
678};
679
0963b4bd 680/* Get at parts of an attribute structure. */
c906108c
SS
681
682#define DW_STRING(attr) ((attr)->u.str)
8285870a 683#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
684#define DW_UNSND(attr) ((attr)->u.unsnd)
685#define DW_BLOCK(attr) ((attr)->u.blk)
686#define DW_SND(attr) ((attr)->u.snd)
687#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 688#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 689
0963b4bd 690/* Blocks are a bunch of untyped bytes. */
c906108c
SS
691struct dwarf_block
692 {
693 unsigned int size;
fe1b8b76 694 gdb_byte *data;
c906108c
SS
695 };
696
c906108c
SS
697#ifndef ATTR_ALLOC_CHUNK
698#define ATTR_ALLOC_CHUNK 4
699#endif
700
c906108c
SS
701/* Allocate fields for structs, unions and enums in this size. */
702#ifndef DW_FIELD_ALLOC_CHUNK
703#define DW_FIELD_ALLOC_CHUNK 4
704#endif
705
c906108c
SS
706/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
707 but this would require a corresponding change in unpack_field_as_long
708 and friends. */
709static int bits_per_byte = 8;
710
711/* The routines that read and process dies for a C struct or C++ class
712 pass lists of data member fields and lists of member function fields
713 in an instance of a field_info structure, as defined below. */
714struct field_info
c5aa993b 715 {
0963b4bd 716 /* List of data member and baseclasses fields. */
c5aa993b
JM
717 struct nextfield
718 {
719 struct nextfield *next;
720 int accessibility;
721 int virtuality;
722 struct field field;
723 }
7d0ccb61 724 *fields, *baseclasses;
c906108c 725
7d0ccb61 726 /* Number of fields (including baseclasses). */
c5aa993b 727 int nfields;
c906108c 728
c5aa993b
JM
729 /* Number of baseclasses. */
730 int nbaseclasses;
c906108c 731
c5aa993b
JM
732 /* Set if the accesibility of one of the fields is not public. */
733 int non_public_fields;
c906108c 734
c5aa993b
JM
735 /* Member function fields array, entries are allocated in the order they
736 are encountered in the object file. */
737 struct nextfnfield
738 {
739 struct nextfnfield *next;
740 struct fn_field fnfield;
741 }
742 *fnfields;
c906108c 743
c5aa993b
JM
744 /* Member function fieldlist array, contains name of possibly overloaded
745 member function, number of overloaded member functions and a pointer
746 to the head of the member function field chain. */
747 struct fnfieldlist
748 {
749 char *name;
750 int length;
751 struct nextfnfield *head;
752 }
753 *fnfieldlists;
c906108c 754
c5aa993b
JM
755 /* Number of entries in the fnfieldlists array. */
756 int nfnfields;
98751a41
JK
757
758 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
759 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
760 struct typedef_field_list
761 {
762 struct typedef_field field;
763 struct typedef_field_list *next;
764 }
765 *typedef_field_list;
766 unsigned typedef_field_list_count;
c5aa993b 767 };
c906108c 768
10b3939b
DJ
769/* One item on the queue of compilation units to read in full symbols
770 for. */
771struct dwarf2_queue_item
772{
773 struct dwarf2_per_cu_data *per_cu;
774 struct dwarf2_queue_item *next;
775};
776
777/* The current queue. */
778static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
779
ae038cb0
DJ
780/* Loaded secondary compilation units are kept in memory until they
781 have not been referenced for the processing of this many
782 compilation units. Set this to zero to disable caching. Cache
783 sizes of up to at least twenty will improve startup time for
784 typical inter-CU-reference binaries, at an obvious memory cost. */
785static int dwarf2_max_cache_age = 5;
920d2a44
AC
786static void
787show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
788 struct cmd_list_element *c, const char *value)
789{
3e43a32a
MS
790 fprintf_filtered (file, _("The upper bound on the age of cached "
791 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
792 value);
793}
794
ae038cb0 795
0963b4bd 796/* Various complaints about symbol reading that don't abort the process. */
c906108c 797
4d3c2250
KB
798static void
799dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 800{
4d3c2250 801 complaint (&symfile_complaints,
e2e0b3e5 802 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
803}
804
25e43795
DJ
805static void
806dwarf2_debug_line_missing_file_complaint (void)
807{
808 complaint (&symfile_complaints,
809 _(".debug_line section has line data without a file"));
810}
811
59205f5a
JB
812static void
813dwarf2_debug_line_missing_end_sequence_complaint (void)
814{
815 complaint (&symfile_complaints,
3e43a32a
MS
816 _(".debug_line section has line "
817 "program sequence without an end"));
59205f5a
JB
818}
819
4d3c2250
KB
820static void
821dwarf2_complex_location_expr_complaint (void)
2e276125 822{
e2e0b3e5 823 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
824}
825
4d3c2250
KB
826static void
827dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
828 int arg3)
2e276125 829{
4d3c2250 830 complaint (&symfile_complaints,
3e43a32a
MS
831 _("const value length mismatch for '%s', got %d, expected %d"),
832 arg1, arg2, arg3);
4d3c2250
KB
833}
834
835static void
836dwarf2_macros_too_long_complaint (void)
2e276125 837{
4d3c2250 838 complaint (&symfile_complaints,
e2e0b3e5 839 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
840}
841
842static void
843dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 844{
4d3c2250 845 complaint (&symfile_complaints,
3e43a32a
MS
846 _("macro debug info contains a "
847 "malformed macro definition:\n`%s'"),
4d3c2250
KB
848 arg1);
849}
850
851static void
852dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 853{
4d3c2250 854 complaint (&symfile_complaints,
3e43a32a
MS
855 _("invalid attribute class or form for '%s' in '%s'"),
856 arg1, arg2);
4d3c2250 857}
c906108c 858
c906108c
SS
859/* local function prototypes */
860
4efb68b1 861static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 862
aaa75496
JB
863static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
864 struct objfile *);
865
c67a9c90 866static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 867
72bf9492
DJ
868static void scan_partial_symbols (struct partial_die_info *,
869 CORE_ADDR *, CORE_ADDR *,
5734ee8b 870 int, struct dwarf2_cu *);
c906108c 871
72bf9492
DJ
872static void add_partial_symbol (struct partial_die_info *,
873 struct dwarf2_cu *);
63d06c5c 874
72bf9492
DJ
875static void add_partial_namespace (struct partial_die_info *pdi,
876 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 877 int need_pc, struct dwarf2_cu *cu);
63d06c5c 878
5d7cb8df
JK
879static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
880 CORE_ADDR *highpc, int need_pc,
881 struct dwarf2_cu *cu);
882
72bf9492
DJ
883static void add_partial_enumeration (struct partial_die_info *enum_pdi,
884 struct dwarf2_cu *cu);
91c24f0a 885
bc30ff58
JB
886static void add_partial_subprogram (struct partial_die_info *pdi,
887 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 888 int need_pc, struct dwarf2_cu *cu);
bc30ff58 889
fe1b8b76 890static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
891 gdb_byte *buffer, gdb_byte *info_ptr,
892 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 893
a14ed312 894static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 895
a14ed312 896static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 897
e7c27a73 898static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 899
f3dd6933 900static void dwarf2_free_abbrev_table (void *);
c906108c 901
fe1b8b76 902static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 903 struct dwarf2_cu *);
72bf9492 904
57349743 905static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 906 struct dwarf2_cu *);
c906108c 907
93311388
DE
908static struct partial_die_info *load_partial_dies (bfd *,
909 gdb_byte *, gdb_byte *,
910 int, struct dwarf2_cu *);
72bf9492 911
fe1b8b76 912static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
913 struct abbrev_info *abbrev,
914 unsigned int, bfd *,
915 gdb_byte *, gdb_byte *,
916 struct dwarf2_cu *);
c906108c 917
c764a876 918static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 919 struct dwarf2_cu *);
72bf9492
DJ
920
921static void fixup_partial_die (struct partial_die_info *,
922 struct dwarf2_cu *);
923
fe1b8b76
JB
924static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
925 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 926
fe1b8b76
JB
927static gdb_byte *read_attribute_value (struct attribute *, unsigned,
928 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 929
fe1b8b76 930static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 931
fe1b8b76 932static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 933
fe1b8b76 934static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 935
fe1b8b76 936static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 937
93311388 938static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 939
fe1b8b76 940static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 941 unsigned int *);
c906108c 942
c764a876
DE
943static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
944
945static LONGEST read_checked_initial_length_and_offset
946 (bfd *, gdb_byte *, const struct comp_unit_head *,
947 unsigned int *, unsigned int *);
613e1657 948
fe1b8b76 949static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
950 unsigned int *);
951
952static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 953
fe1b8b76 954static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 955
9b1c24c8 956static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 957
fe1b8b76
JB
958static char *read_indirect_string (bfd *, gdb_byte *,
959 const struct comp_unit_head *,
960 unsigned int *);
4bdf3d34 961
fe1b8b76 962static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 963
fe1b8b76 964static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 965
fe1b8b76 966static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 967
e142c38c 968static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 969
e142c38c
DJ
970static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
971 struct dwarf2_cu *);
c906108c 972
348e048f
DE
973static struct attribute *dwarf2_attr_no_follow (struct die_info *,
974 unsigned int,
975 struct dwarf2_cu *);
976
05cf31d1
JB
977static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
978 struct dwarf2_cu *cu);
979
e142c38c 980static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 981
e142c38c 982static struct die_info *die_specification (struct die_info *die,
f2f0e013 983 struct dwarf2_cu **);
63d06c5c 984
debd256d
JB
985static void free_line_header (struct line_header *lh);
986
aaa75496
JB
987static void add_file_name (struct line_header *, char *, unsigned int,
988 unsigned int, unsigned int);
989
debd256d
JB
990static struct line_header *(dwarf_decode_line_header
991 (unsigned int offset,
e7c27a73 992 bfd *abfd, struct dwarf2_cu *cu));
debd256d 993
72b9f47f 994static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 995 struct dwarf2_cu *, struct partial_symtab *);
c906108c 996
72b9f47f 997static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 998
a14ed312 999static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1000 struct dwarf2_cu *);
c906108c 1001
34eaf542
TT
1002static struct symbol *new_symbol_full (struct die_info *, struct type *,
1003 struct dwarf2_cu *, struct symbol *);
1004
a14ed312 1005static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1006 struct dwarf2_cu *);
c906108c 1007
98bfdba5
PA
1008static void dwarf2_const_value_attr (struct attribute *attr,
1009 struct type *type,
1010 const char *name,
1011 struct obstack *obstack,
1012 struct dwarf2_cu *cu, long *value,
1013 gdb_byte **bytes,
1014 struct dwarf2_locexpr_baton **baton);
2df3850c 1015
e7c27a73 1016static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1017
b4ba55a1
JB
1018static int need_gnat_info (struct dwarf2_cu *);
1019
3e43a32a
MS
1020static struct type *die_descriptive_type (struct die_info *,
1021 struct dwarf2_cu *);
b4ba55a1
JB
1022
1023static void set_descriptive_type (struct type *, struct die_info *,
1024 struct dwarf2_cu *);
1025
e7c27a73
DJ
1026static struct type *die_containing_type (struct die_info *,
1027 struct dwarf2_cu *);
c906108c 1028
673bfd45
DE
1029static struct type *lookup_die_type (struct die_info *, struct attribute *,
1030 struct dwarf2_cu *);
c906108c 1031
f792889a 1032static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1033
673bfd45
DE
1034static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1035
086ed43d 1036static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1037
6e70227d 1038static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1039 const char *suffix, int physname,
1040 struct dwarf2_cu *cu);
63d06c5c 1041
e7c27a73 1042static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1043
348e048f
DE
1044static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1045
e7c27a73 1046static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1047
e7c27a73 1048static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1049
ff013f42
JK
1050static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1051 struct dwarf2_cu *, struct partial_symtab *);
1052
a14ed312 1053static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1054 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1055 struct partial_symtab *);
c906108c 1056
fae299cd
DC
1057static void get_scope_pc_bounds (struct die_info *,
1058 CORE_ADDR *, CORE_ADDR *,
1059 struct dwarf2_cu *);
1060
801e3a5b
JB
1061static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1062 CORE_ADDR, struct dwarf2_cu *);
1063
a14ed312 1064static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1065 struct dwarf2_cu *);
c906108c 1066
a14ed312 1067static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1068 struct type *, struct dwarf2_cu *);
c906108c 1069
a14ed312 1070static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1071 struct die_info *, struct type *,
e7c27a73 1072 struct dwarf2_cu *);
c906108c 1073
a14ed312 1074static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1075 struct type *,
1076 struct dwarf2_cu *);
c906108c 1077
134d01f1 1078static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1079
e7c27a73 1080static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1081
e7c27a73 1082static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1083
5d7cb8df
JK
1084static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1085
27aa8d6a
SW
1086static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1087
f55ee35c
JK
1088static struct type *read_module_type (struct die_info *die,
1089 struct dwarf2_cu *cu);
1090
38d518c9 1091static const char *namespace_name (struct die_info *die,
e142c38c 1092 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1093
134d01f1 1094static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1095
e7c27a73 1096static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1097
6e70227d 1098static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1099 struct dwarf2_cu *);
1100
93311388 1101static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1102
93311388
DE
1103static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1104 gdb_byte *info_ptr,
d97bc12b
DE
1105 gdb_byte **new_info_ptr,
1106 struct die_info *parent);
1107
93311388
DE
1108static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1109 gdb_byte *info_ptr,
fe1b8b76 1110 gdb_byte **new_info_ptr,
639d11d3
DC
1111 struct die_info *parent);
1112
93311388
DE
1113static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1114 gdb_byte *info_ptr,
fe1b8b76 1115 gdb_byte **new_info_ptr,
639d11d3
DC
1116 struct die_info *parent);
1117
93311388
DE
1118static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1119 struct die_info **, gdb_byte *,
1120 int *);
1121
e7c27a73 1122static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1123
71c25dea
TT
1124static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1125 struct obstack *);
1126
e142c38c 1127static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1128
98bfdba5
PA
1129static const char *dwarf2_full_name (char *name,
1130 struct die_info *die,
1131 struct dwarf2_cu *cu);
1132
e142c38c 1133static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1134 struct dwarf2_cu **);
9219021c 1135
a14ed312 1136static char *dwarf_tag_name (unsigned int);
c906108c 1137
a14ed312 1138static char *dwarf_attr_name (unsigned int);
c906108c 1139
a14ed312 1140static char *dwarf_form_name (unsigned int);
c906108c 1141
a14ed312 1142static char *dwarf_bool_name (unsigned int);
c906108c 1143
a14ed312 1144static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1145
1146#if 0
a14ed312 1147static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1148#endif
1149
f9aca02d 1150static struct die_info *sibling_die (struct die_info *);
c906108c 1151
d97bc12b
DE
1152static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1153
1154static void dump_die_for_error (struct die_info *);
1155
1156static void dump_die_1 (struct ui_file *, int level, int max_level,
1157 struct die_info *);
c906108c 1158
d97bc12b 1159/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1160
51545339 1161static void store_in_ref_table (struct die_info *,
10b3939b 1162 struct dwarf2_cu *);
c906108c 1163
93311388
DE
1164static int is_ref_attr (struct attribute *);
1165
c764a876 1166static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1167
43bbcdc2 1168static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1169
348e048f
DE
1170static struct die_info *follow_die_ref_or_sig (struct die_info *,
1171 struct attribute *,
1172 struct dwarf2_cu **);
1173
10b3939b
DJ
1174static struct die_info *follow_die_ref (struct die_info *,
1175 struct attribute *,
f2f0e013 1176 struct dwarf2_cu **);
c906108c 1177
348e048f
DE
1178static struct die_info *follow_die_sig (struct die_info *,
1179 struct attribute *,
1180 struct dwarf2_cu **);
1181
1182static void read_signatured_type_at_offset (struct objfile *objfile,
1183 unsigned int offset);
1184
1185static void read_signatured_type (struct objfile *,
1186 struct signatured_type *type_sig);
1187
c906108c
SS
1188/* memory allocation interface */
1189
7b5a2f43 1190static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1191
f3dd6933 1192static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1193
b60c80d6 1194static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1195
e142c38c 1196static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1197
e142c38c
DJ
1198static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1199 struct dwarf2_cu *);
5fb290d7 1200
2e276125 1201static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1202 char *, bfd *, struct dwarf2_cu *);
2e276125 1203
8e19ed76
PS
1204static int attr_form_is_block (struct attribute *);
1205
3690dd37
JB
1206static int attr_form_is_section_offset (struct attribute *);
1207
1208static int attr_form_is_constant (struct attribute *);
1209
8cf6f0b1
TT
1210static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1211 struct dwarf2_loclist_baton *baton,
1212 struct attribute *attr);
1213
93e7bd98
DJ
1214static void dwarf2_symbol_mark_computed (struct attribute *attr,
1215 struct symbol *sym,
1216 struct dwarf2_cu *cu);
4c2df51b 1217
93311388
DE
1218static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1219 struct abbrev_info *abbrev,
1220 struct dwarf2_cu *cu);
4bb7a0a7 1221
72bf9492
DJ
1222static void free_stack_comp_unit (void *);
1223
72bf9492
DJ
1224static hashval_t partial_die_hash (const void *item);
1225
1226static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1227
ae038cb0 1228static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1229 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1230
1231static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1232 (unsigned int offset, struct objfile *objfile);
ae038cb0 1233
9816fde3
JK
1234static void init_one_comp_unit (struct dwarf2_cu *cu,
1235 struct objfile *objfile);
1236
1237static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1238 struct die_info *comp_unit_die);
93311388 1239
ae038cb0
DJ
1240static void free_one_comp_unit (void *);
1241
1242static void free_cached_comp_units (void *);
1243
1244static void age_cached_comp_units (void);
1245
1246static void free_one_cached_comp_unit (void *);
1247
f792889a
DJ
1248static struct type *set_die_type (struct die_info *, struct type *,
1249 struct dwarf2_cu *);
1c379e20 1250
ae038cb0
DJ
1251static void create_all_comp_units (struct objfile *);
1252
1fd400ff
TT
1253static int create_debug_types_hash_table (struct objfile *objfile);
1254
93311388
DE
1255static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1256 struct objfile *);
10b3939b
DJ
1257
1258static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1259
1260static void dwarf2_add_dependence (struct dwarf2_cu *,
1261 struct dwarf2_per_cu_data *);
1262
ae038cb0
DJ
1263static void dwarf2_mark (struct dwarf2_cu *);
1264
1265static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1266
673bfd45
DE
1267static struct type *get_die_type_at_offset (unsigned int,
1268 struct dwarf2_per_cu_data *per_cu);
1269
f792889a 1270static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1271
9291a0cd
TT
1272static void dwarf2_release_queue (void *dummy);
1273
1274static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1275 struct objfile *objfile);
1276
1277static void process_queue (struct objfile *objfile);
1278
1279static void find_file_and_directory (struct die_info *die,
1280 struct dwarf2_cu *cu,
1281 char **name, char **comp_dir);
1282
1283static char *file_full_name (int file, struct line_header *lh,
1284 const char *comp_dir);
1285
1286static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1287 gdb_byte *info_ptr,
1288 gdb_byte *buffer,
1289 unsigned int buffer_size,
1290 bfd *abfd);
1291
1292static void init_cu_die_reader (struct die_reader_specs *reader,
1293 struct dwarf2_cu *cu);
1294
673bfd45 1295static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1296
9291a0cd
TT
1297#if WORDS_BIGENDIAN
1298
1299/* Convert VALUE between big- and little-endian. */
1300static offset_type
1301byte_swap (offset_type value)
1302{
1303 offset_type result;
1304
1305 result = (value & 0xff) << 24;
1306 result |= (value & 0xff00) << 8;
1307 result |= (value & 0xff0000) >> 8;
1308 result |= (value & 0xff000000) >> 24;
1309 return result;
1310}
1311
1312#define MAYBE_SWAP(V) byte_swap (V)
1313
1314#else
1315#define MAYBE_SWAP(V) (V)
1316#endif /* WORDS_BIGENDIAN */
1317
1318/* The suffix for an index file. */
1319#define INDEX_SUFFIX ".gdb-index"
1320
3da10d80
KS
1321static const char *dwarf2_physname (char *name, struct die_info *die,
1322 struct dwarf2_cu *cu);
1323
c906108c
SS
1324/* Try to locate the sections we need for DWARF 2 debugging
1325 information and return true if we have enough to do something. */
1326
1327int
6502dd73 1328dwarf2_has_info (struct objfile *objfile)
c906108c 1329{
be391dca
TT
1330 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1331 if (!dwarf2_per_objfile)
1332 {
1333 /* Initialize per-objfile state. */
1334 struct dwarf2_per_objfile *data
1335 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1336
be391dca
TT
1337 memset (data, 0, sizeof (*data));
1338 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1339 dwarf2_per_objfile = data;
6502dd73 1340
be391dca
TT
1341 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1342 dwarf2_per_objfile->objfile = objfile;
1343 }
1344 return (dwarf2_per_objfile->info.asection != NULL
1345 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1346}
1347
233a11ab
CS
1348/* When loading sections, we can either look for ".<name>", or for
1349 * ".z<name>", which indicates a compressed section. */
1350
1351static int
dce234bc 1352section_is_p (const char *section_name, const char *name)
233a11ab 1353{
dce234bc
PP
1354 return (section_name[0] == '.'
1355 && (strcmp (section_name + 1, name) == 0
1356 || (section_name[1] == 'z'
1357 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1358}
1359
c906108c
SS
1360/* This function is mapped across the sections and remembers the
1361 offset and size of each of the debugging sections we are interested
1362 in. */
1363
1364static void
72dca2f5 1365dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1366{
dce234bc 1367 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1368 {
dce234bc
PP
1369 dwarf2_per_objfile->info.asection = sectp;
1370 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1371 }
dce234bc 1372 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1373 {
dce234bc
PP
1374 dwarf2_per_objfile->abbrev.asection = sectp;
1375 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1376 }
dce234bc 1377 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1378 {
dce234bc
PP
1379 dwarf2_per_objfile->line.asection = sectp;
1380 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1381 }
dce234bc 1382 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1383 {
dce234bc
PP
1384 dwarf2_per_objfile->loc.asection = sectp;
1385 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1386 }
dce234bc 1387 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1388 {
dce234bc
PP
1389 dwarf2_per_objfile->macinfo.asection = sectp;
1390 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1391 }
dce234bc 1392 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1393 {
dce234bc
PP
1394 dwarf2_per_objfile->str.asection = sectp;
1395 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1396 }
dce234bc 1397 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1398 {
dce234bc
PP
1399 dwarf2_per_objfile->frame.asection = sectp;
1400 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1401 }
dce234bc 1402 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1403 {
3799ccc6 1404 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1405
3799ccc6
EZ
1406 if (aflag & SEC_HAS_CONTENTS)
1407 {
dce234bc
PP
1408 dwarf2_per_objfile->eh_frame.asection = sectp;
1409 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1410 }
b6af0555 1411 }
dce234bc 1412 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1413 {
dce234bc
PP
1414 dwarf2_per_objfile->ranges.asection = sectp;
1415 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1416 }
348e048f
DE
1417 else if (section_is_p (sectp->name, TYPES_SECTION))
1418 {
1419 dwarf2_per_objfile->types.asection = sectp;
1420 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1421 }
9291a0cd
TT
1422 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1423 {
1424 dwarf2_per_objfile->gdb_index.asection = sectp;
1425 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1426 }
dce234bc 1427
72dca2f5
FR
1428 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1429 && bfd_section_vma (abfd, sectp) == 0)
1430 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1431}
1432
dce234bc
PP
1433/* Decompress a section that was compressed using zlib. Store the
1434 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1435
1436static void
dce234bc
PP
1437zlib_decompress_section (struct objfile *objfile, asection *sectp,
1438 gdb_byte **outbuf, bfd_size_type *outsize)
1439{
1440 bfd *abfd = objfile->obfd;
1441#ifndef HAVE_ZLIB_H
1442 error (_("Support for zlib-compressed DWARF data (from '%s') "
1443 "is disabled in this copy of GDB"),
1444 bfd_get_filename (abfd));
1445#else
1446 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1447 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1448 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1449 bfd_size_type uncompressed_size;
1450 gdb_byte *uncompressed_buffer;
1451 z_stream strm;
1452 int rc;
1453 int header_size = 12;
1454
1455 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1456 || bfd_bread (compressed_buffer,
1457 compressed_size, abfd) != compressed_size)
dce234bc
PP
1458 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1459 bfd_get_filename (abfd));
1460
1461 /* Read the zlib header. In this case, it should be "ZLIB" followed
1462 by the uncompressed section size, 8 bytes in big-endian order. */
1463 if (compressed_size < header_size
1464 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1465 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1466 bfd_get_filename (abfd));
1467 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1468 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1469 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1470 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1471 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1472 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1473 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1474 uncompressed_size += compressed_buffer[11];
1475
1476 /* It is possible the section consists of several compressed
1477 buffers concatenated together, so we uncompress in a loop. */
1478 strm.zalloc = NULL;
1479 strm.zfree = NULL;
1480 strm.opaque = NULL;
1481 strm.avail_in = compressed_size - header_size;
1482 strm.next_in = (Bytef*) compressed_buffer + header_size;
1483 strm.avail_out = uncompressed_size;
1484 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1485 uncompressed_size);
1486 rc = inflateInit (&strm);
1487 while (strm.avail_in > 0)
1488 {
1489 if (rc != Z_OK)
1490 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1491 bfd_get_filename (abfd), rc);
1492 strm.next_out = ((Bytef*) uncompressed_buffer
1493 + (uncompressed_size - strm.avail_out));
1494 rc = inflate (&strm, Z_FINISH);
1495 if (rc != Z_STREAM_END)
1496 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1497 bfd_get_filename (abfd), rc);
1498 rc = inflateReset (&strm);
1499 }
1500 rc = inflateEnd (&strm);
1501 if (rc != Z_OK
1502 || strm.avail_out != 0)
1503 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1504 bfd_get_filename (abfd), rc);
1505
affddf13 1506 do_cleanups (cleanup);
dce234bc
PP
1507 *outbuf = uncompressed_buffer;
1508 *outsize = uncompressed_size;
1509#endif
233a11ab
CS
1510}
1511
9e0ac564
TT
1512/* A helper function that decides whether a section is empty. */
1513
1514static int
1515dwarf2_section_empty_p (struct dwarf2_section_info *info)
1516{
1517 return info->asection == NULL || info->size == 0;
1518}
1519
dce234bc
PP
1520/* Read the contents of the section SECTP from object file specified by
1521 OBJFILE, store info about the section into INFO.
1522 If the section is compressed, uncompress it before returning. */
c906108c 1523
dce234bc
PP
1524static void
1525dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1526{
dce234bc
PP
1527 bfd *abfd = objfile->obfd;
1528 asection *sectp = info->asection;
1529 gdb_byte *buf, *retbuf;
1530 unsigned char header[4];
c906108c 1531
be391dca
TT
1532 if (info->readin)
1533 return;
dce234bc
PP
1534 info->buffer = NULL;
1535 info->was_mmapped = 0;
be391dca 1536 info->readin = 1;
188dd5d6 1537
9e0ac564 1538 if (dwarf2_section_empty_p (info))
dce234bc 1539 return;
c906108c 1540
dce234bc
PP
1541 /* Check if the file has a 4-byte header indicating compression. */
1542 if (info->size > sizeof (header)
1543 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1544 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1545 {
1546 /* Upon decompression, update the buffer and its size. */
1547 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1548 {
1549 zlib_decompress_section (objfile, sectp, &info->buffer,
1550 &info->size);
1551 return;
1552 }
1553 }
4bdf3d34 1554
dce234bc
PP
1555#ifdef HAVE_MMAP
1556 if (pagesize == 0)
1557 pagesize = getpagesize ();
2e276125 1558
dce234bc
PP
1559 /* Only try to mmap sections which are large enough: we don't want to
1560 waste space due to fragmentation. Also, only try mmap for sections
1561 without relocations. */
1562
1563 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1564 {
1565 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1566 size_t map_length = info->size + sectp->filepos - pg_offset;
1567 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1568 MAP_PRIVATE, pg_offset);
1569
1570 if (retbuf != MAP_FAILED)
1571 {
1572 info->was_mmapped = 1;
1573 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1574#if HAVE_POSIX_MADVISE
1575 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1576#endif
dce234bc
PP
1577 return;
1578 }
1579 }
1580#endif
1581
1582 /* If we get here, we are a normal, not-compressed section. */
1583 info->buffer = buf
1584 = obstack_alloc (&objfile->objfile_obstack, info->size);
1585
1586 /* When debugging .o files, we may need to apply relocations; see
1587 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1588 We never compress sections in .o files, so we only need to
1589 try this when the section is not compressed. */
ac8035ab 1590 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1591 if (retbuf != NULL)
1592 {
1593 info->buffer = retbuf;
1594 return;
1595 }
1596
1597 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1598 || bfd_bread (buf, info->size, abfd) != info->size)
1599 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1600 bfd_get_filename (abfd));
1601}
1602
9e0ac564
TT
1603/* A helper function that returns the size of a section in a safe way.
1604 If you are positive that the section has been read before using the
1605 size, then it is safe to refer to the dwarf2_section_info object's
1606 "size" field directly. In other cases, you must call this
1607 function, because for compressed sections the size field is not set
1608 correctly until the section has been read. */
1609
1610static bfd_size_type
1611dwarf2_section_size (struct objfile *objfile,
1612 struct dwarf2_section_info *info)
1613{
1614 if (!info->readin)
1615 dwarf2_read_section (objfile, info);
1616 return info->size;
1617}
1618
dce234bc 1619/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1620 SECTION_NAME. */
af34e669 1621
dce234bc
PP
1622void
1623dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1624 asection **sectp, gdb_byte **bufp,
1625 bfd_size_type *sizep)
1626{
1627 struct dwarf2_per_objfile *data
1628 = objfile_data (objfile, dwarf2_objfile_data_key);
1629 struct dwarf2_section_info *info;
a3b2a86b
TT
1630
1631 /* We may see an objfile without any DWARF, in which case we just
1632 return nothing. */
1633 if (data == NULL)
1634 {
1635 *sectp = NULL;
1636 *bufp = NULL;
1637 *sizep = 0;
1638 return;
1639 }
dce234bc
PP
1640 if (section_is_p (section_name, EH_FRAME_SECTION))
1641 info = &data->eh_frame;
1642 else if (section_is_p (section_name, FRAME_SECTION))
1643 info = &data->frame;
0d53c4c4 1644 else
f3574227 1645 gdb_assert_not_reached ("unexpected section");
dce234bc 1646
9e0ac564 1647 dwarf2_read_section (objfile, info);
dce234bc
PP
1648
1649 *sectp = info->asection;
1650 *bufp = info->buffer;
1651 *sizep = info->size;
1652}
1653
9291a0cd 1654\f
7b9f3c50
DE
1655/* DWARF quick_symbols_functions support. */
1656
1657/* TUs can share .debug_line entries, and there can be a lot more TUs than
1658 unique line tables, so we maintain a separate table of all .debug_line
1659 derived entries to support the sharing.
1660 All the quick functions need is the list of file names. We discard the
1661 line_header when we're done and don't need to record it here. */
1662struct quick_file_names
1663{
1664 /* The offset in .debug_line of the line table. We hash on this. */
1665 unsigned int offset;
1666
1667 /* The number of entries in file_names, real_names. */
1668 unsigned int num_file_names;
1669
1670 /* The file names from the line table, after being run through
1671 file_full_name. */
1672 const char **file_names;
1673
1674 /* The file names from the line table after being run through
1675 gdb_realpath. These are computed lazily. */
1676 const char **real_names;
1677};
1678
1679/* When using the index (and thus not using psymtabs), each CU has an
1680 object of this type. This is used to hold information needed by
1681 the various "quick" methods. */
1682struct dwarf2_per_cu_quick_data
1683{
1684 /* The file table. This can be NULL if there was no file table
1685 or it's currently not read in.
1686 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1687 struct quick_file_names *file_names;
1688
1689 /* The corresponding symbol table. This is NULL if symbols for this
1690 CU have not yet been read. */
1691 struct symtab *symtab;
1692
1693 /* A temporary mark bit used when iterating over all CUs in
1694 expand_symtabs_matching. */
1695 unsigned int mark : 1;
1696
1697 /* True if we've tried to read the file table and found there isn't one.
1698 There will be no point in trying to read it again next time. */
1699 unsigned int no_file_data : 1;
1700};
1701
1702/* Hash function for a quick_file_names. */
1703
1704static hashval_t
1705hash_file_name_entry (const void *e)
1706{
1707 const struct quick_file_names *file_data = e;
1708
1709 return file_data->offset;
1710}
1711
1712/* Equality function for a quick_file_names. */
1713
1714static int
1715eq_file_name_entry (const void *a, const void *b)
1716{
1717 const struct quick_file_names *ea = a;
1718 const struct quick_file_names *eb = b;
1719
1720 return ea->offset == eb->offset;
1721}
1722
1723/* Delete function for a quick_file_names. */
1724
1725static void
1726delete_file_name_entry (void *e)
1727{
1728 struct quick_file_names *file_data = e;
1729 int i;
1730
1731 for (i = 0; i < file_data->num_file_names; ++i)
1732 {
1733 xfree ((void*) file_data->file_names[i]);
1734 if (file_data->real_names)
1735 xfree ((void*) file_data->real_names[i]);
1736 }
1737
1738 /* The space for the struct itself lives on objfile_obstack,
1739 so we don't free it here. */
1740}
1741
1742/* Create a quick_file_names hash table. */
1743
1744static htab_t
1745create_quick_file_names_table (unsigned int nr_initial_entries)
1746{
1747 return htab_create_alloc (nr_initial_entries,
1748 hash_file_name_entry, eq_file_name_entry,
1749 delete_file_name_entry, xcalloc, xfree);
1750}
9291a0cd
TT
1751
1752/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1753 this CU came. */
2fdf6df6 1754
9291a0cd
TT
1755static void
1756dw2_do_instantiate_symtab (struct objfile *objfile,
1757 struct dwarf2_per_cu_data *per_cu)
1758{
1759 struct cleanup *back_to;
1760
1761 back_to = make_cleanup (dwarf2_release_queue, NULL);
1762
1763 queue_comp_unit (per_cu, objfile);
1764
1765 if (per_cu->from_debug_types)
1766 read_signatured_type_at_offset (objfile, per_cu->offset);
1767 else
1768 load_full_comp_unit (per_cu, objfile);
1769
1770 process_queue (objfile);
1771
1772 /* Age the cache, releasing compilation units that have not
1773 been used recently. */
1774 age_cached_comp_units ();
1775
1776 do_cleanups (back_to);
1777}
1778
1779/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1780 the objfile from which this CU came. Returns the resulting symbol
1781 table. */
2fdf6df6 1782
9291a0cd
TT
1783static struct symtab *
1784dw2_instantiate_symtab (struct objfile *objfile,
1785 struct dwarf2_per_cu_data *per_cu)
1786{
1787 if (!per_cu->v.quick->symtab)
1788 {
1789 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1790 increment_reading_symtab ();
1791 dw2_do_instantiate_symtab (objfile, per_cu);
1792 do_cleanups (back_to);
1793 }
1794 return per_cu->v.quick->symtab;
1795}
1796
1fd400ff 1797/* Return the CU given its index. */
2fdf6df6 1798
1fd400ff
TT
1799static struct dwarf2_per_cu_data *
1800dw2_get_cu (int index)
1801{
1802 if (index >= dwarf2_per_objfile->n_comp_units)
1803 {
1804 index -= dwarf2_per_objfile->n_comp_units;
1805 return dwarf2_per_objfile->type_comp_units[index];
1806 }
1807 return dwarf2_per_objfile->all_comp_units[index];
1808}
1809
9291a0cd
TT
1810/* A helper function that knows how to read a 64-bit value in a way
1811 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1812 otherwise. */
2fdf6df6 1813
9291a0cd
TT
1814static int
1815extract_cu_value (const char *bytes, ULONGEST *result)
1816{
1817 if (sizeof (ULONGEST) < 8)
1818 {
1819 int i;
1820
1821 /* Ignore the upper 4 bytes if they are all zero. */
1822 for (i = 0; i < 4; ++i)
1823 if (bytes[i + 4] != 0)
1824 return 0;
1825
1826 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1827 }
1828 else
1829 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1830 return 1;
1831}
1832
1833/* Read the CU list from the mapped index, and use it to create all
1834 the CU objects for this objfile. Return 0 if something went wrong,
1835 1 if everything went ok. */
2fdf6df6 1836
9291a0cd 1837static int
1fd400ff
TT
1838create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1839 offset_type cu_list_elements)
9291a0cd
TT
1840{
1841 offset_type i;
9291a0cd
TT
1842
1843 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1844 dwarf2_per_objfile->all_comp_units
1845 = obstack_alloc (&objfile->objfile_obstack,
1846 dwarf2_per_objfile->n_comp_units
1847 * sizeof (struct dwarf2_per_cu_data *));
1848
1849 for (i = 0; i < cu_list_elements; i += 2)
1850 {
1851 struct dwarf2_per_cu_data *the_cu;
1852 ULONGEST offset, length;
1853
1854 if (!extract_cu_value (cu_list, &offset)
1855 || !extract_cu_value (cu_list + 8, &length))
1856 return 0;
1857 cu_list += 2 * 8;
1858
1859 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1860 struct dwarf2_per_cu_data);
1861 the_cu->offset = offset;
1862 the_cu->length = length;
1863 the_cu->objfile = objfile;
1864 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1865 struct dwarf2_per_cu_quick_data);
1866 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1867 }
1868
1869 return 1;
1870}
1871
1fd400ff 1872/* Create the signatured type hash table from the index. */
673bfd45 1873
1fd400ff 1874static int
673bfd45
DE
1875create_signatured_type_table_from_index (struct objfile *objfile,
1876 const gdb_byte *bytes,
1877 offset_type elements)
1fd400ff
TT
1878{
1879 offset_type i;
673bfd45 1880 htab_t sig_types_hash;
1fd400ff
TT
1881
1882 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1883 dwarf2_per_objfile->type_comp_units
1884 = obstack_alloc (&objfile->objfile_obstack,
1885 dwarf2_per_objfile->n_type_comp_units
1886 * sizeof (struct dwarf2_per_cu_data *));
1887
673bfd45 1888 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1889
1890 for (i = 0; i < elements; i += 3)
1891 {
1892 struct signatured_type *type_sig;
1893 ULONGEST offset, type_offset, signature;
1894 void **slot;
1895
1896 if (!extract_cu_value (bytes, &offset)
1897 || !extract_cu_value (bytes + 8, &type_offset))
1898 return 0;
1899 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1900 bytes += 3 * 8;
1901
1902 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1903 struct signatured_type);
1904 type_sig->signature = signature;
1905 type_sig->offset = offset;
1906 type_sig->type_offset = type_offset;
1907 type_sig->per_cu.from_debug_types = 1;
1908 type_sig->per_cu.offset = offset;
1909 type_sig->per_cu.objfile = objfile;
1910 type_sig->per_cu.v.quick
1911 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1912 struct dwarf2_per_cu_quick_data);
1913
673bfd45 1914 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1915 *slot = type_sig;
1916
1917 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1918 }
1919
673bfd45 1920 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1921
1922 return 1;
1923}
1924
9291a0cd
TT
1925/* Read the address map data from the mapped index, and use it to
1926 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1927
9291a0cd
TT
1928static void
1929create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1930{
1931 const gdb_byte *iter, *end;
1932 struct obstack temp_obstack;
1933 struct addrmap *mutable_map;
1934 struct cleanup *cleanup;
1935 CORE_ADDR baseaddr;
1936
1937 obstack_init (&temp_obstack);
1938 cleanup = make_cleanup_obstack_free (&temp_obstack);
1939 mutable_map = addrmap_create_mutable (&temp_obstack);
1940
1941 iter = index->address_table;
1942 end = iter + index->address_table_size;
1943
1944 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1945
1946 while (iter < end)
1947 {
1948 ULONGEST hi, lo, cu_index;
1949 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1950 iter += 8;
1951 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1952 iter += 8;
1953 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1954 iter += 4;
1955
1956 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1957 dw2_get_cu (cu_index));
9291a0cd
TT
1958 }
1959
1960 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1961 &objfile->objfile_obstack);
1962 do_cleanups (cleanup);
1963}
1964
1965/* The hash function for strings in the mapped index. This is the
1966 same as the hashtab.c hash function, but we keep a separate copy to
1967 maintain control over the implementation. This is necessary
1968 because the hash function is tied to the format of the mapped index
1969 file. */
2fdf6df6 1970
9291a0cd
TT
1971static hashval_t
1972mapped_index_string_hash (const void *p)
1973{
1974 const unsigned char *str = (const unsigned char *) p;
1975 hashval_t r = 0;
1976 unsigned char c;
1977
1978 while ((c = *str++) != 0)
1979 r = r * 67 + c - 113;
1980
1981 return r;
1982}
1983
1984/* Find a slot in the mapped index INDEX for the object named NAME.
1985 If NAME is found, set *VEC_OUT to point to the CU vector in the
1986 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 1987
9291a0cd
TT
1988static int
1989find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1990 offset_type **vec_out)
1991{
1992 offset_type hash = mapped_index_string_hash (name);
1993 offset_type slot, step;
1994
3876f04e
DE
1995 slot = hash & (index->symbol_table_slots - 1);
1996 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
9291a0cd
TT
1997
1998 for (;;)
1999 {
2000 /* Convert a slot number to an offset into the table. */
2001 offset_type i = 2 * slot;
2002 const char *str;
3876f04e 2003 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
9291a0cd
TT
2004 return 0;
2005
3876f04e 2006 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
9291a0cd
TT
2007 if (!strcmp (name, str))
2008 {
2009 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2010 + MAYBE_SWAP (index->symbol_table[i + 1]));
9291a0cd
TT
2011 return 1;
2012 }
2013
3876f04e 2014 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2015 }
2016}
2017
2018/* Read the index file. If everything went ok, initialize the "quick"
2019 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2020
9291a0cd
TT
2021static int
2022dwarf2_read_index (struct objfile *objfile)
2023{
9291a0cd
TT
2024 char *addr;
2025 struct mapped_index *map;
b3b272e1 2026 offset_type *metadata;
ac0b195c
KW
2027 const gdb_byte *cu_list;
2028 const gdb_byte *types_list = NULL;
2029 offset_type version, cu_list_elements;
2030 offset_type types_list_elements = 0;
1fd400ff 2031 int i;
9291a0cd 2032
9e0ac564 2033 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2034 return 0;
82430852
JK
2035
2036 /* Older elfutils strip versions could keep the section in the main
2037 executable while splitting it for the separate debug info file. */
2038 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2039 & SEC_HAS_CONTENTS) == 0)
2040 return 0;
2041
9291a0cd
TT
2042 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2043
2044 addr = dwarf2_per_objfile->gdb_index.buffer;
2045 /* Version check. */
1fd400ff 2046 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2047 /* Versions earlier than 3 emitted every copy of a psymbol. This
831adc1f
JK
2048 causes the index to behave very poorly for certain requests. Version 4
2049 contained incomplete addrmap. So, it seems better to just ignore such
2050 indices. */
2051 if (version < 4)
9291a0cd 2052 return 0;
594e8718
JK
2053 /* Indexes with higher version than the one supported by GDB may be no
2054 longer backward compatible. */
831adc1f 2055 if (version > 4)
594e8718 2056 return 0;
9291a0cd
TT
2057
2058 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 2059 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2060
2061 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2062
2063 i = 0;
2064 cu_list = addr + MAYBE_SWAP (metadata[i]);
2065 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2066 / 8);
1fd400ff
TT
2067 ++i;
2068
987d643c
TT
2069 types_list = addr + MAYBE_SWAP (metadata[i]);
2070 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2071 - MAYBE_SWAP (metadata[i]))
2072 / 8);
2073 ++i;
1fd400ff
TT
2074
2075 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2076 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2077 - MAYBE_SWAP (metadata[i]));
2078 ++i;
2079
3876f04e
DE
2080 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2081 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2082 - MAYBE_SWAP (metadata[i]))
2083 / (2 * sizeof (offset_type)));
1fd400ff 2084 ++i;
9291a0cd 2085
1fd400ff
TT
2086 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2087
2088 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2089 return 0;
2090
987d643c 2091 if (types_list_elements
673bfd45
DE
2092 && !create_signatured_type_table_from_index (objfile, types_list,
2093 types_list_elements))
9291a0cd
TT
2094 return 0;
2095
2096 create_addrmap_from_index (objfile, map);
2097
2098 dwarf2_per_objfile->index_table = map;
2099 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2100 dwarf2_per_objfile->quick_file_names_table =
2101 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2102
2103 return 1;
2104}
2105
2106/* A helper for the "quick" functions which sets the global
2107 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2108
9291a0cd
TT
2109static void
2110dw2_setup (struct objfile *objfile)
2111{
2112 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2113 gdb_assert (dwarf2_per_objfile);
2114}
2115
2116/* A helper for the "quick" functions which attempts to read the line
2117 table for THIS_CU. */
2fdf6df6 2118
7b9f3c50
DE
2119static struct quick_file_names *
2120dw2_get_file_names (struct objfile *objfile,
2121 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2122{
2123 bfd *abfd = objfile->obfd;
7b9f3c50 2124 struct line_header *lh;
9291a0cd
TT
2125 struct attribute *attr;
2126 struct cleanup *cleanups;
2127 struct die_info *comp_unit_die;
36374493 2128 struct dwarf2_section_info* sec;
9291a0cd
TT
2129 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2130 int has_children, i;
2131 struct dwarf2_cu cu;
2132 unsigned int bytes_read, buffer_size;
2133 struct die_reader_specs reader_specs;
2134 char *name, *comp_dir;
7b9f3c50
DE
2135 void **slot;
2136 struct quick_file_names *qfn;
2137 unsigned int line_offset;
9291a0cd 2138
7b9f3c50
DE
2139 if (this_cu->v.quick->file_names != NULL)
2140 return this_cu->v.quick->file_names;
2141 /* If we know there is no line data, no point in looking again. */
2142 if (this_cu->v.quick->no_file_data)
2143 return NULL;
9291a0cd 2144
9816fde3 2145 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2146 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2147
36374493
DE
2148 if (this_cu->from_debug_types)
2149 sec = &dwarf2_per_objfile->types;
2150 else
2151 sec = &dwarf2_per_objfile->info;
2152 dwarf2_read_section (objfile, sec);
2153 buffer_size = sec->size;
2154 buffer = sec->buffer;
9291a0cd
TT
2155 info_ptr = buffer + this_cu->offset;
2156 beg_of_comp_unit = info_ptr;
2157
2158 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2159 buffer, buffer_size,
2160 abfd);
2161
2162 /* Complete the cu_header. */
2163 cu.header.offset = beg_of_comp_unit - buffer;
2164 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2165
2166 this_cu->cu = &cu;
2167 cu.per_cu = this_cu;
2168
2169 dwarf2_read_abbrevs (abfd, &cu);
2170 make_cleanup (dwarf2_free_abbrev_table, &cu);
2171
2172 if (this_cu->from_debug_types)
2173 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2174 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2175 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2176 &has_children);
9291a0cd 2177
7b9f3c50
DE
2178 lh = NULL;
2179 slot = NULL;
2180 line_offset = 0;
9291a0cd
TT
2181 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2182 if (attr)
2183 {
7b9f3c50
DE
2184 struct quick_file_names find_entry;
2185
2186 line_offset = DW_UNSND (attr);
2187
2188 /* We may have already read in this line header (TU line header sharing).
2189 If we have we're done. */
2190 find_entry.offset = line_offset;
2191 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2192 &find_entry, INSERT);
2193 if (*slot != NULL)
2194 {
2195 do_cleanups (cleanups);
2196 this_cu->v.quick->file_names = *slot;
2197 return *slot;
2198 }
2199
9291a0cd
TT
2200 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2201 }
2202 if (lh == NULL)
2203 {
2204 do_cleanups (cleanups);
7b9f3c50
DE
2205 this_cu->v.quick->no_file_data = 1;
2206 return NULL;
9291a0cd
TT
2207 }
2208
7b9f3c50
DE
2209 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2210 qfn->offset = line_offset;
2211 gdb_assert (slot != NULL);
2212 *slot = qfn;
9291a0cd 2213
7b9f3c50 2214 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2215
7b9f3c50
DE
2216 qfn->num_file_names = lh->num_file_names;
2217 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2218 lh->num_file_names * sizeof (char *));
9291a0cd 2219 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2220 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2221 qfn->real_names = NULL;
9291a0cd 2222
7b9f3c50 2223 free_line_header (lh);
9291a0cd 2224 do_cleanups (cleanups);
7b9f3c50
DE
2225
2226 this_cu->v.quick->file_names = qfn;
2227 return qfn;
9291a0cd
TT
2228}
2229
2230/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2231 real path for a given file name from the line table. */
2fdf6df6 2232
9291a0cd 2233static const char *
7b9f3c50
DE
2234dw2_get_real_path (struct objfile *objfile,
2235 struct quick_file_names *qfn, int index)
9291a0cd 2236{
7b9f3c50
DE
2237 if (qfn->real_names == NULL)
2238 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2239 qfn->num_file_names, sizeof (char *));
9291a0cd 2240
7b9f3c50
DE
2241 if (qfn->real_names[index] == NULL)
2242 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2243
7b9f3c50 2244 return qfn->real_names[index];
9291a0cd
TT
2245}
2246
2247static struct symtab *
2248dw2_find_last_source_symtab (struct objfile *objfile)
2249{
2250 int index;
ae2de4f8 2251
9291a0cd
TT
2252 dw2_setup (objfile);
2253 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2254 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2255}
2256
7b9f3c50
DE
2257/* Traversal function for dw2_forget_cached_source_info. */
2258
2259static int
2260dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2261{
7b9f3c50 2262 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2263
7b9f3c50 2264 if (file_data->real_names)
9291a0cd 2265 {
7b9f3c50 2266 int i;
9291a0cd 2267
7b9f3c50 2268 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2269 {
7b9f3c50
DE
2270 xfree ((void*) file_data->real_names[i]);
2271 file_data->real_names[i] = NULL;
9291a0cd
TT
2272 }
2273 }
7b9f3c50
DE
2274
2275 return 1;
2276}
2277
2278static void
2279dw2_forget_cached_source_info (struct objfile *objfile)
2280{
2281 dw2_setup (objfile);
2282
2283 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2284 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2285}
2286
2287static int
2288dw2_lookup_symtab (struct objfile *objfile, const char *name,
2289 const char *full_path, const char *real_path,
2290 struct symtab **result)
2291{
2292 int i;
2293 int check_basename = lbasename (name) == name;
2294 struct dwarf2_per_cu_data *base_cu = NULL;
2295
2296 dw2_setup (objfile);
ae2de4f8 2297
1fd400ff
TT
2298 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2299 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2300 {
2301 int j;
e254ef6a 2302 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2303 struct quick_file_names *file_data;
9291a0cd 2304
e254ef6a 2305 if (per_cu->v.quick->symtab)
9291a0cd
TT
2306 continue;
2307
7b9f3c50
DE
2308 file_data = dw2_get_file_names (objfile, per_cu);
2309 if (file_data == NULL)
9291a0cd
TT
2310 continue;
2311
7b9f3c50 2312 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2313 {
7b9f3c50 2314 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2315
2316 if (FILENAME_CMP (name, this_name) == 0)
2317 {
e254ef6a 2318 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2319 return 1;
2320 }
2321
2322 if (check_basename && ! base_cu
2323 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2324 base_cu = per_cu;
9291a0cd
TT
2325
2326 if (full_path != NULL)
2327 {
7b9f3c50
DE
2328 const char *this_real_name = dw2_get_real_path (objfile,
2329 file_data, j);
9291a0cd 2330
7b9f3c50
DE
2331 if (this_real_name != NULL
2332 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2333 {
e254ef6a 2334 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2335 return 1;
2336 }
2337 }
2338
2339 if (real_path != NULL)
2340 {
7b9f3c50
DE
2341 const char *this_real_name = dw2_get_real_path (objfile,
2342 file_data, j);
9291a0cd 2343
7b9f3c50
DE
2344 if (this_real_name != NULL
2345 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2346 {
74dd2ca6
DE
2347 *result = dw2_instantiate_symtab (objfile, per_cu);
2348 return 1;
9291a0cd
TT
2349 }
2350 }
2351 }
2352 }
2353
2354 if (base_cu)
2355 {
2356 *result = dw2_instantiate_symtab (objfile, base_cu);
2357 return 1;
2358 }
2359
2360 return 0;
2361}
2362
2363static struct symtab *
2364dw2_lookup_symbol (struct objfile *objfile, int block_index,
2365 const char *name, domain_enum domain)
2366{
774b6a14 2367 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2368 instead. */
2369 return NULL;
2370}
2371
2372/* A helper function that expands all symtabs that hold an object
2373 named NAME. */
2fdf6df6 2374
9291a0cd
TT
2375static void
2376dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2377{
2378 dw2_setup (objfile);
2379
ae2de4f8 2380 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2381 if (dwarf2_per_objfile->index_table)
2382 {
2383 offset_type *vec;
2384
2385 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2386 name, &vec))
2387 {
2388 offset_type i, len = MAYBE_SWAP (*vec);
2389 for (i = 0; i < len; ++i)
2390 {
2391 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2392 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2393
e254ef6a 2394 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2395 }
2396 }
2397 }
2398}
2399
774b6a14
TT
2400static void
2401dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2402 int kind, const char *name,
2403 domain_enum domain)
9291a0cd 2404{
774b6a14 2405 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2406}
2407
2408static void
2409dw2_print_stats (struct objfile *objfile)
2410{
2411 int i, count;
2412
2413 dw2_setup (objfile);
2414 count = 0;
1fd400ff
TT
2415 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2416 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2417 {
e254ef6a 2418 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2419
e254ef6a 2420 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2421 ++count;
2422 }
2423 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2424}
2425
2426static void
2427dw2_dump (struct objfile *objfile)
2428{
2429 /* Nothing worth printing. */
2430}
2431
2432static void
2433dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2434 struct section_offsets *delta)
2435{
2436 /* There's nothing to relocate here. */
2437}
2438
2439static void
2440dw2_expand_symtabs_for_function (struct objfile *objfile,
2441 const char *func_name)
2442{
2443 dw2_do_expand_symtabs_matching (objfile, func_name);
2444}
2445
2446static void
2447dw2_expand_all_symtabs (struct objfile *objfile)
2448{
2449 int i;
2450
2451 dw2_setup (objfile);
1fd400ff
TT
2452
2453 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2454 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2455 {
e254ef6a 2456 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2457
e254ef6a 2458 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2459 }
2460}
2461
2462static void
2463dw2_expand_symtabs_with_filename (struct objfile *objfile,
2464 const char *filename)
2465{
2466 int i;
2467
2468 dw2_setup (objfile);
d4637a04
DE
2469
2470 /* We don't need to consider type units here.
2471 This is only called for examining code, e.g. expand_line_sal.
2472 There can be an order of magnitude (or more) more type units
2473 than comp units, and we avoid them if we can. */
2474
2475 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2476 {
2477 int j;
e254ef6a 2478 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2479 struct quick_file_names *file_data;
9291a0cd 2480
e254ef6a 2481 if (per_cu->v.quick->symtab)
9291a0cd
TT
2482 continue;
2483
7b9f3c50
DE
2484 file_data = dw2_get_file_names (objfile, per_cu);
2485 if (file_data == NULL)
9291a0cd
TT
2486 continue;
2487
7b9f3c50 2488 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2489 {
7b9f3c50 2490 const char *this_name = file_data->file_names[j];
1ef75ecc 2491 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2492 {
e254ef6a 2493 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2494 break;
2495 }
2496 }
2497 }
2498}
2499
dd786858 2500static const char *
9291a0cd
TT
2501dw2_find_symbol_file (struct objfile *objfile, const char *name)
2502{
e254ef6a 2503 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2504 offset_type *vec;
7b9f3c50 2505 struct quick_file_names *file_data;
9291a0cd
TT
2506
2507 dw2_setup (objfile);
2508
ae2de4f8 2509 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2510 if (!dwarf2_per_objfile->index_table)
2511 return NULL;
2512
2513 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2514 name, &vec))
2515 return NULL;
2516
2517 /* Note that this just looks at the very first one named NAME -- but
2518 actually we are looking for a function. find_main_filename
2519 should be rewritten so that it doesn't require a custom hook. It
2520 could just use the ordinary symbol tables. */
2521 /* vec[0] is the length, which must always be >0. */
e254ef6a 2522 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2523
7b9f3c50
DE
2524 file_data = dw2_get_file_names (objfile, per_cu);
2525 if (file_data == NULL)
9291a0cd
TT
2526 return NULL;
2527
7b9f3c50 2528 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2529}
2530
2531static void
40658b94
PH
2532dw2_map_matching_symbols (const char * name, domain_enum namespace,
2533 struct objfile *objfile, int global,
2534 int (*callback) (struct block *,
2535 struct symbol *, void *),
2edb89d3
JK
2536 void *data, symbol_compare_ftype *match,
2537 symbol_compare_ftype *ordered_compare)
9291a0cd 2538{
40658b94 2539 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2540 current language is Ada for a non-Ada objfile using GNU index. As Ada
2541 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2542}
2543
2544static void
2545dw2_expand_symtabs_matching (struct objfile *objfile,
2546 int (*file_matcher) (const char *, void *),
2547 int (*name_matcher) (const char *, void *),
2548 domain_enum kind,
2549 void *data)
2550{
2551 int i;
2552 offset_type iter;
4b5246aa 2553 struct mapped_index *index;
9291a0cd
TT
2554
2555 dw2_setup (objfile);
ae2de4f8
DE
2556
2557 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2558 if (!dwarf2_per_objfile->index_table)
2559 return;
4b5246aa 2560 index = dwarf2_per_objfile->index_table;
9291a0cd 2561
1fd400ff
TT
2562 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2563 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2564 {
2565 int j;
e254ef6a 2566 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2567 struct quick_file_names *file_data;
9291a0cd 2568
e254ef6a
DE
2569 per_cu->v.quick->mark = 0;
2570 if (per_cu->v.quick->symtab)
9291a0cd
TT
2571 continue;
2572
7b9f3c50
DE
2573 file_data = dw2_get_file_names (objfile, per_cu);
2574 if (file_data == NULL)
9291a0cd
TT
2575 continue;
2576
7b9f3c50 2577 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2578 {
7b9f3c50 2579 if (file_matcher (file_data->file_names[j], data))
9291a0cd 2580 {
e254ef6a 2581 per_cu->v.quick->mark = 1;
9291a0cd
TT
2582 break;
2583 }
2584 }
2585 }
2586
3876f04e 2587 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2588 {
2589 offset_type idx = 2 * iter;
2590 const char *name;
2591 offset_type *vec, vec_len, vec_idx;
2592
3876f04e 2593 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2594 continue;
2595
3876f04e 2596 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2597
2598 if (! (*name_matcher) (name, data))
2599 continue;
2600
2601 /* The name was matched, now expand corresponding CUs that were
2602 marked. */
4b5246aa 2603 vec = (offset_type *) (index->constant_pool
3876f04e 2604 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2605 vec_len = MAYBE_SWAP (vec[0]);
2606 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2607 {
e254ef6a 2608 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2609
e254ef6a
DE
2610 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2611 if (per_cu->v.quick->mark)
2612 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2613 }
2614 }
2615}
2616
2617static struct symtab *
2618dw2_find_pc_sect_symtab (struct objfile *objfile,
2619 struct minimal_symbol *msymbol,
2620 CORE_ADDR pc,
2621 struct obj_section *section,
2622 int warn_if_readin)
2623{
2624 struct dwarf2_per_cu_data *data;
2625
2626 dw2_setup (objfile);
2627
2628 if (!objfile->psymtabs_addrmap)
2629 return NULL;
2630
2631 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2632 if (!data)
2633 return NULL;
2634
2635 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2636 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2637 paddress (get_objfile_arch (objfile), pc));
2638
2639 return dw2_instantiate_symtab (objfile, data);
2640}
2641
2642static void
2643dw2_map_symbol_names (struct objfile *objfile,
2644 void (*fun) (const char *, void *),
2645 void *data)
2646{
2647 offset_type iter;
4b5246aa
TT
2648 struct mapped_index *index;
2649
9291a0cd
TT
2650 dw2_setup (objfile);
2651
ae2de4f8 2652 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2653 if (!dwarf2_per_objfile->index_table)
2654 return;
4b5246aa 2655 index = dwarf2_per_objfile->index_table;
9291a0cd 2656
3876f04e 2657 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2658 {
2659 offset_type idx = 2 * iter;
2660 const char *name;
2661 offset_type *vec, vec_len, vec_idx;
2662
3876f04e 2663 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2664 continue;
2665
3876f04e 2666 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
9291a0cd
TT
2667
2668 (*fun) (name, data);
2669 }
2670}
2671
2672static void
2673dw2_map_symbol_filenames (struct objfile *objfile,
2674 void (*fun) (const char *, const char *, void *),
2675 void *data)
2676{
2677 int i;
2678
2679 dw2_setup (objfile);
ae2de4f8 2680
1fd400ff
TT
2681 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2682 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2683 {
2684 int j;
e254ef6a 2685 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2686 struct quick_file_names *file_data;
9291a0cd 2687
e254ef6a 2688 if (per_cu->v.quick->symtab)
9291a0cd
TT
2689 continue;
2690
7b9f3c50
DE
2691 file_data = dw2_get_file_names (objfile, per_cu);
2692 if (file_data == NULL)
9291a0cd
TT
2693 continue;
2694
7b9f3c50 2695 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2696 {
7b9f3c50
DE
2697 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2698 j);
2699 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2700 }
2701 }
2702}
2703
2704static int
2705dw2_has_symbols (struct objfile *objfile)
2706{
2707 return 1;
2708}
2709
2710const struct quick_symbol_functions dwarf2_gdb_index_functions =
2711{
2712 dw2_has_symbols,
2713 dw2_find_last_source_symtab,
2714 dw2_forget_cached_source_info,
2715 dw2_lookup_symtab,
2716 dw2_lookup_symbol,
774b6a14 2717 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2718 dw2_print_stats,
2719 dw2_dump,
2720 dw2_relocate,
2721 dw2_expand_symtabs_for_function,
2722 dw2_expand_all_symtabs,
2723 dw2_expand_symtabs_with_filename,
2724 dw2_find_symbol_file,
40658b94 2725 dw2_map_matching_symbols,
9291a0cd
TT
2726 dw2_expand_symtabs_matching,
2727 dw2_find_pc_sect_symtab,
2728 dw2_map_symbol_names,
2729 dw2_map_symbol_filenames
2730};
2731
2732/* Initialize for reading DWARF for this objfile. Return 0 if this
2733 file will use psymtabs, or 1 if using the GNU index. */
2734
2735int
2736dwarf2_initialize_objfile (struct objfile *objfile)
2737{
2738 /* If we're about to read full symbols, don't bother with the
2739 indices. In this case we also don't care if some other debug
2740 format is making psymtabs, because they are all about to be
2741 expanded anyway. */
2742 if ((objfile->flags & OBJF_READNOW))
2743 {
2744 int i;
2745
2746 dwarf2_per_objfile->using_index = 1;
2747 create_all_comp_units (objfile);
1fd400ff 2748 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2749 dwarf2_per_objfile->quick_file_names_table =
2750 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2751
1fd400ff
TT
2752 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2753 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2754 {
e254ef6a 2755 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2756
e254ef6a
DE
2757 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2758 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2759 }
2760
2761 /* Return 1 so that gdb sees the "quick" functions. However,
2762 these functions will be no-ops because we will have expanded
2763 all symtabs. */
2764 return 1;
2765 }
2766
2767 if (dwarf2_read_index (objfile))
2768 return 1;
2769
9291a0cd
TT
2770 return 0;
2771}
2772
2773\f
2774
dce234bc
PP
2775/* Build a partial symbol table. */
2776
2777void
f29dff0a 2778dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2779{
f29dff0a 2780 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2781 {
2782 init_psymbol_list (objfile, 1024);
2783 }
2784
d146bf1e 2785 dwarf2_build_psymtabs_hard (objfile);
c906108c 2786}
c906108c 2787
45452591
DE
2788/* Return TRUE if OFFSET is within CU_HEADER. */
2789
2790static inline int
2791offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2792{
2793 unsigned int bottom = cu_header->offset;
2794 unsigned int top = (cu_header->offset
2795 + cu_header->length
2796 + cu_header->initial_length_size);
9a619af0 2797
45452591
DE
2798 return (offset >= bottom && offset < top);
2799}
2800
93311388
DE
2801/* Read in the comp unit header information from the debug_info at info_ptr.
2802 NOTE: This leaves members offset, first_die_offset to be filled in
2803 by the caller. */
107d2387 2804
fe1b8b76 2805static gdb_byte *
107d2387 2806read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2807 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2808{
2809 int signed_addr;
891d2f0b 2810 unsigned int bytes_read;
c764a876
DE
2811
2812 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2813 cu_header->initial_length_size = bytes_read;
2814 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2815 info_ptr += bytes_read;
107d2387
AC
2816 cu_header->version = read_2_bytes (abfd, info_ptr);
2817 info_ptr += 2;
613e1657 2818 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2819 &bytes_read);
613e1657 2820 info_ptr += bytes_read;
107d2387
AC
2821 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2822 info_ptr += 1;
2823 signed_addr = bfd_get_sign_extend_vma (abfd);
2824 if (signed_addr < 0)
8e65ff28 2825 internal_error (__FILE__, __LINE__,
e2e0b3e5 2826 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2827 cu_header->signed_addr_p = signed_addr;
c764a876 2828
107d2387
AC
2829 return info_ptr;
2830}
2831
fe1b8b76
JB
2832static gdb_byte *
2833partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2834 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2835 bfd *abfd)
2836{
fe1b8b76 2837 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2838
2839 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2840
2dc7f7b3 2841 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2842 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2843 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2844 bfd_get_filename (abfd));
72bf9492 2845
9e0ac564
TT
2846 if (header->abbrev_offset
2847 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2848 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2849 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2850 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2851 (long) header->abbrev_offset,
93311388 2852 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2853 bfd_get_filename (abfd));
2854
2855 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2856 > buffer + buffer_size)
8a3fe4f8
AC
2857 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2858 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2859 (long) header->length,
93311388 2860 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2861 bfd_get_filename (abfd));
2862
2863 return info_ptr;
2864}
2865
348e048f
DE
2866/* Read in the types comp unit header information from .debug_types entry at
2867 types_ptr. The result is a pointer to one past the end of the header. */
2868
2869static gdb_byte *
2870read_type_comp_unit_head (struct comp_unit_head *cu_header,
2871 ULONGEST *signature,
2872 gdb_byte *types_ptr, bfd *abfd)
2873{
348e048f
DE
2874 gdb_byte *initial_types_ptr = types_ptr;
2875
6e70227d 2876 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2877 &dwarf2_per_objfile->types);
348e048f
DE
2878 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2879
2880 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2881
2882 *signature = read_8_bytes (abfd, types_ptr);
2883 types_ptr += 8;
2884 types_ptr += cu_header->offset_size;
2885 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2886
2887 return types_ptr;
2888}
2889
aaa75496
JB
2890/* Allocate a new partial symtab for file named NAME and mark this new
2891 partial symtab as being an include of PST. */
2892
2893static void
2894dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2895 struct objfile *objfile)
2896{
2897 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2898
2899 subpst->section_offsets = pst->section_offsets;
2900 subpst->textlow = 0;
2901 subpst->texthigh = 0;
2902
2903 subpst->dependencies = (struct partial_symtab **)
2904 obstack_alloc (&objfile->objfile_obstack,
2905 sizeof (struct partial_symtab *));
2906 subpst->dependencies[0] = pst;
2907 subpst->number_of_dependencies = 1;
2908
2909 subpst->globals_offset = 0;
2910 subpst->n_global_syms = 0;
2911 subpst->statics_offset = 0;
2912 subpst->n_static_syms = 0;
2913 subpst->symtab = NULL;
2914 subpst->read_symtab = pst->read_symtab;
2915 subpst->readin = 0;
2916
2917 /* No private part is necessary for include psymtabs. This property
2918 can be used to differentiate between such include psymtabs and
10b3939b 2919 the regular ones. */
58a9656e 2920 subpst->read_symtab_private = NULL;
aaa75496
JB
2921}
2922
2923/* Read the Line Number Program data and extract the list of files
2924 included by the source file represented by PST. Build an include
d85a05f0 2925 partial symtab for each of these included files. */
aaa75496
JB
2926
2927static void
2928dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2929 struct die_info *die,
aaa75496
JB
2930 struct partial_symtab *pst)
2931{
2932 struct objfile *objfile = cu->objfile;
2933 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2934 struct line_header *lh = NULL;
2935 struct attribute *attr;
aaa75496 2936
d85a05f0
DJ
2937 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2938 if (attr)
2939 {
2940 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2941
d85a05f0
DJ
2942 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2943 }
aaa75496
JB
2944 if (lh == NULL)
2945 return; /* No linetable, so no includes. */
2946
c6da4cef
DE
2947 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2948 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
2949
2950 free_line_header (lh);
2951}
2952
348e048f
DE
2953static hashval_t
2954hash_type_signature (const void *item)
2955{
2956 const struct signatured_type *type_sig = item;
9a619af0 2957
348e048f
DE
2958 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2959 return type_sig->signature;
2960}
2961
2962static int
2963eq_type_signature (const void *item_lhs, const void *item_rhs)
2964{
2965 const struct signatured_type *lhs = item_lhs;
2966 const struct signatured_type *rhs = item_rhs;
9a619af0 2967
348e048f
DE
2968 return lhs->signature == rhs->signature;
2969}
2970
1fd400ff
TT
2971/* Allocate a hash table for signatured types. */
2972
2973static htab_t
673bfd45 2974allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
2975{
2976 return htab_create_alloc_ex (41,
2977 hash_type_signature,
2978 eq_type_signature,
2979 NULL,
2980 &objfile->objfile_obstack,
2981 hashtab_obstack_allocate,
2982 dummy_obstack_deallocate);
2983}
2984
2985/* A helper function to add a signatured type CU to a list. */
2986
2987static int
2988add_signatured_type_cu_to_list (void **slot, void *datum)
2989{
2990 struct signatured_type *sigt = *slot;
2991 struct dwarf2_per_cu_data ***datap = datum;
2992
2993 **datap = &sigt->per_cu;
2994 ++*datap;
2995
2996 return 1;
2997}
2998
348e048f
DE
2999/* Create the hash table of all entries in the .debug_types section.
3000 The result is zero if there is an error (e.g. missing .debug_types section),
3001 otherwise non-zero. */
3002
3003static int
3004create_debug_types_hash_table (struct objfile *objfile)
3005{
be391dca 3006 gdb_byte *info_ptr;
348e048f 3007 htab_t types_htab;
1fd400ff 3008 struct dwarf2_per_cu_data **iter;
348e048f 3009
be391dca
TT
3010 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3011 info_ptr = dwarf2_per_objfile->types.buffer;
3012
348e048f
DE
3013 if (info_ptr == NULL)
3014 {
3015 dwarf2_per_objfile->signatured_types = NULL;
3016 return 0;
3017 }
3018
673bfd45 3019 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
3020
3021 if (dwarf2_die_debug)
3022 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3023
3e43a32a
MS
3024 while (info_ptr < dwarf2_per_objfile->types.buffer
3025 + dwarf2_per_objfile->types.size)
348e048f
DE
3026 {
3027 unsigned int offset;
3028 unsigned int offset_size;
3029 unsigned int type_offset;
3030 unsigned int length, initial_length_size;
3031 unsigned short version;
3032 ULONGEST signature;
3033 struct signatured_type *type_sig;
3034 void **slot;
3035 gdb_byte *ptr = info_ptr;
3036
3037 offset = ptr - dwarf2_per_objfile->types.buffer;
3038
3039 /* We need to read the type's signature in order to build the hash
3040 table, but we don't need to read anything else just yet. */
3041
3042 /* Sanity check to ensure entire cu is present. */
3043 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3044 if (ptr + length + initial_length_size
3045 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3046 {
3047 complaint (&symfile_complaints,
3e43a32a
MS
3048 _("debug type entry runs off end "
3049 "of `.debug_types' section, ignored"));
348e048f
DE
3050 break;
3051 }
3052
3053 offset_size = initial_length_size == 4 ? 4 : 8;
3054 ptr += initial_length_size;
3055 version = bfd_get_16 (objfile->obfd, ptr);
3056 ptr += 2;
3057 ptr += offset_size; /* abbrev offset */
3058 ptr += 1; /* address size */
3059 signature = bfd_get_64 (objfile->obfd, ptr);
3060 ptr += 8;
3061 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3062
3063 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3064 memset (type_sig, 0, sizeof (*type_sig));
3065 type_sig->signature = signature;
3066 type_sig->offset = offset;
3067 type_sig->type_offset = type_offset;
ca1f3406 3068 type_sig->per_cu.objfile = objfile;
1fd400ff 3069 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
3070
3071 slot = htab_find_slot (types_htab, type_sig, INSERT);
3072 gdb_assert (slot != NULL);
3073 *slot = type_sig;
3074
3075 if (dwarf2_die_debug)
3076 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3077 offset, phex (signature, sizeof (signature)));
3078
3079 info_ptr = info_ptr + initial_length_size + length;
3080 }
3081
3082 dwarf2_per_objfile->signatured_types = types_htab;
3083
1fd400ff
TT
3084 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3085 dwarf2_per_objfile->type_comp_units
3086 = obstack_alloc (&objfile->objfile_obstack,
3087 dwarf2_per_objfile->n_type_comp_units
3088 * sizeof (struct dwarf2_per_cu_data *));
3089 iter = &dwarf2_per_objfile->type_comp_units[0];
3090 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3091 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3092 == dwarf2_per_objfile->n_type_comp_units);
3093
348e048f
DE
3094 return 1;
3095}
3096
3097/* Lookup a signature based type.
3098 Returns NULL if SIG is not present in the table. */
3099
3100static struct signatured_type *
3101lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3102{
3103 struct signatured_type find_entry, *entry;
3104
3105 if (dwarf2_per_objfile->signatured_types == NULL)
3106 {
3107 complaint (&symfile_complaints,
55f1336d 3108 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3109 return 0;
3110 }
3111
3112 find_entry.signature = sig;
3113 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3114 return entry;
3115}
3116
d85a05f0
DJ
3117/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3118
3119static void
3120init_cu_die_reader (struct die_reader_specs *reader,
3121 struct dwarf2_cu *cu)
3122{
3123 reader->abfd = cu->objfile->obfd;
3124 reader->cu = cu;
3125 if (cu->per_cu->from_debug_types)
be391dca
TT
3126 {
3127 gdb_assert (dwarf2_per_objfile->types.readin);
3128 reader->buffer = dwarf2_per_objfile->types.buffer;
3129 }
d85a05f0 3130 else
be391dca
TT
3131 {
3132 gdb_assert (dwarf2_per_objfile->info.readin);
3133 reader->buffer = dwarf2_per_objfile->info.buffer;
3134 }
d85a05f0
DJ
3135}
3136
3137/* Find the base address of the compilation unit for range lists and
3138 location lists. It will normally be specified by DW_AT_low_pc.
3139 In DWARF-3 draft 4, the base address could be overridden by
3140 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3141 compilation units with discontinuous ranges. */
3142
3143static void
3144dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3145{
3146 struct attribute *attr;
3147
3148 cu->base_known = 0;
3149 cu->base_address = 0;
3150
3151 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3152 if (attr)
3153 {
3154 cu->base_address = DW_ADDR (attr);
3155 cu->base_known = 1;
3156 }
3157 else
3158 {
3159 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3160 if (attr)
3161 {
3162 cu->base_address = DW_ADDR (attr);
3163 cu->base_known = 1;
3164 }
3165 }
3166}
3167
348e048f
DE
3168/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3169 to combine the common parts.
93311388 3170 Process a compilation unit for a psymtab.
348e048f
DE
3171 BUFFER is a pointer to the beginning of the dwarf section buffer,
3172 either .debug_info or debug_types.
93311388
DE
3173 INFO_PTR is a pointer to the start of the CU.
3174 Returns a pointer to the next CU. */
aaa75496 3175
93311388
DE
3176static gdb_byte *
3177process_psymtab_comp_unit (struct objfile *objfile,
3178 struct dwarf2_per_cu_data *this_cu,
3179 gdb_byte *buffer, gdb_byte *info_ptr,
3180 unsigned int buffer_size)
c906108c 3181{
c906108c 3182 bfd *abfd = objfile->obfd;
93311388 3183 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3184 struct die_info *comp_unit_die;
c906108c 3185 struct partial_symtab *pst;
5734ee8b 3186 CORE_ADDR baseaddr;
93311388
DE
3187 struct cleanup *back_to_inner;
3188 struct dwarf2_cu cu;
d85a05f0
DJ
3189 int has_children, has_pc_info;
3190 struct attribute *attr;
d85a05f0
DJ
3191 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3192 struct die_reader_specs reader_specs;
3e2a0cee 3193 const char *filename;
c906108c 3194
9816fde3 3195 init_one_comp_unit (&cu, objfile);
93311388 3196 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3197
93311388
DE
3198 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3199 buffer, buffer_size,
3200 abfd);
10b3939b 3201
93311388
DE
3202 /* Complete the cu_header. */
3203 cu.header.offset = beg_of_comp_unit - buffer;
3204 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3205
93311388 3206 cu.list_in_scope = &file_symbols;
af703f96 3207
328c9494
DJ
3208 /* If this compilation unit was already read in, free the
3209 cached copy in order to read it in again. This is
3210 necessary because we skipped some symbols when we first
3211 read in the compilation unit (see load_partial_dies).
3212 This problem could be avoided, but the benefit is
3213 unclear. */
3214 if (this_cu->cu != NULL)
3215 free_one_cached_comp_unit (this_cu->cu);
3216
3217 /* Note that this is a pointer to our stack frame, being
3218 added to a global data structure. It will be cleaned up
3219 in free_stack_comp_unit when we finish with this
3220 compilation unit. */
3221 this_cu->cu = &cu;
d85a05f0
DJ
3222 cu.per_cu = this_cu;
3223
93311388
DE
3224 /* Read the abbrevs for this compilation unit into a table. */
3225 dwarf2_read_abbrevs (abfd, &cu);
3226 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3227
93311388 3228 /* Read the compilation unit die. */
348e048f
DE
3229 if (this_cu->from_debug_types)
3230 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3231 init_cu_die_reader (&reader_specs, &cu);
3232 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3233 &has_children);
93311388 3234
348e048f
DE
3235 if (this_cu->from_debug_types)
3236 {
3237 /* offset,length haven't been set yet for type units. */
3238 this_cu->offset = cu.header.offset;
3239 this_cu->length = cu.header.length + cu.header.initial_length_size;
3240 }
d85a05f0 3241 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3242 {
93311388
DE
3243 info_ptr = (beg_of_comp_unit + cu.header.length
3244 + cu.header.initial_length_size);
3245 do_cleanups (back_to_inner);
3246 return info_ptr;
3247 }
72bf9492 3248
9816fde3 3249 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3250
93311388 3251 /* Allocate a new partial symbol table structure. */
d85a05f0 3252 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3253 if (attr == NULL || !DW_STRING (attr))
3254 filename = "";
3255 else
3256 filename = DW_STRING (attr);
93311388 3257 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3258 filename,
93311388
DE
3259 /* TEXTLOW and TEXTHIGH are set below. */
3260 0,
3261 objfile->global_psymbols.next,
3262 objfile->static_psymbols.next);
72bf9492 3263
d85a05f0
DJ
3264 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3265 if (attr != NULL)
3266 pst->dirname = DW_STRING (attr);
72bf9492 3267
e38df1d0 3268 pst->read_symtab_private = this_cu;
72bf9492 3269
93311388 3270 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3271
0963b4bd 3272 /* Store the function that reads in the rest of the symbol table. */
93311388 3273 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3274
9291a0cd 3275 this_cu->v.psymtab = pst;
c906108c 3276
d85a05f0
DJ
3277 dwarf2_find_base_address (comp_unit_die, &cu);
3278
93311388
DE
3279 /* Possibly set the default values of LOWPC and HIGHPC from
3280 `DW_AT_ranges'. */
d85a05f0
DJ
3281 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3282 &best_highpc, &cu, pst);
3283 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3284 /* Store the contiguous range if it is not empty; it can be empty for
3285 CUs with no code. */
3286 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3287 best_lowpc + baseaddr,
3288 best_highpc + baseaddr - 1, pst);
93311388
DE
3289
3290 /* Check if comp unit has_children.
3291 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3292 If not, there's no more debug_info for this comp unit. */
d85a05f0 3293 if (has_children)
93311388
DE
3294 {
3295 struct partial_die_info *first_die;
3296 CORE_ADDR lowpc, highpc;
31ffec48 3297
93311388
DE
3298 lowpc = ((CORE_ADDR) -1);
3299 highpc = ((CORE_ADDR) 0);
c906108c 3300
93311388 3301 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3302
93311388 3303 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3304 ! has_pc_info, &cu);
57c22c6c 3305
93311388
DE
3306 /* If we didn't find a lowpc, set it to highpc to avoid
3307 complaints from `maint check'. */
3308 if (lowpc == ((CORE_ADDR) -1))
3309 lowpc = highpc;
10b3939b 3310
93311388
DE
3311 /* If the compilation unit didn't have an explicit address range,
3312 then use the information extracted from its child dies. */
d85a05f0 3313 if (! has_pc_info)
93311388 3314 {
d85a05f0
DJ
3315 best_lowpc = lowpc;
3316 best_highpc = highpc;
93311388
DE
3317 }
3318 }
d85a05f0
DJ
3319 pst->textlow = best_lowpc + baseaddr;
3320 pst->texthigh = best_highpc + baseaddr;
c906108c 3321
93311388
DE
3322 pst->n_global_syms = objfile->global_psymbols.next -
3323 (objfile->global_psymbols.list + pst->globals_offset);
3324 pst->n_static_syms = objfile->static_psymbols.next -
3325 (objfile->static_psymbols.list + pst->statics_offset);
3326 sort_pst_symbols (pst);
c906108c 3327
93311388
DE
3328 info_ptr = (beg_of_comp_unit + cu.header.length
3329 + cu.header.initial_length_size);
ae038cb0 3330
348e048f
DE
3331 if (this_cu->from_debug_types)
3332 {
3333 /* It's not clear we want to do anything with stmt lists here.
3334 Waiting to see what gcc ultimately does. */
3335 }
d85a05f0 3336 else
93311388
DE
3337 {
3338 /* Get the list of files included in the current compilation unit,
3339 and build a psymtab for each of them. */
d85a05f0 3340 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3341 }
ae038cb0 3342
93311388 3343 do_cleanups (back_to_inner);
ae038cb0 3344
93311388
DE
3345 return info_ptr;
3346}
ff013f42 3347
348e048f
DE
3348/* Traversal function for htab_traverse_noresize.
3349 Process one .debug_types comp-unit. */
3350
3351static int
3352process_type_comp_unit (void **slot, void *info)
3353{
3354 struct signatured_type *entry = (struct signatured_type *) *slot;
3355 struct objfile *objfile = (struct objfile *) info;
3356 struct dwarf2_per_cu_data *this_cu;
3357
3358 this_cu = &entry->per_cu;
348e048f 3359
be391dca 3360 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3361 process_psymtab_comp_unit (objfile, this_cu,
3362 dwarf2_per_objfile->types.buffer,
3363 dwarf2_per_objfile->types.buffer + entry->offset,
3364 dwarf2_per_objfile->types.size);
3365
3366 return 1;
3367}
3368
3369/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3370 Build partial symbol tables for the .debug_types comp-units. */
3371
3372static void
3373build_type_psymtabs (struct objfile *objfile)
3374{
3375 if (! create_debug_types_hash_table (objfile))
3376 return;
3377
3378 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3379 process_type_comp_unit, objfile);
3380}
3381
60606b2c
TT
3382/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3383
3384static void
3385psymtabs_addrmap_cleanup (void *o)
3386{
3387 struct objfile *objfile = o;
ec61707d 3388
60606b2c
TT
3389 objfile->psymtabs_addrmap = NULL;
3390}
3391
93311388
DE
3392/* Build the partial symbol table by doing a quick pass through the
3393 .debug_info and .debug_abbrev sections. */
72bf9492 3394
93311388 3395static void
c67a9c90 3396dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3397{
93311388 3398 gdb_byte *info_ptr;
60606b2c
TT
3399 struct cleanup *back_to, *addrmap_cleanup;
3400 struct obstack temp_obstack;
93311388 3401
98bfdba5
PA
3402 dwarf2_per_objfile->reading_partial_symbols = 1;
3403
be391dca 3404 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3405 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3406
93311388
DE
3407 /* Any cached compilation units will be linked by the per-objfile
3408 read_in_chain. Make sure to free them when we're done. */
3409 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3410
348e048f
DE
3411 build_type_psymtabs (objfile);
3412
93311388 3413 create_all_comp_units (objfile);
c906108c 3414
60606b2c
TT
3415 /* Create a temporary address map on a temporary obstack. We later
3416 copy this to the final obstack. */
3417 obstack_init (&temp_obstack);
3418 make_cleanup_obstack_free (&temp_obstack);
3419 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3420 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3421
93311388
DE
3422 /* Since the objects we're extracting from .debug_info vary in
3423 length, only the individual functions to extract them (like
3424 read_comp_unit_head and load_partial_die) can really know whether
3425 the buffer is large enough to hold another complete object.
c906108c 3426
93311388
DE
3427 At the moment, they don't actually check that. If .debug_info
3428 holds just one extra byte after the last compilation unit's dies,
3429 then read_comp_unit_head will happily read off the end of the
3430 buffer. read_partial_die is similarly casual. Those functions
3431 should be fixed.
c906108c 3432
93311388
DE
3433 For this loop condition, simply checking whether there's any data
3434 left at all should be sufficient. */
c906108c 3435
93311388
DE
3436 while (info_ptr < (dwarf2_per_objfile->info.buffer
3437 + dwarf2_per_objfile->info.size))
3438 {
3439 struct dwarf2_per_cu_data *this_cu;
dd373385 3440
3e43a32a
MS
3441 this_cu = dwarf2_find_comp_unit (info_ptr
3442 - dwarf2_per_objfile->info.buffer,
93311388 3443 objfile);
aaa75496 3444
93311388
DE
3445 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3446 dwarf2_per_objfile->info.buffer,
3447 info_ptr,
3448 dwarf2_per_objfile->info.size);
c906108c 3449 }
ff013f42
JK
3450
3451 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3452 &objfile->objfile_obstack);
60606b2c 3453 discard_cleanups (addrmap_cleanup);
ff013f42 3454
ae038cb0
DJ
3455 do_cleanups (back_to);
3456}
3457
93311388 3458/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3459
3460static void
93311388
DE
3461load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3462 struct objfile *objfile)
ae038cb0
DJ
3463{
3464 bfd *abfd = objfile->obfd;
fe1b8b76 3465 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3466 struct die_info *comp_unit_die;
ae038cb0 3467 struct dwarf2_cu *cu;
1d9ec526 3468 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3469 int has_children;
3470 struct die_reader_specs reader_specs;
98bfdba5 3471 int read_cu = 0;
ae038cb0 3472
348e048f
DE
3473 gdb_assert (! this_cu->from_debug_types);
3474
be391dca 3475 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3476 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3477 beg_of_comp_unit = info_ptr;
3478
98bfdba5
PA
3479 if (this_cu->cu == NULL)
3480 {
9816fde3
JK
3481 cu = xmalloc (sizeof (*cu));
3482 init_one_comp_unit (cu, objfile);
ae038cb0 3483
98bfdba5 3484 read_cu = 1;
ae038cb0 3485
98bfdba5
PA
3486 /* If an error occurs while loading, release our storage. */
3487 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3488
98bfdba5
PA
3489 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3490 dwarf2_per_objfile->info.buffer,
3491 dwarf2_per_objfile->info.size,
3492 abfd);
ae038cb0 3493
98bfdba5
PA
3494 /* Complete the cu_header. */
3495 cu->header.offset = this_cu->offset;
3496 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3497
3498 /* Link this compilation unit into the compilation unit tree. */
3499 this_cu->cu = cu;
3500 cu->per_cu = this_cu;
98bfdba5
PA
3501
3502 /* Link this CU into read_in_chain. */
3503 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3504 dwarf2_per_objfile->read_in_chain = this_cu;
3505 }
3506 else
3507 {
3508 cu = this_cu->cu;
3509 info_ptr += cu->header.first_die_offset;
3510 }
ae038cb0
DJ
3511
3512 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3513 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3514 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3515 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3516
3517 /* Read the compilation unit die. */
d85a05f0
DJ
3518 init_cu_die_reader (&reader_specs, cu);
3519 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3520 &has_children);
ae038cb0 3521
9816fde3 3522 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3523
ae038cb0
DJ
3524 /* Check if comp unit has_children.
3525 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3526 If not, there's no more debug_info for this comp unit. */
d85a05f0 3527 if (has_children)
93311388 3528 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3529
98bfdba5
PA
3530 do_cleanups (free_abbrevs_cleanup);
3531
3532 if (read_cu)
3533 {
3534 /* We've successfully allocated this compilation unit. Let our
3535 caller clean it up when finished with it. */
3536 discard_cleanups (free_cu_cleanup);
3537 }
ae038cb0
DJ
3538}
3539
3540/* Create a list of all compilation units in OBJFILE. We do this only
3541 if an inter-comp-unit reference is found; presumably if there is one,
3542 there will be many, and one will occur early in the .debug_info section.
3543 So there's no point in building this list incrementally. */
3544
3545static void
3546create_all_comp_units (struct objfile *objfile)
3547{
3548 int n_allocated;
3549 int n_comp_units;
3550 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3551 gdb_byte *info_ptr;
3552
3553 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3554 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3555
3556 n_comp_units = 0;
3557 n_allocated = 10;
3558 all_comp_units = xmalloc (n_allocated
3559 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3560
3e43a32a
MS
3561 while (info_ptr < dwarf2_per_objfile->info.buffer
3562 + dwarf2_per_objfile->info.size)
ae038cb0 3563 {
c764a876 3564 unsigned int length, initial_length_size;
ae038cb0 3565 struct dwarf2_per_cu_data *this_cu;
c764a876 3566 unsigned int offset;
ae038cb0 3567
dce234bc 3568 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3569
3570 /* Read just enough information to find out where the next
3571 compilation unit is. */
c764a876
DE
3572 length = read_initial_length (objfile->obfd, info_ptr,
3573 &initial_length_size);
ae038cb0
DJ
3574
3575 /* Save the compilation unit for later lookup. */
3576 this_cu = obstack_alloc (&objfile->objfile_obstack,
3577 sizeof (struct dwarf2_per_cu_data));
3578 memset (this_cu, 0, sizeof (*this_cu));
3579 this_cu->offset = offset;
c764a876 3580 this_cu->length = length + initial_length_size;
9291a0cd 3581 this_cu->objfile = objfile;
ae038cb0
DJ
3582
3583 if (n_comp_units == n_allocated)
3584 {
3585 n_allocated *= 2;
3586 all_comp_units = xrealloc (all_comp_units,
3587 n_allocated
3588 * sizeof (struct dwarf2_per_cu_data *));
3589 }
3590 all_comp_units[n_comp_units++] = this_cu;
3591
3592 info_ptr = info_ptr + this_cu->length;
3593 }
3594
3595 dwarf2_per_objfile->all_comp_units
3596 = obstack_alloc (&objfile->objfile_obstack,
3597 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3598 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3599 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3600 xfree (all_comp_units);
3601 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3602}
3603
5734ee8b
DJ
3604/* Process all loaded DIEs for compilation unit CU, starting at
3605 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3606 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3607 DW_AT_ranges). If NEED_PC is set, then this function will set
3608 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3609 and record the covered ranges in the addrmap. */
c906108c 3610
72bf9492
DJ
3611static void
3612scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3613 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3614{
72bf9492 3615 struct partial_die_info *pdi;
c906108c 3616
91c24f0a
DC
3617 /* Now, march along the PDI's, descending into ones which have
3618 interesting children but skipping the children of the other ones,
3619 until we reach the end of the compilation unit. */
c906108c 3620
72bf9492 3621 pdi = first_die;
91c24f0a 3622
72bf9492
DJ
3623 while (pdi != NULL)
3624 {
3625 fixup_partial_die (pdi, cu);
c906108c 3626
f55ee35c 3627 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3628 children, so we need to look at them. Ditto for anonymous
3629 enums. */
933c6fe4 3630
72bf9492 3631 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3632 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3633 {
72bf9492 3634 switch (pdi->tag)
c906108c
SS
3635 {
3636 case DW_TAG_subprogram:
5734ee8b 3637 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3638 break;
72929c62 3639 case DW_TAG_constant:
c906108c
SS
3640 case DW_TAG_variable:
3641 case DW_TAG_typedef:
91c24f0a 3642 case DW_TAG_union_type:
72bf9492 3643 if (!pdi->is_declaration)
63d06c5c 3644 {
72bf9492 3645 add_partial_symbol (pdi, cu);
63d06c5c
DC
3646 }
3647 break;
c906108c 3648 case DW_TAG_class_type:
680b30c7 3649 case DW_TAG_interface_type:
c906108c 3650 case DW_TAG_structure_type:
72bf9492 3651 if (!pdi->is_declaration)
c906108c 3652 {
72bf9492 3653 add_partial_symbol (pdi, cu);
c906108c
SS
3654 }
3655 break;
91c24f0a 3656 case DW_TAG_enumeration_type:
72bf9492
DJ
3657 if (!pdi->is_declaration)
3658 add_partial_enumeration (pdi, cu);
c906108c
SS
3659 break;
3660 case DW_TAG_base_type:
a02abb62 3661 case DW_TAG_subrange_type:
c906108c 3662 /* File scope base type definitions are added to the partial
c5aa993b 3663 symbol table. */
72bf9492 3664 add_partial_symbol (pdi, cu);
c906108c 3665 break;
d9fa45fe 3666 case DW_TAG_namespace:
5734ee8b 3667 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3668 break;
5d7cb8df
JK
3669 case DW_TAG_module:
3670 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3671 break;
c906108c
SS
3672 default:
3673 break;
3674 }
3675 }
3676
72bf9492
DJ
3677 /* If the die has a sibling, skip to the sibling. */
3678
3679 pdi = pdi->die_sibling;
3680 }
3681}
3682
3683/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3684
72bf9492 3685 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3686 name is concatenated with "::" and the partial DIE's name. For
3687 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3688 Enumerators are an exception; they use the scope of their parent
3689 enumeration type, i.e. the name of the enumeration type is not
3690 prepended to the enumerator.
91c24f0a 3691
72bf9492
DJ
3692 There are two complexities. One is DW_AT_specification; in this
3693 case "parent" means the parent of the target of the specification,
3694 instead of the direct parent of the DIE. The other is compilers
3695 which do not emit DW_TAG_namespace; in this case we try to guess
3696 the fully qualified name of structure types from their members'
3697 linkage names. This must be done using the DIE's children rather
3698 than the children of any DW_AT_specification target. We only need
3699 to do this for structures at the top level, i.e. if the target of
3700 any DW_AT_specification (if any; otherwise the DIE itself) does not
3701 have a parent. */
3702
3703/* Compute the scope prefix associated with PDI's parent, in
3704 compilation unit CU. The result will be allocated on CU's
3705 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3706 field. NULL is returned if no prefix is necessary. */
3707static char *
3708partial_die_parent_scope (struct partial_die_info *pdi,
3709 struct dwarf2_cu *cu)
3710{
3711 char *grandparent_scope;
3712 struct partial_die_info *parent, *real_pdi;
91c24f0a 3713
72bf9492
DJ
3714 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3715 then this means the parent of the specification DIE. */
3716
3717 real_pdi = pdi;
72bf9492 3718 while (real_pdi->has_specification)
10b3939b 3719 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3720
3721 parent = real_pdi->die_parent;
3722 if (parent == NULL)
3723 return NULL;
3724
3725 if (parent->scope_set)
3726 return parent->scope;
3727
3728 fixup_partial_die (parent, cu);
3729
10b3939b 3730 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3731
acebe513
UW
3732 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3733 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3734 Work around this problem here. */
3735 if (cu->language == language_cplus
6e70227d 3736 && parent->tag == DW_TAG_namespace
acebe513
UW
3737 && strcmp (parent->name, "::") == 0
3738 && grandparent_scope == NULL)
3739 {
3740 parent->scope = NULL;
3741 parent->scope_set = 1;
3742 return NULL;
3743 }
3744
72bf9492 3745 if (parent->tag == DW_TAG_namespace
f55ee35c 3746 || parent->tag == DW_TAG_module
72bf9492
DJ
3747 || parent->tag == DW_TAG_structure_type
3748 || parent->tag == DW_TAG_class_type
680b30c7 3749 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3750 || parent->tag == DW_TAG_union_type
3751 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3752 {
3753 if (grandparent_scope == NULL)
3754 parent->scope = parent->name;
3755 else
3e43a32a
MS
3756 parent->scope = typename_concat (&cu->comp_unit_obstack,
3757 grandparent_scope,
f55ee35c 3758 parent->name, 0, cu);
72bf9492 3759 }
ceeb3d5a 3760 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3761 /* Enumerators should not get the name of the enumeration as a prefix. */
3762 parent->scope = grandparent_scope;
3763 else
3764 {
3765 /* FIXME drow/2004-04-01: What should we be doing with
3766 function-local names? For partial symbols, we should probably be
3767 ignoring them. */
3768 complaint (&symfile_complaints,
e2e0b3e5 3769 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3770 parent->tag, pdi->offset);
3771 parent->scope = grandparent_scope;
c906108c
SS
3772 }
3773
72bf9492
DJ
3774 parent->scope_set = 1;
3775 return parent->scope;
3776}
3777
3778/* Return the fully scoped name associated with PDI, from compilation unit
3779 CU. The result will be allocated with malloc. */
3780static char *
3781partial_die_full_name (struct partial_die_info *pdi,
3782 struct dwarf2_cu *cu)
3783{
3784 char *parent_scope;
3785
98bfdba5
PA
3786 /* If this is a template instantiation, we can not work out the
3787 template arguments from partial DIEs. So, unfortunately, we have
3788 to go through the full DIEs. At least any work we do building
3789 types here will be reused if full symbols are loaded later. */
3790 if (pdi->has_template_arguments)
3791 {
3792 fixup_partial_die (pdi, cu);
3793
3794 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3795 {
3796 struct die_info *die;
3797 struct attribute attr;
3798 struct dwarf2_cu *ref_cu = cu;
3799
3800 attr.name = 0;
3801 attr.form = DW_FORM_ref_addr;
3802 attr.u.addr = pdi->offset;
3803 die = follow_die_ref (NULL, &attr, &ref_cu);
3804
3805 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3806 }
3807 }
3808
72bf9492
DJ
3809 parent_scope = partial_die_parent_scope (pdi, cu);
3810 if (parent_scope == NULL)
3811 return NULL;
3812 else
f55ee35c 3813 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3814}
3815
3816static void
72bf9492 3817add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3818{
e7c27a73 3819 struct objfile *objfile = cu->objfile;
c906108c 3820 CORE_ADDR addr = 0;
decbce07 3821 char *actual_name = NULL;
5c4e30ca 3822 const struct partial_symbol *psym = NULL;
e142c38c 3823 CORE_ADDR baseaddr;
72bf9492 3824 int built_actual_name = 0;
e142c38c
DJ
3825
3826 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3827
94af9270
KS
3828 actual_name = partial_die_full_name (pdi, cu);
3829 if (actual_name)
3830 built_actual_name = 1;
63d06c5c 3831
72bf9492
DJ
3832 if (actual_name == NULL)
3833 actual_name = pdi->name;
3834
c906108c
SS
3835 switch (pdi->tag)
3836 {
3837 case DW_TAG_subprogram:
2cfa0c8d 3838 if (pdi->is_external || cu->language == language_ada)
c906108c 3839 {
2cfa0c8d
JB
3840 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3841 of the global scope. But in Ada, we want to be able to access
3842 nested procedures globally. So all Ada subprograms are stored
3843 in the global scope. */
f47fb265 3844 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3845 mst_text, objfile); */
f47fb265
MS
3846 add_psymbol_to_list (actual_name, strlen (actual_name),
3847 built_actual_name,
3848 VAR_DOMAIN, LOC_BLOCK,
3849 &objfile->global_psymbols,
3850 0, pdi->lowpc + baseaddr,
3851 cu->language, objfile);
c906108c
SS
3852 }
3853 else
3854 {
f47fb265 3855 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3856 mst_file_text, objfile); */
f47fb265
MS
3857 add_psymbol_to_list (actual_name, strlen (actual_name),
3858 built_actual_name,
3859 VAR_DOMAIN, LOC_BLOCK,
3860 &objfile->static_psymbols,
3861 0, pdi->lowpc + baseaddr,
3862 cu->language, objfile);
c906108c
SS
3863 }
3864 break;
72929c62
JB
3865 case DW_TAG_constant:
3866 {
3867 struct psymbol_allocation_list *list;
3868
3869 if (pdi->is_external)
3870 list = &objfile->global_psymbols;
3871 else
3872 list = &objfile->static_psymbols;
f47fb265
MS
3873 add_psymbol_to_list (actual_name, strlen (actual_name),
3874 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3875 list, 0, 0, cu->language, objfile);
72929c62
JB
3876 }
3877 break;
c906108c 3878 case DW_TAG_variable:
caac4577
JG
3879 if (pdi->locdesc)
3880 addr = decode_locdesc (pdi->locdesc, cu);
3881
3882 if (pdi->locdesc
3883 && addr == 0
3884 && !dwarf2_per_objfile->has_section_at_zero)
3885 {
3886 /* A global or static variable may also have been stripped
3887 out by the linker if unused, in which case its address
3888 will be nullified; do not add such variables into partial
3889 symbol table then. */
3890 }
3891 else if (pdi->is_external)
c906108c
SS
3892 {
3893 /* Global Variable.
3894 Don't enter into the minimal symbol tables as there is
3895 a minimal symbol table entry from the ELF symbols already.
3896 Enter into partial symbol table if it has a location
3897 descriptor or a type.
3898 If the location descriptor is missing, new_symbol will create
3899 a LOC_UNRESOLVED symbol, the address of the variable will then
3900 be determined from the minimal symbol table whenever the variable
3901 is referenced.
3902 The address for the partial symbol table entry is not
3903 used by GDB, but it comes in handy for debugging partial symbol
3904 table building. */
3905
c906108c 3906 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
3907 add_psymbol_to_list (actual_name, strlen (actual_name),
3908 built_actual_name,
3909 VAR_DOMAIN, LOC_STATIC,
3910 &objfile->global_psymbols,
3911 0, addr + baseaddr,
3912 cu->language, objfile);
c906108c
SS
3913 }
3914 else
3915 {
0963b4bd 3916 /* Static Variable. Skip symbols without location descriptors. */
c906108c 3917 if (pdi->locdesc == NULL)
decbce07
MS
3918 {
3919 if (built_actual_name)
3920 xfree (actual_name);
3921 return;
3922 }
f47fb265 3923 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3924 mst_file_data, objfile); */
f47fb265
MS
3925 add_psymbol_to_list (actual_name, strlen (actual_name),
3926 built_actual_name,
3927 VAR_DOMAIN, LOC_STATIC,
3928 &objfile->static_psymbols,
3929 0, addr + baseaddr,
3930 cu->language, objfile);
c906108c
SS
3931 }
3932 break;
3933 case DW_TAG_typedef:
3934 case DW_TAG_base_type:
a02abb62 3935 case DW_TAG_subrange_type:
38d518c9 3936 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3937 built_actual_name,
176620f1 3938 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3939 &objfile->static_psymbols,
e142c38c 3940 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3941 break;
72bf9492
DJ
3942 case DW_TAG_namespace:
3943 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3944 built_actual_name,
72bf9492
DJ
3945 VAR_DOMAIN, LOC_TYPEDEF,
3946 &objfile->global_psymbols,
3947 0, (CORE_ADDR) 0, cu->language, objfile);
3948 break;
c906108c 3949 case DW_TAG_class_type:
680b30c7 3950 case DW_TAG_interface_type:
c906108c
SS
3951 case DW_TAG_structure_type:
3952 case DW_TAG_union_type:
3953 case DW_TAG_enumeration_type:
fa4028e9
JB
3954 /* Skip external references. The DWARF standard says in the section
3955 about "Structure, Union, and Class Type Entries": "An incomplete
3956 structure, union or class type is represented by a structure,
3957 union or class entry that does not have a byte size attribute
3958 and that has a DW_AT_declaration attribute." */
3959 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3960 {
3961 if (built_actual_name)
3962 xfree (actual_name);
3963 return;
3964 }
fa4028e9 3965
63d06c5c
DC
3966 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3967 static vs. global. */
38d518c9 3968 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3969 built_actual_name,
176620f1 3970 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3971 (cu->language == language_cplus
3972 || cu->language == language_java)
63d06c5c
DC
3973 ? &objfile->global_psymbols
3974 : &objfile->static_psymbols,
e142c38c 3975 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3976
c906108c
SS
3977 break;
3978 case DW_TAG_enumerator:
38d518c9 3979 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3980 built_actual_name,
176620f1 3981 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3982 (cu->language == language_cplus
3983 || cu->language == language_java)
f6fe98ef
DJ
3984 ? &objfile->global_psymbols
3985 : &objfile->static_psymbols,
e142c38c 3986 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3987 break;
3988 default:
3989 break;
3990 }
5c4e30ca 3991
72bf9492
DJ
3992 if (built_actual_name)
3993 xfree (actual_name);
c906108c
SS
3994}
3995
5c4e30ca
DC
3996/* Read a partial die corresponding to a namespace; also, add a symbol
3997 corresponding to that namespace to the symbol table. NAMESPACE is
3998 the name of the enclosing namespace. */
91c24f0a 3999
72bf9492
DJ
4000static void
4001add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4002 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4003 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4004{
72bf9492 4005 /* Add a symbol for the namespace. */
e7c27a73 4006
72bf9492 4007 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4008
4009 /* Now scan partial symbols in that namespace. */
4010
91c24f0a 4011 if (pdi->has_children)
5734ee8b 4012 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4013}
4014
5d7cb8df
JK
4015/* Read a partial die corresponding to a Fortran module. */
4016
4017static void
4018add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4019 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4020{
f55ee35c 4021 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4022
4023 if (pdi->has_children)
4024 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4025}
4026
bc30ff58
JB
4027/* Read a partial die corresponding to a subprogram and create a partial
4028 symbol for that subprogram. When the CU language allows it, this
4029 routine also defines a partial symbol for each nested subprogram
4030 that this subprogram contains.
6e70227d 4031
bc30ff58
JB
4032 DIE my also be a lexical block, in which case we simply search
4033 recursively for suprograms defined inside that lexical block.
4034 Again, this is only performed when the CU language allows this
4035 type of definitions. */
4036
4037static void
4038add_partial_subprogram (struct partial_die_info *pdi,
4039 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4040 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4041{
4042 if (pdi->tag == DW_TAG_subprogram)
4043 {
4044 if (pdi->has_pc_info)
4045 {
4046 if (pdi->lowpc < *lowpc)
4047 *lowpc = pdi->lowpc;
4048 if (pdi->highpc > *highpc)
4049 *highpc = pdi->highpc;
5734ee8b
DJ
4050 if (need_pc)
4051 {
4052 CORE_ADDR baseaddr;
4053 struct objfile *objfile = cu->objfile;
4054
4055 baseaddr = ANOFFSET (objfile->section_offsets,
4056 SECT_OFF_TEXT (objfile));
4057 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4058 pdi->lowpc + baseaddr,
4059 pdi->highpc - 1 + baseaddr,
9291a0cd 4060 cu->per_cu->v.psymtab);
5734ee8b 4061 }
bc30ff58 4062 if (!pdi->is_declaration)
e8d05480
JB
4063 /* Ignore subprogram DIEs that do not have a name, they are
4064 illegal. Do not emit a complaint at this point, we will
4065 do so when we convert this psymtab into a symtab. */
4066 if (pdi->name)
4067 add_partial_symbol (pdi, cu);
bc30ff58
JB
4068 }
4069 }
6e70227d 4070
bc30ff58
JB
4071 if (! pdi->has_children)
4072 return;
4073
4074 if (cu->language == language_ada)
4075 {
4076 pdi = pdi->die_child;
4077 while (pdi != NULL)
4078 {
4079 fixup_partial_die (pdi, cu);
4080 if (pdi->tag == DW_TAG_subprogram
4081 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4082 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4083 pdi = pdi->die_sibling;
4084 }
4085 }
4086}
4087
91c24f0a
DC
4088/* Read a partial die corresponding to an enumeration type. */
4089
72bf9492
DJ
4090static void
4091add_partial_enumeration (struct partial_die_info *enum_pdi,
4092 struct dwarf2_cu *cu)
91c24f0a 4093{
72bf9492 4094 struct partial_die_info *pdi;
91c24f0a
DC
4095
4096 if (enum_pdi->name != NULL)
72bf9492
DJ
4097 add_partial_symbol (enum_pdi, cu);
4098
4099 pdi = enum_pdi->die_child;
4100 while (pdi)
91c24f0a 4101 {
72bf9492 4102 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4103 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4104 else
72bf9492
DJ
4105 add_partial_symbol (pdi, cu);
4106 pdi = pdi->die_sibling;
91c24f0a 4107 }
91c24f0a
DC
4108}
4109
4bb7a0a7
DJ
4110/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4111 Return the corresponding abbrev, or NULL if the number is zero (indicating
4112 an empty DIE). In either case *BYTES_READ will be set to the length of
4113 the initial number. */
4114
4115static struct abbrev_info *
fe1b8b76 4116peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4117 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4118{
4119 bfd *abfd = cu->objfile->obfd;
4120 unsigned int abbrev_number;
4121 struct abbrev_info *abbrev;
4122
4123 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4124
4125 if (abbrev_number == 0)
4126 return NULL;
4127
4128 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4129 if (!abbrev)
4130 {
3e43a32a
MS
4131 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4132 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4133 }
4134
4135 return abbrev;
4136}
4137
93311388
DE
4138/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4139 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4140 DIE. Any children of the skipped DIEs will also be skipped. */
4141
fe1b8b76 4142static gdb_byte *
93311388 4143skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4144{
4145 struct abbrev_info *abbrev;
4146 unsigned int bytes_read;
4147
4148 while (1)
4149 {
4150 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4151 if (abbrev == NULL)
4152 return info_ptr + bytes_read;
4153 else
93311388 4154 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4155 }
4156}
4157
93311388
DE
4158/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4159 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4160 abbrev corresponding to that skipped uleb128 should be passed in
4161 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4162 children. */
4163
fe1b8b76 4164static gdb_byte *
93311388
DE
4165skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4166 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4167{
4168 unsigned int bytes_read;
4169 struct attribute attr;
4170 bfd *abfd = cu->objfile->obfd;
4171 unsigned int form, i;
4172
4173 for (i = 0; i < abbrev->num_attrs; i++)
4174 {
4175 /* The only abbrev we care about is DW_AT_sibling. */
4176 if (abbrev->attrs[i].name == DW_AT_sibling)
4177 {
4178 read_attribute (&attr, &abbrev->attrs[i],
4179 abfd, info_ptr, cu);
4180 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4181 complaint (&symfile_complaints,
4182 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4183 else
93311388 4184 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4185 }
4186
4187 /* If it isn't DW_AT_sibling, skip this attribute. */
4188 form = abbrev->attrs[i].form;
4189 skip_attribute:
4190 switch (form)
4191 {
4bb7a0a7 4192 case DW_FORM_ref_addr:
ae411497
TT
4193 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4194 and later it is offset sized. */
4195 if (cu->header.version == 2)
4196 info_ptr += cu->header.addr_size;
4197 else
4198 info_ptr += cu->header.offset_size;
4199 break;
4200 case DW_FORM_addr:
4bb7a0a7
DJ
4201 info_ptr += cu->header.addr_size;
4202 break;
4203 case DW_FORM_data1:
4204 case DW_FORM_ref1:
4205 case DW_FORM_flag:
4206 info_ptr += 1;
4207 break;
2dc7f7b3
TT
4208 case DW_FORM_flag_present:
4209 break;
4bb7a0a7
DJ
4210 case DW_FORM_data2:
4211 case DW_FORM_ref2:
4212 info_ptr += 2;
4213 break;
4214 case DW_FORM_data4:
4215 case DW_FORM_ref4:
4216 info_ptr += 4;
4217 break;
4218 case DW_FORM_data8:
4219 case DW_FORM_ref8:
55f1336d 4220 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4221 info_ptr += 8;
4222 break;
4223 case DW_FORM_string:
9b1c24c8 4224 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4225 info_ptr += bytes_read;
4226 break;
2dc7f7b3 4227 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4228 case DW_FORM_strp:
4229 info_ptr += cu->header.offset_size;
4230 break;
2dc7f7b3 4231 case DW_FORM_exprloc:
4bb7a0a7
DJ
4232 case DW_FORM_block:
4233 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4234 info_ptr += bytes_read;
4235 break;
4236 case DW_FORM_block1:
4237 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4238 break;
4239 case DW_FORM_block2:
4240 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4241 break;
4242 case DW_FORM_block4:
4243 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4244 break;
4245 case DW_FORM_sdata:
4246 case DW_FORM_udata:
4247 case DW_FORM_ref_udata:
4248 info_ptr = skip_leb128 (abfd, info_ptr);
4249 break;
4250 case DW_FORM_indirect:
4251 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4252 info_ptr += bytes_read;
4253 /* We need to continue parsing from here, so just go back to
4254 the top. */
4255 goto skip_attribute;
4256
4257 default:
3e43a32a
MS
4258 error (_("Dwarf Error: Cannot handle %s "
4259 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4260 dwarf_form_name (form),
4261 bfd_get_filename (abfd));
4262 }
4263 }
4264
4265 if (abbrev->has_children)
93311388 4266 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4267 else
4268 return info_ptr;
4269}
4270
93311388
DE
4271/* Locate ORIG_PDI's sibling.
4272 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4273 in BUFFER. */
91c24f0a 4274
fe1b8b76 4275static gdb_byte *
93311388
DE
4276locate_pdi_sibling (struct partial_die_info *orig_pdi,
4277 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4278 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4279{
4280 /* Do we know the sibling already? */
72bf9492 4281
91c24f0a
DC
4282 if (orig_pdi->sibling)
4283 return orig_pdi->sibling;
4284
4285 /* Are there any children to deal with? */
4286
4287 if (!orig_pdi->has_children)
4288 return info_ptr;
4289
4bb7a0a7 4290 /* Skip the children the long way. */
91c24f0a 4291
93311388 4292 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4293}
4294
c906108c
SS
4295/* Expand this partial symbol table into a full symbol table. */
4296
4297static void
fba45db2 4298dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4299{
c906108c
SS
4300 if (pst != NULL)
4301 {
4302 if (pst->readin)
4303 {
3e43a32a
MS
4304 warning (_("bug: psymtab for %s is already read in."),
4305 pst->filename);
c906108c
SS
4306 }
4307 else
4308 {
4309 if (info_verbose)
4310 {
3e43a32a
MS
4311 printf_filtered (_("Reading in symbols for %s..."),
4312 pst->filename);
c906108c
SS
4313 gdb_flush (gdb_stdout);
4314 }
4315
10b3939b
DJ
4316 /* Restore our global data. */
4317 dwarf2_per_objfile = objfile_data (pst->objfile,
4318 dwarf2_objfile_data_key);
4319
b2ab525c
KB
4320 /* If this psymtab is constructed from a debug-only objfile, the
4321 has_section_at_zero flag will not necessarily be correct. We
4322 can get the correct value for this flag by looking at the data
4323 associated with the (presumably stripped) associated objfile. */
4324 if (pst->objfile->separate_debug_objfile_backlink)
4325 {
4326 struct dwarf2_per_objfile *dpo_backlink
4327 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4328 dwarf2_objfile_data_key);
9a619af0 4329
b2ab525c
KB
4330 dwarf2_per_objfile->has_section_at_zero
4331 = dpo_backlink->has_section_at_zero;
4332 }
4333
98bfdba5
PA
4334 dwarf2_per_objfile->reading_partial_symbols = 0;
4335
c906108c
SS
4336 psymtab_to_symtab_1 (pst);
4337
4338 /* Finish up the debug error message. */
4339 if (info_verbose)
a3f17187 4340 printf_filtered (_("done.\n"));
c906108c
SS
4341 }
4342 }
4343}
4344
10b3939b
DJ
4345/* Add PER_CU to the queue. */
4346
4347static void
03dd20cc 4348queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4349{
4350 struct dwarf2_queue_item *item;
4351
4352 per_cu->queued = 1;
4353 item = xmalloc (sizeof (*item));
4354 item->per_cu = per_cu;
4355 item->next = NULL;
4356
4357 if (dwarf2_queue == NULL)
4358 dwarf2_queue = item;
4359 else
4360 dwarf2_queue_tail->next = item;
4361
4362 dwarf2_queue_tail = item;
4363}
4364
4365/* Process the queue. */
4366
4367static void
4368process_queue (struct objfile *objfile)
4369{
4370 struct dwarf2_queue_item *item, *next_item;
4371
03dd20cc
DJ
4372 /* The queue starts out with one item, but following a DIE reference
4373 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4374 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4375 {
9291a0cd
TT
4376 if (dwarf2_per_objfile->using_index
4377 ? !item->per_cu->v.quick->symtab
4378 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4379 process_full_comp_unit (item->per_cu);
4380
4381 item->per_cu->queued = 0;
4382 next_item = item->next;
4383 xfree (item);
4384 }
4385
4386 dwarf2_queue_tail = NULL;
4387}
4388
4389/* Free all allocated queue entries. This function only releases anything if
4390 an error was thrown; if the queue was processed then it would have been
4391 freed as we went along. */
4392
4393static void
4394dwarf2_release_queue (void *dummy)
4395{
4396 struct dwarf2_queue_item *item, *last;
4397
4398 item = dwarf2_queue;
4399 while (item)
4400 {
4401 /* Anything still marked queued is likely to be in an
4402 inconsistent state, so discard it. */
4403 if (item->per_cu->queued)
4404 {
4405 if (item->per_cu->cu != NULL)
4406 free_one_cached_comp_unit (item->per_cu->cu);
4407 item->per_cu->queued = 0;
4408 }
4409
4410 last = item;
4411 item = item->next;
4412 xfree (last);
4413 }
4414
4415 dwarf2_queue = dwarf2_queue_tail = NULL;
4416}
4417
4418/* Read in full symbols for PST, and anything it depends on. */
4419
c906108c 4420static void
fba45db2 4421psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4422{
10b3939b 4423 struct dwarf2_per_cu_data *per_cu;
c906108c 4424 struct cleanup *back_to;
aaa75496
JB
4425 int i;
4426
4427 for (i = 0; i < pst->number_of_dependencies; i++)
4428 if (!pst->dependencies[i]->readin)
4429 {
4430 /* Inform about additional files that need to be read in. */
4431 if (info_verbose)
4432 {
a3f17187 4433 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4434 fputs_filtered (" ", gdb_stdout);
4435 wrap_here ("");
4436 fputs_filtered ("and ", gdb_stdout);
4437 wrap_here ("");
4438 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4439 wrap_here (""); /* Flush output. */
aaa75496
JB
4440 gdb_flush (gdb_stdout);
4441 }
4442 psymtab_to_symtab_1 (pst->dependencies[i]);
4443 }
4444
e38df1d0 4445 per_cu = pst->read_symtab_private;
10b3939b
DJ
4446
4447 if (per_cu == NULL)
aaa75496
JB
4448 {
4449 /* It's an include file, no symbols to read for it.
4450 Everything is in the parent symtab. */
4451 pst->readin = 1;
4452 return;
4453 }
c906108c 4454
9291a0cd 4455 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4456}
4457
93311388 4458/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4459
93311388 4460static void
3e43a32a
MS
4461load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4462 struct objfile *objfile)
10b3939b 4463{
31ffec48 4464 bfd *abfd = objfile->obfd;
10b3939b 4465 struct dwarf2_cu *cu;
c764a876 4466 unsigned int offset;
93311388 4467 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4468 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4469 struct attribute *attr;
98bfdba5 4470 int read_cu = 0;
6502dd73 4471
348e048f
DE
4472 gdb_assert (! per_cu->from_debug_types);
4473
c906108c 4474 /* Set local variables from the partial symbol table info. */
10b3939b 4475 offset = per_cu->offset;
6502dd73 4476
be391dca 4477 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4478 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4479 beg_of_comp_unit = info_ptr;
63d06c5c 4480
98bfdba5
PA
4481 if (per_cu->cu == NULL)
4482 {
9816fde3
JK
4483 cu = xmalloc (sizeof (*cu));
4484 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4485
4486 read_cu = 1;
c906108c 4487
98bfdba5
PA
4488 /* If an error occurs while loading, release our storage. */
4489 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4490
98bfdba5
PA
4491 /* Read in the comp_unit header. */
4492 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4493
98bfdba5
PA
4494 /* Complete the cu_header. */
4495 cu->header.offset = offset;
4496 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4497
98bfdba5
PA
4498 /* Read the abbrevs for this compilation unit. */
4499 dwarf2_read_abbrevs (abfd, cu);
4500 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4501
98bfdba5
PA
4502 /* Link this compilation unit into the compilation unit tree. */
4503 per_cu->cu = cu;
4504 cu->per_cu = per_cu;
98bfdba5
PA
4505
4506 /* Link this CU into read_in_chain. */
4507 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4508 dwarf2_per_objfile->read_in_chain = per_cu;
4509 }
4510 else
4511 {
4512 cu = per_cu->cu;
4513 info_ptr += cu->header.first_die_offset;
4514 }
e142c38c 4515
93311388 4516 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4517
4518 /* We try not to read any attributes in this function, because not
4519 all objfiles needed for references have been loaded yet, and symbol
4520 table processing isn't initialized. But we have to set the CU language,
4521 or we won't be able to build types correctly. */
9816fde3 4522 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4523
a6c727b2
DJ
4524 /* Similarly, if we do not read the producer, we can not apply
4525 producer-specific interpretation. */
4526 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4527 if (attr)
4528 cu->producer = DW_STRING (attr);
4529
98bfdba5
PA
4530 if (read_cu)
4531 {
4532 do_cleanups (free_abbrevs_cleanup);
e142c38c 4533
98bfdba5
PA
4534 /* We've successfully allocated this compilation unit. Let our
4535 caller clean it up when finished with it. */
4536 discard_cleanups (free_cu_cleanup);
4537 }
10b3939b
DJ
4538}
4539
3da10d80
KS
4540/* Add a DIE to the delayed physname list. */
4541
4542static void
4543add_to_method_list (struct type *type, int fnfield_index, int index,
4544 const char *name, struct die_info *die,
4545 struct dwarf2_cu *cu)
4546{
4547 struct delayed_method_info mi;
4548 mi.type = type;
4549 mi.fnfield_index = fnfield_index;
4550 mi.index = index;
4551 mi.name = name;
4552 mi.die = die;
4553 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4554}
4555
4556/* A cleanup for freeing the delayed method list. */
4557
4558static void
4559free_delayed_list (void *ptr)
4560{
4561 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4562 if (cu->method_list != NULL)
4563 {
4564 VEC_free (delayed_method_info, cu->method_list);
4565 cu->method_list = NULL;
4566 }
4567}
4568
4569/* Compute the physnames of any methods on the CU's method list.
4570
4571 The computation of method physnames is delayed in order to avoid the
4572 (bad) condition that one of the method's formal parameters is of an as yet
4573 incomplete type. */
4574
4575static void
4576compute_delayed_physnames (struct dwarf2_cu *cu)
4577{
4578 int i;
4579 struct delayed_method_info *mi;
4580 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4581 {
4582 char *physname;
4583 struct fn_fieldlist *fn_flp
4584 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4585 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4586 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4587 }
4588}
4589
10b3939b
DJ
4590/* Generate full symbol information for PST and CU, whose DIEs have
4591 already been loaded into memory. */
4592
4593static void
4594process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4595{
10b3939b 4596 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4597 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4598 CORE_ADDR lowpc, highpc;
4599 struct symtab *symtab;
3da10d80 4600 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4601 CORE_ADDR baseaddr;
4602
4603 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4604
10b3939b
DJ
4605 buildsym_init ();
4606 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4607 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4608
4609 cu->list_in_scope = &file_symbols;
c906108c 4610
d85a05f0 4611 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4612
c906108c 4613 /* Do line number decoding in read_file_scope () */
10b3939b 4614 process_die (cu->dies, cu);
c906108c 4615
3da10d80
KS
4616 /* Now that we have processed all the DIEs in the CU, all the types
4617 should be complete, and it should now be safe to compute all of the
4618 physnames. */
4619 compute_delayed_physnames (cu);
4620 do_cleanups (delayed_list_cleanup);
4621
fae299cd
DC
4622 /* Some compilers don't define a DW_AT_high_pc attribute for the
4623 compilation unit. If the DW_AT_high_pc is missing, synthesize
4624 it, by scanning the DIE's below the compilation unit. */
10b3939b 4625 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4626
613e1657 4627 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4628
4629 /* Set symtab language to language from DW_AT_language.
4630 If the compilation is from a C file generated by language preprocessors,
4631 do not set the language if it was already deduced by start_subfile. */
4632 if (symtab != NULL
10b3939b 4633 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4634 {
10b3939b 4635 symtab->language = cu->language;
c906108c 4636 }
9291a0cd
TT
4637
4638 if (dwarf2_per_objfile->using_index)
4639 per_cu->v.quick->symtab = symtab;
4640 else
4641 {
4642 struct partial_symtab *pst = per_cu->v.psymtab;
4643 pst->symtab = symtab;
4644 pst->readin = 1;
4645 }
c906108c
SS
4646
4647 do_cleanups (back_to);
4648}
4649
4650/* Process a die and its children. */
4651
4652static void
e7c27a73 4653process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4654{
4655 switch (die->tag)
4656 {
4657 case DW_TAG_padding:
4658 break;
4659 case DW_TAG_compile_unit:
e7c27a73 4660 read_file_scope (die, cu);
c906108c 4661 break;
348e048f
DE
4662 case DW_TAG_type_unit:
4663 read_type_unit_scope (die, cu);
4664 break;
c906108c 4665 case DW_TAG_subprogram:
c906108c 4666 case DW_TAG_inlined_subroutine:
edb3359d 4667 read_func_scope (die, cu);
c906108c
SS
4668 break;
4669 case DW_TAG_lexical_block:
14898363
L
4670 case DW_TAG_try_block:
4671 case DW_TAG_catch_block:
e7c27a73 4672 read_lexical_block_scope (die, cu);
c906108c
SS
4673 break;
4674 case DW_TAG_class_type:
680b30c7 4675 case DW_TAG_interface_type:
c906108c
SS
4676 case DW_TAG_structure_type:
4677 case DW_TAG_union_type:
134d01f1 4678 process_structure_scope (die, cu);
c906108c
SS
4679 break;
4680 case DW_TAG_enumeration_type:
134d01f1 4681 process_enumeration_scope (die, cu);
c906108c 4682 break;
134d01f1 4683
f792889a
DJ
4684 /* These dies have a type, but processing them does not create
4685 a symbol or recurse to process the children. Therefore we can
4686 read them on-demand through read_type_die. */
c906108c 4687 case DW_TAG_subroutine_type:
72019c9c 4688 case DW_TAG_set_type:
c906108c 4689 case DW_TAG_array_type:
c906108c 4690 case DW_TAG_pointer_type:
c906108c 4691 case DW_TAG_ptr_to_member_type:
c906108c 4692 case DW_TAG_reference_type:
c906108c 4693 case DW_TAG_string_type:
c906108c 4694 break;
134d01f1 4695
c906108c 4696 case DW_TAG_base_type:
a02abb62 4697 case DW_TAG_subrange_type:
cb249c71 4698 case DW_TAG_typedef:
134d01f1
DJ
4699 /* Add a typedef symbol for the type definition, if it has a
4700 DW_AT_name. */
f792889a 4701 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4702 break;
c906108c 4703 case DW_TAG_common_block:
e7c27a73 4704 read_common_block (die, cu);
c906108c
SS
4705 break;
4706 case DW_TAG_common_inclusion:
4707 break;
d9fa45fe 4708 case DW_TAG_namespace:
63d06c5c 4709 processing_has_namespace_info = 1;
e7c27a73 4710 read_namespace (die, cu);
d9fa45fe 4711 break;
5d7cb8df 4712 case DW_TAG_module:
f55ee35c 4713 processing_has_namespace_info = 1;
5d7cb8df
JK
4714 read_module (die, cu);
4715 break;
d9fa45fe
DC
4716 case DW_TAG_imported_declaration:
4717 case DW_TAG_imported_module:
63d06c5c 4718 processing_has_namespace_info = 1;
27aa8d6a
SW
4719 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4720 || cu->language != language_fortran))
4721 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4722 dwarf_tag_name (die->tag));
4723 read_import_statement (die, cu);
d9fa45fe 4724 break;
c906108c 4725 default:
e7c27a73 4726 new_symbol (die, NULL, cu);
c906108c
SS
4727 break;
4728 }
4729}
4730
94af9270
KS
4731/* A helper function for dwarf2_compute_name which determines whether DIE
4732 needs to have the name of the scope prepended to the name listed in the
4733 die. */
4734
4735static int
4736die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4737{
1c809c68
TT
4738 struct attribute *attr;
4739
94af9270
KS
4740 switch (die->tag)
4741 {
4742 case DW_TAG_namespace:
4743 case DW_TAG_typedef:
4744 case DW_TAG_class_type:
4745 case DW_TAG_interface_type:
4746 case DW_TAG_structure_type:
4747 case DW_TAG_union_type:
4748 case DW_TAG_enumeration_type:
4749 case DW_TAG_enumerator:
4750 case DW_TAG_subprogram:
4751 case DW_TAG_member:
4752 return 1;
4753
4754 case DW_TAG_variable:
c2b0a229 4755 case DW_TAG_constant:
94af9270
KS
4756 /* We only need to prefix "globally" visible variables. These include
4757 any variable marked with DW_AT_external or any variable that
4758 lives in a namespace. [Variables in anonymous namespaces
4759 require prefixing, but they are not DW_AT_external.] */
4760
4761 if (dwarf2_attr (die, DW_AT_specification, cu))
4762 {
4763 struct dwarf2_cu *spec_cu = cu;
9a619af0 4764
94af9270
KS
4765 return die_needs_namespace (die_specification (die, &spec_cu),
4766 spec_cu);
4767 }
4768
1c809c68 4769 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4770 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4771 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4772 return 0;
4773 /* A variable in a lexical block of some kind does not need a
4774 namespace, even though in C++ such variables may be external
4775 and have a mangled name. */
4776 if (die->parent->tag == DW_TAG_lexical_block
4777 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4778 || die->parent->tag == DW_TAG_catch_block
4779 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4780 return 0;
4781 return 1;
94af9270
KS
4782
4783 default:
4784 return 0;
4785 }
4786}
4787
98bfdba5
PA
4788/* Retrieve the last character from a mem_file. */
4789
4790static void
4791do_ui_file_peek_last (void *object, const char *buffer, long length)
4792{
4793 char *last_char_p = (char *) object;
4794
4795 if (length > 0)
4796 *last_char_p = buffer[length - 1];
4797}
4798
94af9270
KS
4799/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4800 compute the physname for the object, which include a method's
4801 formal parameters (C++/Java) and return type (Java).
4802
af6b7be1
JB
4803 For Ada, return the DIE's linkage name rather than the fully qualified
4804 name. PHYSNAME is ignored..
4805
94af9270
KS
4806 The result is allocated on the objfile_obstack and canonicalized. */
4807
4808static const char *
4809dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4810 int physname)
4811{
4812 if (name == NULL)
4813 name = dwarf2_name (die, cu);
4814
f55ee35c
JK
4815 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4816 compute it by typename_concat inside GDB. */
4817 if (cu->language == language_ada
4818 || (cu->language == language_fortran && physname))
4819 {
4820 /* For Ada unit, we prefer the linkage name over the name, as
4821 the former contains the exported name, which the user expects
4822 to be able to reference. Ideally, we want the user to be able
4823 to reference this entity using either natural or linkage name,
4824 but we haven't started looking at this enhancement yet. */
4825 struct attribute *attr;
4826
4827 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4828 if (attr == NULL)
4829 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4830 if (attr && DW_STRING (attr))
4831 return DW_STRING (attr);
4832 }
4833
94af9270
KS
4834 /* These are the only languages we know how to qualify names in. */
4835 if (name != NULL
f55ee35c
JK
4836 && (cu->language == language_cplus || cu->language == language_java
4837 || cu->language == language_fortran))
94af9270
KS
4838 {
4839 if (die_needs_namespace (die, cu))
4840 {
4841 long length;
4842 char *prefix;
4843 struct ui_file *buf;
4844
4845 prefix = determine_prefix (die, cu);
4846 buf = mem_fileopen ();
4847 if (*prefix != '\0')
4848 {
f55ee35c
JK
4849 char *prefixed_name = typename_concat (NULL, prefix, name,
4850 physname, cu);
9a619af0 4851
94af9270
KS
4852 fputs_unfiltered (prefixed_name, buf);
4853 xfree (prefixed_name);
4854 }
4855 else
62d5b8da 4856 fputs_unfiltered (name, buf);
94af9270 4857
98bfdba5
PA
4858 /* Template parameters may be specified in the DIE's DW_AT_name, or
4859 as children with DW_TAG_template_type_param or
4860 DW_TAG_value_type_param. If the latter, add them to the name
4861 here. If the name already has template parameters, then
4862 skip this step; some versions of GCC emit both, and
4863 it is more efficient to use the pre-computed name.
4864
4865 Something to keep in mind about this process: it is very
4866 unlikely, or in some cases downright impossible, to produce
4867 something that will match the mangled name of a function.
4868 If the definition of the function has the same debug info,
4869 we should be able to match up with it anyway. But fallbacks
4870 using the minimal symbol, for instance to find a method
4871 implemented in a stripped copy of libstdc++, will not work.
4872 If we do not have debug info for the definition, we will have to
4873 match them up some other way.
4874
4875 When we do name matching there is a related problem with function
4876 templates; two instantiated function templates are allowed to
4877 differ only by their return types, which we do not add here. */
4878
4879 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4880 {
4881 struct attribute *attr;
4882 struct die_info *child;
4883 int first = 1;
4884
4885 die->building_fullname = 1;
4886
4887 for (child = die->child; child != NULL; child = child->sibling)
4888 {
4889 struct type *type;
4890 long value;
4891 gdb_byte *bytes;
4892 struct dwarf2_locexpr_baton *baton;
4893 struct value *v;
4894
4895 if (child->tag != DW_TAG_template_type_param
4896 && child->tag != DW_TAG_template_value_param)
4897 continue;
4898
4899 if (first)
4900 {
4901 fputs_unfiltered ("<", buf);
4902 first = 0;
4903 }
4904 else
4905 fputs_unfiltered (", ", buf);
4906
4907 attr = dwarf2_attr (child, DW_AT_type, cu);
4908 if (attr == NULL)
4909 {
4910 complaint (&symfile_complaints,
4911 _("template parameter missing DW_AT_type"));
4912 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4913 continue;
4914 }
4915 type = die_type (child, cu);
4916
4917 if (child->tag == DW_TAG_template_type_param)
4918 {
4919 c_print_type (type, "", buf, -1, 0);
4920 continue;
4921 }
4922
4923 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4924 if (attr == NULL)
4925 {
4926 complaint (&symfile_complaints,
3e43a32a
MS
4927 _("template parameter missing "
4928 "DW_AT_const_value"));
98bfdba5
PA
4929 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4930 continue;
4931 }
4932
4933 dwarf2_const_value_attr (attr, type, name,
4934 &cu->comp_unit_obstack, cu,
4935 &value, &bytes, &baton);
4936
4937 if (TYPE_NOSIGN (type))
4938 /* GDB prints characters as NUMBER 'CHAR'. If that's
4939 changed, this can use value_print instead. */
4940 c_printchar (value, type, buf);
4941 else
4942 {
4943 struct value_print_options opts;
4944
4945 if (baton != NULL)
4946 v = dwarf2_evaluate_loc_desc (type, NULL,
4947 baton->data,
4948 baton->size,
4949 baton->per_cu);
4950 else if (bytes != NULL)
4951 {
4952 v = allocate_value (type);
4953 memcpy (value_contents_writeable (v), bytes,
4954 TYPE_LENGTH (type));
4955 }
4956 else
4957 v = value_from_longest (type, value);
4958
3e43a32a
MS
4959 /* Specify decimal so that we do not depend on
4960 the radix. */
98bfdba5
PA
4961 get_formatted_print_options (&opts, 'd');
4962 opts.raw = 1;
4963 value_print (v, buf, &opts);
4964 release_value (v);
4965 value_free (v);
4966 }
4967 }
4968
4969 die->building_fullname = 0;
4970
4971 if (!first)
4972 {
4973 /* Close the argument list, with a space if necessary
4974 (nested templates). */
4975 char last_char = '\0';
4976 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4977 if (last_char == '>')
4978 fputs_unfiltered (" >", buf);
4979 else
4980 fputs_unfiltered (">", buf);
4981 }
4982 }
4983
94af9270
KS
4984 /* For Java and C++ methods, append formal parameter type
4985 information, if PHYSNAME. */
6e70227d 4986
94af9270
KS
4987 if (physname && die->tag == DW_TAG_subprogram
4988 && (cu->language == language_cplus
4989 || cu->language == language_java))
4990 {
4991 struct type *type = read_type_die (die, cu);
4992
3167638f 4993 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
4994
4995 if (cu->language == language_java)
4996 {
4997 /* For java, we must append the return type to method
0963b4bd 4998 names. */
94af9270
KS
4999 if (die->tag == DW_TAG_subprogram)
5000 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5001 0, 0);
5002 }
5003 else if (cu->language == language_cplus)
5004 {
60430eff
DJ
5005 /* Assume that an artificial first parameter is
5006 "this", but do not crash if it is not. RealView
5007 marks unnamed (and thus unused) parameters as
5008 artificial; there is no way to differentiate
5009 the two cases. */
94af9270
KS
5010 if (TYPE_NFIELDS (type) > 0
5011 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5012 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5013 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5014 0))))
94af9270
KS
5015 fputs_unfiltered (" const", buf);
5016 }
5017 }
5018
5019 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5020 &length);
5021 ui_file_delete (buf);
5022
5023 if (cu->language == language_cplus)
5024 {
5025 char *cname
5026 = dwarf2_canonicalize_name (name, cu,
5027 &cu->objfile->objfile_obstack);
9a619af0 5028
94af9270
KS
5029 if (cname != NULL)
5030 name = cname;
5031 }
5032 }
5033 }
5034
5035 return name;
5036}
5037
0114d602
DJ
5038/* Return the fully qualified name of DIE, based on its DW_AT_name.
5039 If scope qualifiers are appropriate they will be added. The result
5040 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5041 not have a name. NAME may either be from a previous call to
5042 dwarf2_name or NULL.
5043
0963b4bd 5044 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5045
5046static const char *
94af9270 5047dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5048{
94af9270
KS
5049 return dwarf2_compute_name (name, die, cu, 0);
5050}
0114d602 5051
94af9270
KS
5052/* Construct a physname for the given DIE in CU. NAME may either be
5053 from a previous call to dwarf2_name or NULL. The result will be
5054 allocated on the objfile_objstack or NULL if the DIE does not have a
5055 name.
0114d602 5056
94af9270 5057 The output string will be canonicalized (if C++/Java). */
0114d602 5058
94af9270
KS
5059static const char *
5060dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5061{
5062 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
5063}
5064
27aa8d6a
SW
5065/* Read the import statement specified by the given die and record it. */
5066
5067static void
5068read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5069{
5070 struct attribute *import_attr;
5071 struct die_info *imported_die;
de4affc9 5072 struct dwarf2_cu *imported_cu;
27aa8d6a 5073 const char *imported_name;
794684b6 5074 const char *imported_name_prefix;
13387711
SW
5075 const char *canonical_name;
5076 const char *import_alias;
5077 const char *imported_declaration = NULL;
794684b6 5078 const char *import_prefix;
13387711
SW
5079
5080 char *temp;
27aa8d6a
SW
5081
5082 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5083 if (import_attr == NULL)
5084 {
5085 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5086 dwarf_tag_name (die->tag));
5087 return;
5088 }
5089
de4affc9
CC
5090 imported_cu = cu;
5091 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5092 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5093 if (imported_name == NULL)
5094 {
5095 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5096
5097 The import in the following code:
5098 namespace A
5099 {
5100 typedef int B;
5101 }
5102
5103 int main ()
5104 {
5105 using A::B;
5106 B b;
5107 return b;
5108 }
5109
5110 ...
5111 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5112 <52> DW_AT_decl_file : 1
5113 <53> DW_AT_decl_line : 6
5114 <54> DW_AT_import : <0x75>
5115 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5116 <59> DW_AT_name : B
5117 <5b> DW_AT_decl_file : 1
5118 <5c> DW_AT_decl_line : 2
5119 <5d> DW_AT_type : <0x6e>
5120 ...
5121 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5122 <76> DW_AT_byte_size : 4
5123 <77> DW_AT_encoding : 5 (signed)
5124
5125 imports the wrong die ( 0x75 instead of 0x58 ).
5126 This case will be ignored until the gcc bug is fixed. */
5127 return;
5128 }
5129
82856980
SW
5130 /* Figure out the local name after import. */
5131 import_alias = dwarf2_name (die, cu);
27aa8d6a 5132
794684b6
SW
5133 /* Figure out where the statement is being imported to. */
5134 import_prefix = determine_prefix (die, cu);
5135
5136 /* Figure out what the scope of the imported die is and prepend it
5137 to the name of the imported die. */
de4affc9 5138 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5139
f55ee35c
JK
5140 if (imported_die->tag != DW_TAG_namespace
5141 && imported_die->tag != DW_TAG_module)
794684b6 5142 {
13387711
SW
5143 imported_declaration = imported_name;
5144 canonical_name = imported_name_prefix;
794684b6 5145 }
13387711 5146 else if (strlen (imported_name_prefix) > 0)
794684b6 5147 {
13387711
SW
5148 temp = alloca (strlen (imported_name_prefix)
5149 + 2 + strlen (imported_name) + 1);
5150 strcpy (temp, imported_name_prefix);
5151 strcat (temp, "::");
5152 strcat (temp, imported_name);
5153 canonical_name = temp;
794684b6 5154 }
13387711
SW
5155 else
5156 canonical_name = imported_name;
794684b6 5157
c0cc3a76
SW
5158 cp_add_using_directive (import_prefix,
5159 canonical_name,
5160 import_alias,
13387711 5161 imported_declaration,
c0cc3a76 5162 &cu->objfile->objfile_obstack);
27aa8d6a
SW
5163}
5164
5fb290d7 5165static void
e142c38c 5166initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5167{
e142c38c 5168 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5169}
5170
ae2de4f8
DE
5171/* Cleanup function for read_file_scope. */
5172
cb1df416
DJ
5173static void
5174free_cu_line_header (void *arg)
5175{
5176 struct dwarf2_cu *cu = arg;
5177
5178 free_line_header (cu->line_header);
5179 cu->line_header = NULL;
5180}
5181
9291a0cd
TT
5182static void
5183find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5184 char **name, char **comp_dir)
5185{
5186 struct attribute *attr;
5187
5188 *name = NULL;
5189 *comp_dir = NULL;
5190
5191 /* Find the filename. Do not use dwarf2_name here, since the filename
5192 is not a source language identifier. */
5193 attr = dwarf2_attr (die, DW_AT_name, cu);
5194 if (attr)
5195 {
5196 *name = DW_STRING (attr);
5197 }
5198
5199 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5200 if (attr)
5201 *comp_dir = DW_STRING (attr);
5202 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5203 {
5204 *comp_dir = ldirname (*name);
5205 if (*comp_dir != NULL)
5206 make_cleanup (xfree, *comp_dir);
5207 }
5208 if (*comp_dir != NULL)
5209 {
5210 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5211 directory, get rid of it. */
5212 char *cp = strchr (*comp_dir, ':');
5213
5214 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5215 *comp_dir = cp + 1;
5216 }
5217
5218 if (*name == NULL)
5219 *name = "<unknown>";
5220}
5221
ae2de4f8
DE
5222/* Process DW_TAG_compile_unit. */
5223
c906108c 5224static void
e7c27a73 5225read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5226{
e7c27a73 5227 struct objfile *objfile = cu->objfile;
debd256d 5228 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5229 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5230 CORE_ADDR highpc = ((CORE_ADDR) 0);
5231 struct attribute *attr;
e1024ff1 5232 char *name = NULL;
c906108c
SS
5233 char *comp_dir = NULL;
5234 struct die_info *child_die;
5235 bfd *abfd = objfile->obfd;
debd256d 5236 struct line_header *line_header = 0;
e142c38c 5237 CORE_ADDR baseaddr;
6e70227d 5238
e142c38c 5239 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5240
fae299cd 5241 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5242
5243 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5244 from finish_block. */
2acceee2 5245 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5246 lowpc = highpc;
5247 lowpc += baseaddr;
5248 highpc += baseaddr;
5249
9291a0cd 5250 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5251
e142c38c 5252 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5253 if (attr)
5254 {
e142c38c 5255 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5256 }
5257
b0f35d58 5258 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5259 if (attr)
b0f35d58 5260 cu->producer = DW_STRING (attr);
303b6f5d 5261
f4b8a18d
KW
5262 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5263 standardised yet. As a workaround for the language detection we fall
5264 back to the DW_AT_producer string. */
5265 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5266 cu->language = language_opencl;
5267
0963b4bd 5268 /* We assume that we're processing GCC output. */
c906108c 5269 processing_gcc_compilation = 2;
c906108c 5270
df8a16a1
DJ
5271 processing_has_namespace_info = 0;
5272
c906108c
SS
5273 start_symtab (name, comp_dir, lowpc);
5274 record_debugformat ("DWARF 2");
303b6f5d 5275 record_producer (cu->producer);
c906108c 5276
e142c38c 5277 initialize_cu_func_list (cu);
c906108c 5278
cb1df416
DJ
5279 /* Decode line number information if present. We do this before
5280 processing child DIEs, so that the line header table is available
5281 for DW_AT_decl_file. */
e142c38c 5282 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
5283 if (attr)
5284 {
debd256d 5285 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 5286 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
5287 if (line_header)
5288 {
cb1df416
DJ
5289 cu->line_header = line_header;
5290 make_cleanup (free_cu_line_header, cu);
aaa75496 5291 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 5292 }
5fb290d7 5293 }
debd256d 5294
cb1df416
DJ
5295 /* Process all dies in compilation unit. */
5296 if (die->child != NULL)
5297 {
5298 child_die = die->child;
5299 while (child_die && child_die->tag)
5300 {
5301 process_die (child_die, cu);
5302 child_die = sibling_die (child_die);
5303 }
5304 }
5305
2e276125
JB
5306 /* Decode macro information, if present. Dwarf 2 macro information
5307 refers to information in the line number info statement program
5308 header, so we can only read it if we've read the header
5309 successfully. */
e142c38c 5310 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 5311 if (attr && line_header)
2e276125
JB
5312 {
5313 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5314
2e276125 5315 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 5316 comp_dir, abfd, cu);
2e276125 5317 }
debd256d 5318 do_cleanups (back_to);
5fb290d7
DJ
5319}
5320
ae2de4f8
DE
5321/* Process DW_TAG_type_unit.
5322 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5323 actual type being defined by this TU. In this case the first top
5324 level sibling is there to provide context only. */
5325
5326static void
5327read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5328{
5329 struct objfile *objfile = cu->objfile;
5330 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5331 CORE_ADDR lowpc;
5332 struct attribute *attr;
5333 char *name = NULL;
5334 char *comp_dir = NULL;
5335 struct die_info *child_die;
5336 bfd *abfd = objfile->obfd;
348e048f
DE
5337
5338 /* start_symtab needs a low pc, but we don't really have one.
5339 Do what read_file_scope would do in the absence of such info. */
5340 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5341
5342 /* Find the filename. Do not use dwarf2_name here, since the filename
5343 is not a source language identifier. */
5344 attr = dwarf2_attr (die, DW_AT_name, cu);
5345 if (attr)
5346 name = DW_STRING (attr);
5347
5348 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5349 if (attr)
5350 comp_dir = DW_STRING (attr);
5351 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5352 {
5353 comp_dir = ldirname (name);
5354 if (comp_dir != NULL)
5355 make_cleanup (xfree, comp_dir);
5356 }
5357
5358 if (name == NULL)
5359 name = "<unknown>";
5360
5361 attr = dwarf2_attr (die, DW_AT_language, cu);
5362 if (attr)
5363 set_cu_language (DW_UNSND (attr), cu);
5364
5365 /* This isn't technically needed today. It is done for symmetry
5366 with read_file_scope. */
5367 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5368 if (attr)
348e048f
DE
5369 cu->producer = DW_STRING (attr);
5370
0963b4bd 5371 /* We assume that we're processing GCC output. */
348e048f
DE
5372 processing_gcc_compilation = 2;
5373
5374 processing_has_namespace_info = 0;
5375
5376 start_symtab (name, comp_dir, lowpc);
5377 record_debugformat ("DWARF 2");
5378 record_producer (cu->producer);
5379
5380 /* Process the dies in the type unit. */
5381 if (die->child == NULL)
5382 {
5383 dump_die_for_error (die);
5384 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5385 bfd_get_filename (abfd));
5386 }
5387
5388 child_die = die->child;
5389
5390 while (child_die && child_die->tag)
5391 {
5392 process_die (child_die, cu);
5393
5394 child_die = sibling_die (child_die);
5395 }
5396
5397 do_cleanups (back_to);
5398}
5399
5fb290d7 5400static void
e142c38c
DJ
5401add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5402 struct dwarf2_cu *cu)
5fb290d7
DJ
5403{
5404 struct function_range *thisfn;
5405
5406 thisfn = (struct function_range *)
7b5a2f43 5407 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5408 thisfn->name = name;
5409 thisfn->lowpc = lowpc;
5410 thisfn->highpc = highpc;
5411 thisfn->seen_line = 0;
5412 thisfn->next = NULL;
5413
e142c38c
DJ
5414 if (cu->last_fn == NULL)
5415 cu->first_fn = thisfn;
5fb290d7 5416 else
e142c38c 5417 cu->last_fn->next = thisfn;
5fb290d7 5418
e142c38c 5419 cu->last_fn = thisfn;
c906108c
SS
5420}
5421
d389af10
JK
5422/* qsort helper for inherit_abstract_dies. */
5423
5424static int
5425unsigned_int_compar (const void *ap, const void *bp)
5426{
5427 unsigned int a = *(unsigned int *) ap;
5428 unsigned int b = *(unsigned int *) bp;
5429
5430 return (a > b) - (b > a);
5431}
5432
5433/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5434 Inherit only the children of the DW_AT_abstract_origin DIE not being
5435 already referenced by DW_AT_abstract_origin from the children of the
5436 current DIE. */
d389af10
JK
5437
5438static void
5439inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5440{
5441 struct die_info *child_die;
5442 unsigned die_children_count;
5443 /* CU offsets which were referenced by children of the current DIE. */
5444 unsigned *offsets;
5445 unsigned *offsets_end, *offsetp;
5446 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5447 struct die_info *origin_die;
5448 /* Iterator of the ORIGIN_DIE children. */
5449 struct die_info *origin_child_die;
5450 struct cleanup *cleanups;
5451 struct attribute *attr;
cd02d79d
PA
5452 struct dwarf2_cu *origin_cu;
5453 struct pending **origin_previous_list_in_scope;
d389af10
JK
5454
5455 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5456 if (!attr)
5457 return;
5458
cd02d79d
PA
5459 /* Note that following die references may follow to a die in a
5460 different cu. */
5461
5462 origin_cu = cu;
5463 origin_die = follow_die_ref (die, attr, &origin_cu);
5464
5465 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5466 symbols in. */
5467 origin_previous_list_in_scope = origin_cu->list_in_scope;
5468 origin_cu->list_in_scope = cu->list_in_scope;
5469
edb3359d
DJ
5470 if (die->tag != origin_die->tag
5471 && !(die->tag == DW_TAG_inlined_subroutine
5472 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5473 complaint (&symfile_complaints,
5474 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5475 die->offset, origin_die->offset);
5476
5477 child_die = die->child;
5478 die_children_count = 0;
5479 while (child_die && child_die->tag)
5480 {
5481 child_die = sibling_die (child_die);
5482 die_children_count++;
5483 }
5484 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5485 cleanups = make_cleanup (xfree, offsets);
5486
5487 offsets_end = offsets;
5488 child_die = die->child;
5489 while (child_die && child_die->tag)
5490 {
c38f313d
DJ
5491 /* For each CHILD_DIE, find the corresponding child of
5492 ORIGIN_DIE. If there is more than one layer of
5493 DW_AT_abstract_origin, follow them all; there shouldn't be,
5494 but GCC versions at least through 4.4 generate this (GCC PR
5495 40573). */
5496 struct die_info *child_origin_die = child_die;
cd02d79d 5497 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5498
c38f313d
DJ
5499 while (1)
5500 {
cd02d79d
PA
5501 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5502 child_origin_cu);
c38f313d
DJ
5503 if (attr == NULL)
5504 break;
cd02d79d
PA
5505 child_origin_die = follow_die_ref (child_origin_die, attr,
5506 &child_origin_cu);
c38f313d
DJ
5507 }
5508
d389af10
JK
5509 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5510 counterpart may exist. */
c38f313d 5511 if (child_origin_die != child_die)
d389af10 5512 {
edb3359d
DJ
5513 if (child_die->tag != child_origin_die->tag
5514 && !(child_die->tag == DW_TAG_inlined_subroutine
5515 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5516 complaint (&symfile_complaints,
5517 _("Child DIE 0x%x and its abstract origin 0x%x have "
5518 "different tags"), child_die->offset,
5519 child_origin_die->offset);
c38f313d
DJ
5520 if (child_origin_die->parent != origin_die)
5521 complaint (&symfile_complaints,
5522 _("Child DIE 0x%x and its abstract origin 0x%x have "
5523 "different parents"), child_die->offset,
5524 child_origin_die->offset);
5525 else
5526 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5527 }
5528 child_die = sibling_die (child_die);
5529 }
5530 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5531 unsigned_int_compar);
5532 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5533 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5534 complaint (&symfile_complaints,
5535 _("Multiple children of DIE 0x%x refer "
5536 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5537 die->offset, *offsetp);
5538
5539 offsetp = offsets;
5540 origin_child_die = origin_die->child;
5541 while (origin_child_die && origin_child_die->tag)
5542 {
5543 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5544 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5545 offsetp++;
5546 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5547 {
5548 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5549 process_die (origin_child_die, origin_cu);
d389af10
JK
5550 }
5551 origin_child_die = sibling_die (origin_child_die);
5552 }
cd02d79d 5553 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5554
5555 do_cleanups (cleanups);
5556}
5557
c906108c 5558static void
e7c27a73 5559read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5560{
e7c27a73 5561 struct objfile *objfile = cu->objfile;
52f0bd74 5562 struct context_stack *new;
c906108c
SS
5563 CORE_ADDR lowpc;
5564 CORE_ADDR highpc;
5565 struct die_info *child_die;
edb3359d 5566 struct attribute *attr, *call_line, *call_file;
c906108c 5567 char *name;
e142c38c 5568 CORE_ADDR baseaddr;
801e3a5b 5569 struct block *block;
edb3359d 5570 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5571 VEC (symbolp) *template_args = NULL;
5572 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5573
5574 if (inlined_func)
5575 {
5576 /* If we do not have call site information, we can't show the
5577 caller of this inlined function. That's too confusing, so
5578 only use the scope for local variables. */
5579 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5580 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5581 if (call_line == NULL || call_file == NULL)
5582 {
5583 read_lexical_block_scope (die, cu);
5584 return;
5585 }
5586 }
c906108c 5587
e142c38c
DJ
5588 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5589
94af9270 5590 name = dwarf2_name (die, cu);
c906108c 5591
e8d05480
JB
5592 /* Ignore functions with missing or empty names. These are actually
5593 illegal according to the DWARF standard. */
5594 if (name == NULL)
5595 {
5596 complaint (&symfile_complaints,
5597 _("missing name for subprogram DIE at %d"), die->offset);
5598 return;
5599 }
5600
5601 /* Ignore functions with missing or invalid low and high pc attributes. */
5602 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5603 {
ae4d0c03
PM
5604 attr = dwarf2_attr (die, DW_AT_external, cu);
5605 if (!attr || !DW_UNSND (attr))
5606 complaint (&symfile_complaints,
3e43a32a
MS
5607 _("cannot get low and high bounds "
5608 "for subprogram DIE at %d"),
ae4d0c03 5609 die->offset);
e8d05480
JB
5610 return;
5611 }
c906108c
SS
5612
5613 lowpc += baseaddr;
5614 highpc += baseaddr;
5615
5fb290d7 5616 /* Record the function range for dwarf_decode_lines. */
e142c38c 5617 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5618
34eaf542
TT
5619 /* If we have any template arguments, then we must allocate a
5620 different sort of symbol. */
5621 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5622 {
5623 if (child_die->tag == DW_TAG_template_type_param
5624 || child_die->tag == DW_TAG_template_value_param)
5625 {
5626 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5627 struct template_symbol);
5628 templ_func->base.is_cplus_template_function = 1;
5629 break;
5630 }
5631 }
5632
c906108c 5633 new = push_context (0, lowpc);
34eaf542
TT
5634 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5635 (struct symbol *) templ_func);
4c2df51b 5636
4cecd739
DJ
5637 /* If there is a location expression for DW_AT_frame_base, record
5638 it. */
e142c38c 5639 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5640 if (attr)
c034e007
AC
5641 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5642 expression is being recorded directly in the function's symbol
5643 and not in a separate frame-base object. I guess this hack is
5644 to avoid adding some sort of frame-base adjunct/annex to the
5645 function's symbol :-(. The problem with doing this is that it
5646 results in a function symbol with a location expression that
5647 has nothing to do with the location of the function, ouch! The
5648 relationship should be: a function's symbol has-a frame base; a
5649 frame-base has-a location expression. */
e7c27a73 5650 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5651
e142c38c 5652 cu->list_in_scope = &local_symbols;
c906108c 5653
639d11d3 5654 if (die->child != NULL)
c906108c 5655 {
639d11d3 5656 child_die = die->child;
c906108c
SS
5657 while (child_die && child_die->tag)
5658 {
34eaf542
TT
5659 if (child_die->tag == DW_TAG_template_type_param
5660 || child_die->tag == DW_TAG_template_value_param)
5661 {
5662 struct symbol *arg = new_symbol (child_die, NULL, cu);
5663
f1078f66
DJ
5664 if (arg != NULL)
5665 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5666 }
5667 else
5668 process_die (child_die, cu);
c906108c
SS
5669 child_die = sibling_die (child_die);
5670 }
5671 }
5672
d389af10
JK
5673 inherit_abstract_dies (die, cu);
5674
4a811a97
UW
5675 /* If we have a DW_AT_specification, we might need to import using
5676 directives from the context of the specification DIE. See the
5677 comment in determine_prefix. */
5678 if (cu->language == language_cplus
5679 && dwarf2_attr (die, DW_AT_specification, cu))
5680 {
5681 struct dwarf2_cu *spec_cu = cu;
5682 struct die_info *spec_die = die_specification (die, &spec_cu);
5683
5684 while (spec_die)
5685 {
5686 child_die = spec_die->child;
5687 while (child_die && child_die->tag)
5688 {
5689 if (child_die->tag == DW_TAG_imported_module)
5690 process_die (child_die, spec_cu);
5691 child_die = sibling_die (child_die);
5692 }
5693
5694 /* In some cases, GCC generates specification DIEs that
5695 themselves contain DW_AT_specification attributes. */
5696 spec_die = die_specification (spec_die, &spec_cu);
5697 }
5698 }
5699
c906108c
SS
5700 new = pop_context ();
5701 /* Make a block for the local symbols within. */
801e3a5b
JB
5702 block = finish_block (new->name, &local_symbols, new->old_blocks,
5703 lowpc, highpc, objfile);
5704
df8a16a1 5705 /* For C++, set the block's scope. */
f55ee35c 5706 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5707 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5708 determine_prefix (die, cu),
df8a16a1
DJ
5709 processing_has_namespace_info);
5710
801e3a5b
JB
5711 /* If we have address ranges, record them. */
5712 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5713
34eaf542
TT
5714 /* Attach template arguments to function. */
5715 if (! VEC_empty (symbolp, template_args))
5716 {
5717 gdb_assert (templ_func != NULL);
5718
5719 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5720 templ_func->template_arguments
5721 = obstack_alloc (&objfile->objfile_obstack,
5722 (templ_func->n_template_arguments
5723 * sizeof (struct symbol *)));
5724 memcpy (templ_func->template_arguments,
5725 VEC_address (symbolp, template_args),
5726 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5727 VEC_free (symbolp, template_args);
5728 }
5729
208d8187
JB
5730 /* In C++, we can have functions nested inside functions (e.g., when
5731 a function declares a class that has methods). This means that
5732 when we finish processing a function scope, we may need to go
5733 back to building a containing block's symbol lists. */
5734 local_symbols = new->locals;
5735 param_symbols = new->params;
27aa8d6a 5736 using_directives = new->using_directives;
208d8187 5737
921e78cf
JB
5738 /* If we've finished processing a top-level function, subsequent
5739 symbols go in the file symbol list. */
5740 if (outermost_context_p ())
e142c38c 5741 cu->list_in_scope = &file_symbols;
c906108c
SS
5742}
5743
5744/* Process all the DIES contained within a lexical block scope. Start
5745 a new scope, process the dies, and then close the scope. */
5746
5747static void
e7c27a73 5748read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5749{
e7c27a73 5750 struct objfile *objfile = cu->objfile;
52f0bd74 5751 struct context_stack *new;
c906108c
SS
5752 CORE_ADDR lowpc, highpc;
5753 struct die_info *child_die;
e142c38c
DJ
5754 CORE_ADDR baseaddr;
5755
5756 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5757
5758 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5759 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5760 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5761 be nasty. Might be easier to properly extend generic blocks to
af34e669 5762 describe ranges. */
d85a05f0 5763 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5764 return;
5765 lowpc += baseaddr;
5766 highpc += baseaddr;
5767
5768 push_context (0, lowpc);
639d11d3 5769 if (die->child != NULL)
c906108c 5770 {
639d11d3 5771 child_die = die->child;
c906108c
SS
5772 while (child_die && child_die->tag)
5773 {
e7c27a73 5774 process_die (child_die, cu);
c906108c
SS
5775 child_die = sibling_die (child_die);
5776 }
5777 }
5778 new = pop_context ();
5779
8540c487 5780 if (local_symbols != NULL || using_directives != NULL)
c906108c 5781 {
801e3a5b
JB
5782 struct block *block
5783 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5784 highpc, objfile);
5785
5786 /* Note that recording ranges after traversing children, as we
5787 do here, means that recording a parent's ranges entails
5788 walking across all its children's ranges as they appear in
5789 the address map, which is quadratic behavior.
5790
5791 It would be nicer to record the parent's ranges before
5792 traversing its children, simply overriding whatever you find
5793 there. But since we don't even decide whether to create a
5794 block until after we've traversed its children, that's hard
5795 to do. */
5796 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5797 }
5798 local_symbols = new->locals;
27aa8d6a 5799 using_directives = new->using_directives;
c906108c
SS
5800}
5801
43039443 5802/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5803 Return 1 if the attributes are present and valid, otherwise, return 0.
5804 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5805
5806static int
5807dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5808 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5809 struct partial_symtab *ranges_pst)
43039443
JK
5810{
5811 struct objfile *objfile = cu->objfile;
5812 struct comp_unit_head *cu_header = &cu->header;
5813 bfd *obfd = objfile->obfd;
5814 unsigned int addr_size = cu_header->addr_size;
5815 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5816 /* Base address selection entry. */
5817 CORE_ADDR base;
5818 int found_base;
5819 unsigned int dummy;
5820 gdb_byte *buffer;
5821 CORE_ADDR marker;
5822 int low_set;
5823 CORE_ADDR low = 0;
5824 CORE_ADDR high = 0;
ff013f42 5825 CORE_ADDR baseaddr;
43039443 5826
d00adf39
DE
5827 found_base = cu->base_known;
5828 base = cu->base_address;
43039443 5829
be391dca 5830 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5831 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5832 {
5833 complaint (&symfile_complaints,
5834 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5835 offset);
5836 return 0;
5837 }
dce234bc 5838 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5839
5840 /* Read in the largest possible address. */
5841 marker = read_address (obfd, buffer, cu, &dummy);
5842 if ((marker & mask) == mask)
5843 {
5844 /* If we found the largest possible address, then
5845 read the base address. */
5846 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5847 buffer += 2 * addr_size;
5848 offset += 2 * addr_size;
5849 found_base = 1;
5850 }
5851
5852 low_set = 0;
5853
e7030f15 5854 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5855
43039443
JK
5856 while (1)
5857 {
5858 CORE_ADDR range_beginning, range_end;
5859
5860 range_beginning = read_address (obfd, buffer, cu, &dummy);
5861 buffer += addr_size;
5862 range_end = read_address (obfd, buffer, cu, &dummy);
5863 buffer += addr_size;
5864 offset += 2 * addr_size;
5865
5866 /* An end of list marker is a pair of zero addresses. */
5867 if (range_beginning == 0 && range_end == 0)
5868 /* Found the end of list entry. */
5869 break;
5870
5871 /* Each base address selection entry is a pair of 2 values.
5872 The first is the largest possible address, the second is
5873 the base address. Check for a base address here. */
5874 if ((range_beginning & mask) == mask)
5875 {
5876 /* If we found the largest possible address, then
5877 read the base address. */
5878 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5879 found_base = 1;
5880 continue;
5881 }
5882
5883 if (!found_base)
5884 {
5885 /* We have no valid base address for the ranges
5886 data. */
5887 complaint (&symfile_complaints,
5888 _("Invalid .debug_ranges data (no base address)"));
5889 return 0;
5890 }
5891
9277c30c
UW
5892 if (range_beginning > range_end)
5893 {
5894 /* Inverted range entries are invalid. */
5895 complaint (&symfile_complaints,
5896 _("Invalid .debug_ranges data (inverted range)"));
5897 return 0;
5898 }
5899
5900 /* Empty range entries have no effect. */
5901 if (range_beginning == range_end)
5902 continue;
5903
43039443
JK
5904 range_beginning += base;
5905 range_end += base;
5906
9277c30c 5907 if (ranges_pst != NULL)
ff013f42 5908 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
5909 range_beginning + baseaddr,
5910 range_end - 1 + baseaddr,
ff013f42
JK
5911 ranges_pst);
5912
43039443
JK
5913 /* FIXME: This is recording everything as a low-high
5914 segment of consecutive addresses. We should have a
5915 data structure for discontiguous block ranges
5916 instead. */
5917 if (! low_set)
5918 {
5919 low = range_beginning;
5920 high = range_end;
5921 low_set = 1;
5922 }
5923 else
5924 {
5925 if (range_beginning < low)
5926 low = range_beginning;
5927 if (range_end > high)
5928 high = range_end;
5929 }
5930 }
5931
5932 if (! low_set)
5933 /* If the first entry is an end-of-list marker, the range
5934 describes an empty scope, i.e. no instructions. */
5935 return 0;
5936
5937 if (low_return)
5938 *low_return = low;
5939 if (high_return)
5940 *high_return = high;
5941 return 1;
5942}
5943
af34e669
DJ
5944/* Get low and high pc attributes from a die. Return 1 if the attributes
5945 are present and valid, otherwise, return 0. Return -1 if the range is
5946 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5947static int
af34e669 5948dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5949 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5950 struct partial_symtab *pst)
c906108c
SS
5951{
5952 struct attribute *attr;
af34e669
DJ
5953 CORE_ADDR low = 0;
5954 CORE_ADDR high = 0;
5955 int ret = 0;
c906108c 5956
e142c38c 5957 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5958 if (attr)
af34e669
DJ
5959 {
5960 high = DW_ADDR (attr);
e142c38c 5961 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5962 if (attr)
5963 low = DW_ADDR (attr);
5964 else
5965 /* Found high w/o low attribute. */
5966 return 0;
5967
5968 /* Found consecutive range of addresses. */
5969 ret = 1;
5970 }
c906108c 5971 else
af34e669 5972 {
e142c38c 5973 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5974 if (attr != NULL)
5975 {
af34e669 5976 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5977 .debug_ranges section. */
d85a05f0 5978 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5979 return 0;
43039443 5980 /* Found discontinuous range of addresses. */
af34e669
DJ
5981 ret = -1;
5982 }
5983 }
c906108c 5984
9373cf26
JK
5985 /* read_partial_die has also the strict LOW < HIGH requirement. */
5986 if (high <= low)
c906108c
SS
5987 return 0;
5988
5989 /* When using the GNU linker, .gnu.linkonce. sections are used to
5990 eliminate duplicate copies of functions and vtables and such.
5991 The linker will arbitrarily choose one and discard the others.
5992 The AT_*_pc values for such functions refer to local labels in
5993 these sections. If the section from that file was discarded, the
5994 labels are not in the output, so the relocs get a value of 0.
5995 If this is a discarded function, mark the pc bounds as invalid,
5996 so that GDB will ignore it. */
72dca2f5 5997 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5998 return 0;
5999
6000 *lowpc = low;
6001 *highpc = high;
af34e669 6002 return ret;
c906108c
SS
6003}
6004
b084d499
JB
6005/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6006 its low and high PC addresses. Do nothing if these addresses could not
6007 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6008 and HIGHPC to the high address if greater than HIGHPC. */
6009
6010static void
6011dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6012 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6013 struct dwarf2_cu *cu)
6014{
6015 CORE_ADDR low, high;
6016 struct die_info *child = die->child;
6017
d85a05f0 6018 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6019 {
6020 *lowpc = min (*lowpc, low);
6021 *highpc = max (*highpc, high);
6022 }
6023
6024 /* If the language does not allow nested subprograms (either inside
6025 subprograms or lexical blocks), we're done. */
6026 if (cu->language != language_ada)
6027 return;
6e70227d 6028
b084d499
JB
6029 /* Check all the children of the given DIE. If it contains nested
6030 subprograms, then check their pc bounds. Likewise, we need to
6031 check lexical blocks as well, as they may also contain subprogram
6032 definitions. */
6033 while (child && child->tag)
6034 {
6035 if (child->tag == DW_TAG_subprogram
6036 || child->tag == DW_TAG_lexical_block)
6037 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6038 child = sibling_die (child);
6039 }
6040}
6041
fae299cd
DC
6042/* Get the low and high pc's represented by the scope DIE, and store
6043 them in *LOWPC and *HIGHPC. If the correct values can't be
6044 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6045
6046static void
6047get_scope_pc_bounds (struct die_info *die,
6048 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6049 struct dwarf2_cu *cu)
6050{
6051 CORE_ADDR best_low = (CORE_ADDR) -1;
6052 CORE_ADDR best_high = (CORE_ADDR) 0;
6053 CORE_ADDR current_low, current_high;
6054
d85a05f0 6055 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6056 {
6057 best_low = current_low;
6058 best_high = current_high;
6059 }
6060 else
6061 {
6062 struct die_info *child = die->child;
6063
6064 while (child && child->tag)
6065 {
6066 switch (child->tag) {
6067 case DW_TAG_subprogram:
b084d499 6068 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6069 break;
6070 case DW_TAG_namespace:
f55ee35c 6071 case DW_TAG_module:
fae299cd
DC
6072 /* FIXME: carlton/2004-01-16: Should we do this for
6073 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6074 that current GCC's always emit the DIEs corresponding
6075 to definitions of methods of classes as children of a
6076 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6077 the DIEs giving the declarations, which could be
6078 anywhere). But I don't see any reason why the
6079 standards says that they have to be there. */
6080 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6081
6082 if (current_low != ((CORE_ADDR) -1))
6083 {
6084 best_low = min (best_low, current_low);
6085 best_high = max (best_high, current_high);
6086 }
6087 break;
6088 default:
0963b4bd 6089 /* Ignore. */
fae299cd
DC
6090 break;
6091 }
6092
6093 child = sibling_die (child);
6094 }
6095 }
6096
6097 *lowpc = best_low;
6098 *highpc = best_high;
6099}
6100
801e3a5b
JB
6101/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6102 in DIE. */
6103static void
6104dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6105 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6106{
6107 struct attribute *attr;
6108
6109 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6110 if (attr)
6111 {
6112 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6113
801e3a5b
JB
6114 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6115 if (attr)
6116 {
6117 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6118
801e3a5b
JB
6119 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6120 }
6121 }
6122
6123 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6124 if (attr)
6125 {
6126 bfd *obfd = cu->objfile->obfd;
6127
6128 /* The value of the DW_AT_ranges attribute is the offset of the
6129 address range list in the .debug_ranges section. */
6130 unsigned long offset = DW_UNSND (attr);
dce234bc 6131 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6132
6133 /* For some target architectures, but not others, the
6134 read_address function sign-extends the addresses it returns.
6135 To recognize base address selection entries, we need a
6136 mask. */
6137 unsigned int addr_size = cu->header.addr_size;
6138 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6139
6140 /* The base address, to which the next pair is relative. Note
6141 that this 'base' is a DWARF concept: most entries in a range
6142 list are relative, to reduce the number of relocs against the
6143 debugging information. This is separate from this function's
6144 'baseaddr' argument, which GDB uses to relocate debugging
6145 information from a shared library based on the address at
6146 which the library was loaded. */
d00adf39
DE
6147 CORE_ADDR base = cu->base_address;
6148 int base_known = cu->base_known;
801e3a5b 6149
be391dca 6150 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6151 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6152 {
6153 complaint (&symfile_complaints,
6154 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6155 offset);
6156 return;
6157 }
6158
6159 for (;;)
6160 {
6161 unsigned int bytes_read;
6162 CORE_ADDR start, end;
6163
6164 start = read_address (obfd, buffer, cu, &bytes_read);
6165 buffer += bytes_read;
6166 end = read_address (obfd, buffer, cu, &bytes_read);
6167 buffer += bytes_read;
6168
6169 /* Did we find the end of the range list? */
6170 if (start == 0 && end == 0)
6171 break;
6172
6173 /* Did we find a base address selection entry? */
6174 else if ((start & base_select_mask) == base_select_mask)
6175 {
6176 base = end;
6177 base_known = 1;
6178 }
6179
6180 /* We found an ordinary address range. */
6181 else
6182 {
6183 if (!base_known)
6184 {
6185 complaint (&symfile_complaints,
3e43a32a
MS
6186 _("Invalid .debug_ranges data "
6187 "(no base address)"));
801e3a5b
JB
6188 return;
6189 }
6190
9277c30c
UW
6191 if (start > end)
6192 {
6193 /* Inverted range entries are invalid. */
6194 complaint (&symfile_complaints,
6195 _("Invalid .debug_ranges data "
6196 "(inverted range)"));
6197 return;
6198 }
6199
6200 /* Empty range entries have no effect. */
6201 if (start == end)
6202 continue;
6203
6e70227d
DE
6204 record_block_range (block,
6205 baseaddr + base + start,
801e3a5b
JB
6206 baseaddr + base + end - 1);
6207 }
6208 }
6209 }
6210}
6211
c906108c
SS
6212/* Add an aggregate field to the field list. */
6213
6214static void
107d2387 6215dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6216 struct dwarf2_cu *cu)
6e70227d 6217{
e7c27a73 6218 struct objfile *objfile = cu->objfile;
5e2b427d 6219 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6220 struct nextfield *new_field;
6221 struct attribute *attr;
6222 struct field *fp;
6223 char *fieldname = "";
6224
6225 /* Allocate a new field list entry and link it in. */
6226 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6227 make_cleanup (xfree, new_field);
c906108c 6228 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6229
6230 if (die->tag == DW_TAG_inheritance)
6231 {
6232 new_field->next = fip->baseclasses;
6233 fip->baseclasses = new_field;
6234 }
6235 else
6236 {
6237 new_field->next = fip->fields;
6238 fip->fields = new_field;
6239 }
c906108c
SS
6240 fip->nfields++;
6241
29e0eb9c
JK
6242 /* Handle accessibility and virtuality of field.
6243 The default accessibility for members is public, the default
6244 accessibility for inheritance is private. */
6245 if (die->tag != DW_TAG_inheritance)
6246 new_field->accessibility = DW_ACCESS_public;
c906108c 6247 else
29e0eb9c 6248 new_field->accessibility = DW_ACCESS_private;
c906108c
SS
6249 new_field->virtuality = DW_VIRTUALITY_none;
6250
e142c38c 6251 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6252 if (attr)
6253 new_field->accessibility = DW_UNSND (attr);
6254 if (new_field->accessibility != DW_ACCESS_public)
6255 fip->non_public_fields = 1;
e142c38c 6256 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6257 if (attr)
6258 new_field->virtuality = DW_UNSND (attr);
6259
6260 fp = &new_field->field;
a9a9bd0f 6261
e142c38c 6262 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6263 {
a9a9bd0f 6264 /* Data member other than a C++ static data member. */
6e70227d 6265
c906108c 6266 /* Get type of field. */
e7c27a73 6267 fp->type = die_type (die, cu);
c906108c 6268
d6a843b5 6269 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6270
c906108c 6271 /* Get bit size of field (zero if none). */
e142c38c 6272 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6273 if (attr)
6274 {
6275 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6276 }
6277 else
6278 {
6279 FIELD_BITSIZE (*fp) = 0;
6280 }
6281
6282 /* Get bit offset of field. */
e142c38c 6283 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
6284 if (attr)
6285 {
d4b96c9a 6286 int byte_offset = 0;
c6a0999f 6287
3690dd37 6288 if (attr_form_is_section_offset (attr))
d4b96c9a 6289 dwarf2_complex_location_expr_complaint ();
3690dd37 6290 else if (attr_form_is_constant (attr))
c6a0999f 6291 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 6292 else if (attr_form_is_block (attr))
c6a0999f 6293 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
6294 else
6295 dwarf2_complex_location_expr_complaint ();
c6a0999f 6296
d6a843b5 6297 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 6298 }
e142c38c 6299 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6300 if (attr)
6301 {
5e2b427d 6302 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6303 {
6304 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6305 additional bit offset from the MSB of the containing
6306 anonymous object to the MSB of the field. We don't
6307 have to do anything special since we don't need to
6308 know the size of the anonymous object. */
c906108c
SS
6309 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6310 }
6311 else
6312 {
6313 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6314 MSB of the anonymous object, subtract off the number of
6315 bits from the MSB of the field to the MSB of the
6316 object, and then subtract off the number of bits of
6317 the field itself. The result is the bit offset of
6318 the LSB of the field. */
c906108c
SS
6319 int anonymous_size;
6320 int bit_offset = DW_UNSND (attr);
6321
e142c38c 6322 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6323 if (attr)
6324 {
6325 /* The size of the anonymous object containing
6326 the bit field is explicit, so use the
6327 indicated size (in bytes). */
6328 anonymous_size = DW_UNSND (attr);
6329 }
6330 else
6331 {
6332 /* The size of the anonymous object containing
6333 the bit field must be inferred from the type
6334 attribute of the data member containing the
6335 bit field. */
6336 anonymous_size = TYPE_LENGTH (fp->type);
6337 }
6338 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6339 - bit_offset - FIELD_BITSIZE (*fp);
6340 }
6341 }
6342
6343 /* Get name of field. */
39cbfefa
DJ
6344 fieldname = dwarf2_name (die, cu);
6345 if (fieldname == NULL)
6346 fieldname = "";
d8151005
DJ
6347
6348 /* The name is already allocated along with this objfile, so we don't
6349 need to duplicate it for the type. */
6350 fp->name = fieldname;
c906108c
SS
6351
6352 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6353 pointer or virtual base class pointer) to private. */
e142c38c 6354 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6355 {
d48cc9dd 6356 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6357 new_field->accessibility = DW_ACCESS_private;
6358 fip->non_public_fields = 1;
6359 }
6360 }
a9a9bd0f 6361 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6362 {
a9a9bd0f
DC
6363 /* C++ static member. */
6364
6365 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6366 is a declaration, but all versions of G++ as of this writing
6367 (so through at least 3.2.1) incorrectly generate
6368 DW_TAG_variable tags. */
6e70227d 6369
c906108c 6370 char *physname;
c906108c 6371
a9a9bd0f 6372 /* Get name of field. */
39cbfefa
DJ
6373 fieldname = dwarf2_name (die, cu);
6374 if (fieldname == NULL)
c906108c
SS
6375 return;
6376
254e6b9e 6377 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6378 if (attr
6379 /* Only create a symbol if this is an external value.
6380 new_symbol checks this and puts the value in the global symbol
6381 table, which we want. If it is not external, new_symbol
6382 will try to put the value in cu->list_in_scope which is wrong. */
6383 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6384 {
6385 /* A static const member, not much different than an enum as far as
6386 we're concerned, except that we can support more types. */
6387 new_symbol (die, NULL, cu);
6388 }
6389
2df3850c 6390 /* Get physical name. */
94af9270 6391 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 6392
d8151005
DJ
6393 /* The name is already allocated along with this objfile, so we don't
6394 need to duplicate it for the type. */
6395 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6396 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6397 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6398 }
6399 else if (die->tag == DW_TAG_inheritance)
6400 {
6401 /* C++ base class field. */
e142c38c 6402 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 6403 if (attr)
d4b96c9a
JK
6404 {
6405 int byte_offset = 0;
6406
6407 if (attr_form_is_section_offset (attr))
6408 dwarf2_complex_location_expr_complaint ();
6409 else if (attr_form_is_constant (attr))
6410 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6411 else if (attr_form_is_block (attr))
6412 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6413 else
6414 dwarf2_complex_location_expr_complaint ();
6415
6416 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6417 }
c906108c 6418 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6419 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6420 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6421 fip->nbaseclasses++;
6422 }
6423}
6424
98751a41
JK
6425/* Add a typedef defined in the scope of the FIP's class. */
6426
6427static void
6428dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6429 struct dwarf2_cu *cu)
6e70227d 6430{
98751a41 6431 struct objfile *objfile = cu->objfile;
98751a41
JK
6432 struct typedef_field_list *new_field;
6433 struct attribute *attr;
6434 struct typedef_field *fp;
6435 char *fieldname = "";
6436
6437 /* Allocate a new field list entry and link it in. */
6438 new_field = xzalloc (sizeof (*new_field));
6439 make_cleanup (xfree, new_field);
6440
6441 gdb_assert (die->tag == DW_TAG_typedef);
6442
6443 fp = &new_field->field;
6444
6445 /* Get name of field. */
6446 fp->name = dwarf2_name (die, cu);
6447 if (fp->name == NULL)
6448 return;
6449
6450 fp->type = read_type_die (die, cu);
6451
6452 new_field->next = fip->typedef_field_list;
6453 fip->typedef_field_list = new_field;
6454 fip->typedef_field_list_count++;
6455}
6456
c906108c
SS
6457/* Create the vector of fields, and attach it to the type. */
6458
6459static void
fba45db2 6460dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6461 struct dwarf2_cu *cu)
c906108c
SS
6462{
6463 int nfields = fip->nfields;
6464
6465 /* Record the field count, allocate space for the array of fields,
6466 and create blank accessibility bitfields if necessary. */
6467 TYPE_NFIELDS (type) = nfields;
6468 TYPE_FIELDS (type) = (struct field *)
6469 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6470 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6471
b4ba55a1 6472 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6473 {
6474 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6475
6476 TYPE_FIELD_PRIVATE_BITS (type) =
6477 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6478 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6479
6480 TYPE_FIELD_PROTECTED_BITS (type) =
6481 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6482 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6483
774b6a14
TT
6484 TYPE_FIELD_IGNORE_BITS (type) =
6485 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6486 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6487 }
6488
6489 /* If the type has baseclasses, allocate and clear a bit vector for
6490 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6491 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6492 {
6493 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6494 unsigned char *pointer;
c906108c
SS
6495
6496 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6497 pointer = TYPE_ALLOC (type, num_bytes);
6498 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6499 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6500 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6501 }
6502
3e43a32a
MS
6503 /* Copy the saved-up fields into the field vector. Start from the head of
6504 the list, adding to the tail of the field array, so that they end up in
6505 the same order in the array in which they were added to the list. */
c906108c
SS
6506 while (nfields-- > 0)
6507 {
7d0ccb61
DJ
6508 struct nextfield *fieldp;
6509
6510 if (fip->fields)
6511 {
6512 fieldp = fip->fields;
6513 fip->fields = fieldp->next;
6514 }
6515 else
6516 {
6517 fieldp = fip->baseclasses;
6518 fip->baseclasses = fieldp->next;
6519 }
6520
6521 TYPE_FIELD (type, nfields) = fieldp->field;
6522 switch (fieldp->accessibility)
c906108c 6523 {
c5aa993b 6524 case DW_ACCESS_private:
b4ba55a1
JB
6525 if (cu->language != language_ada)
6526 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6527 break;
c906108c 6528
c5aa993b 6529 case DW_ACCESS_protected:
b4ba55a1
JB
6530 if (cu->language != language_ada)
6531 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6532 break;
c906108c 6533
c5aa993b
JM
6534 case DW_ACCESS_public:
6535 break;
c906108c 6536
c5aa993b
JM
6537 default:
6538 /* Unknown accessibility. Complain and treat it as public. */
6539 {
e2e0b3e5 6540 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6541 fieldp->accessibility);
c5aa993b
JM
6542 }
6543 break;
c906108c
SS
6544 }
6545 if (nfields < fip->nbaseclasses)
6546 {
7d0ccb61 6547 switch (fieldp->virtuality)
c906108c 6548 {
c5aa993b
JM
6549 case DW_VIRTUALITY_virtual:
6550 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6551 if (cu->language == language_ada)
a73c6dcd 6552 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6553 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6554 break;
c906108c
SS
6555 }
6556 }
c906108c
SS
6557 }
6558}
6559
c906108c
SS
6560/* Add a member function to the proper fieldlist. */
6561
6562static void
107d2387 6563dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6564 struct type *type, struct dwarf2_cu *cu)
c906108c 6565{
e7c27a73 6566 struct objfile *objfile = cu->objfile;
c906108c
SS
6567 struct attribute *attr;
6568 struct fnfieldlist *flp;
6569 int i;
6570 struct fn_field *fnp;
6571 char *fieldname;
c906108c 6572 struct nextfnfield *new_fnfield;
f792889a 6573 struct type *this_type;
c906108c 6574
b4ba55a1 6575 if (cu->language == language_ada)
a73c6dcd 6576 error (_("unexpected member function in Ada type"));
b4ba55a1 6577
2df3850c 6578 /* Get name of member function. */
39cbfefa
DJ
6579 fieldname = dwarf2_name (die, cu);
6580 if (fieldname == NULL)
2df3850c 6581 return;
c906108c 6582
c906108c
SS
6583 /* Look up member function name in fieldlist. */
6584 for (i = 0; i < fip->nfnfields; i++)
6585 {
27bfe10e 6586 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6587 break;
6588 }
6589
6590 /* Create new list element if necessary. */
6591 if (i < fip->nfnfields)
6592 flp = &fip->fnfieldlists[i];
6593 else
6594 {
6595 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6596 {
6597 fip->fnfieldlists = (struct fnfieldlist *)
6598 xrealloc (fip->fnfieldlists,
6599 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6600 * sizeof (struct fnfieldlist));
c906108c 6601 if (fip->nfnfields == 0)
c13c43fd 6602 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6603 }
6604 flp = &fip->fnfieldlists[fip->nfnfields];
6605 flp->name = fieldname;
6606 flp->length = 0;
6607 flp->head = NULL;
3da10d80 6608 i = fip->nfnfields++;
c906108c
SS
6609 }
6610
6611 /* Create a new member function field and chain it to the field list
0963b4bd 6612 entry. */
c906108c 6613 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6614 make_cleanup (xfree, new_fnfield);
c906108c
SS
6615 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6616 new_fnfield->next = flp->head;
6617 flp->head = new_fnfield;
6618 flp->length++;
6619
6620 /* Fill in the member function field info. */
6621 fnp = &new_fnfield->fnfield;
3da10d80
KS
6622
6623 /* Delay processing of the physname until later. */
6624 if (cu->language == language_cplus || cu->language == language_java)
6625 {
6626 add_to_method_list (type, i, flp->length - 1, fieldname,
6627 die, cu);
6628 }
6629 else
6630 {
6631 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6632 fnp->physname = physname ? physname : "";
6633 }
6634
c906108c 6635 fnp->type = alloc_type (objfile);
f792889a
DJ
6636 this_type = read_type_die (die, cu);
6637 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6638 {
f792889a 6639 int nparams = TYPE_NFIELDS (this_type);
c906108c 6640
f792889a 6641 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6642 of the method itself (TYPE_CODE_METHOD). */
6643 smash_to_method_type (fnp->type, type,
f792889a
DJ
6644 TYPE_TARGET_TYPE (this_type),
6645 TYPE_FIELDS (this_type),
6646 TYPE_NFIELDS (this_type),
6647 TYPE_VARARGS (this_type));
c906108c
SS
6648
6649 /* Handle static member functions.
c5aa993b 6650 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
6651 member functions. G++ helps GDB by marking the first
6652 parameter for non-static member functions (which is the this
6653 pointer) as artificial. We obtain this information from
6654 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6655 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6656 fnp->voffset = VOFFSET_STATIC;
6657 }
6658 else
e2e0b3e5 6659 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 6660 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
6661
6662 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6663 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6664 fnp->fcontext = die_containing_type (die, cu);
c906108c 6665
3e43a32a
MS
6666 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6667 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
6668
6669 /* Get accessibility. */
e142c38c 6670 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6671 if (attr)
6672 {
6673 switch (DW_UNSND (attr))
6674 {
c5aa993b
JM
6675 case DW_ACCESS_private:
6676 fnp->is_private = 1;
6677 break;
6678 case DW_ACCESS_protected:
6679 fnp->is_protected = 1;
6680 break;
c906108c
SS
6681 }
6682 }
6683
b02dede2 6684 /* Check for artificial methods. */
e142c38c 6685 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6686 if (attr && DW_UNSND (attr) != 0)
6687 fnp->is_artificial = 1;
6688
0d564a31 6689 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6690 function. For older versions of GCC, this is an offset in the
6691 appropriate virtual table, as specified by DW_AT_containing_type.
6692 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6693 to the object address. */
6694
e142c38c 6695 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6696 if (attr)
8e19ed76 6697 {
aec5aa8b 6698 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6699 {
aec5aa8b
TT
6700 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6701 {
6702 /* Old-style GCC. */
6703 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6704 }
6705 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6706 || (DW_BLOCK (attr)->size > 1
6707 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6708 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6709 {
6710 struct dwarf_block blk;
6711 int offset;
6712
6713 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6714 ? 1 : 2);
6715 blk.size = DW_BLOCK (attr)->size - offset;
6716 blk.data = DW_BLOCK (attr)->data + offset;
6717 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6718 if ((fnp->voffset % cu->header.addr_size) != 0)
6719 dwarf2_complex_location_expr_complaint ();
6720 else
6721 fnp->voffset /= cu->header.addr_size;
6722 fnp->voffset += 2;
6723 }
6724 else
6725 dwarf2_complex_location_expr_complaint ();
6726
6727 if (!fnp->fcontext)
6728 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6729 }
3690dd37 6730 else if (attr_form_is_section_offset (attr))
8e19ed76 6731 {
4d3c2250 6732 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6733 }
6734 else
6735 {
4d3c2250
KB
6736 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6737 fieldname);
8e19ed76 6738 }
0d564a31 6739 }
d48cc9dd
DJ
6740 else
6741 {
6742 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6743 if (attr && DW_UNSND (attr))
6744 {
6745 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6746 complaint (&symfile_complaints,
3e43a32a
MS
6747 _("Member function \"%s\" (offset %d) is virtual "
6748 "but the vtable offset is not specified"),
d48cc9dd 6749 fieldname, die->offset);
9655fd1a 6750 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6751 TYPE_CPLUS_DYNAMIC (type) = 1;
6752 }
6753 }
c906108c
SS
6754}
6755
6756/* Create the vector of member function fields, and attach it to the type. */
6757
6758static void
fba45db2 6759dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6760 struct dwarf2_cu *cu)
c906108c
SS
6761{
6762 struct fnfieldlist *flp;
6763 int total_length = 0;
6764 int i;
6765
b4ba55a1 6766 if (cu->language == language_ada)
a73c6dcd 6767 error (_("unexpected member functions in Ada type"));
b4ba55a1 6768
c906108c
SS
6769 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6770 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6771 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6772
6773 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6774 {
6775 struct nextfnfield *nfp = flp->head;
6776 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6777 int k;
6778
6779 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6780 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6781 fn_flp->fn_fields = (struct fn_field *)
6782 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6783 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6784 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6785
6786 total_length += flp->length;
6787 }
6788
6789 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6790 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6791}
6792
1168df01
JB
6793/* Returns non-zero if NAME is the name of a vtable member in CU's
6794 language, zero otherwise. */
6795static int
6796is_vtable_name (const char *name, struct dwarf2_cu *cu)
6797{
6798 static const char vptr[] = "_vptr";
987504bb 6799 static const char vtable[] = "vtable";
1168df01 6800
987504bb
JJ
6801 /* Look for the C++ and Java forms of the vtable. */
6802 if ((cu->language == language_java
6803 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6804 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6805 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6806 return 1;
6807
6808 return 0;
6809}
6810
c0dd20ea 6811/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6812 functions, with the ABI-specified layout. If TYPE describes
6813 such a structure, smash it into a member function type.
61049d3b
DJ
6814
6815 GCC shouldn't do this; it should just output pointer to member DIEs.
6816 This is GCC PR debug/28767. */
c0dd20ea 6817
0b92b5bb
TT
6818static void
6819quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6820{
0b92b5bb 6821 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6822
6823 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6824 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6825 return;
c0dd20ea
DJ
6826
6827 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6828 if (TYPE_FIELD_NAME (type, 0) == NULL
6829 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6830 || TYPE_FIELD_NAME (type, 1) == NULL
6831 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6832 return;
c0dd20ea
DJ
6833
6834 /* Find the type of the method. */
0b92b5bb 6835 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6836 if (pfn_type == NULL
6837 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6838 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6839 return;
c0dd20ea
DJ
6840
6841 /* Look for the "this" argument. */
6842 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6843 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6844 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6845 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6846 return;
c0dd20ea
DJ
6847
6848 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6849 new_type = alloc_type (objfile);
6850 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6851 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6852 TYPE_VARARGS (pfn_type));
0b92b5bb 6853 smash_to_methodptr_type (type, new_type);
c0dd20ea 6854}
1168df01 6855
c906108c 6856/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
6857 (definition) to create a type for the structure or union. Fill in
6858 the type's name and general properties; the members will not be
6859 processed until process_structure_type.
c906108c 6860
c767944b
DJ
6861 NOTE: we need to call these functions regardless of whether or not the
6862 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
6863 structure or union. This gets the type entered into our set of
6864 user defined types.
6865
6866 However, if the structure is incomplete (an opaque struct/union)
6867 then suppress creating a symbol table entry for it since gdb only
6868 wants to find the one with the complete definition. Note that if
6869 it is complete, we just call new_symbol, which does it's own
6870 checking about whether the struct/union is anonymous or not (and
6871 suppresses creating a symbol table entry itself). */
6872
f792889a 6873static struct type *
134d01f1 6874read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6875{
e7c27a73 6876 struct objfile *objfile = cu->objfile;
c906108c
SS
6877 struct type *type;
6878 struct attribute *attr;
39cbfefa 6879 char *name;
c906108c 6880
348e048f
DE
6881 /* If the definition of this type lives in .debug_types, read that type.
6882 Don't follow DW_AT_specification though, that will take us back up
6883 the chain and we want to go down. */
6884 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6885 if (attr)
6886 {
6887 struct dwarf2_cu *type_cu = cu;
6888 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6889
348e048f
DE
6890 /* We could just recurse on read_structure_type, but we need to call
6891 get_die_type to ensure only one type for this DIE is created.
6892 This is important, for example, because for c++ classes we need
6893 TYPE_NAME set which is only done by new_symbol. Blech. */
6894 type = read_type_die (type_die, type_cu);
9dc481d3
DE
6895
6896 /* TYPE_CU may not be the same as CU.
6897 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
6898 return set_die_type (die, type, cu);
6899 }
6900
c0dd20ea 6901 type = alloc_type (objfile);
c906108c 6902 INIT_CPLUS_SPECIFIC (type);
93311388 6903
39cbfefa
DJ
6904 name = dwarf2_name (die, cu);
6905 if (name != NULL)
c906108c 6906 {
987504bb
JJ
6907 if (cu->language == language_cplus
6908 || cu->language == language_java)
63d06c5c 6909 {
3da10d80
KS
6910 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6911
6912 /* dwarf2_full_name might have already finished building the DIE's
6913 type. If so, there is no need to continue. */
6914 if (get_die_type (die, cu) != NULL)
6915 return get_die_type (die, cu);
6916
6917 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
6918 if (die->tag == DW_TAG_structure_type
6919 || die->tag == DW_TAG_class_type)
6920 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6921 }
6922 else
6923 {
d8151005
DJ
6924 /* The name is already allocated along with this objfile, so
6925 we don't need to duplicate it for the type. */
94af9270
KS
6926 TYPE_TAG_NAME (type) = (char *) name;
6927 if (die->tag == DW_TAG_class_type)
6928 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6929 }
c906108c
SS
6930 }
6931
6932 if (die->tag == DW_TAG_structure_type)
6933 {
6934 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6935 }
6936 else if (die->tag == DW_TAG_union_type)
6937 {
6938 TYPE_CODE (type) = TYPE_CODE_UNION;
6939 }
6940 else
6941 {
c906108c
SS
6942 TYPE_CODE (type) = TYPE_CODE_CLASS;
6943 }
6944
0cc2414c
TT
6945 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6946 TYPE_DECLARED_CLASS (type) = 1;
6947
e142c38c 6948 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6949 if (attr)
6950 {
6951 TYPE_LENGTH (type) = DW_UNSND (attr);
6952 }
6953 else
6954 {
6955 TYPE_LENGTH (type) = 0;
6956 }
6957
876cecd0 6958 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6959 if (die_is_declaration (die, cu))
876cecd0 6960 TYPE_STUB (type) = 1;
a6c727b2
DJ
6961 else if (attr == NULL && die->child == NULL
6962 && producer_is_realview (cu->producer))
6963 /* RealView does not output the required DW_AT_declaration
6964 on incomplete types. */
6965 TYPE_STUB (type) = 1;
dc718098 6966
c906108c
SS
6967 /* We need to add the type field to the die immediately so we don't
6968 infinitely recurse when dealing with pointers to the structure
0963b4bd 6969 type within the structure itself. */
1c379e20 6970 set_die_type (die, type, cu);
c906108c 6971
7e314c57
JK
6972 /* set_die_type should be already done. */
6973 set_descriptive_type (type, die, cu);
6974
c767944b
DJ
6975 return type;
6976}
6977
6978/* Finish creating a structure or union type, including filling in
6979 its members and creating a symbol for it. */
6980
6981static void
6982process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6983{
6984 struct objfile *objfile = cu->objfile;
6985 struct die_info *child_die = die->child;
6986 struct type *type;
6987
6988 type = get_die_type (die, cu);
6989 if (type == NULL)
6990 type = read_structure_type (die, cu);
6991
e142c38c 6992 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6993 {
6994 struct field_info fi;
6995 struct die_info *child_die;
34eaf542 6996 VEC (symbolp) *template_args = NULL;
c767944b 6997 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
6998
6999 memset (&fi, 0, sizeof (struct field_info));
7000
639d11d3 7001 child_die = die->child;
c906108c
SS
7002
7003 while (child_die && child_die->tag)
7004 {
a9a9bd0f
DC
7005 if (child_die->tag == DW_TAG_member
7006 || child_die->tag == DW_TAG_variable)
c906108c 7007 {
a9a9bd0f
DC
7008 /* NOTE: carlton/2002-11-05: A C++ static data member
7009 should be a DW_TAG_member that is a declaration, but
7010 all versions of G++ as of this writing (so through at
7011 least 3.2.1) incorrectly generate DW_TAG_variable
7012 tags for them instead. */
e7c27a73 7013 dwarf2_add_field (&fi, child_die, cu);
c906108c 7014 }
8713b1b1 7015 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7016 {
0963b4bd 7017 /* C++ member function. */
e7c27a73 7018 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7019 }
7020 else if (child_die->tag == DW_TAG_inheritance)
7021 {
7022 /* C++ base class field. */
e7c27a73 7023 dwarf2_add_field (&fi, child_die, cu);
c906108c 7024 }
98751a41
JK
7025 else if (child_die->tag == DW_TAG_typedef)
7026 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7027 else if (child_die->tag == DW_TAG_template_type_param
7028 || child_die->tag == DW_TAG_template_value_param)
7029 {
7030 struct symbol *arg = new_symbol (child_die, NULL, cu);
7031
f1078f66
DJ
7032 if (arg != NULL)
7033 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7034 }
7035
c906108c
SS
7036 child_die = sibling_die (child_die);
7037 }
7038
34eaf542
TT
7039 /* Attach template arguments to type. */
7040 if (! VEC_empty (symbolp, template_args))
7041 {
7042 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7043 TYPE_N_TEMPLATE_ARGUMENTS (type)
7044 = VEC_length (symbolp, template_args);
7045 TYPE_TEMPLATE_ARGUMENTS (type)
7046 = obstack_alloc (&objfile->objfile_obstack,
7047 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7048 * sizeof (struct symbol *)));
7049 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7050 VEC_address (symbolp, template_args),
7051 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7052 * sizeof (struct symbol *)));
7053 VEC_free (symbolp, template_args);
7054 }
7055
c906108c
SS
7056 /* Attach fields and member functions to the type. */
7057 if (fi.nfields)
e7c27a73 7058 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7059 if (fi.nfnfields)
7060 {
e7c27a73 7061 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7062
c5aa993b 7063 /* Get the type which refers to the base class (possibly this
c906108c 7064 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7065 class from the DW_AT_containing_type attribute. This use of
7066 DW_AT_containing_type is a GNU extension. */
c906108c 7067
e142c38c 7068 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7069 {
e7c27a73 7070 struct type *t = die_containing_type (die, cu);
c906108c
SS
7071
7072 TYPE_VPTR_BASETYPE (type) = t;
7073 if (type == t)
7074 {
c906108c
SS
7075 int i;
7076
7077 /* Our own class provides vtbl ptr. */
7078 for (i = TYPE_NFIELDS (t) - 1;
7079 i >= TYPE_N_BASECLASSES (t);
7080 --i)
7081 {
7082 char *fieldname = TYPE_FIELD_NAME (t, i);
7083
1168df01 7084 if (is_vtable_name (fieldname, cu))
c906108c
SS
7085 {
7086 TYPE_VPTR_FIELDNO (type) = i;
7087 break;
7088 }
7089 }
7090
7091 /* Complain if virtual function table field not found. */
7092 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7093 complaint (&symfile_complaints,
3e43a32a
MS
7094 _("virtual function table pointer "
7095 "not found when defining class '%s'"),
4d3c2250
KB
7096 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7097 "");
c906108c
SS
7098 }
7099 else
7100 {
7101 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7102 }
7103 }
f6235d4c
EZ
7104 else if (cu->producer
7105 && strncmp (cu->producer,
7106 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7107 {
7108 /* The IBM XLC compiler does not provide direct indication
7109 of the containing type, but the vtable pointer is
7110 always named __vfp. */
7111
7112 int i;
7113
7114 for (i = TYPE_NFIELDS (type) - 1;
7115 i >= TYPE_N_BASECLASSES (type);
7116 --i)
7117 {
7118 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7119 {
7120 TYPE_VPTR_FIELDNO (type) = i;
7121 TYPE_VPTR_BASETYPE (type) = type;
7122 break;
7123 }
7124 }
7125 }
c906108c 7126 }
98751a41
JK
7127
7128 /* Copy fi.typedef_field_list linked list elements content into the
7129 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7130 if (fi.typedef_field_list)
7131 {
7132 int i = fi.typedef_field_list_count;
7133
a0d7a4ff 7134 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7135 TYPE_TYPEDEF_FIELD_ARRAY (type)
7136 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7137 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7138
7139 /* Reverse the list order to keep the debug info elements order. */
7140 while (--i >= 0)
7141 {
7142 struct typedef_field *dest, *src;
6e70227d 7143
98751a41
JK
7144 dest = &TYPE_TYPEDEF_FIELD (type, i);
7145 src = &fi.typedef_field_list->field;
7146 fi.typedef_field_list = fi.typedef_field_list->next;
7147 *dest = *src;
7148 }
7149 }
c767944b
DJ
7150
7151 do_cleanups (back_to);
c906108c 7152 }
63d06c5c 7153
0b92b5bb
TT
7154 quirk_gcc_member_function_pointer (type, cu->objfile);
7155
90aeadfc
DC
7156 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7157 snapshots) has been known to create a die giving a declaration
7158 for a class that has, as a child, a die giving a definition for a
7159 nested class. So we have to process our children even if the
7160 current die is a declaration. Normally, of course, a declaration
7161 won't have any children at all. */
134d01f1 7162
90aeadfc
DC
7163 while (child_die != NULL && child_die->tag)
7164 {
7165 if (child_die->tag == DW_TAG_member
7166 || child_die->tag == DW_TAG_variable
34eaf542
TT
7167 || child_die->tag == DW_TAG_inheritance
7168 || child_die->tag == DW_TAG_template_value_param
7169 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7170 {
90aeadfc 7171 /* Do nothing. */
134d01f1 7172 }
90aeadfc
DC
7173 else
7174 process_die (child_die, cu);
134d01f1 7175
90aeadfc 7176 child_die = sibling_die (child_die);
134d01f1
DJ
7177 }
7178
fa4028e9
JB
7179 /* Do not consider external references. According to the DWARF standard,
7180 these DIEs are identified by the fact that they have no byte_size
7181 attribute, and a declaration attribute. */
7182 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7183 || !die_is_declaration (die, cu))
c767944b 7184 new_symbol (die, type, cu);
134d01f1
DJ
7185}
7186
7187/* Given a DW_AT_enumeration_type die, set its type. We do not
7188 complete the type's fields yet, or create any symbols. */
c906108c 7189
f792889a 7190static struct type *
134d01f1 7191read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7192{
e7c27a73 7193 struct objfile *objfile = cu->objfile;
c906108c 7194 struct type *type;
c906108c 7195 struct attribute *attr;
0114d602 7196 const char *name;
134d01f1 7197
348e048f
DE
7198 /* If the definition of this type lives in .debug_types, read that type.
7199 Don't follow DW_AT_specification though, that will take us back up
7200 the chain and we want to go down. */
7201 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7202 if (attr)
7203 {
7204 struct dwarf2_cu *type_cu = cu;
7205 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7206
348e048f 7207 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7208
7209 /* TYPE_CU may not be the same as CU.
7210 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7211 return set_die_type (die, type, cu);
7212 }
7213
c906108c
SS
7214 type = alloc_type (objfile);
7215
7216 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7217 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7218 if (name != NULL)
0114d602 7219 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7220
e142c38c 7221 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7222 if (attr)
7223 {
7224 TYPE_LENGTH (type) = DW_UNSND (attr);
7225 }
7226 else
7227 {
7228 TYPE_LENGTH (type) = 0;
7229 }
7230
137033e9
JB
7231 /* The enumeration DIE can be incomplete. In Ada, any type can be
7232 declared as private in the package spec, and then defined only
7233 inside the package body. Such types are known as Taft Amendment
7234 Types. When another package uses such a type, an incomplete DIE
7235 may be generated by the compiler. */
02eb380e 7236 if (die_is_declaration (die, cu))
876cecd0 7237 TYPE_STUB (type) = 1;
02eb380e 7238
f792889a 7239 return set_die_type (die, type, cu);
134d01f1
DJ
7240}
7241
7242/* Given a pointer to a die which begins an enumeration, process all
7243 the dies that define the members of the enumeration, and create the
7244 symbol for the enumeration type.
7245
7246 NOTE: We reverse the order of the element list. */
7247
7248static void
7249process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7250{
f792889a 7251 struct type *this_type;
134d01f1 7252
f792889a
DJ
7253 this_type = get_die_type (die, cu);
7254 if (this_type == NULL)
7255 this_type = read_enumeration_type (die, cu);
9dc481d3 7256
639d11d3 7257 if (die->child != NULL)
c906108c 7258 {
9dc481d3
DE
7259 struct die_info *child_die;
7260 struct symbol *sym;
7261 struct field *fields = NULL;
7262 int num_fields = 0;
7263 int unsigned_enum = 1;
7264 char *name;
7265
639d11d3 7266 child_die = die->child;
c906108c
SS
7267 while (child_die && child_die->tag)
7268 {
7269 if (child_die->tag != DW_TAG_enumerator)
7270 {
e7c27a73 7271 process_die (child_die, cu);
c906108c
SS
7272 }
7273 else
7274 {
39cbfefa
DJ
7275 name = dwarf2_name (child_die, cu);
7276 if (name)
c906108c 7277 {
f792889a 7278 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7279 if (SYMBOL_VALUE (sym) < 0)
7280 unsigned_enum = 0;
7281
7282 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7283 {
7284 fields = (struct field *)
7285 xrealloc (fields,
7286 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7287 * sizeof (struct field));
c906108c
SS
7288 }
7289
3567439c 7290 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7291 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7292 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7293 FIELD_BITSIZE (fields[num_fields]) = 0;
7294
7295 num_fields++;
7296 }
7297 }
7298
7299 child_die = sibling_die (child_die);
7300 }
7301
7302 if (num_fields)
7303 {
f792889a
DJ
7304 TYPE_NFIELDS (this_type) = num_fields;
7305 TYPE_FIELDS (this_type) = (struct field *)
7306 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7307 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7308 sizeof (struct field) * num_fields);
b8c9b27d 7309 xfree (fields);
c906108c
SS
7310 }
7311 if (unsigned_enum)
876cecd0 7312 TYPE_UNSIGNED (this_type) = 1;
c906108c 7313 }
134d01f1 7314
f792889a 7315 new_symbol (die, this_type, cu);
c906108c
SS
7316}
7317
7318/* Extract all information from a DW_TAG_array_type DIE and put it in
7319 the DIE's type field. For now, this only handles one dimensional
7320 arrays. */
7321
f792889a 7322static struct type *
e7c27a73 7323read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7324{
e7c27a73 7325 struct objfile *objfile = cu->objfile;
c906108c 7326 struct die_info *child_die;
7e314c57 7327 struct type *type;
c906108c
SS
7328 struct type *element_type, *range_type, *index_type;
7329 struct type **range_types = NULL;
7330 struct attribute *attr;
7331 int ndim = 0;
7332 struct cleanup *back_to;
39cbfefa 7333 char *name;
c906108c 7334
e7c27a73 7335 element_type = die_type (die, cu);
c906108c 7336
7e314c57
JK
7337 /* The die_type call above may have already set the type for this DIE. */
7338 type = get_die_type (die, cu);
7339 if (type)
7340 return type;
7341
c906108c
SS
7342 /* Irix 6.2 native cc creates array types without children for
7343 arrays with unspecified length. */
639d11d3 7344 if (die->child == NULL)
c906108c 7345 {
46bf5051 7346 index_type = objfile_type (objfile)->builtin_int;
c906108c 7347 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7348 type = create_array_type (NULL, element_type, range_type);
7349 return set_die_type (die, type, cu);
c906108c
SS
7350 }
7351
7352 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7353 child_die = die->child;
c906108c
SS
7354 while (child_die && child_die->tag)
7355 {
7356 if (child_die->tag == DW_TAG_subrange_type)
7357 {
f792889a 7358 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7359
f792889a 7360 if (child_type != NULL)
a02abb62 7361 {
0963b4bd
MS
7362 /* The range type was succesfully read. Save it for the
7363 array type creation. */
a02abb62
JB
7364 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7365 {
7366 range_types = (struct type **)
7367 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7368 * sizeof (struct type *));
7369 if (ndim == 0)
7370 make_cleanup (free_current_contents, &range_types);
7371 }
f792889a 7372 range_types[ndim++] = child_type;
a02abb62 7373 }
c906108c
SS
7374 }
7375 child_die = sibling_die (child_die);
7376 }
7377
7378 /* Dwarf2 dimensions are output from left to right, create the
7379 necessary array types in backwards order. */
7ca2d3a3 7380
c906108c 7381 type = element_type;
7ca2d3a3
DL
7382
7383 if (read_array_order (die, cu) == DW_ORD_col_major)
7384 {
7385 int i = 0;
9a619af0 7386
7ca2d3a3
DL
7387 while (i < ndim)
7388 type = create_array_type (NULL, type, range_types[i++]);
7389 }
7390 else
7391 {
7392 while (ndim-- > 0)
7393 type = create_array_type (NULL, type, range_types[ndim]);
7394 }
c906108c 7395
f5f8a009
EZ
7396 /* Understand Dwarf2 support for vector types (like they occur on
7397 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7398 array type. This is not part of the Dwarf2/3 standard yet, but a
7399 custom vendor extension. The main difference between a regular
7400 array and the vector variant is that vectors are passed by value
7401 to functions. */
e142c38c 7402 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7403 if (attr)
ea37ba09 7404 make_vector_type (type);
f5f8a009 7405
dbc98a8b
KW
7406 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7407 implementation may choose to implement triple vectors using this
7408 attribute. */
7409 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7410 if (attr)
7411 {
7412 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7413 TYPE_LENGTH (type) = DW_UNSND (attr);
7414 else
3e43a32a
MS
7415 complaint (&symfile_complaints,
7416 _("DW_AT_byte_size for array type smaller "
7417 "than the total size of elements"));
dbc98a8b
KW
7418 }
7419
39cbfefa
DJ
7420 name = dwarf2_name (die, cu);
7421 if (name)
7422 TYPE_NAME (type) = name;
6e70227d 7423
0963b4bd 7424 /* Install the type in the die. */
7e314c57
JK
7425 set_die_type (die, type, cu);
7426
7427 /* set_die_type should be already done. */
b4ba55a1
JB
7428 set_descriptive_type (type, die, cu);
7429
c906108c
SS
7430 do_cleanups (back_to);
7431
7e314c57 7432 return type;
c906108c
SS
7433}
7434
7ca2d3a3 7435static enum dwarf_array_dim_ordering
6e70227d 7436read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7437{
7438 struct attribute *attr;
7439
7440 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7441
7442 if (attr) return DW_SND (attr);
7443
0963b4bd
MS
7444 /* GNU F77 is a special case, as at 08/2004 array type info is the
7445 opposite order to the dwarf2 specification, but data is still
7446 laid out as per normal fortran.
7ca2d3a3 7447
0963b4bd
MS
7448 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7449 version checking. */
7ca2d3a3 7450
905e0470
PM
7451 if (cu->language == language_fortran
7452 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7453 {
7454 return DW_ORD_row_major;
7455 }
7456
6e70227d 7457 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7458 {
7459 case array_column_major:
7460 return DW_ORD_col_major;
7461 case array_row_major:
7462 default:
7463 return DW_ORD_row_major;
7464 };
7465}
7466
72019c9c 7467/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7468 the DIE's type field. */
72019c9c 7469
f792889a 7470static struct type *
72019c9c
GM
7471read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7472{
7e314c57
JK
7473 struct type *domain_type, *set_type;
7474 struct attribute *attr;
f792889a 7475
7e314c57
JK
7476 domain_type = die_type (die, cu);
7477
7478 /* The die_type call above may have already set the type for this DIE. */
7479 set_type = get_die_type (die, cu);
7480 if (set_type)
7481 return set_type;
7482
7483 set_type = create_set_type (NULL, domain_type);
7484
7485 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7486 if (attr)
7487 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7488
f792889a 7489 return set_die_type (die, set_type, cu);
72019c9c 7490}
7ca2d3a3 7491
c906108c
SS
7492/* First cut: install each common block member as a global variable. */
7493
7494static void
e7c27a73 7495read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7496{
7497 struct die_info *child_die;
7498 struct attribute *attr;
7499 struct symbol *sym;
7500 CORE_ADDR base = (CORE_ADDR) 0;
7501
e142c38c 7502 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7503 if (attr)
7504 {
0963b4bd 7505 /* Support the .debug_loc offsets. */
8e19ed76
PS
7506 if (attr_form_is_block (attr))
7507 {
e7c27a73 7508 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7509 }
3690dd37 7510 else if (attr_form_is_section_offset (attr))
8e19ed76 7511 {
4d3c2250 7512 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7513 }
7514 else
7515 {
4d3c2250
KB
7516 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7517 "common block member");
8e19ed76 7518 }
c906108c 7519 }
639d11d3 7520 if (die->child != NULL)
c906108c 7521 {
639d11d3 7522 child_die = die->child;
c906108c
SS
7523 while (child_die && child_die->tag)
7524 {
e7c27a73 7525 sym = new_symbol (child_die, NULL, cu);
e142c38c 7526 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
f1078f66 7527 if (sym != NULL && attr != NULL)
c906108c 7528 {
d4b96c9a
JK
7529 CORE_ADDR byte_offset = 0;
7530
7531 if (attr_form_is_section_offset (attr))
7532 dwarf2_complex_location_expr_complaint ();
7533 else if (attr_form_is_constant (attr))
7534 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7535 else if (attr_form_is_block (attr))
7536 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7537 else
7538 dwarf2_complex_location_expr_complaint ();
7539
7540 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
7541 add_symbol_to_list (sym, &global_symbols);
7542 }
7543 child_die = sibling_die (child_die);
7544 }
7545 }
7546}
7547
0114d602 7548/* Create a type for a C++ namespace. */
d9fa45fe 7549
0114d602
DJ
7550static struct type *
7551read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7552{
e7c27a73 7553 struct objfile *objfile = cu->objfile;
0114d602 7554 const char *previous_prefix, *name;
9219021c 7555 int is_anonymous;
0114d602
DJ
7556 struct type *type;
7557
7558 /* For extensions, reuse the type of the original namespace. */
7559 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7560 {
7561 struct die_info *ext_die;
7562 struct dwarf2_cu *ext_cu = cu;
9a619af0 7563
0114d602
DJ
7564 ext_die = dwarf2_extension (die, &ext_cu);
7565 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7566
7567 /* EXT_CU may not be the same as CU.
7568 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7569 return set_die_type (die, type, cu);
7570 }
9219021c 7571
e142c38c 7572 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7573
7574 /* Now build the name of the current namespace. */
7575
0114d602
DJ
7576 previous_prefix = determine_prefix (die, cu);
7577 if (previous_prefix[0] != '\0')
7578 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7579 previous_prefix, name, 0, cu);
0114d602
DJ
7580
7581 /* Create the type. */
7582 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7583 objfile);
7584 TYPE_NAME (type) = (char *) name;
7585 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7586
60531b24 7587 return set_die_type (die, type, cu);
0114d602
DJ
7588}
7589
7590/* Read a C++ namespace. */
7591
7592static void
7593read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7594{
7595 struct objfile *objfile = cu->objfile;
0114d602 7596 int is_anonymous;
9219021c 7597
5c4e30ca
DC
7598 /* Add a symbol associated to this if we haven't seen the namespace
7599 before. Also, add a using directive if it's an anonymous
7600 namespace. */
9219021c 7601
f2f0e013 7602 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7603 {
7604 struct type *type;
7605
0114d602 7606 type = read_type_die (die, cu);
e7c27a73 7607 new_symbol (die, type, cu);
5c4e30ca 7608
e8e80198 7609 namespace_name (die, &is_anonymous, cu);
5c4e30ca 7610 if (is_anonymous)
0114d602
DJ
7611 {
7612 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7613
c0cc3a76 7614 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 7615 NULL, &objfile->objfile_obstack);
0114d602 7616 }
5c4e30ca 7617 }
9219021c 7618
639d11d3 7619 if (die->child != NULL)
d9fa45fe 7620 {
639d11d3 7621 struct die_info *child_die = die->child;
6e70227d 7622
d9fa45fe
DC
7623 while (child_die && child_die->tag)
7624 {
e7c27a73 7625 process_die (child_die, cu);
d9fa45fe
DC
7626 child_die = sibling_die (child_die);
7627 }
7628 }
38d518c9
EZ
7629}
7630
f55ee35c
JK
7631/* Read a Fortran module as type. This DIE can be only a declaration used for
7632 imported module. Still we need that type as local Fortran "use ... only"
7633 declaration imports depend on the created type in determine_prefix. */
7634
7635static struct type *
7636read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7637{
7638 struct objfile *objfile = cu->objfile;
7639 char *module_name;
7640 struct type *type;
7641
7642 module_name = dwarf2_name (die, cu);
7643 if (!module_name)
3e43a32a
MS
7644 complaint (&symfile_complaints,
7645 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
7646 die->offset);
7647 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7648
7649 /* determine_prefix uses TYPE_TAG_NAME. */
7650 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7651
7652 return set_die_type (die, type, cu);
7653}
7654
5d7cb8df
JK
7655/* Read a Fortran module. */
7656
7657static void
7658read_module (struct die_info *die, struct dwarf2_cu *cu)
7659{
7660 struct die_info *child_die = die->child;
7661
5d7cb8df
JK
7662 while (child_die && child_die->tag)
7663 {
7664 process_die (child_die, cu);
7665 child_die = sibling_die (child_die);
7666 }
7667}
7668
38d518c9
EZ
7669/* Return the name of the namespace represented by DIE. Set
7670 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7671 namespace. */
7672
7673static const char *
e142c38c 7674namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7675{
7676 struct die_info *current_die;
7677 const char *name = NULL;
7678
7679 /* Loop through the extensions until we find a name. */
7680
7681 for (current_die = die;
7682 current_die != NULL;
f2f0e013 7683 current_die = dwarf2_extension (die, &cu))
38d518c9 7684 {
e142c38c 7685 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7686 if (name != NULL)
7687 break;
7688 }
7689
7690 /* Is it an anonymous namespace? */
7691
7692 *is_anonymous = (name == NULL);
7693 if (*is_anonymous)
7694 name = "(anonymous namespace)";
7695
7696 return name;
d9fa45fe
DC
7697}
7698
c906108c
SS
7699/* Extract all information from a DW_TAG_pointer_type DIE and add to
7700 the user defined type vector. */
7701
f792889a 7702static struct type *
e7c27a73 7703read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7704{
5e2b427d 7705 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7706 struct comp_unit_head *cu_header = &cu->header;
c906108c 7707 struct type *type;
8b2dbe47
KB
7708 struct attribute *attr_byte_size;
7709 struct attribute *attr_address_class;
7710 int byte_size, addr_class;
7e314c57
JK
7711 struct type *target_type;
7712
7713 target_type = die_type (die, cu);
c906108c 7714
7e314c57
JK
7715 /* The die_type call above may have already set the type for this DIE. */
7716 type = get_die_type (die, cu);
7717 if (type)
7718 return type;
7719
7720 type = lookup_pointer_type (target_type);
8b2dbe47 7721
e142c38c 7722 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7723 if (attr_byte_size)
7724 byte_size = DW_UNSND (attr_byte_size);
c906108c 7725 else
8b2dbe47
KB
7726 byte_size = cu_header->addr_size;
7727
e142c38c 7728 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7729 if (attr_address_class)
7730 addr_class = DW_UNSND (attr_address_class);
7731 else
7732 addr_class = DW_ADDR_none;
7733
7734 /* If the pointer size or address class is different than the
7735 default, create a type variant marked as such and set the
7736 length accordingly. */
7737 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7738 {
5e2b427d 7739 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7740 {
7741 int type_flags;
7742
849957d9 7743 type_flags = gdbarch_address_class_type_flags
5e2b427d 7744 (gdbarch, byte_size, addr_class);
876cecd0
TT
7745 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7746 == 0);
8b2dbe47
KB
7747 type = make_type_with_address_space (type, type_flags);
7748 }
7749 else if (TYPE_LENGTH (type) != byte_size)
7750 {
3e43a32a
MS
7751 complaint (&symfile_complaints,
7752 _("invalid pointer size %d"), byte_size);
8b2dbe47 7753 }
6e70227d 7754 else
9a619af0
MS
7755 {
7756 /* Should we also complain about unhandled address classes? */
7757 }
c906108c 7758 }
8b2dbe47
KB
7759
7760 TYPE_LENGTH (type) = byte_size;
f792889a 7761 return set_die_type (die, type, cu);
c906108c
SS
7762}
7763
7764/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7765 the user defined type vector. */
7766
f792889a 7767static struct type *
e7c27a73 7768read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7769{
7770 struct type *type;
7771 struct type *to_type;
7772 struct type *domain;
7773
e7c27a73
DJ
7774 to_type = die_type (die, cu);
7775 domain = die_containing_type (die, cu);
0d5de010 7776
7e314c57
JK
7777 /* The calls above may have already set the type for this DIE. */
7778 type = get_die_type (die, cu);
7779 if (type)
7780 return type;
7781
0d5de010
DJ
7782 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7783 type = lookup_methodptr_type (to_type);
7784 else
7785 type = lookup_memberptr_type (to_type, domain);
c906108c 7786
f792889a 7787 return set_die_type (die, type, cu);
c906108c
SS
7788}
7789
7790/* Extract all information from a DW_TAG_reference_type DIE and add to
7791 the user defined type vector. */
7792
f792889a 7793static struct type *
e7c27a73 7794read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7795{
e7c27a73 7796 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7797 struct type *type, *target_type;
c906108c
SS
7798 struct attribute *attr;
7799
7e314c57
JK
7800 target_type = die_type (die, cu);
7801
7802 /* The die_type call above may have already set the type for this DIE. */
7803 type = get_die_type (die, cu);
7804 if (type)
7805 return type;
7806
7807 type = lookup_reference_type (target_type);
e142c38c 7808 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7809 if (attr)
7810 {
7811 TYPE_LENGTH (type) = DW_UNSND (attr);
7812 }
7813 else
7814 {
107d2387 7815 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7816 }
f792889a 7817 return set_die_type (die, type, cu);
c906108c
SS
7818}
7819
f792889a 7820static struct type *
e7c27a73 7821read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7822{
f792889a 7823 struct type *base_type, *cv_type;
c906108c 7824
e7c27a73 7825 base_type = die_type (die, cu);
7e314c57
JK
7826
7827 /* The die_type call above may have already set the type for this DIE. */
7828 cv_type = get_die_type (die, cu);
7829 if (cv_type)
7830 return cv_type;
7831
2f608a3a
KW
7832 /* In case the const qualifier is applied to an array type, the element type
7833 is so qualified, not the array type (section 6.7.3 of C99). */
7834 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7835 {
7836 struct type *el_type, *inner_array;
7837
7838 base_type = copy_type (base_type);
7839 inner_array = base_type;
7840
7841 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7842 {
7843 TYPE_TARGET_TYPE (inner_array) =
7844 copy_type (TYPE_TARGET_TYPE (inner_array));
7845 inner_array = TYPE_TARGET_TYPE (inner_array);
7846 }
7847
7848 el_type = TYPE_TARGET_TYPE (inner_array);
7849 TYPE_TARGET_TYPE (inner_array) =
7850 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7851
7852 return set_die_type (die, base_type, cu);
7853 }
7854
f792889a
DJ
7855 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7856 return set_die_type (die, cv_type, cu);
c906108c
SS
7857}
7858
f792889a 7859static struct type *
e7c27a73 7860read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7861{
f792889a 7862 struct type *base_type, *cv_type;
c906108c 7863
e7c27a73 7864 base_type = die_type (die, cu);
7e314c57
JK
7865
7866 /* The die_type call above may have already set the type for this DIE. */
7867 cv_type = get_die_type (die, cu);
7868 if (cv_type)
7869 return cv_type;
7870
f792889a
DJ
7871 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7872 return set_die_type (die, cv_type, cu);
c906108c
SS
7873}
7874
7875/* Extract all information from a DW_TAG_string_type DIE and add to
7876 the user defined type vector. It isn't really a user defined type,
7877 but it behaves like one, with other DIE's using an AT_user_def_type
7878 attribute to reference it. */
7879
f792889a 7880static struct type *
e7c27a73 7881read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7882{
e7c27a73 7883 struct objfile *objfile = cu->objfile;
3b7538c0 7884 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7885 struct type *type, *range_type, *index_type, *char_type;
7886 struct attribute *attr;
7887 unsigned int length;
7888
e142c38c 7889 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7890 if (attr)
7891 {
7892 length = DW_UNSND (attr);
7893 }
7894 else
7895 {
0963b4bd 7896 /* Check for the DW_AT_byte_size attribute. */
e142c38c 7897 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7898 if (attr)
7899 {
7900 length = DW_UNSND (attr);
7901 }
7902 else
7903 {
7904 length = 1;
7905 }
c906108c 7906 }
6ccb9162 7907
46bf5051 7908 index_type = objfile_type (objfile)->builtin_int;
c906108c 7909 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7910 char_type = language_string_char_type (cu->language_defn, gdbarch);
7911 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7912
f792889a 7913 return set_die_type (die, type, cu);
c906108c
SS
7914}
7915
7916/* Handle DIES due to C code like:
7917
7918 struct foo
c5aa993b
JM
7919 {
7920 int (*funcp)(int a, long l);
7921 int b;
7922 };
c906108c 7923
0963b4bd 7924 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 7925
f792889a 7926static struct type *
e7c27a73 7927read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7928{
0963b4bd
MS
7929 struct type *type; /* Type that this function returns. */
7930 struct type *ftype; /* Function that returns above type. */
c906108c
SS
7931 struct attribute *attr;
7932
e7c27a73 7933 type = die_type (die, cu);
7e314c57
JK
7934
7935 /* The die_type call above may have already set the type for this DIE. */
7936 ftype = get_die_type (die, cu);
7937 if (ftype)
7938 return ftype;
7939
0c8b41f1 7940 ftype = lookup_function_type (type);
c906108c 7941
5b8101ae 7942 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7943 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7944 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7945 || cu->language == language_cplus
5b8101ae
PM
7946 || cu->language == language_java
7947 || cu->language == language_pascal)
876cecd0 7948 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7949 else if (producer_is_realview (cu->producer))
7950 /* RealView does not emit DW_AT_prototyped. We can not
7951 distinguish prototyped and unprototyped functions; default to
7952 prototyped, since that is more common in modern code (and
7953 RealView warns about unprototyped functions). */
7954 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7955
c055b101
CV
7956 /* Store the calling convention in the type if it's available in
7957 the subroutine die. Otherwise set the calling convention to
7958 the default value DW_CC_normal. */
7959 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
7960 if (attr)
7961 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
7962 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
7963 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
7964 else
7965 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
7966
7967 /* We need to add the subroutine type to the die immediately so
7968 we don't infinitely recurse when dealing with parameters
0963b4bd 7969 declared as the same subroutine type. */
76c10ea2 7970 set_die_type (die, ftype, cu);
6e70227d 7971
639d11d3 7972 if (die->child != NULL)
c906108c 7973 {
8072405b 7974 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7975 struct die_info *child_die;
8072405b 7976 int nparams, iparams;
c906108c
SS
7977
7978 /* Count the number of parameters.
7979 FIXME: GDB currently ignores vararg functions, but knows about
7980 vararg member functions. */
8072405b 7981 nparams = 0;
639d11d3 7982 child_die = die->child;
c906108c
SS
7983 while (child_die && child_die->tag)
7984 {
7985 if (child_die->tag == DW_TAG_formal_parameter)
7986 nparams++;
7987 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7988 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7989 child_die = sibling_die (child_die);
7990 }
7991
7992 /* Allocate storage for parameters and fill them in. */
7993 TYPE_NFIELDS (ftype) = nparams;
7994 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7995 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7996
8072405b
JK
7997 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7998 even if we error out during the parameters reading below. */
7999 for (iparams = 0; iparams < nparams; iparams++)
8000 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8001
8002 iparams = 0;
639d11d3 8003 child_die = die->child;
c906108c
SS
8004 while (child_die && child_die->tag)
8005 {
8006 if (child_die->tag == DW_TAG_formal_parameter)
8007 {
3ce3b1ba
PA
8008 struct type *arg_type;
8009
8010 /* DWARF version 2 has no clean way to discern C++
8011 static and non-static member functions. G++ helps
8012 GDB by marking the first parameter for non-static
8013 member functions (which is the this pointer) as
8014 artificial. We pass this information to
8015 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8016
8017 DWARF version 3 added DW_AT_object_pointer, which GCC
8018 4.5 does not yet generate. */
e142c38c 8019 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8020 if (attr)
8021 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8022 else
418835cc
KS
8023 {
8024 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8025
8026 /* GCC/43521: In java, the formal parameter
8027 "this" is sometimes not marked with DW_AT_artificial. */
8028 if (cu->language == language_java)
8029 {
8030 const char *name = dwarf2_name (child_die, cu);
9a619af0 8031
418835cc
KS
8032 if (name && !strcmp (name, "this"))
8033 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8034 }
8035 }
3ce3b1ba
PA
8036 arg_type = die_type (child_die, cu);
8037
8038 /* RealView does not mark THIS as const, which the testsuite
8039 expects. GCC marks THIS as const in method definitions,
8040 but not in the class specifications (GCC PR 43053). */
8041 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8042 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8043 {
8044 int is_this = 0;
8045 struct dwarf2_cu *arg_cu = cu;
8046 const char *name = dwarf2_name (child_die, cu);
8047
8048 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8049 if (attr)
8050 {
8051 /* If the compiler emits this, use it. */
8052 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8053 is_this = 1;
8054 }
8055 else if (name && strcmp (name, "this") == 0)
8056 /* Function definitions will have the argument names. */
8057 is_this = 1;
8058 else if (name == NULL && iparams == 0)
8059 /* Declarations may not have the names, so like
8060 elsewhere in GDB, assume an artificial first
8061 argument is "this". */
8062 is_this = 1;
8063
8064 if (is_this)
8065 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8066 arg_type, 0);
8067 }
8068
8069 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8070 iparams++;
8071 }
8072 child_die = sibling_die (child_die);
8073 }
8074 }
8075
76c10ea2 8076 return ftype;
c906108c
SS
8077}
8078
f792889a 8079static struct type *
e7c27a73 8080read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8081{
e7c27a73 8082 struct objfile *objfile = cu->objfile;
0114d602 8083 const char *name = NULL;
f792889a 8084 struct type *this_type;
c906108c 8085
94af9270 8086 name = dwarf2_full_name (NULL, die, cu);
f792889a 8087 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8088 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8089 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8090 set_die_type (die, this_type, cu);
8091 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8092 return this_type;
c906108c
SS
8093}
8094
8095/* Find a representation of a given base type and install
8096 it in the TYPE field of the die. */
8097
f792889a 8098static struct type *
e7c27a73 8099read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8100{
e7c27a73 8101 struct objfile *objfile = cu->objfile;
c906108c
SS
8102 struct type *type;
8103 struct attribute *attr;
8104 int encoding = 0, size = 0;
39cbfefa 8105 char *name;
6ccb9162
UW
8106 enum type_code code = TYPE_CODE_INT;
8107 int type_flags = 0;
8108 struct type *target_type = NULL;
c906108c 8109
e142c38c 8110 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8111 if (attr)
8112 {
8113 encoding = DW_UNSND (attr);
8114 }
e142c38c 8115 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8116 if (attr)
8117 {
8118 size = DW_UNSND (attr);
8119 }
39cbfefa 8120 name = dwarf2_name (die, cu);
6ccb9162 8121 if (!name)
c906108c 8122 {
6ccb9162
UW
8123 complaint (&symfile_complaints,
8124 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8125 }
6ccb9162
UW
8126
8127 switch (encoding)
c906108c 8128 {
6ccb9162
UW
8129 case DW_ATE_address:
8130 /* Turn DW_ATE_address into a void * pointer. */
8131 code = TYPE_CODE_PTR;
8132 type_flags |= TYPE_FLAG_UNSIGNED;
8133 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8134 break;
8135 case DW_ATE_boolean:
8136 code = TYPE_CODE_BOOL;
8137 type_flags |= TYPE_FLAG_UNSIGNED;
8138 break;
8139 case DW_ATE_complex_float:
8140 code = TYPE_CODE_COMPLEX;
8141 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8142 break;
8143 case DW_ATE_decimal_float:
8144 code = TYPE_CODE_DECFLOAT;
8145 break;
8146 case DW_ATE_float:
8147 code = TYPE_CODE_FLT;
8148 break;
8149 case DW_ATE_signed:
8150 break;
8151 case DW_ATE_unsigned:
8152 type_flags |= TYPE_FLAG_UNSIGNED;
8153 break;
8154 case DW_ATE_signed_char:
6e70227d 8155 if (cu->language == language_ada || cu->language == language_m2
868a0084 8156 || cu->language == language_pascal)
6ccb9162
UW
8157 code = TYPE_CODE_CHAR;
8158 break;
8159 case DW_ATE_unsigned_char:
868a0084
PM
8160 if (cu->language == language_ada || cu->language == language_m2
8161 || cu->language == language_pascal)
6ccb9162
UW
8162 code = TYPE_CODE_CHAR;
8163 type_flags |= TYPE_FLAG_UNSIGNED;
8164 break;
75079b2b
TT
8165 case DW_ATE_UTF:
8166 /* We just treat this as an integer and then recognize the
8167 type by name elsewhere. */
8168 break;
8169
6ccb9162
UW
8170 default:
8171 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8172 dwarf_type_encoding_name (encoding));
8173 break;
c906108c 8174 }
6ccb9162 8175
0114d602
DJ
8176 type = init_type (code, size, type_flags, NULL, objfile);
8177 TYPE_NAME (type) = name;
6ccb9162
UW
8178 TYPE_TARGET_TYPE (type) = target_type;
8179
0114d602 8180 if (name && strcmp (name, "char") == 0)
876cecd0 8181 TYPE_NOSIGN (type) = 1;
0114d602 8182
f792889a 8183 return set_die_type (die, type, cu);
c906108c
SS
8184}
8185
a02abb62
JB
8186/* Read the given DW_AT_subrange DIE. */
8187
f792889a 8188static struct type *
a02abb62
JB
8189read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8190{
8191 struct type *base_type;
8192 struct type *range_type;
8193 struct attribute *attr;
43bbcdc2
PH
8194 LONGEST low = 0;
8195 LONGEST high = -1;
39cbfefa 8196 char *name;
43bbcdc2 8197 LONGEST negative_mask;
e77813c8 8198
a02abb62 8199 base_type = die_type (die, cu);
953ac07e
JK
8200 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8201 check_typedef (base_type);
a02abb62 8202
7e314c57
JK
8203 /* The die_type call above may have already set the type for this DIE. */
8204 range_type = get_die_type (die, cu);
8205 if (range_type)
8206 return range_type;
8207
e142c38c 8208 if (cu->language == language_fortran)
6e70227d 8209 {
a02abb62
JB
8210 /* FORTRAN implies a lower bound of 1, if not given. */
8211 low = 1;
8212 }
8213
dd5e6932
DJ
8214 /* FIXME: For variable sized arrays either of these could be
8215 a variable rather than a constant value. We'll allow it,
8216 but we don't know how to handle it. */
e142c38c 8217 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8218 if (attr)
8219 low = dwarf2_get_attr_constant_value (attr, 0);
8220
e142c38c 8221 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8222 if (attr)
6e70227d 8223 {
e77813c8 8224 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
8225 {
8226 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8227 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8228 FIXME: GDB does not yet know how to handle dynamic
8229 arrays properly, treat them as arrays with unspecified
8230 length for now.
8231
8232 FIXME: jimb/2003-09-22: GDB does not really know
8233 how to handle arrays of unspecified length
8234 either; we just represent them as zero-length
8235 arrays. Choose an appropriate upper bound given
8236 the lower bound we've computed above. */
8237 high = low - 1;
8238 }
8239 else
8240 high = dwarf2_get_attr_constant_value (attr, 1);
8241 }
e77813c8
PM
8242 else
8243 {
8244 attr = dwarf2_attr (die, DW_AT_count, cu);
8245 if (attr)
8246 {
8247 int count = dwarf2_get_attr_constant_value (attr, 1);
8248 high = low + count - 1;
8249 }
c2ff108b
JK
8250 else
8251 {
8252 /* Unspecified array length. */
8253 high = low - 1;
8254 }
e77813c8
PM
8255 }
8256
8257 /* Dwarf-2 specifications explicitly allows to create subrange types
8258 without specifying a base type.
8259 In that case, the base type must be set to the type of
8260 the lower bound, upper bound or count, in that order, if any of these
8261 three attributes references an object that has a type.
8262 If no base type is found, the Dwarf-2 specifications say that
8263 a signed integer type of size equal to the size of an address should
8264 be used.
8265 For the following C code: `extern char gdb_int [];'
8266 GCC produces an empty range DIE.
8267 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8268 high bound or count are not yet handled by this code. */
e77813c8
PM
8269 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8270 {
8271 struct objfile *objfile = cu->objfile;
8272 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8273 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8274 struct type *int_type = objfile_type (objfile)->builtin_int;
8275
8276 /* Test "int", "long int", and "long long int" objfile types,
8277 and select the first one having a size above or equal to the
8278 architecture address size. */
8279 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8280 base_type = int_type;
8281 else
8282 {
8283 int_type = objfile_type (objfile)->builtin_long;
8284 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8285 base_type = int_type;
8286 else
8287 {
8288 int_type = objfile_type (objfile)->builtin_long_long;
8289 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8290 base_type = int_type;
8291 }
8292 }
8293 }
a02abb62 8294
6e70227d 8295 negative_mask =
43bbcdc2
PH
8296 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8297 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8298 low |= negative_mask;
8299 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8300 high |= negative_mask;
8301
a02abb62
JB
8302 range_type = create_range_type (NULL, base_type, low, high);
8303
bbb0eef6
JK
8304 /* Mark arrays with dynamic length at least as an array of unspecified
8305 length. GDB could check the boundary but before it gets implemented at
8306 least allow accessing the array elements. */
8307 if (attr && attr->form == DW_FORM_block1)
8308 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8309
c2ff108b
JK
8310 /* Ada expects an empty array on no boundary attributes. */
8311 if (attr == NULL && cu->language != language_ada)
8312 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8313
39cbfefa
DJ
8314 name = dwarf2_name (die, cu);
8315 if (name)
8316 TYPE_NAME (range_type) = name;
6e70227d 8317
e142c38c 8318 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8319 if (attr)
8320 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8321
7e314c57
JK
8322 set_die_type (die, range_type, cu);
8323
8324 /* set_die_type should be already done. */
b4ba55a1
JB
8325 set_descriptive_type (range_type, die, cu);
8326
7e314c57 8327 return range_type;
a02abb62 8328}
6e70227d 8329
f792889a 8330static struct type *
81a17f79
JB
8331read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8332{
8333 struct type *type;
81a17f79 8334
81a17f79
JB
8335 /* For now, we only support the C meaning of an unspecified type: void. */
8336
0114d602
DJ
8337 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8338 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8339
f792889a 8340 return set_die_type (die, type, cu);
81a17f79 8341}
a02abb62 8342
51545339
DJ
8343/* Trivial hash function for die_info: the hash value of a DIE
8344 is its offset in .debug_info for this objfile. */
8345
8346static hashval_t
8347die_hash (const void *item)
8348{
8349 const struct die_info *die = item;
9a619af0 8350
51545339
DJ
8351 return die->offset;
8352}
8353
8354/* Trivial comparison function for die_info structures: two DIEs
8355 are equal if they have the same offset. */
8356
8357static int
8358die_eq (const void *item_lhs, const void *item_rhs)
8359{
8360 const struct die_info *die_lhs = item_lhs;
8361 const struct die_info *die_rhs = item_rhs;
9a619af0 8362
51545339
DJ
8363 return die_lhs->offset == die_rhs->offset;
8364}
8365
c906108c
SS
8366/* Read a whole compilation unit into a linked list of dies. */
8367
f9aca02d 8368static struct die_info *
93311388 8369read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8370{
93311388 8371 struct die_reader_specs reader_specs;
98bfdba5 8372 int read_abbrevs = 0;
1d9ec526 8373 struct cleanup *back_to = NULL;
98bfdba5
PA
8374 struct die_info *die;
8375
8376 if (cu->dwarf2_abbrevs == NULL)
8377 {
8378 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8379 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8380 read_abbrevs = 1;
8381 }
93311388 8382
348e048f 8383 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8384 cu->die_hash
8385 = htab_create_alloc_ex (cu->header.length / 12,
8386 die_hash,
8387 die_eq,
8388 NULL,
8389 &cu->comp_unit_obstack,
8390 hashtab_obstack_allocate,
8391 dummy_obstack_deallocate);
8392
93311388
DE
8393 init_cu_die_reader (&reader_specs, cu);
8394
98bfdba5
PA
8395 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8396
8397 if (read_abbrevs)
8398 do_cleanups (back_to);
8399
8400 return die;
639d11d3
DC
8401}
8402
d97bc12b
DE
8403/* Main entry point for reading a DIE and all children.
8404 Read the DIE and dump it if requested. */
8405
8406static struct die_info *
93311388
DE
8407read_die_and_children (const struct die_reader_specs *reader,
8408 gdb_byte *info_ptr,
d97bc12b
DE
8409 gdb_byte **new_info_ptr,
8410 struct die_info *parent)
8411{
93311388 8412 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8413 new_info_ptr, parent);
8414
8415 if (dwarf2_die_debug)
8416 {
348e048f
DE
8417 fprintf_unfiltered (gdb_stdlog,
8418 "\nRead die from %s of %s:\n",
8419 reader->buffer == dwarf2_per_objfile->info.buffer
8420 ? ".debug_info"
8421 : reader->buffer == dwarf2_per_objfile->types.buffer
8422 ? ".debug_types"
8423 : "unknown section",
8424 reader->abfd->filename);
d97bc12b
DE
8425 dump_die (result, dwarf2_die_debug);
8426 }
8427
8428 return result;
8429}
8430
639d11d3
DC
8431/* Read a single die and all its descendents. Set the die's sibling
8432 field to NULL; set other fields in the die correctly, and set all
8433 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8434 location of the info_ptr after reading all of those dies. PARENT
8435 is the parent of the die in question. */
8436
8437static struct die_info *
93311388
DE
8438read_die_and_children_1 (const struct die_reader_specs *reader,
8439 gdb_byte *info_ptr,
d97bc12b
DE
8440 gdb_byte **new_info_ptr,
8441 struct die_info *parent)
639d11d3
DC
8442{
8443 struct die_info *die;
fe1b8b76 8444 gdb_byte *cur_ptr;
639d11d3
DC
8445 int has_children;
8446
93311388 8447 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8448 if (die == NULL)
8449 {
8450 *new_info_ptr = cur_ptr;
8451 return NULL;
8452 }
93311388 8453 store_in_ref_table (die, reader->cu);
639d11d3
DC
8454
8455 if (has_children)
348e048f 8456 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8457 else
8458 {
8459 die->child = NULL;
8460 *new_info_ptr = cur_ptr;
8461 }
8462
8463 die->sibling = NULL;
8464 die->parent = parent;
8465 return die;
8466}
8467
8468/* Read a die, all of its descendents, and all of its siblings; set
8469 all of the fields of all of the dies correctly. Arguments are as
8470 in read_die_and_children. */
8471
8472static struct die_info *
93311388
DE
8473read_die_and_siblings (const struct die_reader_specs *reader,
8474 gdb_byte *info_ptr,
fe1b8b76 8475 gdb_byte **new_info_ptr,
639d11d3
DC
8476 struct die_info *parent)
8477{
8478 struct die_info *first_die, *last_sibling;
fe1b8b76 8479 gdb_byte *cur_ptr;
639d11d3 8480
c906108c 8481 cur_ptr = info_ptr;
639d11d3
DC
8482 first_die = last_sibling = NULL;
8483
8484 while (1)
c906108c 8485 {
639d11d3 8486 struct die_info *die
93311388 8487 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8488
1d325ec1 8489 if (die == NULL)
c906108c 8490 {
639d11d3
DC
8491 *new_info_ptr = cur_ptr;
8492 return first_die;
c906108c 8493 }
1d325ec1
DJ
8494
8495 if (!first_die)
8496 first_die = die;
c906108c 8497 else
1d325ec1
DJ
8498 last_sibling->sibling = die;
8499
8500 last_sibling = die;
c906108c 8501 }
c906108c
SS
8502}
8503
93311388
DE
8504/* Read the die from the .debug_info section buffer. Set DIEP to
8505 point to a newly allocated die with its information, except for its
8506 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8507 whether the die has children or not. */
8508
8509static gdb_byte *
8510read_full_die (const struct die_reader_specs *reader,
8511 struct die_info **diep, gdb_byte *info_ptr,
8512 int *has_children)
8513{
8514 unsigned int abbrev_number, bytes_read, i, offset;
8515 struct abbrev_info *abbrev;
8516 struct die_info *die;
8517 struct dwarf2_cu *cu = reader->cu;
8518 bfd *abfd = reader->abfd;
8519
8520 offset = info_ptr - reader->buffer;
8521 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8522 info_ptr += bytes_read;
8523 if (!abbrev_number)
8524 {
8525 *diep = NULL;
8526 *has_children = 0;
8527 return info_ptr;
8528 }
8529
8530 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8531 if (!abbrev)
348e048f
DE
8532 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8533 abbrev_number,
8534 bfd_get_filename (abfd));
8535
93311388
DE
8536 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8537 die->offset = offset;
8538 die->tag = abbrev->tag;
8539 die->abbrev = abbrev_number;
8540
8541 die->num_attrs = abbrev->num_attrs;
8542
8543 for (i = 0; i < abbrev->num_attrs; ++i)
8544 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8545 abfd, info_ptr, cu);
8546
8547 *diep = die;
8548 *has_children = abbrev->has_children;
8549 return info_ptr;
8550}
8551
c906108c
SS
8552/* In DWARF version 2, the description of the debugging information is
8553 stored in a separate .debug_abbrev section. Before we read any
8554 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8555 in a hash table. This function also sets flags in CU describing
8556 the data found in the abbrev table. */
c906108c
SS
8557
8558static void
e7c27a73 8559dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8560{
e7c27a73 8561 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8562 gdb_byte *abbrev_ptr;
c906108c
SS
8563 struct abbrev_info *cur_abbrev;
8564 unsigned int abbrev_number, bytes_read, abbrev_name;
8565 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8566 struct attr_abbrev *cur_attrs;
8567 unsigned int allocated_attrs;
c906108c 8568
0963b4bd 8569 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
8570 obstack_init (&cu->abbrev_obstack);
8571 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8572 (ABBREV_HASH_SIZE
8573 * sizeof (struct abbrev_info *)));
8574 memset (cu->dwarf2_abbrevs, 0,
8575 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8576
be391dca
TT
8577 dwarf2_read_section (dwarf2_per_objfile->objfile,
8578 &dwarf2_per_objfile->abbrev);
dce234bc 8579 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8580 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8581 abbrev_ptr += bytes_read;
8582
f3dd6933
DJ
8583 allocated_attrs = ATTR_ALLOC_CHUNK;
8584 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8585
0963b4bd 8586 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
8587 while (abbrev_number)
8588 {
f3dd6933 8589 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8590
8591 /* read in abbrev header */
8592 cur_abbrev->number = abbrev_number;
8593 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8594 abbrev_ptr += bytes_read;
8595 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8596 abbrev_ptr += 1;
8597
72bf9492
DJ
8598 if (cur_abbrev->tag == DW_TAG_namespace)
8599 cu->has_namespace_info = 1;
8600
c906108c
SS
8601 /* now read in declarations */
8602 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8603 abbrev_ptr += bytes_read;
8604 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8605 abbrev_ptr += bytes_read;
8606 while (abbrev_name)
8607 {
f3dd6933 8608 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8609 {
f3dd6933
DJ
8610 allocated_attrs += ATTR_ALLOC_CHUNK;
8611 cur_attrs
8612 = xrealloc (cur_attrs, (allocated_attrs
8613 * sizeof (struct attr_abbrev)));
c906108c 8614 }
ae038cb0
DJ
8615
8616 /* Record whether this compilation unit might have
8617 inter-compilation-unit references. If we don't know what form
8618 this attribute will have, then it might potentially be a
8619 DW_FORM_ref_addr, so we conservatively expect inter-CU
8620 references. */
8621
8622 if (abbrev_form == DW_FORM_ref_addr
8623 || abbrev_form == DW_FORM_indirect)
8624 cu->has_form_ref_addr = 1;
8625
f3dd6933
DJ
8626 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8627 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8628 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8629 abbrev_ptr += bytes_read;
8630 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8631 abbrev_ptr += bytes_read;
8632 }
8633
f3dd6933
DJ
8634 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8635 (cur_abbrev->num_attrs
8636 * sizeof (struct attr_abbrev)));
8637 memcpy (cur_abbrev->attrs, cur_attrs,
8638 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8639
c906108c 8640 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
8641 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8642 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
8643
8644 /* Get next abbreviation.
8645 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
8646 always properly terminated with an abbrev number of 0.
8647 Exit loop if we encounter an abbreviation which we have
8648 already read (which means we are about to read the abbreviations
8649 for the next compile unit) or if the end of the abbreviation
8650 table is reached. */
dce234bc
PP
8651 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8652 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
8653 break;
8654 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8655 abbrev_ptr += bytes_read;
e7c27a73 8656 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
8657 break;
8658 }
f3dd6933
DJ
8659
8660 xfree (cur_attrs);
c906108c
SS
8661}
8662
f3dd6933 8663/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 8664
c906108c 8665static void
f3dd6933 8666dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 8667{
f3dd6933 8668 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 8669
f3dd6933
DJ
8670 obstack_free (&cu->abbrev_obstack, NULL);
8671 cu->dwarf2_abbrevs = NULL;
c906108c
SS
8672}
8673
8674/* Lookup an abbrev_info structure in the abbrev hash table. */
8675
8676static struct abbrev_info *
e7c27a73 8677dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
8678{
8679 unsigned int hash_number;
8680 struct abbrev_info *abbrev;
8681
8682 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 8683 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
8684
8685 while (abbrev)
8686 {
8687 if (abbrev->number == number)
8688 return abbrev;
8689 else
8690 abbrev = abbrev->next;
8691 }
8692 return NULL;
8693}
8694
72bf9492
DJ
8695/* Returns nonzero if TAG represents a type that we might generate a partial
8696 symbol for. */
8697
8698static int
8699is_type_tag_for_partial (int tag)
8700{
8701 switch (tag)
8702 {
8703#if 0
8704 /* Some types that would be reasonable to generate partial symbols for,
8705 that we don't at present. */
8706 case DW_TAG_array_type:
8707 case DW_TAG_file_type:
8708 case DW_TAG_ptr_to_member_type:
8709 case DW_TAG_set_type:
8710 case DW_TAG_string_type:
8711 case DW_TAG_subroutine_type:
8712#endif
8713 case DW_TAG_base_type:
8714 case DW_TAG_class_type:
680b30c7 8715 case DW_TAG_interface_type:
72bf9492
DJ
8716 case DW_TAG_enumeration_type:
8717 case DW_TAG_structure_type:
8718 case DW_TAG_subrange_type:
8719 case DW_TAG_typedef:
8720 case DW_TAG_union_type:
8721 return 1;
8722 default:
8723 return 0;
8724 }
8725}
8726
8727/* Load all DIEs that are interesting for partial symbols into memory. */
8728
8729static struct partial_die_info *
93311388
DE
8730load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8731 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
8732{
8733 struct partial_die_info *part_die;
8734 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8735 struct abbrev_info *abbrev;
8736 unsigned int bytes_read;
5afb4e99 8737 unsigned int load_all = 0;
72bf9492
DJ
8738
8739 int nesting_level = 1;
8740
8741 parent_die = NULL;
8742 last_die = NULL;
8743
5afb4e99
DJ
8744 if (cu->per_cu && cu->per_cu->load_all_dies)
8745 load_all = 1;
8746
72bf9492
DJ
8747 cu->partial_dies
8748 = htab_create_alloc_ex (cu->header.length / 12,
8749 partial_die_hash,
8750 partial_die_eq,
8751 NULL,
8752 &cu->comp_unit_obstack,
8753 hashtab_obstack_allocate,
8754 dummy_obstack_deallocate);
8755
8756 part_die = obstack_alloc (&cu->comp_unit_obstack,
8757 sizeof (struct partial_die_info));
8758
8759 while (1)
8760 {
8761 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8762
8763 /* A NULL abbrev means the end of a series of children. */
8764 if (abbrev == NULL)
8765 {
8766 if (--nesting_level == 0)
8767 {
8768 /* PART_DIE was probably the last thing allocated on the
8769 comp_unit_obstack, so we could call obstack_free
8770 here. We don't do that because the waste is small,
8771 and will be cleaned up when we're done with this
8772 compilation unit. This way, we're also more robust
8773 against other users of the comp_unit_obstack. */
8774 return first_die;
8775 }
8776 info_ptr += bytes_read;
8777 last_die = parent_die;
8778 parent_die = parent_die->die_parent;
8779 continue;
8780 }
8781
98bfdba5
PA
8782 /* Check for template arguments. We never save these; if
8783 they're seen, we just mark the parent, and go on our way. */
8784 if (parent_die != NULL
8785 && cu->language == language_cplus
8786 && (abbrev->tag == DW_TAG_template_type_param
8787 || abbrev->tag == DW_TAG_template_value_param))
8788 {
8789 parent_die->has_template_arguments = 1;
8790
8791 if (!load_all)
8792 {
8793 /* We don't need a partial DIE for the template argument. */
8794 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8795 cu);
8796 continue;
8797 }
8798 }
8799
8800 /* We only recurse into subprograms looking for template arguments.
8801 Skip their other children. */
8802 if (!load_all
8803 && cu->language == language_cplus
8804 && parent_die != NULL
8805 && parent_die->tag == DW_TAG_subprogram)
8806 {
8807 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8808 continue;
8809 }
8810
5afb4e99
DJ
8811 /* Check whether this DIE is interesting enough to save. Normally
8812 we would not be interested in members here, but there may be
8813 later variables referencing them via DW_AT_specification (for
8814 static members). */
8815 if (!load_all
8816 && !is_type_tag_for_partial (abbrev->tag)
72929c62 8817 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
8818 && abbrev->tag != DW_TAG_enumerator
8819 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8820 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8821 && abbrev->tag != DW_TAG_variable
5afb4e99 8822 && abbrev->tag != DW_TAG_namespace
f55ee35c 8823 && abbrev->tag != DW_TAG_module
5afb4e99 8824 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8825 {
8826 /* Otherwise we skip to the next sibling, if any. */
93311388 8827 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8828 continue;
8829 }
8830
93311388
DE
8831 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8832 buffer, info_ptr, cu);
72bf9492
DJ
8833
8834 /* This two-pass algorithm for processing partial symbols has a
8835 high cost in cache pressure. Thus, handle some simple cases
8836 here which cover the majority of C partial symbols. DIEs
8837 which neither have specification tags in them, nor could have
8838 specification tags elsewhere pointing at them, can simply be
8839 processed and discarded.
8840
8841 This segment is also optional; scan_partial_symbols and
8842 add_partial_symbol will handle these DIEs if we chain
8843 them in normally. When compilers which do not emit large
8844 quantities of duplicate debug information are more common,
8845 this code can probably be removed. */
8846
8847 /* Any complete simple types at the top level (pretty much all
8848 of them, for a language without namespaces), can be processed
8849 directly. */
8850 if (parent_die == NULL
8851 && part_die->has_specification == 0
8852 && part_die->is_declaration == 0
8853 && (part_die->tag == DW_TAG_typedef
8854 || part_die->tag == DW_TAG_base_type
8855 || part_die->tag == DW_TAG_subrange_type))
8856 {
8857 if (building_psymtab && part_die->name != NULL)
04a679b8 8858 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8859 VAR_DOMAIN, LOC_TYPEDEF,
8860 &cu->objfile->static_psymbols,
8861 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8862 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8863 continue;
8864 }
8865
8866 /* If we're at the second level, and we're an enumerator, and
8867 our parent has no specification (meaning possibly lives in a
8868 namespace elsewhere), then we can add the partial symbol now
8869 instead of queueing it. */
8870 if (part_die->tag == DW_TAG_enumerator
8871 && parent_die != NULL
8872 && parent_die->die_parent == NULL
8873 && parent_die->tag == DW_TAG_enumeration_type
8874 && parent_die->has_specification == 0)
8875 {
8876 if (part_die->name == NULL)
3e43a32a
MS
8877 complaint (&symfile_complaints,
8878 _("malformed enumerator DIE ignored"));
72bf9492 8879 else if (building_psymtab)
04a679b8 8880 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8881 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8882 (cu->language == language_cplus
8883 || cu->language == language_java)
72bf9492
DJ
8884 ? &cu->objfile->global_psymbols
8885 : &cu->objfile->static_psymbols,
8886 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8887
93311388 8888 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8889 continue;
8890 }
8891
8892 /* We'll save this DIE so link it in. */
8893 part_die->die_parent = parent_die;
8894 part_die->die_sibling = NULL;
8895 part_die->die_child = NULL;
8896
8897 if (last_die && last_die == parent_die)
8898 last_die->die_child = part_die;
8899 else if (last_die)
8900 last_die->die_sibling = part_die;
8901
8902 last_die = part_die;
8903
8904 if (first_die == NULL)
8905 first_die = part_die;
8906
8907 /* Maybe add the DIE to the hash table. Not all DIEs that we
8908 find interesting need to be in the hash table, because we
8909 also have the parent/sibling/child chains; only those that we
8910 might refer to by offset later during partial symbol reading.
8911
8912 For now this means things that might have be the target of a
8913 DW_AT_specification, DW_AT_abstract_origin, or
8914 DW_AT_extension. DW_AT_extension will refer only to
8915 namespaces; DW_AT_abstract_origin refers to functions (and
8916 many things under the function DIE, but we do not recurse
8917 into function DIEs during partial symbol reading) and
8918 possibly variables as well; DW_AT_specification refers to
8919 declarations. Declarations ought to have the DW_AT_declaration
8920 flag. It happens that GCC forgets to put it in sometimes, but
8921 only for functions, not for types.
8922
8923 Adding more things than necessary to the hash table is harmless
8924 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8925 wasted time in find_partial_die, when we reread the compilation
8926 unit with load_all_dies set. */
72bf9492 8927
5afb4e99 8928 if (load_all
72929c62 8929 || abbrev->tag == DW_TAG_constant
5afb4e99 8930 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8931 || abbrev->tag == DW_TAG_variable
8932 || abbrev->tag == DW_TAG_namespace
8933 || part_die->is_declaration)
8934 {
8935 void **slot;
8936
8937 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8938 part_die->offset, INSERT);
8939 *slot = part_die;
8940 }
8941
8942 part_die = obstack_alloc (&cu->comp_unit_obstack,
8943 sizeof (struct partial_die_info));
8944
8945 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8946 we have no reason to follow the children of structures; for other
98bfdba5
PA
8947 languages we have to, so that we can get at method physnames
8948 to infer fully qualified class names, for DW_AT_specification,
8949 and for C++ template arguments. For C++, we also look one level
8950 inside functions to find template arguments (if the name of the
8951 function does not already contain the template arguments).
bc30ff58
JB
8952
8953 For Ada, we need to scan the children of subprograms and lexical
8954 blocks as well because Ada allows the definition of nested
8955 entities that could be interesting for the debugger, such as
8956 nested subprograms for instance. */
72bf9492 8957 if (last_die->has_children
5afb4e99
DJ
8958 && (load_all
8959 || last_die->tag == DW_TAG_namespace
f55ee35c 8960 || last_die->tag == DW_TAG_module
72bf9492 8961 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
8962 || (cu->language == language_cplus
8963 && last_die->tag == DW_TAG_subprogram
8964 && (last_die->name == NULL
8965 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
8966 || (cu->language != language_c
8967 && (last_die->tag == DW_TAG_class_type
680b30c7 8968 || last_die->tag == DW_TAG_interface_type
72bf9492 8969 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8970 || last_die->tag == DW_TAG_union_type))
8971 || (cu->language == language_ada
8972 && (last_die->tag == DW_TAG_subprogram
8973 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8974 {
8975 nesting_level++;
8976 parent_die = last_die;
8977 continue;
8978 }
8979
8980 /* Otherwise we skip to the next sibling, if any. */
93311388 8981 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8982
8983 /* Back to the top, do it again. */
8984 }
8985}
8986
c906108c
SS
8987/* Read a minimal amount of information into the minimal die structure. */
8988
fe1b8b76 8989static gdb_byte *
72bf9492
DJ
8990read_partial_die (struct partial_die_info *part_die,
8991 struct abbrev_info *abbrev,
8992 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8993 gdb_byte *buffer, gdb_byte *info_ptr,
8994 struct dwarf2_cu *cu)
c906108c 8995{
fa238c03 8996 unsigned int i;
c906108c 8997 struct attribute attr;
c5aa993b 8998 int has_low_pc_attr = 0;
c906108c
SS
8999 int has_high_pc_attr = 0;
9000
72bf9492 9001 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9002
93311388 9003 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9004
9005 info_ptr += abbrev_len;
9006
9007 if (abbrev == NULL)
9008 return info_ptr;
9009
c906108c
SS
9010 part_die->tag = abbrev->tag;
9011 part_die->has_children = abbrev->has_children;
c906108c
SS
9012
9013 for (i = 0; i < abbrev->num_attrs; ++i)
9014 {
e7c27a73 9015 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9016
9017 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9018 partial symbol table. */
c906108c
SS
9019 switch (attr.name)
9020 {
9021 case DW_AT_name:
71c25dea
TT
9022 switch (part_die->tag)
9023 {
9024 case DW_TAG_compile_unit:
348e048f 9025 case DW_TAG_type_unit:
71c25dea
TT
9026 /* Compilation units have a DW_AT_name that is a filename, not
9027 a source language identifier. */
9028 case DW_TAG_enumeration_type:
9029 case DW_TAG_enumerator:
9030 /* These tags always have simple identifiers already; no need
9031 to canonicalize them. */
9032 part_die->name = DW_STRING (&attr);
9033 break;
9034 default:
9035 part_die->name
9036 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9037 &cu->objfile->objfile_obstack);
71c25dea
TT
9038 break;
9039 }
c906108c 9040 break;
31ef98ae 9041 case DW_AT_linkage_name:
c906108c 9042 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9043 /* Note that both forms of linkage name might appear. We
9044 assume they will be the same, and we only store the last
9045 one we see. */
94af9270
KS
9046 if (cu->language == language_ada)
9047 part_die->name = DW_STRING (&attr);
abc72ce4 9048 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9049 break;
9050 case DW_AT_low_pc:
9051 has_low_pc_attr = 1;
9052 part_die->lowpc = DW_ADDR (&attr);
9053 break;
9054 case DW_AT_high_pc:
9055 has_high_pc_attr = 1;
9056 part_die->highpc = DW_ADDR (&attr);
9057 break;
9058 case DW_AT_location:
0963b4bd 9059 /* Support the .debug_loc offsets. */
8e19ed76
PS
9060 if (attr_form_is_block (&attr))
9061 {
9062 part_die->locdesc = DW_BLOCK (&attr);
9063 }
3690dd37 9064 else if (attr_form_is_section_offset (&attr))
8e19ed76 9065 {
4d3c2250 9066 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9067 }
9068 else
9069 {
4d3c2250
KB
9070 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9071 "partial symbol information");
8e19ed76 9072 }
c906108c 9073 break;
c906108c
SS
9074 case DW_AT_external:
9075 part_die->is_external = DW_UNSND (&attr);
9076 break;
9077 case DW_AT_declaration:
9078 part_die->is_declaration = DW_UNSND (&attr);
9079 break;
9080 case DW_AT_type:
9081 part_die->has_type = 1;
9082 break;
9083 case DW_AT_abstract_origin:
9084 case DW_AT_specification:
72bf9492
DJ
9085 case DW_AT_extension:
9086 part_die->has_specification = 1;
c764a876 9087 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9088 break;
9089 case DW_AT_sibling:
9090 /* Ignore absolute siblings, they might point outside of
9091 the current compile unit. */
9092 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9093 complaint (&symfile_complaints,
9094 _("ignoring absolute DW_AT_sibling"));
c906108c 9095 else
93311388 9096 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9097 break;
fa4028e9
JB
9098 case DW_AT_byte_size:
9099 part_die->has_byte_size = 1;
9100 break;
68511cec
CES
9101 case DW_AT_calling_convention:
9102 /* DWARF doesn't provide a way to identify a program's source-level
9103 entry point. DW_AT_calling_convention attributes are only meant
9104 to describe functions' calling conventions.
9105
9106 However, because it's a necessary piece of information in
9107 Fortran, and because DW_CC_program is the only piece of debugging
9108 information whose definition refers to a 'main program' at all,
9109 several compilers have begun marking Fortran main programs with
9110 DW_CC_program --- even when those functions use the standard
9111 calling conventions.
9112
9113 So until DWARF specifies a way to provide this information and
9114 compilers pick up the new representation, we'll support this
9115 practice. */
9116 if (DW_UNSND (&attr) == DW_CC_program
9117 && cu->language == language_fortran)
01f8c46d
JK
9118 {
9119 set_main_name (part_die->name);
9120
9121 /* As this DIE has a static linkage the name would be difficult
9122 to look up later. */
9123 language_of_main = language_fortran;
9124 }
68511cec 9125 break;
c906108c
SS
9126 default:
9127 break;
9128 }
9129 }
9130
9373cf26
JK
9131 if (has_low_pc_attr && has_high_pc_attr)
9132 {
9133 /* When using the GNU linker, .gnu.linkonce. sections are used to
9134 eliminate duplicate copies of functions and vtables and such.
9135 The linker will arbitrarily choose one and discard the others.
9136 The AT_*_pc values for such functions refer to local labels in
9137 these sections. If the section from that file was discarded, the
9138 labels are not in the output, so the relocs get a value of 0.
9139 If this is a discarded function, mark the pc bounds as invalid,
9140 so that GDB will ignore it. */
9141 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9142 {
9143 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9144
9145 complaint (&symfile_complaints,
9146 _("DW_AT_low_pc %s is zero "
9147 "for DIE at 0x%x [in module %s]"),
9148 paddress (gdbarch, part_die->lowpc),
9149 part_die->offset, cu->objfile->name);
9150 }
9151 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9152 else if (part_die->lowpc >= part_die->highpc)
9153 {
9154 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9155
9156 complaint (&symfile_complaints,
9157 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9158 "for DIE at 0x%x [in module %s]"),
9159 paddress (gdbarch, part_die->lowpc),
9160 paddress (gdbarch, part_die->highpc),
9161 part_die->offset, cu->objfile->name);
9162 }
9163 else
9164 part_die->has_pc_info = 1;
9165 }
85cbf3d3 9166
c906108c
SS
9167 return info_ptr;
9168}
9169
72bf9492
DJ
9170/* Find a cached partial DIE at OFFSET in CU. */
9171
9172static struct partial_die_info *
c764a876 9173find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9174{
9175 struct partial_die_info *lookup_die = NULL;
9176 struct partial_die_info part_die;
9177
9178 part_die.offset = offset;
9179 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9180
72bf9492
DJ
9181 return lookup_die;
9182}
9183
348e048f
DE
9184/* Find a partial DIE at OFFSET, which may or may not be in CU,
9185 except in the case of .debug_types DIEs which do not reference
9186 outside their CU (they do however referencing other types via
55f1336d 9187 DW_FORM_ref_sig8). */
72bf9492
DJ
9188
9189static struct partial_die_info *
c764a876 9190find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9191{
5afb4e99
DJ
9192 struct dwarf2_per_cu_data *per_cu = NULL;
9193 struct partial_die_info *pd = NULL;
72bf9492 9194
348e048f
DE
9195 if (cu->per_cu->from_debug_types)
9196 {
9197 pd = find_partial_die_in_comp_unit (offset, cu);
9198 if (pd != NULL)
9199 return pd;
9200 goto not_found;
9201 }
9202
45452591 9203 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9204 {
9205 pd = find_partial_die_in_comp_unit (offset, cu);
9206 if (pd != NULL)
9207 return pd;
9208 }
72bf9492 9209
ae038cb0
DJ
9210 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9211
98bfdba5
PA
9212 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9213 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9214
9215 per_cu->cu->last_used = 0;
5afb4e99
DJ
9216 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9217
9218 if (pd == NULL && per_cu->load_all_dies == 0)
9219 {
9220 struct cleanup *back_to;
9221 struct partial_die_info comp_unit_die;
9222 struct abbrev_info *abbrev;
9223 unsigned int bytes_read;
9224 char *info_ptr;
9225
9226 per_cu->load_all_dies = 1;
9227
9228 /* Re-read the DIEs. */
9229 back_to = make_cleanup (null_cleanup, 0);
9230 if (per_cu->cu->dwarf2_abbrevs == NULL)
9231 {
9232 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9233 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9234 }
dce234bc 9235 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9236 + per_cu->cu->header.offset
9237 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9238 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9239 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9240 per_cu->cu->objfile->obfd,
9241 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9242 per_cu->cu);
9243 if (comp_unit_die.has_children)
93311388
DE
9244 load_partial_dies (per_cu->cu->objfile->obfd,
9245 dwarf2_per_objfile->info.buffer, info_ptr,
9246 0, per_cu->cu);
5afb4e99
DJ
9247 do_cleanups (back_to);
9248
9249 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9250 }
9251
348e048f
DE
9252 not_found:
9253
5afb4e99
DJ
9254 if (pd == NULL)
9255 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9256 _("could not find partial DIE 0x%x "
9257 "in cache [from module %s]\n"),
5afb4e99
DJ
9258 offset, bfd_get_filename (cu->objfile->obfd));
9259 return pd;
72bf9492
DJ
9260}
9261
abc72ce4
DE
9262/* See if we can figure out if the class lives in a namespace. We do
9263 this by looking for a member function; its demangled name will
9264 contain namespace info, if there is any. */
9265
9266static void
9267guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9268 struct dwarf2_cu *cu)
9269{
9270 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9271 what template types look like, because the demangler
9272 frequently doesn't give the same name as the debug info. We
9273 could fix this by only using the demangled name to get the
9274 prefix (but see comment in read_structure_type). */
9275
9276 struct partial_die_info *real_pdi;
9277 struct partial_die_info *child_pdi;
9278
9279 /* If this DIE (this DIE's specification, if any) has a parent, then
9280 we should not do this. We'll prepend the parent's fully qualified
9281 name when we create the partial symbol. */
9282
9283 real_pdi = struct_pdi;
9284 while (real_pdi->has_specification)
9285 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9286
9287 if (real_pdi->die_parent != NULL)
9288 return;
9289
9290 for (child_pdi = struct_pdi->die_child;
9291 child_pdi != NULL;
9292 child_pdi = child_pdi->die_sibling)
9293 {
9294 if (child_pdi->tag == DW_TAG_subprogram
9295 && child_pdi->linkage_name != NULL)
9296 {
9297 char *actual_class_name
9298 = language_class_name_from_physname (cu->language_defn,
9299 child_pdi->linkage_name);
9300 if (actual_class_name != NULL)
9301 {
9302 struct_pdi->name
9303 = obsavestring (actual_class_name,
9304 strlen (actual_class_name),
9305 &cu->objfile->objfile_obstack);
9306 xfree (actual_class_name);
9307 }
9308 break;
9309 }
9310 }
9311}
9312
72bf9492
DJ
9313/* Adjust PART_DIE before generating a symbol for it. This function
9314 may set the is_external flag or change the DIE's name. */
9315
9316static void
9317fixup_partial_die (struct partial_die_info *part_die,
9318 struct dwarf2_cu *cu)
9319{
abc72ce4
DE
9320 /* Once we've fixed up a die, there's no point in doing so again.
9321 This also avoids a memory leak if we were to call
9322 guess_partial_die_structure_name multiple times. */
9323 if (part_die->fixup_called)
9324 return;
9325
72bf9492
DJ
9326 /* If we found a reference attribute and the DIE has no name, try
9327 to find a name in the referred to DIE. */
9328
9329 if (part_die->name == NULL && part_die->has_specification)
9330 {
9331 struct partial_die_info *spec_die;
72bf9492 9332
10b3939b 9333 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9334
10b3939b 9335 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9336
9337 if (spec_die->name)
9338 {
9339 part_die->name = spec_die->name;
9340
9341 /* Copy DW_AT_external attribute if it is set. */
9342 if (spec_die->is_external)
9343 part_die->is_external = spec_die->is_external;
9344 }
9345 }
9346
9347 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9348
9349 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9350 part_die->name = "(anonymous namespace)";
9351
abc72ce4
DE
9352 /* If there is no parent die to provide a namespace, and there are
9353 children, see if we can determine the namespace from their linkage
9354 name.
9355 NOTE: We need to do this even if cu->has_namespace_info != 0.
9356 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9357 if (cu->language == language_cplus
9358 && dwarf2_per_objfile->types.asection != NULL
9359 && part_die->die_parent == NULL
9360 && part_die->has_children
9361 && (part_die->tag == DW_TAG_class_type
9362 || part_die->tag == DW_TAG_structure_type
9363 || part_die->tag == DW_TAG_union_type))
9364 guess_partial_die_structure_name (part_die, cu);
9365
9366 part_die->fixup_called = 1;
72bf9492
DJ
9367}
9368
a8329558 9369/* Read an attribute value described by an attribute form. */
c906108c 9370
fe1b8b76 9371static gdb_byte *
a8329558 9372read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9373 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9374 struct dwarf2_cu *cu)
c906108c 9375{
e7c27a73 9376 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9377 unsigned int bytes_read;
9378 struct dwarf_block *blk;
9379
a8329558
KW
9380 attr->form = form;
9381 switch (form)
c906108c 9382 {
c906108c 9383 case DW_FORM_ref_addr:
ae411497
TT
9384 if (cu->header.version == 2)
9385 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9386 else
3e43a32a
MS
9387 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9388 &cu->header, &bytes_read);
ae411497
TT
9389 info_ptr += bytes_read;
9390 break;
9391 case DW_FORM_addr:
e7c27a73 9392 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9393 info_ptr += bytes_read;
c906108c
SS
9394 break;
9395 case DW_FORM_block2:
7b5a2f43 9396 blk = dwarf_alloc_block (cu);
c906108c
SS
9397 blk->size = read_2_bytes (abfd, info_ptr);
9398 info_ptr += 2;
9399 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9400 info_ptr += blk->size;
9401 DW_BLOCK (attr) = blk;
9402 break;
9403 case DW_FORM_block4:
7b5a2f43 9404 blk = dwarf_alloc_block (cu);
c906108c
SS
9405 blk->size = read_4_bytes (abfd, info_ptr);
9406 info_ptr += 4;
9407 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9408 info_ptr += blk->size;
9409 DW_BLOCK (attr) = blk;
9410 break;
9411 case DW_FORM_data2:
9412 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9413 info_ptr += 2;
9414 break;
9415 case DW_FORM_data4:
9416 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9417 info_ptr += 4;
9418 break;
9419 case DW_FORM_data8:
9420 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9421 info_ptr += 8;
9422 break;
2dc7f7b3
TT
9423 case DW_FORM_sec_offset:
9424 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9425 info_ptr += bytes_read;
9426 break;
c906108c 9427 case DW_FORM_string:
9b1c24c8 9428 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9429 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9430 info_ptr += bytes_read;
9431 break;
4bdf3d34
JJ
9432 case DW_FORM_strp:
9433 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9434 &bytes_read);
8285870a 9435 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9436 info_ptr += bytes_read;
9437 break;
2dc7f7b3 9438 case DW_FORM_exprloc:
c906108c 9439 case DW_FORM_block:
7b5a2f43 9440 blk = dwarf_alloc_block (cu);
c906108c
SS
9441 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9442 info_ptr += bytes_read;
9443 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9444 info_ptr += blk->size;
9445 DW_BLOCK (attr) = blk;
9446 break;
9447 case DW_FORM_block1:
7b5a2f43 9448 blk = dwarf_alloc_block (cu);
c906108c
SS
9449 blk->size = read_1_byte (abfd, info_ptr);
9450 info_ptr += 1;
9451 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9452 info_ptr += blk->size;
9453 DW_BLOCK (attr) = blk;
9454 break;
9455 case DW_FORM_data1:
9456 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9457 info_ptr += 1;
9458 break;
9459 case DW_FORM_flag:
9460 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9461 info_ptr += 1;
9462 break;
2dc7f7b3
TT
9463 case DW_FORM_flag_present:
9464 DW_UNSND (attr) = 1;
9465 break;
c906108c
SS
9466 case DW_FORM_sdata:
9467 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9468 info_ptr += bytes_read;
9469 break;
9470 case DW_FORM_udata:
9471 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9472 info_ptr += bytes_read;
9473 break;
9474 case DW_FORM_ref1:
10b3939b 9475 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9476 info_ptr += 1;
9477 break;
9478 case DW_FORM_ref2:
10b3939b 9479 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9480 info_ptr += 2;
9481 break;
9482 case DW_FORM_ref4:
10b3939b 9483 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9484 info_ptr += 4;
9485 break;
613e1657 9486 case DW_FORM_ref8:
10b3939b 9487 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9488 info_ptr += 8;
9489 break;
55f1336d 9490 case DW_FORM_ref_sig8:
348e048f
DE
9491 /* Convert the signature to something we can record in DW_UNSND
9492 for later lookup.
9493 NOTE: This is NULL if the type wasn't found. */
9494 DW_SIGNATURED_TYPE (attr) =
9495 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9496 info_ptr += 8;
9497 break;
c906108c 9498 case DW_FORM_ref_udata:
10b3939b
DJ
9499 DW_ADDR (attr) = (cu->header.offset
9500 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9501 info_ptr += bytes_read;
9502 break;
c906108c 9503 case DW_FORM_indirect:
a8329558
KW
9504 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9505 info_ptr += bytes_read;
e7c27a73 9506 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9507 break;
c906108c 9508 default:
8a3fe4f8 9509 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9510 dwarf_form_name (form),
9511 bfd_get_filename (abfd));
c906108c 9512 }
28e94949
JB
9513
9514 /* We have seen instances where the compiler tried to emit a byte
9515 size attribute of -1 which ended up being encoded as an unsigned
9516 0xffffffff. Although 0xffffffff is technically a valid size value,
9517 an object of this size seems pretty unlikely so we can relatively
9518 safely treat these cases as if the size attribute was invalid and
9519 treat them as zero by default. */
9520 if (attr->name == DW_AT_byte_size
9521 && form == DW_FORM_data4
9522 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9523 {
9524 complaint
9525 (&symfile_complaints,
43bbcdc2
PH
9526 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9527 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9528 DW_UNSND (attr) = 0;
9529 }
28e94949 9530
c906108c
SS
9531 return info_ptr;
9532}
9533
a8329558
KW
9534/* Read an attribute described by an abbreviated attribute. */
9535
fe1b8b76 9536static gdb_byte *
a8329558 9537read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9538 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9539{
9540 attr->name = abbrev->name;
e7c27a73 9541 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9542}
9543
0963b4bd 9544/* Read dwarf information from a buffer. */
c906108c
SS
9545
9546static unsigned int
fe1b8b76 9547read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9548{
fe1b8b76 9549 return bfd_get_8 (abfd, buf);
c906108c
SS
9550}
9551
9552static int
fe1b8b76 9553read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9554{
fe1b8b76 9555 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9556}
9557
9558static unsigned int
fe1b8b76 9559read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9560{
fe1b8b76 9561 return bfd_get_16 (abfd, buf);
c906108c
SS
9562}
9563
9564static int
fe1b8b76 9565read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9566{
fe1b8b76 9567 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9568}
9569
9570static unsigned int
fe1b8b76 9571read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9572{
fe1b8b76 9573 return bfd_get_32 (abfd, buf);
c906108c
SS
9574}
9575
9576static int
fe1b8b76 9577read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9578{
fe1b8b76 9579 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9580}
9581
93311388 9582static ULONGEST
fe1b8b76 9583read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9584{
fe1b8b76 9585 return bfd_get_64 (abfd, buf);
c906108c
SS
9586}
9587
9588static CORE_ADDR
fe1b8b76 9589read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9590 unsigned int *bytes_read)
c906108c 9591{
e7c27a73 9592 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9593 CORE_ADDR retval = 0;
9594
107d2387 9595 if (cu_header->signed_addr_p)
c906108c 9596 {
107d2387
AC
9597 switch (cu_header->addr_size)
9598 {
9599 case 2:
fe1b8b76 9600 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9601 break;
9602 case 4:
fe1b8b76 9603 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9604 break;
9605 case 8:
fe1b8b76 9606 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9607 break;
9608 default:
8e65ff28 9609 internal_error (__FILE__, __LINE__,
e2e0b3e5 9610 _("read_address: bad switch, signed [in module %s]"),
659b0389 9611 bfd_get_filename (abfd));
107d2387
AC
9612 }
9613 }
9614 else
9615 {
9616 switch (cu_header->addr_size)
9617 {
9618 case 2:
fe1b8b76 9619 retval = bfd_get_16 (abfd, buf);
107d2387
AC
9620 break;
9621 case 4:
fe1b8b76 9622 retval = bfd_get_32 (abfd, buf);
107d2387
AC
9623 break;
9624 case 8:
fe1b8b76 9625 retval = bfd_get_64 (abfd, buf);
107d2387
AC
9626 break;
9627 default:
8e65ff28 9628 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
9629 _("read_address: bad switch, "
9630 "unsigned [in module %s]"),
659b0389 9631 bfd_get_filename (abfd));
107d2387 9632 }
c906108c 9633 }
64367e0a 9634
107d2387
AC
9635 *bytes_read = cu_header->addr_size;
9636 return retval;
c906108c
SS
9637}
9638
f7ef9339 9639/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
9640 specification allows the initial length to take up either 4 bytes
9641 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9642 bytes describe the length and all offsets will be 8 bytes in length
9643 instead of 4.
9644
f7ef9339
KB
9645 An older, non-standard 64-bit format is also handled by this
9646 function. The older format in question stores the initial length
9647 as an 8-byte quantity without an escape value. Lengths greater
9648 than 2^32 aren't very common which means that the initial 4 bytes
9649 is almost always zero. Since a length value of zero doesn't make
9650 sense for the 32-bit format, this initial zero can be considered to
9651 be an escape value which indicates the presence of the older 64-bit
9652 format. As written, the code can't detect (old format) lengths
917c78fc
MK
9653 greater than 4GB. If it becomes necessary to handle lengths
9654 somewhat larger than 4GB, we could allow other small values (such
9655 as the non-sensical values of 1, 2, and 3) to also be used as
9656 escape values indicating the presence of the old format.
f7ef9339 9657
917c78fc
MK
9658 The value returned via bytes_read should be used to increment the
9659 relevant pointer after calling read_initial_length().
c764a876 9660
613e1657
KB
9661 [ Note: read_initial_length() and read_offset() are based on the
9662 document entitled "DWARF Debugging Information Format", revision
f7ef9339 9663 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
9664 from:
9665
f7ef9339 9666 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 9667
613e1657
KB
9668 This document is only a draft and is subject to change. (So beware.)
9669
f7ef9339 9670 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
9671 determined empirically by examining 64-bit ELF files produced by
9672 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
9673
9674 - Kevin, July 16, 2002
613e1657
KB
9675 ] */
9676
9677static LONGEST
c764a876 9678read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 9679{
fe1b8b76 9680 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 9681
dd373385 9682 if (length == 0xffffffff)
613e1657 9683 {
fe1b8b76 9684 length = bfd_get_64 (abfd, buf + 4);
613e1657 9685 *bytes_read = 12;
613e1657 9686 }
dd373385 9687 else if (length == 0)
f7ef9339 9688 {
dd373385 9689 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 9690 length = bfd_get_64 (abfd, buf);
f7ef9339 9691 *bytes_read = 8;
f7ef9339 9692 }
613e1657
KB
9693 else
9694 {
9695 *bytes_read = 4;
613e1657
KB
9696 }
9697
c764a876
DE
9698 return length;
9699}
dd373385 9700
c764a876
DE
9701/* Cover function for read_initial_length.
9702 Returns the length of the object at BUF, and stores the size of the
9703 initial length in *BYTES_READ and stores the size that offsets will be in
9704 *OFFSET_SIZE.
9705 If the initial length size is not equivalent to that specified in
9706 CU_HEADER then issue a complaint.
9707 This is useful when reading non-comp-unit headers. */
dd373385 9708
c764a876
DE
9709static LONGEST
9710read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9711 const struct comp_unit_head *cu_header,
9712 unsigned int *bytes_read,
9713 unsigned int *offset_size)
9714{
9715 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9716
9717 gdb_assert (cu_header->initial_length_size == 4
9718 || cu_header->initial_length_size == 8
9719 || cu_header->initial_length_size == 12);
9720
9721 if (cu_header->initial_length_size != *bytes_read)
9722 complaint (&symfile_complaints,
9723 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 9724
c764a876 9725 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 9726 return length;
613e1657
KB
9727}
9728
9729/* Read an offset from the data stream. The size of the offset is
917c78fc 9730 given by cu_header->offset_size. */
613e1657
KB
9731
9732static LONGEST
fe1b8b76 9733read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 9734 unsigned int *bytes_read)
c764a876
DE
9735{
9736 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 9737
c764a876
DE
9738 *bytes_read = cu_header->offset_size;
9739 return offset;
9740}
9741
9742/* Read an offset from the data stream. */
9743
9744static LONGEST
9745read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
9746{
9747 LONGEST retval = 0;
9748
c764a876 9749 switch (offset_size)
613e1657
KB
9750 {
9751 case 4:
fe1b8b76 9752 retval = bfd_get_32 (abfd, buf);
613e1657
KB
9753 break;
9754 case 8:
fe1b8b76 9755 retval = bfd_get_64 (abfd, buf);
613e1657
KB
9756 break;
9757 default:
8e65ff28 9758 internal_error (__FILE__, __LINE__,
c764a876 9759 _("read_offset_1: bad switch [in module %s]"),
659b0389 9760 bfd_get_filename (abfd));
613e1657
KB
9761 }
9762
917c78fc 9763 return retval;
613e1657
KB
9764}
9765
fe1b8b76
JB
9766static gdb_byte *
9767read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
9768{
9769 /* If the size of a host char is 8 bits, we can return a pointer
9770 to the buffer, otherwise we have to copy the data to a buffer
9771 allocated on the temporary obstack. */
4bdf3d34 9772 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 9773 return buf;
c906108c
SS
9774}
9775
9776static char *
9b1c24c8 9777read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
9778{
9779 /* If the size of a host char is 8 bits, we can return a pointer
9780 to the string, otherwise we have to copy the string to a buffer
9781 allocated on the temporary obstack. */
4bdf3d34 9782 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
9783 if (*buf == '\0')
9784 {
9785 *bytes_read_ptr = 1;
9786 return NULL;
9787 }
fe1b8b76
JB
9788 *bytes_read_ptr = strlen ((char *) buf) + 1;
9789 return (char *) buf;
4bdf3d34
JJ
9790}
9791
9792static char *
fe1b8b76 9793read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
9794 const struct comp_unit_head *cu_header,
9795 unsigned int *bytes_read_ptr)
9796{
c764a876 9797 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 9798
be391dca 9799 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 9800 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 9801 {
8a3fe4f8 9802 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 9803 bfd_get_filename (abfd));
4bdf3d34 9804 return NULL;
c906108c 9805 }
dce234bc 9806 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 9807 {
3e43a32a
MS
9808 error (_("DW_FORM_strp pointing outside of "
9809 ".debug_str section [in module %s]"),
9810 bfd_get_filename (abfd));
c906108c
SS
9811 return NULL;
9812 }
4bdf3d34 9813 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 9814 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 9815 return NULL;
dce234bc 9816 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
9817}
9818
ce5d95e1 9819static unsigned long
fe1b8b76 9820read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9821{
ce5d95e1
JB
9822 unsigned long result;
9823 unsigned int num_read;
c906108c
SS
9824 int i, shift;
9825 unsigned char byte;
9826
9827 result = 0;
9828 shift = 0;
9829 num_read = 0;
9830 i = 0;
9831 while (1)
9832 {
fe1b8b76 9833 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9834 buf++;
9835 num_read++;
ce5d95e1 9836 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
9837 if ((byte & 128) == 0)
9838 {
9839 break;
9840 }
9841 shift += 7;
9842 }
9843 *bytes_read_ptr = num_read;
9844 return result;
9845}
9846
ce5d95e1 9847static long
fe1b8b76 9848read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 9849{
ce5d95e1 9850 long result;
77e0b926 9851 int i, shift, num_read;
c906108c
SS
9852 unsigned char byte;
9853
9854 result = 0;
9855 shift = 0;
c906108c
SS
9856 num_read = 0;
9857 i = 0;
9858 while (1)
9859 {
fe1b8b76 9860 byte = bfd_get_8 (abfd, buf);
c906108c
SS
9861 buf++;
9862 num_read++;
ce5d95e1 9863 result |= ((long)(byte & 127) << shift);
c906108c
SS
9864 shift += 7;
9865 if ((byte & 128) == 0)
9866 {
9867 break;
9868 }
9869 }
77e0b926
DJ
9870 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9871 result |= -(((long)1) << shift);
c906108c
SS
9872 *bytes_read_ptr = num_read;
9873 return result;
9874}
9875
4bb7a0a7
DJ
9876/* Return a pointer to just past the end of an LEB128 number in BUF. */
9877
fe1b8b76
JB
9878static gdb_byte *
9879skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
9880{
9881 int byte;
9882
9883 while (1)
9884 {
fe1b8b76 9885 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9886 buf++;
9887 if ((byte & 128) == 0)
9888 return buf;
9889 }
9890}
9891
c906108c 9892static void
e142c38c 9893set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9894{
9895 switch (lang)
9896 {
9897 case DW_LANG_C89:
76bee0cc 9898 case DW_LANG_C99:
c906108c 9899 case DW_LANG_C:
e142c38c 9900 cu->language = language_c;
c906108c
SS
9901 break;
9902 case DW_LANG_C_plus_plus:
e142c38c 9903 cu->language = language_cplus;
c906108c 9904 break;
6aecb9c2
JB
9905 case DW_LANG_D:
9906 cu->language = language_d;
9907 break;
c906108c
SS
9908 case DW_LANG_Fortran77:
9909 case DW_LANG_Fortran90:
b21b22e0 9910 case DW_LANG_Fortran95:
e142c38c 9911 cu->language = language_fortran;
c906108c
SS
9912 break;
9913 case DW_LANG_Mips_Assembler:
e142c38c 9914 cu->language = language_asm;
c906108c 9915 break;
bebd888e 9916 case DW_LANG_Java:
e142c38c 9917 cu->language = language_java;
bebd888e 9918 break;
c906108c 9919 case DW_LANG_Ada83:
8aaf0b47 9920 case DW_LANG_Ada95:
bc5f45f8
JB
9921 cu->language = language_ada;
9922 break;
72019c9c
GM
9923 case DW_LANG_Modula2:
9924 cu->language = language_m2;
9925 break;
fe8e67fd
PM
9926 case DW_LANG_Pascal83:
9927 cu->language = language_pascal;
9928 break;
22566fbd
DJ
9929 case DW_LANG_ObjC:
9930 cu->language = language_objc;
9931 break;
c906108c
SS
9932 case DW_LANG_Cobol74:
9933 case DW_LANG_Cobol85:
c906108c 9934 default:
e142c38c 9935 cu->language = language_minimal;
c906108c
SS
9936 break;
9937 }
e142c38c 9938 cu->language_defn = language_def (cu->language);
c906108c
SS
9939}
9940
9941/* Return the named attribute or NULL if not there. */
9942
9943static struct attribute *
e142c38c 9944dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9945{
9946 unsigned int i;
9947 struct attribute *spec = NULL;
9948
9949 for (i = 0; i < die->num_attrs; ++i)
9950 {
9951 if (die->attrs[i].name == name)
10b3939b 9952 return &die->attrs[i];
c906108c
SS
9953 if (die->attrs[i].name == DW_AT_specification
9954 || die->attrs[i].name == DW_AT_abstract_origin)
9955 spec = &die->attrs[i];
9956 }
c906108c 9957
10b3939b 9958 if (spec)
f2f0e013
DJ
9959 {
9960 die = follow_die_ref (die, spec, &cu);
9961 return dwarf2_attr (die, name, cu);
9962 }
c5aa993b 9963
c906108c
SS
9964 return NULL;
9965}
9966
348e048f
DE
9967/* Return the named attribute or NULL if not there,
9968 but do not follow DW_AT_specification, etc.
9969 This is for use in contexts where we're reading .debug_types dies.
9970 Following DW_AT_specification, DW_AT_abstract_origin will take us
9971 back up the chain, and we want to go down. */
9972
9973static struct attribute *
9974dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9975 struct dwarf2_cu *cu)
9976{
9977 unsigned int i;
9978
9979 for (i = 0; i < die->num_attrs; ++i)
9980 if (die->attrs[i].name == name)
9981 return &die->attrs[i];
9982
9983 return NULL;
9984}
9985
05cf31d1
JB
9986/* Return non-zero iff the attribute NAME is defined for the given DIE,
9987 and holds a non-zero value. This function should only be used for
2dc7f7b3 9988 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9989
9990static int
9991dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9992{
9993 struct attribute *attr = dwarf2_attr (die, name, cu);
9994
9995 return (attr && DW_UNSND (attr));
9996}
9997
3ca72b44 9998static int
e142c38c 9999die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10000{
05cf31d1
JB
10001 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10002 which value is non-zero. However, we have to be careful with
10003 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10004 (via dwarf2_flag_true_p) follows this attribute. So we may
10005 end up accidently finding a declaration attribute that belongs
10006 to a different DIE referenced by the specification attribute,
10007 even though the given DIE does not have a declaration attribute. */
10008 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10009 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10010}
10011
63d06c5c 10012/* Return the die giving the specification for DIE, if there is
f2f0e013 10013 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10014 containing the return value on output. If there is no
10015 specification, but there is an abstract origin, that is
10016 returned. */
63d06c5c
DC
10017
10018static struct die_info *
f2f0e013 10019die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10020{
f2f0e013
DJ
10021 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10022 *spec_cu);
63d06c5c 10023
edb3359d
DJ
10024 if (spec_attr == NULL)
10025 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10026
63d06c5c
DC
10027 if (spec_attr == NULL)
10028 return NULL;
10029 else
f2f0e013 10030 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10031}
c906108c 10032
debd256d 10033/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10034 refers to.
10035 NOTE: This is also used as a "cleanup" function. */
10036
debd256d
JB
10037static void
10038free_line_header (struct line_header *lh)
10039{
10040 if (lh->standard_opcode_lengths)
a8bc7b56 10041 xfree (lh->standard_opcode_lengths);
debd256d
JB
10042
10043 /* Remember that all the lh->file_names[i].name pointers are
10044 pointers into debug_line_buffer, and don't need to be freed. */
10045 if (lh->file_names)
a8bc7b56 10046 xfree (lh->file_names);
debd256d
JB
10047
10048 /* Similarly for the include directory names. */
10049 if (lh->include_dirs)
a8bc7b56 10050 xfree (lh->include_dirs);
debd256d 10051
a8bc7b56 10052 xfree (lh);
debd256d
JB
10053}
10054
debd256d 10055/* Add an entry to LH's include directory table. */
ae2de4f8 10056
debd256d
JB
10057static void
10058add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10059{
debd256d
JB
10060 /* Grow the array if necessary. */
10061 if (lh->include_dirs_size == 0)
c5aa993b 10062 {
debd256d
JB
10063 lh->include_dirs_size = 1; /* for testing */
10064 lh->include_dirs = xmalloc (lh->include_dirs_size
10065 * sizeof (*lh->include_dirs));
10066 }
10067 else if (lh->num_include_dirs >= lh->include_dirs_size)
10068 {
10069 lh->include_dirs_size *= 2;
10070 lh->include_dirs = xrealloc (lh->include_dirs,
10071 (lh->include_dirs_size
10072 * sizeof (*lh->include_dirs)));
c5aa993b 10073 }
c906108c 10074
debd256d
JB
10075 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10076}
6e70227d 10077
debd256d 10078/* Add an entry to LH's file name table. */
ae2de4f8 10079
debd256d
JB
10080static void
10081add_file_name (struct line_header *lh,
10082 char *name,
10083 unsigned int dir_index,
10084 unsigned int mod_time,
10085 unsigned int length)
10086{
10087 struct file_entry *fe;
10088
10089 /* Grow the array if necessary. */
10090 if (lh->file_names_size == 0)
10091 {
10092 lh->file_names_size = 1; /* for testing */
10093 lh->file_names = xmalloc (lh->file_names_size
10094 * sizeof (*lh->file_names));
10095 }
10096 else if (lh->num_file_names >= lh->file_names_size)
10097 {
10098 lh->file_names_size *= 2;
10099 lh->file_names = xrealloc (lh->file_names,
10100 (lh->file_names_size
10101 * sizeof (*lh->file_names)));
10102 }
10103
10104 fe = &lh->file_names[lh->num_file_names++];
10105 fe->name = name;
10106 fe->dir_index = dir_index;
10107 fe->mod_time = mod_time;
10108 fe->length = length;
aaa75496 10109 fe->included_p = 0;
cb1df416 10110 fe->symtab = NULL;
debd256d 10111}
6e70227d 10112
debd256d 10113/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10114 .debug_line, according to the endianness of ABFD. Return a pointer
10115 to a struct line_header, allocated using xmalloc.
debd256d
JB
10116
10117 NOTE: the strings in the include directory and file name tables of
10118 the returned object point into debug_line_buffer, and must not be
10119 freed. */
ae2de4f8 10120
debd256d
JB
10121static struct line_header *
10122dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10123 struct dwarf2_cu *cu)
debd256d
JB
10124{
10125 struct cleanup *back_to;
10126 struct line_header *lh;
fe1b8b76 10127 gdb_byte *line_ptr;
c764a876 10128 unsigned int bytes_read, offset_size;
debd256d
JB
10129 int i;
10130 char *cur_dir, *cur_file;
10131
be391dca 10132 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10133 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10134 {
e2e0b3e5 10135 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10136 return 0;
10137 }
10138
a738430d
MK
10139 /* Make sure that at least there's room for the total_length field.
10140 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10141 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10142 {
4d3c2250 10143 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10144 return 0;
10145 }
10146
10147 lh = xmalloc (sizeof (*lh));
10148 memset (lh, 0, sizeof (*lh));
10149 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10150 (void *) lh);
10151
dce234bc 10152 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10153
a738430d 10154 /* Read in the header. */
6e70227d 10155 lh->total_length =
c764a876
DE
10156 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10157 &bytes_read, &offset_size);
debd256d 10158 line_ptr += bytes_read;
dce234bc
PP
10159 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10160 + dwarf2_per_objfile->line.size))
debd256d 10161 {
4d3c2250 10162 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10163 return 0;
10164 }
10165 lh->statement_program_end = line_ptr + lh->total_length;
10166 lh->version = read_2_bytes (abfd, line_ptr);
10167 line_ptr += 2;
c764a876
DE
10168 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10169 line_ptr += offset_size;
debd256d
JB
10170 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10171 line_ptr += 1;
2dc7f7b3
TT
10172 if (lh->version >= 4)
10173 {
10174 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10175 line_ptr += 1;
10176 }
10177 else
10178 lh->maximum_ops_per_instruction = 1;
10179
10180 if (lh->maximum_ops_per_instruction == 0)
10181 {
10182 lh->maximum_ops_per_instruction = 1;
10183 complaint (&symfile_complaints,
3e43a32a
MS
10184 _("invalid maximum_ops_per_instruction "
10185 "in `.debug_line' section"));
2dc7f7b3
TT
10186 }
10187
debd256d
JB
10188 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10189 line_ptr += 1;
10190 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10191 line_ptr += 1;
10192 lh->line_range = read_1_byte (abfd, line_ptr);
10193 line_ptr += 1;
10194 lh->opcode_base = read_1_byte (abfd, line_ptr);
10195 line_ptr += 1;
10196 lh->standard_opcode_lengths
fe1b8b76 10197 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10198
10199 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10200 for (i = 1; i < lh->opcode_base; ++i)
10201 {
10202 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10203 line_ptr += 1;
10204 }
10205
a738430d 10206 /* Read directory table. */
9b1c24c8 10207 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10208 {
10209 line_ptr += bytes_read;
10210 add_include_dir (lh, cur_dir);
10211 }
10212 line_ptr += bytes_read;
10213
a738430d 10214 /* Read file name table. */
9b1c24c8 10215 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10216 {
10217 unsigned int dir_index, mod_time, length;
10218
10219 line_ptr += bytes_read;
10220 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10221 line_ptr += bytes_read;
10222 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10223 line_ptr += bytes_read;
10224 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10225 line_ptr += bytes_read;
10226
10227 add_file_name (lh, cur_file, dir_index, mod_time, length);
10228 }
10229 line_ptr += bytes_read;
6e70227d 10230 lh->statement_program_start = line_ptr;
debd256d 10231
dce234bc
PP
10232 if (line_ptr > (dwarf2_per_objfile->line.buffer
10233 + dwarf2_per_objfile->line.size))
4d3c2250 10234 complaint (&symfile_complaints,
3e43a32a
MS
10235 _("line number info header doesn't "
10236 "fit in `.debug_line' section"));
debd256d
JB
10237
10238 discard_cleanups (back_to);
10239 return lh;
10240}
c906108c 10241
5fb290d7
DJ
10242/* This function exists to work around a bug in certain compilers
10243 (particularly GCC 2.95), in which the first line number marker of a
10244 function does not show up until after the prologue, right before
10245 the second line number marker. This function shifts ADDRESS down
10246 to the beginning of the function if necessary, and is called on
10247 addresses passed to record_line. */
10248
10249static CORE_ADDR
e142c38c 10250check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10251{
10252 struct function_range *fn;
10253
10254 /* Find the function_range containing address. */
e142c38c 10255 if (!cu->first_fn)
5fb290d7
DJ
10256 return address;
10257
e142c38c
DJ
10258 if (!cu->cached_fn)
10259 cu->cached_fn = cu->first_fn;
5fb290d7 10260
e142c38c 10261 fn = cu->cached_fn;
5fb290d7
DJ
10262 while (fn)
10263 if (fn->lowpc <= address && fn->highpc > address)
10264 goto found;
10265 else
10266 fn = fn->next;
10267
e142c38c
DJ
10268 fn = cu->first_fn;
10269 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10270 if (fn->lowpc <= address && fn->highpc > address)
10271 goto found;
10272 else
10273 fn = fn->next;
10274
10275 return address;
10276
10277 found:
10278 if (fn->seen_line)
10279 return address;
10280 if (address != fn->lowpc)
4d3c2250 10281 complaint (&symfile_complaints,
e2e0b3e5 10282 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10283 (unsigned long) address, fn->name);
5fb290d7
DJ
10284 fn->seen_line = 1;
10285 return fn->lowpc;
10286}
10287
c6da4cef
DE
10288/* Subroutine of dwarf_decode_lines to simplify it.
10289 Return the file name of the psymtab for included file FILE_INDEX
10290 in line header LH of PST.
10291 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10292 If space for the result is malloc'd, it will be freed by a cleanup.
10293 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10294
10295static char *
10296psymtab_include_file_name (const struct line_header *lh, int file_index,
10297 const struct partial_symtab *pst,
10298 const char *comp_dir)
10299{
10300 const struct file_entry fe = lh->file_names [file_index];
10301 char *include_name = fe.name;
10302 char *include_name_to_compare = include_name;
10303 char *dir_name = NULL;
72b9f47f
TT
10304 const char *pst_filename;
10305 char *copied_name = NULL;
c6da4cef
DE
10306 int file_is_pst;
10307
10308 if (fe.dir_index)
10309 dir_name = lh->include_dirs[fe.dir_index - 1];
10310
10311 if (!IS_ABSOLUTE_PATH (include_name)
10312 && (dir_name != NULL || comp_dir != NULL))
10313 {
10314 /* Avoid creating a duplicate psymtab for PST.
10315 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10316 Before we do the comparison, however, we need to account
10317 for DIR_NAME and COMP_DIR.
10318 First prepend dir_name (if non-NULL). If we still don't
10319 have an absolute path prepend comp_dir (if non-NULL).
10320 However, the directory we record in the include-file's
10321 psymtab does not contain COMP_DIR (to match the
10322 corresponding symtab(s)).
10323
10324 Example:
10325
10326 bash$ cd /tmp
10327 bash$ gcc -g ./hello.c
10328 include_name = "hello.c"
10329 dir_name = "."
10330 DW_AT_comp_dir = comp_dir = "/tmp"
10331 DW_AT_name = "./hello.c" */
10332
10333 if (dir_name != NULL)
10334 {
10335 include_name = concat (dir_name, SLASH_STRING,
10336 include_name, (char *)NULL);
10337 include_name_to_compare = include_name;
10338 make_cleanup (xfree, include_name);
10339 }
10340 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10341 {
10342 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10343 include_name, (char *)NULL);
10344 }
10345 }
10346
10347 pst_filename = pst->filename;
10348 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10349 {
72b9f47f
TT
10350 copied_name = concat (pst->dirname, SLASH_STRING,
10351 pst_filename, (char *)NULL);
10352 pst_filename = copied_name;
c6da4cef
DE
10353 }
10354
1e3fad37 10355 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10356
10357 if (include_name_to_compare != include_name)
10358 xfree (include_name_to_compare);
72b9f47f
TT
10359 if (copied_name != NULL)
10360 xfree (copied_name);
c6da4cef
DE
10361
10362 if (file_is_pst)
10363 return NULL;
10364 return include_name;
10365}
10366
c91513d8
PP
10367/* Ignore this record_line request. */
10368
10369static void
10370noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10371{
10372 return;
10373}
10374
aaa75496
JB
10375/* Decode the Line Number Program (LNP) for the given line_header
10376 structure and CU. The actual information extracted and the type
10377 of structures created from the LNP depends on the value of PST.
10378
10379 1. If PST is NULL, then this procedure uses the data from the program
10380 to create all necessary symbol tables, and their linetables.
6e70227d 10381
aaa75496
JB
10382 2. If PST is not NULL, this procedure reads the program to determine
10383 the list of files included by the unit represented by PST, and
c6da4cef
DE
10384 builds all the associated partial symbol tables.
10385
10386 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10387 It is used for relative paths in the line table.
10388 NOTE: When processing partial symtabs (pst != NULL),
10389 comp_dir == pst->dirname.
10390
10391 NOTE: It is important that psymtabs have the same file name (via strcmp)
10392 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10393 symtab we don't use it in the name of the psymtabs we create.
10394 E.g. expand_line_sal requires this when finding psymtabs to expand.
10395 A good testcase for this is mb-inline.exp. */
debd256d 10396
c906108c 10397static void
72b9f47f 10398dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10399 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10400{
a8c50c1f 10401 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10402 gdb_byte *line_end;
a8c50c1f 10403 unsigned int bytes_read, extended_len;
c906108c 10404 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10405 CORE_ADDR baseaddr;
10406 struct objfile *objfile = cu->objfile;
fbf65064 10407 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10408 const int decode_for_pst_p = (pst != NULL);
cb1df416 10409 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
10410 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10411 = record_line;
e142c38c
DJ
10412
10413 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10414
debd256d
JB
10415 line_ptr = lh->statement_program_start;
10416 line_end = lh->statement_program_end;
c906108c
SS
10417
10418 /* Read the statement sequences until there's nothing left. */
10419 while (line_ptr < line_end)
10420 {
10421 /* state machine registers */
10422 CORE_ADDR address = 0;
10423 unsigned int file = 1;
10424 unsigned int line = 1;
10425 unsigned int column = 0;
debd256d 10426 int is_stmt = lh->default_is_stmt;
c906108c
SS
10427 int basic_block = 0;
10428 int end_sequence = 0;
fbf65064 10429 CORE_ADDR addr;
2dc7f7b3 10430 unsigned char op_index = 0;
c906108c 10431
aaa75496 10432 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10433 {
aaa75496 10434 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10435 /* lh->include_dirs and lh->file_names are 0-based, but the
10436 directory and file name numbers in the statement program
10437 are 1-based. */
10438 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10439 char *dir = NULL;
a738430d 10440
debd256d
JB
10441 if (fe->dir_index)
10442 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10443
10444 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10445 }
10446
a738430d 10447 /* Decode the table. */
c5aa993b 10448 while (!end_sequence)
c906108c
SS
10449 {
10450 op_code = read_1_byte (abfd, line_ptr);
10451 line_ptr += 1;
59205f5a
JB
10452 if (line_ptr > line_end)
10453 {
10454 dwarf2_debug_line_missing_end_sequence_complaint ();
10455 break;
10456 }
9aa1fe7e 10457
debd256d 10458 if (op_code >= lh->opcode_base)
6e70227d 10459 {
a738430d 10460 /* Special operand. */
debd256d 10461 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10462 address += (((op_index + (adj_opcode / lh->line_range))
10463 / lh->maximum_ops_per_instruction)
10464 * lh->minimum_instruction_length);
10465 op_index = ((op_index + (adj_opcode / lh->line_range))
10466 % lh->maximum_ops_per_instruction);
debd256d 10467 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10468 if (lh->num_file_names < file || file == 0)
25e43795 10469 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10470 /* For now we ignore lines not starting on an
10471 instruction boundary. */
10472 else if (op_index == 0)
25e43795
DJ
10473 {
10474 lh->file_names[file - 1].included_p = 1;
ca5f395d 10475 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10476 {
10477 if (last_subfile != current_subfile)
10478 {
10479 addr = gdbarch_addr_bits_remove (gdbarch, address);
10480 if (last_subfile)
c91513d8 10481 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10482 last_subfile = current_subfile;
10483 }
25e43795 10484 /* Append row to matrix using current values. */
fbf65064
UW
10485 addr = check_cu_functions (address, cu);
10486 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10487 (*p_record_line) (current_subfile, line, addr);
366da635 10488 }
25e43795 10489 }
ca5f395d 10490 basic_block = 0;
9aa1fe7e
GK
10491 }
10492 else switch (op_code)
c906108c
SS
10493 {
10494 case DW_LNS_extended_op:
3e43a32a
MS
10495 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10496 &bytes_read);
473b7be6 10497 line_ptr += bytes_read;
a8c50c1f 10498 extended_end = line_ptr + extended_len;
c906108c
SS
10499 extended_op = read_1_byte (abfd, line_ptr);
10500 line_ptr += 1;
10501 switch (extended_op)
10502 {
10503 case DW_LNE_end_sequence:
c91513d8 10504 p_record_line = record_line;
c906108c 10505 end_sequence = 1;
c906108c
SS
10506 break;
10507 case DW_LNE_set_address:
e7c27a73 10508 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
10509
10510 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10511 {
10512 /* This line table is for a function which has been
10513 GCd by the linker. Ignore it. PR gdb/12528 */
10514
10515 long line_offset
10516 = line_ptr - dwarf2_per_objfile->line.buffer;
10517
10518 complaint (&symfile_complaints,
10519 _(".debug_line address at offset 0x%lx is 0 "
10520 "[in module %s]"),
10521 line_offset, cu->objfile->name);
10522 p_record_line = noop_record_line;
10523 }
10524
2dc7f7b3 10525 op_index = 0;
107d2387
AC
10526 line_ptr += bytes_read;
10527 address += baseaddr;
c906108c
SS
10528 break;
10529 case DW_LNE_define_file:
debd256d
JB
10530 {
10531 char *cur_file;
10532 unsigned int dir_index, mod_time, length;
6e70227d 10533
3e43a32a
MS
10534 cur_file = read_direct_string (abfd, line_ptr,
10535 &bytes_read);
debd256d
JB
10536 line_ptr += bytes_read;
10537 dir_index =
10538 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10539 line_ptr += bytes_read;
10540 mod_time =
10541 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10542 line_ptr += bytes_read;
10543 length =
10544 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10545 line_ptr += bytes_read;
10546 add_file_name (lh, cur_file, dir_index, mod_time, length);
10547 }
c906108c 10548 break;
d0c6ba3d
CC
10549 case DW_LNE_set_discriminator:
10550 /* The discriminator is not interesting to the debugger;
10551 just ignore it. */
10552 line_ptr = extended_end;
10553 break;
c906108c 10554 default:
4d3c2250 10555 complaint (&symfile_complaints,
e2e0b3e5 10556 _("mangled .debug_line section"));
debd256d 10557 return;
c906108c 10558 }
a8c50c1f
DJ
10559 /* Make sure that we parsed the extended op correctly. If e.g.
10560 we expected a different address size than the producer used,
10561 we may have read the wrong number of bytes. */
10562 if (line_ptr != extended_end)
10563 {
10564 complaint (&symfile_complaints,
10565 _("mangled .debug_line section"));
10566 return;
10567 }
c906108c
SS
10568 break;
10569 case DW_LNS_copy:
59205f5a 10570 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10571 dwarf2_debug_line_missing_file_complaint ();
10572 else
366da635 10573 {
25e43795 10574 lh->file_names[file - 1].included_p = 1;
ca5f395d 10575 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10576 {
10577 if (last_subfile != current_subfile)
10578 {
10579 addr = gdbarch_addr_bits_remove (gdbarch, address);
10580 if (last_subfile)
c91513d8 10581 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10582 last_subfile = current_subfile;
10583 }
10584 addr = check_cu_functions (address, cu);
10585 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10586 (*p_record_line) (current_subfile, line, addr);
fbf65064 10587 }
366da635 10588 }
c906108c
SS
10589 basic_block = 0;
10590 break;
10591 case DW_LNS_advance_pc:
2dc7f7b3
TT
10592 {
10593 CORE_ADDR adjust
10594 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10595
10596 address += (((op_index + adjust)
10597 / lh->maximum_ops_per_instruction)
10598 * lh->minimum_instruction_length);
10599 op_index = ((op_index + adjust)
10600 % lh->maximum_ops_per_instruction);
10601 line_ptr += bytes_read;
10602 }
c906108c
SS
10603 break;
10604 case DW_LNS_advance_line:
10605 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10606 line_ptr += bytes_read;
10607 break;
10608 case DW_LNS_set_file:
debd256d 10609 {
a738430d
MK
10610 /* The arrays lh->include_dirs and lh->file_names are
10611 0-based, but the directory and file name numbers in
10612 the statement program are 1-based. */
debd256d 10613 struct file_entry *fe;
4f1520fb 10614 char *dir = NULL;
a738430d 10615
debd256d
JB
10616 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10617 line_ptr += bytes_read;
59205f5a 10618 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10619 dwarf2_debug_line_missing_file_complaint ();
10620 else
10621 {
10622 fe = &lh->file_names[file - 1];
10623 if (fe->dir_index)
10624 dir = lh->include_dirs[fe->dir_index - 1];
10625 if (!decode_for_pst_p)
10626 {
10627 last_subfile = current_subfile;
10628 dwarf2_start_subfile (fe->name, dir, comp_dir);
10629 }
10630 }
debd256d 10631 }
c906108c
SS
10632 break;
10633 case DW_LNS_set_column:
10634 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10635 line_ptr += bytes_read;
10636 break;
10637 case DW_LNS_negate_stmt:
10638 is_stmt = (!is_stmt);
10639 break;
10640 case DW_LNS_set_basic_block:
10641 basic_block = 1;
10642 break;
c2c6d25f
JM
10643 /* Add to the address register of the state machine the
10644 address increment value corresponding to special opcode
a738430d
MK
10645 255. I.e., this value is scaled by the minimum
10646 instruction length since special opcode 255 would have
b021a221 10647 scaled the increment. */
c906108c 10648 case DW_LNS_const_add_pc:
2dc7f7b3
TT
10649 {
10650 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10651
10652 address += (((op_index + adjust)
10653 / lh->maximum_ops_per_instruction)
10654 * lh->minimum_instruction_length);
10655 op_index = ((op_index + adjust)
10656 % lh->maximum_ops_per_instruction);
10657 }
c906108c
SS
10658 break;
10659 case DW_LNS_fixed_advance_pc:
10660 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 10661 op_index = 0;
c906108c
SS
10662 line_ptr += 2;
10663 break;
9aa1fe7e 10664 default:
a738430d
MK
10665 {
10666 /* Unknown standard opcode, ignore it. */
9aa1fe7e 10667 int i;
a738430d 10668
debd256d 10669 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
10670 {
10671 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10672 line_ptr += bytes_read;
10673 }
10674 }
c906108c
SS
10675 }
10676 }
59205f5a
JB
10677 if (lh->num_file_names < file || file == 0)
10678 dwarf2_debug_line_missing_file_complaint ();
10679 else
10680 {
10681 lh->file_names[file - 1].included_p = 1;
10682 if (!decode_for_pst_p)
fbf65064
UW
10683 {
10684 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 10685 (*p_record_line) (current_subfile, 0, addr);
fbf65064 10686 }
59205f5a 10687 }
c906108c 10688 }
aaa75496
JB
10689
10690 if (decode_for_pst_p)
10691 {
10692 int file_index;
10693
10694 /* Now that we're done scanning the Line Header Program, we can
10695 create the psymtab of each included file. */
10696 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10697 if (lh->file_names[file_index].included_p == 1)
10698 {
c6da4cef
DE
10699 char *include_name =
10700 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10701 if (include_name != NULL)
aaa75496
JB
10702 dwarf2_create_include_psymtab (include_name, pst, objfile);
10703 }
10704 }
cb1df416
DJ
10705 else
10706 {
10707 /* Make sure a symtab is created for every file, even files
10708 which contain only variables (i.e. no code with associated
10709 line numbers). */
10710
10711 int i;
10712 struct file_entry *fe;
10713
10714 for (i = 0; i < lh->num_file_names; i++)
10715 {
10716 char *dir = NULL;
9a619af0 10717
cb1df416
DJ
10718 fe = &lh->file_names[i];
10719 if (fe->dir_index)
10720 dir = lh->include_dirs[fe->dir_index - 1];
10721 dwarf2_start_subfile (fe->name, dir, comp_dir);
10722
10723 /* Skip the main file; we don't need it, and it must be
10724 allocated last, so that it will show up before the
10725 non-primary symtabs in the objfile's symtab list. */
10726 if (current_subfile == first_subfile)
10727 continue;
10728
10729 if (current_subfile->symtab == NULL)
10730 current_subfile->symtab = allocate_symtab (current_subfile->name,
10731 cu->objfile);
10732 fe->symtab = current_subfile->symtab;
10733 }
10734 }
c906108c
SS
10735}
10736
10737/* Start a subfile for DWARF. FILENAME is the name of the file and
10738 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
10739 or NULL if not known. COMP_DIR is the compilation directory for the
10740 linetable's compilation unit or NULL if not known.
c906108c
SS
10741 This routine tries to keep line numbers from identical absolute and
10742 relative file names in a common subfile.
10743
10744 Using the `list' example from the GDB testsuite, which resides in
10745 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10746 of /srcdir/list0.c yields the following debugging information for list0.c:
10747
c5aa993b
JM
10748 DW_AT_name: /srcdir/list0.c
10749 DW_AT_comp_dir: /compdir
357e46e7 10750 files.files[0].name: list0.h
c5aa993b 10751 files.files[0].dir: /srcdir
357e46e7 10752 files.files[1].name: list0.c
c5aa993b 10753 files.files[1].dir: /srcdir
c906108c
SS
10754
10755 The line number information for list0.c has to end up in a single
4f1520fb
FR
10756 subfile, so that `break /srcdir/list0.c:1' works as expected.
10757 start_subfile will ensure that this happens provided that we pass the
10758 concatenation of files.files[1].dir and files.files[1].name as the
10759 subfile's name. */
c906108c
SS
10760
10761static void
3e43a32a
MS
10762dwarf2_start_subfile (char *filename, const char *dirname,
10763 const char *comp_dir)
c906108c 10764{
4f1520fb
FR
10765 char *fullname;
10766
10767 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10768 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10769 second argument to start_subfile. To be consistent, we do the
10770 same here. In order not to lose the line information directory,
10771 we concatenate it to the filename when it makes sense.
10772 Note that the Dwarf3 standard says (speaking of filenames in line
10773 information): ``The directory index is ignored for file names
10774 that represent full path names''. Thus ignoring dirname in the
10775 `else' branch below isn't an issue. */
c906108c 10776
d5166ae1 10777 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
10778 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10779 else
10780 fullname = filename;
c906108c 10781
4f1520fb
FR
10782 start_subfile (fullname, comp_dir);
10783
10784 if (fullname != filename)
10785 xfree (fullname);
c906108c
SS
10786}
10787
4c2df51b
DJ
10788static void
10789var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 10790 struct dwarf2_cu *cu)
4c2df51b 10791{
e7c27a73
DJ
10792 struct objfile *objfile = cu->objfile;
10793 struct comp_unit_head *cu_header = &cu->header;
10794
4c2df51b
DJ
10795 /* NOTE drow/2003-01-30: There used to be a comment and some special
10796 code here to turn a symbol with DW_AT_external and a
10797 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10798 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10799 with some versions of binutils) where shared libraries could have
10800 relocations against symbols in their debug information - the
10801 minimal symbol would have the right address, but the debug info
10802 would not. It's no longer necessary, because we will explicitly
10803 apply relocations when we read in the debug information now. */
10804
10805 /* A DW_AT_location attribute with no contents indicates that a
10806 variable has been optimized away. */
10807 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10808 {
10809 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10810 return;
10811 }
10812
10813 /* Handle one degenerate form of location expression specially, to
10814 preserve GDB's previous behavior when section offsets are
10815 specified. If this is just a DW_OP_addr then mark this symbol
10816 as LOC_STATIC. */
10817
10818 if (attr_form_is_block (attr)
10819 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10820 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10821 {
891d2f0b 10822 unsigned int dummy;
4c2df51b
DJ
10823
10824 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 10825 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 10826 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
10827 fixup_symbol_section (sym, objfile);
10828 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10829 SYMBOL_SECTION (sym));
4c2df51b
DJ
10830 return;
10831 }
10832
10833 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10834 expression evaluator, and use LOC_COMPUTED only when necessary
10835 (i.e. when the value of a register or memory location is
10836 referenced, or a thread-local block, etc.). Then again, it might
10837 not be worthwhile. I'm assuming that it isn't unless performance
10838 or memory numbers show me otherwise. */
10839
e7c27a73 10840 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
10841 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10842}
10843
c906108c
SS
10844/* Given a pointer to a DWARF information entry, figure out if we need
10845 to make a symbol table entry for it, and if so, create a new entry
10846 and return a pointer to it.
10847 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
10848 used the passed type.
10849 If SPACE is not NULL, use it to hold the new symbol. If it is
10850 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
10851
10852static struct symbol *
34eaf542
TT
10853new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10854 struct symbol *space)
c906108c 10855{
e7c27a73 10856 struct objfile *objfile = cu->objfile;
c906108c
SS
10857 struct symbol *sym = NULL;
10858 char *name;
10859 struct attribute *attr = NULL;
10860 struct attribute *attr2 = NULL;
e142c38c 10861 CORE_ADDR baseaddr;
e37fd15a
SW
10862 struct pending **list_to_add = NULL;
10863
edb3359d 10864 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
10865
10866 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10867
94af9270 10868 name = dwarf2_name (die, cu);
c906108c
SS
10869 if (name)
10870 {
94af9270 10871 const char *linkagename;
34eaf542 10872 int suppress_add = 0;
94af9270 10873
34eaf542
TT
10874 if (space)
10875 sym = space;
10876 else
10877 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 10878 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
10879
10880 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 10881 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
10882 linkagename = dwarf2_physname (name, die, cu);
10883 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 10884
f55ee35c
JK
10885 /* Fortran does not have mangling standard and the mangling does differ
10886 between gfortran, iFort etc. */
10887 if (cu->language == language_fortran
b250c185 10888 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
10889 symbol_set_demangled_name (&(sym->ginfo),
10890 (char *) dwarf2_full_name (name, die, cu),
10891 NULL);
f55ee35c 10892
c906108c 10893 /* Default assumptions.
c5aa993b 10894 Use the passed type or decode it from the die. */
176620f1 10895 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 10896 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
10897 if (type != NULL)
10898 SYMBOL_TYPE (sym) = type;
10899 else
e7c27a73 10900 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
10901 attr = dwarf2_attr (die,
10902 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10903 cu);
c906108c
SS
10904 if (attr)
10905 {
10906 SYMBOL_LINE (sym) = DW_UNSND (attr);
10907 }
cb1df416 10908
edb3359d
DJ
10909 attr = dwarf2_attr (die,
10910 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10911 cu);
cb1df416
DJ
10912 if (attr)
10913 {
10914 int file_index = DW_UNSND (attr);
9a619af0 10915
cb1df416
DJ
10916 if (cu->line_header == NULL
10917 || file_index > cu->line_header->num_file_names)
10918 complaint (&symfile_complaints,
10919 _("file index out of range"));
1c3d648d 10920 else if (file_index > 0)
cb1df416
DJ
10921 {
10922 struct file_entry *fe;
9a619af0 10923
cb1df416
DJ
10924 fe = &cu->line_header->file_names[file_index - 1];
10925 SYMBOL_SYMTAB (sym) = fe->symtab;
10926 }
10927 }
10928
c906108c
SS
10929 switch (die->tag)
10930 {
10931 case DW_TAG_label:
e142c38c 10932 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
10933 if (attr)
10934 {
10935 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10936 }
0f5238ed
TT
10937 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10938 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 10939 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 10940 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10941 break;
10942 case DW_TAG_subprogram:
10943 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10944 finish_block. */
10945 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 10946 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
10947 if ((attr2 && (DW_UNSND (attr2) != 0))
10948 || cu->language == language_ada)
c906108c 10949 {
2cfa0c8d
JB
10950 /* Subprograms marked external are stored as a global symbol.
10951 Ada subprograms, whether marked external or not, are always
10952 stored as a global symbol, because we want to be able to
10953 access them globally. For instance, we want to be able
10954 to break on a nested subprogram without having to
10955 specify the context. */
e37fd15a 10956 list_to_add = &global_symbols;
c906108c
SS
10957 }
10958 else
10959 {
e37fd15a 10960 list_to_add = cu->list_in_scope;
c906108c
SS
10961 }
10962 break;
edb3359d
DJ
10963 case DW_TAG_inlined_subroutine:
10964 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10965 finish_block. */
10966 SYMBOL_CLASS (sym) = LOC_BLOCK;
10967 SYMBOL_INLINED (sym) = 1;
10968 /* Do not add the symbol to any lists. It will be found via
10969 BLOCK_FUNCTION from the blockvector. */
10970 break;
34eaf542
TT
10971 case DW_TAG_template_value_param:
10972 suppress_add = 1;
10973 /* Fall through. */
72929c62 10974 case DW_TAG_constant:
c906108c 10975 case DW_TAG_variable:
254e6b9e 10976 case DW_TAG_member:
0963b4bd
MS
10977 /* Compilation with minimal debug info may result in
10978 variables with missing type entries. Change the
10979 misleading `void' type to something sensible. */
c906108c 10980 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 10981 SYMBOL_TYPE (sym)
46bf5051 10982 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 10983
e142c38c 10984 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
10985 /* In the case of DW_TAG_member, we should only be called for
10986 static const members. */
10987 if (die->tag == DW_TAG_member)
10988 {
3863f96c
DE
10989 /* dwarf2_add_field uses die_is_declaration,
10990 so we do the same. */
254e6b9e
DE
10991 gdb_assert (die_is_declaration (die, cu));
10992 gdb_assert (attr);
10993 }
c906108c
SS
10994 if (attr)
10995 {
e7c27a73 10996 dwarf2_const_value (attr, sym, cu);
e142c38c 10997 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 10998 if (!suppress_add)
34eaf542
TT
10999 {
11000 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11001 list_to_add = &global_symbols;
34eaf542 11002 else
e37fd15a 11003 list_to_add = cu->list_in_scope;
34eaf542 11004 }
c906108c
SS
11005 break;
11006 }
e142c38c 11007 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11008 if (attr)
11009 {
e7c27a73 11010 var_decode_location (attr, sym, cu);
e142c38c 11011 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11012 if (SYMBOL_CLASS (sym) == LOC_STATIC
11013 && SYMBOL_VALUE_ADDRESS (sym) == 0
11014 && !dwarf2_per_objfile->has_section_at_zero)
11015 {
11016 /* When a static variable is eliminated by the linker,
11017 the corresponding debug information is not stripped
11018 out, but the variable address is set to null;
11019 do not add such variables into symbol table. */
11020 }
11021 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11022 {
f55ee35c
JK
11023 /* Workaround gfortran PR debug/40040 - it uses
11024 DW_AT_location for variables in -fPIC libraries which may
11025 get overriden by other libraries/executable and get
11026 a different address. Resolve it by the minimal symbol
11027 which may come from inferior's executable using copy
11028 relocation. Make this workaround only for gfortran as for
11029 other compilers GDB cannot guess the minimal symbol
11030 Fortran mangling kind. */
11031 if (cu->language == language_fortran && die->parent
11032 && die->parent->tag == DW_TAG_module
11033 && cu->producer
11034 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11035 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11036
1c809c68
TT
11037 /* A variable with DW_AT_external is never static,
11038 but it may be block-scoped. */
11039 list_to_add = (cu->list_in_scope == &file_symbols
11040 ? &global_symbols : cu->list_in_scope);
1c809c68 11041 }
c906108c 11042 else
e37fd15a 11043 list_to_add = cu->list_in_scope;
c906108c
SS
11044 }
11045 else
11046 {
11047 /* We do not know the address of this symbol.
c5aa993b
JM
11048 If it is an external symbol and we have type information
11049 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11050 The address of the variable will then be determined from
11051 the minimal symbol table whenever the variable is
11052 referenced. */
e142c38c 11053 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11054 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11055 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11056 {
0fe7935b
DJ
11057 /* A variable with DW_AT_external is never static, but it
11058 may be block-scoped. */
11059 list_to_add = (cu->list_in_scope == &file_symbols
11060 ? &global_symbols : cu->list_in_scope);
11061
c906108c 11062 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11063 }
442ddf59
JK
11064 else if (!die_is_declaration (die, cu))
11065 {
11066 /* Use the default LOC_OPTIMIZED_OUT class. */
11067 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11068 if (!suppress_add)
11069 list_to_add = cu->list_in_scope;
442ddf59 11070 }
c906108c
SS
11071 }
11072 break;
11073 case DW_TAG_formal_parameter:
edb3359d
DJ
11074 /* If we are inside a function, mark this as an argument. If
11075 not, we might be looking at an argument to an inlined function
11076 when we do not have enough information to show inlined frames;
11077 pretend it's a local variable in that case so that the user can
11078 still see it. */
11079 if (context_stack_depth > 0
11080 && context_stack[context_stack_depth - 1].name != NULL)
11081 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11082 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11083 if (attr)
11084 {
e7c27a73 11085 var_decode_location (attr, sym, cu);
c906108c 11086 }
e142c38c 11087 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11088 if (attr)
11089 {
e7c27a73 11090 dwarf2_const_value (attr, sym, cu);
c906108c 11091 }
f346a30d
PM
11092 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11093 if (attr && DW_UNSND (attr))
11094 {
11095 struct type *ref_type;
11096
11097 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11098 SYMBOL_TYPE (sym) = ref_type;
11099 }
11100
e37fd15a 11101 list_to_add = cu->list_in_scope;
c906108c
SS
11102 break;
11103 case DW_TAG_unspecified_parameters:
11104 /* From varargs functions; gdb doesn't seem to have any
11105 interest in this information, so just ignore it for now.
11106 (FIXME?) */
11107 break;
34eaf542
TT
11108 case DW_TAG_template_type_param:
11109 suppress_add = 1;
11110 /* Fall through. */
c906108c 11111 case DW_TAG_class_type:
680b30c7 11112 case DW_TAG_interface_type:
c906108c
SS
11113 case DW_TAG_structure_type:
11114 case DW_TAG_union_type:
72019c9c 11115 case DW_TAG_set_type:
c906108c
SS
11116 case DW_TAG_enumeration_type:
11117 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11118 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11119
63d06c5c 11120 {
987504bb 11121 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11122 really ever be static objects: otherwise, if you try
11123 to, say, break of a class's method and you're in a file
11124 which doesn't mention that class, it won't work unless
11125 the check for all static symbols in lookup_symbol_aux
11126 saves you. See the OtherFileClass tests in
11127 gdb.c++/namespace.exp. */
11128
e37fd15a 11129 if (!suppress_add)
34eaf542 11130 {
34eaf542
TT
11131 list_to_add = (cu->list_in_scope == &file_symbols
11132 && (cu->language == language_cplus
11133 || cu->language == language_java)
11134 ? &global_symbols : cu->list_in_scope);
63d06c5c 11135
64382290
TT
11136 /* The semantics of C++ state that "struct foo {
11137 ... }" also defines a typedef for "foo". A Java
11138 class declaration also defines a typedef for the
11139 class. */
11140 if (cu->language == language_cplus
11141 || cu->language == language_java
11142 || cu->language == language_ada)
11143 {
11144 /* The symbol's name is already allocated along
11145 with this objfile, so we don't need to
11146 duplicate it for the type. */
11147 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11148 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11149 }
63d06c5c
DC
11150 }
11151 }
c906108c
SS
11152 break;
11153 case DW_TAG_typedef:
63d06c5c
DC
11154 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11155 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11156 list_to_add = cu->list_in_scope;
63d06c5c 11157 break;
c906108c 11158 case DW_TAG_base_type:
a02abb62 11159 case DW_TAG_subrange_type:
c906108c 11160 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11161 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11162 list_to_add = cu->list_in_scope;
c906108c
SS
11163 break;
11164 case DW_TAG_enumerator:
e142c38c 11165 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11166 if (attr)
11167 {
e7c27a73 11168 dwarf2_const_value (attr, sym, cu);
c906108c 11169 }
63d06c5c
DC
11170 {
11171 /* NOTE: carlton/2003-11-10: See comment above in the
11172 DW_TAG_class_type, etc. block. */
11173
e142c38c 11174 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11175 && (cu->language == language_cplus
11176 || cu->language == language_java)
e142c38c 11177 ? &global_symbols : cu->list_in_scope);
63d06c5c 11178 }
c906108c 11179 break;
5c4e30ca
DC
11180 case DW_TAG_namespace:
11181 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11182 list_to_add = &global_symbols;
5c4e30ca 11183 break;
c906108c
SS
11184 default:
11185 /* Not a tag we recognize. Hopefully we aren't processing
11186 trash data, but since we must specifically ignore things
11187 we don't recognize, there is nothing else we should do at
0963b4bd 11188 this point. */
e2e0b3e5 11189 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11190 dwarf_tag_name (die->tag));
c906108c
SS
11191 break;
11192 }
df8a16a1 11193
e37fd15a
SW
11194 if (suppress_add)
11195 {
11196 sym->hash_next = objfile->template_symbols;
11197 objfile->template_symbols = sym;
11198 list_to_add = NULL;
11199 }
11200
11201 if (list_to_add != NULL)
11202 add_symbol_to_list (sym, list_to_add);
11203
df8a16a1
DJ
11204 /* For the benefit of old versions of GCC, check for anonymous
11205 namespaces based on the demangled name. */
11206 if (!processing_has_namespace_info
94af9270 11207 && cu->language == language_cplus)
df8a16a1 11208 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11209 }
11210 return (sym);
11211}
11212
34eaf542
TT
11213/* A wrapper for new_symbol_full that always allocates a new symbol. */
11214
11215static struct symbol *
11216new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11217{
11218 return new_symbol_full (die, type, cu, NULL);
11219}
11220
98bfdba5
PA
11221/* Given an attr with a DW_FORM_dataN value in host byte order,
11222 zero-extend it as appropriate for the symbol's type. The DWARF
11223 standard (v4) is not entirely clear about the meaning of using
11224 DW_FORM_dataN for a constant with a signed type, where the type is
11225 wider than the data. The conclusion of a discussion on the DWARF
11226 list was that this is unspecified. We choose to always zero-extend
11227 because that is the interpretation long in use by GCC. */
c906108c 11228
98bfdba5
PA
11229static gdb_byte *
11230dwarf2_const_value_data (struct attribute *attr, struct type *type,
11231 const char *name, struct obstack *obstack,
11232 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11233{
e7c27a73 11234 struct objfile *objfile = cu->objfile;
e17a4113
UW
11235 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11236 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11237 LONGEST l = DW_UNSND (attr);
11238
11239 if (bits < sizeof (*value) * 8)
11240 {
11241 l &= ((LONGEST) 1 << bits) - 1;
11242 *value = l;
11243 }
11244 else if (bits == sizeof (*value) * 8)
11245 *value = l;
11246 else
11247 {
11248 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11249 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11250 return bytes;
11251 }
11252
11253 return NULL;
11254}
11255
11256/* Read a constant value from an attribute. Either set *VALUE, or if
11257 the value does not fit in *VALUE, set *BYTES - either already
11258 allocated on the objfile obstack, or newly allocated on OBSTACK,
11259 or, set *BATON, if we translated the constant to a location
11260 expression. */
11261
11262static void
11263dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11264 const char *name, struct obstack *obstack,
11265 struct dwarf2_cu *cu,
11266 long *value, gdb_byte **bytes,
11267 struct dwarf2_locexpr_baton **baton)
11268{
11269 struct objfile *objfile = cu->objfile;
11270 struct comp_unit_head *cu_header = &cu->header;
c906108c 11271 struct dwarf_block *blk;
98bfdba5
PA
11272 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11273 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11274
11275 *value = 0;
11276 *bytes = NULL;
11277 *baton = NULL;
c906108c
SS
11278
11279 switch (attr->form)
11280 {
11281 case DW_FORM_addr:
ac56253d 11282 {
ac56253d
TT
11283 gdb_byte *data;
11284
98bfdba5
PA
11285 if (TYPE_LENGTH (type) != cu_header->addr_size)
11286 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11287 cu_header->addr_size,
98bfdba5 11288 TYPE_LENGTH (type));
ac56253d
TT
11289 /* Symbols of this form are reasonably rare, so we just
11290 piggyback on the existing location code rather than writing
11291 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11292 *baton = obstack_alloc (&objfile->objfile_obstack,
11293 sizeof (struct dwarf2_locexpr_baton));
11294 (*baton)->per_cu = cu->per_cu;
11295 gdb_assert ((*baton)->per_cu);
ac56253d 11296
98bfdba5
PA
11297 (*baton)->size = 2 + cu_header->addr_size;
11298 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11299 (*baton)->data = data;
ac56253d
TT
11300
11301 data[0] = DW_OP_addr;
11302 store_unsigned_integer (&data[1], cu_header->addr_size,
11303 byte_order, DW_ADDR (attr));
11304 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11305 }
c906108c 11306 break;
4ac36638 11307 case DW_FORM_string:
93b5768b 11308 case DW_FORM_strp:
98bfdba5
PA
11309 /* DW_STRING is already allocated on the objfile obstack, point
11310 directly to it. */
11311 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11312 break;
c906108c
SS
11313 case DW_FORM_block1:
11314 case DW_FORM_block2:
11315 case DW_FORM_block4:
11316 case DW_FORM_block:
2dc7f7b3 11317 case DW_FORM_exprloc:
c906108c 11318 blk = DW_BLOCK (attr);
98bfdba5
PA
11319 if (TYPE_LENGTH (type) != blk->size)
11320 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11321 TYPE_LENGTH (type));
11322 *bytes = blk->data;
c906108c 11323 break;
2df3850c
JM
11324
11325 /* The DW_AT_const_value attributes are supposed to carry the
11326 symbol's value "represented as it would be on the target
11327 architecture." By the time we get here, it's already been
11328 converted to host endianness, so we just need to sign- or
11329 zero-extend it as appropriate. */
11330 case DW_FORM_data1:
3e43a32a
MS
11331 *bytes = dwarf2_const_value_data (attr, type, name,
11332 obstack, cu, value, 8);
2df3850c 11333 break;
c906108c 11334 case DW_FORM_data2:
3e43a32a
MS
11335 *bytes = dwarf2_const_value_data (attr, type, name,
11336 obstack, cu, value, 16);
2df3850c 11337 break;
c906108c 11338 case DW_FORM_data4:
3e43a32a
MS
11339 *bytes = dwarf2_const_value_data (attr, type, name,
11340 obstack, cu, value, 32);
2df3850c 11341 break;
c906108c 11342 case DW_FORM_data8:
3e43a32a
MS
11343 *bytes = dwarf2_const_value_data (attr, type, name,
11344 obstack, cu, value, 64);
2df3850c
JM
11345 break;
11346
c906108c 11347 case DW_FORM_sdata:
98bfdba5 11348 *value = DW_SND (attr);
2df3850c
JM
11349 break;
11350
c906108c 11351 case DW_FORM_udata:
98bfdba5 11352 *value = DW_UNSND (attr);
c906108c 11353 break;
2df3850c 11354
c906108c 11355 default:
4d3c2250 11356 complaint (&symfile_complaints,
e2e0b3e5 11357 _("unsupported const value attribute form: '%s'"),
4d3c2250 11358 dwarf_form_name (attr->form));
98bfdba5 11359 *value = 0;
c906108c
SS
11360 break;
11361 }
11362}
11363
2df3850c 11364
98bfdba5
PA
11365/* Copy constant value from an attribute to a symbol. */
11366
2df3850c 11367static void
98bfdba5
PA
11368dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11369 struct dwarf2_cu *cu)
2df3850c 11370{
98bfdba5
PA
11371 struct objfile *objfile = cu->objfile;
11372 struct comp_unit_head *cu_header = &cu->header;
11373 long value;
11374 gdb_byte *bytes;
11375 struct dwarf2_locexpr_baton *baton;
2df3850c 11376
98bfdba5
PA
11377 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11378 SYMBOL_PRINT_NAME (sym),
11379 &objfile->objfile_obstack, cu,
11380 &value, &bytes, &baton);
2df3850c 11381
98bfdba5
PA
11382 if (baton != NULL)
11383 {
11384 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11385 SYMBOL_LOCATION_BATON (sym) = baton;
11386 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11387 }
11388 else if (bytes != NULL)
11389 {
11390 SYMBOL_VALUE_BYTES (sym) = bytes;
11391 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11392 }
11393 else
11394 {
11395 SYMBOL_VALUE (sym) = value;
11396 SYMBOL_CLASS (sym) = LOC_CONST;
11397 }
2df3850c
JM
11398}
11399
c906108c
SS
11400/* Return the type of the die in question using its DW_AT_type attribute. */
11401
11402static struct type *
e7c27a73 11403die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11404{
c906108c 11405 struct attribute *type_attr;
c906108c 11406
e142c38c 11407 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11408 if (!type_attr)
11409 {
11410 /* A missing DW_AT_type represents a void type. */
46bf5051 11411 return objfile_type (cu->objfile)->builtin_void;
c906108c 11412 }
348e048f 11413
673bfd45 11414 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11415}
11416
b4ba55a1
JB
11417/* True iff CU's producer generates GNAT Ada auxiliary information
11418 that allows to find parallel types through that information instead
11419 of having to do expensive parallel lookups by type name. */
11420
11421static int
11422need_gnat_info (struct dwarf2_cu *cu)
11423{
11424 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11425 of GNAT produces this auxiliary information, without any indication
11426 that it is produced. Part of enhancing the FSF version of GNAT
11427 to produce that information will be to put in place an indicator
11428 that we can use in order to determine whether the descriptive type
11429 info is available or not. One suggestion that has been made is
11430 to use a new attribute, attached to the CU die. For now, assume
11431 that the descriptive type info is not available. */
11432 return 0;
11433}
11434
b4ba55a1
JB
11435/* Return the auxiliary type of the die in question using its
11436 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11437 attribute is not present. */
11438
11439static struct type *
11440die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11441{
b4ba55a1 11442 struct attribute *type_attr;
b4ba55a1
JB
11443
11444 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11445 if (!type_attr)
11446 return NULL;
11447
673bfd45 11448 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11449}
11450
11451/* If DIE has a descriptive_type attribute, then set the TYPE's
11452 descriptive type accordingly. */
11453
11454static void
11455set_descriptive_type (struct type *type, struct die_info *die,
11456 struct dwarf2_cu *cu)
11457{
11458 struct type *descriptive_type = die_descriptive_type (die, cu);
11459
11460 if (descriptive_type)
11461 {
11462 ALLOCATE_GNAT_AUX_TYPE (type);
11463 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11464 }
11465}
11466
c906108c
SS
11467/* Return the containing type of the die in question using its
11468 DW_AT_containing_type attribute. */
11469
11470static struct type *
e7c27a73 11471die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11472{
c906108c 11473 struct attribute *type_attr;
c906108c 11474
e142c38c 11475 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11476 if (!type_attr)
11477 error (_("Dwarf Error: Problem turning containing type into gdb type "
11478 "[in module %s]"), cu->objfile->name);
11479
673bfd45 11480 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11481}
11482
673bfd45
DE
11483/* Look up the type of DIE in CU using its type attribute ATTR.
11484 If there is no type substitute an error marker. */
11485
c906108c 11486static struct type *
673bfd45
DE
11487lookup_die_type (struct die_info *die, struct attribute *attr,
11488 struct dwarf2_cu *cu)
c906108c 11489{
f792889a
DJ
11490 struct type *this_type;
11491
673bfd45
DE
11492 /* First see if we have it cached. */
11493
11494 if (is_ref_attr (attr))
11495 {
11496 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11497
11498 this_type = get_die_type_at_offset (offset, cu->per_cu);
11499 }
55f1336d 11500 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
11501 {
11502 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11503 struct dwarf2_cu *sig_cu;
11504 unsigned int offset;
11505
11506 /* sig_type will be NULL if the signatured type is missing from
11507 the debug info. */
11508 if (sig_type == NULL)
11509 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11510 "at 0x%x [in module %s]"),
11511 die->offset, cu->objfile->name);
11512
11513 gdb_assert (sig_type->per_cu.from_debug_types);
11514 offset = sig_type->offset + sig_type->type_offset;
11515 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11516 }
11517 else
11518 {
11519 dump_die_for_error (die);
11520 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11521 dwarf_attr_name (attr->name), cu->objfile->name);
11522 }
11523
11524 /* If not cached we need to read it in. */
11525
11526 if (this_type == NULL)
11527 {
11528 struct die_info *type_die;
11529 struct dwarf2_cu *type_cu = cu;
11530
11531 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11532 /* If the type is cached, we should have found it above. */
11533 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11534 this_type = read_type_die_1 (type_die, type_cu);
11535 }
11536
11537 /* If we still don't have a type use an error marker. */
11538
11539 if (this_type == NULL)
c906108c 11540 {
b00fdb78
TT
11541 char *message, *saved;
11542
11543 /* read_type_die already issued a complaint. */
11544 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11545 cu->objfile->name,
11546 cu->header.offset,
11547 die->offset);
11548 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11549 message, strlen (message));
11550 xfree (message);
11551
11552 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11553 }
673bfd45 11554
f792889a 11555 return this_type;
c906108c
SS
11556}
11557
673bfd45
DE
11558/* Return the type in DIE, CU.
11559 Returns NULL for invalid types.
11560
11561 This first does a lookup in the appropriate type_hash table,
11562 and only reads the die in if necessary.
11563
11564 NOTE: This can be called when reading in partial or full symbols. */
11565
f792889a 11566static struct type *
e7c27a73 11567read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11568{
f792889a
DJ
11569 struct type *this_type;
11570
11571 this_type = get_die_type (die, cu);
11572 if (this_type)
11573 return this_type;
11574
673bfd45
DE
11575 return read_type_die_1 (die, cu);
11576}
11577
11578/* Read the type in DIE, CU.
11579 Returns NULL for invalid types. */
11580
11581static struct type *
11582read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11583{
11584 struct type *this_type = NULL;
11585
c906108c
SS
11586 switch (die->tag)
11587 {
11588 case DW_TAG_class_type:
680b30c7 11589 case DW_TAG_interface_type:
c906108c
SS
11590 case DW_TAG_structure_type:
11591 case DW_TAG_union_type:
f792889a 11592 this_type = read_structure_type (die, cu);
c906108c
SS
11593 break;
11594 case DW_TAG_enumeration_type:
f792889a 11595 this_type = read_enumeration_type (die, cu);
c906108c
SS
11596 break;
11597 case DW_TAG_subprogram:
11598 case DW_TAG_subroutine_type:
edb3359d 11599 case DW_TAG_inlined_subroutine:
f792889a 11600 this_type = read_subroutine_type (die, cu);
c906108c
SS
11601 break;
11602 case DW_TAG_array_type:
f792889a 11603 this_type = read_array_type (die, cu);
c906108c 11604 break;
72019c9c 11605 case DW_TAG_set_type:
f792889a 11606 this_type = read_set_type (die, cu);
72019c9c 11607 break;
c906108c 11608 case DW_TAG_pointer_type:
f792889a 11609 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
11610 break;
11611 case DW_TAG_ptr_to_member_type:
f792889a 11612 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
11613 break;
11614 case DW_TAG_reference_type:
f792889a 11615 this_type = read_tag_reference_type (die, cu);
c906108c
SS
11616 break;
11617 case DW_TAG_const_type:
f792889a 11618 this_type = read_tag_const_type (die, cu);
c906108c
SS
11619 break;
11620 case DW_TAG_volatile_type:
f792889a 11621 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
11622 break;
11623 case DW_TAG_string_type:
f792889a 11624 this_type = read_tag_string_type (die, cu);
c906108c
SS
11625 break;
11626 case DW_TAG_typedef:
f792889a 11627 this_type = read_typedef (die, cu);
c906108c 11628 break;
a02abb62 11629 case DW_TAG_subrange_type:
f792889a 11630 this_type = read_subrange_type (die, cu);
a02abb62 11631 break;
c906108c 11632 case DW_TAG_base_type:
f792889a 11633 this_type = read_base_type (die, cu);
c906108c 11634 break;
81a17f79 11635 case DW_TAG_unspecified_type:
f792889a 11636 this_type = read_unspecified_type (die, cu);
81a17f79 11637 break;
0114d602
DJ
11638 case DW_TAG_namespace:
11639 this_type = read_namespace_type (die, cu);
11640 break;
f55ee35c
JK
11641 case DW_TAG_module:
11642 this_type = read_module_type (die, cu);
11643 break;
c906108c 11644 default:
3e43a32a
MS
11645 complaint (&symfile_complaints,
11646 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 11647 dwarf_tag_name (die->tag));
c906108c
SS
11648 break;
11649 }
63d06c5c 11650
f792889a 11651 return this_type;
63d06c5c
DC
11652}
11653
abc72ce4
DE
11654/* See if we can figure out if the class lives in a namespace. We do
11655 this by looking for a member function; its demangled name will
11656 contain namespace info, if there is any.
11657 Return the computed name or NULL.
11658 Space for the result is allocated on the objfile's obstack.
11659 This is the full-die version of guess_partial_die_structure_name.
11660 In this case we know DIE has no useful parent. */
11661
11662static char *
11663guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11664{
11665 struct die_info *spec_die;
11666 struct dwarf2_cu *spec_cu;
11667 struct die_info *child;
11668
11669 spec_cu = cu;
11670 spec_die = die_specification (die, &spec_cu);
11671 if (spec_die != NULL)
11672 {
11673 die = spec_die;
11674 cu = spec_cu;
11675 }
11676
11677 for (child = die->child;
11678 child != NULL;
11679 child = child->sibling)
11680 {
11681 if (child->tag == DW_TAG_subprogram)
11682 {
11683 struct attribute *attr;
11684
11685 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11686 if (attr == NULL)
11687 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11688 if (attr != NULL)
11689 {
11690 char *actual_name
11691 = language_class_name_from_physname (cu->language_defn,
11692 DW_STRING (attr));
11693 char *name = NULL;
11694
11695 if (actual_name != NULL)
11696 {
11697 char *die_name = dwarf2_name (die, cu);
11698
11699 if (die_name != NULL
11700 && strcmp (die_name, actual_name) != 0)
11701 {
11702 /* Strip off the class name from the full name.
11703 We want the prefix. */
11704 int die_name_len = strlen (die_name);
11705 int actual_name_len = strlen (actual_name);
11706
11707 /* Test for '::' as a sanity check. */
11708 if (actual_name_len > die_name_len + 2
3e43a32a
MS
11709 && actual_name[actual_name_len
11710 - die_name_len - 1] == ':')
abc72ce4
DE
11711 name =
11712 obsavestring (actual_name,
11713 actual_name_len - die_name_len - 2,
11714 &cu->objfile->objfile_obstack);
11715 }
11716 }
11717 xfree (actual_name);
11718 return name;
11719 }
11720 }
11721 }
11722
11723 return NULL;
11724}
11725
fdde2d81 11726/* Return the name of the namespace/class that DIE is defined within,
0114d602 11727 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 11728
0114d602
DJ
11729 For example, if we're within the method foo() in the following
11730 code:
11731
11732 namespace N {
11733 class C {
11734 void foo () {
11735 }
11736 };
11737 }
11738
11739 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
11740
11741static char *
e142c38c 11742determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 11743{
0114d602
DJ
11744 struct die_info *parent, *spec_die;
11745 struct dwarf2_cu *spec_cu;
11746 struct type *parent_type;
63d06c5c 11747
f55ee35c
JK
11748 if (cu->language != language_cplus && cu->language != language_java
11749 && cu->language != language_fortran)
0114d602
DJ
11750 return "";
11751
11752 /* We have to be careful in the presence of DW_AT_specification.
11753 For example, with GCC 3.4, given the code
11754
11755 namespace N {
11756 void foo() {
11757 // Definition of N::foo.
11758 }
11759 }
11760
11761 then we'll have a tree of DIEs like this:
11762
11763 1: DW_TAG_compile_unit
11764 2: DW_TAG_namespace // N
11765 3: DW_TAG_subprogram // declaration of N::foo
11766 4: DW_TAG_subprogram // definition of N::foo
11767 DW_AT_specification // refers to die #3
11768
11769 Thus, when processing die #4, we have to pretend that we're in
11770 the context of its DW_AT_specification, namely the contex of die
11771 #3. */
11772 spec_cu = cu;
11773 spec_die = die_specification (die, &spec_cu);
11774 if (spec_die == NULL)
11775 parent = die->parent;
11776 else
63d06c5c 11777 {
0114d602
DJ
11778 parent = spec_die->parent;
11779 cu = spec_cu;
63d06c5c 11780 }
0114d602
DJ
11781
11782 if (parent == NULL)
11783 return "";
98bfdba5
PA
11784 else if (parent->building_fullname)
11785 {
11786 const char *name;
11787 const char *parent_name;
11788
11789 /* It has been seen on RealView 2.2 built binaries,
11790 DW_TAG_template_type_param types actually _defined_ as
11791 children of the parent class:
11792
11793 enum E {};
11794 template class <class Enum> Class{};
11795 Class<enum E> class_e;
11796
11797 1: DW_TAG_class_type (Class)
11798 2: DW_TAG_enumeration_type (E)
11799 3: DW_TAG_enumerator (enum1:0)
11800 3: DW_TAG_enumerator (enum2:1)
11801 ...
11802 2: DW_TAG_template_type_param
11803 DW_AT_type DW_FORM_ref_udata (E)
11804
11805 Besides being broken debug info, it can put GDB into an
11806 infinite loop. Consider:
11807
11808 When we're building the full name for Class<E>, we'll start
11809 at Class, and go look over its template type parameters,
11810 finding E. We'll then try to build the full name of E, and
11811 reach here. We're now trying to build the full name of E,
11812 and look over the parent DIE for containing scope. In the
11813 broken case, if we followed the parent DIE of E, we'd again
11814 find Class, and once again go look at its template type
11815 arguments, etc., etc. Simply don't consider such parent die
11816 as source-level parent of this die (it can't be, the language
11817 doesn't allow it), and break the loop here. */
11818 name = dwarf2_name (die, cu);
11819 parent_name = dwarf2_name (parent, cu);
11820 complaint (&symfile_complaints,
11821 _("template param type '%s' defined within parent '%s'"),
11822 name ? name : "<unknown>",
11823 parent_name ? parent_name : "<unknown>");
11824 return "";
11825 }
63d06c5c 11826 else
0114d602
DJ
11827 switch (parent->tag)
11828 {
63d06c5c 11829 case DW_TAG_namespace:
0114d602 11830 parent_type = read_type_die (parent, cu);
acebe513
UW
11831 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11832 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11833 Work around this problem here. */
11834 if (cu->language == language_cplus
11835 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11836 return "";
0114d602
DJ
11837 /* We give a name to even anonymous namespaces. */
11838 return TYPE_TAG_NAME (parent_type);
63d06c5c 11839 case DW_TAG_class_type:
680b30c7 11840 case DW_TAG_interface_type:
63d06c5c 11841 case DW_TAG_structure_type:
0114d602 11842 case DW_TAG_union_type:
f55ee35c 11843 case DW_TAG_module:
0114d602
DJ
11844 parent_type = read_type_die (parent, cu);
11845 if (TYPE_TAG_NAME (parent_type) != NULL)
11846 return TYPE_TAG_NAME (parent_type);
11847 else
11848 /* An anonymous structure is only allowed non-static data
11849 members; no typedefs, no member functions, et cetera.
11850 So it does not need a prefix. */
11851 return "";
abc72ce4
DE
11852 case DW_TAG_compile_unit:
11853 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11854 if (cu->language == language_cplus
11855 && dwarf2_per_objfile->types.asection != NULL
11856 && die->child != NULL
11857 && (die->tag == DW_TAG_class_type
11858 || die->tag == DW_TAG_structure_type
11859 || die->tag == DW_TAG_union_type))
11860 {
11861 char *name = guess_full_die_structure_name (die, cu);
11862 if (name != NULL)
11863 return name;
11864 }
11865 return "";
63d06c5c 11866 default:
8176b9b8 11867 return determine_prefix (parent, cu);
63d06c5c 11868 }
63d06c5c
DC
11869}
11870
3e43a32a
MS
11871/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
11872 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11873 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
11874 an obconcat, otherwise allocate storage for the result. The CU argument is
11875 used to determine the language and hence, the appropriate separator. */
987504bb 11876
f55ee35c 11877#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
11878
11879static char *
f55ee35c
JK
11880typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11881 int physname, struct dwarf2_cu *cu)
63d06c5c 11882{
f55ee35c 11883 const char *lead = "";
5c315b68 11884 const char *sep;
63d06c5c 11885
3e43a32a
MS
11886 if (suffix == NULL || suffix[0] == '\0'
11887 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
11888 sep = "";
11889 else if (cu->language == language_java)
11890 sep = ".";
f55ee35c
JK
11891 else if (cu->language == language_fortran && physname)
11892 {
11893 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11894 DW_AT_MIPS_linkage_name is preferred and used instead. */
11895
11896 lead = "__";
11897 sep = "_MOD_";
11898 }
987504bb
JJ
11899 else
11900 sep = "::";
63d06c5c 11901
6dd47d34
DE
11902 if (prefix == NULL)
11903 prefix = "";
11904 if (suffix == NULL)
11905 suffix = "";
11906
987504bb
JJ
11907 if (obs == NULL)
11908 {
3e43a32a
MS
11909 char *retval
11910 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 11911
f55ee35c
JK
11912 strcpy (retval, lead);
11913 strcat (retval, prefix);
6dd47d34
DE
11914 strcat (retval, sep);
11915 strcat (retval, suffix);
63d06c5c
DC
11916 return retval;
11917 }
987504bb
JJ
11918 else
11919 {
11920 /* We have an obstack. */
f55ee35c 11921 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 11922 }
63d06c5c
DC
11923}
11924
c906108c
SS
11925/* Return sibling of die, NULL if no sibling. */
11926
f9aca02d 11927static struct die_info *
fba45db2 11928sibling_die (struct die_info *die)
c906108c 11929{
639d11d3 11930 return die->sibling;
c906108c
SS
11931}
11932
71c25dea
TT
11933/* Get name of a die, return NULL if not found. */
11934
11935static char *
11936dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11937 struct obstack *obstack)
11938{
11939 if (name && cu->language == language_cplus)
11940 {
11941 char *canon_name = cp_canonicalize_string (name);
11942
11943 if (canon_name != NULL)
11944 {
11945 if (strcmp (canon_name, name) != 0)
11946 name = obsavestring (canon_name, strlen (canon_name),
11947 obstack);
11948 xfree (canon_name);
11949 }
11950 }
11951
11952 return name;
c906108c
SS
11953}
11954
9219021c
DC
11955/* Get name of a die, return NULL if not found. */
11956
11957static char *
e142c38c 11958dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
11959{
11960 struct attribute *attr;
11961
e142c38c 11962 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
11963 if (!attr || !DW_STRING (attr))
11964 return NULL;
11965
11966 switch (die->tag)
11967 {
11968 case DW_TAG_compile_unit:
11969 /* Compilation units have a DW_AT_name that is a filename, not
11970 a source language identifier. */
11971 case DW_TAG_enumeration_type:
11972 case DW_TAG_enumerator:
11973 /* These tags always have simple identifiers already; no need
11974 to canonicalize them. */
11975 return DW_STRING (attr);
907af001 11976
418835cc
KS
11977 case DW_TAG_subprogram:
11978 /* Java constructors will all be named "<init>", so return
11979 the class name when we see this special case. */
11980 if (cu->language == language_java
11981 && DW_STRING (attr) != NULL
11982 && strcmp (DW_STRING (attr), "<init>") == 0)
11983 {
11984 struct dwarf2_cu *spec_cu = cu;
11985 struct die_info *spec_die;
11986
11987 /* GCJ will output '<init>' for Java constructor names.
11988 For this special case, return the name of the parent class. */
11989
11990 /* GCJ may output suprogram DIEs with AT_specification set.
11991 If so, use the name of the specified DIE. */
11992 spec_die = die_specification (die, &spec_cu);
11993 if (spec_die != NULL)
11994 return dwarf2_name (spec_die, spec_cu);
11995
11996 do
11997 {
11998 die = die->parent;
11999 if (die->tag == DW_TAG_class_type)
12000 return dwarf2_name (die, cu);
12001 }
12002 while (die->tag != DW_TAG_compile_unit);
12003 }
907af001
UW
12004 break;
12005
12006 case DW_TAG_class_type:
12007 case DW_TAG_interface_type:
12008 case DW_TAG_structure_type:
12009 case DW_TAG_union_type:
12010 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12011 structures or unions. These were of the form "._%d" in GCC 4.1,
12012 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12013 and GCC 4.4. We work around this problem by ignoring these. */
12014 if (strncmp (DW_STRING (attr), "._", 2) == 0
12015 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
12016 return NULL;
12017 break;
12018
71c25dea 12019 default:
907af001
UW
12020 break;
12021 }
12022
12023 if (!DW_STRING_IS_CANONICAL (attr))
12024 {
12025 DW_STRING (attr)
12026 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12027 &cu->objfile->objfile_obstack);
12028 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12029 }
907af001 12030 return DW_STRING (attr);
9219021c
DC
12031}
12032
12033/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12034 is none. *EXT_CU is the CU containing DIE on input, and the CU
12035 containing the return value on output. */
9219021c
DC
12036
12037static struct die_info *
f2f0e013 12038dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12039{
12040 struct attribute *attr;
9219021c 12041
f2f0e013 12042 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12043 if (attr == NULL)
12044 return NULL;
12045
f2f0e013 12046 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12047}
12048
c906108c
SS
12049/* Convert a DIE tag into its string name. */
12050
12051static char *
aa1ee363 12052dwarf_tag_name (unsigned tag)
c906108c
SS
12053{
12054 switch (tag)
12055 {
12056 case DW_TAG_padding:
12057 return "DW_TAG_padding";
12058 case DW_TAG_array_type:
12059 return "DW_TAG_array_type";
12060 case DW_TAG_class_type:
12061 return "DW_TAG_class_type";
12062 case DW_TAG_entry_point:
12063 return "DW_TAG_entry_point";
12064 case DW_TAG_enumeration_type:
12065 return "DW_TAG_enumeration_type";
12066 case DW_TAG_formal_parameter:
12067 return "DW_TAG_formal_parameter";
12068 case DW_TAG_imported_declaration:
12069 return "DW_TAG_imported_declaration";
12070 case DW_TAG_label:
12071 return "DW_TAG_label";
12072 case DW_TAG_lexical_block:
12073 return "DW_TAG_lexical_block";
12074 case DW_TAG_member:
12075 return "DW_TAG_member";
12076 case DW_TAG_pointer_type:
12077 return "DW_TAG_pointer_type";
12078 case DW_TAG_reference_type:
12079 return "DW_TAG_reference_type";
12080 case DW_TAG_compile_unit:
12081 return "DW_TAG_compile_unit";
12082 case DW_TAG_string_type:
12083 return "DW_TAG_string_type";
12084 case DW_TAG_structure_type:
12085 return "DW_TAG_structure_type";
12086 case DW_TAG_subroutine_type:
12087 return "DW_TAG_subroutine_type";
12088 case DW_TAG_typedef:
12089 return "DW_TAG_typedef";
12090 case DW_TAG_union_type:
12091 return "DW_TAG_union_type";
12092 case DW_TAG_unspecified_parameters:
12093 return "DW_TAG_unspecified_parameters";
12094 case DW_TAG_variant:
12095 return "DW_TAG_variant";
12096 case DW_TAG_common_block:
12097 return "DW_TAG_common_block";
12098 case DW_TAG_common_inclusion:
12099 return "DW_TAG_common_inclusion";
12100 case DW_TAG_inheritance:
12101 return "DW_TAG_inheritance";
12102 case DW_TAG_inlined_subroutine:
12103 return "DW_TAG_inlined_subroutine";
12104 case DW_TAG_module:
12105 return "DW_TAG_module";
12106 case DW_TAG_ptr_to_member_type:
12107 return "DW_TAG_ptr_to_member_type";
12108 case DW_TAG_set_type:
12109 return "DW_TAG_set_type";
12110 case DW_TAG_subrange_type:
12111 return "DW_TAG_subrange_type";
12112 case DW_TAG_with_stmt:
12113 return "DW_TAG_with_stmt";
12114 case DW_TAG_access_declaration:
12115 return "DW_TAG_access_declaration";
12116 case DW_TAG_base_type:
12117 return "DW_TAG_base_type";
12118 case DW_TAG_catch_block:
12119 return "DW_TAG_catch_block";
12120 case DW_TAG_const_type:
12121 return "DW_TAG_const_type";
12122 case DW_TAG_constant:
12123 return "DW_TAG_constant";
12124 case DW_TAG_enumerator:
12125 return "DW_TAG_enumerator";
12126 case DW_TAG_file_type:
12127 return "DW_TAG_file_type";
12128 case DW_TAG_friend:
12129 return "DW_TAG_friend";
12130 case DW_TAG_namelist:
12131 return "DW_TAG_namelist";
12132 case DW_TAG_namelist_item:
12133 return "DW_TAG_namelist_item";
12134 case DW_TAG_packed_type:
12135 return "DW_TAG_packed_type";
12136 case DW_TAG_subprogram:
12137 return "DW_TAG_subprogram";
12138 case DW_TAG_template_type_param:
12139 return "DW_TAG_template_type_param";
12140 case DW_TAG_template_value_param:
12141 return "DW_TAG_template_value_param";
12142 case DW_TAG_thrown_type:
12143 return "DW_TAG_thrown_type";
12144 case DW_TAG_try_block:
12145 return "DW_TAG_try_block";
12146 case DW_TAG_variant_part:
12147 return "DW_TAG_variant_part";
12148 case DW_TAG_variable:
12149 return "DW_TAG_variable";
12150 case DW_TAG_volatile_type:
12151 return "DW_TAG_volatile_type";
d9fa45fe
DC
12152 case DW_TAG_dwarf_procedure:
12153 return "DW_TAG_dwarf_procedure";
12154 case DW_TAG_restrict_type:
12155 return "DW_TAG_restrict_type";
12156 case DW_TAG_interface_type:
12157 return "DW_TAG_interface_type";
12158 case DW_TAG_namespace:
12159 return "DW_TAG_namespace";
12160 case DW_TAG_imported_module:
12161 return "DW_TAG_imported_module";
12162 case DW_TAG_unspecified_type:
12163 return "DW_TAG_unspecified_type";
12164 case DW_TAG_partial_unit:
12165 return "DW_TAG_partial_unit";
12166 case DW_TAG_imported_unit:
12167 return "DW_TAG_imported_unit";
b7619582
GF
12168 case DW_TAG_condition:
12169 return "DW_TAG_condition";
12170 case DW_TAG_shared_type:
12171 return "DW_TAG_shared_type";
348e048f
DE
12172 case DW_TAG_type_unit:
12173 return "DW_TAG_type_unit";
c906108c
SS
12174 case DW_TAG_MIPS_loop:
12175 return "DW_TAG_MIPS_loop";
b7619582
GF
12176 case DW_TAG_HP_array_descriptor:
12177 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12178 case DW_TAG_format_label:
12179 return "DW_TAG_format_label";
12180 case DW_TAG_function_template:
12181 return "DW_TAG_function_template";
12182 case DW_TAG_class_template:
12183 return "DW_TAG_class_template";
b7619582
GF
12184 case DW_TAG_GNU_BINCL:
12185 return "DW_TAG_GNU_BINCL";
12186 case DW_TAG_GNU_EINCL:
12187 return "DW_TAG_GNU_EINCL";
12188 case DW_TAG_upc_shared_type:
12189 return "DW_TAG_upc_shared_type";
12190 case DW_TAG_upc_strict_type:
12191 return "DW_TAG_upc_strict_type";
12192 case DW_TAG_upc_relaxed_type:
12193 return "DW_TAG_upc_relaxed_type";
12194 case DW_TAG_PGI_kanji_type:
12195 return "DW_TAG_PGI_kanji_type";
12196 case DW_TAG_PGI_interface_block:
12197 return "DW_TAG_PGI_interface_block";
c906108c
SS
12198 default:
12199 return "DW_TAG_<unknown>";
12200 }
12201}
12202
12203/* Convert a DWARF attribute code into its string name. */
12204
12205static char *
aa1ee363 12206dwarf_attr_name (unsigned attr)
c906108c
SS
12207{
12208 switch (attr)
12209 {
12210 case DW_AT_sibling:
12211 return "DW_AT_sibling";
12212 case DW_AT_location:
12213 return "DW_AT_location";
12214 case DW_AT_name:
12215 return "DW_AT_name";
12216 case DW_AT_ordering:
12217 return "DW_AT_ordering";
12218 case DW_AT_subscr_data:
12219 return "DW_AT_subscr_data";
12220 case DW_AT_byte_size:
12221 return "DW_AT_byte_size";
12222 case DW_AT_bit_offset:
12223 return "DW_AT_bit_offset";
12224 case DW_AT_bit_size:
12225 return "DW_AT_bit_size";
12226 case DW_AT_element_list:
12227 return "DW_AT_element_list";
12228 case DW_AT_stmt_list:
12229 return "DW_AT_stmt_list";
12230 case DW_AT_low_pc:
12231 return "DW_AT_low_pc";
12232 case DW_AT_high_pc:
12233 return "DW_AT_high_pc";
12234 case DW_AT_language:
12235 return "DW_AT_language";
12236 case DW_AT_member:
12237 return "DW_AT_member";
12238 case DW_AT_discr:
12239 return "DW_AT_discr";
12240 case DW_AT_discr_value:
12241 return "DW_AT_discr_value";
12242 case DW_AT_visibility:
12243 return "DW_AT_visibility";
12244 case DW_AT_import:
12245 return "DW_AT_import";
12246 case DW_AT_string_length:
12247 return "DW_AT_string_length";
12248 case DW_AT_common_reference:
12249 return "DW_AT_common_reference";
12250 case DW_AT_comp_dir:
12251 return "DW_AT_comp_dir";
12252 case DW_AT_const_value:
12253 return "DW_AT_const_value";
12254 case DW_AT_containing_type:
12255 return "DW_AT_containing_type";
12256 case DW_AT_default_value:
12257 return "DW_AT_default_value";
12258 case DW_AT_inline:
12259 return "DW_AT_inline";
12260 case DW_AT_is_optional:
12261 return "DW_AT_is_optional";
12262 case DW_AT_lower_bound:
12263 return "DW_AT_lower_bound";
12264 case DW_AT_producer:
12265 return "DW_AT_producer";
12266 case DW_AT_prototyped:
12267 return "DW_AT_prototyped";
12268 case DW_AT_return_addr:
12269 return "DW_AT_return_addr";
12270 case DW_AT_start_scope:
12271 return "DW_AT_start_scope";
09fa0d7c
JK
12272 case DW_AT_bit_stride:
12273 return "DW_AT_bit_stride";
c906108c
SS
12274 case DW_AT_upper_bound:
12275 return "DW_AT_upper_bound";
12276 case DW_AT_abstract_origin:
12277 return "DW_AT_abstract_origin";
12278 case DW_AT_accessibility:
12279 return "DW_AT_accessibility";
12280 case DW_AT_address_class:
12281 return "DW_AT_address_class";
12282 case DW_AT_artificial:
12283 return "DW_AT_artificial";
12284 case DW_AT_base_types:
12285 return "DW_AT_base_types";
12286 case DW_AT_calling_convention:
12287 return "DW_AT_calling_convention";
12288 case DW_AT_count:
12289 return "DW_AT_count";
12290 case DW_AT_data_member_location:
12291 return "DW_AT_data_member_location";
12292 case DW_AT_decl_column:
12293 return "DW_AT_decl_column";
12294 case DW_AT_decl_file:
12295 return "DW_AT_decl_file";
12296 case DW_AT_decl_line:
12297 return "DW_AT_decl_line";
12298 case DW_AT_declaration:
12299 return "DW_AT_declaration";
12300 case DW_AT_discr_list:
12301 return "DW_AT_discr_list";
12302 case DW_AT_encoding:
12303 return "DW_AT_encoding";
12304 case DW_AT_external:
12305 return "DW_AT_external";
12306 case DW_AT_frame_base:
12307 return "DW_AT_frame_base";
12308 case DW_AT_friend:
12309 return "DW_AT_friend";
12310 case DW_AT_identifier_case:
12311 return "DW_AT_identifier_case";
12312 case DW_AT_macro_info:
12313 return "DW_AT_macro_info";
12314 case DW_AT_namelist_items:
12315 return "DW_AT_namelist_items";
12316 case DW_AT_priority:
12317 return "DW_AT_priority";
12318 case DW_AT_segment:
12319 return "DW_AT_segment";
12320 case DW_AT_specification:
12321 return "DW_AT_specification";
12322 case DW_AT_static_link:
12323 return "DW_AT_static_link";
12324 case DW_AT_type:
12325 return "DW_AT_type";
12326 case DW_AT_use_location:
12327 return "DW_AT_use_location";
12328 case DW_AT_variable_parameter:
12329 return "DW_AT_variable_parameter";
12330 case DW_AT_virtuality:
12331 return "DW_AT_virtuality";
12332 case DW_AT_vtable_elem_location:
12333 return "DW_AT_vtable_elem_location";
b7619582 12334 /* DWARF 3 values. */
d9fa45fe
DC
12335 case DW_AT_allocated:
12336 return "DW_AT_allocated";
12337 case DW_AT_associated:
12338 return "DW_AT_associated";
12339 case DW_AT_data_location:
12340 return "DW_AT_data_location";
09fa0d7c
JK
12341 case DW_AT_byte_stride:
12342 return "DW_AT_byte_stride";
d9fa45fe
DC
12343 case DW_AT_entry_pc:
12344 return "DW_AT_entry_pc";
12345 case DW_AT_use_UTF8:
12346 return "DW_AT_use_UTF8";
12347 case DW_AT_extension:
12348 return "DW_AT_extension";
12349 case DW_AT_ranges:
12350 return "DW_AT_ranges";
12351 case DW_AT_trampoline:
12352 return "DW_AT_trampoline";
12353 case DW_AT_call_column:
12354 return "DW_AT_call_column";
12355 case DW_AT_call_file:
12356 return "DW_AT_call_file";
12357 case DW_AT_call_line:
12358 return "DW_AT_call_line";
b7619582
GF
12359 case DW_AT_description:
12360 return "DW_AT_description";
12361 case DW_AT_binary_scale:
12362 return "DW_AT_binary_scale";
12363 case DW_AT_decimal_scale:
12364 return "DW_AT_decimal_scale";
12365 case DW_AT_small:
12366 return "DW_AT_small";
12367 case DW_AT_decimal_sign:
12368 return "DW_AT_decimal_sign";
12369 case DW_AT_digit_count:
12370 return "DW_AT_digit_count";
12371 case DW_AT_picture_string:
12372 return "DW_AT_picture_string";
12373 case DW_AT_mutable:
12374 return "DW_AT_mutable";
12375 case DW_AT_threads_scaled:
12376 return "DW_AT_threads_scaled";
12377 case DW_AT_explicit:
12378 return "DW_AT_explicit";
12379 case DW_AT_object_pointer:
12380 return "DW_AT_object_pointer";
12381 case DW_AT_endianity:
12382 return "DW_AT_endianity";
12383 case DW_AT_elemental:
12384 return "DW_AT_elemental";
12385 case DW_AT_pure:
12386 return "DW_AT_pure";
12387 case DW_AT_recursive:
12388 return "DW_AT_recursive";
348e048f
DE
12389 /* DWARF 4 values. */
12390 case DW_AT_signature:
12391 return "DW_AT_signature";
31ef98ae
TT
12392 case DW_AT_linkage_name:
12393 return "DW_AT_linkage_name";
b7619582 12394 /* SGI/MIPS extensions. */
c764a876 12395#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12396 case DW_AT_MIPS_fde:
12397 return "DW_AT_MIPS_fde";
c764a876 12398#endif
c906108c
SS
12399 case DW_AT_MIPS_loop_begin:
12400 return "DW_AT_MIPS_loop_begin";
12401 case DW_AT_MIPS_tail_loop_begin:
12402 return "DW_AT_MIPS_tail_loop_begin";
12403 case DW_AT_MIPS_epilog_begin:
12404 return "DW_AT_MIPS_epilog_begin";
12405 case DW_AT_MIPS_loop_unroll_factor:
12406 return "DW_AT_MIPS_loop_unroll_factor";
12407 case DW_AT_MIPS_software_pipeline_depth:
12408 return "DW_AT_MIPS_software_pipeline_depth";
12409 case DW_AT_MIPS_linkage_name:
12410 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12411 case DW_AT_MIPS_stride:
12412 return "DW_AT_MIPS_stride";
12413 case DW_AT_MIPS_abstract_name:
12414 return "DW_AT_MIPS_abstract_name";
12415 case DW_AT_MIPS_clone_origin:
12416 return "DW_AT_MIPS_clone_origin";
12417 case DW_AT_MIPS_has_inlines:
12418 return "DW_AT_MIPS_has_inlines";
b7619582 12419 /* HP extensions. */
c764a876 12420#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12421 case DW_AT_HP_block_index:
12422 return "DW_AT_HP_block_index";
c764a876 12423#endif
b7619582
GF
12424 case DW_AT_HP_unmodifiable:
12425 return "DW_AT_HP_unmodifiable";
12426 case DW_AT_HP_actuals_stmt_list:
12427 return "DW_AT_HP_actuals_stmt_list";
12428 case DW_AT_HP_proc_per_section:
12429 return "DW_AT_HP_proc_per_section";
12430 case DW_AT_HP_raw_data_ptr:
12431 return "DW_AT_HP_raw_data_ptr";
12432 case DW_AT_HP_pass_by_reference:
12433 return "DW_AT_HP_pass_by_reference";
12434 case DW_AT_HP_opt_level:
12435 return "DW_AT_HP_opt_level";
12436 case DW_AT_HP_prof_version_id:
12437 return "DW_AT_HP_prof_version_id";
12438 case DW_AT_HP_opt_flags:
12439 return "DW_AT_HP_opt_flags";
12440 case DW_AT_HP_cold_region_low_pc:
12441 return "DW_AT_HP_cold_region_low_pc";
12442 case DW_AT_HP_cold_region_high_pc:
12443 return "DW_AT_HP_cold_region_high_pc";
12444 case DW_AT_HP_all_variables_modifiable:
12445 return "DW_AT_HP_all_variables_modifiable";
12446 case DW_AT_HP_linkage_name:
12447 return "DW_AT_HP_linkage_name";
12448 case DW_AT_HP_prof_flags:
12449 return "DW_AT_HP_prof_flags";
12450 /* GNU extensions. */
c906108c
SS
12451 case DW_AT_sf_names:
12452 return "DW_AT_sf_names";
12453 case DW_AT_src_info:
12454 return "DW_AT_src_info";
12455 case DW_AT_mac_info:
12456 return "DW_AT_mac_info";
12457 case DW_AT_src_coords:
12458 return "DW_AT_src_coords";
12459 case DW_AT_body_begin:
12460 return "DW_AT_body_begin";
12461 case DW_AT_body_end:
12462 return "DW_AT_body_end";
f5f8a009
EZ
12463 case DW_AT_GNU_vector:
12464 return "DW_AT_GNU_vector";
2de00c64
DE
12465 case DW_AT_GNU_odr_signature:
12466 return "DW_AT_GNU_odr_signature";
b7619582
GF
12467 /* VMS extensions. */
12468 case DW_AT_VMS_rtnbeg_pd_address:
12469 return "DW_AT_VMS_rtnbeg_pd_address";
12470 /* UPC extension. */
12471 case DW_AT_upc_threads_scaled:
12472 return "DW_AT_upc_threads_scaled";
12473 /* PGI (STMicroelectronics) extensions. */
12474 case DW_AT_PGI_lbase:
12475 return "DW_AT_PGI_lbase";
12476 case DW_AT_PGI_soffset:
12477 return "DW_AT_PGI_soffset";
12478 case DW_AT_PGI_lstride:
12479 return "DW_AT_PGI_lstride";
c906108c
SS
12480 default:
12481 return "DW_AT_<unknown>";
12482 }
12483}
12484
12485/* Convert a DWARF value form code into its string name. */
12486
12487static char *
aa1ee363 12488dwarf_form_name (unsigned form)
c906108c
SS
12489{
12490 switch (form)
12491 {
12492 case DW_FORM_addr:
12493 return "DW_FORM_addr";
12494 case DW_FORM_block2:
12495 return "DW_FORM_block2";
12496 case DW_FORM_block4:
12497 return "DW_FORM_block4";
12498 case DW_FORM_data2:
12499 return "DW_FORM_data2";
12500 case DW_FORM_data4:
12501 return "DW_FORM_data4";
12502 case DW_FORM_data8:
12503 return "DW_FORM_data8";
12504 case DW_FORM_string:
12505 return "DW_FORM_string";
12506 case DW_FORM_block:
12507 return "DW_FORM_block";
12508 case DW_FORM_block1:
12509 return "DW_FORM_block1";
12510 case DW_FORM_data1:
12511 return "DW_FORM_data1";
12512 case DW_FORM_flag:
12513 return "DW_FORM_flag";
12514 case DW_FORM_sdata:
12515 return "DW_FORM_sdata";
12516 case DW_FORM_strp:
12517 return "DW_FORM_strp";
12518 case DW_FORM_udata:
12519 return "DW_FORM_udata";
12520 case DW_FORM_ref_addr:
12521 return "DW_FORM_ref_addr";
12522 case DW_FORM_ref1:
12523 return "DW_FORM_ref1";
12524 case DW_FORM_ref2:
12525 return "DW_FORM_ref2";
12526 case DW_FORM_ref4:
12527 return "DW_FORM_ref4";
12528 case DW_FORM_ref8:
12529 return "DW_FORM_ref8";
12530 case DW_FORM_ref_udata:
12531 return "DW_FORM_ref_udata";
12532 case DW_FORM_indirect:
12533 return "DW_FORM_indirect";
348e048f
DE
12534 case DW_FORM_sec_offset:
12535 return "DW_FORM_sec_offset";
12536 case DW_FORM_exprloc:
12537 return "DW_FORM_exprloc";
12538 case DW_FORM_flag_present:
12539 return "DW_FORM_flag_present";
55f1336d
TT
12540 case DW_FORM_ref_sig8:
12541 return "DW_FORM_ref_sig8";
c906108c
SS
12542 default:
12543 return "DW_FORM_<unknown>";
12544 }
12545}
12546
12547/* Convert a DWARF stack opcode into its string name. */
12548
9eae7c52 12549const char *
b1bfef65 12550dwarf_stack_op_name (unsigned op)
c906108c
SS
12551{
12552 switch (op)
12553 {
12554 case DW_OP_addr:
12555 return "DW_OP_addr";
12556 case DW_OP_deref:
12557 return "DW_OP_deref";
12558 case DW_OP_const1u:
12559 return "DW_OP_const1u";
12560 case DW_OP_const1s:
12561 return "DW_OP_const1s";
12562 case DW_OP_const2u:
12563 return "DW_OP_const2u";
12564 case DW_OP_const2s:
12565 return "DW_OP_const2s";
12566 case DW_OP_const4u:
12567 return "DW_OP_const4u";
12568 case DW_OP_const4s:
12569 return "DW_OP_const4s";
12570 case DW_OP_const8u:
12571 return "DW_OP_const8u";
12572 case DW_OP_const8s:
12573 return "DW_OP_const8s";
12574 case DW_OP_constu:
12575 return "DW_OP_constu";
12576 case DW_OP_consts:
12577 return "DW_OP_consts";
12578 case DW_OP_dup:
12579 return "DW_OP_dup";
12580 case DW_OP_drop:
12581 return "DW_OP_drop";
12582 case DW_OP_over:
12583 return "DW_OP_over";
12584 case DW_OP_pick:
12585 return "DW_OP_pick";
12586 case DW_OP_swap:
12587 return "DW_OP_swap";
12588 case DW_OP_rot:
12589 return "DW_OP_rot";
12590 case DW_OP_xderef:
12591 return "DW_OP_xderef";
12592 case DW_OP_abs:
12593 return "DW_OP_abs";
12594 case DW_OP_and:
12595 return "DW_OP_and";
12596 case DW_OP_div:
12597 return "DW_OP_div";
12598 case DW_OP_minus:
12599 return "DW_OP_minus";
12600 case DW_OP_mod:
12601 return "DW_OP_mod";
12602 case DW_OP_mul:
12603 return "DW_OP_mul";
12604 case DW_OP_neg:
12605 return "DW_OP_neg";
12606 case DW_OP_not:
12607 return "DW_OP_not";
12608 case DW_OP_or:
12609 return "DW_OP_or";
12610 case DW_OP_plus:
12611 return "DW_OP_plus";
12612 case DW_OP_plus_uconst:
12613 return "DW_OP_plus_uconst";
12614 case DW_OP_shl:
12615 return "DW_OP_shl";
12616 case DW_OP_shr:
12617 return "DW_OP_shr";
12618 case DW_OP_shra:
12619 return "DW_OP_shra";
12620 case DW_OP_xor:
12621 return "DW_OP_xor";
12622 case DW_OP_bra:
12623 return "DW_OP_bra";
12624 case DW_OP_eq:
12625 return "DW_OP_eq";
12626 case DW_OP_ge:
12627 return "DW_OP_ge";
12628 case DW_OP_gt:
12629 return "DW_OP_gt";
12630 case DW_OP_le:
12631 return "DW_OP_le";
12632 case DW_OP_lt:
12633 return "DW_OP_lt";
12634 case DW_OP_ne:
12635 return "DW_OP_ne";
12636 case DW_OP_skip:
12637 return "DW_OP_skip";
12638 case DW_OP_lit0:
12639 return "DW_OP_lit0";
12640 case DW_OP_lit1:
12641 return "DW_OP_lit1";
12642 case DW_OP_lit2:
12643 return "DW_OP_lit2";
12644 case DW_OP_lit3:
12645 return "DW_OP_lit3";
12646 case DW_OP_lit4:
12647 return "DW_OP_lit4";
12648 case DW_OP_lit5:
12649 return "DW_OP_lit5";
12650 case DW_OP_lit6:
12651 return "DW_OP_lit6";
12652 case DW_OP_lit7:
12653 return "DW_OP_lit7";
12654 case DW_OP_lit8:
12655 return "DW_OP_lit8";
12656 case DW_OP_lit9:
12657 return "DW_OP_lit9";
12658 case DW_OP_lit10:
12659 return "DW_OP_lit10";
12660 case DW_OP_lit11:
12661 return "DW_OP_lit11";
12662 case DW_OP_lit12:
12663 return "DW_OP_lit12";
12664 case DW_OP_lit13:
12665 return "DW_OP_lit13";
12666 case DW_OP_lit14:
12667 return "DW_OP_lit14";
12668 case DW_OP_lit15:
12669 return "DW_OP_lit15";
12670 case DW_OP_lit16:
12671 return "DW_OP_lit16";
12672 case DW_OP_lit17:
12673 return "DW_OP_lit17";
12674 case DW_OP_lit18:
12675 return "DW_OP_lit18";
12676 case DW_OP_lit19:
12677 return "DW_OP_lit19";
12678 case DW_OP_lit20:
12679 return "DW_OP_lit20";
12680 case DW_OP_lit21:
12681 return "DW_OP_lit21";
12682 case DW_OP_lit22:
12683 return "DW_OP_lit22";
12684 case DW_OP_lit23:
12685 return "DW_OP_lit23";
12686 case DW_OP_lit24:
12687 return "DW_OP_lit24";
12688 case DW_OP_lit25:
12689 return "DW_OP_lit25";
12690 case DW_OP_lit26:
12691 return "DW_OP_lit26";
12692 case DW_OP_lit27:
12693 return "DW_OP_lit27";
12694 case DW_OP_lit28:
12695 return "DW_OP_lit28";
12696 case DW_OP_lit29:
12697 return "DW_OP_lit29";
12698 case DW_OP_lit30:
12699 return "DW_OP_lit30";
12700 case DW_OP_lit31:
12701 return "DW_OP_lit31";
12702 case DW_OP_reg0:
12703 return "DW_OP_reg0";
12704 case DW_OP_reg1:
12705 return "DW_OP_reg1";
12706 case DW_OP_reg2:
12707 return "DW_OP_reg2";
12708 case DW_OP_reg3:
12709 return "DW_OP_reg3";
12710 case DW_OP_reg4:
12711 return "DW_OP_reg4";
12712 case DW_OP_reg5:
12713 return "DW_OP_reg5";
12714 case DW_OP_reg6:
12715 return "DW_OP_reg6";
12716 case DW_OP_reg7:
12717 return "DW_OP_reg7";
12718 case DW_OP_reg8:
12719 return "DW_OP_reg8";
12720 case DW_OP_reg9:
12721 return "DW_OP_reg9";
12722 case DW_OP_reg10:
12723 return "DW_OP_reg10";
12724 case DW_OP_reg11:
12725 return "DW_OP_reg11";
12726 case DW_OP_reg12:
12727 return "DW_OP_reg12";
12728 case DW_OP_reg13:
12729 return "DW_OP_reg13";
12730 case DW_OP_reg14:
12731 return "DW_OP_reg14";
12732 case DW_OP_reg15:
12733 return "DW_OP_reg15";
12734 case DW_OP_reg16:
12735 return "DW_OP_reg16";
12736 case DW_OP_reg17:
12737 return "DW_OP_reg17";
12738 case DW_OP_reg18:
12739 return "DW_OP_reg18";
12740 case DW_OP_reg19:
12741 return "DW_OP_reg19";
12742 case DW_OP_reg20:
12743 return "DW_OP_reg20";
12744 case DW_OP_reg21:
12745 return "DW_OP_reg21";
12746 case DW_OP_reg22:
12747 return "DW_OP_reg22";
12748 case DW_OP_reg23:
12749 return "DW_OP_reg23";
12750 case DW_OP_reg24:
12751 return "DW_OP_reg24";
12752 case DW_OP_reg25:
12753 return "DW_OP_reg25";
12754 case DW_OP_reg26:
12755 return "DW_OP_reg26";
12756 case DW_OP_reg27:
12757 return "DW_OP_reg27";
12758 case DW_OP_reg28:
12759 return "DW_OP_reg28";
12760 case DW_OP_reg29:
12761 return "DW_OP_reg29";
12762 case DW_OP_reg30:
12763 return "DW_OP_reg30";
12764 case DW_OP_reg31:
12765 return "DW_OP_reg31";
12766 case DW_OP_breg0:
12767 return "DW_OP_breg0";
12768 case DW_OP_breg1:
12769 return "DW_OP_breg1";
12770 case DW_OP_breg2:
12771 return "DW_OP_breg2";
12772 case DW_OP_breg3:
12773 return "DW_OP_breg3";
12774 case DW_OP_breg4:
12775 return "DW_OP_breg4";
12776 case DW_OP_breg5:
12777 return "DW_OP_breg5";
12778 case DW_OP_breg6:
12779 return "DW_OP_breg6";
12780 case DW_OP_breg7:
12781 return "DW_OP_breg7";
12782 case DW_OP_breg8:
12783 return "DW_OP_breg8";
12784 case DW_OP_breg9:
12785 return "DW_OP_breg9";
12786 case DW_OP_breg10:
12787 return "DW_OP_breg10";
12788 case DW_OP_breg11:
12789 return "DW_OP_breg11";
12790 case DW_OP_breg12:
12791 return "DW_OP_breg12";
12792 case DW_OP_breg13:
12793 return "DW_OP_breg13";
12794 case DW_OP_breg14:
12795 return "DW_OP_breg14";
12796 case DW_OP_breg15:
12797 return "DW_OP_breg15";
12798 case DW_OP_breg16:
12799 return "DW_OP_breg16";
12800 case DW_OP_breg17:
12801 return "DW_OP_breg17";
12802 case DW_OP_breg18:
12803 return "DW_OP_breg18";
12804 case DW_OP_breg19:
12805 return "DW_OP_breg19";
12806 case DW_OP_breg20:
12807 return "DW_OP_breg20";
12808 case DW_OP_breg21:
12809 return "DW_OP_breg21";
12810 case DW_OP_breg22:
12811 return "DW_OP_breg22";
12812 case DW_OP_breg23:
12813 return "DW_OP_breg23";
12814 case DW_OP_breg24:
12815 return "DW_OP_breg24";
12816 case DW_OP_breg25:
12817 return "DW_OP_breg25";
12818 case DW_OP_breg26:
12819 return "DW_OP_breg26";
12820 case DW_OP_breg27:
12821 return "DW_OP_breg27";
12822 case DW_OP_breg28:
12823 return "DW_OP_breg28";
12824 case DW_OP_breg29:
12825 return "DW_OP_breg29";
12826 case DW_OP_breg30:
12827 return "DW_OP_breg30";
12828 case DW_OP_breg31:
12829 return "DW_OP_breg31";
12830 case DW_OP_regx:
12831 return "DW_OP_regx";
12832 case DW_OP_fbreg:
12833 return "DW_OP_fbreg";
12834 case DW_OP_bregx:
12835 return "DW_OP_bregx";
12836 case DW_OP_piece:
12837 return "DW_OP_piece";
12838 case DW_OP_deref_size:
12839 return "DW_OP_deref_size";
12840 case DW_OP_xderef_size:
12841 return "DW_OP_xderef_size";
12842 case DW_OP_nop:
12843 return "DW_OP_nop";
b7619582 12844 /* DWARF 3 extensions. */
ed348acc
EZ
12845 case DW_OP_push_object_address:
12846 return "DW_OP_push_object_address";
12847 case DW_OP_call2:
12848 return "DW_OP_call2";
12849 case DW_OP_call4:
12850 return "DW_OP_call4";
12851 case DW_OP_call_ref:
12852 return "DW_OP_call_ref";
b7619582
GF
12853 case DW_OP_form_tls_address:
12854 return "DW_OP_form_tls_address";
12855 case DW_OP_call_frame_cfa:
12856 return "DW_OP_call_frame_cfa";
12857 case DW_OP_bit_piece:
12858 return "DW_OP_bit_piece";
9eae7c52
TT
12859 /* DWARF 4 extensions. */
12860 case DW_OP_implicit_value:
12861 return "DW_OP_implicit_value";
12862 case DW_OP_stack_value:
12863 return "DW_OP_stack_value";
12864 /* GNU extensions. */
ed348acc
EZ
12865 case DW_OP_GNU_push_tls_address:
12866 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
12867 case DW_OP_GNU_uninit:
12868 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
12869 case DW_OP_GNU_implicit_pointer:
12870 return "DW_OP_GNU_implicit_pointer";
c906108c 12871 default:
b1bfef65 12872 return NULL;
c906108c
SS
12873 }
12874}
12875
12876static char *
fba45db2 12877dwarf_bool_name (unsigned mybool)
c906108c
SS
12878{
12879 if (mybool)
12880 return "TRUE";
12881 else
12882 return "FALSE";
12883}
12884
12885/* Convert a DWARF type code into its string name. */
12886
12887static char *
aa1ee363 12888dwarf_type_encoding_name (unsigned enc)
c906108c
SS
12889{
12890 switch (enc)
12891 {
b7619582
GF
12892 case DW_ATE_void:
12893 return "DW_ATE_void";
c906108c
SS
12894 case DW_ATE_address:
12895 return "DW_ATE_address";
12896 case DW_ATE_boolean:
12897 return "DW_ATE_boolean";
12898 case DW_ATE_complex_float:
12899 return "DW_ATE_complex_float";
12900 case DW_ATE_float:
12901 return "DW_ATE_float";
12902 case DW_ATE_signed:
12903 return "DW_ATE_signed";
12904 case DW_ATE_signed_char:
12905 return "DW_ATE_signed_char";
12906 case DW_ATE_unsigned:
12907 return "DW_ATE_unsigned";
12908 case DW_ATE_unsigned_char:
12909 return "DW_ATE_unsigned_char";
b7619582 12910 /* DWARF 3. */
d9fa45fe
DC
12911 case DW_ATE_imaginary_float:
12912 return "DW_ATE_imaginary_float";
b7619582
GF
12913 case DW_ATE_packed_decimal:
12914 return "DW_ATE_packed_decimal";
12915 case DW_ATE_numeric_string:
12916 return "DW_ATE_numeric_string";
12917 case DW_ATE_edited:
12918 return "DW_ATE_edited";
12919 case DW_ATE_signed_fixed:
12920 return "DW_ATE_signed_fixed";
12921 case DW_ATE_unsigned_fixed:
12922 return "DW_ATE_unsigned_fixed";
12923 case DW_ATE_decimal_float:
12924 return "DW_ATE_decimal_float";
75079b2b
TT
12925 /* DWARF 4. */
12926 case DW_ATE_UTF:
12927 return "DW_ATE_UTF";
b7619582
GF
12928 /* HP extensions. */
12929 case DW_ATE_HP_float80:
12930 return "DW_ATE_HP_float80";
12931 case DW_ATE_HP_complex_float80:
12932 return "DW_ATE_HP_complex_float80";
12933 case DW_ATE_HP_float128:
12934 return "DW_ATE_HP_float128";
12935 case DW_ATE_HP_complex_float128:
12936 return "DW_ATE_HP_complex_float128";
12937 case DW_ATE_HP_floathpintel:
12938 return "DW_ATE_HP_floathpintel";
12939 case DW_ATE_HP_imaginary_float80:
12940 return "DW_ATE_HP_imaginary_float80";
12941 case DW_ATE_HP_imaginary_float128:
12942 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
12943 default:
12944 return "DW_ATE_<unknown>";
12945 }
12946}
12947
0963b4bd 12948/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
12949
12950#if 0
12951static char *
aa1ee363 12952dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
12953{
12954 switch (cfi_opc)
12955 {
12956 case DW_CFA_advance_loc:
12957 return "DW_CFA_advance_loc";
12958 case DW_CFA_offset:
12959 return "DW_CFA_offset";
12960 case DW_CFA_restore:
12961 return "DW_CFA_restore";
12962 case DW_CFA_nop:
12963 return "DW_CFA_nop";
12964 case DW_CFA_set_loc:
12965 return "DW_CFA_set_loc";
12966 case DW_CFA_advance_loc1:
12967 return "DW_CFA_advance_loc1";
12968 case DW_CFA_advance_loc2:
12969 return "DW_CFA_advance_loc2";
12970 case DW_CFA_advance_loc4:
12971 return "DW_CFA_advance_loc4";
12972 case DW_CFA_offset_extended:
12973 return "DW_CFA_offset_extended";
12974 case DW_CFA_restore_extended:
12975 return "DW_CFA_restore_extended";
12976 case DW_CFA_undefined:
12977 return "DW_CFA_undefined";
12978 case DW_CFA_same_value:
12979 return "DW_CFA_same_value";
12980 case DW_CFA_register:
12981 return "DW_CFA_register";
12982 case DW_CFA_remember_state:
12983 return "DW_CFA_remember_state";
12984 case DW_CFA_restore_state:
12985 return "DW_CFA_restore_state";
12986 case DW_CFA_def_cfa:
12987 return "DW_CFA_def_cfa";
12988 case DW_CFA_def_cfa_register:
12989 return "DW_CFA_def_cfa_register";
12990 case DW_CFA_def_cfa_offset:
12991 return "DW_CFA_def_cfa_offset";
b7619582 12992 /* DWARF 3. */
985cb1a3
JM
12993 case DW_CFA_def_cfa_expression:
12994 return "DW_CFA_def_cfa_expression";
12995 case DW_CFA_expression:
12996 return "DW_CFA_expression";
12997 case DW_CFA_offset_extended_sf:
12998 return "DW_CFA_offset_extended_sf";
12999 case DW_CFA_def_cfa_sf:
13000 return "DW_CFA_def_cfa_sf";
13001 case DW_CFA_def_cfa_offset_sf:
13002 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13003 case DW_CFA_val_offset:
13004 return "DW_CFA_val_offset";
13005 case DW_CFA_val_offset_sf:
13006 return "DW_CFA_val_offset_sf";
13007 case DW_CFA_val_expression:
13008 return "DW_CFA_val_expression";
13009 /* SGI/MIPS specific. */
c906108c
SS
13010 case DW_CFA_MIPS_advance_loc8:
13011 return "DW_CFA_MIPS_advance_loc8";
b7619582 13012 /* GNU extensions. */
985cb1a3
JM
13013 case DW_CFA_GNU_window_save:
13014 return "DW_CFA_GNU_window_save";
13015 case DW_CFA_GNU_args_size:
13016 return "DW_CFA_GNU_args_size";
13017 case DW_CFA_GNU_negative_offset_extended:
13018 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13019 default:
13020 return "DW_CFA_<unknown>";
13021 }
13022}
13023#endif
13024
f9aca02d 13025static void
d97bc12b 13026dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13027{
13028 unsigned int i;
13029
d97bc12b
DE
13030 print_spaces (indent, f);
13031 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13032 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13033
13034 if (die->parent != NULL)
13035 {
13036 print_spaces (indent, f);
13037 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13038 die->parent->offset);
13039 }
13040
13041 print_spaces (indent, f);
13042 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13043 dwarf_bool_name (die->child != NULL));
c906108c 13044
d97bc12b
DE
13045 print_spaces (indent, f);
13046 fprintf_unfiltered (f, " attributes:\n");
13047
c906108c
SS
13048 for (i = 0; i < die->num_attrs; ++i)
13049 {
d97bc12b
DE
13050 print_spaces (indent, f);
13051 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13052 dwarf_attr_name (die->attrs[i].name),
13053 dwarf_form_name (die->attrs[i].form));
d97bc12b 13054
c906108c
SS
13055 switch (die->attrs[i].form)
13056 {
13057 case DW_FORM_ref_addr:
13058 case DW_FORM_addr:
d97bc12b 13059 fprintf_unfiltered (f, "address: ");
5af949e3 13060 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13061 break;
13062 case DW_FORM_block2:
13063 case DW_FORM_block4:
13064 case DW_FORM_block:
13065 case DW_FORM_block1:
3e43a32a
MS
13066 fprintf_unfiltered (f, "block: size %d",
13067 DW_BLOCK (&die->attrs[i])->size);
c906108c 13068 break;
2dc7f7b3
TT
13069 case DW_FORM_exprloc:
13070 fprintf_unfiltered (f, "expression: size %u",
13071 DW_BLOCK (&die->attrs[i])->size);
13072 break;
10b3939b
DJ
13073 case DW_FORM_ref1:
13074 case DW_FORM_ref2:
13075 case DW_FORM_ref4:
d97bc12b 13076 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13077 (long) (DW_ADDR (&die->attrs[i])));
13078 break;
c906108c
SS
13079 case DW_FORM_data1:
13080 case DW_FORM_data2:
13081 case DW_FORM_data4:
ce5d95e1 13082 case DW_FORM_data8:
c906108c
SS
13083 case DW_FORM_udata:
13084 case DW_FORM_sdata:
43bbcdc2
PH
13085 fprintf_unfiltered (f, "constant: %s",
13086 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13087 break;
2dc7f7b3
TT
13088 case DW_FORM_sec_offset:
13089 fprintf_unfiltered (f, "section offset: %s",
13090 pulongest (DW_UNSND (&die->attrs[i])));
13091 break;
55f1336d 13092 case DW_FORM_ref_sig8:
348e048f
DE
13093 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13094 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13095 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13096 else
13097 fprintf_unfiltered (f, "signatured type, offset: unknown");
13098 break;
c906108c 13099 case DW_FORM_string:
4bdf3d34 13100 case DW_FORM_strp:
8285870a 13101 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13102 DW_STRING (&die->attrs[i])
8285870a
JK
13103 ? DW_STRING (&die->attrs[i]) : "",
13104 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13105 break;
13106 case DW_FORM_flag:
13107 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13108 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13109 else
d97bc12b 13110 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13111 break;
2dc7f7b3
TT
13112 case DW_FORM_flag_present:
13113 fprintf_unfiltered (f, "flag: TRUE");
13114 break;
a8329558 13115 case DW_FORM_indirect:
0963b4bd
MS
13116 /* The reader will have reduced the indirect form to
13117 the "base form" so this form should not occur. */
3e43a32a
MS
13118 fprintf_unfiltered (f,
13119 "unexpected attribute form: DW_FORM_indirect");
a8329558 13120 break;
c906108c 13121 default:
d97bc12b 13122 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13123 die->attrs[i].form);
d97bc12b 13124 break;
c906108c 13125 }
d97bc12b 13126 fprintf_unfiltered (f, "\n");
c906108c
SS
13127 }
13128}
13129
f9aca02d 13130static void
d97bc12b 13131dump_die_for_error (struct die_info *die)
c906108c 13132{
d97bc12b
DE
13133 dump_die_shallow (gdb_stderr, 0, die);
13134}
13135
13136static void
13137dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13138{
13139 int indent = level * 4;
13140
13141 gdb_assert (die != NULL);
13142
13143 if (level >= max_level)
13144 return;
13145
13146 dump_die_shallow (f, indent, die);
13147
13148 if (die->child != NULL)
c906108c 13149 {
d97bc12b
DE
13150 print_spaces (indent, f);
13151 fprintf_unfiltered (f, " Children:");
13152 if (level + 1 < max_level)
13153 {
13154 fprintf_unfiltered (f, "\n");
13155 dump_die_1 (f, level + 1, max_level, die->child);
13156 }
13157 else
13158 {
3e43a32a
MS
13159 fprintf_unfiltered (f,
13160 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13161 }
13162 }
13163
13164 if (die->sibling != NULL && level > 0)
13165 {
13166 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13167 }
13168}
13169
d97bc12b
DE
13170/* This is called from the pdie macro in gdbinit.in.
13171 It's not static so gcc will keep a copy callable from gdb. */
13172
13173void
13174dump_die (struct die_info *die, int max_level)
13175{
13176 dump_die_1 (gdb_stdlog, 0, max_level, die);
13177}
13178
f9aca02d 13179static void
51545339 13180store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13181{
51545339 13182 void **slot;
c906108c 13183
51545339
DJ
13184 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13185
13186 *slot = die;
c906108c
SS
13187}
13188
93311388
DE
13189static int
13190is_ref_attr (struct attribute *attr)
c906108c 13191{
c906108c
SS
13192 switch (attr->form)
13193 {
13194 case DW_FORM_ref_addr:
c906108c
SS
13195 case DW_FORM_ref1:
13196 case DW_FORM_ref2:
13197 case DW_FORM_ref4:
613e1657 13198 case DW_FORM_ref8:
c906108c 13199 case DW_FORM_ref_udata:
93311388 13200 return 1;
c906108c 13201 default:
93311388 13202 return 0;
c906108c 13203 }
93311388
DE
13204}
13205
13206static unsigned int
13207dwarf2_get_ref_die_offset (struct attribute *attr)
13208{
13209 if (is_ref_attr (attr))
13210 return DW_ADDR (attr);
13211
13212 complaint (&symfile_complaints,
13213 _("unsupported die ref attribute form: '%s'"),
13214 dwarf_form_name (attr->form));
13215 return 0;
c906108c
SS
13216}
13217
43bbcdc2
PH
13218/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13219 * the value held by the attribute is not constant. */
a02abb62 13220
43bbcdc2 13221static LONGEST
a02abb62
JB
13222dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13223{
13224 if (attr->form == DW_FORM_sdata)
13225 return DW_SND (attr);
13226 else if (attr->form == DW_FORM_udata
13227 || attr->form == DW_FORM_data1
13228 || attr->form == DW_FORM_data2
13229 || attr->form == DW_FORM_data4
13230 || attr->form == DW_FORM_data8)
13231 return DW_UNSND (attr);
13232 else
13233 {
3e43a32a
MS
13234 complaint (&symfile_complaints,
13235 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13236 dwarf_form_name (attr->form));
13237 return default_value;
13238 }
13239}
13240
03dd20cc 13241/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13242 unit and add it to our queue.
13243 The result is non-zero if PER_CU was queued, otherwise the result is zero
13244 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13245
348e048f 13246static int
03dd20cc
DJ
13247maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13248 struct dwarf2_per_cu_data *per_cu)
13249{
98bfdba5
PA
13250 /* We may arrive here during partial symbol reading, if we need full
13251 DIEs to process an unusual case (e.g. template arguments). Do
13252 not queue PER_CU, just tell our caller to load its DIEs. */
13253 if (dwarf2_per_objfile->reading_partial_symbols)
13254 {
13255 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13256 return 1;
13257 return 0;
13258 }
13259
03dd20cc
DJ
13260 /* Mark the dependence relation so that we don't flush PER_CU
13261 too early. */
13262 dwarf2_add_dependence (this_cu, per_cu);
13263
13264 /* If it's already on the queue, we have nothing to do. */
13265 if (per_cu->queued)
348e048f 13266 return 0;
03dd20cc
DJ
13267
13268 /* If the compilation unit is already loaded, just mark it as
13269 used. */
13270 if (per_cu->cu != NULL)
13271 {
13272 per_cu->cu->last_used = 0;
348e048f 13273 return 0;
03dd20cc
DJ
13274 }
13275
13276 /* Add it to the queue. */
13277 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13278
13279 return 1;
13280}
13281
13282/* Follow reference or signature attribute ATTR of SRC_DIE.
13283 On entry *REF_CU is the CU of SRC_DIE.
13284 On exit *REF_CU is the CU of the result. */
13285
13286static struct die_info *
13287follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13288 struct dwarf2_cu **ref_cu)
13289{
13290 struct die_info *die;
13291
13292 if (is_ref_attr (attr))
13293 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 13294 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
13295 die = follow_die_sig (src_die, attr, ref_cu);
13296 else
13297 {
13298 dump_die_for_error (src_die);
13299 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13300 (*ref_cu)->objfile->name);
13301 }
13302
13303 return die;
03dd20cc
DJ
13304}
13305
5c631832 13306/* Follow reference OFFSET.
673bfd45
DE
13307 On entry *REF_CU is the CU of the source die referencing OFFSET.
13308 On exit *REF_CU is the CU of the result.
13309 Returns NULL if OFFSET is invalid. */
f504f079 13310
f9aca02d 13311static struct die_info *
5c631832 13312follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13313{
10b3939b 13314 struct die_info temp_die;
f2f0e013 13315 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13316
348e048f
DE
13317 gdb_assert (cu->per_cu != NULL);
13318
98bfdba5
PA
13319 target_cu = cu;
13320
348e048f
DE
13321 if (cu->per_cu->from_debug_types)
13322 {
13323 /* .debug_types CUs cannot reference anything outside their CU.
13324 If they need to, they have to reference a signatured type via
55f1336d 13325 DW_FORM_ref_sig8. */
348e048f 13326 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13327 return NULL;
348e048f
DE
13328 }
13329 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13330 {
13331 struct dwarf2_per_cu_data *per_cu;
9a619af0 13332
45452591 13333 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13334
13335 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13336 if (maybe_queue_comp_unit (cu, per_cu))
13337 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13338
10b3939b
DJ
13339 target_cu = per_cu->cu;
13340 }
98bfdba5
PA
13341 else if (cu->dies == NULL)
13342 {
13343 /* We're loading full DIEs during partial symbol reading. */
13344 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13345 load_full_comp_unit (cu->per_cu, cu->objfile);
13346 }
c906108c 13347
f2f0e013 13348 *ref_cu = target_cu;
51545339 13349 temp_die.offset = offset;
5c631832
JK
13350 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13351}
10b3939b 13352
5c631832
JK
13353/* Follow reference attribute ATTR of SRC_DIE.
13354 On entry *REF_CU is the CU of SRC_DIE.
13355 On exit *REF_CU is the CU of the result. */
13356
13357static struct die_info *
13358follow_die_ref (struct die_info *src_die, struct attribute *attr,
13359 struct dwarf2_cu **ref_cu)
13360{
13361 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13362 struct dwarf2_cu *cu = *ref_cu;
13363 struct die_info *die;
13364
13365 die = follow_die_offset (offset, ref_cu);
13366 if (!die)
13367 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13368 "at 0x%x [in module %s]"),
13369 offset, src_die->offset, cu->objfile->name);
348e048f 13370
5c631832
JK
13371 return die;
13372}
13373
13374/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13375 value is intended for DW_OP_call*. */
13376
13377struct dwarf2_locexpr_baton
13378dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13379 struct dwarf2_per_cu_data *per_cu,
13380 CORE_ADDR (*get_frame_pc) (void *baton),
13381 void *baton)
5c631832
JK
13382{
13383 struct dwarf2_cu *cu = per_cu->cu;
13384 struct die_info *die;
13385 struct attribute *attr;
13386 struct dwarf2_locexpr_baton retval;
13387
8cf6f0b1
TT
13388 dw2_setup (per_cu->objfile);
13389
5c631832
JK
13390 die = follow_die_offset (offset, &cu);
13391 if (!die)
13392 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13393 offset, per_cu->cu->objfile->name);
13394
13395 attr = dwarf2_attr (die, DW_AT_location, cu);
13396 if (!attr)
13397 {
13398 /* DWARF: "If there is no such attribute, then there is no effect.". */
13399
13400 retval.data = NULL;
13401 retval.size = 0;
13402 }
8cf6f0b1
TT
13403 else if (attr_form_is_section_offset (attr))
13404 {
13405 struct dwarf2_loclist_baton loclist_baton;
13406 CORE_ADDR pc = (*get_frame_pc) (baton);
13407 size_t size;
13408
13409 fill_in_loclist_baton (cu, &loclist_baton, attr);
13410
13411 retval.data = dwarf2_find_location_expression (&loclist_baton,
13412 &size, pc);
13413 retval.size = size;
13414 }
5c631832
JK
13415 else
13416 {
13417 if (!attr_form_is_block (attr))
13418 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13419 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13420 offset, per_cu->cu->objfile->name);
13421
13422 retval.data = DW_BLOCK (attr)->data;
13423 retval.size = DW_BLOCK (attr)->size;
13424 }
13425 retval.per_cu = cu->per_cu;
13426 return retval;
348e048f
DE
13427}
13428
13429/* Follow the signature attribute ATTR in SRC_DIE.
13430 On entry *REF_CU is the CU of SRC_DIE.
13431 On exit *REF_CU is the CU of the result. */
13432
13433static struct die_info *
13434follow_die_sig (struct die_info *src_die, struct attribute *attr,
13435 struct dwarf2_cu **ref_cu)
13436{
13437 struct objfile *objfile = (*ref_cu)->objfile;
13438 struct die_info temp_die;
13439 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13440 struct dwarf2_cu *sig_cu;
13441 struct die_info *die;
13442
13443 /* sig_type will be NULL if the signatured type is missing from
13444 the debug info. */
13445 if (sig_type == NULL)
13446 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13447 "at 0x%x [in module %s]"),
13448 src_die->offset, objfile->name);
13449
13450 /* If necessary, add it to the queue and load its DIEs. */
13451
13452 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13453 read_signatured_type (objfile, sig_type);
13454
13455 gdb_assert (sig_type->per_cu.cu != NULL);
13456
13457 sig_cu = sig_type->per_cu.cu;
13458 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13459 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13460 if (die)
13461 {
13462 *ref_cu = sig_cu;
13463 return die;
13464 }
13465
3e43a32a
MS
13466 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13467 "from DIE at 0x%x [in module %s]"),
348e048f
DE
13468 sig_type->type_offset, src_die->offset, objfile->name);
13469}
13470
13471/* Given an offset of a signatured type, return its signatured_type. */
13472
13473static struct signatured_type *
13474lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13475{
13476 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13477 unsigned int length, initial_length_size;
13478 unsigned int sig_offset;
13479 struct signatured_type find_entry, *type_sig;
13480
13481 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13482 sig_offset = (initial_length_size
13483 + 2 /*version*/
13484 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13485 + 1 /*address_size*/);
13486 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13487 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13488
13489 /* This is only used to lookup previously recorded types.
13490 If we didn't find it, it's our bug. */
13491 gdb_assert (type_sig != NULL);
13492 gdb_assert (offset == type_sig->offset);
13493
13494 return type_sig;
13495}
13496
13497/* Read in signatured type at OFFSET and build its CU and die(s). */
13498
13499static void
13500read_signatured_type_at_offset (struct objfile *objfile,
13501 unsigned int offset)
13502{
13503 struct signatured_type *type_sig;
13504
be391dca
TT
13505 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13506
348e048f
DE
13507 /* We have the section offset, but we need the signature to do the
13508 hash table lookup. */
13509 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13510
13511 gdb_assert (type_sig->per_cu.cu == NULL);
13512
13513 read_signatured_type (objfile, type_sig);
13514
13515 gdb_assert (type_sig->per_cu.cu != NULL);
13516}
13517
13518/* Read in a signatured type and build its CU and DIEs. */
13519
13520static void
13521read_signatured_type (struct objfile *objfile,
13522 struct signatured_type *type_sig)
13523{
1fd400ff 13524 gdb_byte *types_ptr;
348e048f
DE
13525 struct die_reader_specs reader_specs;
13526 struct dwarf2_cu *cu;
13527 ULONGEST signature;
13528 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13529
1fd400ff
TT
13530 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13531 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13532
348e048f
DE
13533 gdb_assert (type_sig->per_cu.cu == NULL);
13534
9816fde3
JK
13535 cu = xmalloc (sizeof (*cu));
13536 init_one_comp_unit (cu, objfile);
13537
348e048f
DE
13538 type_sig->per_cu.cu = cu;
13539 cu->per_cu = &type_sig->per_cu;
13540
13541 /* If an error occurs while loading, release our storage. */
13542 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13543
13544 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13545 types_ptr, objfile->obfd);
13546 gdb_assert (signature == type_sig->signature);
13547
13548 cu->die_hash
13549 = htab_create_alloc_ex (cu->header.length / 12,
13550 die_hash,
13551 die_eq,
13552 NULL,
13553 &cu->comp_unit_obstack,
13554 hashtab_obstack_allocate,
13555 dummy_obstack_deallocate);
13556
13557 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13558 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13559
13560 init_cu_die_reader (&reader_specs, cu);
13561
13562 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13563 NULL /*parent*/);
13564
13565 /* We try not to read any attributes in this function, because not
13566 all objfiles needed for references have been loaded yet, and symbol
13567 table processing isn't initialized. But we have to set the CU language,
13568 or we won't be able to build types correctly. */
9816fde3 13569 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
13570
13571 do_cleanups (back_to);
13572
13573 /* We've successfully allocated this compilation unit. Let our caller
13574 clean it up when finished with it. */
13575 discard_cleanups (free_cu_cleanup);
13576
13577 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13578 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
13579}
13580
c906108c
SS
13581/* Decode simple location descriptions.
13582 Given a pointer to a dwarf block that defines a location, compute
13583 the location and return the value.
13584
4cecd739
DJ
13585 NOTE drow/2003-11-18: This function is called in two situations
13586 now: for the address of static or global variables (partial symbols
13587 only) and for offsets into structures which are expected to be
13588 (more or less) constant. The partial symbol case should go away,
13589 and only the constant case should remain. That will let this
13590 function complain more accurately. A few special modes are allowed
13591 without complaint for global variables (for instance, global
13592 register values and thread-local values).
c906108c
SS
13593
13594 A location description containing no operations indicates that the
4cecd739 13595 object is optimized out. The return value is 0 for that case.
6b992462
DJ
13596 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13597 callers will only want a very basic result and this can become a
13598 complaint.
c906108c 13599
d53d4ac5 13600 Note that stack[0] is unused except as a default error return. */
c906108c
SS
13601
13602static CORE_ADDR
e7c27a73 13603decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 13604{
e7c27a73 13605 struct objfile *objfile = cu->objfile;
c906108c
SS
13606 int i;
13607 int size = blk->size;
fe1b8b76 13608 gdb_byte *data = blk->data;
c906108c
SS
13609 CORE_ADDR stack[64];
13610 int stacki;
13611 unsigned int bytes_read, unsnd;
fe1b8b76 13612 gdb_byte op;
c906108c
SS
13613
13614 i = 0;
13615 stacki = 0;
13616 stack[stacki] = 0;
d53d4ac5 13617 stack[++stacki] = 0;
c906108c
SS
13618
13619 while (i < size)
13620 {
c906108c
SS
13621 op = data[i++];
13622 switch (op)
13623 {
f1bea926
JM
13624 case DW_OP_lit0:
13625 case DW_OP_lit1:
13626 case DW_OP_lit2:
13627 case DW_OP_lit3:
13628 case DW_OP_lit4:
13629 case DW_OP_lit5:
13630 case DW_OP_lit6:
13631 case DW_OP_lit7:
13632 case DW_OP_lit8:
13633 case DW_OP_lit9:
13634 case DW_OP_lit10:
13635 case DW_OP_lit11:
13636 case DW_OP_lit12:
13637 case DW_OP_lit13:
13638 case DW_OP_lit14:
13639 case DW_OP_lit15:
13640 case DW_OP_lit16:
13641 case DW_OP_lit17:
13642 case DW_OP_lit18:
13643 case DW_OP_lit19:
13644 case DW_OP_lit20:
13645 case DW_OP_lit21:
13646 case DW_OP_lit22:
13647 case DW_OP_lit23:
13648 case DW_OP_lit24:
13649 case DW_OP_lit25:
13650 case DW_OP_lit26:
13651 case DW_OP_lit27:
13652 case DW_OP_lit28:
13653 case DW_OP_lit29:
13654 case DW_OP_lit30:
13655 case DW_OP_lit31:
13656 stack[++stacki] = op - DW_OP_lit0;
13657 break;
13658
c906108c
SS
13659 case DW_OP_reg0:
13660 case DW_OP_reg1:
13661 case DW_OP_reg2:
13662 case DW_OP_reg3:
13663 case DW_OP_reg4:
13664 case DW_OP_reg5:
13665 case DW_OP_reg6:
13666 case DW_OP_reg7:
13667 case DW_OP_reg8:
13668 case DW_OP_reg9:
13669 case DW_OP_reg10:
13670 case DW_OP_reg11:
13671 case DW_OP_reg12:
13672 case DW_OP_reg13:
13673 case DW_OP_reg14:
13674 case DW_OP_reg15:
13675 case DW_OP_reg16:
13676 case DW_OP_reg17:
13677 case DW_OP_reg18:
13678 case DW_OP_reg19:
13679 case DW_OP_reg20:
13680 case DW_OP_reg21:
13681 case DW_OP_reg22:
13682 case DW_OP_reg23:
13683 case DW_OP_reg24:
13684 case DW_OP_reg25:
13685 case DW_OP_reg26:
13686 case DW_OP_reg27:
13687 case DW_OP_reg28:
13688 case DW_OP_reg29:
13689 case DW_OP_reg30:
13690 case DW_OP_reg31:
c906108c 13691 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
13692 if (i < size)
13693 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13694 break;
13695
13696 case DW_OP_regx:
c906108c
SS
13697 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13698 i += bytes_read;
c906108c 13699 stack[++stacki] = unsnd;
4cecd739
DJ
13700 if (i < size)
13701 dwarf2_complex_location_expr_complaint ();
c906108c
SS
13702 break;
13703
13704 case DW_OP_addr:
107d2387 13705 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 13706 cu, &bytes_read);
107d2387 13707 i += bytes_read;
c906108c
SS
13708 break;
13709
13710 case DW_OP_const1u:
13711 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13712 i += 1;
13713 break;
13714
13715 case DW_OP_const1s:
13716 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13717 i += 1;
13718 break;
13719
13720 case DW_OP_const2u:
13721 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13722 i += 2;
13723 break;
13724
13725 case DW_OP_const2s:
13726 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13727 i += 2;
13728 break;
13729
13730 case DW_OP_const4u:
13731 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13732 i += 4;
13733 break;
13734
13735 case DW_OP_const4s:
13736 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13737 i += 4;
13738 break;
13739
13740 case DW_OP_constu:
13741 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 13742 &bytes_read);
c906108c
SS
13743 i += bytes_read;
13744 break;
13745
13746 case DW_OP_consts:
13747 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13748 i += bytes_read;
13749 break;
13750
f1bea926
JM
13751 case DW_OP_dup:
13752 stack[stacki + 1] = stack[stacki];
13753 stacki++;
13754 break;
13755
c906108c
SS
13756 case DW_OP_plus:
13757 stack[stacki - 1] += stack[stacki];
13758 stacki--;
13759 break;
13760
13761 case DW_OP_plus_uconst:
3e43a32a
MS
13762 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
13763 &bytes_read);
c906108c
SS
13764 i += bytes_read;
13765 break;
13766
13767 case DW_OP_minus:
f1bea926 13768 stack[stacki - 1] -= stack[stacki];
c906108c
SS
13769 stacki--;
13770 break;
13771
7a292a7a 13772 case DW_OP_deref:
7a292a7a 13773 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
13774 this using GDB's address_class enum. This is valid for partial
13775 global symbols, although the variable's address will be bogus
13776 in the psymtab. */
7a292a7a 13777 if (i < size)
4d3c2250 13778 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
13779 break;
13780
9d774e44 13781 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
13782 /* The top of the stack has the offset from the beginning
13783 of the thread control block at which the variable is located. */
13784 /* Nothing should follow this operator, so the top of stack would
13785 be returned. */
4cecd739
DJ
13786 /* This is valid for partial global symbols, but the variable's
13787 address will be bogus in the psymtab. */
9d774e44 13788 if (i < size)
4d3c2250 13789 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
13790 break;
13791
42be36b3
CT
13792 case DW_OP_GNU_uninit:
13793 break;
13794
c906108c 13795 default:
b1bfef65
TT
13796 {
13797 const char *name = dwarf_stack_op_name (op);
13798
13799 if (name)
13800 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13801 name);
13802 else
13803 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
13804 op);
13805 }
13806
c906108c
SS
13807 return (stack[stacki]);
13808 }
d53d4ac5
TT
13809
13810 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13811 outside of the allocated space. Also enforce minimum>0. */
13812 if (stacki >= ARRAY_SIZE (stack) - 1)
13813 {
13814 complaint (&symfile_complaints,
13815 _("location description stack overflow"));
13816 return 0;
13817 }
13818
13819 if (stacki <= 0)
13820 {
13821 complaint (&symfile_complaints,
13822 _("location description stack underflow"));
13823 return 0;
13824 }
c906108c
SS
13825 }
13826 return (stack[stacki]);
13827}
13828
13829/* memory allocation interface */
13830
c906108c 13831static struct dwarf_block *
7b5a2f43 13832dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
13833{
13834 struct dwarf_block *blk;
13835
13836 blk = (struct dwarf_block *)
7b5a2f43 13837 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
13838 return (blk);
13839}
13840
13841static struct abbrev_info *
f3dd6933 13842dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
13843{
13844 struct abbrev_info *abbrev;
13845
f3dd6933
DJ
13846 abbrev = (struct abbrev_info *)
13847 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
13848 memset (abbrev, 0, sizeof (struct abbrev_info));
13849 return (abbrev);
13850}
13851
13852static struct die_info *
b60c80d6 13853dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
13854{
13855 struct die_info *die;
b60c80d6
DJ
13856 size_t size = sizeof (struct die_info);
13857
13858 if (num_attrs > 1)
13859 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 13860
b60c80d6 13861 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
13862 memset (die, 0, sizeof (struct die_info));
13863 return (die);
13864}
2e276125
JB
13865
13866\f
13867/* Macro support. */
13868
2e276125
JB
13869/* Return the full name of file number I in *LH's file name table.
13870 Use COMP_DIR as the name of the current directory of the
13871 compilation. The result is allocated using xmalloc; the caller is
13872 responsible for freeing it. */
13873static char *
13874file_full_name (int file, struct line_header *lh, const char *comp_dir)
13875{
6a83a1e6
EZ
13876 /* Is the file number a valid index into the line header's file name
13877 table? Remember that file numbers start with one, not zero. */
13878 if (1 <= file && file <= lh->num_file_names)
13879 {
13880 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 13881
6a83a1e6
EZ
13882 if (IS_ABSOLUTE_PATH (fe->name))
13883 return xstrdup (fe->name);
13884 else
13885 {
13886 const char *dir;
13887 int dir_len;
13888 char *full_name;
13889
13890 if (fe->dir_index)
13891 dir = lh->include_dirs[fe->dir_index - 1];
13892 else
13893 dir = comp_dir;
13894
13895 if (dir)
13896 {
13897 dir_len = strlen (dir);
13898 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13899 strcpy (full_name, dir);
13900 full_name[dir_len] = '/';
13901 strcpy (full_name + dir_len + 1, fe->name);
13902 return full_name;
13903 }
13904 else
13905 return xstrdup (fe->name);
13906 }
13907 }
2e276125
JB
13908 else
13909 {
6a83a1e6
EZ
13910 /* The compiler produced a bogus file number. We can at least
13911 record the macro definitions made in the file, even if we
13912 won't be able to find the file by name. */
13913 char fake_name[80];
9a619af0 13914
6a83a1e6 13915 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 13916
6e70227d 13917 complaint (&symfile_complaints,
6a83a1e6
EZ
13918 _("bad file number in macro information (%d)"),
13919 file);
2e276125 13920
6a83a1e6 13921 return xstrdup (fake_name);
2e276125
JB
13922 }
13923}
13924
13925
13926static struct macro_source_file *
13927macro_start_file (int file, int line,
13928 struct macro_source_file *current_file,
13929 const char *comp_dir,
13930 struct line_header *lh, struct objfile *objfile)
13931{
13932 /* The full name of this source file. */
13933 char *full_name = file_full_name (file, lh, comp_dir);
13934
13935 /* We don't create a macro table for this compilation unit
13936 at all until we actually get a filename. */
13937 if (! pending_macros)
4a146b47 13938 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 13939 objfile->macro_cache);
2e276125
JB
13940
13941 if (! current_file)
13942 /* If we have no current file, then this must be the start_file
13943 directive for the compilation unit's main source file. */
13944 current_file = macro_set_main (pending_macros, full_name);
13945 else
13946 current_file = macro_include (current_file, line, full_name);
13947
13948 xfree (full_name);
6e70227d 13949
2e276125
JB
13950 return current_file;
13951}
13952
13953
13954/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13955 followed by a null byte. */
13956static char *
13957copy_string (const char *buf, int len)
13958{
13959 char *s = xmalloc (len + 1);
9a619af0 13960
2e276125
JB
13961 memcpy (s, buf, len);
13962 s[len] = '\0';
2e276125
JB
13963 return s;
13964}
13965
13966
13967static const char *
13968consume_improper_spaces (const char *p, const char *body)
13969{
13970 if (*p == ' ')
13971 {
4d3c2250 13972 complaint (&symfile_complaints,
3e43a32a
MS
13973 _("macro definition contains spaces "
13974 "in formal argument list:\n`%s'"),
4d3c2250 13975 body);
2e276125
JB
13976
13977 while (*p == ' ')
13978 p++;
13979 }
13980
13981 return p;
13982}
13983
13984
13985static void
13986parse_macro_definition (struct macro_source_file *file, int line,
13987 const char *body)
13988{
13989 const char *p;
13990
13991 /* The body string takes one of two forms. For object-like macro
13992 definitions, it should be:
13993
13994 <macro name> " " <definition>
13995
13996 For function-like macro definitions, it should be:
13997
13998 <macro name> "() " <definition>
13999 or
14000 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14001
14002 Spaces may appear only where explicitly indicated, and in the
14003 <definition>.
14004
14005 The Dwarf 2 spec says that an object-like macro's name is always
14006 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14007 the space when the macro's definition is the empty string.
2e276125
JB
14008
14009 The Dwarf 2 spec says that there should be no spaces between the
14010 formal arguments in a function-like macro's formal argument list,
14011 but versions of GCC around March 2002 include spaces after the
14012 commas. */
14013
14014
14015 /* Find the extent of the macro name. The macro name is terminated
14016 by either a space or null character (for an object-like macro) or
14017 an opening paren (for a function-like macro). */
14018 for (p = body; *p; p++)
14019 if (*p == ' ' || *p == '(')
14020 break;
14021
14022 if (*p == ' ' || *p == '\0')
14023 {
14024 /* It's an object-like macro. */
14025 int name_len = p - body;
14026 char *name = copy_string (body, name_len);
14027 const char *replacement;
14028
14029 if (*p == ' ')
14030 replacement = body + name_len + 1;
14031 else
14032 {
4d3c2250 14033 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14034 replacement = body + name_len;
14035 }
6e70227d 14036
2e276125
JB
14037 macro_define_object (file, line, name, replacement);
14038
14039 xfree (name);
14040 }
14041 else if (*p == '(')
14042 {
14043 /* It's a function-like macro. */
14044 char *name = copy_string (body, p - body);
14045 int argc = 0;
14046 int argv_size = 1;
14047 char **argv = xmalloc (argv_size * sizeof (*argv));
14048
14049 p++;
14050
14051 p = consume_improper_spaces (p, body);
14052
14053 /* Parse the formal argument list. */
14054 while (*p && *p != ')')
14055 {
14056 /* Find the extent of the current argument name. */
14057 const char *arg_start = p;
14058
14059 while (*p && *p != ',' && *p != ')' && *p != ' ')
14060 p++;
14061
14062 if (! *p || p == arg_start)
4d3c2250 14063 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14064 else
14065 {
14066 /* Make sure argv has room for the new argument. */
14067 if (argc >= argv_size)
14068 {
14069 argv_size *= 2;
14070 argv = xrealloc (argv, argv_size * sizeof (*argv));
14071 }
14072
14073 argv[argc++] = copy_string (arg_start, p - arg_start);
14074 }
14075
14076 p = consume_improper_spaces (p, body);
14077
14078 /* Consume the comma, if present. */
14079 if (*p == ',')
14080 {
14081 p++;
14082
14083 p = consume_improper_spaces (p, body);
14084 }
14085 }
14086
14087 if (*p == ')')
14088 {
14089 p++;
14090
14091 if (*p == ' ')
14092 /* Perfectly formed definition, no complaints. */
14093 macro_define_function (file, line, name,
6e70227d 14094 argc, (const char **) argv,
2e276125
JB
14095 p + 1);
14096 else if (*p == '\0')
14097 {
14098 /* Complain, but do define it. */
4d3c2250 14099 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14100 macro_define_function (file, line, name,
6e70227d 14101 argc, (const char **) argv,
2e276125
JB
14102 p);
14103 }
14104 else
14105 /* Just complain. */
4d3c2250 14106 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14107 }
14108 else
14109 /* Just complain. */
4d3c2250 14110 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14111
14112 xfree (name);
14113 {
14114 int i;
14115
14116 for (i = 0; i < argc; i++)
14117 xfree (argv[i]);
14118 }
14119 xfree (argv);
14120 }
14121 else
4d3c2250 14122 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14123}
14124
14125
14126static void
14127dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14128 char *comp_dir, bfd *abfd,
e7c27a73 14129 struct dwarf2_cu *cu)
2e276125 14130{
fe1b8b76 14131 gdb_byte *mac_ptr, *mac_end;
2e276125 14132 struct macro_source_file *current_file = 0;
757a13d0
JK
14133 enum dwarf_macinfo_record_type macinfo_type;
14134 int at_commandline;
2e276125 14135
be391dca
TT
14136 dwarf2_read_section (dwarf2_per_objfile->objfile,
14137 &dwarf2_per_objfile->macinfo);
dce234bc 14138 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 14139 {
e2e0b3e5 14140 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
14141 return;
14142 }
14143
757a13d0
JK
14144 /* First pass: Find the name of the base filename.
14145 This filename is needed in order to process all macros whose definition
14146 (or undefinition) comes from the command line. These macros are defined
14147 before the first DW_MACINFO_start_file entry, and yet still need to be
14148 associated to the base file.
14149
14150 To determine the base file name, we scan the macro definitions until we
14151 reach the first DW_MACINFO_start_file entry. We then initialize
14152 CURRENT_FILE accordingly so that any macro definition found before the
14153 first DW_MACINFO_start_file can still be associated to the base file. */
14154
dce234bc
PP
14155 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14156 mac_end = dwarf2_per_objfile->macinfo.buffer
14157 + dwarf2_per_objfile->macinfo.size;
2e276125 14158
757a13d0 14159 do
2e276125 14160 {
2e276125
JB
14161 /* Do we at least have room for a macinfo type byte? */
14162 if (mac_ptr >= mac_end)
14163 {
757a13d0 14164 /* Complaint is printed during the second pass as GDB will probably
3e43a32a
MS
14165 stop the first pass earlier upon finding
14166 DW_MACINFO_start_file. */
757a13d0 14167 break;
2e276125
JB
14168 }
14169
14170 macinfo_type = read_1_byte (abfd, mac_ptr);
14171 mac_ptr++;
14172
14173 switch (macinfo_type)
14174 {
14175 /* A zero macinfo type indicates the end of the macro
14176 information. */
14177 case 0:
757a13d0
JK
14178 break;
14179
14180 case DW_MACINFO_define:
14181 case DW_MACINFO_undef:
14182 /* Only skip the data by MAC_PTR. */
14183 {
14184 unsigned int bytes_read;
14185
14186 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14187 mac_ptr += bytes_read;
9b1c24c8 14188 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14189 mac_ptr += bytes_read;
14190 }
14191 break;
14192
14193 case DW_MACINFO_start_file:
14194 {
14195 unsigned int bytes_read;
14196 int line, file;
14197
14198 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14199 mac_ptr += bytes_read;
14200 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14201 mac_ptr += bytes_read;
14202
3e43a32a
MS
14203 current_file = macro_start_file (file, line, current_file,
14204 comp_dir, lh, cu->objfile);
757a13d0
JK
14205 }
14206 break;
14207
14208 case DW_MACINFO_end_file:
14209 /* No data to skip by MAC_PTR. */
14210 break;
14211
14212 case DW_MACINFO_vendor_ext:
14213 /* Only skip the data by MAC_PTR. */
14214 {
14215 unsigned int bytes_read;
14216
14217 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14218 mac_ptr += bytes_read;
9b1c24c8 14219 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14220 mac_ptr += bytes_read;
14221 }
14222 break;
14223
14224 default:
14225 break;
14226 }
14227 } while (macinfo_type != 0 && current_file == NULL);
14228
14229 /* Second pass: Process all entries.
14230
14231 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14232 command-line macro definitions/undefinitions. This flag is unset when we
14233 reach the first DW_MACINFO_start_file entry. */
14234
dce234bc 14235 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14236
14237 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14238 GDB is still reading the definitions from command line. First
14239 DW_MACINFO_start_file will need to be ignored as it was already executed
14240 to create CURRENT_FILE for the main source holding also the command line
14241 definitions. On first met DW_MACINFO_start_file this flag is reset to
14242 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14243
14244 at_commandline = 1;
14245
14246 do
14247 {
14248 /* Do we at least have room for a macinfo type byte? */
14249 if (mac_ptr >= mac_end)
14250 {
14251 dwarf2_macros_too_long_complaint ();
14252 break;
14253 }
14254
14255 macinfo_type = read_1_byte (abfd, mac_ptr);
14256 mac_ptr++;
14257
14258 switch (macinfo_type)
14259 {
14260 /* A zero macinfo type indicates the end of the macro
14261 information. */
14262 case 0:
14263 break;
2e276125
JB
14264
14265 case DW_MACINFO_define:
14266 case DW_MACINFO_undef:
14267 {
891d2f0b 14268 unsigned int bytes_read;
2e276125
JB
14269 int line;
14270 char *body;
14271
14272 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14273 mac_ptr += bytes_read;
9b1c24c8 14274 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14275 mac_ptr += bytes_read;
14276
14277 if (! current_file)
757a13d0
JK
14278 {
14279 /* DWARF violation as no main source is present. */
14280 complaint (&symfile_complaints,
14281 _("debug info with no main source gives macro %s "
14282 "on line %d: %s"),
6e70227d
DE
14283 macinfo_type == DW_MACINFO_define ?
14284 _("definition") :
905e0470
PM
14285 macinfo_type == DW_MACINFO_undef ?
14286 _("undefinition") :
14287 _("something-or-other"), line, body);
757a13d0
JK
14288 break;
14289 }
3e43a32a
MS
14290 if ((line == 0 && !at_commandline)
14291 || (line != 0 && at_commandline))
4d3c2250 14292 complaint (&symfile_complaints,
757a13d0
JK
14293 _("debug info gives %s macro %s with %s line %d: %s"),
14294 at_commandline ? _("command-line") : _("in-file"),
905e0470 14295 macinfo_type == DW_MACINFO_define ?
6e70227d 14296 _("definition") :
905e0470
PM
14297 macinfo_type == DW_MACINFO_undef ?
14298 _("undefinition") :
14299 _("something-or-other"),
757a13d0
JK
14300 line == 0 ? _("zero") : _("non-zero"), line, body);
14301
14302 if (macinfo_type == DW_MACINFO_define)
14303 parse_macro_definition (current_file, line, body);
14304 else if (macinfo_type == DW_MACINFO_undef)
14305 macro_undef (current_file, line, body);
2e276125
JB
14306 }
14307 break;
14308
14309 case DW_MACINFO_start_file:
14310 {
891d2f0b 14311 unsigned int bytes_read;
2e276125
JB
14312 int line, file;
14313
14314 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14315 mac_ptr += bytes_read;
14316 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14317 mac_ptr += bytes_read;
14318
3e43a32a
MS
14319 if ((line == 0 && !at_commandline)
14320 || (line != 0 && at_commandline))
757a13d0
JK
14321 complaint (&symfile_complaints,
14322 _("debug info gives source %d included "
14323 "from %s at %s line %d"),
14324 file, at_commandline ? _("command-line") : _("file"),
14325 line == 0 ? _("zero") : _("non-zero"), line);
14326
14327 if (at_commandline)
14328 {
14329 /* This DW_MACINFO_start_file was executed in the pass one. */
14330 at_commandline = 0;
14331 }
14332 else
14333 current_file = macro_start_file (file, line,
14334 current_file, comp_dir,
14335 lh, cu->objfile);
2e276125
JB
14336 }
14337 break;
14338
14339 case DW_MACINFO_end_file:
14340 if (! current_file)
4d3c2250 14341 complaint (&symfile_complaints,
3e43a32a
MS
14342 _("macro debug info has an unmatched "
14343 "`close_file' directive"));
2e276125
JB
14344 else
14345 {
14346 current_file = current_file->included_by;
14347 if (! current_file)
14348 {
14349 enum dwarf_macinfo_record_type next_type;
14350
14351 /* GCC circa March 2002 doesn't produce the zero
14352 type byte marking the end of the compilation
14353 unit. Complain if it's not there, but exit no
14354 matter what. */
14355
14356 /* Do we at least have room for a macinfo type byte? */
14357 if (mac_ptr >= mac_end)
14358 {
4d3c2250 14359 dwarf2_macros_too_long_complaint ();
2e276125
JB
14360 return;
14361 }
14362
14363 /* We don't increment mac_ptr here, so this is just
14364 a look-ahead. */
14365 next_type = read_1_byte (abfd, mac_ptr);
14366 if (next_type != 0)
4d3c2250 14367 complaint (&symfile_complaints,
3e43a32a
MS
14368 _("no terminating 0-type entry for "
14369 "macros in `.debug_macinfo' section"));
2e276125
JB
14370
14371 return;
14372 }
14373 }
14374 break;
14375
14376 case DW_MACINFO_vendor_ext:
14377 {
891d2f0b 14378 unsigned int bytes_read;
2e276125 14379 int constant;
2e276125
JB
14380
14381 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14382 mac_ptr += bytes_read;
e8e80198 14383 read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14384 mac_ptr += bytes_read;
14385
14386 /* We don't recognize any vendor extensions. */
14387 }
14388 break;
14389 }
757a13d0 14390 } while (macinfo_type != 0);
2e276125 14391}
8e19ed76
PS
14392
14393/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 14394 if so return true else false. */
8e19ed76
PS
14395static int
14396attr_form_is_block (struct attribute *attr)
14397{
14398 return (attr == NULL ? 0 :
14399 attr->form == DW_FORM_block1
14400 || attr->form == DW_FORM_block2
14401 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14402 || attr->form == DW_FORM_block
14403 || attr->form == DW_FORM_exprloc);
8e19ed76 14404}
4c2df51b 14405
c6a0999f
JB
14406/* Return non-zero if ATTR's value is a section offset --- classes
14407 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14408 You may use DW_UNSND (attr) to retrieve such offsets.
14409
14410 Section 7.5.4, "Attribute Encodings", explains that no attribute
14411 may have a value that belongs to more than one of these classes; it
14412 would be ambiguous if we did, because we use the same forms for all
14413 of them. */
3690dd37
JB
14414static int
14415attr_form_is_section_offset (struct attribute *attr)
14416{
14417 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14418 || attr->form == DW_FORM_data8
14419 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14420}
14421
14422
14423/* Return non-zero if ATTR's value falls in the 'constant' class, or
14424 zero otherwise. When this function returns true, you can apply
14425 dwarf2_get_attr_constant_value to it.
14426
14427 However, note that for some attributes you must check
14428 attr_form_is_section_offset before using this test. DW_FORM_data4
14429 and DW_FORM_data8 are members of both the constant class, and of
14430 the classes that contain offsets into other debug sections
14431 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14432 that, if an attribute's can be either a constant or one of the
14433 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14434 taken as section offsets, not constants. */
14435static int
14436attr_form_is_constant (struct attribute *attr)
14437{
14438 switch (attr->form)
14439 {
14440 case DW_FORM_sdata:
14441 case DW_FORM_udata:
14442 case DW_FORM_data1:
14443 case DW_FORM_data2:
14444 case DW_FORM_data4:
14445 case DW_FORM_data8:
14446 return 1;
14447 default:
14448 return 0;
14449 }
14450}
14451
8cf6f0b1
TT
14452/* A helper function that fills in a dwarf2_loclist_baton. */
14453
14454static void
14455fill_in_loclist_baton (struct dwarf2_cu *cu,
14456 struct dwarf2_loclist_baton *baton,
14457 struct attribute *attr)
14458{
14459 dwarf2_read_section (dwarf2_per_objfile->objfile,
14460 &dwarf2_per_objfile->loc);
14461
14462 baton->per_cu = cu->per_cu;
14463 gdb_assert (baton->per_cu);
14464 /* We don't know how long the location list is, but make sure we
14465 don't run off the edge of the section. */
14466 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14467 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14468 baton->base_address = cu->base_address;
14469}
14470
4c2df51b
DJ
14471static void
14472dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14473 struct dwarf2_cu *cu)
4c2df51b 14474{
3690dd37 14475 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14476 /* ".debug_loc" may not exist at all, or the offset may be outside
14477 the section. If so, fall through to the complaint in the
14478 other branch. */
9e0ac564
TT
14479 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14480 &dwarf2_per_objfile->loc))
4c2df51b 14481 {
0d53c4c4 14482 struct dwarf2_loclist_baton *baton;
4c2df51b 14483
4a146b47 14484 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14485 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14486
8cf6f0b1 14487 fill_in_loclist_baton (cu, baton, attr);
be391dca 14488
d00adf39 14489 if (cu->base_known == 0)
0d53c4c4 14490 complaint (&symfile_complaints,
3e43a32a
MS
14491 _("Location list used without "
14492 "specifying the CU base address."));
4c2df51b 14493
768a979c 14494 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14495 SYMBOL_LOCATION_BATON (sym) = baton;
14496 }
14497 else
14498 {
14499 struct dwarf2_locexpr_baton *baton;
14500
4a146b47 14501 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14502 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14503 baton->per_cu = cu->per_cu;
14504 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14505
14506 if (attr_form_is_block (attr))
14507 {
14508 /* Note that we're just copying the block's data pointer
14509 here, not the actual data. We're still pointing into the
6502dd73
DJ
14510 info_buffer for SYM's objfile; right now we never release
14511 that buffer, but when we do clean up properly this may
14512 need to change. */
0d53c4c4
DJ
14513 baton->size = DW_BLOCK (attr)->size;
14514 baton->data = DW_BLOCK (attr)->data;
14515 }
14516 else
14517 {
14518 dwarf2_invalid_attrib_class_complaint ("location description",
14519 SYMBOL_NATURAL_NAME (sym));
14520 baton->size = 0;
14521 baton->data = NULL;
14522 }
6e70227d 14523
768a979c 14524 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14525 SYMBOL_LOCATION_BATON (sym) = baton;
14526 }
4c2df51b 14527}
6502dd73 14528
9aa1f1e3
TT
14529/* Return the OBJFILE associated with the compilation unit CU. If CU
14530 came from a separate debuginfo file, then the master objfile is
14531 returned. */
ae0d2f24
UW
14532
14533struct objfile *
14534dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14535{
9291a0cd 14536 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14537
14538 /* Return the master objfile, so that we can report and look up the
14539 correct file containing this variable. */
14540 if (objfile->separate_debug_objfile_backlink)
14541 objfile = objfile->separate_debug_objfile_backlink;
14542
14543 return objfile;
14544}
14545
14546/* Return the address size given in the compilation unit header for CU. */
14547
14548CORE_ADDR
14549dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14550{
14551 if (per_cu->cu)
14552 return per_cu->cu->header.addr_size;
14553 else
14554 {
14555 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14556 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14557 struct dwarf2_per_objfile *per_objfile
14558 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 14559 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 14560 struct comp_unit_head cu_header;
9a619af0 14561
ae0d2f24
UW
14562 memset (&cu_header, 0, sizeof cu_header);
14563 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14564 return cu_header.addr_size;
14565 }
14566}
14567
9eae7c52
TT
14568/* Return the offset size given in the compilation unit header for CU. */
14569
14570int
14571dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14572{
14573 if (per_cu->cu)
14574 return per_cu->cu->header.offset_size;
14575 else
14576 {
14577 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 14578 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
14579 struct dwarf2_per_objfile *per_objfile
14580 = objfile_data (objfile, dwarf2_objfile_data_key);
14581 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14582 struct comp_unit_head cu_header;
14583
14584 memset (&cu_header, 0, sizeof cu_header);
14585 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14586 return cu_header.offset_size;
14587 }
14588}
14589
9aa1f1e3
TT
14590/* Return the text offset of the CU. The returned offset comes from
14591 this CU's objfile. If this objfile came from a separate debuginfo
14592 file, then the offset may be different from the corresponding
14593 offset in the parent objfile. */
14594
14595CORE_ADDR
14596dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14597{
bb3fa9d0 14598 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
14599
14600 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14601}
14602
348e048f
DE
14603/* Locate the .debug_info compilation unit from CU's objfile which contains
14604 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
14605
14606static struct dwarf2_per_cu_data *
c764a876 14607dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
14608 struct objfile *objfile)
14609{
14610 struct dwarf2_per_cu_data *this_cu;
14611 int low, high;
14612
ae038cb0
DJ
14613 low = 0;
14614 high = dwarf2_per_objfile->n_comp_units - 1;
14615 while (high > low)
14616 {
14617 int mid = low + (high - low) / 2;
9a619af0 14618
ae038cb0
DJ
14619 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14620 high = mid;
14621 else
14622 low = mid + 1;
14623 }
14624 gdb_assert (low == high);
14625 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14626 {
10b3939b 14627 if (low == 0)
8a3fe4f8
AC
14628 error (_("Dwarf Error: could not find partial DIE containing "
14629 "offset 0x%lx [in module %s]"),
10b3939b
DJ
14630 (long) offset, bfd_get_filename (objfile->obfd));
14631
ae038cb0
DJ
14632 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14633 return dwarf2_per_objfile->all_comp_units[low-1];
14634 }
14635 else
14636 {
14637 this_cu = dwarf2_per_objfile->all_comp_units[low];
14638 if (low == dwarf2_per_objfile->n_comp_units - 1
14639 && offset >= this_cu->offset + this_cu->length)
c764a876 14640 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
14641 gdb_assert (offset < this_cu->offset + this_cu->length);
14642 return this_cu;
14643 }
14644}
14645
10b3939b
DJ
14646/* Locate the compilation unit from OBJFILE which is located at exactly
14647 OFFSET. Raises an error on failure. */
14648
ae038cb0 14649static struct dwarf2_per_cu_data *
c764a876 14650dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
14651{
14652 struct dwarf2_per_cu_data *this_cu;
9a619af0 14653
ae038cb0
DJ
14654 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14655 if (this_cu->offset != offset)
c764a876 14656 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
14657 return this_cu;
14658}
14659
9816fde3 14660/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 14661
9816fde3
JK
14662static void
14663init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 14664{
9816fde3 14665 memset (cu, 0, sizeof (*cu));
93311388
DE
14666 cu->objfile = objfile;
14667 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
14668}
14669
14670/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14671
14672static void
14673prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14674{
14675 struct attribute *attr;
14676
14677 /* Set the language we're debugging. */
14678 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14679 if (attr)
14680 set_cu_language (DW_UNSND (attr), cu);
14681 else
14682 set_cu_language (language_minimal, cu);
93311388
DE
14683}
14684
ae038cb0
DJ
14685/* Release one cached compilation unit, CU. We unlink it from the tree
14686 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
14687 the caller is responsible for that.
14688 NOTE: DATA is a void * because this function is also used as a
14689 cleanup routine. */
ae038cb0
DJ
14690
14691static void
14692free_one_comp_unit (void *data)
14693{
14694 struct dwarf2_cu *cu = data;
14695
14696 if (cu->per_cu != NULL)
14697 cu->per_cu->cu = NULL;
14698 cu->per_cu = NULL;
14699
14700 obstack_free (&cu->comp_unit_obstack, NULL);
14701
14702 xfree (cu);
14703}
14704
72bf9492 14705/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
14706 when we're finished with it. We can't free the pointer itself, but be
14707 sure to unlink it from the cache. Also release any associated storage
14708 and perform cache maintenance.
72bf9492
DJ
14709
14710 Only used during partial symbol parsing. */
14711
14712static void
14713free_stack_comp_unit (void *data)
14714{
14715 struct dwarf2_cu *cu = data;
14716
14717 obstack_free (&cu->comp_unit_obstack, NULL);
14718 cu->partial_dies = NULL;
ae038cb0
DJ
14719
14720 if (cu->per_cu != NULL)
14721 {
14722 /* This compilation unit is on the stack in our caller, so we
14723 should not xfree it. Just unlink it. */
14724 cu->per_cu->cu = NULL;
14725 cu->per_cu = NULL;
14726
14727 /* If we had a per-cu pointer, then we may have other compilation
14728 units loaded, so age them now. */
14729 age_cached_comp_units ();
14730 }
14731}
14732
14733/* Free all cached compilation units. */
14734
14735static void
14736free_cached_comp_units (void *data)
14737{
14738 struct dwarf2_per_cu_data *per_cu, **last_chain;
14739
14740 per_cu = dwarf2_per_objfile->read_in_chain;
14741 last_chain = &dwarf2_per_objfile->read_in_chain;
14742 while (per_cu != NULL)
14743 {
14744 struct dwarf2_per_cu_data *next_cu;
14745
14746 next_cu = per_cu->cu->read_in_chain;
14747
14748 free_one_comp_unit (per_cu->cu);
14749 *last_chain = next_cu;
14750
14751 per_cu = next_cu;
14752 }
14753}
14754
14755/* Increase the age counter on each cached compilation unit, and free
14756 any that are too old. */
14757
14758static void
14759age_cached_comp_units (void)
14760{
14761 struct dwarf2_per_cu_data *per_cu, **last_chain;
14762
14763 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14764 per_cu = dwarf2_per_objfile->read_in_chain;
14765 while (per_cu != NULL)
14766 {
14767 per_cu->cu->last_used ++;
14768 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14769 dwarf2_mark (per_cu->cu);
14770 per_cu = per_cu->cu->read_in_chain;
14771 }
14772
14773 per_cu = dwarf2_per_objfile->read_in_chain;
14774 last_chain = &dwarf2_per_objfile->read_in_chain;
14775 while (per_cu != NULL)
14776 {
14777 struct dwarf2_per_cu_data *next_cu;
14778
14779 next_cu = per_cu->cu->read_in_chain;
14780
14781 if (!per_cu->cu->mark)
14782 {
14783 free_one_comp_unit (per_cu->cu);
14784 *last_chain = next_cu;
14785 }
14786 else
14787 last_chain = &per_cu->cu->read_in_chain;
14788
14789 per_cu = next_cu;
14790 }
14791}
14792
14793/* Remove a single compilation unit from the cache. */
14794
14795static void
14796free_one_cached_comp_unit (void *target_cu)
14797{
14798 struct dwarf2_per_cu_data *per_cu, **last_chain;
14799
14800 per_cu = dwarf2_per_objfile->read_in_chain;
14801 last_chain = &dwarf2_per_objfile->read_in_chain;
14802 while (per_cu != NULL)
14803 {
14804 struct dwarf2_per_cu_data *next_cu;
14805
14806 next_cu = per_cu->cu->read_in_chain;
14807
14808 if (per_cu->cu == target_cu)
14809 {
14810 free_one_comp_unit (per_cu->cu);
14811 *last_chain = next_cu;
14812 break;
14813 }
14814 else
14815 last_chain = &per_cu->cu->read_in_chain;
14816
14817 per_cu = next_cu;
14818 }
14819}
14820
fe3e1990
DJ
14821/* Release all extra memory associated with OBJFILE. */
14822
14823void
14824dwarf2_free_objfile (struct objfile *objfile)
14825{
14826 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14827
14828 if (dwarf2_per_objfile == NULL)
14829 return;
14830
14831 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14832 free_cached_comp_units (NULL);
14833
7b9f3c50
DE
14834 if (dwarf2_per_objfile->quick_file_names_table)
14835 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 14836
fe3e1990
DJ
14837 /* Everything else should be on the objfile obstack. */
14838}
14839
1c379e20
DJ
14840/* A pair of DIE offset and GDB type pointer. We store these
14841 in a hash table separate from the DIEs, and preserve them
14842 when the DIEs are flushed out of cache. */
14843
14844struct dwarf2_offset_and_type
14845{
14846 unsigned int offset;
14847 struct type *type;
14848};
14849
14850/* Hash function for a dwarf2_offset_and_type. */
14851
14852static hashval_t
14853offset_and_type_hash (const void *item)
14854{
14855 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 14856
1c379e20
DJ
14857 return ofs->offset;
14858}
14859
14860/* Equality function for a dwarf2_offset_and_type. */
14861
14862static int
14863offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14864{
14865 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14866 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 14867
1c379e20
DJ
14868 return ofs_lhs->offset == ofs_rhs->offset;
14869}
14870
14871/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
14872 table if necessary. For convenience, return TYPE.
14873
14874 The DIEs reading must have careful ordering to:
14875 * Not cause infite loops trying to read in DIEs as a prerequisite for
14876 reading current DIE.
14877 * Not trying to dereference contents of still incompletely read in types
14878 while reading in other DIEs.
14879 * Enable referencing still incompletely read in types just by a pointer to
14880 the type without accessing its fields.
14881
14882 Therefore caller should follow these rules:
14883 * Try to fetch any prerequisite types we may need to build this DIE type
14884 before building the type and calling set_die_type.
e71ec853 14885 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
14886 possible before fetching more types to complete the current type.
14887 * Make the type as complete as possible before fetching more types. */
1c379e20 14888
f792889a 14889static struct type *
1c379e20
DJ
14890set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14891{
14892 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
14893 struct objfile *objfile = cu->objfile;
14894 htab_t *type_hash_ptr;
1c379e20 14895
b4ba55a1
JB
14896 /* For Ada types, make sure that the gnat-specific data is always
14897 initialized (if not already set). There are a few types where
14898 we should not be doing so, because the type-specific area is
14899 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14900 where the type-specific area is used to store the floatformat).
14901 But this is not a problem, because the gnat-specific information
14902 is actually not needed for these types. */
14903 if (need_gnat_info (cu)
14904 && TYPE_CODE (type) != TYPE_CODE_FUNC
14905 && TYPE_CODE (type) != TYPE_CODE_FLT
14906 && !HAVE_GNAT_AUX_INFO (type))
14907 INIT_GNAT_SPECIFIC (type);
14908
673bfd45
DE
14909 if (cu->per_cu->from_debug_types)
14910 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14911 else
14912 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14913
14914 if (*type_hash_ptr == NULL)
f792889a 14915 {
673bfd45
DE
14916 *type_hash_ptr
14917 = htab_create_alloc_ex (127,
f792889a
DJ
14918 offset_and_type_hash,
14919 offset_and_type_eq,
14920 NULL,
673bfd45 14921 &objfile->objfile_obstack,
f792889a
DJ
14922 hashtab_obstack_allocate,
14923 dummy_obstack_deallocate);
f792889a 14924 }
1c379e20
DJ
14925
14926 ofs.offset = die->offset;
14927 ofs.type = type;
14928 slot = (struct dwarf2_offset_and_type **)
673bfd45 14929 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
14930 if (*slot)
14931 complaint (&symfile_complaints,
14932 _("A problem internal to GDB: DIE 0x%x has type already set"),
14933 die->offset);
673bfd45 14934 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 14935 **slot = ofs;
f792889a 14936 return type;
1c379e20
DJ
14937}
14938
673bfd45
DE
14939/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14940 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
14941
14942static struct type *
673bfd45
DE
14943get_die_type_at_offset (unsigned int offset,
14944 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
14945{
14946 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 14947 htab_t type_hash;
f792889a 14948
673bfd45
DE
14949 if (per_cu->from_debug_types)
14950 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14951 else
14952 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
14953 if (type_hash == NULL)
14954 return NULL;
1c379e20 14955
673bfd45 14956 ofs.offset = offset;
1c379e20
DJ
14957 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14958 if (slot)
14959 return slot->type;
14960 else
14961 return NULL;
14962}
14963
673bfd45
DE
14964/* Look up the type for DIE in the appropriate type_hash table,
14965 or return NULL if DIE does not have a saved type. */
14966
14967static struct type *
14968get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14969{
14970 return get_die_type_at_offset (die->offset, cu->per_cu);
14971}
14972
10b3939b
DJ
14973/* Add a dependence relationship from CU to REF_PER_CU. */
14974
14975static void
14976dwarf2_add_dependence (struct dwarf2_cu *cu,
14977 struct dwarf2_per_cu_data *ref_per_cu)
14978{
14979 void **slot;
14980
14981 if (cu->dependencies == NULL)
14982 cu->dependencies
14983 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14984 NULL, &cu->comp_unit_obstack,
14985 hashtab_obstack_allocate,
14986 dummy_obstack_deallocate);
14987
14988 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14989 if (*slot == NULL)
14990 *slot = ref_per_cu;
14991}
1c379e20 14992
f504f079
DE
14993/* Subroutine of dwarf2_mark to pass to htab_traverse.
14994 Set the mark field in every compilation unit in the
ae038cb0
DJ
14995 cache that we must keep because we are keeping CU. */
14996
10b3939b
DJ
14997static int
14998dwarf2_mark_helper (void **slot, void *data)
14999{
15000 struct dwarf2_per_cu_data *per_cu;
15001
15002 per_cu = (struct dwarf2_per_cu_data *) *slot;
15003 if (per_cu->cu->mark)
15004 return 1;
15005 per_cu->cu->mark = 1;
15006
15007 if (per_cu->cu->dependencies != NULL)
15008 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15009
15010 return 1;
15011}
15012
f504f079
DE
15013/* Set the mark field in CU and in every other compilation unit in the
15014 cache that we must keep because we are keeping CU. */
15015
ae038cb0
DJ
15016static void
15017dwarf2_mark (struct dwarf2_cu *cu)
15018{
15019 if (cu->mark)
15020 return;
15021 cu->mark = 1;
10b3939b
DJ
15022 if (cu->dependencies != NULL)
15023 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
15024}
15025
15026static void
15027dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15028{
15029 while (per_cu)
15030 {
15031 per_cu->cu->mark = 0;
15032 per_cu = per_cu->cu->read_in_chain;
15033 }
72bf9492
DJ
15034}
15035
72bf9492
DJ
15036/* Trivial hash function for partial_die_info: the hash value of a DIE
15037 is its offset in .debug_info for this objfile. */
15038
15039static hashval_t
15040partial_die_hash (const void *item)
15041{
15042 const struct partial_die_info *part_die = item;
9a619af0 15043
72bf9492
DJ
15044 return part_die->offset;
15045}
15046
15047/* Trivial comparison function for partial_die_info structures: two DIEs
15048 are equal if they have the same offset. */
15049
15050static int
15051partial_die_eq (const void *item_lhs, const void *item_rhs)
15052{
15053 const struct partial_die_info *part_die_lhs = item_lhs;
15054 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 15055
72bf9492
DJ
15056 return part_die_lhs->offset == part_die_rhs->offset;
15057}
15058
ae038cb0
DJ
15059static struct cmd_list_element *set_dwarf2_cmdlist;
15060static struct cmd_list_element *show_dwarf2_cmdlist;
15061
15062static void
15063set_dwarf2_cmd (char *args, int from_tty)
15064{
15065 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15066}
15067
15068static void
15069show_dwarf2_cmd (char *args, int from_tty)
6e70227d 15070{
ae038cb0
DJ
15071 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15072}
15073
dce234bc
PP
15074/* If section described by INFO was mmapped, munmap it now. */
15075
15076static void
15077munmap_section_buffer (struct dwarf2_section_info *info)
15078{
15079 if (info->was_mmapped)
15080 {
15081#ifdef HAVE_MMAP
15082 intptr_t begin = (intptr_t) info->buffer;
15083 intptr_t map_begin = begin & ~(pagesize - 1);
15084 size_t map_length = info->size + begin - map_begin;
9a619af0 15085
dce234bc
PP
15086 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15087#else
15088 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 15089 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
15090#endif
15091 }
15092}
15093
15094/* munmap debug sections for OBJFILE, if necessary. */
15095
15096static void
c1bd65d0 15097dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
15098{
15099 struct dwarf2_per_objfile *data = d;
9a619af0 15100
16be1145
DE
15101 /* This is sorted according to the order they're defined in to make it easier
15102 to keep in sync. */
dce234bc
PP
15103 munmap_section_buffer (&data->info);
15104 munmap_section_buffer (&data->abbrev);
15105 munmap_section_buffer (&data->line);
16be1145 15106 munmap_section_buffer (&data->loc);
dce234bc 15107 munmap_section_buffer (&data->macinfo);
16be1145 15108 munmap_section_buffer (&data->str);
dce234bc 15109 munmap_section_buffer (&data->ranges);
16be1145 15110 munmap_section_buffer (&data->types);
dce234bc
PP
15111 munmap_section_buffer (&data->frame);
15112 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
15113 munmap_section_buffer (&data->gdb_index);
15114}
15115
15116\f
ae2de4f8 15117/* The "save gdb-index" command. */
9291a0cd
TT
15118
15119/* The contents of the hash table we create when building the string
15120 table. */
15121struct strtab_entry
15122{
15123 offset_type offset;
15124 const char *str;
15125};
15126
15127/* Hash function for a strtab_entry. */
b89be57b 15128
9291a0cd
TT
15129static hashval_t
15130hash_strtab_entry (const void *e)
15131{
15132 const struct strtab_entry *entry = e;
15133 return mapped_index_string_hash (entry->str);
15134}
15135
15136/* Equality function for a strtab_entry. */
b89be57b 15137
9291a0cd
TT
15138static int
15139eq_strtab_entry (const void *a, const void *b)
15140{
15141 const struct strtab_entry *ea = a;
15142 const struct strtab_entry *eb = b;
15143 return !strcmp (ea->str, eb->str);
15144}
15145
15146/* Create a strtab_entry hash table. */
b89be57b 15147
9291a0cd
TT
15148static htab_t
15149create_strtab (void)
15150{
15151 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15152 xfree, xcalloc, xfree);
15153}
15154
15155/* Add a string to the constant pool. Return the string's offset in
15156 host order. */
b89be57b 15157
9291a0cd
TT
15158static offset_type
15159add_string (htab_t table, struct obstack *cpool, const char *str)
15160{
15161 void **slot;
15162 struct strtab_entry entry;
15163 struct strtab_entry *result;
15164
15165 entry.str = str;
15166 slot = htab_find_slot (table, &entry, INSERT);
15167 if (*slot)
15168 result = *slot;
15169 else
15170 {
15171 result = XNEW (struct strtab_entry);
15172 result->offset = obstack_object_size (cpool);
15173 result->str = str;
15174 obstack_grow_str0 (cpool, str);
15175 *slot = result;
15176 }
15177 return result->offset;
15178}
15179
15180/* An entry in the symbol table. */
15181struct symtab_index_entry
15182{
15183 /* The name of the symbol. */
15184 const char *name;
15185 /* The offset of the name in the constant pool. */
15186 offset_type index_offset;
15187 /* A sorted vector of the indices of all the CUs that hold an object
15188 of this name. */
15189 VEC (offset_type) *cu_indices;
15190};
15191
15192/* The symbol table. This is a power-of-2-sized hash table. */
15193struct mapped_symtab
15194{
15195 offset_type n_elements;
15196 offset_type size;
15197 struct symtab_index_entry **data;
15198};
15199
15200/* Hash function for a symtab_index_entry. */
b89be57b 15201
9291a0cd
TT
15202static hashval_t
15203hash_symtab_entry (const void *e)
15204{
15205 const struct symtab_index_entry *entry = e;
15206 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15207 sizeof (offset_type) * VEC_length (offset_type,
15208 entry->cu_indices),
15209 0);
15210}
15211
15212/* Equality function for a symtab_index_entry. */
b89be57b 15213
9291a0cd
TT
15214static int
15215eq_symtab_entry (const void *a, const void *b)
15216{
15217 const struct symtab_index_entry *ea = a;
15218 const struct symtab_index_entry *eb = b;
15219 int len = VEC_length (offset_type, ea->cu_indices);
15220 if (len != VEC_length (offset_type, eb->cu_indices))
15221 return 0;
15222 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15223 VEC_address (offset_type, eb->cu_indices),
15224 sizeof (offset_type) * len);
15225}
15226
15227/* Destroy a symtab_index_entry. */
b89be57b 15228
9291a0cd
TT
15229static void
15230delete_symtab_entry (void *p)
15231{
15232 struct symtab_index_entry *entry = p;
15233 VEC_free (offset_type, entry->cu_indices);
15234 xfree (entry);
15235}
15236
15237/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15238
9291a0cd 15239static htab_t
3876f04e 15240create_symbol_hash_table (void)
9291a0cd
TT
15241{
15242 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15243 delete_symtab_entry, xcalloc, xfree);
15244}
15245
15246/* Create a new mapped symtab object. */
b89be57b 15247
9291a0cd
TT
15248static struct mapped_symtab *
15249create_mapped_symtab (void)
15250{
15251 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15252 symtab->n_elements = 0;
15253 symtab->size = 1024;
15254 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15255 return symtab;
15256}
15257
15258/* Destroy a mapped_symtab. */
b89be57b 15259
9291a0cd
TT
15260static void
15261cleanup_mapped_symtab (void *p)
15262{
15263 struct mapped_symtab *symtab = p;
15264 /* The contents of the array are freed when the other hash table is
15265 destroyed. */
15266 xfree (symtab->data);
15267 xfree (symtab);
15268}
15269
15270/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15271 the slot. */
b89be57b 15272
9291a0cd
TT
15273static struct symtab_index_entry **
15274find_slot (struct mapped_symtab *symtab, const char *name)
15275{
15276 offset_type index, step, hash = mapped_index_string_hash (name);
15277
15278 index = hash & (symtab->size - 1);
15279 step = ((hash * 17) & (symtab->size - 1)) | 1;
15280
15281 for (;;)
15282 {
15283 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15284 return &symtab->data[index];
15285 index = (index + step) & (symtab->size - 1);
15286 }
15287}
15288
15289/* Expand SYMTAB's hash table. */
b89be57b 15290
9291a0cd
TT
15291static void
15292hash_expand (struct mapped_symtab *symtab)
15293{
15294 offset_type old_size = symtab->size;
15295 offset_type i;
15296 struct symtab_index_entry **old_entries = symtab->data;
15297
15298 symtab->size *= 2;
15299 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15300
15301 for (i = 0; i < old_size; ++i)
15302 {
15303 if (old_entries[i])
15304 {
15305 struct symtab_index_entry **slot = find_slot (symtab,
15306 old_entries[i]->name);
15307 *slot = old_entries[i];
15308 }
15309 }
15310
15311 xfree (old_entries);
15312}
15313
15314/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15315 is the index of the CU in which the symbol appears. */
b89be57b 15316
9291a0cd
TT
15317static void
15318add_index_entry (struct mapped_symtab *symtab, const char *name,
15319 offset_type cu_index)
15320{
15321 struct symtab_index_entry **slot;
15322
15323 ++symtab->n_elements;
15324 if (4 * symtab->n_elements / 3 >= symtab->size)
15325 hash_expand (symtab);
15326
15327 slot = find_slot (symtab, name);
15328 if (!*slot)
15329 {
15330 *slot = XNEW (struct symtab_index_entry);
15331 (*slot)->name = name;
15332 (*slot)->cu_indices = NULL;
15333 }
15334 /* Don't push an index twice. Due to how we add entries we only
15335 have to check the last one. */
15336 if (VEC_empty (offset_type, (*slot)->cu_indices)
15337 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15338 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15339}
15340
15341/* Add a vector of indices to the constant pool. */
b89be57b 15342
9291a0cd 15343static offset_type
3876f04e 15344add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15345 struct symtab_index_entry *entry)
15346{
15347 void **slot;
15348
3876f04e 15349 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15350 if (!*slot)
15351 {
15352 offset_type len = VEC_length (offset_type, entry->cu_indices);
15353 offset_type val = MAYBE_SWAP (len);
15354 offset_type iter;
15355 int i;
15356
15357 *slot = entry;
15358 entry->index_offset = obstack_object_size (cpool);
15359
15360 obstack_grow (cpool, &val, sizeof (val));
15361 for (i = 0;
15362 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15363 ++i)
15364 {
15365 val = MAYBE_SWAP (iter);
15366 obstack_grow (cpool, &val, sizeof (val));
15367 }
15368 }
15369 else
15370 {
15371 struct symtab_index_entry *old_entry = *slot;
15372 entry->index_offset = old_entry->index_offset;
15373 entry = old_entry;
15374 }
15375 return entry->index_offset;
15376}
15377
15378/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15379 constant pool entries going into the obstack CPOOL. */
b89be57b 15380
9291a0cd
TT
15381static void
15382write_hash_table (struct mapped_symtab *symtab,
15383 struct obstack *output, struct obstack *cpool)
15384{
15385 offset_type i;
3876f04e 15386 htab_t symbol_hash_table;
9291a0cd
TT
15387 htab_t str_table;
15388
3876f04e 15389 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15390 str_table = create_strtab ();
3876f04e 15391
9291a0cd
TT
15392 /* We add all the index vectors to the constant pool first, to
15393 ensure alignment is ok. */
15394 for (i = 0; i < symtab->size; ++i)
15395 {
15396 if (symtab->data[i])
3876f04e 15397 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15398 }
15399
15400 /* Now write out the hash table. */
15401 for (i = 0; i < symtab->size; ++i)
15402 {
15403 offset_type str_off, vec_off;
15404
15405 if (symtab->data[i])
15406 {
15407 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15408 vec_off = symtab->data[i]->index_offset;
15409 }
15410 else
15411 {
15412 /* While 0 is a valid constant pool index, it is not valid
15413 to have 0 for both offsets. */
15414 str_off = 0;
15415 vec_off = 0;
15416 }
15417
15418 str_off = MAYBE_SWAP (str_off);
15419 vec_off = MAYBE_SWAP (vec_off);
15420
15421 obstack_grow (output, &str_off, sizeof (str_off));
15422 obstack_grow (output, &vec_off, sizeof (vec_off));
15423 }
15424
15425 htab_delete (str_table);
3876f04e 15426 htab_delete (symbol_hash_table);
9291a0cd
TT
15427}
15428
0a5429f6
DE
15429/* Struct to map psymtab to CU index in the index file. */
15430struct psymtab_cu_index_map
15431{
15432 struct partial_symtab *psymtab;
15433 unsigned int cu_index;
15434};
15435
15436static hashval_t
15437hash_psymtab_cu_index (const void *item)
15438{
15439 const struct psymtab_cu_index_map *map = item;
15440
15441 return htab_hash_pointer (map->psymtab);
15442}
15443
15444static int
15445eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15446{
15447 const struct psymtab_cu_index_map *lhs = item_lhs;
15448 const struct psymtab_cu_index_map *rhs = item_rhs;
15449
15450 return lhs->psymtab == rhs->psymtab;
15451}
15452
15453/* Helper struct for building the address table. */
15454struct addrmap_index_data
15455{
15456 struct objfile *objfile;
15457 struct obstack *addr_obstack;
15458 htab_t cu_index_htab;
15459
15460 /* Non-zero if the previous_* fields are valid.
15461 We can't write an entry until we see the next entry (since it is only then
15462 that we know the end of the entry). */
15463 int previous_valid;
15464 /* Index of the CU in the table of all CUs in the index file. */
15465 unsigned int previous_cu_index;
0963b4bd 15466 /* Start address of the CU. */
0a5429f6
DE
15467 CORE_ADDR previous_cu_start;
15468};
15469
15470/* Write an address entry to OBSTACK. */
b89be57b 15471
9291a0cd 15472static void
0a5429f6
DE
15473add_address_entry (struct objfile *objfile, struct obstack *obstack,
15474 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15475{
0a5429f6 15476 offset_type cu_index_to_write;
9291a0cd
TT
15477 char addr[8];
15478 CORE_ADDR baseaddr;
15479
15480 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15481
0a5429f6
DE
15482 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15483 obstack_grow (obstack, addr, 8);
15484 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15485 obstack_grow (obstack, addr, 8);
15486 cu_index_to_write = MAYBE_SWAP (cu_index);
15487 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15488}
15489
15490/* Worker function for traversing an addrmap to build the address table. */
15491
15492static int
15493add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15494{
15495 struct addrmap_index_data *data = datap;
15496 struct partial_symtab *pst = obj;
15497 offset_type cu_index;
15498 void **slot;
15499
15500 if (data->previous_valid)
15501 add_address_entry (data->objfile, data->addr_obstack,
15502 data->previous_cu_start, start_addr,
15503 data->previous_cu_index);
15504
15505 data->previous_cu_start = start_addr;
15506 if (pst != NULL)
15507 {
15508 struct psymtab_cu_index_map find_map, *map;
15509 find_map.psymtab = pst;
15510 map = htab_find (data->cu_index_htab, &find_map);
15511 gdb_assert (map != NULL);
15512 data->previous_cu_index = map->cu_index;
15513 data->previous_valid = 1;
15514 }
15515 else
15516 data->previous_valid = 0;
15517
15518 return 0;
15519}
15520
15521/* Write OBJFILE's address map to OBSTACK.
15522 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15523 in the index file. */
15524
15525static void
15526write_address_map (struct objfile *objfile, struct obstack *obstack,
15527 htab_t cu_index_htab)
15528{
15529 struct addrmap_index_data addrmap_index_data;
15530
15531 /* When writing the address table, we have to cope with the fact that
15532 the addrmap iterator only provides the start of a region; we have to
15533 wait until the next invocation to get the start of the next region. */
15534
15535 addrmap_index_data.objfile = objfile;
15536 addrmap_index_data.addr_obstack = obstack;
15537 addrmap_index_data.cu_index_htab = cu_index_htab;
15538 addrmap_index_data.previous_valid = 0;
15539
15540 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15541 &addrmap_index_data);
15542
15543 /* It's highly unlikely the last entry (end address = 0xff...ff)
15544 is valid, but we should still handle it.
15545 The end address is recorded as the start of the next region, but that
15546 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15547 anyway. */
15548 if (addrmap_index_data.previous_valid)
15549 add_address_entry (objfile, obstack,
15550 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15551 addrmap_index_data.previous_cu_index);
9291a0cd
TT
15552}
15553
15554/* Add a list of partial symbols to SYMTAB. */
b89be57b 15555
9291a0cd
TT
15556static void
15557write_psymbols (struct mapped_symtab *symtab,
987d643c 15558 htab_t psyms_seen,
9291a0cd
TT
15559 struct partial_symbol **psymp,
15560 int count,
987d643c
TT
15561 offset_type cu_index,
15562 int is_static)
9291a0cd
TT
15563{
15564 for (; count-- > 0; ++psymp)
15565 {
987d643c
TT
15566 void **slot, *lookup;
15567
9291a0cd
TT
15568 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15569 error (_("Ada is not currently supported by the index"));
987d643c
TT
15570
15571 /* We only want to add a given psymbol once. However, we also
15572 want to account for whether it is global or static. So, we
15573 may add it twice, using slightly different values. */
15574 if (is_static)
15575 {
15576 uintptr_t val = 1 | (uintptr_t) *psymp;
15577
15578 lookup = (void *) val;
15579 }
15580 else
15581 lookup = *psymp;
15582
15583 /* Only add a given psymbol once. */
15584 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15585 if (!*slot)
15586 {
15587 *slot = lookup;
15588 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15589 }
9291a0cd
TT
15590 }
15591}
15592
15593/* Write the contents of an ("unfinished") obstack to FILE. Throw an
15594 exception if there is an error. */
b89be57b 15595
9291a0cd
TT
15596static void
15597write_obstack (FILE *file, struct obstack *obstack)
15598{
15599 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15600 file)
15601 != obstack_object_size (obstack))
15602 error (_("couldn't data write to file"));
15603}
15604
15605/* Unlink a file if the argument is not NULL. */
b89be57b 15606
9291a0cd
TT
15607static void
15608unlink_if_set (void *p)
15609{
15610 char **filename = p;
15611 if (*filename)
15612 unlink (*filename);
15613}
15614
1fd400ff
TT
15615/* A helper struct used when iterating over debug_types. */
15616struct signatured_type_index_data
15617{
15618 struct objfile *objfile;
15619 struct mapped_symtab *symtab;
15620 struct obstack *types_list;
987d643c 15621 htab_t psyms_seen;
1fd400ff
TT
15622 int cu_index;
15623};
15624
15625/* A helper function that writes a single signatured_type to an
15626 obstack. */
b89be57b 15627
1fd400ff
TT
15628static int
15629write_one_signatured_type (void **slot, void *d)
15630{
15631 struct signatured_type_index_data *info = d;
15632 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
15633 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15634 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
15635 gdb_byte val[8];
15636
15637 write_psymbols (info->symtab,
987d643c 15638 info->psyms_seen,
3e43a32a
MS
15639 info->objfile->global_psymbols.list
15640 + psymtab->globals_offset,
987d643c
TT
15641 psymtab->n_global_syms, info->cu_index,
15642 0);
1fd400ff 15643 write_psymbols (info->symtab,
987d643c 15644 info->psyms_seen,
3e43a32a
MS
15645 info->objfile->static_psymbols.list
15646 + psymtab->statics_offset,
987d643c
TT
15647 psymtab->n_static_syms, info->cu_index,
15648 1);
1fd400ff
TT
15649
15650 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15651 obstack_grow (info->types_list, val, 8);
15652 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15653 obstack_grow (info->types_list, val, 8);
15654 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15655 obstack_grow (info->types_list, val, 8);
15656
15657 ++info->cu_index;
15658
15659 return 1;
15660}
15661
987d643c
TT
15662/* A cleanup function for an htab_t. */
15663
15664static void
15665cleanup_htab (void *arg)
15666{
15667 htab_delete (arg);
15668}
15669
9291a0cd 15670/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 15671
9291a0cd
TT
15672static void
15673write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15674{
15675 struct cleanup *cleanup;
15676 char *filename, *cleanup_filename;
1fd400ff
TT
15677 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15678 struct obstack cu_list, types_cu_list;
9291a0cd
TT
15679 int i;
15680 FILE *out_file;
15681 struct mapped_symtab *symtab;
15682 offset_type val, size_of_contents, total_len;
15683 struct stat st;
15684 char buf[8];
987d643c 15685 htab_t psyms_seen;
0a5429f6
DE
15686 htab_t cu_index_htab;
15687 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd
TT
15688
15689 if (!objfile->psymtabs)
15690 return;
15691 if (dwarf2_per_objfile->using_index)
15692 error (_("Cannot use an index to create the index"));
15693
15694 if (stat (objfile->name, &st) < 0)
7e17e088 15695 perror_with_name (objfile->name);
9291a0cd
TT
15696
15697 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15698 INDEX_SUFFIX, (char *) NULL);
15699 cleanup = make_cleanup (xfree, filename);
15700
15701 out_file = fopen (filename, "wb");
15702 if (!out_file)
15703 error (_("Can't open `%s' for writing"), filename);
15704
15705 cleanup_filename = filename;
15706 make_cleanup (unlink_if_set, &cleanup_filename);
15707
15708 symtab = create_mapped_symtab ();
15709 make_cleanup (cleanup_mapped_symtab, symtab);
15710
15711 obstack_init (&addr_obstack);
15712 make_cleanup_obstack_free (&addr_obstack);
15713
15714 obstack_init (&cu_list);
15715 make_cleanup_obstack_free (&cu_list);
15716
1fd400ff
TT
15717 obstack_init (&types_cu_list);
15718 make_cleanup_obstack_free (&types_cu_list);
15719
987d643c
TT
15720 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15721 NULL, xcalloc, xfree);
15722 make_cleanup (cleanup_htab, psyms_seen);
15723
0a5429f6
DE
15724 /* While we're scanning CU's create a table that maps a psymtab pointer
15725 (which is what addrmap records) to its index (which is what is recorded
15726 in the index file). This will later be needed to write the address
15727 table. */
15728 cu_index_htab = htab_create_alloc (100,
15729 hash_psymtab_cu_index,
15730 eq_psymtab_cu_index,
15731 NULL, xcalloc, xfree);
15732 make_cleanup (cleanup_htab, cu_index_htab);
15733 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15734 xmalloc (sizeof (struct psymtab_cu_index_map)
15735 * dwarf2_per_objfile->n_comp_units);
15736 make_cleanup (xfree, psymtab_cu_index_map);
15737
15738 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
15739 work here. Also, the debug_types entries do not appear in
15740 all_comp_units, but only in their own hash table. */
9291a0cd
TT
15741 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15742 {
3e43a32a
MS
15743 struct dwarf2_per_cu_data *per_cu
15744 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 15745 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 15746 gdb_byte val[8];
0a5429f6
DE
15747 struct psymtab_cu_index_map *map;
15748 void **slot;
9291a0cd
TT
15749
15750 write_psymbols (symtab,
987d643c 15751 psyms_seen,
9291a0cd 15752 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
15753 psymtab->n_global_syms, i,
15754 0);
9291a0cd 15755 write_psymbols (symtab,
987d643c 15756 psyms_seen,
9291a0cd 15757 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
15758 psymtab->n_static_syms, i,
15759 1);
9291a0cd 15760
0a5429f6
DE
15761 map = &psymtab_cu_index_map[i];
15762 map->psymtab = psymtab;
15763 map->cu_index = i;
15764 slot = htab_find_slot (cu_index_htab, map, INSERT);
15765 gdb_assert (slot != NULL);
15766 gdb_assert (*slot == NULL);
15767 *slot = map;
9291a0cd 15768
e254ef6a 15769 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 15770 obstack_grow (&cu_list, val, 8);
e254ef6a 15771 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
15772 obstack_grow (&cu_list, val, 8);
15773 }
15774
0a5429f6
DE
15775 /* Dump the address map. */
15776 write_address_map (objfile, &addr_obstack, cu_index_htab);
15777
1fd400ff
TT
15778 /* Write out the .debug_type entries, if any. */
15779 if (dwarf2_per_objfile->signatured_types)
15780 {
15781 struct signatured_type_index_data sig_data;
15782
15783 sig_data.objfile = objfile;
15784 sig_data.symtab = symtab;
15785 sig_data.types_list = &types_cu_list;
987d643c 15786 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
15787 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15788 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15789 write_one_signatured_type, &sig_data);
15790 }
15791
9291a0cd
TT
15792 obstack_init (&constant_pool);
15793 make_cleanup_obstack_free (&constant_pool);
15794 obstack_init (&symtab_obstack);
15795 make_cleanup_obstack_free (&symtab_obstack);
15796 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15797
15798 obstack_init (&contents);
15799 make_cleanup_obstack_free (&contents);
1fd400ff 15800 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
15801 total_len = size_of_contents;
15802
15803 /* The version number. */
831adc1f 15804 val = MAYBE_SWAP (4);
9291a0cd
TT
15805 obstack_grow (&contents, &val, sizeof (val));
15806
15807 /* The offset of the CU list from the start of the file. */
15808 val = MAYBE_SWAP (total_len);
15809 obstack_grow (&contents, &val, sizeof (val));
15810 total_len += obstack_object_size (&cu_list);
15811
1fd400ff
TT
15812 /* The offset of the types CU list from the start of the file. */
15813 val = MAYBE_SWAP (total_len);
15814 obstack_grow (&contents, &val, sizeof (val));
15815 total_len += obstack_object_size (&types_cu_list);
15816
9291a0cd
TT
15817 /* The offset of the address table from the start of the file. */
15818 val = MAYBE_SWAP (total_len);
15819 obstack_grow (&contents, &val, sizeof (val));
15820 total_len += obstack_object_size (&addr_obstack);
15821
15822 /* The offset of the symbol table from the start of the file. */
15823 val = MAYBE_SWAP (total_len);
15824 obstack_grow (&contents, &val, sizeof (val));
15825 total_len += obstack_object_size (&symtab_obstack);
15826
15827 /* The offset of the constant pool from the start of the file. */
15828 val = MAYBE_SWAP (total_len);
15829 obstack_grow (&contents, &val, sizeof (val));
15830 total_len += obstack_object_size (&constant_pool);
15831
15832 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15833
15834 write_obstack (out_file, &contents);
15835 write_obstack (out_file, &cu_list);
1fd400ff 15836 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
15837 write_obstack (out_file, &addr_obstack);
15838 write_obstack (out_file, &symtab_obstack);
15839 write_obstack (out_file, &constant_pool);
15840
15841 fclose (out_file);
15842
15843 /* We want to keep the file, so we set cleanup_filename to NULL
15844 here. See unlink_if_set. */
15845 cleanup_filename = NULL;
15846
15847 do_cleanups (cleanup);
15848}
15849
15850/* The mapped index file format is designed to be directly mmap()able
15851 on any architecture. In most cases, a datum is represented using a
15852 little-endian 32-bit integer value, called an offset_type. Big
15853 endian machines must byte-swap the values before using them.
15854 Exceptions to this rule are noted. The data is laid out such that
15855 alignment is always respected.
15856
15857 A mapped index consists of several sections.
15858
15859 1. The file header. This is a sequence of values, of offset_type
15860 unless otherwise noted:
987d643c 15861
831adc1f 15862 [0] The version number, currently 4. Versions 1, 2 and 3 are
987d643c 15863 obsolete.
9291a0cd 15864 [1] The offset, from the start of the file, of the CU list.
987d643c
TT
15865 [2] The offset, from the start of the file, of the types CU list.
15866 Note that this section can be empty, in which case this offset will
15867 be equal to the next offset.
15868 [3] The offset, from the start of the file, of the address section.
15869 [4] The offset, from the start of the file, of the symbol table.
15870 [5] The offset, from the start of the file, of the constant pool.
9291a0cd
TT
15871
15872 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
15873 little-endian values, sorted by the CU offset. The first element
15874 in each pair is the offset of a CU in the .debug_info section. The
15875 second element in each pair is the length of that CU. References
15876 to a CU elsewhere in the map are done using a CU index, which is
15877 just the 0-based index into this table. Note that if there are
15878 type CUs, then conceptually CUs and type CUs form a single list for
15879 the purposes of CU indices.
15880
987d643c
TT
15881 3. The types CU list. This is a sequence of triplets of 64-bit
15882 little-endian values. In a triplet, the first value is the CU
15883 offset, the second value is the type offset in the CU, and the
15884 third value is the type signature. The types CU list is not
15885 sorted.
9291a0cd 15886
987d643c 15887 4. The address section. The address section consists of a sequence
9291a0cd
TT
15888 of address entries. Each address entry has three elements.
15889 [0] The low address. This is a 64-bit little-endian value.
15890 [1] The high address. This is a 64-bit little-endian value.
148c11bf 15891 Like DW_AT_high_pc, the value is one byte beyond the end.
9291a0cd
TT
15892 [2] The CU index. This is an offset_type value.
15893
987d643c 15894 5. The symbol table. This is a hash table. The size of the hash
9291a0cd
TT
15895 table is always a power of 2. The initial hash and the step are
15896 currently defined by the `find_slot' function.
15897
15898 Each slot in the hash table consists of a pair of offset_type
15899 values. The first value is the offset of the symbol's name in the
15900 constant pool. The second value is the offset of the CU vector in
15901 the constant pool.
15902
15903 If both values are 0, then this slot in the hash table is empty.
15904 This is ok because while 0 is a valid constant pool index, it
15905 cannot be a valid index for both a string and a CU vector.
15906
15907 A string in the constant pool is stored as a \0-terminated string,
15908 as you'd expect.
15909
15910 A CU vector in the constant pool is a sequence of offset_type
15911 values. The first value is the number of CU indices in the vector.
15912 Each subsequent value is the index of a CU in the CU list. This
15913 element in the hash table is used to indicate which CUs define the
15914 symbol.
15915
987d643c 15916 6. The constant pool. This is simply a bunch of bytes. It is
9291a0cd
TT
15917 organized so that alignment is correct: CU vectors are stored
15918 first, followed by strings. */
11570e71 15919
9291a0cd
TT
15920static void
15921save_gdb_index_command (char *arg, int from_tty)
15922{
15923 struct objfile *objfile;
15924
15925 if (!arg || !*arg)
96d19272 15926 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
15927
15928 ALL_OBJFILES (objfile)
15929 {
15930 struct stat st;
15931
15932 /* If the objfile does not correspond to an actual file, skip it. */
15933 if (stat (objfile->name, &st) < 0)
15934 continue;
15935
15936 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15937 if (dwarf2_per_objfile)
15938 {
15939 volatile struct gdb_exception except;
15940
15941 TRY_CATCH (except, RETURN_MASK_ERROR)
15942 {
15943 write_psymtabs_to_index (objfile, arg);
15944 }
15945 if (except.reason < 0)
15946 exception_fprintf (gdb_stderr, except,
15947 _("Error while writing index for `%s': "),
15948 objfile->name);
15949 }
15950 }
dce234bc
PP
15951}
15952
9291a0cd
TT
15953\f
15954
9eae7c52
TT
15955int dwarf2_always_disassemble;
15956
15957static void
15958show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15959 struct cmd_list_element *c, const char *value)
15960{
3e43a32a
MS
15961 fprintf_filtered (file,
15962 _("Whether to always disassemble "
15963 "DWARF expressions is %s.\n"),
9eae7c52
TT
15964 value);
15965}
15966
6502dd73
DJ
15967void _initialize_dwarf2_read (void);
15968
15969void
15970_initialize_dwarf2_read (void)
15971{
96d19272
JK
15972 struct cmd_list_element *c;
15973
dce234bc 15974 dwarf2_objfile_data_key
c1bd65d0 15975 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 15976
1bedd215
AC
15977 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15978Set DWARF 2 specific variables.\n\
15979Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15980 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15981 0/*allow-unknown*/, &maintenance_set_cmdlist);
15982
1bedd215
AC
15983 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15984Show DWARF 2 specific variables\n\
15985Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
15986 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15987 0/*allow-unknown*/, &maintenance_show_cmdlist);
15988
15989 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
15990 &dwarf2_max_cache_age, _("\
15991Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15992Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15993A higher limit means that cached compilation units will be stored\n\
15994in memory longer, and more total memory will be used. Zero disables\n\
15995caching, which can slow down startup."),
2c5b56ce 15996 NULL,
920d2a44 15997 show_dwarf2_max_cache_age,
2c5b56ce 15998 &set_dwarf2_cmdlist,
ae038cb0 15999 &show_dwarf2_cmdlist);
d97bc12b 16000
9eae7c52
TT
16001 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16002 &dwarf2_always_disassemble, _("\
16003Set whether `info address' always disassembles DWARF expressions."), _("\
16004Show whether `info address' always disassembles DWARF expressions."), _("\
16005When enabled, DWARF expressions are always printed in an assembly-like\n\
16006syntax. When disabled, expressions will be printed in a more\n\
16007conversational style, when possible."),
16008 NULL,
16009 show_dwarf2_always_disassemble,
16010 &set_dwarf2_cmdlist,
16011 &show_dwarf2_cmdlist);
16012
d97bc12b
DE
16013 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16014Set debugging of the dwarf2 DIE reader."), _("\
16015Show debugging of the dwarf2 DIE reader."), _("\
16016When enabled (non-zero), DIEs are dumped after they are read in.\n\
16017The value is the maximum depth to print."),
16018 NULL,
16019 NULL,
16020 &setdebuglist, &showdebuglist);
9291a0cd 16021
96d19272 16022 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 16023 _("\
fc1a9d6e 16024Save a gdb-index file.\n\
11570e71 16025Usage: save gdb-index DIRECTORY"),
96d19272
JK
16026 &save_cmdlist);
16027 set_cmd_completer (c, filename_completer);
6502dd73 16028}
This page took 2.292703 seconds and 4 git commands to generate.