gdb/
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
7b6bb8da 4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
60d5a603 60#include <ctype.h>
4c2df51b 61
c906108c
SS
62#include <fcntl.h>
63#include "gdb_string.h"
4bdf3d34 64#include "gdb_assert.h"
c906108c 65#include <sys/types.h>
233a11ab
CS
66#ifdef HAVE_ZLIB_H
67#include <zlib.h>
68#endif
dce234bc
PP
69#ifdef HAVE_MMAP
70#include <sys/mman.h>
85d9bd0e
TT
71#ifndef MAP_FAILED
72#define MAP_FAILED ((void *) -1)
73#endif
dce234bc 74#endif
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
107d2387 79#if 0
357e46e7 80/* .debug_info header for a compilation unit
c906108c
SS
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83typedef struct comp_unit_header
84 {
85 unsigned int length; /* length of the .debug_info
86 contribution */
87 unsigned short version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90 unsigned char addr_size; /* byte size of an address -- 4 */
91 }
92_COMP_UNIT_HEADER;
93#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 94#endif
c906108c 95
c906108c
SS
96/* .debug_line statement program prologue
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99typedef struct statement_prologue
100 {
101 unsigned int total_length; /* byte length of the statement
102 information */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int prologue_length; /* # bytes between prologue &
106 stmt program */
107 unsigned char minimum_instruction_length; /* byte size of
108 smallest instr */
109 unsigned char default_is_stmt; /* initial value of is_stmt
110 register */
111 char line_base;
112 unsigned char line_range;
113 unsigned char opcode_base; /* number assigned to first special
114 opcode */
115 unsigned char *standard_opcode_lengths;
116 }
117_STATEMENT_PROLOGUE;
118
d97bc12b
DE
119/* When non-zero, dump DIEs after they are read in. */
120static int dwarf2_die_debug = 0;
121
900e11f9
JK
122/* When non-zero, cross-check physname against demangler. */
123static int check_physname = 0;
124
dce234bc
PP
125static int pagesize;
126
df8a16a1
DJ
127/* When set, the file that we're processing is known to have debugging
128 info for C++ namespaces. GCC 3.3.x did not produce this information,
129 but later versions do. */
130
131static int processing_has_namespace_info;
132
6502dd73
DJ
133static const struct objfile_data *dwarf2_objfile_data_key;
134
dce234bc
PP
135struct dwarf2_section_info
136{
137 asection *asection;
138 gdb_byte *buffer;
139 bfd_size_type size;
b315ab21
TG
140 /* Not NULL if the section was actually mmapped. */
141 void *map_addr;
142 /* Page aligned size of mmapped area. */
143 bfd_size_type map_len;
be391dca
TT
144 /* True if we have tried to read this section. */
145 int readin;
dce234bc
PP
146};
147
9291a0cd
TT
148/* All offsets in the index are of this type. It must be
149 architecture-independent. */
150typedef uint32_t offset_type;
151
152DEF_VEC_I (offset_type);
153
154/* A description of the mapped index. The file format is described in
155 a comment by the code that writes the index. */
156struct mapped_index
157{
559a7a62
JK
158 /* Index data format version. */
159 int version;
160
9291a0cd
TT
161 /* The total length of the buffer. */
162 off_t total_size;
b11b1f88 163
9291a0cd
TT
164 /* A pointer to the address table data. */
165 const gdb_byte *address_table;
b11b1f88 166
9291a0cd
TT
167 /* Size of the address table data in bytes. */
168 offset_type address_table_size;
b11b1f88 169
3876f04e
DE
170 /* The symbol table, implemented as a hash table. */
171 const offset_type *symbol_table;
b11b1f88 172
9291a0cd 173 /* Size in slots, each slot is 2 offset_types. */
3876f04e 174 offset_type symbol_table_slots;
b11b1f88 175
9291a0cd
TT
176 /* A pointer to the constant pool. */
177 const char *constant_pool;
178};
179
6502dd73
DJ
180struct dwarf2_per_objfile
181{
dce234bc
PP
182 struct dwarf2_section_info info;
183 struct dwarf2_section_info abbrev;
184 struct dwarf2_section_info line;
dce234bc
PP
185 struct dwarf2_section_info loc;
186 struct dwarf2_section_info macinfo;
187 struct dwarf2_section_info str;
188 struct dwarf2_section_info ranges;
348e048f 189 struct dwarf2_section_info types;
dce234bc
PP
190 struct dwarf2_section_info frame;
191 struct dwarf2_section_info eh_frame;
9291a0cd 192 struct dwarf2_section_info gdb_index;
ae038cb0 193
be391dca
TT
194 /* Back link. */
195 struct objfile *objfile;
196
10b3939b
DJ
197 /* A list of all the compilation units. This is used to locate
198 the target compilation unit of a particular reference. */
ae038cb0
DJ
199 struct dwarf2_per_cu_data **all_comp_units;
200
201 /* The number of compilation units in ALL_COMP_UNITS. */
202 int n_comp_units;
203
1fd400ff
TT
204 /* The number of .debug_types-related CUs. */
205 int n_type_comp_units;
206
207 /* The .debug_types-related CUs. */
208 struct dwarf2_per_cu_data **type_comp_units;
209
ae038cb0
DJ
210 /* A chain of compilation units that are currently read in, so that
211 they can be freed later. */
212 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 213
348e048f
DE
214 /* A table mapping .debug_types signatures to its signatured_type entry.
215 This is NULL if the .debug_types section hasn't been read in yet. */
216 htab_t signatured_types;
217
72dca2f5
FR
218 /* A flag indicating wether this objfile has a section loaded at a
219 VMA of 0. */
220 int has_section_at_zero;
9291a0cd 221
ae2de4f8
DE
222 /* True if we are using the mapped index,
223 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
224 unsigned char using_index;
225
ae2de4f8 226 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 227 struct mapped_index *index_table;
98bfdba5 228
7b9f3c50
DE
229 /* When using index_table, this keeps track of all quick_file_names entries.
230 TUs can share line table entries with CUs or other TUs, and there can be
231 a lot more TUs than unique line tables, so we maintain a separate table
232 of all line table entries to support the sharing. */
233 htab_t quick_file_names_table;
234
98bfdba5
PA
235 /* Set during partial symbol reading, to prevent queueing of full
236 symbols. */
237 int reading_partial_symbols;
673bfd45
DE
238
239 /* Table mapping type .debug_info DIE offsets to types.
240 This is NULL if not allocated yet.
241 It (currently) makes sense to allocate debug_types_type_hash lazily.
242 To keep things simple we allocate both lazily. */
243 htab_t debug_info_type_hash;
244
245 /* Table mapping type .debug_types DIE offsets to types.
246 This is NULL if not allocated yet. */
247 htab_t debug_types_type_hash;
6502dd73
DJ
248};
249
250static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 251
251d32d9 252/* Default names of the debugging sections. */
c906108c 253
233a11ab
CS
254/* Note that if the debugging section has been compressed, it might
255 have a name like .zdebug_info. */
256
251d32d9
TG
257static const struct dwarf2_debug_sections dwarf2_elf_names = {
258 { ".debug_info", ".zdebug_info" },
259 { ".debug_abbrev", ".zdebug_abbrev" },
260 { ".debug_line", ".zdebug_line" },
261 { ".debug_loc", ".zdebug_loc" },
262 { ".debug_macinfo", ".zdebug_macinfo" },
263 { ".debug_str", ".zdebug_str" },
264 { ".debug_ranges", ".zdebug_ranges" },
265 { ".debug_types", ".zdebug_types" },
266 { ".debug_frame", ".zdebug_frame" },
267 { ".eh_frame", NULL },
268 { ".gdb_index", ".zgdb_index" }
269};
c906108c
SS
270
271/* local data types */
272
0963b4bd 273/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
274#ifndef ABBREV_HASH_SIZE
275#define ABBREV_HASH_SIZE 121
276#endif
277
107d2387
AC
278/* The data in a compilation unit header, after target2host
279 translation, looks like this. */
c906108c 280struct comp_unit_head
a738430d 281{
c764a876 282 unsigned int length;
a738430d 283 short version;
a738430d
MK
284 unsigned char addr_size;
285 unsigned char signed_addr_p;
9cbfa09e 286 unsigned int abbrev_offset;
57349743 287
a738430d
MK
288 /* Size of file offsets; either 4 or 8. */
289 unsigned int offset_size;
57349743 290
a738430d
MK
291 /* Size of the length field; either 4 or 12. */
292 unsigned int initial_length_size;
57349743 293
a738430d
MK
294 /* Offset to the first byte of this compilation unit header in the
295 .debug_info section, for resolving relative reference dies. */
296 unsigned int offset;
57349743 297
d00adf39
DE
298 /* Offset to first die in this cu from the start of the cu.
299 This will be the first byte following the compilation unit header. */
300 unsigned int first_die_offset;
a738430d 301};
c906108c 302
3da10d80
KS
303/* Type used for delaying computation of method physnames.
304 See comments for compute_delayed_physnames. */
305struct delayed_method_info
306{
307 /* The type to which the method is attached, i.e., its parent class. */
308 struct type *type;
309
310 /* The index of the method in the type's function fieldlists. */
311 int fnfield_index;
312
313 /* The index of the method in the fieldlist. */
314 int index;
315
316 /* The name of the DIE. */
317 const char *name;
318
319 /* The DIE associated with this method. */
320 struct die_info *die;
321};
322
323typedef struct delayed_method_info delayed_method_info;
324DEF_VEC_O (delayed_method_info);
325
e7c27a73
DJ
326/* Internal state when decoding a particular compilation unit. */
327struct dwarf2_cu
328{
329 /* The objfile containing this compilation unit. */
330 struct objfile *objfile;
331
d00adf39 332 /* The header of the compilation unit. */
e7c27a73 333 struct comp_unit_head header;
e142c38c 334
d00adf39
DE
335 /* Base address of this compilation unit. */
336 CORE_ADDR base_address;
337
338 /* Non-zero if base_address has been set. */
339 int base_known;
340
e142c38c
DJ
341 struct function_range *first_fn, *last_fn, *cached_fn;
342
343 /* The language we are debugging. */
344 enum language language;
345 const struct language_defn *language_defn;
346
b0f35d58
DL
347 const char *producer;
348
e142c38c
DJ
349 /* The generic symbol table building routines have separate lists for
350 file scope symbols and all all other scopes (local scopes). So
351 we need to select the right one to pass to add_symbol_to_list().
352 We do it by keeping a pointer to the correct list in list_in_scope.
353
354 FIXME: The original dwarf code just treated the file scope as the
355 first local scope, and all other local scopes as nested local
356 scopes, and worked fine. Check to see if we really need to
357 distinguish these in buildsym.c. */
358 struct pending **list_in_scope;
359
f3dd6933
DJ
360 /* DWARF abbreviation table associated with this compilation unit. */
361 struct abbrev_info **dwarf2_abbrevs;
362
363 /* Storage for the abbrev table. */
364 struct obstack abbrev_obstack;
72bf9492
DJ
365
366 /* Hash table holding all the loaded partial DIEs. */
367 htab_t partial_dies;
368
369 /* Storage for things with the same lifetime as this read-in compilation
370 unit, including partial DIEs. */
371 struct obstack comp_unit_obstack;
372
ae038cb0
DJ
373 /* When multiple dwarf2_cu structures are living in memory, this field
374 chains them all together, so that they can be released efficiently.
375 We will probably also want a generation counter so that most-recently-used
376 compilation units are cached... */
377 struct dwarf2_per_cu_data *read_in_chain;
378
379 /* Backchain to our per_cu entry if the tree has been built. */
380 struct dwarf2_per_cu_data *per_cu;
381
382 /* How many compilation units ago was this CU last referenced? */
383 int last_used;
384
10b3939b 385 /* A hash table of die offsets for following references. */
51545339 386 htab_t die_hash;
10b3939b
DJ
387
388 /* Full DIEs if read in. */
389 struct die_info *dies;
390
391 /* A set of pointers to dwarf2_per_cu_data objects for compilation
392 units referenced by this one. Only set during full symbol processing;
393 partial symbol tables do not have dependencies. */
394 htab_t dependencies;
395
cb1df416
DJ
396 /* Header data from the line table, during full symbol processing. */
397 struct line_header *line_header;
398
3da10d80
KS
399 /* A list of methods which need to have physnames computed
400 after all type information has been read. */
401 VEC (delayed_method_info) *method_list;
402
ae038cb0
DJ
403 /* Mark used when releasing cached dies. */
404 unsigned int mark : 1;
405
406 /* This flag will be set if this compilation unit might include
407 inter-compilation-unit references. */
408 unsigned int has_form_ref_addr : 1;
409
72bf9492
DJ
410 /* This flag will be set if this compilation unit includes any
411 DW_TAG_namespace DIEs. If we know that there are explicit
412 DIEs for namespaces, we don't need to try to infer them
413 from mangled names. */
414 unsigned int has_namespace_info : 1;
8be455d7
JK
415
416 /* This CU references .debug_loc. See the symtab->locations_valid field.
417 This test is imperfect as there may exist optimized debug code not using
418 any location list and still facing inlining issues if handled as
419 unoptimized code. For a future better test see GCC PR other/32998. */
420
421 unsigned int has_loclist : 1;
e7c27a73
DJ
422};
423
10b3939b
DJ
424/* Persistent data held for a compilation unit, even when not
425 processing it. We put a pointer to this structure in the
426 read_symtab_private field of the psymtab. If we encounter
427 inter-compilation-unit references, we also maintain a sorted
428 list of all compilation units. */
429
ae038cb0
DJ
430struct dwarf2_per_cu_data
431{
348e048f 432 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 433 bytes should suffice to store the length of any compilation unit
45452591
DE
434 - if it doesn't, GDB will fall over anyway.
435 NOTE: Unlike comp_unit_head.length, this length includes
436 initial_length_size. */
c764a876 437 unsigned int offset;
348e048f 438 unsigned int length : 29;
ae038cb0
DJ
439
440 /* Flag indicating this compilation unit will be read in before
441 any of the current compilation units are processed. */
c764a876 442 unsigned int queued : 1;
ae038cb0 443
5afb4e99
DJ
444 /* This flag will be set if we need to load absolutely all DIEs
445 for this compilation unit, instead of just the ones we think
446 are interesting. It gets set if we look for a DIE in the
447 hash table and don't find it. */
448 unsigned int load_all_dies : 1;
449
348e048f
DE
450 /* Non-zero if this CU is from .debug_types.
451 Otherwise it's from .debug_info. */
452 unsigned int from_debug_types : 1;
453
17ea53c3
JK
454 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
455 of the CU cache it gets reset to NULL again. */
ae038cb0 456 struct dwarf2_cu *cu;
1c379e20 457
9291a0cd
TT
458 /* The corresponding objfile. */
459 struct objfile *objfile;
460
461 /* When using partial symbol tables, the 'psymtab' field is active.
462 Otherwise the 'quick' field is active. */
463 union
464 {
465 /* The partial symbol table associated with this compilation unit,
466 or NULL for partial units (which do not have an associated
467 symtab). */
468 struct partial_symtab *psymtab;
469
470 /* Data needed by the "quick" functions. */
471 struct dwarf2_per_cu_quick_data *quick;
472 } v;
ae038cb0
DJ
473};
474
348e048f
DE
475/* Entry in the signatured_types hash table. */
476
477struct signatured_type
478{
479 ULONGEST signature;
480
348e048f
DE
481 /* Offset in .debug_types of the type defined by this TU. */
482 unsigned int type_offset;
483
484 /* The CU(/TU) of this type. */
485 struct dwarf2_per_cu_data per_cu;
486};
487
0963b4bd
MS
488/* Struct used to pass misc. parameters to read_die_and_children, et
489 al. which are used for both .debug_info and .debug_types dies.
490 All parameters here are unchanging for the life of the call. This
491 struct exists to abstract away the constant parameters of die
492 reading. */
93311388
DE
493
494struct die_reader_specs
495{
496 /* The bfd of this objfile. */
497 bfd* abfd;
498
499 /* The CU of the DIE we are parsing. */
500 struct dwarf2_cu *cu;
501
502 /* Pointer to start of section buffer.
503 This is either the start of .debug_info or .debug_types. */
504 const gdb_byte *buffer;
505};
506
debd256d
JB
507/* The line number information for a compilation unit (found in the
508 .debug_line section) begins with a "statement program header",
509 which contains the following information. */
510struct line_header
511{
512 unsigned int total_length;
513 unsigned short version;
514 unsigned int header_length;
515 unsigned char minimum_instruction_length;
2dc7f7b3 516 unsigned char maximum_ops_per_instruction;
debd256d
JB
517 unsigned char default_is_stmt;
518 int line_base;
519 unsigned char line_range;
520 unsigned char opcode_base;
521
522 /* standard_opcode_lengths[i] is the number of operands for the
523 standard opcode whose value is i. This means that
524 standard_opcode_lengths[0] is unused, and the last meaningful
525 element is standard_opcode_lengths[opcode_base - 1]. */
526 unsigned char *standard_opcode_lengths;
527
528 /* The include_directories table. NOTE! These strings are not
529 allocated with xmalloc; instead, they are pointers into
530 debug_line_buffer. If you try to free them, `free' will get
531 indigestion. */
532 unsigned int num_include_dirs, include_dirs_size;
533 char **include_dirs;
534
535 /* The file_names table. NOTE! These strings are not allocated
536 with xmalloc; instead, they are pointers into debug_line_buffer.
537 Don't try to free them directly. */
538 unsigned int num_file_names, file_names_size;
539 struct file_entry
c906108c 540 {
debd256d
JB
541 char *name;
542 unsigned int dir_index;
543 unsigned int mod_time;
544 unsigned int length;
aaa75496 545 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 546 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
547 } *file_names;
548
549 /* The start and end of the statement program following this
6502dd73 550 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 551 gdb_byte *statement_program_start, *statement_program_end;
debd256d 552};
c906108c
SS
553
554/* When we construct a partial symbol table entry we only
0963b4bd 555 need this much information. */
c906108c
SS
556struct partial_die_info
557 {
72bf9492 558 /* Offset of this DIE. */
c906108c 559 unsigned int offset;
72bf9492
DJ
560
561 /* DWARF-2 tag for this DIE. */
562 ENUM_BITFIELD(dwarf_tag) tag : 16;
563
72bf9492
DJ
564 /* Assorted flags describing the data found in this DIE. */
565 unsigned int has_children : 1;
566 unsigned int is_external : 1;
567 unsigned int is_declaration : 1;
568 unsigned int has_type : 1;
569 unsigned int has_specification : 1;
570 unsigned int has_pc_info : 1;
571
572 /* Flag set if the SCOPE field of this structure has been
573 computed. */
574 unsigned int scope_set : 1;
575
fa4028e9
JB
576 /* Flag set if the DIE has a byte_size attribute. */
577 unsigned int has_byte_size : 1;
578
98bfdba5
PA
579 /* Flag set if any of the DIE's children are template arguments. */
580 unsigned int has_template_arguments : 1;
581
abc72ce4
DE
582 /* Flag set if fixup_partial_die has been called on this die. */
583 unsigned int fixup_called : 1;
584
72bf9492 585 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 586 sometimes a default name for unnamed DIEs. */
c906108c 587 char *name;
72bf9492 588
abc72ce4
DE
589 /* The linkage name, if present. */
590 const char *linkage_name;
591
72bf9492
DJ
592 /* The scope to prepend to our children. This is generally
593 allocated on the comp_unit_obstack, so will disappear
594 when this compilation unit leaves the cache. */
595 char *scope;
596
597 /* The location description associated with this DIE, if any. */
598 struct dwarf_block *locdesc;
599
600 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
601 CORE_ADDR lowpc;
602 CORE_ADDR highpc;
72bf9492 603
93311388 604 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 605 DW_AT_sibling, if any. */
abc72ce4
DE
606 /* NOTE: This member isn't strictly necessary, read_partial_die could
607 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 608 gdb_byte *sibling;
72bf9492
DJ
609
610 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
611 DW_AT_specification (or DW_AT_abstract_origin or
612 DW_AT_extension). */
613 unsigned int spec_offset;
614
615 /* Pointers to this DIE's parent, first child, and next sibling,
616 if any. */
617 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
618 };
619
0963b4bd 620/* This data structure holds the information of an abbrev. */
c906108c
SS
621struct abbrev_info
622 {
623 unsigned int number; /* number identifying abbrev */
624 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
625 unsigned short has_children; /* boolean */
626 unsigned short num_attrs; /* number of attributes */
c906108c
SS
627 struct attr_abbrev *attrs; /* an array of attribute descriptions */
628 struct abbrev_info *next; /* next in chain */
629 };
630
631struct attr_abbrev
632 {
9d25dd43
DE
633 ENUM_BITFIELD(dwarf_attribute) name : 16;
634 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
635 };
636
0963b4bd 637/* Attributes have a name and a value. */
b60c80d6
DJ
638struct attribute
639 {
9d25dd43 640 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
641 ENUM_BITFIELD(dwarf_form) form : 15;
642
643 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
644 field should be in u.str (existing only for DW_STRING) but it is kept
645 here for better struct attribute alignment. */
646 unsigned int string_is_canonical : 1;
647
b60c80d6
DJ
648 union
649 {
650 char *str;
651 struct dwarf_block *blk;
43bbcdc2
PH
652 ULONGEST unsnd;
653 LONGEST snd;
b60c80d6 654 CORE_ADDR addr;
348e048f 655 struct signatured_type *signatured_type;
b60c80d6
DJ
656 }
657 u;
658 };
659
0963b4bd 660/* This data structure holds a complete die structure. */
c906108c
SS
661struct die_info
662 {
76815b17
DE
663 /* DWARF-2 tag for this DIE. */
664 ENUM_BITFIELD(dwarf_tag) tag : 16;
665
666 /* Number of attributes */
98bfdba5
PA
667 unsigned char num_attrs;
668
669 /* True if we're presently building the full type name for the
670 type derived from this DIE. */
671 unsigned char building_fullname : 1;
76815b17
DE
672
673 /* Abbrev number */
674 unsigned int abbrev;
675
93311388 676 /* Offset in .debug_info or .debug_types section. */
76815b17 677 unsigned int offset;
78ba4af6
JB
678
679 /* The dies in a compilation unit form an n-ary tree. PARENT
680 points to this die's parent; CHILD points to the first child of
681 this node; and all the children of a given node are chained
4950bc1c 682 together via their SIBLING fields. */
639d11d3
DC
683 struct die_info *child; /* Its first child, if any. */
684 struct die_info *sibling; /* Its next sibling, if any. */
685 struct die_info *parent; /* Its parent, if any. */
c906108c 686
b60c80d6
DJ
687 /* An array of attributes, with NUM_ATTRS elements. There may be
688 zero, but it's not common and zero-sized arrays are not
689 sufficiently portable C. */
690 struct attribute attrs[1];
c906108c
SS
691 };
692
5fb290d7
DJ
693struct function_range
694{
695 const char *name;
696 CORE_ADDR lowpc, highpc;
697 int seen_line;
698 struct function_range *next;
699};
700
0963b4bd 701/* Get at parts of an attribute structure. */
c906108c
SS
702
703#define DW_STRING(attr) ((attr)->u.str)
8285870a 704#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
705#define DW_UNSND(attr) ((attr)->u.unsnd)
706#define DW_BLOCK(attr) ((attr)->u.blk)
707#define DW_SND(attr) ((attr)->u.snd)
708#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 709#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 710
0963b4bd 711/* Blocks are a bunch of untyped bytes. */
c906108c
SS
712struct dwarf_block
713 {
714 unsigned int size;
fe1b8b76 715 gdb_byte *data;
c906108c
SS
716 };
717
c906108c
SS
718#ifndef ATTR_ALLOC_CHUNK
719#define ATTR_ALLOC_CHUNK 4
720#endif
721
c906108c
SS
722/* Allocate fields for structs, unions and enums in this size. */
723#ifndef DW_FIELD_ALLOC_CHUNK
724#define DW_FIELD_ALLOC_CHUNK 4
725#endif
726
c906108c
SS
727/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
728 but this would require a corresponding change in unpack_field_as_long
729 and friends. */
730static int bits_per_byte = 8;
731
732/* The routines that read and process dies for a C struct or C++ class
733 pass lists of data member fields and lists of member function fields
734 in an instance of a field_info structure, as defined below. */
735struct field_info
c5aa993b 736 {
0963b4bd 737 /* List of data member and baseclasses fields. */
c5aa993b
JM
738 struct nextfield
739 {
740 struct nextfield *next;
741 int accessibility;
742 int virtuality;
743 struct field field;
744 }
7d0ccb61 745 *fields, *baseclasses;
c906108c 746
7d0ccb61 747 /* Number of fields (including baseclasses). */
c5aa993b 748 int nfields;
c906108c 749
c5aa993b
JM
750 /* Number of baseclasses. */
751 int nbaseclasses;
c906108c 752
c5aa993b
JM
753 /* Set if the accesibility of one of the fields is not public. */
754 int non_public_fields;
c906108c 755
c5aa993b
JM
756 /* Member function fields array, entries are allocated in the order they
757 are encountered in the object file. */
758 struct nextfnfield
759 {
760 struct nextfnfield *next;
761 struct fn_field fnfield;
762 }
763 *fnfields;
c906108c 764
c5aa993b
JM
765 /* Member function fieldlist array, contains name of possibly overloaded
766 member function, number of overloaded member functions and a pointer
767 to the head of the member function field chain. */
768 struct fnfieldlist
769 {
770 char *name;
771 int length;
772 struct nextfnfield *head;
773 }
774 *fnfieldlists;
c906108c 775
c5aa993b
JM
776 /* Number of entries in the fnfieldlists array. */
777 int nfnfields;
98751a41
JK
778
779 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
780 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
781 struct typedef_field_list
782 {
783 struct typedef_field field;
784 struct typedef_field_list *next;
785 }
786 *typedef_field_list;
787 unsigned typedef_field_list_count;
c5aa993b 788 };
c906108c 789
10b3939b
DJ
790/* One item on the queue of compilation units to read in full symbols
791 for. */
792struct dwarf2_queue_item
793{
794 struct dwarf2_per_cu_data *per_cu;
795 struct dwarf2_queue_item *next;
796};
797
798/* The current queue. */
799static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
800
ae038cb0
DJ
801/* Loaded secondary compilation units are kept in memory until they
802 have not been referenced for the processing of this many
803 compilation units. Set this to zero to disable caching. Cache
804 sizes of up to at least twenty will improve startup time for
805 typical inter-CU-reference binaries, at an obvious memory cost. */
806static int dwarf2_max_cache_age = 5;
920d2a44
AC
807static void
808show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
809 struct cmd_list_element *c, const char *value)
810{
3e43a32a
MS
811 fprintf_filtered (file, _("The upper bound on the age of cached "
812 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
813 value);
814}
815
ae038cb0 816
0963b4bd 817/* Various complaints about symbol reading that don't abort the process. */
c906108c 818
4d3c2250
KB
819static void
820dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 821{
4d3c2250 822 complaint (&symfile_complaints,
e2e0b3e5 823 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
824}
825
25e43795
DJ
826static void
827dwarf2_debug_line_missing_file_complaint (void)
828{
829 complaint (&symfile_complaints,
830 _(".debug_line section has line data without a file"));
831}
832
59205f5a
JB
833static void
834dwarf2_debug_line_missing_end_sequence_complaint (void)
835{
836 complaint (&symfile_complaints,
3e43a32a
MS
837 _(".debug_line section has line "
838 "program sequence without an end"));
59205f5a
JB
839}
840
4d3c2250
KB
841static void
842dwarf2_complex_location_expr_complaint (void)
2e276125 843{
e2e0b3e5 844 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
845}
846
4d3c2250
KB
847static void
848dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
849 int arg3)
2e276125 850{
4d3c2250 851 complaint (&symfile_complaints,
3e43a32a
MS
852 _("const value length mismatch for '%s', got %d, expected %d"),
853 arg1, arg2, arg3);
4d3c2250
KB
854}
855
856static void
857dwarf2_macros_too_long_complaint (void)
2e276125 858{
4d3c2250 859 complaint (&symfile_complaints,
e2e0b3e5 860 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
861}
862
863static void
864dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 865{
4d3c2250 866 complaint (&symfile_complaints,
3e43a32a
MS
867 _("macro debug info contains a "
868 "malformed macro definition:\n`%s'"),
4d3c2250
KB
869 arg1);
870}
871
872static void
873dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 874{
4d3c2250 875 complaint (&symfile_complaints,
3e43a32a
MS
876 _("invalid attribute class or form for '%s' in '%s'"),
877 arg1, arg2);
4d3c2250 878}
c906108c 879
c906108c
SS
880/* local function prototypes */
881
4efb68b1 882static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 883
aaa75496
JB
884static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
885 struct objfile *);
886
c67a9c90 887static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 888
72bf9492
DJ
889static void scan_partial_symbols (struct partial_die_info *,
890 CORE_ADDR *, CORE_ADDR *,
5734ee8b 891 int, struct dwarf2_cu *);
c906108c 892
72bf9492
DJ
893static void add_partial_symbol (struct partial_die_info *,
894 struct dwarf2_cu *);
63d06c5c 895
72bf9492
DJ
896static void add_partial_namespace (struct partial_die_info *pdi,
897 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 898 int need_pc, struct dwarf2_cu *cu);
63d06c5c 899
5d7cb8df
JK
900static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
901 CORE_ADDR *highpc, int need_pc,
902 struct dwarf2_cu *cu);
903
72bf9492
DJ
904static void add_partial_enumeration (struct partial_die_info *enum_pdi,
905 struct dwarf2_cu *cu);
91c24f0a 906
bc30ff58
JB
907static void add_partial_subprogram (struct partial_die_info *pdi,
908 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 909 int need_pc, struct dwarf2_cu *cu);
bc30ff58 910
fe1b8b76 911static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
912 gdb_byte *buffer, gdb_byte *info_ptr,
913 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 914
a14ed312 915static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 916
a14ed312 917static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 918
e7c27a73 919static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 920
f3dd6933 921static void dwarf2_free_abbrev_table (void *);
c906108c 922
fe1b8b76 923static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 924 struct dwarf2_cu *);
72bf9492 925
57349743 926static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 927 struct dwarf2_cu *);
c906108c 928
93311388
DE
929static struct partial_die_info *load_partial_dies (bfd *,
930 gdb_byte *, gdb_byte *,
931 int, struct dwarf2_cu *);
72bf9492 932
fe1b8b76 933static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
934 struct abbrev_info *abbrev,
935 unsigned int, bfd *,
936 gdb_byte *, gdb_byte *,
937 struct dwarf2_cu *);
c906108c 938
c764a876 939static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 940 struct dwarf2_cu *);
72bf9492
DJ
941
942static void fixup_partial_die (struct partial_die_info *,
943 struct dwarf2_cu *);
944
fe1b8b76
JB
945static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
946 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 947
fe1b8b76
JB
948static gdb_byte *read_attribute_value (struct attribute *, unsigned,
949 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 950
fe1b8b76 951static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 952
fe1b8b76 953static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 954
fe1b8b76 955static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 956
fe1b8b76 957static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 958
93311388 959static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 960
fe1b8b76 961static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 962 unsigned int *);
c906108c 963
c764a876
DE
964static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
965
966static LONGEST read_checked_initial_length_and_offset
967 (bfd *, gdb_byte *, const struct comp_unit_head *,
968 unsigned int *, unsigned int *);
613e1657 969
fe1b8b76 970static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
971 unsigned int *);
972
973static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 974
fe1b8b76 975static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 976
9b1c24c8 977static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 978
fe1b8b76
JB
979static char *read_indirect_string (bfd *, gdb_byte *,
980 const struct comp_unit_head *,
981 unsigned int *);
4bdf3d34 982
fe1b8b76 983static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 984
fe1b8b76 985static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 986
fe1b8b76 987static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 988
e142c38c 989static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 990
e142c38c
DJ
991static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
992 struct dwarf2_cu *);
c906108c 993
348e048f
DE
994static struct attribute *dwarf2_attr_no_follow (struct die_info *,
995 unsigned int,
996 struct dwarf2_cu *);
997
05cf31d1
JB
998static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
999 struct dwarf2_cu *cu);
1000
e142c38c 1001static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1002
e142c38c 1003static struct die_info *die_specification (struct die_info *die,
f2f0e013 1004 struct dwarf2_cu **);
63d06c5c 1005
debd256d
JB
1006static void free_line_header (struct line_header *lh);
1007
aaa75496
JB
1008static void add_file_name (struct line_header *, char *, unsigned int,
1009 unsigned int, unsigned int);
1010
debd256d
JB
1011static struct line_header *(dwarf_decode_line_header
1012 (unsigned int offset,
e7c27a73 1013 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1014
72b9f47f 1015static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1016 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1017
72b9f47f 1018static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1019
a14ed312 1020static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1021 struct dwarf2_cu *);
c906108c 1022
34eaf542
TT
1023static struct symbol *new_symbol_full (struct die_info *, struct type *,
1024 struct dwarf2_cu *, struct symbol *);
1025
a14ed312 1026static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1027 struct dwarf2_cu *);
c906108c 1028
98bfdba5
PA
1029static void dwarf2_const_value_attr (struct attribute *attr,
1030 struct type *type,
1031 const char *name,
1032 struct obstack *obstack,
1033 struct dwarf2_cu *cu, long *value,
1034 gdb_byte **bytes,
1035 struct dwarf2_locexpr_baton **baton);
2df3850c 1036
e7c27a73 1037static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1038
b4ba55a1
JB
1039static int need_gnat_info (struct dwarf2_cu *);
1040
3e43a32a
MS
1041static struct type *die_descriptive_type (struct die_info *,
1042 struct dwarf2_cu *);
b4ba55a1
JB
1043
1044static void set_descriptive_type (struct type *, struct die_info *,
1045 struct dwarf2_cu *);
1046
e7c27a73
DJ
1047static struct type *die_containing_type (struct die_info *,
1048 struct dwarf2_cu *);
c906108c 1049
673bfd45
DE
1050static struct type *lookup_die_type (struct die_info *, struct attribute *,
1051 struct dwarf2_cu *);
c906108c 1052
f792889a 1053static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1054
673bfd45
DE
1055static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1056
086ed43d 1057static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1058
6e70227d 1059static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1060 const char *suffix, int physname,
1061 struct dwarf2_cu *cu);
63d06c5c 1062
e7c27a73 1063static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1064
348e048f
DE
1065static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1066
e7c27a73 1067static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1068
e7c27a73 1069static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1070
ff013f42
JK
1071static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1072 struct dwarf2_cu *, struct partial_symtab *);
1073
a14ed312 1074static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1075 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1076 struct partial_symtab *);
c906108c 1077
fae299cd
DC
1078static void get_scope_pc_bounds (struct die_info *,
1079 CORE_ADDR *, CORE_ADDR *,
1080 struct dwarf2_cu *);
1081
801e3a5b
JB
1082static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1083 CORE_ADDR, struct dwarf2_cu *);
1084
a14ed312 1085static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1086 struct dwarf2_cu *);
c906108c 1087
a14ed312 1088static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1089 struct type *, struct dwarf2_cu *);
c906108c 1090
a14ed312 1091static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1092 struct die_info *, struct type *,
e7c27a73 1093 struct dwarf2_cu *);
c906108c 1094
a14ed312 1095static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1096 struct type *,
1097 struct dwarf2_cu *);
c906108c 1098
134d01f1 1099static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1100
e7c27a73 1101static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1102
e7c27a73 1103static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1104
5d7cb8df
JK
1105static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1106
27aa8d6a
SW
1107static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1108
f55ee35c
JK
1109static struct type *read_module_type (struct die_info *die,
1110 struct dwarf2_cu *cu);
1111
38d518c9 1112static const char *namespace_name (struct die_info *die,
e142c38c 1113 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1114
134d01f1 1115static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1116
e7c27a73 1117static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1118
6e70227d 1119static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1120 struct dwarf2_cu *);
1121
93311388 1122static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1123
93311388
DE
1124static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1125 gdb_byte *info_ptr,
d97bc12b
DE
1126 gdb_byte **new_info_ptr,
1127 struct die_info *parent);
1128
93311388
DE
1129static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1130 gdb_byte *info_ptr,
fe1b8b76 1131 gdb_byte **new_info_ptr,
639d11d3
DC
1132 struct die_info *parent);
1133
93311388
DE
1134static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1135 gdb_byte *info_ptr,
fe1b8b76 1136 gdb_byte **new_info_ptr,
639d11d3
DC
1137 struct die_info *parent);
1138
93311388
DE
1139static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1140 struct die_info **, gdb_byte *,
1141 int *);
1142
e7c27a73 1143static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1144
71c25dea
TT
1145static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1146 struct obstack *);
1147
e142c38c 1148static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1149
98bfdba5
PA
1150static const char *dwarf2_full_name (char *name,
1151 struct die_info *die,
1152 struct dwarf2_cu *cu);
1153
e142c38c 1154static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1155 struct dwarf2_cu **);
9219021c 1156
a14ed312 1157static char *dwarf_tag_name (unsigned int);
c906108c 1158
a14ed312 1159static char *dwarf_attr_name (unsigned int);
c906108c 1160
a14ed312 1161static char *dwarf_form_name (unsigned int);
c906108c 1162
a14ed312 1163static char *dwarf_bool_name (unsigned int);
c906108c 1164
a14ed312 1165static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1166
1167#if 0
a14ed312 1168static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1169#endif
1170
f9aca02d 1171static struct die_info *sibling_die (struct die_info *);
c906108c 1172
d97bc12b
DE
1173static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1174
1175static void dump_die_for_error (struct die_info *);
1176
1177static void dump_die_1 (struct ui_file *, int level, int max_level,
1178 struct die_info *);
c906108c 1179
d97bc12b 1180/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1181
51545339 1182static void store_in_ref_table (struct die_info *,
10b3939b 1183 struct dwarf2_cu *);
c906108c 1184
93311388
DE
1185static int is_ref_attr (struct attribute *);
1186
c764a876 1187static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1188
43bbcdc2 1189static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1190
348e048f
DE
1191static struct die_info *follow_die_ref_or_sig (struct die_info *,
1192 struct attribute *,
1193 struct dwarf2_cu **);
1194
10b3939b
DJ
1195static struct die_info *follow_die_ref (struct die_info *,
1196 struct attribute *,
f2f0e013 1197 struct dwarf2_cu **);
c906108c 1198
348e048f
DE
1199static struct die_info *follow_die_sig (struct die_info *,
1200 struct attribute *,
1201 struct dwarf2_cu **);
1202
1203static void read_signatured_type_at_offset (struct objfile *objfile,
1204 unsigned int offset);
1205
1206static void read_signatured_type (struct objfile *,
1207 struct signatured_type *type_sig);
1208
c906108c
SS
1209/* memory allocation interface */
1210
7b5a2f43 1211static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1212
f3dd6933 1213static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1214
b60c80d6 1215static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1216
e142c38c 1217static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1218
e142c38c
DJ
1219static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1220 struct dwarf2_cu *);
5fb290d7 1221
2e276125 1222static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1223 char *, bfd *, struct dwarf2_cu *);
2e276125 1224
8e19ed76
PS
1225static int attr_form_is_block (struct attribute *);
1226
3690dd37
JB
1227static int attr_form_is_section_offset (struct attribute *);
1228
1229static int attr_form_is_constant (struct attribute *);
1230
8cf6f0b1
TT
1231static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1232 struct dwarf2_loclist_baton *baton,
1233 struct attribute *attr);
1234
93e7bd98
DJ
1235static void dwarf2_symbol_mark_computed (struct attribute *attr,
1236 struct symbol *sym,
1237 struct dwarf2_cu *cu);
4c2df51b 1238
93311388
DE
1239static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1240 struct abbrev_info *abbrev,
1241 struct dwarf2_cu *cu);
4bb7a0a7 1242
72bf9492
DJ
1243static void free_stack_comp_unit (void *);
1244
72bf9492
DJ
1245static hashval_t partial_die_hash (const void *item);
1246
1247static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1248
ae038cb0 1249static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1250 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1251
1252static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1253 (unsigned int offset, struct objfile *objfile);
ae038cb0 1254
9816fde3
JK
1255static void init_one_comp_unit (struct dwarf2_cu *cu,
1256 struct objfile *objfile);
1257
1258static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1259 struct die_info *comp_unit_die);
93311388 1260
ae038cb0
DJ
1261static void free_one_comp_unit (void *);
1262
1263static void free_cached_comp_units (void *);
1264
1265static void age_cached_comp_units (void);
1266
1267static void free_one_cached_comp_unit (void *);
1268
f792889a
DJ
1269static struct type *set_die_type (struct die_info *, struct type *,
1270 struct dwarf2_cu *);
1c379e20 1271
ae038cb0
DJ
1272static void create_all_comp_units (struct objfile *);
1273
1fd400ff
TT
1274static int create_debug_types_hash_table (struct objfile *objfile);
1275
93311388
DE
1276static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1277 struct objfile *);
10b3939b
DJ
1278
1279static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1280
1281static void dwarf2_add_dependence (struct dwarf2_cu *,
1282 struct dwarf2_per_cu_data *);
1283
ae038cb0
DJ
1284static void dwarf2_mark (struct dwarf2_cu *);
1285
1286static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1287
673bfd45
DE
1288static struct type *get_die_type_at_offset (unsigned int,
1289 struct dwarf2_per_cu_data *per_cu);
1290
f792889a 1291static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1292
9291a0cd
TT
1293static void dwarf2_release_queue (void *dummy);
1294
1295static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1296 struct objfile *objfile);
1297
1298static void process_queue (struct objfile *objfile);
1299
1300static void find_file_and_directory (struct die_info *die,
1301 struct dwarf2_cu *cu,
1302 char **name, char **comp_dir);
1303
1304static char *file_full_name (int file, struct line_header *lh,
1305 const char *comp_dir);
1306
1307static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1308 gdb_byte *info_ptr,
1309 gdb_byte *buffer,
1310 unsigned int buffer_size,
1311 bfd *abfd);
1312
1313static void init_cu_die_reader (struct die_reader_specs *reader,
1314 struct dwarf2_cu *cu);
1315
673bfd45 1316static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1317
9291a0cd
TT
1318#if WORDS_BIGENDIAN
1319
1320/* Convert VALUE between big- and little-endian. */
1321static offset_type
1322byte_swap (offset_type value)
1323{
1324 offset_type result;
1325
1326 result = (value & 0xff) << 24;
1327 result |= (value & 0xff00) << 8;
1328 result |= (value & 0xff0000) >> 8;
1329 result |= (value & 0xff000000) >> 24;
1330 return result;
1331}
1332
1333#define MAYBE_SWAP(V) byte_swap (V)
1334
1335#else
1336#define MAYBE_SWAP(V) (V)
1337#endif /* WORDS_BIGENDIAN */
1338
1339/* The suffix for an index file. */
1340#define INDEX_SUFFIX ".gdb-index"
1341
3da10d80
KS
1342static const char *dwarf2_physname (char *name, struct die_info *die,
1343 struct dwarf2_cu *cu);
1344
c906108c 1345/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1346 information and return true if we have enough to do something.
1347 NAMES points to the dwarf2 section names, or is NULL if the standard
1348 ELF names are used. */
c906108c
SS
1349
1350int
251d32d9
TG
1351dwarf2_has_info (struct objfile *objfile,
1352 const struct dwarf2_debug_sections *names)
c906108c 1353{
be391dca
TT
1354 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1355 if (!dwarf2_per_objfile)
1356 {
1357 /* Initialize per-objfile state. */
1358 struct dwarf2_per_objfile *data
1359 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1360
be391dca
TT
1361 memset (data, 0, sizeof (*data));
1362 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1363 dwarf2_per_objfile = data;
6502dd73 1364
251d32d9
TG
1365 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1366 (void *) names);
be391dca
TT
1367 dwarf2_per_objfile->objfile = objfile;
1368 }
1369 return (dwarf2_per_objfile->info.asection != NULL
1370 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1371}
1372
251d32d9
TG
1373/* When loading sections, we look either for uncompressed section or for
1374 compressed section names. */
233a11ab
CS
1375
1376static int
251d32d9
TG
1377section_is_p (const char *section_name,
1378 const struct dwarf2_section_names *names)
233a11ab 1379{
251d32d9
TG
1380 if (names->normal != NULL
1381 && strcmp (section_name, names->normal) == 0)
1382 return 1;
1383 if (names->compressed != NULL
1384 && strcmp (section_name, names->compressed) == 0)
1385 return 1;
1386 return 0;
233a11ab
CS
1387}
1388
c906108c
SS
1389/* This function is mapped across the sections and remembers the
1390 offset and size of each of the debugging sections we are interested
1391 in. */
1392
1393static void
251d32d9 1394dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1395{
251d32d9
TG
1396 const struct dwarf2_debug_sections *names;
1397
1398 if (vnames == NULL)
1399 names = &dwarf2_elf_names;
1400 else
1401 names = (const struct dwarf2_debug_sections *) vnames;
1402
1403 if (section_is_p (sectp->name, &names->info))
c906108c 1404 {
dce234bc
PP
1405 dwarf2_per_objfile->info.asection = sectp;
1406 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1407 }
251d32d9 1408 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1409 {
dce234bc
PP
1410 dwarf2_per_objfile->abbrev.asection = sectp;
1411 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1412 }
251d32d9 1413 else if (section_is_p (sectp->name, &names->line))
c906108c 1414 {
dce234bc
PP
1415 dwarf2_per_objfile->line.asection = sectp;
1416 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1417 }
251d32d9 1418 else if (section_is_p (sectp->name, &names->loc))
c906108c 1419 {
dce234bc
PP
1420 dwarf2_per_objfile->loc.asection = sectp;
1421 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1422 }
251d32d9 1423 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1424 {
dce234bc
PP
1425 dwarf2_per_objfile->macinfo.asection = sectp;
1426 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1427 }
251d32d9 1428 else if (section_is_p (sectp->name, &names->str))
c906108c 1429 {
dce234bc
PP
1430 dwarf2_per_objfile->str.asection = sectp;
1431 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1432 }
251d32d9 1433 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1434 {
dce234bc
PP
1435 dwarf2_per_objfile->frame.asection = sectp;
1436 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1437 }
251d32d9 1438 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1439 {
3799ccc6 1440 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1441
3799ccc6
EZ
1442 if (aflag & SEC_HAS_CONTENTS)
1443 {
dce234bc
PP
1444 dwarf2_per_objfile->eh_frame.asection = sectp;
1445 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1446 }
b6af0555 1447 }
251d32d9 1448 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1449 {
dce234bc
PP
1450 dwarf2_per_objfile->ranges.asection = sectp;
1451 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1452 }
251d32d9 1453 else if (section_is_p (sectp->name, &names->types))
348e048f
DE
1454 {
1455 dwarf2_per_objfile->types.asection = sectp;
1456 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1457 }
251d32d9 1458 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1459 {
1460 dwarf2_per_objfile->gdb_index.asection = sectp;
1461 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1462 }
dce234bc 1463
72dca2f5
FR
1464 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1465 && bfd_section_vma (abfd, sectp) == 0)
1466 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1467}
1468
dce234bc
PP
1469/* Decompress a section that was compressed using zlib. Store the
1470 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1471
1472static void
dce234bc
PP
1473zlib_decompress_section (struct objfile *objfile, asection *sectp,
1474 gdb_byte **outbuf, bfd_size_type *outsize)
1475{
1476 bfd *abfd = objfile->obfd;
1477#ifndef HAVE_ZLIB_H
1478 error (_("Support for zlib-compressed DWARF data (from '%s') "
1479 "is disabled in this copy of GDB"),
1480 bfd_get_filename (abfd));
1481#else
1482 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1483 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1484 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1485 bfd_size_type uncompressed_size;
1486 gdb_byte *uncompressed_buffer;
1487 z_stream strm;
1488 int rc;
1489 int header_size = 12;
1490
1491 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1492 || bfd_bread (compressed_buffer,
1493 compressed_size, abfd) != compressed_size)
dce234bc
PP
1494 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1495 bfd_get_filename (abfd));
1496
1497 /* Read the zlib header. In this case, it should be "ZLIB" followed
1498 by the uncompressed section size, 8 bytes in big-endian order. */
1499 if (compressed_size < header_size
1500 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1501 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1502 bfd_get_filename (abfd));
1503 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1504 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1505 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1506 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1507 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1508 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1509 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1510 uncompressed_size += compressed_buffer[11];
1511
1512 /* It is possible the section consists of several compressed
1513 buffers concatenated together, so we uncompress in a loop. */
1514 strm.zalloc = NULL;
1515 strm.zfree = NULL;
1516 strm.opaque = NULL;
1517 strm.avail_in = compressed_size - header_size;
1518 strm.next_in = (Bytef*) compressed_buffer + header_size;
1519 strm.avail_out = uncompressed_size;
1520 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1521 uncompressed_size);
1522 rc = inflateInit (&strm);
1523 while (strm.avail_in > 0)
1524 {
1525 if (rc != Z_OK)
1526 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1527 bfd_get_filename (abfd), rc);
1528 strm.next_out = ((Bytef*) uncompressed_buffer
1529 + (uncompressed_size - strm.avail_out));
1530 rc = inflate (&strm, Z_FINISH);
1531 if (rc != Z_STREAM_END)
1532 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1533 bfd_get_filename (abfd), rc);
1534 rc = inflateReset (&strm);
1535 }
1536 rc = inflateEnd (&strm);
1537 if (rc != Z_OK
1538 || strm.avail_out != 0)
1539 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1540 bfd_get_filename (abfd), rc);
1541
affddf13 1542 do_cleanups (cleanup);
dce234bc
PP
1543 *outbuf = uncompressed_buffer;
1544 *outsize = uncompressed_size;
1545#endif
233a11ab
CS
1546}
1547
9e0ac564
TT
1548/* A helper function that decides whether a section is empty. */
1549
1550static int
1551dwarf2_section_empty_p (struct dwarf2_section_info *info)
1552{
1553 return info->asection == NULL || info->size == 0;
1554}
1555
dce234bc
PP
1556/* Read the contents of the section SECTP from object file specified by
1557 OBJFILE, store info about the section into INFO.
1558 If the section is compressed, uncompress it before returning. */
c906108c 1559
dce234bc
PP
1560static void
1561dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1562{
dce234bc
PP
1563 bfd *abfd = objfile->obfd;
1564 asection *sectp = info->asection;
1565 gdb_byte *buf, *retbuf;
1566 unsigned char header[4];
c906108c 1567
be391dca
TT
1568 if (info->readin)
1569 return;
dce234bc 1570 info->buffer = NULL;
b315ab21 1571 info->map_addr = NULL;
be391dca 1572 info->readin = 1;
188dd5d6 1573
9e0ac564 1574 if (dwarf2_section_empty_p (info))
dce234bc 1575 return;
c906108c 1576
dce234bc
PP
1577 /* Check if the file has a 4-byte header indicating compression. */
1578 if (info->size > sizeof (header)
1579 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1580 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1581 {
1582 /* Upon decompression, update the buffer and its size. */
1583 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1584 {
1585 zlib_decompress_section (objfile, sectp, &info->buffer,
1586 &info->size);
1587 return;
1588 }
1589 }
4bdf3d34 1590
dce234bc
PP
1591#ifdef HAVE_MMAP
1592 if (pagesize == 0)
1593 pagesize = getpagesize ();
2e276125 1594
dce234bc
PP
1595 /* Only try to mmap sections which are large enough: we don't want to
1596 waste space due to fragmentation. Also, only try mmap for sections
1597 without relocations. */
1598
1599 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1600 {
b315ab21
TG
1601 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1602 MAP_PRIVATE, sectp->filepos,
1603 &info->map_addr, &info->map_len);
dce234bc 1604
b315ab21 1605 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1606 {
be391dca 1607#if HAVE_POSIX_MADVISE
b315ab21 1608 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1609#endif
dce234bc
PP
1610 return;
1611 }
1612 }
1613#endif
1614
1615 /* If we get here, we are a normal, not-compressed section. */
1616 info->buffer = buf
1617 = obstack_alloc (&objfile->objfile_obstack, info->size);
1618
1619 /* When debugging .o files, we may need to apply relocations; see
1620 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1621 We never compress sections in .o files, so we only need to
1622 try this when the section is not compressed. */
ac8035ab 1623 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1624 if (retbuf != NULL)
1625 {
1626 info->buffer = retbuf;
1627 return;
1628 }
1629
1630 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1631 || bfd_bread (buf, info->size, abfd) != info->size)
1632 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1633 bfd_get_filename (abfd));
1634}
1635
9e0ac564
TT
1636/* A helper function that returns the size of a section in a safe way.
1637 If you are positive that the section has been read before using the
1638 size, then it is safe to refer to the dwarf2_section_info object's
1639 "size" field directly. In other cases, you must call this
1640 function, because for compressed sections the size field is not set
1641 correctly until the section has been read. */
1642
1643static bfd_size_type
1644dwarf2_section_size (struct objfile *objfile,
1645 struct dwarf2_section_info *info)
1646{
1647 if (!info->readin)
1648 dwarf2_read_section (objfile, info);
1649 return info->size;
1650}
1651
dce234bc 1652/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1653 SECTION_NAME. */
af34e669 1654
dce234bc 1655void
3017a003
TG
1656dwarf2_get_section_info (struct objfile *objfile,
1657 enum dwarf2_section_enum sect,
dce234bc
PP
1658 asection **sectp, gdb_byte **bufp,
1659 bfd_size_type *sizep)
1660{
1661 struct dwarf2_per_objfile *data
1662 = objfile_data (objfile, dwarf2_objfile_data_key);
1663 struct dwarf2_section_info *info;
a3b2a86b
TT
1664
1665 /* We may see an objfile without any DWARF, in which case we just
1666 return nothing. */
1667 if (data == NULL)
1668 {
1669 *sectp = NULL;
1670 *bufp = NULL;
1671 *sizep = 0;
1672 return;
1673 }
3017a003
TG
1674 switch (sect)
1675 {
1676 case DWARF2_DEBUG_FRAME:
1677 info = &data->frame;
1678 break;
1679 case DWARF2_EH_FRAME:
1680 info = &data->eh_frame;
1681 break;
1682 default:
1683 gdb_assert_not_reached ("unexpected section");
1684 }
dce234bc 1685
9e0ac564 1686 dwarf2_read_section (objfile, info);
dce234bc
PP
1687
1688 *sectp = info->asection;
1689 *bufp = info->buffer;
1690 *sizep = info->size;
1691}
1692
9291a0cd 1693\f
7b9f3c50
DE
1694/* DWARF quick_symbols_functions support. */
1695
1696/* TUs can share .debug_line entries, and there can be a lot more TUs than
1697 unique line tables, so we maintain a separate table of all .debug_line
1698 derived entries to support the sharing.
1699 All the quick functions need is the list of file names. We discard the
1700 line_header when we're done and don't need to record it here. */
1701struct quick_file_names
1702{
1703 /* The offset in .debug_line of the line table. We hash on this. */
1704 unsigned int offset;
1705
1706 /* The number of entries in file_names, real_names. */
1707 unsigned int num_file_names;
1708
1709 /* The file names from the line table, after being run through
1710 file_full_name. */
1711 const char **file_names;
1712
1713 /* The file names from the line table after being run through
1714 gdb_realpath. These are computed lazily. */
1715 const char **real_names;
1716};
1717
1718/* When using the index (and thus not using psymtabs), each CU has an
1719 object of this type. This is used to hold information needed by
1720 the various "quick" methods. */
1721struct dwarf2_per_cu_quick_data
1722{
1723 /* The file table. This can be NULL if there was no file table
1724 or it's currently not read in.
1725 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1726 struct quick_file_names *file_names;
1727
1728 /* The corresponding symbol table. This is NULL if symbols for this
1729 CU have not yet been read. */
1730 struct symtab *symtab;
1731
1732 /* A temporary mark bit used when iterating over all CUs in
1733 expand_symtabs_matching. */
1734 unsigned int mark : 1;
1735
1736 /* True if we've tried to read the file table and found there isn't one.
1737 There will be no point in trying to read it again next time. */
1738 unsigned int no_file_data : 1;
1739};
1740
1741/* Hash function for a quick_file_names. */
1742
1743static hashval_t
1744hash_file_name_entry (const void *e)
1745{
1746 const struct quick_file_names *file_data = e;
1747
1748 return file_data->offset;
1749}
1750
1751/* Equality function for a quick_file_names. */
1752
1753static int
1754eq_file_name_entry (const void *a, const void *b)
1755{
1756 const struct quick_file_names *ea = a;
1757 const struct quick_file_names *eb = b;
1758
1759 return ea->offset == eb->offset;
1760}
1761
1762/* Delete function for a quick_file_names. */
1763
1764static void
1765delete_file_name_entry (void *e)
1766{
1767 struct quick_file_names *file_data = e;
1768 int i;
1769
1770 for (i = 0; i < file_data->num_file_names; ++i)
1771 {
1772 xfree ((void*) file_data->file_names[i]);
1773 if (file_data->real_names)
1774 xfree ((void*) file_data->real_names[i]);
1775 }
1776
1777 /* The space for the struct itself lives on objfile_obstack,
1778 so we don't free it here. */
1779}
1780
1781/* Create a quick_file_names hash table. */
1782
1783static htab_t
1784create_quick_file_names_table (unsigned int nr_initial_entries)
1785{
1786 return htab_create_alloc (nr_initial_entries,
1787 hash_file_name_entry, eq_file_name_entry,
1788 delete_file_name_entry, xcalloc, xfree);
1789}
9291a0cd
TT
1790
1791/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1792 this CU came. */
2fdf6df6 1793
9291a0cd
TT
1794static void
1795dw2_do_instantiate_symtab (struct objfile *objfile,
1796 struct dwarf2_per_cu_data *per_cu)
1797{
1798 struct cleanup *back_to;
1799
1800 back_to = make_cleanup (dwarf2_release_queue, NULL);
1801
1802 queue_comp_unit (per_cu, objfile);
1803
1804 if (per_cu->from_debug_types)
1805 read_signatured_type_at_offset (objfile, per_cu->offset);
1806 else
1807 load_full_comp_unit (per_cu, objfile);
1808
1809 process_queue (objfile);
1810
1811 /* Age the cache, releasing compilation units that have not
1812 been used recently. */
1813 age_cached_comp_units ();
1814
1815 do_cleanups (back_to);
1816}
1817
1818/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1819 the objfile from which this CU came. Returns the resulting symbol
1820 table. */
2fdf6df6 1821
9291a0cd
TT
1822static struct symtab *
1823dw2_instantiate_symtab (struct objfile *objfile,
1824 struct dwarf2_per_cu_data *per_cu)
1825{
1826 if (!per_cu->v.quick->symtab)
1827 {
1828 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1829 increment_reading_symtab ();
1830 dw2_do_instantiate_symtab (objfile, per_cu);
1831 do_cleanups (back_to);
1832 }
1833 return per_cu->v.quick->symtab;
1834}
1835
1fd400ff 1836/* Return the CU given its index. */
2fdf6df6 1837
1fd400ff
TT
1838static struct dwarf2_per_cu_data *
1839dw2_get_cu (int index)
1840{
1841 if (index >= dwarf2_per_objfile->n_comp_units)
1842 {
1843 index -= dwarf2_per_objfile->n_comp_units;
1844 return dwarf2_per_objfile->type_comp_units[index];
1845 }
1846 return dwarf2_per_objfile->all_comp_units[index];
1847}
1848
9291a0cd
TT
1849/* A helper function that knows how to read a 64-bit value in a way
1850 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1851 otherwise. */
2fdf6df6 1852
9291a0cd
TT
1853static int
1854extract_cu_value (const char *bytes, ULONGEST *result)
1855{
1856 if (sizeof (ULONGEST) < 8)
1857 {
1858 int i;
1859
1860 /* Ignore the upper 4 bytes if they are all zero. */
1861 for (i = 0; i < 4; ++i)
1862 if (bytes[i + 4] != 0)
1863 return 0;
1864
1865 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1866 }
1867 else
1868 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1869 return 1;
1870}
1871
1872/* Read the CU list from the mapped index, and use it to create all
1873 the CU objects for this objfile. Return 0 if something went wrong,
1874 1 if everything went ok. */
2fdf6df6 1875
9291a0cd 1876static int
1fd400ff
TT
1877create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1878 offset_type cu_list_elements)
9291a0cd
TT
1879{
1880 offset_type i;
9291a0cd
TT
1881
1882 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1883 dwarf2_per_objfile->all_comp_units
1884 = obstack_alloc (&objfile->objfile_obstack,
1885 dwarf2_per_objfile->n_comp_units
1886 * sizeof (struct dwarf2_per_cu_data *));
1887
1888 for (i = 0; i < cu_list_elements; i += 2)
1889 {
1890 struct dwarf2_per_cu_data *the_cu;
1891 ULONGEST offset, length;
1892
1893 if (!extract_cu_value (cu_list, &offset)
1894 || !extract_cu_value (cu_list + 8, &length))
1895 return 0;
1896 cu_list += 2 * 8;
1897
1898 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1899 struct dwarf2_per_cu_data);
1900 the_cu->offset = offset;
1901 the_cu->length = length;
1902 the_cu->objfile = objfile;
1903 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1904 struct dwarf2_per_cu_quick_data);
1905 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1906 }
1907
1908 return 1;
1909}
1910
1fd400ff 1911/* Create the signatured type hash table from the index. */
673bfd45 1912
1fd400ff 1913static int
673bfd45
DE
1914create_signatured_type_table_from_index (struct objfile *objfile,
1915 const gdb_byte *bytes,
1916 offset_type elements)
1fd400ff
TT
1917{
1918 offset_type i;
673bfd45 1919 htab_t sig_types_hash;
1fd400ff
TT
1920
1921 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1922 dwarf2_per_objfile->type_comp_units
1923 = obstack_alloc (&objfile->objfile_obstack,
1924 dwarf2_per_objfile->n_type_comp_units
1925 * sizeof (struct dwarf2_per_cu_data *));
1926
673bfd45 1927 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1928
1929 for (i = 0; i < elements; i += 3)
1930 {
1931 struct signatured_type *type_sig;
1932 ULONGEST offset, type_offset, signature;
1933 void **slot;
1934
1935 if (!extract_cu_value (bytes, &offset)
1936 || !extract_cu_value (bytes + 8, &type_offset))
1937 return 0;
1938 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1939 bytes += 3 * 8;
1940
1941 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1942 struct signatured_type);
1943 type_sig->signature = signature;
1fd400ff
TT
1944 type_sig->type_offset = type_offset;
1945 type_sig->per_cu.from_debug_types = 1;
1946 type_sig->per_cu.offset = offset;
1947 type_sig->per_cu.objfile = objfile;
1948 type_sig->per_cu.v.quick
1949 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1950 struct dwarf2_per_cu_quick_data);
1951
673bfd45 1952 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
1953 *slot = type_sig;
1954
1955 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1956 }
1957
673bfd45 1958 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
1959
1960 return 1;
1961}
1962
9291a0cd
TT
1963/* Read the address map data from the mapped index, and use it to
1964 populate the objfile's psymtabs_addrmap. */
2fdf6df6 1965
9291a0cd
TT
1966static void
1967create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1968{
1969 const gdb_byte *iter, *end;
1970 struct obstack temp_obstack;
1971 struct addrmap *mutable_map;
1972 struct cleanup *cleanup;
1973 CORE_ADDR baseaddr;
1974
1975 obstack_init (&temp_obstack);
1976 cleanup = make_cleanup_obstack_free (&temp_obstack);
1977 mutable_map = addrmap_create_mutable (&temp_obstack);
1978
1979 iter = index->address_table;
1980 end = iter + index->address_table_size;
1981
1982 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1983
1984 while (iter < end)
1985 {
1986 ULONGEST hi, lo, cu_index;
1987 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1988 iter += 8;
1989 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1990 iter += 8;
1991 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1992 iter += 4;
1993
1994 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1995 dw2_get_cu (cu_index));
9291a0cd
TT
1996 }
1997
1998 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1999 &objfile->objfile_obstack);
2000 do_cleanups (cleanup);
2001}
2002
59d7bcaf
JK
2003/* The hash function for strings in the mapped index. This is the same as
2004 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2005 implementation. This is necessary because the hash function is tied to the
2006 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2007 SYMBOL_HASH_NEXT.
2008
2009 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2010
9291a0cd 2011static hashval_t
559a7a62 2012mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2013{
2014 const unsigned char *str = (const unsigned char *) p;
2015 hashval_t r = 0;
2016 unsigned char c;
2017
2018 while ((c = *str++) != 0)
559a7a62
JK
2019 {
2020 if (index_version >= 5)
2021 c = tolower (c);
2022 r = r * 67 + c - 113;
2023 }
9291a0cd
TT
2024
2025 return r;
2026}
2027
2028/* Find a slot in the mapped index INDEX for the object named NAME.
2029 If NAME is found, set *VEC_OUT to point to the CU vector in the
2030 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2031
9291a0cd
TT
2032static int
2033find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2034 offset_type **vec_out)
2035{
0cf03b49
JK
2036 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2037 offset_type hash;
9291a0cd 2038 offset_type slot, step;
559a7a62 2039 int (*cmp) (const char *, const char *);
9291a0cd 2040
0cf03b49
JK
2041 if (current_language->la_language == language_cplus
2042 || current_language->la_language == language_java
2043 || current_language->la_language == language_fortran)
2044 {
2045 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2046 not contain any. */
2047 const char *paren = strchr (name, '(');
2048
2049 if (paren)
2050 {
2051 char *dup;
2052
2053 dup = xmalloc (paren - name + 1);
2054 memcpy (dup, name, paren - name);
2055 dup[paren - name] = 0;
2056
2057 make_cleanup (xfree, dup);
2058 name = dup;
2059 }
2060 }
2061
559a7a62
JK
2062 /* Index version 4 did not support case insensitive searches. But the
2063 indexes for case insensitive languages are built in lowercase, therefore
2064 simulate our NAME being searched is also lowercased. */
2065 hash = mapped_index_string_hash ((index->version == 4
2066 && case_sensitivity == case_sensitive_off
2067 ? 5 : index->version),
2068 name);
2069
3876f04e
DE
2070 slot = hash & (index->symbol_table_slots - 1);
2071 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2072 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2073
2074 for (;;)
2075 {
2076 /* Convert a slot number to an offset into the table. */
2077 offset_type i = 2 * slot;
2078 const char *str;
3876f04e 2079 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2080 {
2081 do_cleanups (back_to);
2082 return 0;
2083 }
9291a0cd 2084
3876f04e 2085 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2086 if (!cmp (name, str))
9291a0cd
TT
2087 {
2088 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2089 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2090 do_cleanups (back_to);
9291a0cd
TT
2091 return 1;
2092 }
2093
3876f04e 2094 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2095 }
2096}
2097
2098/* Read the index file. If everything went ok, initialize the "quick"
2099 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2100
9291a0cd
TT
2101static int
2102dwarf2_read_index (struct objfile *objfile)
2103{
9291a0cd
TT
2104 char *addr;
2105 struct mapped_index *map;
b3b272e1 2106 offset_type *metadata;
ac0b195c
KW
2107 const gdb_byte *cu_list;
2108 const gdb_byte *types_list = NULL;
2109 offset_type version, cu_list_elements;
2110 offset_type types_list_elements = 0;
1fd400ff 2111 int i;
9291a0cd 2112
9e0ac564 2113 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2114 return 0;
82430852
JK
2115
2116 /* Older elfutils strip versions could keep the section in the main
2117 executable while splitting it for the separate debug info file. */
2118 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2119 & SEC_HAS_CONTENTS) == 0)
2120 return 0;
2121
9291a0cd
TT
2122 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2123
2124 addr = dwarf2_per_objfile->gdb_index.buffer;
2125 /* Version check. */
1fd400ff 2126 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2127 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2128 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2129 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2130 indices. Index version 4 uses a different hash function than index
2131 version 5 and later. */
831adc1f 2132 if (version < 4)
9291a0cd 2133 return 0;
594e8718
JK
2134 /* Indexes with higher version than the one supported by GDB may be no
2135 longer backward compatible. */
559a7a62 2136 if (version > 5)
594e8718 2137 return 0;
9291a0cd
TT
2138
2139 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2140 map->version = version;
b3b272e1 2141 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2142
2143 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2144
2145 i = 0;
2146 cu_list = addr + MAYBE_SWAP (metadata[i]);
2147 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2148 / 8);
1fd400ff
TT
2149 ++i;
2150
987d643c
TT
2151 types_list = addr + MAYBE_SWAP (metadata[i]);
2152 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2153 - MAYBE_SWAP (metadata[i]))
2154 / 8);
2155 ++i;
1fd400ff
TT
2156
2157 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2158 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2159 - MAYBE_SWAP (metadata[i]));
2160 ++i;
2161
3876f04e
DE
2162 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2163 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2164 - MAYBE_SWAP (metadata[i]))
2165 / (2 * sizeof (offset_type)));
1fd400ff 2166 ++i;
9291a0cd 2167
1fd400ff
TT
2168 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2169
2170 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2171 return 0;
2172
987d643c 2173 if (types_list_elements
673bfd45
DE
2174 && !create_signatured_type_table_from_index (objfile, types_list,
2175 types_list_elements))
9291a0cd
TT
2176 return 0;
2177
2178 create_addrmap_from_index (objfile, map);
2179
2180 dwarf2_per_objfile->index_table = map;
2181 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2182 dwarf2_per_objfile->quick_file_names_table =
2183 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2184
2185 return 1;
2186}
2187
2188/* A helper for the "quick" functions which sets the global
2189 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2190
9291a0cd
TT
2191static void
2192dw2_setup (struct objfile *objfile)
2193{
2194 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2195 gdb_assert (dwarf2_per_objfile);
2196}
2197
2198/* A helper for the "quick" functions which attempts to read the line
2199 table for THIS_CU. */
2fdf6df6 2200
7b9f3c50
DE
2201static struct quick_file_names *
2202dw2_get_file_names (struct objfile *objfile,
2203 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2204{
2205 bfd *abfd = objfile->obfd;
7b9f3c50 2206 struct line_header *lh;
9291a0cd
TT
2207 struct attribute *attr;
2208 struct cleanup *cleanups;
2209 struct die_info *comp_unit_die;
36374493 2210 struct dwarf2_section_info* sec;
9291a0cd
TT
2211 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2212 int has_children, i;
2213 struct dwarf2_cu cu;
2214 unsigned int bytes_read, buffer_size;
2215 struct die_reader_specs reader_specs;
2216 char *name, *comp_dir;
7b9f3c50
DE
2217 void **slot;
2218 struct quick_file_names *qfn;
2219 unsigned int line_offset;
9291a0cd 2220
7b9f3c50
DE
2221 if (this_cu->v.quick->file_names != NULL)
2222 return this_cu->v.quick->file_names;
2223 /* If we know there is no line data, no point in looking again. */
2224 if (this_cu->v.quick->no_file_data)
2225 return NULL;
9291a0cd 2226
9816fde3 2227 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2228 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2229
36374493
DE
2230 if (this_cu->from_debug_types)
2231 sec = &dwarf2_per_objfile->types;
2232 else
2233 sec = &dwarf2_per_objfile->info;
2234 dwarf2_read_section (objfile, sec);
2235 buffer_size = sec->size;
2236 buffer = sec->buffer;
9291a0cd
TT
2237 info_ptr = buffer + this_cu->offset;
2238 beg_of_comp_unit = info_ptr;
2239
2240 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2241 buffer, buffer_size,
2242 abfd);
2243
2244 /* Complete the cu_header. */
2245 cu.header.offset = beg_of_comp_unit - buffer;
2246 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2247
2248 this_cu->cu = &cu;
2249 cu.per_cu = this_cu;
2250
2251 dwarf2_read_abbrevs (abfd, &cu);
2252 make_cleanup (dwarf2_free_abbrev_table, &cu);
2253
2254 if (this_cu->from_debug_types)
2255 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2256 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2257 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2258 &has_children);
9291a0cd 2259
7b9f3c50
DE
2260 lh = NULL;
2261 slot = NULL;
2262 line_offset = 0;
9291a0cd
TT
2263 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2264 if (attr)
2265 {
7b9f3c50
DE
2266 struct quick_file_names find_entry;
2267
2268 line_offset = DW_UNSND (attr);
2269
2270 /* We may have already read in this line header (TU line header sharing).
2271 If we have we're done. */
2272 find_entry.offset = line_offset;
2273 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2274 &find_entry, INSERT);
2275 if (*slot != NULL)
2276 {
2277 do_cleanups (cleanups);
2278 this_cu->v.quick->file_names = *slot;
2279 return *slot;
2280 }
2281
9291a0cd
TT
2282 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2283 }
2284 if (lh == NULL)
2285 {
2286 do_cleanups (cleanups);
7b9f3c50
DE
2287 this_cu->v.quick->no_file_data = 1;
2288 return NULL;
9291a0cd
TT
2289 }
2290
7b9f3c50
DE
2291 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2292 qfn->offset = line_offset;
2293 gdb_assert (slot != NULL);
2294 *slot = qfn;
9291a0cd 2295
7b9f3c50 2296 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2297
7b9f3c50
DE
2298 qfn->num_file_names = lh->num_file_names;
2299 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2300 lh->num_file_names * sizeof (char *));
9291a0cd 2301 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2302 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2303 qfn->real_names = NULL;
9291a0cd 2304
7b9f3c50 2305 free_line_header (lh);
9291a0cd 2306 do_cleanups (cleanups);
7b9f3c50
DE
2307
2308 this_cu->v.quick->file_names = qfn;
2309 return qfn;
9291a0cd
TT
2310}
2311
2312/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2313 real path for a given file name from the line table. */
2fdf6df6 2314
9291a0cd 2315static const char *
7b9f3c50
DE
2316dw2_get_real_path (struct objfile *objfile,
2317 struct quick_file_names *qfn, int index)
9291a0cd 2318{
7b9f3c50
DE
2319 if (qfn->real_names == NULL)
2320 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2321 qfn->num_file_names, sizeof (char *));
9291a0cd 2322
7b9f3c50
DE
2323 if (qfn->real_names[index] == NULL)
2324 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2325
7b9f3c50 2326 return qfn->real_names[index];
9291a0cd
TT
2327}
2328
2329static struct symtab *
2330dw2_find_last_source_symtab (struct objfile *objfile)
2331{
2332 int index;
ae2de4f8 2333
9291a0cd
TT
2334 dw2_setup (objfile);
2335 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2336 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2337}
2338
7b9f3c50
DE
2339/* Traversal function for dw2_forget_cached_source_info. */
2340
2341static int
2342dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2343{
7b9f3c50 2344 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2345
7b9f3c50 2346 if (file_data->real_names)
9291a0cd 2347 {
7b9f3c50 2348 int i;
9291a0cd 2349
7b9f3c50 2350 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2351 {
7b9f3c50
DE
2352 xfree ((void*) file_data->real_names[i]);
2353 file_data->real_names[i] = NULL;
9291a0cd
TT
2354 }
2355 }
7b9f3c50
DE
2356
2357 return 1;
2358}
2359
2360static void
2361dw2_forget_cached_source_info (struct objfile *objfile)
2362{
2363 dw2_setup (objfile);
2364
2365 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2366 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2367}
2368
2369static int
2370dw2_lookup_symtab (struct objfile *objfile, const char *name,
2371 const char *full_path, const char *real_path,
2372 struct symtab **result)
2373{
2374 int i;
2375 int check_basename = lbasename (name) == name;
2376 struct dwarf2_per_cu_data *base_cu = NULL;
2377
2378 dw2_setup (objfile);
ae2de4f8 2379
1fd400ff
TT
2380 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2381 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2382 {
2383 int j;
e254ef6a 2384 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2385 struct quick_file_names *file_data;
9291a0cd 2386
e254ef6a 2387 if (per_cu->v.quick->symtab)
9291a0cd
TT
2388 continue;
2389
7b9f3c50
DE
2390 file_data = dw2_get_file_names (objfile, per_cu);
2391 if (file_data == NULL)
9291a0cd
TT
2392 continue;
2393
7b9f3c50 2394 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2395 {
7b9f3c50 2396 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2397
2398 if (FILENAME_CMP (name, this_name) == 0)
2399 {
e254ef6a 2400 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2401 return 1;
2402 }
2403
2404 if (check_basename && ! base_cu
2405 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2406 base_cu = per_cu;
9291a0cd
TT
2407
2408 if (full_path != NULL)
2409 {
7b9f3c50
DE
2410 const char *this_real_name = dw2_get_real_path (objfile,
2411 file_data, j);
9291a0cd 2412
7b9f3c50
DE
2413 if (this_real_name != NULL
2414 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2415 {
e254ef6a 2416 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2417 return 1;
2418 }
2419 }
2420
2421 if (real_path != NULL)
2422 {
7b9f3c50
DE
2423 const char *this_real_name = dw2_get_real_path (objfile,
2424 file_data, j);
9291a0cd 2425
7b9f3c50
DE
2426 if (this_real_name != NULL
2427 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2428 {
74dd2ca6
DE
2429 *result = dw2_instantiate_symtab (objfile, per_cu);
2430 return 1;
9291a0cd
TT
2431 }
2432 }
2433 }
2434 }
2435
2436 if (base_cu)
2437 {
2438 *result = dw2_instantiate_symtab (objfile, base_cu);
2439 return 1;
2440 }
2441
2442 return 0;
2443}
2444
2445static struct symtab *
2446dw2_lookup_symbol (struct objfile *objfile, int block_index,
2447 const char *name, domain_enum domain)
2448{
774b6a14 2449 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2450 instead. */
2451 return NULL;
2452}
2453
2454/* A helper function that expands all symtabs that hold an object
2455 named NAME. */
2fdf6df6 2456
9291a0cd
TT
2457static void
2458dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2459{
2460 dw2_setup (objfile);
2461
ae2de4f8 2462 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2463 if (dwarf2_per_objfile->index_table)
2464 {
2465 offset_type *vec;
2466
2467 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2468 name, &vec))
2469 {
2470 offset_type i, len = MAYBE_SWAP (*vec);
2471 for (i = 0; i < len; ++i)
2472 {
2473 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2474 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2475
e254ef6a 2476 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2477 }
2478 }
2479 }
2480}
2481
774b6a14
TT
2482static void
2483dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2484 enum block_enum block_kind, const char *name,
774b6a14 2485 domain_enum domain)
9291a0cd 2486{
774b6a14 2487 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2488}
2489
2490static void
2491dw2_print_stats (struct objfile *objfile)
2492{
2493 int i, count;
2494
2495 dw2_setup (objfile);
2496 count = 0;
1fd400ff
TT
2497 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2498 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2499 {
e254ef6a 2500 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2501
e254ef6a 2502 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2503 ++count;
2504 }
2505 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2506}
2507
2508static void
2509dw2_dump (struct objfile *objfile)
2510{
2511 /* Nothing worth printing. */
2512}
2513
2514static void
2515dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2516 struct section_offsets *delta)
2517{
2518 /* There's nothing to relocate here. */
2519}
2520
2521static void
2522dw2_expand_symtabs_for_function (struct objfile *objfile,
2523 const char *func_name)
2524{
2525 dw2_do_expand_symtabs_matching (objfile, func_name);
2526}
2527
2528static void
2529dw2_expand_all_symtabs (struct objfile *objfile)
2530{
2531 int i;
2532
2533 dw2_setup (objfile);
1fd400ff
TT
2534
2535 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2536 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2537 {
e254ef6a 2538 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2539
e254ef6a 2540 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2541 }
2542}
2543
2544static void
2545dw2_expand_symtabs_with_filename (struct objfile *objfile,
2546 const char *filename)
2547{
2548 int i;
2549
2550 dw2_setup (objfile);
d4637a04
DE
2551
2552 /* We don't need to consider type units here.
2553 This is only called for examining code, e.g. expand_line_sal.
2554 There can be an order of magnitude (or more) more type units
2555 than comp units, and we avoid them if we can. */
2556
2557 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2558 {
2559 int j;
e254ef6a 2560 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2561 struct quick_file_names *file_data;
9291a0cd 2562
e254ef6a 2563 if (per_cu->v.quick->symtab)
9291a0cd
TT
2564 continue;
2565
7b9f3c50
DE
2566 file_data = dw2_get_file_names (objfile, per_cu);
2567 if (file_data == NULL)
9291a0cd
TT
2568 continue;
2569
7b9f3c50 2570 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2571 {
7b9f3c50 2572 const char *this_name = file_data->file_names[j];
1ef75ecc 2573 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2574 {
e254ef6a 2575 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2576 break;
2577 }
2578 }
2579 }
2580}
2581
dd786858 2582static const char *
9291a0cd
TT
2583dw2_find_symbol_file (struct objfile *objfile, const char *name)
2584{
e254ef6a 2585 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2586 offset_type *vec;
7b9f3c50 2587 struct quick_file_names *file_data;
9291a0cd
TT
2588
2589 dw2_setup (objfile);
2590
ae2de4f8 2591 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2592 if (!dwarf2_per_objfile->index_table)
2593 return NULL;
2594
2595 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2596 name, &vec))
2597 return NULL;
2598
2599 /* Note that this just looks at the very first one named NAME -- but
2600 actually we are looking for a function. find_main_filename
2601 should be rewritten so that it doesn't require a custom hook. It
2602 could just use the ordinary symbol tables. */
2603 /* vec[0] is the length, which must always be >0. */
e254ef6a 2604 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2605
7b9f3c50
DE
2606 file_data = dw2_get_file_names (objfile, per_cu);
2607 if (file_data == NULL)
9291a0cd
TT
2608 return NULL;
2609
7b9f3c50 2610 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2611}
2612
2613static void
40658b94
PH
2614dw2_map_matching_symbols (const char * name, domain_enum namespace,
2615 struct objfile *objfile, int global,
2616 int (*callback) (struct block *,
2617 struct symbol *, void *),
2edb89d3
JK
2618 void *data, symbol_compare_ftype *match,
2619 symbol_compare_ftype *ordered_compare)
9291a0cd 2620{
40658b94 2621 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2622 current language is Ada for a non-Ada objfile using GNU index. As Ada
2623 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2624}
2625
2626static void
2627dw2_expand_symtabs_matching (struct objfile *objfile,
2628 int (*file_matcher) (const char *, void *),
2629 int (*name_matcher) (const char *, void *),
8903c50d 2630 enum search_domain kind,
9291a0cd
TT
2631 void *data)
2632{
2633 int i;
2634 offset_type iter;
4b5246aa 2635 struct mapped_index *index;
9291a0cd
TT
2636
2637 dw2_setup (objfile);
ae2de4f8
DE
2638
2639 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2640 if (!dwarf2_per_objfile->index_table)
2641 return;
4b5246aa 2642 index = dwarf2_per_objfile->index_table;
9291a0cd 2643
7b08b9eb
JK
2644 if (file_matcher != NULL)
2645 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2646 + dwarf2_per_objfile->n_type_comp_units); ++i)
2647 {
2648 int j;
2649 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2650 struct quick_file_names *file_data;
2651
2652 per_cu->v.quick->mark = 0;
2653 if (per_cu->v.quick->symtab)
2654 continue;
2655
2656 file_data = dw2_get_file_names (objfile, per_cu);
2657 if (file_data == NULL)
2658 continue;
2659
2660 for (j = 0; j < file_data->num_file_names; ++j)
2661 {
2662 if (file_matcher (file_data->file_names[j], data))
2663 {
2664 per_cu->v.quick->mark = 1;
2665 break;
2666 }
2667 }
2668 }
9291a0cd 2669
3876f04e 2670 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2671 {
2672 offset_type idx = 2 * iter;
2673 const char *name;
2674 offset_type *vec, vec_len, vec_idx;
2675
3876f04e 2676 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2677 continue;
2678
3876f04e 2679 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2680
2681 if (! (*name_matcher) (name, data))
2682 continue;
2683
2684 /* The name was matched, now expand corresponding CUs that were
2685 marked. */
4b5246aa 2686 vec = (offset_type *) (index->constant_pool
3876f04e 2687 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2688 vec_len = MAYBE_SWAP (vec[0]);
2689 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2690 {
e254ef6a 2691 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2692
e254ef6a 2693 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2694 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2695 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2696 }
2697 }
2698}
2699
2700static struct symtab *
2701dw2_find_pc_sect_symtab (struct objfile *objfile,
2702 struct minimal_symbol *msymbol,
2703 CORE_ADDR pc,
2704 struct obj_section *section,
2705 int warn_if_readin)
2706{
2707 struct dwarf2_per_cu_data *data;
2708
2709 dw2_setup (objfile);
2710
2711 if (!objfile->psymtabs_addrmap)
2712 return NULL;
2713
2714 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2715 if (!data)
2716 return NULL;
2717
2718 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2719 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2720 paddress (get_objfile_arch (objfile), pc));
2721
2722 return dw2_instantiate_symtab (objfile, data);
2723}
2724
9291a0cd 2725static void
44b13c5a 2726dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
9291a0cd
TT
2727 void *data)
2728{
2729 int i;
2730
2731 dw2_setup (objfile);
ae2de4f8 2732
1fd400ff
TT
2733 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2734 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2735 {
2736 int j;
e254ef6a 2737 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2738 struct quick_file_names *file_data;
9291a0cd 2739
e254ef6a 2740 if (per_cu->v.quick->symtab)
9291a0cd
TT
2741 continue;
2742
7b9f3c50
DE
2743 file_data = dw2_get_file_names (objfile, per_cu);
2744 if (file_data == NULL)
9291a0cd
TT
2745 continue;
2746
7b9f3c50 2747 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2748 {
7b9f3c50
DE
2749 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2750 j);
2751 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2752 }
2753 }
2754}
2755
2756static int
2757dw2_has_symbols (struct objfile *objfile)
2758{
2759 return 1;
2760}
2761
2762const struct quick_symbol_functions dwarf2_gdb_index_functions =
2763{
2764 dw2_has_symbols,
2765 dw2_find_last_source_symtab,
2766 dw2_forget_cached_source_info,
2767 dw2_lookup_symtab,
2768 dw2_lookup_symbol,
774b6a14 2769 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2770 dw2_print_stats,
2771 dw2_dump,
2772 dw2_relocate,
2773 dw2_expand_symtabs_for_function,
2774 dw2_expand_all_symtabs,
2775 dw2_expand_symtabs_with_filename,
2776 dw2_find_symbol_file,
40658b94 2777 dw2_map_matching_symbols,
9291a0cd
TT
2778 dw2_expand_symtabs_matching,
2779 dw2_find_pc_sect_symtab,
9291a0cd
TT
2780 dw2_map_symbol_filenames
2781};
2782
2783/* Initialize for reading DWARF for this objfile. Return 0 if this
2784 file will use psymtabs, or 1 if using the GNU index. */
2785
2786int
2787dwarf2_initialize_objfile (struct objfile *objfile)
2788{
2789 /* If we're about to read full symbols, don't bother with the
2790 indices. In this case we also don't care if some other debug
2791 format is making psymtabs, because they are all about to be
2792 expanded anyway. */
2793 if ((objfile->flags & OBJF_READNOW))
2794 {
2795 int i;
2796
2797 dwarf2_per_objfile->using_index = 1;
2798 create_all_comp_units (objfile);
1fd400ff 2799 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2800 dwarf2_per_objfile->quick_file_names_table =
2801 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2802
1fd400ff
TT
2803 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2804 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2805 {
e254ef6a 2806 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2807
e254ef6a
DE
2808 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2809 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2810 }
2811
2812 /* Return 1 so that gdb sees the "quick" functions. However,
2813 these functions will be no-ops because we will have expanded
2814 all symtabs. */
2815 return 1;
2816 }
2817
2818 if (dwarf2_read_index (objfile))
2819 return 1;
2820
9291a0cd
TT
2821 return 0;
2822}
2823
2824\f
2825
dce234bc
PP
2826/* Build a partial symbol table. */
2827
2828void
f29dff0a 2829dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2830{
f29dff0a 2831 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2832 {
2833 init_psymbol_list (objfile, 1024);
2834 }
2835
d146bf1e 2836 dwarf2_build_psymtabs_hard (objfile);
c906108c 2837}
c906108c 2838
45452591
DE
2839/* Return TRUE if OFFSET is within CU_HEADER. */
2840
2841static inline int
2842offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2843{
2844 unsigned int bottom = cu_header->offset;
2845 unsigned int top = (cu_header->offset
2846 + cu_header->length
2847 + cu_header->initial_length_size);
9a619af0 2848
45452591
DE
2849 return (offset >= bottom && offset < top);
2850}
2851
93311388
DE
2852/* Read in the comp unit header information from the debug_info at info_ptr.
2853 NOTE: This leaves members offset, first_die_offset to be filled in
2854 by the caller. */
107d2387 2855
fe1b8b76 2856static gdb_byte *
107d2387 2857read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2858 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2859{
2860 int signed_addr;
891d2f0b 2861 unsigned int bytes_read;
c764a876
DE
2862
2863 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2864 cu_header->initial_length_size = bytes_read;
2865 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2866 info_ptr += bytes_read;
107d2387
AC
2867 cu_header->version = read_2_bytes (abfd, info_ptr);
2868 info_ptr += 2;
613e1657 2869 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2870 &bytes_read);
613e1657 2871 info_ptr += bytes_read;
107d2387
AC
2872 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2873 info_ptr += 1;
2874 signed_addr = bfd_get_sign_extend_vma (abfd);
2875 if (signed_addr < 0)
8e65ff28 2876 internal_error (__FILE__, __LINE__,
e2e0b3e5 2877 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2878 cu_header->signed_addr_p = signed_addr;
c764a876 2879
107d2387
AC
2880 return info_ptr;
2881}
2882
fe1b8b76
JB
2883static gdb_byte *
2884partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2885 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2886 bfd *abfd)
2887{
fe1b8b76 2888 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2889
2890 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2891
2dc7f7b3 2892 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2893 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2894 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2895 bfd_get_filename (abfd));
72bf9492 2896
9e0ac564
TT
2897 if (header->abbrev_offset
2898 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2899 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2900 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2901 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2902 (long) header->abbrev_offset,
93311388 2903 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2904 bfd_get_filename (abfd));
2905
2906 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2907 > buffer + buffer_size)
8a3fe4f8
AC
2908 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2909 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2910 (long) header->length,
93311388 2911 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2912 bfd_get_filename (abfd));
2913
2914 return info_ptr;
2915}
2916
348e048f
DE
2917/* Read in the types comp unit header information from .debug_types entry at
2918 types_ptr. The result is a pointer to one past the end of the header. */
2919
2920static gdb_byte *
2921read_type_comp_unit_head (struct comp_unit_head *cu_header,
2922 ULONGEST *signature,
2923 gdb_byte *types_ptr, bfd *abfd)
2924{
348e048f
DE
2925 gdb_byte *initial_types_ptr = types_ptr;
2926
6e70227d 2927 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2928 &dwarf2_per_objfile->types);
348e048f
DE
2929 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2930
2931 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2932
2933 *signature = read_8_bytes (abfd, types_ptr);
2934 types_ptr += 8;
2935 types_ptr += cu_header->offset_size;
2936 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2937
2938 return types_ptr;
2939}
2940
aaa75496
JB
2941/* Allocate a new partial symtab for file named NAME and mark this new
2942 partial symtab as being an include of PST. */
2943
2944static void
2945dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2946 struct objfile *objfile)
2947{
2948 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2949
2950 subpst->section_offsets = pst->section_offsets;
2951 subpst->textlow = 0;
2952 subpst->texthigh = 0;
2953
2954 subpst->dependencies = (struct partial_symtab **)
2955 obstack_alloc (&objfile->objfile_obstack,
2956 sizeof (struct partial_symtab *));
2957 subpst->dependencies[0] = pst;
2958 subpst->number_of_dependencies = 1;
2959
2960 subpst->globals_offset = 0;
2961 subpst->n_global_syms = 0;
2962 subpst->statics_offset = 0;
2963 subpst->n_static_syms = 0;
2964 subpst->symtab = NULL;
2965 subpst->read_symtab = pst->read_symtab;
2966 subpst->readin = 0;
2967
2968 /* No private part is necessary for include psymtabs. This property
2969 can be used to differentiate between such include psymtabs and
10b3939b 2970 the regular ones. */
58a9656e 2971 subpst->read_symtab_private = NULL;
aaa75496
JB
2972}
2973
2974/* Read the Line Number Program data and extract the list of files
2975 included by the source file represented by PST. Build an include
d85a05f0 2976 partial symtab for each of these included files. */
aaa75496
JB
2977
2978static void
2979dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2980 struct die_info *die,
aaa75496
JB
2981 struct partial_symtab *pst)
2982{
2983 struct objfile *objfile = cu->objfile;
2984 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2985 struct line_header *lh = NULL;
2986 struct attribute *attr;
aaa75496 2987
d85a05f0
DJ
2988 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2989 if (attr)
2990 {
2991 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2992
d85a05f0
DJ
2993 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2994 }
aaa75496
JB
2995 if (lh == NULL)
2996 return; /* No linetable, so no includes. */
2997
c6da4cef
DE
2998 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2999 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
3000
3001 free_line_header (lh);
3002}
3003
348e048f
DE
3004static hashval_t
3005hash_type_signature (const void *item)
3006{
3007 const struct signatured_type *type_sig = item;
9a619af0 3008
348e048f
DE
3009 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3010 return type_sig->signature;
3011}
3012
3013static int
3014eq_type_signature (const void *item_lhs, const void *item_rhs)
3015{
3016 const struct signatured_type *lhs = item_lhs;
3017 const struct signatured_type *rhs = item_rhs;
9a619af0 3018
348e048f
DE
3019 return lhs->signature == rhs->signature;
3020}
3021
1fd400ff
TT
3022/* Allocate a hash table for signatured types. */
3023
3024static htab_t
673bfd45 3025allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3026{
3027 return htab_create_alloc_ex (41,
3028 hash_type_signature,
3029 eq_type_signature,
3030 NULL,
3031 &objfile->objfile_obstack,
3032 hashtab_obstack_allocate,
3033 dummy_obstack_deallocate);
3034}
3035
3036/* A helper function to add a signatured type CU to a list. */
3037
3038static int
3039add_signatured_type_cu_to_list (void **slot, void *datum)
3040{
3041 struct signatured_type *sigt = *slot;
3042 struct dwarf2_per_cu_data ***datap = datum;
3043
3044 **datap = &sigt->per_cu;
3045 ++*datap;
3046
3047 return 1;
3048}
3049
348e048f
DE
3050/* Create the hash table of all entries in the .debug_types section.
3051 The result is zero if there is an error (e.g. missing .debug_types section),
3052 otherwise non-zero. */
3053
3054static int
3055create_debug_types_hash_table (struct objfile *objfile)
3056{
be391dca 3057 gdb_byte *info_ptr;
348e048f 3058 htab_t types_htab;
1fd400ff 3059 struct dwarf2_per_cu_data **iter;
348e048f 3060
be391dca
TT
3061 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3062 info_ptr = dwarf2_per_objfile->types.buffer;
3063
348e048f
DE
3064 if (info_ptr == NULL)
3065 {
3066 dwarf2_per_objfile->signatured_types = NULL;
3067 return 0;
3068 }
3069
673bfd45 3070 types_htab = allocate_signatured_type_table (objfile);
348e048f
DE
3071
3072 if (dwarf2_die_debug)
3073 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3074
3e43a32a
MS
3075 while (info_ptr < dwarf2_per_objfile->types.buffer
3076 + dwarf2_per_objfile->types.size)
348e048f
DE
3077 {
3078 unsigned int offset;
3079 unsigned int offset_size;
3080 unsigned int type_offset;
3081 unsigned int length, initial_length_size;
3082 unsigned short version;
3083 ULONGEST signature;
3084 struct signatured_type *type_sig;
3085 void **slot;
3086 gdb_byte *ptr = info_ptr;
3087
3088 offset = ptr - dwarf2_per_objfile->types.buffer;
3089
3090 /* We need to read the type's signature in order to build the hash
3091 table, but we don't need to read anything else just yet. */
3092
3093 /* Sanity check to ensure entire cu is present. */
3094 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3095 if (ptr + length + initial_length_size
3096 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3097 {
3098 complaint (&symfile_complaints,
3e43a32a
MS
3099 _("debug type entry runs off end "
3100 "of `.debug_types' section, ignored"));
348e048f
DE
3101 break;
3102 }
3103
3104 offset_size = initial_length_size == 4 ? 4 : 8;
3105 ptr += initial_length_size;
3106 version = bfd_get_16 (objfile->obfd, ptr);
3107 ptr += 2;
3108 ptr += offset_size; /* abbrev offset */
3109 ptr += 1; /* address size */
3110 signature = bfd_get_64 (objfile->obfd, ptr);
3111 ptr += 8;
3112 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3113
3114 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3115 memset (type_sig, 0, sizeof (*type_sig));
3116 type_sig->signature = signature;
348e048f 3117 type_sig->type_offset = type_offset;
ca1f3406 3118 type_sig->per_cu.objfile = objfile;
1fd400ff 3119 type_sig->per_cu.from_debug_types = 1;
b3c8eb43 3120 type_sig->per_cu.offset = offset;
348e048f
DE
3121
3122 slot = htab_find_slot (types_htab, type_sig, INSERT);
3123 gdb_assert (slot != NULL);
b3c8eb43
JK
3124 if (*slot != NULL)
3125 {
3126 const struct signatured_type *dup_sig = *slot;
3127
3128 complaint (&symfile_complaints,
3129 _("debug type entry at offset 0x%x is duplicate to the "
3130 "entry at offset 0x%x, signature 0x%s"),
3131 offset, dup_sig->per_cu.offset,
3132 phex (signature, sizeof (signature)));
3133 gdb_assert (signature == dup_sig->signature);
3134 }
348e048f
DE
3135 *slot = type_sig;
3136
3137 if (dwarf2_die_debug)
3138 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3139 offset, phex (signature, sizeof (signature)));
3140
3141 info_ptr = info_ptr + initial_length_size + length;
3142 }
3143
3144 dwarf2_per_objfile->signatured_types = types_htab;
3145
1fd400ff
TT
3146 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3147 dwarf2_per_objfile->type_comp_units
3148 = obstack_alloc (&objfile->objfile_obstack,
3149 dwarf2_per_objfile->n_type_comp_units
3150 * sizeof (struct dwarf2_per_cu_data *));
3151 iter = &dwarf2_per_objfile->type_comp_units[0];
3152 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3153 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3154 == dwarf2_per_objfile->n_type_comp_units);
3155
348e048f
DE
3156 return 1;
3157}
3158
3159/* Lookup a signature based type.
3160 Returns NULL if SIG is not present in the table. */
3161
3162static struct signatured_type *
3163lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3164{
3165 struct signatured_type find_entry, *entry;
3166
3167 if (dwarf2_per_objfile->signatured_types == NULL)
3168 {
3169 complaint (&symfile_complaints,
55f1336d 3170 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3171 return 0;
3172 }
3173
3174 find_entry.signature = sig;
3175 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3176 return entry;
3177}
3178
d85a05f0
DJ
3179/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3180
3181static void
3182init_cu_die_reader (struct die_reader_specs *reader,
3183 struct dwarf2_cu *cu)
3184{
3185 reader->abfd = cu->objfile->obfd;
3186 reader->cu = cu;
3187 if (cu->per_cu->from_debug_types)
be391dca
TT
3188 {
3189 gdb_assert (dwarf2_per_objfile->types.readin);
3190 reader->buffer = dwarf2_per_objfile->types.buffer;
3191 }
d85a05f0 3192 else
be391dca
TT
3193 {
3194 gdb_assert (dwarf2_per_objfile->info.readin);
3195 reader->buffer = dwarf2_per_objfile->info.buffer;
3196 }
d85a05f0
DJ
3197}
3198
3199/* Find the base address of the compilation unit for range lists and
3200 location lists. It will normally be specified by DW_AT_low_pc.
3201 In DWARF-3 draft 4, the base address could be overridden by
3202 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3203 compilation units with discontinuous ranges. */
3204
3205static void
3206dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3207{
3208 struct attribute *attr;
3209
3210 cu->base_known = 0;
3211 cu->base_address = 0;
3212
3213 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3214 if (attr)
3215 {
3216 cu->base_address = DW_ADDR (attr);
3217 cu->base_known = 1;
3218 }
3219 else
3220 {
3221 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3222 if (attr)
3223 {
3224 cu->base_address = DW_ADDR (attr);
3225 cu->base_known = 1;
3226 }
3227 }
3228}
3229
348e048f
DE
3230/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3231 to combine the common parts.
93311388 3232 Process a compilation unit for a psymtab.
348e048f
DE
3233 BUFFER is a pointer to the beginning of the dwarf section buffer,
3234 either .debug_info or debug_types.
93311388
DE
3235 INFO_PTR is a pointer to the start of the CU.
3236 Returns a pointer to the next CU. */
aaa75496 3237
93311388
DE
3238static gdb_byte *
3239process_psymtab_comp_unit (struct objfile *objfile,
3240 struct dwarf2_per_cu_data *this_cu,
3241 gdb_byte *buffer, gdb_byte *info_ptr,
3242 unsigned int buffer_size)
c906108c 3243{
c906108c 3244 bfd *abfd = objfile->obfd;
93311388 3245 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3246 struct die_info *comp_unit_die;
c906108c 3247 struct partial_symtab *pst;
5734ee8b 3248 CORE_ADDR baseaddr;
93311388
DE
3249 struct cleanup *back_to_inner;
3250 struct dwarf2_cu cu;
d85a05f0
DJ
3251 int has_children, has_pc_info;
3252 struct attribute *attr;
d85a05f0
DJ
3253 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3254 struct die_reader_specs reader_specs;
3e2a0cee 3255 const char *filename;
c906108c 3256
9816fde3 3257 init_one_comp_unit (&cu, objfile);
93311388 3258 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3259
93311388
DE
3260 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3261 buffer, buffer_size,
3262 abfd);
10b3939b 3263
93311388
DE
3264 /* Complete the cu_header. */
3265 cu.header.offset = beg_of_comp_unit - buffer;
3266 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 3267
93311388 3268 cu.list_in_scope = &file_symbols;
af703f96 3269
328c9494
DJ
3270 /* If this compilation unit was already read in, free the
3271 cached copy in order to read it in again. This is
3272 necessary because we skipped some symbols when we first
3273 read in the compilation unit (see load_partial_dies).
3274 This problem could be avoided, but the benefit is
3275 unclear. */
3276 if (this_cu->cu != NULL)
3277 free_one_cached_comp_unit (this_cu->cu);
3278
3279 /* Note that this is a pointer to our stack frame, being
3280 added to a global data structure. It will be cleaned up
3281 in free_stack_comp_unit when we finish with this
3282 compilation unit. */
3283 this_cu->cu = &cu;
d85a05f0
DJ
3284 cu.per_cu = this_cu;
3285
93311388
DE
3286 /* Read the abbrevs for this compilation unit into a table. */
3287 dwarf2_read_abbrevs (abfd, &cu);
3288 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3289
93311388 3290 /* Read the compilation unit die. */
348e048f
DE
3291 if (this_cu->from_debug_types)
3292 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3293 init_cu_die_reader (&reader_specs, &cu);
3294 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3295 &has_children);
93311388 3296
348e048f
DE
3297 if (this_cu->from_debug_types)
3298 {
b3c8eb43
JK
3299 /* LENGTH has not been set yet for type units. */
3300 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3301 this_cu->length = cu.header.length + cu.header.initial_length_size;
3302 }
d85a05f0 3303 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3304 {
93311388
DE
3305 info_ptr = (beg_of_comp_unit + cu.header.length
3306 + cu.header.initial_length_size);
3307 do_cleanups (back_to_inner);
3308 return info_ptr;
3309 }
72bf9492 3310
9816fde3 3311 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3312
93311388 3313 /* Allocate a new partial symbol table structure. */
d85a05f0 3314 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3315 if (attr == NULL || !DW_STRING (attr))
3316 filename = "";
3317 else
3318 filename = DW_STRING (attr);
93311388 3319 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3320 filename,
93311388
DE
3321 /* TEXTLOW and TEXTHIGH are set below. */
3322 0,
3323 objfile->global_psymbols.next,
3324 objfile->static_psymbols.next);
72bf9492 3325
d85a05f0
DJ
3326 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3327 if (attr != NULL)
3328 pst->dirname = DW_STRING (attr);
72bf9492 3329
e38df1d0 3330 pst->read_symtab_private = this_cu;
72bf9492 3331
93311388 3332 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3333
0963b4bd 3334 /* Store the function that reads in the rest of the symbol table. */
93311388 3335 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3336
9291a0cd 3337 this_cu->v.psymtab = pst;
c906108c 3338
d85a05f0
DJ
3339 dwarf2_find_base_address (comp_unit_die, &cu);
3340
93311388
DE
3341 /* Possibly set the default values of LOWPC and HIGHPC from
3342 `DW_AT_ranges'. */
d85a05f0
DJ
3343 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3344 &best_highpc, &cu, pst);
3345 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3346 /* Store the contiguous range if it is not empty; it can be empty for
3347 CUs with no code. */
3348 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3349 best_lowpc + baseaddr,
3350 best_highpc + baseaddr - 1, pst);
93311388
DE
3351
3352 /* Check if comp unit has_children.
3353 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3354 If not, there's no more debug_info for this comp unit. */
d85a05f0 3355 if (has_children)
93311388
DE
3356 {
3357 struct partial_die_info *first_die;
3358 CORE_ADDR lowpc, highpc;
31ffec48 3359
93311388
DE
3360 lowpc = ((CORE_ADDR) -1);
3361 highpc = ((CORE_ADDR) 0);
c906108c 3362
93311388 3363 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3364
93311388 3365 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3366 ! has_pc_info, &cu);
57c22c6c 3367
93311388
DE
3368 /* If we didn't find a lowpc, set it to highpc to avoid
3369 complaints from `maint check'. */
3370 if (lowpc == ((CORE_ADDR) -1))
3371 lowpc = highpc;
10b3939b 3372
93311388
DE
3373 /* If the compilation unit didn't have an explicit address range,
3374 then use the information extracted from its child dies. */
d85a05f0 3375 if (! has_pc_info)
93311388 3376 {
d85a05f0
DJ
3377 best_lowpc = lowpc;
3378 best_highpc = highpc;
93311388
DE
3379 }
3380 }
d85a05f0
DJ
3381 pst->textlow = best_lowpc + baseaddr;
3382 pst->texthigh = best_highpc + baseaddr;
c906108c 3383
93311388
DE
3384 pst->n_global_syms = objfile->global_psymbols.next -
3385 (objfile->global_psymbols.list + pst->globals_offset);
3386 pst->n_static_syms = objfile->static_psymbols.next -
3387 (objfile->static_psymbols.list + pst->statics_offset);
3388 sort_pst_symbols (pst);
c906108c 3389
93311388
DE
3390 info_ptr = (beg_of_comp_unit + cu.header.length
3391 + cu.header.initial_length_size);
ae038cb0 3392
348e048f
DE
3393 if (this_cu->from_debug_types)
3394 {
3395 /* It's not clear we want to do anything with stmt lists here.
3396 Waiting to see what gcc ultimately does. */
3397 }
d85a05f0 3398 else
93311388
DE
3399 {
3400 /* Get the list of files included in the current compilation unit,
3401 and build a psymtab for each of them. */
d85a05f0 3402 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3403 }
ae038cb0 3404
93311388 3405 do_cleanups (back_to_inner);
ae038cb0 3406
93311388
DE
3407 return info_ptr;
3408}
ff013f42 3409
348e048f
DE
3410/* Traversal function for htab_traverse_noresize.
3411 Process one .debug_types comp-unit. */
3412
3413static int
3414process_type_comp_unit (void **slot, void *info)
3415{
3416 struct signatured_type *entry = (struct signatured_type *) *slot;
3417 struct objfile *objfile = (struct objfile *) info;
3418 struct dwarf2_per_cu_data *this_cu;
3419
3420 this_cu = &entry->per_cu;
348e048f 3421
be391dca 3422 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3423 process_psymtab_comp_unit (objfile, this_cu,
3424 dwarf2_per_objfile->types.buffer,
b3c8eb43 3425 dwarf2_per_objfile->types.buffer + this_cu->offset,
348e048f
DE
3426 dwarf2_per_objfile->types.size);
3427
3428 return 1;
3429}
3430
3431/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3432 Build partial symbol tables for the .debug_types comp-units. */
3433
3434static void
3435build_type_psymtabs (struct objfile *objfile)
3436{
3437 if (! create_debug_types_hash_table (objfile))
3438 return;
3439
3440 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3441 process_type_comp_unit, objfile);
3442}
3443
60606b2c
TT
3444/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3445
3446static void
3447psymtabs_addrmap_cleanup (void *o)
3448{
3449 struct objfile *objfile = o;
ec61707d 3450
60606b2c
TT
3451 objfile->psymtabs_addrmap = NULL;
3452}
3453
93311388
DE
3454/* Build the partial symbol table by doing a quick pass through the
3455 .debug_info and .debug_abbrev sections. */
72bf9492 3456
93311388 3457static void
c67a9c90 3458dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3459{
93311388 3460 gdb_byte *info_ptr;
60606b2c
TT
3461 struct cleanup *back_to, *addrmap_cleanup;
3462 struct obstack temp_obstack;
93311388 3463
98bfdba5
PA
3464 dwarf2_per_objfile->reading_partial_symbols = 1;
3465
be391dca 3466 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3467 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3468
93311388
DE
3469 /* Any cached compilation units will be linked by the per-objfile
3470 read_in_chain. Make sure to free them when we're done. */
3471 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3472
348e048f
DE
3473 build_type_psymtabs (objfile);
3474
93311388 3475 create_all_comp_units (objfile);
c906108c 3476
60606b2c
TT
3477 /* Create a temporary address map on a temporary obstack. We later
3478 copy this to the final obstack. */
3479 obstack_init (&temp_obstack);
3480 make_cleanup_obstack_free (&temp_obstack);
3481 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3482 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3483
93311388
DE
3484 /* Since the objects we're extracting from .debug_info vary in
3485 length, only the individual functions to extract them (like
3486 read_comp_unit_head and load_partial_die) can really know whether
3487 the buffer is large enough to hold another complete object.
c906108c 3488
93311388
DE
3489 At the moment, they don't actually check that. If .debug_info
3490 holds just one extra byte after the last compilation unit's dies,
3491 then read_comp_unit_head will happily read off the end of the
3492 buffer. read_partial_die is similarly casual. Those functions
3493 should be fixed.
c906108c 3494
93311388
DE
3495 For this loop condition, simply checking whether there's any data
3496 left at all should be sufficient. */
c906108c 3497
93311388
DE
3498 while (info_ptr < (dwarf2_per_objfile->info.buffer
3499 + dwarf2_per_objfile->info.size))
3500 {
3501 struct dwarf2_per_cu_data *this_cu;
dd373385 3502
3e43a32a
MS
3503 this_cu = dwarf2_find_comp_unit (info_ptr
3504 - dwarf2_per_objfile->info.buffer,
93311388 3505 objfile);
aaa75496 3506
93311388
DE
3507 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3508 dwarf2_per_objfile->info.buffer,
3509 info_ptr,
3510 dwarf2_per_objfile->info.size);
c906108c 3511 }
ff013f42
JK
3512
3513 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3514 &objfile->objfile_obstack);
60606b2c 3515 discard_cleanups (addrmap_cleanup);
ff013f42 3516
ae038cb0
DJ
3517 do_cleanups (back_to);
3518}
3519
93311388 3520/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3521
3522static void
93311388
DE
3523load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3524 struct objfile *objfile)
ae038cb0
DJ
3525{
3526 bfd *abfd = objfile->obfd;
fe1b8b76 3527 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3528 struct die_info *comp_unit_die;
ae038cb0 3529 struct dwarf2_cu *cu;
1d9ec526 3530 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3531 int has_children;
3532 struct die_reader_specs reader_specs;
98bfdba5 3533 int read_cu = 0;
ae038cb0 3534
348e048f
DE
3535 gdb_assert (! this_cu->from_debug_types);
3536
be391dca 3537 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3538 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3539 beg_of_comp_unit = info_ptr;
3540
98bfdba5
PA
3541 if (this_cu->cu == NULL)
3542 {
9816fde3
JK
3543 cu = xmalloc (sizeof (*cu));
3544 init_one_comp_unit (cu, objfile);
ae038cb0 3545
98bfdba5 3546 read_cu = 1;
ae038cb0 3547
98bfdba5
PA
3548 /* If an error occurs while loading, release our storage. */
3549 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3550
98bfdba5
PA
3551 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3552 dwarf2_per_objfile->info.buffer,
3553 dwarf2_per_objfile->info.size,
3554 abfd);
ae038cb0 3555
98bfdba5
PA
3556 /* Complete the cu_header. */
3557 cu->header.offset = this_cu->offset;
3558 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3559
3560 /* Link this compilation unit into the compilation unit tree. */
3561 this_cu->cu = cu;
3562 cu->per_cu = this_cu;
98bfdba5
PA
3563
3564 /* Link this CU into read_in_chain. */
3565 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3566 dwarf2_per_objfile->read_in_chain = this_cu;
3567 }
3568 else
3569 {
3570 cu = this_cu->cu;
3571 info_ptr += cu->header.first_die_offset;
3572 }
ae038cb0
DJ
3573
3574 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3575 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3576 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3577 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3578
3579 /* Read the compilation unit die. */
d85a05f0
DJ
3580 init_cu_die_reader (&reader_specs, cu);
3581 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3582 &has_children);
ae038cb0 3583
9816fde3 3584 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3585
ae038cb0
DJ
3586 /* Check if comp unit has_children.
3587 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3588 If not, there's no more debug_info for this comp unit. */
d85a05f0 3589 if (has_children)
93311388 3590 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3591
98bfdba5
PA
3592 do_cleanups (free_abbrevs_cleanup);
3593
3594 if (read_cu)
3595 {
3596 /* We've successfully allocated this compilation unit. Let our
3597 caller clean it up when finished with it. */
3598 discard_cleanups (free_cu_cleanup);
3599 }
ae038cb0
DJ
3600}
3601
3602/* Create a list of all compilation units in OBJFILE. We do this only
3603 if an inter-comp-unit reference is found; presumably if there is one,
3604 there will be many, and one will occur early in the .debug_info section.
3605 So there's no point in building this list incrementally. */
3606
3607static void
3608create_all_comp_units (struct objfile *objfile)
3609{
3610 int n_allocated;
3611 int n_comp_units;
3612 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3613 gdb_byte *info_ptr;
3614
3615 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3616 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3617
3618 n_comp_units = 0;
3619 n_allocated = 10;
3620 all_comp_units = xmalloc (n_allocated
3621 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3622
3e43a32a
MS
3623 while (info_ptr < dwarf2_per_objfile->info.buffer
3624 + dwarf2_per_objfile->info.size)
ae038cb0 3625 {
c764a876 3626 unsigned int length, initial_length_size;
ae038cb0 3627 struct dwarf2_per_cu_data *this_cu;
c764a876 3628 unsigned int offset;
ae038cb0 3629
dce234bc 3630 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3631
3632 /* Read just enough information to find out where the next
3633 compilation unit is. */
c764a876
DE
3634 length = read_initial_length (objfile->obfd, info_ptr,
3635 &initial_length_size);
ae038cb0
DJ
3636
3637 /* Save the compilation unit for later lookup. */
3638 this_cu = obstack_alloc (&objfile->objfile_obstack,
3639 sizeof (struct dwarf2_per_cu_data));
3640 memset (this_cu, 0, sizeof (*this_cu));
3641 this_cu->offset = offset;
c764a876 3642 this_cu->length = length + initial_length_size;
9291a0cd 3643 this_cu->objfile = objfile;
ae038cb0
DJ
3644
3645 if (n_comp_units == n_allocated)
3646 {
3647 n_allocated *= 2;
3648 all_comp_units = xrealloc (all_comp_units,
3649 n_allocated
3650 * sizeof (struct dwarf2_per_cu_data *));
3651 }
3652 all_comp_units[n_comp_units++] = this_cu;
3653
3654 info_ptr = info_ptr + this_cu->length;
3655 }
3656
3657 dwarf2_per_objfile->all_comp_units
3658 = obstack_alloc (&objfile->objfile_obstack,
3659 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3660 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3661 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3662 xfree (all_comp_units);
3663 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3664}
3665
5734ee8b
DJ
3666/* Process all loaded DIEs for compilation unit CU, starting at
3667 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3668 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3669 DW_AT_ranges). If NEED_PC is set, then this function will set
3670 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3671 and record the covered ranges in the addrmap. */
c906108c 3672
72bf9492
DJ
3673static void
3674scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3675 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3676{
72bf9492 3677 struct partial_die_info *pdi;
c906108c 3678
91c24f0a
DC
3679 /* Now, march along the PDI's, descending into ones which have
3680 interesting children but skipping the children of the other ones,
3681 until we reach the end of the compilation unit. */
c906108c 3682
72bf9492 3683 pdi = first_die;
91c24f0a 3684
72bf9492
DJ
3685 while (pdi != NULL)
3686 {
3687 fixup_partial_die (pdi, cu);
c906108c 3688
f55ee35c 3689 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3690 children, so we need to look at them. Ditto for anonymous
3691 enums. */
933c6fe4 3692
72bf9492 3693 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3694 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3695 {
72bf9492 3696 switch (pdi->tag)
c906108c
SS
3697 {
3698 case DW_TAG_subprogram:
5734ee8b 3699 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3700 break;
72929c62 3701 case DW_TAG_constant:
c906108c
SS
3702 case DW_TAG_variable:
3703 case DW_TAG_typedef:
91c24f0a 3704 case DW_TAG_union_type:
72bf9492 3705 if (!pdi->is_declaration)
63d06c5c 3706 {
72bf9492 3707 add_partial_symbol (pdi, cu);
63d06c5c
DC
3708 }
3709 break;
c906108c 3710 case DW_TAG_class_type:
680b30c7 3711 case DW_TAG_interface_type:
c906108c 3712 case DW_TAG_structure_type:
72bf9492 3713 if (!pdi->is_declaration)
c906108c 3714 {
72bf9492 3715 add_partial_symbol (pdi, cu);
c906108c
SS
3716 }
3717 break;
91c24f0a 3718 case DW_TAG_enumeration_type:
72bf9492
DJ
3719 if (!pdi->is_declaration)
3720 add_partial_enumeration (pdi, cu);
c906108c
SS
3721 break;
3722 case DW_TAG_base_type:
a02abb62 3723 case DW_TAG_subrange_type:
c906108c 3724 /* File scope base type definitions are added to the partial
c5aa993b 3725 symbol table. */
72bf9492 3726 add_partial_symbol (pdi, cu);
c906108c 3727 break;
d9fa45fe 3728 case DW_TAG_namespace:
5734ee8b 3729 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3730 break;
5d7cb8df
JK
3731 case DW_TAG_module:
3732 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3733 break;
c906108c
SS
3734 default:
3735 break;
3736 }
3737 }
3738
72bf9492
DJ
3739 /* If the die has a sibling, skip to the sibling. */
3740
3741 pdi = pdi->die_sibling;
3742 }
3743}
3744
3745/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3746
72bf9492 3747 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3748 name is concatenated with "::" and the partial DIE's name. For
3749 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3750 Enumerators are an exception; they use the scope of their parent
3751 enumeration type, i.e. the name of the enumeration type is not
3752 prepended to the enumerator.
91c24f0a 3753
72bf9492
DJ
3754 There are two complexities. One is DW_AT_specification; in this
3755 case "parent" means the parent of the target of the specification,
3756 instead of the direct parent of the DIE. The other is compilers
3757 which do not emit DW_TAG_namespace; in this case we try to guess
3758 the fully qualified name of structure types from their members'
3759 linkage names. This must be done using the DIE's children rather
3760 than the children of any DW_AT_specification target. We only need
3761 to do this for structures at the top level, i.e. if the target of
3762 any DW_AT_specification (if any; otherwise the DIE itself) does not
3763 have a parent. */
3764
3765/* Compute the scope prefix associated with PDI's parent, in
3766 compilation unit CU. The result will be allocated on CU's
3767 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3768 field. NULL is returned if no prefix is necessary. */
3769static char *
3770partial_die_parent_scope (struct partial_die_info *pdi,
3771 struct dwarf2_cu *cu)
3772{
3773 char *grandparent_scope;
3774 struct partial_die_info *parent, *real_pdi;
91c24f0a 3775
72bf9492
DJ
3776 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3777 then this means the parent of the specification DIE. */
3778
3779 real_pdi = pdi;
72bf9492 3780 while (real_pdi->has_specification)
10b3939b 3781 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3782
3783 parent = real_pdi->die_parent;
3784 if (parent == NULL)
3785 return NULL;
3786
3787 if (parent->scope_set)
3788 return parent->scope;
3789
3790 fixup_partial_die (parent, cu);
3791
10b3939b 3792 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3793
acebe513
UW
3794 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3795 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3796 Work around this problem here. */
3797 if (cu->language == language_cplus
6e70227d 3798 && parent->tag == DW_TAG_namespace
acebe513
UW
3799 && strcmp (parent->name, "::") == 0
3800 && grandparent_scope == NULL)
3801 {
3802 parent->scope = NULL;
3803 parent->scope_set = 1;
3804 return NULL;
3805 }
3806
72bf9492 3807 if (parent->tag == DW_TAG_namespace
f55ee35c 3808 || parent->tag == DW_TAG_module
72bf9492
DJ
3809 || parent->tag == DW_TAG_structure_type
3810 || parent->tag == DW_TAG_class_type
680b30c7 3811 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3812 || parent->tag == DW_TAG_union_type
3813 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3814 {
3815 if (grandparent_scope == NULL)
3816 parent->scope = parent->name;
3817 else
3e43a32a
MS
3818 parent->scope = typename_concat (&cu->comp_unit_obstack,
3819 grandparent_scope,
f55ee35c 3820 parent->name, 0, cu);
72bf9492 3821 }
ceeb3d5a 3822 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3823 /* Enumerators should not get the name of the enumeration as a prefix. */
3824 parent->scope = grandparent_scope;
3825 else
3826 {
3827 /* FIXME drow/2004-04-01: What should we be doing with
3828 function-local names? For partial symbols, we should probably be
3829 ignoring them. */
3830 complaint (&symfile_complaints,
e2e0b3e5 3831 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3832 parent->tag, pdi->offset);
3833 parent->scope = grandparent_scope;
c906108c
SS
3834 }
3835
72bf9492
DJ
3836 parent->scope_set = 1;
3837 return parent->scope;
3838}
3839
3840/* Return the fully scoped name associated with PDI, from compilation unit
3841 CU. The result will be allocated with malloc. */
3842static char *
3843partial_die_full_name (struct partial_die_info *pdi,
3844 struct dwarf2_cu *cu)
3845{
3846 char *parent_scope;
3847
98bfdba5
PA
3848 /* If this is a template instantiation, we can not work out the
3849 template arguments from partial DIEs. So, unfortunately, we have
3850 to go through the full DIEs. At least any work we do building
3851 types here will be reused if full symbols are loaded later. */
3852 if (pdi->has_template_arguments)
3853 {
3854 fixup_partial_die (pdi, cu);
3855
3856 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3857 {
3858 struct die_info *die;
3859 struct attribute attr;
3860 struct dwarf2_cu *ref_cu = cu;
3861
3862 attr.name = 0;
3863 attr.form = DW_FORM_ref_addr;
3864 attr.u.addr = pdi->offset;
3865 die = follow_die_ref (NULL, &attr, &ref_cu);
3866
3867 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3868 }
3869 }
3870
72bf9492
DJ
3871 parent_scope = partial_die_parent_scope (pdi, cu);
3872 if (parent_scope == NULL)
3873 return NULL;
3874 else
f55ee35c 3875 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3876}
3877
3878static void
72bf9492 3879add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3880{
e7c27a73 3881 struct objfile *objfile = cu->objfile;
c906108c 3882 CORE_ADDR addr = 0;
decbce07 3883 char *actual_name = NULL;
5c4e30ca 3884 const struct partial_symbol *psym = NULL;
e142c38c 3885 CORE_ADDR baseaddr;
72bf9492 3886 int built_actual_name = 0;
e142c38c
DJ
3887
3888 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3889
94af9270
KS
3890 actual_name = partial_die_full_name (pdi, cu);
3891 if (actual_name)
3892 built_actual_name = 1;
63d06c5c 3893
72bf9492
DJ
3894 if (actual_name == NULL)
3895 actual_name = pdi->name;
3896
c906108c
SS
3897 switch (pdi->tag)
3898 {
3899 case DW_TAG_subprogram:
2cfa0c8d 3900 if (pdi->is_external || cu->language == language_ada)
c906108c 3901 {
2cfa0c8d
JB
3902 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3903 of the global scope. But in Ada, we want to be able to access
3904 nested procedures globally. So all Ada subprograms are stored
3905 in the global scope. */
f47fb265 3906 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3907 mst_text, objfile); */
f47fb265
MS
3908 add_psymbol_to_list (actual_name, strlen (actual_name),
3909 built_actual_name,
3910 VAR_DOMAIN, LOC_BLOCK,
3911 &objfile->global_psymbols,
3912 0, pdi->lowpc + baseaddr,
3913 cu->language, objfile);
c906108c
SS
3914 }
3915 else
3916 {
f47fb265 3917 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3918 mst_file_text, objfile); */
f47fb265
MS
3919 add_psymbol_to_list (actual_name, strlen (actual_name),
3920 built_actual_name,
3921 VAR_DOMAIN, LOC_BLOCK,
3922 &objfile->static_psymbols,
3923 0, pdi->lowpc + baseaddr,
3924 cu->language, objfile);
c906108c
SS
3925 }
3926 break;
72929c62
JB
3927 case DW_TAG_constant:
3928 {
3929 struct psymbol_allocation_list *list;
3930
3931 if (pdi->is_external)
3932 list = &objfile->global_psymbols;
3933 else
3934 list = &objfile->static_psymbols;
f47fb265
MS
3935 add_psymbol_to_list (actual_name, strlen (actual_name),
3936 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3937 list, 0, 0, cu->language, objfile);
72929c62
JB
3938 }
3939 break;
c906108c 3940 case DW_TAG_variable:
caac4577
JG
3941 if (pdi->locdesc)
3942 addr = decode_locdesc (pdi->locdesc, cu);
3943
3944 if (pdi->locdesc
3945 && addr == 0
3946 && !dwarf2_per_objfile->has_section_at_zero)
3947 {
3948 /* A global or static variable may also have been stripped
3949 out by the linker if unused, in which case its address
3950 will be nullified; do not add such variables into partial
3951 symbol table then. */
3952 }
3953 else if (pdi->is_external)
c906108c
SS
3954 {
3955 /* Global Variable.
3956 Don't enter into the minimal symbol tables as there is
3957 a minimal symbol table entry from the ELF symbols already.
3958 Enter into partial symbol table if it has a location
3959 descriptor or a type.
3960 If the location descriptor is missing, new_symbol will create
3961 a LOC_UNRESOLVED symbol, the address of the variable will then
3962 be determined from the minimal symbol table whenever the variable
3963 is referenced.
3964 The address for the partial symbol table entry is not
3965 used by GDB, but it comes in handy for debugging partial symbol
3966 table building. */
3967
c906108c 3968 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
3969 add_psymbol_to_list (actual_name, strlen (actual_name),
3970 built_actual_name,
3971 VAR_DOMAIN, LOC_STATIC,
3972 &objfile->global_psymbols,
3973 0, addr + baseaddr,
3974 cu->language, objfile);
c906108c
SS
3975 }
3976 else
3977 {
0963b4bd 3978 /* Static Variable. Skip symbols without location descriptors. */
c906108c 3979 if (pdi->locdesc == NULL)
decbce07
MS
3980 {
3981 if (built_actual_name)
3982 xfree (actual_name);
3983 return;
3984 }
f47fb265 3985 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3986 mst_file_data, objfile); */
f47fb265
MS
3987 add_psymbol_to_list (actual_name, strlen (actual_name),
3988 built_actual_name,
3989 VAR_DOMAIN, LOC_STATIC,
3990 &objfile->static_psymbols,
3991 0, addr + baseaddr,
3992 cu->language, objfile);
c906108c
SS
3993 }
3994 break;
3995 case DW_TAG_typedef:
3996 case DW_TAG_base_type:
a02abb62 3997 case DW_TAG_subrange_type:
38d518c9 3998 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3999 built_actual_name,
176620f1 4000 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4001 &objfile->static_psymbols,
e142c38c 4002 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4003 break;
72bf9492
DJ
4004 case DW_TAG_namespace:
4005 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4006 built_actual_name,
72bf9492
DJ
4007 VAR_DOMAIN, LOC_TYPEDEF,
4008 &objfile->global_psymbols,
4009 0, (CORE_ADDR) 0, cu->language, objfile);
4010 break;
c906108c 4011 case DW_TAG_class_type:
680b30c7 4012 case DW_TAG_interface_type:
c906108c
SS
4013 case DW_TAG_structure_type:
4014 case DW_TAG_union_type:
4015 case DW_TAG_enumeration_type:
fa4028e9
JB
4016 /* Skip external references. The DWARF standard says in the section
4017 about "Structure, Union, and Class Type Entries": "An incomplete
4018 structure, union or class type is represented by a structure,
4019 union or class entry that does not have a byte size attribute
4020 and that has a DW_AT_declaration attribute." */
4021 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4022 {
4023 if (built_actual_name)
4024 xfree (actual_name);
4025 return;
4026 }
fa4028e9 4027
63d06c5c
DC
4028 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4029 static vs. global. */
38d518c9 4030 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4031 built_actual_name,
176620f1 4032 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4033 (cu->language == language_cplus
4034 || cu->language == language_java)
63d06c5c
DC
4035 ? &objfile->global_psymbols
4036 : &objfile->static_psymbols,
e142c38c 4037 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4038
c906108c
SS
4039 break;
4040 case DW_TAG_enumerator:
38d518c9 4041 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4042 built_actual_name,
176620f1 4043 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4044 (cu->language == language_cplus
4045 || cu->language == language_java)
f6fe98ef
DJ
4046 ? &objfile->global_psymbols
4047 : &objfile->static_psymbols,
e142c38c 4048 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4049 break;
4050 default:
4051 break;
4052 }
5c4e30ca 4053
72bf9492
DJ
4054 if (built_actual_name)
4055 xfree (actual_name);
c906108c
SS
4056}
4057
5c4e30ca
DC
4058/* Read a partial die corresponding to a namespace; also, add a symbol
4059 corresponding to that namespace to the symbol table. NAMESPACE is
4060 the name of the enclosing namespace. */
91c24f0a 4061
72bf9492
DJ
4062static void
4063add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4064 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4065 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4066{
72bf9492 4067 /* Add a symbol for the namespace. */
e7c27a73 4068
72bf9492 4069 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4070
4071 /* Now scan partial symbols in that namespace. */
4072
91c24f0a 4073 if (pdi->has_children)
5734ee8b 4074 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4075}
4076
5d7cb8df
JK
4077/* Read a partial die corresponding to a Fortran module. */
4078
4079static void
4080add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4081 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4082{
f55ee35c 4083 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4084
4085 if (pdi->has_children)
4086 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4087}
4088
bc30ff58
JB
4089/* Read a partial die corresponding to a subprogram and create a partial
4090 symbol for that subprogram. When the CU language allows it, this
4091 routine also defines a partial symbol for each nested subprogram
4092 that this subprogram contains.
6e70227d 4093
bc30ff58
JB
4094 DIE my also be a lexical block, in which case we simply search
4095 recursively for suprograms defined inside that lexical block.
4096 Again, this is only performed when the CU language allows this
4097 type of definitions. */
4098
4099static void
4100add_partial_subprogram (struct partial_die_info *pdi,
4101 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4102 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4103{
4104 if (pdi->tag == DW_TAG_subprogram)
4105 {
4106 if (pdi->has_pc_info)
4107 {
4108 if (pdi->lowpc < *lowpc)
4109 *lowpc = pdi->lowpc;
4110 if (pdi->highpc > *highpc)
4111 *highpc = pdi->highpc;
5734ee8b
DJ
4112 if (need_pc)
4113 {
4114 CORE_ADDR baseaddr;
4115 struct objfile *objfile = cu->objfile;
4116
4117 baseaddr = ANOFFSET (objfile->section_offsets,
4118 SECT_OFF_TEXT (objfile));
4119 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4120 pdi->lowpc + baseaddr,
4121 pdi->highpc - 1 + baseaddr,
9291a0cd 4122 cu->per_cu->v.psymtab);
5734ee8b 4123 }
bc30ff58 4124 if (!pdi->is_declaration)
e8d05480
JB
4125 /* Ignore subprogram DIEs that do not have a name, they are
4126 illegal. Do not emit a complaint at this point, we will
4127 do so when we convert this psymtab into a symtab. */
4128 if (pdi->name)
4129 add_partial_symbol (pdi, cu);
bc30ff58
JB
4130 }
4131 }
6e70227d 4132
bc30ff58
JB
4133 if (! pdi->has_children)
4134 return;
4135
4136 if (cu->language == language_ada)
4137 {
4138 pdi = pdi->die_child;
4139 while (pdi != NULL)
4140 {
4141 fixup_partial_die (pdi, cu);
4142 if (pdi->tag == DW_TAG_subprogram
4143 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4144 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4145 pdi = pdi->die_sibling;
4146 }
4147 }
4148}
4149
91c24f0a
DC
4150/* Read a partial die corresponding to an enumeration type. */
4151
72bf9492
DJ
4152static void
4153add_partial_enumeration (struct partial_die_info *enum_pdi,
4154 struct dwarf2_cu *cu)
91c24f0a 4155{
72bf9492 4156 struct partial_die_info *pdi;
91c24f0a
DC
4157
4158 if (enum_pdi->name != NULL)
72bf9492
DJ
4159 add_partial_symbol (enum_pdi, cu);
4160
4161 pdi = enum_pdi->die_child;
4162 while (pdi)
91c24f0a 4163 {
72bf9492 4164 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4165 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4166 else
72bf9492
DJ
4167 add_partial_symbol (pdi, cu);
4168 pdi = pdi->die_sibling;
91c24f0a 4169 }
91c24f0a
DC
4170}
4171
4bb7a0a7
DJ
4172/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4173 Return the corresponding abbrev, or NULL if the number is zero (indicating
4174 an empty DIE). In either case *BYTES_READ will be set to the length of
4175 the initial number. */
4176
4177static struct abbrev_info *
fe1b8b76 4178peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4179 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4180{
4181 bfd *abfd = cu->objfile->obfd;
4182 unsigned int abbrev_number;
4183 struct abbrev_info *abbrev;
4184
4185 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4186
4187 if (abbrev_number == 0)
4188 return NULL;
4189
4190 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4191 if (!abbrev)
4192 {
3e43a32a
MS
4193 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4194 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4195 }
4196
4197 return abbrev;
4198}
4199
93311388
DE
4200/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4201 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4202 DIE. Any children of the skipped DIEs will also be skipped. */
4203
fe1b8b76 4204static gdb_byte *
93311388 4205skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4206{
4207 struct abbrev_info *abbrev;
4208 unsigned int bytes_read;
4209
4210 while (1)
4211 {
4212 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4213 if (abbrev == NULL)
4214 return info_ptr + bytes_read;
4215 else
93311388 4216 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4217 }
4218}
4219
93311388
DE
4220/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4221 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4222 abbrev corresponding to that skipped uleb128 should be passed in
4223 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4224 children. */
4225
fe1b8b76 4226static gdb_byte *
93311388
DE
4227skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4228 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4229{
4230 unsigned int bytes_read;
4231 struct attribute attr;
4232 bfd *abfd = cu->objfile->obfd;
4233 unsigned int form, i;
4234
4235 for (i = 0; i < abbrev->num_attrs; i++)
4236 {
4237 /* The only abbrev we care about is DW_AT_sibling. */
4238 if (abbrev->attrs[i].name == DW_AT_sibling)
4239 {
4240 read_attribute (&attr, &abbrev->attrs[i],
4241 abfd, info_ptr, cu);
4242 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4243 complaint (&symfile_complaints,
4244 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4245 else
93311388 4246 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4247 }
4248
4249 /* If it isn't DW_AT_sibling, skip this attribute. */
4250 form = abbrev->attrs[i].form;
4251 skip_attribute:
4252 switch (form)
4253 {
4bb7a0a7 4254 case DW_FORM_ref_addr:
ae411497
TT
4255 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4256 and later it is offset sized. */
4257 if (cu->header.version == 2)
4258 info_ptr += cu->header.addr_size;
4259 else
4260 info_ptr += cu->header.offset_size;
4261 break;
4262 case DW_FORM_addr:
4bb7a0a7
DJ
4263 info_ptr += cu->header.addr_size;
4264 break;
4265 case DW_FORM_data1:
4266 case DW_FORM_ref1:
4267 case DW_FORM_flag:
4268 info_ptr += 1;
4269 break;
2dc7f7b3
TT
4270 case DW_FORM_flag_present:
4271 break;
4bb7a0a7
DJ
4272 case DW_FORM_data2:
4273 case DW_FORM_ref2:
4274 info_ptr += 2;
4275 break;
4276 case DW_FORM_data4:
4277 case DW_FORM_ref4:
4278 info_ptr += 4;
4279 break;
4280 case DW_FORM_data8:
4281 case DW_FORM_ref8:
55f1336d 4282 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4283 info_ptr += 8;
4284 break;
4285 case DW_FORM_string:
9b1c24c8 4286 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4287 info_ptr += bytes_read;
4288 break;
2dc7f7b3 4289 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4290 case DW_FORM_strp:
4291 info_ptr += cu->header.offset_size;
4292 break;
2dc7f7b3 4293 case DW_FORM_exprloc:
4bb7a0a7
DJ
4294 case DW_FORM_block:
4295 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4296 info_ptr += bytes_read;
4297 break;
4298 case DW_FORM_block1:
4299 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4300 break;
4301 case DW_FORM_block2:
4302 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4303 break;
4304 case DW_FORM_block4:
4305 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4306 break;
4307 case DW_FORM_sdata:
4308 case DW_FORM_udata:
4309 case DW_FORM_ref_udata:
4310 info_ptr = skip_leb128 (abfd, info_ptr);
4311 break;
4312 case DW_FORM_indirect:
4313 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4314 info_ptr += bytes_read;
4315 /* We need to continue parsing from here, so just go back to
4316 the top. */
4317 goto skip_attribute;
4318
4319 default:
3e43a32a
MS
4320 error (_("Dwarf Error: Cannot handle %s "
4321 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4322 dwarf_form_name (form),
4323 bfd_get_filename (abfd));
4324 }
4325 }
4326
4327 if (abbrev->has_children)
93311388 4328 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4329 else
4330 return info_ptr;
4331}
4332
93311388
DE
4333/* Locate ORIG_PDI's sibling.
4334 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4335 in BUFFER. */
91c24f0a 4336
fe1b8b76 4337static gdb_byte *
93311388
DE
4338locate_pdi_sibling (struct partial_die_info *orig_pdi,
4339 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4340 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4341{
4342 /* Do we know the sibling already? */
72bf9492 4343
91c24f0a
DC
4344 if (orig_pdi->sibling)
4345 return orig_pdi->sibling;
4346
4347 /* Are there any children to deal with? */
4348
4349 if (!orig_pdi->has_children)
4350 return info_ptr;
4351
4bb7a0a7 4352 /* Skip the children the long way. */
91c24f0a 4353
93311388 4354 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4355}
4356
c906108c
SS
4357/* Expand this partial symbol table into a full symbol table. */
4358
4359static void
fba45db2 4360dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4361{
c906108c
SS
4362 if (pst != NULL)
4363 {
4364 if (pst->readin)
4365 {
3e43a32a
MS
4366 warning (_("bug: psymtab for %s is already read in."),
4367 pst->filename);
c906108c
SS
4368 }
4369 else
4370 {
4371 if (info_verbose)
4372 {
3e43a32a
MS
4373 printf_filtered (_("Reading in symbols for %s..."),
4374 pst->filename);
c906108c
SS
4375 gdb_flush (gdb_stdout);
4376 }
4377
10b3939b
DJ
4378 /* Restore our global data. */
4379 dwarf2_per_objfile = objfile_data (pst->objfile,
4380 dwarf2_objfile_data_key);
4381
b2ab525c
KB
4382 /* If this psymtab is constructed from a debug-only objfile, the
4383 has_section_at_zero flag will not necessarily be correct. We
4384 can get the correct value for this flag by looking at the data
4385 associated with the (presumably stripped) associated objfile. */
4386 if (pst->objfile->separate_debug_objfile_backlink)
4387 {
4388 struct dwarf2_per_objfile *dpo_backlink
4389 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4390 dwarf2_objfile_data_key);
9a619af0 4391
b2ab525c
KB
4392 dwarf2_per_objfile->has_section_at_zero
4393 = dpo_backlink->has_section_at_zero;
4394 }
4395
98bfdba5
PA
4396 dwarf2_per_objfile->reading_partial_symbols = 0;
4397
c906108c
SS
4398 psymtab_to_symtab_1 (pst);
4399
4400 /* Finish up the debug error message. */
4401 if (info_verbose)
a3f17187 4402 printf_filtered (_("done.\n"));
c906108c
SS
4403 }
4404 }
4405}
4406
10b3939b
DJ
4407/* Add PER_CU to the queue. */
4408
4409static void
03dd20cc 4410queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4411{
4412 struct dwarf2_queue_item *item;
4413
4414 per_cu->queued = 1;
4415 item = xmalloc (sizeof (*item));
4416 item->per_cu = per_cu;
4417 item->next = NULL;
4418
4419 if (dwarf2_queue == NULL)
4420 dwarf2_queue = item;
4421 else
4422 dwarf2_queue_tail->next = item;
4423
4424 dwarf2_queue_tail = item;
4425}
4426
4427/* Process the queue. */
4428
4429static void
4430process_queue (struct objfile *objfile)
4431{
4432 struct dwarf2_queue_item *item, *next_item;
4433
03dd20cc
DJ
4434 /* The queue starts out with one item, but following a DIE reference
4435 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4436 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4437 {
9291a0cd
TT
4438 if (dwarf2_per_objfile->using_index
4439 ? !item->per_cu->v.quick->symtab
4440 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4441 process_full_comp_unit (item->per_cu);
4442
4443 item->per_cu->queued = 0;
4444 next_item = item->next;
4445 xfree (item);
4446 }
4447
4448 dwarf2_queue_tail = NULL;
4449}
4450
4451/* Free all allocated queue entries. This function only releases anything if
4452 an error was thrown; if the queue was processed then it would have been
4453 freed as we went along. */
4454
4455static void
4456dwarf2_release_queue (void *dummy)
4457{
4458 struct dwarf2_queue_item *item, *last;
4459
4460 item = dwarf2_queue;
4461 while (item)
4462 {
4463 /* Anything still marked queued is likely to be in an
4464 inconsistent state, so discard it. */
4465 if (item->per_cu->queued)
4466 {
4467 if (item->per_cu->cu != NULL)
4468 free_one_cached_comp_unit (item->per_cu->cu);
4469 item->per_cu->queued = 0;
4470 }
4471
4472 last = item;
4473 item = item->next;
4474 xfree (last);
4475 }
4476
4477 dwarf2_queue = dwarf2_queue_tail = NULL;
4478}
4479
4480/* Read in full symbols for PST, and anything it depends on. */
4481
c906108c 4482static void
fba45db2 4483psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4484{
10b3939b 4485 struct dwarf2_per_cu_data *per_cu;
c906108c 4486 struct cleanup *back_to;
aaa75496
JB
4487 int i;
4488
4489 for (i = 0; i < pst->number_of_dependencies; i++)
4490 if (!pst->dependencies[i]->readin)
4491 {
4492 /* Inform about additional files that need to be read in. */
4493 if (info_verbose)
4494 {
a3f17187 4495 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4496 fputs_filtered (" ", gdb_stdout);
4497 wrap_here ("");
4498 fputs_filtered ("and ", gdb_stdout);
4499 wrap_here ("");
4500 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4501 wrap_here (""); /* Flush output. */
aaa75496
JB
4502 gdb_flush (gdb_stdout);
4503 }
4504 psymtab_to_symtab_1 (pst->dependencies[i]);
4505 }
4506
e38df1d0 4507 per_cu = pst->read_symtab_private;
10b3939b
DJ
4508
4509 if (per_cu == NULL)
aaa75496
JB
4510 {
4511 /* It's an include file, no symbols to read for it.
4512 Everything is in the parent symtab. */
4513 pst->readin = 1;
4514 return;
4515 }
c906108c 4516
9291a0cd 4517 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4518}
4519
93311388 4520/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4521
93311388 4522static void
3e43a32a
MS
4523load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4524 struct objfile *objfile)
10b3939b 4525{
31ffec48 4526 bfd *abfd = objfile->obfd;
10b3939b 4527 struct dwarf2_cu *cu;
c764a876 4528 unsigned int offset;
93311388 4529 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4530 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4531 struct attribute *attr;
98bfdba5 4532 int read_cu = 0;
6502dd73 4533
348e048f
DE
4534 gdb_assert (! per_cu->from_debug_types);
4535
c906108c 4536 /* Set local variables from the partial symbol table info. */
10b3939b 4537 offset = per_cu->offset;
6502dd73 4538
be391dca 4539 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4540 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4541 beg_of_comp_unit = info_ptr;
63d06c5c 4542
98bfdba5
PA
4543 if (per_cu->cu == NULL)
4544 {
9816fde3
JK
4545 cu = xmalloc (sizeof (*cu));
4546 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4547
4548 read_cu = 1;
c906108c 4549
98bfdba5
PA
4550 /* If an error occurs while loading, release our storage. */
4551 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4552
98bfdba5
PA
4553 /* Read in the comp_unit header. */
4554 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4555
98bfdba5
PA
4556 /* Complete the cu_header. */
4557 cu->header.offset = offset;
4558 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4559
98bfdba5
PA
4560 /* Read the abbrevs for this compilation unit. */
4561 dwarf2_read_abbrevs (abfd, cu);
4562 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4563
98bfdba5
PA
4564 /* Link this compilation unit into the compilation unit tree. */
4565 per_cu->cu = cu;
4566 cu->per_cu = per_cu;
98bfdba5
PA
4567
4568 /* Link this CU into read_in_chain. */
4569 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4570 dwarf2_per_objfile->read_in_chain = per_cu;
4571 }
4572 else
4573 {
4574 cu = per_cu->cu;
4575 info_ptr += cu->header.first_die_offset;
4576 }
e142c38c 4577
93311388 4578 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4579
4580 /* We try not to read any attributes in this function, because not
4581 all objfiles needed for references have been loaded yet, and symbol
4582 table processing isn't initialized. But we have to set the CU language,
4583 or we won't be able to build types correctly. */
9816fde3 4584 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4585
a6c727b2
DJ
4586 /* Similarly, if we do not read the producer, we can not apply
4587 producer-specific interpretation. */
4588 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4589 if (attr)
4590 cu->producer = DW_STRING (attr);
4591
98bfdba5
PA
4592 if (read_cu)
4593 {
4594 do_cleanups (free_abbrevs_cleanup);
e142c38c 4595
98bfdba5
PA
4596 /* We've successfully allocated this compilation unit. Let our
4597 caller clean it up when finished with it. */
4598 discard_cleanups (free_cu_cleanup);
4599 }
10b3939b
DJ
4600}
4601
3da10d80
KS
4602/* Add a DIE to the delayed physname list. */
4603
4604static void
4605add_to_method_list (struct type *type, int fnfield_index, int index,
4606 const char *name, struct die_info *die,
4607 struct dwarf2_cu *cu)
4608{
4609 struct delayed_method_info mi;
4610 mi.type = type;
4611 mi.fnfield_index = fnfield_index;
4612 mi.index = index;
4613 mi.name = name;
4614 mi.die = die;
4615 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4616}
4617
4618/* A cleanup for freeing the delayed method list. */
4619
4620static void
4621free_delayed_list (void *ptr)
4622{
4623 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4624 if (cu->method_list != NULL)
4625 {
4626 VEC_free (delayed_method_info, cu->method_list);
4627 cu->method_list = NULL;
4628 }
4629}
4630
4631/* Compute the physnames of any methods on the CU's method list.
4632
4633 The computation of method physnames is delayed in order to avoid the
4634 (bad) condition that one of the method's formal parameters is of an as yet
4635 incomplete type. */
4636
4637static void
4638compute_delayed_physnames (struct dwarf2_cu *cu)
4639{
4640 int i;
4641 struct delayed_method_info *mi;
4642 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4643 {
1d06ead6 4644 const char *physname;
3da10d80
KS
4645 struct fn_fieldlist *fn_flp
4646 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4647 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4648 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4649 }
4650}
4651
4632c0d0
JK
4652/* Check for GCC >= 4.x. Return minor version (x) of 4.x in such case. If it
4653 is not GCC or it is GCC older than 4.x return -1. If it is GCC 5.x or
4654 higher return INT_MAX. */
8be455d7
JK
4655
4656static int
4632c0d0 4657producer_is_gcc_ge_4 (struct dwarf2_cu *cu)
8be455d7
JK
4658{
4659 const char *cs;
4660 int major, minor;
4661
4662 if (cu->producer == NULL)
4663 {
4664 /* For unknown compilers expect their behavior is not compliant. For GCC
4665 this case can also happen for -gdwarf-4 type units supported since
4666 gcc-4.5. */
4667
4632c0d0 4668 return -1;
8be455d7
JK
4669 }
4670
4671 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
4672
4673 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4674 {
4675 /* For non-GCC compilers expect their behavior is not compliant. */
4676
4632c0d0 4677 return -1;
8be455d7
JK
4678 }
4679 cs = &cu->producer[strlen ("GNU ")];
4680 while (*cs && !isdigit (*cs))
4681 cs++;
4682 if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4683 {
4684 /* Not recognized as GCC. */
4685
4632c0d0 4686 return -1;
8be455d7
JK
4687 }
4688
4632c0d0
JK
4689 if (major < 4)
4690 return -1;
4691 if (major > 4)
4692 return INT_MAX;
4693 return minor;
8be455d7
JK
4694}
4695
10b3939b
DJ
4696/* Generate full symbol information for PST and CU, whose DIEs have
4697 already been loaded into memory. */
4698
4699static void
4700process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4701{
10b3939b 4702 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4703 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4704 CORE_ADDR lowpc, highpc;
4705 struct symtab *symtab;
3da10d80 4706 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4707 CORE_ADDR baseaddr;
4708
4709 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4710
10b3939b
DJ
4711 buildsym_init ();
4712 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4713 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4714
4715 cu->list_in_scope = &file_symbols;
c906108c 4716
d85a05f0 4717 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4718
c906108c 4719 /* Do line number decoding in read_file_scope () */
10b3939b 4720 process_die (cu->dies, cu);
c906108c 4721
3da10d80
KS
4722 /* Now that we have processed all the DIEs in the CU, all the types
4723 should be complete, and it should now be safe to compute all of the
4724 physnames. */
4725 compute_delayed_physnames (cu);
4726 do_cleanups (delayed_list_cleanup);
4727
fae299cd
DC
4728 /* Some compilers don't define a DW_AT_high_pc attribute for the
4729 compilation unit. If the DW_AT_high_pc is missing, synthesize
4730 it, by scanning the DIE's below the compilation unit. */
10b3939b 4731 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4732
613e1657 4733 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4734
8be455d7 4735 if (symtab != NULL)
c906108c 4736 {
4632c0d0
JK
4737 int gcc_4_minor = producer_is_gcc_ge_4 (cu);
4738
8be455d7
JK
4739 /* Set symtab language to language from DW_AT_language. If the
4740 compilation is from a C file generated by language preprocessors, do
4741 not set the language if it was already deduced by start_subfile. */
4742 if (!(cu->language == language_c && symtab->language != language_c))
4743 symtab->language = cu->language;
4744
4745 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4746 produce DW_AT_location with location lists but it can be possibly
4747 invalid without -fvar-tracking.
4748
4749 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4750 needed, it would be wrong due to missing DW_AT_producer there.
4751
4752 Still one can confuse GDB by using non-standard GCC compilation
4753 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4754 */
4632c0d0 4755 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4756 symtab->locations_valid = 1;
e0d00bc7
JK
4757
4758 if (gcc_4_minor >= 5)
4759 symtab->epilogue_unwind_valid = 1;
c906108c 4760 }
9291a0cd
TT
4761
4762 if (dwarf2_per_objfile->using_index)
4763 per_cu->v.quick->symtab = symtab;
4764 else
4765 {
4766 struct partial_symtab *pst = per_cu->v.psymtab;
4767 pst->symtab = symtab;
4768 pst->readin = 1;
4769 }
c906108c
SS
4770
4771 do_cleanups (back_to);
4772}
4773
4774/* Process a die and its children. */
4775
4776static void
e7c27a73 4777process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4778{
4779 switch (die->tag)
4780 {
4781 case DW_TAG_padding:
4782 break;
4783 case DW_TAG_compile_unit:
e7c27a73 4784 read_file_scope (die, cu);
c906108c 4785 break;
348e048f
DE
4786 case DW_TAG_type_unit:
4787 read_type_unit_scope (die, cu);
4788 break;
c906108c 4789 case DW_TAG_subprogram:
c906108c 4790 case DW_TAG_inlined_subroutine:
edb3359d 4791 read_func_scope (die, cu);
c906108c
SS
4792 break;
4793 case DW_TAG_lexical_block:
14898363
L
4794 case DW_TAG_try_block:
4795 case DW_TAG_catch_block:
e7c27a73 4796 read_lexical_block_scope (die, cu);
c906108c
SS
4797 break;
4798 case DW_TAG_class_type:
680b30c7 4799 case DW_TAG_interface_type:
c906108c
SS
4800 case DW_TAG_structure_type:
4801 case DW_TAG_union_type:
134d01f1 4802 process_structure_scope (die, cu);
c906108c
SS
4803 break;
4804 case DW_TAG_enumeration_type:
134d01f1 4805 process_enumeration_scope (die, cu);
c906108c 4806 break;
134d01f1 4807
f792889a
DJ
4808 /* These dies have a type, but processing them does not create
4809 a symbol or recurse to process the children. Therefore we can
4810 read them on-demand through read_type_die. */
c906108c 4811 case DW_TAG_subroutine_type:
72019c9c 4812 case DW_TAG_set_type:
c906108c 4813 case DW_TAG_array_type:
c906108c 4814 case DW_TAG_pointer_type:
c906108c 4815 case DW_TAG_ptr_to_member_type:
c906108c 4816 case DW_TAG_reference_type:
c906108c 4817 case DW_TAG_string_type:
c906108c 4818 break;
134d01f1 4819
c906108c 4820 case DW_TAG_base_type:
a02abb62 4821 case DW_TAG_subrange_type:
cb249c71 4822 case DW_TAG_typedef:
134d01f1
DJ
4823 /* Add a typedef symbol for the type definition, if it has a
4824 DW_AT_name. */
f792889a 4825 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4826 break;
c906108c 4827 case DW_TAG_common_block:
e7c27a73 4828 read_common_block (die, cu);
c906108c
SS
4829 break;
4830 case DW_TAG_common_inclusion:
4831 break;
d9fa45fe 4832 case DW_TAG_namespace:
63d06c5c 4833 processing_has_namespace_info = 1;
e7c27a73 4834 read_namespace (die, cu);
d9fa45fe 4835 break;
5d7cb8df 4836 case DW_TAG_module:
f55ee35c 4837 processing_has_namespace_info = 1;
5d7cb8df
JK
4838 read_module (die, cu);
4839 break;
d9fa45fe
DC
4840 case DW_TAG_imported_declaration:
4841 case DW_TAG_imported_module:
63d06c5c 4842 processing_has_namespace_info = 1;
27aa8d6a
SW
4843 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4844 || cu->language != language_fortran))
4845 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4846 dwarf_tag_name (die->tag));
4847 read_import_statement (die, cu);
d9fa45fe 4848 break;
c906108c 4849 default:
e7c27a73 4850 new_symbol (die, NULL, cu);
c906108c
SS
4851 break;
4852 }
4853}
4854
94af9270
KS
4855/* A helper function for dwarf2_compute_name which determines whether DIE
4856 needs to have the name of the scope prepended to the name listed in the
4857 die. */
4858
4859static int
4860die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4861{
1c809c68
TT
4862 struct attribute *attr;
4863
94af9270
KS
4864 switch (die->tag)
4865 {
4866 case DW_TAG_namespace:
4867 case DW_TAG_typedef:
4868 case DW_TAG_class_type:
4869 case DW_TAG_interface_type:
4870 case DW_TAG_structure_type:
4871 case DW_TAG_union_type:
4872 case DW_TAG_enumeration_type:
4873 case DW_TAG_enumerator:
4874 case DW_TAG_subprogram:
4875 case DW_TAG_member:
4876 return 1;
4877
4878 case DW_TAG_variable:
c2b0a229 4879 case DW_TAG_constant:
94af9270
KS
4880 /* We only need to prefix "globally" visible variables. These include
4881 any variable marked with DW_AT_external or any variable that
4882 lives in a namespace. [Variables in anonymous namespaces
4883 require prefixing, but they are not DW_AT_external.] */
4884
4885 if (dwarf2_attr (die, DW_AT_specification, cu))
4886 {
4887 struct dwarf2_cu *spec_cu = cu;
9a619af0 4888
94af9270
KS
4889 return die_needs_namespace (die_specification (die, &spec_cu),
4890 spec_cu);
4891 }
4892
1c809c68 4893 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4894 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4895 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4896 return 0;
4897 /* A variable in a lexical block of some kind does not need a
4898 namespace, even though in C++ such variables may be external
4899 and have a mangled name. */
4900 if (die->parent->tag == DW_TAG_lexical_block
4901 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4902 || die->parent->tag == DW_TAG_catch_block
4903 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4904 return 0;
4905 return 1;
94af9270
KS
4906
4907 default:
4908 return 0;
4909 }
4910}
4911
98bfdba5
PA
4912/* Retrieve the last character from a mem_file. */
4913
4914static void
4915do_ui_file_peek_last (void *object, const char *buffer, long length)
4916{
4917 char *last_char_p = (char *) object;
4918
4919 if (length > 0)
4920 *last_char_p = buffer[length - 1];
4921}
4922
94af9270
KS
4923/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4924 compute the physname for the object, which include a method's
4925 formal parameters (C++/Java) and return type (Java).
4926
af6b7be1
JB
4927 For Ada, return the DIE's linkage name rather than the fully qualified
4928 name. PHYSNAME is ignored..
4929
94af9270
KS
4930 The result is allocated on the objfile_obstack and canonicalized. */
4931
4932static const char *
4933dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4934 int physname)
4935{
4936 if (name == NULL)
4937 name = dwarf2_name (die, cu);
4938
f55ee35c
JK
4939 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4940 compute it by typename_concat inside GDB. */
4941 if (cu->language == language_ada
4942 || (cu->language == language_fortran && physname))
4943 {
4944 /* For Ada unit, we prefer the linkage name over the name, as
4945 the former contains the exported name, which the user expects
4946 to be able to reference. Ideally, we want the user to be able
4947 to reference this entity using either natural or linkage name,
4948 but we haven't started looking at this enhancement yet. */
4949 struct attribute *attr;
4950
4951 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4952 if (attr == NULL)
4953 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4954 if (attr && DW_STRING (attr))
4955 return DW_STRING (attr);
4956 }
4957
94af9270
KS
4958 /* These are the only languages we know how to qualify names in. */
4959 if (name != NULL
f55ee35c
JK
4960 && (cu->language == language_cplus || cu->language == language_java
4961 || cu->language == language_fortran))
94af9270
KS
4962 {
4963 if (die_needs_namespace (die, cu))
4964 {
4965 long length;
4966 char *prefix;
4967 struct ui_file *buf;
4968
4969 prefix = determine_prefix (die, cu);
4970 buf = mem_fileopen ();
4971 if (*prefix != '\0')
4972 {
f55ee35c
JK
4973 char *prefixed_name = typename_concat (NULL, prefix, name,
4974 physname, cu);
9a619af0 4975
94af9270
KS
4976 fputs_unfiltered (prefixed_name, buf);
4977 xfree (prefixed_name);
4978 }
4979 else
62d5b8da 4980 fputs_unfiltered (name, buf);
94af9270 4981
98bfdba5
PA
4982 /* Template parameters may be specified in the DIE's DW_AT_name, or
4983 as children with DW_TAG_template_type_param or
4984 DW_TAG_value_type_param. If the latter, add them to the name
4985 here. If the name already has template parameters, then
4986 skip this step; some versions of GCC emit both, and
4987 it is more efficient to use the pre-computed name.
4988
4989 Something to keep in mind about this process: it is very
4990 unlikely, or in some cases downright impossible, to produce
4991 something that will match the mangled name of a function.
4992 If the definition of the function has the same debug info,
4993 we should be able to match up with it anyway. But fallbacks
4994 using the minimal symbol, for instance to find a method
4995 implemented in a stripped copy of libstdc++, will not work.
4996 If we do not have debug info for the definition, we will have to
4997 match them up some other way.
4998
4999 When we do name matching there is a related problem with function
5000 templates; two instantiated function templates are allowed to
5001 differ only by their return types, which we do not add here. */
5002
5003 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5004 {
5005 struct attribute *attr;
5006 struct die_info *child;
5007 int first = 1;
5008
5009 die->building_fullname = 1;
5010
5011 for (child = die->child; child != NULL; child = child->sibling)
5012 {
5013 struct type *type;
5014 long value;
5015 gdb_byte *bytes;
5016 struct dwarf2_locexpr_baton *baton;
5017 struct value *v;
5018
5019 if (child->tag != DW_TAG_template_type_param
5020 && child->tag != DW_TAG_template_value_param)
5021 continue;
5022
5023 if (first)
5024 {
5025 fputs_unfiltered ("<", buf);
5026 first = 0;
5027 }
5028 else
5029 fputs_unfiltered (", ", buf);
5030
5031 attr = dwarf2_attr (child, DW_AT_type, cu);
5032 if (attr == NULL)
5033 {
5034 complaint (&symfile_complaints,
5035 _("template parameter missing DW_AT_type"));
5036 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5037 continue;
5038 }
5039 type = die_type (child, cu);
5040
5041 if (child->tag == DW_TAG_template_type_param)
5042 {
5043 c_print_type (type, "", buf, -1, 0);
5044 continue;
5045 }
5046
5047 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5048 if (attr == NULL)
5049 {
5050 complaint (&symfile_complaints,
3e43a32a
MS
5051 _("template parameter missing "
5052 "DW_AT_const_value"));
98bfdba5
PA
5053 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5054 continue;
5055 }
5056
5057 dwarf2_const_value_attr (attr, type, name,
5058 &cu->comp_unit_obstack, cu,
5059 &value, &bytes, &baton);
5060
5061 if (TYPE_NOSIGN (type))
5062 /* GDB prints characters as NUMBER 'CHAR'. If that's
5063 changed, this can use value_print instead. */
5064 c_printchar (value, type, buf);
5065 else
5066 {
5067 struct value_print_options opts;
5068
5069 if (baton != NULL)
5070 v = dwarf2_evaluate_loc_desc (type, NULL,
5071 baton->data,
5072 baton->size,
5073 baton->per_cu);
5074 else if (bytes != NULL)
5075 {
5076 v = allocate_value (type);
5077 memcpy (value_contents_writeable (v), bytes,
5078 TYPE_LENGTH (type));
5079 }
5080 else
5081 v = value_from_longest (type, value);
5082
3e43a32a
MS
5083 /* Specify decimal so that we do not depend on
5084 the radix. */
98bfdba5
PA
5085 get_formatted_print_options (&opts, 'd');
5086 opts.raw = 1;
5087 value_print (v, buf, &opts);
5088 release_value (v);
5089 value_free (v);
5090 }
5091 }
5092
5093 die->building_fullname = 0;
5094
5095 if (!first)
5096 {
5097 /* Close the argument list, with a space if necessary
5098 (nested templates). */
5099 char last_char = '\0';
5100 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5101 if (last_char == '>')
5102 fputs_unfiltered (" >", buf);
5103 else
5104 fputs_unfiltered (">", buf);
5105 }
5106 }
5107
94af9270
KS
5108 /* For Java and C++ methods, append formal parameter type
5109 information, if PHYSNAME. */
6e70227d 5110
94af9270
KS
5111 if (physname && die->tag == DW_TAG_subprogram
5112 && (cu->language == language_cplus
5113 || cu->language == language_java))
5114 {
5115 struct type *type = read_type_die (die, cu);
5116
3167638f 5117 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5118
5119 if (cu->language == language_java)
5120 {
5121 /* For java, we must append the return type to method
0963b4bd 5122 names. */
94af9270
KS
5123 if (die->tag == DW_TAG_subprogram)
5124 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5125 0, 0);
5126 }
5127 else if (cu->language == language_cplus)
5128 {
60430eff
DJ
5129 /* Assume that an artificial first parameter is
5130 "this", but do not crash if it is not. RealView
5131 marks unnamed (and thus unused) parameters as
5132 artificial; there is no way to differentiate
5133 the two cases. */
94af9270
KS
5134 if (TYPE_NFIELDS (type) > 0
5135 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5136 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5137 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5138 0))))
94af9270
KS
5139 fputs_unfiltered (" const", buf);
5140 }
5141 }
5142
5143 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5144 &length);
5145 ui_file_delete (buf);
5146
5147 if (cu->language == language_cplus)
5148 {
5149 char *cname
5150 = dwarf2_canonicalize_name (name, cu,
5151 &cu->objfile->objfile_obstack);
9a619af0 5152
94af9270
KS
5153 if (cname != NULL)
5154 name = cname;
5155 }
5156 }
5157 }
5158
5159 return name;
5160}
5161
0114d602
DJ
5162/* Return the fully qualified name of DIE, based on its DW_AT_name.
5163 If scope qualifiers are appropriate they will be added. The result
5164 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5165 not have a name. NAME may either be from a previous call to
5166 dwarf2_name or NULL.
5167
0963b4bd 5168 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5169
5170static const char *
94af9270 5171dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5172{
94af9270
KS
5173 return dwarf2_compute_name (name, die, cu, 0);
5174}
0114d602 5175
94af9270
KS
5176/* Construct a physname for the given DIE in CU. NAME may either be
5177 from a previous call to dwarf2_name or NULL. The result will be
5178 allocated on the objfile_objstack or NULL if the DIE does not have a
5179 name.
0114d602 5180
94af9270 5181 The output string will be canonicalized (if C++/Java). */
0114d602 5182
94af9270
KS
5183static const char *
5184dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5185{
900e11f9
JK
5186 struct attribute *attr;
5187 const char *retval, *mangled = NULL, *canon = NULL;
5188 struct cleanup *back_to;
5189 int need_copy = 1;
5190
5191 /* In this case dwarf2_compute_name is just a shortcut not building anything
5192 on its own. */
5193 if (!die_needs_namespace (die, cu))
5194 return dwarf2_compute_name (name, die, cu, 1);
5195
5196 back_to = make_cleanup (null_cleanup, NULL);
5197
5198 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5199 if (!attr)
5200 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5201
5202 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5203 has computed. */
5204 if (attr && DW_STRING (attr))
5205 {
5206 char *demangled;
5207
5208 mangled = DW_STRING (attr);
5209
5210 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5211 type. It is easier for GDB users to search for such functions as
5212 `name(params)' than `long name(params)'. In such case the minimal
5213 symbol names do not match the full symbol names but for template
5214 functions there is never a need to look up their definition from their
5215 declaration so the only disadvantage remains the minimal symbol
5216 variant `long name(params)' does not have the proper inferior type.
5217 */
5218
5219 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5220 | (cu->language == language_java
5221 ? DMGL_JAVA | DMGL_RET_POSTFIX
5222 : DMGL_RET_DROP)));
5223 if (demangled)
5224 {
5225 make_cleanup (xfree, demangled);
5226 canon = demangled;
5227 }
5228 else
5229 {
5230 canon = mangled;
5231 need_copy = 0;
5232 }
5233 }
5234
5235 if (canon == NULL || check_physname)
5236 {
5237 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5238
5239 if (canon != NULL && strcmp (physname, canon) != 0)
5240 {
5241 /* It may not mean a bug in GDB. The compiler could also
5242 compute DW_AT_linkage_name incorrectly. But in such case
5243 GDB would need to be bug-to-bug compatible. */
5244
5245 complaint (&symfile_complaints,
5246 _("Computed physname <%s> does not match demangled <%s> "
5247 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5248 physname, canon, mangled, die->offset, cu->objfile->name);
5249
5250 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5251 is available here - over computed PHYSNAME. It is safer
5252 against both buggy GDB and buggy compilers. */
5253
5254 retval = canon;
5255 }
5256 else
5257 {
5258 retval = physname;
5259 need_copy = 0;
5260 }
5261 }
5262 else
5263 retval = canon;
5264
5265 if (need_copy)
5266 retval = obsavestring (retval, strlen (retval),
5267 &cu->objfile->objfile_obstack);
5268
5269 do_cleanups (back_to);
5270 return retval;
0114d602
DJ
5271}
5272
27aa8d6a
SW
5273/* Read the import statement specified by the given die and record it. */
5274
5275static void
5276read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5277{
5278 struct attribute *import_attr;
32019081 5279 struct die_info *imported_die, *child_die;
de4affc9 5280 struct dwarf2_cu *imported_cu;
27aa8d6a 5281 const char *imported_name;
794684b6 5282 const char *imported_name_prefix;
13387711
SW
5283 const char *canonical_name;
5284 const char *import_alias;
5285 const char *imported_declaration = NULL;
794684b6 5286 const char *import_prefix;
32019081
JK
5287 VEC (const_char_ptr) *excludes = NULL;
5288 struct cleanup *cleanups;
13387711
SW
5289
5290 char *temp;
27aa8d6a
SW
5291
5292 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5293 if (import_attr == NULL)
5294 {
5295 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5296 dwarf_tag_name (die->tag));
5297 return;
5298 }
5299
de4affc9
CC
5300 imported_cu = cu;
5301 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5302 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5303 if (imported_name == NULL)
5304 {
5305 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5306
5307 The import in the following code:
5308 namespace A
5309 {
5310 typedef int B;
5311 }
5312
5313 int main ()
5314 {
5315 using A::B;
5316 B b;
5317 return b;
5318 }
5319
5320 ...
5321 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5322 <52> DW_AT_decl_file : 1
5323 <53> DW_AT_decl_line : 6
5324 <54> DW_AT_import : <0x75>
5325 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5326 <59> DW_AT_name : B
5327 <5b> DW_AT_decl_file : 1
5328 <5c> DW_AT_decl_line : 2
5329 <5d> DW_AT_type : <0x6e>
5330 ...
5331 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5332 <76> DW_AT_byte_size : 4
5333 <77> DW_AT_encoding : 5 (signed)
5334
5335 imports the wrong die ( 0x75 instead of 0x58 ).
5336 This case will be ignored until the gcc bug is fixed. */
5337 return;
5338 }
5339
82856980
SW
5340 /* Figure out the local name after import. */
5341 import_alias = dwarf2_name (die, cu);
27aa8d6a 5342
794684b6
SW
5343 /* Figure out where the statement is being imported to. */
5344 import_prefix = determine_prefix (die, cu);
5345
5346 /* Figure out what the scope of the imported die is and prepend it
5347 to the name of the imported die. */
de4affc9 5348 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5349
f55ee35c
JK
5350 if (imported_die->tag != DW_TAG_namespace
5351 && imported_die->tag != DW_TAG_module)
794684b6 5352 {
13387711
SW
5353 imported_declaration = imported_name;
5354 canonical_name = imported_name_prefix;
794684b6 5355 }
13387711 5356 else if (strlen (imported_name_prefix) > 0)
794684b6 5357 {
13387711
SW
5358 temp = alloca (strlen (imported_name_prefix)
5359 + 2 + strlen (imported_name) + 1);
5360 strcpy (temp, imported_name_prefix);
5361 strcat (temp, "::");
5362 strcat (temp, imported_name);
5363 canonical_name = temp;
794684b6 5364 }
13387711
SW
5365 else
5366 canonical_name = imported_name;
794684b6 5367
32019081
JK
5368 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5369
5370 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5371 for (child_die = die->child; child_die && child_die->tag;
5372 child_die = sibling_die (child_die))
5373 {
5374 /* DWARF-4: A Fortran use statement with a “rename list” may be
5375 represented by an imported module entry with an import attribute
5376 referring to the module and owned entries corresponding to those
5377 entities that are renamed as part of being imported. */
5378
5379 if (child_die->tag != DW_TAG_imported_declaration)
5380 {
5381 complaint (&symfile_complaints,
5382 _("child DW_TAG_imported_declaration expected "
5383 "- DIE at 0x%x [in module %s]"),
5384 child_die->offset, cu->objfile->name);
5385 continue;
5386 }
5387
5388 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5389 if (import_attr == NULL)
5390 {
5391 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5392 dwarf_tag_name (child_die->tag));
5393 continue;
5394 }
5395
5396 imported_cu = cu;
5397 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5398 &imported_cu);
5399 imported_name = dwarf2_name (imported_die, imported_cu);
5400 if (imported_name == NULL)
5401 {
5402 complaint (&symfile_complaints,
5403 _("child DW_TAG_imported_declaration has unknown "
5404 "imported name - DIE at 0x%x [in module %s]"),
5405 child_die->offset, cu->objfile->name);
5406 continue;
5407 }
5408
5409 VEC_safe_push (const_char_ptr, excludes, imported_name);
5410
5411 process_die (child_die, cu);
5412 }
5413
c0cc3a76
SW
5414 cp_add_using_directive (import_prefix,
5415 canonical_name,
5416 import_alias,
13387711 5417 imported_declaration,
32019081 5418 excludes,
c0cc3a76 5419 &cu->objfile->objfile_obstack);
32019081
JK
5420
5421 do_cleanups (cleanups);
27aa8d6a
SW
5422}
5423
5fb290d7 5424static void
e142c38c 5425initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5426{
e142c38c 5427 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5428}
5429
ae2de4f8
DE
5430/* Cleanup function for read_file_scope. */
5431
cb1df416
DJ
5432static void
5433free_cu_line_header (void *arg)
5434{
5435 struct dwarf2_cu *cu = arg;
5436
5437 free_line_header (cu->line_header);
5438 cu->line_header = NULL;
5439}
5440
9291a0cd
TT
5441static void
5442find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5443 char **name, char **comp_dir)
5444{
5445 struct attribute *attr;
5446
5447 *name = NULL;
5448 *comp_dir = NULL;
5449
5450 /* Find the filename. Do not use dwarf2_name here, since the filename
5451 is not a source language identifier. */
5452 attr = dwarf2_attr (die, DW_AT_name, cu);
5453 if (attr)
5454 {
5455 *name = DW_STRING (attr);
5456 }
5457
5458 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5459 if (attr)
5460 *comp_dir = DW_STRING (attr);
5461 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5462 {
5463 *comp_dir = ldirname (*name);
5464 if (*comp_dir != NULL)
5465 make_cleanup (xfree, *comp_dir);
5466 }
5467 if (*comp_dir != NULL)
5468 {
5469 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5470 directory, get rid of it. */
5471 char *cp = strchr (*comp_dir, ':');
5472
5473 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5474 *comp_dir = cp + 1;
5475 }
5476
5477 if (*name == NULL)
5478 *name = "<unknown>";
5479}
5480
2ab95328
TT
5481/* Handle DW_AT_stmt_list for a compilation unit. */
5482
5483static void
5484handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5485 const char *comp_dir)
5486{
5487 struct attribute *attr;
5488 struct objfile *objfile = cu->objfile;
5489 bfd *abfd = objfile->obfd;
5490
5491 /* Decode line number information if present. We do this before
5492 processing child DIEs, so that the line header table is available
5493 for DW_AT_decl_file. */
5494 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5495 if (attr)
5496 {
5497 unsigned int line_offset = DW_UNSND (attr);
5498 struct line_header *line_header
5499 = dwarf_decode_line_header (line_offset, abfd, cu);
5500
5501 if (line_header)
5502 {
5503 cu->line_header = line_header;
5504 make_cleanup (free_cu_line_header, cu);
5505 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5506 }
5507 }
5508}
5509
ae2de4f8
DE
5510/* Process DW_TAG_compile_unit. */
5511
c906108c 5512static void
e7c27a73 5513read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5514{
e7c27a73 5515 struct objfile *objfile = cu->objfile;
debd256d 5516 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5517 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5518 CORE_ADDR highpc = ((CORE_ADDR) 0);
5519 struct attribute *attr;
e1024ff1 5520 char *name = NULL;
c906108c
SS
5521 char *comp_dir = NULL;
5522 struct die_info *child_die;
5523 bfd *abfd = objfile->obfd;
e142c38c 5524 CORE_ADDR baseaddr;
6e70227d 5525
e142c38c 5526 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5527
fae299cd 5528 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5529
5530 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5531 from finish_block. */
2acceee2 5532 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5533 lowpc = highpc;
5534 lowpc += baseaddr;
5535 highpc += baseaddr;
5536
9291a0cd 5537 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5538
e142c38c 5539 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5540 if (attr)
5541 {
e142c38c 5542 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5543 }
5544
b0f35d58 5545 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5546 if (attr)
b0f35d58 5547 cu->producer = DW_STRING (attr);
303b6f5d 5548
f4b8a18d
KW
5549 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5550 standardised yet. As a workaround for the language detection we fall
5551 back to the DW_AT_producer string. */
5552 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5553 cu->language = language_opencl;
5554
0963b4bd 5555 /* We assume that we're processing GCC output. */
c906108c 5556 processing_gcc_compilation = 2;
c906108c 5557
df8a16a1
DJ
5558 processing_has_namespace_info = 0;
5559
c906108c
SS
5560 start_symtab (name, comp_dir, lowpc);
5561 record_debugformat ("DWARF 2");
303b6f5d 5562 record_producer (cu->producer);
c906108c 5563
e142c38c 5564 initialize_cu_func_list (cu);
c906108c 5565
2ab95328 5566 handle_DW_AT_stmt_list (die, cu, comp_dir);
debd256d 5567
cb1df416
DJ
5568 /* Process all dies in compilation unit. */
5569 if (die->child != NULL)
5570 {
5571 child_die = die->child;
5572 while (child_die && child_die->tag)
5573 {
5574 process_die (child_die, cu);
5575 child_die = sibling_die (child_die);
5576 }
5577 }
5578
2e276125
JB
5579 /* Decode macro information, if present. Dwarf 2 macro information
5580 refers to information in the line number info statement program
5581 header, so we can only read it if we've read the header
5582 successfully. */
e142c38c 5583 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
2ab95328 5584 if (attr && cu->line_header)
2e276125
JB
5585 {
5586 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5587
2ab95328 5588 dwarf_decode_macros (cu->line_header, macro_offset,
e7c27a73 5589 comp_dir, abfd, cu);
2e276125 5590 }
debd256d 5591 do_cleanups (back_to);
5fb290d7
DJ
5592}
5593
ae2de4f8
DE
5594/* Process DW_TAG_type_unit.
5595 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5596 actual type being defined by this TU. In this case the first top
5597 level sibling is there to provide context only. */
5598
5599static void
5600read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5601{
5602 struct objfile *objfile = cu->objfile;
5603 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5604 CORE_ADDR lowpc;
5605 struct attribute *attr;
5606 char *name = NULL;
5607 char *comp_dir = NULL;
5608 struct die_info *child_die;
5609 bfd *abfd = objfile->obfd;
348e048f
DE
5610
5611 /* start_symtab needs a low pc, but we don't really have one.
5612 Do what read_file_scope would do in the absence of such info. */
5613 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5614
5615 /* Find the filename. Do not use dwarf2_name here, since the filename
5616 is not a source language identifier. */
5617 attr = dwarf2_attr (die, DW_AT_name, cu);
5618 if (attr)
5619 name = DW_STRING (attr);
5620
5621 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5622 if (attr)
5623 comp_dir = DW_STRING (attr);
5624 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5625 {
5626 comp_dir = ldirname (name);
5627 if (comp_dir != NULL)
5628 make_cleanup (xfree, comp_dir);
5629 }
5630
5631 if (name == NULL)
5632 name = "<unknown>";
5633
5634 attr = dwarf2_attr (die, DW_AT_language, cu);
5635 if (attr)
5636 set_cu_language (DW_UNSND (attr), cu);
5637
5638 /* This isn't technically needed today. It is done for symmetry
5639 with read_file_scope. */
5640 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5641 if (attr)
348e048f
DE
5642 cu->producer = DW_STRING (attr);
5643
0963b4bd 5644 /* We assume that we're processing GCC output. */
348e048f
DE
5645 processing_gcc_compilation = 2;
5646
5647 processing_has_namespace_info = 0;
5648
5649 start_symtab (name, comp_dir, lowpc);
5650 record_debugformat ("DWARF 2");
5651 record_producer (cu->producer);
5652
2ab95328
TT
5653 handle_DW_AT_stmt_list (die, cu, comp_dir);
5654
348e048f
DE
5655 /* Process the dies in the type unit. */
5656 if (die->child == NULL)
5657 {
5658 dump_die_for_error (die);
5659 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5660 bfd_get_filename (abfd));
5661 }
5662
5663 child_die = die->child;
5664
5665 while (child_die && child_die->tag)
5666 {
5667 process_die (child_die, cu);
5668
5669 child_die = sibling_die (child_die);
5670 }
5671
5672 do_cleanups (back_to);
5673}
5674
5fb290d7 5675static void
e142c38c
DJ
5676add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5677 struct dwarf2_cu *cu)
5fb290d7
DJ
5678{
5679 struct function_range *thisfn;
5680
5681 thisfn = (struct function_range *)
7b5a2f43 5682 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5683 thisfn->name = name;
5684 thisfn->lowpc = lowpc;
5685 thisfn->highpc = highpc;
5686 thisfn->seen_line = 0;
5687 thisfn->next = NULL;
5688
e142c38c
DJ
5689 if (cu->last_fn == NULL)
5690 cu->first_fn = thisfn;
5fb290d7 5691 else
e142c38c 5692 cu->last_fn->next = thisfn;
5fb290d7 5693
e142c38c 5694 cu->last_fn = thisfn;
c906108c
SS
5695}
5696
d389af10
JK
5697/* qsort helper for inherit_abstract_dies. */
5698
5699static int
5700unsigned_int_compar (const void *ap, const void *bp)
5701{
5702 unsigned int a = *(unsigned int *) ap;
5703 unsigned int b = *(unsigned int *) bp;
5704
5705 return (a > b) - (b > a);
5706}
5707
5708/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5709 Inherit only the children of the DW_AT_abstract_origin DIE not being
5710 already referenced by DW_AT_abstract_origin from the children of the
5711 current DIE. */
d389af10
JK
5712
5713static void
5714inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5715{
5716 struct die_info *child_die;
5717 unsigned die_children_count;
5718 /* CU offsets which were referenced by children of the current DIE. */
5719 unsigned *offsets;
5720 unsigned *offsets_end, *offsetp;
5721 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5722 struct die_info *origin_die;
5723 /* Iterator of the ORIGIN_DIE children. */
5724 struct die_info *origin_child_die;
5725 struct cleanup *cleanups;
5726 struct attribute *attr;
cd02d79d
PA
5727 struct dwarf2_cu *origin_cu;
5728 struct pending **origin_previous_list_in_scope;
d389af10
JK
5729
5730 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5731 if (!attr)
5732 return;
5733
cd02d79d
PA
5734 /* Note that following die references may follow to a die in a
5735 different cu. */
5736
5737 origin_cu = cu;
5738 origin_die = follow_die_ref (die, attr, &origin_cu);
5739
5740 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5741 symbols in. */
5742 origin_previous_list_in_scope = origin_cu->list_in_scope;
5743 origin_cu->list_in_scope = cu->list_in_scope;
5744
edb3359d
DJ
5745 if (die->tag != origin_die->tag
5746 && !(die->tag == DW_TAG_inlined_subroutine
5747 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5748 complaint (&symfile_complaints,
5749 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5750 die->offset, origin_die->offset);
5751
5752 child_die = die->child;
5753 die_children_count = 0;
5754 while (child_die && child_die->tag)
5755 {
5756 child_die = sibling_die (child_die);
5757 die_children_count++;
5758 }
5759 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5760 cleanups = make_cleanup (xfree, offsets);
5761
5762 offsets_end = offsets;
5763 child_die = die->child;
5764 while (child_die && child_die->tag)
5765 {
c38f313d
DJ
5766 /* For each CHILD_DIE, find the corresponding child of
5767 ORIGIN_DIE. If there is more than one layer of
5768 DW_AT_abstract_origin, follow them all; there shouldn't be,
5769 but GCC versions at least through 4.4 generate this (GCC PR
5770 40573). */
5771 struct die_info *child_origin_die = child_die;
cd02d79d 5772 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5773
c38f313d
DJ
5774 while (1)
5775 {
cd02d79d
PA
5776 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5777 child_origin_cu);
c38f313d
DJ
5778 if (attr == NULL)
5779 break;
cd02d79d
PA
5780 child_origin_die = follow_die_ref (child_origin_die, attr,
5781 &child_origin_cu);
c38f313d
DJ
5782 }
5783
d389af10
JK
5784 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5785 counterpart may exist. */
c38f313d 5786 if (child_origin_die != child_die)
d389af10 5787 {
edb3359d
DJ
5788 if (child_die->tag != child_origin_die->tag
5789 && !(child_die->tag == DW_TAG_inlined_subroutine
5790 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5791 complaint (&symfile_complaints,
5792 _("Child DIE 0x%x and its abstract origin 0x%x have "
5793 "different tags"), child_die->offset,
5794 child_origin_die->offset);
c38f313d
DJ
5795 if (child_origin_die->parent != origin_die)
5796 complaint (&symfile_complaints,
5797 _("Child DIE 0x%x and its abstract origin 0x%x have "
5798 "different parents"), child_die->offset,
5799 child_origin_die->offset);
5800 else
5801 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5802 }
5803 child_die = sibling_die (child_die);
5804 }
5805 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5806 unsigned_int_compar);
5807 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5808 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5809 complaint (&symfile_complaints,
5810 _("Multiple children of DIE 0x%x refer "
5811 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5812 die->offset, *offsetp);
5813
5814 offsetp = offsets;
5815 origin_child_die = origin_die->child;
5816 while (origin_child_die && origin_child_die->tag)
5817 {
5818 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5819 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5820 offsetp++;
5821 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5822 {
5823 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5824 process_die (origin_child_die, origin_cu);
d389af10
JK
5825 }
5826 origin_child_die = sibling_die (origin_child_die);
5827 }
cd02d79d 5828 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5829
5830 do_cleanups (cleanups);
5831}
5832
c906108c 5833static void
e7c27a73 5834read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5835{
e7c27a73 5836 struct objfile *objfile = cu->objfile;
52f0bd74 5837 struct context_stack *new;
c906108c
SS
5838 CORE_ADDR lowpc;
5839 CORE_ADDR highpc;
5840 struct die_info *child_die;
edb3359d 5841 struct attribute *attr, *call_line, *call_file;
c906108c 5842 char *name;
e142c38c 5843 CORE_ADDR baseaddr;
801e3a5b 5844 struct block *block;
edb3359d 5845 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5846 VEC (symbolp) *template_args = NULL;
5847 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5848
5849 if (inlined_func)
5850 {
5851 /* If we do not have call site information, we can't show the
5852 caller of this inlined function. That's too confusing, so
5853 only use the scope for local variables. */
5854 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5855 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5856 if (call_line == NULL || call_file == NULL)
5857 {
5858 read_lexical_block_scope (die, cu);
5859 return;
5860 }
5861 }
c906108c 5862
e142c38c
DJ
5863 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5864
94af9270 5865 name = dwarf2_name (die, cu);
c906108c 5866
e8d05480
JB
5867 /* Ignore functions with missing or empty names. These are actually
5868 illegal according to the DWARF standard. */
5869 if (name == NULL)
5870 {
5871 complaint (&symfile_complaints,
5872 _("missing name for subprogram DIE at %d"), die->offset);
5873 return;
5874 }
5875
5876 /* Ignore functions with missing or invalid low and high pc attributes. */
5877 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5878 {
ae4d0c03
PM
5879 attr = dwarf2_attr (die, DW_AT_external, cu);
5880 if (!attr || !DW_UNSND (attr))
5881 complaint (&symfile_complaints,
3e43a32a
MS
5882 _("cannot get low and high bounds "
5883 "for subprogram DIE at %d"),
ae4d0c03 5884 die->offset);
e8d05480
JB
5885 return;
5886 }
c906108c
SS
5887
5888 lowpc += baseaddr;
5889 highpc += baseaddr;
5890
5fb290d7 5891 /* Record the function range for dwarf_decode_lines. */
e142c38c 5892 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5893
34eaf542
TT
5894 /* If we have any template arguments, then we must allocate a
5895 different sort of symbol. */
5896 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5897 {
5898 if (child_die->tag == DW_TAG_template_type_param
5899 || child_die->tag == DW_TAG_template_value_param)
5900 {
5901 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5902 struct template_symbol);
5903 templ_func->base.is_cplus_template_function = 1;
5904 break;
5905 }
5906 }
5907
c906108c 5908 new = push_context (0, lowpc);
34eaf542
TT
5909 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5910 (struct symbol *) templ_func);
4c2df51b 5911
4cecd739
DJ
5912 /* If there is a location expression for DW_AT_frame_base, record
5913 it. */
e142c38c 5914 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5915 if (attr)
c034e007
AC
5916 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5917 expression is being recorded directly in the function's symbol
5918 and not in a separate frame-base object. I guess this hack is
5919 to avoid adding some sort of frame-base adjunct/annex to the
5920 function's symbol :-(. The problem with doing this is that it
5921 results in a function symbol with a location expression that
5922 has nothing to do with the location of the function, ouch! The
5923 relationship should be: a function's symbol has-a frame base; a
5924 frame-base has-a location expression. */
e7c27a73 5925 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5926
e142c38c 5927 cu->list_in_scope = &local_symbols;
c906108c 5928
639d11d3 5929 if (die->child != NULL)
c906108c 5930 {
639d11d3 5931 child_die = die->child;
c906108c
SS
5932 while (child_die && child_die->tag)
5933 {
34eaf542
TT
5934 if (child_die->tag == DW_TAG_template_type_param
5935 || child_die->tag == DW_TAG_template_value_param)
5936 {
5937 struct symbol *arg = new_symbol (child_die, NULL, cu);
5938
f1078f66
DJ
5939 if (arg != NULL)
5940 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
5941 }
5942 else
5943 process_die (child_die, cu);
c906108c
SS
5944 child_die = sibling_die (child_die);
5945 }
5946 }
5947
d389af10
JK
5948 inherit_abstract_dies (die, cu);
5949
4a811a97
UW
5950 /* If we have a DW_AT_specification, we might need to import using
5951 directives from the context of the specification DIE. See the
5952 comment in determine_prefix. */
5953 if (cu->language == language_cplus
5954 && dwarf2_attr (die, DW_AT_specification, cu))
5955 {
5956 struct dwarf2_cu *spec_cu = cu;
5957 struct die_info *spec_die = die_specification (die, &spec_cu);
5958
5959 while (spec_die)
5960 {
5961 child_die = spec_die->child;
5962 while (child_die && child_die->tag)
5963 {
5964 if (child_die->tag == DW_TAG_imported_module)
5965 process_die (child_die, spec_cu);
5966 child_die = sibling_die (child_die);
5967 }
5968
5969 /* In some cases, GCC generates specification DIEs that
5970 themselves contain DW_AT_specification attributes. */
5971 spec_die = die_specification (spec_die, &spec_cu);
5972 }
5973 }
5974
c906108c
SS
5975 new = pop_context ();
5976 /* Make a block for the local symbols within. */
801e3a5b
JB
5977 block = finish_block (new->name, &local_symbols, new->old_blocks,
5978 lowpc, highpc, objfile);
5979
df8a16a1 5980 /* For C++, set the block's scope. */
f55ee35c 5981 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5982 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5983 determine_prefix (die, cu),
df8a16a1
DJ
5984 processing_has_namespace_info);
5985
801e3a5b
JB
5986 /* If we have address ranges, record them. */
5987 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5988
34eaf542
TT
5989 /* Attach template arguments to function. */
5990 if (! VEC_empty (symbolp, template_args))
5991 {
5992 gdb_assert (templ_func != NULL);
5993
5994 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5995 templ_func->template_arguments
5996 = obstack_alloc (&objfile->objfile_obstack,
5997 (templ_func->n_template_arguments
5998 * sizeof (struct symbol *)));
5999 memcpy (templ_func->template_arguments,
6000 VEC_address (symbolp, template_args),
6001 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6002 VEC_free (symbolp, template_args);
6003 }
6004
208d8187
JB
6005 /* In C++, we can have functions nested inside functions (e.g., when
6006 a function declares a class that has methods). This means that
6007 when we finish processing a function scope, we may need to go
6008 back to building a containing block's symbol lists. */
6009 local_symbols = new->locals;
6010 param_symbols = new->params;
27aa8d6a 6011 using_directives = new->using_directives;
208d8187 6012
921e78cf
JB
6013 /* If we've finished processing a top-level function, subsequent
6014 symbols go in the file symbol list. */
6015 if (outermost_context_p ())
e142c38c 6016 cu->list_in_scope = &file_symbols;
c906108c
SS
6017}
6018
6019/* Process all the DIES contained within a lexical block scope. Start
6020 a new scope, process the dies, and then close the scope. */
6021
6022static void
e7c27a73 6023read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6024{
e7c27a73 6025 struct objfile *objfile = cu->objfile;
52f0bd74 6026 struct context_stack *new;
c906108c
SS
6027 CORE_ADDR lowpc, highpc;
6028 struct die_info *child_die;
e142c38c
DJ
6029 CORE_ADDR baseaddr;
6030
6031 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6032
6033 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6034 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6035 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6036 be nasty. Might be easier to properly extend generic blocks to
af34e669 6037 describe ranges. */
d85a05f0 6038 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6039 return;
6040 lowpc += baseaddr;
6041 highpc += baseaddr;
6042
6043 push_context (0, lowpc);
639d11d3 6044 if (die->child != NULL)
c906108c 6045 {
639d11d3 6046 child_die = die->child;
c906108c
SS
6047 while (child_die && child_die->tag)
6048 {
e7c27a73 6049 process_die (child_die, cu);
c906108c
SS
6050 child_die = sibling_die (child_die);
6051 }
6052 }
6053 new = pop_context ();
6054
8540c487 6055 if (local_symbols != NULL || using_directives != NULL)
c906108c 6056 {
801e3a5b
JB
6057 struct block *block
6058 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6059 highpc, objfile);
6060
6061 /* Note that recording ranges after traversing children, as we
6062 do here, means that recording a parent's ranges entails
6063 walking across all its children's ranges as they appear in
6064 the address map, which is quadratic behavior.
6065
6066 It would be nicer to record the parent's ranges before
6067 traversing its children, simply overriding whatever you find
6068 there. But since we don't even decide whether to create a
6069 block until after we've traversed its children, that's hard
6070 to do. */
6071 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6072 }
6073 local_symbols = new->locals;
27aa8d6a 6074 using_directives = new->using_directives;
c906108c
SS
6075}
6076
43039443 6077/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6078 Return 1 if the attributes are present and valid, otherwise, return 0.
6079 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6080
6081static int
6082dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6083 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6084 struct partial_symtab *ranges_pst)
43039443
JK
6085{
6086 struct objfile *objfile = cu->objfile;
6087 struct comp_unit_head *cu_header = &cu->header;
6088 bfd *obfd = objfile->obfd;
6089 unsigned int addr_size = cu_header->addr_size;
6090 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6091 /* Base address selection entry. */
6092 CORE_ADDR base;
6093 int found_base;
6094 unsigned int dummy;
6095 gdb_byte *buffer;
6096 CORE_ADDR marker;
6097 int low_set;
6098 CORE_ADDR low = 0;
6099 CORE_ADDR high = 0;
ff013f42 6100 CORE_ADDR baseaddr;
43039443 6101
d00adf39
DE
6102 found_base = cu->base_known;
6103 base = cu->base_address;
43039443 6104
be391dca 6105 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6106 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6107 {
6108 complaint (&symfile_complaints,
6109 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6110 offset);
6111 return 0;
6112 }
dce234bc 6113 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6114
6115 /* Read in the largest possible address. */
6116 marker = read_address (obfd, buffer, cu, &dummy);
6117 if ((marker & mask) == mask)
6118 {
6119 /* If we found the largest possible address, then
6120 read the base address. */
6121 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6122 buffer += 2 * addr_size;
6123 offset += 2 * addr_size;
6124 found_base = 1;
6125 }
6126
6127 low_set = 0;
6128
e7030f15 6129 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6130
43039443
JK
6131 while (1)
6132 {
6133 CORE_ADDR range_beginning, range_end;
6134
6135 range_beginning = read_address (obfd, buffer, cu, &dummy);
6136 buffer += addr_size;
6137 range_end = read_address (obfd, buffer, cu, &dummy);
6138 buffer += addr_size;
6139 offset += 2 * addr_size;
6140
6141 /* An end of list marker is a pair of zero addresses. */
6142 if (range_beginning == 0 && range_end == 0)
6143 /* Found the end of list entry. */
6144 break;
6145
6146 /* Each base address selection entry is a pair of 2 values.
6147 The first is the largest possible address, the second is
6148 the base address. Check for a base address here. */
6149 if ((range_beginning & mask) == mask)
6150 {
6151 /* If we found the largest possible address, then
6152 read the base address. */
6153 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6154 found_base = 1;
6155 continue;
6156 }
6157
6158 if (!found_base)
6159 {
6160 /* We have no valid base address for the ranges
6161 data. */
6162 complaint (&symfile_complaints,
6163 _("Invalid .debug_ranges data (no base address)"));
6164 return 0;
6165 }
6166
9277c30c
UW
6167 if (range_beginning > range_end)
6168 {
6169 /* Inverted range entries are invalid. */
6170 complaint (&symfile_complaints,
6171 _("Invalid .debug_ranges data (inverted range)"));
6172 return 0;
6173 }
6174
6175 /* Empty range entries have no effect. */
6176 if (range_beginning == range_end)
6177 continue;
6178
43039443
JK
6179 range_beginning += base;
6180 range_end += base;
6181
9277c30c 6182 if (ranges_pst != NULL)
ff013f42 6183 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6184 range_beginning + baseaddr,
6185 range_end - 1 + baseaddr,
ff013f42
JK
6186 ranges_pst);
6187
43039443
JK
6188 /* FIXME: This is recording everything as a low-high
6189 segment of consecutive addresses. We should have a
6190 data structure for discontiguous block ranges
6191 instead. */
6192 if (! low_set)
6193 {
6194 low = range_beginning;
6195 high = range_end;
6196 low_set = 1;
6197 }
6198 else
6199 {
6200 if (range_beginning < low)
6201 low = range_beginning;
6202 if (range_end > high)
6203 high = range_end;
6204 }
6205 }
6206
6207 if (! low_set)
6208 /* If the first entry is an end-of-list marker, the range
6209 describes an empty scope, i.e. no instructions. */
6210 return 0;
6211
6212 if (low_return)
6213 *low_return = low;
6214 if (high_return)
6215 *high_return = high;
6216 return 1;
6217}
6218
af34e669
DJ
6219/* Get low and high pc attributes from a die. Return 1 if the attributes
6220 are present and valid, otherwise, return 0. Return -1 if the range is
6221 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6222static int
af34e669 6223dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6224 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6225 struct partial_symtab *pst)
c906108c
SS
6226{
6227 struct attribute *attr;
af34e669
DJ
6228 CORE_ADDR low = 0;
6229 CORE_ADDR high = 0;
6230 int ret = 0;
c906108c 6231
e142c38c 6232 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6233 if (attr)
af34e669
DJ
6234 {
6235 high = DW_ADDR (attr);
e142c38c 6236 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6237 if (attr)
6238 low = DW_ADDR (attr);
6239 else
6240 /* Found high w/o low attribute. */
6241 return 0;
6242
6243 /* Found consecutive range of addresses. */
6244 ret = 1;
6245 }
c906108c 6246 else
af34e669 6247 {
e142c38c 6248 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6249 if (attr != NULL)
6250 {
af34e669 6251 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6252 .debug_ranges section. */
d85a05f0 6253 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6254 return 0;
43039443 6255 /* Found discontinuous range of addresses. */
af34e669
DJ
6256 ret = -1;
6257 }
6258 }
c906108c 6259
9373cf26
JK
6260 /* read_partial_die has also the strict LOW < HIGH requirement. */
6261 if (high <= low)
c906108c
SS
6262 return 0;
6263
6264 /* When using the GNU linker, .gnu.linkonce. sections are used to
6265 eliminate duplicate copies of functions and vtables and such.
6266 The linker will arbitrarily choose one and discard the others.
6267 The AT_*_pc values for such functions refer to local labels in
6268 these sections. If the section from that file was discarded, the
6269 labels are not in the output, so the relocs get a value of 0.
6270 If this is a discarded function, mark the pc bounds as invalid,
6271 so that GDB will ignore it. */
72dca2f5 6272 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6273 return 0;
6274
6275 *lowpc = low;
6276 *highpc = high;
af34e669 6277 return ret;
c906108c
SS
6278}
6279
b084d499
JB
6280/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6281 its low and high PC addresses. Do nothing if these addresses could not
6282 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6283 and HIGHPC to the high address if greater than HIGHPC. */
6284
6285static void
6286dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6287 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6288 struct dwarf2_cu *cu)
6289{
6290 CORE_ADDR low, high;
6291 struct die_info *child = die->child;
6292
d85a05f0 6293 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6294 {
6295 *lowpc = min (*lowpc, low);
6296 *highpc = max (*highpc, high);
6297 }
6298
6299 /* If the language does not allow nested subprograms (either inside
6300 subprograms or lexical blocks), we're done. */
6301 if (cu->language != language_ada)
6302 return;
6e70227d 6303
b084d499
JB
6304 /* Check all the children of the given DIE. If it contains nested
6305 subprograms, then check their pc bounds. Likewise, we need to
6306 check lexical blocks as well, as they may also contain subprogram
6307 definitions. */
6308 while (child && child->tag)
6309 {
6310 if (child->tag == DW_TAG_subprogram
6311 || child->tag == DW_TAG_lexical_block)
6312 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6313 child = sibling_die (child);
6314 }
6315}
6316
fae299cd
DC
6317/* Get the low and high pc's represented by the scope DIE, and store
6318 them in *LOWPC and *HIGHPC. If the correct values can't be
6319 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6320
6321static void
6322get_scope_pc_bounds (struct die_info *die,
6323 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6324 struct dwarf2_cu *cu)
6325{
6326 CORE_ADDR best_low = (CORE_ADDR) -1;
6327 CORE_ADDR best_high = (CORE_ADDR) 0;
6328 CORE_ADDR current_low, current_high;
6329
d85a05f0 6330 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6331 {
6332 best_low = current_low;
6333 best_high = current_high;
6334 }
6335 else
6336 {
6337 struct die_info *child = die->child;
6338
6339 while (child && child->tag)
6340 {
6341 switch (child->tag) {
6342 case DW_TAG_subprogram:
b084d499 6343 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6344 break;
6345 case DW_TAG_namespace:
f55ee35c 6346 case DW_TAG_module:
fae299cd
DC
6347 /* FIXME: carlton/2004-01-16: Should we do this for
6348 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6349 that current GCC's always emit the DIEs corresponding
6350 to definitions of methods of classes as children of a
6351 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6352 the DIEs giving the declarations, which could be
6353 anywhere). But I don't see any reason why the
6354 standards says that they have to be there. */
6355 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6356
6357 if (current_low != ((CORE_ADDR) -1))
6358 {
6359 best_low = min (best_low, current_low);
6360 best_high = max (best_high, current_high);
6361 }
6362 break;
6363 default:
0963b4bd 6364 /* Ignore. */
fae299cd
DC
6365 break;
6366 }
6367
6368 child = sibling_die (child);
6369 }
6370 }
6371
6372 *lowpc = best_low;
6373 *highpc = best_high;
6374}
6375
801e3a5b
JB
6376/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6377 in DIE. */
6378static void
6379dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6380 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6381{
6382 struct attribute *attr;
6383
6384 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6385 if (attr)
6386 {
6387 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6388
801e3a5b
JB
6389 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6390 if (attr)
6391 {
6392 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6393
801e3a5b
JB
6394 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6395 }
6396 }
6397
6398 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6399 if (attr)
6400 {
6401 bfd *obfd = cu->objfile->obfd;
6402
6403 /* The value of the DW_AT_ranges attribute is the offset of the
6404 address range list in the .debug_ranges section. */
6405 unsigned long offset = DW_UNSND (attr);
dce234bc 6406 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6407
6408 /* For some target architectures, but not others, the
6409 read_address function sign-extends the addresses it returns.
6410 To recognize base address selection entries, we need a
6411 mask. */
6412 unsigned int addr_size = cu->header.addr_size;
6413 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6414
6415 /* The base address, to which the next pair is relative. Note
6416 that this 'base' is a DWARF concept: most entries in a range
6417 list are relative, to reduce the number of relocs against the
6418 debugging information. This is separate from this function's
6419 'baseaddr' argument, which GDB uses to relocate debugging
6420 information from a shared library based on the address at
6421 which the library was loaded. */
d00adf39
DE
6422 CORE_ADDR base = cu->base_address;
6423 int base_known = cu->base_known;
801e3a5b 6424
be391dca 6425 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6426 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6427 {
6428 complaint (&symfile_complaints,
6429 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6430 offset);
6431 return;
6432 }
6433
6434 for (;;)
6435 {
6436 unsigned int bytes_read;
6437 CORE_ADDR start, end;
6438
6439 start = read_address (obfd, buffer, cu, &bytes_read);
6440 buffer += bytes_read;
6441 end = read_address (obfd, buffer, cu, &bytes_read);
6442 buffer += bytes_read;
6443
6444 /* Did we find the end of the range list? */
6445 if (start == 0 && end == 0)
6446 break;
6447
6448 /* Did we find a base address selection entry? */
6449 else if ((start & base_select_mask) == base_select_mask)
6450 {
6451 base = end;
6452 base_known = 1;
6453 }
6454
6455 /* We found an ordinary address range. */
6456 else
6457 {
6458 if (!base_known)
6459 {
6460 complaint (&symfile_complaints,
3e43a32a
MS
6461 _("Invalid .debug_ranges data "
6462 "(no base address)"));
801e3a5b
JB
6463 return;
6464 }
6465
9277c30c
UW
6466 if (start > end)
6467 {
6468 /* Inverted range entries are invalid. */
6469 complaint (&symfile_complaints,
6470 _("Invalid .debug_ranges data "
6471 "(inverted range)"));
6472 return;
6473 }
6474
6475 /* Empty range entries have no effect. */
6476 if (start == end)
6477 continue;
6478
6e70227d
DE
6479 record_block_range (block,
6480 baseaddr + base + start,
801e3a5b
JB
6481 baseaddr + base + end - 1);
6482 }
6483 }
6484 }
6485}
6486
60d5a603
JK
6487/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6488 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6489 during 4.6.0 experimental. */
6490
6491static int
6492producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6493{
6494 const char *cs;
6495 int major, minor, release;
6496
6497 if (cu->producer == NULL)
6498 {
6499 /* For unknown compilers expect their behavior is DWARF version
6500 compliant.
6501
6502 GCC started to support .debug_types sections by -gdwarf-4 since
6503 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6504 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6505 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6506 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6507
6508 return 0;
6509 }
6510
6511 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6512
6513 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6514 {
6515 /* For non-GCC compilers expect their behavior is DWARF version
6516 compliant. */
6517
6518 return 0;
6519 }
6520 cs = &cu->producer[strlen ("GNU ")];
6521 while (*cs && !isdigit (*cs))
6522 cs++;
6523 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6524 {
6525 /* Not recognized as GCC. */
6526
6527 return 0;
6528 }
6529
6530 return major < 4 || (major == 4 && minor < 6);
6531}
6532
6533/* Return the default accessibility type if it is not overriden by
6534 DW_AT_accessibility. */
6535
6536static enum dwarf_access_attribute
6537dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6538{
6539 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6540 {
6541 /* The default DWARF 2 accessibility for members is public, the default
6542 accessibility for inheritance is private. */
6543
6544 if (die->tag != DW_TAG_inheritance)
6545 return DW_ACCESS_public;
6546 else
6547 return DW_ACCESS_private;
6548 }
6549 else
6550 {
6551 /* DWARF 3+ defines the default accessibility a different way. The same
6552 rules apply now for DW_TAG_inheritance as for the members and it only
6553 depends on the container kind. */
6554
6555 if (die->parent->tag == DW_TAG_class_type)
6556 return DW_ACCESS_private;
6557 else
6558 return DW_ACCESS_public;
6559 }
6560}
6561
74ac6d43
TT
6562/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6563 offset. If the attribute was not found return 0, otherwise return
6564 1. If it was found but could not properly be handled, set *OFFSET
6565 to 0. */
6566
6567static int
6568handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6569 LONGEST *offset)
6570{
6571 struct attribute *attr;
6572
6573 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6574 if (attr != NULL)
6575 {
6576 *offset = 0;
6577
6578 /* Note that we do not check for a section offset first here.
6579 This is because DW_AT_data_member_location is new in DWARF 4,
6580 so if we see it, we can assume that a constant form is really
6581 a constant and not a section offset. */
6582 if (attr_form_is_constant (attr))
6583 *offset = dwarf2_get_attr_constant_value (attr, 0);
6584 else if (attr_form_is_section_offset (attr))
6585 dwarf2_complex_location_expr_complaint ();
6586 else if (attr_form_is_block (attr))
6587 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6588 else
6589 dwarf2_complex_location_expr_complaint ();
6590
6591 return 1;
6592 }
6593
6594 return 0;
6595}
6596
c906108c
SS
6597/* Add an aggregate field to the field list. */
6598
6599static void
107d2387 6600dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6601 struct dwarf2_cu *cu)
6e70227d 6602{
e7c27a73 6603 struct objfile *objfile = cu->objfile;
5e2b427d 6604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6605 struct nextfield *new_field;
6606 struct attribute *attr;
6607 struct field *fp;
6608 char *fieldname = "";
6609
6610 /* Allocate a new field list entry and link it in. */
6611 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6612 make_cleanup (xfree, new_field);
c906108c 6613 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6614
6615 if (die->tag == DW_TAG_inheritance)
6616 {
6617 new_field->next = fip->baseclasses;
6618 fip->baseclasses = new_field;
6619 }
6620 else
6621 {
6622 new_field->next = fip->fields;
6623 fip->fields = new_field;
6624 }
c906108c
SS
6625 fip->nfields++;
6626
e142c38c 6627 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6628 if (attr)
6629 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
6630 else
6631 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
6632 if (new_field->accessibility != DW_ACCESS_public)
6633 fip->non_public_fields = 1;
60d5a603 6634
e142c38c 6635 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
6636 if (attr)
6637 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
6638 else
6639 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
6640
6641 fp = &new_field->field;
a9a9bd0f 6642
e142c38c 6643 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 6644 {
74ac6d43
TT
6645 LONGEST offset;
6646
a9a9bd0f 6647 /* Data member other than a C++ static data member. */
6e70227d 6648
c906108c 6649 /* Get type of field. */
e7c27a73 6650 fp->type = die_type (die, cu);
c906108c 6651
d6a843b5 6652 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 6653
c906108c 6654 /* Get bit size of field (zero if none). */
e142c38c 6655 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
6656 if (attr)
6657 {
6658 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6659 }
6660 else
6661 {
6662 FIELD_BITSIZE (*fp) = 0;
6663 }
6664
6665 /* Get bit offset of field. */
74ac6d43
TT
6666 if (handle_data_member_location (die, cu, &offset))
6667 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 6668 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
6669 if (attr)
6670 {
5e2b427d 6671 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
6672 {
6673 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
6674 additional bit offset from the MSB of the containing
6675 anonymous object to the MSB of the field. We don't
6676 have to do anything special since we don't need to
6677 know the size of the anonymous object. */
c906108c
SS
6678 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6679 }
6680 else
6681 {
6682 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
6683 MSB of the anonymous object, subtract off the number of
6684 bits from the MSB of the field to the MSB of the
6685 object, and then subtract off the number of bits of
6686 the field itself. The result is the bit offset of
6687 the LSB of the field. */
c906108c
SS
6688 int anonymous_size;
6689 int bit_offset = DW_UNSND (attr);
6690
e142c38c 6691 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6692 if (attr)
6693 {
6694 /* The size of the anonymous object containing
6695 the bit field is explicit, so use the
6696 indicated size (in bytes). */
6697 anonymous_size = DW_UNSND (attr);
6698 }
6699 else
6700 {
6701 /* The size of the anonymous object containing
6702 the bit field must be inferred from the type
6703 attribute of the data member containing the
6704 bit field. */
6705 anonymous_size = TYPE_LENGTH (fp->type);
6706 }
6707 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6708 - bit_offset - FIELD_BITSIZE (*fp);
6709 }
6710 }
6711
6712 /* Get name of field. */
39cbfefa
DJ
6713 fieldname = dwarf2_name (die, cu);
6714 if (fieldname == NULL)
6715 fieldname = "";
d8151005
DJ
6716
6717 /* The name is already allocated along with this objfile, so we don't
6718 need to duplicate it for the type. */
6719 fp->name = fieldname;
c906108c
SS
6720
6721 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 6722 pointer or virtual base class pointer) to private. */
e142c38c 6723 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 6724 {
d48cc9dd 6725 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
6726 new_field->accessibility = DW_ACCESS_private;
6727 fip->non_public_fields = 1;
6728 }
6729 }
a9a9bd0f 6730 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 6731 {
a9a9bd0f
DC
6732 /* C++ static member. */
6733
6734 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6735 is a declaration, but all versions of G++ as of this writing
6736 (so through at least 3.2.1) incorrectly generate
6737 DW_TAG_variable tags. */
6e70227d 6738
ff355380 6739 const char *physname;
c906108c 6740
a9a9bd0f 6741 /* Get name of field. */
39cbfefa
DJ
6742 fieldname = dwarf2_name (die, cu);
6743 if (fieldname == NULL)
c906108c
SS
6744 return;
6745
254e6b9e 6746 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
6747 if (attr
6748 /* Only create a symbol if this is an external value.
6749 new_symbol checks this and puts the value in the global symbol
6750 table, which we want. If it is not external, new_symbol
6751 will try to put the value in cu->list_in_scope which is wrong. */
6752 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
6753 {
6754 /* A static const member, not much different than an enum as far as
6755 we're concerned, except that we can support more types. */
6756 new_symbol (die, NULL, cu);
6757 }
6758
2df3850c 6759 /* Get physical name. */
ff355380 6760 physname = dwarf2_physname (fieldname, die, cu);
c906108c 6761
d8151005
DJ
6762 /* The name is already allocated along with this objfile, so we don't
6763 need to duplicate it for the type. */
6764 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 6765 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 6766 FIELD_NAME (*fp) = fieldname;
c906108c
SS
6767 }
6768 else if (die->tag == DW_TAG_inheritance)
6769 {
74ac6d43 6770 LONGEST offset;
d4b96c9a 6771
74ac6d43
TT
6772 /* C++ base class field. */
6773 if (handle_data_member_location (die, cu, &offset))
6774 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 6775 FIELD_BITSIZE (*fp) = 0;
e7c27a73 6776 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
6777 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6778 fip->nbaseclasses++;
6779 }
6780}
6781
98751a41
JK
6782/* Add a typedef defined in the scope of the FIP's class. */
6783
6784static void
6785dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6786 struct dwarf2_cu *cu)
6e70227d 6787{
98751a41 6788 struct objfile *objfile = cu->objfile;
98751a41
JK
6789 struct typedef_field_list *new_field;
6790 struct attribute *attr;
6791 struct typedef_field *fp;
6792 char *fieldname = "";
6793
6794 /* Allocate a new field list entry and link it in. */
6795 new_field = xzalloc (sizeof (*new_field));
6796 make_cleanup (xfree, new_field);
6797
6798 gdb_assert (die->tag == DW_TAG_typedef);
6799
6800 fp = &new_field->field;
6801
6802 /* Get name of field. */
6803 fp->name = dwarf2_name (die, cu);
6804 if (fp->name == NULL)
6805 return;
6806
6807 fp->type = read_type_die (die, cu);
6808
6809 new_field->next = fip->typedef_field_list;
6810 fip->typedef_field_list = new_field;
6811 fip->typedef_field_list_count++;
6812}
6813
c906108c
SS
6814/* Create the vector of fields, and attach it to the type. */
6815
6816static void
fba45db2 6817dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6818 struct dwarf2_cu *cu)
c906108c
SS
6819{
6820 int nfields = fip->nfields;
6821
6822 /* Record the field count, allocate space for the array of fields,
6823 and create blank accessibility bitfields if necessary. */
6824 TYPE_NFIELDS (type) = nfields;
6825 TYPE_FIELDS (type) = (struct field *)
6826 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6827 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6828
b4ba55a1 6829 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
6830 {
6831 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6832
6833 TYPE_FIELD_PRIVATE_BITS (type) =
6834 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6835 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6836
6837 TYPE_FIELD_PROTECTED_BITS (type) =
6838 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6839 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6840
774b6a14
TT
6841 TYPE_FIELD_IGNORE_BITS (type) =
6842 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6843 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
6844 }
6845
6846 /* If the type has baseclasses, allocate and clear a bit vector for
6847 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 6848 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
6849 {
6850 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 6851 unsigned char *pointer;
c906108c
SS
6852
6853 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
6854 pointer = TYPE_ALLOC (type, num_bytes);
6855 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
6856 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6857 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6858 }
6859
3e43a32a
MS
6860 /* Copy the saved-up fields into the field vector. Start from the head of
6861 the list, adding to the tail of the field array, so that they end up in
6862 the same order in the array in which they were added to the list. */
c906108c
SS
6863 while (nfields-- > 0)
6864 {
7d0ccb61
DJ
6865 struct nextfield *fieldp;
6866
6867 if (fip->fields)
6868 {
6869 fieldp = fip->fields;
6870 fip->fields = fieldp->next;
6871 }
6872 else
6873 {
6874 fieldp = fip->baseclasses;
6875 fip->baseclasses = fieldp->next;
6876 }
6877
6878 TYPE_FIELD (type, nfields) = fieldp->field;
6879 switch (fieldp->accessibility)
c906108c 6880 {
c5aa993b 6881 case DW_ACCESS_private:
b4ba55a1
JB
6882 if (cu->language != language_ada)
6883 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 6884 break;
c906108c 6885
c5aa993b 6886 case DW_ACCESS_protected:
b4ba55a1
JB
6887 if (cu->language != language_ada)
6888 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 6889 break;
c906108c 6890
c5aa993b
JM
6891 case DW_ACCESS_public:
6892 break;
c906108c 6893
c5aa993b
JM
6894 default:
6895 /* Unknown accessibility. Complain and treat it as public. */
6896 {
e2e0b3e5 6897 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 6898 fieldp->accessibility);
c5aa993b
JM
6899 }
6900 break;
c906108c
SS
6901 }
6902 if (nfields < fip->nbaseclasses)
6903 {
7d0ccb61 6904 switch (fieldp->virtuality)
c906108c 6905 {
c5aa993b
JM
6906 case DW_VIRTUALITY_virtual:
6907 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 6908 if (cu->language == language_ada)
a73c6dcd 6909 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
6910 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6911 break;
c906108c
SS
6912 }
6913 }
c906108c
SS
6914 }
6915}
6916
c906108c
SS
6917/* Add a member function to the proper fieldlist. */
6918
6919static void
107d2387 6920dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 6921 struct type *type, struct dwarf2_cu *cu)
c906108c 6922{
e7c27a73 6923 struct objfile *objfile = cu->objfile;
c906108c
SS
6924 struct attribute *attr;
6925 struct fnfieldlist *flp;
6926 int i;
6927 struct fn_field *fnp;
6928 char *fieldname;
c906108c 6929 struct nextfnfield *new_fnfield;
f792889a 6930 struct type *this_type;
60d5a603 6931 enum dwarf_access_attribute accessibility;
c906108c 6932
b4ba55a1 6933 if (cu->language == language_ada)
a73c6dcd 6934 error (_("unexpected member function in Ada type"));
b4ba55a1 6935
2df3850c 6936 /* Get name of member function. */
39cbfefa
DJ
6937 fieldname = dwarf2_name (die, cu);
6938 if (fieldname == NULL)
2df3850c 6939 return;
c906108c 6940
c906108c
SS
6941 /* Look up member function name in fieldlist. */
6942 for (i = 0; i < fip->nfnfields; i++)
6943 {
27bfe10e 6944 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6945 break;
6946 }
6947
6948 /* Create new list element if necessary. */
6949 if (i < fip->nfnfields)
6950 flp = &fip->fnfieldlists[i];
6951 else
6952 {
6953 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6954 {
6955 fip->fnfieldlists = (struct fnfieldlist *)
6956 xrealloc (fip->fnfieldlists,
6957 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6958 * sizeof (struct fnfieldlist));
c906108c 6959 if (fip->nfnfields == 0)
c13c43fd 6960 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6961 }
6962 flp = &fip->fnfieldlists[fip->nfnfields];
6963 flp->name = fieldname;
6964 flp->length = 0;
6965 flp->head = NULL;
3da10d80 6966 i = fip->nfnfields++;
c906108c
SS
6967 }
6968
6969 /* Create a new member function field and chain it to the field list
0963b4bd 6970 entry. */
c906108c 6971 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6972 make_cleanup (xfree, new_fnfield);
c906108c
SS
6973 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6974 new_fnfield->next = flp->head;
6975 flp->head = new_fnfield;
6976 flp->length++;
6977
6978 /* Fill in the member function field info. */
6979 fnp = &new_fnfield->fnfield;
3da10d80
KS
6980
6981 /* Delay processing of the physname until later. */
6982 if (cu->language == language_cplus || cu->language == language_java)
6983 {
6984 add_to_method_list (type, i, flp->length - 1, fieldname,
6985 die, cu);
6986 }
6987 else
6988 {
1d06ead6 6989 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
6990 fnp->physname = physname ? physname : "";
6991 }
6992
c906108c 6993 fnp->type = alloc_type (objfile);
f792889a
DJ
6994 this_type = read_type_die (die, cu);
6995 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6996 {
f792889a 6997 int nparams = TYPE_NFIELDS (this_type);
c906108c 6998
f792889a 6999 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7000 of the method itself (TYPE_CODE_METHOD). */
7001 smash_to_method_type (fnp->type, type,
f792889a
DJ
7002 TYPE_TARGET_TYPE (this_type),
7003 TYPE_FIELDS (this_type),
7004 TYPE_NFIELDS (this_type),
7005 TYPE_VARARGS (this_type));
c906108c
SS
7006
7007 /* Handle static member functions.
c5aa993b 7008 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7009 member functions. G++ helps GDB by marking the first
7010 parameter for non-static member functions (which is the this
7011 pointer) as artificial. We obtain this information from
7012 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7013 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7014 fnp->voffset = VOFFSET_STATIC;
7015 }
7016 else
e2e0b3e5 7017 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7018 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7019
7020 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7021 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7022 fnp->fcontext = die_containing_type (die, cu);
c906108c 7023
3e43a32a
MS
7024 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7025 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7026
7027 /* Get accessibility. */
e142c38c 7028 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7029 if (attr)
60d5a603
JK
7030 accessibility = DW_UNSND (attr);
7031 else
7032 accessibility = dwarf2_default_access_attribute (die, cu);
7033 switch (accessibility)
c906108c 7034 {
60d5a603
JK
7035 case DW_ACCESS_private:
7036 fnp->is_private = 1;
7037 break;
7038 case DW_ACCESS_protected:
7039 fnp->is_protected = 1;
7040 break;
c906108c
SS
7041 }
7042
b02dede2 7043 /* Check for artificial methods. */
e142c38c 7044 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7045 if (attr && DW_UNSND (attr) != 0)
7046 fnp->is_artificial = 1;
7047
0d564a31 7048 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7049 function. For older versions of GCC, this is an offset in the
7050 appropriate virtual table, as specified by DW_AT_containing_type.
7051 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7052 to the object address. */
7053
e142c38c 7054 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7055 if (attr)
8e19ed76 7056 {
aec5aa8b 7057 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7058 {
aec5aa8b
TT
7059 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7060 {
7061 /* Old-style GCC. */
7062 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7063 }
7064 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7065 || (DW_BLOCK (attr)->size > 1
7066 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7067 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7068 {
7069 struct dwarf_block blk;
7070 int offset;
7071
7072 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7073 ? 1 : 2);
7074 blk.size = DW_BLOCK (attr)->size - offset;
7075 blk.data = DW_BLOCK (attr)->data + offset;
7076 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7077 if ((fnp->voffset % cu->header.addr_size) != 0)
7078 dwarf2_complex_location_expr_complaint ();
7079 else
7080 fnp->voffset /= cu->header.addr_size;
7081 fnp->voffset += 2;
7082 }
7083 else
7084 dwarf2_complex_location_expr_complaint ();
7085
7086 if (!fnp->fcontext)
7087 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7088 }
3690dd37 7089 else if (attr_form_is_section_offset (attr))
8e19ed76 7090 {
4d3c2250 7091 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7092 }
7093 else
7094 {
4d3c2250
KB
7095 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7096 fieldname);
8e19ed76 7097 }
0d564a31 7098 }
d48cc9dd
DJ
7099 else
7100 {
7101 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7102 if (attr && DW_UNSND (attr))
7103 {
7104 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7105 complaint (&symfile_complaints,
3e43a32a
MS
7106 _("Member function \"%s\" (offset %d) is virtual "
7107 "but the vtable offset is not specified"),
d48cc9dd 7108 fieldname, die->offset);
9655fd1a 7109 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7110 TYPE_CPLUS_DYNAMIC (type) = 1;
7111 }
7112 }
c906108c
SS
7113}
7114
7115/* Create the vector of member function fields, and attach it to the type. */
7116
7117static void
fba45db2 7118dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7119 struct dwarf2_cu *cu)
c906108c
SS
7120{
7121 struct fnfieldlist *flp;
7122 int total_length = 0;
7123 int i;
7124
b4ba55a1 7125 if (cu->language == language_ada)
a73c6dcd 7126 error (_("unexpected member functions in Ada type"));
b4ba55a1 7127
c906108c
SS
7128 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7129 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7130 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7131
7132 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7133 {
7134 struct nextfnfield *nfp = flp->head;
7135 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7136 int k;
7137
7138 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7139 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7140 fn_flp->fn_fields = (struct fn_field *)
7141 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7142 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7143 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7144
7145 total_length += flp->length;
7146 }
7147
7148 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7149 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7150}
7151
1168df01
JB
7152/* Returns non-zero if NAME is the name of a vtable member in CU's
7153 language, zero otherwise. */
7154static int
7155is_vtable_name (const char *name, struct dwarf2_cu *cu)
7156{
7157 static const char vptr[] = "_vptr";
987504bb 7158 static const char vtable[] = "vtable";
1168df01 7159
987504bb
JJ
7160 /* Look for the C++ and Java forms of the vtable. */
7161 if ((cu->language == language_java
7162 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7163 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7164 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7165 return 1;
7166
7167 return 0;
7168}
7169
c0dd20ea 7170/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7171 functions, with the ABI-specified layout. If TYPE describes
7172 such a structure, smash it into a member function type.
61049d3b
DJ
7173
7174 GCC shouldn't do this; it should just output pointer to member DIEs.
7175 This is GCC PR debug/28767. */
c0dd20ea 7176
0b92b5bb
TT
7177static void
7178quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7179{
0b92b5bb 7180 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7181
7182 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7183 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7184 return;
c0dd20ea
DJ
7185
7186 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7187 if (TYPE_FIELD_NAME (type, 0) == NULL
7188 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7189 || TYPE_FIELD_NAME (type, 1) == NULL
7190 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7191 return;
c0dd20ea
DJ
7192
7193 /* Find the type of the method. */
0b92b5bb 7194 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7195 if (pfn_type == NULL
7196 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7197 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7198 return;
c0dd20ea
DJ
7199
7200 /* Look for the "this" argument. */
7201 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7202 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7203 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7204 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7205 return;
c0dd20ea
DJ
7206
7207 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7208 new_type = alloc_type (objfile);
7209 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7210 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7211 TYPE_VARARGS (pfn_type));
0b92b5bb 7212 smash_to_methodptr_type (type, new_type);
c0dd20ea 7213}
1168df01 7214
c906108c 7215/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7216 (definition) to create a type for the structure or union. Fill in
7217 the type's name and general properties; the members will not be
7218 processed until process_structure_type.
c906108c 7219
c767944b
DJ
7220 NOTE: we need to call these functions regardless of whether or not the
7221 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7222 structure or union. This gets the type entered into our set of
7223 user defined types.
7224
7225 However, if the structure is incomplete (an opaque struct/union)
7226 then suppress creating a symbol table entry for it since gdb only
7227 wants to find the one with the complete definition. Note that if
7228 it is complete, we just call new_symbol, which does it's own
7229 checking about whether the struct/union is anonymous or not (and
7230 suppresses creating a symbol table entry itself). */
7231
f792889a 7232static struct type *
134d01f1 7233read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7234{
e7c27a73 7235 struct objfile *objfile = cu->objfile;
c906108c
SS
7236 struct type *type;
7237 struct attribute *attr;
39cbfefa 7238 char *name;
c906108c 7239
348e048f
DE
7240 /* If the definition of this type lives in .debug_types, read that type.
7241 Don't follow DW_AT_specification though, that will take us back up
7242 the chain and we want to go down. */
7243 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7244 if (attr)
7245 {
7246 struct dwarf2_cu *type_cu = cu;
7247 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7248
348e048f
DE
7249 /* We could just recurse on read_structure_type, but we need to call
7250 get_die_type to ensure only one type for this DIE is created.
7251 This is important, for example, because for c++ classes we need
7252 TYPE_NAME set which is only done by new_symbol. Blech. */
7253 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7254
7255 /* TYPE_CU may not be the same as CU.
7256 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7257 return set_die_type (die, type, cu);
7258 }
7259
c0dd20ea 7260 type = alloc_type (objfile);
c906108c 7261 INIT_CPLUS_SPECIFIC (type);
93311388 7262
39cbfefa
DJ
7263 name = dwarf2_name (die, cu);
7264 if (name != NULL)
c906108c 7265 {
987504bb
JJ
7266 if (cu->language == language_cplus
7267 || cu->language == language_java)
63d06c5c 7268 {
3da10d80
KS
7269 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7270
7271 /* dwarf2_full_name might have already finished building the DIE's
7272 type. If so, there is no need to continue. */
7273 if (get_die_type (die, cu) != NULL)
7274 return get_die_type (die, cu);
7275
7276 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7277 if (die->tag == DW_TAG_structure_type
7278 || die->tag == DW_TAG_class_type)
7279 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7280 }
7281 else
7282 {
d8151005
DJ
7283 /* The name is already allocated along with this objfile, so
7284 we don't need to duplicate it for the type. */
94af9270
KS
7285 TYPE_TAG_NAME (type) = (char *) name;
7286 if (die->tag == DW_TAG_class_type)
7287 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7288 }
c906108c
SS
7289 }
7290
7291 if (die->tag == DW_TAG_structure_type)
7292 {
7293 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7294 }
7295 else if (die->tag == DW_TAG_union_type)
7296 {
7297 TYPE_CODE (type) = TYPE_CODE_UNION;
7298 }
7299 else
7300 {
c906108c
SS
7301 TYPE_CODE (type) = TYPE_CODE_CLASS;
7302 }
7303
0cc2414c
TT
7304 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7305 TYPE_DECLARED_CLASS (type) = 1;
7306
e142c38c 7307 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7308 if (attr)
7309 {
7310 TYPE_LENGTH (type) = DW_UNSND (attr);
7311 }
7312 else
7313 {
7314 TYPE_LENGTH (type) = 0;
7315 }
7316
876cecd0 7317 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7318 if (die_is_declaration (die, cu))
876cecd0 7319 TYPE_STUB (type) = 1;
a6c727b2
DJ
7320 else if (attr == NULL && die->child == NULL
7321 && producer_is_realview (cu->producer))
7322 /* RealView does not output the required DW_AT_declaration
7323 on incomplete types. */
7324 TYPE_STUB (type) = 1;
dc718098 7325
c906108c
SS
7326 /* We need to add the type field to the die immediately so we don't
7327 infinitely recurse when dealing with pointers to the structure
0963b4bd 7328 type within the structure itself. */
1c379e20 7329 set_die_type (die, type, cu);
c906108c 7330
7e314c57
JK
7331 /* set_die_type should be already done. */
7332 set_descriptive_type (type, die, cu);
7333
c767944b
DJ
7334 return type;
7335}
7336
7337/* Finish creating a structure or union type, including filling in
7338 its members and creating a symbol for it. */
7339
7340static void
7341process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7342{
7343 struct objfile *objfile = cu->objfile;
7344 struct die_info *child_die = die->child;
7345 struct type *type;
7346
7347 type = get_die_type (die, cu);
7348 if (type == NULL)
7349 type = read_structure_type (die, cu);
7350
e142c38c 7351 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7352 {
7353 struct field_info fi;
7354 struct die_info *child_die;
34eaf542 7355 VEC (symbolp) *template_args = NULL;
c767944b 7356 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7357
7358 memset (&fi, 0, sizeof (struct field_info));
7359
639d11d3 7360 child_die = die->child;
c906108c
SS
7361
7362 while (child_die && child_die->tag)
7363 {
a9a9bd0f
DC
7364 if (child_die->tag == DW_TAG_member
7365 || child_die->tag == DW_TAG_variable)
c906108c 7366 {
a9a9bd0f
DC
7367 /* NOTE: carlton/2002-11-05: A C++ static data member
7368 should be a DW_TAG_member that is a declaration, but
7369 all versions of G++ as of this writing (so through at
7370 least 3.2.1) incorrectly generate DW_TAG_variable
7371 tags for them instead. */
e7c27a73 7372 dwarf2_add_field (&fi, child_die, cu);
c906108c 7373 }
8713b1b1 7374 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7375 {
0963b4bd 7376 /* C++ member function. */
e7c27a73 7377 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7378 }
7379 else if (child_die->tag == DW_TAG_inheritance)
7380 {
7381 /* C++ base class field. */
e7c27a73 7382 dwarf2_add_field (&fi, child_die, cu);
c906108c 7383 }
98751a41
JK
7384 else if (child_die->tag == DW_TAG_typedef)
7385 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7386 else if (child_die->tag == DW_TAG_template_type_param
7387 || child_die->tag == DW_TAG_template_value_param)
7388 {
7389 struct symbol *arg = new_symbol (child_die, NULL, cu);
7390
f1078f66
DJ
7391 if (arg != NULL)
7392 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7393 }
7394
c906108c
SS
7395 child_die = sibling_die (child_die);
7396 }
7397
34eaf542
TT
7398 /* Attach template arguments to type. */
7399 if (! VEC_empty (symbolp, template_args))
7400 {
7401 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7402 TYPE_N_TEMPLATE_ARGUMENTS (type)
7403 = VEC_length (symbolp, template_args);
7404 TYPE_TEMPLATE_ARGUMENTS (type)
7405 = obstack_alloc (&objfile->objfile_obstack,
7406 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7407 * sizeof (struct symbol *)));
7408 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7409 VEC_address (symbolp, template_args),
7410 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7411 * sizeof (struct symbol *)));
7412 VEC_free (symbolp, template_args);
7413 }
7414
c906108c
SS
7415 /* Attach fields and member functions to the type. */
7416 if (fi.nfields)
e7c27a73 7417 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7418 if (fi.nfnfields)
7419 {
e7c27a73 7420 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7421
c5aa993b 7422 /* Get the type which refers to the base class (possibly this
c906108c 7423 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7424 class from the DW_AT_containing_type attribute. This use of
7425 DW_AT_containing_type is a GNU extension. */
c906108c 7426
e142c38c 7427 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7428 {
e7c27a73 7429 struct type *t = die_containing_type (die, cu);
c906108c
SS
7430
7431 TYPE_VPTR_BASETYPE (type) = t;
7432 if (type == t)
7433 {
c906108c
SS
7434 int i;
7435
7436 /* Our own class provides vtbl ptr. */
7437 for (i = TYPE_NFIELDS (t) - 1;
7438 i >= TYPE_N_BASECLASSES (t);
7439 --i)
7440 {
7441 char *fieldname = TYPE_FIELD_NAME (t, i);
7442
1168df01 7443 if (is_vtable_name (fieldname, cu))
c906108c
SS
7444 {
7445 TYPE_VPTR_FIELDNO (type) = i;
7446 break;
7447 }
7448 }
7449
7450 /* Complain if virtual function table field not found. */
7451 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7452 complaint (&symfile_complaints,
3e43a32a
MS
7453 _("virtual function table pointer "
7454 "not found when defining class '%s'"),
4d3c2250
KB
7455 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7456 "");
c906108c
SS
7457 }
7458 else
7459 {
7460 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7461 }
7462 }
f6235d4c
EZ
7463 else if (cu->producer
7464 && strncmp (cu->producer,
7465 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7466 {
7467 /* The IBM XLC compiler does not provide direct indication
7468 of the containing type, but the vtable pointer is
7469 always named __vfp. */
7470
7471 int i;
7472
7473 for (i = TYPE_NFIELDS (type) - 1;
7474 i >= TYPE_N_BASECLASSES (type);
7475 --i)
7476 {
7477 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7478 {
7479 TYPE_VPTR_FIELDNO (type) = i;
7480 TYPE_VPTR_BASETYPE (type) = type;
7481 break;
7482 }
7483 }
7484 }
c906108c 7485 }
98751a41
JK
7486
7487 /* Copy fi.typedef_field_list linked list elements content into the
7488 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7489 if (fi.typedef_field_list)
7490 {
7491 int i = fi.typedef_field_list_count;
7492
a0d7a4ff 7493 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7494 TYPE_TYPEDEF_FIELD_ARRAY (type)
7495 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7496 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7497
7498 /* Reverse the list order to keep the debug info elements order. */
7499 while (--i >= 0)
7500 {
7501 struct typedef_field *dest, *src;
6e70227d 7502
98751a41
JK
7503 dest = &TYPE_TYPEDEF_FIELD (type, i);
7504 src = &fi.typedef_field_list->field;
7505 fi.typedef_field_list = fi.typedef_field_list->next;
7506 *dest = *src;
7507 }
7508 }
c767944b
DJ
7509
7510 do_cleanups (back_to);
eb2a6f42
TT
7511
7512 if (HAVE_CPLUS_STRUCT (type))
7513 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7514 }
63d06c5c 7515
0b92b5bb
TT
7516 quirk_gcc_member_function_pointer (type, cu->objfile);
7517
90aeadfc
DC
7518 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7519 snapshots) has been known to create a die giving a declaration
7520 for a class that has, as a child, a die giving a definition for a
7521 nested class. So we have to process our children even if the
7522 current die is a declaration. Normally, of course, a declaration
7523 won't have any children at all. */
134d01f1 7524
90aeadfc
DC
7525 while (child_die != NULL && child_die->tag)
7526 {
7527 if (child_die->tag == DW_TAG_member
7528 || child_die->tag == DW_TAG_variable
34eaf542
TT
7529 || child_die->tag == DW_TAG_inheritance
7530 || child_die->tag == DW_TAG_template_value_param
7531 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7532 {
90aeadfc 7533 /* Do nothing. */
134d01f1 7534 }
90aeadfc
DC
7535 else
7536 process_die (child_die, cu);
134d01f1 7537
90aeadfc 7538 child_die = sibling_die (child_die);
134d01f1
DJ
7539 }
7540
fa4028e9
JB
7541 /* Do not consider external references. According to the DWARF standard,
7542 these DIEs are identified by the fact that they have no byte_size
7543 attribute, and a declaration attribute. */
7544 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7545 || !die_is_declaration (die, cu))
c767944b 7546 new_symbol (die, type, cu);
134d01f1
DJ
7547}
7548
7549/* Given a DW_AT_enumeration_type die, set its type. We do not
7550 complete the type's fields yet, or create any symbols. */
c906108c 7551
f792889a 7552static struct type *
134d01f1 7553read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7554{
e7c27a73 7555 struct objfile *objfile = cu->objfile;
c906108c 7556 struct type *type;
c906108c 7557 struct attribute *attr;
0114d602 7558 const char *name;
134d01f1 7559
348e048f
DE
7560 /* If the definition of this type lives in .debug_types, read that type.
7561 Don't follow DW_AT_specification though, that will take us back up
7562 the chain and we want to go down. */
7563 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7564 if (attr)
7565 {
7566 struct dwarf2_cu *type_cu = cu;
7567 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7568
348e048f 7569 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7570
7571 /* TYPE_CU may not be the same as CU.
7572 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7573 return set_die_type (die, type, cu);
7574 }
7575
c906108c
SS
7576 type = alloc_type (objfile);
7577
7578 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7579 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7580 if (name != NULL)
0114d602 7581 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7582
e142c38c 7583 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7584 if (attr)
7585 {
7586 TYPE_LENGTH (type) = DW_UNSND (attr);
7587 }
7588 else
7589 {
7590 TYPE_LENGTH (type) = 0;
7591 }
7592
137033e9
JB
7593 /* The enumeration DIE can be incomplete. In Ada, any type can be
7594 declared as private in the package spec, and then defined only
7595 inside the package body. Such types are known as Taft Amendment
7596 Types. When another package uses such a type, an incomplete DIE
7597 may be generated by the compiler. */
02eb380e 7598 if (die_is_declaration (die, cu))
876cecd0 7599 TYPE_STUB (type) = 1;
02eb380e 7600
f792889a 7601 return set_die_type (die, type, cu);
134d01f1
DJ
7602}
7603
7604/* Given a pointer to a die which begins an enumeration, process all
7605 the dies that define the members of the enumeration, and create the
7606 symbol for the enumeration type.
7607
7608 NOTE: We reverse the order of the element list. */
7609
7610static void
7611process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7612{
f792889a 7613 struct type *this_type;
134d01f1 7614
f792889a
DJ
7615 this_type = get_die_type (die, cu);
7616 if (this_type == NULL)
7617 this_type = read_enumeration_type (die, cu);
9dc481d3 7618
639d11d3 7619 if (die->child != NULL)
c906108c 7620 {
9dc481d3
DE
7621 struct die_info *child_die;
7622 struct symbol *sym;
7623 struct field *fields = NULL;
7624 int num_fields = 0;
7625 int unsigned_enum = 1;
7626 char *name;
7627
639d11d3 7628 child_die = die->child;
c906108c
SS
7629 while (child_die && child_die->tag)
7630 {
7631 if (child_die->tag != DW_TAG_enumerator)
7632 {
e7c27a73 7633 process_die (child_die, cu);
c906108c
SS
7634 }
7635 else
7636 {
39cbfefa
DJ
7637 name = dwarf2_name (child_die, cu);
7638 if (name)
c906108c 7639 {
f792889a 7640 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
7641 if (SYMBOL_VALUE (sym) < 0)
7642 unsigned_enum = 0;
7643
7644 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7645 {
7646 fields = (struct field *)
7647 xrealloc (fields,
7648 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7649 * sizeof (struct field));
c906108c
SS
7650 }
7651
3567439c 7652 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 7653 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 7654 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
7655 FIELD_BITSIZE (fields[num_fields]) = 0;
7656
7657 num_fields++;
7658 }
7659 }
7660
7661 child_die = sibling_die (child_die);
7662 }
7663
7664 if (num_fields)
7665 {
f792889a
DJ
7666 TYPE_NFIELDS (this_type) = num_fields;
7667 TYPE_FIELDS (this_type) = (struct field *)
7668 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7669 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 7670 sizeof (struct field) * num_fields);
b8c9b27d 7671 xfree (fields);
c906108c
SS
7672 }
7673 if (unsigned_enum)
876cecd0 7674 TYPE_UNSIGNED (this_type) = 1;
c906108c 7675 }
134d01f1 7676
f792889a 7677 new_symbol (die, this_type, cu);
c906108c
SS
7678}
7679
7680/* Extract all information from a DW_TAG_array_type DIE and put it in
7681 the DIE's type field. For now, this only handles one dimensional
7682 arrays. */
7683
f792889a 7684static struct type *
e7c27a73 7685read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7686{
e7c27a73 7687 struct objfile *objfile = cu->objfile;
c906108c 7688 struct die_info *child_die;
7e314c57 7689 struct type *type;
c906108c
SS
7690 struct type *element_type, *range_type, *index_type;
7691 struct type **range_types = NULL;
7692 struct attribute *attr;
7693 int ndim = 0;
7694 struct cleanup *back_to;
39cbfefa 7695 char *name;
c906108c 7696
e7c27a73 7697 element_type = die_type (die, cu);
c906108c 7698
7e314c57
JK
7699 /* The die_type call above may have already set the type for this DIE. */
7700 type = get_die_type (die, cu);
7701 if (type)
7702 return type;
7703
c906108c
SS
7704 /* Irix 6.2 native cc creates array types without children for
7705 arrays with unspecified length. */
639d11d3 7706 if (die->child == NULL)
c906108c 7707 {
46bf5051 7708 index_type = objfile_type (objfile)->builtin_int;
c906108c 7709 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
7710 type = create_array_type (NULL, element_type, range_type);
7711 return set_die_type (die, type, cu);
c906108c
SS
7712 }
7713
7714 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 7715 child_die = die->child;
c906108c
SS
7716 while (child_die && child_die->tag)
7717 {
7718 if (child_die->tag == DW_TAG_subrange_type)
7719 {
f792889a 7720 struct type *child_type = read_type_die (child_die, cu);
9a619af0 7721
f792889a 7722 if (child_type != NULL)
a02abb62 7723 {
0963b4bd
MS
7724 /* The range type was succesfully read. Save it for the
7725 array type creation. */
a02abb62
JB
7726 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7727 {
7728 range_types = (struct type **)
7729 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7730 * sizeof (struct type *));
7731 if (ndim == 0)
7732 make_cleanup (free_current_contents, &range_types);
7733 }
f792889a 7734 range_types[ndim++] = child_type;
a02abb62 7735 }
c906108c
SS
7736 }
7737 child_die = sibling_die (child_die);
7738 }
7739
7740 /* Dwarf2 dimensions are output from left to right, create the
7741 necessary array types in backwards order. */
7ca2d3a3 7742
c906108c 7743 type = element_type;
7ca2d3a3
DL
7744
7745 if (read_array_order (die, cu) == DW_ORD_col_major)
7746 {
7747 int i = 0;
9a619af0 7748
7ca2d3a3
DL
7749 while (i < ndim)
7750 type = create_array_type (NULL, type, range_types[i++]);
7751 }
7752 else
7753 {
7754 while (ndim-- > 0)
7755 type = create_array_type (NULL, type, range_types[ndim]);
7756 }
c906108c 7757
f5f8a009
EZ
7758 /* Understand Dwarf2 support for vector types (like they occur on
7759 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7760 array type. This is not part of the Dwarf2/3 standard yet, but a
7761 custom vendor extension. The main difference between a regular
7762 array and the vector variant is that vectors are passed by value
7763 to functions. */
e142c38c 7764 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 7765 if (attr)
ea37ba09 7766 make_vector_type (type);
f5f8a009 7767
dbc98a8b
KW
7768 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7769 implementation may choose to implement triple vectors using this
7770 attribute. */
7771 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7772 if (attr)
7773 {
7774 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7775 TYPE_LENGTH (type) = DW_UNSND (attr);
7776 else
3e43a32a
MS
7777 complaint (&symfile_complaints,
7778 _("DW_AT_byte_size for array type smaller "
7779 "than the total size of elements"));
dbc98a8b
KW
7780 }
7781
39cbfefa
DJ
7782 name = dwarf2_name (die, cu);
7783 if (name)
7784 TYPE_NAME (type) = name;
6e70227d 7785
0963b4bd 7786 /* Install the type in the die. */
7e314c57
JK
7787 set_die_type (die, type, cu);
7788
7789 /* set_die_type should be already done. */
b4ba55a1
JB
7790 set_descriptive_type (type, die, cu);
7791
c906108c
SS
7792 do_cleanups (back_to);
7793
7e314c57 7794 return type;
c906108c
SS
7795}
7796
7ca2d3a3 7797static enum dwarf_array_dim_ordering
6e70227d 7798read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
7799{
7800 struct attribute *attr;
7801
7802 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7803
7804 if (attr) return DW_SND (attr);
7805
0963b4bd
MS
7806 /* GNU F77 is a special case, as at 08/2004 array type info is the
7807 opposite order to the dwarf2 specification, but data is still
7808 laid out as per normal fortran.
7ca2d3a3 7809
0963b4bd
MS
7810 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7811 version checking. */
7ca2d3a3 7812
905e0470
PM
7813 if (cu->language == language_fortran
7814 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
7815 {
7816 return DW_ORD_row_major;
7817 }
7818
6e70227d 7819 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
7820 {
7821 case array_column_major:
7822 return DW_ORD_col_major;
7823 case array_row_major:
7824 default:
7825 return DW_ORD_row_major;
7826 };
7827}
7828
72019c9c 7829/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 7830 the DIE's type field. */
72019c9c 7831
f792889a 7832static struct type *
72019c9c
GM
7833read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7834{
7e314c57
JK
7835 struct type *domain_type, *set_type;
7836 struct attribute *attr;
f792889a 7837
7e314c57
JK
7838 domain_type = die_type (die, cu);
7839
7840 /* The die_type call above may have already set the type for this DIE. */
7841 set_type = get_die_type (die, cu);
7842 if (set_type)
7843 return set_type;
7844
7845 set_type = create_set_type (NULL, domain_type);
7846
7847 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
7848 if (attr)
7849 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 7850
f792889a 7851 return set_die_type (die, set_type, cu);
72019c9c 7852}
7ca2d3a3 7853
c906108c
SS
7854/* First cut: install each common block member as a global variable. */
7855
7856static void
e7c27a73 7857read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7858{
7859 struct die_info *child_die;
7860 struct attribute *attr;
7861 struct symbol *sym;
7862 CORE_ADDR base = (CORE_ADDR) 0;
7863
e142c38c 7864 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7865 if (attr)
7866 {
0963b4bd 7867 /* Support the .debug_loc offsets. */
8e19ed76
PS
7868 if (attr_form_is_block (attr))
7869 {
e7c27a73 7870 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 7871 }
3690dd37 7872 else if (attr_form_is_section_offset (attr))
8e19ed76 7873 {
4d3c2250 7874 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7875 }
7876 else
7877 {
4d3c2250
KB
7878 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7879 "common block member");
8e19ed76 7880 }
c906108c 7881 }
639d11d3 7882 if (die->child != NULL)
c906108c 7883 {
639d11d3 7884 child_die = die->child;
c906108c
SS
7885 while (child_die && child_die->tag)
7886 {
74ac6d43
TT
7887 LONGEST offset;
7888
e7c27a73 7889 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
7890 if (sym != NULL
7891 && handle_data_member_location (child_die, cu, &offset))
c906108c 7892 {
74ac6d43 7893 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
7894 add_symbol_to_list (sym, &global_symbols);
7895 }
7896 child_die = sibling_die (child_die);
7897 }
7898 }
7899}
7900
0114d602 7901/* Create a type for a C++ namespace. */
d9fa45fe 7902
0114d602
DJ
7903static struct type *
7904read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 7905{
e7c27a73 7906 struct objfile *objfile = cu->objfile;
0114d602 7907 const char *previous_prefix, *name;
9219021c 7908 int is_anonymous;
0114d602
DJ
7909 struct type *type;
7910
7911 /* For extensions, reuse the type of the original namespace. */
7912 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7913 {
7914 struct die_info *ext_die;
7915 struct dwarf2_cu *ext_cu = cu;
9a619af0 7916
0114d602
DJ
7917 ext_die = dwarf2_extension (die, &ext_cu);
7918 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
7919
7920 /* EXT_CU may not be the same as CU.
7921 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
7922 return set_die_type (die, type, cu);
7923 }
9219021c 7924
e142c38c 7925 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
7926
7927 /* Now build the name of the current namespace. */
7928
0114d602
DJ
7929 previous_prefix = determine_prefix (die, cu);
7930 if (previous_prefix[0] != '\0')
7931 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 7932 previous_prefix, name, 0, cu);
0114d602
DJ
7933
7934 /* Create the type. */
7935 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7936 objfile);
7937 TYPE_NAME (type) = (char *) name;
7938 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7939
60531b24 7940 return set_die_type (die, type, cu);
0114d602
DJ
7941}
7942
7943/* Read a C++ namespace. */
7944
7945static void
7946read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7947{
7948 struct objfile *objfile = cu->objfile;
0114d602 7949 int is_anonymous;
9219021c 7950
5c4e30ca
DC
7951 /* Add a symbol associated to this if we haven't seen the namespace
7952 before. Also, add a using directive if it's an anonymous
7953 namespace. */
9219021c 7954
f2f0e013 7955 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
7956 {
7957 struct type *type;
7958
0114d602 7959 type = read_type_die (die, cu);
e7c27a73 7960 new_symbol (die, type, cu);
5c4e30ca 7961
e8e80198 7962 namespace_name (die, &is_anonymous, cu);
5c4e30ca 7963 if (is_anonymous)
0114d602
DJ
7964 {
7965 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 7966
c0cc3a76 7967 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 7968 NULL, NULL, &objfile->objfile_obstack);
0114d602 7969 }
5c4e30ca 7970 }
9219021c 7971
639d11d3 7972 if (die->child != NULL)
d9fa45fe 7973 {
639d11d3 7974 struct die_info *child_die = die->child;
6e70227d 7975
d9fa45fe
DC
7976 while (child_die && child_die->tag)
7977 {
e7c27a73 7978 process_die (child_die, cu);
d9fa45fe
DC
7979 child_die = sibling_die (child_die);
7980 }
7981 }
38d518c9
EZ
7982}
7983
f55ee35c
JK
7984/* Read a Fortran module as type. This DIE can be only a declaration used for
7985 imported module. Still we need that type as local Fortran "use ... only"
7986 declaration imports depend on the created type in determine_prefix. */
7987
7988static struct type *
7989read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7990{
7991 struct objfile *objfile = cu->objfile;
7992 char *module_name;
7993 struct type *type;
7994
7995 module_name = dwarf2_name (die, cu);
7996 if (!module_name)
3e43a32a
MS
7997 complaint (&symfile_complaints,
7998 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
7999 die->offset);
8000 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8001
8002 /* determine_prefix uses TYPE_TAG_NAME. */
8003 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8004
8005 return set_die_type (die, type, cu);
8006}
8007
5d7cb8df
JK
8008/* Read a Fortran module. */
8009
8010static void
8011read_module (struct die_info *die, struct dwarf2_cu *cu)
8012{
8013 struct die_info *child_die = die->child;
8014
5d7cb8df
JK
8015 while (child_die && child_die->tag)
8016 {
8017 process_die (child_die, cu);
8018 child_die = sibling_die (child_die);
8019 }
8020}
8021
38d518c9
EZ
8022/* Return the name of the namespace represented by DIE. Set
8023 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8024 namespace. */
8025
8026static const char *
e142c38c 8027namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8028{
8029 struct die_info *current_die;
8030 const char *name = NULL;
8031
8032 /* Loop through the extensions until we find a name. */
8033
8034 for (current_die = die;
8035 current_die != NULL;
f2f0e013 8036 current_die = dwarf2_extension (die, &cu))
38d518c9 8037 {
e142c38c 8038 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8039 if (name != NULL)
8040 break;
8041 }
8042
8043 /* Is it an anonymous namespace? */
8044
8045 *is_anonymous = (name == NULL);
8046 if (*is_anonymous)
2b1dbab0 8047 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8048
8049 return name;
d9fa45fe
DC
8050}
8051
c906108c
SS
8052/* Extract all information from a DW_TAG_pointer_type DIE and add to
8053 the user defined type vector. */
8054
f792889a 8055static struct type *
e7c27a73 8056read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8057{
5e2b427d 8058 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8059 struct comp_unit_head *cu_header = &cu->header;
c906108c 8060 struct type *type;
8b2dbe47
KB
8061 struct attribute *attr_byte_size;
8062 struct attribute *attr_address_class;
8063 int byte_size, addr_class;
7e314c57
JK
8064 struct type *target_type;
8065
8066 target_type = die_type (die, cu);
c906108c 8067
7e314c57
JK
8068 /* The die_type call above may have already set the type for this DIE. */
8069 type = get_die_type (die, cu);
8070 if (type)
8071 return type;
8072
8073 type = lookup_pointer_type (target_type);
8b2dbe47 8074
e142c38c 8075 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8076 if (attr_byte_size)
8077 byte_size = DW_UNSND (attr_byte_size);
c906108c 8078 else
8b2dbe47
KB
8079 byte_size = cu_header->addr_size;
8080
e142c38c 8081 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8082 if (attr_address_class)
8083 addr_class = DW_UNSND (attr_address_class);
8084 else
8085 addr_class = DW_ADDR_none;
8086
8087 /* If the pointer size or address class is different than the
8088 default, create a type variant marked as such and set the
8089 length accordingly. */
8090 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8091 {
5e2b427d 8092 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8093 {
8094 int type_flags;
8095
849957d9 8096 type_flags = gdbarch_address_class_type_flags
5e2b427d 8097 (gdbarch, byte_size, addr_class);
876cecd0
TT
8098 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8099 == 0);
8b2dbe47
KB
8100 type = make_type_with_address_space (type, type_flags);
8101 }
8102 else if (TYPE_LENGTH (type) != byte_size)
8103 {
3e43a32a
MS
8104 complaint (&symfile_complaints,
8105 _("invalid pointer size %d"), byte_size);
8b2dbe47 8106 }
6e70227d 8107 else
9a619af0
MS
8108 {
8109 /* Should we also complain about unhandled address classes? */
8110 }
c906108c 8111 }
8b2dbe47
KB
8112
8113 TYPE_LENGTH (type) = byte_size;
f792889a 8114 return set_die_type (die, type, cu);
c906108c
SS
8115}
8116
8117/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8118 the user defined type vector. */
8119
f792889a 8120static struct type *
e7c27a73 8121read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8122{
8123 struct type *type;
8124 struct type *to_type;
8125 struct type *domain;
8126
e7c27a73
DJ
8127 to_type = die_type (die, cu);
8128 domain = die_containing_type (die, cu);
0d5de010 8129
7e314c57
JK
8130 /* The calls above may have already set the type for this DIE. */
8131 type = get_die_type (die, cu);
8132 if (type)
8133 return type;
8134
0d5de010
DJ
8135 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8136 type = lookup_methodptr_type (to_type);
8137 else
8138 type = lookup_memberptr_type (to_type, domain);
c906108c 8139
f792889a 8140 return set_die_type (die, type, cu);
c906108c
SS
8141}
8142
8143/* Extract all information from a DW_TAG_reference_type DIE and add to
8144 the user defined type vector. */
8145
f792889a 8146static struct type *
e7c27a73 8147read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8148{
e7c27a73 8149 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8150 struct type *type, *target_type;
c906108c
SS
8151 struct attribute *attr;
8152
7e314c57
JK
8153 target_type = die_type (die, cu);
8154
8155 /* The die_type call above may have already set the type for this DIE. */
8156 type = get_die_type (die, cu);
8157 if (type)
8158 return type;
8159
8160 type = lookup_reference_type (target_type);
e142c38c 8161 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8162 if (attr)
8163 {
8164 TYPE_LENGTH (type) = DW_UNSND (attr);
8165 }
8166 else
8167 {
107d2387 8168 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8169 }
f792889a 8170 return set_die_type (die, type, cu);
c906108c
SS
8171}
8172
f792889a 8173static struct type *
e7c27a73 8174read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8175{
f792889a 8176 struct type *base_type, *cv_type;
c906108c 8177
e7c27a73 8178 base_type = die_type (die, cu);
7e314c57
JK
8179
8180 /* The die_type call above may have already set the type for this DIE. */
8181 cv_type = get_die_type (die, cu);
8182 if (cv_type)
8183 return cv_type;
8184
2f608a3a
KW
8185 /* In case the const qualifier is applied to an array type, the element type
8186 is so qualified, not the array type (section 6.7.3 of C99). */
8187 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8188 {
8189 struct type *el_type, *inner_array;
8190
8191 base_type = copy_type (base_type);
8192 inner_array = base_type;
8193
8194 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8195 {
8196 TYPE_TARGET_TYPE (inner_array) =
8197 copy_type (TYPE_TARGET_TYPE (inner_array));
8198 inner_array = TYPE_TARGET_TYPE (inner_array);
8199 }
8200
8201 el_type = TYPE_TARGET_TYPE (inner_array);
8202 TYPE_TARGET_TYPE (inner_array) =
8203 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8204
8205 return set_die_type (die, base_type, cu);
8206 }
8207
f792889a
DJ
8208 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8209 return set_die_type (die, cv_type, cu);
c906108c
SS
8210}
8211
f792889a 8212static struct type *
e7c27a73 8213read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8214{
f792889a 8215 struct type *base_type, *cv_type;
c906108c 8216
e7c27a73 8217 base_type = die_type (die, cu);
7e314c57
JK
8218
8219 /* The die_type call above may have already set the type for this DIE. */
8220 cv_type = get_die_type (die, cu);
8221 if (cv_type)
8222 return cv_type;
8223
f792889a
DJ
8224 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8225 return set_die_type (die, cv_type, cu);
c906108c
SS
8226}
8227
8228/* Extract all information from a DW_TAG_string_type DIE and add to
8229 the user defined type vector. It isn't really a user defined type,
8230 but it behaves like one, with other DIE's using an AT_user_def_type
8231 attribute to reference it. */
8232
f792889a 8233static struct type *
e7c27a73 8234read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8235{
e7c27a73 8236 struct objfile *objfile = cu->objfile;
3b7538c0 8237 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8238 struct type *type, *range_type, *index_type, *char_type;
8239 struct attribute *attr;
8240 unsigned int length;
8241
e142c38c 8242 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8243 if (attr)
8244 {
8245 length = DW_UNSND (attr);
8246 }
8247 else
8248 {
0963b4bd 8249 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8250 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8251 if (attr)
8252 {
8253 length = DW_UNSND (attr);
8254 }
8255 else
8256 {
8257 length = 1;
8258 }
c906108c 8259 }
6ccb9162 8260
46bf5051 8261 index_type = objfile_type (objfile)->builtin_int;
c906108c 8262 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8263 char_type = language_string_char_type (cu->language_defn, gdbarch);
8264 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8265
f792889a 8266 return set_die_type (die, type, cu);
c906108c
SS
8267}
8268
8269/* Handle DIES due to C code like:
8270
8271 struct foo
c5aa993b
JM
8272 {
8273 int (*funcp)(int a, long l);
8274 int b;
8275 };
c906108c 8276
0963b4bd 8277 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8278
f792889a 8279static struct type *
e7c27a73 8280read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8281{
0963b4bd
MS
8282 struct type *type; /* Type that this function returns. */
8283 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8284 struct attribute *attr;
8285
e7c27a73 8286 type = die_type (die, cu);
7e314c57
JK
8287
8288 /* The die_type call above may have already set the type for this DIE. */
8289 ftype = get_die_type (die, cu);
8290 if (ftype)
8291 return ftype;
8292
0c8b41f1 8293 ftype = lookup_function_type (type);
c906108c 8294
5b8101ae 8295 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8296 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8297 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8298 || cu->language == language_cplus
5b8101ae
PM
8299 || cu->language == language_java
8300 || cu->language == language_pascal)
876cecd0 8301 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8302 else if (producer_is_realview (cu->producer))
8303 /* RealView does not emit DW_AT_prototyped. We can not
8304 distinguish prototyped and unprototyped functions; default to
8305 prototyped, since that is more common in modern code (and
8306 RealView warns about unprototyped functions). */
8307 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8308
c055b101
CV
8309 /* Store the calling convention in the type if it's available in
8310 the subroutine die. Otherwise set the calling convention to
8311 the default value DW_CC_normal. */
8312 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8313 if (attr)
8314 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8315 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8316 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8317 else
8318 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8319
8320 /* We need to add the subroutine type to the die immediately so
8321 we don't infinitely recurse when dealing with parameters
0963b4bd 8322 declared as the same subroutine type. */
76c10ea2 8323 set_die_type (die, ftype, cu);
6e70227d 8324
639d11d3 8325 if (die->child != NULL)
c906108c 8326 {
8072405b 8327 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8328 struct die_info *child_die;
8072405b 8329 int nparams, iparams;
c906108c
SS
8330
8331 /* Count the number of parameters.
8332 FIXME: GDB currently ignores vararg functions, but knows about
8333 vararg member functions. */
8072405b 8334 nparams = 0;
639d11d3 8335 child_die = die->child;
c906108c
SS
8336 while (child_die && child_die->tag)
8337 {
8338 if (child_die->tag == DW_TAG_formal_parameter)
8339 nparams++;
8340 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8341 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8342 child_die = sibling_die (child_die);
8343 }
8344
8345 /* Allocate storage for parameters and fill them in. */
8346 TYPE_NFIELDS (ftype) = nparams;
8347 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8348 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8349
8072405b
JK
8350 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8351 even if we error out during the parameters reading below. */
8352 for (iparams = 0; iparams < nparams; iparams++)
8353 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8354
8355 iparams = 0;
639d11d3 8356 child_die = die->child;
c906108c
SS
8357 while (child_die && child_die->tag)
8358 {
8359 if (child_die->tag == DW_TAG_formal_parameter)
8360 {
3ce3b1ba
PA
8361 struct type *arg_type;
8362
8363 /* DWARF version 2 has no clean way to discern C++
8364 static and non-static member functions. G++ helps
8365 GDB by marking the first parameter for non-static
8366 member functions (which is the this pointer) as
8367 artificial. We pass this information to
8368 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8369
8370 DWARF version 3 added DW_AT_object_pointer, which GCC
8371 4.5 does not yet generate. */
e142c38c 8372 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8373 if (attr)
8374 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8375 else
418835cc
KS
8376 {
8377 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8378
8379 /* GCC/43521: In java, the formal parameter
8380 "this" is sometimes not marked with DW_AT_artificial. */
8381 if (cu->language == language_java)
8382 {
8383 const char *name = dwarf2_name (child_die, cu);
9a619af0 8384
418835cc
KS
8385 if (name && !strcmp (name, "this"))
8386 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8387 }
8388 }
3ce3b1ba
PA
8389 arg_type = die_type (child_die, cu);
8390
8391 /* RealView does not mark THIS as const, which the testsuite
8392 expects. GCC marks THIS as const in method definitions,
8393 but not in the class specifications (GCC PR 43053). */
8394 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8395 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8396 {
8397 int is_this = 0;
8398 struct dwarf2_cu *arg_cu = cu;
8399 const char *name = dwarf2_name (child_die, cu);
8400
8401 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8402 if (attr)
8403 {
8404 /* If the compiler emits this, use it. */
8405 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8406 is_this = 1;
8407 }
8408 else if (name && strcmp (name, "this") == 0)
8409 /* Function definitions will have the argument names. */
8410 is_this = 1;
8411 else if (name == NULL && iparams == 0)
8412 /* Declarations may not have the names, so like
8413 elsewhere in GDB, assume an artificial first
8414 argument is "this". */
8415 is_this = 1;
8416
8417 if (is_this)
8418 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8419 arg_type, 0);
8420 }
8421
8422 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8423 iparams++;
8424 }
8425 child_die = sibling_die (child_die);
8426 }
8427 }
8428
76c10ea2 8429 return ftype;
c906108c
SS
8430}
8431
f792889a 8432static struct type *
e7c27a73 8433read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8434{
e7c27a73 8435 struct objfile *objfile = cu->objfile;
0114d602 8436 const char *name = NULL;
f792889a 8437 struct type *this_type;
c906108c 8438
94af9270 8439 name = dwarf2_full_name (NULL, die, cu);
f792889a 8440 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8441 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8442 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8443 set_die_type (die, this_type, cu);
8444 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8445 return this_type;
c906108c
SS
8446}
8447
8448/* Find a representation of a given base type and install
8449 it in the TYPE field of the die. */
8450
f792889a 8451static struct type *
e7c27a73 8452read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8453{
e7c27a73 8454 struct objfile *objfile = cu->objfile;
c906108c
SS
8455 struct type *type;
8456 struct attribute *attr;
8457 int encoding = 0, size = 0;
39cbfefa 8458 char *name;
6ccb9162
UW
8459 enum type_code code = TYPE_CODE_INT;
8460 int type_flags = 0;
8461 struct type *target_type = NULL;
c906108c 8462
e142c38c 8463 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8464 if (attr)
8465 {
8466 encoding = DW_UNSND (attr);
8467 }
e142c38c 8468 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8469 if (attr)
8470 {
8471 size = DW_UNSND (attr);
8472 }
39cbfefa 8473 name = dwarf2_name (die, cu);
6ccb9162 8474 if (!name)
c906108c 8475 {
6ccb9162
UW
8476 complaint (&symfile_complaints,
8477 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8478 }
6ccb9162
UW
8479
8480 switch (encoding)
c906108c 8481 {
6ccb9162
UW
8482 case DW_ATE_address:
8483 /* Turn DW_ATE_address into a void * pointer. */
8484 code = TYPE_CODE_PTR;
8485 type_flags |= TYPE_FLAG_UNSIGNED;
8486 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8487 break;
8488 case DW_ATE_boolean:
8489 code = TYPE_CODE_BOOL;
8490 type_flags |= TYPE_FLAG_UNSIGNED;
8491 break;
8492 case DW_ATE_complex_float:
8493 code = TYPE_CODE_COMPLEX;
8494 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8495 break;
8496 case DW_ATE_decimal_float:
8497 code = TYPE_CODE_DECFLOAT;
8498 break;
8499 case DW_ATE_float:
8500 code = TYPE_CODE_FLT;
8501 break;
8502 case DW_ATE_signed:
8503 break;
8504 case DW_ATE_unsigned:
8505 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8506 if (cu->language == language_fortran
8507 && name
8508 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8509 code = TYPE_CODE_CHAR;
6ccb9162
UW
8510 break;
8511 case DW_ATE_signed_char:
6e70227d 8512 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8513 || cu->language == language_pascal
8514 || cu->language == language_fortran)
6ccb9162
UW
8515 code = TYPE_CODE_CHAR;
8516 break;
8517 case DW_ATE_unsigned_char:
868a0084 8518 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8519 || cu->language == language_pascal
8520 || cu->language == language_fortran)
6ccb9162
UW
8521 code = TYPE_CODE_CHAR;
8522 type_flags |= TYPE_FLAG_UNSIGNED;
8523 break;
75079b2b
TT
8524 case DW_ATE_UTF:
8525 /* We just treat this as an integer and then recognize the
8526 type by name elsewhere. */
8527 break;
8528
6ccb9162
UW
8529 default:
8530 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8531 dwarf_type_encoding_name (encoding));
8532 break;
c906108c 8533 }
6ccb9162 8534
0114d602
DJ
8535 type = init_type (code, size, type_flags, NULL, objfile);
8536 TYPE_NAME (type) = name;
6ccb9162
UW
8537 TYPE_TARGET_TYPE (type) = target_type;
8538
0114d602 8539 if (name && strcmp (name, "char") == 0)
876cecd0 8540 TYPE_NOSIGN (type) = 1;
0114d602 8541
f792889a 8542 return set_die_type (die, type, cu);
c906108c
SS
8543}
8544
a02abb62
JB
8545/* Read the given DW_AT_subrange DIE. */
8546
f792889a 8547static struct type *
a02abb62
JB
8548read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8549{
8550 struct type *base_type;
8551 struct type *range_type;
8552 struct attribute *attr;
43bbcdc2
PH
8553 LONGEST low = 0;
8554 LONGEST high = -1;
39cbfefa 8555 char *name;
43bbcdc2 8556 LONGEST negative_mask;
e77813c8 8557
a02abb62 8558 base_type = die_type (die, cu);
953ac07e
JK
8559 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8560 check_typedef (base_type);
a02abb62 8561
7e314c57
JK
8562 /* The die_type call above may have already set the type for this DIE. */
8563 range_type = get_die_type (die, cu);
8564 if (range_type)
8565 return range_type;
8566
e142c38c 8567 if (cu->language == language_fortran)
6e70227d 8568 {
a02abb62
JB
8569 /* FORTRAN implies a lower bound of 1, if not given. */
8570 low = 1;
8571 }
8572
dd5e6932
DJ
8573 /* FIXME: For variable sized arrays either of these could be
8574 a variable rather than a constant value. We'll allow it,
8575 but we don't know how to handle it. */
e142c38c 8576 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8577 if (attr)
8578 low = dwarf2_get_attr_constant_value (attr, 0);
8579
e142c38c 8580 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8581 if (attr)
6e70227d 8582 {
d48323d8 8583 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
8584 {
8585 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8586 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8587 FIXME: GDB does not yet know how to handle dynamic
8588 arrays properly, treat them as arrays with unspecified
8589 length for now.
8590
8591 FIXME: jimb/2003-09-22: GDB does not really know
8592 how to handle arrays of unspecified length
8593 either; we just represent them as zero-length
8594 arrays. Choose an appropriate upper bound given
8595 the lower bound we've computed above. */
8596 high = low - 1;
8597 }
8598 else
8599 high = dwarf2_get_attr_constant_value (attr, 1);
8600 }
e77813c8
PM
8601 else
8602 {
8603 attr = dwarf2_attr (die, DW_AT_count, cu);
8604 if (attr)
8605 {
8606 int count = dwarf2_get_attr_constant_value (attr, 1);
8607 high = low + count - 1;
8608 }
c2ff108b
JK
8609 else
8610 {
8611 /* Unspecified array length. */
8612 high = low - 1;
8613 }
e77813c8
PM
8614 }
8615
8616 /* Dwarf-2 specifications explicitly allows to create subrange types
8617 without specifying a base type.
8618 In that case, the base type must be set to the type of
8619 the lower bound, upper bound or count, in that order, if any of these
8620 three attributes references an object that has a type.
8621 If no base type is found, the Dwarf-2 specifications say that
8622 a signed integer type of size equal to the size of an address should
8623 be used.
8624 For the following C code: `extern char gdb_int [];'
8625 GCC produces an empty range DIE.
8626 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 8627 high bound or count are not yet handled by this code. */
e77813c8
PM
8628 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8629 {
8630 struct objfile *objfile = cu->objfile;
8631 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8632 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8633 struct type *int_type = objfile_type (objfile)->builtin_int;
8634
8635 /* Test "int", "long int", and "long long int" objfile types,
8636 and select the first one having a size above or equal to the
8637 architecture address size. */
8638 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8639 base_type = int_type;
8640 else
8641 {
8642 int_type = objfile_type (objfile)->builtin_long;
8643 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8644 base_type = int_type;
8645 else
8646 {
8647 int_type = objfile_type (objfile)->builtin_long_long;
8648 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8649 base_type = int_type;
8650 }
8651 }
8652 }
a02abb62 8653
6e70227d 8654 negative_mask =
43bbcdc2
PH
8655 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8656 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8657 low |= negative_mask;
8658 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8659 high |= negative_mask;
8660
a02abb62
JB
8661 range_type = create_range_type (NULL, base_type, low, high);
8662
bbb0eef6
JK
8663 /* Mark arrays with dynamic length at least as an array of unspecified
8664 length. GDB could check the boundary but before it gets implemented at
8665 least allow accessing the array elements. */
d48323d8 8666 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
8667 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8668
c2ff108b
JK
8669 /* Ada expects an empty array on no boundary attributes. */
8670 if (attr == NULL && cu->language != language_ada)
8671 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8672
39cbfefa
DJ
8673 name = dwarf2_name (die, cu);
8674 if (name)
8675 TYPE_NAME (range_type) = name;
6e70227d 8676
e142c38c 8677 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
8678 if (attr)
8679 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8680
7e314c57
JK
8681 set_die_type (die, range_type, cu);
8682
8683 /* set_die_type should be already done. */
b4ba55a1
JB
8684 set_descriptive_type (range_type, die, cu);
8685
7e314c57 8686 return range_type;
a02abb62 8687}
6e70227d 8688
f792889a 8689static struct type *
81a17f79
JB
8690read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8691{
8692 struct type *type;
81a17f79 8693
81a17f79
JB
8694 /* For now, we only support the C meaning of an unspecified type: void. */
8695
0114d602
DJ
8696 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8697 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 8698
f792889a 8699 return set_die_type (die, type, cu);
81a17f79 8700}
a02abb62 8701
51545339
DJ
8702/* Trivial hash function for die_info: the hash value of a DIE
8703 is its offset in .debug_info for this objfile. */
8704
8705static hashval_t
8706die_hash (const void *item)
8707{
8708 const struct die_info *die = item;
9a619af0 8709
51545339
DJ
8710 return die->offset;
8711}
8712
8713/* Trivial comparison function for die_info structures: two DIEs
8714 are equal if they have the same offset. */
8715
8716static int
8717die_eq (const void *item_lhs, const void *item_rhs)
8718{
8719 const struct die_info *die_lhs = item_lhs;
8720 const struct die_info *die_rhs = item_rhs;
9a619af0 8721
51545339
DJ
8722 return die_lhs->offset == die_rhs->offset;
8723}
8724
c906108c
SS
8725/* Read a whole compilation unit into a linked list of dies. */
8726
f9aca02d 8727static struct die_info *
93311388 8728read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 8729{
93311388 8730 struct die_reader_specs reader_specs;
98bfdba5 8731 int read_abbrevs = 0;
1d9ec526 8732 struct cleanup *back_to = NULL;
98bfdba5
PA
8733 struct die_info *die;
8734
8735 if (cu->dwarf2_abbrevs == NULL)
8736 {
8737 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8738 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8739 read_abbrevs = 1;
8740 }
93311388 8741
348e048f 8742 gdb_assert (cu->die_hash == NULL);
51545339
DJ
8743 cu->die_hash
8744 = htab_create_alloc_ex (cu->header.length / 12,
8745 die_hash,
8746 die_eq,
8747 NULL,
8748 &cu->comp_unit_obstack,
8749 hashtab_obstack_allocate,
8750 dummy_obstack_deallocate);
8751
93311388
DE
8752 init_cu_die_reader (&reader_specs, cu);
8753
98bfdba5
PA
8754 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8755
8756 if (read_abbrevs)
8757 do_cleanups (back_to);
8758
8759 return die;
639d11d3
DC
8760}
8761
d97bc12b
DE
8762/* Main entry point for reading a DIE and all children.
8763 Read the DIE and dump it if requested. */
8764
8765static struct die_info *
93311388
DE
8766read_die_and_children (const struct die_reader_specs *reader,
8767 gdb_byte *info_ptr,
d97bc12b
DE
8768 gdb_byte **new_info_ptr,
8769 struct die_info *parent)
8770{
93311388 8771 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
8772 new_info_ptr, parent);
8773
8774 if (dwarf2_die_debug)
8775 {
348e048f
DE
8776 fprintf_unfiltered (gdb_stdlog,
8777 "\nRead die from %s of %s:\n",
8778 reader->buffer == dwarf2_per_objfile->info.buffer
8779 ? ".debug_info"
8780 : reader->buffer == dwarf2_per_objfile->types.buffer
8781 ? ".debug_types"
8782 : "unknown section",
8783 reader->abfd->filename);
d97bc12b
DE
8784 dump_die (result, dwarf2_die_debug);
8785 }
8786
8787 return result;
8788}
8789
639d11d3
DC
8790/* Read a single die and all its descendents. Set the die's sibling
8791 field to NULL; set other fields in the die correctly, and set all
8792 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8793 location of the info_ptr after reading all of those dies. PARENT
8794 is the parent of the die in question. */
8795
8796static struct die_info *
93311388
DE
8797read_die_and_children_1 (const struct die_reader_specs *reader,
8798 gdb_byte *info_ptr,
d97bc12b
DE
8799 gdb_byte **new_info_ptr,
8800 struct die_info *parent)
639d11d3
DC
8801{
8802 struct die_info *die;
fe1b8b76 8803 gdb_byte *cur_ptr;
639d11d3
DC
8804 int has_children;
8805
93311388 8806 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
8807 if (die == NULL)
8808 {
8809 *new_info_ptr = cur_ptr;
8810 return NULL;
8811 }
93311388 8812 store_in_ref_table (die, reader->cu);
639d11d3
DC
8813
8814 if (has_children)
348e048f 8815 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
8816 else
8817 {
8818 die->child = NULL;
8819 *new_info_ptr = cur_ptr;
8820 }
8821
8822 die->sibling = NULL;
8823 die->parent = parent;
8824 return die;
8825}
8826
8827/* Read a die, all of its descendents, and all of its siblings; set
8828 all of the fields of all of the dies correctly. Arguments are as
8829 in read_die_and_children. */
8830
8831static struct die_info *
93311388
DE
8832read_die_and_siblings (const struct die_reader_specs *reader,
8833 gdb_byte *info_ptr,
fe1b8b76 8834 gdb_byte **new_info_ptr,
639d11d3
DC
8835 struct die_info *parent)
8836{
8837 struct die_info *first_die, *last_sibling;
fe1b8b76 8838 gdb_byte *cur_ptr;
639d11d3 8839
c906108c 8840 cur_ptr = info_ptr;
639d11d3
DC
8841 first_die = last_sibling = NULL;
8842
8843 while (1)
c906108c 8844 {
639d11d3 8845 struct die_info *die
93311388 8846 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 8847
1d325ec1 8848 if (die == NULL)
c906108c 8849 {
639d11d3
DC
8850 *new_info_ptr = cur_ptr;
8851 return first_die;
c906108c 8852 }
1d325ec1
DJ
8853
8854 if (!first_die)
8855 first_die = die;
c906108c 8856 else
1d325ec1
DJ
8857 last_sibling->sibling = die;
8858
8859 last_sibling = die;
c906108c 8860 }
c906108c
SS
8861}
8862
93311388
DE
8863/* Read the die from the .debug_info section buffer. Set DIEP to
8864 point to a newly allocated die with its information, except for its
8865 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8866 whether the die has children or not. */
8867
8868static gdb_byte *
8869read_full_die (const struct die_reader_specs *reader,
8870 struct die_info **diep, gdb_byte *info_ptr,
8871 int *has_children)
8872{
8873 unsigned int abbrev_number, bytes_read, i, offset;
8874 struct abbrev_info *abbrev;
8875 struct die_info *die;
8876 struct dwarf2_cu *cu = reader->cu;
8877 bfd *abfd = reader->abfd;
8878
8879 offset = info_ptr - reader->buffer;
8880 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8881 info_ptr += bytes_read;
8882 if (!abbrev_number)
8883 {
8884 *diep = NULL;
8885 *has_children = 0;
8886 return info_ptr;
8887 }
8888
8889 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8890 if (!abbrev)
348e048f
DE
8891 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8892 abbrev_number,
8893 bfd_get_filename (abfd));
8894
93311388
DE
8895 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8896 die->offset = offset;
8897 die->tag = abbrev->tag;
8898 die->abbrev = abbrev_number;
8899
8900 die->num_attrs = abbrev->num_attrs;
8901
8902 for (i = 0; i < abbrev->num_attrs; ++i)
8903 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8904 abfd, info_ptr, cu);
8905
8906 *diep = die;
8907 *has_children = abbrev->has_children;
8908 return info_ptr;
8909}
8910
c906108c
SS
8911/* In DWARF version 2, the description of the debugging information is
8912 stored in a separate .debug_abbrev section. Before we read any
8913 dies from a section we read in all abbreviations and install them
72bf9492
DJ
8914 in a hash table. This function also sets flags in CU describing
8915 the data found in the abbrev table. */
c906108c
SS
8916
8917static void
e7c27a73 8918dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 8919{
e7c27a73 8920 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 8921 gdb_byte *abbrev_ptr;
c906108c
SS
8922 struct abbrev_info *cur_abbrev;
8923 unsigned int abbrev_number, bytes_read, abbrev_name;
8924 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
8925 struct attr_abbrev *cur_attrs;
8926 unsigned int allocated_attrs;
c906108c 8927
0963b4bd 8928 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
8929 obstack_init (&cu->abbrev_obstack);
8930 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8931 (ABBREV_HASH_SIZE
8932 * sizeof (struct abbrev_info *)));
8933 memset (cu->dwarf2_abbrevs, 0,
8934 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 8935
be391dca
TT
8936 dwarf2_read_section (dwarf2_per_objfile->objfile,
8937 &dwarf2_per_objfile->abbrev);
dce234bc 8938 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
8939 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8940 abbrev_ptr += bytes_read;
8941
f3dd6933
DJ
8942 allocated_attrs = ATTR_ALLOC_CHUNK;
8943 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 8944
0963b4bd 8945 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
8946 while (abbrev_number)
8947 {
f3dd6933 8948 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
8949
8950 /* read in abbrev header */
8951 cur_abbrev->number = abbrev_number;
8952 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8953 abbrev_ptr += bytes_read;
8954 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8955 abbrev_ptr += 1;
8956
72bf9492
DJ
8957 if (cur_abbrev->tag == DW_TAG_namespace)
8958 cu->has_namespace_info = 1;
8959
c906108c
SS
8960 /* now read in declarations */
8961 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8962 abbrev_ptr += bytes_read;
8963 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8964 abbrev_ptr += bytes_read;
8965 while (abbrev_name)
8966 {
f3dd6933 8967 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 8968 {
f3dd6933
DJ
8969 allocated_attrs += ATTR_ALLOC_CHUNK;
8970 cur_attrs
8971 = xrealloc (cur_attrs, (allocated_attrs
8972 * sizeof (struct attr_abbrev)));
c906108c 8973 }
ae038cb0
DJ
8974
8975 /* Record whether this compilation unit might have
8976 inter-compilation-unit references. If we don't know what form
8977 this attribute will have, then it might potentially be a
8978 DW_FORM_ref_addr, so we conservatively expect inter-CU
8979 references. */
8980
8981 if (abbrev_form == DW_FORM_ref_addr
8982 || abbrev_form == DW_FORM_indirect)
8983 cu->has_form_ref_addr = 1;
8984
f3dd6933
DJ
8985 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8986 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
8987 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8988 abbrev_ptr += bytes_read;
8989 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8990 abbrev_ptr += bytes_read;
8991 }
8992
f3dd6933
DJ
8993 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8994 (cur_abbrev->num_attrs
8995 * sizeof (struct attr_abbrev)));
8996 memcpy (cur_abbrev->attrs, cur_attrs,
8997 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8998
c906108c 8999 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9000 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9001 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9002
9003 /* Get next abbreviation.
9004 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9005 always properly terminated with an abbrev number of 0.
9006 Exit loop if we encounter an abbreviation which we have
9007 already read (which means we are about to read the abbreviations
9008 for the next compile unit) or if the end of the abbreviation
9009 table is reached. */
dce234bc
PP
9010 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9011 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9012 break;
9013 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9014 abbrev_ptr += bytes_read;
e7c27a73 9015 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9016 break;
9017 }
f3dd6933
DJ
9018
9019 xfree (cur_attrs);
c906108c
SS
9020}
9021
f3dd6933 9022/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9023
c906108c 9024static void
f3dd6933 9025dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9026{
f3dd6933 9027 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9028
f3dd6933
DJ
9029 obstack_free (&cu->abbrev_obstack, NULL);
9030 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9031}
9032
9033/* Lookup an abbrev_info structure in the abbrev hash table. */
9034
9035static struct abbrev_info *
e7c27a73 9036dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9037{
9038 unsigned int hash_number;
9039 struct abbrev_info *abbrev;
9040
9041 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9042 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9043
9044 while (abbrev)
9045 {
9046 if (abbrev->number == number)
9047 return abbrev;
9048 else
9049 abbrev = abbrev->next;
9050 }
9051 return NULL;
9052}
9053
72bf9492
DJ
9054/* Returns nonzero if TAG represents a type that we might generate a partial
9055 symbol for. */
9056
9057static int
9058is_type_tag_for_partial (int tag)
9059{
9060 switch (tag)
9061 {
9062#if 0
9063 /* Some types that would be reasonable to generate partial symbols for,
9064 that we don't at present. */
9065 case DW_TAG_array_type:
9066 case DW_TAG_file_type:
9067 case DW_TAG_ptr_to_member_type:
9068 case DW_TAG_set_type:
9069 case DW_TAG_string_type:
9070 case DW_TAG_subroutine_type:
9071#endif
9072 case DW_TAG_base_type:
9073 case DW_TAG_class_type:
680b30c7 9074 case DW_TAG_interface_type:
72bf9492
DJ
9075 case DW_TAG_enumeration_type:
9076 case DW_TAG_structure_type:
9077 case DW_TAG_subrange_type:
9078 case DW_TAG_typedef:
9079 case DW_TAG_union_type:
9080 return 1;
9081 default:
9082 return 0;
9083 }
9084}
9085
9086/* Load all DIEs that are interesting for partial symbols into memory. */
9087
9088static struct partial_die_info *
93311388
DE
9089load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9090 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
9091{
9092 struct partial_die_info *part_die;
9093 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9094 struct abbrev_info *abbrev;
9095 unsigned int bytes_read;
5afb4e99 9096 unsigned int load_all = 0;
72bf9492
DJ
9097
9098 int nesting_level = 1;
9099
9100 parent_die = NULL;
9101 last_die = NULL;
9102
5afb4e99
DJ
9103 if (cu->per_cu && cu->per_cu->load_all_dies)
9104 load_all = 1;
9105
72bf9492
DJ
9106 cu->partial_dies
9107 = htab_create_alloc_ex (cu->header.length / 12,
9108 partial_die_hash,
9109 partial_die_eq,
9110 NULL,
9111 &cu->comp_unit_obstack,
9112 hashtab_obstack_allocate,
9113 dummy_obstack_deallocate);
9114
9115 part_die = obstack_alloc (&cu->comp_unit_obstack,
9116 sizeof (struct partial_die_info));
9117
9118 while (1)
9119 {
9120 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9121
9122 /* A NULL abbrev means the end of a series of children. */
9123 if (abbrev == NULL)
9124 {
9125 if (--nesting_level == 0)
9126 {
9127 /* PART_DIE was probably the last thing allocated on the
9128 comp_unit_obstack, so we could call obstack_free
9129 here. We don't do that because the waste is small,
9130 and will be cleaned up when we're done with this
9131 compilation unit. This way, we're also more robust
9132 against other users of the comp_unit_obstack. */
9133 return first_die;
9134 }
9135 info_ptr += bytes_read;
9136 last_die = parent_die;
9137 parent_die = parent_die->die_parent;
9138 continue;
9139 }
9140
98bfdba5
PA
9141 /* Check for template arguments. We never save these; if
9142 they're seen, we just mark the parent, and go on our way. */
9143 if (parent_die != NULL
9144 && cu->language == language_cplus
9145 && (abbrev->tag == DW_TAG_template_type_param
9146 || abbrev->tag == DW_TAG_template_value_param))
9147 {
9148 parent_die->has_template_arguments = 1;
9149
9150 if (!load_all)
9151 {
9152 /* We don't need a partial DIE for the template argument. */
9153 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9154 cu);
9155 continue;
9156 }
9157 }
9158
9159 /* We only recurse into subprograms looking for template arguments.
9160 Skip their other children. */
9161 if (!load_all
9162 && cu->language == language_cplus
9163 && parent_die != NULL
9164 && parent_die->tag == DW_TAG_subprogram)
9165 {
9166 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9167 continue;
9168 }
9169
5afb4e99
DJ
9170 /* Check whether this DIE is interesting enough to save. Normally
9171 we would not be interested in members here, but there may be
9172 later variables referencing them via DW_AT_specification (for
9173 static members). */
9174 if (!load_all
9175 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9176 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9177 && abbrev->tag != DW_TAG_enumerator
9178 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9179 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9180 && abbrev->tag != DW_TAG_variable
5afb4e99 9181 && abbrev->tag != DW_TAG_namespace
f55ee35c 9182 && abbrev->tag != DW_TAG_module
5afb4e99 9183 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9184 {
9185 /* Otherwise we skip to the next sibling, if any. */
93311388 9186 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9187 continue;
9188 }
9189
93311388
DE
9190 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9191 buffer, info_ptr, cu);
72bf9492
DJ
9192
9193 /* This two-pass algorithm for processing partial symbols has a
9194 high cost in cache pressure. Thus, handle some simple cases
9195 here which cover the majority of C partial symbols. DIEs
9196 which neither have specification tags in them, nor could have
9197 specification tags elsewhere pointing at them, can simply be
9198 processed and discarded.
9199
9200 This segment is also optional; scan_partial_symbols and
9201 add_partial_symbol will handle these DIEs if we chain
9202 them in normally. When compilers which do not emit large
9203 quantities of duplicate debug information are more common,
9204 this code can probably be removed. */
9205
9206 /* Any complete simple types at the top level (pretty much all
9207 of them, for a language without namespaces), can be processed
9208 directly. */
9209 if (parent_die == NULL
9210 && part_die->has_specification == 0
9211 && part_die->is_declaration == 0
d8228535 9212 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9213 || part_die->tag == DW_TAG_base_type
9214 || part_die->tag == DW_TAG_subrange_type))
9215 {
9216 if (building_psymtab && part_die->name != NULL)
04a679b8 9217 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
9218 VAR_DOMAIN, LOC_TYPEDEF,
9219 &cu->objfile->static_psymbols,
9220 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 9221 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9222 continue;
9223 }
9224
d8228535
JK
9225 /* The exception for DW_TAG_typedef with has_children above is
9226 a workaround of GCC PR debug/47510. In the case of this complaint
9227 type_name_no_tag_or_error will error on such types later.
9228
9229 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9230 it could not find the child DIEs referenced later, this is checked
9231 above. In correct DWARF DW_TAG_typedef should have no children. */
9232
9233 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9234 complaint (&symfile_complaints,
9235 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9236 "- DIE at 0x%x [in module %s]"),
9237 part_die->offset, cu->objfile->name);
9238
72bf9492
DJ
9239 /* If we're at the second level, and we're an enumerator, and
9240 our parent has no specification (meaning possibly lives in a
9241 namespace elsewhere), then we can add the partial symbol now
9242 instead of queueing it. */
9243 if (part_die->tag == DW_TAG_enumerator
9244 && parent_die != NULL
9245 && parent_die->die_parent == NULL
9246 && parent_die->tag == DW_TAG_enumeration_type
9247 && parent_die->has_specification == 0)
9248 {
9249 if (part_die->name == NULL)
3e43a32a
MS
9250 complaint (&symfile_complaints,
9251 _("malformed enumerator DIE ignored"));
72bf9492 9252 else if (building_psymtab)
04a679b8 9253 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9254 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9255 (cu->language == language_cplus
9256 || cu->language == language_java)
72bf9492
DJ
9257 ? &cu->objfile->global_psymbols
9258 : &cu->objfile->static_psymbols,
9259 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9260
93311388 9261 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9262 continue;
9263 }
9264
9265 /* We'll save this DIE so link it in. */
9266 part_die->die_parent = parent_die;
9267 part_die->die_sibling = NULL;
9268 part_die->die_child = NULL;
9269
9270 if (last_die && last_die == parent_die)
9271 last_die->die_child = part_die;
9272 else if (last_die)
9273 last_die->die_sibling = part_die;
9274
9275 last_die = part_die;
9276
9277 if (first_die == NULL)
9278 first_die = part_die;
9279
9280 /* Maybe add the DIE to the hash table. Not all DIEs that we
9281 find interesting need to be in the hash table, because we
9282 also have the parent/sibling/child chains; only those that we
9283 might refer to by offset later during partial symbol reading.
9284
9285 For now this means things that might have be the target of a
9286 DW_AT_specification, DW_AT_abstract_origin, or
9287 DW_AT_extension. DW_AT_extension will refer only to
9288 namespaces; DW_AT_abstract_origin refers to functions (and
9289 many things under the function DIE, but we do not recurse
9290 into function DIEs during partial symbol reading) and
9291 possibly variables as well; DW_AT_specification refers to
9292 declarations. Declarations ought to have the DW_AT_declaration
9293 flag. It happens that GCC forgets to put it in sometimes, but
9294 only for functions, not for types.
9295
9296 Adding more things than necessary to the hash table is harmless
9297 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9298 wasted time in find_partial_die, when we reread the compilation
9299 unit with load_all_dies set. */
72bf9492 9300
5afb4e99 9301 if (load_all
72929c62 9302 || abbrev->tag == DW_TAG_constant
5afb4e99 9303 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9304 || abbrev->tag == DW_TAG_variable
9305 || abbrev->tag == DW_TAG_namespace
9306 || part_die->is_declaration)
9307 {
9308 void **slot;
9309
9310 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9311 part_die->offset, INSERT);
9312 *slot = part_die;
9313 }
9314
9315 part_die = obstack_alloc (&cu->comp_unit_obstack,
9316 sizeof (struct partial_die_info));
9317
9318 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9319 we have no reason to follow the children of structures; for other
98bfdba5
PA
9320 languages we have to, so that we can get at method physnames
9321 to infer fully qualified class names, for DW_AT_specification,
9322 and for C++ template arguments. For C++, we also look one level
9323 inside functions to find template arguments (if the name of the
9324 function does not already contain the template arguments).
bc30ff58
JB
9325
9326 For Ada, we need to scan the children of subprograms and lexical
9327 blocks as well because Ada allows the definition of nested
9328 entities that could be interesting for the debugger, such as
9329 nested subprograms for instance. */
72bf9492 9330 if (last_die->has_children
5afb4e99
DJ
9331 && (load_all
9332 || last_die->tag == DW_TAG_namespace
f55ee35c 9333 || last_die->tag == DW_TAG_module
72bf9492 9334 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9335 || (cu->language == language_cplus
9336 && last_die->tag == DW_TAG_subprogram
9337 && (last_die->name == NULL
9338 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9339 || (cu->language != language_c
9340 && (last_die->tag == DW_TAG_class_type
680b30c7 9341 || last_die->tag == DW_TAG_interface_type
72bf9492 9342 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9343 || last_die->tag == DW_TAG_union_type))
9344 || (cu->language == language_ada
9345 && (last_die->tag == DW_TAG_subprogram
9346 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9347 {
9348 nesting_level++;
9349 parent_die = last_die;
9350 continue;
9351 }
9352
9353 /* Otherwise we skip to the next sibling, if any. */
93311388 9354 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9355
9356 /* Back to the top, do it again. */
9357 }
9358}
9359
c906108c
SS
9360/* Read a minimal amount of information into the minimal die structure. */
9361
fe1b8b76 9362static gdb_byte *
72bf9492
DJ
9363read_partial_die (struct partial_die_info *part_die,
9364 struct abbrev_info *abbrev,
9365 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9366 gdb_byte *buffer, gdb_byte *info_ptr,
9367 struct dwarf2_cu *cu)
c906108c 9368{
fa238c03 9369 unsigned int i;
c906108c 9370 struct attribute attr;
c5aa993b 9371 int has_low_pc_attr = 0;
c906108c
SS
9372 int has_high_pc_attr = 0;
9373
72bf9492 9374 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9375
93311388 9376 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9377
9378 info_ptr += abbrev_len;
9379
9380 if (abbrev == NULL)
9381 return info_ptr;
9382
c906108c
SS
9383 part_die->tag = abbrev->tag;
9384 part_die->has_children = abbrev->has_children;
c906108c
SS
9385
9386 for (i = 0; i < abbrev->num_attrs; ++i)
9387 {
e7c27a73 9388 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9389
9390 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9391 partial symbol table. */
c906108c
SS
9392 switch (attr.name)
9393 {
9394 case DW_AT_name:
71c25dea
TT
9395 switch (part_die->tag)
9396 {
9397 case DW_TAG_compile_unit:
348e048f 9398 case DW_TAG_type_unit:
71c25dea
TT
9399 /* Compilation units have a DW_AT_name that is a filename, not
9400 a source language identifier. */
9401 case DW_TAG_enumeration_type:
9402 case DW_TAG_enumerator:
9403 /* These tags always have simple identifiers already; no need
9404 to canonicalize them. */
9405 part_die->name = DW_STRING (&attr);
9406 break;
9407 default:
9408 part_die->name
9409 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9410 &cu->objfile->objfile_obstack);
71c25dea
TT
9411 break;
9412 }
c906108c 9413 break;
31ef98ae 9414 case DW_AT_linkage_name:
c906108c 9415 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9416 /* Note that both forms of linkage name might appear. We
9417 assume they will be the same, and we only store the last
9418 one we see. */
94af9270
KS
9419 if (cu->language == language_ada)
9420 part_die->name = DW_STRING (&attr);
abc72ce4 9421 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9422 break;
9423 case DW_AT_low_pc:
9424 has_low_pc_attr = 1;
9425 part_die->lowpc = DW_ADDR (&attr);
9426 break;
9427 case DW_AT_high_pc:
9428 has_high_pc_attr = 1;
9429 part_die->highpc = DW_ADDR (&attr);
9430 break;
9431 case DW_AT_location:
0963b4bd 9432 /* Support the .debug_loc offsets. */
8e19ed76
PS
9433 if (attr_form_is_block (&attr))
9434 {
9435 part_die->locdesc = DW_BLOCK (&attr);
9436 }
3690dd37 9437 else if (attr_form_is_section_offset (&attr))
8e19ed76 9438 {
4d3c2250 9439 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9440 }
9441 else
9442 {
4d3c2250
KB
9443 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9444 "partial symbol information");
8e19ed76 9445 }
c906108c 9446 break;
c906108c
SS
9447 case DW_AT_external:
9448 part_die->is_external = DW_UNSND (&attr);
9449 break;
9450 case DW_AT_declaration:
9451 part_die->is_declaration = DW_UNSND (&attr);
9452 break;
9453 case DW_AT_type:
9454 part_die->has_type = 1;
9455 break;
9456 case DW_AT_abstract_origin:
9457 case DW_AT_specification:
72bf9492
DJ
9458 case DW_AT_extension:
9459 part_die->has_specification = 1;
c764a876 9460 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9461 break;
9462 case DW_AT_sibling:
9463 /* Ignore absolute siblings, they might point outside of
9464 the current compile unit. */
9465 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9466 complaint (&symfile_complaints,
9467 _("ignoring absolute DW_AT_sibling"));
c906108c 9468 else
93311388 9469 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9470 break;
fa4028e9
JB
9471 case DW_AT_byte_size:
9472 part_die->has_byte_size = 1;
9473 break;
68511cec
CES
9474 case DW_AT_calling_convention:
9475 /* DWARF doesn't provide a way to identify a program's source-level
9476 entry point. DW_AT_calling_convention attributes are only meant
9477 to describe functions' calling conventions.
9478
9479 However, because it's a necessary piece of information in
9480 Fortran, and because DW_CC_program is the only piece of debugging
9481 information whose definition refers to a 'main program' at all,
9482 several compilers have begun marking Fortran main programs with
9483 DW_CC_program --- even when those functions use the standard
9484 calling conventions.
9485
9486 So until DWARF specifies a way to provide this information and
9487 compilers pick up the new representation, we'll support this
9488 practice. */
9489 if (DW_UNSND (&attr) == DW_CC_program
9490 && cu->language == language_fortran)
01f8c46d
JK
9491 {
9492 set_main_name (part_die->name);
9493
9494 /* As this DIE has a static linkage the name would be difficult
9495 to look up later. */
9496 language_of_main = language_fortran;
9497 }
68511cec 9498 break;
c906108c
SS
9499 default:
9500 break;
9501 }
9502 }
9503
9373cf26
JK
9504 if (has_low_pc_attr && has_high_pc_attr)
9505 {
9506 /* When using the GNU linker, .gnu.linkonce. sections are used to
9507 eliminate duplicate copies of functions and vtables and such.
9508 The linker will arbitrarily choose one and discard the others.
9509 The AT_*_pc values for such functions refer to local labels in
9510 these sections. If the section from that file was discarded, the
9511 labels are not in the output, so the relocs get a value of 0.
9512 If this is a discarded function, mark the pc bounds as invalid,
9513 so that GDB will ignore it. */
9514 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9515 {
9516 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9517
9518 complaint (&symfile_complaints,
9519 _("DW_AT_low_pc %s is zero "
9520 "for DIE at 0x%x [in module %s]"),
9521 paddress (gdbarch, part_die->lowpc),
9522 part_die->offset, cu->objfile->name);
9523 }
9524 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9525 else if (part_die->lowpc >= part_die->highpc)
9526 {
9527 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9528
9529 complaint (&symfile_complaints,
9530 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9531 "for DIE at 0x%x [in module %s]"),
9532 paddress (gdbarch, part_die->lowpc),
9533 paddress (gdbarch, part_die->highpc),
9534 part_die->offset, cu->objfile->name);
9535 }
9536 else
9537 part_die->has_pc_info = 1;
9538 }
85cbf3d3 9539
c906108c
SS
9540 return info_ptr;
9541}
9542
72bf9492
DJ
9543/* Find a cached partial DIE at OFFSET in CU. */
9544
9545static struct partial_die_info *
c764a876 9546find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9547{
9548 struct partial_die_info *lookup_die = NULL;
9549 struct partial_die_info part_die;
9550
9551 part_die.offset = offset;
9552 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9553
72bf9492
DJ
9554 return lookup_die;
9555}
9556
348e048f
DE
9557/* Find a partial DIE at OFFSET, which may or may not be in CU,
9558 except in the case of .debug_types DIEs which do not reference
9559 outside their CU (they do however referencing other types via
55f1336d 9560 DW_FORM_ref_sig8). */
72bf9492
DJ
9561
9562static struct partial_die_info *
c764a876 9563find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9564{
5afb4e99
DJ
9565 struct dwarf2_per_cu_data *per_cu = NULL;
9566 struct partial_die_info *pd = NULL;
72bf9492 9567
348e048f
DE
9568 if (cu->per_cu->from_debug_types)
9569 {
9570 pd = find_partial_die_in_comp_unit (offset, cu);
9571 if (pd != NULL)
9572 return pd;
9573 goto not_found;
9574 }
9575
45452591 9576 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9577 {
9578 pd = find_partial_die_in_comp_unit (offset, cu);
9579 if (pd != NULL)
9580 return pd;
9581 }
72bf9492 9582
ae038cb0
DJ
9583 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9584
98bfdba5
PA
9585 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9586 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9587
9588 per_cu->cu->last_used = 0;
5afb4e99
DJ
9589 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9590
9591 if (pd == NULL && per_cu->load_all_dies == 0)
9592 {
9593 struct cleanup *back_to;
9594 struct partial_die_info comp_unit_die;
9595 struct abbrev_info *abbrev;
9596 unsigned int bytes_read;
9597 char *info_ptr;
9598
9599 per_cu->load_all_dies = 1;
9600
9601 /* Re-read the DIEs. */
9602 back_to = make_cleanup (null_cleanup, 0);
9603 if (per_cu->cu->dwarf2_abbrevs == NULL)
9604 {
9605 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9606 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9607 }
dce234bc 9608 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9609 + per_cu->cu->header.offset
9610 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
9611 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9612 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
9613 per_cu->cu->objfile->obfd,
9614 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
9615 per_cu->cu);
9616 if (comp_unit_die.has_children)
93311388
DE
9617 load_partial_dies (per_cu->cu->objfile->obfd,
9618 dwarf2_per_objfile->info.buffer, info_ptr,
9619 0, per_cu->cu);
5afb4e99
DJ
9620 do_cleanups (back_to);
9621
9622 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9623 }
9624
348e048f
DE
9625 not_found:
9626
5afb4e99
DJ
9627 if (pd == NULL)
9628 internal_error (__FILE__, __LINE__,
3e43a32a
MS
9629 _("could not find partial DIE 0x%x "
9630 "in cache [from module %s]\n"),
5afb4e99
DJ
9631 offset, bfd_get_filename (cu->objfile->obfd));
9632 return pd;
72bf9492
DJ
9633}
9634
abc72ce4
DE
9635/* See if we can figure out if the class lives in a namespace. We do
9636 this by looking for a member function; its demangled name will
9637 contain namespace info, if there is any. */
9638
9639static void
9640guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9641 struct dwarf2_cu *cu)
9642{
9643 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9644 what template types look like, because the demangler
9645 frequently doesn't give the same name as the debug info. We
9646 could fix this by only using the demangled name to get the
9647 prefix (but see comment in read_structure_type). */
9648
9649 struct partial_die_info *real_pdi;
9650 struct partial_die_info *child_pdi;
9651
9652 /* If this DIE (this DIE's specification, if any) has a parent, then
9653 we should not do this. We'll prepend the parent's fully qualified
9654 name when we create the partial symbol. */
9655
9656 real_pdi = struct_pdi;
9657 while (real_pdi->has_specification)
9658 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9659
9660 if (real_pdi->die_parent != NULL)
9661 return;
9662
9663 for (child_pdi = struct_pdi->die_child;
9664 child_pdi != NULL;
9665 child_pdi = child_pdi->die_sibling)
9666 {
9667 if (child_pdi->tag == DW_TAG_subprogram
9668 && child_pdi->linkage_name != NULL)
9669 {
9670 char *actual_class_name
9671 = language_class_name_from_physname (cu->language_defn,
9672 child_pdi->linkage_name);
9673 if (actual_class_name != NULL)
9674 {
9675 struct_pdi->name
9676 = obsavestring (actual_class_name,
9677 strlen (actual_class_name),
9678 &cu->objfile->objfile_obstack);
9679 xfree (actual_class_name);
9680 }
9681 break;
9682 }
9683 }
9684}
9685
72bf9492
DJ
9686/* Adjust PART_DIE before generating a symbol for it. This function
9687 may set the is_external flag or change the DIE's name. */
9688
9689static void
9690fixup_partial_die (struct partial_die_info *part_die,
9691 struct dwarf2_cu *cu)
9692{
abc72ce4
DE
9693 /* Once we've fixed up a die, there's no point in doing so again.
9694 This also avoids a memory leak if we were to call
9695 guess_partial_die_structure_name multiple times. */
9696 if (part_die->fixup_called)
9697 return;
9698
72bf9492
DJ
9699 /* If we found a reference attribute and the DIE has no name, try
9700 to find a name in the referred to DIE. */
9701
9702 if (part_die->name == NULL && part_die->has_specification)
9703 {
9704 struct partial_die_info *spec_die;
72bf9492 9705
10b3939b 9706 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 9707
10b3939b 9708 fixup_partial_die (spec_die, cu);
72bf9492
DJ
9709
9710 if (spec_die->name)
9711 {
9712 part_die->name = spec_die->name;
9713
9714 /* Copy DW_AT_external attribute if it is set. */
9715 if (spec_die->is_external)
9716 part_die->is_external = spec_die->is_external;
9717 }
9718 }
9719
9720 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
9721
9722 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 9723 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 9724
abc72ce4
DE
9725 /* If there is no parent die to provide a namespace, and there are
9726 children, see if we can determine the namespace from their linkage
9727 name.
9728 NOTE: We need to do this even if cu->has_namespace_info != 0.
9729 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9730 if (cu->language == language_cplus
9731 && dwarf2_per_objfile->types.asection != NULL
9732 && part_die->die_parent == NULL
9733 && part_die->has_children
9734 && (part_die->tag == DW_TAG_class_type
9735 || part_die->tag == DW_TAG_structure_type
9736 || part_die->tag == DW_TAG_union_type))
9737 guess_partial_die_structure_name (part_die, cu);
9738
53832f31
TT
9739 /* GCC might emit a nameless struct or union that has a linkage
9740 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9741 if (part_die->name == NULL
9742 && (part_die->tag == DW_TAG_structure_type
9743 || part_die->tag == DW_TAG_union_type
9744 || part_die->tag == DW_TAG_class_type)
9745 && part_die->linkage_name != NULL)
9746 {
9747 char *demangled;
9748
9749 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9750 if (demangled)
9751 {
9752 part_die->name = obsavestring (demangled, strlen (demangled),
9753 &cu->objfile->objfile_obstack);
9754 xfree (demangled);
9755 }
9756 }
9757
abc72ce4 9758 part_die->fixup_called = 1;
72bf9492
DJ
9759}
9760
a8329558 9761/* Read an attribute value described by an attribute form. */
c906108c 9762
fe1b8b76 9763static gdb_byte *
a8329558 9764read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 9765 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 9766 struct dwarf2_cu *cu)
c906108c 9767{
e7c27a73 9768 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9769 unsigned int bytes_read;
9770 struct dwarf_block *blk;
9771
a8329558
KW
9772 attr->form = form;
9773 switch (form)
c906108c 9774 {
c906108c 9775 case DW_FORM_ref_addr:
ae411497
TT
9776 if (cu->header.version == 2)
9777 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9778 else
3e43a32a
MS
9779 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9780 &cu->header, &bytes_read);
ae411497
TT
9781 info_ptr += bytes_read;
9782 break;
9783 case DW_FORM_addr:
e7c27a73 9784 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 9785 info_ptr += bytes_read;
c906108c
SS
9786 break;
9787 case DW_FORM_block2:
7b5a2f43 9788 blk = dwarf_alloc_block (cu);
c906108c
SS
9789 blk->size = read_2_bytes (abfd, info_ptr);
9790 info_ptr += 2;
9791 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9792 info_ptr += blk->size;
9793 DW_BLOCK (attr) = blk;
9794 break;
9795 case DW_FORM_block4:
7b5a2f43 9796 blk = dwarf_alloc_block (cu);
c906108c
SS
9797 blk->size = read_4_bytes (abfd, info_ptr);
9798 info_ptr += 4;
9799 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9800 info_ptr += blk->size;
9801 DW_BLOCK (attr) = blk;
9802 break;
9803 case DW_FORM_data2:
9804 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9805 info_ptr += 2;
9806 break;
9807 case DW_FORM_data4:
9808 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9809 info_ptr += 4;
9810 break;
9811 case DW_FORM_data8:
9812 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9813 info_ptr += 8;
9814 break;
2dc7f7b3
TT
9815 case DW_FORM_sec_offset:
9816 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9817 info_ptr += bytes_read;
9818 break;
c906108c 9819 case DW_FORM_string:
9b1c24c8 9820 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 9821 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
9822 info_ptr += bytes_read;
9823 break;
4bdf3d34
JJ
9824 case DW_FORM_strp:
9825 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9826 &bytes_read);
8285870a 9827 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
9828 info_ptr += bytes_read;
9829 break;
2dc7f7b3 9830 case DW_FORM_exprloc:
c906108c 9831 case DW_FORM_block:
7b5a2f43 9832 blk = dwarf_alloc_block (cu);
c906108c
SS
9833 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9834 info_ptr += bytes_read;
9835 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9836 info_ptr += blk->size;
9837 DW_BLOCK (attr) = blk;
9838 break;
9839 case DW_FORM_block1:
7b5a2f43 9840 blk = dwarf_alloc_block (cu);
c906108c
SS
9841 blk->size = read_1_byte (abfd, info_ptr);
9842 info_ptr += 1;
9843 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9844 info_ptr += blk->size;
9845 DW_BLOCK (attr) = blk;
9846 break;
9847 case DW_FORM_data1:
9848 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9849 info_ptr += 1;
9850 break;
9851 case DW_FORM_flag:
9852 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9853 info_ptr += 1;
9854 break;
2dc7f7b3
TT
9855 case DW_FORM_flag_present:
9856 DW_UNSND (attr) = 1;
9857 break;
c906108c
SS
9858 case DW_FORM_sdata:
9859 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9860 info_ptr += bytes_read;
9861 break;
9862 case DW_FORM_udata:
9863 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9864 info_ptr += bytes_read;
9865 break;
9866 case DW_FORM_ref1:
10b3939b 9867 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
9868 info_ptr += 1;
9869 break;
9870 case DW_FORM_ref2:
10b3939b 9871 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
9872 info_ptr += 2;
9873 break;
9874 case DW_FORM_ref4:
10b3939b 9875 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
9876 info_ptr += 4;
9877 break;
613e1657 9878 case DW_FORM_ref8:
10b3939b 9879 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
9880 info_ptr += 8;
9881 break;
55f1336d 9882 case DW_FORM_ref_sig8:
348e048f
DE
9883 /* Convert the signature to something we can record in DW_UNSND
9884 for later lookup.
9885 NOTE: This is NULL if the type wasn't found. */
9886 DW_SIGNATURED_TYPE (attr) =
9887 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9888 info_ptr += 8;
9889 break;
c906108c 9890 case DW_FORM_ref_udata:
10b3939b
DJ
9891 DW_ADDR (attr) = (cu->header.offset
9892 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
9893 info_ptr += bytes_read;
9894 break;
c906108c 9895 case DW_FORM_indirect:
a8329558
KW
9896 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9897 info_ptr += bytes_read;
e7c27a73 9898 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 9899 break;
c906108c 9900 default:
8a3fe4f8 9901 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
9902 dwarf_form_name (form),
9903 bfd_get_filename (abfd));
c906108c 9904 }
28e94949
JB
9905
9906 /* We have seen instances where the compiler tried to emit a byte
9907 size attribute of -1 which ended up being encoded as an unsigned
9908 0xffffffff. Although 0xffffffff is technically a valid size value,
9909 an object of this size seems pretty unlikely so we can relatively
9910 safely treat these cases as if the size attribute was invalid and
9911 treat them as zero by default. */
9912 if (attr->name == DW_AT_byte_size
9913 && form == DW_FORM_data4
9914 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
9915 {
9916 complaint
9917 (&symfile_complaints,
43bbcdc2
PH
9918 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9919 hex_string (DW_UNSND (attr)));
01c66ae6
JB
9920 DW_UNSND (attr) = 0;
9921 }
28e94949 9922
c906108c
SS
9923 return info_ptr;
9924}
9925
a8329558
KW
9926/* Read an attribute described by an abbreviated attribute. */
9927
fe1b8b76 9928static gdb_byte *
a8329558 9929read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 9930 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
9931{
9932 attr->name = abbrev->name;
e7c27a73 9933 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
9934}
9935
0963b4bd 9936/* Read dwarf information from a buffer. */
c906108c
SS
9937
9938static unsigned int
fe1b8b76 9939read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 9940{
fe1b8b76 9941 return bfd_get_8 (abfd, buf);
c906108c
SS
9942}
9943
9944static int
fe1b8b76 9945read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 9946{
fe1b8b76 9947 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
9948}
9949
9950static unsigned int
fe1b8b76 9951read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9952{
fe1b8b76 9953 return bfd_get_16 (abfd, buf);
c906108c
SS
9954}
9955
9956static int
fe1b8b76 9957read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9958{
fe1b8b76 9959 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
9960}
9961
9962static unsigned int
fe1b8b76 9963read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9964{
fe1b8b76 9965 return bfd_get_32 (abfd, buf);
c906108c
SS
9966}
9967
9968static int
fe1b8b76 9969read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9970{
fe1b8b76 9971 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
9972}
9973
93311388 9974static ULONGEST
fe1b8b76 9975read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 9976{
fe1b8b76 9977 return bfd_get_64 (abfd, buf);
c906108c
SS
9978}
9979
9980static CORE_ADDR
fe1b8b76 9981read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 9982 unsigned int *bytes_read)
c906108c 9983{
e7c27a73 9984 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9985 CORE_ADDR retval = 0;
9986
107d2387 9987 if (cu_header->signed_addr_p)
c906108c 9988 {
107d2387
AC
9989 switch (cu_header->addr_size)
9990 {
9991 case 2:
fe1b8b76 9992 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
9993 break;
9994 case 4:
fe1b8b76 9995 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
9996 break;
9997 case 8:
fe1b8b76 9998 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
9999 break;
10000 default:
8e65ff28 10001 internal_error (__FILE__, __LINE__,
e2e0b3e5 10002 _("read_address: bad switch, signed [in module %s]"),
659b0389 10003 bfd_get_filename (abfd));
107d2387
AC
10004 }
10005 }
10006 else
10007 {
10008 switch (cu_header->addr_size)
10009 {
10010 case 2:
fe1b8b76 10011 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10012 break;
10013 case 4:
fe1b8b76 10014 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10015 break;
10016 case 8:
fe1b8b76 10017 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10018 break;
10019 default:
8e65ff28 10020 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10021 _("read_address: bad switch, "
10022 "unsigned [in module %s]"),
659b0389 10023 bfd_get_filename (abfd));
107d2387 10024 }
c906108c 10025 }
64367e0a 10026
107d2387
AC
10027 *bytes_read = cu_header->addr_size;
10028 return retval;
c906108c
SS
10029}
10030
f7ef9339 10031/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10032 specification allows the initial length to take up either 4 bytes
10033 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10034 bytes describe the length and all offsets will be 8 bytes in length
10035 instead of 4.
10036
f7ef9339
KB
10037 An older, non-standard 64-bit format is also handled by this
10038 function. The older format in question stores the initial length
10039 as an 8-byte quantity without an escape value. Lengths greater
10040 than 2^32 aren't very common which means that the initial 4 bytes
10041 is almost always zero. Since a length value of zero doesn't make
10042 sense for the 32-bit format, this initial zero can be considered to
10043 be an escape value which indicates the presence of the older 64-bit
10044 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10045 greater than 4GB. If it becomes necessary to handle lengths
10046 somewhat larger than 4GB, we could allow other small values (such
10047 as the non-sensical values of 1, 2, and 3) to also be used as
10048 escape values indicating the presence of the old format.
f7ef9339 10049
917c78fc
MK
10050 The value returned via bytes_read should be used to increment the
10051 relevant pointer after calling read_initial_length().
c764a876 10052
613e1657
KB
10053 [ Note: read_initial_length() and read_offset() are based on the
10054 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10055 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10056 from:
10057
f7ef9339 10058 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10059
613e1657
KB
10060 This document is only a draft and is subject to change. (So beware.)
10061
f7ef9339 10062 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10063 determined empirically by examining 64-bit ELF files produced by
10064 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10065
10066 - Kevin, July 16, 2002
613e1657
KB
10067 ] */
10068
10069static LONGEST
c764a876 10070read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10071{
fe1b8b76 10072 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10073
dd373385 10074 if (length == 0xffffffff)
613e1657 10075 {
fe1b8b76 10076 length = bfd_get_64 (abfd, buf + 4);
613e1657 10077 *bytes_read = 12;
613e1657 10078 }
dd373385 10079 else if (length == 0)
f7ef9339 10080 {
dd373385 10081 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10082 length = bfd_get_64 (abfd, buf);
f7ef9339 10083 *bytes_read = 8;
f7ef9339 10084 }
613e1657
KB
10085 else
10086 {
10087 *bytes_read = 4;
613e1657
KB
10088 }
10089
c764a876
DE
10090 return length;
10091}
dd373385 10092
c764a876
DE
10093/* Cover function for read_initial_length.
10094 Returns the length of the object at BUF, and stores the size of the
10095 initial length in *BYTES_READ and stores the size that offsets will be in
10096 *OFFSET_SIZE.
10097 If the initial length size is not equivalent to that specified in
10098 CU_HEADER then issue a complaint.
10099 This is useful when reading non-comp-unit headers. */
dd373385 10100
c764a876
DE
10101static LONGEST
10102read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10103 const struct comp_unit_head *cu_header,
10104 unsigned int *bytes_read,
10105 unsigned int *offset_size)
10106{
10107 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10108
10109 gdb_assert (cu_header->initial_length_size == 4
10110 || cu_header->initial_length_size == 8
10111 || cu_header->initial_length_size == 12);
10112
10113 if (cu_header->initial_length_size != *bytes_read)
10114 complaint (&symfile_complaints,
10115 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10116
c764a876 10117 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10118 return length;
613e1657
KB
10119}
10120
10121/* Read an offset from the data stream. The size of the offset is
917c78fc 10122 given by cu_header->offset_size. */
613e1657
KB
10123
10124static LONGEST
fe1b8b76 10125read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10126 unsigned int *bytes_read)
c764a876
DE
10127{
10128 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10129
c764a876
DE
10130 *bytes_read = cu_header->offset_size;
10131 return offset;
10132}
10133
10134/* Read an offset from the data stream. */
10135
10136static LONGEST
10137read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10138{
10139 LONGEST retval = 0;
10140
c764a876 10141 switch (offset_size)
613e1657
KB
10142 {
10143 case 4:
fe1b8b76 10144 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10145 break;
10146 case 8:
fe1b8b76 10147 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10148 break;
10149 default:
8e65ff28 10150 internal_error (__FILE__, __LINE__,
c764a876 10151 _("read_offset_1: bad switch [in module %s]"),
659b0389 10152 bfd_get_filename (abfd));
613e1657
KB
10153 }
10154
917c78fc 10155 return retval;
613e1657
KB
10156}
10157
fe1b8b76
JB
10158static gdb_byte *
10159read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10160{
10161 /* If the size of a host char is 8 bits, we can return a pointer
10162 to the buffer, otherwise we have to copy the data to a buffer
10163 allocated on the temporary obstack. */
4bdf3d34 10164 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10165 return buf;
c906108c
SS
10166}
10167
10168static char *
9b1c24c8 10169read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10170{
10171 /* If the size of a host char is 8 bits, we can return a pointer
10172 to the string, otherwise we have to copy the string to a buffer
10173 allocated on the temporary obstack. */
4bdf3d34 10174 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10175 if (*buf == '\0')
10176 {
10177 *bytes_read_ptr = 1;
10178 return NULL;
10179 }
fe1b8b76
JB
10180 *bytes_read_ptr = strlen ((char *) buf) + 1;
10181 return (char *) buf;
4bdf3d34
JJ
10182}
10183
10184static char *
fe1b8b76 10185read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
10186 const struct comp_unit_head *cu_header,
10187 unsigned int *bytes_read_ptr)
10188{
c764a876 10189 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 10190
be391dca 10191 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10192 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 10193 {
8a3fe4f8 10194 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 10195 bfd_get_filename (abfd));
4bdf3d34 10196 return NULL;
c906108c 10197 }
dce234bc 10198 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 10199 {
3e43a32a
MS
10200 error (_("DW_FORM_strp pointing outside of "
10201 ".debug_str section [in module %s]"),
10202 bfd_get_filename (abfd));
c906108c
SS
10203 return NULL;
10204 }
4bdf3d34 10205 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10206 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10207 return NULL;
dce234bc 10208 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10209}
10210
ce5d95e1 10211static unsigned long
fe1b8b76 10212read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10213{
ce5d95e1
JB
10214 unsigned long result;
10215 unsigned int num_read;
c906108c
SS
10216 int i, shift;
10217 unsigned char byte;
10218
10219 result = 0;
10220 shift = 0;
10221 num_read = 0;
10222 i = 0;
10223 while (1)
10224 {
fe1b8b76 10225 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10226 buf++;
10227 num_read++;
ce5d95e1 10228 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10229 if ((byte & 128) == 0)
10230 {
10231 break;
10232 }
10233 shift += 7;
10234 }
10235 *bytes_read_ptr = num_read;
10236 return result;
10237}
10238
ce5d95e1 10239static long
fe1b8b76 10240read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10241{
ce5d95e1 10242 long result;
77e0b926 10243 int i, shift, num_read;
c906108c
SS
10244 unsigned char byte;
10245
10246 result = 0;
10247 shift = 0;
c906108c
SS
10248 num_read = 0;
10249 i = 0;
10250 while (1)
10251 {
fe1b8b76 10252 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10253 buf++;
10254 num_read++;
ce5d95e1 10255 result |= ((long)(byte & 127) << shift);
c906108c
SS
10256 shift += 7;
10257 if ((byte & 128) == 0)
10258 {
10259 break;
10260 }
10261 }
77e0b926
DJ
10262 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10263 result |= -(((long)1) << shift);
c906108c
SS
10264 *bytes_read_ptr = num_read;
10265 return result;
10266}
10267
4bb7a0a7
DJ
10268/* Return a pointer to just past the end of an LEB128 number in BUF. */
10269
fe1b8b76
JB
10270static gdb_byte *
10271skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10272{
10273 int byte;
10274
10275 while (1)
10276 {
fe1b8b76 10277 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10278 buf++;
10279 if ((byte & 128) == 0)
10280 return buf;
10281 }
10282}
10283
c906108c 10284static void
e142c38c 10285set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10286{
10287 switch (lang)
10288 {
10289 case DW_LANG_C89:
76bee0cc 10290 case DW_LANG_C99:
c906108c 10291 case DW_LANG_C:
e142c38c 10292 cu->language = language_c;
c906108c
SS
10293 break;
10294 case DW_LANG_C_plus_plus:
e142c38c 10295 cu->language = language_cplus;
c906108c 10296 break;
6aecb9c2
JB
10297 case DW_LANG_D:
10298 cu->language = language_d;
10299 break;
c906108c
SS
10300 case DW_LANG_Fortran77:
10301 case DW_LANG_Fortran90:
b21b22e0 10302 case DW_LANG_Fortran95:
e142c38c 10303 cu->language = language_fortran;
c906108c
SS
10304 break;
10305 case DW_LANG_Mips_Assembler:
e142c38c 10306 cu->language = language_asm;
c906108c 10307 break;
bebd888e 10308 case DW_LANG_Java:
e142c38c 10309 cu->language = language_java;
bebd888e 10310 break;
c906108c 10311 case DW_LANG_Ada83:
8aaf0b47 10312 case DW_LANG_Ada95:
bc5f45f8
JB
10313 cu->language = language_ada;
10314 break;
72019c9c
GM
10315 case DW_LANG_Modula2:
10316 cu->language = language_m2;
10317 break;
fe8e67fd
PM
10318 case DW_LANG_Pascal83:
10319 cu->language = language_pascal;
10320 break;
22566fbd
DJ
10321 case DW_LANG_ObjC:
10322 cu->language = language_objc;
10323 break;
c906108c
SS
10324 case DW_LANG_Cobol74:
10325 case DW_LANG_Cobol85:
c906108c 10326 default:
e142c38c 10327 cu->language = language_minimal;
c906108c
SS
10328 break;
10329 }
e142c38c 10330 cu->language_defn = language_def (cu->language);
c906108c
SS
10331}
10332
10333/* Return the named attribute or NULL if not there. */
10334
10335static struct attribute *
e142c38c 10336dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10337{
10338 unsigned int i;
10339 struct attribute *spec = NULL;
10340
10341 for (i = 0; i < die->num_attrs; ++i)
10342 {
10343 if (die->attrs[i].name == name)
10b3939b 10344 return &die->attrs[i];
c906108c
SS
10345 if (die->attrs[i].name == DW_AT_specification
10346 || die->attrs[i].name == DW_AT_abstract_origin)
10347 spec = &die->attrs[i];
10348 }
c906108c 10349
10b3939b 10350 if (spec)
f2f0e013
DJ
10351 {
10352 die = follow_die_ref (die, spec, &cu);
10353 return dwarf2_attr (die, name, cu);
10354 }
c5aa993b 10355
c906108c
SS
10356 return NULL;
10357}
10358
348e048f
DE
10359/* Return the named attribute or NULL if not there,
10360 but do not follow DW_AT_specification, etc.
10361 This is for use in contexts where we're reading .debug_types dies.
10362 Following DW_AT_specification, DW_AT_abstract_origin will take us
10363 back up the chain, and we want to go down. */
10364
10365static struct attribute *
10366dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10367 struct dwarf2_cu *cu)
10368{
10369 unsigned int i;
10370
10371 for (i = 0; i < die->num_attrs; ++i)
10372 if (die->attrs[i].name == name)
10373 return &die->attrs[i];
10374
10375 return NULL;
10376}
10377
05cf31d1
JB
10378/* Return non-zero iff the attribute NAME is defined for the given DIE,
10379 and holds a non-zero value. This function should only be used for
2dc7f7b3 10380 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10381
10382static int
10383dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10384{
10385 struct attribute *attr = dwarf2_attr (die, name, cu);
10386
10387 return (attr && DW_UNSND (attr));
10388}
10389
3ca72b44 10390static int
e142c38c 10391die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10392{
05cf31d1
JB
10393 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10394 which value is non-zero. However, we have to be careful with
10395 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10396 (via dwarf2_flag_true_p) follows this attribute. So we may
10397 end up accidently finding a declaration attribute that belongs
10398 to a different DIE referenced by the specification attribute,
10399 even though the given DIE does not have a declaration attribute. */
10400 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10401 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10402}
10403
63d06c5c 10404/* Return the die giving the specification for DIE, if there is
f2f0e013 10405 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10406 containing the return value on output. If there is no
10407 specification, but there is an abstract origin, that is
10408 returned. */
63d06c5c
DC
10409
10410static struct die_info *
f2f0e013 10411die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10412{
f2f0e013
DJ
10413 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10414 *spec_cu);
63d06c5c 10415
edb3359d
DJ
10416 if (spec_attr == NULL)
10417 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10418
63d06c5c
DC
10419 if (spec_attr == NULL)
10420 return NULL;
10421 else
f2f0e013 10422 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10423}
c906108c 10424
debd256d 10425/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10426 refers to.
10427 NOTE: This is also used as a "cleanup" function. */
10428
debd256d
JB
10429static void
10430free_line_header (struct line_header *lh)
10431{
10432 if (lh->standard_opcode_lengths)
a8bc7b56 10433 xfree (lh->standard_opcode_lengths);
debd256d
JB
10434
10435 /* Remember that all the lh->file_names[i].name pointers are
10436 pointers into debug_line_buffer, and don't need to be freed. */
10437 if (lh->file_names)
a8bc7b56 10438 xfree (lh->file_names);
debd256d
JB
10439
10440 /* Similarly for the include directory names. */
10441 if (lh->include_dirs)
a8bc7b56 10442 xfree (lh->include_dirs);
debd256d 10443
a8bc7b56 10444 xfree (lh);
debd256d
JB
10445}
10446
debd256d 10447/* Add an entry to LH's include directory table. */
ae2de4f8 10448
debd256d
JB
10449static void
10450add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10451{
debd256d
JB
10452 /* Grow the array if necessary. */
10453 if (lh->include_dirs_size == 0)
c5aa993b 10454 {
debd256d
JB
10455 lh->include_dirs_size = 1; /* for testing */
10456 lh->include_dirs = xmalloc (lh->include_dirs_size
10457 * sizeof (*lh->include_dirs));
10458 }
10459 else if (lh->num_include_dirs >= lh->include_dirs_size)
10460 {
10461 lh->include_dirs_size *= 2;
10462 lh->include_dirs = xrealloc (lh->include_dirs,
10463 (lh->include_dirs_size
10464 * sizeof (*lh->include_dirs)));
c5aa993b 10465 }
c906108c 10466
debd256d
JB
10467 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10468}
6e70227d 10469
debd256d 10470/* Add an entry to LH's file name table. */
ae2de4f8 10471
debd256d
JB
10472static void
10473add_file_name (struct line_header *lh,
10474 char *name,
10475 unsigned int dir_index,
10476 unsigned int mod_time,
10477 unsigned int length)
10478{
10479 struct file_entry *fe;
10480
10481 /* Grow the array if necessary. */
10482 if (lh->file_names_size == 0)
10483 {
10484 lh->file_names_size = 1; /* for testing */
10485 lh->file_names = xmalloc (lh->file_names_size
10486 * sizeof (*lh->file_names));
10487 }
10488 else if (lh->num_file_names >= lh->file_names_size)
10489 {
10490 lh->file_names_size *= 2;
10491 lh->file_names = xrealloc (lh->file_names,
10492 (lh->file_names_size
10493 * sizeof (*lh->file_names)));
10494 }
10495
10496 fe = &lh->file_names[lh->num_file_names++];
10497 fe->name = name;
10498 fe->dir_index = dir_index;
10499 fe->mod_time = mod_time;
10500 fe->length = length;
aaa75496 10501 fe->included_p = 0;
cb1df416 10502 fe->symtab = NULL;
debd256d 10503}
6e70227d 10504
debd256d 10505/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10506 .debug_line, according to the endianness of ABFD. Return a pointer
10507 to a struct line_header, allocated using xmalloc.
debd256d
JB
10508
10509 NOTE: the strings in the include directory and file name tables of
10510 the returned object point into debug_line_buffer, and must not be
10511 freed. */
ae2de4f8 10512
debd256d
JB
10513static struct line_header *
10514dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10515 struct dwarf2_cu *cu)
debd256d
JB
10516{
10517 struct cleanup *back_to;
10518 struct line_header *lh;
fe1b8b76 10519 gdb_byte *line_ptr;
c764a876 10520 unsigned int bytes_read, offset_size;
debd256d
JB
10521 int i;
10522 char *cur_dir, *cur_file;
10523
be391dca 10524 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10525 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10526 {
e2e0b3e5 10527 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10528 return 0;
10529 }
10530
a738430d
MK
10531 /* Make sure that at least there's room for the total_length field.
10532 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10533 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10534 {
4d3c2250 10535 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10536 return 0;
10537 }
10538
10539 lh = xmalloc (sizeof (*lh));
10540 memset (lh, 0, sizeof (*lh));
10541 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10542 (void *) lh);
10543
dce234bc 10544 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10545
a738430d 10546 /* Read in the header. */
6e70227d 10547 lh->total_length =
c764a876
DE
10548 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10549 &bytes_read, &offset_size);
debd256d 10550 line_ptr += bytes_read;
dce234bc
PP
10551 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10552 + dwarf2_per_objfile->line.size))
debd256d 10553 {
4d3c2250 10554 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10555 return 0;
10556 }
10557 lh->statement_program_end = line_ptr + lh->total_length;
10558 lh->version = read_2_bytes (abfd, line_ptr);
10559 line_ptr += 2;
c764a876
DE
10560 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10561 line_ptr += offset_size;
debd256d
JB
10562 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10563 line_ptr += 1;
2dc7f7b3
TT
10564 if (lh->version >= 4)
10565 {
10566 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10567 line_ptr += 1;
10568 }
10569 else
10570 lh->maximum_ops_per_instruction = 1;
10571
10572 if (lh->maximum_ops_per_instruction == 0)
10573 {
10574 lh->maximum_ops_per_instruction = 1;
10575 complaint (&symfile_complaints,
3e43a32a
MS
10576 _("invalid maximum_ops_per_instruction "
10577 "in `.debug_line' section"));
2dc7f7b3
TT
10578 }
10579
debd256d
JB
10580 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10581 line_ptr += 1;
10582 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10583 line_ptr += 1;
10584 lh->line_range = read_1_byte (abfd, line_ptr);
10585 line_ptr += 1;
10586 lh->opcode_base = read_1_byte (abfd, line_ptr);
10587 line_ptr += 1;
10588 lh->standard_opcode_lengths
fe1b8b76 10589 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10590
10591 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10592 for (i = 1; i < lh->opcode_base; ++i)
10593 {
10594 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10595 line_ptr += 1;
10596 }
10597
a738430d 10598 /* Read directory table. */
9b1c24c8 10599 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10600 {
10601 line_ptr += bytes_read;
10602 add_include_dir (lh, cur_dir);
10603 }
10604 line_ptr += bytes_read;
10605
a738430d 10606 /* Read file name table. */
9b1c24c8 10607 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
10608 {
10609 unsigned int dir_index, mod_time, length;
10610
10611 line_ptr += bytes_read;
10612 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10613 line_ptr += bytes_read;
10614 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10615 line_ptr += bytes_read;
10616 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10617 line_ptr += bytes_read;
10618
10619 add_file_name (lh, cur_file, dir_index, mod_time, length);
10620 }
10621 line_ptr += bytes_read;
6e70227d 10622 lh->statement_program_start = line_ptr;
debd256d 10623
dce234bc
PP
10624 if (line_ptr > (dwarf2_per_objfile->line.buffer
10625 + dwarf2_per_objfile->line.size))
4d3c2250 10626 complaint (&symfile_complaints,
3e43a32a
MS
10627 _("line number info header doesn't "
10628 "fit in `.debug_line' section"));
debd256d
JB
10629
10630 discard_cleanups (back_to);
10631 return lh;
10632}
c906108c 10633
5fb290d7
DJ
10634/* This function exists to work around a bug in certain compilers
10635 (particularly GCC 2.95), in which the first line number marker of a
10636 function does not show up until after the prologue, right before
10637 the second line number marker. This function shifts ADDRESS down
10638 to the beginning of the function if necessary, and is called on
10639 addresses passed to record_line. */
10640
10641static CORE_ADDR
e142c38c 10642check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
10643{
10644 struct function_range *fn;
10645
10646 /* Find the function_range containing address. */
e142c38c 10647 if (!cu->first_fn)
5fb290d7
DJ
10648 return address;
10649
e142c38c
DJ
10650 if (!cu->cached_fn)
10651 cu->cached_fn = cu->first_fn;
5fb290d7 10652
e142c38c 10653 fn = cu->cached_fn;
5fb290d7
DJ
10654 while (fn)
10655 if (fn->lowpc <= address && fn->highpc > address)
10656 goto found;
10657 else
10658 fn = fn->next;
10659
e142c38c
DJ
10660 fn = cu->first_fn;
10661 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
10662 if (fn->lowpc <= address && fn->highpc > address)
10663 goto found;
10664 else
10665 fn = fn->next;
10666
10667 return address;
10668
10669 found:
10670 if (fn->seen_line)
10671 return address;
10672 if (address != fn->lowpc)
4d3c2250 10673 complaint (&symfile_complaints,
e2e0b3e5 10674 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 10675 (unsigned long) address, fn->name);
5fb290d7
DJ
10676 fn->seen_line = 1;
10677 return fn->lowpc;
10678}
10679
c6da4cef
DE
10680/* Subroutine of dwarf_decode_lines to simplify it.
10681 Return the file name of the psymtab for included file FILE_INDEX
10682 in line header LH of PST.
10683 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10684 If space for the result is malloc'd, it will be freed by a cleanup.
10685 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10686
10687static char *
10688psymtab_include_file_name (const struct line_header *lh, int file_index,
10689 const struct partial_symtab *pst,
10690 const char *comp_dir)
10691{
10692 const struct file_entry fe = lh->file_names [file_index];
10693 char *include_name = fe.name;
10694 char *include_name_to_compare = include_name;
10695 char *dir_name = NULL;
72b9f47f
TT
10696 const char *pst_filename;
10697 char *copied_name = NULL;
c6da4cef
DE
10698 int file_is_pst;
10699
10700 if (fe.dir_index)
10701 dir_name = lh->include_dirs[fe.dir_index - 1];
10702
10703 if (!IS_ABSOLUTE_PATH (include_name)
10704 && (dir_name != NULL || comp_dir != NULL))
10705 {
10706 /* Avoid creating a duplicate psymtab for PST.
10707 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10708 Before we do the comparison, however, we need to account
10709 for DIR_NAME and COMP_DIR.
10710 First prepend dir_name (if non-NULL). If we still don't
10711 have an absolute path prepend comp_dir (if non-NULL).
10712 However, the directory we record in the include-file's
10713 psymtab does not contain COMP_DIR (to match the
10714 corresponding symtab(s)).
10715
10716 Example:
10717
10718 bash$ cd /tmp
10719 bash$ gcc -g ./hello.c
10720 include_name = "hello.c"
10721 dir_name = "."
10722 DW_AT_comp_dir = comp_dir = "/tmp"
10723 DW_AT_name = "./hello.c" */
10724
10725 if (dir_name != NULL)
10726 {
10727 include_name = concat (dir_name, SLASH_STRING,
10728 include_name, (char *)NULL);
10729 include_name_to_compare = include_name;
10730 make_cleanup (xfree, include_name);
10731 }
10732 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10733 {
10734 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10735 include_name, (char *)NULL);
10736 }
10737 }
10738
10739 pst_filename = pst->filename;
10740 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10741 {
72b9f47f
TT
10742 copied_name = concat (pst->dirname, SLASH_STRING,
10743 pst_filename, (char *)NULL);
10744 pst_filename = copied_name;
c6da4cef
DE
10745 }
10746
1e3fad37 10747 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
10748
10749 if (include_name_to_compare != include_name)
10750 xfree (include_name_to_compare);
72b9f47f
TT
10751 if (copied_name != NULL)
10752 xfree (copied_name);
c6da4cef
DE
10753
10754 if (file_is_pst)
10755 return NULL;
10756 return include_name;
10757}
10758
c91513d8
PP
10759/* Ignore this record_line request. */
10760
10761static void
10762noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10763{
10764 return;
10765}
10766
aaa75496
JB
10767/* Decode the Line Number Program (LNP) for the given line_header
10768 structure and CU. The actual information extracted and the type
10769 of structures created from the LNP depends on the value of PST.
10770
10771 1. If PST is NULL, then this procedure uses the data from the program
10772 to create all necessary symbol tables, and their linetables.
6e70227d 10773
aaa75496
JB
10774 2. If PST is not NULL, this procedure reads the program to determine
10775 the list of files included by the unit represented by PST, and
c6da4cef
DE
10776 builds all the associated partial symbol tables.
10777
10778 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10779 It is used for relative paths in the line table.
10780 NOTE: When processing partial symtabs (pst != NULL),
10781 comp_dir == pst->dirname.
10782
10783 NOTE: It is important that psymtabs have the same file name (via strcmp)
10784 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10785 symtab we don't use it in the name of the psymtabs we create.
10786 E.g. expand_line_sal requires this when finding psymtabs to expand.
10787 A good testcase for this is mb-inline.exp. */
debd256d 10788
c906108c 10789static void
72b9f47f 10790dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 10791 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 10792{
a8c50c1f 10793 gdb_byte *line_ptr, *extended_end;
fe1b8b76 10794 gdb_byte *line_end;
a8c50c1f 10795 unsigned int bytes_read, extended_len;
c906108c 10796 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
10797 CORE_ADDR baseaddr;
10798 struct objfile *objfile = cu->objfile;
fbf65064 10799 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 10800 const int decode_for_pst_p = (pst != NULL);
cb1df416 10801 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
10802 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10803 = record_line;
e142c38c
DJ
10804
10805 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 10806
debd256d
JB
10807 line_ptr = lh->statement_program_start;
10808 line_end = lh->statement_program_end;
c906108c
SS
10809
10810 /* Read the statement sequences until there's nothing left. */
10811 while (line_ptr < line_end)
10812 {
10813 /* state machine registers */
10814 CORE_ADDR address = 0;
10815 unsigned int file = 1;
10816 unsigned int line = 1;
10817 unsigned int column = 0;
debd256d 10818 int is_stmt = lh->default_is_stmt;
c906108c
SS
10819 int basic_block = 0;
10820 int end_sequence = 0;
fbf65064 10821 CORE_ADDR addr;
2dc7f7b3 10822 unsigned char op_index = 0;
c906108c 10823
aaa75496 10824 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 10825 {
aaa75496 10826 /* Start a subfile for the current file of the state machine. */
debd256d
JB
10827 /* lh->include_dirs and lh->file_names are 0-based, but the
10828 directory and file name numbers in the statement program
10829 are 1-based. */
10830 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 10831 char *dir = NULL;
a738430d 10832
debd256d
JB
10833 if (fe->dir_index)
10834 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
10835
10836 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
10837 }
10838
a738430d 10839 /* Decode the table. */
c5aa993b 10840 while (!end_sequence)
c906108c
SS
10841 {
10842 op_code = read_1_byte (abfd, line_ptr);
10843 line_ptr += 1;
59205f5a
JB
10844 if (line_ptr > line_end)
10845 {
10846 dwarf2_debug_line_missing_end_sequence_complaint ();
10847 break;
10848 }
9aa1fe7e 10849
debd256d 10850 if (op_code >= lh->opcode_base)
6e70227d 10851 {
a738430d 10852 /* Special operand. */
debd256d 10853 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
10854 address += (((op_index + (adj_opcode / lh->line_range))
10855 / lh->maximum_ops_per_instruction)
10856 * lh->minimum_instruction_length);
10857 op_index = ((op_index + (adj_opcode / lh->line_range))
10858 % lh->maximum_ops_per_instruction);
debd256d 10859 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 10860 if (lh->num_file_names < file || file == 0)
25e43795 10861 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
10862 /* For now we ignore lines not starting on an
10863 instruction boundary. */
10864 else if (op_index == 0)
25e43795
DJ
10865 {
10866 lh->file_names[file - 1].included_p = 1;
ca5f395d 10867 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10868 {
10869 if (last_subfile != current_subfile)
10870 {
10871 addr = gdbarch_addr_bits_remove (gdbarch, address);
10872 if (last_subfile)
c91513d8 10873 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10874 last_subfile = current_subfile;
10875 }
25e43795 10876 /* Append row to matrix using current values. */
fbf65064
UW
10877 addr = check_cu_functions (address, cu);
10878 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10879 (*p_record_line) (current_subfile, line, addr);
366da635 10880 }
25e43795 10881 }
ca5f395d 10882 basic_block = 0;
9aa1fe7e
GK
10883 }
10884 else switch (op_code)
c906108c
SS
10885 {
10886 case DW_LNS_extended_op:
3e43a32a
MS
10887 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10888 &bytes_read);
473b7be6 10889 line_ptr += bytes_read;
a8c50c1f 10890 extended_end = line_ptr + extended_len;
c906108c
SS
10891 extended_op = read_1_byte (abfd, line_ptr);
10892 line_ptr += 1;
10893 switch (extended_op)
10894 {
10895 case DW_LNE_end_sequence:
c91513d8 10896 p_record_line = record_line;
c906108c 10897 end_sequence = 1;
c906108c
SS
10898 break;
10899 case DW_LNE_set_address:
e7c27a73 10900 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
10901
10902 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10903 {
10904 /* This line table is for a function which has been
10905 GCd by the linker. Ignore it. PR gdb/12528 */
10906
10907 long line_offset
10908 = line_ptr - dwarf2_per_objfile->line.buffer;
10909
10910 complaint (&symfile_complaints,
10911 _(".debug_line address at offset 0x%lx is 0 "
10912 "[in module %s]"),
10913 line_offset, cu->objfile->name);
10914 p_record_line = noop_record_line;
10915 }
10916
2dc7f7b3 10917 op_index = 0;
107d2387
AC
10918 line_ptr += bytes_read;
10919 address += baseaddr;
c906108c
SS
10920 break;
10921 case DW_LNE_define_file:
debd256d
JB
10922 {
10923 char *cur_file;
10924 unsigned int dir_index, mod_time, length;
6e70227d 10925
3e43a32a
MS
10926 cur_file = read_direct_string (abfd, line_ptr,
10927 &bytes_read);
debd256d
JB
10928 line_ptr += bytes_read;
10929 dir_index =
10930 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10931 line_ptr += bytes_read;
10932 mod_time =
10933 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10934 line_ptr += bytes_read;
10935 length =
10936 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10937 line_ptr += bytes_read;
10938 add_file_name (lh, cur_file, dir_index, mod_time, length);
10939 }
c906108c 10940 break;
d0c6ba3d
CC
10941 case DW_LNE_set_discriminator:
10942 /* The discriminator is not interesting to the debugger;
10943 just ignore it. */
10944 line_ptr = extended_end;
10945 break;
c906108c 10946 default:
4d3c2250 10947 complaint (&symfile_complaints,
e2e0b3e5 10948 _("mangled .debug_line section"));
debd256d 10949 return;
c906108c 10950 }
a8c50c1f
DJ
10951 /* Make sure that we parsed the extended op correctly. If e.g.
10952 we expected a different address size than the producer used,
10953 we may have read the wrong number of bytes. */
10954 if (line_ptr != extended_end)
10955 {
10956 complaint (&symfile_complaints,
10957 _("mangled .debug_line section"));
10958 return;
10959 }
c906108c
SS
10960 break;
10961 case DW_LNS_copy:
59205f5a 10962 if (lh->num_file_names < file || file == 0)
25e43795
DJ
10963 dwarf2_debug_line_missing_file_complaint ();
10964 else
366da635 10965 {
25e43795 10966 lh->file_names[file - 1].included_p = 1;
ca5f395d 10967 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
10968 {
10969 if (last_subfile != current_subfile)
10970 {
10971 addr = gdbarch_addr_bits_remove (gdbarch, address);
10972 if (last_subfile)
c91513d8 10973 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
10974 last_subfile = current_subfile;
10975 }
10976 addr = check_cu_functions (address, cu);
10977 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 10978 (*p_record_line) (current_subfile, line, addr);
fbf65064 10979 }
366da635 10980 }
c906108c
SS
10981 basic_block = 0;
10982 break;
10983 case DW_LNS_advance_pc:
2dc7f7b3
TT
10984 {
10985 CORE_ADDR adjust
10986 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10987
10988 address += (((op_index + adjust)
10989 / lh->maximum_ops_per_instruction)
10990 * lh->minimum_instruction_length);
10991 op_index = ((op_index + adjust)
10992 % lh->maximum_ops_per_instruction);
10993 line_ptr += bytes_read;
10994 }
c906108c
SS
10995 break;
10996 case DW_LNS_advance_line:
10997 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10998 line_ptr += bytes_read;
10999 break;
11000 case DW_LNS_set_file:
debd256d 11001 {
a738430d
MK
11002 /* The arrays lh->include_dirs and lh->file_names are
11003 0-based, but the directory and file name numbers in
11004 the statement program are 1-based. */
debd256d 11005 struct file_entry *fe;
4f1520fb 11006 char *dir = NULL;
a738430d 11007
debd256d
JB
11008 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11009 line_ptr += bytes_read;
59205f5a 11010 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11011 dwarf2_debug_line_missing_file_complaint ();
11012 else
11013 {
11014 fe = &lh->file_names[file - 1];
11015 if (fe->dir_index)
11016 dir = lh->include_dirs[fe->dir_index - 1];
11017 if (!decode_for_pst_p)
11018 {
11019 last_subfile = current_subfile;
11020 dwarf2_start_subfile (fe->name, dir, comp_dir);
11021 }
11022 }
debd256d 11023 }
c906108c
SS
11024 break;
11025 case DW_LNS_set_column:
11026 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11027 line_ptr += bytes_read;
11028 break;
11029 case DW_LNS_negate_stmt:
11030 is_stmt = (!is_stmt);
11031 break;
11032 case DW_LNS_set_basic_block:
11033 basic_block = 1;
11034 break;
c2c6d25f
JM
11035 /* Add to the address register of the state machine the
11036 address increment value corresponding to special opcode
a738430d
MK
11037 255. I.e., this value is scaled by the minimum
11038 instruction length since special opcode 255 would have
b021a221 11039 scaled the increment. */
c906108c 11040 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11041 {
11042 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11043
11044 address += (((op_index + adjust)
11045 / lh->maximum_ops_per_instruction)
11046 * lh->minimum_instruction_length);
11047 op_index = ((op_index + adjust)
11048 % lh->maximum_ops_per_instruction);
11049 }
c906108c
SS
11050 break;
11051 case DW_LNS_fixed_advance_pc:
11052 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11053 op_index = 0;
c906108c
SS
11054 line_ptr += 2;
11055 break;
9aa1fe7e 11056 default:
a738430d
MK
11057 {
11058 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11059 int i;
a738430d 11060
debd256d 11061 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11062 {
11063 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11064 line_ptr += bytes_read;
11065 }
11066 }
c906108c
SS
11067 }
11068 }
59205f5a
JB
11069 if (lh->num_file_names < file || file == 0)
11070 dwarf2_debug_line_missing_file_complaint ();
11071 else
11072 {
11073 lh->file_names[file - 1].included_p = 1;
11074 if (!decode_for_pst_p)
fbf65064
UW
11075 {
11076 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11077 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11078 }
59205f5a 11079 }
c906108c 11080 }
aaa75496
JB
11081
11082 if (decode_for_pst_p)
11083 {
11084 int file_index;
11085
11086 /* Now that we're done scanning the Line Header Program, we can
11087 create the psymtab of each included file. */
11088 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11089 if (lh->file_names[file_index].included_p == 1)
11090 {
c6da4cef
DE
11091 char *include_name =
11092 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11093 if (include_name != NULL)
aaa75496
JB
11094 dwarf2_create_include_psymtab (include_name, pst, objfile);
11095 }
11096 }
cb1df416
DJ
11097 else
11098 {
11099 /* Make sure a symtab is created for every file, even files
11100 which contain only variables (i.e. no code with associated
11101 line numbers). */
11102
11103 int i;
11104 struct file_entry *fe;
11105
11106 for (i = 0; i < lh->num_file_names; i++)
11107 {
11108 char *dir = NULL;
9a619af0 11109
cb1df416
DJ
11110 fe = &lh->file_names[i];
11111 if (fe->dir_index)
11112 dir = lh->include_dirs[fe->dir_index - 1];
11113 dwarf2_start_subfile (fe->name, dir, comp_dir);
11114
11115 /* Skip the main file; we don't need it, and it must be
11116 allocated last, so that it will show up before the
11117 non-primary symtabs in the objfile's symtab list. */
11118 if (current_subfile == first_subfile)
11119 continue;
11120
11121 if (current_subfile->symtab == NULL)
11122 current_subfile->symtab = allocate_symtab (current_subfile->name,
11123 cu->objfile);
11124 fe->symtab = current_subfile->symtab;
11125 }
11126 }
c906108c
SS
11127}
11128
11129/* Start a subfile for DWARF. FILENAME is the name of the file and
11130 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11131 or NULL if not known. COMP_DIR is the compilation directory for the
11132 linetable's compilation unit or NULL if not known.
c906108c
SS
11133 This routine tries to keep line numbers from identical absolute and
11134 relative file names in a common subfile.
11135
11136 Using the `list' example from the GDB testsuite, which resides in
11137 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11138 of /srcdir/list0.c yields the following debugging information for list0.c:
11139
c5aa993b
JM
11140 DW_AT_name: /srcdir/list0.c
11141 DW_AT_comp_dir: /compdir
357e46e7 11142 files.files[0].name: list0.h
c5aa993b 11143 files.files[0].dir: /srcdir
357e46e7 11144 files.files[1].name: list0.c
c5aa993b 11145 files.files[1].dir: /srcdir
c906108c
SS
11146
11147 The line number information for list0.c has to end up in a single
4f1520fb
FR
11148 subfile, so that `break /srcdir/list0.c:1' works as expected.
11149 start_subfile will ensure that this happens provided that we pass the
11150 concatenation of files.files[1].dir and files.files[1].name as the
11151 subfile's name. */
c906108c
SS
11152
11153static void
3e43a32a
MS
11154dwarf2_start_subfile (char *filename, const char *dirname,
11155 const char *comp_dir)
c906108c 11156{
4f1520fb
FR
11157 char *fullname;
11158
11159 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11160 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11161 second argument to start_subfile. To be consistent, we do the
11162 same here. In order not to lose the line information directory,
11163 we concatenate it to the filename when it makes sense.
11164 Note that the Dwarf3 standard says (speaking of filenames in line
11165 information): ``The directory index is ignored for file names
11166 that represent full path names''. Thus ignoring dirname in the
11167 `else' branch below isn't an issue. */
c906108c 11168
d5166ae1 11169 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11170 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11171 else
11172 fullname = filename;
c906108c 11173
4f1520fb
FR
11174 start_subfile (fullname, comp_dir);
11175
11176 if (fullname != filename)
11177 xfree (fullname);
c906108c
SS
11178}
11179
4c2df51b
DJ
11180static void
11181var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11182 struct dwarf2_cu *cu)
4c2df51b 11183{
e7c27a73
DJ
11184 struct objfile *objfile = cu->objfile;
11185 struct comp_unit_head *cu_header = &cu->header;
11186
4c2df51b
DJ
11187 /* NOTE drow/2003-01-30: There used to be a comment and some special
11188 code here to turn a symbol with DW_AT_external and a
11189 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11190 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11191 with some versions of binutils) where shared libraries could have
11192 relocations against symbols in their debug information - the
11193 minimal symbol would have the right address, but the debug info
11194 would not. It's no longer necessary, because we will explicitly
11195 apply relocations when we read in the debug information now. */
11196
11197 /* A DW_AT_location attribute with no contents indicates that a
11198 variable has been optimized away. */
11199 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11200 {
11201 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11202 return;
11203 }
11204
11205 /* Handle one degenerate form of location expression specially, to
11206 preserve GDB's previous behavior when section offsets are
11207 specified. If this is just a DW_OP_addr then mark this symbol
11208 as LOC_STATIC. */
11209
11210 if (attr_form_is_block (attr)
11211 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11212 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11213 {
891d2f0b 11214 unsigned int dummy;
4c2df51b
DJ
11215
11216 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11217 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11218 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11219 fixup_symbol_section (sym, objfile);
11220 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11221 SYMBOL_SECTION (sym));
4c2df51b
DJ
11222 return;
11223 }
11224
11225 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11226 expression evaluator, and use LOC_COMPUTED only when necessary
11227 (i.e. when the value of a register or memory location is
11228 referenced, or a thread-local block, etc.). Then again, it might
11229 not be worthwhile. I'm assuming that it isn't unless performance
11230 or memory numbers show me otherwise. */
11231
e7c27a73 11232 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11233 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11234
11235 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11236 cu->has_loclist = 1;
4c2df51b
DJ
11237}
11238
c906108c
SS
11239/* Given a pointer to a DWARF information entry, figure out if we need
11240 to make a symbol table entry for it, and if so, create a new entry
11241 and return a pointer to it.
11242 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11243 used the passed type.
11244 If SPACE is not NULL, use it to hold the new symbol. If it is
11245 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11246
11247static struct symbol *
34eaf542
TT
11248new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11249 struct symbol *space)
c906108c 11250{
e7c27a73 11251 struct objfile *objfile = cu->objfile;
c906108c
SS
11252 struct symbol *sym = NULL;
11253 char *name;
11254 struct attribute *attr = NULL;
11255 struct attribute *attr2 = NULL;
e142c38c 11256 CORE_ADDR baseaddr;
e37fd15a
SW
11257 struct pending **list_to_add = NULL;
11258
edb3359d 11259 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11260
11261 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11262
94af9270 11263 name = dwarf2_name (die, cu);
c906108c
SS
11264 if (name)
11265 {
94af9270 11266 const char *linkagename;
34eaf542 11267 int suppress_add = 0;
94af9270 11268
34eaf542
TT
11269 if (space)
11270 sym = space;
11271 else
11272 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11273 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11274
11275 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11276 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11277 linkagename = dwarf2_physname (name, die, cu);
11278 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11279
f55ee35c
JK
11280 /* Fortran does not have mangling standard and the mangling does differ
11281 between gfortran, iFort etc. */
11282 if (cu->language == language_fortran
b250c185 11283 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11284 symbol_set_demangled_name (&(sym->ginfo),
11285 (char *) dwarf2_full_name (name, die, cu),
11286 NULL);
f55ee35c 11287
c906108c 11288 /* Default assumptions.
c5aa993b 11289 Use the passed type or decode it from the die. */
176620f1 11290 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11291 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11292 if (type != NULL)
11293 SYMBOL_TYPE (sym) = type;
11294 else
e7c27a73 11295 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11296 attr = dwarf2_attr (die,
11297 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11298 cu);
c906108c
SS
11299 if (attr)
11300 {
11301 SYMBOL_LINE (sym) = DW_UNSND (attr);
11302 }
cb1df416 11303
edb3359d
DJ
11304 attr = dwarf2_attr (die,
11305 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11306 cu);
cb1df416
DJ
11307 if (attr)
11308 {
11309 int file_index = DW_UNSND (attr);
9a619af0 11310
cb1df416
DJ
11311 if (cu->line_header == NULL
11312 || file_index > cu->line_header->num_file_names)
11313 complaint (&symfile_complaints,
11314 _("file index out of range"));
1c3d648d 11315 else if (file_index > 0)
cb1df416
DJ
11316 {
11317 struct file_entry *fe;
9a619af0 11318
cb1df416
DJ
11319 fe = &cu->line_header->file_names[file_index - 1];
11320 SYMBOL_SYMTAB (sym) = fe->symtab;
11321 }
11322 }
11323
c906108c
SS
11324 switch (die->tag)
11325 {
11326 case DW_TAG_label:
e142c38c 11327 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11328 if (attr)
11329 {
11330 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11331 }
0f5238ed
TT
11332 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11333 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11334 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11335 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11336 break;
11337 case DW_TAG_subprogram:
11338 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11339 finish_block. */
11340 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11341 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11342 if ((attr2 && (DW_UNSND (attr2) != 0))
11343 || cu->language == language_ada)
c906108c 11344 {
2cfa0c8d
JB
11345 /* Subprograms marked external are stored as a global symbol.
11346 Ada subprograms, whether marked external or not, are always
11347 stored as a global symbol, because we want to be able to
11348 access them globally. For instance, we want to be able
11349 to break on a nested subprogram without having to
11350 specify the context. */
e37fd15a 11351 list_to_add = &global_symbols;
c906108c
SS
11352 }
11353 else
11354 {
e37fd15a 11355 list_to_add = cu->list_in_scope;
c906108c
SS
11356 }
11357 break;
edb3359d
DJ
11358 case DW_TAG_inlined_subroutine:
11359 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11360 finish_block. */
11361 SYMBOL_CLASS (sym) = LOC_BLOCK;
11362 SYMBOL_INLINED (sym) = 1;
11363 /* Do not add the symbol to any lists. It will be found via
11364 BLOCK_FUNCTION from the blockvector. */
11365 break;
34eaf542
TT
11366 case DW_TAG_template_value_param:
11367 suppress_add = 1;
11368 /* Fall through. */
72929c62 11369 case DW_TAG_constant:
c906108c 11370 case DW_TAG_variable:
254e6b9e 11371 case DW_TAG_member:
0963b4bd
MS
11372 /* Compilation with minimal debug info may result in
11373 variables with missing type entries. Change the
11374 misleading `void' type to something sensible. */
c906108c 11375 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11376 SYMBOL_TYPE (sym)
46bf5051 11377 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11378
e142c38c 11379 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11380 /* In the case of DW_TAG_member, we should only be called for
11381 static const members. */
11382 if (die->tag == DW_TAG_member)
11383 {
3863f96c
DE
11384 /* dwarf2_add_field uses die_is_declaration,
11385 so we do the same. */
254e6b9e
DE
11386 gdb_assert (die_is_declaration (die, cu));
11387 gdb_assert (attr);
11388 }
c906108c
SS
11389 if (attr)
11390 {
e7c27a73 11391 dwarf2_const_value (attr, sym, cu);
e142c38c 11392 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11393 if (!suppress_add)
34eaf542
TT
11394 {
11395 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11396 list_to_add = &global_symbols;
34eaf542 11397 else
e37fd15a 11398 list_to_add = cu->list_in_scope;
34eaf542 11399 }
c906108c
SS
11400 break;
11401 }
e142c38c 11402 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11403 if (attr)
11404 {
e7c27a73 11405 var_decode_location (attr, sym, cu);
e142c38c 11406 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11407 if (SYMBOL_CLASS (sym) == LOC_STATIC
11408 && SYMBOL_VALUE_ADDRESS (sym) == 0
11409 && !dwarf2_per_objfile->has_section_at_zero)
11410 {
11411 /* When a static variable is eliminated by the linker,
11412 the corresponding debug information is not stripped
11413 out, but the variable address is set to null;
11414 do not add such variables into symbol table. */
11415 }
11416 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11417 {
f55ee35c
JK
11418 /* Workaround gfortran PR debug/40040 - it uses
11419 DW_AT_location for variables in -fPIC libraries which may
11420 get overriden by other libraries/executable and get
11421 a different address. Resolve it by the minimal symbol
11422 which may come from inferior's executable using copy
11423 relocation. Make this workaround only for gfortran as for
11424 other compilers GDB cannot guess the minimal symbol
11425 Fortran mangling kind. */
11426 if (cu->language == language_fortran && die->parent
11427 && die->parent->tag == DW_TAG_module
11428 && cu->producer
11429 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11430 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11431
1c809c68
TT
11432 /* A variable with DW_AT_external is never static,
11433 but it may be block-scoped. */
11434 list_to_add = (cu->list_in_scope == &file_symbols
11435 ? &global_symbols : cu->list_in_scope);
1c809c68 11436 }
c906108c 11437 else
e37fd15a 11438 list_to_add = cu->list_in_scope;
c906108c
SS
11439 }
11440 else
11441 {
11442 /* We do not know the address of this symbol.
c5aa993b
JM
11443 If it is an external symbol and we have type information
11444 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11445 The address of the variable will then be determined from
11446 the minimal symbol table whenever the variable is
11447 referenced. */
e142c38c 11448 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11449 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11450 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11451 {
0fe7935b
DJ
11452 /* A variable with DW_AT_external is never static, but it
11453 may be block-scoped. */
11454 list_to_add = (cu->list_in_scope == &file_symbols
11455 ? &global_symbols : cu->list_in_scope);
11456
c906108c 11457 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11458 }
442ddf59
JK
11459 else if (!die_is_declaration (die, cu))
11460 {
11461 /* Use the default LOC_OPTIMIZED_OUT class. */
11462 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11463 if (!suppress_add)
11464 list_to_add = cu->list_in_scope;
442ddf59 11465 }
c906108c
SS
11466 }
11467 break;
11468 case DW_TAG_formal_parameter:
edb3359d
DJ
11469 /* If we are inside a function, mark this as an argument. If
11470 not, we might be looking at an argument to an inlined function
11471 when we do not have enough information to show inlined frames;
11472 pretend it's a local variable in that case so that the user can
11473 still see it. */
11474 if (context_stack_depth > 0
11475 && context_stack[context_stack_depth - 1].name != NULL)
11476 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11477 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11478 if (attr)
11479 {
e7c27a73 11480 var_decode_location (attr, sym, cu);
c906108c 11481 }
e142c38c 11482 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11483 if (attr)
11484 {
e7c27a73 11485 dwarf2_const_value (attr, sym, cu);
c906108c 11486 }
f346a30d
PM
11487 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11488 if (attr && DW_UNSND (attr))
11489 {
11490 struct type *ref_type;
11491
11492 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11493 SYMBOL_TYPE (sym) = ref_type;
11494 }
11495
e37fd15a 11496 list_to_add = cu->list_in_scope;
c906108c
SS
11497 break;
11498 case DW_TAG_unspecified_parameters:
11499 /* From varargs functions; gdb doesn't seem to have any
11500 interest in this information, so just ignore it for now.
11501 (FIXME?) */
11502 break;
34eaf542
TT
11503 case DW_TAG_template_type_param:
11504 suppress_add = 1;
11505 /* Fall through. */
c906108c 11506 case DW_TAG_class_type:
680b30c7 11507 case DW_TAG_interface_type:
c906108c
SS
11508 case DW_TAG_structure_type:
11509 case DW_TAG_union_type:
72019c9c 11510 case DW_TAG_set_type:
c906108c
SS
11511 case DW_TAG_enumeration_type:
11512 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11513 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11514
63d06c5c 11515 {
987504bb 11516 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11517 really ever be static objects: otherwise, if you try
11518 to, say, break of a class's method and you're in a file
11519 which doesn't mention that class, it won't work unless
11520 the check for all static symbols in lookup_symbol_aux
11521 saves you. See the OtherFileClass tests in
11522 gdb.c++/namespace.exp. */
11523
e37fd15a 11524 if (!suppress_add)
34eaf542 11525 {
34eaf542
TT
11526 list_to_add = (cu->list_in_scope == &file_symbols
11527 && (cu->language == language_cplus
11528 || cu->language == language_java)
11529 ? &global_symbols : cu->list_in_scope);
63d06c5c 11530
64382290
TT
11531 /* The semantics of C++ state that "struct foo {
11532 ... }" also defines a typedef for "foo". A Java
11533 class declaration also defines a typedef for the
11534 class. */
11535 if (cu->language == language_cplus
11536 || cu->language == language_java
11537 || cu->language == language_ada)
11538 {
11539 /* The symbol's name is already allocated along
11540 with this objfile, so we don't need to
11541 duplicate it for the type. */
11542 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11543 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11544 }
63d06c5c
DC
11545 }
11546 }
c906108c
SS
11547 break;
11548 case DW_TAG_typedef:
63d06c5c
DC
11549 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11550 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11551 list_to_add = cu->list_in_scope;
63d06c5c 11552 break;
c906108c 11553 case DW_TAG_base_type:
a02abb62 11554 case DW_TAG_subrange_type:
c906108c 11555 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11556 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11557 list_to_add = cu->list_in_scope;
c906108c
SS
11558 break;
11559 case DW_TAG_enumerator:
e142c38c 11560 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11561 if (attr)
11562 {
e7c27a73 11563 dwarf2_const_value (attr, sym, cu);
c906108c 11564 }
63d06c5c
DC
11565 {
11566 /* NOTE: carlton/2003-11-10: See comment above in the
11567 DW_TAG_class_type, etc. block. */
11568
e142c38c 11569 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11570 && (cu->language == language_cplus
11571 || cu->language == language_java)
e142c38c 11572 ? &global_symbols : cu->list_in_scope);
63d06c5c 11573 }
c906108c 11574 break;
5c4e30ca
DC
11575 case DW_TAG_namespace:
11576 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11577 list_to_add = &global_symbols;
5c4e30ca 11578 break;
c906108c
SS
11579 default:
11580 /* Not a tag we recognize. Hopefully we aren't processing
11581 trash data, but since we must specifically ignore things
11582 we don't recognize, there is nothing else we should do at
0963b4bd 11583 this point. */
e2e0b3e5 11584 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11585 dwarf_tag_name (die->tag));
c906108c
SS
11586 break;
11587 }
df8a16a1 11588
e37fd15a
SW
11589 if (suppress_add)
11590 {
11591 sym->hash_next = objfile->template_symbols;
11592 objfile->template_symbols = sym;
11593 list_to_add = NULL;
11594 }
11595
11596 if (list_to_add != NULL)
11597 add_symbol_to_list (sym, list_to_add);
11598
df8a16a1
DJ
11599 /* For the benefit of old versions of GCC, check for anonymous
11600 namespaces based on the demangled name. */
11601 if (!processing_has_namespace_info
94af9270 11602 && cu->language == language_cplus)
df8a16a1 11603 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11604 }
11605 return (sym);
11606}
11607
34eaf542
TT
11608/* A wrapper for new_symbol_full that always allocates a new symbol. */
11609
11610static struct symbol *
11611new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11612{
11613 return new_symbol_full (die, type, cu, NULL);
11614}
11615
98bfdba5
PA
11616/* Given an attr with a DW_FORM_dataN value in host byte order,
11617 zero-extend it as appropriate for the symbol's type. The DWARF
11618 standard (v4) is not entirely clear about the meaning of using
11619 DW_FORM_dataN for a constant with a signed type, where the type is
11620 wider than the data. The conclusion of a discussion on the DWARF
11621 list was that this is unspecified. We choose to always zero-extend
11622 because that is the interpretation long in use by GCC. */
c906108c 11623
98bfdba5
PA
11624static gdb_byte *
11625dwarf2_const_value_data (struct attribute *attr, struct type *type,
11626 const char *name, struct obstack *obstack,
11627 struct dwarf2_cu *cu, long *value, int bits)
c906108c 11628{
e7c27a73 11629 struct objfile *objfile = cu->objfile;
e17a4113
UW
11630 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11631 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
11632 LONGEST l = DW_UNSND (attr);
11633
11634 if (bits < sizeof (*value) * 8)
11635 {
11636 l &= ((LONGEST) 1 << bits) - 1;
11637 *value = l;
11638 }
11639 else if (bits == sizeof (*value) * 8)
11640 *value = l;
11641 else
11642 {
11643 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11644 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11645 return bytes;
11646 }
11647
11648 return NULL;
11649}
11650
11651/* Read a constant value from an attribute. Either set *VALUE, or if
11652 the value does not fit in *VALUE, set *BYTES - either already
11653 allocated on the objfile obstack, or newly allocated on OBSTACK,
11654 or, set *BATON, if we translated the constant to a location
11655 expression. */
11656
11657static void
11658dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11659 const char *name, struct obstack *obstack,
11660 struct dwarf2_cu *cu,
11661 long *value, gdb_byte **bytes,
11662 struct dwarf2_locexpr_baton **baton)
11663{
11664 struct objfile *objfile = cu->objfile;
11665 struct comp_unit_head *cu_header = &cu->header;
c906108c 11666 struct dwarf_block *blk;
98bfdba5
PA
11667 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11668 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11669
11670 *value = 0;
11671 *bytes = NULL;
11672 *baton = NULL;
c906108c
SS
11673
11674 switch (attr->form)
11675 {
11676 case DW_FORM_addr:
ac56253d 11677 {
ac56253d
TT
11678 gdb_byte *data;
11679
98bfdba5
PA
11680 if (TYPE_LENGTH (type) != cu_header->addr_size)
11681 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 11682 cu_header->addr_size,
98bfdba5 11683 TYPE_LENGTH (type));
ac56253d
TT
11684 /* Symbols of this form are reasonably rare, so we just
11685 piggyback on the existing location code rather than writing
11686 a new implementation of symbol_computed_ops. */
98bfdba5
PA
11687 *baton = obstack_alloc (&objfile->objfile_obstack,
11688 sizeof (struct dwarf2_locexpr_baton));
11689 (*baton)->per_cu = cu->per_cu;
11690 gdb_assert ((*baton)->per_cu);
ac56253d 11691
98bfdba5
PA
11692 (*baton)->size = 2 + cu_header->addr_size;
11693 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11694 (*baton)->data = data;
ac56253d
TT
11695
11696 data[0] = DW_OP_addr;
11697 store_unsigned_integer (&data[1], cu_header->addr_size,
11698 byte_order, DW_ADDR (attr));
11699 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 11700 }
c906108c 11701 break;
4ac36638 11702 case DW_FORM_string:
93b5768b 11703 case DW_FORM_strp:
98bfdba5
PA
11704 /* DW_STRING is already allocated on the objfile obstack, point
11705 directly to it. */
11706 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 11707 break;
c906108c
SS
11708 case DW_FORM_block1:
11709 case DW_FORM_block2:
11710 case DW_FORM_block4:
11711 case DW_FORM_block:
2dc7f7b3 11712 case DW_FORM_exprloc:
c906108c 11713 blk = DW_BLOCK (attr);
98bfdba5
PA
11714 if (TYPE_LENGTH (type) != blk->size)
11715 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11716 TYPE_LENGTH (type));
11717 *bytes = blk->data;
c906108c 11718 break;
2df3850c
JM
11719
11720 /* The DW_AT_const_value attributes are supposed to carry the
11721 symbol's value "represented as it would be on the target
11722 architecture." By the time we get here, it's already been
11723 converted to host endianness, so we just need to sign- or
11724 zero-extend it as appropriate. */
11725 case DW_FORM_data1:
3e43a32a
MS
11726 *bytes = dwarf2_const_value_data (attr, type, name,
11727 obstack, cu, value, 8);
2df3850c 11728 break;
c906108c 11729 case DW_FORM_data2:
3e43a32a
MS
11730 *bytes = dwarf2_const_value_data (attr, type, name,
11731 obstack, cu, value, 16);
2df3850c 11732 break;
c906108c 11733 case DW_FORM_data4:
3e43a32a
MS
11734 *bytes = dwarf2_const_value_data (attr, type, name,
11735 obstack, cu, value, 32);
2df3850c 11736 break;
c906108c 11737 case DW_FORM_data8:
3e43a32a
MS
11738 *bytes = dwarf2_const_value_data (attr, type, name,
11739 obstack, cu, value, 64);
2df3850c
JM
11740 break;
11741
c906108c 11742 case DW_FORM_sdata:
98bfdba5 11743 *value = DW_SND (attr);
2df3850c
JM
11744 break;
11745
c906108c 11746 case DW_FORM_udata:
98bfdba5 11747 *value = DW_UNSND (attr);
c906108c 11748 break;
2df3850c 11749
c906108c 11750 default:
4d3c2250 11751 complaint (&symfile_complaints,
e2e0b3e5 11752 _("unsupported const value attribute form: '%s'"),
4d3c2250 11753 dwarf_form_name (attr->form));
98bfdba5 11754 *value = 0;
c906108c
SS
11755 break;
11756 }
11757}
11758
2df3850c 11759
98bfdba5
PA
11760/* Copy constant value from an attribute to a symbol. */
11761
2df3850c 11762static void
98bfdba5
PA
11763dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11764 struct dwarf2_cu *cu)
2df3850c 11765{
98bfdba5
PA
11766 struct objfile *objfile = cu->objfile;
11767 struct comp_unit_head *cu_header = &cu->header;
11768 long value;
11769 gdb_byte *bytes;
11770 struct dwarf2_locexpr_baton *baton;
2df3850c 11771
98bfdba5
PA
11772 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11773 SYMBOL_PRINT_NAME (sym),
11774 &objfile->objfile_obstack, cu,
11775 &value, &bytes, &baton);
2df3850c 11776
98bfdba5
PA
11777 if (baton != NULL)
11778 {
11779 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11780 SYMBOL_LOCATION_BATON (sym) = baton;
11781 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11782 }
11783 else if (bytes != NULL)
11784 {
11785 SYMBOL_VALUE_BYTES (sym) = bytes;
11786 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11787 }
11788 else
11789 {
11790 SYMBOL_VALUE (sym) = value;
11791 SYMBOL_CLASS (sym) = LOC_CONST;
11792 }
2df3850c
JM
11793}
11794
c906108c
SS
11795/* Return the type of the die in question using its DW_AT_type attribute. */
11796
11797static struct type *
e7c27a73 11798die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11799{
c906108c 11800 struct attribute *type_attr;
c906108c 11801
e142c38c 11802 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
11803 if (!type_attr)
11804 {
11805 /* A missing DW_AT_type represents a void type. */
46bf5051 11806 return objfile_type (cu->objfile)->builtin_void;
c906108c 11807 }
348e048f 11808
673bfd45 11809 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11810}
11811
b4ba55a1
JB
11812/* True iff CU's producer generates GNAT Ada auxiliary information
11813 that allows to find parallel types through that information instead
11814 of having to do expensive parallel lookups by type name. */
11815
11816static int
11817need_gnat_info (struct dwarf2_cu *cu)
11818{
11819 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11820 of GNAT produces this auxiliary information, without any indication
11821 that it is produced. Part of enhancing the FSF version of GNAT
11822 to produce that information will be to put in place an indicator
11823 that we can use in order to determine whether the descriptive type
11824 info is available or not. One suggestion that has been made is
11825 to use a new attribute, attached to the CU die. For now, assume
11826 that the descriptive type info is not available. */
11827 return 0;
11828}
11829
b4ba55a1
JB
11830/* Return the auxiliary type of the die in question using its
11831 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11832 attribute is not present. */
11833
11834static struct type *
11835die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11836{
b4ba55a1 11837 struct attribute *type_attr;
b4ba55a1
JB
11838
11839 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11840 if (!type_attr)
11841 return NULL;
11842
673bfd45 11843 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
11844}
11845
11846/* If DIE has a descriptive_type attribute, then set the TYPE's
11847 descriptive type accordingly. */
11848
11849static void
11850set_descriptive_type (struct type *type, struct die_info *die,
11851 struct dwarf2_cu *cu)
11852{
11853 struct type *descriptive_type = die_descriptive_type (die, cu);
11854
11855 if (descriptive_type)
11856 {
11857 ALLOCATE_GNAT_AUX_TYPE (type);
11858 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11859 }
11860}
11861
c906108c
SS
11862/* Return the containing type of the die in question using its
11863 DW_AT_containing_type attribute. */
11864
11865static struct type *
e7c27a73 11866die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11867{
c906108c 11868 struct attribute *type_attr;
c906108c 11869
e142c38c 11870 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
11871 if (!type_attr)
11872 error (_("Dwarf Error: Problem turning containing type into gdb type "
11873 "[in module %s]"), cu->objfile->name);
11874
673bfd45 11875 return lookup_die_type (die, type_attr, cu);
c906108c
SS
11876}
11877
673bfd45
DE
11878/* Look up the type of DIE in CU using its type attribute ATTR.
11879 If there is no type substitute an error marker. */
11880
c906108c 11881static struct type *
673bfd45
DE
11882lookup_die_type (struct die_info *die, struct attribute *attr,
11883 struct dwarf2_cu *cu)
c906108c 11884{
f792889a
DJ
11885 struct type *this_type;
11886
673bfd45
DE
11887 /* First see if we have it cached. */
11888
11889 if (is_ref_attr (attr))
11890 {
11891 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11892
11893 this_type = get_die_type_at_offset (offset, cu->per_cu);
11894 }
55f1336d 11895 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
11896 {
11897 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11898 struct dwarf2_cu *sig_cu;
11899 unsigned int offset;
11900
11901 /* sig_type will be NULL if the signatured type is missing from
11902 the debug info. */
11903 if (sig_type == NULL)
11904 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11905 "at 0x%x [in module %s]"),
11906 die->offset, cu->objfile->name);
11907
11908 gdb_assert (sig_type->per_cu.from_debug_types);
b3c8eb43 11909 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
11910 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11911 }
11912 else
11913 {
11914 dump_die_for_error (die);
11915 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11916 dwarf_attr_name (attr->name), cu->objfile->name);
11917 }
11918
11919 /* If not cached we need to read it in. */
11920
11921 if (this_type == NULL)
11922 {
11923 struct die_info *type_die;
11924 struct dwarf2_cu *type_cu = cu;
11925
11926 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11927 /* If the type is cached, we should have found it above. */
11928 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11929 this_type = read_type_die_1 (type_die, type_cu);
11930 }
11931
11932 /* If we still don't have a type use an error marker. */
11933
11934 if (this_type == NULL)
c906108c 11935 {
b00fdb78
TT
11936 char *message, *saved;
11937
11938 /* read_type_die already issued a complaint. */
11939 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11940 cu->objfile->name,
11941 cu->header.offset,
11942 die->offset);
11943 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11944 message, strlen (message));
11945 xfree (message);
11946
11947 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 11948 }
673bfd45 11949
f792889a 11950 return this_type;
c906108c
SS
11951}
11952
673bfd45
DE
11953/* Return the type in DIE, CU.
11954 Returns NULL for invalid types.
11955
11956 This first does a lookup in the appropriate type_hash table,
11957 and only reads the die in if necessary.
11958
11959 NOTE: This can be called when reading in partial or full symbols. */
11960
f792889a 11961static struct type *
e7c27a73 11962read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11963{
f792889a
DJ
11964 struct type *this_type;
11965
11966 this_type = get_die_type (die, cu);
11967 if (this_type)
11968 return this_type;
11969
673bfd45
DE
11970 return read_type_die_1 (die, cu);
11971}
11972
11973/* Read the type in DIE, CU.
11974 Returns NULL for invalid types. */
11975
11976static struct type *
11977read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11978{
11979 struct type *this_type = NULL;
11980
c906108c
SS
11981 switch (die->tag)
11982 {
11983 case DW_TAG_class_type:
680b30c7 11984 case DW_TAG_interface_type:
c906108c
SS
11985 case DW_TAG_structure_type:
11986 case DW_TAG_union_type:
f792889a 11987 this_type = read_structure_type (die, cu);
c906108c
SS
11988 break;
11989 case DW_TAG_enumeration_type:
f792889a 11990 this_type = read_enumeration_type (die, cu);
c906108c
SS
11991 break;
11992 case DW_TAG_subprogram:
11993 case DW_TAG_subroutine_type:
edb3359d 11994 case DW_TAG_inlined_subroutine:
f792889a 11995 this_type = read_subroutine_type (die, cu);
c906108c
SS
11996 break;
11997 case DW_TAG_array_type:
f792889a 11998 this_type = read_array_type (die, cu);
c906108c 11999 break;
72019c9c 12000 case DW_TAG_set_type:
f792889a 12001 this_type = read_set_type (die, cu);
72019c9c 12002 break;
c906108c 12003 case DW_TAG_pointer_type:
f792889a 12004 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12005 break;
12006 case DW_TAG_ptr_to_member_type:
f792889a 12007 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12008 break;
12009 case DW_TAG_reference_type:
f792889a 12010 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12011 break;
12012 case DW_TAG_const_type:
f792889a 12013 this_type = read_tag_const_type (die, cu);
c906108c
SS
12014 break;
12015 case DW_TAG_volatile_type:
f792889a 12016 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12017 break;
12018 case DW_TAG_string_type:
f792889a 12019 this_type = read_tag_string_type (die, cu);
c906108c
SS
12020 break;
12021 case DW_TAG_typedef:
f792889a 12022 this_type = read_typedef (die, cu);
c906108c 12023 break;
a02abb62 12024 case DW_TAG_subrange_type:
f792889a 12025 this_type = read_subrange_type (die, cu);
a02abb62 12026 break;
c906108c 12027 case DW_TAG_base_type:
f792889a 12028 this_type = read_base_type (die, cu);
c906108c 12029 break;
81a17f79 12030 case DW_TAG_unspecified_type:
f792889a 12031 this_type = read_unspecified_type (die, cu);
81a17f79 12032 break;
0114d602
DJ
12033 case DW_TAG_namespace:
12034 this_type = read_namespace_type (die, cu);
12035 break;
f55ee35c
JK
12036 case DW_TAG_module:
12037 this_type = read_module_type (die, cu);
12038 break;
c906108c 12039 default:
3e43a32a
MS
12040 complaint (&symfile_complaints,
12041 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12042 dwarf_tag_name (die->tag));
c906108c
SS
12043 break;
12044 }
63d06c5c 12045
f792889a 12046 return this_type;
63d06c5c
DC
12047}
12048
abc72ce4
DE
12049/* See if we can figure out if the class lives in a namespace. We do
12050 this by looking for a member function; its demangled name will
12051 contain namespace info, if there is any.
12052 Return the computed name or NULL.
12053 Space for the result is allocated on the objfile's obstack.
12054 This is the full-die version of guess_partial_die_structure_name.
12055 In this case we know DIE has no useful parent. */
12056
12057static char *
12058guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12059{
12060 struct die_info *spec_die;
12061 struct dwarf2_cu *spec_cu;
12062 struct die_info *child;
12063
12064 spec_cu = cu;
12065 spec_die = die_specification (die, &spec_cu);
12066 if (spec_die != NULL)
12067 {
12068 die = spec_die;
12069 cu = spec_cu;
12070 }
12071
12072 for (child = die->child;
12073 child != NULL;
12074 child = child->sibling)
12075 {
12076 if (child->tag == DW_TAG_subprogram)
12077 {
12078 struct attribute *attr;
12079
12080 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12081 if (attr == NULL)
12082 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12083 if (attr != NULL)
12084 {
12085 char *actual_name
12086 = language_class_name_from_physname (cu->language_defn,
12087 DW_STRING (attr));
12088 char *name = NULL;
12089
12090 if (actual_name != NULL)
12091 {
12092 char *die_name = dwarf2_name (die, cu);
12093
12094 if (die_name != NULL
12095 && strcmp (die_name, actual_name) != 0)
12096 {
12097 /* Strip off the class name from the full name.
12098 We want the prefix. */
12099 int die_name_len = strlen (die_name);
12100 int actual_name_len = strlen (actual_name);
12101
12102 /* Test for '::' as a sanity check. */
12103 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12104 && actual_name[actual_name_len
12105 - die_name_len - 1] == ':')
abc72ce4
DE
12106 name =
12107 obsavestring (actual_name,
12108 actual_name_len - die_name_len - 2,
12109 &cu->objfile->objfile_obstack);
12110 }
12111 }
12112 xfree (actual_name);
12113 return name;
12114 }
12115 }
12116 }
12117
12118 return NULL;
12119}
12120
fdde2d81 12121/* Return the name of the namespace/class that DIE is defined within,
0114d602 12122 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12123
0114d602
DJ
12124 For example, if we're within the method foo() in the following
12125 code:
12126
12127 namespace N {
12128 class C {
12129 void foo () {
12130 }
12131 };
12132 }
12133
12134 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12135
12136static char *
e142c38c 12137determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12138{
0114d602
DJ
12139 struct die_info *parent, *spec_die;
12140 struct dwarf2_cu *spec_cu;
12141 struct type *parent_type;
63d06c5c 12142
f55ee35c
JK
12143 if (cu->language != language_cplus && cu->language != language_java
12144 && cu->language != language_fortran)
0114d602
DJ
12145 return "";
12146
12147 /* We have to be careful in the presence of DW_AT_specification.
12148 For example, with GCC 3.4, given the code
12149
12150 namespace N {
12151 void foo() {
12152 // Definition of N::foo.
12153 }
12154 }
12155
12156 then we'll have a tree of DIEs like this:
12157
12158 1: DW_TAG_compile_unit
12159 2: DW_TAG_namespace // N
12160 3: DW_TAG_subprogram // declaration of N::foo
12161 4: DW_TAG_subprogram // definition of N::foo
12162 DW_AT_specification // refers to die #3
12163
12164 Thus, when processing die #4, we have to pretend that we're in
12165 the context of its DW_AT_specification, namely the contex of die
12166 #3. */
12167 spec_cu = cu;
12168 spec_die = die_specification (die, &spec_cu);
12169 if (spec_die == NULL)
12170 parent = die->parent;
12171 else
63d06c5c 12172 {
0114d602
DJ
12173 parent = spec_die->parent;
12174 cu = spec_cu;
63d06c5c 12175 }
0114d602
DJ
12176
12177 if (parent == NULL)
12178 return "";
98bfdba5
PA
12179 else if (parent->building_fullname)
12180 {
12181 const char *name;
12182 const char *parent_name;
12183
12184 /* It has been seen on RealView 2.2 built binaries,
12185 DW_TAG_template_type_param types actually _defined_ as
12186 children of the parent class:
12187
12188 enum E {};
12189 template class <class Enum> Class{};
12190 Class<enum E> class_e;
12191
12192 1: DW_TAG_class_type (Class)
12193 2: DW_TAG_enumeration_type (E)
12194 3: DW_TAG_enumerator (enum1:0)
12195 3: DW_TAG_enumerator (enum2:1)
12196 ...
12197 2: DW_TAG_template_type_param
12198 DW_AT_type DW_FORM_ref_udata (E)
12199
12200 Besides being broken debug info, it can put GDB into an
12201 infinite loop. Consider:
12202
12203 When we're building the full name for Class<E>, we'll start
12204 at Class, and go look over its template type parameters,
12205 finding E. We'll then try to build the full name of E, and
12206 reach here. We're now trying to build the full name of E,
12207 and look over the parent DIE for containing scope. In the
12208 broken case, if we followed the parent DIE of E, we'd again
12209 find Class, and once again go look at its template type
12210 arguments, etc., etc. Simply don't consider such parent die
12211 as source-level parent of this die (it can't be, the language
12212 doesn't allow it), and break the loop here. */
12213 name = dwarf2_name (die, cu);
12214 parent_name = dwarf2_name (parent, cu);
12215 complaint (&symfile_complaints,
12216 _("template param type '%s' defined within parent '%s'"),
12217 name ? name : "<unknown>",
12218 parent_name ? parent_name : "<unknown>");
12219 return "";
12220 }
63d06c5c 12221 else
0114d602
DJ
12222 switch (parent->tag)
12223 {
63d06c5c 12224 case DW_TAG_namespace:
0114d602 12225 parent_type = read_type_die (parent, cu);
acebe513
UW
12226 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12227 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12228 Work around this problem here. */
12229 if (cu->language == language_cplus
12230 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12231 return "";
0114d602
DJ
12232 /* We give a name to even anonymous namespaces. */
12233 return TYPE_TAG_NAME (parent_type);
63d06c5c 12234 case DW_TAG_class_type:
680b30c7 12235 case DW_TAG_interface_type:
63d06c5c 12236 case DW_TAG_structure_type:
0114d602 12237 case DW_TAG_union_type:
f55ee35c 12238 case DW_TAG_module:
0114d602
DJ
12239 parent_type = read_type_die (parent, cu);
12240 if (TYPE_TAG_NAME (parent_type) != NULL)
12241 return TYPE_TAG_NAME (parent_type);
12242 else
12243 /* An anonymous structure is only allowed non-static data
12244 members; no typedefs, no member functions, et cetera.
12245 So it does not need a prefix. */
12246 return "";
abc72ce4
DE
12247 case DW_TAG_compile_unit:
12248 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12249 if (cu->language == language_cplus
12250 && dwarf2_per_objfile->types.asection != NULL
12251 && die->child != NULL
12252 && (die->tag == DW_TAG_class_type
12253 || die->tag == DW_TAG_structure_type
12254 || die->tag == DW_TAG_union_type))
12255 {
12256 char *name = guess_full_die_structure_name (die, cu);
12257 if (name != NULL)
12258 return name;
12259 }
12260 return "";
63d06c5c 12261 default:
8176b9b8 12262 return determine_prefix (parent, cu);
63d06c5c 12263 }
63d06c5c
DC
12264}
12265
3e43a32a
MS
12266/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12267 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12268 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12269 an obconcat, otherwise allocate storage for the result. The CU argument is
12270 used to determine the language and hence, the appropriate separator. */
987504bb 12271
f55ee35c 12272#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12273
12274static char *
f55ee35c
JK
12275typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12276 int physname, struct dwarf2_cu *cu)
63d06c5c 12277{
f55ee35c 12278 const char *lead = "";
5c315b68 12279 const char *sep;
63d06c5c 12280
3e43a32a
MS
12281 if (suffix == NULL || suffix[0] == '\0'
12282 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12283 sep = "";
12284 else if (cu->language == language_java)
12285 sep = ".";
f55ee35c
JK
12286 else if (cu->language == language_fortran && physname)
12287 {
12288 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12289 DW_AT_MIPS_linkage_name is preferred and used instead. */
12290
12291 lead = "__";
12292 sep = "_MOD_";
12293 }
987504bb
JJ
12294 else
12295 sep = "::";
63d06c5c 12296
6dd47d34
DE
12297 if (prefix == NULL)
12298 prefix = "";
12299 if (suffix == NULL)
12300 suffix = "";
12301
987504bb
JJ
12302 if (obs == NULL)
12303 {
3e43a32a
MS
12304 char *retval
12305 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12306
f55ee35c
JK
12307 strcpy (retval, lead);
12308 strcat (retval, prefix);
6dd47d34
DE
12309 strcat (retval, sep);
12310 strcat (retval, suffix);
63d06c5c
DC
12311 return retval;
12312 }
987504bb
JJ
12313 else
12314 {
12315 /* We have an obstack. */
f55ee35c 12316 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12317 }
63d06c5c
DC
12318}
12319
c906108c
SS
12320/* Return sibling of die, NULL if no sibling. */
12321
f9aca02d 12322static struct die_info *
fba45db2 12323sibling_die (struct die_info *die)
c906108c 12324{
639d11d3 12325 return die->sibling;
c906108c
SS
12326}
12327
71c25dea
TT
12328/* Get name of a die, return NULL if not found. */
12329
12330static char *
12331dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12332 struct obstack *obstack)
12333{
12334 if (name && cu->language == language_cplus)
12335 {
12336 char *canon_name = cp_canonicalize_string (name);
12337
12338 if (canon_name != NULL)
12339 {
12340 if (strcmp (canon_name, name) != 0)
12341 name = obsavestring (canon_name, strlen (canon_name),
12342 obstack);
12343 xfree (canon_name);
12344 }
12345 }
12346
12347 return name;
c906108c
SS
12348}
12349
9219021c
DC
12350/* Get name of a die, return NULL if not found. */
12351
12352static char *
e142c38c 12353dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12354{
12355 struct attribute *attr;
12356
e142c38c 12357 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12358 if ((!attr || !DW_STRING (attr))
12359 && die->tag != DW_TAG_class_type
12360 && die->tag != DW_TAG_interface_type
12361 && die->tag != DW_TAG_structure_type
12362 && die->tag != DW_TAG_union_type)
71c25dea
TT
12363 return NULL;
12364
12365 switch (die->tag)
12366 {
12367 case DW_TAG_compile_unit:
12368 /* Compilation units have a DW_AT_name that is a filename, not
12369 a source language identifier. */
12370 case DW_TAG_enumeration_type:
12371 case DW_TAG_enumerator:
12372 /* These tags always have simple identifiers already; no need
12373 to canonicalize them. */
12374 return DW_STRING (attr);
907af001 12375
418835cc
KS
12376 case DW_TAG_subprogram:
12377 /* Java constructors will all be named "<init>", so return
12378 the class name when we see this special case. */
12379 if (cu->language == language_java
12380 && DW_STRING (attr) != NULL
12381 && strcmp (DW_STRING (attr), "<init>") == 0)
12382 {
12383 struct dwarf2_cu *spec_cu = cu;
12384 struct die_info *spec_die;
12385
12386 /* GCJ will output '<init>' for Java constructor names.
12387 For this special case, return the name of the parent class. */
12388
12389 /* GCJ may output suprogram DIEs with AT_specification set.
12390 If so, use the name of the specified DIE. */
12391 spec_die = die_specification (die, &spec_cu);
12392 if (spec_die != NULL)
12393 return dwarf2_name (spec_die, spec_cu);
12394
12395 do
12396 {
12397 die = die->parent;
12398 if (die->tag == DW_TAG_class_type)
12399 return dwarf2_name (die, cu);
12400 }
12401 while (die->tag != DW_TAG_compile_unit);
12402 }
907af001
UW
12403 break;
12404
12405 case DW_TAG_class_type:
12406 case DW_TAG_interface_type:
12407 case DW_TAG_structure_type:
12408 case DW_TAG_union_type:
12409 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12410 structures or unions. These were of the form "._%d" in GCC 4.1,
12411 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12412 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12413 if (attr && DW_STRING (attr)
12414 && (strncmp (DW_STRING (attr), "._", 2) == 0
12415 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12416 return NULL;
53832f31
TT
12417
12418 /* GCC might emit a nameless typedef that has a linkage name. See
12419 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12420 if (!attr || DW_STRING (attr) == NULL)
12421 {
df5c6c50 12422 char *demangled = NULL;
53832f31
TT
12423
12424 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12425 if (attr == NULL)
12426 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12427
12428 if (attr == NULL || DW_STRING (attr) == NULL)
12429 return NULL;
12430
df5c6c50
JK
12431 /* Avoid demangling DW_STRING (attr) the second time on a second
12432 call for the same DIE. */
12433 if (!DW_STRING_IS_CANONICAL (attr))
12434 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12435
12436 if (demangled)
12437 {
12438 /* FIXME: we already did this for the partial symbol... */
12439 DW_STRING (attr)
12440 = obsavestring (demangled, strlen (demangled),
12441 &cu->objfile->objfile_obstack);
12442 DW_STRING_IS_CANONICAL (attr) = 1;
12443 xfree (demangled);
12444 }
12445 }
907af001
UW
12446 break;
12447
71c25dea 12448 default:
907af001
UW
12449 break;
12450 }
12451
12452 if (!DW_STRING_IS_CANONICAL (attr))
12453 {
12454 DW_STRING (attr)
12455 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12456 &cu->objfile->objfile_obstack);
12457 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12458 }
907af001 12459 return DW_STRING (attr);
9219021c
DC
12460}
12461
12462/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12463 is none. *EXT_CU is the CU containing DIE on input, and the CU
12464 containing the return value on output. */
9219021c
DC
12465
12466static struct die_info *
f2f0e013 12467dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12468{
12469 struct attribute *attr;
9219021c 12470
f2f0e013 12471 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12472 if (attr == NULL)
12473 return NULL;
12474
f2f0e013 12475 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12476}
12477
c906108c
SS
12478/* Convert a DIE tag into its string name. */
12479
12480static char *
aa1ee363 12481dwarf_tag_name (unsigned tag)
c906108c
SS
12482{
12483 switch (tag)
12484 {
12485 case DW_TAG_padding:
12486 return "DW_TAG_padding";
12487 case DW_TAG_array_type:
12488 return "DW_TAG_array_type";
12489 case DW_TAG_class_type:
12490 return "DW_TAG_class_type";
12491 case DW_TAG_entry_point:
12492 return "DW_TAG_entry_point";
12493 case DW_TAG_enumeration_type:
12494 return "DW_TAG_enumeration_type";
12495 case DW_TAG_formal_parameter:
12496 return "DW_TAG_formal_parameter";
12497 case DW_TAG_imported_declaration:
12498 return "DW_TAG_imported_declaration";
12499 case DW_TAG_label:
12500 return "DW_TAG_label";
12501 case DW_TAG_lexical_block:
12502 return "DW_TAG_lexical_block";
12503 case DW_TAG_member:
12504 return "DW_TAG_member";
12505 case DW_TAG_pointer_type:
12506 return "DW_TAG_pointer_type";
12507 case DW_TAG_reference_type:
12508 return "DW_TAG_reference_type";
12509 case DW_TAG_compile_unit:
12510 return "DW_TAG_compile_unit";
12511 case DW_TAG_string_type:
12512 return "DW_TAG_string_type";
12513 case DW_TAG_structure_type:
12514 return "DW_TAG_structure_type";
12515 case DW_TAG_subroutine_type:
12516 return "DW_TAG_subroutine_type";
12517 case DW_TAG_typedef:
12518 return "DW_TAG_typedef";
12519 case DW_TAG_union_type:
12520 return "DW_TAG_union_type";
12521 case DW_TAG_unspecified_parameters:
12522 return "DW_TAG_unspecified_parameters";
12523 case DW_TAG_variant:
12524 return "DW_TAG_variant";
12525 case DW_TAG_common_block:
12526 return "DW_TAG_common_block";
12527 case DW_TAG_common_inclusion:
12528 return "DW_TAG_common_inclusion";
12529 case DW_TAG_inheritance:
12530 return "DW_TAG_inheritance";
12531 case DW_TAG_inlined_subroutine:
12532 return "DW_TAG_inlined_subroutine";
12533 case DW_TAG_module:
12534 return "DW_TAG_module";
12535 case DW_TAG_ptr_to_member_type:
12536 return "DW_TAG_ptr_to_member_type";
12537 case DW_TAG_set_type:
12538 return "DW_TAG_set_type";
12539 case DW_TAG_subrange_type:
12540 return "DW_TAG_subrange_type";
12541 case DW_TAG_with_stmt:
12542 return "DW_TAG_with_stmt";
12543 case DW_TAG_access_declaration:
12544 return "DW_TAG_access_declaration";
12545 case DW_TAG_base_type:
12546 return "DW_TAG_base_type";
12547 case DW_TAG_catch_block:
12548 return "DW_TAG_catch_block";
12549 case DW_TAG_const_type:
12550 return "DW_TAG_const_type";
12551 case DW_TAG_constant:
12552 return "DW_TAG_constant";
12553 case DW_TAG_enumerator:
12554 return "DW_TAG_enumerator";
12555 case DW_TAG_file_type:
12556 return "DW_TAG_file_type";
12557 case DW_TAG_friend:
12558 return "DW_TAG_friend";
12559 case DW_TAG_namelist:
12560 return "DW_TAG_namelist";
12561 case DW_TAG_namelist_item:
12562 return "DW_TAG_namelist_item";
12563 case DW_TAG_packed_type:
12564 return "DW_TAG_packed_type";
12565 case DW_TAG_subprogram:
12566 return "DW_TAG_subprogram";
12567 case DW_TAG_template_type_param:
12568 return "DW_TAG_template_type_param";
12569 case DW_TAG_template_value_param:
12570 return "DW_TAG_template_value_param";
12571 case DW_TAG_thrown_type:
12572 return "DW_TAG_thrown_type";
12573 case DW_TAG_try_block:
12574 return "DW_TAG_try_block";
12575 case DW_TAG_variant_part:
12576 return "DW_TAG_variant_part";
12577 case DW_TAG_variable:
12578 return "DW_TAG_variable";
12579 case DW_TAG_volatile_type:
12580 return "DW_TAG_volatile_type";
d9fa45fe
DC
12581 case DW_TAG_dwarf_procedure:
12582 return "DW_TAG_dwarf_procedure";
12583 case DW_TAG_restrict_type:
12584 return "DW_TAG_restrict_type";
12585 case DW_TAG_interface_type:
12586 return "DW_TAG_interface_type";
12587 case DW_TAG_namespace:
12588 return "DW_TAG_namespace";
12589 case DW_TAG_imported_module:
12590 return "DW_TAG_imported_module";
12591 case DW_TAG_unspecified_type:
12592 return "DW_TAG_unspecified_type";
12593 case DW_TAG_partial_unit:
12594 return "DW_TAG_partial_unit";
12595 case DW_TAG_imported_unit:
12596 return "DW_TAG_imported_unit";
b7619582
GF
12597 case DW_TAG_condition:
12598 return "DW_TAG_condition";
12599 case DW_TAG_shared_type:
12600 return "DW_TAG_shared_type";
348e048f
DE
12601 case DW_TAG_type_unit:
12602 return "DW_TAG_type_unit";
c906108c
SS
12603 case DW_TAG_MIPS_loop:
12604 return "DW_TAG_MIPS_loop";
b7619582
GF
12605 case DW_TAG_HP_array_descriptor:
12606 return "DW_TAG_HP_array_descriptor";
c906108c
SS
12607 case DW_TAG_format_label:
12608 return "DW_TAG_format_label";
12609 case DW_TAG_function_template:
12610 return "DW_TAG_function_template";
12611 case DW_TAG_class_template:
12612 return "DW_TAG_class_template";
b7619582
GF
12613 case DW_TAG_GNU_BINCL:
12614 return "DW_TAG_GNU_BINCL";
12615 case DW_TAG_GNU_EINCL:
12616 return "DW_TAG_GNU_EINCL";
12617 case DW_TAG_upc_shared_type:
12618 return "DW_TAG_upc_shared_type";
12619 case DW_TAG_upc_strict_type:
12620 return "DW_TAG_upc_strict_type";
12621 case DW_TAG_upc_relaxed_type:
12622 return "DW_TAG_upc_relaxed_type";
12623 case DW_TAG_PGI_kanji_type:
12624 return "DW_TAG_PGI_kanji_type";
12625 case DW_TAG_PGI_interface_block:
12626 return "DW_TAG_PGI_interface_block";
c906108c
SS
12627 default:
12628 return "DW_TAG_<unknown>";
12629 }
12630}
12631
12632/* Convert a DWARF attribute code into its string name. */
12633
12634static char *
aa1ee363 12635dwarf_attr_name (unsigned attr)
c906108c
SS
12636{
12637 switch (attr)
12638 {
12639 case DW_AT_sibling:
12640 return "DW_AT_sibling";
12641 case DW_AT_location:
12642 return "DW_AT_location";
12643 case DW_AT_name:
12644 return "DW_AT_name";
12645 case DW_AT_ordering:
12646 return "DW_AT_ordering";
12647 case DW_AT_subscr_data:
12648 return "DW_AT_subscr_data";
12649 case DW_AT_byte_size:
12650 return "DW_AT_byte_size";
12651 case DW_AT_bit_offset:
12652 return "DW_AT_bit_offset";
12653 case DW_AT_bit_size:
12654 return "DW_AT_bit_size";
12655 case DW_AT_element_list:
12656 return "DW_AT_element_list";
12657 case DW_AT_stmt_list:
12658 return "DW_AT_stmt_list";
12659 case DW_AT_low_pc:
12660 return "DW_AT_low_pc";
12661 case DW_AT_high_pc:
12662 return "DW_AT_high_pc";
12663 case DW_AT_language:
12664 return "DW_AT_language";
12665 case DW_AT_member:
12666 return "DW_AT_member";
12667 case DW_AT_discr:
12668 return "DW_AT_discr";
12669 case DW_AT_discr_value:
12670 return "DW_AT_discr_value";
12671 case DW_AT_visibility:
12672 return "DW_AT_visibility";
12673 case DW_AT_import:
12674 return "DW_AT_import";
12675 case DW_AT_string_length:
12676 return "DW_AT_string_length";
12677 case DW_AT_common_reference:
12678 return "DW_AT_common_reference";
12679 case DW_AT_comp_dir:
12680 return "DW_AT_comp_dir";
12681 case DW_AT_const_value:
12682 return "DW_AT_const_value";
12683 case DW_AT_containing_type:
12684 return "DW_AT_containing_type";
12685 case DW_AT_default_value:
12686 return "DW_AT_default_value";
12687 case DW_AT_inline:
12688 return "DW_AT_inline";
12689 case DW_AT_is_optional:
12690 return "DW_AT_is_optional";
12691 case DW_AT_lower_bound:
12692 return "DW_AT_lower_bound";
12693 case DW_AT_producer:
12694 return "DW_AT_producer";
12695 case DW_AT_prototyped:
12696 return "DW_AT_prototyped";
12697 case DW_AT_return_addr:
12698 return "DW_AT_return_addr";
12699 case DW_AT_start_scope:
12700 return "DW_AT_start_scope";
09fa0d7c
JK
12701 case DW_AT_bit_stride:
12702 return "DW_AT_bit_stride";
c906108c
SS
12703 case DW_AT_upper_bound:
12704 return "DW_AT_upper_bound";
12705 case DW_AT_abstract_origin:
12706 return "DW_AT_abstract_origin";
12707 case DW_AT_accessibility:
12708 return "DW_AT_accessibility";
12709 case DW_AT_address_class:
12710 return "DW_AT_address_class";
12711 case DW_AT_artificial:
12712 return "DW_AT_artificial";
12713 case DW_AT_base_types:
12714 return "DW_AT_base_types";
12715 case DW_AT_calling_convention:
12716 return "DW_AT_calling_convention";
12717 case DW_AT_count:
12718 return "DW_AT_count";
12719 case DW_AT_data_member_location:
12720 return "DW_AT_data_member_location";
12721 case DW_AT_decl_column:
12722 return "DW_AT_decl_column";
12723 case DW_AT_decl_file:
12724 return "DW_AT_decl_file";
12725 case DW_AT_decl_line:
12726 return "DW_AT_decl_line";
12727 case DW_AT_declaration:
12728 return "DW_AT_declaration";
12729 case DW_AT_discr_list:
12730 return "DW_AT_discr_list";
12731 case DW_AT_encoding:
12732 return "DW_AT_encoding";
12733 case DW_AT_external:
12734 return "DW_AT_external";
12735 case DW_AT_frame_base:
12736 return "DW_AT_frame_base";
12737 case DW_AT_friend:
12738 return "DW_AT_friend";
12739 case DW_AT_identifier_case:
12740 return "DW_AT_identifier_case";
12741 case DW_AT_macro_info:
12742 return "DW_AT_macro_info";
12743 case DW_AT_namelist_items:
12744 return "DW_AT_namelist_items";
12745 case DW_AT_priority:
12746 return "DW_AT_priority";
12747 case DW_AT_segment:
12748 return "DW_AT_segment";
12749 case DW_AT_specification:
12750 return "DW_AT_specification";
12751 case DW_AT_static_link:
12752 return "DW_AT_static_link";
12753 case DW_AT_type:
12754 return "DW_AT_type";
12755 case DW_AT_use_location:
12756 return "DW_AT_use_location";
12757 case DW_AT_variable_parameter:
12758 return "DW_AT_variable_parameter";
12759 case DW_AT_virtuality:
12760 return "DW_AT_virtuality";
12761 case DW_AT_vtable_elem_location:
12762 return "DW_AT_vtable_elem_location";
b7619582 12763 /* DWARF 3 values. */
d9fa45fe
DC
12764 case DW_AT_allocated:
12765 return "DW_AT_allocated";
12766 case DW_AT_associated:
12767 return "DW_AT_associated";
12768 case DW_AT_data_location:
12769 return "DW_AT_data_location";
09fa0d7c
JK
12770 case DW_AT_byte_stride:
12771 return "DW_AT_byte_stride";
d9fa45fe
DC
12772 case DW_AT_entry_pc:
12773 return "DW_AT_entry_pc";
12774 case DW_AT_use_UTF8:
12775 return "DW_AT_use_UTF8";
12776 case DW_AT_extension:
12777 return "DW_AT_extension";
12778 case DW_AT_ranges:
12779 return "DW_AT_ranges";
12780 case DW_AT_trampoline:
12781 return "DW_AT_trampoline";
12782 case DW_AT_call_column:
12783 return "DW_AT_call_column";
12784 case DW_AT_call_file:
12785 return "DW_AT_call_file";
12786 case DW_AT_call_line:
12787 return "DW_AT_call_line";
b7619582
GF
12788 case DW_AT_description:
12789 return "DW_AT_description";
12790 case DW_AT_binary_scale:
12791 return "DW_AT_binary_scale";
12792 case DW_AT_decimal_scale:
12793 return "DW_AT_decimal_scale";
12794 case DW_AT_small:
12795 return "DW_AT_small";
12796 case DW_AT_decimal_sign:
12797 return "DW_AT_decimal_sign";
12798 case DW_AT_digit_count:
12799 return "DW_AT_digit_count";
12800 case DW_AT_picture_string:
12801 return "DW_AT_picture_string";
12802 case DW_AT_mutable:
12803 return "DW_AT_mutable";
12804 case DW_AT_threads_scaled:
12805 return "DW_AT_threads_scaled";
12806 case DW_AT_explicit:
12807 return "DW_AT_explicit";
12808 case DW_AT_object_pointer:
12809 return "DW_AT_object_pointer";
12810 case DW_AT_endianity:
12811 return "DW_AT_endianity";
12812 case DW_AT_elemental:
12813 return "DW_AT_elemental";
12814 case DW_AT_pure:
12815 return "DW_AT_pure";
12816 case DW_AT_recursive:
12817 return "DW_AT_recursive";
348e048f
DE
12818 /* DWARF 4 values. */
12819 case DW_AT_signature:
12820 return "DW_AT_signature";
31ef98ae
TT
12821 case DW_AT_linkage_name:
12822 return "DW_AT_linkage_name";
b7619582 12823 /* SGI/MIPS extensions. */
c764a876 12824#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
12825 case DW_AT_MIPS_fde:
12826 return "DW_AT_MIPS_fde";
c764a876 12827#endif
c906108c
SS
12828 case DW_AT_MIPS_loop_begin:
12829 return "DW_AT_MIPS_loop_begin";
12830 case DW_AT_MIPS_tail_loop_begin:
12831 return "DW_AT_MIPS_tail_loop_begin";
12832 case DW_AT_MIPS_epilog_begin:
12833 return "DW_AT_MIPS_epilog_begin";
12834 case DW_AT_MIPS_loop_unroll_factor:
12835 return "DW_AT_MIPS_loop_unroll_factor";
12836 case DW_AT_MIPS_software_pipeline_depth:
12837 return "DW_AT_MIPS_software_pipeline_depth";
12838 case DW_AT_MIPS_linkage_name:
12839 return "DW_AT_MIPS_linkage_name";
b7619582
GF
12840 case DW_AT_MIPS_stride:
12841 return "DW_AT_MIPS_stride";
12842 case DW_AT_MIPS_abstract_name:
12843 return "DW_AT_MIPS_abstract_name";
12844 case DW_AT_MIPS_clone_origin:
12845 return "DW_AT_MIPS_clone_origin";
12846 case DW_AT_MIPS_has_inlines:
12847 return "DW_AT_MIPS_has_inlines";
b7619582 12848 /* HP extensions. */
c764a876 12849#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
12850 case DW_AT_HP_block_index:
12851 return "DW_AT_HP_block_index";
c764a876 12852#endif
b7619582
GF
12853 case DW_AT_HP_unmodifiable:
12854 return "DW_AT_HP_unmodifiable";
12855 case DW_AT_HP_actuals_stmt_list:
12856 return "DW_AT_HP_actuals_stmt_list";
12857 case DW_AT_HP_proc_per_section:
12858 return "DW_AT_HP_proc_per_section";
12859 case DW_AT_HP_raw_data_ptr:
12860 return "DW_AT_HP_raw_data_ptr";
12861 case DW_AT_HP_pass_by_reference:
12862 return "DW_AT_HP_pass_by_reference";
12863 case DW_AT_HP_opt_level:
12864 return "DW_AT_HP_opt_level";
12865 case DW_AT_HP_prof_version_id:
12866 return "DW_AT_HP_prof_version_id";
12867 case DW_AT_HP_opt_flags:
12868 return "DW_AT_HP_opt_flags";
12869 case DW_AT_HP_cold_region_low_pc:
12870 return "DW_AT_HP_cold_region_low_pc";
12871 case DW_AT_HP_cold_region_high_pc:
12872 return "DW_AT_HP_cold_region_high_pc";
12873 case DW_AT_HP_all_variables_modifiable:
12874 return "DW_AT_HP_all_variables_modifiable";
12875 case DW_AT_HP_linkage_name:
12876 return "DW_AT_HP_linkage_name";
12877 case DW_AT_HP_prof_flags:
12878 return "DW_AT_HP_prof_flags";
12879 /* GNU extensions. */
c906108c
SS
12880 case DW_AT_sf_names:
12881 return "DW_AT_sf_names";
12882 case DW_AT_src_info:
12883 return "DW_AT_src_info";
12884 case DW_AT_mac_info:
12885 return "DW_AT_mac_info";
12886 case DW_AT_src_coords:
12887 return "DW_AT_src_coords";
12888 case DW_AT_body_begin:
12889 return "DW_AT_body_begin";
12890 case DW_AT_body_end:
12891 return "DW_AT_body_end";
f5f8a009
EZ
12892 case DW_AT_GNU_vector:
12893 return "DW_AT_GNU_vector";
2de00c64
DE
12894 case DW_AT_GNU_odr_signature:
12895 return "DW_AT_GNU_odr_signature";
b7619582
GF
12896 /* VMS extensions. */
12897 case DW_AT_VMS_rtnbeg_pd_address:
12898 return "DW_AT_VMS_rtnbeg_pd_address";
12899 /* UPC extension. */
12900 case DW_AT_upc_threads_scaled:
12901 return "DW_AT_upc_threads_scaled";
12902 /* PGI (STMicroelectronics) extensions. */
12903 case DW_AT_PGI_lbase:
12904 return "DW_AT_PGI_lbase";
12905 case DW_AT_PGI_soffset:
12906 return "DW_AT_PGI_soffset";
12907 case DW_AT_PGI_lstride:
12908 return "DW_AT_PGI_lstride";
c906108c
SS
12909 default:
12910 return "DW_AT_<unknown>";
12911 }
12912}
12913
12914/* Convert a DWARF value form code into its string name. */
12915
12916static char *
aa1ee363 12917dwarf_form_name (unsigned form)
c906108c
SS
12918{
12919 switch (form)
12920 {
12921 case DW_FORM_addr:
12922 return "DW_FORM_addr";
12923 case DW_FORM_block2:
12924 return "DW_FORM_block2";
12925 case DW_FORM_block4:
12926 return "DW_FORM_block4";
12927 case DW_FORM_data2:
12928 return "DW_FORM_data2";
12929 case DW_FORM_data4:
12930 return "DW_FORM_data4";
12931 case DW_FORM_data8:
12932 return "DW_FORM_data8";
12933 case DW_FORM_string:
12934 return "DW_FORM_string";
12935 case DW_FORM_block:
12936 return "DW_FORM_block";
12937 case DW_FORM_block1:
12938 return "DW_FORM_block1";
12939 case DW_FORM_data1:
12940 return "DW_FORM_data1";
12941 case DW_FORM_flag:
12942 return "DW_FORM_flag";
12943 case DW_FORM_sdata:
12944 return "DW_FORM_sdata";
12945 case DW_FORM_strp:
12946 return "DW_FORM_strp";
12947 case DW_FORM_udata:
12948 return "DW_FORM_udata";
12949 case DW_FORM_ref_addr:
12950 return "DW_FORM_ref_addr";
12951 case DW_FORM_ref1:
12952 return "DW_FORM_ref1";
12953 case DW_FORM_ref2:
12954 return "DW_FORM_ref2";
12955 case DW_FORM_ref4:
12956 return "DW_FORM_ref4";
12957 case DW_FORM_ref8:
12958 return "DW_FORM_ref8";
12959 case DW_FORM_ref_udata:
12960 return "DW_FORM_ref_udata";
12961 case DW_FORM_indirect:
12962 return "DW_FORM_indirect";
348e048f
DE
12963 case DW_FORM_sec_offset:
12964 return "DW_FORM_sec_offset";
12965 case DW_FORM_exprloc:
12966 return "DW_FORM_exprloc";
12967 case DW_FORM_flag_present:
12968 return "DW_FORM_flag_present";
55f1336d
TT
12969 case DW_FORM_ref_sig8:
12970 return "DW_FORM_ref_sig8";
c906108c
SS
12971 default:
12972 return "DW_FORM_<unknown>";
12973 }
12974}
12975
12976/* Convert a DWARF stack opcode into its string name. */
12977
9eae7c52 12978const char *
b1bfef65 12979dwarf_stack_op_name (unsigned op)
c906108c
SS
12980{
12981 switch (op)
12982 {
12983 case DW_OP_addr:
12984 return "DW_OP_addr";
12985 case DW_OP_deref:
12986 return "DW_OP_deref";
12987 case DW_OP_const1u:
12988 return "DW_OP_const1u";
12989 case DW_OP_const1s:
12990 return "DW_OP_const1s";
12991 case DW_OP_const2u:
12992 return "DW_OP_const2u";
12993 case DW_OP_const2s:
12994 return "DW_OP_const2s";
12995 case DW_OP_const4u:
12996 return "DW_OP_const4u";
12997 case DW_OP_const4s:
12998 return "DW_OP_const4s";
12999 case DW_OP_const8u:
13000 return "DW_OP_const8u";
13001 case DW_OP_const8s:
13002 return "DW_OP_const8s";
13003 case DW_OP_constu:
13004 return "DW_OP_constu";
13005 case DW_OP_consts:
13006 return "DW_OP_consts";
13007 case DW_OP_dup:
13008 return "DW_OP_dup";
13009 case DW_OP_drop:
13010 return "DW_OP_drop";
13011 case DW_OP_over:
13012 return "DW_OP_over";
13013 case DW_OP_pick:
13014 return "DW_OP_pick";
13015 case DW_OP_swap:
13016 return "DW_OP_swap";
13017 case DW_OP_rot:
13018 return "DW_OP_rot";
13019 case DW_OP_xderef:
13020 return "DW_OP_xderef";
13021 case DW_OP_abs:
13022 return "DW_OP_abs";
13023 case DW_OP_and:
13024 return "DW_OP_and";
13025 case DW_OP_div:
13026 return "DW_OP_div";
13027 case DW_OP_minus:
13028 return "DW_OP_minus";
13029 case DW_OP_mod:
13030 return "DW_OP_mod";
13031 case DW_OP_mul:
13032 return "DW_OP_mul";
13033 case DW_OP_neg:
13034 return "DW_OP_neg";
13035 case DW_OP_not:
13036 return "DW_OP_not";
13037 case DW_OP_or:
13038 return "DW_OP_or";
13039 case DW_OP_plus:
13040 return "DW_OP_plus";
13041 case DW_OP_plus_uconst:
13042 return "DW_OP_plus_uconst";
13043 case DW_OP_shl:
13044 return "DW_OP_shl";
13045 case DW_OP_shr:
13046 return "DW_OP_shr";
13047 case DW_OP_shra:
13048 return "DW_OP_shra";
13049 case DW_OP_xor:
13050 return "DW_OP_xor";
13051 case DW_OP_bra:
13052 return "DW_OP_bra";
13053 case DW_OP_eq:
13054 return "DW_OP_eq";
13055 case DW_OP_ge:
13056 return "DW_OP_ge";
13057 case DW_OP_gt:
13058 return "DW_OP_gt";
13059 case DW_OP_le:
13060 return "DW_OP_le";
13061 case DW_OP_lt:
13062 return "DW_OP_lt";
13063 case DW_OP_ne:
13064 return "DW_OP_ne";
13065 case DW_OP_skip:
13066 return "DW_OP_skip";
13067 case DW_OP_lit0:
13068 return "DW_OP_lit0";
13069 case DW_OP_lit1:
13070 return "DW_OP_lit1";
13071 case DW_OP_lit2:
13072 return "DW_OP_lit2";
13073 case DW_OP_lit3:
13074 return "DW_OP_lit3";
13075 case DW_OP_lit4:
13076 return "DW_OP_lit4";
13077 case DW_OP_lit5:
13078 return "DW_OP_lit5";
13079 case DW_OP_lit6:
13080 return "DW_OP_lit6";
13081 case DW_OP_lit7:
13082 return "DW_OP_lit7";
13083 case DW_OP_lit8:
13084 return "DW_OP_lit8";
13085 case DW_OP_lit9:
13086 return "DW_OP_lit9";
13087 case DW_OP_lit10:
13088 return "DW_OP_lit10";
13089 case DW_OP_lit11:
13090 return "DW_OP_lit11";
13091 case DW_OP_lit12:
13092 return "DW_OP_lit12";
13093 case DW_OP_lit13:
13094 return "DW_OP_lit13";
13095 case DW_OP_lit14:
13096 return "DW_OP_lit14";
13097 case DW_OP_lit15:
13098 return "DW_OP_lit15";
13099 case DW_OP_lit16:
13100 return "DW_OP_lit16";
13101 case DW_OP_lit17:
13102 return "DW_OP_lit17";
13103 case DW_OP_lit18:
13104 return "DW_OP_lit18";
13105 case DW_OP_lit19:
13106 return "DW_OP_lit19";
13107 case DW_OP_lit20:
13108 return "DW_OP_lit20";
13109 case DW_OP_lit21:
13110 return "DW_OP_lit21";
13111 case DW_OP_lit22:
13112 return "DW_OP_lit22";
13113 case DW_OP_lit23:
13114 return "DW_OP_lit23";
13115 case DW_OP_lit24:
13116 return "DW_OP_lit24";
13117 case DW_OP_lit25:
13118 return "DW_OP_lit25";
13119 case DW_OP_lit26:
13120 return "DW_OP_lit26";
13121 case DW_OP_lit27:
13122 return "DW_OP_lit27";
13123 case DW_OP_lit28:
13124 return "DW_OP_lit28";
13125 case DW_OP_lit29:
13126 return "DW_OP_lit29";
13127 case DW_OP_lit30:
13128 return "DW_OP_lit30";
13129 case DW_OP_lit31:
13130 return "DW_OP_lit31";
13131 case DW_OP_reg0:
13132 return "DW_OP_reg0";
13133 case DW_OP_reg1:
13134 return "DW_OP_reg1";
13135 case DW_OP_reg2:
13136 return "DW_OP_reg2";
13137 case DW_OP_reg3:
13138 return "DW_OP_reg3";
13139 case DW_OP_reg4:
13140 return "DW_OP_reg4";
13141 case DW_OP_reg5:
13142 return "DW_OP_reg5";
13143 case DW_OP_reg6:
13144 return "DW_OP_reg6";
13145 case DW_OP_reg7:
13146 return "DW_OP_reg7";
13147 case DW_OP_reg8:
13148 return "DW_OP_reg8";
13149 case DW_OP_reg9:
13150 return "DW_OP_reg9";
13151 case DW_OP_reg10:
13152 return "DW_OP_reg10";
13153 case DW_OP_reg11:
13154 return "DW_OP_reg11";
13155 case DW_OP_reg12:
13156 return "DW_OP_reg12";
13157 case DW_OP_reg13:
13158 return "DW_OP_reg13";
13159 case DW_OP_reg14:
13160 return "DW_OP_reg14";
13161 case DW_OP_reg15:
13162 return "DW_OP_reg15";
13163 case DW_OP_reg16:
13164 return "DW_OP_reg16";
13165 case DW_OP_reg17:
13166 return "DW_OP_reg17";
13167 case DW_OP_reg18:
13168 return "DW_OP_reg18";
13169 case DW_OP_reg19:
13170 return "DW_OP_reg19";
13171 case DW_OP_reg20:
13172 return "DW_OP_reg20";
13173 case DW_OP_reg21:
13174 return "DW_OP_reg21";
13175 case DW_OP_reg22:
13176 return "DW_OP_reg22";
13177 case DW_OP_reg23:
13178 return "DW_OP_reg23";
13179 case DW_OP_reg24:
13180 return "DW_OP_reg24";
13181 case DW_OP_reg25:
13182 return "DW_OP_reg25";
13183 case DW_OP_reg26:
13184 return "DW_OP_reg26";
13185 case DW_OP_reg27:
13186 return "DW_OP_reg27";
13187 case DW_OP_reg28:
13188 return "DW_OP_reg28";
13189 case DW_OP_reg29:
13190 return "DW_OP_reg29";
13191 case DW_OP_reg30:
13192 return "DW_OP_reg30";
13193 case DW_OP_reg31:
13194 return "DW_OP_reg31";
13195 case DW_OP_breg0:
13196 return "DW_OP_breg0";
13197 case DW_OP_breg1:
13198 return "DW_OP_breg1";
13199 case DW_OP_breg2:
13200 return "DW_OP_breg2";
13201 case DW_OP_breg3:
13202 return "DW_OP_breg3";
13203 case DW_OP_breg4:
13204 return "DW_OP_breg4";
13205 case DW_OP_breg5:
13206 return "DW_OP_breg5";
13207 case DW_OP_breg6:
13208 return "DW_OP_breg6";
13209 case DW_OP_breg7:
13210 return "DW_OP_breg7";
13211 case DW_OP_breg8:
13212 return "DW_OP_breg8";
13213 case DW_OP_breg9:
13214 return "DW_OP_breg9";
13215 case DW_OP_breg10:
13216 return "DW_OP_breg10";
13217 case DW_OP_breg11:
13218 return "DW_OP_breg11";
13219 case DW_OP_breg12:
13220 return "DW_OP_breg12";
13221 case DW_OP_breg13:
13222 return "DW_OP_breg13";
13223 case DW_OP_breg14:
13224 return "DW_OP_breg14";
13225 case DW_OP_breg15:
13226 return "DW_OP_breg15";
13227 case DW_OP_breg16:
13228 return "DW_OP_breg16";
13229 case DW_OP_breg17:
13230 return "DW_OP_breg17";
13231 case DW_OP_breg18:
13232 return "DW_OP_breg18";
13233 case DW_OP_breg19:
13234 return "DW_OP_breg19";
13235 case DW_OP_breg20:
13236 return "DW_OP_breg20";
13237 case DW_OP_breg21:
13238 return "DW_OP_breg21";
13239 case DW_OP_breg22:
13240 return "DW_OP_breg22";
13241 case DW_OP_breg23:
13242 return "DW_OP_breg23";
13243 case DW_OP_breg24:
13244 return "DW_OP_breg24";
13245 case DW_OP_breg25:
13246 return "DW_OP_breg25";
13247 case DW_OP_breg26:
13248 return "DW_OP_breg26";
13249 case DW_OP_breg27:
13250 return "DW_OP_breg27";
13251 case DW_OP_breg28:
13252 return "DW_OP_breg28";
13253 case DW_OP_breg29:
13254 return "DW_OP_breg29";
13255 case DW_OP_breg30:
13256 return "DW_OP_breg30";
13257 case DW_OP_breg31:
13258 return "DW_OP_breg31";
13259 case DW_OP_regx:
13260 return "DW_OP_regx";
13261 case DW_OP_fbreg:
13262 return "DW_OP_fbreg";
13263 case DW_OP_bregx:
13264 return "DW_OP_bregx";
13265 case DW_OP_piece:
13266 return "DW_OP_piece";
13267 case DW_OP_deref_size:
13268 return "DW_OP_deref_size";
13269 case DW_OP_xderef_size:
13270 return "DW_OP_xderef_size";
13271 case DW_OP_nop:
13272 return "DW_OP_nop";
b7619582 13273 /* DWARF 3 extensions. */
ed348acc
EZ
13274 case DW_OP_push_object_address:
13275 return "DW_OP_push_object_address";
13276 case DW_OP_call2:
13277 return "DW_OP_call2";
13278 case DW_OP_call4:
13279 return "DW_OP_call4";
13280 case DW_OP_call_ref:
13281 return "DW_OP_call_ref";
b7619582
GF
13282 case DW_OP_form_tls_address:
13283 return "DW_OP_form_tls_address";
13284 case DW_OP_call_frame_cfa:
13285 return "DW_OP_call_frame_cfa";
13286 case DW_OP_bit_piece:
13287 return "DW_OP_bit_piece";
9eae7c52
TT
13288 /* DWARF 4 extensions. */
13289 case DW_OP_implicit_value:
13290 return "DW_OP_implicit_value";
13291 case DW_OP_stack_value:
13292 return "DW_OP_stack_value";
13293 /* GNU extensions. */
ed348acc
EZ
13294 case DW_OP_GNU_push_tls_address:
13295 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13296 case DW_OP_GNU_uninit:
13297 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13298 case DW_OP_GNU_implicit_pointer:
13299 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13300 case DW_OP_GNU_entry_value:
13301 return "DW_OP_GNU_entry_value";
13302 case DW_OP_GNU_const_type:
13303 return "DW_OP_GNU_const_type";
13304 case DW_OP_GNU_regval_type:
13305 return "DW_OP_GNU_regval_type";
13306 case DW_OP_GNU_deref_type:
13307 return "DW_OP_GNU_deref_type";
13308 case DW_OP_GNU_convert:
13309 return "DW_OP_GNU_convert";
13310 case DW_OP_GNU_reinterpret:
13311 return "DW_OP_GNU_reinterpret";
c906108c 13312 default:
b1bfef65 13313 return NULL;
c906108c
SS
13314 }
13315}
13316
13317static char *
fba45db2 13318dwarf_bool_name (unsigned mybool)
c906108c
SS
13319{
13320 if (mybool)
13321 return "TRUE";
13322 else
13323 return "FALSE";
13324}
13325
13326/* Convert a DWARF type code into its string name. */
13327
13328static char *
aa1ee363 13329dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13330{
13331 switch (enc)
13332 {
b7619582
GF
13333 case DW_ATE_void:
13334 return "DW_ATE_void";
c906108c
SS
13335 case DW_ATE_address:
13336 return "DW_ATE_address";
13337 case DW_ATE_boolean:
13338 return "DW_ATE_boolean";
13339 case DW_ATE_complex_float:
13340 return "DW_ATE_complex_float";
13341 case DW_ATE_float:
13342 return "DW_ATE_float";
13343 case DW_ATE_signed:
13344 return "DW_ATE_signed";
13345 case DW_ATE_signed_char:
13346 return "DW_ATE_signed_char";
13347 case DW_ATE_unsigned:
13348 return "DW_ATE_unsigned";
13349 case DW_ATE_unsigned_char:
13350 return "DW_ATE_unsigned_char";
b7619582 13351 /* DWARF 3. */
d9fa45fe
DC
13352 case DW_ATE_imaginary_float:
13353 return "DW_ATE_imaginary_float";
b7619582
GF
13354 case DW_ATE_packed_decimal:
13355 return "DW_ATE_packed_decimal";
13356 case DW_ATE_numeric_string:
13357 return "DW_ATE_numeric_string";
13358 case DW_ATE_edited:
13359 return "DW_ATE_edited";
13360 case DW_ATE_signed_fixed:
13361 return "DW_ATE_signed_fixed";
13362 case DW_ATE_unsigned_fixed:
13363 return "DW_ATE_unsigned_fixed";
13364 case DW_ATE_decimal_float:
13365 return "DW_ATE_decimal_float";
75079b2b
TT
13366 /* DWARF 4. */
13367 case DW_ATE_UTF:
13368 return "DW_ATE_UTF";
b7619582
GF
13369 /* HP extensions. */
13370 case DW_ATE_HP_float80:
13371 return "DW_ATE_HP_float80";
13372 case DW_ATE_HP_complex_float80:
13373 return "DW_ATE_HP_complex_float80";
13374 case DW_ATE_HP_float128:
13375 return "DW_ATE_HP_float128";
13376 case DW_ATE_HP_complex_float128:
13377 return "DW_ATE_HP_complex_float128";
13378 case DW_ATE_HP_floathpintel:
13379 return "DW_ATE_HP_floathpintel";
13380 case DW_ATE_HP_imaginary_float80:
13381 return "DW_ATE_HP_imaginary_float80";
13382 case DW_ATE_HP_imaginary_float128:
13383 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13384 default:
13385 return "DW_ATE_<unknown>";
13386 }
13387}
13388
0963b4bd 13389/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13390
13391#if 0
13392static char *
aa1ee363 13393dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13394{
13395 switch (cfi_opc)
13396 {
13397 case DW_CFA_advance_loc:
13398 return "DW_CFA_advance_loc";
13399 case DW_CFA_offset:
13400 return "DW_CFA_offset";
13401 case DW_CFA_restore:
13402 return "DW_CFA_restore";
13403 case DW_CFA_nop:
13404 return "DW_CFA_nop";
13405 case DW_CFA_set_loc:
13406 return "DW_CFA_set_loc";
13407 case DW_CFA_advance_loc1:
13408 return "DW_CFA_advance_loc1";
13409 case DW_CFA_advance_loc2:
13410 return "DW_CFA_advance_loc2";
13411 case DW_CFA_advance_loc4:
13412 return "DW_CFA_advance_loc4";
13413 case DW_CFA_offset_extended:
13414 return "DW_CFA_offset_extended";
13415 case DW_CFA_restore_extended:
13416 return "DW_CFA_restore_extended";
13417 case DW_CFA_undefined:
13418 return "DW_CFA_undefined";
13419 case DW_CFA_same_value:
13420 return "DW_CFA_same_value";
13421 case DW_CFA_register:
13422 return "DW_CFA_register";
13423 case DW_CFA_remember_state:
13424 return "DW_CFA_remember_state";
13425 case DW_CFA_restore_state:
13426 return "DW_CFA_restore_state";
13427 case DW_CFA_def_cfa:
13428 return "DW_CFA_def_cfa";
13429 case DW_CFA_def_cfa_register:
13430 return "DW_CFA_def_cfa_register";
13431 case DW_CFA_def_cfa_offset:
13432 return "DW_CFA_def_cfa_offset";
b7619582 13433 /* DWARF 3. */
985cb1a3
JM
13434 case DW_CFA_def_cfa_expression:
13435 return "DW_CFA_def_cfa_expression";
13436 case DW_CFA_expression:
13437 return "DW_CFA_expression";
13438 case DW_CFA_offset_extended_sf:
13439 return "DW_CFA_offset_extended_sf";
13440 case DW_CFA_def_cfa_sf:
13441 return "DW_CFA_def_cfa_sf";
13442 case DW_CFA_def_cfa_offset_sf:
13443 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13444 case DW_CFA_val_offset:
13445 return "DW_CFA_val_offset";
13446 case DW_CFA_val_offset_sf:
13447 return "DW_CFA_val_offset_sf";
13448 case DW_CFA_val_expression:
13449 return "DW_CFA_val_expression";
13450 /* SGI/MIPS specific. */
c906108c
SS
13451 case DW_CFA_MIPS_advance_loc8:
13452 return "DW_CFA_MIPS_advance_loc8";
b7619582 13453 /* GNU extensions. */
985cb1a3
JM
13454 case DW_CFA_GNU_window_save:
13455 return "DW_CFA_GNU_window_save";
13456 case DW_CFA_GNU_args_size:
13457 return "DW_CFA_GNU_args_size";
13458 case DW_CFA_GNU_negative_offset_extended:
13459 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13460 default:
13461 return "DW_CFA_<unknown>";
13462 }
13463}
13464#endif
13465
f9aca02d 13466static void
d97bc12b 13467dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13468{
13469 unsigned int i;
13470
d97bc12b
DE
13471 print_spaces (indent, f);
13472 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13473 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13474
13475 if (die->parent != NULL)
13476 {
13477 print_spaces (indent, f);
13478 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13479 die->parent->offset);
13480 }
13481
13482 print_spaces (indent, f);
13483 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13484 dwarf_bool_name (die->child != NULL));
c906108c 13485
d97bc12b
DE
13486 print_spaces (indent, f);
13487 fprintf_unfiltered (f, " attributes:\n");
13488
c906108c
SS
13489 for (i = 0; i < die->num_attrs; ++i)
13490 {
d97bc12b
DE
13491 print_spaces (indent, f);
13492 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13493 dwarf_attr_name (die->attrs[i].name),
13494 dwarf_form_name (die->attrs[i].form));
d97bc12b 13495
c906108c
SS
13496 switch (die->attrs[i].form)
13497 {
13498 case DW_FORM_ref_addr:
13499 case DW_FORM_addr:
d97bc12b 13500 fprintf_unfiltered (f, "address: ");
5af949e3 13501 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13502 break;
13503 case DW_FORM_block2:
13504 case DW_FORM_block4:
13505 case DW_FORM_block:
13506 case DW_FORM_block1:
3e43a32a
MS
13507 fprintf_unfiltered (f, "block: size %d",
13508 DW_BLOCK (&die->attrs[i])->size);
c906108c 13509 break;
2dc7f7b3
TT
13510 case DW_FORM_exprloc:
13511 fprintf_unfiltered (f, "expression: size %u",
13512 DW_BLOCK (&die->attrs[i])->size);
13513 break;
10b3939b
DJ
13514 case DW_FORM_ref1:
13515 case DW_FORM_ref2:
13516 case DW_FORM_ref4:
d97bc12b 13517 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13518 (long) (DW_ADDR (&die->attrs[i])));
13519 break;
c906108c
SS
13520 case DW_FORM_data1:
13521 case DW_FORM_data2:
13522 case DW_FORM_data4:
ce5d95e1 13523 case DW_FORM_data8:
c906108c
SS
13524 case DW_FORM_udata:
13525 case DW_FORM_sdata:
43bbcdc2
PH
13526 fprintf_unfiltered (f, "constant: %s",
13527 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13528 break;
2dc7f7b3
TT
13529 case DW_FORM_sec_offset:
13530 fprintf_unfiltered (f, "section offset: %s",
13531 pulongest (DW_UNSND (&die->attrs[i])));
13532 break;
55f1336d 13533 case DW_FORM_ref_sig8:
348e048f
DE
13534 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13535 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13536 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13537 else
13538 fprintf_unfiltered (f, "signatured type, offset: unknown");
13539 break;
c906108c 13540 case DW_FORM_string:
4bdf3d34 13541 case DW_FORM_strp:
8285870a 13542 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13543 DW_STRING (&die->attrs[i])
8285870a
JK
13544 ? DW_STRING (&die->attrs[i]) : "",
13545 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13546 break;
13547 case DW_FORM_flag:
13548 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13549 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13550 else
d97bc12b 13551 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13552 break;
2dc7f7b3
TT
13553 case DW_FORM_flag_present:
13554 fprintf_unfiltered (f, "flag: TRUE");
13555 break;
a8329558 13556 case DW_FORM_indirect:
0963b4bd
MS
13557 /* The reader will have reduced the indirect form to
13558 the "base form" so this form should not occur. */
3e43a32a
MS
13559 fprintf_unfiltered (f,
13560 "unexpected attribute form: DW_FORM_indirect");
a8329558 13561 break;
c906108c 13562 default:
d97bc12b 13563 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 13564 die->attrs[i].form);
d97bc12b 13565 break;
c906108c 13566 }
d97bc12b 13567 fprintf_unfiltered (f, "\n");
c906108c
SS
13568 }
13569}
13570
f9aca02d 13571static void
d97bc12b 13572dump_die_for_error (struct die_info *die)
c906108c 13573{
d97bc12b
DE
13574 dump_die_shallow (gdb_stderr, 0, die);
13575}
13576
13577static void
13578dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13579{
13580 int indent = level * 4;
13581
13582 gdb_assert (die != NULL);
13583
13584 if (level >= max_level)
13585 return;
13586
13587 dump_die_shallow (f, indent, die);
13588
13589 if (die->child != NULL)
c906108c 13590 {
d97bc12b
DE
13591 print_spaces (indent, f);
13592 fprintf_unfiltered (f, " Children:");
13593 if (level + 1 < max_level)
13594 {
13595 fprintf_unfiltered (f, "\n");
13596 dump_die_1 (f, level + 1, max_level, die->child);
13597 }
13598 else
13599 {
3e43a32a
MS
13600 fprintf_unfiltered (f,
13601 " [not printed, max nesting level reached]\n");
d97bc12b
DE
13602 }
13603 }
13604
13605 if (die->sibling != NULL && level > 0)
13606 {
13607 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
13608 }
13609}
13610
d97bc12b
DE
13611/* This is called from the pdie macro in gdbinit.in.
13612 It's not static so gcc will keep a copy callable from gdb. */
13613
13614void
13615dump_die (struct die_info *die, int max_level)
13616{
13617 dump_die_1 (gdb_stdlog, 0, max_level, die);
13618}
13619
f9aca02d 13620static void
51545339 13621store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13622{
51545339 13623 void **slot;
c906108c 13624
51545339
DJ
13625 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13626
13627 *slot = die;
c906108c
SS
13628}
13629
93311388
DE
13630static int
13631is_ref_attr (struct attribute *attr)
c906108c 13632{
c906108c
SS
13633 switch (attr->form)
13634 {
13635 case DW_FORM_ref_addr:
c906108c
SS
13636 case DW_FORM_ref1:
13637 case DW_FORM_ref2:
13638 case DW_FORM_ref4:
613e1657 13639 case DW_FORM_ref8:
c906108c 13640 case DW_FORM_ref_udata:
93311388 13641 return 1;
c906108c 13642 default:
93311388 13643 return 0;
c906108c 13644 }
93311388
DE
13645}
13646
13647static unsigned int
13648dwarf2_get_ref_die_offset (struct attribute *attr)
13649{
13650 if (is_ref_attr (attr))
13651 return DW_ADDR (attr);
13652
13653 complaint (&symfile_complaints,
13654 _("unsupported die ref attribute form: '%s'"),
13655 dwarf_form_name (attr->form));
13656 return 0;
c906108c
SS
13657}
13658
43bbcdc2
PH
13659/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13660 * the value held by the attribute is not constant. */
a02abb62 13661
43bbcdc2 13662static LONGEST
a02abb62
JB
13663dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13664{
13665 if (attr->form == DW_FORM_sdata)
13666 return DW_SND (attr);
13667 else if (attr->form == DW_FORM_udata
13668 || attr->form == DW_FORM_data1
13669 || attr->form == DW_FORM_data2
13670 || attr->form == DW_FORM_data4
13671 || attr->form == DW_FORM_data8)
13672 return DW_UNSND (attr);
13673 else
13674 {
3e43a32a
MS
13675 complaint (&symfile_complaints,
13676 _("Attribute value is not a constant (%s)"),
a02abb62
JB
13677 dwarf_form_name (attr->form));
13678 return default_value;
13679 }
13680}
13681
03dd20cc 13682/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
13683 unit and add it to our queue.
13684 The result is non-zero if PER_CU was queued, otherwise the result is zero
13685 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 13686
348e048f 13687static int
03dd20cc
DJ
13688maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13689 struct dwarf2_per_cu_data *per_cu)
13690{
98bfdba5
PA
13691 /* We may arrive here during partial symbol reading, if we need full
13692 DIEs to process an unusual case (e.g. template arguments). Do
13693 not queue PER_CU, just tell our caller to load its DIEs. */
13694 if (dwarf2_per_objfile->reading_partial_symbols)
13695 {
13696 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13697 return 1;
13698 return 0;
13699 }
13700
03dd20cc
DJ
13701 /* Mark the dependence relation so that we don't flush PER_CU
13702 too early. */
13703 dwarf2_add_dependence (this_cu, per_cu);
13704
13705 /* If it's already on the queue, we have nothing to do. */
13706 if (per_cu->queued)
348e048f 13707 return 0;
03dd20cc
DJ
13708
13709 /* If the compilation unit is already loaded, just mark it as
13710 used. */
13711 if (per_cu->cu != NULL)
13712 {
13713 per_cu->cu->last_used = 0;
348e048f 13714 return 0;
03dd20cc
DJ
13715 }
13716
13717 /* Add it to the queue. */
13718 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
13719
13720 return 1;
13721}
13722
13723/* Follow reference or signature attribute ATTR of SRC_DIE.
13724 On entry *REF_CU is the CU of SRC_DIE.
13725 On exit *REF_CU is the CU of the result. */
13726
13727static struct die_info *
13728follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13729 struct dwarf2_cu **ref_cu)
13730{
13731 struct die_info *die;
13732
13733 if (is_ref_attr (attr))
13734 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 13735 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
13736 die = follow_die_sig (src_die, attr, ref_cu);
13737 else
13738 {
13739 dump_die_for_error (src_die);
13740 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13741 (*ref_cu)->objfile->name);
13742 }
13743
13744 return die;
03dd20cc
DJ
13745}
13746
5c631832 13747/* Follow reference OFFSET.
673bfd45
DE
13748 On entry *REF_CU is the CU of the source die referencing OFFSET.
13749 On exit *REF_CU is the CU of the result.
13750 Returns NULL if OFFSET is invalid. */
f504f079 13751
f9aca02d 13752static struct die_info *
5c631832 13753follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 13754{
10b3939b 13755 struct die_info temp_die;
f2f0e013 13756 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 13757
348e048f
DE
13758 gdb_assert (cu->per_cu != NULL);
13759
98bfdba5
PA
13760 target_cu = cu;
13761
348e048f
DE
13762 if (cu->per_cu->from_debug_types)
13763 {
13764 /* .debug_types CUs cannot reference anything outside their CU.
13765 If they need to, they have to reference a signatured type via
55f1336d 13766 DW_FORM_ref_sig8. */
348e048f 13767 if (! offset_in_cu_p (&cu->header, offset))
5c631832 13768 return NULL;
348e048f
DE
13769 }
13770 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
13771 {
13772 struct dwarf2_per_cu_data *per_cu;
9a619af0 13773
45452591 13774 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
13775
13776 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
13777 if (maybe_queue_comp_unit (cu, per_cu))
13778 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 13779
10b3939b
DJ
13780 target_cu = per_cu->cu;
13781 }
98bfdba5
PA
13782 else if (cu->dies == NULL)
13783 {
13784 /* We're loading full DIEs during partial symbol reading. */
13785 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13786 load_full_comp_unit (cu->per_cu, cu->objfile);
13787 }
c906108c 13788
f2f0e013 13789 *ref_cu = target_cu;
51545339 13790 temp_die.offset = offset;
5c631832
JK
13791 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13792}
10b3939b 13793
5c631832
JK
13794/* Follow reference attribute ATTR of SRC_DIE.
13795 On entry *REF_CU is the CU of SRC_DIE.
13796 On exit *REF_CU is the CU of the result. */
13797
13798static struct die_info *
13799follow_die_ref (struct die_info *src_die, struct attribute *attr,
13800 struct dwarf2_cu **ref_cu)
13801{
13802 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13803 struct dwarf2_cu *cu = *ref_cu;
13804 struct die_info *die;
13805
13806 die = follow_die_offset (offset, ref_cu);
13807 if (!die)
13808 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13809 "at 0x%x [in module %s]"),
13810 offset, src_die->offset, cu->objfile->name);
348e048f 13811
5c631832
JK
13812 return die;
13813}
13814
13815/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13816 value is intended for DW_OP_call*. */
13817
13818struct dwarf2_locexpr_baton
13819dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
13820 struct dwarf2_per_cu_data *per_cu,
13821 CORE_ADDR (*get_frame_pc) (void *baton),
13822 void *baton)
5c631832
JK
13823{
13824 struct dwarf2_cu *cu = per_cu->cu;
13825 struct die_info *die;
13826 struct attribute *attr;
13827 struct dwarf2_locexpr_baton retval;
13828
8cf6f0b1
TT
13829 dw2_setup (per_cu->objfile);
13830
5c631832
JK
13831 die = follow_die_offset (offset, &cu);
13832 if (!die)
13833 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13834 offset, per_cu->cu->objfile->name);
13835
13836 attr = dwarf2_attr (die, DW_AT_location, cu);
13837 if (!attr)
13838 {
13839 /* DWARF: "If there is no such attribute, then there is no effect.". */
13840
13841 retval.data = NULL;
13842 retval.size = 0;
13843 }
8cf6f0b1
TT
13844 else if (attr_form_is_section_offset (attr))
13845 {
13846 struct dwarf2_loclist_baton loclist_baton;
13847 CORE_ADDR pc = (*get_frame_pc) (baton);
13848 size_t size;
13849
13850 fill_in_loclist_baton (cu, &loclist_baton, attr);
13851
13852 retval.data = dwarf2_find_location_expression (&loclist_baton,
13853 &size, pc);
13854 retval.size = size;
13855 }
5c631832
JK
13856 else
13857 {
13858 if (!attr_form_is_block (attr))
13859 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13860 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13861 offset, per_cu->cu->objfile->name);
13862
13863 retval.data = DW_BLOCK (attr)->data;
13864 retval.size = DW_BLOCK (attr)->size;
13865 }
13866 retval.per_cu = cu->per_cu;
13867 return retval;
348e048f
DE
13868}
13869
8a9b8146
TT
13870/* Return the type of the DIE at DIE_OFFSET in the CU named by
13871 PER_CU. */
13872
13873struct type *
13874dwarf2_get_die_type (unsigned int die_offset,
13875 struct dwarf2_per_cu_data *per_cu)
13876{
8a9b8146 13877 dw2_setup (per_cu->objfile);
9ff3b74f 13878 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
13879}
13880
348e048f
DE
13881/* Follow the signature attribute ATTR in SRC_DIE.
13882 On entry *REF_CU is the CU of SRC_DIE.
13883 On exit *REF_CU is the CU of the result. */
13884
13885static struct die_info *
13886follow_die_sig (struct die_info *src_die, struct attribute *attr,
13887 struct dwarf2_cu **ref_cu)
13888{
13889 struct objfile *objfile = (*ref_cu)->objfile;
13890 struct die_info temp_die;
13891 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13892 struct dwarf2_cu *sig_cu;
13893 struct die_info *die;
13894
13895 /* sig_type will be NULL if the signatured type is missing from
13896 the debug info. */
13897 if (sig_type == NULL)
13898 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13899 "at 0x%x [in module %s]"),
13900 src_die->offset, objfile->name);
13901
13902 /* If necessary, add it to the queue and load its DIEs. */
13903
13904 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13905 read_signatured_type (objfile, sig_type);
13906
13907 gdb_assert (sig_type->per_cu.cu != NULL);
13908
13909 sig_cu = sig_type->per_cu.cu;
13910 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13911 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13912 if (die)
13913 {
13914 *ref_cu = sig_cu;
13915 return die;
13916 }
13917
3e43a32a
MS
13918 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13919 "from DIE at 0x%x [in module %s]"),
348e048f
DE
13920 sig_type->type_offset, src_die->offset, objfile->name);
13921}
13922
13923/* Given an offset of a signatured type, return its signatured_type. */
13924
13925static struct signatured_type *
13926lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13927{
13928 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13929 unsigned int length, initial_length_size;
13930 unsigned int sig_offset;
13931 struct signatured_type find_entry, *type_sig;
13932
13933 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13934 sig_offset = (initial_length_size
13935 + 2 /*version*/
13936 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13937 + 1 /*address_size*/);
13938 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13939 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13940
13941 /* This is only used to lookup previously recorded types.
13942 If we didn't find it, it's our bug. */
13943 gdb_assert (type_sig != NULL);
b3c8eb43 13944 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
13945
13946 return type_sig;
13947}
13948
13949/* Read in signatured type at OFFSET and build its CU and die(s). */
13950
13951static void
13952read_signatured_type_at_offset (struct objfile *objfile,
13953 unsigned int offset)
13954{
13955 struct signatured_type *type_sig;
13956
be391dca
TT
13957 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13958
348e048f
DE
13959 /* We have the section offset, but we need the signature to do the
13960 hash table lookup. */
13961 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13962
13963 gdb_assert (type_sig->per_cu.cu == NULL);
13964
13965 read_signatured_type (objfile, type_sig);
13966
13967 gdb_assert (type_sig->per_cu.cu != NULL);
13968}
13969
13970/* Read in a signatured type and build its CU and DIEs. */
13971
13972static void
13973read_signatured_type (struct objfile *objfile,
13974 struct signatured_type *type_sig)
13975{
1fd400ff 13976 gdb_byte *types_ptr;
348e048f
DE
13977 struct die_reader_specs reader_specs;
13978 struct dwarf2_cu *cu;
13979 ULONGEST signature;
13980 struct cleanup *back_to, *free_cu_cleanup;
348e048f 13981
1fd400ff 13982 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
b3c8eb43 13983 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->per_cu.offset;
1fd400ff 13984
348e048f
DE
13985 gdb_assert (type_sig->per_cu.cu == NULL);
13986
9816fde3
JK
13987 cu = xmalloc (sizeof (*cu));
13988 init_one_comp_unit (cu, objfile);
13989
348e048f
DE
13990 type_sig->per_cu.cu = cu;
13991 cu->per_cu = &type_sig->per_cu;
13992
13993 /* If an error occurs while loading, release our storage. */
13994 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13995
13996 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13997 types_ptr, objfile->obfd);
13998 gdb_assert (signature == type_sig->signature);
13999
14000 cu->die_hash
14001 = htab_create_alloc_ex (cu->header.length / 12,
14002 die_hash,
14003 die_eq,
14004 NULL,
14005 &cu->comp_unit_obstack,
14006 hashtab_obstack_allocate,
14007 dummy_obstack_deallocate);
14008
14009 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14010 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14011
14012 init_cu_die_reader (&reader_specs, cu);
14013
14014 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14015 NULL /*parent*/);
14016
14017 /* We try not to read any attributes in this function, because not
14018 all objfiles needed for references have been loaded yet, and symbol
14019 table processing isn't initialized. But we have to set the CU language,
14020 or we won't be able to build types correctly. */
9816fde3 14021 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14022
14023 do_cleanups (back_to);
14024
14025 /* We've successfully allocated this compilation unit. Let our caller
14026 clean it up when finished with it. */
14027 discard_cleanups (free_cu_cleanup);
14028
14029 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14030 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14031}
14032
c906108c
SS
14033/* Decode simple location descriptions.
14034 Given a pointer to a dwarf block that defines a location, compute
14035 the location and return the value.
14036
4cecd739
DJ
14037 NOTE drow/2003-11-18: This function is called in two situations
14038 now: for the address of static or global variables (partial symbols
14039 only) and for offsets into structures which are expected to be
14040 (more or less) constant. The partial symbol case should go away,
14041 and only the constant case should remain. That will let this
14042 function complain more accurately. A few special modes are allowed
14043 without complaint for global variables (for instance, global
14044 register values and thread-local values).
c906108c
SS
14045
14046 A location description containing no operations indicates that the
4cecd739 14047 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14048 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14049 callers will only want a very basic result and this can become a
14050 complaint.
c906108c 14051
d53d4ac5 14052 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14053
14054static CORE_ADDR
e7c27a73 14055decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14056{
e7c27a73 14057 struct objfile *objfile = cu->objfile;
c906108c
SS
14058 int i;
14059 int size = blk->size;
fe1b8b76 14060 gdb_byte *data = blk->data;
c906108c
SS
14061 CORE_ADDR stack[64];
14062 int stacki;
14063 unsigned int bytes_read, unsnd;
fe1b8b76 14064 gdb_byte op;
c906108c
SS
14065
14066 i = 0;
14067 stacki = 0;
14068 stack[stacki] = 0;
d53d4ac5 14069 stack[++stacki] = 0;
c906108c
SS
14070
14071 while (i < size)
14072 {
c906108c
SS
14073 op = data[i++];
14074 switch (op)
14075 {
f1bea926
JM
14076 case DW_OP_lit0:
14077 case DW_OP_lit1:
14078 case DW_OP_lit2:
14079 case DW_OP_lit3:
14080 case DW_OP_lit4:
14081 case DW_OP_lit5:
14082 case DW_OP_lit6:
14083 case DW_OP_lit7:
14084 case DW_OP_lit8:
14085 case DW_OP_lit9:
14086 case DW_OP_lit10:
14087 case DW_OP_lit11:
14088 case DW_OP_lit12:
14089 case DW_OP_lit13:
14090 case DW_OP_lit14:
14091 case DW_OP_lit15:
14092 case DW_OP_lit16:
14093 case DW_OP_lit17:
14094 case DW_OP_lit18:
14095 case DW_OP_lit19:
14096 case DW_OP_lit20:
14097 case DW_OP_lit21:
14098 case DW_OP_lit22:
14099 case DW_OP_lit23:
14100 case DW_OP_lit24:
14101 case DW_OP_lit25:
14102 case DW_OP_lit26:
14103 case DW_OP_lit27:
14104 case DW_OP_lit28:
14105 case DW_OP_lit29:
14106 case DW_OP_lit30:
14107 case DW_OP_lit31:
14108 stack[++stacki] = op - DW_OP_lit0;
14109 break;
14110
c906108c
SS
14111 case DW_OP_reg0:
14112 case DW_OP_reg1:
14113 case DW_OP_reg2:
14114 case DW_OP_reg3:
14115 case DW_OP_reg4:
14116 case DW_OP_reg5:
14117 case DW_OP_reg6:
14118 case DW_OP_reg7:
14119 case DW_OP_reg8:
14120 case DW_OP_reg9:
14121 case DW_OP_reg10:
14122 case DW_OP_reg11:
14123 case DW_OP_reg12:
14124 case DW_OP_reg13:
14125 case DW_OP_reg14:
14126 case DW_OP_reg15:
14127 case DW_OP_reg16:
14128 case DW_OP_reg17:
14129 case DW_OP_reg18:
14130 case DW_OP_reg19:
14131 case DW_OP_reg20:
14132 case DW_OP_reg21:
14133 case DW_OP_reg22:
14134 case DW_OP_reg23:
14135 case DW_OP_reg24:
14136 case DW_OP_reg25:
14137 case DW_OP_reg26:
14138 case DW_OP_reg27:
14139 case DW_OP_reg28:
14140 case DW_OP_reg29:
14141 case DW_OP_reg30:
14142 case DW_OP_reg31:
c906108c 14143 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
14144 if (i < size)
14145 dwarf2_complex_location_expr_complaint ();
c906108c
SS
14146 break;
14147
14148 case DW_OP_regx:
c906108c
SS
14149 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14150 i += bytes_read;
c906108c 14151 stack[++stacki] = unsnd;
4cecd739
DJ
14152 if (i < size)
14153 dwarf2_complex_location_expr_complaint ();
c906108c
SS
14154 break;
14155
14156 case DW_OP_addr:
107d2387 14157 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 14158 cu, &bytes_read);
107d2387 14159 i += bytes_read;
c906108c
SS
14160 break;
14161
14162 case DW_OP_const1u:
14163 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14164 i += 1;
14165 break;
14166
14167 case DW_OP_const1s:
14168 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14169 i += 1;
14170 break;
14171
14172 case DW_OP_const2u:
14173 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14174 i += 2;
14175 break;
14176
14177 case DW_OP_const2s:
14178 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14179 i += 2;
14180 break;
14181
14182 case DW_OP_const4u:
14183 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14184 i += 4;
14185 break;
14186
14187 case DW_OP_const4s:
14188 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14189 i += 4;
14190 break;
14191
14192 case DW_OP_constu:
14193 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 14194 &bytes_read);
c906108c
SS
14195 i += bytes_read;
14196 break;
14197
14198 case DW_OP_consts:
14199 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14200 i += bytes_read;
14201 break;
14202
f1bea926
JM
14203 case DW_OP_dup:
14204 stack[stacki + 1] = stack[stacki];
14205 stacki++;
14206 break;
14207
c906108c
SS
14208 case DW_OP_plus:
14209 stack[stacki - 1] += stack[stacki];
14210 stacki--;
14211 break;
14212
14213 case DW_OP_plus_uconst:
3e43a32a
MS
14214 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14215 &bytes_read);
c906108c
SS
14216 i += bytes_read;
14217 break;
14218
14219 case DW_OP_minus:
f1bea926 14220 stack[stacki - 1] -= stack[stacki];
c906108c
SS
14221 stacki--;
14222 break;
14223
7a292a7a 14224 case DW_OP_deref:
7a292a7a 14225 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
14226 this using GDB's address_class enum. This is valid for partial
14227 global symbols, although the variable's address will be bogus
14228 in the psymtab. */
7a292a7a 14229 if (i < size)
4d3c2250 14230 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
14231 break;
14232
9d774e44 14233 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
14234 /* The top of the stack has the offset from the beginning
14235 of the thread control block at which the variable is located. */
14236 /* Nothing should follow this operator, so the top of stack would
14237 be returned. */
4cecd739
DJ
14238 /* This is valid for partial global symbols, but the variable's
14239 address will be bogus in the psymtab. */
9d774e44 14240 if (i < size)
4d3c2250 14241 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
14242 break;
14243
42be36b3
CT
14244 case DW_OP_GNU_uninit:
14245 break;
14246
c906108c 14247 default:
b1bfef65
TT
14248 {
14249 const char *name = dwarf_stack_op_name (op);
14250
14251 if (name)
14252 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14253 name);
14254 else
14255 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14256 op);
14257 }
14258
c906108c
SS
14259 return (stack[stacki]);
14260 }
d53d4ac5
TT
14261
14262 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14263 outside of the allocated space. Also enforce minimum>0. */
14264 if (stacki >= ARRAY_SIZE (stack) - 1)
14265 {
14266 complaint (&symfile_complaints,
14267 _("location description stack overflow"));
14268 return 0;
14269 }
14270
14271 if (stacki <= 0)
14272 {
14273 complaint (&symfile_complaints,
14274 _("location description stack underflow"));
14275 return 0;
14276 }
c906108c
SS
14277 }
14278 return (stack[stacki]);
14279}
14280
14281/* memory allocation interface */
14282
c906108c 14283static struct dwarf_block *
7b5a2f43 14284dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14285{
14286 struct dwarf_block *blk;
14287
14288 blk = (struct dwarf_block *)
7b5a2f43 14289 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14290 return (blk);
14291}
14292
14293static struct abbrev_info *
f3dd6933 14294dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14295{
14296 struct abbrev_info *abbrev;
14297
f3dd6933
DJ
14298 abbrev = (struct abbrev_info *)
14299 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14300 memset (abbrev, 0, sizeof (struct abbrev_info));
14301 return (abbrev);
14302}
14303
14304static struct die_info *
b60c80d6 14305dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14306{
14307 struct die_info *die;
b60c80d6
DJ
14308 size_t size = sizeof (struct die_info);
14309
14310 if (num_attrs > 1)
14311 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14312
b60c80d6 14313 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14314 memset (die, 0, sizeof (struct die_info));
14315 return (die);
14316}
2e276125
JB
14317
14318\f
14319/* Macro support. */
14320
2e276125
JB
14321/* Return the full name of file number I in *LH's file name table.
14322 Use COMP_DIR as the name of the current directory of the
14323 compilation. The result is allocated using xmalloc; the caller is
14324 responsible for freeing it. */
14325static char *
14326file_full_name (int file, struct line_header *lh, const char *comp_dir)
14327{
6a83a1e6
EZ
14328 /* Is the file number a valid index into the line header's file name
14329 table? Remember that file numbers start with one, not zero. */
14330 if (1 <= file && file <= lh->num_file_names)
14331 {
14332 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14333
6a83a1e6
EZ
14334 if (IS_ABSOLUTE_PATH (fe->name))
14335 return xstrdup (fe->name);
14336 else
14337 {
14338 const char *dir;
14339 int dir_len;
14340 char *full_name;
14341
14342 if (fe->dir_index)
14343 dir = lh->include_dirs[fe->dir_index - 1];
14344 else
14345 dir = comp_dir;
14346
14347 if (dir)
14348 {
14349 dir_len = strlen (dir);
14350 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14351 strcpy (full_name, dir);
14352 full_name[dir_len] = '/';
14353 strcpy (full_name + dir_len + 1, fe->name);
14354 return full_name;
14355 }
14356 else
14357 return xstrdup (fe->name);
14358 }
14359 }
2e276125
JB
14360 else
14361 {
6a83a1e6
EZ
14362 /* The compiler produced a bogus file number. We can at least
14363 record the macro definitions made in the file, even if we
14364 won't be able to find the file by name. */
14365 char fake_name[80];
9a619af0 14366
6a83a1e6 14367 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14368
6e70227d 14369 complaint (&symfile_complaints,
6a83a1e6
EZ
14370 _("bad file number in macro information (%d)"),
14371 file);
2e276125 14372
6a83a1e6 14373 return xstrdup (fake_name);
2e276125
JB
14374 }
14375}
14376
14377
14378static struct macro_source_file *
14379macro_start_file (int file, int line,
14380 struct macro_source_file *current_file,
14381 const char *comp_dir,
14382 struct line_header *lh, struct objfile *objfile)
14383{
14384 /* The full name of this source file. */
14385 char *full_name = file_full_name (file, lh, comp_dir);
14386
14387 /* We don't create a macro table for this compilation unit
14388 at all until we actually get a filename. */
14389 if (! pending_macros)
4a146b47 14390 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14391 objfile->macro_cache);
2e276125
JB
14392
14393 if (! current_file)
14394 /* If we have no current file, then this must be the start_file
14395 directive for the compilation unit's main source file. */
14396 current_file = macro_set_main (pending_macros, full_name);
14397 else
14398 current_file = macro_include (current_file, line, full_name);
14399
14400 xfree (full_name);
6e70227d 14401
2e276125
JB
14402 return current_file;
14403}
14404
14405
14406/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14407 followed by a null byte. */
14408static char *
14409copy_string (const char *buf, int len)
14410{
14411 char *s = xmalloc (len + 1);
9a619af0 14412
2e276125
JB
14413 memcpy (s, buf, len);
14414 s[len] = '\0';
2e276125
JB
14415 return s;
14416}
14417
14418
14419static const char *
14420consume_improper_spaces (const char *p, const char *body)
14421{
14422 if (*p == ' ')
14423 {
4d3c2250 14424 complaint (&symfile_complaints,
3e43a32a
MS
14425 _("macro definition contains spaces "
14426 "in formal argument list:\n`%s'"),
4d3c2250 14427 body);
2e276125
JB
14428
14429 while (*p == ' ')
14430 p++;
14431 }
14432
14433 return p;
14434}
14435
14436
14437static void
14438parse_macro_definition (struct macro_source_file *file, int line,
14439 const char *body)
14440{
14441 const char *p;
14442
14443 /* The body string takes one of two forms. For object-like macro
14444 definitions, it should be:
14445
14446 <macro name> " " <definition>
14447
14448 For function-like macro definitions, it should be:
14449
14450 <macro name> "() " <definition>
14451 or
14452 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14453
14454 Spaces may appear only where explicitly indicated, and in the
14455 <definition>.
14456
14457 The Dwarf 2 spec says that an object-like macro's name is always
14458 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14459 the space when the macro's definition is the empty string.
2e276125
JB
14460
14461 The Dwarf 2 spec says that there should be no spaces between the
14462 formal arguments in a function-like macro's formal argument list,
14463 but versions of GCC around March 2002 include spaces after the
14464 commas. */
14465
14466
14467 /* Find the extent of the macro name. The macro name is terminated
14468 by either a space or null character (for an object-like macro) or
14469 an opening paren (for a function-like macro). */
14470 for (p = body; *p; p++)
14471 if (*p == ' ' || *p == '(')
14472 break;
14473
14474 if (*p == ' ' || *p == '\0')
14475 {
14476 /* It's an object-like macro. */
14477 int name_len = p - body;
14478 char *name = copy_string (body, name_len);
14479 const char *replacement;
14480
14481 if (*p == ' ')
14482 replacement = body + name_len + 1;
14483 else
14484 {
4d3c2250 14485 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14486 replacement = body + name_len;
14487 }
6e70227d 14488
2e276125
JB
14489 macro_define_object (file, line, name, replacement);
14490
14491 xfree (name);
14492 }
14493 else if (*p == '(')
14494 {
14495 /* It's a function-like macro. */
14496 char *name = copy_string (body, p - body);
14497 int argc = 0;
14498 int argv_size = 1;
14499 char **argv = xmalloc (argv_size * sizeof (*argv));
14500
14501 p++;
14502
14503 p = consume_improper_spaces (p, body);
14504
14505 /* Parse the formal argument list. */
14506 while (*p && *p != ')')
14507 {
14508 /* Find the extent of the current argument name. */
14509 const char *arg_start = p;
14510
14511 while (*p && *p != ',' && *p != ')' && *p != ' ')
14512 p++;
14513
14514 if (! *p || p == arg_start)
4d3c2250 14515 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14516 else
14517 {
14518 /* Make sure argv has room for the new argument. */
14519 if (argc >= argv_size)
14520 {
14521 argv_size *= 2;
14522 argv = xrealloc (argv, argv_size * sizeof (*argv));
14523 }
14524
14525 argv[argc++] = copy_string (arg_start, p - arg_start);
14526 }
14527
14528 p = consume_improper_spaces (p, body);
14529
14530 /* Consume the comma, if present. */
14531 if (*p == ',')
14532 {
14533 p++;
14534
14535 p = consume_improper_spaces (p, body);
14536 }
14537 }
14538
14539 if (*p == ')')
14540 {
14541 p++;
14542
14543 if (*p == ' ')
14544 /* Perfectly formed definition, no complaints. */
14545 macro_define_function (file, line, name,
6e70227d 14546 argc, (const char **) argv,
2e276125
JB
14547 p + 1);
14548 else if (*p == '\0')
14549 {
14550 /* Complain, but do define it. */
4d3c2250 14551 dwarf2_macro_malformed_definition_complaint (body);
2e276125 14552 macro_define_function (file, line, name,
6e70227d 14553 argc, (const char **) argv,
2e276125
JB
14554 p);
14555 }
14556 else
14557 /* Just complain. */
4d3c2250 14558 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14559 }
14560 else
14561 /* Just complain. */
4d3c2250 14562 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14563
14564 xfree (name);
14565 {
14566 int i;
14567
14568 for (i = 0; i < argc; i++)
14569 xfree (argv[i]);
14570 }
14571 xfree (argv);
14572 }
14573 else
4d3c2250 14574 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14575}
14576
14577
14578static void
14579dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14580 char *comp_dir, bfd *abfd,
e7c27a73 14581 struct dwarf2_cu *cu)
2e276125 14582{
fe1b8b76 14583 gdb_byte *mac_ptr, *mac_end;
2e276125 14584 struct macro_source_file *current_file = 0;
757a13d0
JK
14585 enum dwarf_macinfo_record_type macinfo_type;
14586 int at_commandline;
2e276125 14587
be391dca
TT
14588 dwarf2_read_section (dwarf2_per_objfile->objfile,
14589 &dwarf2_per_objfile->macinfo);
dce234bc 14590 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 14591 {
e2e0b3e5 14592 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
14593 return;
14594 }
14595
757a13d0
JK
14596 /* First pass: Find the name of the base filename.
14597 This filename is needed in order to process all macros whose definition
14598 (or undefinition) comes from the command line. These macros are defined
14599 before the first DW_MACINFO_start_file entry, and yet still need to be
14600 associated to the base file.
14601
14602 To determine the base file name, we scan the macro definitions until we
14603 reach the first DW_MACINFO_start_file entry. We then initialize
14604 CURRENT_FILE accordingly so that any macro definition found before the
14605 first DW_MACINFO_start_file can still be associated to the base file. */
14606
dce234bc
PP
14607 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14608 mac_end = dwarf2_per_objfile->macinfo.buffer
14609 + dwarf2_per_objfile->macinfo.size;
2e276125 14610
757a13d0 14611 do
2e276125 14612 {
2e276125
JB
14613 /* Do we at least have room for a macinfo type byte? */
14614 if (mac_ptr >= mac_end)
14615 {
757a13d0 14616 /* Complaint is printed during the second pass as GDB will probably
3e43a32a
MS
14617 stop the first pass earlier upon finding
14618 DW_MACINFO_start_file. */
757a13d0 14619 break;
2e276125
JB
14620 }
14621
14622 macinfo_type = read_1_byte (abfd, mac_ptr);
14623 mac_ptr++;
14624
14625 switch (macinfo_type)
14626 {
14627 /* A zero macinfo type indicates the end of the macro
14628 information. */
14629 case 0:
757a13d0
JK
14630 break;
14631
14632 case DW_MACINFO_define:
14633 case DW_MACINFO_undef:
14634 /* Only skip the data by MAC_PTR. */
14635 {
14636 unsigned int bytes_read;
14637
14638 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14639 mac_ptr += bytes_read;
9b1c24c8 14640 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14641 mac_ptr += bytes_read;
14642 }
14643 break;
14644
14645 case DW_MACINFO_start_file:
14646 {
14647 unsigned int bytes_read;
14648 int line, file;
14649
14650 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14651 mac_ptr += bytes_read;
14652 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14653 mac_ptr += bytes_read;
14654
3e43a32a
MS
14655 current_file = macro_start_file (file, line, current_file,
14656 comp_dir, lh, cu->objfile);
757a13d0
JK
14657 }
14658 break;
14659
14660 case DW_MACINFO_end_file:
14661 /* No data to skip by MAC_PTR. */
14662 break;
14663
14664 case DW_MACINFO_vendor_ext:
14665 /* Only skip the data by MAC_PTR. */
14666 {
14667 unsigned int bytes_read;
14668
14669 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14670 mac_ptr += bytes_read;
9b1c24c8 14671 read_direct_string (abfd, mac_ptr, &bytes_read);
757a13d0
JK
14672 mac_ptr += bytes_read;
14673 }
14674 break;
14675
14676 default:
14677 break;
14678 }
14679 } while (macinfo_type != 0 && current_file == NULL);
14680
14681 /* Second pass: Process all entries.
14682
14683 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14684 command-line macro definitions/undefinitions. This flag is unset when we
14685 reach the first DW_MACINFO_start_file entry. */
14686
dce234bc 14687 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
14688
14689 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14690 GDB is still reading the definitions from command line. First
14691 DW_MACINFO_start_file will need to be ignored as it was already executed
14692 to create CURRENT_FILE for the main source holding also the command line
14693 definitions. On first met DW_MACINFO_start_file this flag is reset to
14694 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14695
14696 at_commandline = 1;
14697
14698 do
14699 {
14700 /* Do we at least have room for a macinfo type byte? */
14701 if (mac_ptr >= mac_end)
14702 {
14703 dwarf2_macros_too_long_complaint ();
14704 break;
14705 }
14706
14707 macinfo_type = read_1_byte (abfd, mac_ptr);
14708 mac_ptr++;
14709
14710 switch (macinfo_type)
14711 {
14712 /* A zero macinfo type indicates the end of the macro
14713 information. */
14714 case 0:
14715 break;
2e276125
JB
14716
14717 case DW_MACINFO_define:
14718 case DW_MACINFO_undef:
14719 {
891d2f0b 14720 unsigned int bytes_read;
2e276125
JB
14721 int line;
14722 char *body;
14723
14724 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14725 mac_ptr += bytes_read;
9b1c24c8 14726 body = read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14727 mac_ptr += bytes_read;
14728
14729 if (! current_file)
757a13d0
JK
14730 {
14731 /* DWARF violation as no main source is present. */
14732 complaint (&symfile_complaints,
14733 _("debug info with no main source gives macro %s "
14734 "on line %d: %s"),
6e70227d
DE
14735 macinfo_type == DW_MACINFO_define ?
14736 _("definition") :
905e0470
PM
14737 macinfo_type == DW_MACINFO_undef ?
14738 _("undefinition") :
14739 _("something-or-other"), line, body);
757a13d0
JK
14740 break;
14741 }
3e43a32a
MS
14742 if ((line == 0 && !at_commandline)
14743 || (line != 0 && at_commandline))
4d3c2250 14744 complaint (&symfile_complaints,
757a13d0
JK
14745 _("debug info gives %s macro %s with %s line %d: %s"),
14746 at_commandline ? _("command-line") : _("in-file"),
905e0470 14747 macinfo_type == DW_MACINFO_define ?
6e70227d 14748 _("definition") :
905e0470
PM
14749 macinfo_type == DW_MACINFO_undef ?
14750 _("undefinition") :
14751 _("something-or-other"),
757a13d0
JK
14752 line == 0 ? _("zero") : _("non-zero"), line, body);
14753
14754 if (macinfo_type == DW_MACINFO_define)
14755 parse_macro_definition (current_file, line, body);
14756 else if (macinfo_type == DW_MACINFO_undef)
14757 macro_undef (current_file, line, body);
2e276125
JB
14758 }
14759 break;
14760
14761 case DW_MACINFO_start_file:
14762 {
891d2f0b 14763 unsigned int bytes_read;
2e276125
JB
14764 int line, file;
14765
14766 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14767 mac_ptr += bytes_read;
14768 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14769 mac_ptr += bytes_read;
14770
3e43a32a
MS
14771 if ((line == 0 && !at_commandline)
14772 || (line != 0 && at_commandline))
757a13d0
JK
14773 complaint (&symfile_complaints,
14774 _("debug info gives source %d included "
14775 "from %s at %s line %d"),
14776 file, at_commandline ? _("command-line") : _("file"),
14777 line == 0 ? _("zero") : _("non-zero"), line);
14778
14779 if (at_commandline)
14780 {
14781 /* This DW_MACINFO_start_file was executed in the pass one. */
14782 at_commandline = 0;
14783 }
14784 else
14785 current_file = macro_start_file (file, line,
14786 current_file, comp_dir,
14787 lh, cu->objfile);
2e276125
JB
14788 }
14789 break;
14790
14791 case DW_MACINFO_end_file:
14792 if (! current_file)
4d3c2250 14793 complaint (&symfile_complaints,
3e43a32a
MS
14794 _("macro debug info has an unmatched "
14795 "`close_file' directive"));
2e276125
JB
14796 else
14797 {
14798 current_file = current_file->included_by;
14799 if (! current_file)
14800 {
14801 enum dwarf_macinfo_record_type next_type;
14802
14803 /* GCC circa March 2002 doesn't produce the zero
14804 type byte marking the end of the compilation
14805 unit. Complain if it's not there, but exit no
14806 matter what. */
14807
14808 /* Do we at least have room for a macinfo type byte? */
14809 if (mac_ptr >= mac_end)
14810 {
4d3c2250 14811 dwarf2_macros_too_long_complaint ();
2e276125
JB
14812 return;
14813 }
14814
14815 /* We don't increment mac_ptr here, so this is just
14816 a look-ahead. */
14817 next_type = read_1_byte (abfd, mac_ptr);
14818 if (next_type != 0)
4d3c2250 14819 complaint (&symfile_complaints,
3e43a32a
MS
14820 _("no terminating 0-type entry for "
14821 "macros in `.debug_macinfo' section"));
2e276125
JB
14822
14823 return;
14824 }
14825 }
14826 break;
14827
14828 case DW_MACINFO_vendor_ext:
14829 {
891d2f0b 14830 unsigned int bytes_read;
2e276125 14831 int constant;
2e276125
JB
14832
14833 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14834 mac_ptr += bytes_read;
e8e80198 14835 read_direct_string (abfd, mac_ptr, &bytes_read);
2e276125
JB
14836 mac_ptr += bytes_read;
14837
14838 /* We don't recognize any vendor extensions. */
14839 }
14840 break;
14841 }
757a13d0 14842 } while (macinfo_type != 0);
2e276125 14843}
8e19ed76
PS
14844
14845/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 14846 if so return true else false. */
8e19ed76
PS
14847static int
14848attr_form_is_block (struct attribute *attr)
14849{
14850 return (attr == NULL ? 0 :
14851 attr->form == DW_FORM_block1
14852 || attr->form == DW_FORM_block2
14853 || attr->form == DW_FORM_block4
2dc7f7b3
TT
14854 || attr->form == DW_FORM_block
14855 || attr->form == DW_FORM_exprloc);
8e19ed76 14856}
4c2df51b 14857
c6a0999f
JB
14858/* Return non-zero if ATTR's value is a section offset --- classes
14859 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14860 You may use DW_UNSND (attr) to retrieve such offsets.
14861
14862 Section 7.5.4, "Attribute Encodings", explains that no attribute
14863 may have a value that belongs to more than one of these classes; it
14864 would be ambiguous if we did, because we use the same forms for all
14865 of them. */
3690dd37
JB
14866static int
14867attr_form_is_section_offset (struct attribute *attr)
14868{
14869 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
14870 || attr->form == DW_FORM_data8
14871 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
14872}
14873
14874
14875/* Return non-zero if ATTR's value falls in the 'constant' class, or
14876 zero otherwise. When this function returns true, you can apply
14877 dwarf2_get_attr_constant_value to it.
14878
14879 However, note that for some attributes you must check
14880 attr_form_is_section_offset before using this test. DW_FORM_data4
14881 and DW_FORM_data8 are members of both the constant class, and of
14882 the classes that contain offsets into other debug sections
14883 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14884 that, if an attribute's can be either a constant or one of the
14885 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14886 taken as section offsets, not constants. */
14887static int
14888attr_form_is_constant (struct attribute *attr)
14889{
14890 switch (attr->form)
14891 {
14892 case DW_FORM_sdata:
14893 case DW_FORM_udata:
14894 case DW_FORM_data1:
14895 case DW_FORM_data2:
14896 case DW_FORM_data4:
14897 case DW_FORM_data8:
14898 return 1;
14899 default:
14900 return 0;
14901 }
14902}
14903
8cf6f0b1
TT
14904/* A helper function that fills in a dwarf2_loclist_baton. */
14905
14906static void
14907fill_in_loclist_baton (struct dwarf2_cu *cu,
14908 struct dwarf2_loclist_baton *baton,
14909 struct attribute *attr)
14910{
14911 dwarf2_read_section (dwarf2_per_objfile->objfile,
14912 &dwarf2_per_objfile->loc);
14913
14914 baton->per_cu = cu->per_cu;
14915 gdb_assert (baton->per_cu);
14916 /* We don't know how long the location list is, but make sure we
14917 don't run off the edge of the section. */
14918 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14919 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14920 baton->base_address = cu->base_address;
14921}
14922
4c2df51b
DJ
14923static void
14924dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 14925 struct dwarf2_cu *cu)
4c2df51b 14926{
3690dd37 14927 if (attr_form_is_section_offset (attr)
99bcc461
DJ
14928 /* ".debug_loc" may not exist at all, or the offset may be outside
14929 the section. If so, fall through to the complaint in the
14930 other branch. */
9e0ac564
TT
14931 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14932 &dwarf2_per_objfile->loc))
4c2df51b 14933 {
0d53c4c4 14934 struct dwarf2_loclist_baton *baton;
4c2df51b 14935
4a146b47 14936 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14937 sizeof (struct dwarf2_loclist_baton));
4c2df51b 14938
8cf6f0b1 14939 fill_in_loclist_baton (cu, baton, attr);
be391dca 14940
d00adf39 14941 if (cu->base_known == 0)
0d53c4c4 14942 complaint (&symfile_complaints,
3e43a32a
MS
14943 _("Location list used without "
14944 "specifying the CU base address."));
4c2df51b 14945
768a979c 14946 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
14947 SYMBOL_LOCATION_BATON (sym) = baton;
14948 }
14949 else
14950 {
14951 struct dwarf2_locexpr_baton *baton;
14952
4a146b47 14953 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 14954 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
14955 baton->per_cu = cu->per_cu;
14956 gdb_assert (baton->per_cu);
0d53c4c4
DJ
14957
14958 if (attr_form_is_block (attr))
14959 {
14960 /* Note that we're just copying the block's data pointer
14961 here, not the actual data. We're still pointing into the
6502dd73
DJ
14962 info_buffer for SYM's objfile; right now we never release
14963 that buffer, but when we do clean up properly this may
14964 need to change. */
0d53c4c4
DJ
14965 baton->size = DW_BLOCK (attr)->size;
14966 baton->data = DW_BLOCK (attr)->data;
14967 }
14968 else
14969 {
14970 dwarf2_invalid_attrib_class_complaint ("location description",
14971 SYMBOL_NATURAL_NAME (sym));
14972 baton->size = 0;
14973 baton->data = NULL;
14974 }
6e70227d 14975
768a979c 14976 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
14977 SYMBOL_LOCATION_BATON (sym) = baton;
14978 }
4c2df51b 14979}
6502dd73 14980
9aa1f1e3
TT
14981/* Return the OBJFILE associated with the compilation unit CU. If CU
14982 came from a separate debuginfo file, then the master objfile is
14983 returned. */
ae0d2f24
UW
14984
14985struct objfile *
14986dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14987{
9291a0cd 14988 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
14989
14990 /* Return the master objfile, so that we can report and look up the
14991 correct file containing this variable. */
14992 if (objfile->separate_debug_objfile_backlink)
14993 objfile = objfile->separate_debug_objfile_backlink;
14994
14995 return objfile;
14996}
14997
14998/* Return the address size given in the compilation unit header for CU. */
14999
15000CORE_ADDR
15001dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15002{
15003 if (per_cu->cu)
15004 return per_cu->cu->header.addr_size;
15005 else
15006 {
15007 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 15008 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15009 struct dwarf2_per_objfile *per_objfile
15010 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 15011 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 15012 struct comp_unit_head cu_header;
9a619af0 15013
ae0d2f24
UW
15014 memset (&cu_header, 0, sizeof cu_header);
15015 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15016 return cu_header.addr_size;
15017 }
15018}
15019
9eae7c52
TT
15020/* Return the offset size given in the compilation unit header for CU. */
15021
15022int
15023dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15024{
15025 if (per_cu->cu)
15026 return per_cu->cu->header.offset_size;
15027 else
15028 {
15029 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 15030 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
15031 struct dwarf2_per_objfile *per_objfile
15032 = objfile_data (objfile, dwarf2_objfile_data_key);
15033 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
15034 struct comp_unit_head cu_header;
15035
15036 memset (&cu_header, 0, sizeof cu_header);
15037 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15038 return cu_header.offset_size;
15039 }
15040}
15041
9aa1f1e3
TT
15042/* Return the text offset of the CU. The returned offset comes from
15043 this CU's objfile. If this objfile came from a separate debuginfo
15044 file, then the offset may be different from the corresponding
15045 offset in the parent objfile. */
15046
15047CORE_ADDR
15048dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15049{
bb3fa9d0 15050 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15051
15052 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15053}
15054
348e048f
DE
15055/* Locate the .debug_info compilation unit from CU's objfile which contains
15056 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15057
15058static struct dwarf2_per_cu_data *
c764a876 15059dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15060 struct objfile *objfile)
15061{
15062 struct dwarf2_per_cu_data *this_cu;
15063 int low, high;
15064
ae038cb0
DJ
15065 low = 0;
15066 high = dwarf2_per_objfile->n_comp_units - 1;
15067 while (high > low)
15068 {
15069 int mid = low + (high - low) / 2;
9a619af0 15070
ae038cb0
DJ
15071 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15072 high = mid;
15073 else
15074 low = mid + 1;
15075 }
15076 gdb_assert (low == high);
15077 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15078 {
10b3939b 15079 if (low == 0)
8a3fe4f8
AC
15080 error (_("Dwarf Error: could not find partial DIE containing "
15081 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15082 (long) offset, bfd_get_filename (objfile->obfd));
15083
ae038cb0
DJ
15084 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15085 return dwarf2_per_objfile->all_comp_units[low-1];
15086 }
15087 else
15088 {
15089 this_cu = dwarf2_per_objfile->all_comp_units[low];
15090 if (low == dwarf2_per_objfile->n_comp_units - 1
15091 && offset >= this_cu->offset + this_cu->length)
c764a876 15092 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15093 gdb_assert (offset < this_cu->offset + this_cu->length);
15094 return this_cu;
15095 }
15096}
15097
10b3939b
DJ
15098/* Locate the compilation unit from OBJFILE which is located at exactly
15099 OFFSET. Raises an error on failure. */
15100
ae038cb0 15101static struct dwarf2_per_cu_data *
c764a876 15102dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
15103{
15104 struct dwarf2_per_cu_data *this_cu;
9a619af0 15105
ae038cb0
DJ
15106 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15107 if (this_cu->offset != offset)
c764a876 15108 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
15109 return this_cu;
15110}
15111
9816fde3 15112/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 15113
9816fde3
JK
15114static void
15115init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 15116{
9816fde3 15117 memset (cu, 0, sizeof (*cu));
93311388
DE
15118 cu->objfile = objfile;
15119 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15120}
15121
15122/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15123
15124static void
15125prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15126{
15127 struct attribute *attr;
15128
15129 /* Set the language we're debugging. */
15130 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15131 if (attr)
15132 set_cu_language (DW_UNSND (attr), cu);
15133 else
9cded63f
TT
15134 {
15135 cu->language = language_minimal;
15136 cu->language_defn = language_def (cu->language);
15137 }
93311388
DE
15138}
15139
ae038cb0
DJ
15140/* Release one cached compilation unit, CU. We unlink it from the tree
15141 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15142 the caller is responsible for that.
15143 NOTE: DATA is a void * because this function is also used as a
15144 cleanup routine. */
ae038cb0
DJ
15145
15146static void
15147free_one_comp_unit (void *data)
15148{
15149 struct dwarf2_cu *cu = data;
15150
15151 if (cu->per_cu != NULL)
15152 cu->per_cu->cu = NULL;
15153 cu->per_cu = NULL;
15154
15155 obstack_free (&cu->comp_unit_obstack, NULL);
15156
15157 xfree (cu);
15158}
15159
72bf9492 15160/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15161 when we're finished with it. We can't free the pointer itself, but be
15162 sure to unlink it from the cache. Also release any associated storage
15163 and perform cache maintenance.
72bf9492
DJ
15164
15165 Only used during partial symbol parsing. */
15166
15167static void
15168free_stack_comp_unit (void *data)
15169{
15170 struct dwarf2_cu *cu = data;
15171
15172 obstack_free (&cu->comp_unit_obstack, NULL);
15173 cu->partial_dies = NULL;
ae038cb0
DJ
15174
15175 if (cu->per_cu != NULL)
15176 {
15177 /* This compilation unit is on the stack in our caller, so we
15178 should not xfree it. Just unlink it. */
15179 cu->per_cu->cu = NULL;
15180 cu->per_cu = NULL;
15181
15182 /* If we had a per-cu pointer, then we may have other compilation
15183 units loaded, so age them now. */
15184 age_cached_comp_units ();
15185 }
15186}
15187
15188/* Free all cached compilation units. */
15189
15190static void
15191free_cached_comp_units (void *data)
15192{
15193 struct dwarf2_per_cu_data *per_cu, **last_chain;
15194
15195 per_cu = dwarf2_per_objfile->read_in_chain;
15196 last_chain = &dwarf2_per_objfile->read_in_chain;
15197 while (per_cu != NULL)
15198 {
15199 struct dwarf2_per_cu_data *next_cu;
15200
15201 next_cu = per_cu->cu->read_in_chain;
15202
15203 free_one_comp_unit (per_cu->cu);
15204 *last_chain = next_cu;
15205
15206 per_cu = next_cu;
15207 }
15208}
15209
15210/* Increase the age counter on each cached compilation unit, and free
15211 any that are too old. */
15212
15213static void
15214age_cached_comp_units (void)
15215{
15216 struct dwarf2_per_cu_data *per_cu, **last_chain;
15217
15218 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15219 per_cu = dwarf2_per_objfile->read_in_chain;
15220 while (per_cu != NULL)
15221 {
15222 per_cu->cu->last_used ++;
15223 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15224 dwarf2_mark (per_cu->cu);
15225 per_cu = per_cu->cu->read_in_chain;
15226 }
15227
15228 per_cu = dwarf2_per_objfile->read_in_chain;
15229 last_chain = &dwarf2_per_objfile->read_in_chain;
15230 while (per_cu != NULL)
15231 {
15232 struct dwarf2_per_cu_data *next_cu;
15233
15234 next_cu = per_cu->cu->read_in_chain;
15235
15236 if (!per_cu->cu->mark)
15237 {
15238 free_one_comp_unit (per_cu->cu);
15239 *last_chain = next_cu;
15240 }
15241 else
15242 last_chain = &per_cu->cu->read_in_chain;
15243
15244 per_cu = next_cu;
15245 }
15246}
15247
15248/* Remove a single compilation unit from the cache. */
15249
15250static void
15251free_one_cached_comp_unit (void *target_cu)
15252{
15253 struct dwarf2_per_cu_data *per_cu, **last_chain;
15254
15255 per_cu = dwarf2_per_objfile->read_in_chain;
15256 last_chain = &dwarf2_per_objfile->read_in_chain;
15257 while (per_cu != NULL)
15258 {
15259 struct dwarf2_per_cu_data *next_cu;
15260
15261 next_cu = per_cu->cu->read_in_chain;
15262
15263 if (per_cu->cu == target_cu)
15264 {
15265 free_one_comp_unit (per_cu->cu);
15266 *last_chain = next_cu;
15267 break;
15268 }
15269 else
15270 last_chain = &per_cu->cu->read_in_chain;
15271
15272 per_cu = next_cu;
15273 }
15274}
15275
fe3e1990
DJ
15276/* Release all extra memory associated with OBJFILE. */
15277
15278void
15279dwarf2_free_objfile (struct objfile *objfile)
15280{
15281 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15282
15283 if (dwarf2_per_objfile == NULL)
15284 return;
15285
15286 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15287 free_cached_comp_units (NULL);
15288
7b9f3c50
DE
15289 if (dwarf2_per_objfile->quick_file_names_table)
15290 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 15291
fe3e1990
DJ
15292 /* Everything else should be on the objfile obstack. */
15293}
15294
1c379e20
DJ
15295/* A pair of DIE offset and GDB type pointer. We store these
15296 in a hash table separate from the DIEs, and preserve them
15297 when the DIEs are flushed out of cache. */
15298
15299struct dwarf2_offset_and_type
15300{
15301 unsigned int offset;
15302 struct type *type;
15303};
15304
15305/* Hash function for a dwarf2_offset_and_type. */
15306
15307static hashval_t
15308offset_and_type_hash (const void *item)
15309{
15310 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 15311
1c379e20
DJ
15312 return ofs->offset;
15313}
15314
15315/* Equality function for a dwarf2_offset_and_type. */
15316
15317static int
15318offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15319{
15320 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15321 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 15322
1c379e20
DJ
15323 return ofs_lhs->offset == ofs_rhs->offset;
15324}
15325
15326/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
15327 table if necessary. For convenience, return TYPE.
15328
15329 The DIEs reading must have careful ordering to:
15330 * Not cause infite loops trying to read in DIEs as a prerequisite for
15331 reading current DIE.
15332 * Not trying to dereference contents of still incompletely read in types
15333 while reading in other DIEs.
15334 * Enable referencing still incompletely read in types just by a pointer to
15335 the type without accessing its fields.
15336
15337 Therefore caller should follow these rules:
15338 * Try to fetch any prerequisite types we may need to build this DIE type
15339 before building the type and calling set_die_type.
e71ec853 15340 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
15341 possible before fetching more types to complete the current type.
15342 * Make the type as complete as possible before fetching more types. */
1c379e20 15343
f792889a 15344static struct type *
1c379e20
DJ
15345set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15346{
15347 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
15348 struct objfile *objfile = cu->objfile;
15349 htab_t *type_hash_ptr;
1c379e20 15350
b4ba55a1
JB
15351 /* For Ada types, make sure that the gnat-specific data is always
15352 initialized (if not already set). There are a few types where
15353 we should not be doing so, because the type-specific area is
15354 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15355 where the type-specific area is used to store the floatformat).
15356 But this is not a problem, because the gnat-specific information
15357 is actually not needed for these types. */
15358 if (need_gnat_info (cu)
15359 && TYPE_CODE (type) != TYPE_CODE_FUNC
15360 && TYPE_CODE (type) != TYPE_CODE_FLT
15361 && !HAVE_GNAT_AUX_INFO (type))
15362 INIT_GNAT_SPECIFIC (type);
15363
673bfd45
DE
15364 if (cu->per_cu->from_debug_types)
15365 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15366 else
15367 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15368
15369 if (*type_hash_ptr == NULL)
f792889a 15370 {
673bfd45
DE
15371 *type_hash_ptr
15372 = htab_create_alloc_ex (127,
f792889a
DJ
15373 offset_and_type_hash,
15374 offset_and_type_eq,
15375 NULL,
673bfd45 15376 &objfile->objfile_obstack,
f792889a
DJ
15377 hashtab_obstack_allocate,
15378 dummy_obstack_deallocate);
f792889a 15379 }
1c379e20
DJ
15380
15381 ofs.offset = die->offset;
15382 ofs.type = type;
15383 slot = (struct dwarf2_offset_and_type **)
673bfd45 15384 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
15385 if (*slot)
15386 complaint (&symfile_complaints,
15387 _("A problem internal to GDB: DIE 0x%x has type already set"),
15388 die->offset);
673bfd45 15389 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 15390 **slot = ofs;
f792889a 15391 return type;
1c379e20
DJ
15392}
15393
673bfd45
DE
15394/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15395 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
15396
15397static struct type *
673bfd45
DE
15398get_die_type_at_offset (unsigned int offset,
15399 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
15400{
15401 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 15402 htab_t type_hash;
f792889a 15403
673bfd45
DE
15404 if (per_cu->from_debug_types)
15405 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15406 else
15407 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
15408 if (type_hash == NULL)
15409 return NULL;
1c379e20 15410
673bfd45 15411 ofs.offset = offset;
1c379e20
DJ
15412 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15413 if (slot)
15414 return slot->type;
15415 else
15416 return NULL;
15417}
15418
673bfd45
DE
15419/* Look up the type for DIE in the appropriate type_hash table,
15420 or return NULL if DIE does not have a saved type. */
15421
15422static struct type *
15423get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15424{
15425 return get_die_type_at_offset (die->offset, cu->per_cu);
15426}
15427
10b3939b
DJ
15428/* Add a dependence relationship from CU to REF_PER_CU. */
15429
15430static void
15431dwarf2_add_dependence (struct dwarf2_cu *cu,
15432 struct dwarf2_per_cu_data *ref_per_cu)
15433{
15434 void **slot;
15435
15436 if (cu->dependencies == NULL)
15437 cu->dependencies
15438 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15439 NULL, &cu->comp_unit_obstack,
15440 hashtab_obstack_allocate,
15441 dummy_obstack_deallocate);
15442
15443 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15444 if (*slot == NULL)
15445 *slot = ref_per_cu;
15446}
1c379e20 15447
f504f079
DE
15448/* Subroutine of dwarf2_mark to pass to htab_traverse.
15449 Set the mark field in every compilation unit in the
ae038cb0
DJ
15450 cache that we must keep because we are keeping CU. */
15451
10b3939b
DJ
15452static int
15453dwarf2_mark_helper (void **slot, void *data)
15454{
15455 struct dwarf2_per_cu_data *per_cu;
15456
15457 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
15458
15459 /* cu->dependencies references may not yet have been ever read if QUIT aborts
15460 reading of the chain. As such dependencies remain valid it is not much
15461 useful to track and undo them during QUIT cleanups. */
15462 if (per_cu->cu == NULL)
15463 return 1;
15464
10b3939b
DJ
15465 if (per_cu->cu->mark)
15466 return 1;
15467 per_cu->cu->mark = 1;
15468
15469 if (per_cu->cu->dependencies != NULL)
15470 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15471
15472 return 1;
15473}
15474
f504f079
DE
15475/* Set the mark field in CU and in every other compilation unit in the
15476 cache that we must keep because we are keeping CU. */
15477
ae038cb0
DJ
15478static void
15479dwarf2_mark (struct dwarf2_cu *cu)
15480{
15481 if (cu->mark)
15482 return;
15483 cu->mark = 1;
10b3939b
DJ
15484 if (cu->dependencies != NULL)
15485 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
15486}
15487
15488static void
15489dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15490{
15491 while (per_cu)
15492 {
15493 per_cu->cu->mark = 0;
15494 per_cu = per_cu->cu->read_in_chain;
15495 }
72bf9492
DJ
15496}
15497
72bf9492
DJ
15498/* Trivial hash function for partial_die_info: the hash value of a DIE
15499 is its offset in .debug_info for this objfile. */
15500
15501static hashval_t
15502partial_die_hash (const void *item)
15503{
15504 const struct partial_die_info *part_die = item;
9a619af0 15505
72bf9492
DJ
15506 return part_die->offset;
15507}
15508
15509/* Trivial comparison function for partial_die_info structures: two DIEs
15510 are equal if they have the same offset. */
15511
15512static int
15513partial_die_eq (const void *item_lhs, const void *item_rhs)
15514{
15515 const struct partial_die_info *part_die_lhs = item_lhs;
15516 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 15517
72bf9492
DJ
15518 return part_die_lhs->offset == part_die_rhs->offset;
15519}
15520
ae038cb0
DJ
15521static struct cmd_list_element *set_dwarf2_cmdlist;
15522static struct cmd_list_element *show_dwarf2_cmdlist;
15523
15524static void
15525set_dwarf2_cmd (char *args, int from_tty)
15526{
15527 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15528}
15529
15530static void
15531show_dwarf2_cmd (char *args, int from_tty)
6e70227d 15532{
ae038cb0
DJ
15533 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15534}
15535
dce234bc
PP
15536/* If section described by INFO was mmapped, munmap it now. */
15537
15538static void
15539munmap_section_buffer (struct dwarf2_section_info *info)
15540{
b315ab21 15541 if (info->map_addr != NULL)
dce234bc
PP
15542 {
15543#ifdef HAVE_MMAP
b315ab21 15544 int res;
9a619af0 15545
b315ab21
TG
15546 res = munmap (info->map_addr, info->map_len);
15547 gdb_assert (res == 0);
dce234bc
PP
15548#else
15549 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 15550 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
15551#endif
15552 }
15553}
15554
15555/* munmap debug sections for OBJFILE, if necessary. */
15556
15557static void
c1bd65d0 15558dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
15559{
15560 struct dwarf2_per_objfile *data = d;
9a619af0 15561
16be1145
DE
15562 /* This is sorted according to the order they're defined in to make it easier
15563 to keep in sync. */
dce234bc
PP
15564 munmap_section_buffer (&data->info);
15565 munmap_section_buffer (&data->abbrev);
15566 munmap_section_buffer (&data->line);
16be1145 15567 munmap_section_buffer (&data->loc);
dce234bc 15568 munmap_section_buffer (&data->macinfo);
16be1145 15569 munmap_section_buffer (&data->str);
dce234bc 15570 munmap_section_buffer (&data->ranges);
16be1145 15571 munmap_section_buffer (&data->types);
dce234bc
PP
15572 munmap_section_buffer (&data->frame);
15573 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
15574 munmap_section_buffer (&data->gdb_index);
15575}
15576
15577\f
ae2de4f8 15578/* The "save gdb-index" command. */
9291a0cd
TT
15579
15580/* The contents of the hash table we create when building the string
15581 table. */
15582struct strtab_entry
15583{
15584 offset_type offset;
15585 const char *str;
15586};
15587
559a7a62
JK
15588/* Hash function for a strtab_entry.
15589
15590 Function is used only during write_hash_table so no index format backward
15591 compatibility is needed. */
b89be57b 15592
9291a0cd
TT
15593static hashval_t
15594hash_strtab_entry (const void *e)
15595{
15596 const struct strtab_entry *entry = e;
559a7a62 15597 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
15598}
15599
15600/* Equality function for a strtab_entry. */
b89be57b 15601
9291a0cd
TT
15602static int
15603eq_strtab_entry (const void *a, const void *b)
15604{
15605 const struct strtab_entry *ea = a;
15606 const struct strtab_entry *eb = b;
15607 return !strcmp (ea->str, eb->str);
15608}
15609
15610/* Create a strtab_entry hash table. */
b89be57b 15611
9291a0cd
TT
15612static htab_t
15613create_strtab (void)
15614{
15615 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15616 xfree, xcalloc, xfree);
15617}
15618
15619/* Add a string to the constant pool. Return the string's offset in
15620 host order. */
b89be57b 15621
9291a0cd
TT
15622static offset_type
15623add_string (htab_t table, struct obstack *cpool, const char *str)
15624{
15625 void **slot;
15626 struct strtab_entry entry;
15627 struct strtab_entry *result;
15628
15629 entry.str = str;
15630 slot = htab_find_slot (table, &entry, INSERT);
15631 if (*slot)
15632 result = *slot;
15633 else
15634 {
15635 result = XNEW (struct strtab_entry);
15636 result->offset = obstack_object_size (cpool);
15637 result->str = str;
15638 obstack_grow_str0 (cpool, str);
15639 *slot = result;
15640 }
15641 return result->offset;
15642}
15643
15644/* An entry in the symbol table. */
15645struct symtab_index_entry
15646{
15647 /* The name of the symbol. */
15648 const char *name;
15649 /* The offset of the name in the constant pool. */
15650 offset_type index_offset;
15651 /* A sorted vector of the indices of all the CUs that hold an object
15652 of this name. */
15653 VEC (offset_type) *cu_indices;
15654};
15655
15656/* The symbol table. This is a power-of-2-sized hash table. */
15657struct mapped_symtab
15658{
15659 offset_type n_elements;
15660 offset_type size;
15661 struct symtab_index_entry **data;
15662};
15663
15664/* Hash function for a symtab_index_entry. */
b89be57b 15665
9291a0cd
TT
15666static hashval_t
15667hash_symtab_entry (const void *e)
15668{
15669 const struct symtab_index_entry *entry = e;
15670 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15671 sizeof (offset_type) * VEC_length (offset_type,
15672 entry->cu_indices),
15673 0);
15674}
15675
15676/* Equality function for a symtab_index_entry. */
b89be57b 15677
9291a0cd
TT
15678static int
15679eq_symtab_entry (const void *a, const void *b)
15680{
15681 const struct symtab_index_entry *ea = a;
15682 const struct symtab_index_entry *eb = b;
15683 int len = VEC_length (offset_type, ea->cu_indices);
15684 if (len != VEC_length (offset_type, eb->cu_indices))
15685 return 0;
15686 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15687 VEC_address (offset_type, eb->cu_indices),
15688 sizeof (offset_type) * len);
15689}
15690
15691/* Destroy a symtab_index_entry. */
b89be57b 15692
9291a0cd
TT
15693static void
15694delete_symtab_entry (void *p)
15695{
15696 struct symtab_index_entry *entry = p;
15697 VEC_free (offset_type, entry->cu_indices);
15698 xfree (entry);
15699}
15700
15701/* Create a hash table holding symtab_index_entry objects. */
b89be57b 15702
9291a0cd 15703static htab_t
3876f04e 15704create_symbol_hash_table (void)
9291a0cd
TT
15705{
15706 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15707 delete_symtab_entry, xcalloc, xfree);
15708}
15709
15710/* Create a new mapped symtab object. */
b89be57b 15711
9291a0cd
TT
15712static struct mapped_symtab *
15713create_mapped_symtab (void)
15714{
15715 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15716 symtab->n_elements = 0;
15717 symtab->size = 1024;
15718 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15719 return symtab;
15720}
15721
15722/* Destroy a mapped_symtab. */
b89be57b 15723
9291a0cd
TT
15724static void
15725cleanup_mapped_symtab (void *p)
15726{
15727 struct mapped_symtab *symtab = p;
15728 /* The contents of the array are freed when the other hash table is
15729 destroyed. */
15730 xfree (symtab->data);
15731 xfree (symtab);
15732}
15733
15734/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
15735 the slot.
15736
15737 Function is used only during write_hash_table so no index format backward
15738 compatibility is needed. */
b89be57b 15739
9291a0cd
TT
15740static struct symtab_index_entry **
15741find_slot (struct mapped_symtab *symtab, const char *name)
15742{
559a7a62 15743 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
15744
15745 index = hash & (symtab->size - 1);
15746 step = ((hash * 17) & (symtab->size - 1)) | 1;
15747
15748 for (;;)
15749 {
15750 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15751 return &symtab->data[index];
15752 index = (index + step) & (symtab->size - 1);
15753 }
15754}
15755
15756/* Expand SYMTAB's hash table. */
b89be57b 15757
9291a0cd
TT
15758static void
15759hash_expand (struct mapped_symtab *symtab)
15760{
15761 offset_type old_size = symtab->size;
15762 offset_type i;
15763 struct symtab_index_entry **old_entries = symtab->data;
15764
15765 symtab->size *= 2;
15766 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15767
15768 for (i = 0; i < old_size; ++i)
15769 {
15770 if (old_entries[i])
15771 {
15772 struct symtab_index_entry **slot = find_slot (symtab,
15773 old_entries[i]->name);
15774 *slot = old_entries[i];
15775 }
15776 }
15777
15778 xfree (old_entries);
15779}
15780
15781/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15782 is the index of the CU in which the symbol appears. */
b89be57b 15783
9291a0cd
TT
15784static void
15785add_index_entry (struct mapped_symtab *symtab, const char *name,
15786 offset_type cu_index)
15787{
15788 struct symtab_index_entry **slot;
15789
15790 ++symtab->n_elements;
15791 if (4 * symtab->n_elements / 3 >= symtab->size)
15792 hash_expand (symtab);
15793
15794 slot = find_slot (symtab, name);
15795 if (!*slot)
15796 {
15797 *slot = XNEW (struct symtab_index_entry);
15798 (*slot)->name = name;
15799 (*slot)->cu_indices = NULL;
15800 }
15801 /* Don't push an index twice. Due to how we add entries we only
15802 have to check the last one. */
15803 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 15804 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
15805 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15806}
15807
15808/* Add a vector of indices to the constant pool. */
b89be57b 15809
9291a0cd 15810static offset_type
3876f04e 15811add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
15812 struct symtab_index_entry *entry)
15813{
15814 void **slot;
15815
3876f04e 15816 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
15817 if (!*slot)
15818 {
15819 offset_type len = VEC_length (offset_type, entry->cu_indices);
15820 offset_type val = MAYBE_SWAP (len);
15821 offset_type iter;
15822 int i;
15823
15824 *slot = entry;
15825 entry->index_offset = obstack_object_size (cpool);
15826
15827 obstack_grow (cpool, &val, sizeof (val));
15828 for (i = 0;
15829 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15830 ++i)
15831 {
15832 val = MAYBE_SWAP (iter);
15833 obstack_grow (cpool, &val, sizeof (val));
15834 }
15835 }
15836 else
15837 {
15838 struct symtab_index_entry *old_entry = *slot;
15839 entry->index_offset = old_entry->index_offset;
15840 entry = old_entry;
15841 }
15842 return entry->index_offset;
15843}
15844
15845/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15846 constant pool entries going into the obstack CPOOL. */
b89be57b 15847
9291a0cd
TT
15848static void
15849write_hash_table (struct mapped_symtab *symtab,
15850 struct obstack *output, struct obstack *cpool)
15851{
15852 offset_type i;
3876f04e 15853 htab_t symbol_hash_table;
9291a0cd
TT
15854 htab_t str_table;
15855
3876f04e 15856 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 15857 str_table = create_strtab ();
3876f04e 15858
9291a0cd
TT
15859 /* We add all the index vectors to the constant pool first, to
15860 ensure alignment is ok. */
15861 for (i = 0; i < symtab->size; ++i)
15862 {
15863 if (symtab->data[i])
3876f04e 15864 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
15865 }
15866
15867 /* Now write out the hash table. */
15868 for (i = 0; i < symtab->size; ++i)
15869 {
15870 offset_type str_off, vec_off;
15871
15872 if (symtab->data[i])
15873 {
15874 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15875 vec_off = symtab->data[i]->index_offset;
15876 }
15877 else
15878 {
15879 /* While 0 is a valid constant pool index, it is not valid
15880 to have 0 for both offsets. */
15881 str_off = 0;
15882 vec_off = 0;
15883 }
15884
15885 str_off = MAYBE_SWAP (str_off);
15886 vec_off = MAYBE_SWAP (vec_off);
15887
15888 obstack_grow (output, &str_off, sizeof (str_off));
15889 obstack_grow (output, &vec_off, sizeof (vec_off));
15890 }
15891
15892 htab_delete (str_table);
3876f04e 15893 htab_delete (symbol_hash_table);
9291a0cd
TT
15894}
15895
0a5429f6
DE
15896/* Struct to map psymtab to CU index in the index file. */
15897struct psymtab_cu_index_map
15898{
15899 struct partial_symtab *psymtab;
15900 unsigned int cu_index;
15901};
15902
15903static hashval_t
15904hash_psymtab_cu_index (const void *item)
15905{
15906 const struct psymtab_cu_index_map *map = item;
15907
15908 return htab_hash_pointer (map->psymtab);
15909}
15910
15911static int
15912eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15913{
15914 const struct psymtab_cu_index_map *lhs = item_lhs;
15915 const struct psymtab_cu_index_map *rhs = item_rhs;
15916
15917 return lhs->psymtab == rhs->psymtab;
15918}
15919
15920/* Helper struct for building the address table. */
15921struct addrmap_index_data
15922{
15923 struct objfile *objfile;
15924 struct obstack *addr_obstack;
15925 htab_t cu_index_htab;
15926
15927 /* Non-zero if the previous_* fields are valid.
15928 We can't write an entry until we see the next entry (since it is only then
15929 that we know the end of the entry). */
15930 int previous_valid;
15931 /* Index of the CU in the table of all CUs in the index file. */
15932 unsigned int previous_cu_index;
0963b4bd 15933 /* Start address of the CU. */
0a5429f6
DE
15934 CORE_ADDR previous_cu_start;
15935};
15936
15937/* Write an address entry to OBSTACK. */
b89be57b 15938
9291a0cd 15939static void
0a5429f6
DE
15940add_address_entry (struct objfile *objfile, struct obstack *obstack,
15941 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 15942{
0a5429f6 15943 offset_type cu_index_to_write;
9291a0cd
TT
15944 char addr[8];
15945 CORE_ADDR baseaddr;
15946
15947 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15948
0a5429f6
DE
15949 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15950 obstack_grow (obstack, addr, 8);
15951 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15952 obstack_grow (obstack, addr, 8);
15953 cu_index_to_write = MAYBE_SWAP (cu_index);
15954 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15955}
15956
15957/* Worker function for traversing an addrmap to build the address table. */
15958
15959static int
15960add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15961{
15962 struct addrmap_index_data *data = datap;
15963 struct partial_symtab *pst = obj;
15964 offset_type cu_index;
15965 void **slot;
15966
15967 if (data->previous_valid)
15968 add_address_entry (data->objfile, data->addr_obstack,
15969 data->previous_cu_start, start_addr,
15970 data->previous_cu_index);
15971
15972 data->previous_cu_start = start_addr;
15973 if (pst != NULL)
15974 {
15975 struct psymtab_cu_index_map find_map, *map;
15976 find_map.psymtab = pst;
15977 map = htab_find (data->cu_index_htab, &find_map);
15978 gdb_assert (map != NULL);
15979 data->previous_cu_index = map->cu_index;
15980 data->previous_valid = 1;
15981 }
15982 else
15983 data->previous_valid = 0;
15984
15985 return 0;
15986}
15987
15988/* Write OBJFILE's address map to OBSTACK.
15989 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15990 in the index file. */
15991
15992static void
15993write_address_map (struct objfile *objfile, struct obstack *obstack,
15994 htab_t cu_index_htab)
15995{
15996 struct addrmap_index_data addrmap_index_data;
15997
15998 /* When writing the address table, we have to cope with the fact that
15999 the addrmap iterator only provides the start of a region; we have to
16000 wait until the next invocation to get the start of the next region. */
16001
16002 addrmap_index_data.objfile = objfile;
16003 addrmap_index_data.addr_obstack = obstack;
16004 addrmap_index_data.cu_index_htab = cu_index_htab;
16005 addrmap_index_data.previous_valid = 0;
16006
16007 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16008 &addrmap_index_data);
16009
16010 /* It's highly unlikely the last entry (end address = 0xff...ff)
16011 is valid, but we should still handle it.
16012 The end address is recorded as the start of the next region, but that
16013 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16014 anyway. */
16015 if (addrmap_index_data.previous_valid)
16016 add_address_entry (objfile, obstack,
16017 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16018 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16019}
16020
16021/* Add a list of partial symbols to SYMTAB. */
b89be57b 16022
9291a0cd
TT
16023static void
16024write_psymbols (struct mapped_symtab *symtab,
987d643c 16025 htab_t psyms_seen,
9291a0cd
TT
16026 struct partial_symbol **psymp,
16027 int count,
987d643c
TT
16028 offset_type cu_index,
16029 int is_static)
9291a0cd
TT
16030{
16031 for (; count-- > 0; ++psymp)
16032 {
987d643c
TT
16033 void **slot, *lookup;
16034
9291a0cd
TT
16035 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16036 error (_("Ada is not currently supported by the index"));
987d643c
TT
16037
16038 /* We only want to add a given psymbol once. However, we also
16039 want to account for whether it is global or static. So, we
16040 may add it twice, using slightly different values. */
16041 if (is_static)
16042 {
16043 uintptr_t val = 1 | (uintptr_t) *psymp;
16044
16045 lookup = (void *) val;
16046 }
16047 else
16048 lookup = *psymp;
16049
16050 /* Only add a given psymbol once. */
16051 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16052 if (!*slot)
16053 {
16054 *slot = lookup;
16055 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16056 }
9291a0cd
TT
16057 }
16058}
16059
16060/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16061 exception if there is an error. */
b89be57b 16062
9291a0cd
TT
16063static void
16064write_obstack (FILE *file, struct obstack *obstack)
16065{
16066 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16067 file)
16068 != obstack_object_size (obstack))
16069 error (_("couldn't data write to file"));
16070}
16071
16072/* Unlink a file if the argument is not NULL. */
b89be57b 16073
9291a0cd
TT
16074static void
16075unlink_if_set (void *p)
16076{
16077 char **filename = p;
16078 if (*filename)
16079 unlink (*filename);
16080}
16081
1fd400ff
TT
16082/* A helper struct used when iterating over debug_types. */
16083struct signatured_type_index_data
16084{
16085 struct objfile *objfile;
16086 struct mapped_symtab *symtab;
16087 struct obstack *types_list;
987d643c 16088 htab_t psyms_seen;
1fd400ff
TT
16089 int cu_index;
16090};
16091
16092/* A helper function that writes a single signatured_type to an
16093 obstack. */
b89be57b 16094
1fd400ff
TT
16095static int
16096write_one_signatured_type (void **slot, void *d)
16097{
16098 struct signatured_type_index_data *info = d;
16099 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16100 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16101 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16102 gdb_byte val[8];
16103
16104 write_psymbols (info->symtab,
987d643c 16105 info->psyms_seen,
3e43a32a
MS
16106 info->objfile->global_psymbols.list
16107 + psymtab->globals_offset,
987d643c
TT
16108 psymtab->n_global_syms, info->cu_index,
16109 0);
1fd400ff 16110 write_psymbols (info->symtab,
987d643c 16111 info->psyms_seen,
3e43a32a
MS
16112 info->objfile->static_psymbols.list
16113 + psymtab->statics_offset,
987d643c
TT
16114 psymtab->n_static_syms, info->cu_index,
16115 1);
1fd400ff 16116
b3c8eb43 16117 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16118 obstack_grow (info->types_list, val, 8);
16119 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16120 obstack_grow (info->types_list, val, 8);
16121 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16122 obstack_grow (info->types_list, val, 8);
16123
16124 ++info->cu_index;
16125
16126 return 1;
16127}
16128
987d643c
TT
16129/* A cleanup function for an htab_t. */
16130
16131static void
16132cleanup_htab (void *arg)
16133{
16134 htab_delete (arg);
16135}
16136
9291a0cd 16137/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16138
9291a0cd
TT
16139static void
16140write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16141{
16142 struct cleanup *cleanup;
16143 char *filename, *cleanup_filename;
1fd400ff
TT
16144 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16145 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16146 int i;
16147 FILE *out_file;
16148 struct mapped_symtab *symtab;
16149 offset_type val, size_of_contents, total_len;
16150 struct stat st;
16151 char buf[8];
987d643c 16152 htab_t psyms_seen;
0a5429f6
DE
16153 htab_t cu_index_htab;
16154 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16155
b4f2f049 16156 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16157 return;
b4f2f049 16158
9291a0cd
TT
16159 if (dwarf2_per_objfile->using_index)
16160 error (_("Cannot use an index to create the index"));
16161
16162 if (stat (objfile->name, &st) < 0)
7e17e088 16163 perror_with_name (objfile->name);
9291a0cd
TT
16164
16165 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16166 INDEX_SUFFIX, (char *) NULL);
16167 cleanup = make_cleanup (xfree, filename);
16168
16169 out_file = fopen (filename, "wb");
16170 if (!out_file)
16171 error (_("Can't open `%s' for writing"), filename);
16172
16173 cleanup_filename = filename;
16174 make_cleanup (unlink_if_set, &cleanup_filename);
16175
16176 symtab = create_mapped_symtab ();
16177 make_cleanup (cleanup_mapped_symtab, symtab);
16178
16179 obstack_init (&addr_obstack);
16180 make_cleanup_obstack_free (&addr_obstack);
16181
16182 obstack_init (&cu_list);
16183 make_cleanup_obstack_free (&cu_list);
16184
1fd400ff
TT
16185 obstack_init (&types_cu_list);
16186 make_cleanup_obstack_free (&types_cu_list);
16187
987d643c
TT
16188 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16189 NULL, xcalloc, xfree);
16190 make_cleanup (cleanup_htab, psyms_seen);
16191
0a5429f6
DE
16192 /* While we're scanning CU's create a table that maps a psymtab pointer
16193 (which is what addrmap records) to its index (which is what is recorded
16194 in the index file). This will later be needed to write the address
16195 table. */
16196 cu_index_htab = htab_create_alloc (100,
16197 hash_psymtab_cu_index,
16198 eq_psymtab_cu_index,
16199 NULL, xcalloc, xfree);
16200 make_cleanup (cleanup_htab, cu_index_htab);
16201 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16202 xmalloc (sizeof (struct psymtab_cu_index_map)
16203 * dwarf2_per_objfile->n_comp_units);
16204 make_cleanup (xfree, psymtab_cu_index_map);
16205
16206 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16207 work here. Also, the debug_types entries do not appear in
16208 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16209 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16210 {
3e43a32a
MS
16211 struct dwarf2_per_cu_data *per_cu
16212 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16213 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16214 gdb_byte val[8];
0a5429f6
DE
16215 struct psymtab_cu_index_map *map;
16216 void **slot;
9291a0cd
TT
16217
16218 write_psymbols (symtab,
987d643c 16219 psyms_seen,
9291a0cd 16220 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16221 psymtab->n_global_syms, i,
16222 0);
9291a0cd 16223 write_psymbols (symtab,
987d643c 16224 psyms_seen,
9291a0cd 16225 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16226 psymtab->n_static_syms, i,
16227 1);
9291a0cd 16228
0a5429f6
DE
16229 map = &psymtab_cu_index_map[i];
16230 map->psymtab = psymtab;
16231 map->cu_index = i;
16232 slot = htab_find_slot (cu_index_htab, map, INSERT);
16233 gdb_assert (slot != NULL);
16234 gdb_assert (*slot == NULL);
16235 *slot = map;
9291a0cd 16236
e254ef6a 16237 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 16238 obstack_grow (&cu_list, val, 8);
e254ef6a 16239 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
16240 obstack_grow (&cu_list, val, 8);
16241 }
16242
0a5429f6
DE
16243 /* Dump the address map. */
16244 write_address_map (objfile, &addr_obstack, cu_index_htab);
16245
1fd400ff
TT
16246 /* Write out the .debug_type entries, if any. */
16247 if (dwarf2_per_objfile->signatured_types)
16248 {
16249 struct signatured_type_index_data sig_data;
16250
16251 sig_data.objfile = objfile;
16252 sig_data.symtab = symtab;
16253 sig_data.types_list = &types_cu_list;
987d643c 16254 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
16255 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16256 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16257 write_one_signatured_type, &sig_data);
16258 }
16259
9291a0cd
TT
16260 obstack_init (&constant_pool);
16261 make_cleanup_obstack_free (&constant_pool);
16262 obstack_init (&symtab_obstack);
16263 make_cleanup_obstack_free (&symtab_obstack);
16264 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16265
16266 obstack_init (&contents);
16267 make_cleanup_obstack_free (&contents);
1fd400ff 16268 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
16269 total_len = size_of_contents;
16270
16271 /* The version number. */
559a7a62 16272 val = MAYBE_SWAP (5);
9291a0cd
TT
16273 obstack_grow (&contents, &val, sizeof (val));
16274
16275 /* The offset of the CU list from the start of the file. */
16276 val = MAYBE_SWAP (total_len);
16277 obstack_grow (&contents, &val, sizeof (val));
16278 total_len += obstack_object_size (&cu_list);
16279
1fd400ff
TT
16280 /* The offset of the types CU list from the start of the file. */
16281 val = MAYBE_SWAP (total_len);
16282 obstack_grow (&contents, &val, sizeof (val));
16283 total_len += obstack_object_size (&types_cu_list);
16284
9291a0cd
TT
16285 /* The offset of the address table from the start of the file. */
16286 val = MAYBE_SWAP (total_len);
16287 obstack_grow (&contents, &val, sizeof (val));
16288 total_len += obstack_object_size (&addr_obstack);
16289
16290 /* The offset of the symbol table from the start of the file. */
16291 val = MAYBE_SWAP (total_len);
16292 obstack_grow (&contents, &val, sizeof (val));
16293 total_len += obstack_object_size (&symtab_obstack);
16294
16295 /* The offset of the constant pool from the start of the file. */
16296 val = MAYBE_SWAP (total_len);
16297 obstack_grow (&contents, &val, sizeof (val));
16298 total_len += obstack_object_size (&constant_pool);
16299
16300 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16301
16302 write_obstack (out_file, &contents);
16303 write_obstack (out_file, &cu_list);
1fd400ff 16304 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
16305 write_obstack (out_file, &addr_obstack);
16306 write_obstack (out_file, &symtab_obstack);
16307 write_obstack (out_file, &constant_pool);
16308
16309 fclose (out_file);
16310
16311 /* We want to keep the file, so we set cleanup_filename to NULL
16312 here. See unlink_if_set. */
16313 cleanup_filename = NULL;
16314
16315 do_cleanups (cleanup);
16316}
16317
90476074
TT
16318/* Implementation of the `save gdb-index' command.
16319
16320 Note that the file format used by this command is documented in the
16321 GDB manual. Any changes here must be documented there. */
11570e71 16322
9291a0cd
TT
16323static void
16324save_gdb_index_command (char *arg, int from_tty)
16325{
16326 struct objfile *objfile;
16327
16328 if (!arg || !*arg)
96d19272 16329 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
16330
16331 ALL_OBJFILES (objfile)
16332 {
16333 struct stat st;
16334
16335 /* If the objfile does not correspond to an actual file, skip it. */
16336 if (stat (objfile->name, &st) < 0)
16337 continue;
16338
16339 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16340 if (dwarf2_per_objfile)
16341 {
16342 volatile struct gdb_exception except;
16343
16344 TRY_CATCH (except, RETURN_MASK_ERROR)
16345 {
16346 write_psymtabs_to_index (objfile, arg);
16347 }
16348 if (except.reason < 0)
16349 exception_fprintf (gdb_stderr, except,
16350 _("Error while writing index for `%s': "),
16351 objfile->name);
16352 }
16353 }
dce234bc
PP
16354}
16355
9291a0cd
TT
16356\f
16357
9eae7c52
TT
16358int dwarf2_always_disassemble;
16359
16360static void
16361show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16362 struct cmd_list_element *c, const char *value)
16363{
3e43a32a
MS
16364 fprintf_filtered (file,
16365 _("Whether to always disassemble "
16366 "DWARF expressions is %s.\n"),
9eae7c52
TT
16367 value);
16368}
16369
900e11f9
JK
16370static void
16371show_check_physname (struct ui_file *file, int from_tty,
16372 struct cmd_list_element *c, const char *value)
16373{
16374 fprintf_filtered (file,
16375 _("Whether to check \"physname\" is %s.\n"),
16376 value);
16377}
16378
6502dd73
DJ
16379void _initialize_dwarf2_read (void);
16380
16381void
16382_initialize_dwarf2_read (void)
16383{
96d19272
JK
16384 struct cmd_list_element *c;
16385
dce234bc 16386 dwarf2_objfile_data_key
c1bd65d0 16387 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 16388
1bedd215
AC
16389 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16390Set DWARF 2 specific variables.\n\
16391Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16392 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16393 0/*allow-unknown*/, &maintenance_set_cmdlist);
16394
1bedd215
AC
16395 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16396Show DWARF 2 specific variables\n\
16397Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
16398 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16399 0/*allow-unknown*/, &maintenance_show_cmdlist);
16400
16401 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
16402 &dwarf2_max_cache_age, _("\
16403Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16404Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16405A higher limit means that cached compilation units will be stored\n\
16406in memory longer, and more total memory will be used. Zero disables\n\
16407caching, which can slow down startup."),
2c5b56ce 16408 NULL,
920d2a44 16409 show_dwarf2_max_cache_age,
2c5b56ce 16410 &set_dwarf2_cmdlist,
ae038cb0 16411 &show_dwarf2_cmdlist);
d97bc12b 16412
9eae7c52
TT
16413 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16414 &dwarf2_always_disassemble, _("\
16415Set whether `info address' always disassembles DWARF expressions."), _("\
16416Show whether `info address' always disassembles DWARF expressions."), _("\
16417When enabled, DWARF expressions are always printed in an assembly-like\n\
16418syntax. When disabled, expressions will be printed in a more\n\
16419conversational style, when possible."),
16420 NULL,
16421 show_dwarf2_always_disassemble,
16422 &set_dwarf2_cmdlist,
16423 &show_dwarf2_cmdlist);
16424
d97bc12b
DE
16425 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16426Set debugging of the dwarf2 DIE reader."), _("\
16427Show debugging of the dwarf2 DIE reader."), _("\
16428When enabled (non-zero), DIEs are dumped after they are read in.\n\
16429The value is the maximum depth to print."),
16430 NULL,
16431 NULL,
16432 &setdebuglist, &showdebuglist);
9291a0cd 16433
900e11f9
JK
16434 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
16435Set cross-checking of \"physname\" code against demangler."), _("\
16436Show cross-checking of \"physname\" code against demangler."), _("\
16437When enabled, GDB's internal \"physname\" code is checked against\n\
16438the demangler."),
16439 NULL, show_check_physname,
16440 &setdebuglist, &showdebuglist);
16441
96d19272 16442 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 16443 _("\
fc1a9d6e 16444Save a gdb-index file.\n\
11570e71 16445Usage: save gdb-index DIRECTORY"),
96d19272
JK
16446 &save_cmdlist);
16447 set_cmd_completer (c, filename_completer);
6502dd73 16448}
This page took 2.224706 seconds and 4 git commands to generate.