Remove language param from name_matcher in struct quick_symbol_functions
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
c906108c
SS
33#include "symtab.h"
34#include "gdbtypes.h"
c906108c 35#include "objfiles.h"
fa8f86ff 36#include "dwarf2.h"
c906108c
SS
37#include "buildsym.h"
38#include "demangle.h"
50f182aa 39#include "gdb-demangle.h"
c906108c 40#include "expression.h"
d5166ae1 41#include "filenames.h" /* for DOSish file names */
2e276125 42#include "macrotab.h"
c906108c
SS
43#include "language.h"
44#include "complaints.h"
357e46e7 45#include "bcache.h"
4c2df51b
DJ
46#include "dwarf2expr.h"
47#include "dwarf2loc.h"
9219021c 48#include "cp-support.h"
72bf9492 49#include "hashtab.h"
ae038cb0
DJ
50#include "command.h"
51#include "gdbcmd.h"
edb3359d 52#include "block.h"
ff013f42 53#include "addrmap.h"
94af9270
KS
54#include "typeprint.h"
55#include "jv-lang.h"
ccefe4c4 56#include "psympriv.h"
9291a0cd
TT
57#include "exceptions.h"
58#include "gdb_stat.h"
96d19272 59#include "completer.h"
34eaf542 60#include "vec.h"
98bfdba5
PA
61#include "c-lang.h"
62#include "valprint.h"
60d5a603 63#include <ctype.h>
4c2df51b 64
c906108c
SS
65#include <fcntl.h>
66#include "gdb_string.h"
4bdf3d34 67#include "gdb_assert.h"
c906108c 68#include <sys/types.h>
233a11ab
CS
69#ifdef HAVE_ZLIB_H
70#include <zlib.h>
71#endif
dce234bc
PP
72#ifdef HAVE_MMAP
73#include <sys/mman.h>
85d9bd0e
TT
74#ifndef MAP_FAILED
75#define MAP_FAILED ((void *) -1)
76#endif
dce234bc 77#endif
d8151005 78
34eaf542
TT
79typedef struct symbol *symbolp;
80DEF_VEC_P (symbolp);
81
d97bc12b
DE
82/* When non-zero, dump DIEs after they are read in. */
83static int dwarf2_die_debug = 0;
84
900e11f9
JK
85/* When non-zero, cross-check physname against demangler. */
86static int check_physname = 0;
87
dce234bc
PP
88static int pagesize;
89
df8a16a1
DJ
90/* When set, the file that we're processing is known to have debugging
91 info for C++ namespaces. GCC 3.3.x did not produce this information,
92 but later versions do. */
93
94static int processing_has_namespace_info;
95
6502dd73
DJ
96static const struct objfile_data *dwarf2_objfile_data_key;
97
dce234bc
PP
98struct dwarf2_section_info
99{
100 asection *asection;
101 gdb_byte *buffer;
102 bfd_size_type size;
b315ab21
TG
103 /* Not NULL if the section was actually mmapped. */
104 void *map_addr;
105 /* Page aligned size of mmapped area. */
106 bfd_size_type map_len;
be391dca
TT
107 /* True if we have tried to read this section. */
108 int readin;
dce234bc
PP
109};
110
8b70b953
TT
111typedef struct dwarf2_section_info dwarf2_section_info_def;
112DEF_VEC_O (dwarf2_section_info_def);
113
9291a0cd
TT
114/* All offsets in the index are of this type. It must be
115 architecture-independent. */
116typedef uint32_t offset_type;
117
118DEF_VEC_I (offset_type);
119
120/* A description of the mapped index. The file format is described in
121 a comment by the code that writes the index. */
122struct mapped_index
123{
559a7a62
JK
124 /* Index data format version. */
125 int version;
126
9291a0cd
TT
127 /* The total length of the buffer. */
128 off_t total_size;
b11b1f88 129
9291a0cd
TT
130 /* A pointer to the address table data. */
131 const gdb_byte *address_table;
b11b1f88 132
9291a0cd
TT
133 /* Size of the address table data in bytes. */
134 offset_type address_table_size;
b11b1f88 135
3876f04e
DE
136 /* The symbol table, implemented as a hash table. */
137 const offset_type *symbol_table;
b11b1f88 138
9291a0cd 139 /* Size in slots, each slot is 2 offset_types. */
3876f04e 140 offset_type symbol_table_slots;
b11b1f88 141
9291a0cd
TT
142 /* A pointer to the constant pool. */
143 const char *constant_pool;
144};
145
9cdd5dbd
DE
146/* Collection of data recorded per objfile.
147 This hangs off of dwarf2_objfile_data_key. */
148
6502dd73
DJ
149struct dwarf2_per_objfile
150{
dce234bc
PP
151 struct dwarf2_section_info info;
152 struct dwarf2_section_info abbrev;
153 struct dwarf2_section_info line;
dce234bc
PP
154 struct dwarf2_section_info loc;
155 struct dwarf2_section_info macinfo;
cf2c3c16 156 struct dwarf2_section_info macro;
dce234bc
PP
157 struct dwarf2_section_info str;
158 struct dwarf2_section_info ranges;
159 struct dwarf2_section_info frame;
160 struct dwarf2_section_info eh_frame;
9291a0cd 161 struct dwarf2_section_info gdb_index;
ae038cb0 162
8b70b953
TT
163 VEC (dwarf2_section_info_def) *types;
164
be391dca
TT
165 /* Back link. */
166 struct objfile *objfile;
167
d467dd73 168 /* Table of all the compilation units. This is used to locate
10b3939b 169 the target compilation unit of a particular reference. */
ae038cb0
DJ
170 struct dwarf2_per_cu_data **all_comp_units;
171
172 /* The number of compilation units in ALL_COMP_UNITS. */
173 int n_comp_units;
174
1fd400ff 175 /* The number of .debug_types-related CUs. */
d467dd73 176 int n_type_units;
1fd400ff 177
d467dd73
DE
178 /* The .debug_types-related CUs (TUs). */
179 struct dwarf2_per_cu_data **all_type_units;
1fd400ff 180
ae038cb0
DJ
181 /* A chain of compilation units that are currently read in, so that
182 they can be freed later. */
183 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 184
348e048f
DE
185 /* A table mapping .debug_types signatures to its signatured_type entry.
186 This is NULL if the .debug_types section hasn't been read in yet. */
187 htab_t signatured_types;
188
72dca2f5
FR
189 /* A flag indicating wether this objfile has a section loaded at a
190 VMA of 0. */
191 int has_section_at_zero;
9291a0cd 192
ae2de4f8
DE
193 /* True if we are using the mapped index,
194 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
195 unsigned char using_index;
196
ae2de4f8 197 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 198 struct mapped_index *index_table;
98bfdba5 199
7b9f3c50
DE
200 /* When using index_table, this keeps track of all quick_file_names entries.
201 TUs can share line table entries with CUs or other TUs, and there can be
202 a lot more TUs than unique line tables, so we maintain a separate table
203 of all line table entries to support the sharing. */
204 htab_t quick_file_names_table;
205
98bfdba5
PA
206 /* Set during partial symbol reading, to prevent queueing of full
207 symbols. */
208 int reading_partial_symbols;
673bfd45
DE
209
210 /* Table mapping type .debug_info DIE offsets to types.
211 This is NULL if not allocated yet.
212 It (currently) makes sense to allocate debug_types_type_hash lazily.
213 To keep things simple we allocate both lazily. */
214 htab_t debug_info_type_hash;
215
216 /* Table mapping type .debug_types DIE offsets to types.
217 This is NULL if not allocated yet. */
218 htab_t debug_types_type_hash;
6502dd73
DJ
219};
220
221static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 222
251d32d9 223/* Default names of the debugging sections. */
c906108c 224
233a11ab
CS
225/* Note that if the debugging section has been compressed, it might
226 have a name like .zdebug_info. */
227
9cdd5dbd
DE
228static const struct dwarf2_debug_sections dwarf2_elf_names =
229{
251d32d9
TG
230 { ".debug_info", ".zdebug_info" },
231 { ".debug_abbrev", ".zdebug_abbrev" },
232 { ".debug_line", ".zdebug_line" },
233 { ".debug_loc", ".zdebug_loc" },
234 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 235 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
236 { ".debug_str", ".zdebug_str" },
237 { ".debug_ranges", ".zdebug_ranges" },
238 { ".debug_types", ".zdebug_types" },
239 { ".debug_frame", ".zdebug_frame" },
240 { ".eh_frame", NULL },
24d3216f
TT
241 { ".gdb_index", ".zgdb_index" },
242 23
251d32d9 243};
c906108c
SS
244
245/* local data types */
246
0963b4bd 247/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
248#ifndef ABBREV_HASH_SIZE
249#define ABBREV_HASH_SIZE 121
250#endif
251
107d2387
AC
252/* The data in a compilation unit header, after target2host
253 translation, looks like this. */
c906108c 254struct comp_unit_head
a738430d 255{
c764a876 256 unsigned int length;
a738430d 257 short version;
a738430d
MK
258 unsigned char addr_size;
259 unsigned char signed_addr_p;
9cbfa09e 260 unsigned int abbrev_offset;
57349743 261
a738430d
MK
262 /* Size of file offsets; either 4 or 8. */
263 unsigned int offset_size;
57349743 264
a738430d
MK
265 /* Size of the length field; either 4 or 12. */
266 unsigned int initial_length_size;
57349743 267
a738430d
MK
268 /* Offset to the first byte of this compilation unit header in the
269 .debug_info section, for resolving relative reference dies. */
270 unsigned int offset;
57349743 271
d00adf39
DE
272 /* Offset to first die in this cu from the start of the cu.
273 This will be the first byte following the compilation unit header. */
274 unsigned int first_die_offset;
a738430d 275};
c906108c 276
3da10d80
KS
277/* Type used for delaying computation of method physnames.
278 See comments for compute_delayed_physnames. */
279struct delayed_method_info
280{
281 /* The type to which the method is attached, i.e., its parent class. */
282 struct type *type;
283
284 /* The index of the method in the type's function fieldlists. */
285 int fnfield_index;
286
287 /* The index of the method in the fieldlist. */
288 int index;
289
290 /* The name of the DIE. */
291 const char *name;
292
293 /* The DIE associated with this method. */
294 struct die_info *die;
295};
296
297typedef struct delayed_method_info delayed_method_info;
298DEF_VEC_O (delayed_method_info);
299
e7c27a73
DJ
300/* Internal state when decoding a particular compilation unit. */
301struct dwarf2_cu
302{
303 /* The objfile containing this compilation unit. */
304 struct objfile *objfile;
305
d00adf39 306 /* The header of the compilation unit. */
e7c27a73 307 struct comp_unit_head header;
e142c38c 308
d00adf39
DE
309 /* Base address of this compilation unit. */
310 CORE_ADDR base_address;
311
312 /* Non-zero if base_address has been set. */
313 int base_known;
314
e142c38c
DJ
315 /* The language we are debugging. */
316 enum language language;
317 const struct language_defn *language_defn;
318
b0f35d58
DL
319 const char *producer;
320
e142c38c
DJ
321 /* The generic symbol table building routines have separate lists for
322 file scope symbols and all all other scopes (local scopes). So
323 we need to select the right one to pass to add_symbol_to_list().
324 We do it by keeping a pointer to the correct list in list_in_scope.
325
326 FIXME: The original dwarf code just treated the file scope as the
327 first local scope, and all other local scopes as nested local
328 scopes, and worked fine. Check to see if we really need to
329 distinguish these in buildsym.c. */
330 struct pending **list_in_scope;
331
f3dd6933
DJ
332 /* DWARF abbreviation table associated with this compilation unit. */
333 struct abbrev_info **dwarf2_abbrevs;
334
335 /* Storage for the abbrev table. */
336 struct obstack abbrev_obstack;
72bf9492
DJ
337
338 /* Hash table holding all the loaded partial DIEs. */
339 htab_t partial_dies;
340
341 /* Storage for things with the same lifetime as this read-in compilation
342 unit, including partial DIEs. */
343 struct obstack comp_unit_obstack;
344
ae038cb0
DJ
345 /* When multiple dwarf2_cu structures are living in memory, this field
346 chains them all together, so that they can be released efficiently.
347 We will probably also want a generation counter so that most-recently-used
348 compilation units are cached... */
349 struct dwarf2_per_cu_data *read_in_chain;
350
351 /* Backchain to our per_cu entry if the tree has been built. */
352 struct dwarf2_per_cu_data *per_cu;
353
354 /* How many compilation units ago was this CU last referenced? */
355 int last_used;
356
10b3939b 357 /* A hash table of die offsets for following references. */
51545339 358 htab_t die_hash;
10b3939b
DJ
359
360 /* Full DIEs if read in. */
361 struct die_info *dies;
362
363 /* A set of pointers to dwarf2_per_cu_data objects for compilation
364 units referenced by this one. Only set during full symbol processing;
365 partial symbol tables do not have dependencies. */
366 htab_t dependencies;
367
cb1df416
DJ
368 /* Header data from the line table, during full symbol processing. */
369 struct line_header *line_header;
370
3da10d80
KS
371 /* A list of methods which need to have physnames computed
372 after all type information has been read. */
373 VEC (delayed_method_info) *method_list;
374
96408a79
SA
375 /* To be copied to symtab->call_site_htab. */
376 htab_t call_site_htab;
377
ae038cb0
DJ
378 /* Mark used when releasing cached dies. */
379 unsigned int mark : 1;
380
381 /* This flag will be set if this compilation unit might include
382 inter-compilation-unit references. */
383 unsigned int has_form_ref_addr : 1;
384
72bf9492
DJ
385 /* This flag will be set if this compilation unit includes any
386 DW_TAG_namespace DIEs. If we know that there are explicit
387 DIEs for namespaces, we don't need to try to infer them
388 from mangled names. */
389 unsigned int has_namespace_info : 1;
8be455d7
JK
390
391 /* This CU references .debug_loc. See the symtab->locations_valid field.
392 This test is imperfect as there may exist optimized debug code not using
393 any location list and still facing inlining issues if handled as
394 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 395 unsigned int has_loclist : 1;
e7c27a73
DJ
396};
397
10b3939b
DJ
398/* Persistent data held for a compilation unit, even when not
399 processing it. We put a pointer to this structure in the
28dee7f5 400 read_symtab_private field of the psymtab. */
10b3939b 401
ae038cb0
DJ
402struct dwarf2_per_cu_data
403{
348e048f 404 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 405 bytes should suffice to store the length of any compilation unit
45452591
DE
406 - if it doesn't, GDB will fall over anyway.
407 NOTE: Unlike comp_unit_head.length, this length includes
408 initial_length_size. */
c764a876 409 unsigned int offset;
348e048f 410 unsigned int length : 29;
ae038cb0
DJ
411
412 /* Flag indicating this compilation unit will be read in before
413 any of the current compilation units are processed. */
c764a876 414 unsigned int queued : 1;
ae038cb0 415
5afb4e99
DJ
416 /* This flag will be set if we need to load absolutely all DIEs
417 for this compilation unit, instead of just the ones we think
418 are interesting. It gets set if we look for a DIE in the
419 hash table and don't find it. */
420 unsigned int load_all_dies : 1;
421
8b70b953
TT
422 /* Non-null if this CU is from .debug_types; in which case it points
423 to the section. Otherwise it's from .debug_info. */
b0df02fd 424 struct dwarf2_section_info *debug_types_section;
348e048f 425
17ea53c3
JK
426 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
427 of the CU cache it gets reset to NULL again. */
ae038cb0 428 struct dwarf2_cu *cu;
1c379e20 429
9cdd5dbd
DE
430 /* The corresponding objfile.
431 Normally we can get the objfile from dwarf2_per_objfile.
432 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
433 struct objfile *objfile;
434
435 /* When using partial symbol tables, the 'psymtab' field is active.
436 Otherwise the 'quick' field is active. */
437 union
438 {
439 /* The partial symbol table associated with this compilation unit,
440 or NULL for partial units (which do not have an associated
441 symtab). */
442 struct partial_symtab *psymtab;
443
444 /* Data needed by the "quick" functions. */
445 struct dwarf2_per_cu_quick_data *quick;
446 } v;
ae038cb0
DJ
447};
448
348e048f
DE
449/* Entry in the signatured_types hash table. */
450
451struct signatured_type
452{
453 ULONGEST signature;
454
348e048f
DE
455 /* Offset in .debug_types of the type defined by this TU. */
456 unsigned int type_offset;
457
458 /* The CU(/TU) of this type. */
459 struct dwarf2_per_cu_data per_cu;
460};
461
0963b4bd
MS
462/* Struct used to pass misc. parameters to read_die_and_children, et
463 al. which are used for both .debug_info and .debug_types dies.
464 All parameters here are unchanging for the life of the call. This
465 struct exists to abstract away the constant parameters of die
466 reading. */
93311388
DE
467
468struct die_reader_specs
469{
470 /* The bfd of this objfile. */
471 bfd* abfd;
472
473 /* The CU of the DIE we are parsing. */
474 struct dwarf2_cu *cu;
475
476 /* Pointer to start of section buffer.
477 This is either the start of .debug_info or .debug_types. */
478 const gdb_byte *buffer;
479};
480
debd256d
JB
481/* The line number information for a compilation unit (found in the
482 .debug_line section) begins with a "statement program header",
483 which contains the following information. */
484struct line_header
485{
486 unsigned int total_length;
487 unsigned short version;
488 unsigned int header_length;
489 unsigned char minimum_instruction_length;
2dc7f7b3 490 unsigned char maximum_ops_per_instruction;
debd256d
JB
491 unsigned char default_is_stmt;
492 int line_base;
493 unsigned char line_range;
494 unsigned char opcode_base;
495
496 /* standard_opcode_lengths[i] is the number of operands for the
497 standard opcode whose value is i. This means that
498 standard_opcode_lengths[0] is unused, and the last meaningful
499 element is standard_opcode_lengths[opcode_base - 1]. */
500 unsigned char *standard_opcode_lengths;
501
502 /* The include_directories table. NOTE! These strings are not
503 allocated with xmalloc; instead, they are pointers into
504 debug_line_buffer. If you try to free them, `free' will get
505 indigestion. */
506 unsigned int num_include_dirs, include_dirs_size;
507 char **include_dirs;
508
509 /* The file_names table. NOTE! These strings are not allocated
510 with xmalloc; instead, they are pointers into debug_line_buffer.
511 Don't try to free them directly. */
512 unsigned int num_file_names, file_names_size;
513 struct file_entry
c906108c 514 {
debd256d
JB
515 char *name;
516 unsigned int dir_index;
517 unsigned int mod_time;
518 unsigned int length;
aaa75496 519 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 520 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
521 } *file_names;
522
523 /* The start and end of the statement program following this
6502dd73 524 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 525 gdb_byte *statement_program_start, *statement_program_end;
debd256d 526};
c906108c
SS
527
528/* When we construct a partial symbol table entry we only
0963b4bd 529 need this much information. */
c906108c
SS
530struct partial_die_info
531 {
72bf9492 532 /* Offset of this DIE. */
c906108c 533 unsigned int offset;
72bf9492
DJ
534
535 /* DWARF-2 tag for this DIE. */
536 ENUM_BITFIELD(dwarf_tag) tag : 16;
537
72bf9492
DJ
538 /* Assorted flags describing the data found in this DIE. */
539 unsigned int has_children : 1;
540 unsigned int is_external : 1;
541 unsigned int is_declaration : 1;
542 unsigned int has_type : 1;
543 unsigned int has_specification : 1;
544 unsigned int has_pc_info : 1;
545
546 /* Flag set if the SCOPE field of this structure has been
547 computed. */
548 unsigned int scope_set : 1;
549
fa4028e9
JB
550 /* Flag set if the DIE has a byte_size attribute. */
551 unsigned int has_byte_size : 1;
552
98bfdba5
PA
553 /* Flag set if any of the DIE's children are template arguments. */
554 unsigned int has_template_arguments : 1;
555
abc72ce4
DE
556 /* Flag set if fixup_partial_die has been called on this die. */
557 unsigned int fixup_called : 1;
558
72bf9492 559 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 560 sometimes a default name for unnamed DIEs. */
c906108c 561 char *name;
72bf9492 562
abc72ce4
DE
563 /* The linkage name, if present. */
564 const char *linkage_name;
565
72bf9492
DJ
566 /* The scope to prepend to our children. This is generally
567 allocated on the comp_unit_obstack, so will disappear
568 when this compilation unit leaves the cache. */
569 char *scope;
570
571 /* The location description associated with this DIE, if any. */
572 struct dwarf_block *locdesc;
573
574 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
575 CORE_ADDR lowpc;
576 CORE_ADDR highpc;
72bf9492 577
93311388 578 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 579 DW_AT_sibling, if any. */
abc72ce4
DE
580 /* NOTE: This member isn't strictly necessary, read_partial_die could
581 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 582 gdb_byte *sibling;
72bf9492
DJ
583
584 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
585 DW_AT_specification (or DW_AT_abstract_origin or
586 DW_AT_extension). */
587 unsigned int spec_offset;
588
589 /* Pointers to this DIE's parent, first child, and next sibling,
590 if any. */
591 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
592 };
593
0963b4bd 594/* This data structure holds the information of an abbrev. */
c906108c
SS
595struct abbrev_info
596 {
597 unsigned int number; /* number identifying abbrev */
598 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
599 unsigned short has_children; /* boolean */
600 unsigned short num_attrs; /* number of attributes */
c906108c
SS
601 struct attr_abbrev *attrs; /* an array of attribute descriptions */
602 struct abbrev_info *next; /* next in chain */
603 };
604
605struct attr_abbrev
606 {
9d25dd43
DE
607 ENUM_BITFIELD(dwarf_attribute) name : 16;
608 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
609 };
610
0963b4bd 611/* Attributes have a name and a value. */
b60c80d6
DJ
612struct attribute
613 {
9d25dd43 614 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
615 ENUM_BITFIELD(dwarf_form) form : 15;
616
617 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
618 field should be in u.str (existing only for DW_STRING) but it is kept
619 here for better struct attribute alignment. */
620 unsigned int string_is_canonical : 1;
621
b60c80d6
DJ
622 union
623 {
624 char *str;
625 struct dwarf_block *blk;
43bbcdc2
PH
626 ULONGEST unsnd;
627 LONGEST snd;
b60c80d6 628 CORE_ADDR addr;
348e048f 629 struct signatured_type *signatured_type;
b60c80d6
DJ
630 }
631 u;
632 };
633
0963b4bd 634/* This data structure holds a complete die structure. */
c906108c
SS
635struct die_info
636 {
76815b17
DE
637 /* DWARF-2 tag for this DIE. */
638 ENUM_BITFIELD(dwarf_tag) tag : 16;
639
640 /* Number of attributes */
98bfdba5
PA
641 unsigned char num_attrs;
642
643 /* True if we're presently building the full type name for the
644 type derived from this DIE. */
645 unsigned char building_fullname : 1;
76815b17
DE
646
647 /* Abbrev number */
648 unsigned int abbrev;
649
93311388 650 /* Offset in .debug_info or .debug_types section. */
76815b17 651 unsigned int offset;
78ba4af6
JB
652
653 /* The dies in a compilation unit form an n-ary tree. PARENT
654 points to this die's parent; CHILD points to the first child of
655 this node; and all the children of a given node are chained
4950bc1c 656 together via their SIBLING fields. */
639d11d3
DC
657 struct die_info *child; /* Its first child, if any. */
658 struct die_info *sibling; /* Its next sibling, if any. */
659 struct die_info *parent; /* Its parent, if any. */
c906108c 660
b60c80d6
DJ
661 /* An array of attributes, with NUM_ATTRS elements. There may be
662 zero, but it's not common and zero-sized arrays are not
663 sufficiently portable C. */
664 struct attribute attrs[1];
c906108c
SS
665 };
666
0963b4bd 667/* Get at parts of an attribute structure. */
c906108c
SS
668
669#define DW_STRING(attr) ((attr)->u.str)
8285870a 670#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
671#define DW_UNSND(attr) ((attr)->u.unsnd)
672#define DW_BLOCK(attr) ((attr)->u.blk)
673#define DW_SND(attr) ((attr)->u.snd)
674#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 675#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 676
0963b4bd 677/* Blocks are a bunch of untyped bytes. */
c906108c
SS
678struct dwarf_block
679 {
680 unsigned int size;
1d6edc3c
JK
681
682 /* Valid only if SIZE is not zero. */
fe1b8b76 683 gdb_byte *data;
c906108c
SS
684 };
685
c906108c
SS
686#ifndef ATTR_ALLOC_CHUNK
687#define ATTR_ALLOC_CHUNK 4
688#endif
689
c906108c
SS
690/* Allocate fields for structs, unions and enums in this size. */
691#ifndef DW_FIELD_ALLOC_CHUNK
692#define DW_FIELD_ALLOC_CHUNK 4
693#endif
694
c906108c
SS
695/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
696 but this would require a corresponding change in unpack_field_as_long
697 and friends. */
698static int bits_per_byte = 8;
699
700/* The routines that read and process dies for a C struct or C++ class
701 pass lists of data member fields and lists of member function fields
702 in an instance of a field_info structure, as defined below. */
703struct field_info
c5aa993b 704 {
0963b4bd 705 /* List of data member and baseclasses fields. */
c5aa993b
JM
706 struct nextfield
707 {
708 struct nextfield *next;
709 int accessibility;
710 int virtuality;
711 struct field field;
712 }
7d0ccb61 713 *fields, *baseclasses;
c906108c 714
7d0ccb61 715 /* Number of fields (including baseclasses). */
c5aa993b 716 int nfields;
c906108c 717
c5aa993b
JM
718 /* Number of baseclasses. */
719 int nbaseclasses;
c906108c 720
c5aa993b
JM
721 /* Set if the accesibility of one of the fields is not public. */
722 int non_public_fields;
c906108c 723
c5aa993b
JM
724 /* Member function fields array, entries are allocated in the order they
725 are encountered in the object file. */
726 struct nextfnfield
727 {
728 struct nextfnfield *next;
729 struct fn_field fnfield;
730 }
731 *fnfields;
c906108c 732
c5aa993b
JM
733 /* Member function fieldlist array, contains name of possibly overloaded
734 member function, number of overloaded member functions and a pointer
735 to the head of the member function field chain. */
736 struct fnfieldlist
737 {
738 char *name;
739 int length;
740 struct nextfnfield *head;
741 }
742 *fnfieldlists;
c906108c 743
c5aa993b
JM
744 /* Number of entries in the fnfieldlists array. */
745 int nfnfields;
98751a41
JK
746
747 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
748 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
749 struct typedef_field_list
750 {
751 struct typedef_field field;
752 struct typedef_field_list *next;
753 }
754 *typedef_field_list;
755 unsigned typedef_field_list_count;
c5aa993b 756 };
c906108c 757
10b3939b
DJ
758/* One item on the queue of compilation units to read in full symbols
759 for. */
760struct dwarf2_queue_item
761{
762 struct dwarf2_per_cu_data *per_cu;
763 struct dwarf2_queue_item *next;
764};
765
766/* The current queue. */
767static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
768
ae038cb0
DJ
769/* Loaded secondary compilation units are kept in memory until they
770 have not been referenced for the processing of this many
771 compilation units. Set this to zero to disable caching. Cache
772 sizes of up to at least twenty will improve startup time for
773 typical inter-CU-reference binaries, at an obvious memory cost. */
774static int dwarf2_max_cache_age = 5;
920d2a44
AC
775static void
776show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
777 struct cmd_list_element *c, const char *value)
778{
3e43a32a
MS
779 fprintf_filtered (file, _("The upper bound on the age of cached "
780 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
781 value);
782}
783
ae038cb0 784
0963b4bd 785/* Various complaints about symbol reading that don't abort the process. */
c906108c 786
4d3c2250
KB
787static void
788dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 789{
4d3c2250 790 complaint (&symfile_complaints,
e2e0b3e5 791 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
792}
793
25e43795
DJ
794static void
795dwarf2_debug_line_missing_file_complaint (void)
796{
797 complaint (&symfile_complaints,
798 _(".debug_line section has line data without a file"));
799}
800
59205f5a
JB
801static void
802dwarf2_debug_line_missing_end_sequence_complaint (void)
803{
804 complaint (&symfile_complaints,
3e43a32a
MS
805 _(".debug_line section has line "
806 "program sequence without an end"));
59205f5a
JB
807}
808
4d3c2250
KB
809static void
810dwarf2_complex_location_expr_complaint (void)
2e276125 811{
e2e0b3e5 812 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
813}
814
4d3c2250
KB
815static void
816dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
817 int arg3)
2e276125 818{
4d3c2250 819 complaint (&symfile_complaints,
3e43a32a
MS
820 _("const value length mismatch for '%s', got %d, expected %d"),
821 arg1, arg2, arg3);
4d3c2250
KB
822}
823
824static void
cf2c3c16 825dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 826{
4d3c2250 827 complaint (&symfile_complaints,
cf2c3c16
TT
828 _("macro info runs off end of `%s' section"),
829 section->asection->name);
4d3c2250
KB
830}
831
832static void
833dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 834{
4d3c2250 835 complaint (&symfile_complaints,
3e43a32a
MS
836 _("macro debug info contains a "
837 "malformed macro definition:\n`%s'"),
4d3c2250
KB
838 arg1);
839}
840
841static void
842dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 843{
4d3c2250 844 complaint (&symfile_complaints,
3e43a32a
MS
845 _("invalid attribute class or form for '%s' in '%s'"),
846 arg1, arg2);
4d3c2250 847}
c906108c 848
c906108c
SS
849/* local function prototypes */
850
4efb68b1 851static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 852
aaa75496
JB
853static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
854 struct objfile *);
855
918dd910
JK
856static void dwarf2_find_base_address (struct die_info *die,
857 struct dwarf2_cu *cu);
858
c67a9c90 859static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 860
72bf9492
DJ
861static void scan_partial_symbols (struct partial_die_info *,
862 CORE_ADDR *, CORE_ADDR *,
5734ee8b 863 int, struct dwarf2_cu *);
c906108c 864
72bf9492
DJ
865static void add_partial_symbol (struct partial_die_info *,
866 struct dwarf2_cu *);
63d06c5c 867
72bf9492
DJ
868static void add_partial_namespace (struct partial_die_info *pdi,
869 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 870 int need_pc, struct dwarf2_cu *cu);
63d06c5c 871
5d7cb8df
JK
872static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
873 CORE_ADDR *highpc, int need_pc,
874 struct dwarf2_cu *cu);
875
72bf9492
DJ
876static void add_partial_enumeration (struct partial_die_info *enum_pdi,
877 struct dwarf2_cu *cu);
91c24f0a 878
bc30ff58
JB
879static void add_partial_subprogram (struct partial_die_info *pdi,
880 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 881 int need_pc, struct dwarf2_cu *cu);
bc30ff58 882
fe1b8b76 883static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
884 gdb_byte *buffer, gdb_byte *info_ptr,
885 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 886
a14ed312 887static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 888
a14ed312 889static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 890
e5fe5e75 891static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
c906108c 892
f3dd6933 893static void dwarf2_free_abbrev_table (void *);
c906108c 894
6caca83c
CC
895static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
896
fe1b8b76 897static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 898 struct dwarf2_cu *);
72bf9492 899
57349743 900static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 901 struct dwarf2_cu *);
c906108c 902
93311388
DE
903static struct partial_die_info *load_partial_dies (bfd *,
904 gdb_byte *, gdb_byte *,
905 int, struct dwarf2_cu *);
72bf9492 906
fe1b8b76 907static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
908 struct abbrev_info *abbrev,
909 unsigned int, bfd *,
910 gdb_byte *, gdb_byte *,
911 struct dwarf2_cu *);
c906108c 912
c764a876 913static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 914 struct dwarf2_cu *);
72bf9492
DJ
915
916static void fixup_partial_die (struct partial_die_info *,
917 struct dwarf2_cu *);
918
fe1b8b76
JB
919static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
920 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 921
fe1b8b76
JB
922static gdb_byte *read_attribute_value (struct attribute *, unsigned,
923 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 924
fe1b8b76 925static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 926
fe1b8b76 927static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 928
fe1b8b76 929static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 930
fe1b8b76 931static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 932
93311388 933static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 934
fe1b8b76 935static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 936 unsigned int *);
c906108c 937
c764a876
DE
938static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
939
940static LONGEST read_checked_initial_length_and_offset
941 (bfd *, gdb_byte *, const struct comp_unit_head *,
942 unsigned int *, unsigned int *);
613e1657 943
fe1b8b76 944static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
945 unsigned int *);
946
947static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 948
fe1b8b76 949static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 950
9b1c24c8 951static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 952
fe1b8b76
JB
953static char *read_indirect_string (bfd *, gdb_byte *,
954 const struct comp_unit_head *,
955 unsigned int *);
4bdf3d34 956
fe1b8b76 957static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 958
fe1b8b76 959static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 960
fe1b8b76 961static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 962
e142c38c 963static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 964
e142c38c
DJ
965static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
966 struct dwarf2_cu *);
c906108c 967
348e048f
DE
968static struct attribute *dwarf2_attr_no_follow (struct die_info *,
969 unsigned int,
970 struct dwarf2_cu *);
971
05cf31d1
JB
972static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
973 struct dwarf2_cu *cu);
974
e142c38c 975static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 976
e142c38c 977static struct die_info *die_specification (struct die_info *die,
f2f0e013 978 struct dwarf2_cu **);
63d06c5c 979
debd256d
JB
980static void free_line_header (struct line_header *lh);
981
aaa75496
JB
982static void add_file_name (struct line_header *, char *, unsigned int,
983 unsigned int, unsigned int);
984
debd256d
JB
985static struct line_header *(dwarf_decode_line_header
986 (unsigned int offset,
e7c27a73 987 bfd *abfd, struct dwarf2_cu *cu));
debd256d 988
f3f5162e
DE
989static void dwarf_decode_lines (struct line_header *, const char *,
990 struct dwarf2_cu *, struct partial_symtab *,
991 int);
c906108c 992
72b9f47f 993static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 994
a14ed312 995static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 996 struct dwarf2_cu *);
c906108c 997
34eaf542
TT
998static struct symbol *new_symbol_full (struct die_info *, struct type *,
999 struct dwarf2_cu *, struct symbol *);
1000
a14ed312 1001static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1002 struct dwarf2_cu *);
c906108c 1003
98bfdba5
PA
1004static void dwarf2_const_value_attr (struct attribute *attr,
1005 struct type *type,
1006 const char *name,
1007 struct obstack *obstack,
1008 struct dwarf2_cu *cu, long *value,
1009 gdb_byte **bytes,
1010 struct dwarf2_locexpr_baton **baton);
2df3850c 1011
e7c27a73 1012static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1013
b4ba55a1
JB
1014static int need_gnat_info (struct dwarf2_cu *);
1015
3e43a32a
MS
1016static struct type *die_descriptive_type (struct die_info *,
1017 struct dwarf2_cu *);
b4ba55a1
JB
1018
1019static void set_descriptive_type (struct type *, struct die_info *,
1020 struct dwarf2_cu *);
1021
e7c27a73
DJ
1022static struct type *die_containing_type (struct die_info *,
1023 struct dwarf2_cu *);
c906108c 1024
673bfd45
DE
1025static struct type *lookup_die_type (struct die_info *, struct attribute *,
1026 struct dwarf2_cu *);
c906108c 1027
f792889a 1028static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1029
673bfd45
DE
1030static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1031
086ed43d 1032static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1033
6e70227d 1034static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1035 const char *suffix, int physname,
1036 struct dwarf2_cu *cu);
63d06c5c 1037
e7c27a73 1038static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1039
348e048f
DE
1040static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1041
e7c27a73 1042static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1043
e7c27a73 1044static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1045
96408a79
SA
1046static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1047
ff013f42
JK
1048static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1049 struct dwarf2_cu *, struct partial_symtab *);
1050
a14ed312 1051static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1052 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1053 struct partial_symtab *);
c906108c 1054
fae299cd
DC
1055static void get_scope_pc_bounds (struct die_info *,
1056 CORE_ADDR *, CORE_ADDR *,
1057 struct dwarf2_cu *);
1058
801e3a5b
JB
1059static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1060 CORE_ADDR, struct dwarf2_cu *);
1061
a14ed312 1062static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1063 struct dwarf2_cu *);
c906108c 1064
a14ed312 1065static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1066 struct type *, struct dwarf2_cu *);
c906108c 1067
a14ed312 1068static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1069 struct die_info *, struct type *,
e7c27a73 1070 struct dwarf2_cu *);
c906108c 1071
a14ed312 1072static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1073 struct type *,
1074 struct dwarf2_cu *);
c906108c 1075
134d01f1 1076static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1077
e7c27a73 1078static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1079
e7c27a73 1080static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1081
5d7cb8df
JK
1082static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1083
27aa8d6a
SW
1084static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1085
f55ee35c
JK
1086static struct type *read_module_type (struct die_info *die,
1087 struct dwarf2_cu *cu);
1088
38d518c9 1089static const char *namespace_name (struct die_info *die,
e142c38c 1090 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1091
134d01f1 1092static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1093
e7c27a73 1094static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1095
6e70227d 1096static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1097 struct dwarf2_cu *);
1098
93311388 1099static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1100
93311388
DE
1101static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1102 gdb_byte *info_ptr,
d97bc12b
DE
1103 gdb_byte **new_info_ptr,
1104 struct die_info *parent);
1105
93311388
DE
1106static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1107 gdb_byte *info_ptr,
fe1b8b76 1108 gdb_byte **new_info_ptr,
639d11d3
DC
1109 struct die_info *parent);
1110
93311388
DE
1111static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1112 gdb_byte *info_ptr,
fe1b8b76 1113 gdb_byte **new_info_ptr,
639d11d3
DC
1114 struct die_info *parent);
1115
93311388
DE
1116static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1117 struct die_info **, gdb_byte *,
1118 int *);
1119
e7c27a73 1120static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1121
71c25dea
TT
1122static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1123 struct obstack *);
1124
e142c38c 1125static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1126
98bfdba5
PA
1127static const char *dwarf2_full_name (char *name,
1128 struct die_info *die,
1129 struct dwarf2_cu *cu);
1130
e142c38c 1131static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1132 struct dwarf2_cu **);
9219021c 1133
a14ed312 1134static char *dwarf_tag_name (unsigned int);
c906108c 1135
a14ed312 1136static char *dwarf_attr_name (unsigned int);
c906108c 1137
a14ed312 1138static char *dwarf_form_name (unsigned int);
c906108c 1139
a14ed312 1140static char *dwarf_bool_name (unsigned int);
c906108c 1141
a14ed312 1142static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1143
1144#if 0
a14ed312 1145static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1146#endif
1147
f9aca02d 1148static struct die_info *sibling_die (struct die_info *);
c906108c 1149
d97bc12b
DE
1150static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1151
1152static void dump_die_for_error (struct die_info *);
1153
1154static void dump_die_1 (struct ui_file *, int level, int max_level,
1155 struct die_info *);
c906108c 1156
d97bc12b 1157/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1158
51545339 1159static void store_in_ref_table (struct die_info *,
10b3939b 1160 struct dwarf2_cu *);
c906108c 1161
93311388
DE
1162static int is_ref_attr (struct attribute *);
1163
c764a876 1164static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1165
43bbcdc2 1166static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1167
348e048f
DE
1168static struct die_info *follow_die_ref_or_sig (struct die_info *,
1169 struct attribute *,
1170 struct dwarf2_cu **);
1171
10b3939b
DJ
1172static struct die_info *follow_die_ref (struct die_info *,
1173 struct attribute *,
f2f0e013 1174 struct dwarf2_cu **);
c906108c 1175
348e048f
DE
1176static struct die_info *follow_die_sig (struct die_info *,
1177 struct attribute *,
1178 struct dwarf2_cu **);
1179
6c83ed52
TT
1180static struct signatured_type *lookup_signatured_type_at_offset
1181 (struct objfile *objfile,
1182 struct dwarf2_section_info *section,
1183 unsigned int offset);
1184
e5fe5e75 1185static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1186
a0f42c21 1187static void read_signatured_type (struct signatured_type *type_sig);
348e048f 1188
c906108c
SS
1189/* memory allocation interface */
1190
7b5a2f43 1191static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1192
f3dd6933 1193static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1194
b60c80d6 1195static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1196
2e276125 1197static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1198 char *, bfd *, struct dwarf2_cu *,
1199 struct dwarf2_section_info *,
1200 int);
2e276125 1201
8e19ed76
PS
1202static int attr_form_is_block (struct attribute *);
1203
3690dd37
JB
1204static int attr_form_is_section_offset (struct attribute *);
1205
1206static int attr_form_is_constant (struct attribute *);
1207
8cf6f0b1
TT
1208static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1209 struct dwarf2_loclist_baton *baton,
1210 struct attribute *attr);
1211
93e7bd98
DJ
1212static void dwarf2_symbol_mark_computed (struct attribute *attr,
1213 struct symbol *sym,
1214 struct dwarf2_cu *cu);
4c2df51b 1215
93311388
DE
1216static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1217 struct abbrev_info *abbrev,
1218 struct dwarf2_cu *cu);
4bb7a0a7 1219
72bf9492
DJ
1220static void free_stack_comp_unit (void *);
1221
72bf9492
DJ
1222static hashval_t partial_die_hash (const void *item);
1223
1224static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1225
ae038cb0 1226static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1227 (unsigned int offset, struct objfile *objfile);
ae038cb0 1228
9816fde3 1229static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1230 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1231
1232static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1233 struct die_info *comp_unit_die);
93311388 1234
68dc6402 1235static void free_heap_comp_unit (void *);
ae038cb0
DJ
1236
1237static void free_cached_comp_units (void *);
1238
1239static void age_cached_comp_units (void);
1240
1241static void free_one_cached_comp_unit (void *);
1242
f792889a
DJ
1243static struct type *set_die_type (struct die_info *, struct type *,
1244 struct dwarf2_cu *);
1c379e20 1245
ae038cb0
DJ
1246static void create_all_comp_units (struct objfile *);
1247
1fd400ff
TT
1248static int create_debug_types_hash_table (struct objfile *objfile);
1249
a0f42c21 1250static void load_full_comp_unit (struct dwarf2_per_cu_data *);
10b3939b
DJ
1251
1252static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1253
1254static void dwarf2_add_dependence (struct dwarf2_cu *,
1255 struct dwarf2_per_cu_data *);
1256
ae038cb0
DJ
1257static void dwarf2_mark (struct dwarf2_cu *);
1258
1259static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1260
673bfd45
DE
1261static struct type *get_die_type_at_offset (unsigned int,
1262 struct dwarf2_per_cu_data *per_cu);
1263
f792889a 1264static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1265
9291a0cd
TT
1266static void dwarf2_release_queue (void *dummy);
1267
a0f42c21 1268static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
9291a0cd 1269
a0f42c21 1270static void process_queue (void);
9291a0cd
TT
1271
1272static void find_file_and_directory (struct die_info *die,
1273 struct dwarf2_cu *cu,
1274 char **name, char **comp_dir);
1275
1276static char *file_full_name (int file, struct line_header *lh,
1277 const char *comp_dir);
1278
9ff913ba
DE
1279static gdb_byte *read_and_check_comp_unit_head
1280 (struct comp_unit_head *header,
1281 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1282 int is_debug_types_section);
9291a0cd
TT
1283
1284static void init_cu_die_reader (struct die_reader_specs *reader,
1285 struct dwarf2_cu *cu);
1286
673bfd45 1287static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1288
9291a0cd
TT
1289#if WORDS_BIGENDIAN
1290
1291/* Convert VALUE between big- and little-endian. */
1292static offset_type
1293byte_swap (offset_type value)
1294{
1295 offset_type result;
1296
1297 result = (value & 0xff) << 24;
1298 result |= (value & 0xff00) << 8;
1299 result |= (value & 0xff0000) >> 8;
1300 result |= (value & 0xff000000) >> 24;
1301 return result;
1302}
1303
1304#define MAYBE_SWAP(V) byte_swap (V)
1305
1306#else
1307#define MAYBE_SWAP(V) (V)
1308#endif /* WORDS_BIGENDIAN */
1309
1310/* The suffix for an index file. */
1311#define INDEX_SUFFIX ".gdb-index"
1312
3da10d80
KS
1313static const char *dwarf2_physname (char *name, struct die_info *die,
1314 struct dwarf2_cu *cu);
1315
c906108c 1316/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1317 information and return true if we have enough to do something.
1318 NAMES points to the dwarf2 section names, or is NULL if the standard
1319 ELF names are used. */
c906108c
SS
1320
1321int
251d32d9
TG
1322dwarf2_has_info (struct objfile *objfile,
1323 const struct dwarf2_debug_sections *names)
c906108c 1324{
be391dca
TT
1325 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1326 if (!dwarf2_per_objfile)
1327 {
1328 /* Initialize per-objfile state. */
1329 struct dwarf2_per_objfile *data
1330 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1331
be391dca
TT
1332 memset (data, 0, sizeof (*data));
1333 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1334 dwarf2_per_objfile = data;
6502dd73 1335
251d32d9
TG
1336 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1337 (void *) names);
be391dca
TT
1338 dwarf2_per_objfile->objfile = objfile;
1339 }
1340 return (dwarf2_per_objfile->info.asection != NULL
1341 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1342}
1343
251d32d9
TG
1344/* When loading sections, we look either for uncompressed section or for
1345 compressed section names. */
233a11ab
CS
1346
1347static int
251d32d9
TG
1348section_is_p (const char *section_name,
1349 const struct dwarf2_section_names *names)
233a11ab 1350{
251d32d9
TG
1351 if (names->normal != NULL
1352 && strcmp (section_name, names->normal) == 0)
1353 return 1;
1354 if (names->compressed != NULL
1355 && strcmp (section_name, names->compressed) == 0)
1356 return 1;
1357 return 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
251d32d9 1365dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1366{
251d32d9
TG
1367 const struct dwarf2_debug_sections *names;
1368
1369 if (vnames == NULL)
1370 names = &dwarf2_elf_names;
1371 else
1372 names = (const struct dwarf2_debug_sections *) vnames;
1373
1374 if (section_is_p (sectp->name, &names->info))
c906108c 1375 {
dce234bc
PP
1376 dwarf2_per_objfile->info.asection = sectp;
1377 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1378 }
251d32d9 1379 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1380 {
dce234bc
PP
1381 dwarf2_per_objfile->abbrev.asection = sectp;
1382 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1383 }
251d32d9 1384 else if (section_is_p (sectp->name, &names->line))
c906108c 1385 {
dce234bc
PP
1386 dwarf2_per_objfile->line.asection = sectp;
1387 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1388 }
251d32d9 1389 else if (section_is_p (sectp->name, &names->loc))
c906108c 1390 {
dce234bc
PP
1391 dwarf2_per_objfile->loc.asection = sectp;
1392 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1393 }
251d32d9 1394 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1395 {
dce234bc
PP
1396 dwarf2_per_objfile->macinfo.asection = sectp;
1397 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1398 }
cf2c3c16
TT
1399 else if (section_is_p (sectp->name, &names->macro))
1400 {
1401 dwarf2_per_objfile->macro.asection = sectp;
1402 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1403 }
251d32d9 1404 else if (section_is_p (sectp->name, &names->str))
c906108c 1405 {
dce234bc
PP
1406 dwarf2_per_objfile->str.asection = sectp;
1407 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1408 }
251d32d9 1409 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1410 {
dce234bc
PP
1411 dwarf2_per_objfile->frame.asection = sectp;
1412 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1413 }
251d32d9 1414 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1415 {
3799ccc6 1416 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1417
3799ccc6
EZ
1418 if (aflag & SEC_HAS_CONTENTS)
1419 {
dce234bc
PP
1420 dwarf2_per_objfile->eh_frame.asection = sectp;
1421 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1422 }
b6af0555 1423 }
251d32d9 1424 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1425 {
dce234bc
PP
1426 dwarf2_per_objfile->ranges.asection = sectp;
1427 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1428 }
251d32d9 1429 else if (section_is_p (sectp->name, &names->types))
348e048f 1430 {
8b70b953
TT
1431 struct dwarf2_section_info type_section;
1432
1433 memset (&type_section, 0, sizeof (type_section));
1434 type_section.asection = sectp;
1435 type_section.size = bfd_get_section_size (sectp);
1436
1437 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1438 &type_section);
348e048f 1439 }
251d32d9 1440 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1441 {
1442 dwarf2_per_objfile->gdb_index.asection = sectp;
1443 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1444 }
dce234bc 1445
72dca2f5
FR
1446 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1447 && bfd_section_vma (abfd, sectp) == 0)
1448 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1449}
1450
dce234bc
PP
1451/* Decompress a section that was compressed using zlib. Store the
1452 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1453
1454static void
dce234bc
PP
1455zlib_decompress_section (struct objfile *objfile, asection *sectp,
1456 gdb_byte **outbuf, bfd_size_type *outsize)
1457{
1458 bfd *abfd = objfile->obfd;
1459#ifndef HAVE_ZLIB_H
1460 error (_("Support for zlib-compressed DWARF data (from '%s') "
1461 "is disabled in this copy of GDB"),
1462 bfd_get_filename (abfd));
1463#else
1464 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1465 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1466 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1467 bfd_size_type uncompressed_size;
1468 gdb_byte *uncompressed_buffer;
1469 z_stream strm;
1470 int rc;
1471 int header_size = 12;
1472
1473 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1474 || bfd_bread (compressed_buffer,
1475 compressed_size, abfd) != compressed_size)
dce234bc
PP
1476 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1477 bfd_get_filename (abfd));
1478
1479 /* Read the zlib header. In this case, it should be "ZLIB" followed
1480 by the uncompressed section size, 8 bytes in big-endian order. */
1481 if (compressed_size < header_size
1482 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1483 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1484 bfd_get_filename (abfd));
1485 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1486 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1490 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1491 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1492 uncompressed_size += compressed_buffer[11];
1493
1494 /* It is possible the section consists of several compressed
1495 buffers concatenated together, so we uncompress in a loop. */
1496 strm.zalloc = NULL;
1497 strm.zfree = NULL;
1498 strm.opaque = NULL;
1499 strm.avail_in = compressed_size - header_size;
1500 strm.next_in = (Bytef*) compressed_buffer + header_size;
1501 strm.avail_out = uncompressed_size;
1502 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1503 uncompressed_size);
1504 rc = inflateInit (&strm);
1505 while (strm.avail_in > 0)
1506 {
1507 if (rc != Z_OK)
1508 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1509 bfd_get_filename (abfd), rc);
1510 strm.next_out = ((Bytef*) uncompressed_buffer
1511 + (uncompressed_size - strm.avail_out));
1512 rc = inflate (&strm, Z_FINISH);
1513 if (rc != Z_STREAM_END)
1514 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1515 bfd_get_filename (abfd), rc);
1516 rc = inflateReset (&strm);
1517 }
1518 rc = inflateEnd (&strm);
1519 if (rc != Z_OK
1520 || strm.avail_out != 0)
1521 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1522 bfd_get_filename (abfd), rc);
1523
affddf13 1524 do_cleanups (cleanup);
dce234bc
PP
1525 *outbuf = uncompressed_buffer;
1526 *outsize = uncompressed_size;
1527#endif
233a11ab
CS
1528}
1529
9e0ac564
TT
1530/* A helper function that decides whether a section is empty. */
1531
1532static int
1533dwarf2_section_empty_p (struct dwarf2_section_info *info)
1534{
1535 return info->asection == NULL || info->size == 0;
1536}
1537
9cdd5dbd 1538/* Read the contents of the section INFO from object file specified by
dce234bc
PP
1539 OBJFILE, store info about the section into INFO.
1540 If the section is compressed, uncompress it before returning. */
c906108c 1541
dce234bc
PP
1542static void
1543dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1544{
dce234bc
PP
1545 bfd *abfd = objfile->obfd;
1546 asection *sectp = info->asection;
1547 gdb_byte *buf, *retbuf;
1548 unsigned char header[4];
c906108c 1549
be391dca
TT
1550 if (info->readin)
1551 return;
dce234bc 1552 info->buffer = NULL;
b315ab21 1553 info->map_addr = NULL;
be391dca 1554 info->readin = 1;
188dd5d6 1555
9e0ac564 1556 if (dwarf2_section_empty_p (info))
dce234bc 1557 return;
c906108c 1558
dce234bc
PP
1559 /* Check if the file has a 4-byte header indicating compression. */
1560 if (info->size > sizeof (header)
1561 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1562 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1563 {
1564 /* Upon decompression, update the buffer and its size. */
1565 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1566 {
1567 zlib_decompress_section (objfile, sectp, &info->buffer,
1568 &info->size);
1569 return;
1570 }
1571 }
4bdf3d34 1572
dce234bc
PP
1573#ifdef HAVE_MMAP
1574 if (pagesize == 0)
1575 pagesize = getpagesize ();
2e276125 1576
dce234bc
PP
1577 /* Only try to mmap sections which are large enough: we don't want to
1578 waste space due to fragmentation. Also, only try mmap for sections
1579 without relocations. */
1580
1581 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1582 {
b315ab21
TG
1583 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1584 MAP_PRIVATE, sectp->filepos,
1585 &info->map_addr, &info->map_len);
dce234bc 1586
b315ab21 1587 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1588 {
be391dca 1589#if HAVE_POSIX_MADVISE
b315ab21 1590 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1591#endif
dce234bc
PP
1592 return;
1593 }
1594 }
1595#endif
1596
1597 /* If we get here, we are a normal, not-compressed section. */
1598 info->buffer = buf
1599 = obstack_alloc (&objfile->objfile_obstack, info->size);
1600
1601 /* When debugging .o files, we may need to apply relocations; see
1602 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1603 We never compress sections in .o files, so we only need to
1604 try this when the section is not compressed. */
ac8035ab 1605 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1606 if (retbuf != NULL)
1607 {
1608 info->buffer = retbuf;
1609 return;
1610 }
1611
1612 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1613 || bfd_bread (buf, info->size, abfd) != info->size)
1614 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1615 bfd_get_filename (abfd));
1616}
1617
9e0ac564
TT
1618/* A helper function that returns the size of a section in a safe way.
1619 If you are positive that the section has been read before using the
1620 size, then it is safe to refer to the dwarf2_section_info object's
1621 "size" field directly. In other cases, you must call this
1622 function, because for compressed sections the size field is not set
1623 correctly until the section has been read. */
1624
1625static bfd_size_type
1626dwarf2_section_size (struct objfile *objfile,
1627 struct dwarf2_section_info *info)
1628{
1629 if (!info->readin)
1630 dwarf2_read_section (objfile, info);
1631 return info->size;
1632}
1633
dce234bc 1634/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1635 SECTION_NAME. */
af34e669 1636
dce234bc 1637void
3017a003
TG
1638dwarf2_get_section_info (struct objfile *objfile,
1639 enum dwarf2_section_enum sect,
dce234bc
PP
1640 asection **sectp, gdb_byte **bufp,
1641 bfd_size_type *sizep)
1642{
1643 struct dwarf2_per_objfile *data
1644 = objfile_data (objfile, dwarf2_objfile_data_key);
1645 struct dwarf2_section_info *info;
a3b2a86b
TT
1646
1647 /* We may see an objfile without any DWARF, in which case we just
1648 return nothing. */
1649 if (data == NULL)
1650 {
1651 *sectp = NULL;
1652 *bufp = NULL;
1653 *sizep = 0;
1654 return;
1655 }
3017a003
TG
1656 switch (sect)
1657 {
1658 case DWARF2_DEBUG_FRAME:
1659 info = &data->frame;
1660 break;
1661 case DWARF2_EH_FRAME:
1662 info = &data->eh_frame;
1663 break;
1664 default:
1665 gdb_assert_not_reached ("unexpected section");
1666 }
dce234bc 1667
9e0ac564 1668 dwarf2_read_section (objfile, info);
dce234bc
PP
1669
1670 *sectp = info->asection;
1671 *bufp = info->buffer;
1672 *sizep = info->size;
1673}
1674
9291a0cd 1675\f
7b9f3c50
DE
1676/* DWARF quick_symbols_functions support. */
1677
1678/* TUs can share .debug_line entries, and there can be a lot more TUs than
1679 unique line tables, so we maintain a separate table of all .debug_line
1680 derived entries to support the sharing.
1681 All the quick functions need is the list of file names. We discard the
1682 line_header when we're done and don't need to record it here. */
1683struct quick_file_names
1684{
1685 /* The offset in .debug_line of the line table. We hash on this. */
1686 unsigned int offset;
1687
1688 /* The number of entries in file_names, real_names. */
1689 unsigned int num_file_names;
1690
1691 /* The file names from the line table, after being run through
1692 file_full_name. */
1693 const char **file_names;
1694
1695 /* The file names from the line table after being run through
1696 gdb_realpath. These are computed lazily. */
1697 const char **real_names;
1698};
1699
1700/* When using the index (and thus not using psymtabs), each CU has an
1701 object of this type. This is used to hold information needed by
1702 the various "quick" methods. */
1703struct dwarf2_per_cu_quick_data
1704{
1705 /* The file table. This can be NULL if there was no file table
1706 or it's currently not read in.
1707 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1708 struct quick_file_names *file_names;
1709
1710 /* The corresponding symbol table. This is NULL if symbols for this
1711 CU have not yet been read. */
1712 struct symtab *symtab;
1713
1714 /* A temporary mark bit used when iterating over all CUs in
1715 expand_symtabs_matching. */
1716 unsigned int mark : 1;
1717
1718 /* True if we've tried to read the file table and found there isn't one.
1719 There will be no point in trying to read it again next time. */
1720 unsigned int no_file_data : 1;
1721};
1722
1723/* Hash function for a quick_file_names. */
1724
1725static hashval_t
1726hash_file_name_entry (const void *e)
1727{
1728 const struct quick_file_names *file_data = e;
1729
1730 return file_data->offset;
1731}
1732
1733/* Equality function for a quick_file_names. */
1734
1735static int
1736eq_file_name_entry (const void *a, const void *b)
1737{
1738 const struct quick_file_names *ea = a;
1739 const struct quick_file_names *eb = b;
1740
1741 return ea->offset == eb->offset;
1742}
1743
1744/* Delete function for a quick_file_names. */
1745
1746static void
1747delete_file_name_entry (void *e)
1748{
1749 struct quick_file_names *file_data = e;
1750 int i;
1751
1752 for (i = 0; i < file_data->num_file_names; ++i)
1753 {
1754 xfree ((void*) file_data->file_names[i]);
1755 if (file_data->real_names)
1756 xfree ((void*) file_data->real_names[i]);
1757 }
1758
1759 /* The space for the struct itself lives on objfile_obstack,
1760 so we don't free it here. */
1761}
1762
1763/* Create a quick_file_names hash table. */
1764
1765static htab_t
1766create_quick_file_names_table (unsigned int nr_initial_entries)
1767{
1768 return htab_create_alloc (nr_initial_entries,
1769 hash_file_name_entry, eq_file_name_entry,
1770 delete_file_name_entry, xcalloc, xfree);
1771}
9291a0cd 1772
918dd910
JK
1773/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1774 have to be created afterwards. You should call age_cached_comp_units after
1775 processing PER_CU->CU. dw2_setup must have been already called. */
1776
1777static void
1778load_cu (struct dwarf2_per_cu_data *per_cu)
1779{
b0df02fd 1780 if (per_cu->debug_types_section)
e5fe5e75 1781 load_full_type_unit (per_cu);
918dd910 1782 else
a0f42c21 1783 load_full_comp_unit (per_cu);
918dd910 1784
918dd910 1785 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1786
1787 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1788}
1789
a0f42c21 1790/* Read in the symbols for PER_CU. */
2fdf6df6 1791
9291a0cd 1792static void
a0f42c21 1793dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1794{
1795 struct cleanup *back_to;
1796
1797 back_to = make_cleanup (dwarf2_release_queue, NULL);
1798
a0f42c21 1799 queue_comp_unit (per_cu);
9291a0cd 1800
918dd910 1801 load_cu (per_cu);
9291a0cd 1802
a0f42c21 1803 process_queue ();
9291a0cd
TT
1804
1805 /* Age the cache, releasing compilation units that have not
1806 been used recently. */
1807 age_cached_comp_units ();
1808
1809 do_cleanups (back_to);
1810}
1811
1812/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1813 the objfile from which this CU came. Returns the resulting symbol
1814 table. */
2fdf6df6 1815
9291a0cd 1816static struct symtab *
a0f42c21 1817dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1818{
1819 if (!per_cu->v.quick->symtab)
1820 {
1821 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1822 increment_reading_symtab ();
a0f42c21 1823 dw2_do_instantiate_symtab (per_cu);
9291a0cd
TT
1824 do_cleanups (back_to);
1825 }
1826 return per_cu->v.quick->symtab;
1827}
1828
1fd400ff 1829/* Return the CU given its index. */
2fdf6df6 1830
1fd400ff
TT
1831static struct dwarf2_per_cu_data *
1832dw2_get_cu (int index)
1833{
1834 if (index >= dwarf2_per_objfile->n_comp_units)
1835 {
1836 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 1837 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
1838 }
1839 return dwarf2_per_objfile->all_comp_units[index];
1840}
1841
9291a0cd
TT
1842/* A helper function that knows how to read a 64-bit value in a way
1843 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1844 otherwise. */
2fdf6df6 1845
9291a0cd
TT
1846static int
1847extract_cu_value (const char *bytes, ULONGEST *result)
1848{
1849 if (sizeof (ULONGEST) < 8)
1850 {
1851 int i;
1852
1853 /* Ignore the upper 4 bytes if they are all zero. */
1854 for (i = 0; i < 4; ++i)
1855 if (bytes[i + 4] != 0)
1856 return 0;
1857
1858 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1859 }
1860 else
1861 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1862 return 1;
1863}
1864
1865/* Read the CU list from the mapped index, and use it to create all
1866 the CU objects for this objfile. Return 0 if something went wrong,
1867 1 if everything went ok. */
2fdf6df6 1868
9291a0cd 1869static int
1fd400ff
TT
1870create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1871 offset_type cu_list_elements)
9291a0cd
TT
1872{
1873 offset_type i;
9291a0cd
TT
1874
1875 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1876 dwarf2_per_objfile->all_comp_units
1877 = obstack_alloc (&objfile->objfile_obstack,
1878 dwarf2_per_objfile->n_comp_units
1879 * sizeof (struct dwarf2_per_cu_data *));
1880
1881 for (i = 0; i < cu_list_elements; i += 2)
1882 {
1883 struct dwarf2_per_cu_data *the_cu;
1884 ULONGEST offset, length;
1885
1886 if (!extract_cu_value (cu_list, &offset)
1887 || !extract_cu_value (cu_list + 8, &length))
1888 return 0;
1889 cu_list += 2 * 8;
1890
1891 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1892 struct dwarf2_per_cu_data);
1893 the_cu->offset = offset;
1894 the_cu->length = length;
1895 the_cu->objfile = objfile;
1896 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1897 struct dwarf2_per_cu_quick_data);
1898 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1899 }
1900
1901 return 1;
1902}
1903
1fd400ff 1904/* Create the signatured type hash table from the index. */
673bfd45 1905
1fd400ff 1906static int
673bfd45 1907create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1908 struct dwarf2_section_info *section,
673bfd45
DE
1909 const gdb_byte *bytes,
1910 offset_type elements)
1fd400ff
TT
1911{
1912 offset_type i;
673bfd45 1913 htab_t sig_types_hash;
1fd400ff 1914
d467dd73
DE
1915 dwarf2_per_objfile->n_type_units = elements / 3;
1916 dwarf2_per_objfile->all_type_units
1fd400ff 1917 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 1918 dwarf2_per_objfile->n_type_units
1fd400ff
TT
1919 * sizeof (struct dwarf2_per_cu_data *));
1920
673bfd45 1921 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1922
1923 for (i = 0; i < elements; i += 3)
1924 {
1925 struct signatured_type *type_sig;
1926 ULONGEST offset, type_offset, signature;
1927 void **slot;
1928
1929 if (!extract_cu_value (bytes, &offset)
1930 || !extract_cu_value (bytes + 8, &type_offset))
1931 return 0;
1932 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1933 bytes += 3 * 8;
1934
1935 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1936 struct signatured_type);
1937 type_sig->signature = signature;
1fd400ff 1938 type_sig->type_offset = type_offset;
b0df02fd 1939 type_sig->per_cu.debug_types_section = section;
1fd400ff
TT
1940 type_sig->per_cu.offset = offset;
1941 type_sig->per_cu.objfile = objfile;
1942 type_sig->per_cu.v.quick
1943 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1944 struct dwarf2_per_cu_quick_data);
1945
673bfd45 1946 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1947 *slot = type_sig;
1948
d467dd73 1949 dwarf2_per_objfile->all_type_units[i / 3] = &type_sig->per_cu;
1fd400ff
TT
1950 }
1951
673bfd45 1952 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1953
1954 return 1;
1955}
1956
9291a0cd
TT
1957/* Read the address map data from the mapped index, and use it to
1958 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1959
9291a0cd
TT
1960static void
1961create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1962{
1963 const gdb_byte *iter, *end;
1964 struct obstack temp_obstack;
1965 struct addrmap *mutable_map;
1966 struct cleanup *cleanup;
1967 CORE_ADDR baseaddr;
1968
1969 obstack_init (&temp_obstack);
1970 cleanup = make_cleanup_obstack_free (&temp_obstack);
1971 mutable_map = addrmap_create_mutable (&temp_obstack);
1972
1973 iter = index->address_table;
1974 end = iter + index->address_table_size;
1975
1976 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1977
1978 while (iter < end)
1979 {
1980 ULONGEST hi, lo, cu_index;
1981 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1982 iter += 8;
1983 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1984 iter += 8;
1985 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1986 iter += 4;
1987
1988 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1989 dw2_get_cu (cu_index));
9291a0cd
TT
1990 }
1991
1992 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1993 &objfile->objfile_obstack);
1994 do_cleanups (cleanup);
1995}
1996
59d7bcaf
JK
1997/* The hash function for strings in the mapped index. This is the same as
1998 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1999 implementation. This is necessary because the hash function is tied to the
2000 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2001 SYMBOL_HASH_NEXT.
2002
2003 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2004
9291a0cd 2005static hashval_t
559a7a62 2006mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2007{
2008 const unsigned char *str = (const unsigned char *) p;
2009 hashval_t r = 0;
2010 unsigned char c;
2011
2012 while ((c = *str++) != 0)
559a7a62
JK
2013 {
2014 if (index_version >= 5)
2015 c = tolower (c);
2016 r = r * 67 + c - 113;
2017 }
9291a0cd
TT
2018
2019 return r;
2020}
2021
2022/* Find a slot in the mapped index INDEX for the object named NAME.
2023 If NAME is found, set *VEC_OUT to point to the CU vector in the
2024 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2025
9291a0cd
TT
2026static int
2027find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2028 offset_type **vec_out)
2029{
0cf03b49
JK
2030 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2031 offset_type hash;
9291a0cd 2032 offset_type slot, step;
559a7a62 2033 int (*cmp) (const char *, const char *);
9291a0cd 2034
0cf03b49
JK
2035 if (current_language->la_language == language_cplus
2036 || current_language->la_language == language_java
2037 || current_language->la_language == language_fortran)
2038 {
2039 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2040 not contain any. */
2041 const char *paren = strchr (name, '(');
2042
2043 if (paren)
2044 {
2045 char *dup;
2046
2047 dup = xmalloc (paren - name + 1);
2048 memcpy (dup, name, paren - name);
2049 dup[paren - name] = 0;
2050
2051 make_cleanup (xfree, dup);
2052 name = dup;
2053 }
2054 }
2055
559a7a62
JK
2056 /* Index version 4 did not support case insensitive searches. But the
2057 indexes for case insensitive languages are built in lowercase, therefore
2058 simulate our NAME being searched is also lowercased. */
2059 hash = mapped_index_string_hash ((index->version == 4
2060 && case_sensitivity == case_sensitive_off
2061 ? 5 : index->version),
2062 name);
2063
3876f04e
DE
2064 slot = hash & (index->symbol_table_slots - 1);
2065 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2066 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2067
2068 for (;;)
2069 {
2070 /* Convert a slot number to an offset into the table. */
2071 offset_type i = 2 * slot;
2072 const char *str;
3876f04e 2073 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2074 {
2075 do_cleanups (back_to);
2076 return 0;
2077 }
9291a0cd 2078
3876f04e 2079 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2080 if (!cmp (name, str))
9291a0cd
TT
2081 {
2082 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2083 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2084 do_cleanups (back_to);
9291a0cd
TT
2085 return 1;
2086 }
2087
3876f04e 2088 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2089 }
2090}
2091
2092/* Read the index file. If everything went ok, initialize the "quick"
2093 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2094
9291a0cd
TT
2095static int
2096dwarf2_read_index (struct objfile *objfile)
2097{
9291a0cd
TT
2098 char *addr;
2099 struct mapped_index *map;
b3b272e1 2100 offset_type *metadata;
ac0b195c
KW
2101 const gdb_byte *cu_list;
2102 const gdb_byte *types_list = NULL;
2103 offset_type version, cu_list_elements;
2104 offset_type types_list_elements = 0;
1fd400ff 2105 int i;
9291a0cd 2106
9e0ac564 2107 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2108 return 0;
82430852
JK
2109
2110 /* Older elfutils strip versions could keep the section in the main
2111 executable while splitting it for the separate debug info file. */
2112 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2113 & SEC_HAS_CONTENTS) == 0)
2114 return 0;
2115
9291a0cd
TT
2116 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2117
2118 addr = dwarf2_per_objfile->gdb_index.buffer;
2119 /* Version check. */
1fd400ff 2120 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2121 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2122 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2123 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2124 indices. Index version 4 uses a different hash function than index
2125 version 5 and later. */
831adc1f 2126 if (version < 4)
9291a0cd 2127 return 0;
594e8718
JK
2128 /* Indexes with higher version than the one supported by GDB may be no
2129 longer backward compatible. */
559a7a62 2130 if (version > 5)
594e8718 2131 return 0;
9291a0cd
TT
2132
2133 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2134 map->version = version;
b3b272e1 2135 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2136
2137 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2138
2139 i = 0;
2140 cu_list = addr + MAYBE_SWAP (metadata[i]);
2141 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2142 / 8);
1fd400ff
TT
2143 ++i;
2144
987d643c
TT
2145 types_list = addr + MAYBE_SWAP (metadata[i]);
2146 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2147 - MAYBE_SWAP (metadata[i]))
2148 / 8);
2149 ++i;
1fd400ff
TT
2150
2151 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2152 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2153 - MAYBE_SWAP (metadata[i]));
2154 ++i;
2155
3876f04e
DE
2156 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2157 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2158 - MAYBE_SWAP (metadata[i]))
2159 / (2 * sizeof (offset_type)));
1fd400ff 2160 ++i;
9291a0cd 2161
1fd400ff
TT
2162 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2163
2164 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2165 return 0;
2166
8b70b953
TT
2167 if (types_list_elements)
2168 {
2169 struct dwarf2_section_info *section;
2170
2171 /* We can only handle a single .debug_types when we have an
2172 index. */
2173 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2174 return 0;
2175
2176 section = VEC_index (dwarf2_section_info_def,
2177 dwarf2_per_objfile->types, 0);
2178
2179 if (!create_signatured_type_table_from_index (objfile, section,
2180 types_list,
2181 types_list_elements))
2182 return 0;
2183 }
9291a0cd
TT
2184
2185 create_addrmap_from_index (objfile, map);
2186
2187 dwarf2_per_objfile->index_table = map;
2188 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2189 dwarf2_per_objfile->quick_file_names_table =
2190 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2191
2192 return 1;
2193}
2194
2195/* A helper for the "quick" functions which sets the global
2196 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2197
9291a0cd
TT
2198static void
2199dw2_setup (struct objfile *objfile)
2200{
2201 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2202 gdb_assert (dwarf2_per_objfile);
2203}
2204
2205/* A helper for the "quick" functions which attempts to read the line
2206 table for THIS_CU. */
2fdf6df6 2207
7b9f3c50
DE
2208static struct quick_file_names *
2209dw2_get_file_names (struct objfile *objfile,
2210 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2211{
2212 bfd *abfd = objfile->obfd;
7b9f3c50 2213 struct line_header *lh;
9291a0cd
TT
2214 struct attribute *attr;
2215 struct cleanup *cleanups;
2216 struct die_info *comp_unit_die;
36374493 2217 struct dwarf2_section_info* sec;
9ff913ba 2218 gdb_byte *info_ptr;
9291a0cd
TT
2219 int has_children, i;
2220 struct dwarf2_cu cu;
9ff913ba 2221 unsigned int bytes_read;
9291a0cd
TT
2222 struct die_reader_specs reader_specs;
2223 char *name, *comp_dir;
7b9f3c50
DE
2224 void **slot;
2225 struct quick_file_names *qfn;
2226 unsigned int line_offset;
9291a0cd 2227
7b9f3c50
DE
2228 if (this_cu->v.quick->file_names != NULL)
2229 return this_cu->v.quick->file_names;
2230 /* If we know there is no line data, no point in looking again. */
2231 if (this_cu->v.quick->no_file_data)
2232 return NULL;
9291a0cd 2233
23745b47 2234 init_one_comp_unit (&cu, this_cu);
9291a0cd
TT
2235 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2236
b0df02fd
DE
2237 if (this_cu->debug_types_section)
2238 sec = this_cu->debug_types_section;
36374493
DE
2239 else
2240 sec = &dwarf2_per_objfile->info;
2241 dwarf2_read_section (objfile, sec);
9ff913ba 2242 info_ptr = sec->buffer + this_cu->offset;
9291a0cd 2243
9ff913ba
DE
2244 info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2245 this_cu->debug_types_section != NULL);
9291a0cd 2246
6caca83c 2247 /* Skip dummy compilation units. */
9ff913ba 2248 if (info_ptr >= (sec->buffer + sec->size)
6caca83c
CC
2249 || peek_abbrev_code (abfd, info_ptr) == 0)
2250 {
2251 do_cleanups (cleanups);
2252 return NULL;
2253 }
2254
e5fe5e75 2255 dwarf2_read_abbrevs (&cu);
9291a0cd
TT
2256 make_cleanup (dwarf2_free_abbrev_table, &cu);
2257
9291a0cd 2258 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2259 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2260 &has_children);
9291a0cd 2261
7b9f3c50
DE
2262 lh = NULL;
2263 slot = NULL;
2264 line_offset = 0;
9291a0cd
TT
2265 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2266 if (attr)
2267 {
7b9f3c50
DE
2268 struct quick_file_names find_entry;
2269
2270 line_offset = DW_UNSND (attr);
2271
2272 /* We may have already read in this line header (TU line header sharing).
2273 If we have we're done. */
2274 find_entry.offset = line_offset;
2275 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2276 &find_entry, INSERT);
2277 if (*slot != NULL)
2278 {
2279 do_cleanups (cleanups);
2280 this_cu->v.quick->file_names = *slot;
2281 return *slot;
2282 }
2283
9291a0cd
TT
2284 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2285 }
2286 if (lh == NULL)
2287 {
2288 do_cleanups (cleanups);
7b9f3c50
DE
2289 this_cu->v.quick->no_file_data = 1;
2290 return NULL;
9291a0cd
TT
2291 }
2292
7b9f3c50
DE
2293 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2294 qfn->offset = line_offset;
2295 gdb_assert (slot != NULL);
2296 *slot = qfn;
9291a0cd 2297
7b9f3c50 2298 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2299
7b9f3c50
DE
2300 qfn->num_file_names = lh->num_file_names;
2301 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2302 lh->num_file_names * sizeof (char *));
9291a0cd 2303 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2304 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2305 qfn->real_names = NULL;
9291a0cd 2306
7b9f3c50 2307 free_line_header (lh);
9291a0cd 2308 do_cleanups (cleanups);
7b9f3c50
DE
2309
2310 this_cu->v.quick->file_names = qfn;
2311 return qfn;
9291a0cd
TT
2312}
2313
2314/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2315 real path for a given file name from the line table. */
2fdf6df6 2316
9291a0cd 2317static const char *
7b9f3c50
DE
2318dw2_get_real_path (struct objfile *objfile,
2319 struct quick_file_names *qfn, int index)
9291a0cd 2320{
7b9f3c50
DE
2321 if (qfn->real_names == NULL)
2322 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2323 qfn->num_file_names, sizeof (char *));
9291a0cd 2324
7b9f3c50
DE
2325 if (qfn->real_names[index] == NULL)
2326 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2327
7b9f3c50 2328 return qfn->real_names[index];
9291a0cd
TT
2329}
2330
2331static struct symtab *
2332dw2_find_last_source_symtab (struct objfile *objfile)
2333{
2334 int index;
ae2de4f8 2335
9291a0cd
TT
2336 dw2_setup (objfile);
2337 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2338 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2339}
2340
7b9f3c50
DE
2341/* Traversal function for dw2_forget_cached_source_info. */
2342
2343static int
2344dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2345{
7b9f3c50 2346 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2347
7b9f3c50 2348 if (file_data->real_names)
9291a0cd 2349 {
7b9f3c50 2350 int i;
9291a0cd 2351
7b9f3c50 2352 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2353 {
7b9f3c50
DE
2354 xfree ((void*) file_data->real_names[i]);
2355 file_data->real_names[i] = NULL;
9291a0cd
TT
2356 }
2357 }
7b9f3c50
DE
2358
2359 return 1;
2360}
2361
2362static void
2363dw2_forget_cached_source_info (struct objfile *objfile)
2364{
2365 dw2_setup (objfile);
2366
2367 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2368 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2369}
2370
f8eba3c6
TT
2371/* Helper function for dw2_map_symtabs_matching_filename that expands
2372 the symtabs and calls the iterator. */
2373
2374static int
2375dw2_map_expand_apply (struct objfile *objfile,
2376 struct dwarf2_per_cu_data *per_cu,
2377 const char *name,
2378 const char *full_path, const char *real_path,
2379 int (*callback) (struct symtab *, void *),
2380 void *data)
2381{
2382 struct symtab *last_made = objfile->symtabs;
2383
2384 /* Don't visit already-expanded CUs. */
2385 if (per_cu->v.quick->symtab)
2386 return 0;
2387
2388 /* This may expand more than one symtab, and we want to iterate over
2389 all of them. */
a0f42c21 2390 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2391
2392 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2393 objfile->symtabs, last_made);
2394}
2395
2396/* Implementation of the map_symtabs_matching_filename method. */
2397
9291a0cd 2398static int
f8eba3c6
TT
2399dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2400 const char *full_path, const char *real_path,
2401 int (*callback) (struct symtab *, void *),
2402 void *data)
9291a0cd
TT
2403{
2404 int i;
c011a4f4 2405 const char *name_basename = lbasename (name);
4aac40c8
TT
2406 int name_len = strlen (name);
2407 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2408
2409 dw2_setup (objfile);
ae2de4f8 2410
1fd400ff 2411 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2412 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2413 {
2414 int j;
e254ef6a 2415 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2416 struct quick_file_names *file_data;
9291a0cd 2417
3d7bb9d9 2418 /* We only need to look at symtabs not already expanded. */
e254ef6a 2419 if (per_cu->v.quick->symtab)
9291a0cd
TT
2420 continue;
2421
7b9f3c50
DE
2422 file_data = dw2_get_file_names (objfile, per_cu);
2423 if (file_data == NULL)
9291a0cd
TT
2424 continue;
2425
7b9f3c50 2426 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2427 {
7b9f3c50 2428 const char *this_name = file_data->file_names[j];
9291a0cd 2429
4aac40c8
TT
2430 if (FILENAME_CMP (name, this_name) == 0
2431 || (!is_abs && compare_filenames_for_search (this_name,
2432 name, name_len)))
9291a0cd 2433 {
f8eba3c6
TT
2434 if (dw2_map_expand_apply (objfile, per_cu,
2435 name, full_path, real_path,
2436 callback, data))
2437 return 1;
9291a0cd
TT
2438 }
2439
4aac40c8
TT
2440 {
2441 if (dw2_map_expand_apply (objfile, per_cu,
2442 name, full_path, real_path,
2443 callback, data))
2444 return 1;
2445 }
9291a0cd 2446
c011a4f4
DE
2447 /* Before we invoke realpath, which can get expensive when many
2448 files are involved, do a quick comparison of the basenames. */
2449 if (! basenames_may_differ
2450 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2451 continue;
2452
9291a0cd
TT
2453 if (full_path != NULL)
2454 {
7b9f3c50
DE
2455 const char *this_real_name = dw2_get_real_path (objfile,
2456 file_data, j);
9291a0cd 2457
7b9f3c50 2458 if (this_real_name != NULL
4aac40c8
TT
2459 && (FILENAME_CMP (full_path, this_real_name) == 0
2460 || (!is_abs
2461 && compare_filenames_for_search (this_real_name,
2462 name, name_len))))
9291a0cd 2463 {
f8eba3c6
TT
2464 if (dw2_map_expand_apply (objfile, per_cu,
2465 name, full_path, real_path,
2466 callback, data))
2467 return 1;
9291a0cd
TT
2468 }
2469 }
2470
2471 if (real_path != NULL)
2472 {
7b9f3c50
DE
2473 const char *this_real_name = dw2_get_real_path (objfile,
2474 file_data, j);
9291a0cd 2475
7b9f3c50 2476 if (this_real_name != NULL
4aac40c8
TT
2477 && (FILENAME_CMP (real_path, this_real_name) == 0
2478 || (!is_abs
2479 && compare_filenames_for_search (this_real_name,
2480 name, name_len))))
9291a0cd 2481 {
f8eba3c6
TT
2482 if (dw2_map_expand_apply (objfile, per_cu,
2483 name, full_path, real_path,
2484 callback, data))
2485 return 1;
9291a0cd
TT
2486 }
2487 }
2488 }
2489 }
2490
9291a0cd
TT
2491 return 0;
2492}
2493
2494static struct symtab *
2495dw2_lookup_symbol (struct objfile *objfile, int block_index,
2496 const char *name, domain_enum domain)
2497{
774b6a14 2498 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2499 instead. */
2500 return NULL;
2501}
2502
2503/* A helper function that expands all symtabs that hold an object
2504 named NAME. */
2fdf6df6 2505
9291a0cd
TT
2506static void
2507dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2508{
2509 dw2_setup (objfile);
2510
ae2de4f8 2511 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2512 if (dwarf2_per_objfile->index_table)
2513 {
2514 offset_type *vec;
2515
2516 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2517 name, &vec))
2518 {
2519 offset_type i, len = MAYBE_SWAP (*vec);
2520 for (i = 0; i < len; ++i)
2521 {
2522 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2523 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2524
a0f42c21 2525 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2526 }
2527 }
2528 }
2529}
2530
774b6a14
TT
2531static void
2532dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2533 enum block_enum block_kind, const char *name,
774b6a14 2534 domain_enum domain)
9291a0cd 2535{
774b6a14 2536 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2537}
2538
2539static void
2540dw2_print_stats (struct objfile *objfile)
2541{
2542 int i, count;
2543
2544 dw2_setup (objfile);
2545 count = 0;
1fd400ff 2546 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2547 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2548 {
e254ef6a 2549 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2550
e254ef6a 2551 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2552 ++count;
2553 }
2554 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2555}
2556
2557static void
2558dw2_dump (struct objfile *objfile)
2559{
2560 /* Nothing worth printing. */
2561}
2562
2563static void
2564dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2565 struct section_offsets *delta)
2566{
2567 /* There's nothing to relocate here. */
2568}
2569
2570static void
2571dw2_expand_symtabs_for_function (struct objfile *objfile,
2572 const char *func_name)
2573{
2574 dw2_do_expand_symtabs_matching (objfile, func_name);
2575}
2576
2577static void
2578dw2_expand_all_symtabs (struct objfile *objfile)
2579{
2580 int i;
2581
2582 dw2_setup (objfile);
1fd400ff
TT
2583
2584 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2585 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2586 {
e254ef6a 2587 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2588
a0f42c21 2589 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2590 }
2591}
2592
2593static void
2594dw2_expand_symtabs_with_filename (struct objfile *objfile,
2595 const char *filename)
2596{
2597 int i;
2598
2599 dw2_setup (objfile);
d4637a04
DE
2600
2601 /* We don't need to consider type units here.
2602 This is only called for examining code, e.g. expand_line_sal.
2603 There can be an order of magnitude (or more) more type units
2604 than comp units, and we avoid them if we can. */
2605
2606 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2607 {
2608 int j;
e254ef6a 2609 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2610 struct quick_file_names *file_data;
9291a0cd 2611
3d7bb9d9 2612 /* We only need to look at symtabs not already expanded. */
e254ef6a 2613 if (per_cu->v.quick->symtab)
9291a0cd
TT
2614 continue;
2615
7b9f3c50
DE
2616 file_data = dw2_get_file_names (objfile, per_cu);
2617 if (file_data == NULL)
9291a0cd
TT
2618 continue;
2619
7b9f3c50 2620 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2621 {
7b9f3c50 2622 const char *this_name = file_data->file_names[j];
1ef75ecc 2623 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2624 {
a0f42c21 2625 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2626 break;
2627 }
2628 }
2629 }
2630}
2631
dd786858 2632static const char *
9291a0cd
TT
2633dw2_find_symbol_file (struct objfile *objfile, const char *name)
2634{
e254ef6a 2635 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2636 offset_type *vec;
7b9f3c50 2637 struct quick_file_names *file_data;
9291a0cd
TT
2638
2639 dw2_setup (objfile);
2640
ae2de4f8 2641 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2642 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2643 {
2644 struct symtab *s;
2645
2646 ALL_OBJFILE_SYMTABS (objfile, s)
2647 if (s->primary)
2648 {
2649 struct blockvector *bv = BLOCKVECTOR (s);
2650 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2651 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2652
2653 if (sym)
2654 return sym->symtab->filename;
2655 }
2656 return NULL;
2657 }
9291a0cd
TT
2658
2659 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2660 name, &vec))
2661 return NULL;
2662
2663 /* Note that this just looks at the very first one named NAME -- but
2664 actually we are looking for a function. find_main_filename
2665 should be rewritten so that it doesn't require a custom hook. It
2666 could just use the ordinary symbol tables. */
2667 /* vec[0] is the length, which must always be >0. */
e254ef6a 2668 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2669
7b9f3c50
DE
2670 file_data = dw2_get_file_names (objfile, per_cu);
2671 if (file_data == NULL)
9291a0cd
TT
2672 return NULL;
2673
7b9f3c50 2674 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2675}
2676
2677static void
40658b94
PH
2678dw2_map_matching_symbols (const char * name, domain_enum namespace,
2679 struct objfile *objfile, int global,
2680 int (*callback) (struct block *,
2681 struct symbol *, void *),
2edb89d3
JK
2682 void *data, symbol_compare_ftype *match,
2683 symbol_compare_ftype *ordered_compare)
9291a0cd 2684{
40658b94 2685 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2686 current language is Ada for a non-Ada objfile using GNU index. As Ada
2687 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2688}
2689
2690static void
f8eba3c6
TT
2691dw2_expand_symtabs_matching
2692 (struct objfile *objfile,
2693 int (*file_matcher) (const char *, void *),
e078317b 2694 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2695 enum search_domain kind,
2696 void *data)
9291a0cd
TT
2697{
2698 int i;
2699 offset_type iter;
4b5246aa 2700 struct mapped_index *index;
9291a0cd
TT
2701
2702 dw2_setup (objfile);
ae2de4f8
DE
2703
2704 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2705 if (!dwarf2_per_objfile->index_table)
2706 return;
4b5246aa 2707 index = dwarf2_per_objfile->index_table;
9291a0cd 2708
7b08b9eb
JK
2709 if (file_matcher != NULL)
2710 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2711 + dwarf2_per_objfile->n_type_units); ++i)
7b08b9eb
JK
2712 {
2713 int j;
2714 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2715 struct quick_file_names *file_data;
2716
2717 per_cu->v.quick->mark = 0;
3d7bb9d9
DE
2718
2719 /* We only need to look at symtabs not already expanded. */
7b08b9eb
JK
2720 if (per_cu->v.quick->symtab)
2721 continue;
2722
2723 file_data = dw2_get_file_names (objfile, per_cu);
2724 if (file_data == NULL)
2725 continue;
2726
2727 for (j = 0; j < file_data->num_file_names; ++j)
2728 {
2729 if (file_matcher (file_data->file_names[j], data))
2730 {
2731 per_cu->v.quick->mark = 1;
2732 break;
2733 }
2734 }
2735 }
9291a0cd 2736
3876f04e 2737 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2738 {
2739 offset_type idx = 2 * iter;
2740 const char *name;
2741 offset_type *vec, vec_len, vec_idx;
2742
3876f04e 2743 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2744 continue;
2745
3876f04e 2746 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2747
e078317b 2748 if (! (*name_matcher) (name, data))
9291a0cd
TT
2749 continue;
2750
2751 /* The name was matched, now expand corresponding CUs that were
2752 marked. */
4b5246aa 2753 vec = (offset_type *) (index->constant_pool
3876f04e 2754 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2755 vec_len = MAYBE_SWAP (vec[0]);
2756 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2757 {
e254ef6a 2758 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2759
e254ef6a 2760 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2761 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2762 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2763 }
2764 }
2765}
2766
2767static struct symtab *
2768dw2_find_pc_sect_symtab (struct objfile *objfile,
2769 struct minimal_symbol *msymbol,
2770 CORE_ADDR pc,
2771 struct obj_section *section,
2772 int warn_if_readin)
2773{
2774 struct dwarf2_per_cu_data *data;
2775
2776 dw2_setup (objfile);
2777
2778 if (!objfile->psymtabs_addrmap)
2779 return NULL;
2780
2781 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2782 if (!data)
2783 return NULL;
2784
2785 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2786 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2787 paddress (get_objfile_arch (objfile), pc));
2788
a0f42c21 2789 return dw2_instantiate_symtab (data);
9291a0cd
TT
2790}
2791
9291a0cd 2792static void
44b13c5a 2793dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2794 void *data, int need_fullname)
9291a0cd
TT
2795{
2796 int i;
2797
2798 dw2_setup (objfile);
ae2de4f8 2799
1fd400ff 2800 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2801 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2802 {
2803 int j;
e254ef6a 2804 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2805 struct quick_file_names *file_data;
9291a0cd 2806
3d7bb9d9 2807 /* We only need to look at symtabs not already expanded. */
e254ef6a 2808 if (per_cu->v.quick->symtab)
9291a0cd
TT
2809 continue;
2810
7b9f3c50
DE
2811 file_data = dw2_get_file_names (objfile, per_cu);
2812 if (file_data == NULL)
9291a0cd
TT
2813 continue;
2814
7b9f3c50 2815 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2816 {
74e2f255
DE
2817 const char *this_real_name;
2818
2819 if (need_fullname)
2820 this_real_name = dw2_get_real_path (objfile, file_data, j);
2821 else
2822 this_real_name = NULL;
7b9f3c50 2823 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2824 }
2825 }
2826}
2827
2828static int
2829dw2_has_symbols (struct objfile *objfile)
2830{
2831 return 1;
2832}
2833
2834const struct quick_symbol_functions dwarf2_gdb_index_functions =
2835{
2836 dw2_has_symbols,
2837 dw2_find_last_source_symtab,
2838 dw2_forget_cached_source_info,
f8eba3c6 2839 dw2_map_symtabs_matching_filename,
9291a0cd 2840 dw2_lookup_symbol,
774b6a14 2841 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2842 dw2_print_stats,
2843 dw2_dump,
2844 dw2_relocate,
2845 dw2_expand_symtabs_for_function,
2846 dw2_expand_all_symtabs,
2847 dw2_expand_symtabs_with_filename,
2848 dw2_find_symbol_file,
40658b94 2849 dw2_map_matching_symbols,
9291a0cd
TT
2850 dw2_expand_symtabs_matching,
2851 dw2_find_pc_sect_symtab,
9291a0cd
TT
2852 dw2_map_symbol_filenames
2853};
2854
2855/* Initialize for reading DWARF for this objfile. Return 0 if this
2856 file will use psymtabs, or 1 if using the GNU index. */
2857
2858int
2859dwarf2_initialize_objfile (struct objfile *objfile)
2860{
2861 /* If we're about to read full symbols, don't bother with the
2862 indices. In this case we also don't care if some other debug
2863 format is making psymtabs, because they are all about to be
2864 expanded anyway. */
2865 if ((objfile->flags & OBJF_READNOW))
2866 {
2867 int i;
2868
2869 dwarf2_per_objfile->using_index = 1;
2870 create_all_comp_units (objfile);
1fd400ff 2871 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2872 dwarf2_per_objfile->quick_file_names_table =
2873 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2874
1fd400ff 2875 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2876 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2877 {
e254ef6a 2878 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2879
e254ef6a
DE
2880 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2881 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2882 }
2883
2884 /* Return 1 so that gdb sees the "quick" functions. However,
2885 these functions will be no-ops because we will have expanded
2886 all symtabs. */
2887 return 1;
2888 }
2889
2890 if (dwarf2_read_index (objfile))
2891 return 1;
2892
9291a0cd
TT
2893 return 0;
2894}
2895
2896\f
2897
dce234bc
PP
2898/* Build a partial symbol table. */
2899
2900void
f29dff0a 2901dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2902{
f29dff0a 2903 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2904 {
2905 init_psymbol_list (objfile, 1024);
2906 }
2907
d146bf1e 2908 dwarf2_build_psymtabs_hard (objfile);
c906108c 2909}
c906108c 2910
45452591
DE
2911/* Return TRUE if OFFSET is within CU_HEADER. */
2912
2913static inline int
2914offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2915{
2916 unsigned int bottom = cu_header->offset;
2917 unsigned int top = (cu_header->offset
2918 + cu_header->length
2919 + cu_header->initial_length_size);
9a619af0 2920
45452591
DE
2921 return (offset >= bottom && offset < top);
2922}
2923
93311388
DE
2924/* Read in the comp unit header information from the debug_info at info_ptr.
2925 NOTE: This leaves members offset, first_die_offset to be filled in
2926 by the caller. */
107d2387 2927
fe1b8b76 2928static gdb_byte *
107d2387 2929read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2930 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2931{
2932 int signed_addr;
891d2f0b 2933 unsigned int bytes_read;
c764a876
DE
2934
2935 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2936 cu_header->initial_length_size = bytes_read;
2937 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2938 info_ptr += bytes_read;
107d2387
AC
2939 cu_header->version = read_2_bytes (abfd, info_ptr);
2940 info_ptr += 2;
613e1657 2941 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2942 &bytes_read);
613e1657 2943 info_ptr += bytes_read;
107d2387
AC
2944 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2945 info_ptr += 1;
2946 signed_addr = bfd_get_sign_extend_vma (abfd);
2947 if (signed_addr < 0)
8e65ff28 2948 internal_error (__FILE__, __LINE__,
e2e0b3e5 2949 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2950 cu_header->signed_addr_p = signed_addr;
c764a876 2951
107d2387
AC
2952 return info_ptr;
2953}
2954
9ff913ba
DE
2955/* Subroutine of read_and_check_comp_unit_head and
2956 read_and_check_type_unit_head to simplify them.
2957 Perform various error checking on the header. */
2958
2959static void
2960error_check_comp_unit_head (struct comp_unit_head *header,
2961 struct dwarf2_section_info *section)
2962{
2963 bfd *abfd = section->asection->owner;
2964 const char *filename = bfd_get_filename (abfd);
2965
2966 if (header->version != 2 && header->version != 3 && header->version != 4)
2967 error (_("Dwarf Error: wrong version in compilation unit header "
2968 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2969 filename);
2970
2971 if (header->abbrev_offset
2972 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2973 &dwarf2_per_objfile->abbrev))
2974 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2975 "(offset 0x%lx + 6) [in module %s]"),
2976 (long) header->abbrev_offset, (long) header->offset,
2977 filename);
2978
2979 /* Cast to unsigned long to use 64-bit arithmetic when possible to
2980 avoid potential 32-bit overflow. */
2981 if (((unsigned long) header->offset
2982 + header->length + header->initial_length_size)
2983 > section->size)
2984 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2985 "(offset 0x%lx + 0) [in module %s]"),
2986 (long) header->length, (long) header->offset,
2987 filename);
2988}
2989
2990/* Read in a CU/TU header and perform some basic error checking.
2991 The contents of the header are stored in HEADER.
2992 The result is a pointer to the start of the first DIE. */
adabb602 2993
fe1b8b76 2994static gdb_byte *
9ff913ba
DE
2995read_and_check_comp_unit_head (struct comp_unit_head *header,
2996 struct dwarf2_section_info *section,
2997 gdb_byte *info_ptr,
2998 int is_debug_types_section)
72bf9492 2999{
fe1b8b76 3000 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3001 bfd *abfd = section->asection->owner;
72bf9492 3002
9ff913ba 3003 header->offset = beg_of_comp_unit - section->buffer;
adabb602 3004
72bf9492
DJ
3005 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3006
460c1c54
CC
3007 /* If we're reading a type unit, skip over the signature and
3008 type_offset fields. */
b0df02fd 3009 if (is_debug_types_section)
460c1c54
CC
3010 info_ptr += 8 /*signature*/ + header->offset_size;
3011
adabb602
DE
3012 header->first_die_offset = info_ptr - beg_of_comp_unit;
3013
9ff913ba 3014 error_check_comp_unit_head (header, section);
72bf9492
DJ
3015
3016 return info_ptr;
3017}
3018
348e048f
DE
3019/* Read in the types comp unit header information from .debug_types entry at
3020 types_ptr. The result is a pointer to one past the end of the header. */
3021
3022static gdb_byte *
9ff913ba
DE
3023read_and_check_type_unit_head (struct comp_unit_head *header,
3024 struct dwarf2_section_info *section,
3025 gdb_byte *info_ptr,
3026 ULONGEST *signature, unsigned int *type_offset)
348e048f 3027{
9ff913ba
DE
3028 gdb_byte *beg_of_comp_unit = info_ptr;
3029 bfd *abfd = section->asection->owner;
348e048f 3030
9ff913ba 3031 header->offset = beg_of_comp_unit - section->buffer;
348e048f 3032
9ff913ba 3033 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3034
9ff913ba
DE
3035 /* If we're reading a type unit, skip over the signature and
3036 type_offset fields. */
3037 if (signature != NULL)
3038 *signature = read_8_bytes (abfd, info_ptr);
3039 info_ptr += 8;
3040 if (type_offset != NULL)
3041 *type_offset = read_offset_1 (abfd, info_ptr, header->offset_size);
3042 info_ptr += header->offset_size;
3043
3044 header->first_die_offset = info_ptr - beg_of_comp_unit;
348e048f 3045
9ff913ba
DE
3046 error_check_comp_unit_head (header, section);
3047
3048 return info_ptr;
348e048f
DE
3049}
3050
aaa75496
JB
3051/* Allocate a new partial symtab for file named NAME and mark this new
3052 partial symtab as being an include of PST. */
3053
3054static void
3055dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3056 struct objfile *objfile)
3057{
3058 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3059
3060 subpst->section_offsets = pst->section_offsets;
3061 subpst->textlow = 0;
3062 subpst->texthigh = 0;
3063
3064 subpst->dependencies = (struct partial_symtab **)
3065 obstack_alloc (&objfile->objfile_obstack,
3066 sizeof (struct partial_symtab *));
3067 subpst->dependencies[0] = pst;
3068 subpst->number_of_dependencies = 1;
3069
3070 subpst->globals_offset = 0;
3071 subpst->n_global_syms = 0;
3072 subpst->statics_offset = 0;
3073 subpst->n_static_syms = 0;
3074 subpst->symtab = NULL;
3075 subpst->read_symtab = pst->read_symtab;
3076 subpst->readin = 0;
3077
3078 /* No private part is necessary for include psymtabs. This property
3079 can be used to differentiate between such include psymtabs and
10b3939b 3080 the regular ones. */
58a9656e 3081 subpst->read_symtab_private = NULL;
aaa75496
JB
3082}
3083
3084/* Read the Line Number Program data and extract the list of files
3085 included by the source file represented by PST. Build an include
d85a05f0 3086 partial symtab for each of these included files. */
aaa75496
JB
3087
3088static void
3089dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3090 struct die_info *die,
aaa75496
JB
3091 struct partial_symtab *pst)
3092{
3093 struct objfile *objfile = cu->objfile;
3094 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3095 struct line_header *lh = NULL;
3096 struct attribute *attr;
aaa75496 3097
d85a05f0
DJ
3098 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3099 if (attr)
3100 {
3101 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3102
d85a05f0
DJ
3103 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3104 }
aaa75496
JB
3105 if (lh == NULL)
3106 return; /* No linetable, so no includes. */
3107
c6da4cef 3108 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3109 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3110
3111 free_line_header (lh);
3112}
3113
348e048f
DE
3114static hashval_t
3115hash_type_signature (const void *item)
3116{
3117 const struct signatured_type *type_sig = item;
9a619af0 3118
348e048f
DE
3119 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3120 return type_sig->signature;
3121}
3122
3123static int
3124eq_type_signature (const void *item_lhs, const void *item_rhs)
3125{
3126 const struct signatured_type *lhs = item_lhs;
3127 const struct signatured_type *rhs = item_rhs;
9a619af0 3128
348e048f
DE
3129 return lhs->signature == rhs->signature;
3130}
3131
1fd400ff
TT
3132/* Allocate a hash table for signatured types. */
3133
3134static htab_t
673bfd45 3135allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3136{
3137 return htab_create_alloc_ex (41,
3138 hash_type_signature,
3139 eq_type_signature,
3140 NULL,
3141 &objfile->objfile_obstack,
3142 hashtab_obstack_allocate,
3143 dummy_obstack_deallocate);
3144}
3145
d467dd73 3146/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3147
3148static int
d467dd73 3149add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3150{
3151 struct signatured_type *sigt = *slot;
3152 struct dwarf2_per_cu_data ***datap = datum;
3153
3154 **datap = &sigt->per_cu;
3155 ++*datap;
3156
3157 return 1;
3158}
3159
d467dd73 3160/* Create the hash table of all entries in the .debug_types section(s).
348e048f
DE
3161 The result is zero if there is an error (e.g. missing .debug_types section),
3162 otherwise non-zero. */
3163
3164static int
3165create_debug_types_hash_table (struct objfile *objfile)
3166{
8b70b953 3167 htab_t types_htab = NULL;
1fd400ff 3168 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3169 int ix;
3170 struct dwarf2_section_info *section;
348e048f 3171
8b70b953 3172 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3173 {
3174 dwarf2_per_objfile->signatured_types = NULL;
3175 return 0;
3176 }
3177
8b70b953
TT
3178 for (ix = 0;
3179 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3180 ix, section);
3181 ++ix)
3182 {
3183 gdb_byte *info_ptr, *end_ptr;
348e048f 3184
8b70b953
TT
3185 dwarf2_read_section (objfile, section);
3186 info_ptr = section->buffer;
348e048f 3187
8b70b953
TT
3188 if (info_ptr == NULL)
3189 continue;
348e048f 3190
8b70b953
TT
3191 if (types_htab == NULL)
3192 types_htab = allocate_signatured_type_table (objfile);
348e048f 3193
8b70b953
TT
3194 if (dwarf2_die_debug)
3195 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3196
3197 end_ptr = info_ptr + section->size;
3198 while (info_ptr < end_ptr)
3199 {
3200 unsigned int offset;
8b70b953 3201 unsigned int type_offset;
8b70b953
TT
3202 ULONGEST signature;
3203 struct signatured_type *type_sig;
3204 void **slot;
3205 gdb_byte *ptr = info_ptr;
9ff913ba 3206 struct comp_unit_head header;
348e048f 3207
8b70b953 3208 offset = ptr - section->buffer;
348e048f 3209
8b70b953 3210 /* We need to read the type's signature in order to build the hash
9ff913ba 3211 table, but we don't need anything else just yet. */
348e048f 3212
9ff913ba
DE
3213 ptr = read_and_check_type_unit_head (&header, section, ptr,
3214 &signature, &type_offset);
6caca83c
CC
3215
3216 /* Skip dummy type units. */
3217 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3218 {
9ff913ba 3219 info_ptr = info_ptr + header.initial_length_size + header.length;
6caca83c
CC
3220 continue;
3221 }
8b70b953
TT
3222
3223 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3224 memset (type_sig, 0, sizeof (*type_sig));
3225 type_sig->signature = signature;
3226 type_sig->type_offset = type_offset;
3227 type_sig->per_cu.objfile = objfile;
b0df02fd 3228 type_sig->per_cu.debug_types_section = section;
8b70b953
TT
3229 type_sig->per_cu.offset = offset;
3230
3231 slot = htab_find_slot (types_htab, type_sig, INSERT);
3232 gdb_assert (slot != NULL);
3233 if (*slot != NULL)
3234 {
3235 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3236
8b70b953
TT
3237 complaint (&symfile_complaints,
3238 _("debug type entry at offset 0x%x is duplicate to the "
3239 "entry at offset 0x%x, signature 0x%s"),
3240 offset, dup_sig->per_cu.offset,
3241 phex (signature, sizeof (signature)));
3242 gdb_assert (signature == dup_sig->signature);
3243 }
3244 *slot = type_sig;
348e048f 3245
8b70b953
TT
3246 if (dwarf2_die_debug)
3247 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3248 offset, phex (signature, sizeof (signature)));
348e048f 3249
9ff913ba 3250 info_ptr = info_ptr + header.initial_length_size + header.length;
8b70b953 3251 }
348e048f
DE
3252 }
3253
3254 dwarf2_per_objfile->signatured_types = types_htab;
3255
d467dd73
DE
3256 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3257 dwarf2_per_objfile->all_type_units
1fd400ff 3258 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3259 dwarf2_per_objfile->n_type_units
1fd400ff 3260 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3261 iter = &dwarf2_per_objfile->all_type_units[0];
3262 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3263 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3264 == dwarf2_per_objfile->n_type_units);
1fd400ff 3265
348e048f
DE
3266 return 1;
3267}
3268
3269/* Lookup a signature based type.
3270 Returns NULL if SIG is not present in the table. */
3271
3272static struct signatured_type *
3273lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3274{
3275 struct signatured_type find_entry, *entry;
3276
3277 if (dwarf2_per_objfile->signatured_types == NULL)
3278 {
3279 complaint (&symfile_complaints,
55f1336d 3280 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3281 return 0;
3282 }
3283
3284 find_entry.signature = sig;
3285 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3286 return entry;
3287}
3288
d85a05f0
DJ
3289/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3290
3291static void
3292init_cu_die_reader (struct die_reader_specs *reader,
3293 struct dwarf2_cu *cu)
3294{
3295 reader->abfd = cu->objfile->obfd;
3296 reader->cu = cu;
b0df02fd 3297 if (cu->per_cu->debug_types_section)
be391dca 3298 {
b0df02fd
DE
3299 gdb_assert (cu->per_cu->debug_types_section->readin);
3300 reader->buffer = cu->per_cu->debug_types_section->buffer;
be391dca 3301 }
d85a05f0 3302 else
be391dca
TT
3303 {
3304 gdb_assert (dwarf2_per_objfile->info.readin);
3305 reader->buffer = dwarf2_per_objfile->info.buffer;
3306 }
d85a05f0
DJ
3307}
3308
3309/* Find the base address of the compilation unit for range lists and
3310 location lists. It will normally be specified by DW_AT_low_pc.
3311 In DWARF-3 draft 4, the base address could be overridden by
3312 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3313 compilation units with discontinuous ranges. */
3314
3315static void
3316dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3317{
3318 struct attribute *attr;
3319
3320 cu->base_known = 0;
3321 cu->base_address = 0;
3322
3323 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3324 if (attr)
3325 {
3326 cu->base_address = DW_ADDR (attr);
3327 cu->base_known = 1;
3328 }
3329 else
3330 {
3331 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3332 if (attr)
3333 {
3334 cu->base_address = DW_ADDR (attr);
3335 cu->base_known = 1;
3336 }
3337 }
3338}
3339
348e048f
DE
3340/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3341 to combine the common parts.
21b2bd31
DE
3342 Process compilation unit THIS_CU for a psymtab.
3343 SECTION is the section the CU/TU comes from,
3344 either .debug_info or .debug_types. */
aaa75496 3345
21b2bd31 3346void
a0f42c21 3347process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
21b2bd31
DE
3348 struct dwarf2_section_info *section,
3349 int is_debug_types_section)
c906108c 3350{
a0f42c21 3351 struct objfile *objfile = this_cu->objfile;
c906108c 3352 bfd *abfd = objfile->obfd;
21b2bd31
DE
3353 gdb_byte *buffer = section->buffer;
3354 gdb_byte *info_ptr = buffer + this_cu->offset;
3355 unsigned int buffer_size = section->size;
93311388 3356 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3357 struct die_info *comp_unit_die;
c906108c 3358 struct partial_symtab *pst;
5734ee8b 3359 CORE_ADDR baseaddr;
93311388
DE
3360 struct cleanup *back_to_inner;
3361 struct dwarf2_cu cu;
d85a05f0
DJ
3362 int has_children, has_pc_info;
3363 struct attribute *attr;
d85a05f0
DJ
3364 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3365 struct die_reader_specs reader_specs;
3e2a0cee 3366 const char *filename;
c906108c 3367
23745b47
DE
3368 /* If this compilation unit was already read in, free the
3369 cached copy in order to read it in again. This is
3370 necessary because we skipped some symbols when we first
3371 read in the compilation unit (see load_partial_dies).
3372 This problem could be avoided, but the benefit is
3373 unclear. */
3374 if (this_cu->cu != NULL)
3375 free_one_cached_comp_unit (this_cu->cu);
3376
3377 /* Note that this is a pointer to our stack frame, being
3378 added to a global data structure. It will be cleaned up
3379 in free_stack_comp_unit when we finish with this
3380 compilation unit. */
3381 init_one_comp_unit (&cu, this_cu);
93311388 3382 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3383
9ff913ba
DE
3384 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3385 is_debug_types_section);
10b3939b 3386
6caca83c
CC
3387 /* Skip dummy compilation units. */
3388 if (info_ptr >= buffer + buffer_size
3389 || peek_abbrev_code (abfd, info_ptr) == 0)
3390 {
6caca83c 3391 do_cleanups (back_to_inner);
21b2bd31 3392 return;
6caca83c
CC
3393 }
3394
93311388 3395 cu.list_in_scope = &file_symbols;
af703f96 3396
93311388 3397 /* Read the abbrevs for this compilation unit into a table. */
e5fe5e75 3398 dwarf2_read_abbrevs (&cu);
93311388 3399 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3400
93311388 3401 /* Read the compilation unit die. */
d85a05f0
DJ
3402 init_cu_die_reader (&reader_specs, &cu);
3403 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3404 &has_children);
93311388 3405
21b2bd31 3406 if (is_debug_types_section)
348e048f 3407 {
b3c8eb43
JK
3408 /* LENGTH has not been set yet for type units. */
3409 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3410 this_cu->length = cu.header.length + cu.header.initial_length_size;
3411 }
d85a05f0 3412 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3413 {
93311388 3414 do_cleanups (back_to_inner);
21b2bd31 3415 return;
93311388 3416 }
72bf9492 3417
9816fde3 3418 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3419
93311388 3420 /* Allocate a new partial symbol table structure. */
d85a05f0 3421 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3422 if (attr == NULL || !DW_STRING (attr))
3423 filename = "";
3424 else
3425 filename = DW_STRING (attr);
93311388 3426 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3427 filename,
93311388
DE
3428 /* TEXTLOW and TEXTHIGH are set below. */
3429 0,
3430 objfile->global_psymbols.next,
3431 objfile->static_psymbols.next);
9750bca9 3432 pst->psymtabs_addrmap_supported = 1;
72bf9492 3433
d85a05f0
DJ
3434 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3435 if (attr != NULL)
3436 pst->dirname = DW_STRING (attr);
72bf9492 3437
e38df1d0 3438 pst->read_symtab_private = this_cu;
72bf9492 3439
93311388 3440 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3441
0963b4bd 3442 /* Store the function that reads in the rest of the symbol table. */
93311388 3443 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3444
9291a0cd 3445 this_cu->v.psymtab = pst;
c906108c 3446
d85a05f0
DJ
3447 dwarf2_find_base_address (comp_unit_die, &cu);
3448
93311388
DE
3449 /* Possibly set the default values of LOWPC and HIGHPC from
3450 `DW_AT_ranges'. */
d85a05f0
DJ
3451 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3452 &best_highpc, &cu, pst);
3453 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3454 /* Store the contiguous range if it is not empty; it can be empty for
3455 CUs with no code. */
3456 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3457 best_lowpc + baseaddr,
3458 best_highpc + baseaddr - 1, pst);
93311388
DE
3459
3460 /* Check if comp unit has_children.
3461 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3462 If not, there's no more debug_info for this comp unit. */
d85a05f0 3463 if (has_children)
93311388
DE
3464 {
3465 struct partial_die_info *first_die;
3466 CORE_ADDR lowpc, highpc;
31ffec48 3467
93311388
DE
3468 lowpc = ((CORE_ADDR) -1);
3469 highpc = ((CORE_ADDR) 0);
c906108c 3470
93311388 3471 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3472
93311388 3473 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3474 ! has_pc_info, &cu);
57c22c6c 3475
93311388
DE
3476 /* If we didn't find a lowpc, set it to highpc to avoid
3477 complaints from `maint check'. */
3478 if (lowpc == ((CORE_ADDR) -1))
3479 lowpc = highpc;
10b3939b 3480
93311388
DE
3481 /* If the compilation unit didn't have an explicit address range,
3482 then use the information extracted from its child dies. */
d85a05f0 3483 if (! has_pc_info)
93311388 3484 {
d85a05f0
DJ
3485 best_lowpc = lowpc;
3486 best_highpc = highpc;
93311388
DE
3487 }
3488 }
d85a05f0
DJ
3489 pst->textlow = best_lowpc + baseaddr;
3490 pst->texthigh = best_highpc + baseaddr;
c906108c 3491
93311388
DE
3492 pst->n_global_syms = objfile->global_psymbols.next -
3493 (objfile->global_psymbols.list + pst->globals_offset);
3494 pst->n_static_syms = objfile->static_psymbols.next -
3495 (objfile->static_psymbols.list + pst->statics_offset);
3496 sort_pst_symbols (pst);
c906108c 3497
21b2bd31 3498 if (is_debug_types_section)
348e048f
DE
3499 {
3500 /* It's not clear we want to do anything with stmt lists here.
3501 Waiting to see what gcc ultimately does. */
3502 }
d85a05f0 3503 else
93311388
DE
3504 {
3505 /* Get the list of files included in the current compilation unit,
3506 and build a psymtab for each of them. */
d85a05f0 3507 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3508 }
ae038cb0 3509
93311388 3510 do_cleanups (back_to_inner);
93311388 3511}
ff013f42 3512
348e048f
DE
3513/* Traversal function for htab_traverse_noresize.
3514 Process one .debug_types comp-unit. */
3515
3516static int
3517process_type_comp_unit (void **slot, void *info)
3518{
3519 struct signatured_type *entry = (struct signatured_type *) *slot;
348e048f
DE
3520 struct dwarf2_per_cu_data *this_cu;
3521
a0f42c21 3522 gdb_assert (info == NULL);
348e048f 3523 this_cu = &entry->per_cu;
348e048f 3524
b0df02fd 3525 gdb_assert (this_cu->debug_types_section->readin);
21b2bd31 3526 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
348e048f
DE
3527
3528 return 1;
3529}
3530
3531/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3532 Build partial symbol tables for the .debug_types comp-units. */
3533
3534static void
3535build_type_psymtabs (struct objfile *objfile)
3536{
3537 if (! create_debug_types_hash_table (objfile))
3538 return;
3539
3540 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
a0f42c21 3541 process_type_comp_unit, NULL);
348e048f
DE
3542}
3543
60606b2c
TT
3544/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3545
3546static void
3547psymtabs_addrmap_cleanup (void *o)
3548{
3549 struct objfile *objfile = o;
ec61707d 3550
60606b2c
TT
3551 objfile->psymtabs_addrmap = NULL;
3552}
3553
93311388
DE
3554/* Build the partial symbol table by doing a quick pass through the
3555 .debug_info and .debug_abbrev sections. */
72bf9492 3556
93311388 3557static void
c67a9c90 3558dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3559{
60606b2c
TT
3560 struct cleanup *back_to, *addrmap_cleanup;
3561 struct obstack temp_obstack;
21b2bd31 3562 int i;
93311388 3563
98bfdba5
PA
3564 dwarf2_per_objfile->reading_partial_symbols = 1;
3565
be391dca 3566 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 3567
93311388
DE
3568 /* Any cached compilation units will be linked by the per-objfile
3569 read_in_chain. Make sure to free them when we're done. */
3570 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3571
348e048f
DE
3572 build_type_psymtabs (objfile);
3573
93311388 3574 create_all_comp_units (objfile);
c906108c 3575
60606b2c
TT
3576 /* Create a temporary address map on a temporary obstack. We later
3577 copy this to the final obstack. */
3578 obstack_init (&temp_obstack);
3579 make_cleanup_obstack_free (&temp_obstack);
3580 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3581 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3582
21b2bd31 3583 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 3584 {
21b2bd31 3585 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 3586
21b2bd31 3587 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
c906108c 3588 }
ff013f42
JK
3589
3590 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3591 &objfile->objfile_obstack);
60606b2c 3592 discard_cleanups (addrmap_cleanup);
ff013f42 3593
ae038cb0
DJ
3594 do_cleanups (back_to);
3595}
3596
93311388 3597/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3598
3599static void
a0f42c21 3600load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
ae038cb0 3601{
a0f42c21 3602 struct objfile *objfile = this_cu->objfile;
ae038cb0 3603 bfd *abfd = objfile->obfd;
adabb602 3604 gdb_byte *info_ptr;
d85a05f0 3605 struct die_info *comp_unit_die;
ae038cb0 3606 struct dwarf2_cu *cu;
1d9ec526 3607 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3608 int has_children;
3609 struct die_reader_specs reader_specs;
98bfdba5 3610 int read_cu = 0;
9ff913ba 3611 struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
ae038cb0 3612
b0df02fd 3613 gdb_assert (! this_cu->debug_types_section);
348e048f 3614
9ff913ba
DE
3615 gdb_assert (section->readin);
3616 info_ptr = section->buffer + this_cu->offset;
ae038cb0 3617
98bfdba5
PA
3618 if (this_cu->cu == NULL)
3619 {
9816fde3 3620 cu = xmalloc (sizeof (*cu));
23745b47 3621 init_one_comp_unit (cu, this_cu);
ae038cb0 3622
98bfdba5 3623 read_cu = 1;
ae038cb0 3624
98bfdba5 3625 /* If an error occurs while loading, release our storage. */
68dc6402 3626 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
328c9494 3627
9ff913ba
DE
3628 info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3629 0);
ae038cb0 3630
6caca83c 3631 /* Skip dummy compilation units. */
9ff913ba 3632 if (info_ptr >= (section->buffer + section->size)
6caca83c
CC
3633 || peek_abbrev_code (abfd, info_ptr) == 0)
3634 {
3635 do_cleanups (free_cu_cleanup);
3636 return;
3637 }
3638
98bfdba5
PA
3639 /* Link this CU into read_in_chain. */
3640 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3641 dwarf2_per_objfile->read_in_chain = this_cu;
3642 }
3643 else
3644 {
3645 cu = this_cu->cu;
3646 info_ptr += cu->header.first_die_offset;
3647 }
ae038cb0
DJ
3648
3649 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3650 gdb_assert (cu->dwarf2_abbrevs == NULL);
e5fe5e75 3651 dwarf2_read_abbrevs (cu);
98bfdba5 3652 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3653
3654 /* Read the compilation unit die. */
d85a05f0
DJ
3655 init_cu_die_reader (&reader_specs, cu);
3656 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3657 &has_children);
ae038cb0 3658
9816fde3 3659 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3660
ae038cb0
DJ
3661 /* Check if comp unit has_children.
3662 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3663 If not, there's no more debug_info for this comp unit. */
d85a05f0 3664 if (has_children)
9ff913ba 3665 load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
ae038cb0 3666
98bfdba5
PA
3667 do_cleanups (free_abbrevs_cleanup);
3668
3669 if (read_cu)
3670 {
3671 /* We've successfully allocated this compilation unit. Let our
3672 caller clean it up when finished with it. */
3673 discard_cleanups (free_cu_cleanup);
3674 }
ae038cb0
DJ
3675}
3676
9cdd5dbd
DE
3677/* Create a list of all compilation units in OBJFILE.
3678 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
3679
3680static void
3681create_all_comp_units (struct objfile *objfile)
3682{
3683 int n_allocated;
3684 int n_comp_units;
3685 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3686 gdb_byte *info_ptr;
3687
3688 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3689 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3690
3691 n_comp_units = 0;
3692 n_allocated = 10;
3693 all_comp_units = xmalloc (n_allocated
3694 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3695
3e43a32a
MS
3696 while (info_ptr < dwarf2_per_objfile->info.buffer
3697 + dwarf2_per_objfile->info.size)
ae038cb0 3698 {
c764a876 3699 unsigned int length, initial_length_size;
ae038cb0 3700 struct dwarf2_per_cu_data *this_cu;
c764a876 3701 unsigned int offset;
ae038cb0 3702
dce234bc 3703 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3704
3705 /* Read just enough information to find out where the next
3706 compilation unit is. */
c764a876
DE
3707 length = read_initial_length (objfile->obfd, info_ptr,
3708 &initial_length_size);
ae038cb0
DJ
3709
3710 /* Save the compilation unit for later lookup. */
3711 this_cu = obstack_alloc (&objfile->objfile_obstack,
3712 sizeof (struct dwarf2_per_cu_data));
3713 memset (this_cu, 0, sizeof (*this_cu));
3714 this_cu->offset = offset;
c764a876 3715 this_cu->length = length + initial_length_size;
9291a0cd 3716 this_cu->objfile = objfile;
ae038cb0
DJ
3717
3718 if (n_comp_units == n_allocated)
3719 {
3720 n_allocated *= 2;
3721 all_comp_units = xrealloc (all_comp_units,
3722 n_allocated
3723 * sizeof (struct dwarf2_per_cu_data *));
3724 }
3725 all_comp_units[n_comp_units++] = this_cu;
3726
3727 info_ptr = info_ptr + this_cu->length;
3728 }
3729
3730 dwarf2_per_objfile->all_comp_units
3731 = obstack_alloc (&objfile->objfile_obstack,
3732 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3733 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3734 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3735 xfree (all_comp_units);
3736 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3737}
3738
5734ee8b
DJ
3739/* Process all loaded DIEs for compilation unit CU, starting at
3740 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3741 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3742 DW_AT_ranges). If NEED_PC is set, then this function will set
3743 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3744 and record the covered ranges in the addrmap. */
c906108c 3745
72bf9492
DJ
3746static void
3747scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3748 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3749{
72bf9492 3750 struct partial_die_info *pdi;
c906108c 3751
91c24f0a
DC
3752 /* Now, march along the PDI's, descending into ones which have
3753 interesting children but skipping the children of the other ones,
3754 until we reach the end of the compilation unit. */
c906108c 3755
72bf9492 3756 pdi = first_die;
91c24f0a 3757
72bf9492
DJ
3758 while (pdi != NULL)
3759 {
3760 fixup_partial_die (pdi, cu);
c906108c 3761
f55ee35c 3762 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3763 children, so we need to look at them. Ditto for anonymous
3764 enums. */
933c6fe4 3765
72bf9492 3766 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3767 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3768 {
72bf9492 3769 switch (pdi->tag)
c906108c
SS
3770 {
3771 case DW_TAG_subprogram:
5734ee8b 3772 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3773 break;
72929c62 3774 case DW_TAG_constant:
c906108c
SS
3775 case DW_TAG_variable:
3776 case DW_TAG_typedef:
91c24f0a 3777 case DW_TAG_union_type:
72bf9492 3778 if (!pdi->is_declaration)
63d06c5c 3779 {
72bf9492 3780 add_partial_symbol (pdi, cu);
63d06c5c
DC
3781 }
3782 break;
c906108c 3783 case DW_TAG_class_type:
680b30c7 3784 case DW_TAG_interface_type:
c906108c 3785 case DW_TAG_structure_type:
72bf9492 3786 if (!pdi->is_declaration)
c906108c 3787 {
72bf9492 3788 add_partial_symbol (pdi, cu);
c906108c
SS
3789 }
3790 break;
91c24f0a 3791 case DW_TAG_enumeration_type:
72bf9492
DJ
3792 if (!pdi->is_declaration)
3793 add_partial_enumeration (pdi, cu);
c906108c
SS
3794 break;
3795 case DW_TAG_base_type:
a02abb62 3796 case DW_TAG_subrange_type:
c906108c 3797 /* File scope base type definitions are added to the partial
c5aa993b 3798 symbol table. */
72bf9492 3799 add_partial_symbol (pdi, cu);
c906108c 3800 break;
d9fa45fe 3801 case DW_TAG_namespace:
5734ee8b 3802 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3803 break;
5d7cb8df
JK
3804 case DW_TAG_module:
3805 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3806 break;
c906108c
SS
3807 default:
3808 break;
3809 }
3810 }
3811
72bf9492
DJ
3812 /* If the die has a sibling, skip to the sibling. */
3813
3814 pdi = pdi->die_sibling;
3815 }
3816}
3817
3818/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3819
72bf9492 3820 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3821 name is concatenated with "::" and the partial DIE's name. For
3822 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3823 Enumerators are an exception; they use the scope of their parent
3824 enumeration type, i.e. the name of the enumeration type is not
3825 prepended to the enumerator.
91c24f0a 3826
72bf9492
DJ
3827 There are two complexities. One is DW_AT_specification; in this
3828 case "parent" means the parent of the target of the specification,
3829 instead of the direct parent of the DIE. The other is compilers
3830 which do not emit DW_TAG_namespace; in this case we try to guess
3831 the fully qualified name of structure types from their members'
3832 linkage names. This must be done using the DIE's children rather
3833 than the children of any DW_AT_specification target. We only need
3834 to do this for structures at the top level, i.e. if the target of
3835 any DW_AT_specification (if any; otherwise the DIE itself) does not
3836 have a parent. */
3837
3838/* Compute the scope prefix associated with PDI's parent, in
3839 compilation unit CU. The result will be allocated on CU's
3840 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3841 field. NULL is returned if no prefix is necessary. */
3842static char *
3843partial_die_parent_scope (struct partial_die_info *pdi,
3844 struct dwarf2_cu *cu)
3845{
3846 char *grandparent_scope;
3847 struct partial_die_info *parent, *real_pdi;
91c24f0a 3848
72bf9492
DJ
3849 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3850 then this means the parent of the specification DIE. */
3851
3852 real_pdi = pdi;
72bf9492 3853 while (real_pdi->has_specification)
10b3939b 3854 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3855
3856 parent = real_pdi->die_parent;
3857 if (parent == NULL)
3858 return NULL;
3859
3860 if (parent->scope_set)
3861 return parent->scope;
3862
3863 fixup_partial_die (parent, cu);
3864
10b3939b 3865 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3866
acebe513
UW
3867 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3868 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3869 Work around this problem here. */
3870 if (cu->language == language_cplus
6e70227d 3871 && parent->tag == DW_TAG_namespace
acebe513
UW
3872 && strcmp (parent->name, "::") == 0
3873 && grandparent_scope == NULL)
3874 {
3875 parent->scope = NULL;
3876 parent->scope_set = 1;
3877 return NULL;
3878 }
3879
9c6c53f7
SA
3880 if (pdi->tag == DW_TAG_enumerator)
3881 /* Enumerators should not get the name of the enumeration as a prefix. */
3882 parent->scope = grandparent_scope;
3883 else if (parent->tag == DW_TAG_namespace
f55ee35c 3884 || parent->tag == DW_TAG_module
72bf9492
DJ
3885 || parent->tag == DW_TAG_structure_type
3886 || parent->tag == DW_TAG_class_type
680b30c7 3887 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3888 || parent->tag == DW_TAG_union_type
3889 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3890 {
3891 if (grandparent_scope == NULL)
3892 parent->scope = parent->name;
3893 else
3e43a32a
MS
3894 parent->scope = typename_concat (&cu->comp_unit_obstack,
3895 grandparent_scope,
f55ee35c 3896 parent->name, 0, cu);
72bf9492 3897 }
72bf9492
DJ
3898 else
3899 {
3900 /* FIXME drow/2004-04-01: What should we be doing with
3901 function-local names? For partial symbols, we should probably be
3902 ignoring them. */
3903 complaint (&symfile_complaints,
e2e0b3e5 3904 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3905 parent->tag, pdi->offset);
3906 parent->scope = grandparent_scope;
c906108c
SS
3907 }
3908
72bf9492
DJ
3909 parent->scope_set = 1;
3910 return parent->scope;
3911}
3912
3913/* Return the fully scoped name associated with PDI, from compilation unit
3914 CU. The result will be allocated with malloc. */
3915static char *
3916partial_die_full_name (struct partial_die_info *pdi,
3917 struct dwarf2_cu *cu)
3918{
3919 char *parent_scope;
3920
98bfdba5
PA
3921 /* If this is a template instantiation, we can not work out the
3922 template arguments from partial DIEs. So, unfortunately, we have
3923 to go through the full DIEs. At least any work we do building
3924 types here will be reused if full symbols are loaded later. */
3925 if (pdi->has_template_arguments)
3926 {
3927 fixup_partial_die (pdi, cu);
3928
3929 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3930 {
3931 struct die_info *die;
3932 struct attribute attr;
3933 struct dwarf2_cu *ref_cu = cu;
3934
3935 attr.name = 0;
3936 attr.form = DW_FORM_ref_addr;
3937 attr.u.addr = pdi->offset;
3938 die = follow_die_ref (NULL, &attr, &ref_cu);
3939
3940 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3941 }
3942 }
3943
72bf9492
DJ
3944 parent_scope = partial_die_parent_scope (pdi, cu);
3945 if (parent_scope == NULL)
3946 return NULL;
3947 else
f55ee35c 3948 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3949}
3950
3951static void
72bf9492 3952add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3953{
e7c27a73 3954 struct objfile *objfile = cu->objfile;
c906108c 3955 CORE_ADDR addr = 0;
decbce07 3956 char *actual_name = NULL;
5c4e30ca 3957 const struct partial_symbol *psym = NULL;
e142c38c 3958 CORE_ADDR baseaddr;
72bf9492 3959 int built_actual_name = 0;
e142c38c
DJ
3960
3961 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3962
94af9270
KS
3963 actual_name = partial_die_full_name (pdi, cu);
3964 if (actual_name)
3965 built_actual_name = 1;
63d06c5c 3966
72bf9492
DJ
3967 if (actual_name == NULL)
3968 actual_name = pdi->name;
3969
c906108c
SS
3970 switch (pdi->tag)
3971 {
3972 case DW_TAG_subprogram:
2cfa0c8d 3973 if (pdi->is_external || cu->language == language_ada)
c906108c 3974 {
2cfa0c8d
JB
3975 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3976 of the global scope. But in Ada, we want to be able to access
3977 nested procedures globally. So all Ada subprograms are stored
3978 in the global scope. */
f47fb265 3979 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3980 mst_text, objfile); */
f47fb265
MS
3981 add_psymbol_to_list (actual_name, strlen (actual_name),
3982 built_actual_name,
3983 VAR_DOMAIN, LOC_BLOCK,
3984 &objfile->global_psymbols,
3985 0, pdi->lowpc + baseaddr,
3986 cu->language, objfile);
c906108c
SS
3987 }
3988 else
3989 {
f47fb265 3990 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3991 mst_file_text, objfile); */
f47fb265
MS
3992 add_psymbol_to_list (actual_name, strlen (actual_name),
3993 built_actual_name,
3994 VAR_DOMAIN, LOC_BLOCK,
3995 &objfile->static_psymbols,
3996 0, pdi->lowpc + baseaddr,
3997 cu->language, objfile);
c906108c
SS
3998 }
3999 break;
72929c62
JB
4000 case DW_TAG_constant:
4001 {
4002 struct psymbol_allocation_list *list;
4003
4004 if (pdi->is_external)
4005 list = &objfile->global_psymbols;
4006 else
4007 list = &objfile->static_psymbols;
f47fb265
MS
4008 add_psymbol_to_list (actual_name, strlen (actual_name),
4009 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4010 list, 0, 0, cu->language, objfile);
72929c62
JB
4011 }
4012 break;
c906108c 4013 case DW_TAG_variable:
caac4577
JG
4014 if (pdi->locdesc)
4015 addr = decode_locdesc (pdi->locdesc, cu);
4016
4017 if (pdi->locdesc
4018 && addr == 0
4019 && !dwarf2_per_objfile->has_section_at_zero)
4020 {
4021 /* A global or static variable may also have been stripped
4022 out by the linker if unused, in which case its address
4023 will be nullified; do not add such variables into partial
4024 symbol table then. */
4025 }
4026 else if (pdi->is_external)
c906108c
SS
4027 {
4028 /* Global Variable.
4029 Don't enter into the minimal symbol tables as there is
4030 a minimal symbol table entry from the ELF symbols already.
4031 Enter into partial symbol table if it has a location
4032 descriptor or a type.
4033 If the location descriptor is missing, new_symbol will create
4034 a LOC_UNRESOLVED symbol, the address of the variable will then
4035 be determined from the minimal symbol table whenever the variable
4036 is referenced.
4037 The address for the partial symbol table entry is not
4038 used by GDB, but it comes in handy for debugging partial symbol
4039 table building. */
4040
c906108c 4041 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4042 add_psymbol_to_list (actual_name, strlen (actual_name),
4043 built_actual_name,
4044 VAR_DOMAIN, LOC_STATIC,
4045 &objfile->global_psymbols,
4046 0, addr + baseaddr,
4047 cu->language, objfile);
c906108c
SS
4048 }
4049 else
4050 {
0963b4bd 4051 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4052 if (pdi->locdesc == NULL)
decbce07
MS
4053 {
4054 if (built_actual_name)
4055 xfree (actual_name);
4056 return;
4057 }
f47fb265 4058 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4059 mst_file_data, objfile); */
f47fb265
MS
4060 add_psymbol_to_list (actual_name, strlen (actual_name),
4061 built_actual_name,
4062 VAR_DOMAIN, LOC_STATIC,
4063 &objfile->static_psymbols,
4064 0, addr + baseaddr,
4065 cu->language, objfile);
c906108c
SS
4066 }
4067 break;
4068 case DW_TAG_typedef:
4069 case DW_TAG_base_type:
a02abb62 4070 case DW_TAG_subrange_type:
38d518c9 4071 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4072 built_actual_name,
176620f1 4073 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4074 &objfile->static_psymbols,
e142c38c 4075 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4076 break;
72bf9492
DJ
4077 case DW_TAG_namespace:
4078 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4079 built_actual_name,
72bf9492
DJ
4080 VAR_DOMAIN, LOC_TYPEDEF,
4081 &objfile->global_psymbols,
4082 0, (CORE_ADDR) 0, cu->language, objfile);
4083 break;
c906108c 4084 case DW_TAG_class_type:
680b30c7 4085 case DW_TAG_interface_type:
c906108c
SS
4086 case DW_TAG_structure_type:
4087 case DW_TAG_union_type:
4088 case DW_TAG_enumeration_type:
fa4028e9
JB
4089 /* Skip external references. The DWARF standard says in the section
4090 about "Structure, Union, and Class Type Entries": "An incomplete
4091 structure, union or class type is represented by a structure,
4092 union or class entry that does not have a byte size attribute
4093 and that has a DW_AT_declaration attribute." */
4094 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4095 {
4096 if (built_actual_name)
4097 xfree (actual_name);
4098 return;
4099 }
fa4028e9 4100
63d06c5c
DC
4101 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4102 static vs. global. */
38d518c9 4103 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4104 built_actual_name,
176620f1 4105 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4106 (cu->language == language_cplus
4107 || cu->language == language_java)
63d06c5c
DC
4108 ? &objfile->global_psymbols
4109 : &objfile->static_psymbols,
e142c38c 4110 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4111
c906108c
SS
4112 break;
4113 case DW_TAG_enumerator:
38d518c9 4114 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4115 built_actual_name,
176620f1 4116 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4117 (cu->language == language_cplus
4118 || cu->language == language_java)
f6fe98ef
DJ
4119 ? &objfile->global_psymbols
4120 : &objfile->static_psymbols,
e142c38c 4121 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4122 break;
4123 default:
4124 break;
4125 }
5c4e30ca 4126
72bf9492
DJ
4127 if (built_actual_name)
4128 xfree (actual_name);
c906108c
SS
4129}
4130
5c4e30ca
DC
4131/* Read a partial die corresponding to a namespace; also, add a symbol
4132 corresponding to that namespace to the symbol table. NAMESPACE is
4133 the name of the enclosing namespace. */
91c24f0a 4134
72bf9492
DJ
4135static void
4136add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4137 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4138 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4139{
72bf9492 4140 /* Add a symbol for the namespace. */
e7c27a73 4141
72bf9492 4142 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4143
4144 /* Now scan partial symbols in that namespace. */
4145
91c24f0a 4146 if (pdi->has_children)
5734ee8b 4147 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4148}
4149
5d7cb8df
JK
4150/* Read a partial die corresponding to a Fortran module. */
4151
4152static void
4153add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4154 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4155{
f55ee35c 4156 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4157
4158 if (pdi->has_children)
4159 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4160}
4161
bc30ff58
JB
4162/* Read a partial die corresponding to a subprogram and create a partial
4163 symbol for that subprogram. When the CU language allows it, this
4164 routine also defines a partial symbol for each nested subprogram
4165 that this subprogram contains.
6e70227d 4166
bc30ff58
JB
4167 DIE my also be a lexical block, in which case we simply search
4168 recursively for suprograms defined inside that lexical block.
4169 Again, this is only performed when the CU language allows this
4170 type of definitions. */
4171
4172static void
4173add_partial_subprogram (struct partial_die_info *pdi,
4174 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4175 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4176{
4177 if (pdi->tag == DW_TAG_subprogram)
4178 {
4179 if (pdi->has_pc_info)
4180 {
4181 if (pdi->lowpc < *lowpc)
4182 *lowpc = pdi->lowpc;
4183 if (pdi->highpc > *highpc)
4184 *highpc = pdi->highpc;
5734ee8b
DJ
4185 if (need_pc)
4186 {
4187 CORE_ADDR baseaddr;
4188 struct objfile *objfile = cu->objfile;
4189
4190 baseaddr = ANOFFSET (objfile->section_offsets,
4191 SECT_OFF_TEXT (objfile));
4192 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4193 pdi->lowpc + baseaddr,
4194 pdi->highpc - 1 + baseaddr,
9291a0cd 4195 cu->per_cu->v.psymtab);
5734ee8b 4196 }
bc30ff58 4197 if (!pdi->is_declaration)
e8d05480
JB
4198 /* Ignore subprogram DIEs that do not have a name, they are
4199 illegal. Do not emit a complaint at this point, we will
4200 do so when we convert this psymtab into a symtab. */
4201 if (pdi->name)
4202 add_partial_symbol (pdi, cu);
bc30ff58
JB
4203 }
4204 }
6e70227d 4205
bc30ff58
JB
4206 if (! pdi->has_children)
4207 return;
4208
4209 if (cu->language == language_ada)
4210 {
4211 pdi = pdi->die_child;
4212 while (pdi != NULL)
4213 {
4214 fixup_partial_die (pdi, cu);
4215 if (pdi->tag == DW_TAG_subprogram
4216 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4217 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4218 pdi = pdi->die_sibling;
4219 }
4220 }
4221}
4222
91c24f0a
DC
4223/* Read a partial die corresponding to an enumeration type. */
4224
72bf9492
DJ
4225static void
4226add_partial_enumeration (struct partial_die_info *enum_pdi,
4227 struct dwarf2_cu *cu)
91c24f0a 4228{
72bf9492 4229 struct partial_die_info *pdi;
91c24f0a
DC
4230
4231 if (enum_pdi->name != NULL)
72bf9492
DJ
4232 add_partial_symbol (enum_pdi, cu);
4233
4234 pdi = enum_pdi->die_child;
4235 while (pdi)
91c24f0a 4236 {
72bf9492 4237 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4238 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4239 else
72bf9492
DJ
4240 add_partial_symbol (pdi, cu);
4241 pdi = pdi->die_sibling;
91c24f0a 4242 }
91c24f0a
DC
4243}
4244
6caca83c
CC
4245/* Return the initial uleb128 in the die at INFO_PTR. */
4246
4247static unsigned int
4248peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4249{
4250 unsigned int bytes_read;
4251
4252 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4253}
4254
4bb7a0a7
DJ
4255/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4256 Return the corresponding abbrev, or NULL if the number is zero (indicating
4257 an empty DIE). In either case *BYTES_READ will be set to the length of
4258 the initial number. */
4259
4260static struct abbrev_info *
fe1b8b76 4261peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4262 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4263{
4264 bfd *abfd = cu->objfile->obfd;
4265 unsigned int abbrev_number;
4266 struct abbrev_info *abbrev;
4267
4268 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4269
4270 if (abbrev_number == 0)
4271 return NULL;
4272
4273 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4274 if (!abbrev)
4275 {
3e43a32a
MS
4276 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4277 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4278 }
4279
4280 return abbrev;
4281}
4282
93311388
DE
4283/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4284 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4285 DIE. Any children of the skipped DIEs will also be skipped. */
4286
fe1b8b76 4287static gdb_byte *
93311388 4288skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4289{
4290 struct abbrev_info *abbrev;
4291 unsigned int bytes_read;
4292
4293 while (1)
4294 {
4295 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4296 if (abbrev == NULL)
4297 return info_ptr + bytes_read;
4298 else
93311388 4299 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4300 }
4301}
4302
93311388
DE
4303/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4304 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4305 abbrev corresponding to that skipped uleb128 should be passed in
4306 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4307 children. */
4308
fe1b8b76 4309static gdb_byte *
93311388
DE
4310skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4311 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4312{
4313 unsigned int bytes_read;
4314 struct attribute attr;
4315 bfd *abfd = cu->objfile->obfd;
4316 unsigned int form, i;
4317
4318 for (i = 0; i < abbrev->num_attrs; i++)
4319 {
4320 /* The only abbrev we care about is DW_AT_sibling. */
4321 if (abbrev->attrs[i].name == DW_AT_sibling)
4322 {
4323 read_attribute (&attr, &abbrev->attrs[i],
4324 abfd, info_ptr, cu);
4325 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4326 complaint (&symfile_complaints,
4327 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4328 else
93311388 4329 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4330 }
4331
4332 /* If it isn't DW_AT_sibling, skip this attribute. */
4333 form = abbrev->attrs[i].form;
4334 skip_attribute:
4335 switch (form)
4336 {
4bb7a0a7 4337 case DW_FORM_ref_addr:
ae411497
TT
4338 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4339 and later it is offset sized. */
4340 if (cu->header.version == 2)
4341 info_ptr += cu->header.addr_size;
4342 else
4343 info_ptr += cu->header.offset_size;
4344 break;
4345 case DW_FORM_addr:
4bb7a0a7
DJ
4346 info_ptr += cu->header.addr_size;
4347 break;
4348 case DW_FORM_data1:
4349 case DW_FORM_ref1:
4350 case DW_FORM_flag:
4351 info_ptr += 1;
4352 break;
2dc7f7b3
TT
4353 case DW_FORM_flag_present:
4354 break;
4bb7a0a7
DJ
4355 case DW_FORM_data2:
4356 case DW_FORM_ref2:
4357 info_ptr += 2;
4358 break;
4359 case DW_FORM_data4:
4360 case DW_FORM_ref4:
4361 info_ptr += 4;
4362 break;
4363 case DW_FORM_data8:
4364 case DW_FORM_ref8:
55f1336d 4365 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4366 info_ptr += 8;
4367 break;
4368 case DW_FORM_string:
9b1c24c8 4369 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4370 info_ptr += bytes_read;
4371 break;
2dc7f7b3 4372 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4373 case DW_FORM_strp:
4374 info_ptr += cu->header.offset_size;
4375 break;
2dc7f7b3 4376 case DW_FORM_exprloc:
4bb7a0a7
DJ
4377 case DW_FORM_block:
4378 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4379 info_ptr += bytes_read;
4380 break;
4381 case DW_FORM_block1:
4382 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4383 break;
4384 case DW_FORM_block2:
4385 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4386 break;
4387 case DW_FORM_block4:
4388 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4389 break;
4390 case DW_FORM_sdata:
4391 case DW_FORM_udata:
4392 case DW_FORM_ref_udata:
4393 info_ptr = skip_leb128 (abfd, info_ptr);
4394 break;
4395 case DW_FORM_indirect:
4396 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4397 info_ptr += bytes_read;
4398 /* We need to continue parsing from here, so just go back to
4399 the top. */
4400 goto skip_attribute;
4401
4402 default:
3e43a32a
MS
4403 error (_("Dwarf Error: Cannot handle %s "
4404 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4405 dwarf_form_name (form),
4406 bfd_get_filename (abfd));
4407 }
4408 }
4409
4410 if (abbrev->has_children)
93311388 4411 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4412 else
4413 return info_ptr;
4414}
4415
93311388
DE
4416/* Locate ORIG_PDI's sibling.
4417 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4418 in BUFFER. */
91c24f0a 4419
fe1b8b76 4420static gdb_byte *
93311388
DE
4421locate_pdi_sibling (struct partial_die_info *orig_pdi,
4422 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4423 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4424{
4425 /* Do we know the sibling already? */
72bf9492 4426
91c24f0a
DC
4427 if (orig_pdi->sibling)
4428 return orig_pdi->sibling;
4429
4430 /* Are there any children to deal with? */
4431
4432 if (!orig_pdi->has_children)
4433 return info_ptr;
4434
4bb7a0a7 4435 /* Skip the children the long way. */
91c24f0a 4436
93311388 4437 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4438}
4439
c906108c
SS
4440/* Expand this partial symbol table into a full symbol table. */
4441
4442static void
fba45db2 4443dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4444{
c906108c
SS
4445 if (pst != NULL)
4446 {
4447 if (pst->readin)
4448 {
3e43a32a
MS
4449 warning (_("bug: psymtab for %s is already read in."),
4450 pst->filename);
c906108c
SS
4451 }
4452 else
4453 {
4454 if (info_verbose)
4455 {
3e43a32a
MS
4456 printf_filtered (_("Reading in symbols for %s..."),
4457 pst->filename);
c906108c
SS
4458 gdb_flush (gdb_stdout);
4459 }
4460
10b3939b
DJ
4461 /* Restore our global data. */
4462 dwarf2_per_objfile = objfile_data (pst->objfile,
4463 dwarf2_objfile_data_key);
4464
b2ab525c
KB
4465 /* If this psymtab is constructed from a debug-only objfile, the
4466 has_section_at_zero flag will not necessarily be correct. We
4467 can get the correct value for this flag by looking at the data
4468 associated with the (presumably stripped) associated objfile. */
4469 if (pst->objfile->separate_debug_objfile_backlink)
4470 {
4471 struct dwarf2_per_objfile *dpo_backlink
4472 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4473 dwarf2_objfile_data_key);
9a619af0 4474
b2ab525c
KB
4475 dwarf2_per_objfile->has_section_at_zero
4476 = dpo_backlink->has_section_at_zero;
4477 }
4478
98bfdba5
PA
4479 dwarf2_per_objfile->reading_partial_symbols = 0;
4480
c906108c
SS
4481 psymtab_to_symtab_1 (pst);
4482
4483 /* Finish up the debug error message. */
4484 if (info_verbose)
a3f17187 4485 printf_filtered (_("done.\n"));
c906108c
SS
4486 }
4487 }
4488}
9cdd5dbd
DE
4489\f
4490/* Reading in full CUs. */
c906108c 4491
10b3939b
DJ
4492/* Add PER_CU to the queue. */
4493
4494static void
a0f42c21 4495queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b
DJ
4496{
4497 struct dwarf2_queue_item *item;
4498
4499 per_cu->queued = 1;
4500 item = xmalloc (sizeof (*item));
4501 item->per_cu = per_cu;
4502 item->next = NULL;
4503
4504 if (dwarf2_queue == NULL)
4505 dwarf2_queue = item;
4506 else
4507 dwarf2_queue_tail->next = item;
4508
4509 dwarf2_queue_tail = item;
4510}
4511
4512/* Process the queue. */
4513
4514static void
a0f42c21 4515process_queue (void)
10b3939b
DJ
4516{
4517 struct dwarf2_queue_item *item, *next_item;
4518
03dd20cc
DJ
4519 /* The queue starts out with one item, but following a DIE reference
4520 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4521 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4522 {
9291a0cd
TT
4523 if (dwarf2_per_objfile->using_index
4524 ? !item->per_cu->v.quick->symtab
4525 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4526 process_full_comp_unit (item->per_cu);
4527
4528 item->per_cu->queued = 0;
4529 next_item = item->next;
4530 xfree (item);
4531 }
4532
4533 dwarf2_queue_tail = NULL;
4534}
4535
4536/* Free all allocated queue entries. This function only releases anything if
4537 an error was thrown; if the queue was processed then it would have been
4538 freed as we went along. */
4539
4540static void
4541dwarf2_release_queue (void *dummy)
4542{
4543 struct dwarf2_queue_item *item, *last;
4544
4545 item = dwarf2_queue;
4546 while (item)
4547 {
4548 /* Anything still marked queued is likely to be in an
4549 inconsistent state, so discard it. */
4550 if (item->per_cu->queued)
4551 {
4552 if (item->per_cu->cu != NULL)
4553 free_one_cached_comp_unit (item->per_cu->cu);
4554 item->per_cu->queued = 0;
4555 }
4556
4557 last = item;
4558 item = item->next;
4559 xfree (last);
4560 }
4561
4562 dwarf2_queue = dwarf2_queue_tail = NULL;
4563}
4564
4565/* Read in full symbols for PST, and anything it depends on. */
4566
c906108c 4567static void
fba45db2 4568psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4569{
10b3939b 4570 struct dwarf2_per_cu_data *per_cu;
c906108c 4571 struct cleanup *back_to;
aaa75496
JB
4572 int i;
4573
4574 for (i = 0; i < pst->number_of_dependencies; i++)
4575 if (!pst->dependencies[i]->readin)
4576 {
4577 /* Inform about additional files that need to be read in. */
4578 if (info_verbose)
4579 {
a3f17187 4580 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4581 fputs_filtered (" ", gdb_stdout);
4582 wrap_here ("");
4583 fputs_filtered ("and ", gdb_stdout);
4584 wrap_here ("");
4585 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4586 wrap_here (""); /* Flush output. */
aaa75496
JB
4587 gdb_flush (gdb_stdout);
4588 }
4589 psymtab_to_symtab_1 (pst->dependencies[i]);
4590 }
4591
e38df1d0 4592 per_cu = pst->read_symtab_private;
10b3939b
DJ
4593
4594 if (per_cu == NULL)
aaa75496
JB
4595 {
4596 /* It's an include file, no symbols to read for it.
4597 Everything is in the parent symtab. */
4598 pst->readin = 1;
4599 return;
4600 }
c906108c 4601
a0f42c21 4602 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
4603}
4604
93311388 4605/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4606
93311388 4607static void
a0f42c21 4608load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b 4609{
a0f42c21 4610 struct objfile *objfile = per_cu->objfile;
31ffec48 4611 bfd *abfd = objfile->obfd;
10b3939b 4612 struct dwarf2_cu *cu;
c764a876 4613 unsigned int offset;
93311388 4614 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4615 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4616 struct attribute *attr;
98bfdba5 4617 int read_cu = 0;
6502dd73 4618
b0df02fd 4619 gdb_assert (! per_cu->debug_types_section);
348e048f 4620
c906108c 4621 /* Set local variables from the partial symbol table info. */
10b3939b 4622 offset = per_cu->offset;
6502dd73 4623
be391dca 4624 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4625 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4626 beg_of_comp_unit = info_ptr;
63d06c5c 4627
98bfdba5
PA
4628 if (per_cu->cu == NULL)
4629 {
9816fde3 4630 cu = xmalloc (sizeof (*cu));
23745b47 4631 init_one_comp_unit (cu, per_cu);
98bfdba5
PA
4632
4633 read_cu = 1;
c906108c 4634
98bfdba5 4635 /* If an error occurs while loading, release our storage. */
68dc6402 4636 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
c906108c 4637
98bfdba5
PA
4638 /* Read in the comp_unit header. */
4639 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4640
6caca83c
CC
4641 /* Skip dummy compilation units. */
4642 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4643 + dwarf2_per_objfile->info.size)
4644 || peek_abbrev_code (abfd, info_ptr) == 0)
4645 {
4646 do_cleanups (free_cu_cleanup);
4647 return;
4648 }
4649
98bfdba5
PA
4650 /* Complete the cu_header. */
4651 cu->header.offset = offset;
4652 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4653
98bfdba5 4654 /* Read the abbrevs for this compilation unit. */
e5fe5e75 4655 dwarf2_read_abbrevs (cu);
98bfdba5 4656 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4657
98bfdba5
PA
4658 /* Link this CU into read_in_chain. */
4659 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4660 dwarf2_per_objfile->read_in_chain = per_cu;
4661 }
4662 else
4663 {
4664 cu = per_cu->cu;
4665 info_ptr += cu->header.first_die_offset;
4666 }
e142c38c 4667
93311388 4668 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4669
4670 /* We try not to read any attributes in this function, because not
9cdd5dbd 4671 all CUs needed for references have been loaded yet, and symbol
10b3939b
DJ
4672 table processing isn't initialized. But we have to set the CU language,
4673 or we won't be able to build types correctly. */
9816fde3 4674 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4675
a6c727b2
DJ
4676 /* Similarly, if we do not read the producer, we can not apply
4677 producer-specific interpretation. */
4678 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4679 if (attr)
4680 cu->producer = DW_STRING (attr);
4681
98bfdba5
PA
4682 if (read_cu)
4683 {
4684 do_cleanups (free_abbrevs_cleanup);
e142c38c 4685
98bfdba5
PA
4686 /* We've successfully allocated this compilation unit. Let our
4687 caller clean it up when finished with it. */
4688 discard_cleanups (free_cu_cleanup);
4689 }
10b3939b
DJ
4690}
4691
3da10d80
KS
4692/* Add a DIE to the delayed physname list. */
4693
4694static void
4695add_to_method_list (struct type *type, int fnfield_index, int index,
4696 const char *name, struct die_info *die,
4697 struct dwarf2_cu *cu)
4698{
4699 struct delayed_method_info mi;
4700 mi.type = type;
4701 mi.fnfield_index = fnfield_index;
4702 mi.index = index;
4703 mi.name = name;
4704 mi.die = die;
4705 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4706}
4707
4708/* A cleanup for freeing the delayed method list. */
4709
4710static void
4711free_delayed_list (void *ptr)
4712{
4713 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4714 if (cu->method_list != NULL)
4715 {
4716 VEC_free (delayed_method_info, cu->method_list);
4717 cu->method_list = NULL;
4718 }
4719}
4720
4721/* Compute the physnames of any methods on the CU's method list.
4722
4723 The computation of method physnames is delayed in order to avoid the
4724 (bad) condition that one of the method's formal parameters is of an as yet
4725 incomplete type. */
4726
4727static void
4728compute_delayed_physnames (struct dwarf2_cu *cu)
4729{
4730 int i;
4731 struct delayed_method_info *mi;
4732 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4733 {
1d06ead6 4734 const char *physname;
3da10d80
KS
4735 struct fn_fieldlist *fn_flp
4736 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4737 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4738 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4739 }
4740}
4741
9cdd5dbd 4742/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
4743 already been loaded into memory. */
4744
4745static void
4746process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4747{
10b3939b 4748 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4749 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4750 CORE_ADDR lowpc, highpc;
4751 struct symtab *symtab;
3da10d80 4752 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4753 CORE_ADDR baseaddr;
4754
4755 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4756
10b3939b
DJ
4757 buildsym_init ();
4758 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4759 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4760
4761 cu->list_in_scope = &file_symbols;
c906108c
SS
4762
4763 /* Do line number decoding in read_file_scope () */
10b3939b 4764 process_die (cu->dies, cu);
c906108c 4765
3da10d80
KS
4766 /* Now that we have processed all the DIEs in the CU, all the types
4767 should be complete, and it should now be safe to compute all of the
4768 physnames. */
4769 compute_delayed_physnames (cu);
4770 do_cleanups (delayed_list_cleanup);
4771
fae299cd
DC
4772 /* Some compilers don't define a DW_AT_high_pc attribute for the
4773 compilation unit. If the DW_AT_high_pc is missing, synthesize
4774 it, by scanning the DIE's below the compilation unit. */
10b3939b 4775 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4776
613e1657 4777 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4778
8be455d7 4779 if (symtab != NULL)
c906108c 4780 {
df15bd07 4781 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4782
8be455d7
JK
4783 /* Set symtab language to language from DW_AT_language. If the
4784 compilation is from a C file generated by language preprocessors, do
4785 not set the language if it was already deduced by start_subfile. */
4786 if (!(cu->language == language_c && symtab->language != language_c))
4787 symtab->language = cu->language;
4788
4789 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4790 produce DW_AT_location with location lists but it can be possibly
4791 invalid without -fvar-tracking.
4792
4793 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4794 needed, it would be wrong due to missing DW_AT_producer there.
4795
4796 Still one can confuse GDB by using non-standard GCC compilation
4797 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4798 */
4632c0d0 4799 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4800 symtab->locations_valid = 1;
e0d00bc7
JK
4801
4802 if (gcc_4_minor >= 5)
4803 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4804
4805 symtab->call_site_htab = cu->call_site_htab;
c906108c 4806 }
9291a0cd
TT
4807
4808 if (dwarf2_per_objfile->using_index)
4809 per_cu->v.quick->symtab = symtab;
4810 else
4811 {
4812 struct partial_symtab *pst = per_cu->v.psymtab;
4813 pst->symtab = symtab;
4814 pst->readin = 1;
4815 }
c906108c
SS
4816
4817 do_cleanups (back_to);
4818}
4819
4820/* Process a die and its children. */
4821
4822static void
e7c27a73 4823process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4824{
4825 switch (die->tag)
4826 {
4827 case DW_TAG_padding:
4828 break;
4829 case DW_TAG_compile_unit:
e7c27a73 4830 read_file_scope (die, cu);
c906108c 4831 break;
348e048f
DE
4832 case DW_TAG_type_unit:
4833 read_type_unit_scope (die, cu);
4834 break;
c906108c 4835 case DW_TAG_subprogram:
c906108c 4836 case DW_TAG_inlined_subroutine:
edb3359d 4837 read_func_scope (die, cu);
c906108c
SS
4838 break;
4839 case DW_TAG_lexical_block:
14898363
L
4840 case DW_TAG_try_block:
4841 case DW_TAG_catch_block:
e7c27a73 4842 read_lexical_block_scope (die, cu);
c906108c 4843 break;
96408a79
SA
4844 case DW_TAG_GNU_call_site:
4845 read_call_site_scope (die, cu);
4846 break;
c906108c 4847 case DW_TAG_class_type:
680b30c7 4848 case DW_TAG_interface_type:
c906108c
SS
4849 case DW_TAG_structure_type:
4850 case DW_TAG_union_type:
134d01f1 4851 process_structure_scope (die, cu);
c906108c
SS
4852 break;
4853 case DW_TAG_enumeration_type:
134d01f1 4854 process_enumeration_scope (die, cu);
c906108c 4855 break;
134d01f1 4856
f792889a
DJ
4857 /* These dies have a type, but processing them does not create
4858 a symbol or recurse to process the children. Therefore we can
4859 read them on-demand through read_type_die. */
c906108c 4860 case DW_TAG_subroutine_type:
72019c9c 4861 case DW_TAG_set_type:
c906108c 4862 case DW_TAG_array_type:
c906108c 4863 case DW_TAG_pointer_type:
c906108c 4864 case DW_TAG_ptr_to_member_type:
c906108c 4865 case DW_TAG_reference_type:
c906108c 4866 case DW_TAG_string_type:
c906108c 4867 break;
134d01f1 4868
c906108c 4869 case DW_TAG_base_type:
a02abb62 4870 case DW_TAG_subrange_type:
cb249c71 4871 case DW_TAG_typedef:
134d01f1
DJ
4872 /* Add a typedef symbol for the type definition, if it has a
4873 DW_AT_name. */
f792889a 4874 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4875 break;
c906108c 4876 case DW_TAG_common_block:
e7c27a73 4877 read_common_block (die, cu);
c906108c
SS
4878 break;
4879 case DW_TAG_common_inclusion:
4880 break;
d9fa45fe 4881 case DW_TAG_namespace:
63d06c5c 4882 processing_has_namespace_info = 1;
e7c27a73 4883 read_namespace (die, cu);
d9fa45fe 4884 break;
5d7cb8df 4885 case DW_TAG_module:
f55ee35c 4886 processing_has_namespace_info = 1;
5d7cb8df
JK
4887 read_module (die, cu);
4888 break;
d9fa45fe
DC
4889 case DW_TAG_imported_declaration:
4890 case DW_TAG_imported_module:
63d06c5c 4891 processing_has_namespace_info = 1;
27aa8d6a
SW
4892 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4893 || cu->language != language_fortran))
4894 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4895 dwarf_tag_name (die->tag));
4896 read_import_statement (die, cu);
d9fa45fe 4897 break;
c906108c 4898 default:
e7c27a73 4899 new_symbol (die, NULL, cu);
c906108c
SS
4900 break;
4901 }
4902}
4903
94af9270
KS
4904/* A helper function for dwarf2_compute_name which determines whether DIE
4905 needs to have the name of the scope prepended to the name listed in the
4906 die. */
4907
4908static int
4909die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4910{
1c809c68
TT
4911 struct attribute *attr;
4912
94af9270
KS
4913 switch (die->tag)
4914 {
4915 case DW_TAG_namespace:
4916 case DW_TAG_typedef:
4917 case DW_TAG_class_type:
4918 case DW_TAG_interface_type:
4919 case DW_TAG_structure_type:
4920 case DW_TAG_union_type:
4921 case DW_TAG_enumeration_type:
4922 case DW_TAG_enumerator:
4923 case DW_TAG_subprogram:
4924 case DW_TAG_member:
4925 return 1;
4926
4927 case DW_TAG_variable:
c2b0a229 4928 case DW_TAG_constant:
94af9270
KS
4929 /* We only need to prefix "globally" visible variables. These include
4930 any variable marked with DW_AT_external or any variable that
4931 lives in a namespace. [Variables in anonymous namespaces
4932 require prefixing, but they are not DW_AT_external.] */
4933
4934 if (dwarf2_attr (die, DW_AT_specification, cu))
4935 {
4936 struct dwarf2_cu *spec_cu = cu;
9a619af0 4937
94af9270
KS
4938 return die_needs_namespace (die_specification (die, &spec_cu),
4939 spec_cu);
4940 }
4941
1c809c68 4942 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4943 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4944 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4945 return 0;
4946 /* A variable in a lexical block of some kind does not need a
4947 namespace, even though in C++ such variables may be external
4948 and have a mangled name. */
4949 if (die->parent->tag == DW_TAG_lexical_block
4950 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4951 || die->parent->tag == DW_TAG_catch_block
4952 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4953 return 0;
4954 return 1;
94af9270
KS
4955
4956 default:
4957 return 0;
4958 }
4959}
4960
98bfdba5
PA
4961/* Retrieve the last character from a mem_file. */
4962
4963static void
4964do_ui_file_peek_last (void *object, const char *buffer, long length)
4965{
4966 char *last_char_p = (char *) object;
4967
4968 if (length > 0)
4969 *last_char_p = buffer[length - 1];
4970}
4971
94af9270
KS
4972/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4973 compute the physname for the object, which include a method's
4974 formal parameters (C++/Java) and return type (Java).
4975
af6b7be1
JB
4976 For Ada, return the DIE's linkage name rather than the fully qualified
4977 name. PHYSNAME is ignored..
4978
94af9270
KS
4979 The result is allocated on the objfile_obstack and canonicalized. */
4980
4981static const char *
4982dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4983 int physname)
4984{
bb5ed363
DE
4985 struct objfile *objfile = cu->objfile;
4986
94af9270
KS
4987 if (name == NULL)
4988 name = dwarf2_name (die, cu);
4989
f55ee35c
JK
4990 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4991 compute it by typename_concat inside GDB. */
4992 if (cu->language == language_ada
4993 || (cu->language == language_fortran && physname))
4994 {
4995 /* For Ada unit, we prefer the linkage name over the name, as
4996 the former contains the exported name, which the user expects
4997 to be able to reference. Ideally, we want the user to be able
4998 to reference this entity using either natural or linkage name,
4999 but we haven't started looking at this enhancement yet. */
5000 struct attribute *attr;
5001
5002 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5003 if (attr == NULL)
5004 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5005 if (attr && DW_STRING (attr))
5006 return DW_STRING (attr);
5007 }
5008
94af9270
KS
5009 /* These are the only languages we know how to qualify names in. */
5010 if (name != NULL
f55ee35c
JK
5011 && (cu->language == language_cplus || cu->language == language_java
5012 || cu->language == language_fortran))
94af9270
KS
5013 {
5014 if (die_needs_namespace (die, cu))
5015 {
5016 long length;
5017 char *prefix;
5018 struct ui_file *buf;
5019
5020 prefix = determine_prefix (die, cu);
5021 buf = mem_fileopen ();
5022 if (*prefix != '\0')
5023 {
f55ee35c
JK
5024 char *prefixed_name = typename_concat (NULL, prefix, name,
5025 physname, cu);
9a619af0 5026
94af9270
KS
5027 fputs_unfiltered (prefixed_name, buf);
5028 xfree (prefixed_name);
5029 }
5030 else
62d5b8da 5031 fputs_unfiltered (name, buf);
94af9270 5032
98bfdba5
PA
5033 /* Template parameters may be specified in the DIE's DW_AT_name, or
5034 as children with DW_TAG_template_type_param or
5035 DW_TAG_value_type_param. If the latter, add them to the name
5036 here. If the name already has template parameters, then
5037 skip this step; some versions of GCC emit both, and
5038 it is more efficient to use the pre-computed name.
5039
5040 Something to keep in mind about this process: it is very
5041 unlikely, or in some cases downright impossible, to produce
5042 something that will match the mangled name of a function.
5043 If the definition of the function has the same debug info,
5044 we should be able to match up with it anyway. But fallbacks
5045 using the minimal symbol, for instance to find a method
5046 implemented in a stripped copy of libstdc++, will not work.
5047 If we do not have debug info for the definition, we will have to
5048 match them up some other way.
5049
5050 When we do name matching there is a related problem with function
5051 templates; two instantiated function templates are allowed to
5052 differ only by their return types, which we do not add here. */
5053
5054 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5055 {
5056 struct attribute *attr;
5057 struct die_info *child;
5058 int first = 1;
5059
5060 die->building_fullname = 1;
5061
5062 for (child = die->child; child != NULL; child = child->sibling)
5063 {
5064 struct type *type;
5065 long value;
5066 gdb_byte *bytes;
5067 struct dwarf2_locexpr_baton *baton;
5068 struct value *v;
5069
5070 if (child->tag != DW_TAG_template_type_param
5071 && child->tag != DW_TAG_template_value_param)
5072 continue;
5073
5074 if (first)
5075 {
5076 fputs_unfiltered ("<", buf);
5077 first = 0;
5078 }
5079 else
5080 fputs_unfiltered (", ", buf);
5081
5082 attr = dwarf2_attr (child, DW_AT_type, cu);
5083 if (attr == NULL)
5084 {
5085 complaint (&symfile_complaints,
5086 _("template parameter missing DW_AT_type"));
5087 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5088 continue;
5089 }
5090 type = die_type (child, cu);
5091
5092 if (child->tag == DW_TAG_template_type_param)
5093 {
5094 c_print_type (type, "", buf, -1, 0);
5095 continue;
5096 }
5097
5098 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5099 if (attr == NULL)
5100 {
5101 complaint (&symfile_complaints,
3e43a32a
MS
5102 _("template parameter missing "
5103 "DW_AT_const_value"));
98bfdba5
PA
5104 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5105 continue;
5106 }
5107
5108 dwarf2_const_value_attr (attr, type, name,
5109 &cu->comp_unit_obstack, cu,
5110 &value, &bytes, &baton);
5111
5112 if (TYPE_NOSIGN (type))
5113 /* GDB prints characters as NUMBER 'CHAR'. If that's
5114 changed, this can use value_print instead. */
5115 c_printchar (value, type, buf);
5116 else
5117 {
5118 struct value_print_options opts;
5119
5120 if (baton != NULL)
5121 v = dwarf2_evaluate_loc_desc (type, NULL,
5122 baton->data,
5123 baton->size,
5124 baton->per_cu);
5125 else if (bytes != NULL)
5126 {
5127 v = allocate_value (type);
5128 memcpy (value_contents_writeable (v), bytes,
5129 TYPE_LENGTH (type));
5130 }
5131 else
5132 v = value_from_longest (type, value);
5133
3e43a32a
MS
5134 /* Specify decimal so that we do not depend on
5135 the radix. */
98bfdba5
PA
5136 get_formatted_print_options (&opts, 'd');
5137 opts.raw = 1;
5138 value_print (v, buf, &opts);
5139 release_value (v);
5140 value_free (v);
5141 }
5142 }
5143
5144 die->building_fullname = 0;
5145
5146 if (!first)
5147 {
5148 /* Close the argument list, with a space if necessary
5149 (nested templates). */
5150 char last_char = '\0';
5151 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5152 if (last_char == '>')
5153 fputs_unfiltered (" >", buf);
5154 else
5155 fputs_unfiltered (">", buf);
5156 }
5157 }
5158
94af9270
KS
5159 /* For Java and C++ methods, append formal parameter type
5160 information, if PHYSNAME. */
6e70227d 5161
94af9270
KS
5162 if (physname && die->tag == DW_TAG_subprogram
5163 && (cu->language == language_cplus
5164 || cu->language == language_java))
5165 {
5166 struct type *type = read_type_die (die, cu);
5167
3167638f 5168 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5169
5170 if (cu->language == language_java)
5171 {
5172 /* For java, we must append the return type to method
0963b4bd 5173 names. */
94af9270
KS
5174 if (die->tag == DW_TAG_subprogram)
5175 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5176 0, 0);
5177 }
5178 else if (cu->language == language_cplus)
5179 {
60430eff
DJ
5180 /* Assume that an artificial first parameter is
5181 "this", but do not crash if it is not. RealView
5182 marks unnamed (and thus unused) parameters as
5183 artificial; there is no way to differentiate
5184 the two cases. */
94af9270
KS
5185 if (TYPE_NFIELDS (type) > 0
5186 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5187 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5188 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5189 0))))
94af9270
KS
5190 fputs_unfiltered (" const", buf);
5191 }
5192 }
5193
bb5ed363 5194 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
5195 &length);
5196 ui_file_delete (buf);
5197
5198 if (cu->language == language_cplus)
5199 {
5200 char *cname
5201 = dwarf2_canonicalize_name (name, cu,
bb5ed363 5202 &objfile->objfile_obstack);
9a619af0 5203
94af9270
KS
5204 if (cname != NULL)
5205 name = cname;
5206 }
5207 }
5208 }
5209
5210 return name;
5211}
5212
0114d602
DJ
5213/* Return the fully qualified name of DIE, based on its DW_AT_name.
5214 If scope qualifiers are appropriate they will be added. The result
5215 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5216 not have a name. NAME may either be from a previous call to
5217 dwarf2_name or NULL.
5218
0963b4bd 5219 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5220
5221static const char *
94af9270 5222dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5223{
94af9270
KS
5224 return dwarf2_compute_name (name, die, cu, 0);
5225}
0114d602 5226
94af9270
KS
5227/* Construct a physname for the given DIE in CU. NAME may either be
5228 from a previous call to dwarf2_name or NULL. The result will be
5229 allocated on the objfile_objstack or NULL if the DIE does not have a
5230 name.
0114d602 5231
94af9270 5232 The output string will be canonicalized (if C++/Java). */
0114d602 5233
94af9270
KS
5234static const char *
5235dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5236{
bb5ed363 5237 struct objfile *objfile = cu->objfile;
900e11f9
JK
5238 struct attribute *attr;
5239 const char *retval, *mangled = NULL, *canon = NULL;
5240 struct cleanup *back_to;
5241 int need_copy = 1;
5242
5243 /* In this case dwarf2_compute_name is just a shortcut not building anything
5244 on its own. */
5245 if (!die_needs_namespace (die, cu))
5246 return dwarf2_compute_name (name, die, cu, 1);
5247
5248 back_to = make_cleanup (null_cleanup, NULL);
5249
5250 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5251 if (!attr)
5252 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5253
5254 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5255 has computed. */
5256 if (attr && DW_STRING (attr))
5257 {
5258 char *demangled;
5259
5260 mangled = DW_STRING (attr);
5261
5262 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5263 type. It is easier for GDB users to search for such functions as
5264 `name(params)' than `long name(params)'. In such case the minimal
5265 symbol names do not match the full symbol names but for template
5266 functions there is never a need to look up their definition from their
5267 declaration so the only disadvantage remains the minimal symbol
5268 variant `long name(params)' does not have the proper inferior type.
5269 */
5270
5271 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5272 | (cu->language == language_java
5273 ? DMGL_JAVA | DMGL_RET_POSTFIX
5274 : DMGL_RET_DROP)));
5275 if (demangled)
5276 {
5277 make_cleanup (xfree, demangled);
5278 canon = demangled;
5279 }
5280 else
5281 {
5282 canon = mangled;
5283 need_copy = 0;
5284 }
5285 }
5286
5287 if (canon == NULL || check_physname)
5288 {
5289 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5290
5291 if (canon != NULL && strcmp (physname, canon) != 0)
5292 {
5293 /* It may not mean a bug in GDB. The compiler could also
5294 compute DW_AT_linkage_name incorrectly. But in such case
5295 GDB would need to be bug-to-bug compatible. */
5296
5297 complaint (&symfile_complaints,
5298 _("Computed physname <%s> does not match demangled <%s> "
5299 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
bb5ed363 5300 physname, canon, mangled, die->offset, objfile->name);
900e11f9
JK
5301
5302 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5303 is available here - over computed PHYSNAME. It is safer
5304 against both buggy GDB and buggy compilers. */
5305
5306 retval = canon;
5307 }
5308 else
5309 {
5310 retval = physname;
5311 need_copy = 0;
5312 }
5313 }
5314 else
5315 retval = canon;
5316
5317 if (need_copy)
5318 retval = obsavestring (retval, strlen (retval),
bb5ed363 5319 &objfile->objfile_obstack);
900e11f9
JK
5320
5321 do_cleanups (back_to);
5322 return retval;
0114d602
DJ
5323}
5324
27aa8d6a
SW
5325/* Read the import statement specified by the given die and record it. */
5326
5327static void
5328read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5329{
bb5ed363 5330 struct objfile *objfile = cu->objfile;
27aa8d6a 5331 struct attribute *import_attr;
32019081 5332 struct die_info *imported_die, *child_die;
de4affc9 5333 struct dwarf2_cu *imported_cu;
27aa8d6a 5334 const char *imported_name;
794684b6 5335 const char *imported_name_prefix;
13387711
SW
5336 const char *canonical_name;
5337 const char *import_alias;
5338 const char *imported_declaration = NULL;
794684b6 5339 const char *import_prefix;
32019081
JK
5340 VEC (const_char_ptr) *excludes = NULL;
5341 struct cleanup *cleanups;
13387711
SW
5342
5343 char *temp;
27aa8d6a
SW
5344
5345 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5346 if (import_attr == NULL)
5347 {
5348 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5349 dwarf_tag_name (die->tag));
5350 return;
5351 }
5352
de4affc9
CC
5353 imported_cu = cu;
5354 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5355 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5356 if (imported_name == NULL)
5357 {
5358 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5359
5360 The import in the following code:
5361 namespace A
5362 {
5363 typedef int B;
5364 }
5365
5366 int main ()
5367 {
5368 using A::B;
5369 B b;
5370 return b;
5371 }
5372
5373 ...
5374 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5375 <52> DW_AT_decl_file : 1
5376 <53> DW_AT_decl_line : 6
5377 <54> DW_AT_import : <0x75>
5378 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5379 <59> DW_AT_name : B
5380 <5b> DW_AT_decl_file : 1
5381 <5c> DW_AT_decl_line : 2
5382 <5d> DW_AT_type : <0x6e>
5383 ...
5384 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5385 <76> DW_AT_byte_size : 4
5386 <77> DW_AT_encoding : 5 (signed)
5387
5388 imports the wrong die ( 0x75 instead of 0x58 ).
5389 This case will be ignored until the gcc bug is fixed. */
5390 return;
5391 }
5392
82856980
SW
5393 /* Figure out the local name after import. */
5394 import_alias = dwarf2_name (die, cu);
27aa8d6a 5395
794684b6
SW
5396 /* Figure out where the statement is being imported to. */
5397 import_prefix = determine_prefix (die, cu);
5398
5399 /* Figure out what the scope of the imported die is and prepend it
5400 to the name of the imported die. */
de4affc9 5401 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5402
f55ee35c
JK
5403 if (imported_die->tag != DW_TAG_namespace
5404 && imported_die->tag != DW_TAG_module)
794684b6 5405 {
13387711
SW
5406 imported_declaration = imported_name;
5407 canonical_name = imported_name_prefix;
794684b6 5408 }
13387711 5409 else if (strlen (imported_name_prefix) > 0)
794684b6 5410 {
13387711
SW
5411 temp = alloca (strlen (imported_name_prefix)
5412 + 2 + strlen (imported_name) + 1);
5413 strcpy (temp, imported_name_prefix);
5414 strcat (temp, "::");
5415 strcat (temp, imported_name);
5416 canonical_name = temp;
794684b6 5417 }
13387711
SW
5418 else
5419 canonical_name = imported_name;
794684b6 5420
32019081
JK
5421 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5422
5423 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5424 for (child_die = die->child; child_die && child_die->tag;
5425 child_die = sibling_die (child_die))
5426 {
5427 /* DWARF-4: A Fortran use statement with a “rename list” may be
5428 represented by an imported module entry with an import attribute
5429 referring to the module and owned entries corresponding to those
5430 entities that are renamed as part of being imported. */
5431
5432 if (child_die->tag != DW_TAG_imported_declaration)
5433 {
5434 complaint (&symfile_complaints,
5435 _("child DW_TAG_imported_declaration expected "
5436 "- DIE at 0x%x [in module %s]"),
bb5ed363 5437 child_die->offset, objfile->name);
32019081
JK
5438 continue;
5439 }
5440
5441 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5442 if (import_attr == NULL)
5443 {
5444 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5445 dwarf_tag_name (child_die->tag));
5446 continue;
5447 }
5448
5449 imported_cu = cu;
5450 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5451 &imported_cu);
5452 imported_name = dwarf2_name (imported_die, imported_cu);
5453 if (imported_name == NULL)
5454 {
5455 complaint (&symfile_complaints,
5456 _("child DW_TAG_imported_declaration has unknown "
5457 "imported name - DIE at 0x%x [in module %s]"),
bb5ed363 5458 child_die->offset, objfile->name);
32019081
JK
5459 continue;
5460 }
5461
5462 VEC_safe_push (const_char_ptr, excludes, imported_name);
5463
5464 process_die (child_die, cu);
5465 }
5466
c0cc3a76
SW
5467 cp_add_using_directive (import_prefix,
5468 canonical_name,
5469 import_alias,
13387711 5470 imported_declaration,
32019081 5471 excludes,
bb5ed363 5472 &objfile->objfile_obstack);
32019081
JK
5473
5474 do_cleanups (cleanups);
27aa8d6a
SW
5475}
5476
ae2de4f8
DE
5477/* Cleanup function for read_file_scope. */
5478
cb1df416
DJ
5479static void
5480free_cu_line_header (void *arg)
5481{
5482 struct dwarf2_cu *cu = arg;
5483
5484 free_line_header (cu->line_header);
5485 cu->line_header = NULL;
5486}
5487
9291a0cd
TT
5488static void
5489find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5490 char **name, char **comp_dir)
5491{
5492 struct attribute *attr;
5493
5494 *name = NULL;
5495 *comp_dir = NULL;
5496
5497 /* Find the filename. Do not use dwarf2_name here, since the filename
5498 is not a source language identifier. */
5499 attr = dwarf2_attr (die, DW_AT_name, cu);
5500 if (attr)
5501 {
5502 *name = DW_STRING (attr);
5503 }
5504
5505 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5506 if (attr)
5507 *comp_dir = DW_STRING (attr);
5508 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5509 {
5510 *comp_dir = ldirname (*name);
5511 if (*comp_dir != NULL)
5512 make_cleanup (xfree, *comp_dir);
5513 }
5514 if (*comp_dir != NULL)
5515 {
5516 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5517 directory, get rid of it. */
5518 char *cp = strchr (*comp_dir, ':');
5519
5520 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5521 *comp_dir = cp + 1;
5522 }
5523
5524 if (*name == NULL)
5525 *name = "<unknown>";
5526}
5527
f3f5162e
DE
5528/* Handle DW_AT_stmt_list for a compilation unit or type unit.
5529 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5530 COMP_DIR is the compilation directory.
5531 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
5532
5533static void
5534handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 5535 const char *comp_dir, int want_line_info)
2ab95328
TT
5536{
5537 struct attribute *attr;
5538 struct objfile *objfile = cu->objfile;
5539 bfd *abfd = objfile->obfd;
5540
2ab95328
TT
5541 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5542 if (attr)
5543 {
5544 unsigned int line_offset = DW_UNSND (attr);
5545 struct line_header *line_header
5546 = dwarf_decode_line_header (line_offset, abfd, cu);
5547
5548 if (line_header)
5549 {
5550 cu->line_header = line_header;
5551 make_cleanup (free_cu_line_header, cu);
f3f5162e 5552 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
2ab95328
TT
5553 }
5554 }
5555}
5556
ae2de4f8
DE
5557/* Process DW_TAG_compile_unit. */
5558
c906108c 5559static void
e7c27a73 5560read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5561{
e7c27a73 5562 struct objfile *objfile = cu->objfile;
debd256d 5563 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5564 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5565 CORE_ADDR highpc = ((CORE_ADDR) 0);
5566 struct attribute *attr;
e1024ff1 5567 char *name = NULL;
c906108c
SS
5568 char *comp_dir = NULL;
5569 struct die_info *child_die;
5570 bfd *abfd = objfile->obfd;
e142c38c 5571 CORE_ADDR baseaddr;
6e70227d 5572
e142c38c 5573 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5574
fae299cd 5575 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5576
5577 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5578 from finish_block. */
2acceee2 5579 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5580 lowpc = highpc;
5581 lowpc += baseaddr;
5582 highpc += baseaddr;
5583
9291a0cd 5584 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5585
e142c38c 5586 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5587 if (attr)
5588 {
e142c38c 5589 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5590 }
5591
b0f35d58 5592 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5593 if (attr)
b0f35d58 5594 cu->producer = DW_STRING (attr);
303b6f5d 5595
f4b8a18d
KW
5596 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5597 standardised yet. As a workaround for the language detection we fall
5598 back to the DW_AT_producer string. */
5599 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5600 cu->language = language_opencl;
5601
0963b4bd 5602 /* We assume that we're processing GCC output. */
c906108c 5603 processing_gcc_compilation = 2;
c906108c 5604
df8a16a1
DJ
5605 processing_has_namespace_info = 0;
5606
c906108c
SS
5607 start_symtab (name, comp_dir, lowpc);
5608 record_debugformat ("DWARF 2");
303b6f5d 5609 record_producer (cu->producer);
c906108c 5610
f3f5162e
DE
5611 /* Decode line number information if present. We do this before
5612 processing child DIEs, so that the line header table is available
5613 for DW_AT_decl_file. */
5614 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
debd256d 5615
cb1df416
DJ
5616 /* Process all dies in compilation unit. */
5617 if (die->child != NULL)
5618 {
5619 child_die = die->child;
5620 while (child_die && child_die->tag)
5621 {
5622 process_die (child_die, cu);
5623 child_die = sibling_die (child_die);
5624 }
5625 }
5626
2e276125
JB
5627 /* Decode macro information, if present. Dwarf 2 macro information
5628 refers to information in the line number info statement program
5629 header, so we can only read it if we've read the header
5630 successfully. */
cf2c3c16 5631 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5632 if (attr && cu->line_header)
2e276125 5633 {
cf2c3c16
TT
5634 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5635 complaint (&symfile_complaints,
5636 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5637
5638 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5639 comp_dir, abfd, cu,
5640 &dwarf2_per_objfile->macro, 1);
5641 }
5642 else
5643 {
5644 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5645 if (attr && cu->line_header)
5646 {
5647 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5648
cf2c3c16
TT
5649 dwarf_decode_macros (cu->line_header, macro_offset,
5650 comp_dir, abfd, cu,
5651 &dwarf2_per_objfile->macinfo, 0);
5652 }
2e276125 5653 }
9cdd5dbd 5654
debd256d 5655 do_cleanups (back_to);
5fb290d7
DJ
5656}
5657
ae2de4f8
DE
5658/* Process DW_TAG_type_unit.
5659 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5660 actual type being defined by this TU. In this case the first top
5661 level sibling is there to provide context only. */
5662
5663static void
5664read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5665{
5666 struct objfile *objfile = cu->objfile;
5667 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5668 CORE_ADDR lowpc;
5669 struct attribute *attr;
5670 char *name = NULL;
5671 char *comp_dir = NULL;
5672 struct die_info *child_die;
5673 bfd *abfd = objfile->obfd;
348e048f
DE
5674
5675 /* start_symtab needs a low pc, but we don't really have one.
5676 Do what read_file_scope would do in the absence of such info. */
5677 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5678
5679 /* Find the filename. Do not use dwarf2_name here, since the filename
5680 is not a source language identifier. */
5681 attr = dwarf2_attr (die, DW_AT_name, cu);
5682 if (attr)
5683 name = DW_STRING (attr);
5684
5685 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5686 if (attr)
5687 comp_dir = DW_STRING (attr);
5688 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5689 {
5690 comp_dir = ldirname (name);
5691 if (comp_dir != NULL)
5692 make_cleanup (xfree, comp_dir);
5693 }
5694
5695 if (name == NULL)
5696 name = "<unknown>";
5697
5698 attr = dwarf2_attr (die, DW_AT_language, cu);
5699 if (attr)
5700 set_cu_language (DW_UNSND (attr), cu);
5701
5702 /* This isn't technically needed today. It is done for symmetry
5703 with read_file_scope. */
5704 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5705 if (attr)
348e048f
DE
5706 cu->producer = DW_STRING (attr);
5707
0963b4bd 5708 /* We assume that we're processing GCC output. */
348e048f
DE
5709 processing_gcc_compilation = 2;
5710
5711 processing_has_namespace_info = 0;
5712
5713 start_symtab (name, comp_dir, lowpc);
5714 record_debugformat ("DWARF 2");
5715 record_producer (cu->producer);
5716
f3f5162e
DE
5717 /* Decode line number information if present. We do this before
5718 processing child DIEs, so that the line header table is available
5719 for DW_AT_decl_file.
5720 We don't need the pc/line-number mapping for type units. */
5721 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
2ab95328 5722
348e048f
DE
5723 /* Process the dies in the type unit. */
5724 if (die->child == NULL)
5725 {
5726 dump_die_for_error (die);
5727 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5728 bfd_get_filename (abfd));
5729 }
5730
5731 child_die = die->child;
5732
5733 while (child_die && child_die->tag)
5734 {
5735 process_die (child_die, cu);
5736
5737 child_die = sibling_die (child_die);
5738 }
5739
5740 do_cleanups (back_to);
5741}
5742
d389af10
JK
5743/* qsort helper for inherit_abstract_dies. */
5744
5745static int
5746unsigned_int_compar (const void *ap, const void *bp)
5747{
5748 unsigned int a = *(unsigned int *) ap;
5749 unsigned int b = *(unsigned int *) bp;
5750
5751 return (a > b) - (b > a);
5752}
5753
5754/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5755 Inherit only the children of the DW_AT_abstract_origin DIE not being
5756 already referenced by DW_AT_abstract_origin from the children of the
5757 current DIE. */
d389af10
JK
5758
5759static void
5760inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5761{
5762 struct die_info *child_die;
5763 unsigned die_children_count;
5764 /* CU offsets which were referenced by children of the current DIE. */
5765 unsigned *offsets;
5766 unsigned *offsets_end, *offsetp;
5767 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5768 struct die_info *origin_die;
5769 /* Iterator of the ORIGIN_DIE children. */
5770 struct die_info *origin_child_die;
5771 struct cleanup *cleanups;
5772 struct attribute *attr;
cd02d79d
PA
5773 struct dwarf2_cu *origin_cu;
5774 struct pending **origin_previous_list_in_scope;
d389af10
JK
5775
5776 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5777 if (!attr)
5778 return;
5779
cd02d79d
PA
5780 /* Note that following die references may follow to a die in a
5781 different cu. */
5782
5783 origin_cu = cu;
5784 origin_die = follow_die_ref (die, attr, &origin_cu);
5785
5786 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5787 symbols in. */
5788 origin_previous_list_in_scope = origin_cu->list_in_scope;
5789 origin_cu->list_in_scope = cu->list_in_scope;
5790
edb3359d
DJ
5791 if (die->tag != origin_die->tag
5792 && !(die->tag == DW_TAG_inlined_subroutine
5793 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5794 complaint (&symfile_complaints,
5795 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5796 die->offset, origin_die->offset);
5797
5798 child_die = die->child;
5799 die_children_count = 0;
5800 while (child_die && child_die->tag)
5801 {
5802 child_die = sibling_die (child_die);
5803 die_children_count++;
5804 }
5805 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5806 cleanups = make_cleanup (xfree, offsets);
5807
5808 offsets_end = offsets;
5809 child_die = die->child;
5810 while (child_die && child_die->tag)
5811 {
c38f313d
DJ
5812 /* For each CHILD_DIE, find the corresponding child of
5813 ORIGIN_DIE. If there is more than one layer of
5814 DW_AT_abstract_origin, follow them all; there shouldn't be,
5815 but GCC versions at least through 4.4 generate this (GCC PR
5816 40573). */
5817 struct die_info *child_origin_die = child_die;
cd02d79d 5818 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5819
c38f313d
DJ
5820 while (1)
5821 {
cd02d79d
PA
5822 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5823 child_origin_cu);
c38f313d
DJ
5824 if (attr == NULL)
5825 break;
cd02d79d
PA
5826 child_origin_die = follow_die_ref (child_origin_die, attr,
5827 &child_origin_cu);
c38f313d
DJ
5828 }
5829
d389af10
JK
5830 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5831 counterpart may exist. */
c38f313d 5832 if (child_origin_die != child_die)
d389af10 5833 {
edb3359d
DJ
5834 if (child_die->tag != child_origin_die->tag
5835 && !(child_die->tag == DW_TAG_inlined_subroutine
5836 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5837 complaint (&symfile_complaints,
5838 _("Child DIE 0x%x and its abstract origin 0x%x have "
5839 "different tags"), child_die->offset,
5840 child_origin_die->offset);
c38f313d
DJ
5841 if (child_origin_die->parent != origin_die)
5842 complaint (&symfile_complaints,
5843 _("Child DIE 0x%x and its abstract origin 0x%x have "
5844 "different parents"), child_die->offset,
5845 child_origin_die->offset);
5846 else
5847 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5848 }
5849 child_die = sibling_die (child_die);
5850 }
5851 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5852 unsigned_int_compar);
5853 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5854 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5855 complaint (&symfile_complaints,
5856 _("Multiple children of DIE 0x%x refer "
5857 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5858 die->offset, *offsetp);
5859
5860 offsetp = offsets;
5861 origin_child_die = origin_die->child;
5862 while (origin_child_die && origin_child_die->tag)
5863 {
5864 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5865 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5866 offsetp++;
5867 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5868 {
5869 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5870 process_die (origin_child_die, origin_cu);
d389af10
JK
5871 }
5872 origin_child_die = sibling_die (origin_child_die);
5873 }
cd02d79d 5874 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5875
5876 do_cleanups (cleanups);
5877}
5878
c906108c 5879static void
e7c27a73 5880read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5881{
e7c27a73 5882 struct objfile *objfile = cu->objfile;
52f0bd74 5883 struct context_stack *new;
c906108c
SS
5884 CORE_ADDR lowpc;
5885 CORE_ADDR highpc;
5886 struct die_info *child_die;
edb3359d 5887 struct attribute *attr, *call_line, *call_file;
c906108c 5888 char *name;
e142c38c 5889 CORE_ADDR baseaddr;
801e3a5b 5890 struct block *block;
edb3359d 5891 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5892 VEC (symbolp) *template_args = NULL;
5893 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5894
5895 if (inlined_func)
5896 {
5897 /* If we do not have call site information, we can't show the
5898 caller of this inlined function. That's too confusing, so
5899 only use the scope for local variables. */
5900 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5901 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5902 if (call_line == NULL || call_file == NULL)
5903 {
5904 read_lexical_block_scope (die, cu);
5905 return;
5906 }
5907 }
c906108c 5908
e142c38c
DJ
5909 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5910
94af9270 5911 name = dwarf2_name (die, cu);
c906108c 5912
e8d05480
JB
5913 /* Ignore functions with missing or empty names. These are actually
5914 illegal according to the DWARF standard. */
5915 if (name == NULL)
5916 {
5917 complaint (&symfile_complaints,
5918 _("missing name for subprogram DIE at %d"), die->offset);
5919 return;
5920 }
5921
5922 /* Ignore functions with missing or invalid low and high pc attributes. */
5923 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5924 {
ae4d0c03
PM
5925 attr = dwarf2_attr (die, DW_AT_external, cu);
5926 if (!attr || !DW_UNSND (attr))
5927 complaint (&symfile_complaints,
3e43a32a
MS
5928 _("cannot get low and high bounds "
5929 "for subprogram DIE at %d"),
ae4d0c03 5930 die->offset);
e8d05480
JB
5931 return;
5932 }
c906108c
SS
5933
5934 lowpc += baseaddr;
5935 highpc += baseaddr;
5936
34eaf542
TT
5937 /* If we have any template arguments, then we must allocate a
5938 different sort of symbol. */
5939 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5940 {
5941 if (child_die->tag == DW_TAG_template_type_param
5942 || child_die->tag == DW_TAG_template_value_param)
5943 {
5944 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5945 struct template_symbol);
5946 templ_func->base.is_cplus_template_function = 1;
5947 break;
5948 }
5949 }
5950
c906108c 5951 new = push_context (0, lowpc);
34eaf542
TT
5952 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5953 (struct symbol *) templ_func);
4c2df51b 5954
4cecd739
DJ
5955 /* If there is a location expression for DW_AT_frame_base, record
5956 it. */
e142c38c 5957 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5958 if (attr)
c034e007
AC
5959 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5960 expression is being recorded directly in the function's symbol
5961 and not in a separate frame-base object. I guess this hack is
5962 to avoid adding some sort of frame-base adjunct/annex to the
5963 function's symbol :-(. The problem with doing this is that it
5964 results in a function symbol with a location expression that
5965 has nothing to do with the location of the function, ouch! The
5966 relationship should be: a function's symbol has-a frame base; a
5967 frame-base has-a location expression. */
e7c27a73 5968 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5969
e142c38c 5970 cu->list_in_scope = &local_symbols;
c906108c 5971
639d11d3 5972 if (die->child != NULL)
c906108c 5973 {
639d11d3 5974 child_die = die->child;
c906108c
SS
5975 while (child_die && child_die->tag)
5976 {
34eaf542
TT
5977 if (child_die->tag == DW_TAG_template_type_param
5978 || child_die->tag == DW_TAG_template_value_param)
5979 {
5980 struct symbol *arg = new_symbol (child_die, NULL, cu);
5981
f1078f66
DJ
5982 if (arg != NULL)
5983 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5984 }
5985 else
5986 process_die (child_die, cu);
c906108c
SS
5987 child_die = sibling_die (child_die);
5988 }
5989 }
5990
d389af10
JK
5991 inherit_abstract_dies (die, cu);
5992
4a811a97
UW
5993 /* If we have a DW_AT_specification, we might need to import using
5994 directives from the context of the specification DIE. See the
5995 comment in determine_prefix. */
5996 if (cu->language == language_cplus
5997 && dwarf2_attr (die, DW_AT_specification, cu))
5998 {
5999 struct dwarf2_cu *spec_cu = cu;
6000 struct die_info *spec_die = die_specification (die, &spec_cu);
6001
6002 while (spec_die)
6003 {
6004 child_die = spec_die->child;
6005 while (child_die && child_die->tag)
6006 {
6007 if (child_die->tag == DW_TAG_imported_module)
6008 process_die (child_die, spec_cu);
6009 child_die = sibling_die (child_die);
6010 }
6011
6012 /* In some cases, GCC generates specification DIEs that
6013 themselves contain DW_AT_specification attributes. */
6014 spec_die = die_specification (spec_die, &spec_cu);
6015 }
6016 }
6017
c906108c
SS
6018 new = pop_context ();
6019 /* Make a block for the local symbols within. */
801e3a5b
JB
6020 block = finish_block (new->name, &local_symbols, new->old_blocks,
6021 lowpc, highpc, objfile);
6022
df8a16a1 6023 /* For C++, set the block's scope. */
f55ee35c 6024 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6025 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6026 determine_prefix (die, cu),
df8a16a1
DJ
6027 processing_has_namespace_info);
6028
801e3a5b
JB
6029 /* If we have address ranges, record them. */
6030 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6031
34eaf542
TT
6032 /* Attach template arguments to function. */
6033 if (! VEC_empty (symbolp, template_args))
6034 {
6035 gdb_assert (templ_func != NULL);
6036
6037 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6038 templ_func->template_arguments
6039 = obstack_alloc (&objfile->objfile_obstack,
6040 (templ_func->n_template_arguments
6041 * sizeof (struct symbol *)));
6042 memcpy (templ_func->template_arguments,
6043 VEC_address (symbolp, template_args),
6044 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6045 VEC_free (symbolp, template_args);
6046 }
6047
208d8187
JB
6048 /* In C++, we can have functions nested inside functions (e.g., when
6049 a function declares a class that has methods). This means that
6050 when we finish processing a function scope, we may need to go
6051 back to building a containing block's symbol lists. */
6052 local_symbols = new->locals;
6053 param_symbols = new->params;
27aa8d6a 6054 using_directives = new->using_directives;
208d8187 6055
921e78cf
JB
6056 /* If we've finished processing a top-level function, subsequent
6057 symbols go in the file symbol list. */
6058 if (outermost_context_p ())
e142c38c 6059 cu->list_in_scope = &file_symbols;
c906108c
SS
6060}
6061
6062/* Process all the DIES contained within a lexical block scope. Start
6063 a new scope, process the dies, and then close the scope. */
6064
6065static void
e7c27a73 6066read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6067{
e7c27a73 6068 struct objfile *objfile = cu->objfile;
52f0bd74 6069 struct context_stack *new;
c906108c
SS
6070 CORE_ADDR lowpc, highpc;
6071 struct die_info *child_die;
e142c38c
DJ
6072 CORE_ADDR baseaddr;
6073
6074 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6075
6076 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6077 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6078 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6079 be nasty. Might be easier to properly extend generic blocks to
af34e669 6080 describe ranges. */
d85a05f0 6081 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6082 return;
6083 lowpc += baseaddr;
6084 highpc += baseaddr;
6085
6086 push_context (0, lowpc);
639d11d3 6087 if (die->child != NULL)
c906108c 6088 {
639d11d3 6089 child_die = die->child;
c906108c
SS
6090 while (child_die && child_die->tag)
6091 {
e7c27a73 6092 process_die (child_die, cu);
c906108c
SS
6093 child_die = sibling_die (child_die);
6094 }
6095 }
6096 new = pop_context ();
6097
8540c487 6098 if (local_symbols != NULL || using_directives != NULL)
c906108c 6099 {
801e3a5b
JB
6100 struct block *block
6101 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6102 highpc, objfile);
6103
6104 /* Note that recording ranges after traversing children, as we
6105 do here, means that recording a parent's ranges entails
6106 walking across all its children's ranges as they appear in
6107 the address map, which is quadratic behavior.
6108
6109 It would be nicer to record the parent's ranges before
6110 traversing its children, simply overriding whatever you find
6111 there. But since we don't even decide whether to create a
6112 block until after we've traversed its children, that's hard
6113 to do. */
6114 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6115 }
6116 local_symbols = new->locals;
27aa8d6a 6117 using_directives = new->using_directives;
c906108c
SS
6118}
6119
96408a79
SA
6120/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6121
6122static void
6123read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6124{
6125 struct objfile *objfile = cu->objfile;
6126 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6127 CORE_ADDR pc, baseaddr;
6128 struct attribute *attr;
6129 struct call_site *call_site, call_site_local;
6130 void **slot;
6131 int nparams;
6132 struct die_info *child_die;
6133
6134 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6135
6136 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6137 if (!attr)
6138 {
6139 complaint (&symfile_complaints,
6140 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6141 "DIE 0x%x [in module %s]"),
bb5ed363 6142 die->offset, objfile->name);
96408a79
SA
6143 return;
6144 }
6145 pc = DW_ADDR (attr) + baseaddr;
6146
6147 if (cu->call_site_htab == NULL)
6148 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6149 NULL, &objfile->objfile_obstack,
6150 hashtab_obstack_allocate, NULL);
6151 call_site_local.pc = pc;
6152 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6153 if (*slot != NULL)
6154 {
6155 complaint (&symfile_complaints,
6156 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6157 "DIE 0x%x [in module %s]"),
bb5ed363 6158 paddress (gdbarch, pc), die->offset, objfile->name);
96408a79
SA
6159 return;
6160 }
6161
6162 /* Count parameters at the caller. */
6163
6164 nparams = 0;
6165 for (child_die = die->child; child_die && child_die->tag;
6166 child_die = sibling_die (child_die))
6167 {
6168 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6169 {
6170 complaint (&symfile_complaints,
6171 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6172 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6173 child_die->tag, child_die->offset, objfile->name);
96408a79
SA
6174 continue;
6175 }
6176
6177 nparams++;
6178 }
6179
6180 call_site = obstack_alloc (&objfile->objfile_obstack,
6181 (sizeof (*call_site)
6182 + (sizeof (*call_site->parameter)
6183 * (nparams - 1))));
6184 *slot = call_site;
6185 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6186 call_site->pc = pc;
6187
6188 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6189 {
6190 struct die_info *func_die;
6191
6192 /* Skip also over DW_TAG_inlined_subroutine. */
6193 for (func_die = die->parent;
6194 func_die && func_die->tag != DW_TAG_subprogram
6195 && func_die->tag != DW_TAG_subroutine_type;
6196 func_die = func_die->parent);
6197
6198 /* DW_AT_GNU_all_call_sites is a superset
6199 of DW_AT_GNU_all_tail_call_sites. */
6200 if (func_die
6201 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6202 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6203 {
6204 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6205 not complete. But keep CALL_SITE for look ups via call_site_htab,
6206 both the initial caller containing the real return address PC and
6207 the final callee containing the current PC of a chain of tail
6208 calls do not need to have the tail call list complete. But any
6209 function candidate for a virtual tail call frame searched via
6210 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6211 determined unambiguously. */
6212 }
6213 else
6214 {
6215 struct type *func_type = NULL;
6216
6217 if (func_die)
6218 func_type = get_die_type (func_die, cu);
6219 if (func_type != NULL)
6220 {
6221 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6222
6223 /* Enlist this call site to the function. */
6224 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6225 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6226 }
6227 else
6228 complaint (&symfile_complaints,
6229 _("Cannot find function owning DW_TAG_GNU_call_site "
6230 "DIE 0x%x [in module %s]"),
bb5ed363 6231 die->offset, objfile->name);
96408a79
SA
6232 }
6233 }
6234
6235 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6236 if (attr == NULL)
6237 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6238 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6239 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6240 /* Keep NULL DWARF_BLOCK. */;
6241 else if (attr_form_is_block (attr))
6242 {
6243 struct dwarf2_locexpr_baton *dlbaton;
6244
6245 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6246 dlbaton->data = DW_BLOCK (attr)->data;
6247 dlbaton->size = DW_BLOCK (attr)->size;
6248 dlbaton->per_cu = cu->per_cu;
6249
6250 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6251 }
6252 else if (is_ref_attr (attr))
6253 {
96408a79
SA
6254 struct dwarf2_cu *target_cu = cu;
6255 struct die_info *target_die;
6256
6257 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6258 gdb_assert (target_cu->objfile == objfile);
6259 if (die_is_declaration (target_die, target_cu))
6260 {
6261 const char *target_physname;
6262
6263 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6264 if (target_physname == NULL)
6265 complaint (&symfile_complaints,
6266 _("DW_AT_GNU_call_site_target target DIE has invalid "
6267 "physname, for referencing DIE 0x%x [in module %s]"),
bb5ed363 6268 die->offset, objfile->name);
96408a79
SA
6269 else
6270 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6271 }
6272 else
6273 {
6274 CORE_ADDR lowpc;
6275
6276 /* DW_AT_entry_pc should be preferred. */
6277 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6278 complaint (&symfile_complaints,
6279 _("DW_AT_GNU_call_site_target target DIE has invalid "
6280 "low pc, for referencing DIE 0x%x [in module %s]"),
bb5ed363 6281 die->offset, objfile->name);
96408a79
SA
6282 else
6283 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6284 }
6285 }
6286 else
6287 complaint (&symfile_complaints,
6288 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6289 "block nor reference, for DIE 0x%x [in module %s]"),
bb5ed363 6290 die->offset, objfile->name);
96408a79
SA
6291
6292 call_site->per_cu = cu->per_cu;
6293
6294 for (child_die = die->child;
6295 child_die && child_die->tag;
6296 child_die = sibling_die (child_die))
6297 {
6298 struct dwarf2_locexpr_baton *dlbaton;
6299 struct call_site_parameter *parameter;
6300
6301 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6302 {
6303 /* Already printed the complaint above. */
6304 continue;
6305 }
6306
6307 gdb_assert (call_site->parameter_count < nparams);
6308 parameter = &call_site->parameter[call_site->parameter_count];
6309
6310 /* DW_AT_location specifies the register number. Value of the data
6311 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6312
6313 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6314 if (!attr || !attr_form_is_block (attr))
6315 {
6316 complaint (&symfile_complaints,
6317 _("No DW_FORM_block* DW_AT_location for "
6318 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6319 child_die->offset, objfile->name);
96408a79
SA
6320 continue;
6321 }
6322 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6323 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6324 if (parameter->dwarf_reg == -1
6325 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6326 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6327 &parameter->fb_offset))
6328 {
6329 complaint (&symfile_complaints,
6330 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6331 "for DW_FORM_block* DW_AT_location for "
6332 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6333 child_die->offset, objfile->name);
96408a79
SA
6334 continue;
6335 }
6336
6337 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6338 if (!attr_form_is_block (attr))
6339 {
6340 complaint (&symfile_complaints,
6341 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6342 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6343 child_die->offset, objfile->name);
96408a79
SA
6344 continue;
6345 }
6346 parameter->value = DW_BLOCK (attr)->data;
6347 parameter->value_size = DW_BLOCK (attr)->size;
6348
6349 /* Parameters are not pre-cleared by memset above. */
6350 parameter->data_value = NULL;
6351 parameter->data_value_size = 0;
6352 call_site->parameter_count++;
6353
6354 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6355 if (attr)
6356 {
6357 if (!attr_form_is_block (attr))
6358 complaint (&symfile_complaints,
6359 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6360 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
bb5ed363 6361 child_die->offset, objfile->name);
96408a79
SA
6362 else
6363 {
6364 parameter->data_value = DW_BLOCK (attr)->data;
6365 parameter->data_value_size = DW_BLOCK (attr)->size;
6366 }
6367 }
6368 }
6369}
6370
43039443 6371/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6372 Return 1 if the attributes are present and valid, otherwise, return 0.
6373 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6374
6375static int
6376dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6377 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6378 struct partial_symtab *ranges_pst)
43039443
JK
6379{
6380 struct objfile *objfile = cu->objfile;
6381 struct comp_unit_head *cu_header = &cu->header;
6382 bfd *obfd = objfile->obfd;
6383 unsigned int addr_size = cu_header->addr_size;
6384 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6385 /* Base address selection entry. */
6386 CORE_ADDR base;
6387 int found_base;
6388 unsigned int dummy;
6389 gdb_byte *buffer;
6390 CORE_ADDR marker;
6391 int low_set;
6392 CORE_ADDR low = 0;
6393 CORE_ADDR high = 0;
ff013f42 6394 CORE_ADDR baseaddr;
43039443 6395
d00adf39
DE
6396 found_base = cu->base_known;
6397 base = cu->base_address;
43039443 6398
be391dca 6399 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6400 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6401 {
6402 complaint (&symfile_complaints,
6403 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6404 offset);
6405 return 0;
6406 }
dce234bc 6407 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6408
6409 /* Read in the largest possible address. */
6410 marker = read_address (obfd, buffer, cu, &dummy);
6411 if ((marker & mask) == mask)
6412 {
6413 /* If we found the largest possible address, then
6414 read the base address. */
6415 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6416 buffer += 2 * addr_size;
6417 offset += 2 * addr_size;
6418 found_base = 1;
6419 }
6420
6421 low_set = 0;
6422
e7030f15 6423 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6424
43039443
JK
6425 while (1)
6426 {
6427 CORE_ADDR range_beginning, range_end;
6428
6429 range_beginning = read_address (obfd, buffer, cu, &dummy);
6430 buffer += addr_size;
6431 range_end = read_address (obfd, buffer, cu, &dummy);
6432 buffer += addr_size;
6433 offset += 2 * addr_size;
6434
6435 /* An end of list marker is a pair of zero addresses. */
6436 if (range_beginning == 0 && range_end == 0)
6437 /* Found the end of list entry. */
6438 break;
6439
6440 /* Each base address selection entry is a pair of 2 values.
6441 The first is the largest possible address, the second is
6442 the base address. Check for a base address here. */
6443 if ((range_beginning & mask) == mask)
6444 {
6445 /* If we found the largest possible address, then
6446 read the base address. */
6447 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6448 found_base = 1;
6449 continue;
6450 }
6451
6452 if (!found_base)
6453 {
6454 /* We have no valid base address for the ranges
6455 data. */
6456 complaint (&symfile_complaints,
6457 _("Invalid .debug_ranges data (no base address)"));
6458 return 0;
6459 }
6460
9277c30c
UW
6461 if (range_beginning > range_end)
6462 {
6463 /* Inverted range entries are invalid. */
6464 complaint (&symfile_complaints,
6465 _("Invalid .debug_ranges data (inverted range)"));
6466 return 0;
6467 }
6468
6469 /* Empty range entries have no effect. */
6470 if (range_beginning == range_end)
6471 continue;
6472
43039443
JK
6473 range_beginning += base;
6474 range_end += base;
6475
9277c30c 6476 if (ranges_pst != NULL)
ff013f42 6477 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6478 range_beginning + baseaddr,
6479 range_end - 1 + baseaddr,
ff013f42
JK
6480 ranges_pst);
6481
43039443
JK
6482 /* FIXME: This is recording everything as a low-high
6483 segment of consecutive addresses. We should have a
6484 data structure for discontiguous block ranges
6485 instead. */
6486 if (! low_set)
6487 {
6488 low = range_beginning;
6489 high = range_end;
6490 low_set = 1;
6491 }
6492 else
6493 {
6494 if (range_beginning < low)
6495 low = range_beginning;
6496 if (range_end > high)
6497 high = range_end;
6498 }
6499 }
6500
6501 if (! low_set)
6502 /* If the first entry is an end-of-list marker, the range
6503 describes an empty scope, i.e. no instructions. */
6504 return 0;
6505
6506 if (low_return)
6507 *low_return = low;
6508 if (high_return)
6509 *high_return = high;
6510 return 1;
6511}
6512
af34e669
DJ
6513/* Get low and high pc attributes from a die. Return 1 if the attributes
6514 are present and valid, otherwise, return 0. Return -1 if the range is
6515 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6516static int
af34e669 6517dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6518 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6519 struct partial_symtab *pst)
c906108c
SS
6520{
6521 struct attribute *attr;
af34e669
DJ
6522 CORE_ADDR low = 0;
6523 CORE_ADDR high = 0;
6524 int ret = 0;
c906108c 6525
e142c38c 6526 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6527 if (attr)
af34e669
DJ
6528 {
6529 high = DW_ADDR (attr);
e142c38c 6530 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6531 if (attr)
6532 low = DW_ADDR (attr);
6533 else
6534 /* Found high w/o low attribute. */
6535 return 0;
6536
6537 /* Found consecutive range of addresses. */
6538 ret = 1;
6539 }
c906108c 6540 else
af34e669 6541 {
e142c38c 6542 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6543 if (attr != NULL)
6544 {
af34e669 6545 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6546 .debug_ranges section. */
d85a05f0 6547 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6548 return 0;
43039443 6549 /* Found discontinuous range of addresses. */
af34e669
DJ
6550 ret = -1;
6551 }
6552 }
c906108c 6553
9373cf26
JK
6554 /* read_partial_die has also the strict LOW < HIGH requirement. */
6555 if (high <= low)
c906108c
SS
6556 return 0;
6557
6558 /* When using the GNU linker, .gnu.linkonce. sections are used to
6559 eliminate duplicate copies of functions and vtables and such.
6560 The linker will arbitrarily choose one and discard the others.
6561 The AT_*_pc values for such functions refer to local labels in
6562 these sections. If the section from that file was discarded, the
6563 labels are not in the output, so the relocs get a value of 0.
6564 If this is a discarded function, mark the pc bounds as invalid,
6565 so that GDB will ignore it. */
72dca2f5 6566 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6567 return 0;
6568
6569 *lowpc = low;
96408a79
SA
6570 if (highpc)
6571 *highpc = high;
af34e669 6572 return ret;
c906108c
SS
6573}
6574
b084d499
JB
6575/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6576 its low and high PC addresses. Do nothing if these addresses could not
6577 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6578 and HIGHPC to the high address if greater than HIGHPC. */
6579
6580static void
6581dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6582 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6583 struct dwarf2_cu *cu)
6584{
6585 CORE_ADDR low, high;
6586 struct die_info *child = die->child;
6587
d85a05f0 6588 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6589 {
6590 *lowpc = min (*lowpc, low);
6591 *highpc = max (*highpc, high);
6592 }
6593
6594 /* If the language does not allow nested subprograms (either inside
6595 subprograms or lexical blocks), we're done. */
6596 if (cu->language != language_ada)
6597 return;
6e70227d 6598
b084d499
JB
6599 /* Check all the children of the given DIE. If it contains nested
6600 subprograms, then check their pc bounds. Likewise, we need to
6601 check lexical blocks as well, as they may also contain subprogram
6602 definitions. */
6603 while (child && child->tag)
6604 {
6605 if (child->tag == DW_TAG_subprogram
6606 || child->tag == DW_TAG_lexical_block)
6607 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6608 child = sibling_die (child);
6609 }
6610}
6611
fae299cd
DC
6612/* Get the low and high pc's represented by the scope DIE, and store
6613 them in *LOWPC and *HIGHPC. If the correct values can't be
6614 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6615
6616static void
6617get_scope_pc_bounds (struct die_info *die,
6618 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6619 struct dwarf2_cu *cu)
6620{
6621 CORE_ADDR best_low = (CORE_ADDR) -1;
6622 CORE_ADDR best_high = (CORE_ADDR) 0;
6623 CORE_ADDR current_low, current_high;
6624
d85a05f0 6625 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6626 {
6627 best_low = current_low;
6628 best_high = current_high;
6629 }
6630 else
6631 {
6632 struct die_info *child = die->child;
6633
6634 while (child && child->tag)
6635 {
6636 switch (child->tag) {
6637 case DW_TAG_subprogram:
b084d499 6638 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6639 break;
6640 case DW_TAG_namespace:
f55ee35c 6641 case DW_TAG_module:
fae299cd
DC
6642 /* FIXME: carlton/2004-01-16: Should we do this for
6643 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6644 that current GCC's always emit the DIEs corresponding
6645 to definitions of methods of classes as children of a
6646 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6647 the DIEs giving the declarations, which could be
6648 anywhere). But I don't see any reason why the
6649 standards says that they have to be there. */
6650 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6651
6652 if (current_low != ((CORE_ADDR) -1))
6653 {
6654 best_low = min (best_low, current_low);
6655 best_high = max (best_high, current_high);
6656 }
6657 break;
6658 default:
0963b4bd 6659 /* Ignore. */
fae299cd
DC
6660 break;
6661 }
6662
6663 child = sibling_die (child);
6664 }
6665 }
6666
6667 *lowpc = best_low;
6668 *highpc = best_high;
6669}
6670
801e3a5b
JB
6671/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6672 in DIE. */
6673static void
6674dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6675 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6676{
bb5ed363 6677 struct objfile *objfile = cu->objfile;
801e3a5b
JB
6678 struct attribute *attr;
6679
6680 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6681 if (attr)
6682 {
6683 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6684
801e3a5b
JB
6685 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6686 if (attr)
6687 {
6688 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6689
801e3a5b
JB
6690 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6691 }
6692 }
6693
6694 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6695 if (attr)
6696 {
bb5ed363 6697 bfd *obfd = objfile->obfd;
801e3a5b
JB
6698
6699 /* The value of the DW_AT_ranges attribute is the offset of the
6700 address range list in the .debug_ranges section. */
6701 unsigned long offset = DW_UNSND (attr);
dce234bc 6702 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6703
6704 /* For some target architectures, but not others, the
6705 read_address function sign-extends the addresses it returns.
6706 To recognize base address selection entries, we need a
6707 mask. */
6708 unsigned int addr_size = cu->header.addr_size;
6709 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6710
6711 /* The base address, to which the next pair is relative. Note
6712 that this 'base' is a DWARF concept: most entries in a range
6713 list are relative, to reduce the number of relocs against the
6714 debugging information. This is separate from this function's
6715 'baseaddr' argument, which GDB uses to relocate debugging
6716 information from a shared library based on the address at
6717 which the library was loaded. */
d00adf39
DE
6718 CORE_ADDR base = cu->base_address;
6719 int base_known = cu->base_known;
801e3a5b 6720
be391dca 6721 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6722 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6723 {
6724 complaint (&symfile_complaints,
6725 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6726 offset);
6727 return;
6728 }
6729
6730 for (;;)
6731 {
6732 unsigned int bytes_read;
6733 CORE_ADDR start, end;
6734
6735 start = read_address (obfd, buffer, cu, &bytes_read);
6736 buffer += bytes_read;
6737 end = read_address (obfd, buffer, cu, &bytes_read);
6738 buffer += bytes_read;
6739
6740 /* Did we find the end of the range list? */
6741 if (start == 0 && end == 0)
6742 break;
6743
6744 /* Did we find a base address selection entry? */
6745 else if ((start & base_select_mask) == base_select_mask)
6746 {
6747 base = end;
6748 base_known = 1;
6749 }
6750
6751 /* We found an ordinary address range. */
6752 else
6753 {
6754 if (!base_known)
6755 {
6756 complaint (&symfile_complaints,
3e43a32a
MS
6757 _("Invalid .debug_ranges data "
6758 "(no base address)"));
801e3a5b
JB
6759 return;
6760 }
6761
9277c30c
UW
6762 if (start > end)
6763 {
6764 /* Inverted range entries are invalid. */
6765 complaint (&symfile_complaints,
6766 _("Invalid .debug_ranges data "
6767 "(inverted range)"));
6768 return;
6769 }
6770
6771 /* Empty range entries have no effect. */
6772 if (start == end)
6773 continue;
6774
6e70227d
DE
6775 record_block_range (block,
6776 baseaddr + base + start,
801e3a5b
JB
6777 baseaddr + base + end - 1);
6778 }
6779 }
6780 }
6781}
6782
60d5a603
JK
6783/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6784 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6785 during 4.6.0 experimental. */
6786
6787static int
6788producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6789{
6790 const char *cs;
6791 int major, minor, release;
6792
6793 if (cu->producer == NULL)
6794 {
6795 /* For unknown compilers expect their behavior is DWARF version
6796 compliant.
6797
6798 GCC started to support .debug_types sections by -gdwarf-4 since
6799 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6800 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6801 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6802 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6803
6804 return 0;
6805 }
6806
6807 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6808
6809 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6810 {
6811 /* For non-GCC compilers expect their behavior is DWARF version
6812 compliant. */
6813
6814 return 0;
6815 }
6816 cs = &cu->producer[strlen ("GNU ")];
6817 while (*cs && !isdigit (*cs))
6818 cs++;
6819 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6820 {
6821 /* Not recognized as GCC. */
6822
6823 return 0;
6824 }
6825
6826 return major < 4 || (major == 4 && minor < 6);
6827}
6828
6829/* Return the default accessibility type if it is not overriden by
6830 DW_AT_accessibility. */
6831
6832static enum dwarf_access_attribute
6833dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6834{
6835 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6836 {
6837 /* The default DWARF 2 accessibility for members is public, the default
6838 accessibility for inheritance is private. */
6839
6840 if (die->tag != DW_TAG_inheritance)
6841 return DW_ACCESS_public;
6842 else
6843 return DW_ACCESS_private;
6844 }
6845 else
6846 {
6847 /* DWARF 3+ defines the default accessibility a different way. The same
6848 rules apply now for DW_TAG_inheritance as for the members and it only
6849 depends on the container kind. */
6850
6851 if (die->parent->tag == DW_TAG_class_type)
6852 return DW_ACCESS_private;
6853 else
6854 return DW_ACCESS_public;
6855 }
6856}
6857
74ac6d43
TT
6858/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6859 offset. If the attribute was not found return 0, otherwise return
6860 1. If it was found but could not properly be handled, set *OFFSET
6861 to 0. */
6862
6863static int
6864handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6865 LONGEST *offset)
6866{
6867 struct attribute *attr;
6868
6869 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6870 if (attr != NULL)
6871 {
6872 *offset = 0;
6873
6874 /* Note that we do not check for a section offset first here.
6875 This is because DW_AT_data_member_location is new in DWARF 4,
6876 so if we see it, we can assume that a constant form is really
6877 a constant and not a section offset. */
6878 if (attr_form_is_constant (attr))
6879 *offset = dwarf2_get_attr_constant_value (attr, 0);
6880 else if (attr_form_is_section_offset (attr))
6881 dwarf2_complex_location_expr_complaint ();
6882 else if (attr_form_is_block (attr))
6883 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6884 else
6885 dwarf2_complex_location_expr_complaint ();
6886
6887 return 1;
6888 }
6889
6890 return 0;
6891}
6892
c906108c
SS
6893/* Add an aggregate field to the field list. */
6894
6895static void
107d2387 6896dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6897 struct dwarf2_cu *cu)
6e70227d 6898{
e7c27a73 6899 struct objfile *objfile = cu->objfile;
5e2b427d 6900 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6901 struct nextfield *new_field;
6902 struct attribute *attr;
6903 struct field *fp;
6904 char *fieldname = "";
6905
6906 /* Allocate a new field list entry and link it in. */
6907 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6908 make_cleanup (xfree, new_field);
c906108c 6909 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6910
6911 if (die->tag == DW_TAG_inheritance)
6912 {
6913 new_field->next = fip->baseclasses;
6914 fip->baseclasses = new_field;
6915 }
6916 else
6917 {
6918 new_field->next = fip->fields;
6919 fip->fields = new_field;
6920 }
c906108c
SS
6921 fip->nfields++;
6922
e142c38c 6923 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6924 if (attr)
6925 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6926 else
6927 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6928 if (new_field->accessibility != DW_ACCESS_public)
6929 fip->non_public_fields = 1;
60d5a603 6930
e142c38c 6931 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6932 if (attr)
6933 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6934 else
6935 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6936
6937 fp = &new_field->field;
a9a9bd0f 6938
e142c38c 6939 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6940 {
74ac6d43
TT
6941 LONGEST offset;
6942
a9a9bd0f 6943 /* Data member other than a C++ static data member. */
6e70227d 6944
c906108c 6945 /* Get type of field. */
e7c27a73 6946 fp->type = die_type (die, cu);
c906108c 6947
d6a843b5 6948 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6949
c906108c 6950 /* Get bit size of field (zero if none). */
e142c38c 6951 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6952 if (attr)
6953 {
6954 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6955 }
6956 else
6957 {
6958 FIELD_BITSIZE (*fp) = 0;
6959 }
6960
6961 /* Get bit offset of field. */
74ac6d43
TT
6962 if (handle_data_member_location (die, cu, &offset))
6963 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 6964 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6965 if (attr)
6966 {
5e2b427d 6967 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6968 {
6969 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6970 additional bit offset from the MSB of the containing
6971 anonymous object to the MSB of the field. We don't
6972 have to do anything special since we don't need to
6973 know the size of the anonymous object. */
c906108c
SS
6974 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6975 }
6976 else
6977 {
6978 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6979 MSB of the anonymous object, subtract off the number of
6980 bits from the MSB of the field to the MSB of the
6981 object, and then subtract off the number of bits of
6982 the field itself. The result is the bit offset of
6983 the LSB of the field. */
c906108c
SS
6984 int anonymous_size;
6985 int bit_offset = DW_UNSND (attr);
6986
e142c38c 6987 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6988 if (attr)
6989 {
6990 /* The size of the anonymous object containing
6991 the bit field is explicit, so use the
6992 indicated size (in bytes). */
6993 anonymous_size = DW_UNSND (attr);
6994 }
6995 else
6996 {
6997 /* The size of the anonymous object containing
6998 the bit field must be inferred from the type
6999 attribute of the data member containing the
7000 bit field. */
7001 anonymous_size = TYPE_LENGTH (fp->type);
7002 }
7003 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7004 - bit_offset - FIELD_BITSIZE (*fp);
7005 }
7006 }
7007
7008 /* Get name of field. */
39cbfefa
DJ
7009 fieldname = dwarf2_name (die, cu);
7010 if (fieldname == NULL)
7011 fieldname = "";
d8151005
DJ
7012
7013 /* The name is already allocated along with this objfile, so we don't
7014 need to duplicate it for the type. */
7015 fp->name = fieldname;
c906108c
SS
7016
7017 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7018 pointer or virtual base class pointer) to private. */
e142c38c 7019 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7020 {
d48cc9dd 7021 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7022 new_field->accessibility = DW_ACCESS_private;
7023 fip->non_public_fields = 1;
7024 }
7025 }
a9a9bd0f 7026 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7027 {
a9a9bd0f
DC
7028 /* C++ static member. */
7029
7030 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7031 is a declaration, but all versions of G++ as of this writing
7032 (so through at least 3.2.1) incorrectly generate
7033 DW_TAG_variable tags. */
6e70227d 7034
ff355380 7035 const char *physname;
c906108c 7036
a9a9bd0f 7037 /* Get name of field. */
39cbfefa
DJ
7038 fieldname = dwarf2_name (die, cu);
7039 if (fieldname == NULL)
c906108c
SS
7040 return;
7041
254e6b9e 7042 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7043 if (attr
7044 /* Only create a symbol if this is an external value.
7045 new_symbol checks this and puts the value in the global symbol
7046 table, which we want. If it is not external, new_symbol
7047 will try to put the value in cu->list_in_scope which is wrong. */
7048 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7049 {
7050 /* A static const member, not much different than an enum as far as
7051 we're concerned, except that we can support more types. */
7052 new_symbol (die, NULL, cu);
7053 }
7054
2df3850c 7055 /* Get physical name. */
ff355380 7056 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7057
d8151005
DJ
7058 /* The name is already allocated along with this objfile, so we don't
7059 need to duplicate it for the type. */
7060 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7061 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7062 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7063 }
7064 else if (die->tag == DW_TAG_inheritance)
7065 {
74ac6d43 7066 LONGEST offset;
d4b96c9a 7067
74ac6d43
TT
7068 /* C++ base class field. */
7069 if (handle_data_member_location (die, cu, &offset))
7070 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7071 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7072 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7073 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7074 fip->nbaseclasses++;
7075 }
7076}
7077
98751a41
JK
7078/* Add a typedef defined in the scope of the FIP's class. */
7079
7080static void
7081dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7082 struct dwarf2_cu *cu)
6e70227d 7083{
98751a41 7084 struct objfile *objfile = cu->objfile;
98751a41
JK
7085 struct typedef_field_list *new_field;
7086 struct attribute *attr;
7087 struct typedef_field *fp;
7088 char *fieldname = "";
7089
7090 /* Allocate a new field list entry and link it in. */
7091 new_field = xzalloc (sizeof (*new_field));
7092 make_cleanup (xfree, new_field);
7093
7094 gdb_assert (die->tag == DW_TAG_typedef);
7095
7096 fp = &new_field->field;
7097
7098 /* Get name of field. */
7099 fp->name = dwarf2_name (die, cu);
7100 if (fp->name == NULL)
7101 return;
7102
7103 fp->type = read_type_die (die, cu);
7104
7105 new_field->next = fip->typedef_field_list;
7106 fip->typedef_field_list = new_field;
7107 fip->typedef_field_list_count++;
7108}
7109
c906108c
SS
7110/* Create the vector of fields, and attach it to the type. */
7111
7112static void
fba45db2 7113dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7114 struct dwarf2_cu *cu)
c906108c
SS
7115{
7116 int nfields = fip->nfields;
7117
7118 /* Record the field count, allocate space for the array of fields,
7119 and create blank accessibility bitfields if necessary. */
7120 TYPE_NFIELDS (type) = nfields;
7121 TYPE_FIELDS (type) = (struct field *)
7122 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7123 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7124
b4ba55a1 7125 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7126 {
7127 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7128
7129 TYPE_FIELD_PRIVATE_BITS (type) =
7130 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7131 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7132
7133 TYPE_FIELD_PROTECTED_BITS (type) =
7134 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7135 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7136
774b6a14
TT
7137 TYPE_FIELD_IGNORE_BITS (type) =
7138 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7139 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7140 }
7141
7142 /* If the type has baseclasses, allocate and clear a bit vector for
7143 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7144 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7145 {
7146 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7147 unsigned char *pointer;
c906108c
SS
7148
7149 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7150 pointer = TYPE_ALLOC (type, num_bytes);
7151 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7152 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7153 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7154 }
7155
3e43a32a
MS
7156 /* Copy the saved-up fields into the field vector. Start from the head of
7157 the list, adding to the tail of the field array, so that they end up in
7158 the same order in the array in which they were added to the list. */
c906108c
SS
7159 while (nfields-- > 0)
7160 {
7d0ccb61
DJ
7161 struct nextfield *fieldp;
7162
7163 if (fip->fields)
7164 {
7165 fieldp = fip->fields;
7166 fip->fields = fieldp->next;
7167 }
7168 else
7169 {
7170 fieldp = fip->baseclasses;
7171 fip->baseclasses = fieldp->next;
7172 }
7173
7174 TYPE_FIELD (type, nfields) = fieldp->field;
7175 switch (fieldp->accessibility)
c906108c 7176 {
c5aa993b 7177 case DW_ACCESS_private:
b4ba55a1
JB
7178 if (cu->language != language_ada)
7179 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7180 break;
c906108c 7181
c5aa993b 7182 case DW_ACCESS_protected:
b4ba55a1
JB
7183 if (cu->language != language_ada)
7184 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7185 break;
c906108c 7186
c5aa993b
JM
7187 case DW_ACCESS_public:
7188 break;
c906108c 7189
c5aa993b
JM
7190 default:
7191 /* Unknown accessibility. Complain and treat it as public. */
7192 {
e2e0b3e5 7193 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7194 fieldp->accessibility);
c5aa993b
JM
7195 }
7196 break;
c906108c
SS
7197 }
7198 if (nfields < fip->nbaseclasses)
7199 {
7d0ccb61 7200 switch (fieldp->virtuality)
c906108c 7201 {
c5aa993b
JM
7202 case DW_VIRTUALITY_virtual:
7203 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7204 if (cu->language == language_ada)
a73c6dcd 7205 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7206 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7207 break;
c906108c
SS
7208 }
7209 }
c906108c
SS
7210 }
7211}
7212
c906108c
SS
7213/* Add a member function to the proper fieldlist. */
7214
7215static void
107d2387 7216dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7217 struct type *type, struct dwarf2_cu *cu)
c906108c 7218{
e7c27a73 7219 struct objfile *objfile = cu->objfile;
c906108c
SS
7220 struct attribute *attr;
7221 struct fnfieldlist *flp;
7222 int i;
7223 struct fn_field *fnp;
7224 char *fieldname;
c906108c 7225 struct nextfnfield *new_fnfield;
f792889a 7226 struct type *this_type;
60d5a603 7227 enum dwarf_access_attribute accessibility;
c906108c 7228
b4ba55a1 7229 if (cu->language == language_ada)
a73c6dcd 7230 error (_("unexpected member function in Ada type"));
b4ba55a1 7231
2df3850c 7232 /* Get name of member function. */
39cbfefa
DJ
7233 fieldname = dwarf2_name (die, cu);
7234 if (fieldname == NULL)
2df3850c 7235 return;
c906108c 7236
c906108c
SS
7237 /* Look up member function name in fieldlist. */
7238 for (i = 0; i < fip->nfnfields; i++)
7239 {
27bfe10e 7240 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7241 break;
7242 }
7243
7244 /* Create new list element if necessary. */
7245 if (i < fip->nfnfields)
7246 flp = &fip->fnfieldlists[i];
7247 else
7248 {
7249 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7250 {
7251 fip->fnfieldlists = (struct fnfieldlist *)
7252 xrealloc (fip->fnfieldlists,
7253 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7254 * sizeof (struct fnfieldlist));
c906108c 7255 if (fip->nfnfields == 0)
c13c43fd 7256 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7257 }
7258 flp = &fip->fnfieldlists[fip->nfnfields];
7259 flp->name = fieldname;
7260 flp->length = 0;
7261 flp->head = NULL;
3da10d80 7262 i = fip->nfnfields++;
c906108c
SS
7263 }
7264
7265 /* Create a new member function field and chain it to the field list
0963b4bd 7266 entry. */
c906108c 7267 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7268 make_cleanup (xfree, new_fnfield);
c906108c
SS
7269 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7270 new_fnfield->next = flp->head;
7271 flp->head = new_fnfield;
7272 flp->length++;
7273
7274 /* Fill in the member function field info. */
7275 fnp = &new_fnfield->fnfield;
3da10d80
KS
7276
7277 /* Delay processing of the physname until later. */
7278 if (cu->language == language_cplus || cu->language == language_java)
7279 {
7280 add_to_method_list (type, i, flp->length - 1, fieldname,
7281 die, cu);
7282 }
7283 else
7284 {
1d06ead6 7285 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7286 fnp->physname = physname ? physname : "";
7287 }
7288
c906108c 7289 fnp->type = alloc_type (objfile);
f792889a
DJ
7290 this_type = read_type_die (die, cu);
7291 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7292 {
f792889a 7293 int nparams = TYPE_NFIELDS (this_type);
c906108c 7294
f792889a 7295 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7296 of the method itself (TYPE_CODE_METHOD). */
7297 smash_to_method_type (fnp->type, type,
f792889a
DJ
7298 TYPE_TARGET_TYPE (this_type),
7299 TYPE_FIELDS (this_type),
7300 TYPE_NFIELDS (this_type),
7301 TYPE_VARARGS (this_type));
c906108c
SS
7302
7303 /* Handle static member functions.
c5aa993b 7304 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7305 member functions. G++ helps GDB by marking the first
7306 parameter for non-static member functions (which is the this
7307 pointer) as artificial. We obtain this information from
7308 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7309 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7310 fnp->voffset = VOFFSET_STATIC;
7311 }
7312 else
e2e0b3e5 7313 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7314 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7315
7316 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7317 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7318 fnp->fcontext = die_containing_type (die, cu);
c906108c 7319
3e43a32a
MS
7320 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7321 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7322
7323 /* Get accessibility. */
e142c38c 7324 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7325 if (attr)
60d5a603
JK
7326 accessibility = DW_UNSND (attr);
7327 else
7328 accessibility = dwarf2_default_access_attribute (die, cu);
7329 switch (accessibility)
c906108c 7330 {
60d5a603
JK
7331 case DW_ACCESS_private:
7332 fnp->is_private = 1;
7333 break;
7334 case DW_ACCESS_protected:
7335 fnp->is_protected = 1;
7336 break;
c906108c
SS
7337 }
7338
b02dede2 7339 /* Check for artificial methods. */
e142c38c 7340 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7341 if (attr && DW_UNSND (attr) != 0)
7342 fnp->is_artificial = 1;
7343
0d564a31 7344 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7345 function. For older versions of GCC, this is an offset in the
7346 appropriate virtual table, as specified by DW_AT_containing_type.
7347 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7348 to the object address. */
7349
e142c38c 7350 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7351 if (attr)
8e19ed76 7352 {
aec5aa8b 7353 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7354 {
aec5aa8b
TT
7355 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7356 {
7357 /* Old-style GCC. */
7358 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7359 }
7360 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7361 || (DW_BLOCK (attr)->size > 1
7362 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7363 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7364 {
7365 struct dwarf_block blk;
7366 int offset;
7367
7368 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7369 ? 1 : 2);
7370 blk.size = DW_BLOCK (attr)->size - offset;
7371 blk.data = DW_BLOCK (attr)->data + offset;
7372 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7373 if ((fnp->voffset % cu->header.addr_size) != 0)
7374 dwarf2_complex_location_expr_complaint ();
7375 else
7376 fnp->voffset /= cu->header.addr_size;
7377 fnp->voffset += 2;
7378 }
7379 else
7380 dwarf2_complex_location_expr_complaint ();
7381
7382 if (!fnp->fcontext)
7383 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7384 }
3690dd37 7385 else if (attr_form_is_section_offset (attr))
8e19ed76 7386 {
4d3c2250 7387 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7388 }
7389 else
7390 {
4d3c2250
KB
7391 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7392 fieldname);
8e19ed76 7393 }
0d564a31 7394 }
d48cc9dd
DJ
7395 else
7396 {
7397 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7398 if (attr && DW_UNSND (attr))
7399 {
7400 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7401 complaint (&symfile_complaints,
3e43a32a
MS
7402 _("Member function \"%s\" (offset %d) is virtual "
7403 "but the vtable offset is not specified"),
d48cc9dd 7404 fieldname, die->offset);
9655fd1a 7405 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7406 TYPE_CPLUS_DYNAMIC (type) = 1;
7407 }
7408 }
c906108c
SS
7409}
7410
7411/* Create the vector of member function fields, and attach it to the type. */
7412
7413static void
fba45db2 7414dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7415 struct dwarf2_cu *cu)
c906108c
SS
7416{
7417 struct fnfieldlist *flp;
c906108c
SS
7418 int i;
7419
b4ba55a1 7420 if (cu->language == language_ada)
a73c6dcd 7421 error (_("unexpected member functions in Ada type"));
b4ba55a1 7422
c906108c
SS
7423 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7424 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7425 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7426
7427 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7428 {
7429 struct nextfnfield *nfp = flp->head;
7430 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7431 int k;
7432
7433 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7434 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7435 fn_flp->fn_fields = (struct fn_field *)
7436 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7437 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7438 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7439 }
7440
7441 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
7442}
7443
1168df01
JB
7444/* Returns non-zero if NAME is the name of a vtable member in CU's
7445 language, zero otherwise. */
7446static int
7447is_vtable_name (const char *name, struct dwarf2_cu *cu)
7448{
7449 static const char vptr[] = "_vptr";
987504bb 7450 static const char vtable[] = "vtable";
1168df01 7451
987504bb
JJ
7452 /* Look for the C++ and Java forms of the vtable. */
7453 if ((cu->language == language_java
7454 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7455 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7456 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7457 return 1;
7458
7459 return 0;
7460}
7461
c0dd20ea 7462/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7463 functions, with the ABI-specified layout. If TYPE describes
7464 such a structure, smash it into a member function type.
61049d3b
DJ
7465
7466 GCC shouldn't do this; it should just output pointer to member DIEs.
7467 This is GCC PR debug/28767. */
c0dd20ea 7468
0b92b5bb
TT
7469static void
7470quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7471{
0b92b5bb 7472 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7473
7474 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7475 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7476 return;
c0dd20ea
DJ
7477
7478 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7479 if (TYPE_FIELD_NAME (type, 0) == NULL
7480 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7481 || TYPE_FIELD_NAME (type, 1) == NULL
7482 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7483 return;
c0dd20ea
DJ
7484
7485 /* Find the type of the method. */
0b92b5bb 7486 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7487 if (pfn_type == NULL
7488 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7489 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7490 return;
c0dd20ea
DJ
7491
7492 /* Look for the "this" argument. */
7493 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7494 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7495 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7496 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7497 return;
c0dd20ea
DJ
7498
7499 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7500 new_type = alloc_type (objfile);
7501 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7502 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7503 TYPE_VARARGS (pfn_type));
0b92b5bb 7504 smash_to_methodptr_type (type, new_type);
c0dd20ea 7505}
1168df01 7506
c906108c 7507/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7508 (definition) to create a type for the structure or union. Fill in
7509 the type's name and general properties; the members will not be
7510 processed until process_structure_type.
c906108c 7511
c767944b
DJ
7512 NOTE: we need to call these functions regardless of whether or not the
7513 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7514 structure or union. This gets the type entered into our set of
7515 user defined types.
7516
7517 However, if the structure is incomplete (an opaque struct/union)
7518 then suppress creating a symbol table entry for it since gdb only
7519 wants to find the one with the complete definition. Note that if
7520 it is complete, we just call new_symbol, which does it's own
7521 checking about whether the struct/union is anonymous or not (and
7522 suppresses creating a symbol table entry itself). */
7523
f792889a 7524static struct type *
134d01f1 7525read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7526{
e7c27a73 7527 struct objfile *objfile = cu->objfile;
c906108c
SS
7528 struct type *type;
7529 struct attribute *attr;
39cbfefa 7530 char *name;
c906108c 7531
348e048f
DE
7532 /* If the definition of this type lives in .debug_types, read that type.
7533 Don't follow DW_AT_specification though, that will take us back up
7534 the chain and we want to go down. */
7535 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7536 if (attr)
7537 {
7538 struct dwarf2_cu *type_cu = cu;
7539 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7540
348e048f
DE
7541 /* We could just recurse on read_structure_type, but we need to call
7542 get_die_type to ensure only one type for this DIE is created.
7543 This is important, for example, because for c++ classes we need
7544 TYPE_NAME set which is only done by new_symbol. Blech. */
7545 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7546
7547 /* TYPE_CU may not be the same as CU.
7548 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7549 return set_die_type (die, type, cu);
7550 }
7551
c0dd20ea 7552 type = alloc_type (objfile);
c906108c 7553 INIT_CPLUS_SPECIFIC (type);
93311388 7554
39cbfefa
DJ
7555 name = dwarf2_name (die, cu);
7556 if (name != NULL)
c906108c 7557 {
987504bb
JJ
7558 if (cu->language == language_cplus
7559 || cu->language == language_java)
63d06c5c 7560 {
3da10d80
KS
7561 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7562
7563 /* dwarf2_full_name might have already finished building the DIE's
7564 type. If so, there is no need to continue. */
7565 if (get_die_type (die, cu) != NULL)
7566 return get_die_type (die, cu);
7567
7568 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7569 if (die->tag == DW_TAG_structure_type
7570 || die->tag == DW_TAG_class_type)
7571 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7572 }
7573 else
7574 {
d8151005
DJ
7575 /* The name is already allocated along with this objfile, so
7576 we don't need to duplicate it for the type. */
94af9270
KS
7577 TYPE_TAG_NAME (type) = (char *) name;
7578 if (die->tag == DW_TAG_class_type)
7579 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7580 }
c906108c
SS
7581 }
7582
7583 if (die->tag == DW_TAG_structure_type)
7584 {
7585 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7586 }
7587 else if (die->tag == DW_TAG_union_type)
7588 {
7589 TYPE_CODE (type) = TYPE_CODE_UNION;
7590 }
7591 else
7592 {
c906108c
SS
7593 TYPE_CODE (type) = TYPE_CODE_CLASS;
7594 }
7595
0cc2414c
TT
7596 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7597 TYPE_DECLARED_CLASS (type) = 1;
7598
e142c38c 7599 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7600 if (attr)
7601 {
7602 TYPE_LENGTH (type) = DW_UNSND (attr);
7603 }
7604 else
7605 {
7606 TYPE_LENGTH (type) = 0;
7607 }
7608
876cecd0 7609 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7610 if (die_is_declaration (die, cu))
876cecd0 7611 TYPE_STUB (type) = 1;
a6c727b2
DJ
7612 else if (attr == NULL && die->child == NULL
7613 && producer_is_realview (cu->producer))
7614 /* RealView does not output the required DW_AT_declaration
7615 on incomplete types. */
7616 TYPE_STUB (type) = 1;
dc718098 7617
c906108c
SS
7618 /* We need to add the type field to the die immediately so we don't
7619 infinitely recurse when dealing with pointers to the structure
0963b4bd 7620 type within the structure itself. */
1c379e20 7621 set_die_type (die, type, cu);
c906108c 7622
7e314c57
JK
7623 /* set_die_type should be already done. */
7624 set_descriptive_type (type, die, cu);
7625
c767944b
DJ
7626 return type;
7627}
7628
7629/* Finish creating a structure or union type, including filling in
7630 its members and creating a symbol for it. */
7631
7632static void
7633process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7634{
7635 struct objfile *objfile = cu->objfile;
7636 struct die_info *child_die = die->child;
7637 struct type *type;
7638
7639 type = get_die_type (die, cu);
7640 if (type == NULL)
7641 type = read_structure_type (die, cu);
7642
e142c38c 7643 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7644 {
7645 struct field_info fi;
7646 struct die_info *child_die;
34eaf542 7647 VEC (symbolp) *template_args = NULL;
c767944b 7648 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7649
7650 memset (&fi, 0, sizeof (struct field_info));
7651
639d11d3 7652 child_die = die->child;
c906108c
SS
7653
7654 while (child_die && child_die->tag)
7655 {
a9a9bd0f
DC
7656 if (child_die->tag == DW_TAG_member
7657 || child_die->tag == DW_TAG_variable)
c906108c 7658 {
a9a9bd0f
DC
7659 /* NOTE: carlton/2002-11-05: A C++ static data member
7660 should be a DW_TAG_member that is a declaration, but
7661 all versions of G++ as of this writing (so through at
7662 least 3.2.1) incorrectly generate DW_TAG_variable
7663 tags for them instead. */
e7c27a73 7664 dwarf2_add_field (&fi, child_die, cu);
c906108c 7665 }
8713b1b1 7666 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7667 {
0963b4bd 7668 /* C++ member function. */
e7c27a73 7669 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7670 }
7671 else if (child_die->tag == DW_TAG_inheritance)
7672 {
7673 /* C++ base class field. */
e7c27a73 7674 dwarf2_add_field (&fi, child_die, cu);
c906108c 7675 }
98751a41
JK
7676 else if (child_die->tag == DW_TAG_typedef)
7677 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7678 else if (child_die->tag == DW_TAG_template_type_param
7679 || child_die->tag == DW_TAG_template_value_param)
7680 {
7681 struct symbol *arg = new_symbol (child_die, NULL, cu);
7682
f1078f66
DJ
7683 if (arg != NULL)
7684 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7685 }
7686
c906108c
SS
7687 child_die = sibling_die (child_die);
7688 }
7689
34eaf542
TT
7690 /* Attach template arguments to type. */
7691 if (! VEC_empty (symbolp, template_args))
7692 {
7693 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7694 TYPE_N_TEMPLATE_ARGUMENTS (type)
7695 = VEC_length (symbolp, template_args);
7696 TYPE_TEMPLATE_ARGUMENTS (type)
7697 = obstack_alloc (&objfile->objfile_obstack,
7698 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7699 * sizeof (struct symbol *)));
7700 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7701 VEC_address (symbolp, template_args),
7702 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7703 * sizeof (struct symbol *)));
7704 VEC_free (symbolp, template_args);
7705 }
7706
c906108c
SS
7707 /* Attach fields and member functions to the type. */
7708 if (fi.nfields)
e7c27a73 7709 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7710 if (fi.nfnfields)
7711 {
e7c27a73 7712 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7713
c5aa993b 7714 /* Get the type which refers to the base class (possibly this
c906108c 7715 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7716 class from the DW_AT_containing_type attribute. This use of
7717 DW_AT_containing_type is a GNU extension. */
c906108c 7718
e142c38c 7719 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7720 {
e7c27a73 7721 struct type *t = die_containing_type (die, cu);
c906108c
SS
7722
7723 TYPE_VPTR_BASETYPE (type) = t;
7724 if (type == t)
7725 {
c906108c
SS
7726 int i;
7727
7728 /* Our own class provides vtbl ptr. */
7729 for (i = TYPE_NFIELDS (t) - 1;
7730 i >= TYPE_N_BASECLASSES (t);
7731 --i)
7732 {
7733 char *fieldname = TYPE_FIELD_NAME (t, i);
7734
1168df01 7735 if (is_vtable_name (fieldname, cu))
c906108c
SS
7736 {
7737 TYPE_VPTR_FIELDNO (type) = i;
7738 break;
7739 }
7740 }
7741
7742 /* Complain if virtual function table field not found. */
7743 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7744 complaint (&symfile_complaints,
3e43a32a
MS
7745 _("virtual function table pointer "
7746 "not found when defining class '%s'"),
4d3c2250
KB
7747 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7748 "");
c906108c
SS
7749 }
7750 else
7751 {
7752 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7753 }
7754 }
f6235d4c
EZ
7755 else if (cu->producer
7756 && strncmp (cu->producer,
7757 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7758 {
7759 /* The IBM XLC compiler does not provide direct indication
7760 of the containing type, but the vtable pointer is
7761 always named __vfp. */
7762
7763 int i;
7764
7765 for (i = TYPE_NFIELDS (type) - 1;
7766 i >= TYPE_N_BASECLASSES (type);
7767 --i)
7768 {
7769 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7770 {
7771 TYPE_VPTR_FIELDNO (type) = i;
7772 TYPE_VPTR_BASETYPE (type) = type;
7773 break;
7774 }
7775 }
7776 }
c906108c 7777 }
98751a41
JK
7778
7779 /* Copy fi.typedef_field_list linked list elements content into the
7780 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7781 if (fi.typedef_field_list)
7782 {
7783 int i = fi.typedef_field_list_count;
7784
a0d7a4ff 7785 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7786 TYPE_TYPEDEF_FIELD_ARRAY (type)
7787 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7788 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7789
7790 /* Reverse the list order to keep the debug info elements order. */
7791 while (--i >= 0)
7792 {
7793 struct typedef_field *dest, *src;
6e70227d 7794
98751a41
JK
7795 dest = &TYPE_TYPEDEF_FIELD (type, i);
7796 src = &fi.typedef_field_list->field;
7797 fi.typedef_field_list = fi.typedef_field_list->next;
7798 *dest = *src;
7799 }
7800 }
c767944b
DJ
7801
7802 do_cleanups (back_to);
eb2a6f42
TT
7803
7804 if (HAVE_CPLUS_STRUCT (type))
7805 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7806 }
63d06c5c 7807
bb5ed363 7808 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 7809
90aeadfc
DC
7810 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7811 snapshots) has been known to create a die giving a declaration
7812 for a class that has, as a child, a die giving a definition for a
7813 nested class. So we have to process our children even if the
7814 current die is a declaration. Normally, of course, a declaration
7815 won't have any children at all. */
134d01f1 7816
90aeadfc
DC
7817 while (child_die != NULL && child_die->tag)
7818 {
7819 if (child_die->tag == DW_TAG_member
7820 || child_die->tag == DW_TAG_variable
34eaf542
TT
7821 || child_die->tag == DW_TAG_inheritance
7822 || child_die->tag == DW_TAG_template_value_param
7823 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7824 {
90aeadfc 7825 /* Do nothing. */
134d01f1 7826 }
90aeadfc
DC
7827 else
7828 process_die (child_die, cu);
134d01f1 7829
90aeadfc 7830 child_die = sibling_die (child_die);
134d01f1
DJ
7831 }
7832
fa4028e9
JB
7833 /* Do not consider external references. According to the DWARF standard,
7834 these DIEs are identified by the fact that they have no byte_size
7835 attribute, and a declaration attribute. */
7836 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7837 || !die_is_declaration (die, cu))
c767944b 7838 new_symbol (die, type, cu);
134d01f1
DJ
7839}
7840
7841/* Given a DW_AT_enumeration_type die, set its type. We do not
7842 complete the type's fields yet, or create any symbols. */
c906108c 7843
f792889a 7844static struct type *
134d01f1 7845read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7846{
e7c27a73 7847 struct objfile *objfile = cu->objfile;
c906108c 7848 struct type *type;
c906108c 7849 struct attribute *attr;
0114d602 7850 const char *name;
134d01f1 7851
348e048f
DE
7852 /* If the definition of this type lives in .debug_types, read that type.
7853 Don't follow DW_AT_specification though, that will take us back up
7854 the chain and we want to go down. */
7855 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7856 if (attr)
7857 {
7858 struct dwarf2_cu *type_cu = cu;
7859 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7860
348e048f 7861 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7862
7863 /* TYPE_CU may not be the same as CU.
7864 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7865 return set_die_type (die, type, cu);
7866 }
7867
c906108c
SS
7868 type = alloc_type (objfile);
7869
7870 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7871 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7872 if (name != NULL)
0114d602 7873 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7874
e142c38c 7875 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7876 if (attr)
7877 {
7878 TYPE_LENGTH (type) = DW_UNSND (attr);
7879 }
7880 else
7881 {
7882 TYPE_LENGTH (type) = 0;
7883 }
7884
137033e9
JB
7885 /* The enumeration DIE can be incomplete. In Ada, any type can be
7886 declared as private in the package spec, and then defined only
7887 inside the package body. Such types are known as Taft Amendment
7888 Types. When another package uses such a type, an incomplete DIE
7889 may be generated by the compiler. */
02eb380e 7890 if (die_is_declaration (die, cu))
876cecd0 7891 TYPE_STUB (type) = 1;
02eb380e 7892
f792889a 7893 return set_die_type (die, type, cu);
134d01f1
DJ
7894}
7895
7896/* Given a pointer to a die which begins an enumeration, process all
7897 the dies that define the members of the enumeration, and create the
7898 symbol for the enumeration type.
7899
7900 NOTE: We reverse the order of the element list. */
7901
7902static void
7903process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7904{
f792889a 7905 struct type *this_type;
134d01f1 7906
f792889a
DJ
7907 this_type = get_die_type (die, cu);
7908 if (this_type == NULL)
7909 this_type = read_enumeration_type (die, cu);
9dc481d3 7910
639d11d3 7911 if (die->child != NULL)
c906108c 7912 {
9dc481d3
DE
7913 struct die_info *child_die;
7914 struct symbol *sym;
7915 struct field *fields = NULL;
7916 int num_fields = 0;
7917 int unsigned_enum = 1;
7918 char *name;
cafec441
TT
7919 int flag_enum = 1;
7920 ULONGEST mask = 0;
9dc481d3 7921
639d11d3 7922 child_die = die->child;
c906108c
SS
7923 while (child_die && child_die->tag)
7924 {
7925 if (child_die->tag != DW_TAG_enumerator)
7926 {
e7c27a73 7927 process_die (child_die, cu);
c906108c
SS
7928 }
7929 else
7930 {
39cbfefa
DJ
7931 name = dwarf2_name (child_die, cu);
7932 if (name)
c906108c 7933 {
f792889a 7934 sym = new_symbol (child_die, this_type, cu);
c906108c 7935 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
7936 {
7937 unsigned_enum = 0;
7938 flag_enum = 0;
7939 }
7940 else if ((mask & SYMBOL_VALUE (sym)) != 0)
7941 flag_enum = 0;
7942 else
7943 mask |= SYMBOL_VALUE (sym);
c906108c
SS
7944
7945 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7946 {
7947 fields = (struct field *)
7948 xrealloc (fields,
7949 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7950 * sizeof (struct field));
c906108c
SS
7951 }
7952
3567439c 7953 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7954 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7955 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7956 FIELD_BITSIZE (fields[num_fields]) = 0;
7957
7958 num_fields++;
7959 }
7960 }
7961
7962 child_die = sibling_die (child_die);
7963 }
7964
7965 if (num_fields)
7966 {
f792889a
DJ
7967 TYPE_NFIELDS (this_type) = num_fields;
7968 TYPE_FIELDS (this_type) = (struct field *)
7969 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7970 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7971 sizeof (struct field) * num_fields);
b8c9b27d 7972 xfree (fields);
c906108c
SS
7973 }
7974 if (unsigned_enum)
876cecd0 7975 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
7976 if (flag_enum)
7977 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 7978 }
134d01f1 7979
6c83ed52
TT
7980 /* If we are reading an enum from a .debug_types unit, and the enum
7981 is a declaration, and the enum is not the signatured type in the
7982 unit, then we do not want to add a symbol for it. Adding a
7983 symbol would in some cases obscure the true definition of the
7984 enum, giving users an incomplete type when the definition is
7985 actually available. Note that we do not want to do this for all
7986 enums which are just declarations, because C++0x allows forward
7987 enum declarations. */
b0df02fd 7988 if (cu->per_cu->debug_types_section
6c83ed52
TT
7989 && die_is_declaration (die, cu))
7990 {
7991 struct signatured_type *type_sig;
7992
7993 type_sig
7994 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
b0df02fd 7995 cu->per_cu->debug_types_section,
6c83ed52
TT
7996 cu->per_cu->offset);
7997 if (type_sig->type_offset != die->offset)
7998 return;
7999 }
8000
f792889a 8001 new_symbol (die, this_type, cu);
c906108c
SS
8002}
8003
8004/* Extract all information from a DW_TAG_array_type DIE and put it in
8005 the DIE's type field. For now, this only handles one dimensional
8006 arrays. */
8007
f792889a 8008static struct type *
e7c27a73 8009read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8010{
e7c27a73 8011 struct objfile *objfile = cu->objfile;
c906108c 8012 struct die_info *child_die;
7e314c57 8013 struct type *type;
c906108c
SS
8014 struct type *element_type, *range_type, *index_type;
8015 struct type **range_types = NULL;
8016 struct attribute *attr;
8017 int ndim = 0;
8018 struct cleanup *back_to;
39cbfefa 8019 char *name;
c906108c 8020
e7c27a73 8021 element_type = die_type (die, cu);
c906108c 8022
7e314c57
JK
8023 /* The die_type call above may have already set the type for this DIE. */
8024 type = get_die_type (die, cu);
8025 if (type)
8026 return type;
8027
c906108c
SS
8028 /* Irix 6.2 native cc creates array types without children for
8029 arrays with unspecified length. */
639d11d3 8030 if (die->child == NULL)
c906108c 8031 {
46bf5051 8032 index_type = objfile_type (objfile)->builtin_int;
c906108c 8033 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8034 type = create_array_type (NULL, element_type, range_type);
8035 return set_die_type (die, type, cu);
c906108c
SS
8036 }
8037
8038 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8039 child_die = die->child;
c906108c
SS
8040 while (child_die && child_die->tag)
8041 {
8042 if (child_die->tag == DW_TAG_subrange_type)
8043 {
f792889a 8044 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8045
f792889a 8046 if (child_type != NULL)
a02abb62 8047 {
0963b4bd
MS
8048 /* The range type was succesfully read. Save it for the
8049 array type creation. */
a02abb62
JB
8050 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8051 {
8052 range_types = (struct type **)
8053 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8054 * sizeof (struct type *));
8055 if (ndim == 0)
8056 make_cleanup (free_current_contents, &range_types);
8057 }
f792889a 8058 range_types[ndim++] = child_type;
a02abb62 8059 }
c906108c
SS
8060 }
8061 child_die = sibling_die (child_die);
8062 }
8063
8064 /* Dwarf2 dimensions are output from left to right, create the
8065 necessary array types in backwards order. */
7ca2d3a3 8066
c906108c 8067 type = element_type;
7ca2d3a3
DL
8068
8069 if (read_array_order (die, cu) == DW_ORD_col_major)
8070 {
8071 int i = 0;
9a619af0 8072
7ca2d3a3
DL
8073 while (i < ndim)
8074 type = create_array_type (NULL, type, range_types[i++]);
8075 }
8076 else
8077 {
8078 while (ndim-- > 0)
8079 type = create_array_type (NULL, type, range_types[ndim]);
8080 }
c906108c 8081
f5f8a009
EZ
8082 /* Understand Dwarf2 support for vector types (like they occur on
8083 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8084 array type. This is not part of the Dwarf2/3 standard yet, but a
8085 custom vendor extension. The main difference between a regular
8086 array and the vector variant is that vectors are passed by value
8087 to functions. */
e142c38c 8088 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8089 if (attr)
ea37ba09 8090 make_vector_type (type);
f5f8a009 8091
dbc98a8b
KW
8092 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8093 implementation may choose to implement triple vectors using this
8094 attribute. */
8095 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8096 if (attr)
8097 {
8098 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8099 TYPE_LENGTH (type) = DW_UNSND (attr);
8100 else
3e43a32a
MS
8101 complaint (&symfile_complaints,
8102 _("DW_AT_byte_size for array type smaller "
8103 "than the total size of elements"));
dbc98a8b
KW
8104 }
8105
39cbfefa
DJ
8106 name = dwarf2_name (die, cu);
8107 if (name)
8108 TYPE_NAME (type) = name;
6e70227d 8109
0963b4bd 8110 /* Install the type in the die. */
7e314c57
JK
8111 set_die_type (die, type, cu);
8112
8113 /* set_die_type should be already done. */
b4ba55a1
JB
8114 set_descriptive_type (type, die, cu);
8115
c906108c
SS
8116 do_cleanups (back_to);
8117
7e314c57 8118 return type;
c906108c
SS
8119}
8120
7ca2d3a3 8121static enum dwarf_array_dim_ordering
6e70227d 8122read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8123{
8124 struct attribute *attr;
8125
8126 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8127
8128 if (attr) return DW_SND (attr);
8129
0963b4bd
MS
8130 /* GNU F77 is a special case, as at 08/2004 array type info is the
8131 opposite order to the dwarf2 specification, but data is still
8132 laid out as per normal fortran.
7ca2d3a3 8133
0963b4bd
MS
8134 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8135 version checking. */
7ca2d3a3 8136
905e0470
PM
8137 if (cu->language == language_fortran
8138 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8139 {
8140 return DW_ORD_row_major;
8141 }
8142
6e70227d 8143 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8144 {
8145 case array_column_major:
8146 return DW_ORD_col_major;
8147 case array_row_major:
8148 default:
8149 return DW_ORD_row_major;
8150 };
8151}
8152
72019c9c 8153/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8154 the DIE's type field. */
72019c9c 8155
f792889a 8156static struct type *
72019c9c
GM
8157read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8158{
7e314c57
JK
8159 struct type *domain_type, *set_type;
8160 struct attribute *attr;
f792889a 8161
7e314c57
JK
8162 domain_type = die_type (die, cu);
8163
8164 /* The die_type call above may have already set the type for this DIE. */
8165 set_type = get_die_type (die, cu);
8166 if (set_type)
8167 return set_type;
8168
8169 set_type = create_set_type (NULL, domain_type);
8170
8171 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8172 if (attr)
8173 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8174
f792889a 8175 return set_die_type (die, set_type, cu);
72019c9c 8176}
7ca2d3a3 8177
c906108c
SS
8178/* First cut: install each common block member as a global variable. */
8179
8180static void
e7c27a73 8181read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8182{
8183 struct die_info *child_die;
8184 struct attribute *attr;
8185 struct symbol *sym;
8186 CORE_ADDR base = (CORE_ADDR) 0;
8187
e142c38c 8188 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8189 if (attr)
8190 {
0963b4bd 8191 /* Support the .debug_loc offsets. */
8e19ed76
PS
8192 if (attr_form_is_block (attr))
8193 {
e7c27a73 8194 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8195 }
3690dd37 8196 else if (attr_form_is_section_offset (attr))
8e19ed76 8197 {
4d3c2250 8198 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8199 }
8200 else
8201 {
4d3c2250
KB
8202 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8203 "common block member");
8e19ed76 8204 }
c906108c 8205 }
639d11d3 8206 if (die->child != NULL)
c906108c 8207 {
639d11d3 8208 child_die = die->child;
c906108c
SS
8209 while (child_die && child_die->tag)
8210 {
74ac6d43
TT
8211 LONGEST offset;
8212
e7c27a73 8213 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8214 if (sym != NULL
8215 && handle_data_member_location (child_die, cu, &offset))
c906108c 8216 {
74ac6d43 8217 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8218 add_symbol_to_list (sym, &global_symbols);
8219 }
8220 child_die = sibling_die (child_die);
8221 }
8222 }
8223}
8224
0114d602 8225/* Create a type for a C++ namespace. */
d9fa45fe 8226
0114d602
DJ
8227static struct type *
8228read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8229{
e7c27a73 8230 struct objfile *objfile = cu->objfile;
0114d602 8231 const char *previous_prefix, *name;
9219021c 8232 int is_anonymous;
0114d602
DJ
8233 struct type *type;
8234
8235 /* For extensions, reuse the type of the original namespace. */
8236 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8237 {
8238 struct die_info *ext_die;
8239 struct dwarf2_cu *ext_cu = cu;
9a619af0 8240
0114d602
DJ
8241 ext_die = dwarf2_extension (die, &ext_cu);
8242 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8243
8244 /* EXT_CU may not be the same as CU.
8245 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8246 return set_die_type (die, type, cu);
8247 }
9219021c 8248
e142c38c 8249 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8250
8251 /* Now build the name of the current namespace. */
8252
0114d602
DJ
8253 previous_prefix = determine_prefix (die, cu);
8254 if (previous_prefix[0] != '\0')
8255 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8256 previous_prefix, name, 0, cu);
0114d602
DJ
8257
8258 /* Create the type. */
8259 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8260 objfile);
8261 TYPE_NAME (type) = (char *) name;
8262 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8263
60531b24 8264 return set_die_type (die, type, cu);
0114d602
DJ
8265}
8266
8267/* Read a C++ namespace. */
8268
8269static void
8270read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8271{
8272 struct objfile *objfile = cu->objfile;
0114d602 8273 int is_anonymous;
9219021c 8274
5c4e30ca
DC
8275 /* Add a symbol associated to this if we haven't seen the namespace
8276 before. Also, add a using directive if it's an anonymous
8277 namespace. */
9219021c 8278
f2f0e013 8279 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8280 {
8281 struct type *type;
8282
0114d602 8283 type = read_type_die (die, cu);
e7c27a73 8284 new_symbol (die, type, cu);
5c4e30ca 8285
e8e80198 8286 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8287 if (is_anonymous)
0114d602
DJ
8288 {
8289 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8290
c0cc3a76 8291 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8292 NULL, NULL, &objfile->objfile_obstack);
0114d602 8293 }
5c4e30ca 8294 }
9219021c 8295
639d11d3 8296 if (die->child != NULL)
d9fa45fe 8297 {
639d11d3 8298 struct die_info *child_die = die->child;
6e70227d 8299
d9fa45fe
DC
8300 while (child_die && child_die->tag)
8301 {
e7c27a73 8302 process_die (child_die, cu);
d9fa45fe
DC
8303 child_die = sibling_die (child_die);
8304 }
8305 }
38d518c9
EZ
8306}
8307
f55ee35c
JK
8308/* Read a Fortran module as type. This DIE can be only a declaration used for
8309 imported module. Still we need that type as local Fortran "use ... only"
8310 declaration imports depend on the created type in determine_prefix. */
8311
8312static struct type *
8313read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8314{
8315 struct objfile *objfile = cu->objfile;
8316 char *module_name;
8317 struct type *type;
8318
8319 module_name = dwarf2_name (die, cu);
8320 if (!module_name)
3e43a32a
MS
8321 complaint (&symfile_complaints,
8322 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8323 die->offset);
8324 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8325
8326 /* determine_prefix uses TYPE_TAG_NAME. */
8327 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8328
8329 return set_die_type (die, type, cu);
8330}
8331
5d7cb8df
JK
8332/* Read a Fortran module. */
8333
8334static void
8335read_module (struct die_info *die, struct dwarf2_cu *cu)
8336{
8337 struct die_info *child_die = die->child;
8338
5d7cb8df
JK
8339 while (child_die && child_die->tag)
8340 {
8341 process_die (child_die, cu);
8342 child_die = sibling_die (child_die);
8343 }
8344}
8345
38d518c9
EZ
8346/* Return the name of the namespace represented by DIE. Set
8347 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8348 namespace. */
8349
8350static const char *
e142c38c 8351namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8352{
8353 struct die_info *current_die;
8354 const char *name = NULL;
8355
8356 /* Loop through the extensions until we find a name. */
8357
8358 for (current_die = die;
8359 current_die != NULL;
f2f0e013 8360 current_die = dwarf2_extension (die, &cu))
38d518c9 8361 {
e142c38c 8362 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8363 if (name != NULL)
8364 break;
8365 }
8366
8367 /* Is it an anonymous namespace? */
8368
8369 *is_anonymous = (name == NULL);
8370 if (*is_anonymous)
2b1dbab0 8371 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8372
8373 return name;
d9fa45fe
DC
8374}
8375
c906108c
SS
8376/* Extract all information from a DW_TAG_pointer_type DIE and add to
8377 the user defined type vector. */
8378
f792889a 8379static struct type *
e7c27a73 8380read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8381{
5e2b427d 8382 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8383 struct comp_unit_head *cu_header = &cu->header;
c906108c 8384 struct type *type;
8b2dbe47
KB
8385 struct attribute *attr_byte_size;
8386 struct attribute *attr_address_class;
8387 int byte_size, addr_class;
7e314c57
JK
8388 struct type *target_type;
8389
8390 target_type = die_type (die, cu);
c906108c 8391
7e314c57
JK
8392 /* The die_type call above may have already set the type for this DIE. */
8393 type = get_die_type (die, cu);
8394 if (type)
8395 return type;
8396
8397 type = lookup_pointer_type (target_type);
8b2dbe47 8398
e142c38c 8399 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8400 if (attr_byte_size)
8401 byte_size = DW_UNSND (attr_byte_size);
c906108c 8402 else
8b2dbe47
KB
8403 byte_size = cu_header->addr_size;
8404
e142c38c 8405 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8406 if (attr_address_class)
8407 addr_class = DW_UNSND (attr_address_class);
8408 else
8409 addr_class = DW_ADDR_none;
8410
8411 /* If the pointer size or address class is different than the
8412 default, create a type variant marked as such and set the
8413 length accordingly. */
8414 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8415 {
5e2b427d 8416 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8417 {
8418 int type_flags;
8419
849957d9 8420 type_flags = gdbarch_address_class_type_flags
5e2b427d 8421 (gdbarch, byte_size, addr_class);
876cecd0
TT
8422 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8423 == 0);
8b2dbe47
KB
8424 type = make_type_with_address_space (type, type_flags);
8425 }
8426 else if (TYPE_LENGTH (type) != byte_size)
8427 {
3e43a32a
MS
8428 complaint (&symfile_complaints,
8429 _("invalid pointer size %d"), byte_size);
8b2dbe47 8430 }
6e70227d 8431 else
9a619af0
MS
8432 {
8433 /* Should we also complain about unhandled address classes? */
8434 }
c906108c 8435 }
8b2dbe47
KB
8436
8437 TYPE_LENGTH (type) = byte_size;
f792889a 8438 return set_die_type (die, type, cu);
c906108c
SS
8439}
8440
8441/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8442 the user defined type vector. */
8443
f792889a 8444static struct type *
e7c27a73 8445read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8446{
8447 struct type *type;
8448 struct type *to_type;
8449 struct type *domain;
8450
e7c27a73
DJ
8451 to_type = die_type (die, cu);
8452 domain = die_containing_type (die, cu);
0d5de010 8453
7e314c57
JK
8454 /* The calls above may have already set the type for this DIE. */
8455 type = get_die_type (die, cu);
8456 if (type)
8457 return type;
8458
0d5de010
DJ
8459 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8460 type = lookup_methodptr_type (to_type);
8461 else
8462 type = lookup_memberptr_type (to_type, domain);
c906108c 8463
f792889a 8464 return set_die_type (die, type, cu);
c906108c
SS
8465}
8466
8467/* Extract all information from a DW_TAG_reference_type DIE and add to
8468 the user defined type vector. */
8469
f792889a 8470static struct type *
e7c27a73 8471read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8472{
e7c27a73 8473 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8474 struct type *type, *target_type;
c906108c
SS
8475 struct attribute *attr;
8476
7e314c57
JK
8477 target_type = die_type (die, cu);
8478
8479 /* The die_type call above may have already set the type for this DIE. */
8480 type = get_die_type (die, cu);
8481 if (type)
8482 return type;
8483
8484 type = lookup_reference_type (target_type);
e142c38c 8485 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8486 if (attr)
8487 {
8488 TYPE_LENGTH (type) = DW_UNSND (attr);
8489 }
8490 else
8491 {
107d2387 8492 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8493 }
f792889a 8494 return set_die_type (die, type, cu);
c906108c
SS
8495}
8496
f792889a 8497static struct type *
e7c27a73 8498read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8499{
f792889a 8500 struct type *base_type, *cv_type;
c906108c 8501
e7c27a73 8502 base_type = die_type (die, cu);
7e314c57
JK
8503
8504 /* The die_type call above may have already set the type for this DIE. */
8505 cv_type = get_die_type (die, cu);
8506 if (cv_type)
8507 return cv_type;
8508
2f608a3a
KW
8509 /* In case the const qualifier is applied to an array type, the element type
8510 is so qualified, not the array type (section 6.7.3 of C99). */
8511 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8512 {
8513 struct type *el_type, *inner_array;
8514
8515 base_type = copy_type (base_type);
8516 inner_array = base_type;
8517
8518 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8519 {
8520 TYPE_TARGET_TYPE (inner_array) =
8521 copy_type (TYPE_TARGET_TYPE (inner_array));
8522 inner_array = TYPE_TARGET_TYPE (inner_array);
8523 }
8524
8525 el_type = TYPE_TARGET_TYPE (inner_array);
8526 TYPE_TARGET_TYPE (inner_array) =
8527 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8528
8529 return set_die_type (die, base_type, cu);
8530 }
8531
f792889a
DJ
8532 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8533 return set_die_type (die, cv_type, cu);
c906108c
SS
8534}
8535
f792889a 8536static struct type *
e7c27a73 8537read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8538{
f792889a 8539 struct type *base_type, *cv_type;
c906108c 8540
e7c27a73 8541 base_type = die_type (die, cu);
7e314c57
JK
8542
8543 /* The die_type call above may have already set the type for this DIE. */
8544 cv_type = get_die_type (die, cu);
8545 if (cv_type)
8546 return cv_type;
8547
f792889a
DJ
8548 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8549 return set_die_type (die, cv_type, cu);
c906108c
SS
8550}
8551
8552/* Extract all information from a DW_TAG_string_type DIE and add to
8553 the user defined type vector. It isn't really a user defined type,
8554 but it behaves like one, with other DIE's using an AT_user_def_type
8555 attribute to reference it. */
8556
f792889a 8557static struct type *
e7c27a73 8558read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8559{
e7c27a73 8560 struct objfile *objfile = cu->objfile;
3b7538c0 8561 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8562 struct type *type, *range_type, *index_type, *char_type;
8563 struct attribute *attr;
8564 unsigned int length;
8565
e142c38c 8566 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8567 if (attr)
8568 {
8569 length = DW_UNSND (attr);
8570 }
8571 else
8572 {
0963b4bd 8573 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8574 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8575 if (attr)
8576 {
8577 length = DW_UNSND (attr);
8578 }
8579 else
8580 {
8581 length = 1;
8582 }
c906108c 8583 }
6ccb9162 8584
46bf5051 8585 index_type = objfile_type (objfile)->builtin_int;
c906108c 8586 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8587 char_type = language_string_char_type (cu->language_defn, gdbarch);
8588 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8589
f792889a 8590 return set_die_type (die, type, cu);
c906108c
SS
8591}
8592
8593/* Handle DIES due to C code like:
8594
8595 struct foo
c5aa993b
JM
8596 {
8597 int (*funcp)(int a, long l);
8598 int b;
8599 };
c906108c 8600
0963b4bd 8601 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8602
f792889a 8603static struct type *
e7c27a73 8604read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8605{
bb5ed363 8606 struct objfile *objfile = cu->objfile;
0963b4bd
MS
8607 struct type *type; /* Type that this function returns. */
8608 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8609 struct attribute *attr;
8610
e7c27a73 8611 type = die_type (die, cu);
7e314c57
JK
8612
8613 /* The die_type call above may have already set the type for this DIE. */
8614 ftype = get_die_type (die, cu);
8615 if (ftype)
8616 return ftype;
8617
0c8b41f1 8618 ftype = lookup_function_type (type);
c906108c 8619
5b8101ae 8620 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8621 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8622 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8623 || cu->language == language_cplus
5b8101ae
PM
8624 || cu->language == language_java
8625 || cu->language == language_pascal)
876cecd0 8626 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8627 else if (producer_is_realview (cu->producer))
8628 /* RealView does not emit DW_AT_prototyped. We can not
8629 distinguish prototyped and unprototyped functions; default to
8630 prototyped, since that is more common in modern code (and
8631 RealView warns about unprototyped functions). */
8632 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8633
c055b101
CV
8634 /* Store the calling convention in the type if it's available in
8635 the subroutine die. Otherwise set the calling convention to
8636 the default value DW_CC_normal. */
8637 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8638 if (attr)
8639 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8640 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8641 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8642 else
8643 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8644
8645 /* We need to add the subroutine type to the die immediately so
8646 we don't infinitely recurse when dealing with parameters
0963b4bd 8647 declared as the same subroutine type. */
76c10ea2 8648 set_die_type (die, ftype, cu);
6e70227d 8649
639d11d3 8650 if (die->child != NULL)
c906108c 8651 {
bb5ed363 8652 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 8653 struct die_info *child_die;
8072405b 8654 int nparams, iparams;
c906108c
SS
8655
8656 /* Count the number of parameters.
8657 FIXME: GDB currently ignores vararg functions, but knows about
8658 vararg member functions. */
8072405b 8659 nparams = 0;
639d11d3 8660 child_die = die->child;
c906108c
SS
8661 while (child_die && child_die->tag)
8662 {
8663 if (child_die->tag == DW_TAG_formal_parameter)
8664 nparams++;
8665 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8666 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8667 child_die = sibling_die (child_die);
8668 }
8669
8670 /* Allocate storage for parameters and fill them in. */
8671 TYPE_NFIELDS (ftype) = nparams;
8672 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8673 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8674
8072405b
JK
8675 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8676 even if we error out during the parameters reading below. */
8677 for (iparams = 0; iparams < nparams; iparams++)
8678 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8679
8680 iparams = 0;
639d11d3 8681 child_die = die->child;
c906108c
SS
8682 while (child_die && child_die->tag)
8683 {
8684 if (child_die->tag == DW_TAG_formal_parameter)
8685 {
3ce3b1ba
PA
8686 struct type *arg_type;
8687
8688 /* DWARF version 2 has no clean way to discern C++
8689 static and non-static member functions. G++ helps
8690 GDB by marking the first parameter for non-static
8691 member functions (which is the this pointer) as
8692 artificial. We pass this information to
8693 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8694
8695 DWARF version 3 added DW_AT_object_pointer, which GCC
8696 4.5 does not yet generate. */
e142c38c 8697 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8698 if (attr)
8699 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8700 else
418835cc
KS
8701 {
8702 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8703
8704 /* GCC/43521: In java, the formal parameter
8705 "this" is sometimes not marked with DW_AT_artificial. */
8706 if (cu->language == language_java)
8707 {
8708 const char *name = dwarf2_name (child_die, cu);
9a619af0 8709
418835cc
KS
8710 if (name && !strcmp (name, "this"))
8711 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8712 }
8713 }
3ce3b1ba
PA
8714 arg_type = die_type (child_die, cu);
8715
8716 /* RealView does not mark THIS as const, which the testsuite
8717 expects. GCC marks THIS as const in method definitions,
8718 but not in the class specifications (GCC PR 43053). */
8719 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8720 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8721 {
8722 int is_this = 0;
8723 struct dwarf2_cu *arg_cu = cu;
8724 const char *name = dwarf2_name (child_die, cu);
8725
8726 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8727 if (attr)
8728 {
8729 /* If the compiler emits this, use it. */
8730 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8731 is_this = 1;
8732 }
8733 else if (name && strcmp (name, "this") == 0)
8734 /* Function definitions will have the argument names. */
8735 is_this = 1;
8736 else if (name == NULL && iparams == 0)
8737 /* Declarations may not have the names, so like
8738 elsewhere in GDB, assume an artificial first
8739 argument is "this". */
8740 is_this = 1;
8741
8742 if (is_this)
8743 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8744 arg_type, 0);
8745 }
8746
8747 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8748 iparams++;
8749 }
8750 child_die = sibling_die (child_die);
8751 }
8752 }
8753
76c10ea2 8754 return ftype;
c906108c
SS
8755}
8756
f792889a 8757static struct type *
e7c27a73 8758read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8759{
e7c27a73 8760 struct objfile *objfile = cu->objfile;
0114d602 8761 const char *name = NULL;
3c8e0968 8762 struct type *this_type, *target_type;
c906108c 8763
94af9270 8764 name = dwarf2_full_name (NULL, die, cu);
f792889a 8765 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8766 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8767 TYPE_NAME (this_type) = (char *) name;
f792889a 8768 set_die_type (die, this_type, cu);
3c8e0968
DE
8769 target_type = die_type (die, cu);
8770 if (target_type != this_type)
8771 TYPE_TARGET_TYPE (this_type) = target_type;
8772 else
8773 {
8774 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8775 spec and cause infinite loops in GDB. */
8776 complaint (&symfile_complaints,
8777 _("Self-referential DW_TAG_typedef "
8778 "- DIE at 0x%x [in module %s]"),
bb5ed363 8779 die->offset, objfile->name);
3c8e0968
DE
8780 TYPE_TARGET_TYPE (this_type) = NULL;
8781 }
f792889a 8782 return this_type;
c906108c
SS
8783}
8784
8785/* Find a representation of a given base type and install
8786 it in the TYPE field of the die. */
8787
f792889a 8788static struct type *
e7c27a73 8789read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8790{
e7c27a73 8791 struct objfile *objfile = cu->objfile;
c906108c
SS
8792 struct type *type;
8793 struct attribute *attr;
8794 int encoding = 0, size = 0;
39cbfefa 8795 char *name;
6ccb9162
UW
8796 enum type_code code = TYPE_CODE_INT;
8797 int type_flags = 0;
8798 struct type *target_type = NULL;
c906108c 8799
e142c38c 8800 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8801 if (attr)
8802 {
8803 encoding = DW_UNSND (attr);
8804 }
e142c38c 8805 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8806 if (attr)
8807 {
8808 size = DW_UNSND (attr);
8809 }
39cbfefa 8810 name = dwarf2_name (die, cu);
6ccb9162 8811 if (!name)
c906108c 8812 {
6ccb9162
UW
8813 complaint (&symfile_complaints,
8814 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8815 }
6ccb9162
UW
8816
8817 switch (encoding)
c906108c 8818 {
6ccb9162
UW
8819 case DW_ATE_address:
8820 /* Turn DW_ATE_address into a void * pointer. */
8821 code = TYPE_CODE_PTR;
8822 type_flags |= TYPE_FLAG_UNSIGNED;
8823 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8824 break;
8825 case DW_ATE_boolean:
8826 code = TYPE_CODE_BOOL;
8827 type_flags |= TYPE_FLAG_UNSIGNED;
8828 break;
8829 case DW_ATE_complex_float:
8830 code = TYPE_CODE_COMPLEX;
8831 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8832 break;
8833 case DW_ATE_decimal_float:
8834 code = TYPE_CODE_DECFLOAT;
8835 break;
8836 case DW_ATE_float:
8837 code = TYPE_CODE_FLT;
8838 break;
8839 case DW_ATE_signed:
8840 break;
8841 case DW_ATE_unsigned:
8842 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8843 if (cu->language == language_fortran
8844 && name
8845 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8846 code = TYPE_CODE_CHAR;
6ccb9162
UW
8847 break;
8848 case DW_ATE_signed_char:
6e70227d 8849 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8850 || cu->language == language_pascal
8851 || cu->language == language_fortran)
6ccb9162
UW
8852 code = TYPE_CODE_CHAR;
8853 break;
8854 case DW_ATE_unsigned_char:
868a0084 8855 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8856 || cu->language == language_pascal
8857 || cu->language == language_fortran)
6ccb9162
UW
8858 code = TYPE_CODE_CHAR;
8859 type_flags |= TYPE_FLAG_UNSIGNED;
8860 break;
75079b2b
TT
8861 case DW_ATE_UTF:
8862 /* We just treat this as an integer and then recognize the
8863 type by name elsewhere. */
8864 break;
8865
6ccb9162
UW
8866 default:
8867 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8868 dwarf_type_encoding_name (encoding));
8869 break;
c906108c 8870 }
6ccb9162 8871
0114d602
DJ
8872 type = init_type (code, size, type_flags, NULL, objfile);
8873 TYPE_NAME (type) = name;
6ccb9162
UW
8874 TYPE_TARGET_TYPE (type) = target_type;
8875
0114d602 8876 if (name && strcmp (name, "char") == 0)
876cecd0 8877 TYPE_NOSIGN (type) = 1;
0114d602 8878
f792889a 8879 return set_die_type (die, type, cu);
c906108c
SS
8880}
8881
a02abb62
JB
8882/* Read the given DW_AT_subrange DIE. */
8883
f792889a 8884static struct type *
a02abb62
JB
8885read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8886{
8887 struct type *base_type;
8888 struct type *range_type;
8889 struct attribute *attr;
43bbcdc2
PH
8890 LONGEST low = 0;
8891 LONGEST high = -1;
39cbfefa 8892 char *name;
43bbcdc2 8893 LONGEST negative_mask;
e77813c8 8894
a02abb62 8895 base_type = die_type (die, cu);
953ac07e
JK
8896 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8897 check_typedef (base_type);
a02abb62 8898
7e314c57
JK
8899 /* The die_type call above may have already set the type for this DIE. */
8900 range_type = get_die_type (die, cu);
8901 if (range_type)
8902 return range_type;
8903
e142c38c 8904 if (cu->language == language_fortran)
6e70227d 8905 {
a02abb62
JB
8906 /* FORTRAN implies a lower bound of 1, if not given. */
8907 low = 1;
8908 }
8909
dd5e6932
DJ
8910 /* FIXME: For variable sized arrays either of these could be
8911 a variable rather than a constant value. We'll allow it,
8912 but we don't know how to handle it. */
e142c38c 8913 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8914 if (attr)
8915 low = dwarf2_get_attr_constant_value (attr, 0);
8916
e142c38c 8917 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8918 if (attr)
6e70227d 8919 {
d48323d8 8920 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
8921 {
8922 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8923 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8924 FIXME: GDB does not yet know how to handle dynamic
8925 arrays properly, treat them as arrays with unspecified
8926 length for now.
8927
8928 FIXME: jimb/2003-09-22: GDB does not really know
8929 how to handle arrays of unspecified length
8930 either; we just represent them as zero-length
8931 arrays. Choose an appropriate upper bound given
8932 the lower bound we've computed above. */
8933 high = low - 1;
8934 }
8935 else
8936 high = dwarf2_get_attr_constant_value (attr, 1);
8937 }
e77813c8
PM
8938 else
8939 {
8940 attr = dwarf2_attr (die, DW_AT_count, cu);
8941 if (attr)
8942 {
8943 int count = dwarf2_get_attr_constant_value (attr, 1);
8944 high = low + count - 1;
8945 }
c2ff108b
JK
8946 else
8947 {
8948 /* Unspecified array length. */
8949 high = low - 1;
8950 }
e77813c8
PM
8951 }
8952
8953 /* Dwarf-2 specifications explicitly allows to create subrange types
8954 without specifying a base type.
8955 In that case, the base type must be set to the type of
8956 the lower bound, upper bound or count, in that order, if any of these
8957 three attributes references an object that has a type.
8958 If no base type is found, the Dwarf-2 specifications say that
8959 a signed integer type of size equal to the size of an address should
8960 be used.
8961 For the following C code: `extern char gdb_int [];'
8962 GCC produces an empty range DIE.
8963 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8964 high bound or count are not yet handled by this code. */
e77813c8
PM
8965 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8966 {
8967 struct objfile *objfile = cu->objfile;
8968 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8969 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8970 struct type *int_type = objfile_type (objfile)->builtin_int;
8971
8972 /* Test "int", "long int", and "long long int" objfile types,
8973 and select the first one having a size above or equal to the
8974 architecture address size. */
8975 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8976 base_type = int_type;
8977 else
8978 {
8979 int_type = objfile_type (objfile)->builtin_long;
8980 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8981 base_type = int_type;
8982 else
8983 {
8984 int_type = objfile_type (objfile)->builtin_long_long;
8985 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8986 base_type = int_type;
8987 }
8988 }
8989 }
a02abb62 8990
6e70227d 8991 negative_mask =
43bbcdc2
PH
8992 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8993 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8994 low |= negative_mask;
8995 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8996 high |= negative_mask;
8997
a02abb62
JB
8998 range_type = create_range_type (NULL, base_type, low, high);
8999
bbb0eef6
JK
9000 /* Mark arrays with dynamic length at least as an array of unspecified
9001 length. GDB could check the boundary but before it gets implemented at
9002 least allow accessing the array elements. */
d48323d8 9003 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9004 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9005
c2ff108b
JK
9006 /* Ada expects an empty array on no boundary attributes. */
9007 if (attr == NULL && cu->language != language_ada)
9008 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9009
39cbfefa
DJ
9010 name = dwarf2_name (die, cu);
9011 if (name)
9012 TYPE_NAME (range_type) = name;
6e70227d 9013
e142c38c 9014 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9015 if (attr)
9016 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9017
7e314c57
JK
9018 set_die_type (die, range_type, cu);
9019
9020 /* set_die_type should be already done. */
b4ba55a1
JB
9021 set_descriptive_type (range_type, die, cu);
9022
7e314c57 9023 return range_type;
a02abb62 9024}
6e70227d 9025
f792889a 9026static struct type *
81a17f79
JB
9027read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9028{
9029 struct type *type;
81a17f79 9030
81a17f79
JB
9031 /* For now, we only support the C meaning of an unspecified type: void. */
9032
0114d602
DJ
9033 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9034 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9035
f792889a 9036 return set_die_type (die, type, cu);
81a17f79 9037}
a02abb62 9038
51545339
DJ
9039/* Trivial hash function for die_info: the hash value of a DIE
9040 is its offset in .debug_info for this objfile. */
9041
9042static hashval_t
9043die_hash (const void *item)
9044{
9045 const struct die_info *die = item;
9a619af0 9046
51545339
DJ
9047 return die->offset;
9048}
9049
9050/* Trivial comparison function for die_info structures: two DIEs
9051 are equal if they have the same offset. */
9052
9053static int
9054die_eq (const void *item_lhs, const void *item_rhs)
9055{
9056 const struct die_info *die_lhs = item_lhs;
9057 const struct die_info *die_rhs = item_rhs;
9a619af0 9058
51545339
DJ
9059 return die_lhs->offset == die_rhs->offset;
9060}
9061
c906108c
SS
9062/* Read a whole compilation unit into a linked list of dies. */
9063
f9aca02d 9064static struct die_info *
93311388 9065read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9066{
93311388 9067 struct die_reader_specs reader_specs;
98bfdba5 9068 int read_abbrevs = 0;
1d9ec526 9069 struct cleanup *back_to = NULL;
98bfdba5
PA
9070 struct die_info *die;
9071
9072 if (cu->dwarf2_abbrevs == NULL)
9073 {
e5fe5e75 9074 dwarf2_read_abbrevs (cu);
98bfdba5
PA
9075 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9076 read_abbrevs = 1;
9077 }
93311388 9078
348e048f 9079 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9080 cu->die_hash
9081 = htab_create_alloc_ex (cu->header.length / 12,
9082 die_hash,
9083 die_eq,
9084 NULL,
9085 &cu->comp_unit_obstack,
9086 hashtab_obstack_allocate,
9087 dummy_obstack_deallocate);
9088
93311388
DE
9089 init_cu_die_reader (&reader_specs, cu);
9090
98bfdba5
PA
9091 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9092
9093 if (read_abbrevs)
9094 do_cleanups (back_to);
9095
9096 return die;
639d11d3
DC
9097}
9098
d97bc12b
DE
9099/* Main entry point for reading a DIE and all children.
9100 Read the DIE and dump it if requested. */
9101
9102static struct die_info *
93311388
DE
9103read_die_and_children (const struct die_reader_specs *reader,
9104 gdb_byte *info_ptr,
d97bc12b
DE
9105 gdb_byte **new_info_ptr,
9106 struct die_info *parent)
9107{
93311388 9108 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9109 new_info_ptr, parent);
9110
9111 if (dwarf2_die_debug)
9112 {
348e048f
DE
9113 fprintf_unfiltered (gdb_stdlog,
9114 "\nRead die from %s of %s:\n",
b0df02fd 9115 (reader->cu->per_cu->debug_types_section
8b70b953
TT
9116 ? ".debug_types"
9117 : ".debug_info"),
348e048f 9118 reader->abfd->filename);
d97bc12b
DE
9119 dump_die (result, dwarf2_die_debug);
9120 }
9121
9122 return result;
9123}
9124
639d11d3
DC
9125/* Read a single die and all its descendents. Set the die's sibling
9126 field to NULL; set other fields in the die correctly, and set all
9127 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9128 location of the info_ptr after reading all of those dies. PARENT
9129 is the parent of the die in question. */
9130
9131static struct die_info *
93311388
DE
9132read_die_and_children_1 (const struct die_reader_specs *reader,
9133 gdb_byte *info_ptr,
d97bc12b
DE
9134 gdb_byte **new_info_ptr,
9135 struct die_info *parent)
639d11d3
DC
9136{
9137 struct die_info *die;
fe1b8b76 9138 gdb_byte *cur_ptr;
639d11d3
DC
9139 int has_children;
9140
93311388 9141 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9142 if (die == NULL)
9143 {
9144 *new_info_ptr = cur_ptr;
9145 return NULL;
9146 }
93311388 9147 store_in_ref_table (die, reader->cu);
639d11d3
DC
9148
9149 if (has_children)
348e048f 9150 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9151 else
9152 {
9153 die->child = NULL;
9154 *new_info_ptr = cur_ptr;
9155 }
9156
9157 die->sibling = NULL;
9158 die->parent = parent;
9159 return die;
9160}
9161
9162/* Read a die, all of its descendents, and all of its siblings; set
9163 all of the fields of all of the dies correctly. Arguments are as
9164 in read_die_and_children. */
9165
9166static struct die_info *
93311388
DE
9167read_die_and_siblings (const struct die_reader_specs *reader,
9168 gdb_byte *info_ptr,
fe1b8b76 9169 gdb_byte **new_info_ptr,
639d11d3
DC
9170 struct die_info *parent)
9171{
9172 struct die_info *first_die, *last_sibling;
fe1b8b76 9173 gdb_byte *cur_ptr;
639d11d3 9174
c906108c 9175 cur_ptr = info_ptr;
639d11d3
DC
9176 first_die = last_sibling = NULL;
9177
9178 while (1)
c906108c 9179 {
639d11d3 9180 struct die_info *die
93311388 9181 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9182
1d325ec1 9183 if (die == NULL)
c906108c 9184 {
639d11d3
DC
9185 *new_info_ptr = cur_ptr;
9186 return first_die;
c906108c 9187 }
1d325ec1
DJ
9188
9189 if (!first_die)
9190 first_die = die;
c906108c 9191 else
1d325ec1
DJ
9192 last_sibling->sibling = die;
9193
9194 last_sibling = die;
c906108c 9195 }
c906108c
SS
9196}
9197
93311388
DE
9198/* Read the die from the .debug_info section buffer. Set DIEP to
9199 point to a newly allocated die with its information, except for its
9200 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9201 whether the die has children or not. */
9202
9203static gdb_byte *
9204read_full_die (const struct die_reader_specs *reader,
9205 struct die_info **diep, gdb_byte *info_ptr,
9206 int *has_children)
9207{
9208 unsigned int abbrev_number, bytes_read, i, offset;
9209 struct abbrev_info *abbrev;
9210 struct die_info *die;
9211 struct dwarf2_cu *cu = reader->cu;
9212 bfd *abfd = reader->abfd;
9213
9214 offset = info_ptr - reader->buffer;
9215 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9216 info_ptr += bytes_read;
9217 if (!abbrev_number)
9218 {
9219 *diep = NULL;
9220 *has_children = 0;
9221 return info_ptr;
9222 }
9223
9224 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9225 if (!abbrev)
348e048f
DE
9226 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9227 abbrev_number,
9228 bfd_get_filename (abfd));
9229
93311388
DE
9230 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9231 die->offset = offset;
9232 die->tag = abbrev->tag;
9233 die->abbrev = abbrev_number;
9234
9235 die->num_attrs = abbrev->num_attrs;
9236
9237 for (i = 0; i < abbrev->num_attrs; ++i)
9238 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9239 abfd, info_ptr, cu);
9240
9241 *diep = die;
9242 *has_children = abbrev->has_children;
9243 return info_ptr;
9244}
9245
c906108c
SS
9246/* In DWARF version 2, the description of the debugging information is
9247 stored in a separate .debug_abbrev section. Before we read any
9248 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9249 in a hash table. This function also sets flags in CU describing
9250 the data found in the abbrev table. */
c906108c
SS
9251
9252static void
e5fe5e75 9253dwarf2_read_abbrevs (struct dwarf2_cu *cu)
c906108c 9254{
e5fe5e75 9255 bfd *abfd = cu->objfile->obfd;
e7c27a73 9256 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9257 gdb_byte *abbrev_ptr;
c906108c
SS
9258 struct abbrev_info *cur_abbrev;
9259 unsigned int abbrev_number, bytes_read, abbrev_name;
9260 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9261 struct attr_abbrev *cur_attrs;
9262 unsigned int allocated_attrs;
c906108c 9263
0963b4bd 9264 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9265 obstack_init (&cu->abbrev_obstack);
9266 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9267 (ABBREV_HASH_SIZE
9268 * sizeof (struct abbrev_info *)));
9269 memset (cu->dwarf2_abbrevs, 0,
9270 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9271
be391dca
TT
9272 dwarf2_read_section (dwarf2_per_objfile->objfile,
9273 &dwarf2_per_objfile->abbrev);
dce234bc 9274 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9275 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9276 abbrev_ptr += bytes_read;
9277
f3dd6933
DJ
9278 allocated_attrs = ATTR_ALLOC_CHUNK;
9279 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9280
0963b4bd 9281 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9282 while (abbrev_number)
9283 {
f3dd6933 9284 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9285
9286 /* read in abbrev header */
9287 cur_abbrev->number = abbrev_number;
9288 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9289 abbrev_ptr += bytes_read;
9290 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9291 abbrev_ptr += 1;
9292
72bf9492
DJ
9293 if (cur_abbrev->tag == DW_TAG_namespace)
9294 cu->has_namespace_info = 1;
9295
c906108c
SS
9296 /* now read in declarations */
9297 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9298 abbrev_ptr += bytes_read;
9299 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9300 abbrev_ptr += bytes_read;
9301 while (abbrev_name)
9302 {
f3dd6933 9303 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9304 {
f3dd6933
DJ
9305 allocated_attrs += ATTR_ALLOC_CHUNK;
9306 cur_attrs
9307 = xrealloc (cur_attrs, (allocated_attrs
9308 * sizeof (struct attr_abbrev)));
c906108c 9309 }
ae038cb0
DJ
9310
9311 /* Record whether this compilation unit might have
9312 inter-compilation-unit references. If we don't know what form
9313 this attribute will have, then it might potentially be a
9314 DW_FORM_ref_addr, so we conservatively expect inter-CU
9315 references. */
9316
9317 if (abbrev_form == DW_FORM_ref_addr
9318 || abbrev_form == DW_FORM_indirect)
9319 cu->has_form_ref_addr = 1;
9320
f3dd6933
DJ
9321 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9322 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9323 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9324 abbrev_ptr += bytes_read;
9325 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9326 abbrev_ptr += bytes_read;
9327 }
9328
f3dd6933
DJ
9329 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9330 (cur_abbrev->num_attrs
9331 * sizeof (struct attr_abbrev)));
9332 memcpy (cur_abbrev->attrs, cur_attrs,
9333 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9334
c906108c 9335 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9336 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9337 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9338
9339 /* Get next abbreviation.
9340 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9341 always properly terminated with an abbrev number of 0.
9342 Exit loop if we encounter an abbreviation which we have
9343 already read (which means we are about to read the abbreviations
9344 for the next compile unit) or if the end of the abbreviation
9345 table is reached. */
dce234bc
PP
9346 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9347 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9348 break;
9349 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9350 abbrev_ptr += bytes_read;
e7c27a73 9351 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9352 break;
9353 }
f3dd6933
DJ
9354
9355 xfree (cur_attrs);
c906108c
SS
9356}
9357
f3dd6933 9358/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9359
c906108c 9360static void
f3dd6933 9361dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9362{
f3dd6933 9363 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9364
f3dd6933
DJ
9365 obstack_free (&cu->abbrev_obstack, NULL);
9366 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9367}
9368
9369/* Lookup an abbrev_info structure in the abbrev hash table. */
9370
9371static struct abbrev_info *
e7c27a73 9372dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9373{
9374 unsigned int hash_number;
9375 struct abbrev_info *abbrev;
9376
9377 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9378 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9379
9380 while (abbrev)
9381 {
9382 if (abbrev->number == number)
9383 return abbrev;
9384 else
9385 abbrev = abbrev->next;
9386 }
9387 return NULL;
9388}
9389
72bf9492
DJ
9390/* Returns nonzero if TAG represents a type that we might generate a partial
9391 symbol for. */
9392
9393static int
9394is_type_tag_for_partial (int tag)
9395{
9396 switch (tag)
9397 {
9398#if 0
9399 /* Some types that would be reasonable to generate partial symbols for,
9400 that we don't at present. */
9401 case DW_TAG_array_type:
9402 case DW_TAG_file_type:
9403 case DW_TAG_ptr_to_member_type:
9404 case DW_TAG_set_type:
9405 case DW_TAG_string_type:
9406 case DW_TAG_subroutine_type:
9407#endif
9408 case DW_TAG_base_type:
9409 case DW_TAG_class_type:
680b30c7 9410 case DW_TAG_interface_type:
72bf9492
DJ
9411 case DW_TAG_enumeration_type:
9412 case DW_TAG_structure_type:
9413 case DW_TAG_subrange_type:
9414 case DW_TAG_typedef:
9415 case DW_TAG_union_type:
9416 return 1;
9417 default:
9418 return 0;
9419 }
9420}
9421
9422/* Load all DIEs that are interesting for partial symbols into memory. */
9423
9424static struct partial_die_info *
93311388
DE
9425load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9426 int building_psymtab, struct dwarf2_cu *cu)
72bf9492 9427{
bb5ed363 9428 struct objfile *objfile = cu->objfile;
72bf9492
DJ
9429 struct partial_die_info *part_die;
9430 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9431 struct abbrev_info *abbrev;
9432 unsigned int bytes_read;
5afb4e99 9433 unsigned int load_all = 0;
72bf9492
DJ
9434
9435 int nesting_level = 1;
9436
9437 parent_die = NULL;
9438 last_die = NULL;
9439
5afb4e99
DJ
9440 if (cu->per_cu && cu->per_cu->load_all_dies)
9441 load_all = 1;
9442
72bf9492
DJ
9443 cu->partial_dies
9444 = htab_create_alloc_ex (cu->header.length / 12,
9445 partial_die_hash,
9446 partial_die_eq,
9447 NULL,
9448 &cu->comp_unit_obstack,
9449 hashtab_obstack_allocate,
9450 dummy_obstack_deallocate);
9451
9452 part_die = obstack_alloc (&cu->comp_unit_obstack,
9453 sizeof (struct partial_die_info));
9454
9455 while (1)
9456 {
9457 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9458
9459 /* A NULL abbrev means the end of a series of children. */
9460 if (abbrev == NULL)
9461 {
9462 if (--nesting_level == 0)
9463 {
9464 /* PART_DIE was probably the last thing allocated on the
9465 comp_unit_obstack, so we could call obstack_free
9466 here. We don't do that because the waste is small,
9467 and will be cleaned up when we're done with this
9468 compilation unit. This way, we're also more robust
9469 against other users of the comp_unit_obstack. */
9470 return first_die;
9471 }
9472 info_ptr += bytes_read;
9473 last_die = parent_die;
9474 parent_die = parent_die->die_parent;
9475 continue;
9476 }
9477
98bfdba5
PA
9478 /* Check for template arguments. We never save these; if
9479 they're seen, we just mark the parent, and go on our way. */
9480 if (parent_die != NULL
9481 && cu->language == language_cplus
9482 && (abbrev->tag == DW_TAG_template_type_param
9483 || abbrev->tag == DW_TAG_template_value_param))
9484 {
9485 parent_die->has_template_arguments = 1;
9486
9487 if (!load_all)
9488 {
9489 /* We don't need a partial DIE for the template argument. */
9490 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9491 cu);
9492 continue;
9493 }
9494 }
9495
9496 /* We only recurse into subprograms looking for template arguments.
9497 Skip their other children. */
9498 if (!load_all
9499 && cu->language == language_cplus
9500 && parent_die != NULL
9501 && parent_die->tag == DW_TAG_subprogram)
9502 {
9503 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9504 continue;
9505 }
9506
5afb4e99
DJ
9507 /* Check whether this DIE is interesting enough to save. Normally
9508 we would not be interested in members here, but there may be
9509 later variables referencing them via DW_AT_specification (for
9510 static members). */
9511 if (!load_all
9512 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9513 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9514 && abbrev->tag != DW_TAG_enumerator
9515 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9516 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9517 && abbrev->tag != DW_TAG_variable
5afb4e99 9518 && abbrev->tag != DW_TAG_namespace
f55ee35c 9519 && abbrev->tag != DW_TAG_module
5afb4e99 9520 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9521 {
9522 /* Otherwise we skip to the next sibling, if any. */
93311388 9523 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9524 continue;
9525 }
9526
93311388
DE
9527 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9528 buffer, info_ptr, cu);
72bf9492
DJ
9529
9530 /* This two-pass algorithm for processing partial symbols has a
9531 high cost in cache pressure. Thus, handle some simple cases
9532 here which cover the majority of C partial symbols. DIEs
9533 which neither have specification tags in them, nor could have
9534 specification tags elsewhere pointing at them, can simply be
9535 processed and discarded.
9536
9537 This segment is also optional; scan_partial_symbols and
9538 add_partial_symbol will handle these DIEs if we chain
9539 them in normally. When compilers which do not emit large
9540 quantities of duplicate debug information are more common,
9541 this code can probably be removed. */
9542
9543 /* Any complete simple types at the top level (pretty much all
9544 of them, for a language without namespaces), can be processed
9545 directly. */
9546 if (parent_die == NULL
9547 && part_die->has_specification == 0
9548 && part_die->is_declaration == 0
d8228535 9549 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9550 || part_die->tag == DW_TAG_base_type
9551 || part_die->tag == DW_TAG_subrange_type))
9552 {
9553 if (building_psymtab && part_die->name != NULL)
04a679b8 9554 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9555 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
9556 &objfile->static_psymbols,
9557 0, (CORE_ADDR) 0, cu->language, objfile);
93311388 9558 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9559 continue;
9560 }
9561
d8228535
JK
9562 /* The exception for DW_TAG_typedef with has_children above is
9563 a workaround of GCC PR debug/47510. In the case of this complaint
9564 type_name_no_tag_or_error will error on such types later.
9565
9566 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9567 it could not find the child DIEs referenced later, this is checked
9568 above. In correct DWARF DW_TAG_typedef should have no children. */
9569
9570 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9571 complaint (&symfile_complaints,
9572 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9573 "- DIE at 0x%x [in module %s]"),
bb5ed363 9574 part_die->offset, objfile->name);
d8228535 9575
72bf9492
DJ
9576 /* If we're at the second level, and we're an enumerator, and
9577 our parent has no specification (meaning possibly lives in a
9578 namespace elsewhere), then we can add the partial symbol now
9579 instead of queueing it. */
9580 if (part_die->tag == DW_TAG_enumerator
9581 && parent_die != NULL
9582 && parent_die->die_parent == NULL
9583 && parent_die->tag == DW_TAG_enumeration_type
9584 && parent_die->has_specification == 0)
9585 {
9586 if (part_die->name == NULL)
3e43a32a
MS
9587 complaint (&symfile_complaints,
9588 _("malformed enumerator DIE ignored"));
72bf9492 9589 else if (building_psymtab)
04a679b8 9590 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9591 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9592 (cu->language == language_cplus
9593 || cu->language == language_java)
bb5ed363
DE
9594 ? &objfile->global_psymbols
9595 : &objfile->static_psymbols,
9596 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 9597
93311388 9598 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9599 continue;
9600 }
9601
9602 /* We'll save this DIE so link it in. */
9603 part_die->die_parent = parent_die;
9604 part_die->die_sibling = NULL;
9605 part_die->die_child = NULL;
9606
9607 if (last_die && last_die == parent_die)
9608 last_die->die_child = part_die;
9609 else if (last_die)
9610 last_die->die_sibling = part_die;
9611
9612 last_die = part_die;
9613
9614 if (first_die == NULL)
9615 first_die = part_die;
9616
9617 /* Maybe add the DIE to the hash table. Not all DIEs that we
9618 find interesting need to be in the hash table, because we
9619 also have the parent/sibling/child chains; only those that we
9620 might refer to by offset later during partial symbol reading.
9621
9622 For now this means things that might have be the target of a
9623 DW_AT_specification, DW_AT_abstract_origin, or
9624 DW_AT_extension. DW_AT_extension will refer only to
9625 namespaces; DW_AT_abstract_origin refers to functions (and
9626 many things under the function DIE, but we do not recurse
9627 into function DIEs during partial symbol reading) and
9628 possibly variables as well; DW_AT_specification refers to
9629 declarations. Declarations ought to have the DW_AT_declaration
9630 flag. It happens that GCC forgets to put it in sometimes, but
9631 only for functions, not for types.
9632
9633 Adding more things than necessary to the hash table is harmless
9634 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9635 wasted time in find_partial_die, when we reread the compilation
9636 unit with load_all_dies set. */
72bf9492 9637
5afb4e99 9638 if (load_all
72929c62 9639 || abbrev->tag == DW_TAG_constant
5afb4e99 9640 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9641 || abbrev->tag == DW_TAG_variable
9642 || abbrev->tag == DW_TAG_namespace
9643 || part_die->is_declaration)
9644 {
9645 void **slot;
9646
9647 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9648 part_die->offset, INSERT);
9649 *slot = part_die;
9650 }
9651
9652 part_die = obstack_alloc (&cu->comp_unit_obstack,
9653 sizeof (struct partial_die_info));
9654
9655 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9656 we have no reason to follow the children of structures; for other
98bfdba5
PA
9657 languages we have to, so that we can get at method physnames
9658 to infer fully qualified class names, for DW_AT_specification,
9659 and for C++ template arguments. For C++, we also look one level
9660 inside functions to find template arguments (if the name of the
9661 function does not already contain the template arguments).
bc30ff58
JB
9662
9663 For Ada, we need to scan the children of subprograms and lexical
9664 blocks as well because Ada allows the definition of nested
9665 entities that could be interesting for the debugger, such as
9666 nested subprograms for instance. */
72bf9492 9667 if (last_die->has_children
5afb4e99
DJ
9668 && (load_all
9669 || last_die->tag == DW_TAG_namespace
f55ee35c 9670 || last_die->tag == DW_TAG_module
72bf9492 9671 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9672 || (cu->language == language_cplus
9673 && last_die->tag == DW_TAG_subprogram
9674 && (last_die->name == NULL
9675 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9676 || (cu->language != language_c
9677 && (last_die->tag == DW_TAG_class_type
680b30c7 9678 || last_die->tag == DW_TAG_interface_type
72bf9492 9679 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9680 || last_die->tag == DW_TAG_union_type))
9681 || (cu->language == language_ada
9682 && (last_die->tag == DW_TAG_subprogram
9683 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9684 {
9685 nesting_level++;
9686 parent_die = last_die;
9687 continue;
9688 }
9689
9690 /* Otherwise we skip to the next sibling, if any. */
93311388 9691 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9692
9693 /* Back to the top, do it again. */
9694 }
9695}
9696
c906108c
SS
9697/* Read a minimal amount of information into the minimal die structure. */
9698
fe1b8b76 9699static gdb_byte *
72bf9492
DJ
9700read_partial_die (struct partial_die_info *part_die,
9701 struct abbrev_info *abbrev,
9702 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9703 gdb_byte *buffer, gdb_byte *info_ptr,
9704 struct dwarf2_cu *cu)
c906108c 9705{
bb5ed363 9706 struct objfile *objfile = cu->objfile;
fa238c03 9707 unsigned int i;
c906108c 9708 struct attribute attr;
c5aa993b 9709 int has_low_pc_attr = 0;
c906108c
SS
9710 int has_high_pc_attr = 0;
9711
72bf9492 9712 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9713
93311388 9714 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9715
9716 info_ptr += abbrev_len;
9717
9718 if (abbrev == NULL)
9719 return info_ptr;
9720
c906108c
SS
9721 part_die->tag = abbrev->tag;
9722 part_die->has_children = abbrev->has_children;
c906108c
SS
9723
9724 for (i = 0; i < abbrev->num_attrs; ++i)
9725 {
e7c27a73 9726 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9727
9728 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9729 partial symbol table. */
c906108c
SS
9730 switch (attr.name)
9731 {
9732 case DW_AT_name:
71c25dea
TT
9733 switch (part_die->tag)
9734 {
9735 case DW_TAG_compile_unit:
348e048f 9736 case DW_TAG_type_unit:
71c25dea
TT
9737 /* Compilation units have a DW_AT_name that is a filename, not
9738 a source language identifier. */
9739 case DW_TAG_enumeration_type:
9740 case DW_TAG_enumerator:
9741 /* These tags always have simple identifiers already; no need
9742 to canonicalize them. */
9743 part_die->name = DW_STRING (&attr);
9744 break;
9745 default:
9746 part_die->name
9747 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 9748 &objfile->objfile_obstack);
71c25dea
TT
9749 break;
9750 }
c906108c 9751 break;
31ef98ae 9752 case DW_AT_linkage_name:
c906108c 9753 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9754 /* Note that both forms of linkage name might appear. We
9755 assume they will be the same, and we only store the last
9756 one we see. */
94af9270
KS
9757 if (cu->language == language_ada)
9758 part_die->name = DW_STRING (&attr);
abc72ce4 9759 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9760 break;
9761 case DW_AT_low_pc:
9762 has_low_pc_attr = 1;
9763 part_die->lowpc = DW_ADDR (&attr);
9764 break;
9765 case DW_AT_high_pc:
9766 has_high_pc_attr = 1;
9767 part_die->highpc = DW_ADDR (&attr);
9768 break;
9769 case DW_AT_location:
0963b4bd 9770 /* Support the .debug_loc offsets. */
8e19ed76
PS
9771 if (attr_form_is_block (&attr))
9772 {
9773 part_die->locdesc = DW_BLOCK (&attr);
9774 }
3690dd37 9775 else if (attr_form_is_section_offset (&attr))
8e19ed76 9776 {
4d3c2250 9777 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9778 }
9779 else
9780 {
4d3c2250
KB
9781 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9782 "partial symbol information");
8e19ed76 9783 }
c906108c 9784 break;
c906108c
SS
9785 case DW_AT_external:
9786 part_die->is_external = DW_UNSND (&attr);
9787 break;
9788 case DW_AT_declaration:
9789 part_die->is_declaration = DW_UNSND (&attr);
9790 break;
9791 case DW_AT_type:
9792 part_die->has_type = 1;
9793 break;
9794 case DW_AT_abstract_origin:
9795 case DW_AT_specification:
72bf9492
DJ
9796 case DW_AT_extension:
9797 part_die->has_specification = 1;
c764a876 9798 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9799 break;
9800 case DW_AT_sibling:
9801 /* Ignore absolute siblings, they might point outside of
9802 the current compile unit. */
9803 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9804 complaint (&symfile_complaints,
9805 _("ignoring absolute DW_AT_sibling"));
c906108c 9806 else
93311388 9807 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9808 break;
fa4028e9
JB
9809 case DW_AT_byte_size:
9810 part_die->has_byte_size = 1;
9811 break;
68511cec
CES
9812 case DW_AT_calling_convention:
9813 /* DWARF doesn't provide a way to identify a program's source-level
9814 entry point. DW_AT_calling_convention attributes are only meant
9815 to describe functions' calling conventions.
9816
9817 However, because it's a necessary piece of information in
9818 Fortran, and because DW_CC_program is the only piece of debugging
9819 information whose definition refers to a 'main program' at all,
9820 several compilers have begun marking Fortran main programs with
9821 DW_CC_program --- even when those functions use the standard
9822 calling conventions.
9823
9824 So until DWARF specifies a way to provide this information and
9825 compilers pick up the new representation, we'll support this
9826 practice. */
9827 if (DW_UNSND (&attr) == DW_CC_program
9828 && cu->language == language_fortran)
01f8c46d
JK
9829 {
9830 set_main_name (part_die->name);
9831
9832 /* As this DIE has a static linkage the name would be difficult
9833 to look up later. */
9834 language_of_main = language_fortran;
9835 }
68511cec 9836 break;
c906108c
SS
9837 default:
9838 break;
9839 }
9840 }
9841
9373cf26
JK
9842 if (has_low_pc_attr && has_high_pc_attr)
9843 {
9844 /* When using the GNU linker, .gnu.linkonce. sections are used to
9845 eliminate duplicate copies of functions and vtables and such.
9846 The linker will arbitrarily choose one and discard the others.
9847 The AT_*_pc values for such functions refer to local labels in
9848 these sections. If the section from that file was discarded, the
9849 labels are not in the output, so the relocs get a value of 0.
9850 If this is a discarded function, mark the pc bounds as invalid,
9851 so that GDB will ignore it. */
9852 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9853 {
bb5ed363 9854 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9855
9856 complaint (&symfile_complaints,
9857 _("DW_AT_low_pc %s is zero "
9858 "for DIE at 0x%x [in module %s]"),
9859 paddress (gdbarch, part_die->lowpc),
bb5ed363 9860 part_die->offset, objfile->name);
9373cf26
JK
9861 }
9862 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9863 else if (part_die->lowpc >= part_die->highpc)
9864 {
bb5ed363 9865 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
9866
9867 complaint (&symfile_complaints,
9868 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9869 "for DIE at 0x%x [in module %s]"),
9870 paddress (gdbarch, part_die->lowpc),
9871 paddress (gdbarch, part_die->highpc),
bb5ed363 9872 part_die->offset, objfile->name);
9373cf26
JK
9873 }
9874 else
9875 part_die->has_pc_info = 1;
9876 }
85cbf3d3 9877
c906108c
SS
9878 return info_ptr;
9879}
9880
72bf9492
DJ
9881/* Find a cached partial DIE at OFFSET in CU. */
9882
9883static struct partial_die_info *
c764a876 9884find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9885{
9886 struct partial_die_info *lookup_die = NULL;
9887 struct partial_die_info part_die;
9888
9889 part_die.offset = offset;
9890 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9891
72bf9492
DJ
9892 return lookup_die;
9893}
9894
348e048f
DE
9895/* Find a partial DIE at OFFSET, which may or may not be in CU,
9896 except in the case of .debug_types DIEs which do not reference
9897 outside their CU (they do however referencing other types via
55f1336d 9898 DW_FORM_ref_sig8). */
72bf9492
DJ
9899
9900static struct partial_die_info *
c764a876 9901find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9902{
bb5ed363 9903 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
9904 struct dwarf2_per_cu_data *per_cu = NULL;
9905 struct partial_die_info *pd = NULL;
72bf9492 9906
b0df02fd 9907 if (cu->per_cu->debug_types_section)
348e048f
DE
9908 {
9909 pd = find_partial_die_in_comp_unit (offset, cu);
9910 if (pd != NULL)
9911 return pd;
9912 goto not_found;
9913 }
9914
45452591 9915 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9916 {
9917 pd = find_partial_die_in_comp_unit (offset, cu);
9918 if (pd != NULL)
9919 return pd;
9920 }
72bf9492 9921
bb5ed363 9922 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
ae038cb0 9923
98bfdba5 9924 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
a0f42c21 9925 load_partial_comp_unit (per_cu);
ae038cb0
DJ
9926
9927 per_cu->cu->last_used = 0;
5afb4e99
DJ
9928 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9929
9930 if (pd == NULL && per_cu->load_all_dies == 0)
9931 {
9932 struct cleanup *back_to;
9933 struct partial_die_info comp_unit_die;
9934 struct abbrev_info *abbrev;
9935 unsigned int bytes_read;
9936 char *info_ptr;
9937
9938 per_cu->load_all_dies = 1;
9939
9940 /* Re-read the DIEs. */
9941 back_to = make_cleanup (null_cleanup, 0);
9942 if (per_cu->cu->dwarf2_abbrevs == NULL)
9943 {
e5fe5e75 9944 dwarf2_read_abbrevs (per_cu->cu);
53d72f98 9945 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9946 }
dce234bc 9947 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9948 + per_cu->cu->header.offset
9949 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9950 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9951 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
bb5ed363 9952 objfile->obfd,
93311388 9953 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9954 per_cu->cu);
9955 if (comp_unit_die.has_children)
bb5ed363 9956 load_partial_dies (objfile->obfd,
93311388
DE
9957 dwarf2_per_objfile->info.buffer, info_ptr,
9958 0, per_cu->cu);
5afb4e99
DJ
9959 do_cleanups (back_to);
9960
9961 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9962 }
9963
348e048f
DE
9964 not_found:
9965
5afb4e99
DJ
9966 if (pd == NULL)
9967 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9968 _("could not find partial DIE 0x%x "
9969 "in cache [from module %s]\n"),
bb5ed363 9970 offset, bfd_get_filename (objfile->obfd));
5afb4e99 9971 return pd;
72bf9492
DJ
9972}
9973
abc72ce4
DE
9974/* See if we can figure out if the class lives in a namespace. We do
9975 this by looking for a member function; its demangled name will
9976 contain namespace info, if there is any. */
9977
9978static void
9979guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9980 struct dwarf2_cu *cu)
9981{
9982 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9983 what template types look like, because the demangler
9984 frequently doesn't give the same name as the debug info. We
9985 could fix this by only using the demangled name to get the
9986 prefix (but see comment in read_structure_type). */
9987
9988 struct partial_die_info *real_pdi;
9989 struct partial_die_info *child_pdi;
9990
9991 /* If this DIE (this DIE's specification, if any) has a parent, then
9992 we should not do this. We'll prepend the parent's fully qualified
9993 name when we create the partial symbol. */
9994
9995 real_pdi = struct_pdi;
9996 while (real_pdi->has_specification)
9997 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9998
9999 if (real_pdi->die_parent != NULL)
10000 return;
10001
10002 for (child_pdi = struct_pdi->die_child;
10003 child_pdi != NULL;
10004 child_pdi = child_pdi->die_sibling)
10005 {
10006 if (child_pdi->tag == DW_TAG_subprogram
10007 && child_pdi->linkage_name != NULL)
10008 {
10009 char *actual_class_name
10010 = language_class_name_from_physname (cu->language_defn,
10011 child_pdi->linkage_name);
10012 if (actual_class_name != NULL)
10013 {
10014 struct_pdi->name
10015 = obsavestring (actual_class_name,
10016 strlen (actual_class_name),
10017 &cu->objfile->objfile_obstack);
10018 xfree (actual_class_name);
10019 }
10020 break;
10021 }
10022 }
10023}
10024
72bf9492
DJ
10025/* Adjust PART_DIE before generating a symbol for it. This function
10026 may set the is_external flag or change the DIE's name. */
10027
10028static void
10029fixup_partial_die (struct partial_die_info *part_die,
10030 struct dwarf2_cu *cu)
10031{
abc72ce4
DE
10032 /* Once we've fixed up a die, there's no point in doing so again.
10033 This also avoids a memory leak if we were to call
10034 guess_partial_die_structure_name multiple times. */
10035 if (part_die->fixup_called)
10036 return;
10037
72bf9492
DJ
10038 /* If we found a reference attribute and the DIE has no name, try
10039 to find a name in the referred to DIE. */
10040
10041 if (part_die->name == NULL && part_die->has_specification)
10042 {
10043 struct partial_die_info *spec_die;
72bf9492 10044
10b3939b 10045 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10046
10b3939b 10047 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10048
10049 if (spec_die->name)
10050 {
10051 part_die->name = spec_die->name;
10052
10053 /* Copy DW_AT_external attribute if it is set. */
10054 if (spec_die->is_external)
10055 part_die->is_external = spec_die->is_external;
10056 }
10057 }
10058
10059 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10060
10061 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10062 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10063
abc72ce4
DE
10064 /* If there is no parent die to provide a namespace, and there are
10065 children, see if we can determine the namespace from their linkage
10066 name.
10067 NOTE: We need to do this even if cu->has_namespace_info != 0.
10068 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10069 if (cu->language == language_cplus
8b70b953 10070 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10071 && part_die->die_parent == NULL
10072 && part_die->has_children
10073 && (part_die->tag == DW_TAG_class_type
10074 || part_die->tag == DW_TAG_structure_type
10075 || part_die->tag == DW_TAG_union_type))
10076 guess_partial_die_structure_name (part_die, cu);
10077
53832f31
TT
10078 /* GCC might emit a nameless struct or union that has a linkage
10079 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10080 if (part_die->name == NULL
96408a79
SA
10081 && (part_die->tag == DW_TAG_class_type
10082 || part_die->tag == DW_TAG_interface_type
10083 || part_die->tag == DW_TAG_structure_type
10084 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10085 && part_die->linkage_name != NULL)
10086 {
10087 char *demangled;
10088
10089 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10090 if (demangled)
10091 {
96408a79
SA
10092 const char *base;
10093
10094 /* Strip any leading namespaces/classes, keep only the base name.
10095 DW_AT_name for named DIEs does not contain the prefixes. */
10096 base = strrchr (demangled, ':');
10097 if (base && base > demangled && base[-1] == ':')
10098 base++;
10099 else
10100 base = demangled;
10101
10102 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10103 &cu->objfile->objfile_obstack);
10104 xfree (demangled);
10105 }
10106 }
10107
abc72ce4 10108 part_die->fixup_called = 1;
72bf9492
DJ
10109}
10110
a8329558 10111/* Read an attribute value described by an attribute form. */
c906108c 10112
fe1b8b76 10113static gdb_byte *
a8329558 10114read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10115 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10116 struct dwarf2_cu *cu)
c906108c 10117{
e7c27a73 10118 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10119 unsigned int bytes_read;
10120 struct dwarf_block *blk;
10121
a8329558
KW
10122 attr->form = form;
10123 switch (form)
c906108c 10124 {
c906108c 10125 case DW_FORM_ref_addr:
ae411497
TT
10126 if (cu->header.version == 2)
10127 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10128 else
3e43a32a
MS
10129 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10130 &cu->header, &bytes_read);
ae411497
TT
10131 info_ptr += bytes_read;
10132 break;
10133 case DW_FORM_addr:
e7c27a73 10134 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10135 info_ptr += bytes_read;
c906108c
SS
10136 break;
10137 case DW_FORM_block2:
7b5a2f43 10138 blk = dwarf_alloc_block (cu);
c906108c
SS
10139 blk->size = read_2_bytes (abfd, info_ptr);
10140 info_ptr += 2;
10141 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10142 info_ptr += blk->size;
10143 DW_BLOCK (attr) = blk;
10144 break;
10145 case DW_FORM_block4:
7b5a2f43 10146 blk = dwarf_alloc_block (cu);
c906108c
SS
10147 blk->size = read_4_bytes (abfd, info_ptr);
10148 info_ptr += 4;
10149 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10150 info_ptr += blk->size;
10151 DW_BLOCK (attr) = blk;
10152 break;
10153 case DW_FORM_data2:
10154 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10155 info_ptr += 2;
10156 break;
10157 case DW_FORM_data4:
10158 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10159 info_ptr += 4;
10160 break;
10161 case DW_FORM_data8:
10162 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10163 info_ptr += 8;
10164 break;
2dc7f7b3
TT
10165 case DW_FORM_sec_offset:
10166 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10167 info_ptr += bytes_read;
10168 break;
c906108c 10169 case DW_FORM_string:
9b1c24c8 10170 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10171 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10172 info_ptr += bytes_read;
10173 break;
4bdf3d34
JJ
10174 case DW_FORM_strp:
10175 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10176 &bytes_read);
8285870a 10177 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10178 info_ptr += bytes_read;
10179 break;
2dc7f7b3 10180 case DW_FORM_exprloc:
c906108c 10181 case DW_FORM_block:
7b5a2f43 10182 blk = dwarf_alloc_block (cu);
c906108c
SS
10183 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10184 info_ptr += bytes_read;
10185 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10186 info_ptr += blk->size;
10187 DW_BLOCK (attr) = blk;
10188 break;
10189 case DW_FORM_block1:
7b5a2f43 10190 blk = dwarf_alloc_block (cu);
c906108c
SS
10191 blk->size = read_1_byte (abfd, info_ptr);
10192 info_ptr += 1;
10193 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10194 info_ptr += blk->size;
10195 DW_BLOCK (attr) = blk;
10196 break;
10197 case DW_FORM_data1:
10198 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10199 info_ptr += 1;
10200 break;
10201 case DW_FORM_flag:
10202 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10203 info_ptr += 1;
10204 break;
2dc7f7b3
TT
10205 case DW_FORM_flag_present:
10206 DW_UNSND (attr) = 1;
10207 break;
c906108c
SS
10208 case DW_FORM_sdata:
10209 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10210 info_ptr += bytes_read;
10211 break;
10212 case DW_FORM_udata:
10213 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10214 info_ptr += bytes_read;
10215 break;
10216 case DW_FORM_ref1:
10b3939b 10217 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
10218 info_ptr += 1;
10219 break;
10220 case DW_FORM_ref2:
10b3939b 10221 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
10222 info_ptr += 2;
10223 break;
10224 case DW_FORM_ref4:
10b3939b 10225 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
10226 info_ptr += 4;
10227 break;
613e1657 10228 case DW_FORM_ref8:
10b3939b 10229 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
10230 info_ptr += 8;
10231 break;
55f1336d 10232 case DW_FORM_ref_sig8:
348e048f
DE
10233 /* Convert the signature to something we can record in DW_UNSND
10234 for later lookup.
10235 NOTE: This is NULL if the type wasn't found. */
10236 DW_SIGNATURED_TYPE (attr) =
10237 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10238 info_ptr += 8;
10239 break;
c906108c 10240 case DW_FORM_ref_udata:
10b3939b
DJ
10241 DW_ADDR (attr) = (cu->header.offset
10242 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10243 info_ptr += bytes_read;
10244 break;
c906108c 10245 case DW_FORM_indirect:
a8329558
KW
10246 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10247 info_ptr += bytes_read;
e7c27a73 10248 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10249 break;
c906108c 10250 default:
8a3fe4f8 10251 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10252 dwarf_form_name (form),
10253 bfd_get_filename (abfd));
c906108c 10254 }
28e94949
JB
10255
10256 /* We have seen instances where the compiler tried to emit a byte
10257 size attribute of -1 which ended up being encoded as an unsigned
10258 0xffffffff. Although 0xffffffff is technically a valid size value,
10259 an object of this size seems pretty unlikely so we can relatively
10260 safely treat these cases as if the size attribute was invalid and
10261 treat them as zero by default. */
10262 if (attr->name == DW_AT_byte_size
10263 && form == DW_FORM_data4
10264 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10265 {
10266 complaint
10267 (&symfile_complaints,
43bbcdc2
PH
10268 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10269 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10270 DW_UNSND (attr) = 0;
10271 }
28e94949 10272
c906108c
SS
10273 return info_ptr;
10274}
10275
a8329558
KW
10276/* Read an attribute described by an abbreviated attribute. */
10277
fe1b8b76 10278static gdb_byte *
a8329558 10279read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10280 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10281{
10282 attr->name = abbrev->name;
e7c27a73 10283 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10284}
10285
0963b4bd 10286/* Read dwarf information from a buffer. */
c906108c
SS
10287
10288static unsigned int
fe1b8b76 10289read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10290{
fe1b8b76 10291 return bfd_get_8 (abfd, buf);
c906108c
SS
10292}
10293
10294static int
fe1b8b76 10295read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10296{
fe1b8b76 10297 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10298}
10299
10300static unsigned int
fe1b8b76 10301read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10302{
fe1b8b76 10303 return bfd_get_16 (abfd, buf);
c906108c
SS
10304}
10305
21ae7a4d
JK
10306static int
10307read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10308{
10309 return bfd_get_signed_16 (abfd, buf);
10310}
10311
c906108c 10312static unsigned int
fe1b8b76 10313read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10314{
fe1b8b76 10315 return bfd_get_32 (abfd, buf);
c906108c
SS
10316}
10317
21ae7a4d
JK
10318static int
10319read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10320{
10321 return bfd_get_signed_32 (abfd, buf);
10322}
10323
93311388 10324static ULONGEST
fe1b8b76 10325read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10326{
fe1b8b76 10327 return bfd_get_64 (abfd, buf);
c906108c
SS
10328}
10329
10330static CORE_ADDR
fe1b8b76 10331read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10332 unsigned int *bytes_read)
c906108c 10333{
e7c27a73 10334 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10335 CORE_ADDR retval = 0;
10336
107d2387 10337 if (cu_header->signed_addr_p)
c906108c 10338 {
107d2387
AC
10339 switch (cu_header->addr_size)
10340 {
10341 case 2:
fe1b8b76 10342 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10343 break;
10344 case 4:
fe1b8b76 10345 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10346 break;
10347 case 8:
fe1b8b76 10348 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10349 break;
10350 default:
8e65ff28 10351 internal_error (__FILE__, __LINE__,
e2e0b3e5 10352 _("read_address: bad switch, signed [in module %s]"),
659b0389 10353 bfd_get_filename (abfd));
107d2387
AC
10354 }
10355 }
10356 else
10357 {
10358 switch (cu_header->addr_size)
10359 {
10360 case 2:
fe1b8b76 10361 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10362 break;
10363 case 4:
fe1b8b76 10364 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10365 break;
10366 case 8:
fe1b8b76 10367 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10368 break;
10369 default:
8e65ff28 10370 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10371 _("read_address: bad switch, "
10372 "unsigned [in module %s]"),
659b0389 10373 bfd_get_filename (abfd));
107d2387 10374 }
c906108c 10375 }
64367e0a 10376
107d2387
AC
10377 *bytes_read = cu_header->addr_size;
10378 return retval;
c906108c
SS
10379}
10380
f7ef9339 10381/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10382 specification allows the initial length to take up either 4 bytes
10383 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10384 bytes describe the length and all offsets will be 8 bytes in length
10385 instead of 4.
10386
f7ef9339
KB
10387 An older, non-standard 64-bit format is also handled by this
10388 function. The older format in question stores the initial length
10389 as an 8-byte quantity without an escape value. Lengths greater
10390 than 2^32 aren't very common which means that the initial 4 bytes
10391 is almost always zero. Since a length value of zero doesn't make
10392 sense for the 32-bit format, this initial zero can be considered to
10393 be an escape value which indicates the presence of the older 64-bit
10394 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10395 greater than 4GB. If it becomes necessary to handle lengths
10396 somewhat larger than 4GB, we could allow other small values (such
10397 as the non-sensical values of 1, 2, and 3) to also be used as
10398 escape values indicating the presence of the old format.
f7ef9339 10399
917c78fc
MK
10400 The value returned via bytes_read should be used to increment the
10401 relevant pointer after calling read_initial_length().
c764a876 10402
613e1657
KB
10403 [ Note: read_initial_length() and read_offset() are based on the
10404 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10405 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10406 from:
10407
f7ef9339 10408 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10409
613e1657
KB
10410 This document is only a draft and is subject to change. (So beware.)
10411
f7ef9339 10412 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10413 determined empirically by examining 64-bit ELF files produced by
10414 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10415
10416 - Kevin, July 16, 2002
613e1657
KB
10417 ] */
10418
10419static LONGEST
c764a876 10420read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10421{
fe1b8b76 10422 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10423
dd373385 10424 if (length == 0xffffffff)
613e1657 10425 {
fe1b8b76 10426 length = bfd_get_64 (abfd, buf + 4);
613e1657 10427 *bytes_read = 12;
613e1657 10428 }
dd373385 10429 else if (length == 0)
f7ef9339 10430 {
dd373385 10431 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10432 length = bfd_get_64 (abfd, buf);
f7ef9339 10433 *bytes_read = 8;
f7ef9339 10434 }
613e1657
KB
10435 else
10436 {
10437 *bytes_read = 4;
613e1657
KB
10438 }
10439
c764a876
DE
10440 return length;
10441}
dd373385 10442
c764a876
DE
10443/* Cover function for read_initial_length.
10444 Returns the length of the object at BUF, and stores the size of the
10445 initial length in *BYTES_READ and stores the size that offsets will be in
10446 *OFFSET_SIZE.
10447 If the initial length size is not equivalent to that specified in
10448 CU_HEADER then issue a complaint.
10449 This is useful when reading non-comp-unit headers. */
dd373385 10450
c764a876
DE
10451static LONGEST
10452read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10453 const struct comp_unit_head *cu_header,
10454 unsigned int *bytes_read,
10455 unsigned int *offset_size)
10456{
10457 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10458
10459 gdb_assert (cu_header->initial_length_size == 4
10460 || cu_header->initial_length_size == 8
10461 || cu_header->initial_length_size == 12);
10462
10463 if (cu_header->initial_length_size != *bytes_read)
10464 complaint (&symfile_complaints,
10465 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10466
c764a876 10467 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10468 return length;
613e1657
KB
10469}
10470
10471/* Read an offset from the data stream. The size of the offset is
917c78fc 10472 given by cu_header->offset_size. */
613e1657
KB
10473
10474static LONGEST
fe1b8b76 10475read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10476 unsigned int *bytes_read)
c764a876
DE
10477{
10478 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10479
c764a876
DE
10480 *bytes_read = cu_header->offset_size;
10481 return offset;
10482}
10483
10484/* Read an offset from the data stream. */
10485
10486static LONGEST
10487read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10488{
10489 LONGEST retval = 0;
10490
c764a876 10491 switch (offset_size)
613e1657
KB
10492 {
10493 case 4:
fe1b8b76 10494 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10495 break;
10496 case 8:
fe1b8b76 10497 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10498 break;
10499 default:
8e65ff28 10500 internal_error (__FILE__, __LINE__,
c764a876 10501 _("read_offset_1: bad switch [in module %s]"),
659b0389 10502 bfd_get_filename (abfd));
613e1657
KB
10503 }
10504
917c78fc 10505 return retval;
613e1657
KB
10506}
10507
fe1b8b76
JB
10508static gdb_byte *
10509read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10510{
10511 /* If the size of a host char is 8 bits, we can return a pointer
10512 to the buffer, otherwise we have to copy the data to a buffer
10513 allocated on the temporary obstack. */
4bdf3d34 10514 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10515 return buf;
c906108c
SS
10516}
10517
10518static char *
9b1c24c8 10519read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10520{
10521 /* If the size of a host char is 8 bits, we can return a pointer
10522 to the string, otherwise we have to copy the string to a buffer
10523 allocated on the temporary obstack. */
4bdf3d34 10524 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10525 if (*buf == '\0')
10526 {
10527 *bytes_read_ptr = 1;
10528 return NULL;
10529 }
fe1b8b76
JB
10530 *bytes_read_ptr = strlen ((char *) buf) + 1;
10531 return (char *) buf;
4bdf3d34
JJ
10532}
10533
10534static char *
cf2c3c16 10535read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10536{
be391dca 10537 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10538 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10539 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10540 bfd_get_filename (abfd));
dce234bc 10541 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10542 error (_("DW_FORM_strp pointing outside of "
10543 ".debug_str section [in module %s]"),
10544 bfd_get_filename (abfd));
4bdf3d34 10545 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10546 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10547 return NULL;
dce234bc 10548 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10549}
10550
cf2c3c16
TT
10551static char *
10552read_indirect_string (bfd *abfd, gdb_byte *buf,
10553 const struct comp_unit_head *cu_header,
10554 unsigned int *bytes_read_ptr)
10555{
10556 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10557
10558 return read_indirect_string_at_offset (abfd, str_offset);
10559}
10560
ce5d95e1 10561static unsigned long
fe1b8b76 10562read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10563{
ce5d95e1
JB
10564 unsigned long result;
10565 unsigned int num_read;
c906108c
SS
10566 int i, shift;
10567 unsigned char byte;
10568
10569 result = 0;
10570 shift = 0;
10571 num_read = 0;
10572 i = 0;
10573 while (1)
10574 {
fe1b8b76 10575 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10576 buf++;
10577 num_read++;
ce5d95e1 10578 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10579 if ((byte & 128) == 0)
10580 {
10581 break;
10582 }
10583 shift += 7;
10584 }
10585 *bytes_read_ptr = num_read;
10586 return result;
10587}
10588
ce5d95e1 10589static long
fe1b8b76 10590read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10591{
ce5d95e1 10592 long result;
77e0b926 10593 int i, shift, num_read;
c906108c
SS
10594 unsigned char byte;
10595
10596 result = 0;
10597 shift = 0;
c906108c
SS
10598 num_read = 0;
10599 i = 0;
10600 while (1)
10601 {
fe1b8b76 10602 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10603 buf++;
10604 num_read++;
ce5d95e1 10605 result |= ((long)(byte & 127) << shift);
c906108c
SS
10606 shift += 7;
10607 if ((byte & 128) == 0)
10608 {
10609 break;
10610 }
10611 }
77e0b926
DJ
10612 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10613 result |= -(((long)1) << shift);
c906108c
SS
10614 *bytes_read_ptr = num_read;
10615 return result;
10616}
10617
4bb7a0a7
DJ
10618/* Return a pointer to just past the end of an LEB128 number in BUF. */
10619
fe1b8b76
JB
10620static gdb_byte *
10621skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10622{
10623 int byte;
10624
10625 while (1)
10626 {
fe1b8b76 10627 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10628 buf++;
10629 if ((byte & 128) == 0)
10630 return buf;
10631 }
10632}
10633
c906108c 10634static void
e142c38c 10635set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10636{
10637 switch (lang)
10638 {
10639 case DW_LANG_C89:
76bee0cc 10640 case DW_LANG_C99:
c906108c 10641 case DW_LANG_C:
e142c38c 10642 cu->language = language_c;
c906108c
SS
10643 break;
10644 case DW_LANG_C_plus_plus:
e142c38c 10645 cu->language = language_cplus;
c906108c 10646 break;
6aecb9c2
JB
10647 case DW_LANG_D:
10648 cu->language = language_d;
10649 break;
c906108c
SS
10650 case DW_LANG_Fortran77:
10651 case DW_LANG_Fortran90:
b21b22e0 10652 case DW_LANG_Fortran95:
e142c38c 10653 cu->language = language_fortran;
c906108c
SS
10654 break;
10655 case DW_LANG_Mips_Assembler:
e142c38c 10656 cu->language = language_asm;
c906108c 10657 break;
bebd888e 10658 case DW_LANG_Java:
e142c38c 10659 cu->language = language_java;
bebd888e 10660 break;
c906108c 10661 case DW_LANG_Ada83:
8aaf0b47 10662 case DW_LANG_Ada95:
bc5f45f8
JB
10663 cu->language = language_ada;
10664 break;
72019c9c
GM
10665 case DW_LANG_Modula2:
10666 cu->language = language_m2;
10667 break;
fe8e67fd
PM
10668 case DW_LANG_Pascal83:
10669 cu->language = language_pascal;
10670 break;
22566fbd
DJ
10671 case DW_LANG_ObjC:
10672 cu->language = language_objc;
10673 break;
c906108c
SS
10674 case DW_LANG_Cobol74:
10675 case DW_LANG_Cobol85:
c906108c 10676 default:
e142c38c 10677 cu->language = language_minimal;
c906108c
SS
10678 break;
10679 }
e142c38c 10680 cu->language_defn = language_def (cu->language);
c906108c
SS
10681}
10682
10683/* Return the named attribute or NULL if not there. */
10684
10685static struct attribute *
e142c38c 10686dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10687{
10688 unsigned int i;
10689 struct attribute *spec = NULL;
10690
10691 for (i = 0; i < die->num_attrs; ++i)
10692 {
10693 if (die->attrs[i].name == name)
10b3939b 10694 return &die->attrs[i];
c906108c
SS
10695 if (die->attrs[i].name == DW_AT_specification
10696 || die->attrs[i].name == DW_AT_abstract_origin)
10697 spec = &die->attrs[i];
10698 }
c906108c 10699
10b3939b 10700 if (spec)
f2f0e013
DJ
10701 {
10702 die = follow_die_ref (die, spec, &cu);
10703 return dwarf2_attr (die, name, cu);
10704 }
c5aa993b 10705
c906108c
SS
10706 return NULL;
10707}
10708
348e048f
DE
10709/* Return the named attribute or NULL if not there,
10710 but do not follow DW_AT_specification, etc.
10711 This is for use in contexts where we're reading .debug_types dies.
10712 Following DW_AT_specification, DW_AT_abstract_origin will take us
10713 back up the chain, and we want to go down. */
10714
10715static struct attribute *
10716dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10717 struct dwarf2_cu *cu)
10718{
10719 unsigned int i;
10720
10721 for (i = 0; i < die->num_attrs; ++i)
10722 if (die->attrs[i].name == name)
10723 return &die->attrs[i];
10724
10725 return NULL;
10726}
10727
05cf31d1
JB
10728/* Return non-zero iff the attribute NAME is defined for the given DIE,
10729 and holds a non-zero value. This function should only be used for
2dc7f7b3 10730 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10731
10732static int
10733dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10734{
10735 struct attribute *attr = dwarf2_attr (die, name, cu);
10736
10737 return (attr && DW_UNSND (attr));
10738}
10739
3ca72b44 10740static int
e142c38c 10741die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10742{
05cf31d1
JB
10743 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10744 which value is non-zero. However, we have to be careful with
10745 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10746 (via dwarf2_flag_true_p) follows this attribute. So we may
10747 end up accidently finding a declaration attribute that belongs
10748 to a different DIE referenced by the specification attribute,
10749 even though the given DIE does not have a declaration attribute. */
10750 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10751 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10752}
10753
63d06c5c 10754/* Return the die giving the specification for DIE, if there is
f2f0e013 10755 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10756 containing the return value on output. If there is no
10757 specification, but there is an abstract origin, that is
10758 returned. */
63d06c5c
DC
10759
10760static struct die_info *
f2f0e013 10761die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10762{
f2f0e013
DJ
10763 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10764 *spec_cu);
63d06c5c 10765
edb3359d
DJ
10766 if (spec_attr == NULL)
10767 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10768
63d06c5c
DC
10769 if (spec_attr == NULL)
10770 return NULL;
10771 else
f2f0e013 10772 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10773}
c906108c 10774
debd256d 10775/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10776 refers to.
10777 NOTE: This is also used as a "cleanup" function. */
10778
debd256d
JB
10779static void
10780free_line_header (struct line_header *lh)
10781{
10782 if (lh->standard_opcode_lengths)
a8bc7b56 10783 xfree (lh->standard_opcode_lengths);
debd256d
JB
10784
10785 /* Remember that all the lh->file_names[i].name pointers are
10786 pointers into debug_line_buffer, and don't need to be freed. */
10787 if (lh->file_names)
a8bc7b56 10788 xfree (lh->file_names);
debd256d
JB
10789
10790 /* Similarly for the include directory names. */
10791 if (lh->include_dirs)
a8bc7b56 10792 xfree (lh->include_dirs);
debd256d 10793
a8bc7b56 10794 xfree (lh);
debd256d
JB
10795}
10796
debd256d 10797/* Add an entry to LH's include directory table. */
ae2de4f8 10798
debd256d
JB
10799static void
10800add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10801{
debd256d
JB
10802 /* Grow the array if necessary. */
10803 if (lh->include_dirs_size == 0)
c5aa993b 10804 {
debd256d
JB
10805 lh->include_dirs_size = 1; /* for testing */
10806 lh->include_dirs = xmalloc (lh->include_dirs_size
10807 * sizeof (*lh->include_dirs));
10808 }
10809 else if (lh->num_include_dirs >= lh->include_dirs_size)
10810 {
10811 lh->include_dirs_size *= 2;
10812 lh->include_dirs = xrealloc (lh->include_dirs,
10813 (lh->include_dirs_size
10814 * sizeof (*lh->include_dirs)));
c5aa993b 10815 }
c906108c 10816
debd256d
JB
10817 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10818}
6e70227d 10819
debd256d 10820/* Add an entry to LH's file name table. */
ae2de4f8 10821
debd256d
JB
10822static void
10823add_file_name (struct line_header *lh,
10824 char *name,
10825 unsigned int dir_index,
10826 unsigned int mod_time,
10827 unsigned int length)
10828{
10829 struct file_entry *fe;
10830
10831 /* Grow the array if necessary. */
10832 if (lh->file_names_size == 0)
10833 {
10834 lh->file_names_size = 1; /* for testing */
10835 lh->file_names = xmalloc (lh->file_names_size
10836 * sizeof (*lh->file_names));
10837 }
10838 else if (lh->num_file_names >= lh->file_names_size)
10839 {
10840 lh->file_names_size *= 2;
10841 lh->file_names = xrealloc (lh->file_names,
10842 (lh->file_names_size
10843 * sizeof (*lh->file_names)));
10844 }
10845
10846 fe = &lh->file_names[lh->num_file_names++];
10847 fe->name = name;
10848 fe->dir_index = dir_index;
10849 fe->mod_time = mod_time;
10850 fe->length = length;
aaa75496 10851 fe->included_p = 0;
cb1df416 10852 fe->symtab = NULL;
debd256d 10853}
6e70227d 10854
debd256d 10855/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10856 .debug_line, according to the endianness of ABFD. Return a pointer
10857 to a struct line_header, allocated using xmalloc.
debd256d
JB
10858
10859 NOTE: the strings in the include directory and file name tables of
10860 the returned object point into debug_line_buffer, and must not be
10861 freed. */
ae2de4f8 10862
debd256d
JB
10863static struct line_header *
10864dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10865 struct dwarf2_cu *cu)
debd256d
JB
10866{
10867 struct cleanup *back_to;
10868 struct line_header *lh;
fe1b8b76 10869 gdb_byte *line_ptr;
c764a876 10870 unsigned int bytes_read, offset_size;
debd256d
JB
10871 int i;
10872 char *cur_dir, *cur_file;
10873
be391dca 10874 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10875 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10876 {
e2e0b3e5 10877 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10878 return 0;
10879 }
10880
a738430d
MK
10881 /* Make sure that at least there's room for the total_length field.
10882 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10883 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10884 {
4d3c2250 10885 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10886 return 0;
10887 }
10888
10889 lh = xmalloc (sizeof (*lh));
10890 memset (lh, 0, sizeof (*lh));
10891 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10892 (void *) lh);
10893
dce234bc 10894 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10895
a738430d 10896 /* Read in the header. */
6e70227d 10897 lh->total_length =
c764a876
DE
10898 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10899 &bytes_read, &offset_size);
debd256d 10900 line_ptr += bytes_read;
dce234bc
PP
10901 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10902 + dwarf2_per_objfile->line.size))
debd256d 10903 {
4d3c2250 10904 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10905 return 0;
10906 }
10907 lh->statement_program_end = line_ptr + lh->total_length;
10908 lh->version = read_2_bytes (abfd, line_ptr);
10909 line_ptr += 2;
c764a876
DE
10910 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10911 line_ptr += offset_size;
debd256d
JB
10912 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10913 line_ptr += 1;
2dc7f7b3
TT
10914 if (lh->version >= 4)
10915 {
10916 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10917 line_ptr += 1;
10918 }
10919 else
10920 lh->maximum_ops_per_instruction = 1;
10921
10922 if (lh->maximum_ops_per_instruction == 0)
10923 {
10924 lh->maximum_ops_per_instruction = 1;
10925 complaint (&symfile_complaints,
3e43a32a
MS
10926 _("invalid maximum_ops_per_instruction "
10927 "in `.debug_line' section"));
2dc7f7b3
TT
10928 }
10929
debd256d
JB
10930 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10931 line_ptr += 1;
10932 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10933 line_ptr += 1;
10934 lh->line_range = read_1_byte (abfd, line_ptr);
10935 line_ptr += 1;
10936 lh->opcode_base = read_1_byte (abfd, line_ptr);
10937 line_ptr += 1;
10938 lh->standard_opcode_lengths
fe1b8b76 10939 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10940
10941 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10942 for (i = 1; i < lh->opcode_base; ++i)
10943 {
10944 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10945 line_ptr += 1;
10946 }
10947
a738430d 10948 /* Read directory table. */
9b1c24c8 10949 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10950 {
10951 line_ptr += bytes_read;
10952 add_include_dir (lh, cur_dir);
10953 }
10954 line_ptr += bytes_read;
10955
a738430d 10956 /* Read file name table. */
9b1c24c8 10957 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10958 {
10959 unsigned int dir_index, mod_time, length;
10960
10961 line_ptr += bytes_read;
10962 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10963 line_ptr += bytes_read;
10964 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10965 line_ptr += bytes_read;
10966 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10967 line_ptr += bytes_read;
10968
10969 add_file_name (lh, cur_file, dir_index, mod_time, length);
10970 }
10971 line_ptr += bytes_read;
6e70227d 10972 lh->statement_program_start = line_ptr;
debd256d 10973
dce234bc
PP
10974 if (line_ptr > (dwarf2_per_objfile->line.buffer
10975 + dwarf2_per_objfile->line.size))
4d3c2250 10976 complaint (&symfile_complaints,
3e43a32a
MS
10977 _("line number info header doesn't "
10978 "fit in `.debug_line' section"));
debd256d
JB
10979
10980 discard_cleanups (back_to);
10981 return lh;
10982}
c906108c 10983
c6da4cef
DE
10984/* Subroutine of dwarf_decode_lines to simplify it.
10985 Return the file name of the psymtab for included file FILE_INDEX
10986 in line header LH of PST.
10987 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10988 If space for the result is malloc'd, it will be freed by a cleanup.
10989 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10990
10991static char *
10992psymtab_include_file_name (const struct line_header *lh, int file_index,
10993 const struct partial_symtab *pst,
10994 const char *comp_dir)
10995{
10996 const struct file_entry fe = lh->file_names [file_index];
10997 char *include_name = fe.name;
10998 char *include_name_to_compare = include_name;
10999 char *dir_name = NULL;
72b9f47f
TT
11000 const char *pst_filename;
11001 char *copied_name = NULL;
c6da4cef
DE
11002 int file_is_pst;
11003
11004 if (fe.dir_index)
11005 dir_name = lh->include_dirs[fe.dir_index - 1];
11006
11007 if (!IS_ABSOLUTE_PATH (include_name)
11008 && (dir_name != NULL || comp_dir != NULL))
11009 {
11010 /* Avoid creating a duplicate psymtab for PST.
11011 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11012 Before we do the comparison, however, we need to account
11013 for DIR_NAME and COMP_DIR.
11014 First prepend dir_name (if non-NULL). If we still don't
11015 have an absolute path prepend comp_dir (if non-NULL).
11016 However, the directory we record in the include-file's
11017 psymtab does not contain COMP_DIR (to match the
11018 corresponding symtab(s)).
11019
11020 Example:
11021
11022 bash$ cd /tmp
11023 bash$ gcc -g ./hello.c
11024 include_name = "hello.c"
11025 dir_name = "."
11026 DW_AT_comp_dir = comp_dir = "/tmp"
11027 DW_AT_name = "./hello.c" */
11028
11029 if (dir_name != NULL)
11030 {
11031 include_name = concat (dir_name, SLASH_STRING,
11032 include_name, (char *)NULL);
11033 include_name_to_compare = include_name;
11034 make_cleanup (xfree, include_name);
11035 }
11036 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11037 {
11038 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11039 include_name, (char *)NULL);
11040 }
11041 }
11042
11043 pst_filename = pst->filename;
11044 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11045 {
72b9f47f
TT
11046 copied_name = concat (pst->dirname, SLASH_STRING,
11047 pst_filename, (char *)NULL);
11048 pst_filename = copied_name;
c6da4cef
DE
11049 }
11050
1e3fad37 11051 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11052
11053 if (include_name_to_compare != include_name)
11054 xfree (include_name_to_compare);
72b9f47f
TT
11055 if (copied_name != NULL)
11056 xfree (copied_name);
c6da4cef
DE
11057
11058 if (file_is_pst)
11059 return NULL;
11060 return include_name;
11061}
11062
c91513d8
PP
11063/* Ignore this record_line request. */
11064
11065static void
11066noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11067{
11068 return;
11069}
11070
f3f5162e
DE
11071/* Subroutine of dwarf_decode_lines to simplify it.
11072 Process the line number information in LH. */
debd256d 11073
c906108c 11074static void
f3f5162e
DE
11075dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11076 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11077{
a8c50c1f 11078 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11079 gdb_byte *line_end;
a8c50c1f 11080 unsigned int bytes_read, extended_len;
c906108c 11081 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11082 CORE_ADDR baseaddr;
11083 struct objfile *objfile = cu->objfile;
f3f5162e 11084 bfd *abfd = objfile->obfd;
fbf65064 11085 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11086 const int decode_for_pst_p = (pst != NULL);
f3f5162e 11087 struct subfile *last_subfile = NULL;
c91513d8
PP
11088 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11089 = record_line;
e142c38c
DJ
11090
11091 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11092
debd256d
JB
11093 line_ptr = lh->statement_program_start;
11094 line_end = lh->statement_program_end;
c906108c
SS
11095
11096 /* Read the statement sequences until there's nothing left. */
11097 while (line_ptr < line_end)
11098 {
11099 /* state machine registers */
11100 CORE_ADDR address = 0;
11101 unsigned int file = 1;
11102 unsigned int line = 1;
11103 unsigned int column = 0;
debd256d 11104 int is_stmt = lh->default_is_stmt;
c906108c
SS
11105 int basic_block = 0;
11106 int end_sequence = 0;
fbf65064 11107 CORE_ADDR addr;
2dc7f7b3 11108 unsigned char op_index = 0;
c906108c 11109
aaa75496 11110 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11111 {
aaa75496 11112 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11113 /* lh->include_dirs and lh->file_names are 0-based, but the
11114 directory and file name numbers in the statement program
11115 are 1-based. */
11116 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11117 char *dir = NULL;
a738430d 11118
debd256d
JB
11119 if (fe->dir_index)
11120 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11121
11122 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11123 }
11124
a738430d 11125 /* Decode the table. */
c5aa993b 11126 while (!end_sequence)
c906108c
SS
11127 {
11128 op_code = read_1_byte (abfd, line_ptr);
11129 line_ptr += 1;
59205f5a
JB
11130 if (line_ptr > line_end)
11131 {
11132 dwarf2_debug_line_missing_end_sequence_complaint ();
11133 break;
11134 }
9aa1fe7e 11135
debd256d 11136 if (op_code >= lh->opcode_base)
6e70227d 11137 {
a738430d 11138 /* Special operand. */
debd256d 11139 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11140 address += (((op_index + (adj_opcode / lh->line_range))
11141 / lh->maximum_ops_per_instruction)
11142 * lh->minimum_instruction_length);
11143 op_index = ((op_index + (adj_opcode / lh->line_range))
11144 % lh->maximum_ops_per_instruction);
debd256d 11145 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11146 if (lh->num_file_names < file || file == 0)
25e43795 11147 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11148 /* For now we ignore lines not starting on an
11149 instruction boundary. */
11150 else if (op_index == 0)
25e43795
DJ
11151 {
11152 lh->file_names[file - 1].included_p = 1;
ca5f395d 11153 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11154 {
11155 if (last_subfile != current_subfile)
11156 {
11157 addr = gdbarch_addr_bits_remove (gdbarch, address);
11158 if (last_subfile)
c91513d8 11159 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11160 last_subfile = current_subfile;
11161 }
25e43795 11162 /* Append row to matrix using current values. */
7019d805 11163 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11164 (*p_record_line) (current_subfile, line, addr);
366da635 11165 }
25e43795 11166 }
ca5f395d 11167 basic_block = 0;
9aa1fe7e
GK
11168 }
11169 else switch (op_code)
c906108c
SS
11170 {
11171 case DW_LNS_extended_op:
3e43a32a
MS
11172 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11173 &bytes_read);
473b7be6 11174 line_ptr += bytes_read;
a8c50c1f 11175 extended_end = line_ptr + extended_len;
c906108c
SS
11176 extended_op = read_1_byte (abfd, line_ptr);
11177 line_ptr += 1;
11178 switch (extended_op)
11179 {
11180 case DW_LNE_end_sequence:
c91513d8 11181 p_record_line = record_line;
c906108c 11182 end_sequence = 1;
c906108c
SS
11183 break;
11184 case DW_LNE_set_address:
e7c27a73 11185 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11186
11187 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11188 {
11189 /* This line table is for a function which has been
11190 GCd by the linker. Ignore it. PR gdb/12528 */
11191
11192 long line_offset
11193 = line_ptr - dwarf2_per_objfile->line.buffer;
11194
11195 complaint (&symfile_complaints,
11196 _(".debug_line address at offset 0x%lx is 0 "
11197 "[in module %s]"),
bb5ed363 11198 line_offset, objfile->name);
c91513d8
PP
11199 p_record_line = noop_record_line;
11200 }
11201
2dc7f7b3 11202 op_index = 0;
107d2387
AC
11203 line_ptr += bytes_read;
11204 address += baseaddr;
c906108c
SS
11205 break;
11206 case DW_LNE_define_file:
debd256d
JB
11207 {
11208 char *cur_file;
11209 unsigned int dir_index, mod_time, length;
6e70227d 11210
3e43a32a
MS
11211 cur_file = read_direct_string (abfd, line_ptr,
11212 &bytes_read);
debd256d
JB
11213 line_ptr += bytes_read;
11214 dir_index =
11215 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11216 line_ptr += bytes_read;
11217 mod_time =
11218 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11219 line_ptr += bytes_read;
11220 length =
11221 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11222 line_ptr += bytes_read;
11223 add_file_name (lh, cur_file, dir_index, mod_time, length);
11224 }
c906108c 11225 break;
d0c6ba3d
CC
11226 case DW_LNE_set_discriminator:
11227 /* The discriminator is not interesting to the debugger;
11228 just ignore it. */
11229 line_ptr = extended_end;
11230 break;
c906108c 11231 default:
4d3c2250 11232 complaint (&symfile_complaints,
e2e0b3e5 11233 _("mangled .debug_line section"));
debd256d 11234 return;
c906108c 11235 }
a8c50c1f
DJ
11236 /* Make sure that we parsed the extended op correctly. If e.g.
11237 we expected a different address size than the producer used,
11238 we may have read the wrong number of bytes. */
11239 if (line_ptr != extended_end)
11240 {
11241 complaint (&symfile_complaints,
11242 _("mangled .debug_line section"));
11243 return;
11244 }
c906108c
SS
11245 break;
11246 case DW_LNS_copy:
59205f5a 11247 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11248 dwarf2_debug_line_missing_file_complaint ();
11249 else
366da635 11250 {
25e43795 11251 lh->file_names[file - 1].included_p = 1;
ca5f395d 11252 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11253 {
11254 if (last_subfile != current_subfile)
11255 {
11256 addr = gdbarch_addr_bits_remove (gdbarch, address);
11257 if (last_subfile)
c91513d8 11258 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11259 last_subfile = current_subfile;
11260 }
7019d805 11261 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11262 (*p_record_line) (current_subfile, line, addr);
fbf65064 11263 }
366da635 11264 }
c906108c
SS
11265 basic_block = 0;
11266 break;
11267 case DW_LNS_advance_pc:
2dc7f7b3
TT
11268 {
11269 CORE_ADDR adjust
11270 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11271
11272 address += (((op_index + adjust)
11273 / lh->maximum_ops_per_instruction)
11274 * lh->minimum_instruction_length);
11275 op_index = ((op_index + adjust)
11276 % lh->maximum_ops_per_instruction);
11277 line_ptr += bytes_read;
11278 }
c906108c
SS
11279 break;
11280 case DW_LNS_advance_line:
11281 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11282 line_ptr += bytes_read;
11283 break;
11284 case DW_LNS_set_file:
debd256d 11285 {
a738430d
MK
11286 /* The arrays lh->include_dirs and lh->file_names are
11287 0-based, but the directory and file name numbers in
11288 the statement program are 1-based. */
debd256d 11289 struct file_entry *fe;
4f1520fb 11290 char *dir = NULL;
a738430d 11291
debd256d
JB
11292 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11293 line_ptr += bytes_read;
59205f5a 11294 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11295 dwarf2_debug_line_missing_file_complaint ();
11296 else
11297 {
11298 fe = &lh->file_names[file - 1];
11299 if (fe->dir_index)
11300 dir = lh->include_dirs[fe->dir_index - 1];
11301 if (!decode_for_pst_p)
11302 {
11303 last_subfile = current_subfile;
11304 dwarf2_start_subfile (fe->name, dir, comp_dir);
11305 }
11306 }
debd256d 11307 }
c906108c
SS
11308 break;
11309 case DW_LNS_set_column:
11310 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11311 line_ptr += bytes_read;
11312 break;
11313 case DW_LNS_negate_stmt:
11314 is_stmt = (!is_stmt);
11315 break;
11316 case DW_LNS_set_basic_block:
11317 basic_block = 1;
11318 break;
c2c6d25f
JM
11319 /* Add to the address register of the state machine the
11320 address increment value corresponding to special opcode
a738430d
MK
11321 255. I.e., this value is scaled by the minimum
11322 instruction length since special opcode 255 would have
b021a221 11323 scaled the increment. */
c906108c 11324 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11325 {
11326 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11327
11328 address += (((op_index + adjust)
11329 / lh->maximum_ops_per_instruction)
11330 * lh->minimum_instruction_length);
11331 op_index = ((op_index + adjust)
11332 % lh->maximum_ops_per_instruction);
11333 }
c906108c
SS
11334 break;
11335 case DW_LNS_fixed_advance_pc:
11336 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11337 op_index = 0;
c906108c
SS
11338 line_ptr += 2;
11339 break;
9aa1fe7e 11340 default:
a738430d
MK
11341 {
11342 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11343 int i;
a738430d 11344
debd256d 11345 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11346 {
11347 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11348 line_ptr += bytes_read;
11349 }
11350 }
c906108c
SS
11351 }
11352 }
59205f5a
JB
11353 if (lh->num_file_names < file || file == 0)
11354 dwarf2_debug_line_missing_file_complaint ();
11355 else
11356 {
11357 lh->file_names[file - 1].included_p = 1;
11358 if (!decode_for_pst_p)
fbf65064
UW
11359 {
11360 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11361 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11362 }
59205f5a 11363 }
c906108c 11364 }
f3f5162e
DE
11365}
11366
11367/* Decode the Line Number Program (LNP) for the given line_header
11368 structure and CU. The actual information extracted and the type
11369 of structures created from the LNP depends on the value of PST.
11370
11371 1. If PST is NULL, then this procedure uses the data from the program
11372 to create all necessary symbol tables, and their linetables.
11373
11374 2. If PST is not NULL, this procedure reads the program to determine
11375 the list of files included by the unit represented by PST, and
11376 builds all the associated partial symbol tables.
11377
11378 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11379 It is used for relative paths in the line table.
11380 NOTE: When processing partial symtabs (pst != NULL),
11381 comp_dir == pst->dirname.
11382
11383 NOTE: It is important that psymtabs have the same file name (via strcmp)
11384 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11385 symtab we don't use it in the name of the psymtabs we create.
11386 E.g. expand_line_sal requires this when finding psymtabs to expand.
11387 A good testcase for this is mb-inline.exp. */
11388
11389static void
11390dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11391 struct dwarf2_cu *cu, struct partial_symtab *pst,
11392 int want_line_info)
11393{
11394 struct objfile *objfile = cu->objfile;
11395 const int decode_for_pst_p = (pst != NULL);
11396 struct subfile *first_subfile = current_subfile;
11397
11398 if (want_line_info)
11399 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
11400
11401 if (decode_for_pst_p)
11402 {
11403 int file_index;
11404
11405 /* Now that we're done scanning the Line Header Program, we can
11406 create the psymtab of each included file. */
11407 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11408 if (lh->file_names[file_index].included_p == 1)
11409 {
c6da4cef
DE
11410 char *include_name =
11411 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11412 if (include_name != NULL)
aaa75496
JB
11413 dwarf2_create_include_psymtab (include_name, pst, objfile);
11414 }
11415 }
cb1df416
DJ
11416 else
11417 {
11418 /* Make sure a symtab is created for every file, even files
11419 which contain only variables (i.e. no code with associated
11420 line numbers). */
cb1df416 11421 int i;
cb1df416
DJ
11422
11423 for (i = 0; i < lh->num_file_names; i++)
11424 {
11425 char *dir = NULL;
f3f5162e 11426 struct file_entry *fe;
9a619af0 11427
cb1df416
DJ
11428 fe = &lh->file_names[i];
11429 if (fe->dir_index)
11430 dir = lh->include_dirs[fe->dir_index - 1];
11431 dwarf2_start_subfile (fe->name, dir, comp_dir);
11432
11433 /* Skip the main file; we don't need it, and it must be
11434 allocated last, so that it will show up before the
11435 non-primary symtabs in the objfile's symtab list. */
11436 if (current_subfile == first_subfile)
11437 continue;
11438
11439 if (current_subfile->symtab == NULL)
11440 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 11441 objfile);
cb1df416
DJ
11442 fe->symtab = current_subfile->symtab;
11443 }
11444 }
c906108c
SS
11445}
11446
11447/* Start a subfile for DWARF. FILENAME is the name of the file and
11448 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11449 or NULL if not known. COMP_DIR is the compilation directory for the
11450 linetable's compilation unit or NULL if not known.
c906108c
SS
11451 This routine tries to keep line numbers from identical absolute and
11452 relative file names in a common subfile.
11453
11454 Using the `list' example from the GDB testsuite, which resides in
11455 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11456 of /srcdir/list0.c yields the following debugging information for list0.c:
11457
c5aa993b
JM
11458 DW_AT_name: /srcdir/list0.c
11459 DW_AT_comp_dir: /compdir
357e46e7 11460 files.files[0].name: list0.h
c5aa993b 11461 files.files[0].dir: /srcdir
357e46e7 11462 files.files[1].name: list0.c
c5aa993b 11463 files.files[1].dir: /srcdir
c906108c
SS
11464
11465 The line number information for list0.c has to end up in a single
4f1520fb
FR
11466 subfile, so that `break /srcdir/list0.c:1' works as expected.
11467 start_subfile will ensure that this happens provided that we pass the
11468 concatenation of files.files[1].dir and files.files[1].name as the
11469 subfile's name. */
c906108c
SS
11470
11471static void
3e43a32a
MS
11472dwarf2_start_subfile (char *filename, const char *dirname,
11473 const char *comp_dir)
c906108c 11474{
4f1520fb
FR
11475 char *fullname;
11476
11477 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11478 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11479 second argument to start_subfile. To be consistent, we do the
11480 same here. In order not to lose the line information directory,
11481 we concatenate it to the filename when it makes sense.
11482 Note that the Dwarf3 standard says (speaking of filenames in line
11483 information): ``The directory index is ignored for file names
11484 that represent full path names''. Thus ignoring dirname in the
11485 `else' branch below isn't an issue. */
c906108c 11486
d5166ae1 11487 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11488 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11489 else
11490 fullname = filename;
c906108c 11491
4f1520fb
FR
11492 start_subfile (fullname, comp_dir);
11493
11494 if (fullname != filename)
11495 xfree (fullname);
c906108c
SS
11496}
11497
4c2df51b
DJ
11498static void
11499var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11500 struct dwarf2_cu *cu)
4c2df51b 11501{
e7c27a73
DJ
11502 struct objfile *objfile = cu->objfile;
11503 struct comp_unit_head *cu_header = &cu->header;
11504
4c2df51b
DJ
11505 /* NOTE drow/2003-01-30: There used to be a comment and some special
11506 code here to turn a symbol with DW_AT_external and a
11507 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11508 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11509 with some versions of binutils) where shared libraries could have
11510 relocations against symbols in their debug information - the
11511 minimal symbol would have the right address, but the debug info
11512 would not. It's no longer necessary, because we will explicitly
11513 apply relocations when we read in the debug information now. */
11514
11515 /* A DW_AT_location attribute with no contents indicates that a
11516 variable has been optimized away. */
11517 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11518 {
11519 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11520 return;
11521 }
11522
11523 /* Handle one degenerate form of location expression specially, to
11524 preserve GDB's previous behavior when section offsets are
11525 specified. If this is just a DW_OP_addr then mark this symbol
11526 as LOC_STATIC. */
11527
11528 if (attr_form_is_block (attr)
11529 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11530 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11531 {
891d2f0b 11532 unsigned int dummy;
4c2df51b
DJ
11533
11534 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11535 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11536 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11537 fixup_symbol_section (sym, objfile);
11538 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11539 SYMBOL_SECTION (sym));
4c2df51b
DJ
11540 return;
11541 }
11542
11543 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11544 expression evaluator, and use LOC_COMPUTED only when necessary
11545 (i.e. when the value of a register or memory location is
11546 referenced, or a thread-local block, etc.). Then again, it might
11547 not be worthwhile. I'm assuming that it isn't unless performance
11548 or memory numbers show me otherwise. */
11549
e7c27a73 11550 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11551 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11552
11553 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11554 cu->has_loclist = 1;
4c2df51b
DJ
11555}
11556
c906108c
SS
11557/* Given a pointer to a DWARF information entry, figure out if we need
11558 to make a symbol table entry for it, and if so, create a new entry
11559 and return a pointer to it.
11560 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11561 used the passed type.
11562 If SPACE is not NULL, use it to hold the new symbol. If it is
11563 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11564
11565static struct symbol *
34eaf542
TT
11566new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11567 struct symbol *space)
c906108c 11568{
e7c27a73 11569 struct objfile *objfile = cu->objfile;
c906108c
SS
11570 struct symbol *sym = NULL;
11571 char *name;
11572 struct attribute *attr = NULL;
11573 struct attribute *attr2 = NULL;
e142c38c 11574 CORE_ADDR baseaddr;
e37fd15a
SW
11575 struct pending **list_to_add = NULL;
11576
edb3359d 11577 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11578
11579 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11580
94af9270 11581 name = dwarf2_name (die, cu);
c906108c
SS
11582 if (name)
11583 {
94af9270 11584 const char *linkagename;
34eaf542 11585 int suppress_add = 0;
94af9270 11586
34eaf542
TT
11587 if (space)
11588 sym = space;
11589 else
11590 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11591 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11592
11593 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11594 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11595 linkagename = dwarf2_physname (name, die, cu);
11596 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11597
f55ee35c
JK
11598 /* Fortran does not have mangling standard and the mangling does differ
11599 between gfortran, iFort etc. */
11600 if (cu->language == language_fortran
b250c185 11601 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11602 symbol_set_demangled_name (&(sym->ginfo),
11603 (char *) dwarf2_full_name (name, die, cu),
11604 NULL);
f55ee35c 11605
c906108c 11606 /* Default assumptions.
c5aa993b 11607 Use the passed type or decode it from the die. */
176620f1 11608 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11609 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11610 if (type != NULL)
11611 SYMBOL_TYPE (sym) = type;
11612 else
e7c27a73 11613 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11614 attr = dwarf2_attr (die,
11615 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11616 cu);
c906108c
SS
11617 if (attr)
11618 {
11619 SYMBOL_LINE (sym) = DW_UNSND (attr);
11620 }
cb1df416 11621
edb3359d
DJ
11622 attr = dwarf2_attr (die,
11623 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11624 cu);
cb1df416
DJ
11625 if (attr)
11626 {
11627 int file_index = DW_UNSND (attr);
9a619af0 11628
cb1df416
DJ
11629 if (cu->line_header == NULL
11630 || file_index > cu->line_header->num_file_names)
11631 complaint (&symfile_complaints,
11632 _("file index out of range"));
1c3d648d 11633 else if (file_index > 0)
cb1df416
DJ
11634 {
11635 struct file_entry *fe;
9a619af0 11636
cb1df416
DJ
11637 fe = &cu->line_header->file_names[file_index - 1];
11638 SYMBOL_SYMTAB (sym) = fe->symtab;
11639 }
11640 }
11641
c906108c
SS
11642 switch (die->tag)
11643 {
11644 case DW_TAG_label:
e142c38c 11645 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11646 if (attr)
11647 {
11648 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11649 }
0f5238ed
TT
11650 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11651 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11652 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11653 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11654 break;
11655 case DW_TAG_subprogram:
11656 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11657 finish_block. */
11658 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11659 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11660 if ((attr2 && (DW_UNSND (attr2) != 0))
11661 || cu->language == language_ada)
c906108c 11662 {
2cfa0c8d
JB
11663 /* Subprograms marked external are stored as a global symbol.
11664 Ada subprograms, whether marked external or not, are always
11665 stored as a global symbol, because we want to be able to
11666 access them globally. For instance, we want to be able
11667 to break on a nested subprogram without having to
11668 specify the context. */
e37fd15a 11669 list_to_add = &global_symbols;
c906108c
SS
11670 }
11671 else
11672 {
e37fd15a 11673 list_to_add = cu->list_in_scope;
c906108c
SS
11674 }
11675 break;
edb3359d
DJ
11676 case DW_TAG_inlined_subroutine:
11677 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11678 finish_block. */
11679 SYMBOL_CLASS (sym) = LOC_BLOCK;
11680 SYMBOL_INLINED (sym) = 1;
11681 /* Do not add the symbol to any lists. It will be found via
11682 BLOCK_FUNCTION from the blockvector. */
11683 break;
34eaf542
TT
11684 case DW_TAG_template_value_param:
11685 suppress_add = 1;
11686 /* Fall through. */
72929c62 11687 case DW_TAG_constant:
c906108c 11688 case DW_TAG_variable:
254e6b9e 11689 case DW_TAG_member:
0963b4bd
MS
11690 /* Compilation with minimal debug info may result in
11691 variables with missing type entries. Change the
11692 misleading `void' type to something sensible. */
c906108c 11693 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11694 SYMBOL_TYPE (sym)
46bf5051 11695 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11696
e142c38c 11697 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11698 /* In the case of DW_TAG_member, we should only be called for
11699 static const members. */
11700 if (die->tag == DW_TAG_member)
11701 {
3863f96c
DE
11702 /* dwarf2_add_field uses die_is_declaration,
11703 so we do the same. */
254e6b9e
DE
11704 gdb_assert (die_is_declaration (die, cu));
11705 gdb_assert (attr);
11706 }
c906108c
SS
11707 if (attr)
11708 {
e7c27a73 11709 dwarf2_const_value (attr, sym, cu);
e142c38c 11710 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11711 if (!suppress_add)
34eaf542
TT
11712 {
11713 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11714 list_to_add = &global_symbols;
34eaf542 11715 else
e37fd15a 11716 list_to_add = cu->list_in_scope;
34eaf542 11717 }
c906108c
SS
11718 break;
11719 }
e142c38c 11720 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11721 if (attr)
11722 {
e7c27a73 11723 var_decode_location (attr, sym, cu);
e142c38c 11724 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11725 if (SYMBOL_CLASS (sym) == LOC_STATIC
11726 && SYMBOL_VALUE_ADDRESS (sym) == 0
11727 && !dwarf2_per_objfile->has_section_at_zero)
11728 {
11729 /* When a static variable is eliminated by the linker,
11730 the corresponding debug information is not stripped
11731 out, but the variable address is set to null;
11732 do not add such variables into symbol table. */
11733 }
11734 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11735 {
f55ee35c
JK
11736 /* Workaround gfortran PR debug/40040 - it uses
11737 DW_AT_location for variables in -fPIC libraries which may
11738 get overriden by other libraries/executable and get
11739 a different address. Resolve it by the minimal symbol
11740 which may come from inferior's executable using copy
11741 relocation. Make this workaround only for gfortran as for
11742 other compilers GDB cannot guess the minimal symbol
11743 Fortran mangling kind. */
11744 if (cu->language == language_fortran && die->parent
11745 && die->parent->tag == DW_TAG_module
11746 && cu->producer
11747 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11748 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11749
1c809c68
TT
11750 /* A variable with DW_AT_external is never static,
11751 but it may be block-scoped. */
11752 list_to_add = (cu->list_in_scope == &file_symbols
11753 ? &global_symbols : cu->list_in_scope);
1c809c68 11754 }
c906108c 11755 else
e37fd15a 11756 list_to_add = cu->list_in_scope;
c906108c
SS
11757 }
11758 else
11759 {
11760 /* We do not know the address of this symbol.
c5aa993b
JM
11761 If it is an external symbol and we have type information
11762 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11763 The address of the variable will then be determined from
11764 the minimal symbol table whenever the variable is
11765 referenced. */
e142c38c 11766 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11767 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11768 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11769 {
0fe7935b
DJ
11770 /* A variable with DW_AT_external is never static, but it
11771 may be block-scoped. */
11772 list_to_add = (cu->list_in_scope == &file_symbols
11773 ? &global_symbols : cu->list_in_scope);
11774
c906108c 11775 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11776 }
442ddf59
JK
11777 else if (!die_is_declaration (die, cu))
11778 {
11779 /* Use the default LOC_OPTIMIZED_OUT class. */
11780 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11781 if (!suppress_add)
11782 list_to_add = cu->list_in_scope;
442ddf59 11783 }
c906108c
SS
11784 }
11785 break;
11786 case DW_TAG_formal_parameter:
edb3359d
DJ
11787 /* If we are inside a function, mark this as an argument. If
11788 not, we might be looking at an argument to an inlined function
11789 when we do not have enough information to show inlined frames;
11790 pretend it's a local variable in that case so that the user can
11791 still see it. */
11792 if (context_stack_depth > 0
11793 && context_stack[context_stack_depth - 1].name != NULL)
11794 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11795 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11796 if (attr)
11797 {
e7c27a73 11798 var_decode_location (attr, sym, cu);
c906108c 11799 }
e142c38c 11800 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11801 if (attr)
11802 {
e7c27a73 11803 dwarf2_const_value (attr, sym, cu);
c906108c 11804 }
f346a30d 11805
e37fd15a 11806 list_to_add = cu->list_in_scope;
c906108c
SS
11807 break;
11808 case DW_TAG_unspecified_parameters:
11809 /* From varargs functions; gdb doesn't seem to have any
11810 interest in this information, so just ignore it for now.
11811 (FIXME?) */
11812 break;
34eaf542
TT
11813 case DW_TAG_template_type_param:
11814 suppress_add = 1;
11815 /* Fall through. */
c906108c 11816 case DW_TAG_class_type:
680b30c7 11817 case DW_TAG_interface_type:
c906108c
SS
11818 case DW_TAG_structure_type:
11819 case DW_TAG_union_type:
72019c9c 11820 case DW_TAG_set_type:
c906108c
SS
11821 case DW_TAG_enumeration_type:
11822 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11823 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11824
63d06c5c 11825 {
987504bb 11826 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11827 really ever be static objects: otherwise, if you try
11828 to, say, break of a class's method and you're in a file
11829 which doesn't mention that class, it won't work unless
11830 the check for all static symbols in lookup_symbol_aux
11831 saves you. See the OtherFileClass tests in
11832 gdb.c++/namespace.exp. */
11833
e37fd15a 11834 if (!suppress_add)
34eaf542 11835 {
34eaf542
TT
11836 list_to_add = (cu->list_in_scope == &file_symbols
11837 && (cu->language == language_cplus
11838 || cu->language == language_java)
11839 ? &global_symbols : cu->list_in_scope);
63d06c5c 11840
64382290
TT
11841 /* The semantics of C++ state that "struct foo {
11842 ... }" also defines a typedef for "foo". A Java
11843 class declaration also defines a typedef for the
11844 class. */
11845 if (cu->language == language_cplus
11846 || cu->language == language_java
11847 || cu->language == language_ada)
11848 {
11849 /* The symbol's name is already allocated along
11850 with this objfile, so we don't need to
11851 duplicate it for the type. */
11852 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11853 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11854 }
63d06c5c
DC
11855 }
11856 }
c906108c
SS
11857 break;
11858 case DW_TAG_typedef:
63d06c5c
DC
11859 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11860 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11861 list_to_add = cu->list_in_scope;
63d06c5c 11862 break;
c906108c 11863 case DW_TAG_base_type:
a02abb62 11864 case DW_TAG_subrange_type:
c906108c 11865 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11866 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11867 list_to_add = cu->list_in_scope;
c906108c
SS
11868 break;
11869 case DW_TAG_enumerator:
e142c38c 11870 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11871 if (attr)
11872 {
e7c27a73 11873 dwarf2_const_value (attr, sym, cu);
c906108c 11874 }
63d06c5c
DC
11875 {
11876 /* NOTE: carlton/2003-11-10: See comment above in the
11877 DW_TAG_class_type, etc. block. */
11878
e142c38c 11879 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11880 && (cu->language == language_cplus
11881 || cu->language == language_java)
e142c38c 11882 ? &global_symbols : cu->list_in_scope);
63d06c5c 11883 }
c906108c 11884 break;
5c4e30ca
DC
11885 case DW_TAG_namespace:
11886 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11887 list_to_add = &global_symbols;
5c4e30ca 11888 break;
c906108c
SS
11889 default:
11890 /* Not a tag we recognize. Hopefully we aren't processing
11891 trash data, but since we must specifically ignore things
11892 we don't recognize, there is nothing else we should do at
0963b4bd 11893 this point. */
e2e0b3e5 11894 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11895 dwarf_tag_name (die->tag));
c906108c
SS
11896 break;
11897 }
df8a16a1 11898
e37fd15a
SW
11899 if (suppress_add)
11900 {
11901 sym->hash_next = objfile->template_symbols;
11902 objfile->template_symbols = sym;
11903 list_to_add = NULL;
11904 }
11905
11906 if (list_to_add != NULL)
11907 add_symbol_to_list (sym, list_to_add);
11908
df8a16a1
DJ
11909 /* For the benefit of old versions of GCC, check for anonymous
11910 namespaces based on the demangled name. */
11911 if (!processing_has_namespace_info
94af9270 11912 && cu->language == language_cplus)
a10964d1 11913 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
11914 }
11915 return (sym);
11916}
11917
34eaf542
TT
11918/* A wrapper for new_symbol_full that always allocates a new symbol. */
11919
11920static struct symbol *
11921new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11922{
11923 return new_symbol_full (die, type, cu, NULL);
11924}
11925
98bfdba5
PA
11926/* Given an attr with a DW_FORM_dataN value in host byte order,
11927 zero-extend it as appropriate for the symbol's type. The DWARF
11928 standard (v4) is not entirely clear about the meaning of using
11929 DW_FORM_dataN for a constant with a signed type, where the type is
11930 wider than the data. The conclusion of a discussion on the DWARF
11931 list was that this is unspecified. We choose to always zero-extend
11932 because that is the interpretation long in use by GCC. */
c906108c 11933
98bfdba5
PA
11934static gdb_byte *
11935dwarf2_const_value_data (struct attribute *attr, struct type *type,
11936 const char *name, struct obstack *obstack,
11937 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11938{
e7c27a73 11939 struct objfile *objfile = cu->objfile;
e17a4113
UW
11940 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11941 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11942 LONGEST l = DW_UNSND (attr);
11943
11944 if (bits < sizeof (*value) * 8)
11945 {
11946 l &= ((LONGEST) 1 << bits) - 1;
11947 *value = l;
11948 }
11949 else if (bits == sizeof (*value) * 8)
11950 *value = l;
11951 else
11952 {
11953 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11954 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11955 return bytes;
11956 }
11957
11958 return NULL;
11959}
11960
11961/* Read a constant value from an attribute. Either set *VALUE, or if
11962 the value does not fit in *VALUE, set *BYTES - either already
11963 allocated on the objfile obstack, or newly allocated on OBSTACK,
11964 or, set *BATON, if we translated the constant to a location
11965 expression. */
11966
11967static void
11968dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11969 const char *name, struct obstack *obstack,
11970 struct dwarf2_cu *cu,
11971 long *value, gdb_byte **bytes,
11972 struct dwarf2_locexpr_baton **baton)
11973{
11974 struct objfile *objfile = cu->objfile;
11975 struct comp_unit_head *cu_header = &cu->header;
c906108c 11976 struct dwarf_block *blk;
98bfdba5
PA
11977 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11978 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11979
11980 *value = 0;
11981 *bytes = NULL;
11982 *baton = NULL;
c906108c
SS
11983
11984 switch (attr->form)
11985 {
11986 case DW_FORM_addr:
ac56253d 11987 {
ac56253d
TT
11988 gdb_byte *data;
11989
98bfdba5
PA
11990 if (TYPE_LENGTH (type) != cu_header->addr_size)
11991 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11992 cu_header->addr_size,
98bfdba5 11993 TYPE_LENGTH (type));
ac56253d
TT
11994 /* Symbols of this form are reasonably rare, so we just
11995 piggyback on the existing location code rather than writing
11996 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11997 *baton = obstack_alloc (&objfile->objfile_obstack,
11998 sizeof (struct dwarf2_locexpr_baton));
11999 (*baton)->per_cu = cu->per_cu;
12000 gdb_assert ((*baton)->per_cu);
ac56253d 12001
98bfdba5
PA
12002 (*baton)->size = 2 + cu_header->addr_size;
12003 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12004 (*baton)->data = data;
ac56253d
TT
12005
12006 data[0] = DW_OP_addr;
12007 store_unsigned_integer (&data[1], cu_header->addr_size,
12008 byte_order, DW_ADDR (attr));
12009 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12010 }
c906108c 12011 break;
4ac36638 12012 case DW_FORM_string:
93b5768b 12013 case DW_FORM_strp:
98bfdba5
PA
12014 /* DW_STRING is already allocated on the objfile obstack, point
12015 directly to it. */
12016 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12017 break;
c906108c
SS
12018 case DW_FORM_block1:
12019 case DW_FORM_block2:
12020 case DW_FORM_block4:
12021 case DW_FORM_block:
2dc7f7b3 12022 case DW_FORM_exprloc:
c906108c 12023 blk = DW_BLOCK (attr);
98bfdba5
PA
12024 if (TYPE_LENGTH (type) != blk->size)
12025 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12026 TYPE_LENGTH (type));
12027 *bytes = blk->data;
c906108c 12028 break;
2df3850c
JM
12029
12030 /* The DW_AT_const_value attributes are supposed to carry the
12031 symbol's value "represented as it would be on the target
12032 architecture." By the time we get here, it's already been
12033 converted to host endianness, so we just need to sign- or
12034 zero-extend it as appropriate. */
12035 case DW_FORM_data1:
3e43a32a
MS
12036 *bytes = dwarf2_const_value_data (attr, type, name,
12037 obstack, cu, value, 8);
2df3850c 12038 break;
c906108c 12039 case DW_FORM_data2:
3e43a32a
MS
12040 *bytes = dwarf2_const_value_data (attr, type, name,
12041 obstack, cu, value, 16);
2df3850c 12042 break;
c906108c 12043 case DW_FORM_data4:
3e43a32a
MS
12044 *bytes = dwarf2_const_value_data (attr, type, name,
12045 obstack, cu, value, 32);
2df3850c 12046 break;
c906108c 12047 case DW_FORM_data8:
3e43a32a
MS
12048 *bytes = dwarf2_const_value_data (attr, type, name,
12049 obstack, cu, value, 64);
2df3850c
JM
12050 break;
12051
c906108c 12052 case DW_FORM_sdata:
98bfdba5 12053 *value = DW_SND (attr);
2df3850c
JM
12054 break;
12055
c906108c 12056 case DW_FORM_udata:
98bfdba5 12057 *value = DW_UNSND (attr);
c906108c 12058 break;
2df3850c 12059
c906108c 12060 default:
4d3c2250 12061 complaint (&symfile_complaints,
e2e0b3e5 12062 _("unsupported const value attribute form: '%s'"),
4d3c2250 12063 dwarf_form_name (attr->form));
98bfdba5 12064 *value = 0;
c906108c
SS
12065 break;
12066 }
12067}
12068
2df3850c 12069
98bfdba5
PA
12070/* Copy constant value from an attribute to a symbol. */
12071
2df3850c 12072static void
98bfdba5
PA
12073dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12074 struct dwarf2_cu *cu)
2df3850c 12075{
98bfdba5
PA
12076 struct objfile *objfile = cu->objfile;
12077 struct comp_unit_head *cu_header = &cu->header;
12078 long value;
12079 gdb_byte *bytes;
12080 struct dwarf2_locexpr_baton *baton;
2df3850c 12081
98bfdba5
PA
12082 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12083 SYMBOL_PRINT_NAME (sym),
12084 &objfile->objfile_obstack, cu,
12085 &value, &bytes, &baton);
2df3850c 12086
98bfdba5
PA
12087 if (baton != NULL)
12088 {
12089 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12090 SYMBOL_LOCATION_BATON (sym) = baton;
12091 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12092 }
12093 else if (bytes != NULL)
12094 {
12095 SYMBOL_VALUE_BYTES (sym) = bytes;
12096 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12097 }
12098 else
12099 {
12100 SYMBOL_VALUE (sym) = value;
12101 SYMBOL_CLASS (sym) = LOC_CONST;
12102 }
2df3850c
JM
12103}
12104
c906108c
SS
12105/* Return the type of the die in question using its DW_AT_type attribute. */
12106
12107static struct type *
e7c27a73 12108die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12109{
c906108c 12110 struct attribute *type_attr;
c906108c 12111
e142c38c 12112 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12113 if (!type_attr)
12114 {
12115 /* A missing DW_AT_type represents a void type. */
46bf5051 12116 return objfile_type (cu->objfile)->builtin_void;
c906108c 12117 }
348e048f 12118
673bfd45 12119 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12120}
12121
b4ba55a1
JB
12122/* True iff CU's producer generates GNAT Ada auxiliary information
12123 that allows to find parallel types through that information instead
12124 of having to do expensive parallel lookups by type name. */
12125
12126static int
12127need_gnat_info (struct dwarf2_cu *cu)
12128{
12129 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12130 of GNAT produces this auxiliary information, without any indication
12131 that it is produced. Part of enhancing the FSF version of GNAT
12132 to produce that information will be to put in place an indicator
12133 that we can use in order to determine whether the descriptive type
12134 info is available or not. One suggestion that has been made is
12135 to use a new attribute, attached to the CU die. For now, assume
12136 that the descriptive type info is not available. */
12137 return 0;
12138}
12139
b4ba55a1
JB
12140/* Return the auxiliary type of the die in question using its
12141 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12142 attribute is not present. */
12143
12144static struct type *
12145die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12146{
b4ba55a1 12147 struct attribute *type_attr;
b4ba55a1
JB
12148
12149 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12150 if (!type_attr)
12151 return NULL;
12152
673bfd45 12153 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12154}
12155
12156/* If DIE has a descriptive_type attribute, then set the TYPE's
12157 descriptive type accordingly. */
12158
12159static void
12160set_descriptive_type (struct type *type, struct die_info *die,
12161 struct dwarf2_cu *cu)
12162{
12163 struct type *descriptive_type = die_descriptive_type (die, cu);
12164
12165 if (descriptive_type)
12166 {
12167 ALLOCATE_GNAT_AUX_TYPE (type);
12168 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12169 }
12170}
12171
c906108c
SS
12172/* Return the containing type of the die in question using its
12173 DW_AT_containing_type attribute. */
12174
12175static struct type *
e7c27a73 12176die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12177{
c906108c 12178 struct attribute *type_attr;
c906108c 12179
e142c38c 12180 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12181 if (!type_attr)
12182 error (_("Dwarf Error: Problem turning containing type into gdb type "
12183 "[in module %s]"), cu->objfile->name);
12184
673bfd45 12185 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12186}
12187
673bfd45
DE
12188/* Look up the type of DIE in CU using its type attribute ATTR.
12189 If there is no type substitute an error marker. */
12190
c906108c 12191static struct type *
673bfd45
DE
12192lookup_die_type (struct die_info *die, struct attribute *attr,
12193 struct dwarf2_cu *cu)
c906108c 12194{
bb5ed363 12195 struct objfile *objfile = cu->objfile;
f792889a
DJ
12196 struct type *this_type;
12197
673bfd45
DE
12198 /* First see if we have it cached. */
12199
12200 if (is_ref_attr (attr))
12201 {
12202 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12203
12204 this_type = get_die_type_at_offset (offset, cu->per_cu);
12205 }
55f1336d 12206 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12207 {
12208 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12209 struct dwarf2_cu *sig_cu;
12210 unsigned int offset;
12211
12212 /* sig_type will be NULL if the signatured type is missing from
12213 the debug info. */
12214 if (sig_type == NULL)
12215 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12216 "at 0x%x [in module %s]"),
bb5ed363 12217 die->offset, objfile->name);
673bfd45 12218
b0df02fd 12219 gdb_assert (sig_type->per_cu.debug_types_section);
b3c8eb43 12220 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
12221 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12222 }
12223 else
12224 {
12225 dump_die_for_error (die);
12226 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 12227 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
12228 }
12229
12230 /* If not cached we need to read it in. */
12231
12232 if (this_type == NULL)
12233 {
12234 struct die_info *type_die;
12235 struct dwarf2_cu *type_cu = cu;
12236
12237 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12238 /* If the type is cached, we should have found it above. */
12239 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12240 this_type = read_type_die_1 (type_die, type_cu);
12241 }
12242
12243 /* If we still don't have a type use an error marker. */
12244
12245 if (this_type == NULL)
c906108c 12246 {
b00fdb78
TT
12247 char *message, *saved;
12248
12249 /* read_type_die already issued a complaint. */
12250 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 12251 objfile->name,
b00fdb78
TT
12252 cu->header.offset,
12253 die->offset);
bb5ed363 12254 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
12255 message, strlen (message));
12256 xfree (message);
12257
bb5ed363 12258 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 12259 }
673bfd45 12260
f792889a 12261 return this_type;
c906108c
SS
12262}
12263
673bfd45
DE
12264/* Return the type in DIE, CU.
12265 Returns NULL for invalid types.
12266
12267 This first does a lookup in the appropriate type_hash table,
12268 and only reads the die in if necessary.
12269
12270 NOTE: This can be called when reading in partial or full symbols. */
12271
f792889a 12272static struct type *
e7c27a73 12273read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12274{
f792889a
DJ
12275 struct type *this_type;
12276
12277 this_type = get_die_type (die, cu);
12278 if (this_type)
12279 return this_type;
12280
673bfd45
DE
12281 return read_type_die_1 (die, cu);
12282}
12283
12284/* Read the type in DIE, CU.
12285 Returns NULL for invalid types. */
12286
12287static struct type *
12288read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12289{
12290 struct type *this_type = NULL;
12291
c906108c
SS
12292 switch (die->tag)
12293 {
12294 case DW_TAG_class_type:
680b30c7 12295 case DW_TAG_interface_type:
c906108c
SS
12296 case DW_TAG_structure_type:
12297 case DW_TAG_union_type:
f792889a 12298 this_type = read_structure_type (die, cu);
c906108c
SS
12299 break;
12300 case DW_TAG_enumeration_type:
f792889a 12301 this_type = read_enumeration_type (die, cu);
c906108c
SS
12302 break;
12303 case DW_TAG_subprogram:
12304 case DW_TAG_subroutine_type:
edb3359d 12305 case DW_TAG_inlined_subroutine:
f792889a 12306 this_type = read_subroutine_type (die, cu);
c906108c
SS
12307 break;
12308 case DW_TAG_array_type:
f792889a 12309 this_type = read_array_type (die, cu);
c906108c 12310 break;
72019c9c 12311 case DW_TAG_set_type:
f792889a 12312 this_type = read_set_type (die, cu);
72019c9c 12313 break;
c906108c 12314 case DW_TAG_pointer_type:
f792889a 12315 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12316 break;
12317 case DW_TAG_ptr_to_member_type:
f792889a 12318 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12319 break;
12320 case DW_TAG_reference_type:
f792889a 12321 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12322 break;
12323 case DW_TAG_const_type:
f792889a 12324 this_type = read_tag_const_type (die, cu);
c906108c
SS
12325 break;
12326 case DW_TAG_volatile_type:
f792889a 12327 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12328 break;
12329 case DW_TAG_string_type:
f792889a 12330 this_type = read_tag_string_type (die, cu);
c906108c
SS
12331 break;
12332 case DW_TAG_typedef:
f792889a 12333 this_type = read_typedef (die, cu);
c906108c 12334 break;
a02abb62 12335 case DW_TAG_subrange_type:
f792889a 12336 this_type = read_subrange_type (die, cu);
a02abb62 12337 break;
c906108c 12338 case DW_TAG_base_type:
f792889a 12339 this_type = read_base_type (die, cu);
c906108c 12340 break;
81a17f79 12341 case DW_TAG_unspecified_type:
f792889a 12342 this_type = read_unspecified_type (die, cu);
81a17f79 12343 break;
0114d602
DJ
12344 case DW_TAG_namespace:
12345 this_type = read_namespace_type (die, cu);
12346 break;
f55ee35c
JK
12347 case DW_TAG_module:
12348 this_type = read_module_type (die, cu);
12349 break;
c906108c 12350 default:
3e43a32a
MS
12351 complaint (&symfile_complaints,
12352 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12353 dwarf_tag_name (die->tag));
c906108c
SS
12354 break;
12355 }
63d06c5c 12356
f792889a 12357 return this_type;
63d06c5c
DC
12358}
12359
abc72ce4
DE
12360/* See if we can figure out if the class lives in a namespace. We do
12361 this by looking for a member function; its demangled name will
12362 contain namespace info, if there is any.
12363 Return the computed name or NULL.
12364 Space for the result is allocated on the objfile's obstack.
12365 This is the full-die version of guess_partial_die_structure_name.
12366 In this case we know DIE has no useful parent. */
12367
12368static char *
12369guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12370{
12371 struct die_info *spec_die;
12372 struct dwarf2_cu *spec_cu;
12373 struct die_info *child;
12374
12375 spec_cu = cu;
12376 spec_die = die_specification (die, &spec_cu);
12377 if (spec_die != NULL)
12378 {
12379 die = spec_die;
12380 cu = spec_cu;
12381 }
12382
12383 for (child = die->child;
12384 child != NULL;
12385 child = child->sibling)
12386 {
12387 if (child->tag == DW_TAG_subprogram)
12388 {
12389 struct attribute *attr;
12390
12391 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12392 if (attr == NULL)
12393 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12394 if (attr != NULL)
12395 {
12396 char *actual_name
12397 = language_class_name_from_physname (cu->language_defn,
12398 DW_STRING (attr));
12399 char *name = NULL;
12400
12401 if (actual_name != NULL)
12402 {
12403 char *die_name = dwarf2_name (die, cu);
12404
12405 if (die_name != NULL
12406 && strcmp (die_name, actual_name) != 0)
12407 {
12408 /* Strip off the class name from the full name.
12409 We want the prefix. */
12410 int die_name_len = strlen (die_name);
12411 int actual_name_len = strlen (actual_name);
12412
12413 /* Test for '::' as a sanity check. */
12414 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12415 && actual_name[actual_name_len
12416 - die_name_len - 1] == ':')
abc72ce4
DE
12417 name =
12418 obsavestring (actual_name,
12419 actual_name_len - die_name_len - 2,
12420 &cu->objfile->objfile_obstack);
12421 }
12422 }
12423 xfree (actual_name);
12424 return name;
12425 }
12426 }
12427 }
12428
12429 return NULL;
12430}
12431
96408a79
SA
12432/* GCC might emit a nameless typedef that has a linkage name. Determine the
12433 prefix part in such case. See
12434 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12435
12436static char *
12437anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12438{
12439 struct attribute *attr;
12440 char *base;
12441
12442 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12443 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12444 return NULL;
12445
12446 attr = dwarf2_attr (die, DW_AT_name, cu);
12447 if (attr != NULL && DW_STRING (attr) != NULL)
12448 return NULL;
12449
12450 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12451 if (attr == NULL)
12452 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12453 if (attr == NULL || DW_STRING (attr) == NULL)
12454 return NULL;
12455
12456 /* dwarf2_name had to be already called. */
12457 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12458
12459 /* Strip the base name, keep any leading namespaces/classes. */
12460 base = strrchr (DW_STRING (attr), ':');
12461 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12462 return "";
12463
12464 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12465 &cu->objfile->objfile_obstack);
12466}
12467
fdde2d81 12468/* Return the name of the namespace/class that DIE is defined within,
0114d602 12469 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12470
0114d602
DJ
12471 For example, if we're within the method foo() in the following
12472 code:
12473
12474 namespace N {
12475 class C {
12476 void foo () {
12477 }
12478 };
12479 }
12480
12481 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12482
12483static char *
e142c38c 12484determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12485{
0114d602
DJ
12486 struct die_info *parent, *spec_die;
12487 struct dwarf2_cu *spec_cu;
12488 struct type *parent_type;
96408a79 12489 char *retval;
63d06c5c 12490
f55ee35c
JK
12491 if (cu->language != language_cplus && cu->language != language_java
12492 && cu->language != language_fortran)
0114d602
DJ
12493 return "";
12494
96408a79
SA
12495 retval = anonymous_struct_prefix (die, cu);
12496 if (retval)
12497 return retval;
12498
0114d602
DJ
12499 /* We have to be careful in the presence of DW_AT_specification.
12500 For example, with GCC 3.4, given the code
12501
12502 namespace N {
12503 void foo() {
12504 // Definition of N::foo.
12505 }
12506 }
12507
12508 then we'll have a tree of DIEs like this:
12509
12510 1: DW_TAG_compile_unit
12511 2: DW_TAG_namespace // N
12512 3: DW_TAG_subprogram // declaration of N::foo
12513 4: DW_TAG_subprogram // definition of N::foo
12514 DW_AT_specification // refers to die #3
12515
12516 Thus, when processing die #4, we have to pretend that we're in
12517 the context of its DW_AT_specification, namely the contex of die
12518 #3. */
12519 spec_cu = cu;
12520 spec_die = die_specification (die, &spec_cu);
12521 if (spec_die == NULL)
12522 parent = die->parent;
12523 else
63d06c5c 12524 {
0114d602
DJ
12525 parent = spec_die->parent;
12526 cu = spec_cu;
63d06c5c 12527 }
0114d602
DJ
12528
12529 if (parent == NULL)
12530 return "";
98bfdba5
PA
12531 else if (parent->building_fullname)
12532 {
12533 const char *name;
12534 const char *parent_name;
12535
12536 /* It has been seen on RealView 2.2 built binaries,
12537 DW_TAG_template_type_param types actually _defined_ as
12538 children of the parent class:
12539
12540 enum E {};
12541 template class <class Enum> Class{};
12542 Class<enum E> class_e;
12543
12544 1: DW_TAG_class_type (Class)
12545 2: DW_TAG_enumeration_type (E)
12546 3: DW_TAG_enumerator (enum1:0)
12547 3: DW_TAG_enumerator (enum2:1)
12548 ...
12549 2: DW_TAG_template_type_param
12550 DW_AT_type DW_FORM_ref_udata (E)
12551
12552 Besides being broken debug info, it can put GDB into an
12553 infinite loop. Consider:
12554
12555 When we're building the full name for Class<E>, we'll start
12556 at Class, and go look over its template type parameters,
12557 finding E. We'll then try to build the full name of E, and
12558 reach here. We're now trying to build the full name of E,
12559 and look over the parent DIE for containing scope. In the
12560 broken case, if we followed the parent DIE of E, we'd again
12561 find Class, and once again go look at its template type
12562 arguments, etc., etc. Simply don't consider such parent die
12563 as source-level parent of this die (it can't be, the language
12564 doesn't allow it), and break the loop here. */
12565 name = dwarf2_name (die, cu);
12566 parent_name = dwarf2_name (parent, cu);
12567 complaint (&symfile_complaints,
12568 _("template param type '%s' defined within parent '%s'"),
12569 name ? name : "<unknown>",
12570 parent_name ? parent_name : "<unknown>");
12571 return "";
12572 }
63d06c5c 12573 else
0114d602
DJ
12574 switch (parent->tag)
12575 {
63d06c5c 12576 case DW_TAG_namespace:
0114d602 12577 parent_type = read_type_die (parent, cu);
acebe513
UW
12578 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12579 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12580 Work around this problem here. */
12581 if (cu->language == language_cplus
12582 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12583 return "";
0114d602
DJ
12584 /* We give a name to even anonymous namespaces. */
12585 return TYPE_TAG_NAME (parent_type);
63d06c5c 12586 case DW_TAG_class_type:
680b30c7 12587 case DW_TAG_interface_type:
63d06c5c 12588 case DW_TAG_structure_type:
0114d602 12589 case DW_TAG_union_type:
f55ee35c 12590 case DW_TAG_module:
0114d602
DJ
12591 parent_type = read_type_die (parent, cu);
12592 if (TYPE_TAG_NAME (parent_type) != NULL)
12593 return TYPE_TAG_NAME (parent_type);
12594 else
12595 /* An anonymous structure is only allowed non-static data
12596 members; no typedefs, no member functions, et cetera.
12597 So it does not need a prefix. */
12598 return "";
abc72ce4
DE
12599 case DW_TAG_compile_unit:
12600 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12601 if (cu->language == language_cplus
8b70b953 12602 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12603 && die->child != NULL
12604 && (die->tag == DW_TAG_class_type
12605 || die->tag == DW_TAG_structure_type
12606 || die->tag == DW_TAG_union_type))
12607 {
12608 char *name = guess_full_die_structure_name (die, cu);
12609 if (name != NULL)
12610 return name;
12611 }
12612 return "";
63d06c5c 12613 default:
8176b9b8 12614 return determine_prefix (parent, cu);
63d06c5c 12615 }
63d06c5c
DC
12616}
12617
3e43a32a
MS
12618/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12619 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12620 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12621 an obconcat, otherwise allocate storage for the result. The CU argument is
12622 used to determine the language and hence, the appropriate separator. */
987504bb 12623
f55ee35c 12624#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12625
12626static char *
f55ee35c
JK
12627typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12628 int physname, struct dwarf2_cu *cu)
63d06c5c 12629{
f55ee35c 12630 const char *lead = "";
5c315b68 12631 const char *sep;
63d06c5c 12632
3e43a32a
MS
12633 if (suffix == NULL || suffix[0] == '\0'
12634 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12635 sep = "";
12636 else if (cu->language == language_java)
12637 sep = ".";
f55ee35c
JK
12638 else if (cu->language == language_fortran && physname)
12639 {
12640 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12641 DW_AT_MIPS_linkage_name is preferred and used instead. */
12642
12643 lead = "__";
12644 sep = "_MOD_";
12645 }
987504bb
JJ
12646 else
12647 sep = "::";
63d06c5c 12648
6dd47d34
DE
12649 if (prefix == NULL)
12650 prefix = "";
12651 if (suffix == NULL)
12652 suffix = "";
12653
987504bb
JJ
12654 if (obs == NULL)
12655 {
3e43a32a
MS
12656 char *retval
12657 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12658
f55ee35c
JK
12659 strcpy (retval, lead);
12660 strcat (retval, prefix);
6dd47d34
DE
12661 strcat (retval, sep);
12662 strcat (retval, suffix);
63d06c5c
DC
12663 return retval;
12664 }
987504bb
JJ
12665 else
12666 {
12667 /* We have an obstack. */
f55ee35c 12668 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12669 }
63d06c5c
DC
12670}
12671
c906108c
SS
12672/* Return sibling of die, NULL if no sibling. */
12673
f9aca02d 12674static struct die_info *
fba45db2 12675sibling_die (struct die_info *die)
c906108c 12676{
639d11d3 12677 return die->sibling;
c906108c
SS
12678}
12679
71c25dea
TT
12680/* Get name of a die, return NULL if not found. */
12681
12682static char *
12683dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12684 struct obstack *obstack)
12685{
12686 if (name && cu->language == language_cplus)
12687 {
12688 char *canon_name = cp_canonicalize_string (name);
12689
12690 if (canon_name != NULL)
12691 {
12692 if (strcmp (canon_name, name) != 0)
12693 name = obsavestring (canon_name, strlen (canon_name),
12694 obstack);
12695 xfree (canon_name);
12696 }
12697 }
12698
12699 return name;
c906108c
SS
12700}
12701
9219021c
DC
12702/* Get name of a die, return NULL if not found. */
12703
12704static char *
e142c38c 12705dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12706{
12707 struct attribute *attr;
12708
e142c38c 12709 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12710 if ((!attr || !DW_STRING (attr))
12711 && die->tag != DW_TAG_class_type
12712 && die->tag != DW_TAG_interface_type
12713 && die->tag != DW_TAG_structure_type
12714 && die->tag != DW_TAG_union_type)
71c25dea
TT
12715 return NULL;
12716
12717 switch (die->tag)
12718 {
12719 case DW_TAG_compile_unit:
12720 /* Compilation units have a DW_AT_name that is a filename, not
12721 a source language identifier. */
12722 case DW_TAG_enumeration_type:
12723 case DW_TAG_enumerator:
12724 /* These tags always have simple identifiers already; no need
12725 to canonicalize them. */
12726 return DW_STRING (attr);
907af001 12727
418835cc
KS
12728 case DW_TAG_subprogram:
12729 /* Java constructors will all be named "<init>", so return
12730 the class name when we see this special case. */
12731 if (cu->language == language_java
12732 && DW_STRING (attr) != NULL
12733 && strcmp (DW_STRING (attr), "<init>") == 0)
12734 {
12735 struct dwarf2_cu *spec_cu = cu;
12736 struct die_info *spec_die;
12737
12738 /* GCJ will output '<init>' for Java constructor names.
12739 For this special case, return the name of the parent class. */
12740
12741 /* GCJ may output suprogram DIEs with AT_specification set.
12742 If so, use the name of the specified DIE. */
12743 spec_die = die_specification (die, &spec_cu);
12744 if (spec_die != NULL)
12745 return dwarf2_name (spec_die, spec_cu);
12746
12747 do
12748 {
12749 die = die->parent;
12750 if (die->tag == DW_TAG_class_type)
12751 return dwarf2_name (die, cu);
12752 }
12753 while (die->tag != DW_TAG_compile_unit);
12754 }
907af001
UW
12755 break;
12756
12757 case DW_TAG_class_type:
12758 case DW_TAG_interface_type:
12759 case DW_TAG_structure_type:
12760 case DW_TAG_union_type:
12761 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12762 structures or unions. These were of the form "._%d" in GCC 4.1,
12763 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12764 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12765 if (attr && DW_STRING (attr)
12766 && (strncmp (DW_STRING (attr), "._", 2) == 0
12767 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12768 return NULL;
53832f31
TT
12769
12770 /* GCC might emit a nameless typedef that has a linkage name. See
12771 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12772 if (!attr || DW_STRING (attr) == NULL)
12773 {
df5c6c50 12774 char *demangled = NULL;
53832f31
TT
12775
12776 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12777 if (attr == NULL)
12778 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12779
12780 if (attr == NULL || DW_STRING (attr) == NULL)
12781 return NULL;
12782
df5c6c50
JK
12783 /* Avoid demangling DW_STRING (attr) the second time on a second
12784 call for the same DIE. */
12785 if (!DW_STRING_IS_CANONICAL (attr))
12786 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12787
12788 if (demangled)
12789 {
96408a79
SA
12790 char *base;
12791
53832f31 12792 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12793 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12794 &cu->objfile->objfile_obstack);
53832f31
TT
12795 DW_STRING_IS_CANONICAL (attr) = 1;
12796 xfree (demangled);
96408a79
SA
12797
12798 /* Strip any leading namespaces/classes, keep only the base name.
12799 DW_AT_name for named DIEs does not contain the prefixes. */
12800 base = strrchr (DW_STRING (attr), ':');
12801 if (base && base > DW_STRING (attr) && base[-1] == ':')
12802 return &base[1];
12803 else
12804 return DW_STRING (attr);
53832f31
TT
12805 }
12806 }
907af001
UW
12807 break;
12808
71c25dea 12809 default:
907af001
UW
12810 break;
12811 }
12812
12813 if (!DW_STRING_IS_CANONICAL (attr))
12814 {
12815 DW_STRING (attr)
12816 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12817 &cu->objfile->objfile_obstack);
12818 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12819 }
907af001 12820 return DW_STRING (attr);
9219021c
DC
12821}
12822
12823/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12824 is none. *EXT_CU is the CU containing DIE on input, and the CU
12825 containing the return value on output. */
9219021c
DC
12826
12827static struct die_info *
f2f0e013 12828dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12829{
12830 struct attribute *attr;
9219021c 12831
f2f0e013 12832 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12833 if (attr == NULL)
12834 return NULL;
12835
f2f0e013 12836 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12837}
12838
c906108c
SS
12839/* Convert a DIE tag into its string name. */
12840
12841static char *
aa1ee363 12842dwarf_tag_name (unsigned tag)
c906108c
SS
12843{
12844 switch (tag)
12845 {
12846 case DW_TAG_padding:
12847 return "DW_TAG_padding";
12848 case DW_TAG_array_type:
12849 return "DW_TAG_array_type";
12850 case DW_TAG_class_type:
12851 return "DW_TAG_class_type";
12852 case DW_TAG_entry_point:
12853 return "DW_TAG_entry_point";
12854 case DW_TAG_enumeration_type:
12855 return "DW_TAG_enumeration_type";
12856 case DW_TAG_formal_parameter:
12857 return "DW_TAG_formal_parameter";
12858 case DW_TAG_imported_declaration:
12859 return "DW_TAG_imported_declaration";
12860 case DW_TAG_label:
12861 return "DW_TAG_label";
12862 case DW_TAG_lexical_block:
12863 return "DW_TAG_lexical_block";
12864 case DW_TAG_member:
12865 return "DW_TAG_member";
12866 case DW_TAG_pointer_type:
12867 return "DW_TAG_pointer_type";
12868 case DW_TAG_reference_type:
12869 return "DW_TAG_reference_type";
12870 case DW_TAG_compile_unit:
12871 return "DW_TAG_compile_unit";
12872 case DW_TAG_string_type:
12873 return "DW_TAG_string_type";
12874 case DW_TAG_structure_type:
12875 return "DW_TAG_structure_type";
12876 case DW_TAG_subroutine_type:
12877 return "DW_TAG_subroutine_type";
12878 case DW_TAG_typedef:
12879 return "DW_TAG_typedef";
12880 case DW_TAG_union_type:
12881 return "DW_TAG_union_type";
12882 case DW_TAG_unspecified_parameters:
12883 return "DW_TAG_unspecified_parameters";
12884 case DW_TAG_variant:
12885 return "DW_TAG_variant";
12886 case DW_TAG_common_block:
12887 return "DW_TAG_common_block";
12888 case DW_TAG_common_inclusion:
12889 return "DW_TAG_common_inclusion";
12890 case DW_TAG_inheritance:
12891 return "DW_TAG_inheritance";
12892 case DW_TAG_inlined_subroutine:
12893 return "DW_TAG_inlined_subroutine";
12894 case DW_TAG_module:
12895 return "DW_TAG_module";
12896 case DW_TAG_ptr_to_member_type:
12897 return "DW_TAG_ptr_to_member_type";
12898 case DW_TAG_set_type:
12899 return "DW_TAG_set_type";
12900 case DW_TAG_subrange_type:
12901 return "DW_TAG_subrange_type";
12902 case DW_TAG_with_stmt:
12903 return "DW_TAG_with_stmt";
12904 case DW_TAG_access_declaration:
12905 return "DW_TAG_access_declaration";
12906 case DW_TAG_base_type:
12907 return "DW_TAG_base_type";
12908 case DW_TAG_catch_block:
12909 return "DW_TAG_catch_block";
12910 case DW_TAG_const_type:
12911 return "DW_TAG_const_type";
12912 case DW_TAG_constant:
12913 return "DW_TAG_constant";
12914 case DW_TAG_enumerator:
12915 return "DW_TAG_enumerator";
12916 case DW_TAG_file_type:
12917 return "DW_TAG_file_type";
12918 case DW_TAG_friend:
12919 return "DW_TAG_friend";
12920 case DW_TAG_namelist:
12921 return "DW_TAG_namelist";
12922 case DW_TAG_namelist_item:
12923 return "DW_TAG_namelist_item";
12924 case DW_TAG_packed_type:
12925 return "DW_TAG_packed_type";
12926 case DW_TAG_subprogram:
12927 return "DW_TAG_subprogram";
12928 case DW_TAG_template_type_param:
12929 return "DW_TAG_template_type_param";
12930 case DW_TAG_template_value_param:
12931 return "DW_TAG_template_value_param";
12932 case DW_TAG_thrown_type:
12933 return "DW_TAG_thrown_type";
12934 case DW_TAG_try_block:
12935 return "DW_TAG_try_block";
12936 case DW_TAG_variant_part:
12937 return "DW_TAG_variant_part";
12938 case DW_TAG_variable:
12939 return "DW_TAG_variable";
12940 case DW_TAG_volatile_type:
12941 return "DW_TAG_volatile_type";
d9fa45fe
DC
12942 case DW_TAG_dwarf_procedure:
12943 return "DW_TAG_dwarf_procedure";
12944 case DW_TAG_restrict_type:
12945 return "DW_TAG_restrict_type";
12946 case DW_TAG_interface_type:
12947 return "DW_TAG_interface_type";
12948 case DW_TAG_namespace:
12949 return "DW_TAG_namespace";
12950 case DW_TAG_imported_module:
12951 return "DW_TAG_imported_module";
12952 case DW_TAG_unspecified_type:
12953 return "DW_TAG_unspecified_type";
12954 case DW_TAG_partial_unit:
12955 return "DW_TAG_partial_unit";
12956 case DW_TAG_imported_unit:
12957 return "DW_TAG_imported_unit";
b7619582
GF
12958 case DW_TAG_condition:
12959 return "DW_TAG_condition";
12960 case DW_TAG_shared_type:
12961 return "DW_TAG_shared_type";
348e048f
DE
12962 case DW_TAG_type_unit:
12963 return "DW_TAG_type_unit";
c906108c
SS
12964 case DW_TAG_MIPS_loop:
12965 return "DW_TAG_MIPS_loop";
b7619582
GF
12966 case DW_TAG_HP_array_descriptor:
12967 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12968 case DW_TAG_format_label:
12969 return "DW_TAG_format_label";
12970 case DW_TAG_function_template:
12971 return "DW_TAG_function_template";
12972 case DW_TAG_class_template:
12973 return "DW_TAG_class_template";
b7619582
GF
12974 case DW_TAG_GNU_BINCL:
12975 return "DW_TAG_GNU_BINCL";
12976 case DW_TAG_GNU_EINCL:
12977 return "DW_TAG_GNU_EINCL";
12978 case DW_TAG_upc_shared_type:
12979 return "DW_TAG_upc_shared_type";
12980 case DW_TAG_upc_strict_type:
12981 return "DW_TAG_upc_strict_type";
12982 case DW_TAG_upc_relaxed_type:
12983 return "DW_TAG_upc_relaxed_type";
12984 case DW_TAG_PGI_kanji_type:
12985 return "DW_TAG_PGI_kanji_type";
12986 case DW_TAG_PGI_interface_block:
12987 return "DW_TAG_PGI_interface_block";
96408a79
SA
12988 case DW_TAG_GNU_call_site:
12989 return "DW_TAG_GNU_call_site";
c906108c
SS
12990 default:
12991 return "DW_TAG_<unknown>";
12992 }
12993}
12994
12995/* Convert a DWARF attribute code into its string name. */
12996
12997static char *
aa1ee363 12998dwarf_attr_name (unsigned attr)
c906108c
SS
12999{
13000 switch (attr)
13001 {
13002 case DW_AT_sibling:
13003 return "DW_AT_sibling";
13004 case DW_AT_location:
13005 return "DW_AT_location";
13006 case DW_AT_name:
13007 return "DW_AT_name";
13008 case DW_AT_ordering:
13009 return "DW_AT_ordering";
13010 case DW_AT_subscr_data:
13011 return "DW_AT_subscr_data";
13012 case DW_AT_byte_size:
13013 return "DW_AT_byte_size";
13014 case DW_AT_bit_offset:
13015 return "DW_AT_bit_offset";
13016 case DW_AT_bit_size:
13017 return "DW_AT_bit_size";
13018 case DW_AT_element_list:
13019 return "DW_AT_element_list";
13020 case DW_AT_stmt_list:
13021 return "DW_AT_stmt_list";
13022 case DW_AT_low_pc:
13023 return "DW_AT_low_pc";
13024 case DW_AT_high_pc:
13025 return "DW_AT_high_pc";
13026 case DW_AT_language:
13027 return "DW_AT_language";
13028 case DW_AT_member:
13029 return "DW_AT_member";
13030 case DW_AT_discr:
13031 return "DW_AT_discr";
13032 case DW_AT_discr_value:
13033 return "DW_AT_discr_value";
13034 case DW_AT_visibility:
13035 return "DW_AT_visibility";
13036 case DW_AT_import:
13037 return "DW_AT_import";
13038 case DW_AT_string_length:
13039 return "DW_AT_string_length";
13040 case DW_AT_common_reference:
13041 return "DW_AT_common_reference";
13042 case DW_AT_comp_dir:
13043 return "DW_AT_comp_dir";
13044 case DW_AT_const_value:
13045 return "DW_AT_const_value";
13046 case DW_AT_containing_type:
13047 return "DW_AT_containing_type";
13048 case DW_AT_default_value:
13049 return "DW_AT_default_value";
13050 case DW_AT_inline:
13051 return "DW_AT_inline";
13052 case DW_AT_is_optional:
13053 return "DW_AT_is_optional";
13054 case DW_AT_lower_bound:
13055 return "DW_AT_lower_bound";
13056 case DW_AT_producer:
13057 return "DW_AT_producer";
13058 case DW_AT_prototyped:
13059 return "DW_AT_prototyped";
13060 case DW_AT_return_addr:
13061 return "DW_AT_return_addr";
13062 case DW_AT_start_scope:
13063 return "DW_AT_start_scope";
09fa0d7c
JK
13064 case DW_AT_bit_stride:
13065 return "DW_AT_bit_stride";
c906108c
SS
13066 case DW_AT_upper_bound:
13067 return "DW_AT_upper_bound";
13068 case DW_AT_abstract_origin:
13069 return "DW_AT_abstract_origin";
13070 case DW_AT_accessibility:
13071 return "DW_AT_accessibility";
13072 case DW_AT_address_class:
13073 return "DW_AT_address_class";
13074 case DW_AT_artificial:
13075 return "DW_AT_artificial";
13076 case DW_AT_base_types:
13077 return "DW_AT_base_types";
13078 case DW_AT_calling_convention:
13079 return "DW_AT_calling_convention";
13080 case DW_AT_count:
13081 return "DW_AT_count";
13082 case DW_AT_data_member_location:
13083 return "DW_AT_data_member_location";
13084 case DW_AT_decl_column:
13085 return "DW_AT_decl_column";
13086 case DW_AT_decl_file:
13087 return "DW_AT_decl_file";
13088 case DW_AT_decl_line:
13089 return "DW_AT_decl_line";
13090 case DW_AT_declaration:
13091 return "DW_AT_declaration";
13092 case DW_AT_discr_list:
13093 return "DW_AT_discr_list";
13094 case DW_AT_encoding:
13095 return "DW_AT_encoding";
13096 case DW_AT_external:
13097 return "DW_AT_external";
13098 case DW_AT_frame_base:
13099 return "DW_AT_frame_base";
13100 case DW_AT_friend:
13101 return "DW_AT_friend";
13102 case DW_AT_identifier_case:
13103 return "DW_AT_identifier_case";
13104 case DW_AT_macro_info:
13105 return "DW_AT_macro_info";
13106 case DW_AT_namelist_items:
13107 return "DW_AT_namelist_items";
13108 case DW_AT_priority:
13109 return "DW_AT_priority";
13110 case DW_AT_segment:
13111 return "DW_AT_segment";
13112 case DW_AT_specification:
13113 return "DW_AT_specification";
13114 case DW_AT_static_link:
13115 return "DW_AT_static_link";
13116 case DW_AT_type:
13117 return "DW_AT_type";
13118 case DW_AT_use_location:
13119 return "DW_AT_use_location";
13120 case DW_AT_variable_parameter:
13121 return "DW_AT_variable_parameter";
13122 case DW_AT_virtuality:
13123 return "DW_AT_virtuality";
13124 case DW_AT_vtable_elem_location:
13125 return "DW_AT_vtable_elem_location";
b7619582 13126 /* DWARF 3 values. */
d9fa45fe
DC
13127 case DW_AT_allocated:
13128 return "DW_AT_allocated";
13129 case DW_AT_associated:
13130 return "DW_AT_associated";
13131 case DW_AT_data_location:
13132 return "DW_AT_data_location";
09fa0d7c
JK
13133 case DW_AT_byte_stride:
13134 return "DW_AT_byte_stride";
d9fa45fe
DC
13135 case DW_AT_entry_pc:
13136 return "DW_AT_entry_pc";
13137 case DW_AT_use_UTF8:
13138 return "DW_AT_use_UTF8";
13139 case DW_AT_extension:
13140 return "DW_AT_extension";
13141 case DW_AT_ranges:
13142 return "DW_AT_ranges";
13143 case DW_AT_trampoline:
13144 return "DW_AT_trampoline";
13145 case DW_AT_call_column:
13146 return "DW_AT_call_column";
13147 case DW_AT_call_file:
13148 return "DW_AT_call_file";
13149 case DW_AT_call_line:
13150 return "DW_AT_call_line";
b7619582
GF
13151 case DW_AT_description:
13152 return "DW_AT_description";
13153 case DW_AT_binary_scale:
13154 return "DW_AT_binary_scale";
13155 case DW_AT_decimal_scale:
13156 return "DW_AT_decimal_scale";
13157 case DW_AT_small:
13158 return "DW_AT_small";
13159 case DW_AT_decimal_sign:
13160 return "DW_AT_decimal_sign";
13161 case DW_AT_digit_count:
13162 return "DW_AT_digit_count";
13163 case DW_AT_picture_string:
13164 return "DW_AT_picture_string";
13165 case DW_AT_mutable:
13166 return "DW_AT_mutable";
13167 case DW_AT_threads_scaled:
13168 return "DW_AT_threads_scaled";
13169 case DW_AT_explicit:
13170 return "DW_AT_explicit";
13171 case DW_AT_object_pointer:
13172 return "DW_AT_object_pointer";
13173 case DW_AT_endianity:
13174 return "DW_AT_endianity";
13175 case DW_AT_elemental:
13176 return "DW_AT_elemental";
13177 case DW_AT_pure:
13178 return "DW_AT_pure";
13179 case DW_AT_recursive:
13180 return "DW_AT_recursive";
348e048f
DE
13181 /* DWARF 4 values. */
13182 case DW_AT_signature:
13183 return "DW_AT_signature";
31ef98ae
TT
13184 case DW_AT_linkage_name:
13185 return "DW_AT_linkage_name";
b7619582 13186 /* SGI/MIPS extensions. */
c764a876 13187#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13188 case DW_AT_MIPS_fde:
13189 return "DW_AT_MIPS_fde";
c764a876 13190#endif
c906108c
SS
13191 case DW_AT_MIPS_loop_begin:
13192 return "DW_AT_MIPS_loop_begin";
13193 case DW_AT_MIPS_tail_loop_begin:
13194 return "DW_AT_MIPS_tail_loop_begin";
13195 case DW_AT_MIPS_epilog_begin:
13196 return "DW_AT_MIPS_epilog_begin";
13197 case DW_AT_MIPS_loop_unroll_factor:
13198 return "DW_AT_MIPS_loop_unroll_factor";
13199 case DW_AT_MIPS_software_pipeline_depth:
13200 return "DW_AT_MIPS_software_pipeline_depth";
13201 case DW_AT_MIPS_linkage_name:
13202 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13203 case DW_AT_MIPS_stride:
13204 return "DW_AT_MIPS_stride";
13205 case DW_AT_MIPS_abstract_name:
13206 return "DW_AT_MIPS_abstract_name";
13207 case DW_AT_MIPS_clone_origin:
13208 return "DW_AT_MIPS_clone_origin";
13209 case DW_AT_MIPS_has_inlines:
13210 return "DW_AT_MIPS_has_inlines";
b7619582 13211 /* HP extensions. */
c764a876 13212#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13213 case DW_AT_HP_block_index:
13214 return "DW_AT_HP_block_index";
c764a876 13215#endif
b7619582
GF
13216 case DW_AT_HP_unmodifiable:
13217 return "DW_AT_HP_unmodifiable";
13218 case DW_AT_HP_actuals_stmt_list:
13219 return "DW_AT_HP_actuals_stmt_list";
13220 case DW_AT_HP_proc_per_section:
13221 return "DW_AT_HP_proc_per_section";
13222 case DW_AT_HP_raw_data_ptr:
13223 return "DW_AT_HP_raw_data_ptr";
13224 case DW_AT_HP_pass_by_reference:
13225 return "DW_AT_HP_pass_by_reference";
13226 case DW_AT_HP_opt_level:
13227 return "DW_AT_HP_opt_level";
13228 case DW_AT_HP_prof_version_id:
13229 return "DW_AT_HP_prof_version_id";
13230 case DW_AT_HP_opt_flags:
13231 return "DW_AT_HP_opt_flags";
13232 case DW_AT_HP_cold_region_low_pc:
13233 return "DW_AT_HP_cold_region_low_pc";
13234 case DW_AT_HP_cold_region_high_pc:
13235 return "DW_AT_HP_cold_region_high_pc";
13236 case DW_AT_HP_all_variables_modifiable:
13237 return "DW_AT_HP_all_variables_modifiable";
13238 case DW_AT_HP_linkage_name:
13239 return "DW_AT_HP_linkage_name";
13240 case DW_AT_HP_prof_flags:
13241 return "DW_AT_HP_prof_flags";
13242 /* GNU extensions. */
c906108c
SS
13243 case DW_AT_sf_names:
13244 return "DW_AT_sf_names";
13245 case DW_AT_src_info:
13246 return "DW_AT_src_info";
13247 case DW_AT_mac_info:
13248 return "DW_AT_mac_info";
13249 case DW_AT_src_coords:
13250 return "DW_AT_src_coords";
13251 case DW_AT_body_begin:
13252 return "DW_AT_body_begin";
13253 case DW_AT_body_end:
13254 return "DW_AT_body_end";
f5f8a009
EZ
13255 case DW_AT_GNU_vector:
13256 return "DW_AT_GNU_vector";
2de00c64
DE
13257 case DW_AT_GNU_odr_signature:
13258 return "DW_AT_GNU_odr_signature";
b7619582
GF
13259 /* VMS extensions. */
13260 case DW_AT_VMS_rtnbeg_pd_address:
13261 return "DW_AT_VMS_rtnbeg_pd_address";
13262 /* UPC extension. */
13263 case DW_AT_upc_threads_scaled:
13264 return "DW_AT_upc_threads_scaled";
13265 /* PGI (STMicroelectronics) extensions. */
13266 case DW_AT_PGI_lbase:
13267 return "DW_AT_PGI_lbase";
13268 case DW_AT_PGI_soffset:
13269 return "DW_AT_PGI_soffset";
13270 case DW_AT_PGI_lstride:
13271 return "DW_AT_PGI_lstride";
c906108c
SS
13272 default:
13273 return "DW_AT_<unknown>";
13274 }
13275}
13276
13277/* Convert a DWARF value form code into its string name. */
13278
13279static char *
aa1ee363 13280dwarf_form_name (unsigned form)
c906108c
SS
13281{
13282 switch (form)
13283 {
13284 case DW_FORM_addr:
13285 return "DW_FORM_addr";
13286 case DW_FORM_block2:
13287 return "DW_FORM_block2";
13288 case DW_FORM_block4:
13289 return "DW_FORM_block4";
13290 case DW_FORM_data2:
13291 return "DW_FORM_data2";
13292 case DW_FORM_data4:
13293 return "DW_FORM_data4";
13294 case DW_FORM_data8:
13295 return "DW_FORM_data8";
13296 case DW_FORM_string:
13297 return "DW_FORM_string";
13298 case DW_FORM_block:
13299 return "DW_FORM_block";
13300 case DW_FORM_block1:
13301 return "DW_FORM_block1";
13302 case DW_FORM_data1:
13303 return "DW_FORM_data1";
13304 case DW_FORM_flag:
13305 return "DW_FORM_flag";
13306 case DW_FORM_sdata:
13307 return "DW_FORM_sdata";
13308 case DW_FORM_strp:
13309 return "DW_FORM_strp";
13310 case DW_FORM_udata:
13311 return "DW_FORM_udata";
13312 case DW_FORM_ref_addr:
13313 return "DW_FORM_ref_addr";
13314 case DW_FORM_ref1:
13315 return "DW_FORM_ref1";
13316 case DW_FORM_ref2:
13317 return "DW_FORM_ref2";
13318 case DW_FORM_ref4:
13319 return "DW_FORM_ref4";
13320 case DW_FORM_ref8:
13321 return "DW_FORM_ref8";
13322 case DW_FORM_ref_udata:
13323 return "DW_FORM_ref_udata";
13324 case DW_FORM_indirect:
13325 return "DW_FORM_indirect";
348e048f
DE
13326 case DW_FORM_sec_offset:
13327 return "DW_FORM_sec_offset";
13328 case DW_FORM_exprloc:
13329 return "DW_FORM_exprloc";
13330 case DW_FORM_flag_present:
13331 return "DW_FORM_flag_present";
55f1336d
TT
13332 case DW_FORM_ref_sig8:
13333 return "DW_FORM_ref_sig8";
c906108c
SS
13334 default:
13335 return "DW_FORM_<unknown>";
13336 }
13337}
13338
13339/* Convert a DWARF stack opcode into its string name. */
13340
9eae7c52 13341const char *
b1bfef65 13342dwarf_stack_op_name (unsigned op)
c906108c
SS
13343{
13344 switch (op)
13345 {
13346 case DW_OP_addr:
13347 return "DW_OP_addr";
13348 case DW_OP_deref:
13349 return "DW_OP_deref";
13350 case DW_OP_const1u:
13351 return "DW_OP_const1u";
13352 case DW_OP_const1s:
13353 return "DW_OP_const1s";
13354 case DW_OP_const2u:
13355 return "DW_OP_const2u";
13356 case DW_OP_const2s:
13357 return "DW_OP_const2s";
13358 case DW_OP_const4u:
13359 return "DW_OP_const4u";
13360 case DW_OP_const4s:
13361 return "DW_OP_const4s";
13362 case DW_OP_const8u:
13363 return "DW_OP_const8u";
13364 case DW_OP_const8s:
13365 return "DW_OP_const8s";
13366 case DW_OP_constu:
13367 return "DW_OP_constu";
13368 case DW_OP_consts:
13369 return "DW_OP_consts";
13370 case DW_OP_dup:
13371 return "DW_OP_dup";
13372 case DW_OP_drop:
13373 return "DW_OP_drop";
13374 case DW_OP_over:
13375 return "DW_OP_over";
13376 case DW_OP_pick:
13377 return "DW_OP_pick";
13378 case DW_OP_swap:
13379 return "DW_OP_swap";
13380 case DW_OP_rot:
13381 return "DW_OP_rot";
13382 case DW_OP_xderef:
13383 return "DW_OP_xderef";
13384 case DW_OP_abs:
13385 return "DW_OP_abs";
13386 case DW_OP_and:
13387 return "DW_OP_and";
13388 case DW_OP_div:
13389 return "DW_OP_div";
13390 case DW_OP_minus:
13391 return "DW_OP_minus";
13392 case DW_OP_mod:
13393 return "DW_OP_mod";
13394 case DW_OP_mul:
13395 return "DW_OP_mul";
13396 case DW_OP_neg:
13397 return "DW_OP_neg";
13398 case DW_OP_not:
13399 return "DW_OP_not";
13400 case DW_OP_or:
13401 return "DW_OP_or";
13402 case DW_OP_plus:
13403 return "DW_OP_plus";
13404 case DW_OP_plus_uconst:
13405 return "DW_OP_plus_uconst";
13406 case DW_OP_shl:
13407 return "DW_OP_shl";
13408 case DW_OP_shr:
13409 return "DW_OP_shr";
13410 case DW_OP_shra:
13411 return "DW_OP_shra";
13412 case DW_OP_xor:
13413 return "DW_OP_xor";
13414 case DW_OP_bra:
13415 return "DW_OP_bra";
13416 case DW_OP_eq:
13417 return "DW_OP_eq";
13418 case DW_OP_ge:
13419 return "DW_OP_ge";
13420 case DW_OP_gt:
13421 return "DW_OP_gt";
13422 case DW_OP_le:
13423 return "DW_OP_le";
13424 case DW_OP_lt:
13425 return "DW_OP_lt";
13426 case DW_OP_ne:
13427 return "DW_OP_ne";
13428 case DW_OP_skip:
13429 return "DW_OP_skip";
13430 case DW_OP_lit0:
13431 return "DW_OP_lit0";
13432 case DW_OP_lit1:
13433 return "DW_OP_lit1";
13434 case DW_OP_lit2:
13435 return "DW_OP_lit2";
13436 case DW_OP_lit3:
13437 return "DW_OP_lit3";
13438 case DW_OP_lit4:
13439 return "DW_OP_lit4";
13440 case DW_OP_lit5:
13441 return "DW_OP_lit5";
13442 case DW_OP_lit6:
13443 return "DW_OP_lit6";
13444 case DW_OP_lit7:
13445 return "DW_OP_lit7";
13446 case DW_OP_lit8:
13447 return "DW_OP_lit8";
13448 case DW_OP_lit9:
13449 return "DW_OP_lit9";
13450 case DW_OP_lit10:
13451 return "DW_OP_lit10";
13452 case DW_OP_lit11:
13453 return "DW_OP_lit11";
13454 case DW_OP_lit12:
13455 return "DW_OP_lit12";
13456 case DW_OP_lit13:
13457 return "DW_OP_lit13";
13458 case DW_OP_lit14:
13459 return "DW_OP_lit14";
13460 case DW_OP_lit15:
13461 return "DW_OP_lit15";
13462 case DW_OP_lit16:
13463 return "DW_OP_lit16";
13464 case DW_OP_lit17:
13465 return "DW_OP_lit17";
13466 case DW_OP_lit18:
13467 return "DW_OP_lit18";
13468 case DW_OP_lit19:
13469 return "DW_OP_lit19";
13470 case DW_OP_lit20:
13471 return "DW_OP_lit20";
13472 case DW_OP_lit21:
13473 return "DW_OP_lit21";
13474 case DW_OP_lit22:
13475 return "DW_OP_lit22";
13476 case DW_OP_lit23:
13477 return "DW_OP_lit23";
13478 case DW_OP_lit24:
13479 return "DW_OP_lit24";
13480 case DW_OP_lit25:
13481 return "DW_OP_lit25";
13482 case DW_OP_lit26:
13483 return "DW_OP_lit26";
13484 case DW_OP_lit27:
13485 return "DW_OP_lit27";
13486 case DW_OP_lit28:
13487 return "DW_OP_lit28";
13488 case DW_OP_lit29:
13489 return "DW_OP_lit29";
13490 case DW_OP_lit30:
13491 return "DW_OP_lit30";
13492 case DW_OP_lit31:
13493 return "DW_OP_lit31";
13494 case DW_OP_reg0:
13495 return "DW_OP_reg0";
13496 case DW_OP_reg1:
13497 return "DW_OP_reg1";
13498 case DW_OP_reg2:
13499 return "DW_OP_reg2";
13500 case DW_OP_reg3:
13501 return "DW_OP_reg3";
13502 case DW_OP_reg4:
13503 return "DW_OP_reg4";
13504 case DW_OP_reg5:
13505 return "DW_OP_reg5";
13506 case DW_OP_reg6:
13507 return "DW_OP_reg6";
13508 case DW_OP_reg7:
13509 return "DW_OP_reg7";
13510 case DW_OP_reg8:
13511 return "DW_OP_reg8";
13512 case DW_OP_reg9:
13513 return "DW_OP_reg9";
13514 case DW_OP_reg10:
13515 return "DW_OP_reg10";
13516 case DW_OP_reg11:
13517 return "DW_OP_reg11";
13518 case DW_OP_reg12:
13519 return "DW_OP_reg12";
13520 case DW_OP_reg13:
13521 return "DW_OP_reg13";
13522 case DW_OP_reg14:
13523 return "DW_OP_reg14";
13524 case DW_OP_reg15:
13525 return "DW_OP_reg15";
13526 case DW_OP_reg16:
13527 return "DW_OP_reg16";
13528 case DW_OP_reg17:
13529 return "DW_OP_reg17";
13530 case DW_OP_reg18:
13531 return "DW_OP_reg18";
13532 case DW_OP_reg19:
13533 return "DW_OP_reg19";
13534 case DW_OP_reg20:
13535 return "DW_OP_reg20";
13536 case DW_OP_reg21:
13537 return "DW_OP_reg21";
13538 case DW_OP_reg22:
13539 return "DW_OP_reg22";
13540 case DW_OP_reg23:
13541 return "DW_OP_reg23";
13542 case DW_OP_reg24:
13543 return "DW_OP_reg24";
13544 case DW_OP_reg25:
13545 return "DW_OP_reg25";
13546 case DW_OP_reg26:
13547 return "DW_OP_reg26";
13548 case DW_OP_reg27:
13549 return "DW_OP_reg27";
13550 case DW_OP_reg28:
13551 return "DW_OP_reg28";
13552 case DW_OP_reg29:
13553 return "DW_OP_reg29";
13554 case DW_OP_reg30:
13555 return "DW_OP_reg30";
13556 case DW_OP_reg31:
13557 return "DW_OP_reg31";
13558 case DW_OP_breg0:
13559 return "DW_OP_breg0";
13560 case DW_OP_breg1:
13561 return "DW_OP_breg1";
13562 case DW_OP_breg2:
13563 return "DW_OP_breg2";
13564 case DW_OP_breg3:
13565 return "DW_OP_breg3";
13566 case DW_OP_breg4:
13567 return "DW_OP_breg4";
13568 case DW_OP_breg5:
13569 return "DW_OP_breg5";
13570 case DW_OP_breg6:
13571 return "DW_OP_breg6";
13572 case DW_OP_breg7:
13573 return "DW_OP_breg7";
13574 case DW_OP_breg8:
13575 return "DW_OP_breg8";
13576 case DW_OP_breg9:
13577 return "DW_OP_breg9";
13578 case DW_OP_breg10:
13579 return "DW_OP_breg10";
13580 case DW_OP_breg11:
13581 return "DW_OP_breg11";
13582 case DW_OP_breg12:
13583 return "DW_OP_breg12";
13584 case DW_OP_breg13:
13585 return "DW_OP_breg13";
13586 case DW_OP_breg14:
13587 return "DW_OP_breg14";
13588 case DW_OP_breg15:
13589 return "DW_OP_breg15";
13590 case DW_OP_breg16:
13591 return "DW_OP_breg16";
13592 case DW_OP_breg17:
13593 return "DW_OP_breg17";
13594 case DW_OP_breg18:
13595 return "DW_OP_breg18";
13596 case DW_OP_breg19:
13597 return "DW_OP_breg19";
13598 case DW_OP_breg20:
13599 return "DW_OP_breg20";
13600 case DW_OP_breg21:
13601 return "DW_OP_breg21";
13602 case DW_OP_breg22:
13603 return "DW_OP_breg22";
13604 case DW_OP_breg23:
13605 return "DW_OP_breg23";
13606 case DW_OP_breg24:
13607 return "DW_OP_breg24";
13608 case DW_OP_breg25:
13609 return "DW_OP_breg25";
13610 case DW_OP_breg26:
13611 return "DW_OP_breg26";
13612 case DW_OP_breg27:
13613 return "DW_OP_breg27";
13614 case DW_OP_breg28:
13615 return "DW_OP_breg28";
13616 case DW_OP_breg29:
13617 return "DW_OP_breg29";
13618 case DW_OP_breg30:
13619 return "DW_OP_breg30";
13620 case DW_OP_breg31:
13621 return "DW_OP_breg31";
13622 case DW_OP_regx:
13623 return "DW_OP_regx";
13624 case DW_OP_fbreg:
13625 return "DW_OP_fbreg";
13626 case DW_OP_bregx:
13627 return "DW_OP_bregx";
13628 case DW_OP_piece:
13629 return "DW_OP_piece";
13630 case DW_OP_deref_size:
13631 return "DW_OP_deref_size";
13632 case DW_OP_xderef_size:
13633 return "DW_OP_xderef_size";
13634 case DW_OP_nop:
13635 return "DW_OP_nop";
b7619582 13636 /* DWARF 3 extensions. */
ed348acc
EZ
13637 case DW_OP_push_object_address:
13638 return "DW_OP_push_object_address";
13639 case DW_OP_call2:
13640 return "DW_OP_call2";
13641 case DW_OP_call4:
13642 return "DW_OP_call4";
13643 case DW_OP_call_ref:
13644 return "DW_OP_call_ref";
b7619582
GF
13645 case DW_OP_form_tls_address:
13646 return "DW_OP_form_tls_address";
13647 case DW_OP_call_frame_cfa:
13648 return "DW_OP_call_frame_cfa";
13649 case DW_OP_bit_piece:
13650 return "DW_OP_bit_piece";
9eae7c52
TT
13651 /* DWARF 4 extensions. */
13652 case DW_OP_implicit_value:
13653 return "DW_OP_implicit_value";
13654 case DW_OP_stack_value:
13655 return "DW_OP_stack_value";
13656 /* GNU extensions. */
ed348acc
EZ
13657 case DW_OP_GNU_push_tls_address:
13658 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13659 case DW_OP_GNU_uninit:
13660 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13661 case DW_OP_GNU_implicit_pointer:
13662 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13663 case DW_OP_GNU_entry_value:
13664 return "DW_OP_GNU_entry_value";
13665 case DW_OP_GNU_const_type:
13666 return "DW_OP_GNU_const_type";
13667 case DW_OP_GNU_regval_type:
13668 return "DW_OP_GNU_regval_type";
13669 case DW_OP_GNU_deref_type:
13670 return "DW_OP_GNU_deref_type";
13671 case DW_OP_GNU_convert:
13672 return "DW_OP_GNU_convert";
13673 case DW_OP_GNU_reinterpret:
13674 return "DW_OP_GNU_reinterpret";
c906108c 13675 default:
b1bfef65 13676 return NULL;
c906108c
SS
13677 }
13678}
13679
13680static char *
fba45db2 13681dwarf_bool_name (unsigned mybool)
c906108c
SS
13682{
13683 if (mybool)
13684 return "TRUE";
13685 else
13686 return "FALSE";
13687}
13688
13689/* Convert a DWARF type code into its string name. */
13690
13691static char *
aa1ee363 13692dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13693{
13694 switch (enc)
13695 {
b7619582
GF
13696 case DW_ATE_void:
13697 return "DW_ATE_void";
c906108c
SS
13698 case DW_ATE_address:
13699 return "DW_ATE_address";
13700 case DW_ATE_boolean:
13701 return "DW_ATE_boolean";
13702 case DW_ATE_complex_float:
13703 return "DW_ATE_complex_float";
13704 case DW_ATE_float:
13705 return "DW_ATE_float";
13706 case DW_ATE_signed:
13707 return "DW_ATE_signed";
13708 case DW_ATE_signed_char:
13709 return "DW_ATE_signed_char";
13710 case DW_ATE_unsigned:
13711 return "DW_ATE_unsigned";
13712 case DW_ATE_unsigned_char:
13713 return "DW_ATE_unsigned_char";
b7619582 13714 /* DWARF 3. */
d9fa45fe
DC
13715 case DW_ATE_imaginary_float:
13716 return "DW_ATE_imaginary_float";
b7619582
GF
13717 case DW_ATE_packed_decimal:
13718 return "DW_ATE_packed_decimal";
13719 case DW_ATE_numeric_string:
13720 return "DW_ATE_numeric_string";
13721 case DW_ATE_edited:
13722 return "DW_ATE_edited";
13723 case DW_ATE_signed_fixed:
13724 return "DW_ATE_signed_fixed";
13725 case DW_ATE_unsigned_fixed:
13726 return "DW_ATE_unsigned_fixed";
13727 case DW_ATE_decimal_float:
13728 return "DW_ATE_decimal_float";
75079b2b
TT
13729 /* DWARF 4. */
13730 case DW_ATE_UTF:
13731 return "DW_ATE_UTF";
b7619582
GF
13732 /* HP extensions. */
13733 case DW_ATE_HP_float80:
13734 return "DW_ATE_HP_float80";
13735 case DW_ATE_HP_complex_float80:
13736 return "DW_ATE_HP_complex_float80";
13737 case DW_ATE_HP_float128:
13738 return "DW_ATE_HP_float128";
13739 case DW_ATE_HP_complex_float128:
13740 return "DW_ATE_HP_complex_float128";
13741 case DW_ATE_HP_floathpintel:
13742 return "DW_ATE_HP_floathpintel";
13743 case DW_ATE_HP_imaginary_float80:
13744 return "DW_ATE_HP_imaginary_float80";
13745 case DW_ATE_HP_imaginary_float128:
13746 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13747 default:
13748 return "DW_ATE_<unknown>";
13749 }
13750}
13751
0963b4bd 13752/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13753
13754#if 0
13755static char *
aa1ee363 13756dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13757{
13758 switch (cfi_opc)
13759 {
13760 case DW_CFA_advance_loc:
13761 return "DW_CFA_advance_loc";
13762 case DW_CFA_offset:
13763 return "DW_CFA_offset";
13764 case DW_CFA_restore:
13765 return "DW_CFA_restore";
13766 case DW_CFA_nop:
13767 return "DW_CFA_nop";
13768 case DW_CFA_set_loc:
13769 return "DW_CFA_set_loc";
13770 case DW_CFA_advance_loc1:
13771 return "DW_CFA_advance_loc1";
13772 case DW_CFA_advance_loc2:
13773 return "DW_CFA_advance_loc2";
13774 case DW_CFA_advance_loc4:
13775 return "DW_CFA_advance_loc4";
13776 case DW_CFA_offset_extended:
13777 return "DW_CFA_offset_extended";
13778 case DW_CFA_restore_extended:
13779 return "DW_CFA_restore_extended";
13780 case DW_CFA_undefined:
13781 return "DW_CFA_undefined";
13782 case DW_CFA_same_value:
13783 return "DW_CFA_same_value";
13784 case DW_CFA_register:
13785 return "DW_CFA_register";
13786 case DW_CFA_remember_state:
13787 return "DW_CFA_remember_state";
13788 case DW_CFA_restore_state:
13789 return "DW_CFA_restore_state";
13790 case DW_CFA_def_cfa:
13791 return "DW_CFA_def_cfa";
13792 case DW_CFA_def_cfa_register:
13793 return "DW_CFA_def_cfa_register";
13794 case DW_CFA_def_cfa_offset:
13795 return "DW_CFA_def_cfa_offset";
b7619582 13796 /* DWARF 3. */
985cb1a3
JM
13797 case DW_CFA_def_cfa_expression:
13798 return "DW_CFA_def_cfa_expression";
13799 case DW_CFA_expression:
13800 return "DW_CFA_expression";
13801 case DW_CFA_offset_extended_sf:
13802 return "DW_CFA_offset_extended_sf";
13803 case DW_CFA_def_cfa_sf:
13804 return "DW_CFA_def_cfa_sf";
13805 case DW_CFA_def_cfa_offset_sf:
13806 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13807 case DW_CFA_val_offset:
13808 return "DW_CFA_val_offset";
13809 case DW_CFA_val_offset_sf:
13810 return "DW_CFA_val_offset_sf";
13811 case DW_CFA_val_expression:
13812 return "DW_CFA_val_expression";
13813 /* SGI/MIPS specific. */
c906108c
SS
13814 case DW_CFA_MIPS_advance_loc8:
13815 return "DW_CFA_MIPS_advance_loc8";
b7619582 13816 /* GNU extensions. */
985cb1a3
JM
13817 case DW_CFA_GNU_window_save:
13818 return "DW_CFA_GNU_window_save";
13819 case DW_CFA_GNU_args_size:
13820 return "DW_CFA_GNU_args_size";
13821 case DW_CFA_GNU_negative_offset_extended:
13822 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13823 default:
13824 return "DW_CFA_<unknown>";
13825 }
13826}
13827#endif
13828
f9aca02d 13829static void
d97bc12b 13830dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13831{
13832 unsigned int i;
13833
d97bc12b
DE
13834 print_spaces (indent, f);
13835 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13836 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13837
13838 if (die->parent != NULL)
13839 {
13840 print_spaces (indent, f);
13841 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13842 die->parent->offset);
13843 }
13844
13845 print_spaces (indent, f);
13846 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13847 dwarf_bool_name (die->child != NULL));
c906108c 13848
d97bc12b
DE
13849 print_spaces (indent, f);
13850 fprintf_unfiltered (f, " attributes:\n");
13851
c906108c
SS
13852 for (i = 0; i < die->num_attrs; ++i)
13853 {
d97bc12b
DE
13854 print_spaces (indent, f);
13855 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13856 dwarf_attr_name (die->attrs[i].name),
13857 dwarf_form_name (die->attrs[i].form));
d97bc12b 13858
c906108c
SS
13859 switch (die->attrs[i].form)
13860 {
13861 case DW_FORM_ref_addr:
13862 case DW_FORM_addr:
d97bc12b 13863 fprintf_unfiltered (f, "address: ");
5af949e3 13864 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13865 break;
13866 case DW_FORM_block2:
13867 case DW_FORM_block4:
13868 case DW_FORM_block:
13869 case DW_FORM_block1:
3e43a32a
MS
13870 fprintf_unfiltered (f, "block: size %d",
13871 DW_BLOCK (&die->attrs[i])->size);
c906108c 13872 break;
2dc7f7b3
TT
13873 case DW_FORM_exprloc:
13874 fprintf_unfiltered (f, "expression: size %u",
13875 DW_BLOCK (&die->attrs[i])->size);
13876 break;
10b3939b
DJ
13877 case DW_FORM_ref1:
13878 case DW_FORM_ref2:
13879 case DW_FORM_ref4:
d97bc12b 13880 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13881 (long) (DW_ADDR (&die->attrs[i])));
13882 break;
c906108c
SS
13883 case DW_FORM_data1:
13884 case DW_FORM_data2:
13885 case DW_FORM_data4:
ce5d95e1 13886 case DW_FORM_data8:
c906108c
SS
13887 case DW_FORM_udata:
13888 case DW_FORM_sdata:
43bbcdc2
PH
13889 fprintf_unfiltered (f, "constant: %s",
13890 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13891 break;
2dc7f7b3
TT
13892 case DW_FORM_sec_offset:
13893 fprintf_unfiltered (f, "section offset: %s",
13894 pulongest (DW_UNSND (&die->attrs[i])));
13895 break;
55f1336d 13896 case DW_FORM_ref_sig8:
348e048f
DE
13897 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13898 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13899 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13900 else
13901 fprintf_unfiltered (f, "signatured type, offset: unknown");
13902 break;
c906108c 13903 case DW_FORM_string:
4bdf3d34 13904 case DW_FORM_strp:
8285870a 13905 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13906 DW_STRING (&die->attrs[i])
8285870a
JK
13907 ? DW_STRING (&die->attrs[i]) : "",
13908 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13909 break;
13910 case DW_FORM_flag:
13911 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13912 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13913 else
d97bc12b 13914 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13915 break;
2dc7f7b3
TT
13916 case DW_FORM_flag_present:
13917 fprintf_unfiltered (f, "flag: TRUE");
13918 break;
a8329558 13919 case DW_FORM_indirect:
0963b4bd
MS
13920 /* The reader will have reduced the indirect form to
13921 the "base form" so this form should not occur. */
3e43a32a
MS
13922 fprintf_unfiltered (f,
13923 "unexpected attribute form: DW_FORM_indirect");
a8329558 13924 break;
c906108c 13925 default:
d97bc12b 13926 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13927 die->attrs[i].form);
d97bc12b 13928 break;
c906108c 13929 }
d97bc12b 13930 fprintf_unfiltered (f, "\n");
c906108c
SS
13931 }
13932}
13933
f9aca02d 13934static void
d97bc12b 13935dump_die_for_error (struct die_info *die)
c906108c 13936{
d97bc12b
DE
13937 dump_die_shallow (gdb_stderr, 0, die);
13938}
13939
13940static void
13941dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13942{
13943 int indent = level * 4;
13944
13945 gdb_assert (die != NULL);
13946
13947 if (level >= max_level)
13948 return;
13949
13950 dump_die_shallow (f, indent, die);
13951
13952 if (die->child != NULL)
c906108c 13953 {
d97bc12b
DE
13954 print_spaces (indent, f);
13955 fprintf_unfiltered (f, " Children:");
13956 if (level + 1 < max_level)
13957 {
13958 fprintf_unfiltered (f, "\n");
13959 dump_die_1 (f, level + 1, max_level, die->child);
13960 }
13961 else
13962 {
3e43a32a
MS
13963 fprintf_unfiltered (f,
13964 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13965 }
13966 }
13967
13968 if (die->sibling != NULL && level > 0)
13969 {
13970 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13971 }
13972}
13973
d97bc12b
DE
13974/* This is called from the pdie macro in gdbinit.in.
13975 It's not static so gcc will keep a copy callable from gdb. */
13976
13977void
13978dump_die (struct die_info *die, int max_level)
13979{
13980 dump_die_1 (gdb_stdlog, 0, max_level, die);
13981}
13982
f9aca02d 13983static void
51545339 13984store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13985{
51545339 13986 void **slot;
c906108c 13987
51545339
DJ
13988 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13989
13990 *slot = die;
c906108c
SS
13991}
13992
93311388
DE
13993static int
13994is_ref_attr (struct attribute *attr)
c906108c 13995{
c906108c
SS
13996 switch (attr->form)
13997 {
13998 case DW_FORM_ref_addr:
c906108c
SS
13999 case DW_FORM_ref1:
14000 case DW_FORM_ref2:
14001 case DW_FORM_ref4:
613e1657 14002 case DW_FORM_ref8:
c906108c 14003 case DW_FORM_ref_udata:
93311388 14004 return 1;
c906108c 14005 default:
93311388 14006 return 0;
c906108c 14007 }
93311388
DE
14008}
14009
14010static unsigned int
14011dwarf2_get_ref_die_offset (struct attribute *attr)
14012{
14013 if (is_ref_attr (attr))
14014 return DW_ADDR (attr);
14015
14016 complaint (&symfile_complaints,
14017 _("unsupported die ref attribute form: '%s'"),
14018 dwarf_form_name (attr->form));
14019 return 0;
c906108c
SS
14020}
14021
43bbcdc2
PH
14022/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14023 * the value held by the attribute is not constant. */
a02abb62 14024
43bbcdc2 14025static LONGEST
a02abb62
JB
14026dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14027{
14028 if (attr->form == DW_FORM_sdata)
14029 return DW_SND (attr);
14030 else if (attr->form == DW_FORM_udata
14031 || attr->form == DW_FORM_data1
14032 || attr->form == DW_FORM_data2
14033 || attr->form == DW_FORM_data4
14034 || attr->form == DW_FORM_data8)
14035 return DW_UNSND (attr);
14036 else
14037 {
3e43a32a
MS
14038 complaint (&symfile_complaints,
14039 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14040 dwarf_form_name (attr->form));
14041 return default_value;
14042 }
14043}
14044
03dd20cc 14045/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14046 unit and add it to our queue.
14047 The result is non-zero if PER_CU was queued, otherwise the result is zero
14048 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14049
348e048f 14050static int
03dd20cc
DJ
14051maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14052 struct dwarf2_per_cu_data *per_cu)
14053{
98bfdba5
PA
14054 /* We may arrive here during partial symbol reading, if we need full
14055 DIEs to process an unusual case (e.g. template arguments). Do
14056 not queue PER_CU, just tell our caller to load its DIEs. */
14057 if (dwarf2_per_objfile->reading_partial_symbols)
14058 {
14059 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14060 return 1;
14061 return 0;
14062 }
14063
03dd20cc
DJ
14064 /* Mark the dependence relation so that we don't flush PER_CU
14065 too early. */
14066 dwarf2_add_dependence (this_cu, per_cu);
14067
14068 /* If it's already on the queue, we have nothing to do. */
14069 if (per_cu->queued)
348e048f 14070 return 0;
03dd20cc
DJ
14071
14072 /* If the compilation unit is already loaded, just mark it as
14073 used. */
14074 if (per_cu->cu != NULL)
14075 {
14076 per_cu->cu->last_used = 0;
348e048f 14077 return 0;
03dd20cc
DJ
14078 }
14079
14080 /* Add it to the queue. */
a0f42c21 14081 queue_comp_unit (per_cu);
348e048f
DE
14082
14083 return 1;
14084}
14085
14086/* Follow reference or signature attribute ATTR of SRC_DIE.
14087 On entry *REF_CU is the CU of SRC_DIE.
14088 On exit *REF_CU is the CU of the result. */
14089
14090static struct die_info *
14091follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14092 struct dwarf2_cu **ref_cu)
14093{
14094 struct die_info *die;
14095
14096 if (is_ref_attr (attr))
14097 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14098 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14099 die = follow_die_sig (src_die, attr, ref_cu);
14100 else
14101 {
14102 dump_die_for_error (src_die);
14103 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14104 (*ref_cu)->objfile->name);
14105 }
14106
14107 return die;
03dd20cc
DJ
14108}
14109
5c631832 14110/* Follow reference OFFSET.
673bfd45
DE
14111 On entry *REF_CU is the CU of the source die referencing OFFSET.
14112 On exit *REF_CU is the CU of the result.
14113 Returns NULL if OFFSET is invalid. */
f504f079 14114
f9aca02d 14115static struct die_info *
5c631832 14116follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 14117{
10b3939b 14118 struct die_info temp_die;
f2f0e013 14119 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14120
348e048f
DE
14121 gdb_assert (cu->per_cu != NULL);
14122
98bfdba5
PA
14123 target_cu = cu;
14124
b0df02fd 14125 if (cu->per_cu->debug_types_section)
348e048f
DE
14126 {
14127 /* .debug_types CUs cannot reference anything outside their CU.
14128 If they need to, they have to reference a signatured type via
55f1336d 14129 DW_FORM_ref_sig8. */
348e048f 14130 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14131 return NULL;
348e048f
DE
14132 }
14133 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14134 {
14135 struct dwarf2_per_cu_data *per_cu;
9a619af0 14136
45452591 14137 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14138
14139 /* If necessary, add it to the queue and load its DIEs. */
348e048f 14140 if (maybe_queue_comp_unit (cu, per_cu))
a0f42c21 14141 load_full_comp_unit (per_cu);
03dd20cc 14142
10b3939b
DJ
14143 target_cu = per_cu->cu;
14144 }
98bfdba5
PA
14145 else if (cu->dies == NULL)
14146 {
14147 /* We're loading full DIEs during partial symbol reading. */
14148 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
a0f42c21 14149 load_full_comp_unit (cu->per_cu);
98bfdba5 14150 }
c906108c 14151
f2f0e013 14152 *ref_cu = target_cu;
51545339 14153 temp_die.offset = offset;
5c631832
JK
14154 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14155}
10b3939b 14156
5c631832
JK
14157/* Follow reference attribute ATTR of SRC_DIE.
14158 On entry *REF_CU is the CU of SRC_DIE.
14159 On exit *REF_CU is the CU of the result. */
14160
14161static struct die_info *
14162follow_die_ref (struct die_info *src_die, struct attribute *attr,
14163 struct dwarf2_cu **ref_cu)
14164{
14165 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14166 struct dwarf2_cu *cu = *ref_cu;
14167 struct die_info *die;
14168
14169 die = follow_die_offset (offset, ref_cu);
14170 if (!die)
14171 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14172 "at 0x%x [in module %s]"),
14173 offset, src_die->offset, cu->objfile->name);
348e048f 14174
5c631832
JK
14175 return die;
14176}
14177
d83e736b
JK
14178/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14179 Returned value is intended for DW_OP_call*. Returned
14180 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14181
14182struct dwarf2_locexpr_baton
14183dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
14184 struct dwarf2_per_cu_data *per_cu,
14185 CORE_ADDR (*get_frame_pc) (void *baton),
14186 void *baton)
5c631832 14187{
918dd910 14188 struct dwarf2_cu *cu;
5c631832
JK
14189 struct die_info *die;
14190 struct attribute *attr;
14191 struct dwarf2_locexpr_baton retval;
14192
8cf6f0b1
TT
14193 dw2_setup (per_cu->objfile);
14194
918dd910
JK
14195 if (per_cu->cu == NULL)
14196 load_cu (per_cu);
14197 cu = per_cu->cu;
14198
5c631832
JK
14199 die = follow_die_offset (offset, &cu);
14200 if (!die)
14201 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
bb5ed363 14202 offset, per_cu->objfile->name);
5c631832
JK
14203
14204 attr = dwarf2_attr (die, DW_AT_location, cu);
14205 if (!attr)
14206 {
e103e986
JK
14207 /* DWARF: "If there is no such attribute, then there is no effect.".
14208 DATA is ignored if SIZE is 0. */
5c631832 14209
e103e986 14210 retval.data = NULL;
5c631832
JK
14211 retval.size = 0;
14212 }
8cf6f0b1
TT
14213 else if (attr_form_is_section_offset (attr))
14214 {
14215 struct dwarf2_loclist_baton loclist_baton;
14216 CORE_ADDR pc = (*get_frame_pc) (baton);
14217 size_t size;
14218
14219 fill_in_loclist_baton (cu, &loclist_baton, attr);
14220
14221 retval.data = dwarf2_find_location_expression (&loclist_baton,
14222 &size, pc);
14223 retval.size = size;
14224 }
5c631832
JK
14225 else
14226 {
14227 if (!attr_form_is_block (attr))
14228 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14229 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
bb5ed363 14230 offset, per_cu->objfile->name);
5c631832
JK
14231
14232 retval.data = DW_BLOCK (attr)->data;
14233 retval.size = DW_BLOCK (attr)->size;
14234 }
14235 retval.per_cu = cu->per_cu;
918dd910 14236
918dd910
JK
14237 age_cached_comp_units ();
14238
5c631832 14239 return retval;
348e048f
DE
14240}
14241
8a9b8146
TT
14242/* Return the type of the DIE at DIE_OFFSET in the CU named by
14243 PER_CU. */
14244
14245struct type *
14246dwarf2_get_die_type (unsigned int die_offset,
14247 struct dwarf2_per_cu_data *per_cu)
14248{
8a9b8146 14249 dw2_setup (per_cu->objfile);
9ff3b74f 14250 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
14251}
14252
348e048f
DE
14253/* Follow the signature attribute ATTR in SRC_DIE.
14254 On entry *REF_CU is the CU of SRC_DIE.
14255 On exit *REF_CU is the CU of the result. */
14256
14257static struct die_info *
14258follow_die_sig (struct die_info *src_die, struct attribute *attr,
14259 struct dwarf2_cu **ref_cu)
14260{
14261 struct objfile *objfile = (*ref_cu)->objfile;
14262 struct die_info temp_die;
14263 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14264 struct dwarf2_cu *sig_cu;
14265 struct die_info *die;
14266
14267 /* sig_type will be NULL if the signatured type is missing from
14268 the debug info. */
14269 if (sig_type == NULL)
14270 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14271 "at 0x%x [in module %s]"),
14272 src_die->offset, objfile->name);
14273
14274 /* If necessary, add it to the queue and load its DIEs. */
14275
14276 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
a0f42c21 14277 read_signatured_type (sig_type);
348e048f
DE
14278
14279 gdb_assert (sig_type->per_cu.cu != NULL);
14280
14281 sig_cu = sig_type->per_cu.cu;
14282 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14283 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14284 if (die)
14285 {
14286 *ref_cu = sig_cu;
14287 return die;
14288 }
14289
3e43a32a
MS
14290 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14291 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14292 sig_type->type_offset, src_die->offset, objfile->name);
14293}
14294
14295/* Given an offset of a signatured type, return its signatured_type. */
14296
14297static struct signatured_type *
8b70b953
TT
14298lookup_signatured_type_at_offset (struct objfile *objfile,
14299 struct dwarf2_section_info *section,
14300 unsigned int offset)
348e048f 14301{
8b70b953 14302 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14303 unsigned int length, initial_length_size;
14304 unsigned int sig_offset;
14305 struct signatured_type find_entry, *type_sig;
14306
14307 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14308 sig_offset = (initial_length_size
14309 + 2 /*version*/
14310 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14311 + 1 /*address_size*/);
14312 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14313 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14314
14315 /* This is only used to lookup previously recorded types.
14316 If we didn't find it, it's our bug. */
14317 gdb_assert (type_sig != NULL);
b3c8eb43 14318 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14319
14320 return type_sig;
14321}
14322
e5fe5e75 14323/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
14324
14325static void
e5fe5e75 14326load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 14327{
e5fe5e75
DE
14328 struct objfile *objfile = per_cu->objfile;
14329 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14330 unsigned int offset = per_cu->offset;
348e048f
DE
14331 struct signatured_type *type_sig;
14332
8b70b953 14333 dwarf2_read_section (objfile, sect);
be391dca 14334
348e048f 14335 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
14336 hash table lookup. */
14337 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14338 the signature to assert we found the right one.
14339 Ok, but it's a lot of work. We should simplify things so any needed
14340 assert doesn't require all this clumsiness. */
8b70b953 14341 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14342
14343 gdb_assert (type_sig->per_cu.cu == NULL);
14344
a0f42c21 14345 read_signatured_type (type_sig);
348e048f
DE
14346
14347 gdb_assert (type_sig->per_cu.cu != NULL);
14348}
14349
14350/* Read in a signatured type and build its CU and DIEs. */
14351
14352static void
a0f42c21 14353read_signatured_type (struct signatured_type *type_sig)
348e048f 14354{
a0f42c21 14355 struct objfile *objfile = type_sig->per_cu.objfile;
1fd400ff 14356 gdb_byte *types_ptr;
348e048f
DE
14357 struct die_reader_specs reader_specs;
14358 struct dwarf2_cu *cu;
14359 ULONGEST signature;
14360 struct cleanup *back_to, *free_cu_cleanup;
b0df02fd 14361 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
348e048f 14362
8b70b953
TT
14363 dwarf2_read_section (objfile, section);
14364 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14365
348e048f
DE
14366 gdb_assert (type_sig->per_cu.cu == NULL);
14367
9816fde3 14368 cu = xmalloc (sizeof (*cu));
23745b47 14369 init_one_comp_unit (cu, &type_sig->per_cu);
348e048f
DE
14370
14371 /* If an error occurs while loading, release our storage. */
68dc6402 14372 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
348e048f 14373
9ff913ba
DE
14374 types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14375 &signature, NULL);
348e048f
DE
14376 gdb_assert (signature == type_sig->signature);
14377
14378 cu->die_hash
14379 = htab_create_alloc_ex (cu->header.length / 12,
14380 die_hash,
14381 die_eq,
14382 NULL,
14383 &cu->comp_unit_obstack,
14384 hashtab_obstack_allocate,
14385 dummy_obstack_deallocate);
14386
e5fe5e75 14387 dwarf2_read_abbrevs (cu);
348e048f
DE
14388 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14389
14390 init_cu_die_reader (&reader_specs, cu);
14391
14392 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14393 NULL /*parent*/);
14394
14395 /* We try not to read any attributes in this function, because not
9cdd5dbd 14396 all CUs needed for references have been loaded yet, and symbol
348e048f
DE
14397 table processing isn't initialized. But we have to set the CU language,
14398 or we won't be able to build types correctly. */
9816fde3 14399 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14400
14401 do_cleanups (back_to);
14402
14403 /* We've successfully allocated this compilation unit. Let our caller
14404 clean it up when finished with it. */
14405 discard_cleanups (free_cu_cleanup);
14406
14407 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14408 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14409}
14410
c906108c
SS
14411/* Decode simple location descriptions.
14412 Given a pointer to a dwarf block that defines a location, compute
14413 the location and return the value.
14414
4cecd739
DJ
14415 NOTE drow/2003-11-18: This function is called in two situations
14416 now: for the address of static or global variables (partial symbols
14417 only) and for offsets into structures which are expected to be
14418 (more or less) constant. The partial symbol case should go away,
14419 and only the constant case should remain. That will let this
14420 function complain more accurately. A few special modes are allowed
14421 without complaint for global variables (for instance, global
14422 register values and thread-local values).
c906108c
SS
14423
14424 A location description containing no operations indicates that the
4cecd739 14425 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14426 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14427 callers will only want a very basic result and this can become a
21ae7a4d
JK
14428 complaint.
14429
14430 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14431
14432static CORE_ADDR
e7c27a73 14433decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14434{
e7c27a73 14435 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14436 int i;
14437 int size = blk->size;
14438 gdb_byte *data = blk->data;
14439 CORE_ADDR stack[64];
14440 int stacki;
14441 unsigned int bytes_read, unsnd;
14442 gdb_byte op;
c906108c 14443
21ae7a4d
JK
14444 i = 0;
14445 stacki = 0;
14446 stack[stacki] = 0;
14447 stack[++stacki] = 0;
14448
14449 while (i < size)
14450 {
14451 op = data[i++];
14452 switch (op)
14453 {
14454 case DW_OP_lit0:
14455 case DW_OP_lit1:
14456 case DW_OP_lit2:
14457 case DW_OP_lit3:
14458 case DW_OP_lit4:
14459 case DW_OP_lit5:
14460 case DW_OP_lit6:
14461 case DW_OP_lit7:
14462 case DW_OP_lit8:
14463 case DW_OP_lit9:
14464 case DW_OP_lit10:
14465 case DW_OP_lit11:
14466 case DW_OP_lit12:
14467 case DW_OP_lit13:
14468 case DW_OP_lit14:
14469 case DW_OP_lit15:
14470 case DW_OP_lit16:
14471 case DW_OP_lit17:
14472 case DW_OP_lit18:
14473 case DW_OP_lit19:
14474 case DW_OP_lit20:
14475 case DW_OP_lit21:
14476 case DW_OP_lit22:
14477 case DW_OP_lit23:
14478 case DW_OP_lit24:
14479 case DW_OP_lit25:
14480 case DW_OP_lit26:
14481 case DW_OP_lit27:
14482 case DW_OP_lit28:
14483 case DW_OP_lit29:
14484 case DW_OP_lit30:
14485 case DW_OP_lit31:
14486 stack[++stacki] = op - DW_OP_lit0;
14487 break;
f1bea926 14488
21ae7a4d
JK
14489 case DW_OP_reg0:
14490 case DW_OP_reg1:
14491 case DW_OP_reg2:
14492 case DW_OP_reg3:
14493 case DW_OP_reg4:
14494 case DW_OP_reg5:
14495 case DW_OP_reg6:
14496 case DW_OP_reg7:
14497 case DW_OP_reg8:
14498 case DW_OP_reg9:
14499 case DW_OP_reg10:
14500 case DW_OP_reg11:
14501 case DW_OP_reg12:
14502 case DW_OP_reg13:
14503 case DW_OP_reg14:
14504 case DW_OP_reg15:
14505 case DW_OP_reg16:
14506 case DW_OP_reg17:
14507 case DW_OP_reg18:
14508 case DW_OP_reg19:
14509 case DW_OP_reg20:
14510 case DW_OP_reg21:
14511 case DW_OP_reg22:
14512 case DW_OP_reg23:
14513 case DW_OP_reg24:
14514 case DW_OP_reg25:
14515 case DW_OP_reg26:
14516 case DW_OP_reg27:
14517 case DW_OP_reg28:
14518 case DW_OP_reg29:
14519 case DW_OP_reg30:
14520 case DW_OP_reg31:
14521 stack[++stacki] = op - DW_OP_reg0;
14522 if (i < size)
14523 dwarf2_complex_location_expr_complaint ();
14524 break;
c906108c 14525
21ae7a4d
JK
14526 case DW_OP_regx:
14527 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14528 i += bytes_read;
14529 stack[++stacki] = unsnd;
14530 if (i < size)
14531 dwarf2_complex_location_expr_complaint ();
14532 break;
c906108c 14533
21ae7a4d
JK
14534 case DW_OP_addr:
14535 stack[++stacki] = read_address (objfile->obfd, &data[i],
14536 cu, &bytes_read);
14537 i += bytes_read;
14538 break;
d53d4ac5 14539
21ae7a4d
JK
14540 case DW_OP_const1u:
14541 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14542 i += 1;
14543 break;
14544
14545 case DW_OP_const1s:
14546 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14547 i += 1;
14548 break;
14549
14550 case DW_OP_const2u:
14551 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14552 i += 2;
14553 break;
14554
14555 case DW_OP_const2s:
14556 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14557 i += 2;
14558 break;
d53d4ac5 14559
21ae7a4d
JK
14560 case DW_OP_const4u:
14561 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14562 i += 4;
14563 break;
14564
14565 case DW_OP_const4s:
14566 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14567 i += 4;
14568 break;
14569
585861ea
JK
14570 case DW_OP_const8u:
14571 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14572 i += 8;
14573 break;
14574
21ae7a4d
JK
14575 case DW_OP_constu:
14576 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14577 &bytes_read);
14578 i += bytes_read;
14579 break;
14580
14581 case DW_OP_consts:
14582 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14583 i += bytes_read;
14584 break;
14585
14586 case DW_OP_dup:
14587 stack[stacki + 1] = stack[stacki];
14588 stacki++;
14589 break;
14590
14591 case DW_OP_plus:
14592 stack[stacki - 1] += stack[stacki];
14593 stacki--;
14594 break;
14595
14596 case DW_OP_plus_uconst:
14597 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14598 &bytes_read);
14599 i += bytes_read;
14600 break;
14601
14602 case DW_OP_minus:
14603 stack[stacki - 1] -= stack[stacki];
14604 stacki--;
14605 break;
14606
14607 case DW_OP_deref:
14608 /* If we're not the last op, then we definitely can't encode
14609 this using GDB's address_class enum. This is valid for partial
14610 global symbols, although the variable's address will be bogus
14611 in the psymtab. */
14612 if (i < size)
14613 dwarf2_complex_location_expr_complaint ();
14614 break;
14615
14616 case DW_OP_GNU_push_tls_address:
14617 /* The top of the stack has the offset from the beginning
14618 of the thread control block at which the variable is located. */
14619 /* Nothing should follow this operator, so the top of stack would
14620 be returned. */
14621 /* This is valid for partial global symbols, but the variable's
585861ea
JK
14622 address will be bogus in the psymtab. Make it always at least
14623 non-zero to not look as a variable garbage collected by linker
14624 which have DW_OP_addr 0. */
21ae7a4d
JK
14625 if (i < size)
14626 dwarf2_complex_location_expr_complaint ();
585861ea 14627 stack[stacki]++;
21ae7a4d
JK
14628 break;
14629
14630 case DW_OP_GNU_uninit:
14631 break;
14632
14633 default:
14634 {
14635 const char *name = dwarf_stack_op_name (op);
14636
14637 if (name)
14638 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14639 name);
14640 else
14641 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14642 op);
14643 }
14644
14645 return (stack[stacki]);
d53d4ac5 14646 }
3c6e0cb3 14647
21ae7a4d
JK
14648 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14649 outside of the allocated space. Also enforce minimum>0. */
14650 if (stacki >= ARRAY_SIZE (stack) - 1)
14651 {
14652 complaint (&symfile_complaints,
14653 _("location description stack overflow"));
14654 return 0;
14655 }
14656
14657 if (stacki <= 0)
14658 {
14659 complaint (&symfile_complaints,
14660 _("location description stack underflow"));
14661 return 0;
14662 }
14663 }
14664 return (stack[stacki]);
c906108c
SS
14665}
14666
14667/* memory allocation interface */
14668
c906108c 14669static struct dwarf_block *
7b5a2f43 14670dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14671{
14672 struct dwarf_block *blk;
14673
14674 blk = (struct dwarf_block *)
7b5a2f43 14675 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14676 return (blk);
14677}
14678
14679static struct abbrev_info *
f3dd6933 14680dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14681{
14682 struct abbrev_info *abbrev;
14683
f3dd6933
DJ
14684 abbrev = (struct abbrev_info *)
14685 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14686 memset (abbrev, 0, sizeof (struct abbrev_info));
14687 return (abbrev);
14688}
14689
14690static struct die_info *
b60c80d6 14691dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14692{
14693 struct die_info *die;
b60c80d6
DJ
14694 size_t size = sizeof (struct die_info);
14695
14696 if (num_attrs > 1)
14697 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14698
b60c80d6 14699 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14700 memset (die, 0, sizeof (struct die_info));
14701 return (die);
14702}
2e276125
JB
14703
14704\f
14705/* Macro support. */
14706
2e276125
JB
14707/* Return the full name of file number I in *LH's file name table.
14708 Use COMP_DIR as the name of the current directory of the
14709 compilation. The result is allocated using xmalloc; the caller is
14710 responsible for freeing it. */
14711static char *
14712file_full_name (int file, struct line_header *lh, const char *comp_dir)
14713{
6a83a1e6
EZ
14714 /* Is the file number a valid index into the line header's file name
14715 table? Remember that file numbers start with one, not zero. */
14716 if (1 <= file && file <= lh->num_file_names)
14717 {
14718 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14719
6a83a1e6
EZ
14720 if (IS_ABSOLUTE_PATH (fe->name))
14721 return xstrdup (fe->name);
14722 else
14723 {
14724 const char *dir;
14725 int dir_len;
14726 char *full_name;
14727
14728 if (fe->dir_index)
14729 dir = lh->include_dirs[fe->dir_index - 1];
14730 else
14731 dir = comp_dir;
14732
14733 if (dir)
14734 {
14735 dir_len = strlen (dir);
14736 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14737 strcpy (full_name, dir);
14738 full_name[dir_len] = '/';
14739 strcpy (full_name + dir_len + 1, fe->name);
14740 return full_name;
14741 }
14742 else
14743 return xstrdup (fe->name);
14744 }
14745 }
2e276125
JB
14746 else
14747 {
6a83a1e6
EZ
14748 /* The compiler produced a bogus file number. We can at least
14749 record the macro definitions made in the file, even if we
14750 won't be able to find the file by name. */
14751 char fake_name[80];
9a619af0 14752
6a83a1e6 14753 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14754
6e70227d 14755 complaint (&symfile_complaints,
6a83a1e6
EZ
14756 _("bad file number in macro information (%d)"),
14757 file);
2e276125 14758
6a83a1e6 14759 return xstrdup (fake_name);
2e276125
JB
14760 }
14761}
14762
14763
14764static struct macro_source_file *
14765macro_start_file (int file, int line,
14766 struct macro_source_file *current_file,
14767 const char *comp_dir,
14768 struct line_header *lh, struct objfile *objfile)
14769{
14770 /* The full name of this source file. */
14771 char *full_name = file_full_name (file, lh, comp_dir);
14772
14773 /* We don't create a macro table for this compilation unit
14774 at all until we actually get a filename. */
14775 if (! pending_macros)
4a146b47 14776 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14777 objfile->macro_cache);
2e276125
JB
14778
14779 if (! current_file)
14780 /* If we have no current file, then this must be the start_file
14781 directive for the compilation unit's main source file. */
14782 current_file = macro_set_main (pending_macros, full_name);
14783 else
14784 current_file = macro_include (current_file, line, full_name);
14785
14786 xfree (full_name);
6e70227d 14787
2e276125
JB
14788 return current_file;
14789}
14790
14791
14792/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14793 followed by a null byte. */
14794static char *
14795copy_string (const char *buf, int len)
14796{
14797 char *s = xmalloc (len + 1);
9a619af0 14798
2e276125
JB
14799 memcpy (s, buf, len);
14800 s[len] = '\0';
2e276125
JB
14801 return s;
14802}
14803
14804
14805static const char *
14806consume_improper_spaces (const char *p, const char *body)
14807{
14808 if (*p == ' ')
14809 {
4d3c2250 14810 complaint (&symfile_complaints,
3e43a32a
MS
14811 _("macro definition contains spaces "
14812 "in formal argument list:\n`%s'"),
4d3c2250 14813 body);
2e276125
JB
14814
14815 while (*p == ' ')
14816 p++;
14817 }
14818
14819 return p;
14820}
14821
14822
14823static void
14824parse_macro_definition (struct macro_source_file *file, int line,
14825 const char *body)
14826{
14827 const char *p;
14828
14829 /* The body string takes one of two forms. For object-like macro
14830 definitions, it should be:
14831
14832 <macro name> " " <definition>
14833
14834 For function-like macro definitions, it should be:
14835
14836 <macro name> "() " <definition>
14837 or
14838 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14839
14840 Spaces may appear only where explicitly indicated, and in the
14841 <definition>.
14842
14843 The Dwarf 2 spec says that an object-like macro's name is always
14844 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14845 the space when the macro's definition is the empty string.
2e276125
JB
14846
14847 The Dwarf 2 spec says that there should be no spaces between the
14848 formal arguments in a function-like macro's formal argument list,
14849 but versions of GCC around March 2002 include spaces after the
14850 commas. */
14851
14852
14853 /* Find the extent of the macro name. The macro name is terminated
14854 by either a space or null character (for an object-like macro) or
14855 an opening paren (for a function-like macro). */
14856 for (p = body; *p; p++)
14857 if (*p == ' ' || *p == '(')
14858 break;
14859
14860 if (*p == ' ' || *p == '\0')
14861 {
14862 /* It's an object-like macro. */
14863 int name_len = p - body;
14864 char *name = copy_string (body, name_len);
14865 const char *replacement;
14866
14867 if (*p == ' ')
14868 replacement = body + name_len + 1;
14869 else
14870 {
4d3c2250 14871 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14872 replacement = body + name_len;
14873 }
6e70227d 14874
2e276125
JB
14875 macro_define_object (file, line, name, replacement);
14876
14877 xfree (name);
14878 }
14879 else if (*p == '(')
14880 {
14881 /* It's a function-like macro. */
14882 char *name = copy_string (body, p - body);
14883 int argc = 0;
14884 int argv_size = 1;
14885 char **argv = xmalloc (argv_size * sizeof (*argv));
14886
14887 p++;
14888
14889 p = consume_improper_spaces (p, body);
14890
14891 /* Parse the formal argument list. */
14892 while (*p && *p != ')')
14893 {
14894 /* Find the extent of the current argument name. */
14895 const char *arg_start = p;
14896
14897 while (*p && *p != ',' && *p != ')' && *p != ' ')
14898 p++;
14899
14900 if (! *p || p == arg_start)
4d3c2250 14901 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14902 else
14903 {
14904 /* Make sure argv has room for the new argument. */
14905 if (argc >= argv_size)
14906 {
14907 argv_size *= 2;
14908 argv = xrealloc (argv, argv_size * sizeof (*argv));
14909 }
14910
14911 argv[argc++] = copy_string (arg_start, p - arg_start);
14912 }
14913
14914 p = consume_improper_spaces (p, body);
14915
14916 /* Consume the comma, if present. */
14917 if (*p == ',')
14918 {
14919 p++;
14920
14921 p = consume_improper_spaces (p, body);
14922 }
14923 }
14924
14925 if (*p == ')')
14926 {
14927 p++;
14928
14929 if (*p == ' ')
14930 /* Perfectly formed definition, no complaints. */
14931 macro_define_function (file, line, name,
6e70227d 14932 argc, (const char **) argv,
2e276125
JB
14933 p + 1);
14934 else if (*p == '\0')
14935 {
14936 /* Complain, but do define it. */
4d3c2250 14937 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14938 macro_define_function (file, line, name,
6e70227d 14939 argc, (const char **) argv,
2e276125
JB
14940 p);
14941 }
14942 else
14943 /* Just complain. */
4d3c2250 14944 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14945 }
14946 else
14947 /* Just complain. */
4d3c2250 14948 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14949
14950 xfree (name);
14951 {
14952 int i;
14953
14954 for (i = 0; i < argc; i++)
14955 xfree (argv[i]);
14956 }
14957 xfree (argv);
14958 }
14959 else
4d3c2250 14960 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14961}
14962
cf2c3c16
TT
14963/* Skip some bytes from BYTES according to the form given in FORM.
14964 Returns the new pointer. */
2e276125 14965
cf2c3c16
TT
14966static gdb_byte *
14967skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14968 enum dwarf_form form,
14969 unsigned int offset_size,
14970 struct dwarf2_section_info *section)
2e276125 14971{
cf2c3c16 14972 unsigned int bytes_read;
2e276125 14973
cf2c3c16 14974 switch (form)
2e276125 14975 {
cf2c3c16
TT
14976 case DW_FORM_data1:
14977 case DW_FORM_flag:
14978 ++bytes;
14979 break;
14980
14981 case DW_FORM_data2:
14982 bytes += 2;
14983 break;
14984
14985 case DW_FORM_data4:
14986 bytes += 4;
14987 break;
14988
14989 case DW_FORM_data8:
14990 bytes += 8;
14991 break;
14992
14993 case DW_FORM_string:
14994 read_direct_string (abfd, bytes, &bytes_read);
14995 bytes += bytes_read;
14996 break;
14997
14998 case DW_FORM_sec_offset:
14999 case DW_FORM_strp:
15000 bytes += offset_size;
15001 break;
15002
15003 case DW_FORM_block:
15004 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15005 bytes += bytes_read;
15006 break;
15007
15008 case DW_FORM_block1:
15009 bytes += 1 + read_1_byte (abfd, bytes);
15010 break;
15011 case DW_FORM_block2:
15012 bytes += 2 + read_2_bytes (abfd, bytes);
15013 break;
15014 case DW_FORM_block4:
15015 bytes += 4 + read_4_bytes (abfd, bytes);
15016 break;
15017
15018 case DW_FORM_sdata:
15019 case DW_FORM_udata:
15020 bytes = skip_leb128 (abfd, bytes);
15021 break;
15022
15023 default:
15024 {
15025 complain:
15026 complaint (&symfile_complaints,
15027 _("invalid form 0x%x in `%s'"),
15028 form,
15029 section->asection->name);
15030 return NULL;
15031 }
2e276125
JB
15032 }
15033
cf2c3c16
TT
15034 return bytes;
15035}
757a13d0 15036
cf2c3c16
TT
15037/* A helper for dwarf_decode_macros that handles skipping an unknown
15038 opcode. Returns an updated pointer to the macro data buffer; or,
15039 on error, issues a complaint and returns NULL. */
757a13d0 15040
cf2c3c16
TT
15041static gdb_byte *
15042skip_unknown_opcode (unsigned int opcode,
15043 gdb_byte **opcode_definitions,
15044 gdb_byte *mac_ptr,
15045 bfd *abfd,
15046 unsigned int offset_size,
15047 struct dwarf2_section_info *section)
15048{
15049 unsigned int bytes_read, i;
15050 unsigned long arg;
15051 gdb_byte *defn;
2e276125 15052
cf2c3c16 15053 if (opcode_definitions[opcode] == NULL)
2e276125 15054 {
cf2c3c16
TT
15055 complaint (&symfile_complaints,
15056 _("unrecognized DW_MACFINO opcode 0x%x"),
15057 opcode);
15058 return NULL;
15059 }
2e276125 15060
cf2c3c16
TT
15061 defn = opcode_definitions[opcode];
15062 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15063 defn += bytes_read;
2e276125 15064
cf2c3c16
TT
15065 for (i = 0; i < arg; ++i)
15066 {
15067 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15068 if (mac_ptr == NULL)
15069 {
15070 /* skip_form_bytes already issued the complaint. */
15071 return NULL;
15072 }
15073 }
757a13d0 15074
cf2c3c16
TT
15075 return mac_ptr;
15076}
757a13d0 15077
cf2c3c16
TT
15078/* A helper function which parses the header of a macro section.
15079 If the macro section is the extended (for now called "GNU") type,
15080 then this updates *OFFSET_SIZE. Returns a pointer to just after
15081 the header, or issues a complaint and returns NULL on error. */
757a13d0 15082
cf2c3c16
TT
15083static gdb_byte *
15084dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15085 bfd *abfd,
15086 gdb_byte *mac_ptr,
15087 unsigned int *offset_size,
15088 int section_is_gnu)
15089{
15090 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15091
cf2c3c16
TT
15092 if (section_is_gnu)
15093 {
15094 unsigned int version, flags;
757a13d0 15095
cf2c3c16
TT
15096 version = read_2_bytes (abfd, mac_ptr);
15097 if (version != 4)
15098 {
15099 complaint (&symfile_complaints,
15100 _("unrecognized version `%d' in .debug_macro section"),
15101 version);
15102 return NULL;
15103 }
15104 mac_ptr += 2;
757a13d0 15105
cf2c3c16
TT
15106 flags = read_1_byte (abfd, mac_ptr);
15107 ++mac_ptr;
15108 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15109
cf2c3c16
TT
15110 if ((flags & 2) != 0)
15111 /* We don't need the line table offset. */
15112 mac_ptr += *offset_size;
757a13d0 15113
cf2c3c16
TT
15114 /* Vendor opcode descriptions. */
15115 if ((flags & 4) != 0)
15116 {
15117 unsigned int i, count;
757a13d0 15118
cf2c3c16
TT
15119 count = read_1_byte (abfd, mac_ptr);
15120 ++mac_ptr;
15121 for (i = 0; i < count; ++i)
15122 {
15123 unsigned int opcode, bytes_read;
15124 unsigned long arg;
15125
15126 opcode = read_1_byte (abfd, mac_ptr);
15127 ++mac_ptr;
15128 opcode_definitions[opcode] = mac_ptr;
15129 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15130 mac_ptr += bytes_read;
15131 mac_ptr += arg;
15132 }
757a13d0 15133 }
cf2c3c16 15134 }
757a13d0 15135
cf2c3c16
TT
15136 return mac_ptr;
15137}
757a13d0 15138
cf2c3c16
TT
15139/* A helper for dwarf_decode_macros that handles the GNU extensions,
15140 including DW_GNU_MACINFO_transparent_include. */
15141
15142static void
15143dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15144 struct macro_source_file *current_file,
15145 struct line_header *lh, char *comp_dir,
15146 struct dwarf2_section_info *section,
15147 int section_is_gnu,
15148 unsigned int offset_size,
15149 struct objfile *objfile)
15150{
15151 enum dwarf_macro_record_type macinfo_type;
15152 int at_commandline;
15153 gdb_byte *opcode_definitions[256];
757a13d0 15154
cf2c3c16
TT
15155 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15156 &offset_size, section_is_gnu);
15157 if (mac_ptr == NULL)
15158 {
15159 /* We already issued a complaint. */
15160 return;
15161 }
757a13d0
JK
15162
15163 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15164 GDB is still reading the definitions from command line. First
15165 DW_MACINFO_start_file will need to be ignored as it was already executed
15166 to create CURRENT_FILE for the main source holding also the command line
15167 definitions. On first met DW_MACINFO_start_file this flag is reset to
15168 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15169
15170 at_commandline = 1;
15171
15172 do
15173 {
15174 /* Do we at least have room for a macinfo type byte? */
15175 if (mac_ptr >= mac_end)
15176 {
cf2c3c16 15177 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15178 break;
15179 }
15180
15181 macinfo_type = read_1_byte (abfd, mac_ptr);
15182 mac_ptr++;
15183
cf2c3c16
TT
15184 /* Note that we rely on the fact that the corresponding GNU and
15185 DWARF constants are the same. */
757a13d0
JK
15186 switch (macinfo_type)
15187 {
15188 /* A zero macinfo type indicates the end of the macro
15189 information. */
15190 case 0:
15191 break;
2e276125 15192
cf2c3c16
TT
15193 case DW_MACRO_GNU_define:
15194 case DW_MACRO_GNU_undef:
15195 case DW_MACRO_GNU_define_indirect:
15196 case DW_MACRO_GNU_undef_indirect:
2e276125 15197 {
891d2f0b 15198 unsigned int bytes_read;
2e276125
JB
15199 int line;
15200 char *body;
cf2c3c16 15201 int is_define;
2e276125 15202
cf2c3c16
TT
15203 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15204 mac_ptr += bytes_read;
15205
15206 if (macinfo_type == DW_MACRO_GNU_define
15207 || macinfo_type == DW_MACRO_GNU_undef)
15208 {
15209 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15210 mac_ptr += bytes_read;
15211 }
15212 else
15213 {
15214 LONGEST str_offset;
15215
15216 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15217 mac_ptr += offset_size;
2e276125 15218
cf2c3c16
TT
15219 body = read_indirect_string_at_offset (abfd, str_offset);
15220 }
15221
15222 is_define = (macinfo_type == DW_MACRO_GNU_define
15223 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15224 if (! current_file)
757a13d0
JK
15225 {
15226 /* DWARF violation as no main source is present. */
15227 complaint (&symfile_complaints,
15228 _("debug info with no main source gives macro %s "
15229 "on line %d: %s"),
cf2c3c16
TT
15230 is_define ? _("definition") : _("undefinition"),
15231 line, body);
757a13d0
JK
15232 break;
15233 }
3e43a32a
MS
15234 if ((line == 0 && !at_commandline)
15235 || (line != 0 && at_commandline))
4d3c2250 15236 complaint (&symfile_complaints,
757a13d0
JK
15237 _("debug info gives %s macro %s with %s line %d: %s"),
15238 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15239 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15240 line == 0 ? _("zero") : _("non-zero"), line, body);
15241
cf2c3c16 15242 if (is_define)
757a13d0 15243 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15244 else
15245 {
15246 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15247 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15248 macro_undef (current_file, line, body);
15249 }
2e276125
JB
15250 }
15251 break;
15252
cf2c3c16 15253 case DW_MACRO_GNU_start_file:
2e276125 15254 {
891d2f0b 15255 unsigned int bytes_read;
2e276125
JB
15256 int line, file;
15257
15258 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15259 mac_ptr += bytes_read;
15260 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15261 mac_ptr += bytes_read;
15262
3e43a32a
MS
15263 if ((line == 0 && !at_commandline)
15264 || (line != 0 && at_commandline))
757a13d0
JK
15265 complaint (&symfile_complaints,
15266 _("debug info gives source %d included "
15267 "from %s at %s line %d"),
15268 file, at_commandline ? _("command-line") : _("file"),
15269 line == 0 ? _("zero") : _("non-zero"), line);
15270
15271 if (at_commandline)
15272 {
cf2c3c16
TT
15273 /* This DW_MACRO_GNU_start_file was executed in the
15274 pass one. */
757a13d0
JK
15275 at_commandline = 0;
15276 }
15277 else
15278 current_file = macro_start_file (file, line,
15279 current_file, comp_dir,
cf2c3c16 15280 lh, objfile);
2e276125
JB
15281 }
15282 break;
15283
cf2c3c16 15284 case DW_MACRO_GNU_end_file:
2e276125 15285 if (! current_file)
4d3c2250 15286 complaint (&symfile_complaints,
3e43a32a
MS
15287 _("macro debug info has an unmatched "
15288 "`close_file' directive"));
2e276125
JB
15289 else
15290 {
15291 current_file = current_file->included_by;
15292 if (! current_file)
15293 {
cf2c3c16 15294 enum dwarf_macro_record_type next_type;
2e276125
JB
15295
15296 /* GCC circa March 2002 doesn't produce the zero
15297 type byte marking the end of the compilation
15298 unit. Complain if it's not there, but exit no
15299 matter what. */
15300
15301 /* Do we at least have room for a macinfo type byte? */
15302 if (mac_ptr >= mac_end)
15303 {
cf2c3c16 15304 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15305 return;
15306 }
15307
15308 /* We don't increment mac_ptr here, so this is just
15309 a look-ahead. */
15310 next_type = read_1_byte (abfd, mac_ptr);
15311 if (next_type != 0)
4d3c2250 15312 complaint (&symfile_complaints,
3e43a32a
MS
15313 _("no terminating 0-type entry for "
15314 "macros in `.debug_macinfo' section"));
2e276125
JB
15315
15316 return;
15317 }
15318 }
15319 break;
15320
cf2c3c16
TT
15321 case DW_MACRO_GNU_transparent_include:
15322 {
15323 LONGEST offset;
15324
15325 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15326 mac_ptr += offset_size;
15327
15328 dwarf_decode_macro_bytes (abfd,
15329 section->buffer + offset,
15330 mac_end, current_file,
15331 lh, comp_dir,
15332 section, section_is_gnu,
15333 offset_size, objfile);
15334 }
15335 break;
15336
2e276125 15337 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15338 if (!section_is_gnu)
15339 {
15340 unsigned int bytes_read;
15341 int constant;
2e276125 15342
cf2c3c16
TT
15343 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15344 mac_ptr += bytes_read;
15345 read_direct_string (abfd, mac_ptr, &bytes_read);
15346 mac_ptr += bytes_read;
2e276125 15347
cf2c3c16
TT
15348 /* We don't recognize any vendor extensions. */
15349 break;
15350 }
15351 /* FALLTHROUGH */
15352
15353 default:
15354 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15355 mac_ptr, abfd, offset_size,
15356 section);
15357 if (mac_ptr == NULL)
15358 return;
15359 break;
2e276125 15360 }
757a13d0 15361 } while (macinfo_type != 0);
2e276125 15362}
8e19ed76 15363
cf2c3c16
TT
15364static void
15365dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15366 char *comp_dir, bfd *abfd,
15367 struct dwarf2_cu *cu,
15368 struct dwarf2_section_info *section,
15369 int section_is_gnu)
15370{
bb5ed363 15371 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
15372 gdb_byte *mac_ptr, *mac_end;
15373 struct macro_source_file *current_file = 0;
15374 enum dwarf_macro_record_type macinfo_type;
15375 unsigned int offset_size = cu->header.offset_size;
15376 gdb_byte *opcode_definitions[256];
15377
bb5ed363 15378 dwarf2_read_section (objfile, section);
cf2c3c16
TT
15379 if (section->buffer == NULL)
15380 {
15381 complaint (&symfile_complaints, _("missing %s section"),
15382 section->asection->name);
15383 return;
15384 }
15385
15386 /* First pass: Find the name of the base filename.
15387 This filename is needed in order to process all macros whose definition
15388 (or undefinition) comes from the command line. These macros are defined
15389 before the first DW_MACINFO_start_file entry, and yet still need to be
15390 associated to the base file.
15391
15392 To determine the base file name, we scan the macro definitions until we
15393 reach the first DW_MACINFO_start_file entry. We then initialize
15394 CURRENT_FILE accordingly so that any macro definition found before the
15395 first DW_MACINFO_start_file can still be associated to the base file. */
15396
15397 mac_ptr = section->buffer + offset;
15398 mac_end = section->buffer + section->size;
15399
15400 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15401 &offset_size, section_is_gnu);
15402 if (mac_ptr == NULL)
15403 {
15404 /* We already issued a complaint. */
15405 return;
15406 }
15407
15408 do
15409 {
15410 /* Do we at least have room for a macinfo type byte? */
15411 if (mac_ptr >= mac_end)
15412 {
15413 /* Complaint is printed during the second pass as GDB will probably
15414 stop the first pass earlier upon finding
15415 DW_MACINFO_start_file. */
15416 break;
15417 }
15418
15419 macinfo_type = read_1_byte (abfd, mac_ptr);
15420 mac_ptr++;
15421
15422 /* Note that we rely on the fact that the corresponding GNU and
15423 DWARF constants are the same. */
15424 switch (macinfo_type)
15425 {
15426 /* A zero macinfo type indicates the end of the macro
15427 information. */
15428 case 0:
15429 break;
15430
15431 case DW_MACRO_GNU_define:
15432 case DW_MACRO_GNU_undef:
15433 /* Only skip the data by MAC_PTR. */
15434 {
15435 unsigned int bytes_read;
15436
15437 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15438 mac_ptr += bytes_read;
15439 read_direct_string (abfd, mac_ptr, &bytes_read);
15440 mac_ptr += bytes_read;
15441 }
15442 break;
15443
15444 case DW_MACRO_GNU_start_file:
15445 {
15446 unsigned int bytes_read;
15447 int line, file;
15448
15449 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15450 mac_ptr += bytes_read;
15451 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15452 mac_ptr += bytes_read;
15453
15454 current_file = macro_start_file (file, line, current_file,
bb5ed363 15455 comp_dir, lh, objfile);
cf2c3c16
TT
15456 }
15457 break;
15458
15459 case DW_MACRO_GNU_end_file:
15460 /* No data to skip by MAC_PTR. */
15461 break;
15462
15463 case DW_MACRO_GNU_define_indirect:
15464 case DW_MACRO_GNU_undef_indirect:
15465 {
15466 unsigned int bytes_read;
15467
15468 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15469 mac_ptr += bytes_read;
15470 mac_ptr += offset_size;
15471 }
15472 break;
15473
15474 case DW_MACRO_GNU_transparent_include:
15475 /* Note that, according to the spec, a transparent include
15476 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15477 skip this opcode. */
15478 mac_ptr += offset_size;
15479 break;
15480
15481 case DW_MACINFO_vendor_ext:
15482 /* Only skip the data by MAC_PTR. */
15483 if (!section_is_gnu)
15484 {
15485 unsigned int bytes_read;
15486
15487 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15488 mac_ptr += bytes_read;
15489 read_direct_string (abfd, mac_ptr, &bytes_read);
15490 mac_ptr += bytes_read;
15491 }
15492 /* FALLTHROUGH */
15493
15494 default:
15495 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15496 mac_ptr, abfd, offset_size,
15497 section);
15498 if (mac_ptr == NULL)
15499 return;
15500 break;
15501 }
15502 } while (macinfo_type != 0 && current_file == NULL);
15503
15504 /* Second pass: Process all entries.
15505
15506 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15507 command-line macro definitions/undefinitions. This flag is unset when we
15508 reach the first DW_MACINFO_start_file entry. */
15509
15510 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15511 current_file, lh, comp_dir, section, section_is_gnu,
bb5ed363 15512 offset_size, objfile);
cf2c3c16
TT
15513}
15514
8e19ed76 15515/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15516 if so return true else false. */
8e19ed76
PS
15517static int
15518attr_form_is_block (struct attribute *attr)
15519{
15520 return (attr == NULL ? 0 :
15521 attr->form == DW_FORM_block1
15522 || attr->form == DW_FORM_block2
15523 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15524 || attr->form == DW_FORM_block
15525 || attr->form == DW_FORM_exprloc);
8e19ed76 15526}
4c2df51b 15527
c6a0999f
JB
15528/* Return non-zero if ATTR's value is a section offset --- classes
15529 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15530 You may use DW_UNSND (attr) to retrieve such offsets.
15531
15532 Section 7.5.4, "Attribute Encodings", explains that no attribute
15533 may have a value that belongs to more than one of these classes; it
15534 would be ambiguous if we did, because we use the same forms for all
15535 of them. */
3690dd37
JB
15536static int
15537attr_form_is_section_offset (struct attribute *attr)
15538{
15539 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15540 || attr->form == DW_FORM_data8
15541 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15542}
15543
15544
15545/* Return non-zero if ATTR's value falls in the 'constant' class, or
15546 zero otherwise. When this function returns true, you can apply
15547 dwarf2_get_attr_constant_value to it.
15548
15549 However, note that for some attributes you must check
15550 attr_form_is_section_offset before using this test. DW_FORM_data4
15551 and DW_FORM_data8 are members of both the constant class, and of
15552 the classes that contain offsets into other debug sections
15553 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15554 that, if an attribute's can be either a constant or one of the
15555 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15556 taken as section offsets, not constants. */
15557static int
15558attr_form_is_constant (struct attribute *attr)
15559{
15560 switch (attr->form)
15561 {
15562 case DW_FORM_sdata:
15563 case DW_FORM_udata:
15564 case DW_FORM_data1:
15565 case DW_FORM_data2:
15566 case DW_FORM_data4:
15567 case DW_FORM_data8:
15568 return 1;
15569 default:
15570 return 0;
15571 }
15572}
15573
8cf6f0b1
TT
15574/* A helper function that fills in a dwarf2_loclist_baton. */
15575
15576static void
15577fill_in_loclist_baton (struct dwarf2_cu *cu,
15578 struct dwarf2_loclist_baton *baton,
15579 struct attribute *attr)
15580{
15581 dwarf2_read_section (dwarf2_per_objfile->objfile,
15582 &dwarf2_per_objfile->loc);
15583
15584 baton->per_cu = cu->per_cu;
15585 gdb_assert (baton->per_cu);
15586 /* We don't know how long the location list is, but make sure we
15587 don't run off the edge of the section. */
15588 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15589 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15590 baton->base_address = cu->base_address;
15591}
15592
4c2df51b
DJ
15593static void
15594dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15595 struct dwarf2_cu *cu)
4c2df51b 15596{
bb5ed363
DE
15597 struct objfile *objfile = dwarf2_per_objfile->objfile;
15598
3690dd37 15599 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15600 /* ".debug_loc" may not exist at all, or the offset may be outside
15601 the section. If so, fall through to the complaint in the
15602 other branch. */
bb5ed363 15603 && DW_UNSND (attr) < dwarf2_section_size (objfile,
9e0ac564 15604 &dwarf2_per_objfile->loc))
4c2df51b 15605 {
0d53c4c4 15606 struct dwarf2_loclist_baton *baton;
4c2df51b 15607
bb5ed363 15608 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15609 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15610
8cf6f0b1 15611 fill_in_loclist_baton (cu, baton, attr);
be391dca 15612
d00adf39 15613 if (cu->base_known == 0)
0d53c4c4 15614 complaint (&symfile_complaints,
3e43a32a
MS
15615 _("Location list used without "
15616 "specifying the CU base address."));
4c2df51b 15617
768a979c 15618 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15619 SYMBOL_LOCATION_BATON (sym) = baton;
15620 }
15621 else
15622 {
15623 struct dwarf2_locexpr_baton *baton;
15624
bb5ed363 15625 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 15626 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15627 baton->per_cu = cu->per_cu;
15628 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15629
15630 if (attr_form_is_block (attr))
15631 {
15632 /* Note that we're just copying the block's data pointer
15633 here, not the actual data. We're still pointing into the
6502dd73
DJ
15634 info_buffer for SYM's objfile; right now we never release
15635 that buffer, but when we do clean up properly this may
15636 need to change. */
0d53c4c4
DJ
15637 baton->size = DW_BLOCK (attr)->size;
15638 baton->data = DW_BLOCK (attr)->data;
15639 }
15640 else
15641 {
15642 dwarf2_invalid_attrib_class_complaint ("location description",
15643 SYMBOL_NATURAL_NAME (sym));
15644 baton->size = 0;
0d53c4c4 15645 }
6e70227d 15646
768a979c 15647 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15648 SYMBOL_LOCATION_BATON (sym) = baton;
15649 }
4c2df51b 15650}
6502dd73 15651
9aa1f1e3
TT
15652/* Return the OBJFILE associated with the compilation unit CU. If CU
15653 came from a separate debuginfo file, then the master objfile is
15654 returned. */
ae0d2f24
UW
15655
15656struct objfile *
15657dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15658{
9291a0cd 15659 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15660
15661 /* Return the master objfile, so that we can report and look up the
15662 correct file containing this variable. */
15663 if (objfile->separate_debug_objfile_backlink)
15664 objfile = objfile->separate_debug_objfile_backlink;
15665
15666 return objfile;
15667}
15668
96408a79
SA
15669/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15670 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15671 CU_HEADERP first. */
15672
15673static const struct comp_unit_head *
15674per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15675 struct dwarf2_per_cu_data *per_cu)
15676{
15677 struct objfile *objfile;
15678 struct dwarf2_per_objfile *per_objfile;
15679 gdb_byte *info_ptr;
15680
15681 if (per_cu->cu)
15682 return &per_cu->cu->header;
15683
15684 objfile = per_cu->objfile;
15685 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15686 info_ptr = per_objfile->info.buffer + per_cu->offset;
15687
15688 memset (cu_headerp, 0, sizeof (*cu_headerp));
15689 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15690
15691 return cu_headerp;
15692}
15693
ae0d2f24
UW
15694/* Return the address size given in the compilation unit header for CU. */
15695
98714339 15696int
ae0d2f24
UW
15697dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15698{
96408a79
SA
15699 struct comp_unit_head cu_header_local;
15700 const struct comp_unit_head *cu_headerp;
c471e790 15701
96408a79
SA
15702 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15703
15704 return cu_headerp->addr_size;
ae0d2f24
UW
15705}
15706
9eae7c52
TT
15707/* Return the offset size given in the compilation unit header for CU. */
15708
15709int
15710dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15711{
96408a79
SA
15712 struct comp_unit_head cu_header_local;
15713 const struct comp_unit_head *cu_headerp;
9c6c53f7 15714
96408a79
SA
15715 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15716
15717 return cu_headerp->offset_size;
15718}
15719
15720/* See its dwarf2loc.h declaration. */
15721
15722int
15723dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15724{
15725 struct comp_unit_head cu_header_local;
15726 const struct comp_unit_head *cu_headerp;
15727
15728 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15729
15730 if (cu_headerp->version == 2)
15731 return cu_headerp->addr_size;
15732 else
15733 return cu_headerp->offset_size;
181cebd4
JK
15734}
15735
9aa1f1e3
TT
15736/* Return the text offset of the CU. The returned offset comes from
15737 this CU's objfile. If this objfile came from a separate debuginfo
15738 file, then the offset may be different from the corresponding
15739 offset in the parent objfile. */
15740
15741CORE_ADDR
15742dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15743{
bb3fa9d0 15744 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15745
15746 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15747}
15748
348e048f
DE
15749/* Locate the .debug_info compilation unit from CU's objfile which contains
15750 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15751
15752static struct dwarf2_per_cu_data *
c764a876 15753dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15754 struct objfile *objfile)
15755{
15756 struct dwarf2_per_cu_data *this_cu;
15757 int low, high;
15758
ae038cb0
DJ
15759 low = 0;
15760 high = dwarf2_per_objfile->n_comp_units - 1;
15761 while (high > low)
15762 {
15763 int mid = low + (high - low) / 2;
9a619af0 15764
ae038cb0
DJ
15765 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15766 high = mid;
15767 else
15768 low = mid + 1;
15769 }
15770 gdb_assert (low == high);
15771 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15772 {
10b3939b 15773 if (low == 0)
8a3fe4f8
AC
15774 error (_("Dwarf Error: could not find partial DIE containing "
15775 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15776 (long) offset, bfd_get_filename (objfile->obfd));
15777
ae038cb0
DJ
15778 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15779 return dwarf2_per_objfile->all_comp_units[low-1];
15780 }
15781 else
15782 {
15783 this_cu = dwarf2_per_objfile->all_comp_units[low];
15784 if (low == dwarf2_per_objfile->n_comp_units - 1
15785 && offset >= this_cu->offset + this_cu->length)
c764a876 15786 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15787 gdb_assert (offset < this_cu->offset + this_cu->length);
15788 return this_cu;
15789 }
15790}
15791
23745b47 15792/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 15793
9816fde3 15794static void
23745b47 15795init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 15796{
9816fde3 15797 memset (cu, 0, sizeof (*cu));
23745b47
DE
15798 per_cu->cu = cu;
15799 cu->per_cu = per_cu;
15800 cu->objfile = per_cu->objfile;
93311388 15801 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15802}
15803
15804/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15805
15806static void
15807prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15808{
15809 struct attribute *attr;
15810
15811 /* Set the language we're debugging. */
15812 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15813 if (attr)
15814 set_cu_language (DW_UNSND (attr), cu);
15815 else
9cded63f
TT
15816 {
15817 cu->language = language_minimal;
15818 cu->language_defn = language_def (cu->language);
15819 }
93311388
DE
15820}
15821
ae038cb0
DJ
15822/* Release one cached compilation unit, CU. We unlink it from the tree
15823 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15824 the caller is responsible for that.
15825 NOTE: DATA is a void * because this function is also used as a
15826 cleanup routine. */
ae038cb0
DJ
15827
15828static void
68dc6402 15829free_heap_comp_unit (void *data)
ae038cb0
DJ
15830{
15831 struct dwarf2_cu *cu = data;
15832
23745b47
DE
15833 gdb_assert (cu->per_cu != NULL);
15834 cu->per_cu->cu = NULL;
ae038cb0
DJ
15835 cu->per_cu = NULL;
15836
15837 obstack_free (&cu->comp_unit_obstack, NULL);
15838
15839 xfree (cu);
15840}
15841
72bf9492 15842/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15843 when we're finished with it. We can't free the pointer itself, but be
15844 sure to unlink it from the cache. Also release any associated storage
15845 and perform cache maintenance.
72bf9492
DJ
15846
15847 Only used during partial symbol parsing. */
15848
15849static void
15850free_stack_comp_unit (void *data)
15851{
15852 struct dwarf2_cu *cu = data;
15853
23745b47
DE
15854 gdb_assert (cu->per_cu != NULL);
15855 cu->per_cu->cu = NULL;
15856 cu->per_cu = NULL;
15857
72bf9492
DJ
15858 obstack_free (&cu->comp_unit_obstack, NULL);
15859 cu->partial_dies = NULL;
ae038cb0 15860
23745b47
DE
15861 /* The previous code only did this if per_cu != NULL.
15862 But that would always succeed, so now we just unconditionally do
15863 the aging. This seems like the wrong place to do such aging,
15864 but cleaning that up is left for later. */
15865 age_cached_comp_units ();
ae038cb0
DJ
15866}
15867
15868/* Free all cached compilation units. */
15869
15870static void
15871free_cached_comp_units (void *data)
15872{
15873 struct dwarf2_per_cu_data *per_cu, **last_chain;
15874
15875 per_cu = dwarf2_per_objfile->read_in_chain;
15876 last_chain = &dwarf2_per_objfile->read_in_chain;
15877 while (per_cu != NULL)
15878 {
15879 struct dwarf2_per_cu_data *next_cu;
15880
15881 next_cu = per_cu->cu->read_in_chain;
15882
68dc6402 15883 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
15884 *last_chain = next_cu;
15885
15886 per_cu = next_cu;
15887 }
15888}
15889
15890/* Increase the age counter on each cached compilation unit, and free
15891 any that are too old. */
15892
15893static void
15894age_cached_comp_units (void)
15895{
15896 struct dwarf2_per_cu_data *per_cu, **last_chain;
15897
15898 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15899 per_cu = dwarf2_per_objfile->read_in_chain;
15900 while (per_cu != NULL)
15901 {
15902 per_cu->cu->last_used ++;
15903 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15904 dwarf2_mark (per_cu->cu);
15905 per_cu = per_cu->cu->read_in_chain;
15906 }
15907
15908 per_cu = dwarf2_per_objfile->read_in_chain;
15909 last_chain = &dwarf2_per_objfile->read_in_chain;
15910 while (per_cu != NULL)
15911 {
15912 struct dwarf2_per_cu_data *next_cu;
15913
15914 next_cu = per_cu->cu->read_in_chain;
15915
15916 if (!per_cu->cu->mark)
15917 {
68dc6402 15918 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
15919 *last_chain = next_cu;
15920 }
15921 else
15922 last_chain = &per_cu->cu->read_in_chain;
15923
15924 per_cu = next_cu;
15925 }
15926}
15927
15928/* Remove a single compilation unit from the cache. */
15929
15930static void
15931free_one_cached_comp_unit (void *target_cu)
15932{
15933 struct dwarf2_per_cu_data *per_cu, **last_chain;
15934
15935 per_cu = dwarf2_per_objfile->read_in_chain;
15936 last_chain = &dwarf2_per_objfile->read_in_chain;
15937 while (per_cu != NULL)
15938 {
15939 struct dwarf2_per_cu_data *next_cu;
15940
15941 next_cu = per_cu->cu->read_in_chain;
15942
15943 if (per_cu->cu == target_cu)
15944 {
68dc6402 15945 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
15946 *last_chain = next_cu;
15947 break;
15948 }
15949 else
15950 last_chain = &per_cu->cu->read_in_chain;
15951
15952 per_cu = next_cu;
15953 }
15954}
15955
fe3e1990
DJ
15956/* Release all extra memory associated with OBJFILE. */
15957
15958void
15959dwarf2_free_objfile (struct objfile *objfile)
15960{
15961 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15962
15963 if (dwarf2_per_objfile == NULL)
15964 return;
15965
15966 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15967 free_cached_comp_units (NULL);
15968
7b9f3c50
DE
15969 if (dwarf2_per_objfile->quick_file_names_table)
15970 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 15971
fe3e1990
DJ
15972 /* Everything else should be on the objfile obstack. */
15973}
15974
1c379e20
DJ
15975/* A pair of DIE offset and GDB type pointer. We store these
15976 in a hash table separate from the DIEs, and preserve them
15977 when the DIEs are flushed out of cache. */
15978
15979struct dwarf2_offset_and_type
15980{
15981 unsigned int offset;
15982 struct type *type;
15983};
15984
15985/* Hash function for a dwarf2_offset_and_type. */
15986
15987static hashval_t
15988offset_and_type_hash (const void *item)
15989{
15990 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 15991
1c379e20
DJ
15992 return ofs->offset;
15993}
15994
15995/* Equality function for a dwarf2_offset_and_type. */
15996
15997static int
15998offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15999{
16000 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16001 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16002
1c379e20
DJ
16003 return ofs_lhs->offset == ofs_rhs->offset;
16004}
16005
16006/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16007 table if necessary. For convenience, return TYPE.
16008
16009 The DIEs reading must have careful ordering to:
16010 * Not cause infite loops trying to read in DIEs as a prerequisite for
16011 reading current DIE.
16012 * Not trying to dereference contents of still incompletely read in types
16013 while reading in other DIEs.
16014 * Enable referencing still incompletely read in types just by a pointer to
16015 the type without accessing its fields.
16016
16017 Therefore caller should follow these rules:
16018 * Try to fetch any prerequisite types we may need to build this DIE type
16019 before building the type and calling set_die_type.
e71ec853 16020 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16021 possible before fetching more types to complete the current type.
16022 * Make the type as complete as possible before fetching more types. */
1c379e20 16023
f792889a 16024static struct type *
1c379e20
DJ
16025set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16026{
16027 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
16028 struct objfile *objfile = cu->objfile;
16029 htab_t *type_hash_ptr;
1c379e20 16030
b4ba55a1
JB
16031 /* For Ada types, make sure that the gnat-specific data is always
16032 initialized (if not already set). There are a few types where
16033 we should not be doing so, because the type-specific area is
16034 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16035 where the type-specific area is used to store the floatformat).
16036 But this is not a problem, because the gnat-specific information
16037 is actually not needed for these types. */
16038 if (need_gnat_info (cu)
16039 && TYPE_CODE (type) != TYPE_CODE_FUNC
16040 && TYPE_CODE (type) != TYPE_CODE_FLT
16041 && !HAVE_GNAT_AUX_INFO (type))
16042 INIT_GNAT_SPECIFIC (type);
16043
b0df02fd 16044 if (cu->per_cu->debug_types_section)
673bfd45
DE
16045 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16046 else
16047 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16048
16049 if (*type_hash_ptr == NULL)
f792889a 16050 {
673bfd45
DE
16051 *type_hash_ptr
16052 = htab_create_alloc_ex (127,
f792889a
DJ
16053 offset_and_type_hash,
16054 offset_and_type_eq,
16055 NULL,
673bfd45 16056 &objfile->objfile_obstack,
f792889a
DJ
16057 hashtab_obstack_allocate,
16058 dummy_obstack_deallocate);
f792889a 16059 }
1c379e20
DJ
16060
16061 ofs.offset = die->offset;
16062 ofs.type = type;
16063 slot = (struct dwarf2_offset_and_type **)
673bfd45 16064 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
16065 if (*slot)
16066 complaint (&symfile_complaints,
16067 _("A problem internal to GDB: DIE 0x%x has type already set"),
16068 die->offset);
673bfd45 16069 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16070 **slot = ofs;
f792889a 16071 return type;
1c379e20
DJ
16072}
16073
673bfd45
DE
16074/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16075 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16076
16077static struct type *
673bfd45
DE
16078get_die_type_at_offset (unsigned int offset,
16079 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16080{
16081 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16082 htab_t type_hash;
f792889a 16083
b0df02fd 16084 if (per_cu->debug_types_section)
673bfd45
DE
16085 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16086 else
16087 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16088 if (type_hash == NULL)
16089 return NULL;
1c379e20 16090
673bfd45 16091 ofs.offset = offset;
1c379e20
DJ
16092 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16093 if (slot)
16094 return slot->type;
16095 else
16096 return NULL;
16097}
16098
673bfd45
DE
16099/* Look up the type for DIE in the appropriate type_hash table,
16100 or return NULL if DIE does not have a saved type. */
16101
16102static struct type *
16103get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16104{
16105 return get_die_type_at_offset (die->offset, cu->per_cu);
16106}
16107
10b3939b
DJ
16108/* Add a dependence relationship from CU to REF_PER_CU. */
16109
16110static void
16111dwarf2_add_dependence (struct dwarf2_cu *cu,
16112 struct dwarf2_per_cu_data *ref_per_cu)
16113{
16114 void **slot;
16115
16116 if (cu->dependencies == NULL)
16117 cu->dependencies
16118 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16119 NULL, &cu->comp_unit_obstack,
16120 hashtab_obstack_allocate,
16121 dummy_obstack_deallocate);
16122
16123 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16124 if (*slot == NULL)
16125 *slot = ref_per_cu;
16126}
1c379e20 16127
f504f079
DE
16128/* Subroutine of dwarf2_mark to pass to htab_traverse.
16129 Set the mark field in every compilation unit in the
ae038cb0
DJ
16130 cache that we must keep because we are keeping CU. */
16131
10b3939b
DJ
16132static int
16133dwarf2_mark_helper (void **slot, void *data)
16134{
16135 struct dwarf2_per_cu_data *per_cu;
16136
16137 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16138
16139 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16140 reading of the chain. As such dependencies remain valid it is not much
16141 useful to track and undo them during QUIT cleanups. */
16142 if (per_cu->cu == NULL)
16143 return 1;
16144
10b3939b
DJ
16145 if (per_cu->cu->mark)
16146 return 1;
16147 per_cu->cu->mark = 1;
16148
16149 if (per_cu->cu->dependencies != NULL)
16150 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16151
16152 return 1;
16153}
16154
f504f079
DE
16155/* Set the mark field in CU and in every other compilation unit in the
16156 cache that we must keep because we are keeping CU. */
16157
ae038cb0
DJ
16158static void
16159dwarf2_mark (struct dwarf2_cu *cu)
16160{
16161 if (cu->mark)
16162 return;
16163 cu->mark = 1;
10b3939b
DJ
16164 if (cu->dependencies != NULL)
16165 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16166}
16167
16168static void
16169dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16170{
16171 while (per_cu)
16172 {
16173 per_cu->cu->mark = 0;
16174 per_cu = per_cu->cu->read_in_chain;
16175 }
72bf9492
DJ
16176}
16177
72bf9492
DJ
16178/* Trivial hash function for partial_die_info: the hash value of a DIE
16179 is its offset in .debug_info for this objfile. */
16180
16181static hashval_t
16182partial_die_hash (const void *item)
16183{
16184 const struct partial_die_info *part_die = item;
9a619af0 16185
72bf9492
DJ
16186 return part_die->offset;
16187}
16188
16189/* Trivial comparison function for partial_die_info structures: two DIEs
16190 are equal if they have the same offset. */
16191
16192static int
16193partial_die_eq (const void *item_lhs, const void *item_rhs)
16194{
16195 const struct partial_die_info *part_die_lhs = item_lhs;
16196 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16197
72bf9492
DJ
16198 return part_die_lhs->offset == part_die_rhs->offset;
16199}
16200
ae038cb0
DJ
16201static struct cmd_list_element *set_dwarf2_cmdlist;
16202static struct cmd_list_element *show_dwarf2_cmdlist;
16203
16204static void
16205set_dwarf2_cmd (char *args, int from_tty)
16206{
16207 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16208}
16209
16210static void
16211show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16212{
ae038cb0
DJ
16213 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16214}
16215
dce234bc
PP
16216/* If section described by INFO was mmapped, munmap it now. */
16217
16218static void
16219munmap_section_buffer (struct dwarf2_section_info *info)
16220{
b315ab21 16221 if (info->map_addr != NULL)
dce234bc
PP
16222 {
16223#ifdef HAVE_MMAP
b315ab21 16224 int res;
9a619af0 16225
b315ab21
TG
16226 res = munmap (info->map_addr, info->map_len);
16227 gdb_assert (res == 0);
dce234bc
PP
16228#else
16229 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16230 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16231#endif
16232 }
16233}
16234
16235/* munmap debug sections for OBJFILE, if necessary. */
16236
16237static void
c1bd65d0 16238dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16239{
16240 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16241 int ix;
16242 struct dwarf2_section_info *section;
9a619af0 16243
16be1145
DE
16244 /* This is sorted according to the order they're defined in to make it easier
16245 to keep in sync. */
dce234bc
PP
16246 munmap_section_buffer (&data->info);
16247 munmap_section_buffer (&data->abbrev);
16248 munmap_section_buffer (&data->line);
16be1145 16249 munmap_section_buffer (&data->loc);
dce234bc 16250 munmap_section_buffer (&data->macinfo);
cf2c3c16 16251 munmap_section_buffer (&data->macro);
16be1145 16252 munmap_section_buffer (&data->str);
dce234bc 16253 munmap_section_buffer (&data->ranges);
dce234bc
PP
16254 munmap_section_buffer (&data->frame);
16255 munmap_section_buffer (&data->eh_frame);
9291a0cd 16256 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16257
16258 for (ix = 0;
16259 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16260 ++ix)
16261 munmap_section_buffer (section);
16262
16263 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16264}
16265
16266\f
ae2de4f8 16267/* The "save gdb-index" command. */
9291a0cd
TT
16268
16269/* The contents of the hash table we create when building the string
16270 table. */
16271struct strtab_entry
16272{
16273 offset_type offset;
16274 const char *str;
16275};
16276
559a7a62
JK
16277/* Hash function for a strtab_entry.
16278
16279 Function is used only during write_hash_table so no index format backward
16280 compatibility is needed. */
b89be57b 16281
9291a0cd
TT
16282static hashval_t
16283hash_strtab_entry (const void *e)
16284{
16285 const struct strtab_entry *entry = e;
559a7a62 16286 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16287}
16288
16289/* Equality function for a strtab_entry. */
b89be57b 16290
9291a0cd
TT
16291static int
16292eq_strtab_entry (const void *a, const void *b)
16293{
16294 const struct strtab_entry *ea = a;
16295 const struct strtab_entry *eb = b;
16296 return !strcmp (ea->str, eb->str);
16297}
16298
16299/* Create a strtab_entry hash table. */
b89be57b 16300
9291a0cd
TT
16301static htab_t
16302create_strtab (void)
16303{
16304 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16305 xfree, xcalloc, xfree);
16306}
16307
16308/* Add a string to the constant pool. Return the string's offset in
16309 host order. */
b89be57b 16310
9291a0cd
TT
16311static offset_type
16312add_string (htab_t table, struct obstack *cpool, const char *str)
16313{
16314 void **slot;
16315 struct strtab_entry entry;
16316 struct strtab_entry *result;
16317
16318 entry.str = str;
16319 slot = htab_find_slot (table, &entry, INSERT);
16320 if (*slot)
16321 result = *slot;
16322 else
16323 {
16324 result = XNEW (struct strtab_entry);
16325 result->offset = obstack_object_size (cpool);
16326 result->str = str;
16327 obstack_grow_str0 (cpool, str);
16328 *slot = result;
16329 }
16330 return result->offset;
16331}
16332
16333/* An entry in the symbol table. */
16334struct symtab_index_entry
16335{
16336 /* The name of the symbol. */
16337 const char *name;
16338 /* The offset of the name in the constant pool. */
16339 offset_type index_offset;
16340 /* A sorted vector of the indices of all the CUs that hold an object
16341 of this name. */
16342 VEC (offset_type) *cu_indices;
16343};
16344
16345/* The symbol table. This is a power-of-2-sized hash table. */
16346struct mapped_symtab
16347{
16348 offset_type n_elements;
16349 offset_type size;
16350 struct symtab_index_entry **data;
16351};
16352
16353/* Hash function for a symtab_index_entry. */
b89be57b 16354
9291a0cd
TT
16355static hashval_t
16356hash_symtab_entry (const void *e)
16357{
16358 const struct symtab_index_entry *entry = e;
16359 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16360 sizeof (offset_type) * VEC_length (offset_type,
16361 entry->cu_indices),
16362 0);
16363}
16364
16365/* Equality function for a symtab_index_entry. */
b89be57b 16366
9291a0cd
TT
16367static int
16368eq_symtab_entry (const void *a, const void *b)
16369{
16370 const struct symtab_index_entry *ea = a;
16371 const struct symtab_index_entry *eb = b;
16372 int len = VEC_length (offset_type, ea->cu_indices);
16373 if (len != VEC_length (offset_type, eb->cu_indices))
16374 return 0;
16375 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16376 VEC_address (offset_type, eb->cu_indices),
16377 sizeof (offset_type) * len);
16378}
16379
16380/* Destroy a symtab_index_entry. */
b89be57b 16381
9291a0cd
TT
16382static void
16383delete_symtab_entry (void *p)
16384{
16385 struct symtab_index_entry *entry = p;
16386 VEC_free (offset_type, entry->cu_indices);
16387 xfree (entry);
16388}
16389
16390/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16391
9291a0cd 16392static htab_t
3876f04e 16393create_symbol_hash_table (void)
9291a0cd
TT
16394{
16395 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16396 delete_symtab_entry, xcalloc, xfree);
16397}
16398
16399/* Create a new mapped symtab object. */
b89be57b 16400
9291a0cd
TT
16401static struct mapped_symtab *
16402create_mapped_symtab (void)
16403{
16404 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16405 symtab->n_elements = 0;
16406 symtab->size = 1024;
16407 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16408 return symtab;
16409}
16410
16411/* Destroy a mapped_symtab. */
b89be57b 16412
9291a0cd
TT
16413static void
16414cleanup_mapped_symtab (void *p)
16415{
16416 struct mapped_symtab *symtab = p;
16417 /* The contents of the array are freed when the other hash table is
16418 destroyed. */
16419 xfree (symtab->data);
16420 xfree (symtab);
16421}
16422
16423/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16424 the slot.
16425
16426 Function is used only during write_hash_table so no index format backward
16427 compatibility is needed. */
b89be57b 16428
9291a0cd
TT
16429static struct symtab_index_entry **
16430find_slot (struct mapped_symtab *symtab, const char *name)
16431{
559a7a62 16432 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16433
16434 index = hash & (symtab->size - 1);
16435 step = ((hash * 17) & (symtab->size - 1)) | 1;
16436
16437 for (;;)
16438 {
16439 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16440 return &symtab->data[index];
16441 index = (index + step) & (symtab->size - 1);
16442 }
16443}
16444
16445/* Expand SYMTAB's hash table. */
b89be57b 16446
9291a0cd
TT
16447static void
16448hash_expand (struct mapped_symtab *symtab)
16449{
16450 offset_type old_size = symtab->size;
16451 offset_type i;
16452 struct symtab_index_entry **old_entries = symtab->data;
16453
16454 symtab->size *= 2;
16455 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16456
16457 for (i = 0; i < old_size; ++i)
16458 {
16459 if (old_entries[i])
16460 {
16461 struct symtab_index_entry **slot = find_slot (symtab,
16462 old_entries[i]->name);
16463 *slot = old_entries[i];
16464 }
16465 }
16466
16467 xfree (old_entries);
16468}
16469
16470/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16471 is the index of the CU in which the symbol appears. */
b89be57b 16472
9291a0cd
TT
16473static void
16474add_index_entry (struct mapped_symtab *symtab, const char *name,
16475 offset_type cu_index)
16476{
16477 struct symtab_index_entry **slot;
16478
16479 ++symtab->n_elements;
16480 if (4 * symtab->n_elements / 3 >= symtab->size)
16481 hash_expand (symtab);
16482
16483 slot = find_slot (symtab, name);
16484 if (!*slot)
16485 {
16486 *slot = XNEW (struct symtab_index_entry);
16487 (*slot)->name = name;
16488 (*slot)->cu_indices = NULL;
16489 }
16490 /* Don't push an index twice. Due to how we add entries we only
16491 have to check the last one. */
16492 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16493 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16494 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16495}
16496
16497/* Add a vector of indices to the constant pool. */
b89be57b 16498
9291a0cd 16499static offset_type
3876f04e 16500add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16501 struct symtab_index_entry *entry)
16502{
16503 void **slot;
16504
3876f04e 16505 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16506 if (!*slot)
16507 {
16508 offset_type len = VEC_length (offset_type, entry->cu_indices);
16509 offset_type val = MAYBE_SWAP (len);
16510 offset_type iter;
16511 int i;
16512
16513 *slot = entry;
16514 entry->index_offset = obstack_object_size (cpool);
16515
16516 obstack_grow (cpool, &val, sizeof (val));
16517 for (i = 0;
16518 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16519 ++i)
16520 {
16521 val = MAYBE_SWAP (iter);
16522 obstack_grow (cpool, &val, sizeof (val));
16523 }
16524 }
16525 else
16526 {
16527 struct symtab_index_entry *old_entry = *slot;
16528 entry->index_offset = old_entry->index_offset;
16529 entry = old_entry;
16530 }
16531 return entry->index_offset;
16532}
16533
16534/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16535 constant pool entries going into the obstack CPOOL. */
b89be57b 16536
9291a0cd
TT
16537static void
16538write_hash_table (struct mapped_symtab *symtab,
16539 struct obstack *output, struct obstack *cpool)
16540{
16541 offset_type i;
3876f04e 16542 htab_t symbol_hash_table;
9291a0cd
TT
16543 htab_t str_table;
16544
3876f04e 16545 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16546 str_table = create_strtab ();
3876f04e 16547
9291a0cd
TT
16548 /* We add all the index vectors to the constant pool first, to
16549 ensure alignment is ok. */
16550 for (i = 0; i < symtab->size; ++i)
16551 {
16552 if (symtab->data[i])
3876f04e 16553 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16554 }
16555
16556 /* Now write out the hash table. */
16557 for (i = 0; i < symtab->size; ++i)
16558 {
16559 offset_type str_off, vec_off;
16560
16561 if (symtab->data[i])
16562 {
16563 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16564 vec_off = symtab->data[i]->index_offset;
16565 }
16566 else
16567 {
16568 /* While 0 is a valid constant pool index, it is not valid
16569 to have 0 for both offsets. */
16570 str_off = 0;
16571 vec_off = 0;
16572 }
16573
16574 str_off = MAYBE_SWAP (str_off);
16575 vec_off = MAYBE_SWAP (vec_off);
16576
16577 obstack_grow (output, &str_off, sizeof (str_off));
16578 obstack_grow (output, &vec_off, sizeof (vec_off));
16579 }
16580
16581 htab_delete (str_table);
3876f04e 16582 htab_delete (symbol_hash_table);
9291a0cd
TT
16583}
16584
0a5429f6
DE
16585/* Struct to map psymtab to CU index in the index file. */
16586struct psymtab_cu_index_map
16587{
16588 struct partial_symtab *psymtab;
16589 unsigned int cu_index;
16590};
16591
16592static hashval_t
16593hash_psymtab_cu_index (const void *item)
16594{
16595 const struct psymtab_cu_index_map *map = item;
16596
16597 return htab_hash_pointer (map->psymtab);
16598}
16599
16600static int
16601eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16602{
16603 const struct psymtab_cu_index_map *lhs = item_lhs;
16604 const struct psymtab_cu_index_map *rhs = item_rhs;
16605
16606 return lhs->psymtab == rhs->psymtab;
16607}
16608
16609/* Helper struct for building the address table. */
16610struct addrmap_index_data
16611{
16612 struct objfile *objfile;
16613 struct obstack *addr_obstack;
16614 htab_t cu_index_htab;
16615
16616 /* Non-zero if the previous_* fields are valid.
16617 We can't write an entry until we see the next entry (since it is only then
16618 that we know the end of the entry). */
16619 int previous_valid;
16620 /* Index of the CU in the table of all CUs in the index file. */
16621 unsigned int previous_cu_index;
0963b4bd 16622 /* Start address of the CU. */
0a5429f6
DE
16623 CORE_ADDR previous_cu_start;
16624};
16625
16626/* Write an address entry to OBSTACK. */
b89be57b 16627
9291a0cd 16628static void
0a5429f6
DE
16629add_address_entry (struct objfile *objfile, struct obstack *obstack,
16630 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16631{
0a5429f6 16632 offset_type cu_index_to_write;
9291a0cd
TT
16633 char addr[8];
16634 CORE_ADDR baseaddr;
16635
16636 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16637
0a5429f6
DE
16638 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16639 obstack_grow (obstack, addr, 8);
16640 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16641 obstack_grow (obstack, addr, 8);
16642 cu_index_to_write = MAYBE_SWAP (cu_index);
16643 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16644}
16645
16646/* Worker function for traversing an addrmap to build the address table. */
16647
16648static int
16649add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16650{
16651 struct addrmap_index_data *data = datap;
16652 struct partial_symtab *pst = obj;
16653 offset_type cu_index;
16654 void **slot;
16655
16656 if (data->previous_valid)
16657 add_address_entry (data->objfile, data->addr_obstack,
16658 data->previous_cu_start, start_addr,
16659 data->previous_cu_index);
16660
16661 data->previous_cu_start = start_addr;
16662 if (pst != NULL)
16663 {
16664 struct psymtab_cu_index_map find_map, *map;
16665 find_map.psymtab = pst;
16666 map = htab_find (data->cu_index_htab, &find_map);
16667 gdb_assert (map != NULL);
16668 data->previous_cu_index = map->cu_index;
16669 data->previous_valid = 1;
16670 }
16671 else
16672 data->previous_valid = 0;
16673
16674 return 0;
16675}
16676
16677/* Write OBJFILE's address map to OBSTACK.
16678 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16679 in the index file. */
16680
16681static void
16682write_address_map (struct objfile *objfile, struct obstack *obstack,
16683 htab_t cu_index_htab)
16684{
16685 struct addrmap_index_data addrmap_index_data;
16686
16687 /* When writing the address table, we have to cope with the fact that
16688 the addrmap iterator only provides the start of a region; we have to
16689 wait until the next invocation to get the start of the next region. */
16690
16691 addrmap_index_data.objfile = objfile;
16692 addrmap_index_data.addr_obstack = obstack;
16693 addrmap_index_data.cu_index_htab = cu_index_htab;
16694 addrmap_index_data.previous_valid = 0;
16695
16696 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16697 &addrmap_index_data);
16698
16699 /* It's highly unlikely the last entry (end address = 0xff...ff)
16700 is valid, but we should still handle it.
16701 The end address is recorded as the start of the next region, but that
16702 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16703 anyway. */
16704 if (addrmap_index_data.previous_valid)
16705 add_address_entry (objfile, obstack,
16706 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16707 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16708}
16709
16710/* Add a list of partial symbols to SYMTAB. */
b89be57b 16711
9291a0cd
TT
16712static void
16713write_psymbols (struct mapped_symtab *symtab,
987d643c 16714 htab_t psyms_seen,
9291a0cd
TT
16715 struct partial_symbol **psymp,
16716 int count,
987d643c
TT
16717 offset_type cu_index,
16718 int is_static)
9291a0cd
TT
16719{
16720 for (; count-- > 0; ++psymp)
16721 {
987d643c
TT
16722 void **slot, *lookup;
16723
9291a0cd
TT
16724 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16725 error (_("Ada is not currently supported by the index"));
987d643c
TT
16726
16727 /* We only want to add a given psymbol once. However, we also
16728 want to account for whether it is global or static. So, we
16729 may add it twice, using slightly different values. */
16730 if (is_static)
16731 {
16732 uintptr_t val = 1 | (uintptr_t) *psymp;
16733
16734 lookup = (void *) val;
16735 }
16736 else
16737 lookup = *psymp;
16738
16739 /* Only add a given psymbol once. */
16740 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16741 if (!*slot)
16742 {
16743 *slot = lookup;
bb2f58dc 16744 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 16745 }
9291a0cd
TT
16746 }
16747}
16748
16749/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16750 exception if there is an error. */
b89be57b 16751
9291a0cd
TT
16752static void
16753write_obstack (FILE *file, struct obstack *obstack)
16754{
16755 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16756 file)
16757 != obstack_object_size (obstack))
16758 error (_("couldn't data write to file"));
16759}
16760
16761/* Unlink a file if the argument is not NULL. */
b89be57b 16762
9291a0cd
TT
16763static void
16764unlink_if_set (void *p)
16765{
16766 char **filename = p;
16767 if (*filename)
16768 unlink (*filename);
16769}
16770
1fd400ff
TT
16771/* A helper struct used when iterating over debug_types. */
16772struct signatured_type_index_data
16773{
16774 struct objfile *objfile;
16775 struct mapped_symtab *symtab;
16776 struct obstack *types_list;
987d643c 16777 htab_t psyms_seen;
1fd400ff
TT
16778 int cu_index;
16779};
16780
16781/* A helper function that writes a single signatured_type to an
16782 obstack. */
b89be57b 16783
1fd400ff
TT
16784static int
16785write_one_signatured_type (void **slot, void *d)
16786{
16787 struct signatured_type_index_data *info = d;
16788 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16789 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16790 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16791 gdb_byte val[8];
16792
16793 write_psymbols (info->symtab,
987d643c 16794 info->psyms_seen,
3e43a32a
MS
16795 info->objfile->global_psymbols.list
16796 + psymtab->globals_offset,
987d643c
TT
16797 psymtab->n_global_syms, info->cu_index,
16798 0);
1fd400ff 16799 write_psymbols (info->symtab,
987d643c 16800 info->psyms_seen,
3e43a32a
MS
16801 info->objfile->static_psymbols.list
16802 + psymtab->statics_offset,
987d643c
TT
16803 psymtab->n_static_syms, info->cu_index,
16804 1);
1fd400ff 16805
b3c8eb43 16806 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16807 obstack_grow (info->types_list, val, 8);
16808 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16809 obstack_grow (info->types_list, val, 8);
16810 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16811 obstack_grow (info->types_list, val, 8);
16812
16813 ++info->cu_index;
16814
16815 return 1;
16816}
16817
9291a0cd 16818/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16819
9291a0cd
TT
16820static void
16821write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16822{
16823 struct cleanup *cleanup;
16824 char *filename, *cleanup_filename;
1fd400ff
TT
16825 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16826 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16827 int i;
16828 FILE *out_file;
16829 struct mapped_symtab *symtab;
16830 offset_type val, size_of_contents, total_len;
16831 struct stat st;
16832 char buf[8];
987d643c 16833 htab_t psyms_seen;
0a5429f6
DE
16834 htab_t cu_index_htab;
16835 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16836
b4f2f049 16837 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16838 return;
b4f2f049 16839
9291a0cd
TT
16840 if (dwarf2_per_objfile->using_index)
16841 error (_("Cannot use an index to create the index"));
16842
8b70b953
TT
16843 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16844 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16845
9291a0cd 16846 if (stat (objfile->name, &st) < 0)
7e17e088 16847 perror_with_name (objfile->name);
9291a0cd
TT
16848
16849 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16850 INDEX_SUFFIX, (char *) NULL);
16851 cleanup = make_cleanup (xfree, filename);
16852
16853 out_file = fopen (filename, "wb");
16854 if (!out_file)
16855 error (_("Can't open `%s' for writing"), filename);
16856
16857 cleanup_filename = filename;
16858 make_cleanup (unlink_if_set, &cleanup_filename);
16859
16860 symtab = create_mapped_symtab ();
16861 make_cleanup (cleanup_mapped_symtab, symtab);
16862
16863 obstack_init (&addr_obstack);
16864 make_cleanup_obstack_free (&addr_obstack);
16865
16866 obstack_init (&cu_list);
16867 make_cleanup_obstack_free (&cu_list);
16868
1fd400ff
TT
16869 obstack_init (&types_cu_list);
16870 make_cleanup_obstack_free (&types_cu_list);
16871
987d643c
TT
16872 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16873 NULL, xcalloc, xfree);
96408a79 16874 make_cleanup_htab_delete (psyms_seen);
987d643c 16875
0a5429f6
DE
16876 /* While we're scanning CU's create a table that maps a psymtab pointer
16877 (which is what addrmap records) to its index (which is what is recorded
16878 in the index file). This will later be needed to write the address
16879 table. */
16880 cu_index_htab = htab_create_alloc (100,
16881 hash_psymtab_cu_index,
16882 eq_psymtab_cu_index,
16883 NULL, xcalloc, xfree);
96408a79 16884 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
16885 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16886 xmalloc (sizeof (struct psymtab_cu_index_map)
16887 * dwarf2_per_objfile->n_comp_units);
16888 make_cleanup (xfree, psymtab_cu_index_map);
16889
16890 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16891 work here. Also, the debug_types entries do not appear in
16892 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16893 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16894 {
3e43a32a
MS
16895 struct dwarf2_per_cu_data *per_cu
16896 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16897 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16898 gdb_byte val[8];
0a5429f6
DE
16899 struct psymtab_cu_index_map *map;
16900 void **slot;
9291a0cd
TT
16901
16902 write_psymbols (symtab,
987d643c 16903 psyms_seen,
9291a0cd 16904 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16905 psymtab->n_global_syms, i,
16906 0);
9291a0cd 16907 write_psymbols (symtab,
987d643c 16908 psyms_seen,
9291a0cd 16909 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16910 psymtab->n_static_syms, i,
16911 1);
9291a0cd 16912
0a5429f6
DE
16913 map = &psymtab_cu_index_map[i];
16914 map->psymtab = psymtab;
16915 map->cu_index = i;
16916 slot = htab_find_slot (cu_index_htab, map, INSERT);
16917 gdb_assert (slot != NULL);
16918 gdb_assert (*slot == NULL);
16919 *slot = map;
9291a0cd 16920
e254ef6a 16921 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16922 obstack_grow (&cu_list, val, 8);
e254ef6a 16923 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16924 obstack_grow (&cu_list, val, 8);
16925 }
16926
0a5429f6
DE
16927 /* Dump the address map. */
16928 write_address_map (objfile, &addr_obstack, cu_index_htab);
16929
1fd400ff
TT
16930 /* Write out the .debug_type entries, if any. */
16931 if (dwarf2_per_objfile->signatured_types)
16932 {
16933 struct signatured_type_index_data sig_data;
16934
16935 sig_data.objfile = objfile;
16936 sig_data.symtab = symtab;
16937 sig_data.types_list = &types_cu_list;
987d643c 16938 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
16939 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16940 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16941 write_one_signatured_type, &sig_data);
16942 }
16943
9291a0cd
TT
16944 obstack_init (&constant_pool);
16945 make_cleanup_obstack_free (&constant_pool);
16946 obstack_init (&symtab_obstack);
16947 make_cleanup_obstack_free (&symtab_obstack);
16948 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16949
16950 obstack_init (&contents);
16951 make_cleanup_obstack_free (&contents);
1fd400ff 16952 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
16953 total_len = size_of_contents;
16954
16955 /* The version number. */
559a7a62 16956 val = MAYBE_SWAP (5);
9291a0cd
TT
16957 obstack_grow (&contents, &val, sizeof (val));
16958
16959 /* The offset of the CU list from the start of the file. */
16960 val = MAYBE_SWAP (total_len);
16961 obstack_grow (&contents, &val, sizeof (val));
16962 total_len += obstack_object_size (&cu_list);
16963
1fd400ff
TT
16964 /* The offset of the types CU list from the start of the file. */
16965 val = MAYBE_SWAP (total_len);
16966 obstack_grow (&contents, &val, sizeof (val));
16967 total_len += obstack_object_size (&types_cu_list);
16968
9291a0cd
TT
16969 /* The offset of the address table from the start of the file. */
16970 val = MAYBE_SWAP (total_len);
16971 obstack_grow (&contents, &val, sizeof (val));
16972 total_len += obstack_object_size (&addr_obstack);
16973
16974 /* The offset of the symbol table from the start of the file. */
16975 val = MAYBE_SWAP (total_len);
16976 obstack_grow (&contents, &val, sizeof (val));
16977 total_len += obstack_object_size (&symtab_obstack);
16978
16979 /* The offset of the constant pool from the start of the file. */
16980 val = MAYBE_SWAP (total_len);
16981 obstack_grow (&contents, &val, sizeof (val));
16982 total_len += obstack_object_size (&constant_pool);
16983
16984 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16985
16986 write_obstack (out_file, &contents);
16987 write_obstack (out_file, &cu_list);
1fd400ff 16988 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
16989 write_obstack (out_file, &addr_obstack);
16990 write_obstack (out_file, &symtab_obstack);
16991 write_obstack (out_file, &constant_pool);
16992
16993 fclose (out_file);
16994
16995 /* We want to keep the file, so we set cleanup_filename to NULL
16996 here. See unlink_if_set. */
16997 cleanup_filename = NULL;
16998
16999 do_cleanups (cleanup);
17000}
17001
90476074
TT
17002/* Implementation of the `save gdb-index' command.
17003
17004 Note that the file format used by this command is documented in the
17005 GDB manual. Any changes here must be documented there. */
11570e71 17006
9291a0cd
TT
17007static void
17008save_gdb_index_command (char *arg, int from_tty)
17009{
17010 struct objfile *objfile;
17011
17012 if (!arg || !*arg)
96d19272 17013 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17014
17015 ALL_OBJFILES (objfile)
17016 {
17017 struct stat st;
17018
17019 /* If the objfile does not correspond to an actual file, skip it. */
17020 if (stat (objfile->name, &st) < 0)
17021 continue;
17022
17023 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17024 if (dwarf2_per_objfile)
17025 {
17026 volatile struct gdb_exception except;
17027
17028 TRY_CATCH (except, RETURN_MASK_ERROR)
17029 {
17030 write_psymtabs_to_index (objfile, arg);
17031 }
17032 if (except.reason < 0)
17033 exception_fprintf (gdb_stderr, except,
17034 _("Error while writing index for `%s': "),
17035 objfile->name);
17036 }
17037 }
dce234bc
PP
17038}
17039
9291a0cd
TT
17040\f
17041
9eae7c52
TT
17042int dwarf2_always_disassemble;
17043
17044static void
17045show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17046 struct cmd_list_element *c, const char *value)
17047{
3e43a32a
MS
17048 fprintf_filtered (file,
17049 _("Whether to always disassemble "
17050 "DWARF expressions is %s.\n"),
9eae7c52
TT
17051 value);
17052}
17053
900e11f9
JK
17054static void
17055show_check_physname (struct ui_file *file, int from_tty,
17056 struct cmd_list_element *c, const char *value)
17057{
17058 fprintf_filtered (file,
17059 _("Whether to check \"physname\" is %s.\n"),
17060 value);
17061}
17062
6502dd73
DJ
17063void _initialize_dwarf2_read (void);
17064
17065void
17066_initialize_dwarf2_read (void)
17067{
96d19272
JK
17068 struct cmd_list_element *c;
17069
dce234bc 17070 dwarf2_objfile_data_key
c1bd65d0 17071 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17072
1bedd215
AC
17073 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17074Set DWARF 2 specific variables.\n\
17075Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17076 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17077 0/*allow-unknown*/, &maintenance_set_cmdlist);
17078
1bedd215
AC
17079 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17080Show DWARF 2 specific variables\n\
17081Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17082 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17083 0/*allow-unknown*/, &maintenance_show_cmdlist);
17084
17085 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17086 &dwarf2_max_cache_age, _("\
17087Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17088Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17089A higher limit means that cached compilation units will be stored\n\
17090in memory longer, and more total memory will be used. Zero disables\n\
17091caching, which can slow down startup."),
2c5b56ce 17092 NULL,
920d2a44 17093 show_dwarf2_max_cache_age,
2c5b56ce 17094 &set_dwarf2_cmdlist,
ae038cb0 17095 &show_dwarf2_cmdlist);
d97bc12b 17096
9eae7c52
TT
17097 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17098 &dwarf2_always_disassemble, _("\
17099Set whether `info address' always disassembles DWARF expressions."), _("\
17100Show whether `info address' always disassembles DWARF expressions."), _("\
17101When enabled, DWARF expressions are always printed in an assembly-like\n\
17102syntax. When disabled, expressions will be printed in a more\n\
17103conversational style, when possible."),
17104 NULL,
17105 show_dwarf2_always_disassemble,
17106 &set_dwarf2_cmdlist,
17107 &show_dwarf2_cmdlist);
17108
d97bc12b
DE
17109 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17110Set debugging of the dwarf2 DIE reader."), _("\
17111Show debugging of the dwarf2 DIE reader."), _("\
17112When enabled (non-zero), DIEs are dumped after they are read in.\n\
17113The value is the maximum depth to print."),
17114 NULL,
17115 NULL,
17116 &setdebuglist, &showdebuglist);
9291a0cd 17117
900e11f9
JK
17118 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17119Set cross-checking of \"physname\" code against demangler."), _("\
17120Show cross-checking of \"physname\" code against demangler."), _("\
17121When enabled, GDB's internal \"physname\" code is checked against\n\
17122the demangler."),
17123 NULL, show_check_physname,
17124 &setdebuglist, &showdebuglist);
17125
96d19272 17126 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17127 _("\
fc1a9d6e 17128Save a gdb-index file.\n\
11570e71 17129Usage: save gdb-index DIRECTORY"),
96d19272
JK
17130 &save_cmdlist);
17131 set_cmd_completer (c, filename_completer);
6502dd73 17132}
This page took 2.342043 seconds and 4 git commands to generate.