daily update
[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"
50f182aa 37#include "gdb-demangle.h"
c906108c 38#include "expression.h"
d5166ae1 39#include "filenames.h" /* for DOSish file names */
2e276125 40#include "macrotab.h"
c906108c
SS
41#include "language.h"
42#include "complaints.h"
357e46e7 43#include "bcache.h"
4c2df51b
DJ
44#include "dwarf2expr.h"
45#include "dwarf2loc.h"
9219021c 46#include "cp-support.h"
72bf9492 47#include "hashtab.h"
ae038cb0
DJ
48#include "command.h"
49#include "gdbcmd.h"
edb3359d 50#include "block.h"
ff013f42 51#include "addrmap.h"
94af9270
KS
52#include "typeprint.h"
53#include "jv-lang.h"
ccefe4c4 54#include "psympriv.h"
9291a0cd
TT
55#include "exceptions.h"
56#include "gdb_stat.h"
96d19272 57#include "completer.h"
34eaf542 58#include "vec.h"
98bfdba5
PA
59#include "c-lang.h"
60#include "valprint.h"
60d5a603 61#include <ctype.h>
4c2df51b 62
c906108c
SS
63#include <fcntl.h>
64#include "gdb_string.h"
4bdf3d34 65#include "gdb_assert.h"
c906108c 66#include <sys/types.h>
233a11ab
CS
67#ifdef HAVE_ZLIB_H
68#include <zlib.h>
69#endif
dce234bc
PP
70#ifdef HAVE_MMAP
71#include <sys/mman.h>
85d9bd0e
TT
72#ifndef MAP_FAILED
73#define MAP_FAILED ((void *) -1)
74#endif
dce234bc 75#endif
d8151005 76
34eaf542
TT
77typedef struct symbol *symbolp;
78DEF_VEC_P (symbolp);
79
107d2387 80#if 0
357e46e7 81/* .debug_info header for a compilation unit
c906108c
SS
82 Because of alignment constraints, this structure has padding and cannot
83 be mapped directly onto the beginning of the .debug_info section. */
84typedef struct comp_unit_header
85 {
86 unsigned int length; /* length of the .debug_info
87 contribution */
88 unsigned short version; /* version number -- 2 for DWARF
89 version 2 */
90 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
91 unsigned char addr_size; /* byte size of an address -- 4 */
92 }
93_COMP_UNIT_HEADER;
94#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 95#endif
c906108c 96
c906108c
SS
97/* .debug_line statement program prologue
98 Because of alignment constraints, this structure has padding and cannot
99 be mapped directly onto the beginning of the .debug_info section. */
100typedef struct statement_prologue
101 {
102 unsigned int total_length; /* byte length of the statement
103 information */
104 unsigned short version; /* version number -- 2 for DWARF
105 version 2 */
106 unsigned int prologue_length; /* # bytes between prologue &
107 stmt program */
108 unsigned char minimum_instruction_length; /* byte size of
109 smallest instr */
110 unsigned char default_is_stmt; /* initial value of is_stmt
111 register */
112 char line_base;
113 unsigned char line_range;
114 unsigned char opcode_base; /* number assigned to first special
115 opcode */
116 unsigned char *standard_opcode_lengths;
117 }
118_STATEMENT_PROLOGUE;
119
d97bc12b
DE
120/* When non-zero, dump DIEs after they are read in. */
121static int dwarf2_die_debug = 0;
122
900e11f9
JK
123/* When non-zero, cross-check physname against demangler. */
124static int check_physname = 0;
125
dce234bc
PP
126static int pagesize;
127
df8a16a1
DJ
128/* When set, the file that we're processing is known to have debugging
129 info for C++ namespaces. GCC 3.3.x did not produce this information,
130 but later versions do. */
131
132static int processing_has_namespace_info;
133
6502dd73
DJ
134static const struct objfile_data *dwarf2_objfile_data_key;
135
dce234bc
PP
136struct dwarf2_section_info
137{
138 asection *asection;
139 gdb_byte *buffer;
140 bfd_size_type size;
b315ab21
TG
141 /* Not NULL if the section was actually mmapped. */
142 void *map_addr;
143 /* Page aligned size of mmapped area. */
144 bfd_size_type map_len;
be391dca
TT
145 /* True if we have tried to read this section. */
146 int readin;
dce234bc
PP
147};
148
8b70b953
TT
149typedef struct dwarf2_section_info dwarf2_section_info_def;
150DEF_VEC_O (dwarf2_section_info_def);
151
9291a0cd
TT
152/* All offsets in the index are of this type. It must be
153 architecture-independent. */
154typedef uint32_t offset_type;
155
156DEF_VEC_I (offset_type);
157
158/* A description of the mapped index. The file format is described in
159 a comment by the code that writes the index. */
160struct mapped_index
161{
559a7a62
JK
162 /* Index data format version. */
163 int version;
164
9291a0cd
TT
165 /* The total length of the buffer. */
166 off_t total_size;
b11b1f88 167
9291a0cd
TT
168 /* A pointer to the address table data. */
169 const gdb_byte *address_table;
b11b1f88 170
9291a0cd
TT
171 /* Size of the address table data in bytes. */
172 offset_type address_table_size;
b11b1f88 173
3876f04e
DE
174 /* The symbol table, implemented as a hash table. */
175 const offset_type *symbol_table;
b11b1f88 176
9291a0cd 177 /* Size in slots, each slot is 2 offset_types. */
3876f04e 178 offset_type symbol_table_slots;
b11b1f88 179
9291a0cd
TT
180 /* A pointer to the constant pool. */
181 const char *constant_pool;
182};
183
6502dd73
DJ
184struct dwarf2_per_objfile
185{
dce234bc
PP
186 struct dwarf2_section_info info;
187 struct dwarf2_section_info abbrev;
188 struct dwarf2_section_info line;
dce234bc
PP
189 struct dwarf2_section_info loc;
190 struct dwarf2_section_info macinfo;
cf2c3c16 191 struct dwarf2_section_info macro;
dce234bc
PP
192 struct dwarf2_section_info str;
193 struct dwarf2_section_info ranges;
194 struct dwarf2_section_info frame;
195 struct dwarf2_section_info eh_frame;
9291a0cd 196 struct dwarf2_section_info gdb_index;
ae038cb0 197
8b70b953
TT
198 VEC (dwarf2_section_info_def) *types;
199
be391dca
TT
200 /* Back link. */
201 struct objfile *objfile;
202
10b3939b
DJ
203 /* A list of all the compilation units. This is used to locate
204 the target compilation unit of a particular reference. */
ae038cb0
DJ
205 struct dwarf2_per_cu_data **all_comp_units;
206
207 /* The number of compilation units in ALL_COMP_UNITS. */
208 int n_comp_units;
209
1fd400ff
TT
210 /* The number of .debug_types-related CUs. */
211 int n_type_comp_units;
212
213 /* The .debug_types-related CUs. */
214 struct dwarf2_per_cu_data **type_comp_units;
215
ae038cb0
DJ
216 /* A chain of compilation units that are currently read in, so that
217 they can be freed later. */
218 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 219
348e048f
DE
220 /* A table mapping .debug_types signatures to its signatured_type entry.
221 This is NULL if the .debug_types section hasn't been read in yet. */
222 htab_t signatured_types;
223
72dca2f5
FR
224 /* A flag indicating wether this objfile has a section loaded at a
225 VMA of 0. */
226 int has_section_at_zero;
9291a0cd 227
ae2de4f8
DE
228 /* True if we are using the mapped index,
229 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
230 unsigned char using_index;
231
ae2de4f8 232 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 233 struct mapped_index *index_table;
98bfdba5 234
7b9f3c50
DE
235 /* When using index_table, this keeps track of all quick_file_names entries.
236 TUs can share line table entries with CUs or other TUs, and there can be
237 a lot more TUs than unique line tables, so we maintain a separate table
238 of all line table entries to support the sharing. */
239 htab_t quick_file_names_table;
240
98bfdba5
PA
241 /* Set during partial symbol reading, to prevent queueing of full
242 symbols. */
243 int reading_partial_symbols;
673bfd45
DE
244
245 /* Table mapping type .debug_info DIE offsets to types.
246 This is NULL if not allocated yet.
247 It (currently) makes sense to allocate debug_types_type_hash lazily.
248 To keep things simple we allocate both lazily. */
249 htab_t debug_info_type_hash;
250
251 /* Table mapping type .debug_types DIE offsets to types.
252 This is NULL if not allocated yet. */
253 htab_t debug_types_type_hash;
6502dd73
DJ
254};
255
256static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 257
251d32d9 258/* Default names of the debugging sections. */
c906108c 259
233a11ab
CS
260/* Note that if the debugging section has been compressed, it might
261 have a name like .zdebug_info. */
262
251d32d9
TG
263static const struct dwarf2_debug_sections dwarf2_elf_names = {
264 { ".debug_info", ".zdebug_info" },
265 { ".debug_abbrev", ".zdebug_abbrev" },
266 { ".debug_line", ".zdebug_line" },
267 { ".debug_loc", ".zdebug_loc" },
268 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 269 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
270 { ".debug_str", ".zdebug_str" },
271 { ".debug_ranges", ".zdebug_ranges" },
272 { ".debug_types", ".zdebug_types" },
273 { ".debug_frame", ".zdebug_frame" },
274 { ".eh_frame", NULL },
24d3216f
TT
275 { ".gdb_index", ".zgdb_index" },
276 23
251d32d9 277};
c906108c
SS
278
279/* local data types */
280
0963b4bd 281/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
282#ifndef ABBREV_HASH_SIZE
283#define ABBREV_HASH_SIZE 121
284#endif
285
107d2387
AC
286/* The data in a compilation unit header, after target2host
287 translation, looks like this. */
c906108c 288struct comp_unit_head
a738430d 289{
c764a876 290 unsigned int length;
a738430d 291 short version;
a738430d
MK
292 unsigned char addr_size;
293 unsigned char signed_addr_p;
9cbfa09e 294 unsigned int abbrev_offset;
57349743 295
a738430d
MK
296 /* Size of file offsets; either 4 or 8. */
297 unsigned int offset_size;
57349743 298
a738430d
MK
299 /* Size of the length field; either 4 or 12. */
300 unsigned int initial_length_size;
57349743 301
a738430d
MK
302 /* Offset to the first byte of this compilation unit header in the
303 .debug_info section, for resolving relative reference dies. */
304 unsigned int offset;
57349743 305
d00adf39
DE
306 /* Offset to first die in this cu from the start of the cu.
307 This will be the first byte following the compilation unit header. */
308 unsigned int first_die_offset;
a738430d 309};
c906108c 310
3da10d80
KS
311/* Type used for delaying computation of method physnames.
312 See comments for compute_delayed_physnames. */
313struct delayed_method_info
314{
315 /* The type to which the method is attached, i.e., its parent class. */
316 struct type *type;
317
318 /* The index of the method in the type's function fieldlists. */
319 int fnfield_index;
320
321 /* The index of the method in the fieldlist. */
322 int index;
323
324 /* The name of the DIE. */
325 const char *name;
326
327 /* The DIE associated with this method. */
328 struct die_info *die;
329};
330
331typedef struct delayed_method_info delayed_method_info;
332DEF_VEC_O (delayed_method_info);
333
e7c27a73
DJ
334/* Internal state when decoding a particular compilation unit. */
335struct dwarf2_cu
336{
337 /* The objfile containing this compilation unit. */
338 struct objfile *objfile;
339
d00adf39 340 /* The header of the compilation unit. */
e7c27a73 341 struct comp_unit_head header;
e142c38c 342
d00adf39
DE
343 /* Base address of this compilation unit. */
344 CORE_ADDR base_address;
345
346 /* Non-zero if base_address has been set. */
347 int base_known;
348
e142c38c
DJ
349 struct function_range *first_fn, *last_fn, *cached_fn;
350
351 /* The language we are debugging. */
352 enum language language;
353 const struct language_defn *language_defn;
354
b0f35d58
DL
355 const char *producer;
356
e142c38c
DJ
357 /* The generic symbol table building routines have separate lists for
358 file scope symbols and all all other scopes (local scopes). So
359 we need to select the right one to pass to add_symbol_to_list().
360 We do it by keeping a pointer to the correct list in list_in_scope.
361
362 FIXME: The original dwarf code just treated the file scope as the
363 first local scope, and all other local scopes as nested local
364 scopes, and worked fine. Check to see if we really need to
365 distinguish these in buildsym.c. */
366 struct pending **list_in_scope;
367
f3dd6933
DJ
368 /* DWARF abbreviation table associated with this compilation unit. */
369 struct abbrev_info **dwarf2_abbrevs;
370
371 /* Storage for the abbrev table. */
372 struct obstack abbrev_obstack;
72bf9492
DJ
373
374 /* Hash table holding all the loaded partial DIEs. */
375 htab_t partial_dies;
376
377 /* Storage for things with the same lifetime as this read-in compilation
378 unit, including partial DIEs. */
379 struct obstack comp_unit_obstack;
380
ae038cb0
DJ
381 /* When multiple dwarf2_cu structures are living in memory, this field
382 chains them all together, so that they can be released efficiently.
383 We will probably also want a generation counter so that most-recently-used
384 compilation units are cached... */
385 struct dwarf2_per_cu_data *read_in_chain;
386
387 /* Backchain to our per_cu entry if the tree has been built. */
388 struct dwarf2_per_cu_data *per_cu;
389
390 /* How many compilation units ago was this CU last referenced? */
391 int last_used;
392
10b3939b 393 /* A hash table of die offsets for following references. */
51545339 394 htab_t die_hash;
10b3939b
DJ
395
396 /* Full DIEs if read in. */
397 struct die_info *dies;
398
399 /* A set of pointers to dwarf2_per_cu_data objects for compilation
400 units referenced by this one. Only set during full symbol processing;
401 partial symbol tables do not have dependencies. */
402 htab_t dependencies;
403
cb1df416
DJ
404 /* Header data from the line table, during full symbol processing. */
405 struct line_header *line_header;
406
3da10d80
KS
407 /* A list of methods which need to have physnames computed
408 after all type information has been read. */
409 VEC (delayed_method_info) *method_list;
410
96408a79
SA
411 /* To be copied to symtab->call_site_htab. */
412 htab_t call_site_htab;
413
ae038cb0
DJ
414 /* Mark used when releasing cached dies. */
415 unsigned int mark : 1;
416
417 /* This flag will be set if this compilation unit might include
418 inter-compilation-unit references. */
419 unsigned int has_form_ref_addr : 1;
420
72bf9492
DJ
421 /* This flag will be set if this compilation unit includes any
422 DW_TAG_namespace DIEs. If we know that there are explicit
423 DIEs for namespaces, we don't need to try to infer them
424 from mangled names. */
425 unsigned int has_namespace_info : 1;
8be455d7
JK
426
427 /* This CU references .debug_loc. See the symtab->locations_valid field.
428 This test is imperfect as there may exist optimized debug code not using
429 any location list and still facing inlining issues if handled as
430 unoptimized code. For a future better test see GCC PR other/32998. */
431
432 unsigned int has_loclist : 1;
e7c27a73
DJ
433};
434
10b3939b
DJ
435/* Persistent data held for a compilation unit, even when not
436 processing it. We put a pointer to this structure in the
437 read_symtab_private field of the psymtab. If we encounter
438 inter-compilation-unit references, we also maintain a sorted
439 list of all compilation units. */
440
ae038cb0
DJ
441struct dwarf2_per_cu_data
442{
348e048f 443 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 444 bytes should suffice to store the length of any compilation unit
45452591
DE
445 - if it doesn't, GDB will fall over anyway.
446 NOTE: Unlike comp_unit_head.length, this length includes
447 initial_length_size. */
c764a876 448 unsigned int offset;
348e048f 449 unsigned int length : 29;
ae038cb0
DJ
450
451 /* Flag indicating this compilation unit will be read in before
452 any of the current compilation units are processed. */
c764a876 453 unsigned int queued : 1;
ae038cb0 454
5afb4e99
DJ
455 /* This flag will be set if we need to load absolutely all DIEs
456 for this compilation unit, instead of just the ones we think
457 are interesting. It gets set if we look for a DIE in the
458 hash table and don't find it. */
459 unsigned int load_all_dies : 1;
460
8b70b953
TT
461 /* Non-null if this CU is from .debug_types; in which case it points
462 to the section. Otherwise it's from .debug_info. */
b0df02fd 463 struct dwarf2_section_info *debug_types_section;
348e048f 464
17ea53c3
JK
465 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
466 of the CU cache it gets reset to NULL again. */
ae038cb0 467 struct dwarf2_cu *cu;
1c379e20 468
9291a0cd
TT
469 /* The corresponding objfile. */
470 struct objfile *objfile;
471
472 /* When using partial symbol tables, the 'psymtab' field is active.
473 Otherwise the 'quick' field is active. */
474 union
475 {
476 /* The partial symbol table associated with this compilation unit,
477 or NULL for partial units (which do not have an associated
478 symtab). */
479 struct partial_symtab *psymtab;
480
481 /* Data needed by the "quick" functions. */
482 struct dwarf2_per_cu_quick_data *quick;
483 } v;
ae038cb0
DJ
484};
485
348e048f
DE
486/* Entry in the signatured_types hash table. */
487
488struct signatured_type
489{
490 ULONGEST signature;
491
348e048f
DE
492 /* Offset in .debug_types of the type defined by this TU. */
493 unsigned int type_offset;
494
495 /* The CU(/TU) of this type. */
496 struct dwarf2_per_cu_data per_cu;
497};
498
0963b4bd
MS
499/* Struct used to pass misc. parameters to read_die_and_children, et
500 al. which are used for both .debug_info and .debug_types dies.
501 All parameters here are unchanging for the life of the call. This
502 struct exists to abstract away the constant parameters of die
503 reading. */
93311388
DE
504
505struct die_reader_specs
506{
507 /* The bfd of this objfile. */
508 bfd* abfd;
509
510 /* The CU of the DIE we are parsing. */
511 struct dwarf2_cu *cu;
512
513 /* Pointer to start of section buffer.
514 This is either the start of .debug_info or .debug_types. */
515 const gdb_byte *buffer;
516};
517
debd256d
JB
518/* The line number information for a compilation unit (found in the
519 .debug_line section) begins with a "statement program header",
520 which contains the following information. */
521struct line_header
522{
523 unsigned int total_length;
524 unsigned short version;
525 unsigned int header_length;
526 unsigned char minimum_instruction_length;
2dc7f7b3 527 unsigned char maximum_ops_per_instruction;
debd256d
JB
528 unsigned char default_is_stmt;
529 int line_base;
530 unsigned char line_range;
531 unsigned char opcode_base;
532
533 /* standard_opcode_lengths[i] is the number of operands for the
534 standard opcode whose value is i. This means that
535 standard_opcode_lengths[0] is unused, and the last meaningful
536 element is standard_opcode_lengths[opcode_base - 1]. */
537 unsigned char *standard_opcode_lengths;
538
539 /* The include_directories table. NOTE! These strings are not
540 allocated with xmalloc; instead, they are pointers into
541 debug_line_buffer. If you try to free them, `free' will get
542 indigestion. */
543 unsigned int num_include_dirs, include_dirs_size;
544 char **include_dirs;
545
546 /* The file_names table. NOTE! These strings are not allocated
547 with xmalloc; instead, they are pointers into debug_line_buffer.
548 Don't try to free them directly. */
549 unsigned int num_file_names, file_names_size;
550 struct file_entry
c906108c 551 {
debd256d
JB
552 char *name;
553 unsigned int dir_index;
554 unsigned int mod_time;
555 unsigned int length;
aaa75496 556 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 557 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
558 } *file_names;
559
560 /* The start and end of the statement program following this
6502dd73 561 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 562 gdb_byte *statement_program_start, *statement_program_end;
debd256d 563};
c906108c
SS
564
565/* When we construct a partial symbol table entry we only
0963b4bd 566 need this much information. */
c906108c
SS
567struct partial_die_info
568 {
72bf9492 569 /* Offset of this DIE. */
c906108c 570 unsigned int offset;
72bf9492
DJ
571
572 /* DWARF-2 tag for this DIE. */
573 ENUM_BITFIELD(dwarf_tag) tag : 16;
574
72bf9492
DJ
575 /* Assorted flags describing the data found in this DIE. */
576 unsigned int has_children : 1;
577 unsigned int is_external : 1;
578 unsigned int is_declaration : 1;
579 unsigned int has_type : 1;
580 unsigned int has_specification : 1;
581 unsigned int has_pc_info : 1;
582
583 /* Flag set if the SCOPE field of this structure has been
584 computed. */
585 unsigned int scope_set : 1;
586
fa4028e9
JB
587 /* Flag set if the DIE has a byte_size attribute. */
588 unsigned int has_byte_size : 1;
589
98bfdba5
PA
590 /* Flag set if any of the DIE's children are template arguments. */
591 unsigned int has_template_arguments : 1;
592
abc72ce4
DE
593 /* Flag set if fixup_partial_die has been called on this die. */
594 unsigned int fixup_called : 1;
595
72bf9492 596 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 597 sometimes a default name for unnamed DIEs. */
c906108c 598 char *name;
72bf9492 599
abc72ce4
DE
600 /* The linkage name, if present. */
601 const char *linkage_name;
602
72bf9492
DJ
603 /* The scope to prepend to our children. This is generally
604 allocated on the comp_unit_obstack, so will disappear
605 when this compilation unit leaves the cache. */
606 char *scope;
607
608 /* The location description associated with this DIE, if any. */
609 struct dwarf_block *locdesc;
610
611 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
612 CORE_ADDR lowpc;
613 CORE_ADDR highpc;
72bf9492 614
93311388 615 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 616 DW_AT_sibling, if any. */
abc72ce4
DE
617 /* NOTE: This member isn't strictly necessary, read_partial_die could
618 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 619 gdb_byte *sibling;
72bf9492
DJ
620
621 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
622 DW_AT_specification (or DW_AT_abstract_origin or
623 DW_AT_extension). */
624 unsigned int spec_offset;
625
626 /* Pointers to this DIE's parent, first child, and next sibling,
627 if any. */
628 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
629 };
630
0963b4bd 631/* This data structure holds the information of an abbrev. */
c906108c
SS
632struct abbrev_info
633 {
634 unsigned int number; /* number identifying abbrev */
635 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
636 unsigned short has_children; /* boolean */
637 unsigned short num_attrs; /* number of attributes */
c906108c
SS
638 struct attr_abbrev *attrs; /* an array of attribute descriptions */
639 struct abbrev_info *next; /* next in chain */
640 };
641
642struct attr_abbrev
643 {
9d25dd43
DE
644 ENUM_BITFIELD(dwarf_attribute) name : 16;
645 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
646 };
647
0963b4bd 648/* Attributes have a name and a value. */
b60c80d6
DJ
649struct attribute
650 {
9d25dd43 651 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
652 ENUM_BITFIELD(dwarf_form) form : 15;
653
654 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
655 field should be in u.str (existing only for DW_STRING) but it is kept
656 here for better struct attribute alignment. */
657 unsigned int string_is_canonical : 1;
658
b60c80d6
DJ
659 union
660 {
661 char *str;
662 struct dwarf_block *blk;
43bbcdc2
PH
663 ULONGEST unsnd;
664 LONGEST snd;
b60c80d6 665 CORE_ADDR addr;
348e048f 666 struct signatured_type *signatured_type;
b60c80d6
DJ
667 }
668 u;
669 };
670
0963b4bd 671/* This data structure holds a complete die structure. */
c906108c
SS
672struct die_info
673 {
76815b17
DE
674 /* DWARF-2 tag for this DIE. */
675 ENUM_BITFIELD(dwarf_tag) tag : 16;
676
677 /* Number of attributes */
98bfdba5
PA
678 unsigned char num_attrs;
679
680 /* True if we're presently building the full type name for the
681 type derived from this DIE. */
682 unsigned char building_fullname : 1;
76815b17
DE
683
684 /* Abbrev number */
685 unsigned int abbrev;
686
93311388 687 /* Offset in .debug_info or .debug_types section. */
76815b17 688 unsigned int offset;
78ba4af6
JB
689
690 /* The dies in a compilation unit form an n-ary tree. PARENT
691 points to this die's parent; CHILD points to the first child of
692 this node; and all the children of a given node are chained
4950bc1c 693 together via their SIBLING fields. */
639d11d3
DC
694 struct die_info *child; /* Its first child, if any. */
695 struct die_info *sibling; /* Its next sibling, if any. */
696 struct die_info *parent; /* Its parent, if any. */
c906108c 697
b60c80d6
DJ
698 /* An array of attributes, with NUM_ATTRS elements. There may be
699 zero, but it's not common and zero-sized arrays are not
700 sufficiently portable C. */
701 struct attribute attrs[1];
c906108c
SS
702 };
703
5fb290d7
DJ
704struct function_range
705{
706 const char *name;
707 CORE_ADDR lowpc, highpc;
708 int seen_line;
709 struct function_range *next;
710};
711
0963b4bd 712/* Get at parts of an attribute structure. */
c906108c
SS
713
714#define DW_STRING(attr) ((attr)->u.str)
8285870a 715#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
716#define DW_UNSND(attr) ((attr)->u.unsnd)
717#define DW_BLOCK(attr) ((attr)->u.blk)
718#define DW_SND(attr) ((attr)->u.snd)
719#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 720#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 721
0963b4bd 722/* Blocks are a bunch of untyped bytes. */
c906108c
SS
723struct dwarf_block
724 {
725 unsigned int size;
1d6edc3c
JK
726
727 /* Valid only if SIZE is not zero. */
fe1b8b76 728 gdb_byte *data;
c906108c
SS
729 };
730
c906108c
SS
731#ifndef ATTR_ALLOC_CHUNK
732#define ATTR_ALLOC_CHUNK 4
733#endif
734
c906108c
SS
735/* Allocate fields for structs, unions and enums in this size. */
736#ifndef DW_FIELD_ALLOC_CHUNK
737#define DW_FIELD_ALLOC_CHUNK 4
738#endif
739
c906108c
SS
740/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
741 but this would require a corresponding change in unpack_field_as_long
742 and friends. */
743static int bits_per_byte = 8;
744
745/* The routines that read and process dies for a C struct or C++ class
746 pass lists of data member fields and lists of member function fields
747 in an instance of a field_info structure, as defined below. */
748struct field_info
c5aa993b 749 {
0963b4bd 750 /* List of data member and baseclasses fields. */
c5aa993b
JM
751 struct nextfield
752 {
753 struct nextfield *next;
754 int accessibility;
755 int virtuality;
756 struct field field;
757 }
7d0ccb61 758 *fields, *baseclasses;
c906108c 759
7d0ccb61 760 /* Number of fields (including baseclasses). */
c5aa993b 761 int nfields;
c906108c 762
c5aa993b
JM
763 /* Number of baseclasses. */
764 int nbaseclasses;
c906108c 765
c5aa993b
JM
766 /* Set if the accesibility of one of the fields is not public. */
767 int non_public_fields;
c906108c 768
c5aa993b
JM
769 /* Member function fields array, entries are allocated in the order they
770 are encountered in the object file. */
771 struct nextfnfield
772 {
773 struct nextfnfield *next;
774 struct fn_field fnfield;
775 }
776 *fnfields;
c906108c 777
c5aa993b
JM
778 /* Member function fieldlist array, contains name of possibly overloaded
779 member function, number of overloaded member functions and a pointer
780 to the head of the member function field chain. */
781 struct fnfieldlist
782 {
783 char *name;
784 int length;
785 struct nextfnfield *head;
786 }
787 *fnfieldlists;
c906108c 788
c5aa993b
JM
789 /* Number of entries in the fnfieldlists array. */
790 int nfnfields;
98751a41
JK
791
792 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
793 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
794 struct typedef_field_list
795 {
796 struct typedef_field field;
797 struct typedef_field_list *next;
798 }
799 *typedef_field_list;
800 unsigned typedef_field_list_count;
c5aa993b 801 };
c906108c 802
10b3939b
DJ
803/* One item on the queue of compilation units to read in full symbols
804 for. */
805struct dwarf2_queue_item
806{
807 struct dwarf2_per_cu_data *per_cu;
808 struct dwarf2_queue_item *next;
809};
810
811/* The current queue. */
812static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
813
ae038cb0
DJ
814/* Loaded secondary compilation units are kept in memory until they
815 have not been referenced for the processing of this many
816 compilation units. Set this to zero to disable caching. Cache
817 sizes of up to at least twenty will improve startup time for
818 typical inter-CU-reference binaries, at an obvious memory cost. */
819static int dwarf2_max_cache_age = 5;
920d2a44
AC
820static void
821show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
822 struct cmd_list_element *c, const char *value)
823{
3e43a32a
MS
824 fprintf_filtered (file, _("The upper bound on the age of cached "
825 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
826 value);
827}
828
ae038cb0 829
0963b4bd 830/* Various complaints about symbol reading that don't abort the process. */
c906108c 831
4d3c2250
KB
832static void
833dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 834{
4d3c2250 835 complaint (&symfile_complaints,
e2e0b3e5 836 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
837}
838
25e43795
DJ
839static void
840dwarf2_debug_line_missing_file_complaint (void)
841{
842 complaint (&symfile_complaints,
843 _(".debug_line section has line data without a file"));
844}
845
59205f5a
JB
846static void
847dwarf2_debug_line_missing_end_sequence_complaint (void)
848{
849 complaint (&symfile_complaints,
3e43a32a
MS
850 _(".debug_line section has line "
851 "program sequence without an end"));
59205f5a
JB
852}
853
4d3c2250
KB
854static void
855dwarf2_complex_location_expr_complaint (void)
2e276125 856{
e2e0b3e5 857 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
858}
859
4d3c2250
KB
860static void
861dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
862 int arg3)
2e276125 863{
4d3c2250 864 complaint (&symfile_complaints,
3e43a32a
MS
865 _("const value length mismatch for '%s', got %d, expected %d"),
866 arg1, arg2, arg3);
4d3c2250
KB
867}
868
869static void
cf2c3c16 870dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 871{
4d3c2250 872 complaint (&symfile_complaints,
cf2c3c16
TT
873 _("macro info runs off end of `%s' section"),
874 section->asection->name);
4d3c2250
KB
875}
876
877static void
878dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 879{
4d3c2250 880 complaint (&symfile_complaints,
3e43a32a
MS
881 _("macro debug info contains a "
882 "malformed macro definition:\n`%s'"),
4d3c2250
KB
883 arg1);
884}
885
886static void
887dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 888{
4d3c2250 889 complaint (&symfile_complaints,
3e43a32a
MS
890 _("invalid attribute class or form for '%s' in '%s'"),
891 arg1, arg2);
4d3c2250 892}
c906108c 893
c906108c
SS
894/* local function prototypes */
895
4efb68b1 896static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 897
aaa75496
JB
898static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
899 struct objfile *);
900
918dd910
JK
901static void dwarf2_find_base_address (struct die_info *die,
902 struct dwarf2_cu *cu);
903
c67a9c90 904static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 905
72bf9492
DJ
906static void scan_partial_symbols (struct partial_die_info *,
907 CORE_ADDR *, CORE_ADDR *,
5734ee8b 908 int, struct dwarf2_cu *);
c906108c 909
72bf9492
DJ
910static void add_partial_symbol (struct partial_die_info *,
911 struct dwarf2_cu *);
63d06c5c 912
72bf9492
DJ
913static void add_partial_namespace (struct partial_die_info *pdi,
914 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 915 int need_pc, struct dwarf2_cu *cu);
63d06c5c 916
5d7cb8df
JK
917static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
918 CORE_ADDR *highpc, int need_pc,
919 struct dwarf2_cu *cu);
920
72bf9492
DJ
921static void add_partial_enumeration (struct partial_die_info *enum_pdi,
922 struct dwarf2_cu *cu);
91c24f0a 923
bc30ff58
JB
924static void add_partial_subprogram (struct partial_die_info *pdi,
925 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 926 int need_pc, struct dwarf2_cu *cu);
bc30ff58 927
fe1b8b76 928static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
929 gdb_byte *buffer, gdb_byte *info_ptr,
930 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 931
a14ed312 932static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 933
a14ed312 934static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 935
e7c27a73 936static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 937
f3dd6933 938static void dwarf2_free_abbrev_table (void *);
c906108c 939
6caca83c
CC
940static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
941
fe1b8b76 942static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 943 struct dwarf2_cu *);
72bf9492 944
57349743 945static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 946 struct dwarf2_cu *);
c906108c 947
93311388
DE
948static struct partial_die_info *load_partial_dies (bfd *,
949 gdb_byte *, gdb_byte *,
950 int, struct dwarf2_cu *);
72bf9492 951
fe1b8b76 952static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
953 struct abbrev_info *abbrev,
954 unsigned int, bfd *,
955 gdb_byte *, gdb_byte *,
956 struct dwarf2_cu *);
c906108c 957
c764a876 958static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 959 struct dwarf2_cu *);
72bf9492
DJ
960
961static void fixup_partial_die (struct partial_die_info *,
962 struct dwarf2_cu *);
963
fe1b8b76
JB
964static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
965 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 966
fe1b8b76
JB
967static gdb_byte *read_attribute_value (struct attribute *, unsigned,
968 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 969
fe1b8b76 970static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 971
fe1b8b76 972static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 973
fe1b8b76 974static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 975
fe1b8b76 976static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 977
93311388 978static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 979
fe1b8b76 980static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 981 unsigned int *);
c906108c 982
c764a876
DE
983static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
984
985static LONGEST read_checked_initial_length_and_offset
986 (bfd *, gdb_byte *, const struct comp_unit_head *,
987 unsigned int *, unsigned int *);
613e1657 988
fe1b8b76 989static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
990 unsigned int *);
991
992static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 993
fe1b8b76 994static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 995
9b1c24c8 996static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 997
fe1b8b76
JB
998static char *read_indirect_string (bfd *, gdb_byte *,
999 const struct comp_unit_head *,
1000 unsigned int *);
4bdf3d34 1001
fe1b8b76 1002static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1003
fe1b8b76 1004static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1005
fe1b8b76 1006static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 1007
e142c38c 1008static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1009
e142c38c
DJ
1010static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1011 struct dwarf2_cu *);
c906108c 1012
348e048f
DE
1013static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1014 unsigned int,
1015 struct dwarf2_cu *);
1016
05cf31d1
JB
1017static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1018 struct dwarf2_cu *cu);
1019
e142c38c 1020static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1021
e142c38c 1022static struct die_info *die_specification (struct die_info *die,
f2f0e013 1023 struct dwarf2_cu **);
63d06c5c 1024
debd256d
JB
1025static void free_line_header (struct line_header *lh);
1026
aaa75496
JB
1027static void add_file_name (struct line_header *, char *, unsigned int,
1028 unsigned int, unsigned int);
1029
debd256d
JB
1030static struct line_header *(dwarf_decode_line_header
1031 (unsigned int offset,
e7c27a73 1032 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1033
72b9f47f 1034static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1035 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1036
72b9f47f 1037static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1038
a14ed312 1039static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1040 struct dwarf2_cu *);
c906108c 1041
34eaf542
TT
1042static struct symbol *new_symbol_full (struct die_info *, struct type *,
1043 struct dwarf2_cu *, struct symbol *);
1044
a14ed312 1045static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1046 struct dwarf2_cu *);
c906108c 1047
98bfdba5
PA
1048static void dwarf2_const_value_attr (struct attribute *attr,
1049 struct type *type,
1050 const char *name,
1051 struct obstack *obstack,
1052 struct dwarf2_cu *cu, long *value,
1053 gdb_byte **bytes,
1054 struct dwarf2_locexpr_baton **baton);
2df3850c 1055
e7c27a73 1056static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1057
b4ba55a1
JB
1058static int need_gnat_info (struct dwarf2_cu *);
1059
3e43a32a
MS
1060static struct type *die_descriptive_type (struct die_info *,
1061 struct dwarf2_cu *);
b4ba55a1
JB
1062
1063static void set_descriptive_type (struct type *, struct die_info *,
1064 struct dwarf2_cu *);
1065
e7c27a73
DJ
1066static struct type *die_containing_type (struct die_info *,
1067 struct dwarf2_cu *);
c906108c 1068
673bfd45
DE
1069static struct type *lookup_die_type (struct die_info *, struct attribute *,
1070 struct dwarf2_cu *);
c906108c 1071
f792889a 1072static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1073
673bfd45
DE
1074static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1075
086ed43d 1076static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1077
6e70227d 1078static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1079 const char *suffix, int physname,
1080 struct dwarf2_cu *cu);
63d06c5c 1081
e7c27a73 1082static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1083
348e048f
DE
1084static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1085
e7c27a73 1086static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1087
e7c27a73 1088static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1089
96408a79
SA
1090static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1091
ff013f42
JK
1092static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1093 struct dwarf2_cu *, struct partial_symtab *);
1094
a14ed312 1095static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1096 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1097 struct partial_symtab *);
c906108c 1098
fae299cd
DC
1099static void get_scope_pc_bounds (struct die_info *,
1100 CORE_ADDR *, CORE_ADDR *,
1101 struct dwarf2_cu *);
1102
801e3a5b
JB
1103static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1104 CORE_ADDR, struct dwarf2_cu *);
1105
a14ed312 1106static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1107 struct dwarf2_cu *);
c906108c 1108
a14ed312 1109static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1110 struct type *, struct dwarf2_cu *);
c906108c 1111
a14ed312 1112static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1113 struct die_info *, struct type *,
e7c27a73 1114 struct dwarf2_cu *);
c906108c 1115
a14ed312 1116static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1117 struct type *,
1118 struct dwarf2_cu *);
c906108c 1119
134d01f1 1120static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1121
e7c27a73 1122static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1123
e7c27a73 1124static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1125
5d7cb8df
JK
1126static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1127
27aa8d6a
SW
1128static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1129
f55ee35c
JK
1130static struct type *read_module_type (struct die_info *die,
1131 struct dwarf2_cu *cu);
1132
38d518c9 1133static const char *namespace_name (struct die_info *die,
e142c38c 1134 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1135
134d01f1 1136static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1137
e7c27a73 1138static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1139
6e70227d 1140static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1141 struct dwarf2_cu *);
1142
93311388 1143static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1144
93311388
DE
1145static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1146 gdb_byte *info_ptr,
d97bc12b
DE
1147 gdb_byte **new_info_ptr,
1148 struct die_info *parent);
1149
93311388
DE
1150static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1151 gdb_byte *info_ptr,
fe1b8b76 1152 gdb_byte **new_info_ptr,
639d11d3
DC
1153 struct die_info *parent);
1154
93311388
DE
1155static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1156 gdb_byte *info_ptr,
fe1b8b76 1157 gdb_byte **new_info_ptr,
639d11d3
DC
1158 struct die_info *parent);
1159
93311388
DE
1160static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1161 struct die_info **, gdb_byte *,
1162 int *);
1163
e7c27a73 1164static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1165
71c25dea
TT
1166static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1167 struct obstack *);
1168
e142c38c 1169static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1170
98bfdba5
PA
1171static const char *dwarf2_full_name (char *name,
1172 struct die_info *die,
1173 struct dwarf2_cu *cu);
1174
e142c38c 1175static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1176 struct dwarf2_cu **);
9219021c 1177
a14ed312 1178static char *dwarf_tag_name (unsigned int);
c906108c 1179
a14ed312 1180static char *dwarf_attr_name (unsigned int);
c906108c 1181
a14ed312 1182static char *dwarf_form_name (unsigned int);
c906108c 1183
a14ed312 1184static char *dwarf_bool_name (unsigned int);
c906108c 1185
a14ed312 1186static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1187
1188#if 0
a14ed312 1189static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1190#endif
1191
f9aca02d 1192static struct die_info *sibling_die (struct die_info *);
c906108c 1193
d97bc12b
DE
1194static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1195
1196static void dump_die_for_error (struct die_info *);
1197
1198static void dump_die_1 (struct ui_file *, int level, int max_level,
1199 struct die_info *);
c906108c 1200
d97bc12b 1201/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1202
51545339 1203static void store_in_ref_table (struct die_info *,
10b3939b 1204 struct dwarf2_cu *);
c906108c 1205
93311388
DE
1206static int is_ref_attr (struct attribute *);
1207
c764a876 1208static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1209
43bbcdc2 1210static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1211
348e048f
DE
1212static struct die_info *follow_die_ref_or_sig (struct die_info *,
1213 struct attribute *,
1214 struct dwarf2_cu **);
1215
10b3939b
DJ
1216static struct die_info *follow_die_ref (struct die_info *,
1217 struct attribute *,
f2f0e013 1218 struct dwarf2_cu **);
c906108c 1219
348e048f
DE
1220static struct die_info *follow_die_sig (struct die_info *,
1221 struct attribute *,
1222 struct dwarf2_cu **);
1223
6c83ed52
TT
1224static struct signatured_type *lookup_signatured_type_at_offset
1225 (struct objfile *objfile,
1226 struct dwarf2_section_info *section,
1227 unsigned int offset);
1228
348e048f 1229static void read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 1230 struct dwarf2_section_info *sect,
348e048f
DE
1231 unsigned int offset);
1232
1233static void read_signatured_type (struct objfile *,
1234 struct signatured_type *type_sig);
1235
c906108c
SS
1236/* memory allocation interface */
1237
7b5a2f43 1238static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1239
f3dd6933 1240static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1241
b60c80d6 1242static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1243
e142c38c 1244static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1245
e142c38c
DJ
1246static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1247 struct dwarf2_cu *);
5fb290d7 1248
2e276125 1249static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1250 char *, bfd *, struct dwarf2_cu *,
1251 struct dwarf2_section_info *,
1252 int);
2e276125 1253
8e19ed76
PS
1254static int attr_form_is_block (struct attribute *);
1255
3690dd37
JB
1256static int attr_form_is_section_offset (struct attribute *);
1257
1258static int attr_form_is_constant (struct attribute *);
1259
8cf6f0b1
TT
1260static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1261 struct dwarf2_loclist_baton *baton,
1262 struct attribute *attr);
1263
93e7bd98
DJ
1264static void dwarf2_symbol_mark_computed (struct attribute *attr,
1265 struct symbol *sym,
1266 struct dwarf2_cu *cu);
4c2df51b 1267
93311388
DE
1268static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1269 struct abbrev_info *abbrev,
1270 struct dwarf2_cu *cu);
4bb7a0a7 1271
72bf9492
DJ
1272static void free_stack_comp_unit (void *);
1273
72bf9492
DJ
1274static hashval_t partial_die_hash (const void *item);
1275
1276static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1277
ae038cb0 1278static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1279 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1280
1281static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1282 (unsigned int offset, struct objfile *objfile);
ae038cb0 1283
9816fde3
JK
1284static void init_one_comp_unit (struct dwarf2_cu *cu,
1285 struct objfile *objfile);
1286
1287static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1288 struct die_info *comp_unit_die);
93311388 1289
ae038cb0
DJ
1290static void free_one_comp_unit (void *);
1291
1292static void free_cached_comp_units (void *);
1293
1294static void age_cached_comp_units (void);
1295
1296static void free_one_cached_comp_unit (void *);
1297
f792889a
DJ
1298static struct type *set_die_type (struct die_info *, struct type *,
1299 struct dwarf2_cu *);
1c379e20 1300
ae038cb0
DJ
1301static void create_all_comp_units (struct objfile *);
1302
1fd400ff
TT
1303static int create_debug_types_hash_table (struct objfile *objfile);
1304
93311388
DE
1305static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1306 struct objfile *);
10b3939b
DJ
1307
1308static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1309
1310static void dwarf2_add_dependence (struct dwarf2_cu *,
1311 struct dwarf2_per_cu_data *);
1312
ae038cb0
DJ
1313static void dwarf2_mark (struct dwarf2_cu *);
1314
1315static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1316
673bfd45
DE
1317static struct type *get_die_type_at_offset (unsigned int,
1318 struct dwarf2_per_cu_data *per_cu);
1319
f792889a 1320static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1321
9291a0cd
TT
1322static void dwarf2_release_queue (void *dummy);
1323
1324static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1325 struct objfile *objfile);
1326
1327static void process_queue (struct objfile *objfile);
1328
1329static void find_file_and_directory (struct die_info *die,
1330 struct dwarf2_cu *cu,
1331 char **name, char **comp_dir);
1332
1333static char *file_full_name (int file, struct line_header *lh,
1334 const char *comp_dir);
1335
1336static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1337 gdb_byte *info_ptr,
1338 gdb_byte *buffer,
1339 unsigned int buffer_size,
460c1c54 1340 bfd *abfd,
b0df02fd 1341 int is_debug_types_section);
9291a0cd
TT
1342
1343static void init_cu_die_reader (struct die_reader_specs *reader,
1344 struct dwarf2_cu *cu);
1345
673bfd45 1346static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1347
9291a0cd
TT
1348#if WORDS_BIGENDIAN
1349
1350/* Convert VALUE between big- and little-endian. */
1351static offset_type
1352byte_swap (offset_type value)
1353{
1354 offset_type result;
1355
1356 result = (value & 0xff) << 24;
1357 result |= (value & 0xff00) << 8;
1358 result |= (value & 0xff0000) >> 8;
1359 result |= (value & 0xff000000) >> 24;
1360 return result;
1361}
1362
1363#define MAYBE_SWAP(V) byte_swap (V)
1364
1365#else
1366#define MAYBE_SWAP(V) (V)
1367#endif /* WORDS_BIGENDIAN */
1368
1369/* The suffix for an index file. */
1370#define INDEX_SUFFIX ".gdb-index"
1371
3da10d80
KS
1372static const char *dwarf2_physname (char *name, struct die_info *die,
1373 struct dwarf2_cu *cu);
1374
c906108c 1375/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1376 information and return true if we have enough to do something.
1377 NAMES points to the dwarf2 section names, or is NULL if the standard
1378 ELF names are used. */
c906108c
SS
1379
1380int
251d32d9
TG
1381dwarf2_has_info (struct objfile *objfile,
1382 const struct dwarf2_debug_sections *names)
c906108c 1383{
be391dca
TT
1384 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1385 if (!dwarf2_per_objfile)
1386 {
1387 /* Initialize per-objfile state. */
1388 struct dwarf2_per_objfile *data
1389 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1390
be391dca
TT
1391 memset (data, 0, sizeof (*data));
1392 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1393 dwarf2_per_objfile = data;
6502dd73 1394
251d32d9
TG
1395 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1396 (void *) names);
be391dca
TT
1397 dwarf2_per_objfile->objfile = objfile;
1398 }
1399 return (dwarf2_per_objfile->info.asection != NULL
1400 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1401}
1402
251d32d9
TG
1403/* When loading sections, we look either for uncompressed section or for
1404 compressed section names. */
233a11ab
CS
1405
1406static int
251d32d9
TG
1407section_is_p (const char *section_name,
1408 const struct dwarf2_section_names *names)
233a11ab 1409{
251d32d9
TG
1410 if (names->normal != NULL
1411 && strcmp (section_name, names->normal) == 0)
1412 return 1;
1413 if (names->compressed != NULL
1414 && strcmp (section_name, names->compressed) == 0)
1415 return 1;
1416 return 0;
233a11ab
CS
1417}
1418
c906108c
SS
1419/* This function is mapped across the sections and remembers the
1420 offset and size of each of the debugging sections we are interested
1421 in. */
1422
1423static void
251d32d9 1424dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1425{
251d32d9
TG
1426 const struct dwarf2_debug_sections *names;
1427
1428 if (vnames == NULL)
1429 names = &dwarf2_elf_names;
1430 else
1431 names = (const struct dwarf2_debug_sections *) vnames;
1432
1433 if (section_is_p (sectp->name, &names->info))
c906108c 1434 {
dce234bc
PP
1435 dwarf2_per_objfile->info.asection = sectp;
1436 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1437 }
251d32d9 1438 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1439 {
dce234bc
PP
1440 dwarf2_per_objfile->abbrev.asection = sectp;
1441 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1442 }
251d32d9 1443 else if (section_is_p (sectp->name, &names->line))
c906108c 1444 {
dce234bc
PP
1445 dwarf2_per_objfile->line.asection = sectp;
1446 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1447 }
251d32d9 1448 else if (section_is_p (sectp->name, &names->loc))
c906108c 1449 {
dce234bc
PP
1450 dwarf2_per_objfile->loc.asection = sectp;
1451 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1452 }
251d32d9 1453 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1454 {
dce234bc
PP
1455 dwarf2_per_objfile->macinfo.asection = sectp;
1456 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1457 }
cf2c3c16
TT
1458 else if (section_is_p (sectp->name, &names->macro))
1459 {
1460 dwarf2_per_objfile->macro.asection = sectp;
1461 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1462 }
251d32d9 1463 else if (section_is_p (sectp->name, &names->str))
c906108c 1464 {
dce234bc
PP
1465 dwarf2_per_objfile->str.asection = sectp;
1466 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1467 }
251d32d9 1468 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1469 {
dce234bc
PP
1470 dwarf2_per_objfile->frame.asection = sectp;
1471 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1472 }
251d32d9 1473 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1474 {
3799ccc6 1475 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1476
3799ccc6
EZ
1477 if (aflag & SEC_HAS_CONTENTS)
1478 {
dce234bc
PP
1479 dwarf2_per_objfile->eh_frame.asection = sectp;
1480 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1481 }
b6af0555 1482 }
251d32d9 1483 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1484 {
dce234bc
PP
1485 dwarf2_per_objfile->ranges.asection = sectp;
1486 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1487 }
251d32d9 1488 else if (section_is_p (sectp->name, &names->types))
348e048f 1489 {
8b70b953
TT
1490 struct dwarf2_section_info type_section;
1491
1492 memset (&type_section, 0, sizeof (type_section));
1493 type_section.asection = sectp;
1494 type_section.size = bfd_get_section_size (sectp);
1495
1496 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1497 &type_section);
348e048f 1498 }
251d32d9 1499 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1500 {
1501 dwarf2_per_objfile->gdb_index.asection = sectp;
1502 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1503 }
dce234bc 1504
72dca2f5
FR
1505 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1506 && bfd_section_vma (abfd, sectp) == 0)
1507 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1508}
1509
dce234bc
PP
1510/* Decompress a section that was compressed using zlib. Store the
1511 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1512
1513static void
dce234bc
PP
1514zlib_decompress_section (struct objfile *objfile, asection *sectp,
1515 gdb_byte **outbuf, bfd_size_type *outsize)
1516{
1517 bfd *abfd = objfile->obfd;
1518#ifndef HAVE_ZLIB_H
1519 error (_("Support for zlib-compressed DWARF data (from '%s') "
1520 "is disabled in this copy of GDB"),
1521 bfd_get_filename (abfd));
1522#else
1523 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1524 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1525 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1526 bfd_size_type uncompressed_size;
1527 gdb_byte *uncompressed_buffer;
1528 z_stream strm;
1529 int rc;
1530 int header_size = 12;
1531
1532 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1533 || bfd_bread (compressed_buffer,
1534 compressed_size, abfd) != compressed_size)
dce234bc
PP
1535 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1536 bfd_get_filename (abfd));
1537
1538 /* Read the zlib header. In this case, it should be "ZLIB" followed
1539 by the uncompressed section size, 8 bytes in big-endian order. */
1540 if (compressed_size < header_size
1541 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1542 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1543 bfd_get_filename (abfd));
1544 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1545 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1546 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1547 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1548 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1549 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1550 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1551 uncompressed_size += compressed_buffer[11];
1552
1553 /* It is possible the section consists of several compressed
1554 buffers concatenated together, so we uncompress in a loop. */
1555 strm.zalloc = NULL;
1556 strm.zfree = NULL;
1557 strm.opaque = NULL;
1558 strm.avail_in = compressed_size - header_size;
1559 strm.next_in = (Bytef*) compressed_buffer + header_size;
1560 strm.avail_out = uncompressed_size;
1561 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1562 uncompressed_size);
1563 rc = inflateInit (&strm);
1564 while (strm.avail_in > 0)
1565 {
1566 if (rc != Z_OK)
1567 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1568 bfd_get_filename (abfd), rc);
1569 strm.next_out = ((Bytef*) uncompressed_buffer
1570 + (uncompressed_size - strm.avail_out));
1571 rc = inflate (&strm, Z_FINISH);
1572 if (rc != Z_STREAM_END)
1573 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1574 bfd_get_filename (abfd), rc);
1575 rc = inflateReset (&strm);
1576 }
1577 rc = inflateEnd (&strm);
1578 if (rc != Z_OK
1579 || strm.avail_out != 0)
1580 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1581 bfd_get_filename (abfd), rc);
1582
affddf13 1583 do_cleanups (cleanup);
dce234bc
PP
1584 *outbuf = uncompressed_buffer;
1585 *outsize = uncompressed_size;
1586#endif
233a11ab
CS
1587}
1588
9e0ac564
TT
1589/* A helper function that decides whether a section is empty. */
1590
1591static int
1592dwarf2_section_empty_p (struct dwarf2_section_info *info)
1593{
1594 return info->asection == NULL || info->size == 0;
1595}
1596
dce234bc
PP
1597/* Read the contents of the section SECTP from object file specified by
1598 OBJFILE, store info about the section into INFO.
1599 If the section is compressed, uncompress it before returning. */
c906108c 1600
dce234bc
PP
1601static void
1602dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1603{
dce234bc
PP
1604 bfd *abfd = objfile->obfd;
1605 asection *sectp = info->asection;
1606 gdb_byte *buf, *retbuf;
1607 unsigned char header[4];
c906108c 1608
be391dca
TT
1609 if (info->readin)
1610 return;
dce234bc 1611 info->buffer = NULL;
b315ab21 1612 info->map_addr = NULL;
be391dca 1613 info->readin = 1;
188dd5d6 1614
9e0ac564 1615 if (dwarf2_section_empty_p (info))
dce234bc 1616 return;
c906108c 1617
dce234bc
PP
1618 /* Check if the file has a 4-byte header indicating compression. */
1619 if (info->size > sizeof (header)
1620 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1621 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1622 {
1623 /* Upon decompression, update the buffer and its size. */
1624 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1625 {
1626 zlib_decompress_section (objfile, sectp, &info->buffer,
1627 &info->size);
1628 return;
1629 }
1630 }
4bdf3d34 1631
dce234bc
PP
1632#ifdef HAVE_MMAP
1633 if (pagesize == 0)
1634 pagesize = getpagesize ();
2e276125 1635
dce234bc
PP
1636 /* Only try to mmap sections which are large enough: we don't want to
1637 waste space due to fragmentation. Also, only try mmap for sections
1638 without relocations. */
1639
1640 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1641 {
b315ab21
TG
1642 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1643 MAP_PRIVATE, sectp->filepos,
1644 &info->map_addr, &info->map_len);
dce234bc 1645
b315ab21 1646 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1647 {
be391dca 1648#if HAVE_POSIX_MADVISE
b315ab21 1649 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1650#endif
dce234bc
PP
1651 return;
1652 }
1653 }
1654#endif
1655
1656 /* If we get here, we are a normal, not-compressed section. */
1657 info->buffer = buf
1658 = obstack_alloc (&objfile->objfile_obstack, info->size);
1659
1660 /* When debugging .o files, we may need to apply relocations; see
1661 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1662 We never compress sections in .o files, so we only need to
1663 try this when the section is not compressed. */
ac8035ab 1664 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1665 if (retbuf != NULL)
1666 {
1667 info->buffer = retbuf;
1668 return;
1669 }
1670
1671 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1672 || bfd_bread (buf, info->size, abfd) != info->size)
1673 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1674 bfd_get_filename (abfd));
1675}
1676
9e0ac564
TT
1677/* A helper function that returns the size of a section in a safe way.
1678 If you are positive that the section has been read before using the
1679 size, then it is safe to refer to the dwarf2_section_info object's
1680 "size" field directly. In other cases, you must call this
1681 function, because for compressed sections the size field is not set
1682 correctly until the section has been read. */
1683
1684static bfd_size_type
1685dwarf2_section_size (struct objfile *objfile,
1686 struct dwarf2_section_info *info)
1687{
1688 if (!info->readin)
1689 dwarf2_read_section (objfile, info);
1690 return info->size;
1691}
1692
dce234bc 1693/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1694 SECTION_NAME. */
af34e669 1695
dce234bc 1696void
3017a003
TG
1697dwarf2_get_section_info (struct objfile *objfile,
1698 enum dwarf2_section_enum sect,
dce234bc
PP
1699 asection **sectp, gdb_byte **bufp,
1700 bfd_size_type *sizep)
1701{
1702 struct dwarf2_per_objfile *data
1703 = objfile_data (objfile, dwarf2_objfile_data_key);
1704 struct dwarf2_section_info *info;
a3b2a86b
TT
1705
1706 /* We may see an objfile without any DWARF, in which case we just
1707 return nothing. */
1708 if (data == NULL)
1709 {
1710 *sectp = NULL;
1711 *bufp = NULL;
1712 *sizep = 0;
1713 return;
1714 }
3017a003
TG
1715 switch (sect)
1716 {
1717 case DWARF2_DEBUG_FRAME:
1718 info = &data->frame;
1719 break;
1720 case DWARF2_EH_FRAME:
1721 info = &data->eh_frame;
1722 break;
1723 default:
1724 gdb_assert_not_reached ("unexpected section");
1725 }
dce234bc 1726
9e0ac564 1727 dwarf2_read_section (objfile, info);
dce234bc
PP
1728
1729 *sectp = info->asection;
1730 *bufp = info->buffer;
1731 *sizep = info->size;
1732}
1733
9291a0cd 1734\f
7b9f3c50
DE
1735/* DWARF quick_symbols_functions support. */
1736
1737/* TUs can share .debug_line entries, and there can be a lot more TUs than
1738 unique line tables, so we maintain a separate table of all .debug_line
1739 derived entries to support the sharing.
1740 All the quick functions need is the list of file names. We discard the
1741 line_header when we're done and don't need to record it here. */
1742struct quick_file_names
1743{
1744 /* The offset in .debug_line of the line table. We hash on this. */
1745 unsigned int offset;
1746
1747 /* The number of entries in file_names, real_names. */
1748 unsigned int num_file_names;
1749
1750 /* The file names from the line table, after being run through
1751 file_full_name. */
1752 const char **file_names;
1753
1754 /* The file names from the line table after being run through
1755 gdb_realpath. These are computed lazily. */
1756 const char **real_names;
1757};
1758
1759/* When using the index (and thus not using psymtabs), each CU has an
1760 object of this type. This is used to hold information needed by
1761 the various "quick" methods. */
1762struct dwarf2_per_cu_quick_data
1763{
1764 /* The file table. This can be NULL if there was no file table
1765 or it's currently not read in.
1766 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1767 struct quick_file_names *file_names;
1768
1769 /* The corresponding symbol table. This is NULL if symbols for this
1770 CU have not yet been read. */
1771 struct symtab *symtab;
1772
1773 /* A temporary mark bit used when iterating over all CUs in
1774 expand_symtabs_matching. */
1775 unsigned int mark : 1;
1776
1777 /* True if we've tried to read the file table and found there isn't one.
1778 There will be no point in trying to read it again next time. */
1779 unsigned int no_file_data : 1;
1780};
1781
1782/* Hash function for a quick_file_names. */
1783
1784static hashval_t
1785hash_file_name_entry (const void *e)
1786{
1787 const struct quick_file_names *file_data = e;
1788
1789 return file_data->offset;
1790}
1791
1792/* Equality function for a quick_file_names. */
1793
1794static int
1795eq_file_name_entry (const void *a, const void *b)
1796{
1797 const struct quick_file_names *ea = a;
1798 const struct quick_file_names *eb = b;
1799
1800 return ea->offset == eb->offset;
1801}
1802
1803/* Delete function for a quick_file_names. */
1804
1805static void
1806delete_file_name_entry (void *e)
1807{
1808 struct quick_file_names *file_data = e;
1809 int i;
1810
1811 for (i = 0; i < file_data->num_file_names; ++i)
1812 {
1813 xfree ((void*) file_data->file_names[i]);
1814 if (file_data->real_names)
1815 xfree ((void*) file_data->real_names[i]);
1816 }
1817
1818 /* The space for the struct itself lives on objfile_obstack,
1819 so we don't free it here. */
1820}
1821
1822/* Create a quick_file_names hash table. */
1823
1824static htab_t
1825create_quick_file_names_table (unsigned int nr_initial_entries)
1826{
1827 return htab_create_alloc (nr_initial_entries,
1828 hash_file_name_entry, eq_file_name_entry,
1829 delete_file_name_entry, xcalloc, xfree);
1830}
9291a0cd 1831
918dd910
JK
1832/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1833 have to be created afterwards. You should call age_cached_comp_units after
1834 processing PER_CU->CU. dw2_setup must have been already called. */
1835
1836static void
1837load_cu (struct dwarf2_per_cu_data *per_cu)
1838{
b0df02fd 1839 if (per_cu->debug_types_section)
8b70b953 1840 read_signatured_type_at_offset (per_cu->objfile,
b0df02fd 1841 per_cu->debug_types_section,
8b70b953 1842 per_cu->offset);
918dd910
JK
1843 else
1844 load_full_comp_unit (per_cu, per_cu->objfile);
1845
1846 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1847
1848 gdb_assert (per_cu->cu != NULL);
1849}
1850
9291a0cd
TT
1851/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1852 this CU came. */
2fdf6df6 1853
9291a0cd
TT
1854static void
1855dw2_do_instantiate_symtab (struct objfile *objfile,
1856 struct dwarf2_per_cu_data *per_cu)
1857{
1858 struct cleanup *back_to;
1859
1860 back_to = make_cleanup (dwarf2_release_queue, NULL);
1861
1862 queue_comp_unit (per_cu, objfile);
1863
918dd910 1864 load_cu (per_cu);
9291a0cd
TT
1865
1866 process_queue (objfile);
1867
1868 /* Age the cache, releasing compilation units that have not
1869 been used recently. */
1870 age_cached_comp_units ();
1871
1872 do_cleanups (back_to);
1873}
1874
1875/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1876 the objfile from which this CU came. Returns the resulting symbol
1877 table. */
2fdf6df6 1878
9291a0cd
TT
1879static struct symtab *
1880dw2_instantiate_symtab (struct objfile *objfile,
1881 struct dwarf2_per_cu_data *per_cu)
1882{
1883 if (!per_cu->v.quick->symtab)
1884 {
1885 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1886 increment_reading_symtab ();
1887 dw2_do_instantiate_symtab (objfile, per_cu);
1888 do_cleanups (back_to);
1889 }
1890 return per_cu->v.quick->symtab;
1891}
1892
1fd400ff 1893/* Return the CU given its index. */
2fdf6df6 1894
1fd400ff
TT
1895static struct dwarf2_per_cu_data *
1896dw2_get_cu (int index)
1897{
1898 if (index >= dwarf2_per_objfile->n_comp_units)
1899 {
1900 index -= dwarf2_per_objfile->n_comp_units;
1901 return dwarf2_per_objfile->type_comp_units[index];
1902 }
1903 return dwarf2_per_objfile->all_comp_units[index];
1904}
1905
9291a0cd
TT
1906/* A helper function that knows how to read a 64-bit value in a way
1907 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1908 otherwise. */
2fdf6df6 1909
9291a0cd
TT
1910static int
1911extract_cu_value (const char *bytes, ULONGEST *result)
1912{
1913 if (sizeof (ULONGEST) < 8)
1914 {
1915 int i;
1916
1917 /* Ignore the upper 4 bytes if they are all zero. */
1918 for (i = 0; i < 4; ++i)
1919 if (bytes[i + 4] != 0)
1920 return 0;
1921
1922 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1923 }
1924 else
1925 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1926 return 1;
1927}
1928
1929/* Read the CU list from the mapped index, and use it to create all
1930 the CU objects for this objfile. Return 0 if something went wrong,
1931 1 if everything went ok. */
2fdf6df6 1932
9291a0cd 1933static int
1fd400ff
TT
1934create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1935 offset_type cu_list_elements)
9291a0cd
TT
1936{
1937 offset_type i;
9291a0cd
TT
1938
1939 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1940 dwarf2_per_objfile->all_comp_units
1941 = obstack_alloc (&objfile->objfile_obstack,
1942 dwarf2_per_objfile->n_comp_units
1943 * sizeof (struct dwarf2_per_cu_data *));
1944
1945 for (i = 0; i < cu_list_elements; i += 2)
1946 {
1947 struct dwarf2_per_cu_data *the_cu;
1948 ULONGEST offset, length;
1949
1950 if (!extract_cu_value (cu_list, &offset)
1951 || !extract_cu_value (cu_list + 8, &length))
1952 return 0;
1953 cu_list += 2 * 8;
1954
1955 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1956 struct dwarf2_per_cu_data);
1957 the_cu->offset = offset;
1958 the_cu->length = length;
1959 the_cu->objfile = objfile;
1960 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1961 struct dwarf2_per_cu_quick_data);
1962 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1963 }
1964
1965 return 1;
1966}
1967
1fd400ff 1968/* Create the signatured type hash table from the index. */
673bfd45 1969
1fd400ff 1970static int
673bfd45 1971create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1972 struct dwarf2_section_info *section,
673bfd45
DE
1973 const gdb_byte *bytes,
1974 offset_type elements)
1fd400ff
TT
1975{
1976 offset_type i;
673bfd45 1977 htab_t sig_types_hash;
1fd400ff
TT
1978
1979 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1980 dwarf2_per_objfile->type_comp_units
1981 = obstack_alloc (&objfile->objfile_obstack,
1982 dwarf2_per_objfile->n_type_comp_units
1983 * sizeof (struct dwarf2_per_cu_data *));
1984
673bfd45 1985 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1986
1987 for (i = 0; i < elements; i += 3)
1988 {
1989 struct signatured_type *type_sig;
1990 ULONGEST offset, type_offset, signature;
1991 void **slot;
1992
1993 if (!extract_cu_value (bytes, &offset)
1994 || !extract_cu_value (bytes + 8, &type_offset))
1995 return 0;
1996 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1997 bytes += 3 * 8;
1998
1999 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2000 struct signatured_type);
2001 type_sig->signature = signature;
1fd400ff 2002 type_sig->type_offset = type_offset;
b0df02fd 2003 type_sig->per_cu.debug_types_section = section;
1fd400ff
TT
2004 type_sig->per_cu.offset = offset;
2005 type_sig->per_cu.objfile = objfile;
2006 type_sig->per_cu.v.quick
2007 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2008 struct dwarf2_per_cu_quick_data);
2009
673bfd45 2010 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
2011 *slot = type_sig;
2012
2013 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
2014 }
2015
673bfd45 2016 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2017
2018 return 1;
2019}
2020
9291a0cd
TT
2021/* Read the address map data from the mapped index, and use it to
2022 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2023
9291a0cd
TT
2024static void
2025create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2026{
2027 const gdb_byte *iter, *end;
2028 struct obstack temp_obstack;
2029 struct addrmap *mutable_map;
2030 struct cleanup *cleanup;
2031 CORE_ADDR baseaddr;
2032
2033 obstack_init (&temp_obstack);
2034 cleanup = make_cleanup_obstack_free (&temp_obstack);
2035 mutable_map = addrmap_create_mutable (&temp_obstack);
2036
2037 iter = index->address_table;
2038 end = iter + index->address_table_size;
2039
2040 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2041
2042 while (iter < end)
2043 {
2044 ULONGEST hi, lo, cu_index;
2045 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2046 iter += 8;
2047 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2048 iter += 8;
2049 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2050 iter += 4;
2051
2052 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2053 dw2_get_cu (cu_index));
9291a0cd
TT
2054 }
2055
2056 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2057 &objfile->objfile_obstack);
2058 do_cleanups (cleanup);
2059}
2060
59d7bcaf
JK
2061/* The hash function for strings in the mapped index. This is the same as
2062 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2063 implementation. This is necessary because the hash function is tied to the
2064 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2065 SYMBOL_HASH_NEXT.
2066
2067 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2068
9291a0cd 2069static hashval_t
559a7a62 2070mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2071{
2072 const unsigned char *str = (const unsigned char *) p;
2073 hashval_t r = 0;
2074 unsigned char c;
2075
2076 while ((c = *str++) != 0)
559a7a62
JK
2077 {
2078 if (index_version >= 5)
2079 c = tolower (c);
2080 r = r * 67 + c - 113;
2081 }
9291a0cd
TT
2082
2083 return r;
2084}
2085
2086/* Find a slot in the mapped index INDEX for the object named NAME.
2087 If NAME is found, set *VEC_OUT to point to the CU vector in the
2088 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2089
9291a0cd
TT
2090static int
2091find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2092 offset_type **vec_out)
2093{
0cf03b49
JK
2094 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2095 offset_type hash;
9291a0cd 2096 offset_type slot, step;
559a7a62 2097 int (*cmp) (const char *, const char *);
9291a0cd 2098
0cf03b49
JK
2099 if (current_language->la_language == language_cplus
2100 || current_language->la_language == language_java
2101 || current_language->la_language == language_fortran)
2102 {
2103 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2104 not contain any. */
2105 const char *paren = strchr (name, '(');
2106
2107 if (paren)
2108 {
2109 char *dup;
2110
2111 dup = xmalloc (paren - name + 1);
2112 memcpy (dup, name, paren - name);
2113 dup[paren - name] = 0;
2114
2115 make_cleanup (xfree, dup);
2116 name = dup;
2117 }
2118 }
2119
559a7a62
JK
2120 /* Index version 4 did not support case insensitive searches. But the
2121 indexes for case insensitive languages are built in lowercase, therefore
2122 simulate our NAME being searched is also lowercased. */
2123 hash = mapped_index_string_hash ((index->version == 4
2124 && case_sensitivity == case_sensitive_off
2125 ? 5 : index->version),
2126 name);
2127
3876f04e
DE
2128 slot = hash & (index->symbol_table_slots - 1);
2129 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2130 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2131
2132 for (;;)
2133 {
2134 /* Convert a slot number to an offset into the table. */
2135 offset_type i = 2 * slot;
2136 const char *str;
3876f04e 2137 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2138 {
2139 do_cleanups (back_to);
2140 return 0;
2141 }
9291a0cd 2142
3876f04e 2143 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2144 if (!cmp (name, str))
9291a0cd
TT
2145 {
2146 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2147 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2148 do_cleanups (back_to);
9291a0cd
TT
2149 return 1;
2150 }
2151
3876f04e 2152 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2153 }
2154}
2155
2156/* Read the index file. If everything went ok, initialize the "quick"
2157 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2158
9291a0cd
TT
2159static int
2160dwarf2_read_index (struct objfile *objfile)
2161{
9291a0cd
TT
2162 char *addr;
2163 struct mapped_index *map;
b3b272e1 2164 offset_type *metadata;
ac0b195c
KW
2165 const gdb_byte *cu_list;
2166 const gdb_byte *types_list = NULL;
2167 offset_type version, cu_list_elements;
2168 offset_type types_list_elements = 0;
1fd400ff 2169 int i;
9291a0cd 2170
9e0ac564 2171 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2172 return 0;
82430852
JK
2173
2174 /* Older elfutils strip versions could keep the section in the main
2175 executable while splitting it for the separate debug info file. */
2176 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2177 & SEC_HAS_CONTENTS) == 0)
2178 return 0;
2179
9291a0cd
TT
2180 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2181
2182 addr = dwarf2_per_objfile->gdb_index.buffer;
2183 /* Version check. */
1fd400ff 2184 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2185 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2186 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2187 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2188 indices. Index version 4 uses a different hash function than index
2189 version 5 and later. */
831adc1f 2190 if (version < 4)
9291a0cd 2191 return 0;
594e8718
JK
2192 /* Indexes with higher version than the one supported by GDB may be no
2193 longer backward compatible. */
559a7a62 2194 if (version > 5)
594e8718 2195 return 0;
9291a0cd
TT
2196
2197 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2198 map->version = version;
b3b272e1 2199 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2200
2201 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2202
2203 i = 0;
2204 cu_list = addr + MAYBE_SWAP (metadata[i]);
2205 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2206 / 8);
1fd400ff
TT
2207 ++i;
2208
987d643c
TT
2209 types_list = addr + MAYBE_SWAP (metadata[i]);
2210 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2211 - MAYBE_SWAP (metadata[i]))
2212 / 8);
2213 ++i;
1fd400ff
TT
2214
2215 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2216 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2217 - MAYBE_SWAP (metadata[i]));
2218 ++i;
2219
3876f04e
DE
2220 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2221 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2222 - MAYBE_SWAP (metadata[i]))
2223 / (2 * sizeof (offset_type)));
1fd400ff 2224 ++i;
9291a0cd 2225
1fd400ff
TT
2226 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2227
2228 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2229 return 0;
2230
8b70b953
TT
2231 if (types_list_elements)
2232 {
2233 struct dwarf2_section_info *section;
2234
2235 /* We can only handle a single .debug_types when we have an
2236 index. */
2237 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2238 return 0;
2239
2240 section = VEC_index (dwarf2_section_info_def,
2241 dwarf2_per_objfile->types, 0);
2242
2243 if (!create_signatured_type_table_from_index (objfile, section,
2244 types_list,
2245 types_list_elements))
2246 return 0;
2247 }
9291a0cd
TT
2248
2249 create_addrmap_from_index (objfile, map);
2250
2251 dwarf2_per_objfile->index_table = map;
2252 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2253 dwarf2_per_objfile->quick_file_names_table =
2254 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2255
2256 return 1;
2257}
2258
2259/* A helper for the "quick" functions which sets the global
2260 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2261
9291a0cd
TT
2262static void
2263dw2_setup (struct objfile *objfile)
2264{
2265 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2266 gdb_assert (dwarf2_per_objfile);
2267}
2268
2269/* A helper for the "quick" functions which attempts to read the line
2270 table for THIS_CU. */
2fdf6df6 2271
7b9f3c50
DE
2272static struct quick_file_names *
2273dw2_get_file_names (struct objfile *objfile,
2274 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2275{
2276 bfd *abfd = objfile->obfd;
7b9f3c50 2277 struct line_header *lh;
9291a0cd
TT
2278 struct attribute *attr;
2279 struct cleanup *cleanups;
2280 struct die_info *comp_unit_die;
36374493 2281 struct dwarf2_section_info* sec;
adabb602 2282 gdb_byte *info_ptr, *buffer;
9291a0cd
TT
2283 int has_children, i;
2284 struct dwarf2_cu cu;
2285 unsigned int bytes_read, buffer_size;
2286 struct die_reader_specs reader_specs;
2287 char *name, *comp_dir;
7b9f3c50
DE
2288 void **slot;
2289 struct quick_file_names *qfn;
2290 unsigned int line_offset;
9291a0cd 2291
7b9f3c50
DE
2292 if (this_cu->v.quick->file_names != NULL)
2293 return this_cu->v.quick->file_names;
2294 /* If we know there is no line data, no point in looking again. */
2295 if (this_cu->v.quick->no_file_data)
2296 return NULL;
9291a0cd 2297
9816fde3 2298 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2299 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2300
b0df02fd
DE
2301 if (this_cu->debug_types_section)
2302 sec = this_cu->debug_types_section;
36374493
DE
2303 else
2304 sec = &dwarf2_per_objfile->info;
2305 dwarf2_read_section (objfile, sec);
2306 buffer_size = sec->size;
2307 buffer = sec->buffer;
9291a0cd 2308 info_ptr = buffer + this_cu->offset;
9291a0cd
TT
2309
2310 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2311 buffer, buffer_size,
460c1c54 2312 abfd,
b0df02fd 2313 this_cu->debug_types_section != NULL);
9291a0cd 2314
6caca83c
CC
2315 /* Skip dummy compilation units. */
2316 if (info_ptr >= buffer + buffer_size
2317 || peek_abbrev_code (abfd, info_ptr) == 0)
2318 {
2319 do_cleanups (cleanups);
2320 return NULL;
2321 }
2322
9291a0cd
TT
2323 this_cu->cu = &cu;
2324 cu.per_cu = this_cu;
2325
2326 dwarf2_read_abbrevs (abfd, &cu);
2327 make_cleanup (dwarf2_free_abbrev_table, &cu);
2328
9291a0cd 2329 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2330 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2331 &has_children);
9291a0cd 2332
7b9f3c50
DE
2333 lh = NULL;
2334 slot = NULL;
2335 line_offset = 0;
9291a0cd
TT
2336 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2337 if (attr)
2338 {
7b9f3c50
DE
2339 struct quick_file_names find_entry;
2340
2341 line_offset = DW_UNSND (attr);
2342
2343 /* We may have already read in this line header (TU line header sharing).
2344 If we have we're done. */
2345 find_entry.offset = line_offset;
2346 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2347 &find_entry, INSERT);
2348 if (*slot != NULL)
2349 {
2350 do_cleanups (cleanups);
2351 this_cu->v.quick->file_names = *slot;
2352 return *slot;
2353 }
2354
9291a0cd
TT
2355 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2356 }
2357 if (lh == NULL)
2358 {
2359 do_cleanups (cleanups);
7b9f3c50
DE
2360 this_cu->v.quick->no_file_data = 1;
2361 return NULL;
9291a0cd
TT
2362 }
2363
7b9f3c50
DE
2364 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2365 qfn->offset = line_offset;
2366 gdb_assert (slot != NULL);
2367 *slot = qfn;
9291a0cd 2368
7b9f3c50 2369 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2370
7b9f3c50
DE
2371 qfn->num_file_names = lh->num_file_names;
2372 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2373 lh->num_file_names * sizeof (char *));
9291a0cd 2374 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2375 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2376 qfn->real_names = NULL;
9291a0cd 2377
7b9f3c50 2378 free_line_header (lh);
9291a0cd 2379 do_cleanups (cleanups);
7b9f3c50
DE
2380
2381 this_cu->v.quick->file_names = qfn;
2382 return qfn;
9291a0cd
TT
2383}
2384
2385/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2386 real path for a given file name from the line table. */
2fdf6df6 2387
9291a0cd 2388static const char *
7b9f3c50
DE
2389dw2_get_real_path (struct objfile *objfile,
2390 struct quick_file_names *qfn, int index)
9291a0cd 2391{
7b9f3c50
DE
2392 if (qfn->real_names == NULL)
2393 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2394 qfn->num_file_names, sizeof (char *));
9291a0cd 2395
7b9f3c50
DE
2396 if (qfn->real_names[index] == NULL)
2397 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2398
7b9f3c50 2399 return qfn->real_names[index];
9291a0cd
TT
2400}
2401
2402static struct symtab *
2403dw2_find_last_source_symtab (struct objfile *objfile)
2404{
2405 int index;
ae2de4f8 2406
9291a0cd
TT
2407 dw2_setup (objfile);
2408 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2409 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2410}
2411
7b9f3c50
DE
2412/* Traversal function for dw2_forget_cached_source_info. */
2413
2414static int
2415dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2416{
7b9f3c50 2417 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2418
7b9f3c50 2419 if (file_data->real_names)
9291a0cd 2420 {
7b9f3c50 2421 int i;
9291a0cd 2422
7b9f3c50 2423 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2424 {
7b9f3c50
DE
2425 xfree ((void*) file_data->real_names[i]);
2426 file_data->real_names[i] = NULL;
9291a0cd
TT
2427 }
2428 }
7b9f3c50
DE
2429
2430 return 1;
2431}
2432
2433static void
2434dw2_forget_cached_source_info (struct objfile *objfile)
2435{
2436 dw2_setup (objfile);
2437
2438 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2439 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2440}
2441
f8eba3c6
TT
2442/* Helper function for dw2_map_symtabs_matching_filename that expands
2443 the symtabs and calls the iterator. */
2444
2445static int
2446dw2_map_expand_apply (struct objfile *objfile,
2447 struct dwarf2_per_cu_data *per_cu,
2448 const char *name,
2449 const char *full_path, const char *real_path,
2450 int (*callback) (struct symtab *, void *),
2451 void *data)
2452{
2453 struct symtab *last_made = objfile->symtabs;
2454
2455 /* Don't visit already-expanded CUs. */
2456 if (per_cu->v.quick->symtab)
2457 return 0;
2458
2459 /* This may expand more than one symtab, and we want to iterate over
2460 all of them. */
2461 dw2_instantiate_symtab (objfile, per_cu);
2462
2463 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2464 objfile->symtabs, last_made);
2465}
2466
2467/* Implementation of the map_symtabs_matching_filename method. */
2468
9291a0cd 2469static int
f8eba3c6
TT
2470dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2471 const char *full_path, const char *real_path,
2472 int (*callback) (struct symtab *, void *),
2473 void *data)
9291a0cd
TT
2474{
2475 int i;
c011a4f4
DE
2476 const char *name_basename = lbasename (name);
2477 int check_basename = name_basename == name;
9291a0cd
TT
2478 struct dwarf2_per_cu_data *base_cu = NULL;
2479
2480 dw2_setup (objfile);
ae2de4f8 2481
1fd400ff
TT
2482 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2483 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2484 {
2485 int j;
e254ef6a 2486 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2487 struct quick_file_names *file_data;
9291a0cd 2488
3d7bb9d9 2489 /* We only need to look at symtabs not already expanded. */
e254ef6a 2490 if (per_cu->v.quick->symtab)
9291a0cd
TT
2491 continue;
2492
7b9f3c50
DE
2493 file_data = dw2_get_file_names (objfile, per_cu);
2494 if (file_data == NULL)
9291a0cd
TT
2495 continue;
2496
7b9f3c50 2497 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2498 {
7b9f3c50 2499 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2500
2501 if (FILENAME_CMP (name, this_name) == 0)
2502 {
f8eba3c6
TT
2503 if (dw2_map_expand_apply (objfile, per_cu,
2504 name, full_path, real_path,
2505 callback, data))
2506 return 1;
9291a0cd
TT
2507 }
2508
2509 if (check_basename && ! base_cu
2510 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2511 base_cu = per_cu;
9291a0cd 2512
c011a4f4
DE
2513 /* Before we invoke realpath, which can get expensive when many
2514 files are involved, do a quick comparison of the basenames. */
2515 if (! basenames_may_differ
2516 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2517 continue;
2518
9291a0cd
TT
2519 if (full_path != NULL)
2520 {
7b9f3c50
DE
2521 const char *this_real_name = dw2_get_real_path (objfile,
2522 file_data, j);
9291a0cd 2523
7b9f3c50
DE
2524 if (this_real_name != NULL
2525 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2526 {
f8eba3c6
TT
2527 if (dw2_map_expand_apply (objfile, per_cu,
2528 name, full_path, real_path,
2529 callback, data))
2530 return 1;
9291a0cd
TT
2531 }
2532 }
2533
2534 if (real_path != NULL)
2535 {
7b9f3c50
DE
2536 const char *this_real_name = dw2_get_real_path (objfile,
2537 file_data, j);
9291a0cd 2538
7b9f3c50
DE
2539 if (this_real_name != NULL
2540 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2541 {
f8eba3c6
TT
2542 if (dw2_map_expand_apply (objfile, per_cu,
2543 name, full_path, real_path,
2544 callback, data))
2545 return 1;
9291a0cd
TT
2546 }
2547 }
2548 }
2549 }
2550
2551 if (base_cu)
2552 {
f8eba3c6
TT
2553 if (dw2_map_expand_apply (objfile, base_cu,
2554 name, full_path, real_path,
2555 callback, data))
2556 return 1;
9291a0cd
TT
2557 }
2558
2559 return 0;
2560}
2561
2562static struct symtab *
2563dw2_lookup_symbol (struct objfile *objfile, int block_index,
2564 const char *name, domain_enum domain)
2565{
774b6a14 2566 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2567 instead. */
2568 return NULL;
2569}
2570
2571/* A helper function that expands all symtabs that hold an object
2572 named NAME. */
2fdf6df6 2573
9291a0cd
TT
2574static void
2575dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2576{
2577 dw2_setup (objfile);
2578
ae2de4f8 2579 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2580 if (dwarf2_per_objfile->index_table)
2581 {
2582 offset_type *vec;
2583
2584 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2585 name, &vec))
2586 {
2587 offset_type i, len = MAYBE_SWAP (*vec);
2588 for (i = 0; i < len; ++i)
2589 {
2590 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2591 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2592
e254ef6a 2593 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2594 }
2595 }
2596 }
2597}
2598
774b6a14
TT
2599static void
2600dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2601 enum block_enum block_kind, const char *name,
774b6a14 2602 domain_enum domain)
9291a0cd 2603{
774b6a14 2604 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2605}
2606
2607static void
2608dw2_print_stats (struct objfile *objfile)
2609{
2610 int i, count;
2611
2612 dw2_setup (objfile);
2613 count = 0;
1fd400ff
TT
2614 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2615 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2616 {
e254ef6a 2617 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2618
e254ef6a 2619 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2620 ++count;
2621 }
2622 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2623}
2624
2625static void
2626dw2_dump (struct objfile *objfile)
2627{
2628 /* Nothing worth printing. */
2629}
2630
2631static void
2632dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2633 struct section_offsets *delta)
2634{
2635 /* There's nothing to relocate here. */
2636}
2637
2638static void
2639dw2_expand_symtabs_for_function (struct objfile *objfile,
2640 const char *func_name)
2641{
2642 dw2_do_expand_symtabs_matching (objfile, func_name);
2643}
2644
2645static void
2646dw2_expand_all_symtabs (struct objfile *objfile)
2647{
2648 int i;
2649
2650 dw2_setup (objfile);
1fd400ff
TT
2651
2652 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2653 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2654 {
e254ef6a 2655 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2656
e254ef6a 2657 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2658 }
2659}
2660
2661static void
2662dw2_expand_symtabs_with_filename (struct objfile *objfile,
2663 const char *filename)
2664{
2665 int i;
2666
2667 dw2_setup (objfile);
d4637a04
DE
2668
2669 /* We don't need to consider type units here.
2670 This is only called for examining code, e.g. expand_line_sal.
2671 There can be an order of magnitude (or more) more type units
2672 than comp units, and we avoid them if we can. */
2673
2674 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2675 {
2676 int j;
e254ef6a 2677 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2678 struct quick_file_names *file_data;
9291a0cd 2679
3d7bb9d9 2680 /* We only need to look at symtabs not already expanded. */
e254ef6a 2681 if (per_cu->v.quick->symtab)
9291a0cd
TT
2682 continue;
2683
7b9f3c50
DE
2684 file_data = dw2_get_file_names (objfile, per_cu);
2685 if (file_data == NULL)
9291a0cd
TT
2686 continue;
2687
7b9f3c50 2688 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2689 {
7b9f3c50 2690 const char *this_name = file_data->file_names[j];
1ef75ecc 2691 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2692 {
e254ef6a 2693 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2694 break;
2695 }
2696 }
2697 }
2698}
2699
dd786858 2700static const char *
9291a0cd
TT
2701dw2_find_symbol_file (struct objfile *objfile, const char *name)
2702{
e254ef6a 2703 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2704 offset_type *vec;
7b9f3c50 2705 struct quick_file_names *file_data;
9291a0cd
TT
2706
2707 dw2_setup (objfile);
2708
ae2de4f8 2709 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2710 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2711 {
2712 struct symtab *s;
2713
2714 ALL_OBJFILE_SYMTABS (objfile, s)
2715 if (s->primary)
2716 {
2717 struct blockvector *bv = BLOCKVECTOR (s);
2718 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2719 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2720
2721 if (sym)
2722 return sym->symtab->filename;
2723 }
2724 return NULL;
2725 }
9291a0cd
TT
2726
2727 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2728 name, &vec))
2729 return NULL;
2730
2731 /* Note that this just looks at the very first one named NAME -- but
2732 actually we are looking for a function. find_main_filename
2733 should be rewritten so that it doesn't require a custom hook. It
2734 could just use the ordinary symbol tables. */
2735 /* vec[0] is the length, which must always be >0. */
e254ef6a 2736 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2737
7b9f3c50
DE
2738 file_data = dw2_get_file_names (objfile, per_cu);
2739 if (file_data == NULL)
9291a0cd
TT
2740 return NULL;
2741
7b9f3c50 2742 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2743}
2744
2745static void
40658b94
PH
2746dw2_map_matching_symbols (const char * name, domain_enum namespace,
2747 struct objfile *objfile, int global,
2748 int (*callback) (struct block *,
2749 struct symbol *, void *),
2edb89d3
JK
2750 void *data, symbol_compare_ftype *match,
2751 symbol_compare_ftype *ordered_compare)
9291a0cd 2752{
40658b94 2753 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2754 current language is Ada for a non-Ada objfile using GNU index. As Ada
2755 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2756}
2757
2758static void
f8eba3c6
TT
2759dw2_expand_symtabs_matching
2760 (struct objfile *objfile,
2761 int (*file_matcher) (const char *, void *),
2762 int (*name_matcher) (const struct language_defn *, const char *, void *),
2763 enum search_domain kind,
2764 void *data)
9291a0cd
TT
2765{
2766 int i;
2767 offset_type iter;
4b5246aa 2768 struct mapped_index *index;
9291a0cd
TT
2769
2770 dw2_setup (objfile);
ae2de4f8
DE
2771
2772 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2773 if (!dwarf2_per_objfile->index_table)
2774 return;
4b5246aa 2775 index = dwarf2_per_objfile->index_table;
9291a0cd 2776
7b08b9eb
JK
2777 if (file_matcher != NULL)
2778 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2779 + dwarf2_per_objfile->n_type_comp_units); ++i)
2780 {
2781 int j;
2782 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2783 struct quick_file_names *file_data;
2784
2785 per_cu->v.quick->mark = 0;
3d7bb9d9
DE
2786
2787 /* We only need to look at symtabs not already expanded. */
7b08b9eb
JK
2788 if (per_cu->v.quick->symtab)
2789 continue;
2790
2791 file_data = dw2_get_file_names (objfile, per_cu);
2792 if (file_data == NULL)
2793 continue;
2794
2795 for (j = 0; j < file_data->num_file_names; ++j)
2796 {
2797 if (file_matcher (file_data->file_names[j], data))
2798 {
2799 per_cu->v.quick->mark = 1;
2800 break;
2801 }
2802 }
2803 }
9291a0cd 2804
3876f04e 2805 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2806 {
2807 offset_type idx = 2 * iter;
2808 const char *name;
2809 offset_type *vec, vec_len, vec_idx;
2810
3876f04e 2811 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2812 continue;
2813
3876f04e 2814 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2815
f8eba3c6 2816 if (! (*name_matcher) (current_language, name, data))
9291a0cd
TT
2817 continue;
2818
2819 /* The name was matched, now expand corresponding CUs that were
2820 marked. */
4b5246aa 2821 vec = (offset_type *) (index->constant_pool
3876f04e 2822 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2823 vec_len = MAYBE_SWAP (vec[0]);
2824 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2825 {
e254ef6a 2826 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2827
e254ef6a 2828 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2829 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2830 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2831 }
2832 }
2833}
2834
2835static struct symtab *
2836dw2_find_pc_sect_symtab (struct objfile *objfile,
2837 struct minimal_symbol *msymbol,
2838 CORE_ADDR pc,
2839 struct obj_section *section,
2840 int warn_if_readin)
2841{
2842 struct dwarf2_per_cu_data *data;
2843
2844 dw2_setup (objfile);
2845
2846 if (!objfile->psymtabs_addrmap)
2847 return NULL;
2848
2849 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2850 if (!data)
2851 return NULL;
2852
2853 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2854 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2855 paddress (get_objfile_arch (objfile), pc));
2856
2857 return dw2_instantiate_symtab (objfile, data);
2858}
2859
9291a0cd 2860static void
44b13c5a 2861dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2862 void *data, int need_fullname)
9291a0cd
TT
2863{
2864 int i;
2865
2866 dw2_setup (objfile);
ae2de4f8 2867
1fd400ff
TT
2868 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2869 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2870 {
2871 int j;
e254ef6a 2872 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2873 struct quick_file_names *file_data;
9291a0cd 2874
3d7bb9d9 2875 /* We only need to look at symtabs not already expanded. */
e254ef6a 2876 if (per_cu->v.quick->symtab)
9291a0cd
TT
2877 continue;
2878
7b9f3c50
DE
2879 file_data = dw2_get_file_names (objfile, per_cu);
2880 if (file_data == NULL)
9291a0cd
TT
2881 continue;
2882
7b9f3c50 2883 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2884 {
74e2f255
DE
2885 const char *this_real_name;
2886
2887 if (need_fullname)
2888 this_real_name = dw2_get_real_path (objfile, file_data, j);
2889 else
2890 this_real_name = NULL;
7b9f3c50 2891 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2892 }
2893 }
2894}
2895
2896static int
2897dw2_has_symbols (struct objfile *objfile)
2898{
2899 return 1;
2900}
2901
2902const struct quick_symbol_functions dwarf2_gdb_index_functions =
2903{
2904 dw2_has_symbols,
2905 dw2_find_last_source_symtab,
2906 dw2_forget_cached_source_info,
f8eba3c6 2907 dw2_map_symtabs_matching_filename,
9291a0cd 2908 dw2_lookup_symbol,
774b6a14 2909 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2910 dw2_print_stats,
2911 dw2_dump,
2912 dw2_relocate,
2913 dw2_expand_symtabs_for_function,
2914 dw2_expand_all_symtabs,
2915 dw2_expand_symtabs_with_filename,
2916 dw2_find_symbol_file,
40658b94 2917 dw2_map_matching_symbols,
9291a0cd
TT
2918 dw2_expand_symtabs_matching,
2919 dw2_find_pc_sect_symtab,
9291a0cd
TT
2920 dw2_map_symbol_filenames
2921};
2922
2923/* Initialize for reading DWARF for this objfile. Return 0 if this
2924 file will use psymtabs, or 1 if using the GNU index. */
2925
2926int
2927dwarf2_initialize_objfile (struct objfile *objfile)
2928{
2929 /* If we're about to read full symbols, don't bother with the
2930 indices. In this case we also don't care if some other debug
2931 format is making psymtabs, because they are all about to be
2932 expanded anyway. */
2933 if ((objfile->flags & OBJF_READNOW))
2934 {
2935 int i;
2936
2937 dwarf2_per_objfile->using_index = 1;
2938 create_all_comp_units (objfile);
1fd400ff 2939 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2940 dwarf2_per_objfile->quick_file_names_table =
2941 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2942
1fd400ff
TT
2943 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2944 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2945 {
e254ef6a 2946 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2947
e254ef6a
DE
2948 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2949 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2950 }
2951
2952 /* Return 1 so that gdb sees the "quick" functions. However,
2953 these functions will be no-ops because we will have expanded
2954 all symtabs. */
2955 return 1;
2956 }
2957
2958 if (dwarf2_read_index (objfile))
2959 return 1;
2960
9291a0cd
TT
2961 return 0;
2962}
2963
2964\f
2965
dce234bc
PP
2966/* Build a partial symbol table. */
2967
2968void
f29dff0a 2969dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2970{
f29dff0a 2971 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2972 {
2973 init_psymbol_list (objfile, 1024);
2974 }
2975
d146bf1e 2976 dwarf2_build_psymtabs_hard (objfile);
c906108c 2977}
c906108c 2978
45452591
DE
2979/* Return TRUE if OFFSET is within CU_HEADER. */
2980
2981static inline int
2982offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2983{
2984 unsigned int bottom = cu_header->offset;
2985 unsigned int top = (cu_header->offset
2986 + cu_header->length
2987 + cu_header->initial_length_size);
9a619af0 2988
45452591
DE
2989 return (offset >= bottom && offset < top);
2990}
2991
93311388
DE
2992/* Read in the comp unit header information from the debug_info at info_ptr.
2993 NOTE: This leaves members offset, first_die_offset to be filled in
2994 by the caller. */
107d2387 2995
fe1b8b76 2996static gdb_byte *
107d2387 2997read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2998 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2999{
3000 int signed_addr;
891d2f0b 3001 unsigned int bytes_read;
c764a876
DE
3002
3003 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3004 cu_header->initial_length_size = bytes_read;
3005 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3006 info_ptr += bytes_read;
107d2387
AC
3007 cu_header->version = read_2_bytes (abfd, info_ptr);
3008 info_ptr += 2;
613e1657 3009 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 3010 &bytes_read);
613e1657 3011 info_ptr += bytes_read;
107d2387
AC
3012 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3013 info_ptr += 1;
3014 signed_addr = bfd_get_sign_extend_vma (abfd);
3015 if (signed_addr < 0)
8e65ff28 3016 internal_error (__FILE__, __LINE__,
e2e0b3e5 3017 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3018 cu_header->signed_addr_p = signed_addr;
c764a876 3019
107d2387
AC
3020 return info_ptr;
3021}
3022
adabb602
DE
3023/* Read in a CU header and perform some basic error checking. */
3024
fe1b8b76
JB
3025static gdb_byte *
3026partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 3027 gdb_byte *buffer, unsigned int buffer_size,
b0df02fd 3028 bfd *abfd, int is_debug_types_section)
72bf9492 3029{
fe1b8b76 3030 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 3031
adabb602
DE
3032 header->offset = beg_of_comp_unit - buffer;
3033
72bf9492
DJ
3034 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3035
460c1c54
CC
3036 /* If we're reading a type unit, skip over the signature and
3037 type_offset fields. */
b0df02fd 3038 if (is_debug_types_section)
460c1c54
CC
3039 info_ptr += 8 /*signature*/ + header->offset_size;
3040
adabb602
DE
3041 header->first_die_offset = info_ptr - beg_of_comp_unit;
3042
2dc7f7b3 3043 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 3044 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
3045 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3046 bfd_get_filename (abfd));
72bf9492 3047
9e0ac564
TT
3048 if (header->abbrev_offset
3049 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3050 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
3051 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3052 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 3053 (long) header->abbrev_offset,
93311388 3054 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
3055 bfd_get_filename (abfd));
3056
3057 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 3058 > buffer + buffer_size)
8a3fe4f8
AC
3059 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3060 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 3061 (long) header->length,
93311388 3062 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
3063 bfd_get_filename (abfd));
3064
3065 return info_ptr;
3066}
3067
348e048f
DE
3068/* Read in the types comp unit header information from .debug_types entry at
3069 types_ptr. The result is a pointer to one past the end of the header. */
3070
3071static gdb_byte *
3072read_type_comp_unit_head (struct comp_unit_head *cu_header,
8b70b953 3073 struct dwarf2_section_info *section,
348e048f
DE
3074 ULONGEST *signature,
3075 gdb_byte *types_ptr, bfd *abfd)
3076{
348e048f
DE
3077 gdb_byte *initial_types_ptr = types_ptr;
3078
8b70b953
TT
3079 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3080 cu_header->offset = types_ptr - section->buffer;
348e048f
DE
3081
3082 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3083
3084 *signature = read_8_bytes (abfd, types_ptr);
3085 types_ptr += 8;
3086 types_ptr += cu_header->offset_size;
3087 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3088
3089 return types_ptr;
3090}
3091
aaa75496
JB
3092/* Allocate a new partial symtab for file named NAME and mark this new
3093 partial symtab as being an include of PST. */
3094
3095static void
3096dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3097 struct objfile *objfile)
3098{
3099 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3100
3101 subpst->section_offsets = pst->section_offsets;
3102 subpst->textlow = 0;
3103 subpst->texthigh = 0;
3104
3105 subpst->dependencies = (struct partial_symtab **)
3106 obstack_alloc (&objfile->objfile_obstack,
3107 sizeof (struct partial_symtab *));
3108 subpst->dependencies[0] = pst;
3109 subpst->number_of_dependencies = 1;
3110
3111 subpst->globals_offset = 0;
3112 subpst->n_global_syms = 0;
3113 subpst->statics_offset = 0;
3114 subpst->n_static_syms = 0;
3115 subpst->symtab = NULL;
3116 subpst->read_symtab = pst->read_symtab;
3117 subpst->readin = 0;
3118
3119 /* No private part is necessary for include psymtabs. This property
3120 can be used to differentiate between such include psymtabs and
10b3939b 3121 the regular ones. */
58a9656e 3122 subpst->read_symtab_private = NULL;
aaa75496
JB
3123}
3124
3125/* Read the Line Number Program data and extract the list of files
3126 included by the source file represented by PST. Build an include
d85a05f0 3127 partial symtab for each of these included files. */
aaa75496
JB
3128
3129static void
3130dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3131 struct die_info *die,
aaa75496
JB
3132 struct partial_symtab *pst)
3133{
3134 struct objfile *objfile = cu->objfile;
3135 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3136 struct line_header *lh = NULL;
3137 struct attribute *attr;
aaa75496 3138
d85a05f0
DJ
3139 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3140 if (attr)
3141 {
3142 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3143
d85a05f0
DJ
3144 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3145 }
aaa75496
JB
3146 if (lh == NULL)
3147 return; /* No linetable, so no includes. */
3148
c6da4cef
DE
3149 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3150 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
3151
3152 free_line_header (lh);
3153}
3154
348e048f
DE
3155static hashval_t
3156hash_type_signature (const void *item)
3157{
3158 const struct signatured_type *type_sig = item;
9a619af0 3159
348e048f
DE
3160 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3161 return type_sig->signature;
3162}
3163
3164static int
3165eq_type_signature (const void *item_lhs, const void *item_rhs)
3166{
3167 const struct signatured_type *lhs = item_lhs;
3168 const struct signatured_type *rhs = item_rhs;
9a619af0 3169
348e048f
DE
3170 return lhs->signature == rhs->signature;
3171}
3172
1fd400ff
TT
3173/* Allocate a hash table for signatured types. */
3174
3175static htab_t
673bfd45 3176allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3177{
3178 return htab_create_alloc_ex (41,
3179 hash_type_signature,
3180 eq_type_signature,
3181 NULL,
3182 &objfile->objfile_obstack,
3183 hashtab_obstack_allocate,
3184 dummy_obstack_deallocate);
3185}
3186
3187/* A helper function to add a signatured type CU to a list. */
3188
3189static int
3190add_signatured_type_cu_to_list (void **slot, void *datum)
3191{
3192 struct signatured_type *sigt = *slot;
3193 struct dwarf2_per_cu_data ***datap = datum;
3194
3195 **datap = &sigt->per_cu;
3196 ++*datap;
3197
3198 return 1;
3199}
3200
348e048f
DE
3201/* Create the hash table of all entries in the .debug_types section.
3202 The result is zero if there is an error (e.g. missing .debug_types section),
3203 otherwise non-zero. */
3204
3205static int
3206create_debug_types_hash_table (struct objfile *objfile)
3207{
8b70b953 3208 htab_t types_htab = NULL;
1fd400ff 3209 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3210 int ix;
3211 struct dwarf2_section_info *section;
348e048f 3212
8b70b953 3213 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3214 {
3215 dwarf2_per_objfile->signatured_types = NULL;
3216 return 0;
3217 }
3218
8b70b953
TT
3219 for (ix = 0;
3220 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3221 ix, section);
3222 ++ix)
3223 {
3224 gdb_byte *info_ptr, *end_ptr;
348e048f 3225
8b70b953
TT
3226 dwarf2_read_section (objfile, section);
3227 info_ptr = section->buffer;
348e048f 3228
8b70b953
TT
3229 if (info_ptr == NULL)
3230 continue;
348e048f 3231
8b70b953
TT
3232 if (types_htab == NULL)
3233 types_htab = allocate_signatured_type_table (objfile);
348e048f 3234
8b70b953
TT
3235 if (dwarf2_die_debug)
3236 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3237
3238 end_ptr = info_ptr + section->size;
3239 while (info_ptr < end_ptr)
3240 {
3241 unsigned int offset;
3242 unsigned int offset_size;
3243 unsigned int type_offset;
3244 unsigned int length, initial_length_size;
3245 unsigned short version;
3246 ULONGEST signature;
3247 struct signatured_type *type_sig;
3248 void **slot;
3249 gdb_byte *ptr = info_ptr;
348e048f 3250
8b70b953 3251 offset = ptr - section->buffer;
348e048f 3252
8b70b953
TT
3253 /* We need to read the type's signature in order to build the hash
3254 table, but we don't need to read anything else just yet. */
348e048f 3255
8b70b953
TT
3256 /* Sanity check to ensure entire cu is present. */
3257 length = read_initial_length (objfile->obfd, ptr,
3258 &initial_length_size);
3259 if (ptr + length + initial_length_size > end_ptr)
3260 {
3261 complaint (&symfile_complaints,
3262 _("debug type entry runs off end "
3263 "of `.debug_types' section, ignored"));
3264 break;
3265 }
348e048f 3266
8b70b953
TT
3267 offset_size = initial_length_size == 4 ? 4 : 8;
3268 ptr += initial_length_size;
3269 version = bfd_get_16 (objfile->obfd, ptr);
3270 ptr += 2;
3271 ptr += offset_size; /* abbrev offset */
3272 ptr += 1; /* address size */
3273 signature = bfd_get_64 (objfile->obfd, ptr);
3274 ptr += 8;
3275 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
4743b735 3276 ptr += offset_size;
6caca83c
CC
3277
3278 /* Skip dummy type units. */
3279 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3280 {
3281 info_ptr = info_ptr + initial_length_size + length;
3282 continue;
3283 }
8b70b953
TT
3284
3285 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3286 memset (type_sig, 0, sizeof (*type_sig));
3287 type_sig->signature = signature;
3288 type_sig->type_offset = type_offset;
3289 type_sig->per_cu.objfile = objfile;
b0df02fd 3290 type_sig->per_cu.debug_types_section = section;
8b70b953
TT
3291 type_sig->per_cu.offset = offset;
3292
3293 slot = htab_find_slot (types_htab, type_sig, INSERT);
3294 gdb_assert (slot != NULL);
3295 if (*slot != NULL)
3296 {
3297 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3298
8b70b953
TT
3299 complaint (&symfile_complaints,
3300 _("debug type entry at offset 0x%x is duplicate to the "
3301 "entry at offset 0x%x, signature 0x%s"),
3302 offset, dup_sig->per_cu.offset,
3303 phex (signature, sizeof (signature)));
3304 gdb_assert (signature == dup_sig->signature);
3305 }
3306 *slot = type_sig;
348e048f 3307
8b70b953
TT
3308 if (dwarf2_die_debug)
3309 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3310 offset, phex (signature, sizeof (signature)));
348e048f 3311
8b70b953
TT
3312 info_ptr = info_ptr + initial_length_size + length;
3313 }
348e048f
DE
3314 }
3315
3316 dwarf2_per_objfile->signatured_types = types_htab;
3317
1fd400ff
TT
3318 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3319 dwarf2_per_objfile->type_comp_units
3320 = obstack_alloc (&objfile->objfile_obstack,
3321 dwarf2_per_objfile->n_type_comp_units
3322 * sizeof (struct dwarf2_per_cu_data *));
3323 iter = &dwarf2_per_objfile->type_comp_units[0];
3324 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3325 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3326 == dwarf2_per_objfile->n_type_comp_units);
3327
348e048f
DE
3328 return 1;
3329}
3330
3331/* Lookup a signature based type.
3332 Returns NULL if SIG is not present in the table. */
3333
3334static struct signatured_type *
3335lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3336{
3337 struct signatured_type find_entry, *entry;
3338
3339 if (dwarf2_per_objfile->signatured_types == NULL)
3340 {
3341 complaint (&symfile_complaints,
55f1336d 3342 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3343 return 0;
3344 }
3345
3346 find_entry.signature = sig;
3347 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3348 return entry;
3349}
3350
d85a05f0
DJ
3351/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3352
3353static void
3354init_cu_die_reader (struct die_reader_specs *reader,
3355 struct dwarf2_cu *cu)
3356{
3357 reader->abfd = cu->objfile->obfd;
3358 reader->cu = cu;
b0df02fd 3359 if (cu->per_cu->debug_types_section)
be391dca 3360 {
b0df02fd
DE
3361 gdb_assert (cu->per_cu->debug_types_section->readin);
3362 reader->buffer = cu->per_cu->debug_types_section->buffer;
be391dca 3363 }
d85a05f0 3364 else
be391dca
TT
3365 {
3366 gdb_assert (dwarf2_per_objfile->info.readin);
3367 reader->buffer = dwarf2_per_objfile->info.buffer;
3368 }
d85a05f0
DJ
3369}
3370
3371/* Find the base address of the compilation unit for range lists and
3372 location lists. It will normally be specified by DW_AT_low_pc.
3373 In DWARF-3 draft 4, the base address could be overridden by
3374 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3375 compilation units with discontinuous ranges. */
3376
3377static void
3378dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3379{
3380 struct attribute *attr;
3381
3382 cu->base_known = 0;
3383 cu->base_address = 0;
3384
3385 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3386 if (attr)
3387 {
3388 cu->base_address = DW_ADDR (attr);
3389 cu->base_known = 1;
3390 }
3391 else
3392 {
3393 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3394 if (attr)
3395 {
3396 cu->base_address = DW_ADDR (attr);
3397 cu->base_known = 1;
3398 }
3399 }
3400}
3401
348e048f
DE
3402/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3403 to combine the common parts.
93311388 3404 Process a compilation unit for a psymtab.
348e048f
DE
3405 BUFFER is a pointer to the beginning of the dwarf section buffer,
3406 either .debug_info or debug_types.
93311388
DE
3407 INFO_PTR is a pointer to the start of the CU.
3408 Returns a pointer to the next CU. */
aaa75496 3409
93311388
DE
3410static gdb_byte *
3411process_psymtab_comp_unit (struct objfile *objfile,
3412 struct dwarf2_per_cu_data *this_cu,
3413 gdb_byte *buffer, gdb_byte *info_ptr,
3414 unsigned int buffer_size)
c906108c 3415{
c906108c 3416 bfd *abfd = objfile->obfd;
93311388 3417 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3418 struct die_info *comp_unit_die;
c906108c 3419 struct partial_symtab *pst;
5734ee8b 3420 CORE_ADDR baseaddr;
93311388
DE
3421 struct cleanup *back_to_inner;
3422 struct dwarf2_cu cu;
d85a05f0
DJ
3423 int has_children, has_pc_info;
3424 struct attribute *attr;
d85a05f0
DJ
3425 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3426 struct die_reader_specs reader_specs;
3e2a0cee 3427 const char *filename;
c906108c 3428
9816fde3 3429 init_one_comp_unit (&cu, objfile);
93311388 3430 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3431
93311388
DE
3432 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3433 buffer, buffer_size,
460c1c54 3434 abfd,
b0df02fd 3435 this_cu->debug_types_section != NULL);
10b3939b 3436
6caca83c
CC
3437 /* Skip dummy compilation units. */
3438 if (info_ptr >= buffer + buffer_size
3439 || peek_abbrev_code (abfd, info_ptr) == 0)
3440 {
3441 info_ptr = (beg_of_comp_unit + cu.header.length
3442 + cu.header.initial_length_size);
3443 do_cleanups (back_to_inner);
3444 return info_ptr;
3445 }
3446
93311388 3447 cu.list_in_scope = &file_symbols;
af703f96 3448
328c9494
DJ
3449 /* If this compilation unit was already read in, free the
3450 cached copy in order to read it in again. This is
3451 necessary because we skipped some symbols when we first
3452 read in the compilation unit (see load_partial_dies).
3453 This problem could be avoided, but the benefit is
3454 unclear. */
3455 if (this_cu->cu != NULL)
3456 free_one_cached_comp_unit (this_cu->cu);
3457
3458 /* Note that this is a pointer to our stack frame, being
3459 added to a global data structure. It will be cleaned up
3460 in free_stack_comp_unit when we finish with this
3461 compilation unit. */
3462 this_cu->cu = &cu;
d85a05f0
DJ
3463 cu.per_cu = this_cu;
3464
93311388
DE
3465 /* Read the abbrevs for this compilation unit into a table. */
3466 dwarf2_read_abbrevs (abfd, &cu);
3467 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3468
93311388 3469 /* Read the compilation unit die. */
d85a05f0
DJ
3470 init_cu_die_reader (&reader_specs, &cu);
3471 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3472 &has_children);
93311388 3473
b0df02fd 3474 if (this_cu->debug_types_section)
348e048f 3475 {
b3c8eb43
JK
3476 /* LENGTH has not been set yet for type units. */
3477 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3478 this_cu->length = cu.header.length + cu.header.initial_length_size;
3479 }
d85a05f0 3480 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3481 {
93311388
DE
3482 info_ptr = (beg_of_comp_unit + cu.header.length
3483 + cu.header.initial_length_size);
3484 do_cleanups (back_to_inner);
3485 return info_ptr;
3486 }
72bf9492 3487
9816fde3 3488 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3489
93311388 3490 /* Allocate a new partial symbol table structure. */
d85a05f0 3491 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3492 if (attr == NULL || !DW_STRING (attr))
3493 filename = "";
3494 else
3495 filename = DW_STRING (attr);
93311388 3496 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3497 filename,
93311388
DE
3498 /* TEXTLOW and TEXTHIGH are set below. */
3499 0,
3500 objfile->global_psymbols.next,
3501 objfile->static_psymbols.next);
9750bca9 3502 pst->psymtabs_addrmap_supported = 1;
72bf9492 3503
d85a05f0
DJ
3504 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3505 if (attr != NULL)
3506 pst->dirname = DW_STRING (attr);
72bf9492 3507
e38df1d0 3508 pst->read_symtab_private = this_cu;
72bf9492 3509
93311388 3510 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3511
0963b4bd 3512 /* Store the function that reads in the rest of the symbol table. */
93311388 3513 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3514
9291a0cd 3515 this_cu->v.psymtab = pst;
c906108c 3516
d85a05f0
DJ
3517 dwarf2_find_base_address (comp_unit_die, &cu);
3518
93311388
DE
3519 /* Possibly set the default values of LOWPC and HIGHPC from
3520 `DW_AT_ranges'. */
d85a05f0
DJ
3521 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3522 &best_highpc, &cu, pst);
3523 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3524 /* Store the contiguous range if it is not empty; it can be empty for
3525 CUs with no code. */
3526 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3527 best_lowpc + baseaddr,
3528 best_highpc + baseaddr - 1, pst);
93311388
DE
3529
3530 /* Check if comp unit has_children.
3531 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3532 If not, there's no more debug_info for this comp unit. */
d85a05f0 3533 if (has_children)
93311388
DE
3534 {
3535 struct partial_die_info *first_die;
3536 CORE_ADDR lowpc, highpc;
31ffec48 3537
93311388
DE
3538 lowpc = ((CORE_ADDR) -1);
3539 highpc = ((CORE_ADDR) 0);
c906108c 3540
93311388 3541 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3542
93311388 3543 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3544 ! has_pc_info, &cu);
57c22c6c 3545
93311388
DE
3546 /* If we didn't find a lowpc, set it to highpc to avoid
3547 complaints from `maint check'. */
3548 if (lowpc == ((CORE_ADDR) -1))
3549 lowpc = highpc;
10b3939b 3550
93311388
DE
3551 /* If the compilation unit didn't have an explicit address range,
3552 then use the information extracted from its child dies. */
d85a05f0 3553 if (! has_pc_info)
93311388 3554 {
d85a05f0
DJ
3555 best_lowpc = lowpc;
3556 best_highpc = highpc;
93311388
DE
3557 }
3558 }
d85a05f0
DJ
3559 pst->textlow = best_lowpc + baseaddr;
3560 pst->texthigh = best_highpc + baseaddr;
c906108c 3561
93311388
DE
3562 pst->n_global_syms = objfile->global_psymbols.next -
3563 (objfile->global_psymbols.list + pst->globals_offset);
3564 pst->n_static_syms = objfile->static_psymbols.next -
3565 (objfile->static_psymbols.list + pst->statics_offset);
3566 sort_pst_symbols (pst);
c906108c 3567
93311388
DE
3568 info_ptr = (beg_of_comp_unit + cu.header.length
3569 + cu.header.initial_length_size);
ae038cb0 3570
b0df02fd 3571 if (this_cu->debug_types_section)
348e048f
DE
3572 {
3573 /* It's not clear we want to do anything with stmt lists here.
3574 Waiting to see what gcc ultimately does. */
3575 }
d85a05f0 3576 else
93311388
DE
3577 {
3578 /* Get the list of files included in the current compilation unit,
3579 and build a psymtab for each of them. */
d85a05f0 3580 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3581 }
ae038cb0 3582
93311388 3583 do_cleanups (back_to_inner);
ae038cb0 3584
93311388
DE
3585 return info_ptr;
3586}
ff013f42 3587
348e048f
DE
3588/* Traversal function for htab_traverse_noresize.
3589 Process one .debug_types comp-unit. */
3590
3591static int
3592process_type_comp_unit (void **slot, void *info)
3593{
3594 struct signatured_type *entry = (struct signatured_type *) *slot;
3595 struct objfile *objfile = (struct objfile *) info;
3596 struct dwarf2_per_cu_data *this_cu;
3597
3598 this_cu = &entry->per_cu;
348e048f 3599
b0df02fd 3600 gdb_assert (this_cu->debug_types_section->readin);
348e048f 3601 process_psymtab_comp_unit (objfile, this_cu,
b0df02fd
DE
3602 this_cu->debug_types_section->buffer,
3603 (this_cu->debug_types_section->buffer
8b70b953 3604 + this_cu->offset),
b0df02fd 3605 this_cu->debug_types_section->size);
348e048f
DE
3606
3607 return 1;
3608}
3609
3610/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3611 Build partial symbol tables for the .debug_types comp-units. */
3612
3613static void
3614build_type_psymtabs (struct objfile *objfile)
3615{
3616 if (! create_debug_types_hash_table (objfile))
3617 return;
3618
3619 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3620 process_type_comp_unit, objfile);
3621}
3622
60606b2c
TT
3623/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3624
3625static void
3626psymtabs_addrmap_cleanup (void *o)
3627{
3628 struct objfile *objfile = o;
ec61707d 3629
60606b2c
TT
3630 objfile->psymtabs_addrmap = NULL;
3631}
3632
93311388
DE
3633/* Build the partial symbol table by doing a quick pass through the
3634 .debug_info and .debug_abbrev sections. */
72bf9492 3635
93311388 3636static void
c67a9c90 3637dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3638{
93311388 3639 gdb_byte *info_ptr;
60606b2c
TT
3640 struct cleanup *back_to, *addrmap_cleanup;
3641 struct obstack temp_obstack;
93311388 3642
98bfdba5
PA
3643 dwarf2_per_objfile->reading_partial_symbols = 1;
3644
be391dca 3645 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3646 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3647
93311388
DE
3648 /* Any cached compilation units will be linked by the per-objfile
3649 read_in_chain. Make sure to free them when we're done. */
3650 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3651
348e048f
DE
3652 build_type_psymtabs (objfile);
3653
93311388 3654 create_all_comp_units (objfile);
c906108c 3655
60606b2c
TT
3656 /* Create a temporary address map on a temporary obstack. We later
3657 copy this to the final obstack. */
3658 obstack_init (&temp_obstack);
3659 make_cleanup_obstack_free (&temp_obstack);
3660 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3661 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3662
93311388
DE
3663 /* Since the objects we're extracting from .debug_info vary in
3664 length, only the individual functions to extract them (like
3665 read_comp_unit_head and load_partial_die) can really know whether
3666 the buffer is large enough to hold another complete object.
c906108c 3667
93311388
DE
3668 At the moment, they don't actually check that. If .debug_info
3669 holds just one extra byte after the last compilation unit's dies,
3670 then read_comp_unit_head will happily read off the end of the
3671 buffer. read_partial_die is similarly casual. Those functions
3672 should be fixed.
c906108c 3673
93311388
DE
3674 For this loop condition, simply checking whether there's any data
3675 left at all should be sufficient. */
c906108c 3676
93311388
DE
3677 while (info_ptr < (dwarf2_per_objfile->info.buffer
3678 + dwarf2_per_objfile->info.size))
3679 {
3680 struct dwarf2_per_cu_data *this_cu;
dd373385 3681
3e43a32a
MS
3682 this_cu = dwarf2_find_comp_unit (info_ptr
3683 - dwarf2_per_objfile->info.buffer,
93311388 3684 objfile);
aaa75496 3685
93311388
DE
3686 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3687 dwarf2_per_objfile->info.buffer,
3688 info_ptr,
3689 dwarf2_per_objfile->info.size);
c906108c 3690 }
ff013f42
JK
3691
3692 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3693 &objfile->objfile_obstack);
60606b2c 3694 discard_cleanups (addrmap_cleanup);
ff013f42 3695
ae038cb0
DJ
3696 do_cleanups (back_to);
3697}
3698
93311388 3699/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3700
3701static void
93311388
DE
3702load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3703 struct objfile *objfile)
ae038cb0
DJ
3704{
3705 bfd *abfd = objfile->obfd;
adabb602 3706 gdb_byte *info_ptr;
d85a05f0 3707 struct die_info *comp_unit_die;
ae038cb0 3708 struct dwarf2_cu *cu;
1d9ec526 3709 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3710 int has_children;
3711 struct die_reader_specs reader_specs;
98bfdba5 3712 int read_cu = 0;
ae038cb0 3713
b0df02fd 3714 gdb_assert (! this_cu->debug_types_section);
348e048f 3715
be391dca 3716 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3717 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0 3718
98bfdba5
PA
3719 if (this_cu->cu == NULL)
3720 {
9816fde3
JK
3721 cu = xmalloc (sizeof (*cu));
3722 init_one_comp_unit (cu, objfile);
ae038cb0 3723
98bfdba5 3724 read_cu = 1;
ae038cb0 3725
98bfdba5
PA
3726 /* If an error occurs while loading, release our storage. */
3727 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3728
98bfdba5
PA
3729 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3730 dwarf2_per_objfile->info.buffer,
3731 dwarf2_per_objfile->info.size,
460c1c54 3732 abfd, 0);
ae038cb0 3733
6caca83c
CC
3734 /* Skip dummy compilation units. */
3735 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3736 + dwarf2_per_objfile->info.size)
3737 || peek_abbrev_code (abfd, info_ptr) == 0)
3738 {
3739 do_cleanups (free_cu_cleanup);
3740 return;
3741 }
3742
98bfdba5
PA
3743 /* Link this compilation unit into the compilation unit tree. */
3744 this_cu->cu = cu;
3745 cu->per_cu = this_cu;
98bfdba5
PA
3746
3747 /* Link this CU into read_in_chain. */
3748 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3749 dwarf2_per_objfile->read_in_chain = this_cu;
3750 }
3751 else
3752 {
3753 cu = this_cu->cu;
3754 info_ptr += cu->header.first_die_offset;
3755 }
ae038cb0
DJ
3756
3757 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3758 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3759 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3760 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3761
3762 /* Read the compilation unit die. */
d85a05f0
DJ
3763 init_cu_die_reader (&reader_specs, cu);
3764 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3765 &has_children);
ae038cb0 3766
9816fde3 3767 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3768
ae038cb0
DJ
3769 /* Check if comp unit has_children.
3770 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3771 If not, there's no more debug_info for this comp unit. */
d85a05f0 3772 if (has_children)
93311388 3773 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3774
98bfdba5
PA
3775 do_cleanups (free_abbrevs_cleanup);
3776
3777 if (read_cu)
3778 {
3779 /* We've successfully allocated this compilation unit. Let our
3780 caller clean it up when finished with it. */
3781 discard_cleanups (free_cu_cleanup);
3782 }
ae038cb0
DJ
3783}
3784
3785/* Create a list of all compilation units in OBJFILE. We do this only
3786 if an inter-comp-unit reference is found; presumably if there is one,
3787 there will be many, and one will occur early in the .debug_info section.
3788 So there's no point in building this list incrementally. */
3789
3790static void
3791create_all_comp_units (struct objfile *objfile)
3792{
3793 int n_allocated;
3794 int n_comp_units;
3795 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3796 gdb_byte *info_ptr;
3797
3798 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3799 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3800
3801 n_comp_units = 0;
3802 n_allocated = 10;
3803 all_comp_units = xmalloc (n_allocated
3804 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3805
3e43a32a
MS
3806 while (info_ptr < dwarf2_per_objfile->info.buffer
3807 + dwarf2_per_objfile->info.size)
ae038cb0 3808 {
c764a876 3809 unsigned int length, initial_length_size;
ae038cb0 3810 struct dwarf2_per_cu_data *this_cu;
c764a876 3811 unsigned int offset;
ae038cb0 3812
dce234bc 3813 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3814
3815 /* Read just enough information to find out where the next
3816 compilation unit is. */
c764a876
DE
3817 length = read_initial_length (objfile->obfd, info_ptr,
3818 &initial_length_size);
ae038cb0
DJ
3819
3820 /* Save the compilation unit for later lookup. */
3821 this_cu = obstack_alloc (&objfile->objfile_obstack,
3822 sizeof (struct dwarf2_per_cu_data));
3823 memset (this_cu, 0, sizeof (*this_cu));
3824 this_cu->offset = offset;
c764a876 3825 this_cu->length = length + initial_length_size;
9291a0cd 3826 this_cu->objfile = objfile;
ae038cb0
DJ
3827
3828 if (n_comp_units == n_allocated)
3829 {
3830 n_allocated *= 2;
3831 all_comp_units = xrealloc (all_comp_units,
3832 n_allocated
3833 * sizeof (struct dwarf2_per_cu_data *));
3834 }
3835 all_comp_units[n_comp_units++] = this_cu;
3836
3837 info_ptr = info_ptr + this_cu->length;
3838 }
3839
3840 dwarf2_per_objfile->all_comp_units
3841 = obstack_alloc (&objfile->objfile_obstack,
3842 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3843 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3844 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3845 xfree (all_comp_units);
3846 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3847}
3848
5734ee8b
DJ
3849/* Process all loaded DIEs for compilation unit CU, starting at
3850 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3851 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3852 DW_AT_ranges). If NEED_PC is set, then this function will set
3853 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3854 and record the covered ranges in the addrmap. */
c906108c 3855
72bf9492
DJ
3856static void
3857scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3858 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3859{
72bf9492 3860 struct partial_die_info *pdi;
c906108c 3861
91c24f0a
DC
3862 /* Now, march along the PDI's, descending into ones which have
3863 interesting children but skipping the children of the other ones,
3864 until we reach the end of the compilation unit. */
c906108c 3865
72bf9492 3866 pdi = first_die;
91c24f0a 3867
72bf9492
DJ
3868 while (pdi != NULL)
3869 {
3870 fixup_partial_die (pdi, cu);
c906108c 3871
f55ee35c 3872 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3873 children, so we need to look at them. Ditto for anonymous
3874 enums. */
933c6fe4 3875
72bf9492 3876 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3877 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3878 {
72bf9492 3879 switch (pdi->tag)
c906108c
SS
3880 {
3881 case DW_TAG_subprogram:
5734ee8b 3882 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3883 break;
72929c62 3884 case DW_TAG_constant:
c906108c
SS
3885 case DW_TAG_variable:
3886 case DW_TAG_typedef:
91c24f0a 3887 case DW_TAG_union_type:
72bf9492 3888 if (!pdi->is_declaration)
63d06c5c 3889 {
72bf9492 3890 add_partial_symbol (pdi, cu);
63d06c5c
DC
3891 }
3892 break;
c906108c 3893 case DW_TAG_class_type:
680b30c7 3894 case DW_TAG_interface_type:
c906108c 3895 case DW_TAG_structure_type:
72bf9492 3896 if (!pdi->is_declaration)
c906108c 3897 {
72bf9492 3898 add_partial_symbol (pdi, cu);
c906108c
SS
3899 }
3900 break;
91c24f0a 3901 case DW_TAG_enumeration_type:
72bf9492
DJ
3902 if (!pdi->is_declaration)
3903 add_partial_enumeration (pdi, cu);
c906108c
SS
3904 break;
3905 case DW_TAG_base_type:
a02abb62 3906 case DW_TAG_subrange_type:
c906108c 3907 /* File scope base type definitions are added to the partial
c5aa993b 3908 symbol table. */
72bf9492 3909 add_partial_symbol (pdi, cu);
c906108c 3910 break;
d9fa45fe 3911 case DW_TAG_namespace:
5734ee8b 3912 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3913 break;
5d7cb8df
JK
3914 case DW_TAG_module:
3915 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3916 break;
c906108c
SS
3917 default:
3918 break;
3919 }
3920 }
3921
72bf9492
DJ
3922 /* If the die has a sibling, skip to the sibling. */
3923
3924 pdi = pdi->die_sibling;
3925 }
3926}
3927
3928/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3929
72bf9492 3930 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3931 name is concatenated with "::" and the partial DIE's name. For
3932 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3933 Enumerators are an exception; they use the scope of their parent
3934 enumeration type, i.e. the name of the enumeration type is not
3935 prepended to the enumerator.
91c24f0a 3936
72bf9492
DJ
3937 There are two complexities. One is DW_AT_specification; in this
3938 case "parent" means the parent of the target of the specification,
3939 instead of the direct parent of the DIE. The other is compilers
3940 which do not emit DW_TAG_namespace; in this case we try to guess
3941 the fully qualified name of structure types from their members'
3942 linkage names. This must be done using the DIE's children rather
3943 than the children of any DW_AT_specification target. We only need
3944 to do this for structures at the top level, i.e. if the target of
3945 any DW_AT_specification (if any; otherwise the DIE itself) does not
3946 have a parent. */
3947
3948/* Compute the scope prefix associated with PDI's parent, in
3949 compilation unit CU. The result will be allocated on CU's
3950 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3951 field. NULL is returned if no prefix is necessary. */
3952static char *
3953partial_die_parent_scope (struct partial_die_info *pdi,
3954 struct dwarf2_cu *cu)
3955{
3956 char *grandparent_scope;
3957 struct partial_die_info *parent, *real_pdi;
91c24f0a 3958
72bf9492
DJ
3959 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3960 then this means the parent of the specification DIE. */
3961
3962 real_pdi = pdi;
72bf9492 3963 while (real_pdi->has_specification)
10b3939b 3964 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3965
3966 parent = real_pdi->die_parent;
3967 if (parent == NULL)
3968 return NULL;
3969
3970 if (parent->scope_set)
3971 return parent->scope;
3972
3973 fixup_partial_die (parent, cu);
3974
10b3939b 3975 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3976
acebe513
UW
3977 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3978 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3979 Work around this problem here. */
3980 if (cu->language == language_cplus
6e70227d 3981 && parent->tag == DW_TAG_namespace
acebe513
UW
3982 && strcmp (parent->name, "::") == 0
3983 && grandparent_scope == NULL)
3984 {
3985 parent->scope = NULL;
3986 parent->scope_set = 1;
3987 return NULL;
3988 }
3989
9c6c53f7
SA
3990 if (pdi->tag == DW_TAG_enumerator)
3991 /* Enumerators should not get the name of the enumeration as a prefix. */
3992 parent->scope = grandparent_scope;
3993 else if (parent->tag == DW_TAG_namespace
f55ee35c 3994 || parent->tag == DW_TAG_module
72bf9492
DJ
3995 || parent->tag == DW_TAG_structure_type
3996 || parent->tag == DW_TAG_class_type
680b30c7 3997 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3998 || parent->tag == DW_TAG_union_type
3999 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
4000 {
4001 if (grandparent_scope == NULL)
4002 parent->scope = parent->name;
4003 else
3e43a32a
MS
4004 parent->scope = typename_concat (&cu->comp_unit_obstack,
4005 grandparent_scope,
f55ee35c 4006 parent->name, 0, cu);
72bf9492 4007 }
72bf9492
DJ
4008 else
4009 {
4010 /* FIXME drow/2004-04-01: What should we be doing with
4011 function-local names? For partial symbols, we should probably be
4012 ignoring them. */
4013 complaint (&symfile_complaints,
e2e0b3e5 4014 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
4015 parent->tag, pdi->offset);
4016 parent->scope = grandparent_scope;
c906108c
SS
4017 }
4018
72bf9492
DJ
4019 parent->scope_set = 1;
4020 return parent->scope;
4021}
4022
4023/* Return the fully scoped name associated with PDI, from compilation unit
4024 CU. The result will be allocated with malloc. */
4025static char *
4026partial_die_full_name (struct partial_die_info *pdi,
4027 struct dwarf2_cu *cu)
4028{
4029 char *parent_scope;
4030
98bfdba5
PA
4031 /* If this is a template instantiation, we can not work out the
4032 template arguments from partial DIEs. So, unfortunately, we have
4033 to go through the full DIEs. At least any work we do building
4034 types here will be reused if full symbols are loaded later. */
4035 if (pdi->has_template_arguments)
4036 {
4037 fixup_partial_die (pdi, cu);
4038
4039 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4040 {
4041 struct die_info *die;
4042 struct attribute attr;
4043 struct dwarf2_cu *ref_cu = cu;
4044
4045 attr.name = 0;
4046 attr.form = DW_FORM_ref_addr;
4047 attr.u.addr = pdi->offset;
4048 die = follow_die_ref (NULL, &attr, &ref_cu);
4049
4050 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4051 }
4052 }
4053
72bf9492
DJ
4054 parent_scope = partial_die_parent_scope (pdi, cu);
4055 if (parent_scope == NULL)
4056 return NULL;
4057 else
f55ee35c 4058 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4059}
4060
4061static void
72bf9492 4062add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4063{
e7c27a73 4064 struct objfile *objfile = cu->objfile;
c906108c 4065 CORE_ADDR addr = 0;
decbce07 4066 char *actual_name = NULL;
5c4e30ca 4067 const struct partial_symbol *psym = NULL;
e142c38c 4068 CORE_ADDR baseaddr;
72bf9492 4069 int built_actual_name = 0;
e142c38c
DJ
4070
4071 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4072
94af9270
KS
4073 actual_name = partial_die_full_name (pdi, cu);
4074 if (actual_name)
4075 built_actual_name = 1;
63d06c5c 4076
72bf9492
DJ
4077 if (actual_name == NULL)
4078 actual_name = pdi->name;
4079
c906108c
SS
4080 switch (pdi->tag)
4081 {
4082 case DW_TAG_subprogram:
2cfa0c8d 4083 if (pdi->is_external || cu->language == language_ada)
c906108c 4084 {
2cfa0c8d
JB
4085 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4086 of the global scope. But in Ada, we want to be able to access
4087 nested procedures globally. So all Ada subprograms are stored
4088 in the global scope. */
f47fb265 4089 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4090 mst_text, objfile); */
f47fb265
MS
4091 add_psymbol_to_list (actual_name, strlen (actual_name),
4092 built_actual_name,
4093 VAR_DOMAIN, LOC_BLOCK,
4094 &objfile->global_psymbols,
4095 0, pdi->lowpc + baseaddr,
4096 cu->language, objfile);
c906108c
SS
4097 }
4098 else
4099 {
f47fb265 4100 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4101 mst_file_text, objfile); */
f47fb265
MS
4102 add_psymbol_to_list (actual_name, strlen (actual_name),
4103 built_actual_name,
4104 VAR_DOMAIN, LOC_BLOCK,
4105 &objfile->static_psymbols,
4106 0, pdi->lowpc + baseaddr,
4107 cu->language, objfile);
c906108c
SS
4108 }
4109 break;
72929c62
JB
4110 case DW_TAG_constant:
4111 {
4112 struct psymbol_allocation_list *list;
4113
4114 if (pdi->is_external)
4115 list = &objfile->global_psymbols;
4116 else
4117 list = &objfile->static_psymbols;
f47fb265
MS
4118 add_psymbol_to_list (actual_name, strlen (actual_name),
4119 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4120 list, 0, 0, cu->language, objfile);
72929c62
JB
4121 }
4122 break;
c906108c 4123 case DW_TAG_variable:
caac4577
JG
4124 if (pdi->locdesc)
4125 addr = decode_locdesc (pdi->locdesc, cu);
4126
4127 if (pdi->locdesc
4128 && addr == 0
4129 && !dwarf2_per_objfile->has_section_at_zero)
4130 {
4131 /* A global or static variable may also have been stripped
4132 out by the linker if unused, in which case its address
4133 will be nullified; do not add such variables into partial
4134 symbol table then. */
4135 }
4136 else if (pdi->is_external)
c906108c
SS
4137 {
4138 /* Global Variable.
4139 Don't enter into the minimal symbol tables as there is
4140 a minimal symbol table entry from the ELF symbols already.
4141 Enter into partial symbol table if it has a location
4142 descriptor or a type.
4143 If the location descriptor is missing, new_symbol will create
4144 a LOC_UNRESOLVED symbol, the address of the variable will then
4145 be determined from the minimal symbol table whenever the variable
4146 is referenced.
4147 The address for the partial symbol table entry is not
4148 used by GDB, but it comes in handy for debugging partial symbol
4149 table building. */
4150
c906108c 4151 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4152 add_psymbol_to_list (actual_name, strlen (actual_name),
4153 built_actual_name,
4154 VAR_DOMAIN, LOC_STATIC,
4155 &objfile->global_psymbols,
4156 0, addr + baseaddr,
4157 cu->language, objfile);
c906108c
SS
4158 }
4159 else
4160 {
0963b4bd 4161 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4162 if (pdi->locdesc == NULL)
decbce07
MS
4163 {
4164 if (built_actual_name)
4165 xfree (actual_name);
4166 return;
4167 }
f47fb265 4168 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4169 mst_file_data, objfile); */
f47fb265
MS
4170 add_psymbol_to_list (actual_name, strlen (actual_name),
4171 built_actual_name,
4172 VAR_DOMAIN, LOC_STATIC,
4173 &objfile->static_psymbols,
4174 0, addr + baseaddr,
4175 cu->language, objfile);
c906108c
SS
4176 }
4177 break;
4178 case DW_TAG_typedef:
4179 case DW_TAG_base_type:
a02abb62 4180 case DW_TAG_subrange_type:
38d518c9 4181 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4182 built_actual_name,
176620f1 4183 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4184 &objfile->static_psymbols,
e142c38c 4185 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4186 break;
72bf9492
DJ
4187 case DW_TAG_namespace:
4188 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4189 built_actual_name,
72bf9492
DJ
4190 VAR_DOMAIN, LOC_TYPEDEF,
4191 &objfile->global_psymbols,
4192 0, (CORE_ADDR) 0, cu->language, objfile);
4193 break;
c906108c 4194 case DW_TAG_class_type:
680b30c7 4195 case DW_TAG_interface_type:
c906108c
SS
4196 case DW_TAG_structure_type:
4197 case DW_TAG_union_type:
4198 case DW_TAG_enumeration_type:
fa4028e9
JB
4199 /* Skip external references. The DWARF standard says in the section
4200 about "Structure, Union, and Class Type Entries": "An incomplete
4201 structure, union or class type is represented by a structure,
4202 union or class entry that does not have a byte size attribute
4203 and that has a DW_AT_declaration attribute." */
4204 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4205 {
4206 if (built_actual_name)
4207 xfree (actual_name);
4208 return;
4209 }
fa4028e9 4210
63d06c5c
DC
4211 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4212 static vs. global. */
38d518c9 4213 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4214 built_actual_name,
176620f1 4215 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4216 (cu->language == language_cplus
4217 || cu->language == language_java)
63d06c5c
DC
4218 ? &objfile->global_psymbols
4219 : &objfile->static_psymbols,
e142c38c 4220 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4221
c906108c
SS
4222 break;
4223 case DW_TAG_enumerator:
38d518c9 4224 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4225 built_actual_name,
176620f1 4226 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4227 (cu->language == language_cplus
4228 || cu->language == language_java)
f6fe98ef
DJ
4229 ? &objfile->global_psymbols
4230 : &objfile->static_psymbols,
e142c38c 4231 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4232 break;
4233 default:
4234 break;
4235 }
5c4e30ca 4236
72bf9492
DJ
4237 if (built_actual_name)
4238 xfree (actual_name);
c906108c
SS
4239}
4240
5c4e30ca
DC
4241/* Read a partial die corresponding to a namespace; also, add a symbol
4242 corresponding to that namespace to the symbol table. NAMESPACE is
4243 the name of the enclosing namespace. */
91c24f0a 4244
72bf9492
DJ
4245static void
4246add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4247 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4248 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4249{
72bf9492 4250 /* Add a symbol for the namespace. */
e7c27a73 4251
72bf9492 4252 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4253
4254 /* Now scan partial symbols in that namespace. */
4255
91c24f0a 4256 if (pdi->has_children)
5734ee8b 4257 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4258}
4259
5d7cb8df
JK
4260/* Read a partial die corresponding to a Fortran module. */
4261
4262static void
4263add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4264 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4265{
f55ee35c 4266 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4267
4268 if (pdi->has_children)
4269 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4270}
4271
bc30ff58
JB
4272/* Read a partial die corresponding to a subprogram and create a partial
4273 symbol for that subprogram. When the CU language allows it, this
4274 routine also defines a partial symbol for each nested subprogram
4275 that this subprogram contains.
6e70227d 4276
bc30ff58
JB
4277 DIE my also be a lexical block, in which case we simply search
4278 recursively for suprograms defined inside that lexical block.
4279 Again, this is only performed when the CU language allows this
4280 type of definitions. */
4281
4282static void
4283add_partial_subprogram (struct partial_die_info *pdi,
4284 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4285 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4286{
4287 if (pdi->tag == DW_TAG_subprogram)
4288 {
4289 if (pdi->has_pc_info)
4290 {
4291 if (pdi->lowpc < *lowpc)
4292 *lowpc = pdi->lowpc;
4293 if (pdi->highpc > *highpc)
4294 *highpc = pdi->highpc;
5734ee8b
DJ
4295 if (need_pc)
4296 {
4297 CORE_ADDR baseaddr;
4298 struct objfile *objfile = cu->objfile;
4299
4300 baseaddr = ANOFFSET (objfile->section_offsets,
4301 SECT_OFF_TEXT (objfile));
4302 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4303 pdi->lowpc + baseaddr,
4304 pdi->highpc - 1 + baseaddr,
9291a0cd 4305 cu->per_cu->v.psymtab);
5734ee8b 4306 }
bc30ff58 4307 if (!pdi->is_declaration)
e8d05480
JB
4308 /* Ignore subprogram DIEs that do not have a name, they are
4309 illegal. Do not emit a complaint at this point, we will
4310 do so when we convert this psymtab into a symtab. */
4311 if (pdi->name)
4312 add_partial_symbol (pdi, cu);
bc30ff58
JB
4313 }
4314 }
6e70227d 4315
bc30ff58
JB
4316 if (! pdi->has_children)
4317 return;
4318
4319 if (cu->language == language_ada)
4320 {
4321 pdi = pdi->die_child;
4322 while (pdi != NULL)
4323 {
4324 fixup_partial_die (pdi, cu);
4325 if (pdi->tag == DW_TAG_subprogram
4326 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4327 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4328 pdi = pdi->die_sibling;
4329 }
4330 }
4331}
4332
91c24f0a
DC
4333/* Read a partial die corresponding to an enumeration type. */
4334
72bf9492
DJ
4335static void
4336add_partial_enumeration (struct partial_die_info *enum_pdi,
4337 struct dwarf2_cu *cu)
91c24f0a 4338{
72bf9492 4339 struct partial_die_info *pdi;
91c24f0a
DC
4340
4341 if (enum_pdi->name != NULL)
72bf9492
DJ
4342 add_partial_symbol (enum_pdi, cu);
4343
4344 pdi = enum_pdi->die_child;
4345 while (pdi)
91c24f0a 4346 {
72bf9492 4347 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4348 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4349 else
72bf9492
DJ
4350 add_partial_symbol (pdi, cu);
4351 pdi = pdi->die_sibling;
91c24f0a 4352 }
91c24f0a
DC
4353}
4354
6caca83c
CC
4355/* Return the initial uleb128 in the die at INFO_PTR. */
4356
4357static unsigned int
4358peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4359{
4360 unsigned int bytes_read;
4361
4362 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4363}
4364
4bb7a0a7
DJ
4365/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4366 Return the corresponding abbrev, or NULL if the number is zero (indicating
4367 an empty DIE). In either case *BYTES_READ will be set to the length of
4368 the initial number. */
4369
4370static struct abbrev_info *
fe1b8b76 4371peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4372 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4373{
4374 bfd *abfd = cu->objfile->obfd;
4375 unsigned int abbrev_number;
4376 struct abbrev_info *abbrev;
4377
4378 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4379
4380 if (abbrev_number == 0)
4381 return NULL;
4382
4383 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4384 if (!abbrev)
4385 {
3e43a32a
MS
4386 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4387 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4388 }
4389
4390 return abbrev;
4391}
4392
93311388
DE
4393/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4394 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4395 DIE. Any children of the skipped DIEs will also be skipped. */
4396
fe1b8b76 4397static gdb_byte *
93311388 4398skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4399{
4400 struct abbrev_info *abbrev;
4401 unsigned int bytes_read;
4402
4403 while (1)
4404 {
4405 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4406 if (abbrev == NULL)
4407 return info_ptr + bytes_read;
4408 else
93311388 4409 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4410 }
4411}
4412
93311388
DE
4413/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4414 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4415 abbrev corresponding to that skipped uleb128 should be passed in
4416 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4417 children. */
4418
fe1b8b76 4419static gdb_byte *
93311388
DE
4420skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4421 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4422{
4423 unsigned int bytes_read;
4424 struct attribute attr;
4425 bfd *abfd = cu->objfile->obfd;
4426 unsigned int form, i;
4427
4428 for (i = 0; i < abbrev->num_attrs; i++)
4429 {
4430 /* The only abbrev we care about is DW_AT_sibling. */
4431 if (abbrev->attrs[i].name == DW_AT_sibling)
4432 {
4433 read_attribute (&attr, &abbrev->attrs[i],
4434 abfd, info_ptr, cu);
4435 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4436 complaint (&symfile_complaints,
4437 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4438 else
93311388 4439 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4440 }
4441
4442 /* If it isn't DW_AT_sibling, skip this attribute. */
4443 form = abbrev->attrs[i].form;
4444 skip_attribute:
4445 switch (form)
4446 {
4bb7a0a7 4447 case DW_FORM_ref_addr:
ae411497
TT
4448 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4449 and later it is offset sized. */
4450 if (cu->header.version == 2)
4451 info_ptr += cu->header.addr_size;
4452 else
4453 info_ptr += cu->header.offset_size;
4454 break;
4455 case DW_FORM_addr:
4bb7a0a7
DJ
4456 info_ptr += cu->header.addr_size;
4457 break;
4458 case DW_FORM_data1:
4459 case DW_FORM_ref1:
4460 case DW_FORM_flag:
4461 info_ptr += 1;
4462 break;
2dc7f7b3
TT
4463 case DW_FORM_flag_present:
4464 break;
4bb7a0a7
DJ
4465 case DW_FORM_data2:
4466 case DW_FORM_ref2:
4467 info_ptr += 2;
4468 break;
4469 case DW_FORM_data4:
4470 case DW_FORM_ref4:
4471 info_ptr += 4;
4472 break;
4473 case DW_FORM_data8:
4474 case DW_FORM_ref8:
55f1336d 4475 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4476 info_ptr += 8;
4477 break;
4478 case DW_FORM_string:
9b1c24c8 4479 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4480 info_ptr += bytes_read;
4481 break;
2dc7f7b3 4482 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4483 case DW_FORM_strp:
4484 info_ptr += cu->header.offset_size;
4485 break;
2dc7f7b3 4486 case DW_FORM_exprloc:
4bb7a0a7
DJ
4487 case DW_FORM_block:
4488 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4489 info_ptr += bytes_read;
4490 break;
4491 case DW_FORM_block1:
4492 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4493 break;
4494 case DW_FORM_block2:
4495 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4496 break;
4497 case DW_FORM_block4:
4498 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4499 break;
4500 case DW_FORM_sdata:
4501 case DW_FORM_udata:
4502 case DW_FORM_ref_udata:
4503 info_ptr = skip_leb128 (abfd, info_ptr);
4504 break;
4505 case DW_FORM_indirect:
4506 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4507 info_ptr += bytes_read;
4508 /* We need to continue parsing from here, so just go back to
4509 the top. */
4510 goto skip_attribute;
4511
4512 default:
3e43a32a
MS
4513 error (_("Dwarf Error: Cannot handle %s "
4514 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4515 dwarf_form_name (form),
4516 bfd_get_filename (abfd));
4517 }
4518 }
4519
4520 if (abbrev->has_children)
93311388 4521 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4522 else
4523 return info_ptr;
4524}
4525
93311388
DE
4526/* Locate ORIG_PDI's sibling.
4527 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4528 in BUFFER. */
91c24f0a 4529
fe1b8b76 4530static gdb_byte *
93311388
DE
4531locate_pdi_sibling (struct partial_die_info *orig_pdi,
4532 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4533 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4534{
4535 /* Do we know the sibling already? */
72bf9492 4536
91c24f0a
DC
4537 if (orig_pdi->sibling)
4538 return orig_pdi->sibling;
4539
4540 /* Are there any children to deal with? */
4541
4542 if (!orig_pdi->has_children)
4543 return info_ptr;
4544
4bb7a0a7 4545 /* Skip the children the long way. */
91c24f0a 4546
93311388 4547 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4548}
4549
c906108c
SS
4550/* Expand this partial symbol table into a full symbol table. */
4551
4552static void
fba45db2 4553dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4554{
c906108c
SS
4555 if (pst != NULL)
4556 {
4557 if (pst->readin)
4558 {
3e43a32a
MS
4559 warning (_("bug: psymtab for %s is already read in."),
4560 pst->filename);
c906108c
SS
4561 }
4562 else
4563 {
4564 if (info_verbose)
4565 {
3e43a32a
MS
4566 printf_filtered (_("Reading in symbols for %s..."),
4567 pst->filename);
c906108c
SS
4568 gdb_flush (gdb_stdout);
4569 }
4570
10b3939b
DJ
4571 /* Restore our global data. */
4572 dwarf2_per_objfile = objfile_data (pst->objfile,
4573 dwarf2_objfile_data_key);
4574
b2ab525c
KB
4575 /* If this psymtab is constructed from a debug-only objfile, the
4576 has_section_at_zero flag will not necessarily be correct. We
4577 can get the correct value for this flag by looking at the data
4578 associated with the (presumably stripped) associated objfile. */
4579 if (pst->objfile->separate_debug_objfile_backlink)
4580 {
4581 struct dwarf2_per_objfile *dpo_backlink
4582 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4583 dwarf2_objfile_data_key);
9a619af0 4584
b2ab525c
KB
4585 dwarf2_per_objfile->has_section_at_zero
4586 = dpo_backlink->has_section_at_zero;
4587 }
4588
98bfdba5
PA
4589 dwarf2_per_objfile->reading_partial_symbols = 0;
4590
c906108c
SS
4591 psymtab_to_symtab_1 (pst);
4592
4593 /* Finish up the debug error message. */
4594 if (info_verbose)
a3f17187 4595 printf_filtered (_("done.\n"));
c906108c
SS
4596 }
4597 }
4598}
4599
10b3939b
DJ
4600/* Add PER_CU to the queue. */
4601
4602static void
03dd20cc 4603queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4604{
4605 struct dwarf2_queue_item *item;
4606
4607 per_cu->queued = 1;
4608 item = xmalloc (sizeof (*item));
4609 item->per_cu = per_cu;
4610 item->next = NULL;
4611
4612 if (dwarf2_queue == NULL)
4613 dwarf2_queue = item;
4614 else
4615 dwarf2_queue_tail->next = item;
4616
4617 dwarf2_queue_tail = item;
4618}
4619
4620/* Process the queue. */
4621
4622static void
4623process_queue (struct objfile *objfile)
4624{
4625 struct dwarf2_queue_item *item, *next_item;
4626
03dd20cc
DJ
4627 /* The queue starts out with one item, but following a DIE reference
4628 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4629 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4630 {
9291a0cd
TT
4631 if (dwarf2_per_objfile->using_index
4632 ? !item->per_cu->v.quick->symtab
4633 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4634 process_full_comp_unit (item->per_cu);
4635
4636 item->per_cu->queued = 0;
4637 next_item = item->next;
4638 xfree (item);
4639 }
4640
4641 dwarf2_queue_tail = NULL;
4642}
4643
4644/* Free all allocated queue entries. This function only releases anything if
4645 an error was thrown; if the queue was processed then it would have been
4646 freed as we went along. */
4647
4648static void
4649dwarf2_release_queue (void *dummy)
4650{
4651 struct dwarf2_queue_item *item, *last;
4652
4653 item = dwarf2_queue;
4654 while (item)
4655 {
4656 /* Anything still marked queued is likely to be in an
4657 inconsistent state, so discard it. */
4658 if (item->per_cu->queued)
4659 {
4660 if (item->per_cu->cu != NULL)
4661 free_one_cached_comp_unit (item->per_cu->cu);
4662 item->per_cu->queued = 0;
4663 }
4664
4665 last = item;
4666 item = item->next;
4667 xfree (last);
4668 }
4669
4670 dwarf2_queue = dwarf2_queue_tail = NULL;
4671}
4672
4673/* Read in full symbols for PST, and anything it depends on. */
4674
c906108c 4675static void
fba45db2 4676psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4677{
10b3939b 4678 struct dwarf2_per_cu_data *per_cu;
c906108c 4679 struct cleanup *back_to;
aaa75496
JB
4680 int i;
4681
4682 for (i = 0; i < pst->number_of_dependencies; i++)
4683 if (!pst->dependencies[i]->readin)
4684 {
4685 /* Inform about additional files that need to be read in. */
4686 if (info_verbose)
4687 {
a3f17187 4688 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4689 fputs_filtered (" ", gdb_stdout);
4690 wrap_here ("");
4691 fputs_filtered ("and ", gdb_stdout);
4692 wrap_here ("");
4693 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4694 wrap_here (""); /* Flush output. */
aaa75496
JB
4695 gdb_flush (gdb_stdout);
4696 }
4697 psymtab_to_symtab_1 (pst->dependencies[i]);
4698 }
4699
e38df1d0 4700 per_cu = pst->read_symtab_private;
10b3939b
DJ
4701
4702 if (per_cu == NULL)
aaa75496
JB
4703 {
4704 /* It's an include file, no symbols to read for it.
4705 Everything is in the parent symtab. */
4706 pst->readin = 1;
4707 return;
4708 }
c906108c 4709
9291a0cd 4710 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4711}
4712
93311388 4713/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4714
93311388 4715static void
3e43a32a
MS
4716load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4717 struct objfile *objfile)
10b3939b 4718{
31ffec48 4719 bfd *abfd = objfile->obfd;
10b3939b 4720 struct dwarf2_cu *cu;
c764a876 4721 unsigned int offset;
93311388 4722 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4723 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4724 struct attribute *attr;
98bfdba5 4725 int read_cu = 0;
6502dd73 4726
b0df02fd 4727 gdb_assert (! per_cu->debug_types_section);
348e048f 4728
c906108c 4729 /* Set local variables from the partial symbol table info. */
10b3939b 4730 offset = per_cu->offset;
6502dd73 4731
be391dca 4732 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4733 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4734 beg_of_comp_unit = info_ptr;
63d06c5c 4735
98bfdba5
PA
4736 if (per_cu->cu == NULL)
4737 {
9816fde3
JK
4738 cu = xmalloc (sizeof (*cu));
4739 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4740
4741 read_cu = 1;
c906108c 4742
98bfdba5
PA
4743 /* If an error occurs while loading, release our storage. */
4744 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4745
98bfdba5
PA
4746 /* Read in the comp_unit header. */
4747 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4748
6caca83c
CC
4749 /* Skip dummy compilation units. */
4750 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4751 + dwarf2_per_objfile->info.size)
4752 || peek_abbrev_code (abfd, info_ptr) == 0)
4753 {
4754 do_cleanups (free_cu_cleanup);
4755 return;
4756 }
4757
98bfdba5
PA
4758 /* Complete the cu_header. */
4759 cu->header.offset = offset;
4760 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4761
98bfdba5
PA
4762 /* Read the abbrevs for this compilation unit. */
4763 dwarf2_read_abbrevs (abfd, cu);
4764 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4765
98bfdba5
PA
4766 /* Link this compilation unit into the compilation unit tree. */
4767 per_cu->cu = cu;
4768 cu->per_cu = per_cu;
98bfdba5
PA
4769
4770 /* Link this CU into read_in_chain. */
4771 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4772 dwarf2_per_objfile->read_in_chain = per_cu;
4773 }
4774 else
4775 {
4776 cu = per_cu->cu;
4777 info_ptr += cu->header.first_die_offset;
4778 }
e142c38c 4779
93311388 4780 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4781
4782 /* We try not to read any attributes in this function, because not
4783 all objfiles needed for references have been loaded yet, and symbol
4784 table processing isn't initialized. But we have to set the CU language,
4785 or we won't be able to build types correctly. */
9816fde3 4786 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4787
a6c727b2
DJ
4788 /* Similarly, if we do not read the producer, we can not apply
4789 producer-specific interpretation. */
4790 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4791 if (attr)
4792 cu->producer = DW_STRING (attr);
4793
98bfdba5
PA
4794 if (read_cu)
4795 {
4796 do_cleanups (free_abbrevs_cleanup);
e142c38c 4797
98bfdba5
PA
4798 /* We've successfully allocated this compilation unit. Let our
4799 caller clean it up when finished with it. */
4800 discard_cleanups (free_cu_cleanup);
4801 }
10b3939b
DJ
4802}
4803
3da10d80
KS
4804/* Add a DIE to the delayed physname list. */
4805
4806static void
4807add_to_method_list (struct type *type, int fnfield_index, int index,
4808 const char *name, struct die_info *die,
4809 struct dwarf2_cu *cu)
4810{
4811 struct delayed_method_info mi;
4812 mi.type = type;
4813 mi.fnfield_index = fnfield_index;
4814 mi.index = index;
4815 mi.name = name;
4816 mi.die = die;
4817 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4818}
4819
4820/* A cleanup for freeing the delayed method list. */
4821
4822static void
4823free_delayed_list (void *ptr)
4824{
4825 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4826 if (cu->method_list != NULL)
4827 {
4828 VEC_free (delayed_method_info, cu->method_list);
4829 cu->method_list = NULL;
4830 }
4831}
4832
4833/* Compute the physnames of any methods on the CU's method list.
4834
4835 The computation of method physnames is delayed in order to avoid the
4836 (bad) condition that one of the method's formal parameters is of an as yet
4837 incomplete type. */
4838
4839static void
4840compute_delayed_physnames (struct dwarf2_cu *cu)
4841{
4842 int i;
4843 struct delayed_method_info *mi;
4844 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4845 {
1d06ead6 4846 const char *physname;
3da10d80
KS
4847 struct fn_fieldlist *fn_flp
4848 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4849 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4850 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4851 }
4852}
4853
10b3939b
DJ
4854/* Generate full symbol information for PST and CU, whose DIEs have
4855 already been loaded into memory. */
4856
4857static void
4858process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4859{
10b3939b 4860 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4861 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4862 CORE_ADDR lowpc, highpc;
4863 struct symtab *symtab;
3da10d80 4864 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4865 CORE_ADDR baseaddr;
4866
4867 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4868
10b3939b
DJ
4869 buildsym_init ();
4870 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4871 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4872
4873 cu->list_in_scope = &file_symbols;
c906108c
SS
4874
4875 /* Do line number decoding in read_file_scope () */
10b3939b 4876 process_die (cu->dies, cu);
c906108c 4877
3da10d80
KS
4878 /* Now that we have processed all the DIEs in the CU, all the types
4879 should be complete, and it should now be safe to compute all of the
4880 physnames. */
4881 compute_delayed_physnames (cu);
4882 do_cleanups (delayed_list_cleanup);
4883
fae299cd
DC
4884 /* Some compilers don't define a DW_AT_high_pc attribute for the
4885 compilation unit. If the DW_AT_high_pc is missing, synthesize
4886 it, by scanning the DIE's below the compilation unit. */
10b3939b 4887 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4888
613e1657 4889 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4890
8be455d7 4891 if (symtab != NULL)
c906108c 4892 {
df15bd07 4893 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4894
8be455d7
JK
4895 /* Set symtab language to language from DW_AT_language. If the
4896 compilation is from a C file generated by language preprocessors, do
4897 not set the language if it was already deduced by start_subfile. */
4898 if (!(cu->language == language_c && symtab->language != language_c))
4899 symtab->language = cu->language;
4900
4901 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4902 produce DW_AT_location with location lists but it can be possibly
4903 invalid without -fvar-tracking.
4904
4905 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4906 needed, it would be wrong due to missing DW_AT_producer there.
4907
4908 Still one can confuse GDB by using non-standard GCC compilation
4909 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4910 */
4632c0d0 4911 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4912 symtab->locations_valid = 1;
e0d00bc7
JK
4913
4914 if (gcc_4_minor >= 5)
4915 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4916
4917 symtab->call_site_htab = cu->call_site_htab;
c906108c 4918 }
9291a0cd
TT
4919
4920 if (dwarf2_per_objfile->using_index)
4921 per_cu->v.quick->symtab = symtab;
4922 else
4923 {
4924 struct partial_symtab *pst = per_cu->v.psymtab;
4925 pst->symtab = symtab;
4926 pst->readin = 1;
4927 }
c906108c
SS
4928
4929 do_cleanups (back_to);
4930}
4931
4932/* Process a die and its children. */
4933
4934static void
e7c27a73 4935process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4936{
4937 switch (die->tag)
4938 {
4939 case DW_TAG_padding:
4940 break;
4941 case DW_TAG_compile_unit:
e7c27a73 4942 read_file_scope (die, cu);
c906108c 4943 break;
348e048f
DE
4944 case DW_TAG_type_unit:
4945 read_type_unit_scope (die, cu);
4946 break;
c906108c 4947 case DW_TAG_subprogram:
c906108c 4948 case DW_TAG_inlined_subroutine:
edb3359d 4949 read_func_scope (die, cu);
c906108c
SS
4950 break;
4951 case DW_TAG_lexical_block:
14898363
L
4952 case DW_TAG_try_block:
4953 case DW_TAG_catch_block:
e7c27a73 4954 read_lexical_block_scope (die, cu);
c906108c 4955 break;
96408a79
SA
4956 case DW_TAG_GNU_call_site:
4957 read_call_site_scope (die, cu);
4958 break;
c906108c 4959 case DW_TAG_class_type:
680b30c7 4960 case DW_TAG_interface_type:
c906108c
SS
4961 case DW_TAG_structure_type:
4962 case DW_TAG_union_type:
134d01f1 4963 process_structure_scope (die, cu);
c906108c
SS
4964 break;
4965 case DW_TAG_enumeration_type:
134d01f1 4966 process_enumeration_scope (die, cu);
c906108c 4967 break;
134d01f1 4968
f792889a
DJ
4969 /* These dies have a type, but processing them does not create
4970 a symbol or recurse to process the children. Therefore we can
4971 read them on-demand through read_type_die. */
c906108c 4972 case DW_TAG_subroutine_type:
72019c9c 4973 case DW_TAG_set_type:
c906108c 4974 case DW_TAG_array_type:
c906108c 4975 case DW_TAG_pointer_type:
c906108c 4976 case DW_TAG_ptr_to_member_type:
c906108c 4977 case DW_TAG_reference_type:
c906108c 4978 case DW_TAG_string_type:
c906108c 4979 break;
134d01f1 4980
c906108c 4981 case DW_TAG_base_type:
a02abb62 4982 case DW_TAG_subrange_type:
cb249c71 4983 case DW_TAG_typedef:
134d01f1
DJ
4984 /* Add a typedef symbol for the type definition, if it has a
4985 DW_AT_name. */
f792889a 4986 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4987 break;
c906108c 4988 case DW_TAG_common_block:
e7c27a73 4989 read_common_block (die, cu);
c906108c
SS
4990 break;
4991 case DW_TAG_common_inclusion:
4992 break;
d9fa45fe 4993 case DW_TAG_namespace:
63d06c5c 4994 processing_has_namespace_info = 1;
e7c27a73 4995 read_namespace (die, cu);
d9fa45fe 4996 break;
5d7cb8df 4997 case DW_TAG_module:
f55ee35c 4998 processing_has_namespace_info = 1;
5d7cb8df
JK
4999 read_module (die, cu);
5000 break;
d9fa45fe
DC
5001 case DW_TAG_imported_declaration:
5002 case DW_TAG_imported_module:
63d06c5c 5003 processing_has_namespace_info = 1;
27aa8d6a
SW
5004 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5005 || cu->language != language_fortran))
5006 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5007 dwarf_tag_name (die->tag));
5008 read_import_statement (die, cu);
d9fa45fe 5009 break;
c906108c 5010 default:
e7c27a73 5011 new_symbol (die, NULL, cu);
c906108c
SS
5012 break;
5013 }
5014}
5015
94af9270
KS
5016/* A helper function for dwarf2_compute_name which determines whether DIE
5017 needs to have the name of the scope prepended to the name listed in the
5018 die. */
5019
5020static int
5021die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5022{
1c809c68
TT
5023 struct attribute *attr;
5024
94af9270
KS
5025 switch (die->tag)
5026 {
5027 case DW_TAG_namespace:
5028 case DW_TAG_typedef:
5029 case DW_TAG_class_type:
5030 case DW_TAG_interface_type:
5031 case DW_TAG_structure_type:
5032 case DW_TAG_union_type:
5033 case DW_TAG_enumeration_type:
5034 case DW_TAG_enumerator:
5035 case DW_TAG_subprogram:
5036 case DW_TAG_member:
5037 return 1;
5038
5039 case DW_TAG_variable:
c2b0a229 5040 case DW_TAG_constant:
94af9270
KS
5041 /* We only need to prefix "globally" visible variables. These include
5042 any variable marked with DW_AT_external or any variable that
5043 lives in a namespace. [Variables in anonymous namespaces
5044 require prefixing, but they are not DW_AT_external.] */
5045
5046 if (dwarf2_attr (die, DW_AT_specification, cu))
5047 {
5048 struct dwarf2_cu *spec_cu = cu;
9a619af0 5049
94af9270
KS
5050 return die_needs_namespace (die_specification (die, &spec_cu),
5051 spec_cu);
5052 }
5053
1c809c68 5054 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5055 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5056 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5057 return 0;
5058 /* A variable in a lexical block of some kind does not need a
5059 namespace, even though in C++ such variables may be external
5060 and have a mangled name. */
5061 if (die->parent->tag == DW_TAG_lexical_block
5062 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5063 || die->parent->tag == DW_TAG_catch_block
5064 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5065 return 0;
5066 return 1;
94af9270
KS
5067
5068 default:
5069 return 0;
5070 }
5071}
5072
98bfdba5
PA
5073/* Retrieve the last character from a mem_file. */
5074
5075static void
5076do_ui_file_peek_last (void *object, const char *buffer, long length)
5077{
5078 char *last_char_p = (char *) object;
5079
5080 if (length > 0)
5081 *last_char_p = buffer[length - 1];
5082}
5083
94af9270
KS
5084/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5085 compute the physname for the object, which include a method's
5086 formal parameters (C++/Java) and return type (Java).
5087
af6b7be1
JB
5088 For Ada, return the DIE's linkage name rather than the fully qualified
5089 name. PHYSNAME is ignored..
5090
94af9270
KS
5091 The result is allocated on the objfile_obstack and canonicalized. */
5092
5093static const char *
5094dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5095 int physname)
5096{
5097 if (name == NULL)
5098 name = dwarf2_name (die, cu);
5099
f55ee35c
JK
5100 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5101 compute it by typename_concat inside GDB. */
5102 if (cu->language == language_ada
5103 || (cu->language == language_fortran && physname))
5104 {
5105 /* For Ada unit, we prefer the linkage name over the name, as
5106 the former contains the exported name, which the user expects
5107 to be able to reference. Ideally, we want the user to be able
5108 to reference this entity using either natural or linkage name,
5109 but we haven't started looking at this enhancement yet. */
5110 struct attribute *attr;
5111
5112 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5113 if (attr == NULL)
5114 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5115 if (attr && DW_STRING (attr))
5116 return DW_STRING (attr);
5117 }
5118
94af9270
KS
5119 /* These are the only languages we know how to qualify names in. */
5120 if (name != NULL
f55ee35c
JK
5121 && (cu->language == language_cplus || cu->language == language_java
5122 || cu->language == language_fortran))
94af9270
KS
5123 {
5124 if (die_needs_namespace (die, cu))
5125 {
5126 long length;
5127 char *prefix;
5128 struct ui_file *buf;
5129
5130 prefix = determine_prefix (die, cu);
5131 buf = mem_fileopen ();
5132 if (*prefix != '\0')
5133 {
f55ee35c
JK
5134 char *prefixed_name = typename_concat (NULL, prefix, name,
5135 physname, cu);
9a619af0 5136
94af9270
KS
5137 fputs_unfiltered (prefixed_name, buf);
5138 xfree (prefixed_name);
5139 }
5140 else
62d5b8da 5141 fputs_unfiltered (name, buf);
94af9270 5142
98bfdba5
PA
5143 /* Template parameters may be specified in the DIE's DW_AT_name, or
5144 as children with DW_TAG_template_type_param or
5145 DW_TAG_value_type_param. If the latter, add them to the name
5146 here. If the name already has template parameters, then
5147 skip this step; some versions of GCC emit both, and
5148 it is more efficient to use the pre-computed name.
5149
5150 Something to keep in mind about this process: it is very
5151 unlikely, or in some cases downright impossible, to produce
5152 something that will match the mangled name of a function.
5153 If the definition of the function has the same debug info,
5154 we should be able to match up with it anyway. But fallbacks
5155 using the minimal symbol, for instance to find a method
5156 implemented in a stripped copy of libstdc++, will not work.
5157 If we do not have debug info for the definition, we will have to
5158 match them up some other way.
5159
5160 When we do name matching there is a related problem with function
5161 templates; two instantiated function templates are allowed to
5162 differ only by their return types, which we do not add here. */
5163
5164 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5165 {
5166 struct attribute *attr;
5167 struct die_info *child;
5168 int first = 1;
5169
5170 die->building_fullname = 1;
5171
5172 for (child = die->child; child != NULL; child = child->sibling)
5173 {
5174 struct type *type;
5175 long value;
5176 gdb_byte *bytes;
5177 struct dwarf2_locexpr_baton *baton;
5178 struct value *v;
5179
5180 if (child->tag != DW_TAG_template_type_param
5181 && child->tag != DW_TAG_template_value_param)
5182 continue;
5183
5184 if (first)
5185 {
5186 fputs_unfiltered ("<", buf);
5187 first = 0;
5188 }
5189 else
5190 fputs_unfiltered (", ", buf);
5191
5192 attr = dwarf2_attr (child, DW_AT_type, cu);
5193 if (attr == NULL)
5194 {
5195 complaint (&symfile_complaints,
5196 _("template parameter missing DW_AT_type"));
5197 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5198 continue;
5199 }
5200 type = die_type (child, cu);
5201
5202 if (child->tag == DW_TAG_template_type_param)
5203 {
5204 c_print_type (type, "", buf, -1, 0);
5205 continue;
5206 }
5207
5208 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5209 if (attr == NULL)
5210 {
5211 complaint (&symfile_complaints,
3e43a32a
MS
5212 _("template parameter missing "
5213 "DW_AT_const_value"));
98bfdba5
PA
5214 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5215 continue;
5216 }
5217
5218 dwarf2_const_value_attr (attr, type, name,
5219 &cu->comp_unit_obstack, cu,
5220 &value, &bytes, &baton);
5221
5222 if (TYPE_NOSIGN (type))
5223 /* GDB prints characters as NUMBER 'CHAR'. If that's
5224 changed, this can use value_print instead. */
5225 c_printchar (value, type, buf);
5226 else
5227 {
5228 struct value_print_options opts;
5229
5230 if (baton != NULL)
5231 v = dwarf2_evaluate_loc_desc (type, NULL,
5232 baton->data,
5233 baton->size,
5234 baton->per_cu);
5235 else if (bytes != NULL)
5236 {
5237 v = allocate_value (type);
5238 memcpy (value_contents_writeable (v), bytes,
5239 TYPE_LENGTH (type));
5240 }
5241 else
5242 v = value_from_longest (type, value);
5243
3e43a32a
MS
5244 /* Specify decimal so that we do not depend on
5245 the radix. */
98bfdba5
PA
5246 get_formatted_print_options (&opts, 'd');
5247 opts.raw = 1;
5248 value_print (v, buf, &opts);
5249 release_value (v);
5250 value_free (v);
5251 }
5252 }
5253
5254 die->building_fullname = 0;
5255
5256 if (!first)
5257 {
5258 /* Close the argument list, with a space if necessary
5259 (nested templates). */
5260 char last_char = '\0';
5261 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5262 if (last_char == '>')
5263 fputs_unfiltered (" >", buf);
5264 else
5265 fputs_unfiltered (">", buf);
5266 }
5267 }
5268
94af9270
KS
5269 /* For Java and C++ methods, append formal parameter type
5270 information, if PHYSNAME. */
6e70227d 5271
94af9270
KS
5272 if (physname && die->tag == DW_TAG_subprogram
5273 && (cu->language == language_cplus
5274 || cu->language == language_java))
5275 {
5276 struct type *type = read_type_die (die, cu);
5277
3167638f 5278 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5279
5280 if (cu->language == language_java)
5281 {
5282 /* For java, we must append the return type to method
0963b4bd 5283 names. */
94af9270
KS
5284 if (die->tag == DW_TAG_subprogram)
5285 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5286 0, 0);
5287 }
5288 else if (cu->language == language_cplus)
5289 {
60430eff
DJ
5290 /* Assume that an artificial first parameter is
5291 "this", but do not crash if it is not. RealView
5292 marks unnamed (and thus unused) parameters as
5293 artificial; there is no way to differentiate
5294 the two cases. */
94af9270
KS
5295 if (TYPE_NFIELDS (type) > 0
5296 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5297 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5298 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5299 0))))
94af9270
KS
5300 fputs_unfiltered (" const", buf);
5301 }
5302 }
5303
5304 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5305 &length);
5306 ui_file_delete (buf);
5307
5308 if (cu->language == language_cplus)
5309 {
5310 char *cname
5311 = dwarf2_canonicalize_name (name, cu,
5312 &cu->objfile->objfile_obstack);
9a619af0 5313
94af9270
KS
5314 if (cname != NULL)
5315 name = cname;
5316 }
5317 }
5318 }
5319
5320 return name;
5321}
5322
0114d602
DJ
5323/* Return the fully qualified name of DIE, based on its DW_AT_name.
5324 If scope qualifiers are appropriate they will be added. The result
5325 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5326 not have a name. NAME may either be from a previous call to
5327 dwarf2_name or NULL.
5328
0963b4bd 5329 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5330
5331static const char *
94af9270 5332dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5333{
94af9270
KS
5334 return dwarf2_compute_name (name, die, cu, 0);
5335}
0114d602 5336
94af9270
KS
5337/* Construct a physname for the given DIE in CU. NAME may either be
5338 from a previous call to dwarf2_name or NULL. The result will be
5339 allocated on the objfile_objstack or NULL if the DIE does not have a
5340 name.
0114d602 5341
94af9270 5342 The output string will be canonicalized (if C++/Java). */
0114d602 5343
94af9270
KS
5344static const char *
5345dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5346{
900e11f9
JK
5347 struct attribute *attr;
5348 const char *retval, *mangled = NULL, *canon = NULL;
5349 struct cleanup *back_to;
5350 int need_copy = 1;
5351
5352 /* In this case dwarf2_compute_name is just a shortcut not building anything
5353 on its own. */
5354 if (!die_needs_namespace (die, cu))
5355 return dwarf2_compute_name (name, die, cu, 1);
5356
5357 back_to = make_cleanup (null_cleanup, NULL);
5358
5359 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5360 if (!attr)
5361 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5362
5363 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5364 has computed. */
5365 if (attr && DW_STRING (attr))
5366 {
5367 char *demangled;
5368
5369 mangled = DW_STRING (attr);
5370
5371 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5372 type. It is easier for GDB users to search for such functions as
5373 `name(params)' than `long name(params)'. In such case the minimal
5374 symbol names do not match the full symbol names but for template
5375 functions there is never a need to look up their definition from their
5376 declaration so the only disadvantage remains the minimal symbol
5377 variant `long name(params)' does not have the proper inferior type.
5378 */
5379
5380 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5381 | (cu->language == language_java
5382 ? DMGL_JAVA | DMGL_RET_POSTFIX
5383 : DMGL_RET_DROP)));
5384 if (demangled)
5385 {
5386 make_cleanup (xfree, demangled);
5387 canon = demangled;
5388 }
5389 else
5390 {
5391 canon = mangled;
5392 need_copy = 0;
5393 }
5394 }
5395
5396 if (canon == NULL || check_physname)
5397 {
5398 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5399
5400 if (canon != NULL && strcmp (physname, canon) != 0)
5401 {
5402 /* It may not mean a bug in GDB. The compiler could also
5403 compute DW_AT_linkage_name incorrectly. But in such case
5404 GDB would need to be bug-to-bug compatible. */
5405
5406 complaint (&symfile_complaints,
5407 _("Computed physname <%s> does not match demangled <%s> "
5408 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5409 physname, canon, mangled, die->offset, cu->objfile->name);
5410
5411 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5412 is available here - over computed PHYSNAME. It is safer
5413 against both buggy GDB and buggy compilers. */
5414
5415 retval = canon;
5416 }
5417 else
5418 {
5419 retval = physname;
5420 need_copy = 0;
5421 }
5422 }
5423 else
5424 retval = canon;
5425
5426 if (need_copy)
5427 retval = obsavestring (retval, strlen (retval),
5428 &cu->objfile->objfile_obstack);
5429
5430 do_cleanups (back_to);
5431 return retval;
0114d602
DJ
5432}
5433
27aa8d6a
SW
5434/* Read the import statement specified by the given die and record it. */
5435
5436static void
5437read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5438{
5439 struct attribute *import_attr;
32019081 5440 struct die_info *imported_die, *child_die;
de4affc9 5441 struct dwarf2_cu *imported_cu;
27aa8d6a 5442 const char *imported_name;
794684b6 5443 const char *imported_name_prefix;
13387711
SW
5444 const char *canonical_name;
5445 const char *import_alias;
5446 const char *imported_declaration = NULL;
794684b6 5447 const char *import_prefix;
32019081
JK
5448 VEC (const_char_ptr) *excludes = NULL;
5449 struct cleanup *cleanups;
13387711
SW
5450
5451 char *temp;
27aa8d6a
SW
5452
5453 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5454 if (import_attr == NULL)
5455 {
5456 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5457 dwarf_tag_name (die->tag));
5458 return;
5459 }
5460
de4affc9
CC
5461 imported_cu = cu;
5462 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5463 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5464 if (imported_name == NULL)
5465 {
5466 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5467
5468 The import in the following code:
5469 namespace A
5470 {
5471 typedef int B;
5472 }
5473
5474 int main ()
5475 {
5476 using A::B;
5477 B b;
5478 return b;
5479 }
5480
5481 ...
5482 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5483 <52> DW_AT_decl_file : 1
5484 <53> DW_AT_decl_line : 6
5485 <54> DW_AT_import : <0x75>
5486 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5487 <59> DW_AT_name : B
5488 <5b> DW_AT_decl_file : 1
5489 <5c> DW_AT_decl_line : 2
5490 <5d> DW_AT_type : <0x6e>
5491 ...
5492 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5493 <76> DW_AT_byte_size : 4
5494 <77> DW_AT_encoding : 5 (signed)
5495
5496 imports the wrong die ( 0x75 instead of 0x58 ).
5497 This case will be ignored until the gcc bug is fixed. */
5498 return;
5499 }
5500
82856980
SW
5501 /* Figure out the local name after import. */
5502 import_alias = dwarf2_name (die, cu);
27aa8d6a 5503
794684b6
SW
5504 /* Figure out where the statement is being imported to. */
5505 import_prefix = determine_prefix (die, cu);
5506
5507 /* Figure out what the scope of the imported die is and prepend it
5508 to the name of the imported die. */
de4affc9 5509 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5510
f55ee35c
JK
5511 if (imported_die->tag != DW_TAG_namespace
5512 && imported_die->tag != DW_TAG_module)
794684b6 5513 {
13387711
SW
5514 imported_declaration = imported_name;
5515 canonical_name = imported_name_prefix;
794684b6 5516 }
13387711 5517 else if (strlen (imported_name_prefix) > 0)
794684b6 5518 {
13387711
SW
5519 temp = alloca (strlen (imported_name_prefix)
5520 + 2 + strlen (imported_name) + 1);
5521 strcpy (temp, imported_name_prefix);
5522 strcat (temp, "::");
5523 strcat (temp, imported_name);
5524 canonical_name = temp;
794684b6 5525 }
13387711
SW
5526 else
5527 canonical_name = imported_name;
794684b6 5528
32019081
JK
5529 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5530
5531 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5532 for (child_die = die->child; child_die && child_die->tag;
5533 child_die = sibling_die (child_die))
5534 {
5535 /* DWARF-4: A Fortran use statement with a “rename list” may be
5536 represented by an imported module entry with an import attribute
5537 referring to the module and owned entries corresponding to those
5538 entities that are renamed as part of being imported. */
5539
5540 if (child_die->tag != DW_TAG_imported_declaration)
5541 {
5542 complaint (&symfile_complaints,
5543 _("child DW_TAG_imported_declaration expected "
5544 "- DIE at 0x%x [in module %s]"),
5545 child_die->offset, cu->objfile->name);
5546 continue;
5547 }
5548
5549 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5550 if (import_attr == NULL)
5551 {
5552 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5553 dwarf_tag_name (child_die->tag));
5554 continue;
5555 }
5556
5557 imported_cu = cu;
5558 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5559 &imported_cu);
5560 imported_name = dwarf2_name (imported_die, imported_cu);
5561 if (imported_name == NULL)
5562 {
5563 complaint (&symfile_complaints,
5564 _("child DW_TAG_imported_declaration has unknown "
5565 "imported name - DIE at 0x%x [in module %s]"),
5566 child_die->offset, cu->objfile->name);
5567 continue;
5568 }
5569
5570 VEC_safe_push (const_char_ptr, excludes, imported_name);
5571
5572 process_die (child_die, cu);
5573 }
5574
c0cc3a76
SW
5575 cp_add_using_directive (import_prefix,
5576 canonical_name,
5577 import_alias,
13387711 5578 imported_declaration,
32019081 5579 excludes,
c0cc3a76 5580 &cu->objfile->objfile_obstack);
32019081
JK
5581
5582 do_cleanups (cleanups);
27aa8d6a
SW
5583}
5584
5fb290d7 5585static void
e142c38c 5586initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5587{
e142c38c 5588 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5589}
5590
ae2de4f8
DE
5591/* Cleanup function for read_file_scope. */
5592
cb1df416
DJ
5593static void
5594free_cu_line_header (void *arg)
5595{
5596 struct dwarf2_cu *cu = arg;
5597
5598 free_line_header (cu->line_header);
5599 cu->line_header = NULL;
5600}
5601
9291a0cd
TT
5602static void
5603find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5604 char **name, char **comp_dir)
5605{
5606 struct attribute *attr;
5607
5608 *name = NULL;
5609 *comp_dir = NULL;
5610
5611 /* Find the filename. Do not use dwarf2_name here, since the filename
5612 is not a source language identifier. */
5613 attr = dwarf2_attr (die, DW_AT_name, cu);
5614 if (attr)
5615 {
5616 *name = DW_STRING (attr);
5617 }
5618
5619 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5620 if (attr)
5621 *comp_dir = DW_STRING (attr);
5622 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5623 {
5624 *comp_dir = ldirname (*name);
5625 if (*comp_dir != NULL)
5626 make_cleanup (xfree, *comp_dir);
5627 }
5628 if (*comp_dir != NULL)
5629 {
5630 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5631 directory, get rid of it. */
5632 char *cp = strchr (*comp_dir, ':');
5633
5634 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5635 *comp_dir = cp + 1;
5636 }
5637
5638 if (*name == NULL)
5639 *name = "<unknown>";
5640}
5641
2ab95328
TT
5642/* Handle DW_AT_stmt_list for a compilation unit. */
5643
5644static void
5645handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5646 const char *comp_dir)
5647{
5648 struct attribute *attr;
5649 struct objfile *objfile = cu->objfile;
5650 bfd *abfd = objfile->obfd;
5651
5652 /* Decode line number information if present. We do this before
5653 processing child DIEs, so that the line header table is available
5654 for DW_AT_decl_file. */
5655 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5656 if (attr)
5657 {
5658 unsigned int line_offset = DW_UNSND (attr);
5659 struct line_header *line_header
5660 = dwarf_decode_line_header (line_offset, abfd, cu);
5661
5662 if (line_header)
5663 {
5664 cu->line_header = line_header;
5665 make_cleanup (free_cu_line_header, cu);
5666 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5667 }
5668 }
5669}
5670
ae2de4f8
DE
5671/* Process DW_TAG_compile_unit. */
5672
c906108c 5673static void
e7c27a73 5674read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5675{
e7c27a73 5676 struct objfile *objfile = cu->objfile;
debd256d 5677 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5678 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5679 CORE_ADDR highpc = ((CORE_ADDR) 0);
5680 struct attribute *attr;
e1024ff1 5681 char *name = NULL;
c906108c
SS
5682 char *comp_dir = NULL;
5683 struct die_info *child_die;
5684 bfd *abfd = objfile->obfd;
e142c38c 5685 CORE_ADDR baseaddr;
6e70227d 5686
e142c38c 5687 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5688
fae299cd 5689 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5690
5691 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5692 from finish_block. */
2acceee2 5693 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5694 lowpc = highpc;
5695 lowpc += baseaddr;
5696 highpc += baseaddr;
5697
9291a0cd 5698 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5699
e142c38c 5700 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5701 if (attr)
5702 {
e142c38c 5703 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5704 }
5705
b0f35d58 5706 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5707 if (attr)
b0f35d58 5708 cu->producer = DW_STRING (attr);
303b6f5d 5709
f4b8a18d
KW
5710 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5711 standardised yet. As a workaround for the language detection we fall
5712 back to the DW_AT_producer string. */
5713 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5714 cu->language = language_opencl;
5715
0963b4bd 5716 /* We assume that we're processing GCC output. */
c906108c 5717 processing_gcc_compilation = 2;
c906108c 5718
df8a16a1
DJ
5719 processing_has_namespace_info = 0;
5720
c906108c
SS
5721 start_symtab (name, comp_dir, lowpc);
5722 record_debugformat ("DWARF 2");
303b6f5d 5723 record_producer (cu->producer);
c906108c 5724
e142c38c 5725 initialize_cu_func_list (cu);
c906108c 5726
2ab95328 5727 handle_DW_AT_stmt_list (die, cu, comp_dir);
debd256d 5728
cb1df416
DJ
5729 /* Process all dies in compilation unit. */
5730 if (die->child != NULL)
5731 {
5732 child_die = die->child;
5733 while (child_die && child_die->tag)
5734 {
5735 process_die (child_die, cu);
5736 child_die = sibling_die (child_die);
5737 }
5738 }
5739
2e276125
JB
5740 /* Decode macro information, if present. Dwarf 2 macro information
5741 refers to information in the line number info statement program
5742 header, so we can only read it if we've read the header
5743 successfully. */
cf2c3c16 5744 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5745 if (attr && cu->line_header)
2e276125 5746 {
cf2c3c16
TT
5747 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5748 complaint (&symfile_complaints,
5749 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5750
5751 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5752 comp_dir, abfd, cu,
5753 &dwarf2_per_objfile->macro, 1);
5754 }
5755 else
5756 {
5757 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5758 if (attr && cu->line_header)
5759 {
5760 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5761
cf2c3c16
TT
5762 dwarf_decode_macros (cu->line_header, macro_offset,
5763 comp_dir, abfd, cu,
5764 &dwarf2_per_objfile->macinfo, 0);
5765 }
2e276125 5766 }
debd256d 5767 do_cleanups (back_to);
5fb290d7
DJ
5768}
5769
ae2de4f8
DE
5770/* Process DW_TAG_type_unit.
5771 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5772 actual type being defined by this TU. In this case the first top
5773 level sibling is there to provide context only. */
5774
5775static void
5776read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5777{
5778 struct objfile *objfile = cu->objfile;
5779 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5780 CORE_ADDR lowpc;
5781 struct attribute *attr;
5782 char *name = NULL;
5783 char *comp_dir = NULL;
5784 struct die_info *child_die;
5785 bfd *abfd = objfile->obfd;
348e048f
DE
5786
5787 /* start_symtab needs a low pc, but we don't really have one.
5788 Do what read_file_scope would do in the absence of such info. */
5789 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5790
5791 /* Find the filename. Do not use dwarf2_name here, since the filename
5792 is not a source language identifier. */
5793 attr = dwarf2_attr (die, DW_AT_name, cu);
5794 if (attr)
5795 name = DW_STRING (attr);
5796
5797 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5798 if (attr)
5799 comp_dir = DW_STRING (attr);
5800 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5801 {
5802 comp_dir = ldirname (name);
5803 if (comp_dir != NULL)
5804 make_cleanup (xfree, comp_dir);
5805 }
5806
5807 if (name == NULL)
5808 name = "<unknown>";
5809
5810 attr = dwarf2_attr (die, DW_AT_language, cu);
5811 if (attr)
5812 set_cu_language (DW_UNSND (attr), cu);
5813
5814 /* This isn't technically needed today. It is done for symmetry
5815 with read_file_scope. */
5816 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5817 if (attr)
348e048f
DE
5818 cu->producer = DW_STRING (attr);
5819
0963b4bd 5820 /* We assume that we're processing GCC output. */
348e048f
DE
5821 processing_gcc_compilation = 2;
5822
5823 processing_has_namespace_info = 0;
5824
5825 start_symtab (name, comp_dir, lowpc);
5826 record_debugformat ("DWARF 2");
5827 record_producer (cu->producer);
5828
2ab95328
TT
5829 handle_DW_AT_stmt_list (die, cu, comp_dir);
5830
348e048f
DE
5831 /* Process the dies in the type unit. */
5832 if (die->child == NULL)
5833 {
5834 dump_die_for_error (die);
5835 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5836 bfd_get_filename (abfd));
5837 }
5838
5839 child_die = die->child;
5840
5841 while (child_die && child_die->tag)
5842 {
5843 process_die (child_die, cu);
5844
5845 child_die = sibling_die (child_die);
5846 }
5847
5848 do_cleanups (back_to);
5849}
5850
5fb290d7 5851static void
e142c38c
DJ
5852add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5853 struct dwarf2_cu *cu)
5fb290d7
DJ
5854{
5855 struct function_range *thisfn;
5856
5857 thisfn = (struct function_range *)
7b5a2f43 5858 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5859 thisfn->name = name;
5860 thisfn->lowpc = lowpc;
5861 thisfn->highpc = highpc;
5862 thisfn->seen_line = 0;
5863 thisfn->next = NULL;
5864
e142c38c
DJ
5865 if (cu->last_fn == NULL)
5866 cu->first_fn = thisfn;
5fb290d7 5867 else
e142c38c 5868 cu->last_fn->next = thisfn;
5fb290d7 5869
e142c38c 5870 cu->last_fn = thisfn;
c906108c
SS
5871}
5872
d389af10
JK
5873/* qsort helper for inherit_abstract_dies. */
5874
5875static int
5876unsigned_int_compar (const void *ap, const void *bp)
5877{
5878 unsigned int a = *(unsigned int *) ap;
5879 unsigned int b = *(unsigned int *) bp;
5880
5881 return (a > b) - (b > a);
5882}
5883
5884/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5885 Inherit only the children of the DW_AT_abstract_origin DIE not being
5886 already referenced by DW_AT_abstract_origin from the children of the
5887 current DIE. */
d389af10
JK
5888
5889static void
5890inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5891{
5892 struct die_info *child_die;
5893 unsigned die_children_count;
5894 /* CU offsets which were referenced by children of the current DIE. */
5895 unsigned *offsets;
5896 unsigned *offsets_end, *offsetp;
5897 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5898 struct die_info *origin_die;
5899 /* Iterator of the ORIGIN_DIE children. */
5900 struct die_info *origin_child_die;
5901 struct cleanup *cleanups;
5902 struct attribute *attr;
cd02d79d
PA
5903 struct dwarf2_cu *origin_cu;
5904 struct pending **origin_previous_list_in_scope;
d389af10
JK
5905
5906 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5907 if (!attr)
5908 return;
5909
cd02d79d
PA
5910 /* Note that following die references may follow to a die in a
5911 different cu. */
5912
5913 origin_cu = cu;
5914 origin_die = follow_die_ref (die, attr, &origin_cu);
5915
5916 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5917 symbols in. */
5918 origin_previous_list_in_scope = origin_cu->list_in_scope;
5919 origin_cu->list_in_scope = cu->list_in_scope;
5920
edb3359d
DJ
5921 if (die->tag != origin_die->tag
5922 && !(die->tag == DW_TAG_inlined_subroutine
5923 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5924 complaint (&symfile_complaints,
5925 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5926 die->offset, origin_die->offset);
5927
5928 child_die = die->child;
5929 die_children_count = 0;
5930 while (child_die && child_die->tag)
5931 {
5932 child_die = sibling_die (child_die);
5933 die_children_count++;
5934 }
5935 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5936 cleanups = make_cleanup (xfree, offsets);
5937
5938 offsets_end = offsets;
5939 child_die = die->child;
5940 while (child_die && child_die->tag)
5941 {
c38f313d
DJ
5942 /* For each CHILD_DIE, find the corresponding child of
5943 ORIGIN_DIE. If there is more than one layer of
5944 DW_AT_abstract_origin, follow them all; there shouldn't be,
5945 but GCC versions at least through 4.4 generate this (GCC PR
5946 40573). */
5947 struct die_info *child_origin_die = child_die;
cd02d79d 5948 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5949
c38f313d
DJ
5950 while (1)
5951 {
cd02d79d
PA
5952 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5953 child_origin_cu);
c38f313d
DJ
5954 if (attr == NULL)
5955 break;
cd02d79d
PA
5956 child_origin_die = follow_die_ref (child_origin_die, attr,
5957 &child_origin_cu);
c38f313d
DJ
5958 }
5959
d389af10
JK
5960 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5961 counterpart may exist. */
c38f313d 5962 if (child_origin_die != child_die)
d389af10 5963 {
edb3359d
DJ
5964 if (child_die->tag != child_origin_die->tag
5965 && !(child_die->tag == DW_TAG_inlined_subroutine
5966 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5967 complaint (&symfile_complaints,
5968 _("Child DIE 0x%x and its abstract origin 0x%x have "
5969 "different tags"), child_die->offset,
5970 child_origin_die->offset);
c38f313d
DJ
5971 if (child_origin_die->parent != origin_die)
5972 complaint (&symfile_complaints,
5973 _("Child DIE 0x%x and its abstract origin 0x%x have "
5974 "different parents"), child_die->offset,
5975 child_origin_die->offset);
5976 else
5977 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5978 }
5979 child_die = sibling_die (child_die);
5980 }
5981 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5982 unsigned_int_compar);
5983 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5984 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5985 complaint (&symfile_complaints,
5986 _("Multiple children of DIE 0x%x refer "
5987 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5988 die->offset, *offsetp);
5989
5990 offsetp = offsets;
5991 origin_child_die = origin_die->child;
5992 while (origin_child_die && origin_child_die->tag)
5993 {
5994 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5995 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5996 offsetp++;
5997 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5998 {
5999 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 6000 process_die (origin_child_die, origin_cu);
d389af10
JK
6001 }
6002 origin_child_die = sibling_die (origin_child_die);
6003 }
cd02d79d 6004 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
6005
6006 do_cleanups (cleanups);
6007}
6008
c906108c 6009static void
e7c27a73 6010read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6011{
e7c27a73 6012 struct objfile *objfile = cu->objfile;
52f0bd74 6013 struct context_stack *new;
c906108c
SS
6014 CORE_ADDR lowpc;
6015 CORE_ADDR highpc;
6016 struct die_info *child_die;
edb3359d 6017 struct attribute *attr, *call_line, *call_file;
c906108c 6018 char *name;
e142c38c 6019 CORE_ADDR baseaddr;
801e3a5b 6020 struct block *block;
edb3359d 6021 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
6022 VEC (symbolp) *template_args = NULL;
6023 struct template_symbol *templ_func = NULL;
edb3359d
DJ
6024
6025 if (inlined_func)
6026 {
6027 /* If we do not have call site information, we can't show the
6028 caller of this inlined function. That's too confusing, so
6029 only use the scope for local variables. */
6030 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
6031 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
6032 if (call_line == NULL || call_file == NULL)
6033 {
6034 read_lexical_block_scope (die, cu);
6035 return;
6036 }
6037 }
c906108c 6038
e142c38c
DJ
6039 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6040
94af9270 6041 name = dwarf2_name (die, cu);
c906108c 6042
e8d05480
JB
6043 /* Ignore functions with missing or empty names. These are actually
6044 illegal according to the DWARF standard. */
6045 if (name == NULL)
6046 {
6047 complaint (&symfile_complaints,
6048 _("missing name for subprogram DIE at %d"), die->offset);
6049 return;
6050 }
6051
6052 /* Ignore functions with missing or invalid low and high pc attributes. */
6053 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6054 {
ae4d0c03
PM
6055 attr = dwarf2_attr (die, DW_AT_external, cu);
6056 if (!attr || !DW_UNSND (attr))
6057 complaint (&symfile_complaints,
3e43a32a
MS
6058 _("cannot get low and high bounds "
6059 "for subprogram DIE at %d"),
ae4d0c03 6060 die->offset);
e8d05480
JB
6061 return;
6062 }
c906108c
SS
6063
6064 lowpc += baseaddr;
6065 highpc += baseaddr;
6066
5fb290d7 6067 /* Record the function range for dwarf_decode_lines. */
e142c38c 6068 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 6069
34eaf542
TT
6070 /* If we have any template arguments, then we must allocate a
6071 different sort of symbol. */
6072 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6073 {
6074 if (child_die->tag == DW_TAG_template_type_param
6075 || child_die->tag == DW_TAG_template_value_param)
6076 {
6077 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6078 struct template_symbol);
6079 templ_func->base.is_cplus_template_function = 1;
6080 break;
6081 }
6082 }
6083
c906108c 6084 new = push_context (0, lowpc);
34eaf542
TT
6085 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6086 (struct symbol *) templ_func);
4c2df51b 6087
4cecd739
DJ
6088 /* If there is a location expression for DW_AT_frame_base, record
6089 it. */
e142c38c 6090 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 6091 if (attr)
c034e007
AC
6092 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6093 expression is being recorded directly in the function's symbol
6094 and not in a separate frame-base object. I guess this hack is
6095 to avoid adding some sort of frame-base adjunct/annex to the
6096 function's symbol :-(. The problem with doing this is that it
6097 results in a function symbol with a location expression that
6098 has nothing to do with the location of the function, ouch! The
6099 relationship should be: a function's symbol has-a frame base; a
6100 frame-base has-a location expression. */
e7c27a73 6101 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 6102
e142c38c 6103 cu->list_in_scope = &local_symbols;
c906108c 6104
639d11d3 6105 if (die->child != NULL)
c906108c 6106 {
639d11d3 6107 child_die = die->child;
c906108c
SS
6108 while (child_die && child_die->tag)
6109 {
34eaf542
TT
6110 if (child_die->tag == DW_TAG_template_type_param
6111 || child_die->tag == DW_TAG_template_value_param)
6112 {
6113 struct symbol *arg = new_symbol (child_die, NULL, cu);
6114
f1078f66
DJ
6115 if (arg != NULL)
6116 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6117 }
6118 else
6119 process_die (child_die, cu);
c906108c
SS
6120 child_die = sibling_die (child_die);
6121 }
6122 }
6123
d389af10
JK
6124 inherit_abstract_dies (die, cu);
6125
4a811a97
UW
6126 /* If we have a DW_AT_specification, we might need to import using
6127 directives from the context of the specification DIE. See the
6128 comment in determine_prefix. */
6129 if (cu->language == language_cplus
6130 && dwarf2_attr (die, DW_AT_specification, cu))
6131 {
6132 struct dwarf2_cu *spec_cu = cu;
6133 struct die_info *spec_die = die_specification (die, &spec_cu);
6134
6135 while (spec_die)
6136 {
6137 child_die = spec_die->child;
6138 while (child_die && child_die->tag)
6139 {
6140 if (child_die->tag == DW_TAG_imported_module)
6141 process_die (child_die, spec_cu);
6142 child_die = sibling_die (child_die);
6143 }
6144
6145 /* In some cases, GCC generates specification DIEs that
6146 themselves contain DW_AT_specification attributes. */
6147 spec_die = die_specification (spec_die, &spec_cu);
6148 }
6149 }
6150
c906108c
SS
6151 new = pop_context ();
6152 /* Make a block for the local symbols within. */
801e3a5b
JB
6153 block = finish_block (new->name, &local_symbols, new->old_blocks,
6154 lowpc, highpc, objfile);
6155
df8a16a1 6156 /* For C++, set the block's scope. */
f55ee35c 6157 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6158 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6159 determine_prefix (die, cu),
df8a16a1
DJ
6160 processing_has_namespace_info);
6161
801e3a5b
JB
6162 /* If we have address ranges, record them. */
6163 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6164
34eaf542
TT
6165 /* Attach template arguments to function. */
6166 if (! VEC_empty (symbolp, template_args))
6167 {
6168 gdb_assert (templ_func != NULL);
6169
6170 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6171 templ_func->template_arguments
6172 = obstack_alloc (&objfile->objfile_obstack,
6173 (templ_func->n_template_arguments
6174 * sizeof (struct symbol *)));
6175 memcpy (templ_func->template_arguments,
6176 VEC_address (symbolp, template_args),
6177 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6178 VEC_free (symbolp, template_args);
6179 }
6180
208d8187
JB
6181 /* In C++, we can have functions nested inside functions (e.g., when
6182 a function declares a class that has methods). This means that
6183 when we finish processing a function scope, we may need to go
6184 back to building a containing block's symbol lists. */
6185 local_symbols = new->locals;
6186 param_symbols = new->params;
27aa8d6a 6187 using_directives = new->using_directives;
208d8187 6188
921e78cf
JB
6189 /* If we've finished processing a top-level function, subsequent
6190 symbols go in the file symbol list. */
6191 if (outermost_context_p ())
e142c38c 6192 cu->list_in_scope = &file_symbols;
c906108c
SS
6193}
6194
6195/* Process all the DIES contained within a lexical block scope. Start
6196 a new scope, process the dies, and then close the scope. */
6197
6198static void
e7c27a73 6199read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6200{
e7c27a73 6201 struct objfile *objfile = cu->objfile;
52f0bd74 6202 struct context_stack *new;
c906108c
SS
6203 CORE_ADDR lowpc, highpc;
6204 struct die_info *child_die;
e142c38c
DJ
6205 CORE_ADDR baseaddr;
6206
6207 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6208
6209 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6210 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6211 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6212 be nasty. Might be easier to properly extend generic blocks to
af34e669 6213 describe ranges. */
d85a05f0 6214 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6215 return;
6216 lowpc += baseaddr;
6217 highpc += baseaddr;
6218
6219 push_context (0, lowpc);
639d11d3 6220 if (die->child != NULL)
c906108c 6221 {
639d11d3 6222 child_die = die->child;
c906108c
SS
6223 while (child_die && child_die->tag)
6224 {
e7c27a73 6225 process_die (child_die, cu);
c906108c
SS
6226 child_die = sibling_die (child_die);
6227 }
6228 }
6229 new = pop_context ();
6230
8540c487 6231 if (local_symbols != NULL || using_directives != NULL)
c906108c 6232 {
801e3a5b
JB
6233 struct block *block
6234 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6235 highpc, objfile);
6236
6237 /* Note that recording ranges after traversing children, as we
6238 do here, means that recording a parent's ranges entails
6239 walking across all its children's ranges as they appear in
6240 the address map, which is quadratic behavior.
6241
6242 It would be nicer to record the parent's ranges before
6243 traversing its children, simply overriding whatever you find
6244 there. But since we don't even decide whether to create a
6245 block until after we've traversed its children, that's hard
6246 to do. */
6247 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6248 }
6249 local_symbols = new->locals;
27aa8d6a 6250 using_directives = new->using_directives;
c906108c
SS
6251}
6252
96408a79
SA
6253/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6254
6255static void
6256read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6257{
6258 struct objfile *objfile = cu->objfile;
6259 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6260 CORE_ADDR pc, baseaddr;
6261 struct attribute *attr;
6262 struct call_site *call_site, call_site_local;
6263 void **slot;
6264 int nparams;
6265 struct die_info *child_die;
6266
6267 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6268
6269 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6270 if (!attr)
6271 {
6272 complaint (&symfile_complaints,
6273 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6274 "DIE 0x%x [in module %s]"),
6275 die->offset, cu->objfile->name);
6276 return;
6277 }
6278 pc = DW_ADDR (attr) + baseaddr;
6279
6280 if (cu->call_site_htab == NULL)
6281 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6282 NULL, &objfile->objfile_obstack,
6283 hashtab_obstack_allocate, NULL);
6284 call_site_local.pc = pc;
6285 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6286 if (*slot != NULL)
6287 {
6288 complaint (&symfile_complaints,
6289 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6290 "DIE 0x%x [in module %s]"),
6291 paddress (gdbarch, pc), die->offset, cu->objfile->name);
6292 return;
6293 }
6294
6295 /* Count parameters at the caller. */
6296
6297 nparams = 0;
6298 for (child_die = die->child; child_die && child_die->tag;
6299 child_die = sibling_die (child_die))
6300 {
6301 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6302 {
6303 complaint (&symfile_complaints,
6304 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6305 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6306 child_die->tag, child_die->offset, cu->objfile->name);
6307 continue;
6308 }
6309
6310 nparams++;
6311 }
6312
6313 call_site = obstack_alloc (&objfile->objfile_obstack,
6314 (sizeof (*call_site)
6315 + (sizeof (*call_site->parameter)
6316 * (nparams - 1))));
6317 *slot = call_site;
6318 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6319 call_site->pc = pc;
6320
6321 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6322 {
6323 struct die_info *func_die;
6324
6325 /* Skip also over DW_TAG_inlined_subroutine. */
6326 for (func_die = die->parent;
6327 func_die && func_die->tag != DW_TAG_subprogram
6328 && func_die->tag != DW_TAG_subroutine_type;
6329 func_die = func_die->parent);
6330
6331 /* DW_AT_GNU_all_call_sites is a superset
6332 of DW_AT_GNU_all_tail_call_sites. */
6333 if (func_die
6334 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6335 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6336 {
6337 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6338 not complete. But keep CALL_SITE for look ups via call_site_htab,
6339 both the initial caller containing the real return address PC and
6340 the final callee containing the current PC of a chain of tail
6341 calls do not need to have the tail call list complete. But any
6342 function candidate for a virtual tail call frame searched via
6343 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6344 determined unambiguously. */
6345 }
6346 else
6347 {
6348 struct type *func_type = NULL;
6349
6350 if (func_die)
6351 func_type = get_die_type (func_die, cu);
6352 if (func_type != NULL)
6353 {
6354 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6355
6356 /* Enlist this call site to the function. */
6357 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6358 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6359 }
6360 else
6361 complaint (&symfile_complaints,
6362 _("Cannot find function owning DW_TAG_GNU_call_site "
6363 "DIE 0x%x [in module %s]"),
6364 die->offset, cu->objfile->name);
6365 }
6366 }
6367
6368 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6369 if (attr == NULL)
6370 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6371 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6372 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6373 /* Keep NULL DWARF_BLOCK. */;
6374 else if (attr_form_is_block (attr))
6375 {
6376 struct dwarf2_locexpr_baton *dlbaton;
6377
6378 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6379 dlbaton->data = DW_BLOCK (attr)->data;
6380 dlbaton->size = DW_BLOCK (attr)->size;
6381 dlbaton->per_cu = cu->per_cu;
6382
6383 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6384 }
6385 else if (is_ref_attr (attr))
6386 {
6387 struct objfile *objfile = cu->objfile;
6388 struct dwarf2_cu *target_cu = cu;
6389 struct die_info *target_die;
6390
6391 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6392 gdb_assert (target_cu->objfile == objfile);
6393 if (die_is_declaration (target_die, target_cu))
6394 {
6395 const char *target_physname;
6396
6397 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6398 if (target_physname == NULL)
6399 complaint (&symfile_complaints,
6400 _("DW_AT_GNU_call_site_target target DIE has invalid "
6401 "physname, for referencing DIE 0x%x [in module %s]"),
6402 die->offset, cu->objfile->name);
6403 else
6404 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6405 }
6406 else
6407 {
6408 CORE_ADDR lowpc;
6409
6410 /* DW_AT_entry_pc should be preferred. */
6411 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6412 complaint (&symfile_complaints,
6413 _("DW_AT_GNU_call_site_target target DIE has invalid "
6414 "low pc, for referencing DIE 0x%x [in module %s]"),
6415 die->offset, cu->objfile->name);
6416 else
6417 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6418 }
6419 }
6420 else
6421 complaint (&symfile_complaints,
6422 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6423 "block nor reference, for DIE 0x%x [in module %s]"),
6424 die->offset, cu->objfile->name);
6425
6426 call_site->per_cu = cu->per_cu;
6427
6428 for (child_die = die->child;
6429 child_die && child_die->tag;
6430 child_die = sibling_die (child_die))
6431 {
6432 struct dwarf2_locexpr_baton *dlbaton;
6433 struct call_site_parameter *parameter;
6434
6435 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6436 {
6437 /* Already printed the complaint above. */
6438 continue;
6439 }
6440
6441 gdb_assert (call_site->parameter_count < nparams);
6442 parameter = &call_site->parameter[call_site->parameter_count];
6443
6444 /* DW_AT_location specifies the register number. Value of the data
6445 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6446
6447 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6448 if (!attr || !attr_form_is_block (attr))
6449 {
6450 complaint (&symfile_complaints,
6451 _("No DW_FORM_block* DW_AT_location for "
6452 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6453 child_die->offset, cu->objfile->name);
6454 continue;
6455 }
6456 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6457 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6458 if (parameter->dwarf_reg == -1
6459 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6460 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6461 &parameter->fb_offset))
6462 {
6463 complaint (&symfile_complaints,
6464 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6465 "for DW_FORM_block* DW_AT_location for "
6466 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6467 child_die->offset, cu->objfile->name);
6468 continue;
6469 }
6470
6471 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6472 if (!attr_form_is_block (attr))
6473 {
6474 complaint (&symfile_complaints,
6475 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6476 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6477 child_die->offset, cu->objfile->name);
6478 continue;
6479 }
6480 parameter->value = DW_BLOCK (attr)->data;
6481 parameter->value_size = DW_BLOCK (attr)->size;
6482
6483 /* Parameters are not pre-cleared by memset above. */
6484 parameter->data_value = NULL;
6485 parameter->data_value_size = 0;
6486 call_site->parameter_count++;
6487
6488 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6489 if (attr)
6490 {
6491 if (!attr_form_is_block (attr))
6492 complaint (&symfile_complaints,
6493 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6494 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6495 child_die->offset, cu->objfile->name);
6496 else
6497 {
6498 parameter->data_value = DW_BLOCK (attr)->data;
6499 parameter->data_value_size = DW_BLOCK (attr)->size;
6500 }
6501 }
6502 }
6503}
6504
43039443 6505/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6506 Return 1 if the attributes are present and valid, otherwise, return 0.
6507 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6508
6509static int
6510dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6511 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6512 struct partial_symtab *ranges_pst)
43039443
JK
6513{
6514 struct objfile *objfile = cu->objfile;
6515 struct comp_unit_head *cu_header = &cu->header;
6516 bfd *obfd = objfile->obfd;
6517 unsigned int addr_size = cu_header->addr_size;
6518 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6519 /* Base address selection entry. */
6520 CORE_ADDR base;
6521 int found_base;
6522 unsigned int dummy;
6523 gdb_byte *buffer;
6524 CORE_ADDR marker;
6525 int low_set;
6526 CORE_ADDR low = 0;
6527 CORE_ADDR high = 0;
ff013f42 6528 CORE_ADDR baseaddr;
43039443 6529
d00adf39
DE
6530 found_base = cu->base_known;
6531 base = cu->base_address;
43039443 6532
be391dca 6533 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6534 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6535 {
6536 complaint (&symfile_complaints,
6537 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6538 offset);
6539 return 0;
6540 }
dce234bc 6541 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6542
6543 /* Read in the largest possible address. */
6544 marker = read_address (obfd, buffer, cu, &dummy);
6545 if ((marker & mask) == mask)
6546 {
6547 /* If we found the largest possible address, then
6548 read the base address. */
6549 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6550 buffer += 2 * addr_size;
6551 offset += 2 * addr_size;
6552 found_base = 1;
6553 }
6554
6555 low_set = 0;
6556
e7030f15 6557 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6558
43039443
JK
6559 while (1)
6560 {
6561 CORE_ADDR range_beginning, range_end;
6562
6563 range_beginning = read_address (obfd, buffer, cu, &dummy);
6564 buffer += addr_size;
6565 range_end = read_address (obfd, buffer, cu, &dummy);
6566 buffer += addr_size;
6567 offset += 2 * addr_size;
6568
6569 /* An end of list marker is a pair of zero addresses. */
6570 if (range_beginning == 0 && range_end == 0)
6571 /* Found the end of list entry. */
6572 break;
6573
6574 /* Each base address selection entry is a pair of 2 values.
6575 The first is the largest possible address, the second is
6576 the base address. Check for a base address here. */
6577 if ((range_beginning & mask) == mask)
6578 {
6579 /* If we found the largest possible address, then
6580 read the base address. */
6581 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6582 found_base = 1;
6583 continue;
6584 }
6585
6586 if (!found_base)
6587 {
6588 /* We have no valid base address for the ranges
6589 data. */
6590 complaint (&symfile_complaints,
6591 _("Invalid .debug_ranges data (no base address)"));
6592 return 0;
6593 }
6594
9277c30c
UW
6595 if (range_beginning > range_end)
6596 {
6597 /* Inverted range entries are invalid. */
6598 complaint (&symfile_complaints,
6599 _("Invalid .debug_ranges data (inverted range)"));
6600 return 0;
6601 }
6602
6603 /* Empty range entries have no effect. */
6604 if (range_beginning == range_end)
6605 continue;
6606
43039443
JK
6607 range_beginning += base;
6608 range_end += base;
6609
9277c30c 6610 if (ranges_pst != NULL)
ff013f42 6611 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6612 range_beginning + baseaddr,
6613 range_end - 1 + baseaddr,
ff013f42
JK
6614 ranges_pst);
6615
43039443
JK
6616 /* FIXME: This is recording everything as a low-high
6617 segment of consecutive addresses. We should have a
6618 data structure for discontiguous block ranges
6619 instead. */
6620 if (! low_set)
6621 {
6622 low = range_beginning;
6623 high = range_end;
6624 low_set = 1;
6625 }
6626 else
6627 {
6628 if (range_beginning < low)
6629 low = range_beginning;
6630 if (range_end > high)
6631 high = range_end;
6632 }
6633 }
6634
6635 if (! low_set)
6636 /* If the first entry is an end-of-list marker, the range
6637 describes an empty scope, i.e. no instructions. */
6638 return 0;
6639
6640 if (low_return)
6641 *low_return = low;
6642 if (high_return)
6643 *high_return = high;
6644 return 1;
6645}
6646
af34e669
DJ
6647/* Get low and high pc attributes from a die. Return 1 if the attributes
6648 are present and valid, otherwise, return 0. Return -1 if the range is
6649 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6650static int
af34e669 6651dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6652 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6653 struct partial_symtab *pst)
c906108c
SS
6654{
6655 struct attribute *attr;
af34e669
DJ
6656 CORE_ADDR low = 0;
6657 CORE_ADDR high = 0;
6658 int ret = 0;
c906108c 6659
e142c38c 6660 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6661 if (attr)
af34e669
DJ
6662 {
6663 high = DW_ADDR (attr);
e142c38c 6664 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6665 if (attr)
6666 low = DW_ADDR (attr);
6667 else
6668 /* Found high w/o low attribute. */
6669 return 0;
6670
6671 /* Found consecutive range of addresses. */
6672 ret = 1;
6673 }
c906108c 6674 else
af34e669 6675 {
e142c38c 6676 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6677 if (attr != NULL)
6678 {
af34e669 6679 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6680 .debug_ranges section. */
d85a05f0 6681 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6682 return 0;
43039443 6683 /* Found discontinuous range of addresses. */
af34e669
DJ
6684 ret = -1;
6685 }
6686 }
c906108c 6687
9373cf26
JK
6688 /* read_partial_die has also the strict LOW < HIGH requirement. */
6689 if (high <= low)
c906108c
SS
6690 return 0;
6691
6692 /* When using the GNU linker, .gnu.linkonce. sections are used to
6693 eliminate duplicate copies of functions and vtables and such.
6694 The linker will arbitrarily choose one and discard the others.
6695 The AT_*_pc values for such functions refer to local labels in
6696 these sections. If the section from that file was discarded, the
6697 labels are not in the output, so the relocs get a value of 0.
6698 If this is a discarded function, mark the pc bounds as invalid,
6699 so that GDB will ignore it. */
72dca2f5 6700 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6701 return 0;
6702
6703 *lowpc = low;
96408a79
SA
6704 if (highpc)
6705 *highpc = high;
af34e669 6706 return ret;
c906108c
SS
6707}
6708
b084d499
JB
6709/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6710 its low and high PC addresses. Do nothing if these addresses could not
6711 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6712 and HIGHPC to the high address if greater than HIGHPC. */
6713
6714static void
6715dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6716 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6717 struct dwarf2_cu *cu)
6718{
6719 CORE_ADDR low, high;
6720 struct die_info *child = die->child;
6721
d85a05f0 6722 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6723 {
6724 *lowpc = min (*lowpc, low);
6725 *highpc = max (*highpc, high);
6726 }
6727
6728 /* If the language does not allow nested subprograms (either inside
6729 subprograms or lexical blocks), we're done. */
6730 if (cu->language != language_ada)
6731 return;
6e70227d 6732
b084d499
JB
6733 /* Check all the children of the given DIE. If it contains nested
6734 subprograms, then check their pc bounds. Likewise, we need to
6735 check lexical blocks as well, as they may also contain subprogram
6736 definitions. */
6737 while (child && child->tag)
6738 {
6739 if (child->tag == DW_TAG_subprogram
6740 || child->tag == DW_TAG_lexical_block)
6741 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6742 child = sibling_die (child);
6743 }
6744}
6745
fae299cd
DC
6746/* Get the low and high pc's represented by the scope DIE, and store
6747 them in *LOWPC and *HIGHPC. If the correct values can't be
6748 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6749
6750static void
6751get_scope_pc_bounds (struct die_info *die,
6752 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6753 struct dwarf2_cu *cu)
6754{
6755 CORE_ADDR best_low = (CORE_ADDR) -1;
6756 CORE_ADDR best_high = (CORE_ADDR) 0;
6757 CORE_ADDR current_low, current_high;
6758
d85a05f0 6759 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6760 {
6761 best_low = current_low;
6762 best_high = current_high;
6763 }
6764 else
6765 {
6766 struct die_info *child = die->child;
6767
6768 while (child && child->tag)
6769 {
6770 switch (child->tag) {
6771 case DW_TAG_subprogram:
b084d499 6772 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6773 break;
6774 case DW_TAG_namespace:
f55ee35c 6775 case DW_TAG_module:
fae299cd
DC
6776 /* FIXME: carlton/2004-01-16: Should we do this for
6777 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6778 that current GCC's always emit the DIEs corresponding
6779 to definitions of methods of classes as children of a
6780 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6781 the DIEs giving the declarations, which could be
6782 anywhere). But I don't see any reason why the
6783 standards says that they have to be there. */
6784 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6785
6786 if (current_low != ((CORE_ADDR) -1))
6787 {
6788 best_low = min (best_low, current_low);
6789 best_high = max (best_high, current_high);
6790 }
6791 break;
6792 default:
0963b4bd 6793 /* Ignore. */
fae299cd
DC
6794 break;
6795 }
6796
6797 child = sibling_die (child);
6798 }
6799 }
6800
6801 *lowpc = best_low;
6802 *highpc = best_high;
6803}
6804
801e3a5b
JB
6805/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6806 in DIE. */
6807static void
6808dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6809 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6810{
6811 struct attribute *attr;
6812
6813 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6814 if (attr)
6815 {
6816 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6817
801e3a5b
JB
6818 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6819 if (attr)
6820 {
6821 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6822
801e3a5b
JB
6823 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6824 }
6825 }
6826
6827 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6828 if (attr)
6829 {
6830 bfd *obfd = cu->objfile->obfd;
6831
6832 /* The value of the DW_AT_ranges attribute is the offset of the
6833 address range list in the .debug_ranges section. */
6834 unsigned long offset = DW_UNSND (attr);
dce234bc 6835 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6836
6837 /* For some target architectures, but not others, the
6838 read_address function sign-extends the addresses it returns.
6839 To recognize base address selection entries, we need a
6840 mask. */
6841 unsigned int addr_size = cu->header.addr_size;
6842 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6843
6844 /* The base address, to which the next pair is relative. Note
6845 that this 'base' is a DWARF concept: most entries in a range
6846 list are relative, to reduce the number of relocs against the
6847 debugging information. This is separate from this function's
6848 'baseaddr' argument, which GDB uses to relocate debugging
6849 information from a shared library based on the address at
6850 which the library was loaded. */
d00adf39
DE
6851 CORE_ADDR base = cu->base_address;
6852 int base_known = cu->base_known;
801e3a5b 6853
be391dca 6854 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6855 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6856 {
6857 complaint (&symfile_complaints,
6858 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6859 offset);
6860 return;
6861 }
6862
6863 for (;;)
6864 {
6865 unsigned int bytes_read;
6866 CORE_ADDR start, end;
6867
6868 start = read_address (obfd, buffer, cu, &bytes_read);
6869 buffer += bytes_read;
6870 end = read_address (obfd, buffer, cu, &bytes_read);
6871 buffer += bytes_read;
6872
6873 /* Did we find the end of the range list? */
6874 if (start == 0 && end == 0)
6875 break;
6876
6877 /* Did we find a base address selection entry? */
6878 else if ((start & base_select_mask) == base_select_mask)
6879 {
6880 base = end;
6881 base_known = 1;
6882 }
6883
6884 /* We found an ordinary address range. */
6885 else
6886 {
6887 if (!base_known)
6888 {
6889 complaint (&symfile_complaints,
3e43a32a
MS
6890 _("Invalid .debug_ranges data "
6891 "(no base address)"));
801e3a5b
JB
6892 return;
6893 }
6894
9277c30c
UW
6895 if (start > end)
6896 {
6897 /* Inverted range entries are invalid. */
6898 complaint (&symfile_complaints,
6899 _("Invalid .debug_ranges data "
6900 "(inverted range)"));
6901 return;
6902 }
6903
6904 /* Empty range entries have no effect. */
6905 if (start == end)
6906 continue;
6907
6e70227d
DE
6908 record_block_range (block,
6909 baseaddr + base + start,
801e3a5b
JB
6910 baseaddr + base + end - 1);
6911 }
6912 }
6913 }
6914}
6915
60d5a603
JK
6916/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6917 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6918 during 4.6.0 experimental. */
6919
6920static int
6921producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6922{
6923 const char *cs;
6924 int major, minor, release;
6925
6926 if (cu->producer == NULL)
6927 {
6928 /* For unknown compilers expect their behavior is DWARF version
6929 compliant.
6930
6931 GCC started to support .debug_types sections by -gdwarf-4 since
6932 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6933 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6934 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6935 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6936
6937 return 0;
6938 }
6939
6940 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6941
6942 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6943 {
6944 /* For non-GCC compilers expect their behavior is DWARF version
6945 compliant. */
6946
6947 return 0;
6948 }
6949 cs = &cu->producer[strlen ("GNU ")];
6950 while (*cs && !isdigit (*cs))
6951 cs++;
6952 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6953 {
6954 /* Not recognized as GCC. */
6955
6956 return 0;
6957 }
6958
6959 return major < 4 || (major == 4 && minor < 6);
6960}
6961
6962/* Return the default accessibility type if it is not overriden by
6963 DW_AT_accessibility. */
6964
6965static enum dwarf_access_attribute
6966dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6967{
6968 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6969 {
6970 /* The default DWARF 2 accessibility for members is public, the default
6971 accessibility for inheritance is private. */
6972
6973 if (die->tag != DW_TAG_inheritance)
6974 return DW_ACCESS_public;
6975 else
6976 return DW_ACCESS_private;
6977 }
6978 else
6979 {
6980 /* DWARF 3+ defines the default accessibility a different way. The same
6981 rules apply now for DW_TAG_inheritance as for the members and it only
6982 depends on the container kind. */
6983
6984 if (die->parent->tag == DW_TAG_class_type)
6985 return DW_ACCESS_private;
6986 else
6987 return DW_ACCESS_public;
6988 }
6989}
6990
74ac6d43
TT
6991/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6992 offset. If the attribute was not found return 0, otherwise return
6993 1. If it was found but could not properly be handled, set *OFFSET
6994 to 0. */
6995
6996static int
6997handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6998 LONGEST *offset)
6999{
7000 struct attribute *attr;
7001
7002 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
7003 if (attr != NULL)
7004 {
7005 *offset = 0;
7006
7007 /* Note that we do not check for a section offset first here.
7008 This is because DW_AT_data_member_location is new in DWARF 4,
7009 so if we see it, we can assume that a constant form is really
7010 a constant and not a section offset. */
7011 if (attr_form_is_constant (attr))
7012 *offset = dwarf2_get_attr_constant_value (attr, 0);
7013 else if (attr_form_is_section_offset (attr))
7014 dwarf2_complex_location_expr_complaint ();
7015 else if (attr_form_is_block (attr))
7016 *offset = decode_locdesc (DW_BLOCK (attr), cu);
7017 else
7018 dwarf2_complex_location_expr_complaint ();
7019
7020 return 1;
7021 }
7022
7023 return 0;
7024}
7025
c906108c
SS
7026/* Add an aggregate field to the field list. */
7027
7028static void
107d2387 7029dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 7030 struct dwarf2_cu *cu)
6e70227d 7031{
e7c27a73 7032 struct objfile *objfile = cu->objfile;
5e2b427d 7033 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7034 struct nextfield *new_field;
7035 struct attribute *attr;
7036 struct field *fp;
7037 char *fieldname = "";
7038
7039 /* Allocate a new field list entry and link it in. */
7040 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 7041 make_cleanup (xfree, new_field);
c906108c 7042 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
7043
7044 if (die->tag == DW_TAG_inheritance)
7045 {
7046 new_field->next = fip->baseclasses;
7047 fip->baseclasses = new_field;
7048 }
7049 else
7050 {
7051 new_field->next = fip->fields;
7052 fip->fields = new_field;
7053 }
c906108c
SS
7054 fip->nfields++;
7055
e142c38c 7056 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
7057 if (attr)
7058 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
7059 else
7060 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
7061 if (new_field->accessibility != DW_ACCESS_public)
7062 fip->non_public_fields = 1;
60d5a603 7063
e142c38c 7064 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
7065 if (attr)
7066 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
7067 else
7068 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
7069
7070 fp = &new_field->field;
a9a9bd0f 7071
e142c38c 7072 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 7073 {
74ac6d43
TT
7074 LONGEST offset;
7075
a9a9bd0f 7076 /* Data member other than a C++ static data member. */
6e70227d 7077
c906108c 7078 /* Get type of field. */
e7c27a73 7079 fp->type = die_type (die, cu);
c906108c 7080
d6a843b5 7081 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 7082
c906108c 7083 /* Get bit size of field (zero if none). */
e142c38c 7084 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
7085 if (attr)
7086 {
7087 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7088 }
7089 else
7090 {
7091 FIELD_BITSIZE (*fp) = 0;
7092 }
7093
7094 /* Get bit offset of field. */
74ac6d43
TT
7095 if (handle_data_member_location (die, cu, &offset))
7096 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 7097 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
7098 if (attr)
7099 {
5e2b427d 7100 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
7101 {
7102 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
7103 additional bit offset from the MSB of the containing
7104 anonymous object to the MSB of the field. We don't
7105 have to do anything special since we don't need to
7106 know the size of the anonymous object. */
c906108c
SS
7107 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7108 }
7109 else
7110 {
7111 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
7112 MSB of the anonymous object, subtract off the number of
7113 bits from the MSB of the field to the MSB of the
7114 object, and then subtract off the number of bits of
7115 the field itself. The result is the bit offset of
7116 the LSB of the field. */
c906108c
SS
7117 int anonymous_size;
7118 int bit_offset = DW_UNSND (attr);
7119
e142c38c 7120 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7121 if (attr)
7122 {
7123 /* The size of the anonymous object containing
7124 the bit field is explicit, so use the
7125 indicated size (in bytes). */
7126 anonymous_size = DW_UNSND (attr);
7127 }
7128 else
7129 {
7130 /* The size of the anonymous object containing
7131 the bit field must be inferred from the type
7132 attribute of the data member containing the
7133 bit field. */
7134 anonymous_size = TYPE_LENGTH (fp->type);
7135 }
7136 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7137 - bit_offset - FIELD_BITSIZE (*fp);
7138 }
7139 }
7140
7141 /* Get name of field. */
39cbfefa
DJ
7142 fieldname = dwarf2_name (die, cu);
7143 if (fieldname == NULL)
7144 fieldname = "";
d8151005
DJ
7145
7146 /* The name is already allocated along with this objfile, so we don't
7147 need to duplicate it for the type. */
7148 fp->name = fieldname;
c906108c
SS
7149
7150 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7151 pointer or virtual base class pointer) to private. */
e142c38c 7152 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7153 {
d48cc9dd 7154 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7155 new_field->accessibility = DW_ACCESS_private;
7156 fip->non_public_fields = 1;
7157 }
7158 }
a9a9bd0f 7159 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7160 {
a9a9bd0f
DC
7161 /* C++ static member. */
7162
7163 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7164 is a declaration, but all versions of G++ as of this writing
7165 (so through at least 3.2.1) incorrectly generate
7166 DW_TAG_variable tags. */
6e70227d 7167
ff355380 7168 const char *physname;
c906108c 7169
a9a9bd0f 7170 /* Get name of field. */
39cbfefa
DJ
7171 fieldname = dwarf2_name (die, cu);
7172 if (fieldname == NULL)
c906108c
SS
7173 return;
7174
254e6b9e 7175 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7176 if (attr
7177 /* Only create a symbol if this is an external value.
7178 new_symbol checks this and puts the value in the global symbol
7179 table, which we want. If it is not external, new_symbol
7180 will try to put the value in cu->list_in_scope which is wrong. */
7181 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7182 {
7183 /* A static const member, not much different than an enum as far as
7184 we're concerned, except that we can support more types. */
7185 new_symbol (die, NULL, cu);
7186 }
7187
2df3850c 7188 /* Get physical name. */
ff355380 7189 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7190
d8151005
DJ
7191 /* The name is already allocated along with this objfile, so we don't
7192 need to duplicate it for the type. */
7193 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7194 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7195 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7196 }
7197 else if (die->tag == DW_TAG_inheritance)
7198 {
74ac6d43 7199 LONGEST offset;
d4b96c9a 7200
74ac6d43
TT
7201 /* C++ base class field. */
7202 if (handle_data_member_location (die, cu, &offset))
7203 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7204 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7205 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7206 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7207 fip->nbaseclasses++;
7208 }
7209}
7210
98751a41
JK
7211/* Add a typedef defined in the scope of the FIP's class. */
7212
7213static void
7214dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7215 struct dwarf2_cu *cu)
6e70227d 7216{
98751a41 7217 struct objfile *objfile = cu->objfile;
98751a41
JK
7218 struct typedef_field_list *new_field;
7219 struct attribute *attr;
7220 struct typedef_field *fp;
7221 char *fieldname = "";
7222
7223 /* Allocate a new field list entry and link it in. */
7224 new_field = xzalloc (sizeof (*new_field));
7225 make_cleanup (xfree, new_field);
7226
7227 gdb_assert (die->tag == DW_TAG_typedef);
7228
7229 fp = &new_field->field;
7230
7231 /* Get name of field. */
7232 fp->name = dwarf2_name (die, cu);
7233 if (fp->name == NULL)
7234 return;
7235
7236 fp->type = read_type_die (die, cu);
7237
7238 new_field->next = fip->typedef_field_list;
7239 fip->typedef_field_list = new_field;
7240 fip->typedef_field_list_count++;
7241}
7242
c906108c
SS
7243/* Create the vector of fields, and attach it to the type. */
7244
7245static void
fba45db2 7246dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7247 struct dwarf2_cu *cu)
c906108c
SS
7248{
7249 int nfields = fip->nfields;
7250
7251 /* Record the field count, allocate space for the array of fields,
7252 and create blank accessibility bitfields if necessary. */
7253 TYPE_NFIELDS (type) = nfields;
7254 TYPE_FIELDS (type) = (struct field *)
7255 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7256 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7257
b4ba55a1 7258 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7259 {
7260 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7261
7262 TYPE_FIELD_PRIVATE_BITS (type) =
7263 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7264 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7265
7266 TYPE_FIELD_PROTECTED_BITS (type) =
7267 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7268 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7269
774b6a14
TT
7270 TYPE_FIELD_IGNORE_BITS (type) =
7271 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7272 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7273 }
7274
7275 /* If the type has baseclasses, allocate and clear a bit vector for
7276 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7277 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7278 {
7279 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7280 unsigned char *pointer;
c906108c
SS
7281
7282 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7283 pointer = TYPE_ALLOC (type, num_bytes);
7284 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7285 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7286 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7287 }
7288
3e43a32a
MS
7289 /* Copy the saved-up fields into the field vector. Start from the head of
7290 the list, adding to the tail of the field array, so that they end up in
7291 the same order in the array in which they were added to the list. */
c906108c
SS
7292 while (nfields-- > 0)
7293 {
7d0ccb61
DJ
7294 struct nextfield *fieldp;
7295
7296 if (fip->fields)
7297 {
7298 fieldp = fip->fields;
7299 fip->fields = fieldp->next;
7300 }
7301 else
7302 {
7303 fieldp = fip->baseclasses;
7304 fip->baseclasses = fieldp->next;
7305 }
7306
7307 TYPE_FIELD (type, nfields) = fieldp->field;
7308 switch (fieldp->accessibility)
c906108c 7309 {
c5aa993b 7310 case DW_ACCESS_private:
b4ba55a1
JB
7311 if (cu->language != language_ada)
7312 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7313 break;
c906108c 7314
c5aa993b 7315 case DW_ACCESS_protected:
b4ba55a1
JB
7316 if (cu->language != language_ada)
7317 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7318 break;
c906108c 7319
c5aa993b
JM
7320 case DW_ACCESS_public:
7321 break;
c906108c 7322
c5aa993b
JM
7323 default:
7324 /* Unknown accessibility. Complain and treat it as public. */
7325 {
e2e0b3e5 7326 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7327 fieldp->accessibility);
c5aa993b
JM
7328 }
7329 break;
c906108c
SS
7330 }
7331 if (nfields < fip->nbaseclasses)
7332 {
7d0ccb61 7333 switch (fieldp->virtuality)
c906108c 7334 {
c5aa993b
JM
7335 case DW_VIRTUALITY_virtual:
7336 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7337 if (cu->language == language_ada)
a73c6dcd 7338 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7339 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7340 break;
c906108c
SS
7341 }
7342 }
c906108c
SS
7343 }
7344}
7345
c906108c
SS
7346/* Add a member function to the proper fieldlist. */
7347
7348static void
107d2387 7349dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7350 struct type *type, struct dwarf2_cu *cu)
c906108c 7351{
e7c27a73 7352 struct objfile *objfile = cu->objfile;
c906108c
SS
7353 struct attribute *attr;
7354 struct fnfieldlist *flp;
7355 int i;
7356 struct fn_field *fnp;
7357 char *fieldname;
c906108c 7358 struct nextfnfield *new_fnfield;
f792889a 7359 struct type *this_type;
60d5a603 7360 enum dwarf_access_attribute accessibility;
c906108c 7361
b4ba55a1 7362 if (cu->language == language_ada)
a73c6dcd 7363 error (_("unexpected member function in Ada type"));
b4ba55a1 7364
2df3850c 7365 /* Get name of member function. */
39cbfefa
DJ
7366 fieldname = dwarf2_name (die, cu);
7367 if (fieldname == NULL)
2df3850c 7368 return;
c906108c 7369
c906108c
SS
7370 /* Look up member function name in fieldlist. */
7371 for (i = 0; i < fip->nfnfields; i++)
7372 {
27bfe10e 7373 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7374 break;
7375 }
7376
7377 /* Create new list element if necessary. */
7378 if (i < fip->nfnfields)
7379 flp = &fip->fnfieldlists[i];
7380 else
7381 {
7382 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7383 {
7384 fip->fnfieldlists = (struct fnfieldlist *)
7385 xrealloc (fip->fnfieldlists,
7386 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7387 * sizeof (struct fnfieldlist));
c906108c 7388 if (fip->nfnfields == 0)
c13c43fd 7389 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7390 }
7391 flp = &fip->fnfieldlists[fip->nfnfields];
7392 flp->name = fieldname;
7393 flp->length = 0;
7394 flp->head = NULL;
3da10d80 7395 i = fip->nfnfields++;
c906108c
SS
7396 }
7397
7398 /* Create a new member function field and chain it to the field list
0963b4bd 7399 entry. */
c906108c 7400 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7401 make_cleanup (xfree, new_fnfield);
c906108c
SS
7402 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7403 new_fnfield->next = flp->head;
7404 flp->head = new_fnfield;
7405 flp->length++;
7406
7407 /* Fill in the member function field info. */
7408 fnp = &new_fnfield->fnfield;
3da10d80
KS
7409
7410 /* Delay processing of the physname until later. */
7411 if (cu->language == language_cplus || cu->language == language_java)
7412 {
7413 add_to_method_list (type, i, flp->length - 1, fieldname,
7414 die, cu);
7415 }
7416 else
7417 {
1d06ead6 7418 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7419 fnp->physname = physname ? physname : "";
7420 }
7421
c906108c 7422 fnp->type = alloc_type (objfile);
f792889a
DJ
7423 this_type = read_type_die (die, cu);
7424 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7425 {
f792889a 7426 int nparams = TYPE_NFIELDS (this_type);
c906108c 7427
f792889a 7428 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7429 of the method itself (TYPE_CODE_METHOD). */
7430 smash_to_method_type (fnp->type, type,
f792889a
DJ
7431 TYPE_TARGET_TYPE (this_type),
7432 TYPE_FIELDS (this_type),
7433 TYPE_NFIELDS (this_type),
7434 TYPE_VARARGS (this_type));
c906108c
SS
7435
7436 /* Handle static member functions.
c5aa993b 7437 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7438 member functions. G++ helps GDB by marking the first
7439 parameter for non-static member functions (which is the this
7440 pointer) as artificial. We obtain this information from
7441 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7442 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7443 fnp->voffset = VOFFSET_STATIC;
7444 }
7445 else
e2e0b3e5 7446 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7447 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7448
7449 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7450 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7451 fnp->fcontext = die_containing_type (die, cu);
c906108c 7452
3e43a32a
MS
7453 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7454 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7455
7456 /* Get accessibility. */
e142c38c 7457 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7458 if (attr)
60d5a603
JK
7459 accessibility = DW_UNSND (attr);
7460 else
7461 accessibility = dwarf2_default_access_attribute (die, cu);
7462 switch (accessibility)
c906108c 7463 {
60d5a603
JK
7464 case DW_ACCESS_private:
7465 fnp->is_private = 1;
7466 break;
7467 case DW_ACCESS_protected:
7468 fnp->is_protected = 1;
7469 break;
c906108c
SS
7470 }
7471
b02dede2 7472 /* Check for artificial methods. */
e142c38c 7473 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7474 if (attr && DW_UNSND (attr) != 0)
7475 fnp->is_artificial = 1;
7476
0d564a31 7477 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7478 function. For older versions of GCC, this is an offset in the
7479 appropriate virtual table, as specified by DW_AT_containing_type.
7480 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7481 to the object address. */
7482
e142c38c 7483 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7484 if (attr)
8e19ed76 7485 {
aec5aa8b 7486 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7487 {
aec5aa8b
TT
7488 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7489 {
7490 /* Old-style GCC. */
7491 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7492 }
7493 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7494 || (DW_BLOCK (attr)->size > 1
7495 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7496 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7497 {
7498 struct dwarf_block blk;
7499 int offset;
7500
7501 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7502 ? 1 : 2);
7503 blk.size = DW_BLOCK (attr)->size - offset;
7504 blk.data = DW_BLOCK (attr)->data + offset;
7505 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7506 if ((fnp->voffset % cu->header.addr_size) != 0)
7507 dwarf2_complex_location_expr_complaint ();
7508 else
7509 fnp->voffset /= cu->header.addr_size;
7510 fnp->voffset += 2;
7511 }
7512 else
7513 dwarf2_complex_location_expr_complaint ();
7514
7515 if (!fnp->fcontext)
7516 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7517 }
3690dd37 7518 else if (attr_form_is_section_offset (attr))
8e19ed76 7519 {
4d3c2250 7520 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7521 }
7522 else
7523 {
4d3c2250
KB
7524 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7525 fieldname);
8e19ed76 7526 }
0d564a31 7527 }
d48cc9dd
DJ
7528 else
7529 {
7530 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7531 if (attr && DW_UNSND (attr))
7532 {
7533 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7534 complaint (&symfile_complaints,
3e43a32a
MS
7535 _("Member function \"%s\" (offset %d) is virtual "
7536 "but the vtable offset is not specified"),
d48cc9dd 7537 fieldname, die->offset);
9655fd1a 7538 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7539 TYPE_CPLUS_DYNAMIC (type) = 1;
7540 }
7541 }
c906108c
SS
7542}
7543
7544/* Create the vector of member function fields, and attach it to the type. */
7545
7546static void
fba45db2 7547dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7548 struct dwarf2_cu *cu)
c906108c
SS
7549{
7550 struct fnfieldlist *flp;
7551 int total_length = 0;
7552 int i;
7553
b4ba55a1 7554 if (cu->language == language_ada)
a73c6dcd 7555 error (_("unexpected member functions in Ada type"));
b4ba55a1 7556
c906108c
SS
7557 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7558 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7559 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7560
7561 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7562 {
7563 struct nextfnfield *nfp = flp->head;
7564 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7565 int k;
7566
7567 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7568 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7569 fn_flp->fn_fields = (struct fn_field *)
7570 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7571 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7572 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7573
7574 total_length += flp->length;
7575 }
7576
7577 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7578 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7579}
7580
1168df01
JB
7581/* Returns non-zero if NAME is the name of a vtable member in CU's
7582 language, zero otherwise. */
7583static int
7584is_vtable_name (const char *name, struct dwarf2_cu *cu)
7585{
7586 static const char vptr[] = "_vptr";
987504bb 7587 static const char vtable[] = "vtable";
1168df01 7588
987504bb
JJ
7589 /* Look for the C++ and Java forms of the vtable. */
7590 if ((cu->language == language_java
7591 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7592 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7593 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7594 return 1;
7595
7596 return 0;
7597}
7598
c0dd20ea 7599/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7600 functions, with the ABI-specified layout. If TYPE describes
7601 such a structure, smash it into a member function type.
61049d3b
DJ
7602
7603 GCC shouldn't do this; it should just output pointer to member DIEs.
7604 This is GCC PR debug/28767. */
c0dd20ea 7605
0b92b5bb
TT
7606static void
7607quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7608{
0b92b5bb 7609 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7610
7611 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7612 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7613 return;
c0dd20ea
DJ
7614
7615 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7616 if (TYPE_FIELD_NAME (type, 0) == NULL
7617 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7618 || TYPE_FIELD_NAME (type, 1) == NULL
7619 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7620 return;
c0dd20ea
DJ
7621
7622 /* Find the type of the method. */
0b92b5bb 7623 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7624 if (pfn_type == NULL
7625 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7626 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7627 return;
c0dd20ea
DJ
7628
7629 /* Look for the "this" argument. */
7630 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7631 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7632 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7633 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7634 return;
c0dd20ea
DJ
7635
7636 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7637 new_type = alloc_type (objfile);
7638 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7639 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7640 TYPE_VARARGS (pfn_type));
0b92b5bb 7641 smash_to_methodptr_type (type, new_type);
c0dd20ea 7642}
1168df01 7643
c906108c 7644/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7645 (definition) to create a type for the structure or union. Fill in
7646 the type's name and general properties; the members will not be
7647 processed until process_structure_type.
c906108c 7648
c767944b
DJ
7649 NOTE: we need to call these functions regardless of whether or not the
7650 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7651 structure or union. This gets the type entered into our set of
7652 user defined types.
7653
7654 However, if the structure is incomplete (an opaque struct/union)
7655 then suppress creating a symbol table entry for it since gdb only
7656 wants to find the one with the complete definition. Note that if
7657 it is complete, we just call new_symbol, which does it's own
7658 checking about whether the struct/union is anonymous or not (and
7659 suppresses creating a symbol table entry itself). */
7660
f792889a 7661static struct type *
134d01f1 7662read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7663{
e7c27a73 7664 struct objfile *objfile = cu->objfile;
c906108c
SS
7665 struct type *type;
7666 struct attribute *attr;
39cbfefa 7667 char *name;
c906108c 7668
348e048f
DE
7669 /* If the definition of this type lives in .debug_types, read that type.
7670 Don't follow DW_AT_specification though, that will take us back up
7671 the chain and we want to go down. */
7672 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7673 if (attr)
7674 {
7675 struct dwarf2_cu *type_cu = cu;
7676 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7677
348e048f
DE
7678 /* We could just recurse on read_structure_type, but we need to call
7679 get_die_type to ensure only one type for this DIE is created.
7680 This is important, for example, because for c++ classes we need
7681 TYPE_NAME set which is only done by new_symbol. Blech. */
7682 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7683
7684 /* TYPE_CU may not be the same as CU.
7685 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7686 return set_die_type (die, type, cu);
7687 }
7688
c0dd20ea 7689 type = alloc_type (objfile);
c906108c 7690 INIT_CPLUS_SPECIFIC (type);
93311388 7691
39cbfefa
DJ
7692 name = dwarf2_name (die, cu);
7693 if (name != NULL)
c906108c 7694 {
987504bb
JJ
7695 if (cu->language == language_cplus
7696 || cu->language == language_java)
63d06c5c 7697 {
3da10d80
KS
7698 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7699
7700 /* dwarf2_full_name might have already finished building the DIE's
7701 type. If so, there is no need to continue. */
7702 if (get_die_type (die, cu) != NULL)
7703 return get_die_type (die, cu);
7704
7705 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7706 if (die->tag == DW_TAG_structure_type
7707 || die->tag == DW_TAG_class_type)
7708 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7709 }
7710 else
7711 {
d8151005
DJ
7712 /* The name is already allocated along with this objfile, so
7713 we don't need to duplicate it for the type. */
94af9270
KS
7714 TYPE_TAG_NAME (type) = (char *) name;
7715 if (die->tag == DW_TAG_class_type)
7716 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7717 }
c906108c
SS
7718 }
7719
7720 if (die->tag == DW_TAG_structure_type)
7721 {
7722 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7723 }
7724 else if (die->tag == DW_TAG_union_type)
7725 {
7726 TYPE_CODE (type) = TYPE_CODE_UNION;
7727 }
7728 else
7729 {
c906108c
SS
7730 TYPE_CODE (type) = TYPE_CODE_CLASS;
7731 }
7732
0cc2414c
TT
7733 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7734 TYPE_DECLARED_CLASS (type) = 1;
7735
e142c38c 7736 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7737 if (attr)
7738 {
7739 TYPE_LENGTH (type) = DW_UNSND (attr);
7740 }
7741 else
7742 {
7743 TYPE_LENGTH (type) = 0;
7744 }
7745
876cecd0 7746 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7747 if (die_is_declaration (die, cu))
876cecd0 7748 TYPE_STUB (type) = 1;
a6c727b2
DJ
7749 else if (attr == NULL && die->child == NULL
7750 && producer_is_realview (cu->producer))
7751 /* RealView does not output the required DW_AT_declaration
7752 on incomplete types. */
7753 TYPE_STUB (type) = 1;
dc718098 7754
c906108c
SS
7755 /* We need to add the type field to the die immediately so we don't
7756 infinitely recurse when dealing with pointers to the structure
0963b4bd 7757 type within the structure itself. */
1c379e20 7758 set_die_type (die, type, cu);
c906108c 7759
7e314c57
JK
7760 /* set_die_type should be already done. */
7761 set_descriptive_type (type, die, cu);
7762
c767944b
DJ
7763 return type;
7764}
7765
7766/* Finish creating a structure or union type, including filling in
7767 its members and creating a symbol for it. */
7768
7769static void
7770process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7771{
7772 struct objfile *objfile = cu->objfile;
7773 struct die_info *child_die = die->child;
7774 struct type *type;
7775
7776 type = get_die_type (die, cu);
7777 if (type == NULL)
7778 type = read_structure_type (die, cu);
7779
e142c38c 7780 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7781 {
7782 struct field_info fi;
7783 struct die_info *child_die;
34eaf542 7784 VEC (symbolp) *template_args = NULL;
c767944b 7785 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7786
7787 memset (&fi, 0, sizeof (struct field_info));
7788
639d11d3 7789 child_die = die->child;
c906108c
SS
7790
7791 while (child_die && child_die->tag)
7792 {
a9a9bd0f
DC
7793 if (child_die->tag == DW_TAG_member
7794 || child_die->tag == DW_TAG_variable)
c906108c 7795 {
a9a9bd0f
DC
7796 /* NOTE: carlton/2002-11-05: A C++ static data member
7797 should be a DW_TAG_member that is a declaration, but
7798 all versions of G++ as of this writing (so through at
7799 least 3.2.1) incorrectly generate DW_TAG_variable
7800 tags for them instead. */
e7c27a73 7801 dwarf2_add_field (&fi, child_die, cu);
c906108c 7802 }
8713b1b1 7803 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7804 {
0963b4bd 7805 /* C++ member function. */
e7c27a73 7806 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7807 }
7808 else if (child_die->tag == DW_TAG_inheritance)
7809 {
7810 /* C++ base class field. */
e7c27a73 7811 dwarf2_add_field (&fi, child_die, cu);
c906108c 7812 }
98751a41
JK
7813 else if (child_die->tag == DW_TAG_typedef)
7814 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7815 else if (child_die->tag == DW_TAG_template_type_param
7816 || child_die->tag == DW_TAG_template_value_param)
7817 {
7818 struct symbol *arg = new_symbol (child_die, NULL, cu);
7819
f1078f66
DJ
7820 if (arg != NULL)
7821 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7822 }
7823
c906108c
SS
7824 child_die = sibling_die (child_die);
7825 }
7826
34eaf542
TT
7827 /* Attach template arguments to type. */
7828 if (! VEC_empty (symbolp, template_args))
7829 {
7830 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7831 TYPE_N_TEMPLATE_ARGUMENTS (type)
7832 = VEC_length (symbolp, template_args);
7833 TYPE_TEMPLATE_ARGUMENTS (type)
7834 = obstack_alloc (&objfile->objfile_obstack,
7835 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7836 * sizeof (struct symbol *)));
7837 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7838 VEC_address (symbolp, template_args),
7839 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7840 * sizeof (struct symbol *)));
7841 VEC_free (symbolp, template_args);
7842 }
7843
c906108c
SS
7844 /* Attach fields and member functions to the type. */
7845 if (fi.nfields)
e7c27a73 7846 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7847 if (fi.nfnfields)
7848 {
e7c27a73 7849 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7850
c5aa993b 7851 /* Get the type which refers to the base class (possibly this
c906108c 7852 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7853 class from the DW_AT_containing_type attribute. This use of
7854 DW_AT_containing_type is a GNU extension. */
c906108c 7855
e142c38c 7856 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7857 {
e7c27a73 7858 struct type *t = die_containing_type (die, cu);
c906108c
SS
7859
7860 TYPE_VPTR_BASETYPE (type) = t;
7861 if (type == t)
7862 {
c906108c
SS
7863 int i;
7864
7865 /* Our own class provides vtbl ptr. */
7866 for (i = TYPE_NFIELDS (t) - 1;
7867 i >= TYPE_N_BASECLASSES (t);
7868 --i)
7869 {
7870 char *fieldname = TYPE_FIELD_NAME (t, i);
7871
1168df01 7872 if (is_vtable_name (fieldname, cu))
c906108c
SS
7873 {
7874 TYPE_VPTR_FIELDNO (type) = i;
7875 break;
7876 }
7877 }
7878
7879 /* Complain if virtual function table field not found. */
7880 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7881 complaint (&symfile_complaints,
3e43a32a
MS
7882 _("virtual function table pointer "
7883 "not found when defining class '%s'"),
4d3c2250
KB
7884 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7885 "");
c906108c
SS
7886 }
7887 else
7888 {
7889 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7890 }
7891 }
f6235d4c
EZ
7892 else if (cu->producer
7893 && strncmp (cu->producer,
7894 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7895 {
7896 /* The IBM XLC compiler does not provide direct indication
7897 of the containing type, but the vtable pointer is
7898 always named __vfp. */
7899
7900 int i;
7901
7902 for (i = TYPE_NFIELDS (type) - 1;
7903 i >= TYPE_N_BASECLASSES (type);
7904 --i)
7905 {
7906 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7907 {
7908 TYPE_VPTR_FIELDNO (type) = i;
7909 TYPE_VPTR_BASETYPE (type) = type;
7910 break;
7911 }
7912 }
7913 }
c906108c 7914 }
98751a41
JK
7915
7916 /* Copy fi.typedef_field_list linked list elements content into the
7917 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7918 if (fi.typedef_field_list)
7919 {
7920 int i = fi.typedef_field_list_count;
7921
a0d7a4ff 7922 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7923 TYPE_TYPEDEF_FIELD_ARRAY (type)
7924 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7925 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7926
7927 /* Reverse the list order to keep the debug info elements order. */
7928 while (--i >= 0)
7929 {
7930 struct typedef_field *dest, *src;
6e70227d 7931
98751a41
JK
7932 dest = &TYPE_TYPEDEF_FIELD (type, i);
7933 src = &fi.typedef_field_list->field;
7934 fi.typedef_field_list = fi.typedef_field_list->next;
7935 *dest = *src;
7936 }
7937 }
c767944b
DJ
7938
7939 do_cleanups (back_to);
eb2a6f42
TT
7940
7941 if (HAVE_CPLUS_STRUCT (type))
7942 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7943 }
63d06c5c 7944
0b92b5bb
TT
7945 quirk_gcc_member_function_pointer (type, cu->objfile);
7946
90aeadfc
DC
7947 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7948 snapshots) has been known to create a die giving a declaration
7949 for a class that has, as a child, a die giving a definition for a
7950 nested class. So we have to process our children even if the
7951 current die is a declaration. Normally, of course, a declaration
7952 won't have any children at all. */
134d01f1 7953
90aeadfc
DC
7954 while (child_die != NULL && child_die->tag)
7955 {
7956 if (child_die->tag == DW_TAG_member
7957 || child_die->tag == DW_TAG_variable
34eaf542
TT
7958 || child_die->tag == DW_TAG_inheritance
7959 || child_die->tag == DW_TAG_template_value_param
7960 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7961 {
90aeadfc 7962 /* Do nothing. */
134d01f1 7963 }
90aeadfc
DC
7964 else
7965 process_die (child_die, cu);
134d01f1 7966
90aeadfc 7967 child_die = sibling_die (child_die);
134d01f1
DJ
7968 }
7969
fa4028e9
JB
7970 /* Do not consider external references. According to the DWARF standard,
7971 these DIEs are identified by the fact that they have no byte_size
7972 attribute, and a declaration attribute. */
7973 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7974 || !die_is_declaration (die, cu))
c767944b 7975 new_symbol (die, type, cu);
134d01f1
DJ
7976}
7977
7978/* Given a DW_AT_enumeration_type die, set its type. We do not
7979 complete the type's fields yet, or create any symbols. */
c906108c 7980
f792889a 7981static struct type *
134d01f1 7982read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7983{
e7c27a73 7984 struct objfile *objfile = cu->objfile;
c906108c 7985 struct type *type;
c906108c 7986 struct attribute *attr;
0114d602 7987 const char *name;
134d01f1 7988
348e048f
DE
7989 /* If the definition of this type lives in .debug_types, read that type.
7990 Don't follow DW_AT_specification though, that will take us back up
7991 the chain and we want to go down. */
7992 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7993 if (attr)
7994 {
7995 struct dwarf2_cu *type_cu = cu;
7996 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7997
348e048f 7998 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7999
8000 /* TYPE_CU may not be the same as CU.
8001 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
8002 return set_die_type (die, type, cu);
8003 }
8004
c906108c
SS
8005 type = alloc_type (objfile);
8006
8007 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 8008 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 8009 if (name != NULL)
0114d602 8010 TYPE_TAG_NAME (type) = (char *) name;
c906108c 8011
e142c38c 8012 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8013 if (attr)
8014 {
8015 TYPE_LENGTH (type) = DW_UNSND (attr);
8016 }
8017 else
8018 {
8019 TYPE_LENGTH (type) = 0;
8020 }
8021
137033e9
JB
8022 /* The enumeration DIE can be incomplete. In Ada, any type can be
8023 declared as private in the package spec, and then defined only
8024 inside the package body. Such types are known as Taft Amendment
8025 Types. When another package uses such a type, an incomplete DIE
8026 may be generated by the compiler. */
02eb380e 8027 if (die_is_declaration (die, cu))
876cecd0 8028 TYPE_STUB (type) = 1;
02eb380e 8029
f792889a 8030 return set_die_type (die, type, cu);
134d01f1
DJ
8031}
8032
8033/* Given a pointer to a die which begins an enumeration, process all
8034 the dies that define the members of the enumeration, and create the
8035 symbol for the enumeration type.
8036
8037 NOTE: We reverse the order of the element list. */
8038
8039static void
8040process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
8041{
f792889a 8042 struct type *this_type;
134d01f1 8043
f792889a
DJ
8044 this_type = get_die_type (die, cu);
8045 if (this_type == NULL)
8046 this_type = read_enumeration_type (die, cu);
9dc481d3 8047
639d11d3 8048 if (die->child != NULL)
c906108c 8049 {
9dc481d3
DE
8050 struct die_info *child_die;
8051 struct symbol *sym;
8052 struct field *fields = NULL;
8053 int num_fields = 0;
8054 int unsigned_enum = 1;
8055 char *name;
8056
639d11d3 8057 child_die = die->child;
c906108c
SS
8058 while (child_die && child_die->tag)
8059 {
8060 if (child_die->tag != DW_TAG_enumerator)
8061 {
e7c27a73 8062 process_die (child_die, cu);
c906108c
SS
8063 }
8064 else
8065 {
39cbfefa
DJ
8066 name = dwarf2_name (child_die, cu);
8067 if (name)
c906108c 8068 {
f792889a 8069 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
8070 if (SYMBOL_VALUE (sym) < 0)
8071 unsigned_enum = 0;
8072
8073 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8074 {
8075 fields = (struct field *)
8076 xrealloc (fields,
8077 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8078 * sizeof (struct field));
c906108c
SS
8079 }
8080
3567439c 8081 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 8082 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 8083 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
8084 FIELD_BITSIZE (fields[num_fields]) = 0;
8085
8086 num_fields++;
8087 }
8088 }
8089
8090 child_die = sibling_die (child_die);
8091 }
8092
8093 if (num_fields)
8094 {
f792889a
DJ
8095 TYPE_NFIELDS (this_type) = num_fields;
8096 TYPE_FIELDS (this_type) = (struct field *)
8097 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8098 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 8099 sizeof (struct field) * num_fields);
b8c9b27d 8100 xfree (fields);
c906108c
SS
8101 }
8102 if (unsigned_enum)
876cecd0 8103 TYPE_UNSIGNED (this_type) = 1;
c906108c 8104 }
134d01f1 8105
6c83ed52
TT
8106 /* If we are reading an enum from a .debug_types unit, and the enum
8107 is a declaration, and the enum is not the signatured type in the
8108 unit, then we do not want to add a symbol for it. Adding a
8109 symbol would in some cases obscure the true definition of the
8110 enum, giving users an incomplete type when the definition is
8111 actually available. Note that we do not want to do this for all
8112 enums which are just declarations, because C++0x allows forward
8113 enum declarations. */
b0df02fd 8114 if (cu->per_cu->debug_types_section
6c83ed52
TT
8115 && die_is_declaration (die, cu))
8116 {
8117 struct signatured_type *type_sig;
8118
8119 type_sig
8120 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
b0df02fd 8121 cu->per_cu->debug_types_section,
6c83ed52
TT
8122 cu->per_cu->offset);
8123 if (type_sig->type_offset != die->offset)
8124 return;
8125 }
8126
f792889a 8127 new_symbol (die, this_type, cu);
c906108c
SS
8128}
8129
8130/* Extract all information from a DW_TAG_array_type DIE and put it in
8131 the DIE's type field. For now, this only handles one dimensional
8132 arrays. */
8133
f792889a 8134static struct type *
e7c27a73 8135read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8136{
e7c27a73 8137 struct objfile *objfile = cu->objfile;
c906108c 8138 struct die_info *child_die;
7e314c57 8139 struct type *type;
c906108c
SS
8140 struct type *element_type, *range_type, *index_type;
8141 struct type **range_types = NULL;
8142 struct attribute *attr;
8143 int ndim = 0;
8144 struct cleanup *back_to;
39cbfefa 8145 char *name;
c906108c 8146
e7c27a73 8147 element_type = die_type (die, cu);
c906108c 8148
7e314c57
JK
8149 /* The die_type call above may have already set the type for this DIE. */
8150 type = get_die_type (die, cu);
8151 if (type)
8152 return type;
8153
c906108c
SS
8154 /* Irix 6.2 native cc creates array types without children for
8155 arrays with unspecified length. */
639d11d3 8156 if (die->child == NULL)
c906108c 8157 {
46bf5051 8158 index_type = objfile_type (objfile)->builtin_int;
c906108c 8159 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8160 type = create_array_type (NULL, element_type, range_type);
8161 return set_die_type (die, type, cu);
c906108c
SS
8162 }
8163
8164 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8165 child_die = die->child;
c906108c
SS
8166 while (child_die && child_die->tag)
8167 {
8168 if (child_die->tag == DW_TAG_subrange_type)
8169 {
f792889a 8170 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8171
f792889a 8172 if (child_type != NULL)
a02abb62 8173 {
0963b4bd
MS
8174 /* The range type was succesfully read. Save it for the
8175 array type creation. */
a02abb62
JB
8176 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8177 {
8178 range_types = (struct type **)
8179 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8180 * sizeof (struct type *));
8181 if (ndim == 0)
8182 make_cleanup (free_current_contents, &range_types);
8183 }
f792889a 8184 range_types[ndim++] = child_type;
a02abb62 8185 }
c906108c
SS
8186 }
8187 child_die = sibling_die (child_die);
8188 }
8189
8190 /* Dwarf2 dimensions are output from left to right, create the
8191 necessary array types in backwards order. */
7ca2d3a3 8192
c906108c 8193 type = element_type;
7ca2d3a3
DL
8194
8195 if (read_array_order (die, cu) == DW_ORD_col_major)
8196 {
8197 int i = 0;
9a619af0 8198
7ca2d3a3
DL
8199 while (i < ndim)
8200 type = create_array_type (NULL, type, range_types[i++]);
8201 }
8202 else
8203 {
8204 while (ndim-- > 0)
8205 type = create_array_type (NULL, type, range_types[ndim]);
8206 }
c906108c 8207
f5f8a009
EZ
8208 /* Understand Dwarf2 support for vector types (like they occur on
8209 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8210 array type. This is not part of the Dwarf2/3 standard yet, but a
8211 custom vendor extension. The main difference between a regular
8212 array and the vector variant is that vectors are passed by value
8213 to functions. */
e142c38c 8214 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8215 if (attr)
ea37ba09 8216 make_vector_type (type);
f5f8a009 8217
dbc98a8b
KW
8218 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8219 implementation may choose to implement triple vectors using this
8220 attribute. */
8221 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8222 if (attr)
8223 {
8224 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8225 TYPE_LENGTH (type) = DW_UNSND (attr);
8226 else
3e43a32a
MS
8227 complaint (&symfile_complaints,
8228 _("DW_AT_byte_size for array type smaller "
8229 "than the total size of elements"));
dbc98a8b
KW
8230 }
8231
39cbfefa
DJ
8232 name = dwarf2_name (die, cu);
8233 if (name)
8234 TYPE_NAME (type) = name;
6e70227d 8235
0963b4bd 8236 /* Install the type in the die. */
7e314c57
JK
8237 set_die_type (die, type, cu);
8238
8239 /* set_die_type should be already done. */
b4ba55a1
JB
8240 set_descriptive_type (type, die, cu);
8241
c906108c
SS
8242 do_cleanups (back_to);
8243
7e314c57 8244 return type;
c906108c
SS
8245}
8246
7ca2d3a3 8247static enum dwarf_array_dim_ordering
6e70227d 8248read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8249{
8250 struct attribute *attr;
8251
8252 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8253
8254 if (attr) return DW_SND (attr);
8255
0963b4bd
MS
8256 /* GNU F77 is a special case, as at 08/2004 array type info is the
8257 opposite order to the dwarf2 specification, but data is still
8258 laid out as per normal fortran.
7ca2d3a3 8259
0963b4bd
MS
8260 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8261 version checking. */
7ca2d3a3 8262
905e0470
PM
8263 if (cu->language == language_fortran
8264 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8265 {
8266 return DW_ORD_row_major;
8267 }
8268
6e70227d 8269 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8270 {
8271 case array_column_major:
8272 return DW_ORD_col_major;
8273 case array_row_major:
8274 default:
8275 return DW_ORD_row_major;
8276 };
8277}
8278
72019c9c 8279/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8280 the DIE's type field. */
72019c9c 8281
f792889a 8282static struct type *
72019c9c
GM
8283read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8284{
7e314c57
JK
8285 struct type *domain_type, *set_type;
8286 struct attribute *attr;
f792889a 8287
7e314c57
JK
8288 domain_type = die_type (die, cu);
8289
8290 /* The die_type call above may have already set the type for this DIE. */
8291 set_type = get_die_type (die, cu);
8292 if (set_type)
8293 return set_type;
8294
8295 set_type = create_set_type (NULL, domain_type);
8296
8297 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8298 if (attr)
8299 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8300
f792889a 8301 return set_die_type (die, set_type, cu);
72019c9c 8302}
7ca2d3a3 8303
c906108c
SS
8304/* First cut: install each common block member as a global variable. */
8305
8306static void
e7c27a73 8307read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8308{
8309 struct die_info *child_die;
8310 struct attribute *attr;
8311 struct symbol *sym;
8312 CORE_ADDR base = (CORE_ADDR) 0;
8313
e142c38c 8314 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8315 if (attr)
8316 {
0963b4bd 8317 /* Support the .debug_loc offsets. */
8e19ed76
PS
8318 if (attr_form_is_block (attr))
8319 {
e7c27a73 8320 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8321 }
3690dd37 8322 else if (attr_form_is_section_offset (attr))
8e19ed76 8323 {
4d3c2250 8324 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8325 }
8326 else
8327 {
4d3c2250
KB
8328 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8329 "common block member");
8e19ed76 8330 }
c906108c 8331 }
639d11d3 8332 if (die->child != NULL)
c906108c 8333 {
639d11d3 8334 child_die = die->child;
c906108c
SS
8335 while (child_die && child_die->tag)
8336 {
74ac6d43
TT
8337 LONGEST offset;
8338
e7c27a73 8339 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8340 if (sym != NULL
8341 && handle_data_member_location (child_die, cu, &offset))
c906108c 8342 {
74ac6d43 8343 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8344 add_symbol_to_list (sym, &global_symbols);
8345 }
8346 child_die = sibling_die (child_die);
8347 }
8348 }
8349}
8350
0114d602 8351/* Create a type for a C++ namespace. */
d9fa45fe 8352
0114d602
DJ
8353static struct type *
8354read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8355{
e7c27a73 8356 struct objfile *objfile = cu->objfile;
0114d602 8357 const char *previous_prefix, *name;
9219021c 8358 int is_anonymous;
0114d602
DJ
8359 struct type *type;
8360
8361 /* For extensions, reuse the type of the original namespace. */
8362 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8363 {
8364 struct die_info *ext_die;
8365 struct dwarf2_cu *ext_cu = cu;
9a619af0 8366
0114d602
DJ
8367 ext_die = dwarf2_extension (die, &ext_cu);
8368 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8369
8370 /* EXT_CU may not be the same as CU.
8371 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8372 return set_die_type (die, type, cu);
8373 }
9219021c 8374
e142c38c 8375 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8376
8377 /* Now build the name of the current namespace. */
8378
0114d602
DJ
8379 previous_prefix = determine_prefix (die, cu);
8380 if (previous_prefix[0] != '\0')
8381 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8382 previous_prefix, name, 0, cu);
0114d602
DJ
8383
8384 /* Create the type. */
8385 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8386 objfile);
8387 TYPE_NAME (type) = (char *) name;
8388 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8389
60531b24 8390 return set_die_type (die, type, cu);
0114d602
DJ
8391}
8392
8393/* Read a C++ namespace. */
8394
8395static void
8396read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8397{
8398 struct objfile *objfile = cu->objfile;
0114d602 8399 int is_anonymous;
9219021c 8400
5c4e30ca
DC
8401 /* Add a symbol associated to this if we haven't seen the namespace
8402 before. Also, add a using directive if it's an anonymous
8403 namespace. */
9219021c 8404
f2f0e013 8405 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8406 {
8407 struct type *type;
8408
0114d602 8409 type = read_type_die (die, cu);
e7c27a73 8410 new_symbol (die, type, cu);
5c4e30ca 8411
e8e80198 8412 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8413 if (is_anonymous)
0114d602
DJ
8414 {
8415 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8416
c0cc3a76 8417 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8418 NULL, NULL, &objfile->objfile_obstack);
0114d602 8419 }
5c4e30ca 8420 }
9219021c 8421
639d11d3 8422 if (die->child != NULL)
d9fa45fe 8423 {
639d11d3 8424 struct die_info *child_die = die->child;
6e70227d 8425
d9fa45fe
DC
8426 while (child_die && child_die->tag)
8427 {
e7c27a73 8428 process_die (child_die, cu);
d9fa45fe
DC
8429 child_die = sibling_die (child_die);
8430 }
8431 }
38d518c9
EZ
8432}
8433
f55ee35c
JK
8434/* Read a Fortran module as type. This DIE can be only a declaration used for
8435 imported module. Still we need that type as local Fortran "use ... only"
8436 declaration imports depend on the created type in determine_prefix. */
8437
8438static struct type *
8439read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8440{
8441 struct objfile *objfile = cu->objfile;
8442 char *module_name;
8443 struct type *type;
8444
8445 module_name = dwarf2_name (die, cu);
8446 if (!module_name)
3e43a32a
MS
8447 complaint (&symfile_complaints,
8448 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8449 die->offset);
8450 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8451
8452 /* determine_prefix uses TYPE_TAG_NAME. */
8453 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8454
8455 return set_die_type (die, type, cu);
8456}
8457
5d7cb8df
JK
8458/* Read a Fortran module. */
8459
8460static void
8461read_module (struct die_info *die, struct dwarf2_cu *cu)
8462{
8463 struct die_info *child_die = die->child;
8464
5d7cb8df
JK
8465 while (child_die && child_die->tag)
8466 {
8467 process_die (child_die, cu);
8468 child_die = sibling_die (child_die);
8469 }
8470}
8471
38d518c9
EZ
8472/* Return the name of the namespace represented by DIE. Set
8473 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8474 namespace. */
8475
8476static const char *
e142c38c 8477namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8478{
8479 struct die_info *current_die;
8480 const char *name = NULL;
8481
8482 /* Loop through the extensions until we find a name. */
8483
8484 for (current_die = die;
8485 current_die != NULL;
f2f0e013 8486 current_die = dwarf2_extension (die, &cu))
38d518c9 8487 {
e142c38c 8488 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8489 if (name != NULL)
8490 break;
8491 }
8492
8493 /* Is it an anonymous namespace? */
8494
8495 *is_anonymous = (name == NULL);
8496 if (*is_anonymous)
2b1dbab0 8497 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8498
8499 return name;
d9fa45fe
DC
8500}
8501
c906108c
SS
8502/* Extract all information from a DW_TAG_pointer_type DIE and add to
8503 the user defined type vector. */
8504
f792889a 8505static struct type *
e7c27a73 8506read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8507{
5e2b427d 8508 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8509 struct comp_unit_head *cu_header = &cu->header;
c906108c 8510 struct type *type;
8b2dbe47
KB
8511 struct attribute *attr_byte_size;
8512 struct attribute *attr_address_class;
8513 int byte_size, addr_class;
7e314c57
JK
8514 struct type *target_type;
8515
8516 target_type = die_type (die, cu);
c906108c 8517
7e314c57
JK
8518 /* The die_type call above may have already set the type for this DIE. */
8519 type = get_die_type (die, cu);
8520 if (type)
8521 return type;
8522
8523 type = lookup_pointer_type (target_type);
8b2dbe47 8524
e142c38c 8525 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8526 if (attr_byte_size)
8527 byte_size = DW_UNSND (attr_byte_size);
c906108c 8528 else
8b2dbe47
KB
8529 byte_size = cu_header->addr_size;
8530
e142c38c 8531 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8532 if (attr_address_class)
8533 addr_class = DW_UNSND (attr_address_class);
8534 else
8535 addr_class = DW_ADDR_none;
8536
8537 /* If the pointer size or address class is different than the
8538 default, create a type variant marked as such and set the
8539 length accordingly. */
8540 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8541 {
5e2b427d 8542 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8543 {
8544 int type_flags;
8545
849957d9 8546 type_flags = gdbarch_address_class_type_flags
5e2b427d 8547 (gdbarch, byte_size, addr_class);
876cecd0
TT
8548 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8549 == 0);
8b2dbe47
KB
8550 type = make_type_with_address_space (type, type_flags);
8551 }
8552 else if (TYPE_LENGTH (type) != byte_size)
8553 {
3e43a32a
MS
8554 complaint (&symfile_complaints,
8555 _("invalid pointer size %d"), byte_size);
8b2dbe47 8556 }
6e70227d 8557 else
9a619af0
MS
8558 {
8559 /* Should we also complain about unhandled address classes? */
8560 }
c906108c 8561 }
8b2dbe47
KB
8562
8563 TYPE_LENGTH (type) = byte_size;
f792889a 8564 return set_die_type (die, type, cu);
c906108c
SS
8565}
8566
8567/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8568 the user defined type vector. */
8569
f792889a 8570static struct type *
e7c27a73 8571read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8572{
8573 struct type *type;
8574 struct type *to_type;
8575 struct type *domain;
8576
e7c27a73
DJ
8577 to_type = die_type (die, cu);
8578 domain = die_containing_type (die, cu);
0d5de010 8579
7e314c57
JK
8580 /* The calls above may have already set the type for this DIE. */
8581 type = get_die_type (die, cu);
8582 if (type)
8583 return type;
8584
0d5de010
DJ
8585 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8586 type = lookup_methodptr_type (to_type);
8587 else
8588 type = lookup_memberptr_type (to_type, domain);
c906108c 8589
f792889a 8590 return set_die_type (die, type, cu);
c906108c
SS
8591}
8592
8593/* Extract all information from a DW_TAG_reference_type DIE and add to
8594 the user defined type vector. */
8595
f792889a 8596static struct type *
e7c27a73 8597read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8598{
e7c27a73 8599 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8600 struct type *type, *target_type;
c906108c
SS
8601 struct attribute *attr;
8602
7e314c57
JK
8603 target_type = die_type (die, cu);
8604
8605 /* The die_type call above may have already set the type for this DIE. */
8606 type = get_die_type (die, cu);
8607 if (type)
8608 return type;
8609
8610 type = lookup_reference_type (target_type);
e142c38c 8611 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8612 if (attr)
8613 {
8614 TYPE_LENGTH (type) = DW_UNSND (attr);
8615 }
8616 else
8617 {
107d2387 8618 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8619 }
f792889a 8620 return set_die_type (die, type, cu);
c906108c
SS
8621}
8622
f792889a 8623static struct type *
e7c27a73 8624read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8625{
f792889a 8626 struct type *base_type, *cv_type;
c906108c 8627
e7c27a73 8628 base_type = die_type (die, cu);
7e314c57
JK
8629
8630 /* The die_type call above may have already set the type for this DIE. */
8631 cv_type = get_die_type (die, cu);
8632 if (cv_type)
8633 return cv_type;
8634
2f608a3a
KW
8635 /* In case the const qualifier is applied to an array type, the element type
8636 is so qualified, not the array type (section 6.7.3 of C99). */
8637 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8638 {
8639 struct type *el_type, *inner_array;
8640
8641 base_type = copy_type (base_type);
8642 inner_array = base_type;
8643
8644 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8645 {
8646 TYPE_TARGET_TYPE (inner_array) =
8647 copy_type (TYPE_TARGET_TYPE (inner_array));
8648 inner_array = TYPE_TARGET_TYPE (inner_array);
8649 }
8650
8651 el_type = TYPE_TARGET_TYPE (inner_array);
8652 TYPE_TARGET_TYPE (inner_array) =
8653 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8654
8655 return set_die_type (die, base_type, cu);
8656 }
8657
f792889a
DJ
8658 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8659 return set_die_type (die, cv_type, cu);
c906108c
SS
8660}
8661
f792889a 8662static struct type *
e7c27a73 8663read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8664{
f792889a 8665 struct type *base_type, *cv_type;
c906108c 8666
e7c27a73 8667 base_type = die_type (die, cu);
7e314c57
JK
8668
8669 /* The die_type call above may have already set the type for this DIE. */
8670 cv_type = get_die_type (die, cu);
8671 if (cv_type)
8672 return cv_type;
8673
f792889a
DJ
8674 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8675 return set_die_type (die, cv_type, cu);
c906108c
SS
8676}
8677
8678/* Extract all information from a DW_TAG_string_type DIE and add to
8679 the user defined type vector. It isn't really a user defined type,
8680 but it behaves like one, with other DIE's using an AT_user_def_type
8681 attribute to reference it. */
8682
f792889a 8683static struct type *
e7c27a73 8684read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8685{
e7c27a73 8686 struct objfile *objfile = cu->objfile;
3b7538c0 8687 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8688 struct type *type, *range_type, *index_type, *char_type;
8689 struct attribute *attr;
8690 unsigned int length;
8691
e142c38c 8692 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8693 if (attr)
8694 {
8695 length = DW_UNSND (attr);
8696 }
8697 else
8698 {
0963b4bd 8699 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8700 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8701 if (attr)
8702 {
8703 length = DW_UNSND (attr);
8704 }
8705 else
8706 {
8707 length = 1;
8708 }
c906108c 8709 }
6ccb9162 8710
46bf5051 8711 index_type = objfile_type (objfile)->builtin_int;
c906108c 8712 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8713 char_type = language_string_char_type (cu->language_defn, gdbarch);
8714 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8715
f792889a 8716 return set_die_type (die, type, cu);
c906108c
SS
8717}
8718
8719/* Handle DIES due to C code like:
8720
8721 struct foo
c5aa993b
JM
8722 {
8723 int (*funcp)(int a, long l);
8724 int b;
8725 };
c906108c 8726
0963b4bd 8727 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8728
f792889a 8729static struct type *
e7c27a73 8730read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8731{
0963b4bd
MS
8732 struct type *type; /* Type that this function returns. */
8733 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8734 struct attribute *attr;
8735
e7c27a73 8736 type = die_type (die, cu);
7e314c57
JK
8737
8738 /* The die_type call above may have already set the type for this DIE. */
8739 ftype = get_die_type (die, cu);
8740 if (ftype)
8741 return ftype;
8742
0c8b41f1 8743 ftype = lookup_function_type (type);
c906108c 8744
5b8101ae 8745 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8746 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8747 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8748 || cu->language == language_cplus
5b8101ae
PM
8749 || cu->language == language_java
8750 || cu->language == language_pascal)
876cecd0 8751 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8752 else if (producer_is_realview (cu->producer))
8753 /* RealView does not emit DW_AT_prototyped. We can not
8754 distinguish prototyped and unprototyped functions; default to
8755 prototyped, since that is more common in modern code (and
8756 RealView warns about unprototyped functions). */
8757 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8758
c055b101
CV
8759 /* Store the calling convention in the type if it's available in
8760 the subroutine die. Otherwise set the calling convention to
8761 the default value DW_CC_normal. */
8762 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8763 if (attr)
8764 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8765 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8766 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8767 else
8768 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8769
8770 /* We need to add the subroutine type to the die immediately so
8771 we don't infinitely recurse when dealing with parameters
0963b4bd 8772 declared as the same subroutine type. */
76c10ea2 8773 set_die_type (die, ftype, cu);
6e70227d 8774
639d11d3 8775 if (die->child != NULL)
c906108c 8776 {
8072405b 8777 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8778 struct die_info *child_die;
8072405b 8779 int nparams, iparams;
c906108c
SS
8780
8781 /* Count the number of parameters.
8782 FIXME: GDB currently ignores vararg functions, but knows about
8783 vararg member functions. */
8072405b 8784 nparams = 0;
639d11d3 8785 child_die = die->child;
c906108c
SS
8786 while (child_die && child_die->tag)
8787 {
8788 if (child_die->tag == DW_TAG_formal_parameter)
8789 nparams++;
8790 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8791 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8792 child_die = sibling_die (child_die);
8793 }
8794
8795 /* Allocate storage for parameters and fill them in. */
8796 TYPE_NFIELDS (ftype) = nparams;
8797 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8798 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8799
8072405b
JK
8800 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8801 even if we error out during the parameters reading below. */
8802 for (iparams = 0; iparams < nparams; iparams++)
8803 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8804
8805 iparams = 0;
639d11d3 8806 child_die = die->child;
c906108c
SS
8807 while (child_die && child_die->tag)
8808 {
8809 if (child_die->tag == DW_TAG_formal_parameter)
8810 {
3ce3b1ba
PA
8811 struct type *arg_type;
8812
8813 /* DWARF version 2 has no clean way to discern C++
8814 static and non-static member functions. G++ helps
8815 GDB by marking the first parameter for non-static
8816 member functions (which is the this pointer) as
8817 artificial. We pass this information to
8818 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8819
8820 DWARF version 3 added DW_AT_object_pointer, which GCC
8821 4.5 does not yet generate. */
e142c38c 8822 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8823 if (attr)
8824 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8825 else
418835cc
KS
8826 {
8827 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8828
8829 /* GCC/43521: In java, the formal parameter
8830 "this" is sometimes not marked with DW_AT_artificial. */
8831 if (cu->language == language_java)
8832 {
8833 const char *name = dwarf2_name (child_die, cu);
9a619af0 8834
418835cc
KS
8835 if (name && !strcmp (name, "this"))
8836 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8837 }
8838 }
3ce3b1ba
PA
8839 arg_type = die_type (child_die, cu);
8840
8841 /* RealView does not mark THIS as const, which the testsuite
8842 expects. GCC marks THIS as const in method definitions,
8843 but not in the class specifications (GCC PR 43053). */
8844 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8845 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8846 {
8847 int is_this = 0;
8848 struct dwarf2_cu *arg_cu = cu;
8849 const char *name = dwarf2_name (child_die, cu);
8850
8851 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8852 if (attr)
8853 {
8854 /* If the compiler emits this, use it. */
8855 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8856 is_this = 1;
8857 }
8858 else if (name && strcmp (name, "this") == 0)
8859 /* Function definitions will have the argument names. */
8860 is_this = 1;
8861 else if (name == NULL && iparams == 0)
8862 /* Declarations may not have the names, so like
8863 elsewhere in GDB, assume an artificial first
8864 argument is "this". */
8865 is_this = 1;
8866
8867 if (is_this)
8868 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8869 arg_type, 0);
8870 }
8871
8872 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8873 iparams++;
8874 }
8875 child_die = sibling_die (child_die);
8876 }
8877 }
8878
76c10ea2 8879 return ftype;
c906108c
SS
8880}
8881
f792889a 8882static struct type *
e7c27a73 8883read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8884{
e7c27a73 8885 struct objfile *objfile = cu->objfile;
0114d602 8886 const char *name = NULL;
f792889a 8887 struct type *this_type;
c906108c 8888
94af9270 8889 name = dwarf2_full_name (NULL, die, cu);
f792889a 8890 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8891 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8892 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8893 set_die_type (die, this_type, cu);
8894 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8895 return this_type;
c906108c
SS
8896}
8897
8898/* Find a representation of a given base type and install
8899 it in the TYPE field of the die. */
8900
f792889a 8901static struct type *
e7c27a73 8902read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8903{
e7c27a73 8904 struct objfile *objfile = cu->objfile;
c906108c
SS
8905 struct type *type;
8906 struct attribute *attr;
8907 int encoding = 0, size = 0;
39cbfefa 8908 char *name;
6ccb9162
UW
8909 enum type_code code = TYPE_CODE_INT;
8910 int type_flags = 0;
8911 struct type *target_type = NULL;
c906108c 8912
e142c38c 8913 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8914 if (attr)
8915 {
8916 encoding = DW_UNSND (attr);
8917 }
e142c38c 8918 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8919 if (attr)
8920 {
8921 size = DW_UNSND (attr);
8922 }
39cbfefa 8923 name = dwarf2_name (die, cu);
6ccb9162 8924 if (!name)
c906108c 8925 {
6ccb9162
UW
8926 complaint (&symfile_complaints,
8927 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8928 }
6ccb9162
UW
8929
8930 switch (encoding)
c906108c 8931 {
6ccb9162
UW
8932 case DW_ATE_address:
8933 /* Turn DW_ATE_address into a void * pointer. */
8934 code = TYPE_CODE_PTR;
8935 type_flags |= TYPE_FLAG_UNSIGNED;
8936 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8937 break;
8938 case DW_ATE_boolean:
8939 code = TYPE_CODE_BOOL;
8940 type_flags |= TYPE_FLAG_UNSIGNED;
8941 break;
8942 case DW_ATE_complex_float:
8943 code = TYPE_CODE_COMPLEX;
8944 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8945 break;
8946 case DW_ATE_decimal_float:
8947 code = TYPE_CODE_DECFLOAT;
8948 break;
8949 case DW_ATE_float:
8950 code = TYPE_CODE_FLT;
8951 break;
8952 case DW_ATE_signed:
8953 break;
8954 case DW_ATE_unsigned:
8955 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8956 if (cu->language == language_fortran
8957 && name
8958 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8959 code = TYPE_CODE_CHAR;
6ccb9162
UW
8960 break;
8961 case DW_ATE_signed_char:
6e70227d 8962 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8963 || cu->language == language_pascal
8964 || cu->language == language_fortran)
6ccb9162
UW
8965 code = TYPE_CODE_CHAR;
8966 break;
8967 case DW_ATE_unsigned_char:
868a0084 8968 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8969 || cu->language == language_pascal
8970 || cu->language == language_fortran)
6ccb9162
UW
8971 code = TYPE_CODE_CHAR;
8972 type_flags |= TYPE_FLAG_UNSIGNED;
8973 break;
75079b2b
TT
8974 case DW_ATE_UTF:
8975 /* We just treat this as an integer and then recognize the
8976 type by name elsewhere. */
8977 break;
8978
6ccb9162
UW
8979 default:
8980 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8981 dwarf_type_encoding_name (encoding));
8982 break;
c906108c 8983 }
6ccb9162 8984
0114d602
DJ
8985 type = init_type (code, size, type_flags, NULL, objfile);
8986 TYPE_NAME (type) = name;
6ccb9162
UW
8987 TYPE_TARGET_TYPE (type) = target_type;
8988
0114d602 8989 if (name && strcmp (name, "char") == 0)
876cecd0 8990 TYPE_NOSIGN (type) = 1;
0114d602 8991
f792889a 8992 return set_die_type (die, type, cu);
c906108c
SS
8993}
8994
a02abb62
JB
8995/* Read the given DW_AT_subrange DIE. */
8996
f792889a 8997static struct type *
a02abb62
JB
8998read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8999{
9000 struct type *base_type;
9001 struct type *range_type;
9002 struct attribute *attr;
43bbcdc2
PH
9003 LONGEST low = 0;
9004 LONGEST high = -1;
39cbfefa 9005 char *name;
43bbcdc2 9006 LONGEST negative_mask;
e77813c8 9007
a02abb62 9008 base_type = die_type (die, cu);
953ac07e
JK
9009 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
9010 check_typedef (base_type);
a02abb62 9011
7e314c57
JK
9012 /* The die_type call above may have already set the type for this DIE. */
9013 range_type = get_die_type (die, cu);
9014 if (range_type)
9015 return range_type;
9016
e142c38c 9017 if (cu->language == language_fortran)
6e70227d 9018 {
a02abb62
JB
9019 /* FORTRAN implies a lower bound of 1, if not given. */
9020 low = 1;
9021 }
9022
dd5e6932
DJ
9023 /* FIXME: For variable sized arrays either of these could be
9024 a variable rather than a constant value. We'll allow it,
9025 but we don't know how to handle it. */
e142c38c 9026 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
9027 if (attr)
9028 low = dwarf2_get_attr_constant_value (attr, 0);
9029
e142c38c 9030 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 9031 if (attr)
6e70227d 9032 {
d48323d8 9033 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
9034 {
9035 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 9036 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
9037 FIXME: GDB does not yet know how to handle dynamic
9038 arrays properly, treat them as arrays with unspecified
9039 length for now.
9040
9041 FIXME: jimb/2003-09-22: GDB does not really know
9042 how to handle arrays of unspecified length
9043 either; we just represent them as zero-length
9044 arrays. Choose an appropriate upper bound given
9045 the lower bound we've computed above. */
9046 high = low - 1;
9047 }
9048 else
9049 high = dwarf2_get_attr_constant_value (attr, 1);
9050 }
e77813c8
PM
9051 else
9052 {
9053 attr = dwarf2_attr (die, DW_AT_count, cu);
9054 if (attr)
9055 {
9056 int count = dwarf2_get_attr_constant_value (attr, 1);
9057 high = low + count - 1;
9058 }
c2ff108b
JK
9059 else
9060 {
9061 /* Unspecified array length. */
9062 high = low - 1;
9063 }
e77813c8
PM
9064 }
9065
9066 /* Dwarf-2 specifications explicitly allows to create subrange types
9067 without specifying a base type.
9068 In that case, the base type must be set to the type of
9069 the lower bound, upper bound or count, in that order, if any of these
9070 three attributes references an object that has a type.
9071 If no base type is found, the Dwarf-2 specifications say that
9072 a signed integer type of size equal to the size of an address should
9073 be used.
9074 For the following C code: `extern char gdb_int [];'
9075 GCC produces an empty range DIE.
9076 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 9077 high bound or count are not yet handled by this code. */
e77813c8
PM
9078 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9079 {
9080 struct objfile *objfile = cu->objfile;
9081 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9082 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9083 struct type *int_type = objfile_type (objfile)->builtin_int;
9084
9085 /* Test "int", "long int", and "long long int" objfile types,
9086 and select the first one having a size above or equal to the
9087 architecture address size. */
9088 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9089 base_type = int_type;
9090 else
9091 {
9092 int_type = objfile_type (objfile)->builtin_long;
9093 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9094 base_type = int_type;
9095 else
9096 {
9097 int_type = objfile_type (objfile)->builtin_long_long;
9098 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9099 base_type = int_type;
9100 }
9101 }
9102 }
a02abb62 9103
6e70227d 9104 negative_mask =
43bbcdc2
PH
9105 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9106 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9107 low |= negative_mask;
9108 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9109 high |= negative_mask;
9110
a02abb62
JB
9111 range_type = create_range_type (NULL, base_type, low, high);
9112
bbb0eef6
JK
9113 /* Mark arrays with dynamic length at least as an array of unspecified
9114 length. GDB could check the boundary but before it gets implemented at
9115 least allow accessing the array elements. */
d48323d8 9116 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9117 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9118
c2ff108b
JK
9119 /* Ada expects an empty array on no boundary attributes. */
9120 if (attr == NULL && cu->language != language_ada)
9121 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9122
39cbfefa
DJ
9123 name = dwarf2_name (die, cu);
9124 if (name)
9125 TYPE_NAME (range_type) = name;
6e70227d 9126
e142c38c 9127 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9128 if (attr)
9129 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9130
7e314c57
JK
9131 set_die_type (die, range_type, cu);
9132
9133 /* set_die_type should be already done. */
b4ba55a1
JB
9134 set_descriptive_type (range_type, die, cu);
9135
7e314c57 9136 return range_type;
a02abb62 9137}
6e70227d 9138
f792889a 9139static struct type *
81a17f79
JB
9140read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9141{
9142 struct type *type;
81a17f79 9143
81a17f79
JB
9144 /* For now, we only support the C meaning of an unspecified type: void. */
9145
0114d602
DJ
9146 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9147 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9148
f792889a 9149 return set_die_type (die, type, cu);
81a17f79 9150}
a02abb62 9151
51545339
DJ
9152/* Trivial hash function for die_info: the hash value of a DIE
9153 is its offset in .debug_info for this objfile. */
9154
9155static hashval_t
9156die_hash (const void *item)
9157{
9158 const struct die_info *die = item;
9a619af0 9159
51545339
DJ
9160 return die->offset;
9161}
9162
9163/* Trivial comparison function for die_info structures: two DIEs
9164 are equal if they have the same offset. */
9165
9166static int
9167die_eq (const void *item_lhs, const void *item_rhs)
9168{
9169 const struct die_info *die_lhs = item_lhs;
9170 const struct die_info *die_rhs = item_rhs;
9a619af0 9171
51545339
DJ
9172 return die_lhs->offset == die_rhs->offset;
9173}
9174
c906108c
SS
9175/* Read a whole compilation unit into a linked list of dies. */
9176
f9aca02d 9177static struct die_info *
93311388 9178read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9179{
93311388 9180 struct die_reader_specs reader_specs;
98bfdba5 9181 int read_abbrevs = 0;
1d9ec526 9182 struct cleanup *back_to = NULL;
98bfdba5
PA
9183 struct die_info *die;
9184
9185 if (cu->dwarf2_abbrevs == NULL)
9186 {
9187 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9188 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9189 read_abbrevs = 1;
9190 }
93311388 9191
348e048f 9192 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9193 cu->die_hash
9194 = htab_create_alloc_ex (cu->header.length / 12,
9195 die_hash,
9196 die_eq,
9197 NULL,
9198 &cu->comp_unit_obstack,
9199 hashtab_obstack_allocate,
9200 dummy_obstack_deallocate);
9201
93311388
DE
9202 init_cu_die_reader (&reader_specs, cu);
9203
98bfdba5
PA
9204 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9205
9206 if (read_abbrevs)
9207 do_cleanups (back_to);
9208
9209 return die;
639d11d3
DC
9210}
9211
d97bc12b
DE
9212/* Main entry point for reading a DIE and all children.
9213 Read the DIE and dump it if requested. */
9214
9215static struct die_info *
93311388
DE
9216read_die_and_children (const struct die_reader_specs *reader,
9217 gdb_byte *info_ptr,
d97bc12b
DE
9218 gdb_byte **new_info_ptr,
9219 struct die_info *parent)
9220{
93311388 9221 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9222 new_info_ptr, parent);
9223
9224 if (dwarf2_die_debug)
9225 {
348e048f
DE
9226 fprintf_unfiltered (gdb_stdlog,
9227 "\nRead die from %s of %s:\n",
b0df02fd 9228 (reader->cu->per_cu->debug_types_section
8b70b953
TT
9229 ? ".debug_types"
9230 : ".debug_info"),
348e048f 9231 reader->abfd->filename);
d97bc12b
DE
9232 dump_die (result, dwarf2_die_debug);
9233 }
9234
9235 return result;
9236}
9237
639d11d3
DC
9238/* Read a single die and all its descendents. Set the die's sibling
9239 field to NULL; set other fields in the die correctly, and set all
9240 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9241 location of the info_ptr after reading all of those dies. PARENT
9242 is the parent of the die in question. */
9243
9244static struct die_info *
93311388
DE
9245read_die_and_children_1 (const struct die_reader_specs *reader,
9246 gdb_byte *info_ptr,
d97bc12b
DE
9247 gdb_byte **new_info_ptr,
9248 struct die_info *parent)
639d11d3
DC
9249{
9250 struct die_info *die;
fe1b8b76 9251 gdb_byte *cur_ptr;
639d11d3
DC
9252 int has_children;
9253
93311388 9254 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9255 if (die == NULL)
9256 {
9257 *new_info_ptr = cur_ptr;
9258 return NULL;
9259 }
93311388 9260 store_in_ref_table (die, reader->cu);
639d11d3
DC
9261
9262 if (has_children)
348e048f 9263 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9264 else
9265 {
9266 die->child = NULL;
9267 *new_info_ptr = cur_ptr;
9268 }
9269
9270 die->sibling = NULL;
9271 die->parent = parent;
9272 return die;
9273}
9274
9275/* Read a die, all of its descendents, and all of its siblings; set
9276 all of the fields of all of the dies correctly. Arguments are as
9277 in read_die_and_children. */
9278
9279static struct die_info *
93311388
DE
9280read_die_and_siblings (const struct die_reader_specs *reader,
9281 gdb_byte *info_ptr,
fe1b8b76 9282 gdb_byte **new_info_ptr,
639d11d3
DC
9283 struct die_info *parent)
9284{
9285 struct die_info *first_die, *last_sibling;
fe1b8b76 9286 gdb_byte *cur_ptr;
639d11d3 9287
c906108c 9288 cur_ptr = info_ptr;
639d11d3
DC
9289 first_die = last_sibling = NULL;
9290
9291 while (1)
c906108c 9292 {
639d11d3 9293 struct die_info *die
93311388 9294 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9295
1d325ec1 9296 if (die == NULL)
c906108c 9297 {
639d11d3
DC
9298 *new_info_ptr = cur_ptr;
9299 return first_die;
c906108c 9300 }
1d325ec1
DJ
9301
9302 if (!first_die)
9303 first_die = die;
c906108c 9304 else
1d325ec1
DJ
9305 last_sibling->sibling = die;
9306
9307 last_sibling = die;
c906108c 9308 }
c906108c
SS
9309}
9310
93311388
DE
9311/* Read the die from the .debug_info section buffer. Set DIEP to
9312 point to a newly allocated die with its information, except for its
9313 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9314 whether the die has children or not. */
9315
9316static gdb_byte *
9317read_full_die (const struct die_reader_specs *reader,
9318 struct die_info **diep, gdb_byte *info_ptr,
9319 int *has_children)
9320{
9321 unsigned int abbrev_number, bytes_read, i, offset;
9322 struct abbrev_info *abbrev;
9323 struct die_info *die;
9324 struct dwarf2_cu *cu = reader->cu;
9325 bfd *abfd = reader->abfd;
9326
9327 offset = info_ptr - reader->buffer;
9328 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9329 info_ptr += bytes_read;
9330 if (!abbrev_number)
9331 {
9332 *diep = NULL;
9333 *has_children = 0;
9334 return info_ptr;
9335 }
9336
9337 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9338 if (!abbrev)
348e048f
DE
9339 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9340 abbrev_number,
9341 bfd_get_filename (abfd));
9342
93311388
DE
9343 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9344 die->offset = offset;
9345 die->tag = abbrev->tag;
9346 die->abbrev = abbrev_number;
9347
9348 die->num_attrs = abbrev->num_attrs;
9349
9350 for (i = 0; i < abbrev->num_attrs; ++i)
9351 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9352 abfd, info_ptr, cu);
9353
9354 *diep = die;
9355 *has_children = abbrev->has_children;
9356 return info_ptr;
9357}
9358
c906108c
SS
9359/* In DWARF version 2, the description of the debugging information is
9360 stored in a separate .debug_abbrev section. Before we read any
9361 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9362 in a hash table. This function also sets flags in CU describing
9363 the data found in the abbrev table. */
c906108c
SS
9364
9365static void
e7c27a73 9366dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 9367{
e7c27a73 9368 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9369 gdb_byte *abbrev_ptr;
c906108c
SS
9370 struct abbrev_info *cur_abbrev;
9371 unsigned int abbrev_number, bytes_read, abbrev_name;
9372 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9373 struct attr_abbrev *cur_attrs;
9374 unsigned int allocated_attrs;
c906108c 9375
0963b4bd 9376 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9377 obstack_init (&cu->abbrev_obstack);
9378 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9379 (ABBREV_HASH_SIZE
9380 * sizeof (struct abbrev_info *)));
9381 memset (cu->dwarf2_abbrevs, 0,
9382 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9383
be391dca
TT
9384 dwarf2_read_section (dwarf2_per_objfile->objfile,
9385 &dwarf2_per_objfile->abbrev);
dce234bc 9386 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9387 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9388 abbrev_ptr += bytes_read;
9389
f3dd6933
DJ
9390 allocated_attrs = ATTR_ALLOC_CHUNK;
9391 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9392
0963b4bd 9393 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9394 while (abbrev_number)
9395 {
f3dd6933 9396 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9397
9398 /* read in abbrev header */
9399 cur_abbrev->number = abbrev_number;
9400 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9401 abbrev_ptr += bytes_read;
9402 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9403 abbrev_ptr += 1;
9404
72bf9492
DJ
9405 if (cur_abbrev->tag == DW_TAG_namespace)
9406 cu->has_namespace_info = 1;
9407
c906108c
SS
9408 /* now read in declarations */
9409 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9410 abbrev_ptr += bytes_read;
9411 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9412 abbrev_ptr += bytes_read;
9413 while (abbrev_name)
9414 {
f3dd6933 9415 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9416 {
f3dd6933
DJ
9417 allocated_attrs += ATTR_ALLOC_CHUNK;
9418 cur_attrs
9419 = xrealloc (cur_attrs, (allocated_attrs
9420 * sizeof (struct attr_abbrev)));
c906108c 9421 }
ae038cb0
DJ
9422
9423 /* Record whether this compilation unit might have
9424 inter-compilation-unit references. If we don't know what form
9425 this attribute will have, then it might potentially be a
9426 DW_FORM_ref_addr, so we conservatively expect inter-CU
9427 references. */
9428
9429 if (abbrev_form == DW_FORM_ref_addr
9430 || abbrev_form == DW_FORM_indirect)
9431 cu->has_form_ref_addr = 1;
9432
f3dd6933
DJ
9433 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9434 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9435 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9436 abbrev_ptr += bytes_read;
9437 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9438 abbrev_ptr += bytes_read;
9439 }
9440
f3dd6933
DJ
9441 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9442 (cur_abbrev->num_attrs
9443 * sizeof (struct attr_abbrev)));
9444 memcpy (cur_abbrev->attrs, cur_attrs,
9445 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9446
c906108c 9447 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9448 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9449 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9450
9451 /* Get next abbreviation.
9452 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9453 always properly terminated with an abbrev number of 0.
9454 Exit loop if we encounter an abbreviation which we have
9455 already read (which means we are about to read the abbreviations
9456 for the next compile unit) or if the end of the abbreviation
9457 table is reached. */
dce234bc
PP
9458 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9459 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9460 break;
9461 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9462 abbrev_ptr += bytes_read;
e7c27a73 9463 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9464 break;
9465 }
f3dd6933
DJ
9466
9467 xfree (cur_attrs);
c906108c
SS
9468}
9469
f3dd6933 9470/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9471
c906108c 9472static void
f3dd6933 9473dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9474{
f3dd6933 9475 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9476
f3dd6933
DJ
9477 obstack_free (&cu->abbrev_obstack, NULL);
9478 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9479}
9480
9481/* Lookup an abbrev_info structure in the abbrev hash table. */
9482
9483static struct abbrev_info *
e7c27a73 9484dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9485{
9486 unsigned int hash_number;
9487 struct abbrev_info *abbrev;
9488
9489 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9490 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9491
9492 while (abbrev)
9493 {
9494 if (abbrev->number == number)
9495 return abbrev;
9496 else
9497 abbrev = abbrev->next;
9498 }
9499 return NULL;
9500}
9501
72bf9492
DJ
9502/* Returns nonzero if TAG represents a type that we might generate a partial
9503 symbol for. */
9504
9505static int
9506is_type_tag_for_partial (int tag)
9507{
9508 switch (tag)
9509 {
9510#if 0
9511 /* Some types that would be reasonable to generate partial symbols for,
9512 that we don't at present. */
9513 case DW_TAG_array_type:
9514 case DW_TAG_file_type:
9515 case DW_TAG_ptr_to_member_type:
9516 case DW_TAG_set_type:
9517 case DW_TAG_string_type:
9518 case DW_TAG_subroutine_type:
9519#endif
9520 case DW_TAG_base_type:
9521 case DW_TAG_class_type:
680b30c7 9522 case DW_TAG_interface_type:
72bf9492
DJ
9523 case DW_TAG_enumeration_type:
9524 case DW_TAG_structure_type:
9525 case DW_TAG_subrange_type:
9526 case DW_TAG_typedef:
9527 case DW_TAG_union_type:
9528 return 1;
9529 default:
9530 return 0;
9531 }
9532}
9533
9534/* Load all DIEs that are interesting for partial symbols into memory. */
9535
9536static struct partial_die_info *
93311388
DE
9537load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9538 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
9539{
9540 struct partial_die_info *part_die;
9541 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9542 struct abbrev_info *abbrev;
9543 unsigned int bytes_read;
5afb4e99 9544 unsigned int load_all = 0;
72bf9492
DJ
9545
9546 int nesting_level = 1;
9547
9548 parent_die = NULL;
9549 last_die = NULL;
9550
5afb4e99
DJ
9551 if (cu->per_cu && cu->per_cu->load_all_dies)
9552 load_all = 1;
9553
72bf9492
DJ
9554 cu->partial_dies
9555 = htab_create_alloc_ex (cu->header.length / 12,
9556 partial_die_hash,
9557 partial_die_eq,
9558 NULL,
9559 &cu->comp_unit_obstack,
9560 hashtab_obstack_allocate,
9561 dummy_obstack_deallocate);
9562
9563 part_die = obstack_alloc (&cu->comp_unit_obstack,
9564 sizeof (struct partial_die_info));
9565
9566 while (1)
9567 {
9568 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9569
9570 /* A NULL abbrev means the end of a series of children. */
9571 if (abbrev == NULL)
9572 {
9573 if (--nesting_level == 0)
9574 {
9575 /* PART_DIE was probably the last thing allocated on the
9576 comp_unit_obstack, so we could call obstack_free
9577 here. We don't do that because the waste is small,
9578 and will be cleaned up when we're done with this
9579 compilation unit. This way, we're also more robust
9580 against other users of the comp_unit_obstack. */
9581 return first_die;
9582 }
9583 info_ptr += bytes_read;
9584 last_die = parent_die;
9585 parent_die = parent_die->die_parent;
9586 continue;
9587 }
9588
98bfdba5
PA
9589 /* Check for template arguments. We never save these; if
9590 they're seen, we just mark the parent, and go on our way. */
9591 if (parent_die != NULL
9592 && cu->language == language_cplus
9593 && (abbrev->tag == DW_TAG_template_type_param
9594 || abbrev->tag == DW_TAG_template_value_param))
9595 {
9596 parent_die->has_template_arguments = 1;
9597
9598 if (!load_all)
9599 {
9600 /* We don't need a partial DIE for the template argument. */
9601 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9602 cu);
9603 continue;
9604 }
9605 }
9606
9607 /* We only recurse into subprograms looking for template arguments.
9608 Skip their other children. */
9609 if (!load_all
9610 && cu->language == language_cplus
9611 && parent_die != NULL
9612 && parent_die->tag == DW_TAG_subprogram)
9613 {
9614 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9615 continue;
9616 }
9617
5afb4e99
DJ
9618 /* Check whether this DIE is interesting enough to save. Normally
9619 we would not be interested in members here, but there may be
9620 later variables referencing them via DW_AT_specification (for
9621 static members). */
9622 if (!load_all
9623 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9624 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9625 && abbrev->tag != DW_TAG_enumerator
9626 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9627 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9628 && abbrev->tag != DW_TAG_variable
5afb4e99 9629 && abbrev->tag != DW_TAG_namespace
f55ee35c 9630 && abbrev->tag != DW_TAG_module
5afb4e99 9631 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9632 {
9633 /* Otherwise we skip to the next sibling, if any. */
93311388 9634 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9635 continue;
9636 }
9637
93311388
DE
9638 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9639 buffer, info_ptr, cu);
72bf9492
DJ
9640
9641 /* This two-pass algorithm for processing partial symbols has a
9642 high cost in cache pressure. Thus, handle some simple cases
9643 here which cover the majority of C partial symbols. DIEs
9644 which neither have specification tags in them, nor could have
9645 specification tags elsewhere pointing at them, can simply be
9646 processed and discarded.
9647
9648 This segment is also optional; scan_partial_symbols and
9649 add_partial_symbol will handle these DIEs if we chain
9650 them in normally. When compilers which do not emit large
9651 quantities of duplicate debug information are more common,
9652 this code can probably be removed. */
9653
9654 /* Any complete simple types at the top level (pretty much all
9655 of them, for a language without namespaces), can be processed
9656 directly. */
9657 if (parent_die == NULL
9658 && part_die->has_specification == 0
9659 && part_die->is_declaration == 0
d8228535 9660 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9661 || part_die->tag == DW_TAG_base_type
9662 || part_die->tag == DW_TAG_subrange_type))
9663 {
9664 if (building_psymtab && part_die->name != NULL)
04a679b8 9665 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
9666 VAR_DOMAIN, LOC_TYPEDEF,
9667 &cu->objfile->static_psymbols,
9668 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 9669 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9670 continue;
9671 }
9672
d8228535
JK
9673 /* The exception for DW_TAG_typedef with has_children above is
9674 a workaround of GCC PR debug/47510. In the case of this complaint
9675 type_name_no_tag_or_error will error on such types later.
9676
9677 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9678 it could not find the child DIEs referenced later, this is checked
9679 above. In correct DWARF DW_TAG_typedef should have no children. */
9680
9681 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9682 complaint (&symfile_complaints,
9683 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9684 "- DIE at 0x%x [in module %s]"),
9685 part_die->offset, cu->objfile->name);
9686
72bf9492
DJ
9687 /* If we're at the second level, and we're an enumerator, and
9688 our parent has no specification (meaning possibly lives in a
9689 namespace elsewhere), then we can add the partial symbol now
9690 instead of queueing it. */
9691 if (part_die->tag == DW_TAG_enumerator
9692 && parent_die != NULL
9693 && parent_die->die_parent == NULL
9694 && parent_die->tag == DW_TAG_enumeration_type
9695 && parent_die->has_specification == 0)
9696 {
9697 if (part_die->name == NULL)
3e43a32a
MS
9698 complaint (&symfile_complaints,
9699 _("malformed enumerator DIE ignored"));
72bf9492 9700 else if (building_psymtab)
04a679b8 9701 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9702 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9703 (cu->language == language_cplus
9704 || cu->language == language_java)
72bf9492
DJ
9705 ? &cu->objfile->global_psymbols
9706 : &cu->objfile->static_psymbols,
9707 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9708
93311388 9709 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9710 continue;
9711 }
9712
9713 /* We'll save this DIE so link it in. */
9714 part_die->die_parent = parent_die;
9715 part_die->die_sibling = NULL;
9716 part_die->die_child = NULL;
9717
9718 if (last_die && last_die == parent_die)
9719 last_die->die_child = part_die;
9720 else if (last_die)
9721 last_die->die_sibling = part_die;
9722
9723 last_die = part_die;
9724
9725 if (first_die == NULL)
9726 first_die = part_die;
9727
9728 /* Maybe add the DIE to the hash table. Not all DIEs that we
9729 find interesting need to be in the hash table, because we
9730 also have the parent/sibling/child chains; only those that we
9731 might refer to by offset later during partial symbol reading.
9732
9733 For now this means things that might have be the target of a
9734 DW_AT_specification, DW_AT_abstract_origin, or
9735 DW_AT_extension. DW_AT_extension will refer only to
9736 namespaces; DW_AT_abstract_origin refers to functions (and
9737 many things under the function DIE, but we do not recurse
9738 into function DIEs during partial symbol reading) and
9739 possibly variables as well; DW_AT_specification refers to
9740 declarations. Declarations ought to have the DW_AT_declaration
9741 flag. It happens that GCC forgets to put it in sometimes, but
9742 only for functions, not for types.
9743
9744 Adding more things than necessary to the hash table is harmless
9745 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9746 wasted time in find_partial_die, when we reread the compilation
9747 unit with load_all_dies set. */
72bf9492 9748
5afb4e99 9749 if (load_all
72929c62 9750 || abbrev->tag == DW_TAG_constant
5afb4e99 9751 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9752 || abbrev->tag == DW_TAG_variable
9753 || abbrev->tag == DW_TAG_namespace
9754 || part_die->is_declaration)
9755 {
9756 void **slot;
9757
9758 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9759 part_die->offset, INSERT);
9760 *slot = part_die;
9761 }
9762
9763 part_die = obstack_alloc (&cu->comp_unit_obstack,
9764 sizeof (struct partial_die_info));
9765
9766 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9767 we have no reason to follow the children of structures; for other
98bfdba5
PA
9768 languages we have to, so that we can get at method physnames
9769 to infer fully qualified class names, for DW_AT_specification,
9770 and for C++ template arguments. For C++, we also look one level
9771 inside functions to find template arguments (if the name of the
9772 function does not already contain the template arguments).
bc30ff58
JB
9773
9774 For Ada, we need to scan the children of subprograms and lexical
9775 blocks as well because Ada allows the definition of nested
9776 entities that could be interesting for the debugger, such as
9777 nested subprograms for instance. */
72bf9492 9778 if (last_die->has_children
5afb4e99
DJ
9779 && (load_all
9780 || last_die->tag == DW_TAG_namespace
f55ee35c 9781 || last_die->tag == DW_TAG_module
72bf9492 9782 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9783 || (cu->language == language_cplus
9784 && last_die->tag == DW_TAG_subprogram
9785 && (last_die->name == NULL
9786 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9787 || (cu->language != language_c
9788 && (last_die->tag == DW_TAG_class_type
680b30c7 9789 || last_die->tag == DW_TAG_interface_type
72bf9492 9790 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9791 || last_die->tag == DW_TAG_union_type))
9792 || (cu->language == language_ada
9793 && (last_die->tag == DW_TAG_subprogram
9794 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9795 {
9796 nesting_level++;
9797 parent_die = last_die;
9798 continue;
9799 }
9800
9801 /* Otherwise we skip to the next sibling, if any. */
93311388 9802 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9803
9804 /* Back to the top, do it again. */
9805 }
9806}
9807
c906108c
SS
9808/* Read a minimal amount of information into the minimal die structure. */
9809
fe1b8b76 9810static gdb_byte *
72bf9492
DJ
9811read_partial_die (struct partial_die_info *part_die,
9812 struct abbrev_info *abbrev,
9813 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9814 gdb_byte *buffer, gdb_byte *info_ptr,
9815 struct dwarf2_cu *cu)
c906108c 9816{
fa238c03 9817 unsigned int i;
c906108c 9818 struct attribute attr;
c5aa993b 9819 int has_low_pc_attr = 0;
c906108c
SS
9820 int has_high_pc_attr = 0;
9821
72bf9492 9822 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9823
93311388 9824 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9825
9826 info_ptr += abbrev_len;
9827
9828 if (abbrev == NULL)
9829 return info_ptr;
9830
c906108c
SS
9831 part_die->tag = abbrev->tag;
9832 part_die->has_children = abbrev->has_children;
c906108c
SS
9833
9834 for (i = 0; i < abbrev->num_attrs; ++i)
9835 {
e7c27a73 9836 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9837
9838 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9839 partial symbol table. */
c906108c
SS
9840 switch (attr.name)
9841 {
9842 case DW_AT_name:
71c25dea
TT
9843 switch (part_die->tag)
9844 {
9845 case DW_TAG_compile_unit:
348e048f 9846 case DW_TAG_type_unit:
71c25dea
TT
9847 /* Compilation units have a DW_AT_name that is a filename, not
9848 a source language identifier. */
9849 case DW_TAG_enumeration_type:
9850 case DW_TAG_enumerator:
9851 /* These tags always have simple identifiers already; no need
9852 to canonicalize them. */
9853 part_die->name = DW_STRING (&attr);
9854 break;
9855 default:
9856 part_die->name
9857 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9858 &cu->objfile->objfile_obstack);
71c25dea
TT
9859 break;
9860 }
c906108c 9861 break;
31ef98ae 9862 case DW_AT_linkage_name:
c906108c 9863 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9864 /* Note that both forms of linkage name might appear. We
9865 assume they will be the same, and we only store the last
9866 one we see. */
94af9270
KS
9867 if (cu->language == language_ada)
9868 part_die->name = DW_STRING (&attr);
abc72ce4 9869 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9870 break;
9871 case DW_AT_low_pc:
9872 has_low_pc_attr = 1;
9873 part_die->lowpc = DW_ADDR (&attr);
9874 break;
9875 case DW_AT_high_pc:
9876 has_high_pc_attr = 1;
9877 part_die->highpc = DW_ADDR (&attr);
9878 break;
9879 case DW_AT_location:
0963b4bd 9880 /* Support the .debug_loc offsets. */
8e19ed76
PS
9881 if (attr_form_is_block (&attr))
9882 {
9883 part_die->locdesc = DW_BLOCK (&attr);
9884 }
3690dd37 9885 else if (attr_form_is_section_offset (&attr))
8e19ed76 9886 {
4d3c2250 9887 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9888 }
9889 else
9890 {
4d3c2250
KB
9891 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9892 "partial symbol information");
8e19ed76 9893 }
c906108c 9894 break;
c906108c
SS
9895 case DW_AT_external:
9896 part_die->is_external = DW_UNSND (&attr);
9897 break;
9898 case DW_AT_declaration:
9899 part_die->is_declaration = DW_UNSND (&attr);
9900 break;
9901 case DW_AT_type:
9902 part_die->has_type = 1;
9903 break;
9904 case DW_AT_abstract_origin:
9905 case DW_AT_specification:
72bf9492
DJ
9906 case DW_AT_extension:
9907 part_die->has_specification = 1;
c764a876 9908 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9909 break;
9910 case DW_AT_sibling:
9911 /* Ignore absolute siblings, they might point outside of
9912 the current compile unit. */
9913 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9914 complaint (&symfile_complaints,
9915 _("ignoring absolute DW_AT_sibling"));
c906108c 9916 else
93311388 9917 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9918 break;
fa4028e9
JB
9919 case DW_AT_byte_size:
9920 part_die->has_byte_size = 1;
9921 break;
68511cec
CES
9922 case DW_AT_calling_convention:
9923 /* DWARF doesn't provide a way to identify a program's source-level
9924 entry point. DW_AT_calling_convention attributes are only meant
9925 to describe functions' calling conventions.
9926
9927 However, because it's a necessary piece of information in
9928 Fortran, and because DW_CC_program is the only piece of debugging
9929 information whose definition refers to a 'main program' at all,
9930 several compilers have begun marking Fortran main programs with
9931 DW_CC_program --- even when those functions use the standard
9932 calling conventions.
9933
9934 So until DWARF specifies a way to provide this information and
9935 compilers pick up the new representation, we'll support this
9936 practice. */
9937 if (DW_UNSND (&attr) == DW_CC_program
9938 && cu->language == language_fortran)
01f8c46d
JK
9939 {
9940 set_main_name (part_die->name);
9941
9942 /* As this DIE has a static linkage the name would be difficult
9943 to look up later. */
9944 language_of_main = language_fortran;
9945 }
68511cec 9946 break;
c906108c
SS
9947 default:
9948 break;
9949 }
9950 }
9951
9373cf26
JK
9952 if (has_low_pc_attr && has_high_pc_attr)
9953 {
9954 /* When using the GNU linker, .gnu.linkonce. sections are used to
9955 eliminate duplicate copies of functions and vtables and such.
9956 The linker will arbitrarily choose one and discard the others.
9957 The AT_*_pc values for such functions refer to local labels in
9958 these sections. If the section from that file was discarded, the
9959 labels are not in the output, so the relocs get a value of 0.
9960 If this is a discarded function, mark the pc bounds as invalid,
9961 so that GDB will ignore it. */
9962 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9963 {
9964 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9965
9966 complaint (&symfile_complaints,
9967 _("DW_AT_low_pc %s is zero "
9968 "for DIE at 0x%x [in module %s]"),
9969 paddress (gdbarch, part_die->lowpc),
9970 part_die->offset, cu->objfile->name);
9971 }
9972 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9973 else if (part_die->lowpc >= part_die->highpc)
9974 {
9975 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9976
9977 complaint (&symfile_complaints,
9978 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9979 "for DIE at 0x%x [in module %s]"),
9980 paddress (gdbarch, part_die->lowpc),
9981 paddress (gdbarch, part_die->highpc),
9982 part_die->offset, cu->objfile->name);
9983 }
9984 else
9985 part_die->has_pc_info = 1;
9986 }
85cbf3d3 9987
c906108c
SS
9988 return info_ptr;
9989}
9990
72bf9492
DJ
9991/* Find a cached partial DIE at OFFSET in CU. */
9992
9993static struct partial_die_info *
c764a876 9994find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9995{
9996 struct partial_die_info *lookup_die = NULL;
9997 struct partial_die_info part_die;
9998
9999 part_die.offset = offset;
10000 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
10001
72bf9492
DJ
10002 return lookup_die;
10003}
10004
348e048f
DE
10005/* Find a partial DIE at OFFSET, which may or may not be in CU,
10006 except in the case of .debug_types DIEs which do not reference
10007 outside their CU (they do however referencing other types via
55f1336d 10008 DW_FORM_ref_sig8). */
72bf9492
DJ
10009
10010static struct partial_die_info *
c764a876 10011find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 10012{
5afb4e99
DJ
10013 struct dwarf2_per_cu_data *per_cu = NULL;
10014 struct partial_die_info *pd = NULL;
72bf9492 10015
b0df02fd 10016 if (cu->per_cu->debug_types_section)
348e048f
DE
10017 {
10018 pd = find_partial_die_in_comp_unit (offset, cu);
10019 if (pd != NULL)
10020 return pd;
10021 goto not_found;
10022 }
10023
45452591 10024 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
10025 {
10026 pd = find_partial_die_in_comp_unit (offset, cu);
10027 if (pd != NULL)
10028 return pd;
10029 }
72bf9492 10030
ae038cb0
DJ
10031 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
10032
98bfdba5
PA
10033 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
10034 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
10035
10036 per_cu->cu->last_used = 0;
5afb4e99
DJ
10037 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10038
10039 if (pd == NULL && per_cu->load_all_dies == 0)
10040 {
10041 struct cleanup *back_to;
10042 struct partial_die_info comp_unit_die;
10043 struct abbrev_info *abbrev;
10044 unsigned int bytes_read;
10045 char *info_ptr;
10046
10047 per_cu->load_all_dies = 1;
10048
10049 /* Re-read the DIEs. */
10050 back_to = make_cleanup (null_cleanup, 0);
10051 if (per_cu->cu->dwarf2_abbrevs == NULL)
10052 {
10053 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 10054 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 10055 }
dce234bc 10056 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
10057 + per_cu->cu->header.offset
10058 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
10059 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10060 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
10061 per_cu->cu->objfile->obfd,
10062 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
10063 per_cu->cu);
10064 if (comp_unit_die.has_children)
93311388
DE
10065 load_partial_dies (per_cu->cu->objfile->obfd,
10066 dwarf2_per_objfile->info.buffer, info_ptr,
10067 0, per_cu->cu);
5afb4e99
DJ
10068 do_cleanups (back_to);
10069
10070 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10071 }
10072
348e048f
DE
10073 not_found:
10074
5afb4e99
DJ
10075 if (pd == NULL)
10076 internal_error (__FILE__, __LINE__,
3e43a32a
MS
10077 _("could not find partial DIE 0x%x "
10078 "in cache [from module %s]\n"),
5afb4e99
DJ
10079 offset, bfd_get_filename (cu->objfile->obfd));
10080 return pd;
72bf9492
DJ
10081}
10082
abc72ce4
DE
10083/* See if we can figure out if the class lives in a namespace. We do
10084 this by looking for a member function; its demangled name will
10085 contain namespace info, if there is any. */
10086
10087static void
10088guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10089 struct dwarf2_cu *cu)
10090{
10091 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10092 what template types look like, because the demangler
10093 frequently doesn't give the same name as the debug info. We
10094 could fix this by only using the demangled name to get the
10095 prefix (but see comment in read_structure_type). */
10096
10097 struct partial_die_info *real_pdi;
10098 struct partial_die_info *child_pdi;
10099
10100 /* If this DIE (this DIE's specification, if any) has a parent, then
10101 we should not do this. We'll prepend the parent's fully qualified
10102 name when we create the partial symbol. */
10103
10104 real_pdi = struct_pdi;
10105 while (real_pdi->has_specification)
10106 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10107
10108 if (real_pdi->die_parent != NULL)
10109 return;
10110
10111 for (child_pdi = struct_pdi->die_child;
10112 child_pdi != NULL;
10113 child_pdi = child_pdi->die_sibling)
10114 {
10115 if (child_pdi->tag == DW_TAG_subprogram
10116 && child_pdi->linkage_name != NULL)
10117 {
10118 char *actual_class_name
10119 = language_class_name_from_physname (cu->language_defn,
10120 child_pdi->linkage_name);
10121 if (actual_class_name != NULL)
10122 {
10123 struct_pdi->name
10124 = obsavestring (actual_class_name,
10125 strlen (actual_class_name),
10126 &cu->objfile->objfile_obstack);
10127 xfree (actual_class_name);
10128 }
10129 break;
10130 }
10131 }
10132}
10133
72bf9492
DJ
10134/* Adjust PART_DIE before generating a symbol for it. This function
10135 may set the is_external flag or change the DIE's name. */
10136
10137static void
10138fixup_partial_die (struct partial_die_info *part_die,
10139 struct dwarf2_cu *cu)
10140{
abc72ce4
DE
10141 /* Once we've fixed up a die, there's no point in doing so again.
10142 This also avoids a memory leak if we were to call
10143 guess_partial_die_structure_name multiple times. */
10144 if (part_die->fixup_called)
10145 return;
10146
72bf9492
DJ
10147 /* If we found a reference attribute and the DIE has no name, try
10148 to find a name in the referred to DIE. */
10149
10150 if (part_die->name == NULL && part_die->has_specification)
10151 {
10152 struct partial_die_info *spec_die;
72bf9492 10153
10b3939b 10154 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10155
10b3939b 10156 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10157
10158 if (spec_die->name)
10159 {
10160 part_die->name = spec_die->name;
10161
10162 /* Copy DW_AT_external attribute if it is set. */
10163 if (spec_die->is_external)
10164 part_die->is_external = spec_die->is_external;
10165 }
10166 }
10167
10168 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10169
10170 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10171 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10172
abc72ce4
DE
10173 /* If there is no parent die to provide a namespace, and there are
10174 children, see if we can determine the namespace from their linkage
10175 name.
10176 NOTE: We need to do this even if cu->has_namespace_info != 0.
10177 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10178 if (cu->language == language_cplus
8b70b953 10179 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10180 && part_die->die_parent == NULL
10181 && part_die->has_children
10182 && (part_die->tag == DW_TAG_class_type
10183 || part_die->tag == DW_TAG_structure_type
10184 || part_die->tag == DW_TAG_union_type))
10185 guess_partial_die_structure_name (part_die, cu);
10186
53832f31
TT
10187 /* GCC might emit a nameless struct or union that has a linkage
10188 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10189 if (part_die->name == NULL
96408a79
SA
10190 && (part_die->tag == DW_TAG_class_type
10191 || part_die->tag == DW_TAG_interface_type
10192 || part_die->tag == DW_TAG_structure_type
10193 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10194 && part_die->linkage_name != NULL)
10195 {
10196 char *demangled;
10197
10198 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10199 if (demangled)
10200 {
96408a79
SA
10201 const char *base;
10202
10203 /* Strip any leading namespaces/classes, keep only the base name.
10204 DW_AT_name for named DIEs does not contain the prefixes. */
10205 base = strrchr (demangled, ':');
10206 if (base && base > demangled && base[-1] == ':')
10207 base++;
10208 else
10209 base = demangled;
10210
10211 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10212 &cu->objfile->objfile_obstack);
10213 xfree (demangled);
10214 }
10215 }
10216
abc72ce4 10217 part_die->fixup_called = 1;
72bf9492
DJ
10218}
10219
a8329558 10220/* Read an attribute value described by an attribute form. */
c906108c 10221
fe1b8b76 10222static gdb_byte *
a8329558 10223read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10224 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10225 struct dwarf2_cu *cu)
c906108c 10226{
e7c27a73 10227 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10228 unsigned int bytes_read;
10229 struct dwarf_block *blk;
10230
a8329558
KW
10231 attr->form = form;
10232 switch (form)
c906108c 10233 {
c906108c 10234 case DW_FORM_ref_addr:
ae411497
TT
10235 if (cu->header.version == 2)
10236 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10237 else
3e43a32a
MS
10238 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10239 &cu->header, &bytes_read);
ae411497
TT
10240 info_ptr += bytes_read;
10241 break;
10242 case DW_FORM_addr:
e7c27a73 10243 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10244 info_ptr += bytes_read;
c906108c
SS
10245 break;
10246 case DW_FORM_block2:
7b5a2f43 10247 blk = dwarf_alloc_block (cu);
c906108c
SS
10248 blk->size = read_2_bytes (abfd, info_ptr);
10249 info_ptr += 2;
10250 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10251 info_ptr += blk->size;
10252 DW_BLOCK (attr) = blk;
10253 break;
10254 case DW_FORM_block4:
7b5a2f43 10255 blk = dwarf_alloc_block (cu);
c906108c
SS
10256 blk->size = read_4_bytes (abfd, info_ptr);
10257 info_ptr += 4;
10258 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10259 info_ptr += blk->size;
10260 DW_BLOCK (attr) = blk;
10261 break;
10262 case DW_FORM_data2:
10263 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10264 info_ptr += 2;
10265 break;
10266 case DW_FORM_data4:
10267 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10268 info_ptr += 4;
10269 break;
10270 case DW_FORM_data8:
10271 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10272 info_ptr += 8;
10273 break;
2dc7f7b3
TT
10274 case DW_FORM_sec_offset:
10275 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10276 info_ptr += bytes_read;
10277 break;
c906108c 10278 case DW_FORM_string:
9b1c24c8 10279 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10280 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10281 info_ptr += bytes_read;
10282 break;
4bdf3d34
JJ
10283 case DW_FORM_strp:
10284 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10285 &bytes_read);
8285870a 10286 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10287 info_ptr += bytes_read;
10288 break;
2dc7f7b3 10289 case DW_FORM_exprloc:
c906108c 10290 case DW_FORM_block:
7b5a2f43 10291 blk = dwarf_alloc_block (cu);
c906108c
SS
10292 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10293 info_ptr += bytes_read;
10294 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10295 info_ptr += blk->size;
10296 DW_BLOCK (attr) = blk;
10297 break;
10298 case DW_FORM_block1:
7b5a2f43 10299 blk = dwarf_alloc_block (cu);
c906108c
SS
10300 blk->size = read_1_byte (abfd, info_ptr);
10301 info_ptr += 1;
10302 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10303 info_ptr += blk->size;
10304 DW_BLOCK (attr) = blk;
10305 break;
10306 case DW_FORM_data1:
10307 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10308 info_ptr += 1;
10309 break;
10310 case DW_FORM_flag:
10311 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10312 info_ptr += 1;
10313 break;
2dc7f7b3
TT
10314 case DW_FORM_flag_present:
10315 DW_UNSND (attr) = 1;
10316 break;
c906108c
SS
10317 case DW_FORM_sdata:
10318 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10319 info_ptr += bytes_read;
10320 break;
10321 case DW_FORM_udata:
10322 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10323 info_ptr += bytes_read;
10324 break;
10325 case DW_FORM_ref1:
10b3939b 10326 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
10327 info_ptr += 1;
10328 break;
10329 case DW_FORM_ref2:
10b3939b 10330 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
10331 info_ptr += 2;
10332 break;
10333 case DW_FORM_ref4:
10b3939b 10334 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
10335 info_ptr += 4;
10336 break;
613e1657 10337 case DW_FORM_ref8:
10b3939b 10338 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
10339 info_ptr += 8;
10340 break;
55f1336d 10341 case DW_FORM_ref_sig8:
348e048f
DE
10342 /* Convert the signature to something we can record in DW_UNSND
10343 for later lookup.
10344 NOTE: This is NULL if the type wasn't found. */
10345 DW_SIGNATURED_TYPE (attr) =
10346 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10347 info_ptr += 8;
10348 break;
c906108c 10349 case DW_FORM_ref_udata:
10b3939b
DJ
10350 DW_ADDR (attr) = (cu->header.offset
10351 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10352 info_ptr += bytes_read;
10353 break;
c906108c 10354 case DW_FORM_indirect:
a8329558
KW
10355 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10356 info_ptr += bytes_read;
e7c27a73 10357 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10358 break;
c906108c 10359 default:
8a3fe4f8 10360 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10361 dwarf_form_name (form),
10362 bfd_get_filename (abfd));
c906108c 10363 }
28e94949
JB
10364
10365 /* We have seen instances where the compiler tried to emit a byte
10366 size attribute of -1 which ended up being encoded as an unsigned
10367 0xffffffff. Although 0xffffffff is technically a valid size value,
10368 an object of this size seems pretty unlikely so we can relatively
10369 safely treat these cases as if the size attribute was invalid and
10370 treat them as zero by default. */
10371 if (attr->name == DW_AT_byte_size
10372 && form == DW_FORM_data4
10373 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10374 {
10375 complaint
10376 (&symfile_complaints,
43bbcdc2
PH
10377 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10378 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10379 DW_UNSND (attr) = 0;
10380 }
28e94949 10381
c906108c
SS
10382 return info_ptr;
10383}
10384
a8329558
KW
10385/* Read an attribute described by an abbreviated attribute. */
10386
fe1b8b76 10387static gdb_byte *
a8329558 10388read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10389 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10390{
10391 attr->name = abbrev->name;
e7c27a73 10392 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10393}
10394
0963b4bd 10395/* Read dwarf information from a buffer. */
c906108c
SS
10396
10397static unsigned int
fe1b8b76 10398read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10399{
fe1b8b76 10400 return bfd_get_8 (abfd, buf);
c906108c
SS
10401}
10402
10403static int
fe1b8b76 10404read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10405{
fe1b8b76 10406 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10407}
10408
10409static unsigned int
fe1b8b76 10410read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10411{
fe1b8b76 10412 return bfd_get_16 (abfd, buf);
c906108c
SS
10413}
10414
21ae7a4d
JK
10415static int
10416read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10417{
10418 return bfd_get_signed_16 (abfd, buf);
10419}
10420
c906108c 10421static unsigned int
fe1b8b76 10422read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10423{
fe1b8b76 10424 return bfd_get_32 (abfd, buf);
c906108c
SS
10425}
10426
21ae7a4d
JK
10427static int
10428read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10429{
10430 return bfd_get_signed_32 (abfd, buf);
10431}
10432
93311388 10433static ULONGEST
fe1b8b76 10434read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10435{
fe1b8b76 10436 return bfd_get_64 (abfd, buf);
c906108c
SS
10437}
10438
10439static CORE_ADDR
fe1b8b76 10440read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10441 unsigned int *bytes_read)
c906108c 10442{
e7c27a73 10443 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10444 CORE_ADDR retval = 0;
10445
107d2387 10446 if (cu_header->signed_addr_p)
c906108c 10447 {
107d2387
AC
10448 switch (cu_header->addr_size)
10449 {
10450 case 2:
fe1b8b76 10451 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10452 break;
10453 case 4:
fe1b8b76 10454 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10455 break;
10456 case 8:
fe1b8b76 10457 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10458 break;
10459 default:
8e65ff28 10460 internal_error (__FILE__, __LINE__,
e2e0b3e5 10461 _("read_address: bad switch, signed [in module %s]"),
659b0389 10462 bfd_get_filename (abfd));
107d2387
AC
10463 }
10464 }
10465 else
10466 {
10467 switch (cu_header->addr_size)
10468 {
10469 case 2:
fe1b8b76 10470 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10471 break;
10472 case 4:
fe1b8b76 10473 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10474 break;
10475 case 8:
fe1b8b76 10476 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10477 break;
10478 default:
8e65ff28 10479 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10480 _("read_address: bad switch, "
10481 "unsigned [in module %s]"),
659b0389 10482 bfd_get_filename (abfd));
107d2387 10483 }
c906108c 10484 }
64367e0a 10485
107d2387
AC
10486 *bytes_read = cu_header->addr_size;
10487 return retval;
c906108c
SS
10488}
10489
f7ef9339 10490/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10491 specification allows the initial length to take up either 4 bytes
10492 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10493 bytes describe the length and all offsets will be 8 bytes in length
10494 instead of 4.
10495
f7ef9339
KB
10496 An older, non-standard 64-bit format is also handled by this
10497 function. The older format in question stores the initial length
10498 as an 8-byte quantity without an escape value. Lengths greater
10499 than 2^32 aren't very common which means that the initial 4 bytes
10500 is almost always zero. Since a length value of zero doesn't make
10501 sense for the 32-bit format, this initial zero can be considered to
10502 be an escape value which indicates the presence of the older 64-bit
10503 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10504 greater than 4GB. If it becomes necessary to handle lengths
10505 somewhat larger than 4GB, we could allow other small values (such
10506 as the non-sensical values of 1, 2, and 3) to also be used as
10507 escape values indicating the presence of the old format.
f7ef9339 10508
917c78fc
MK
10509 The value returned via bytes_read should be used to increment the
10510 relevant pointer after calling read_initial_length().
c764a876 10511
613e1657
KB
10512 [ Note: read_initial_length() and read_offset() are based on the
10513 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10514 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10515 from:
10516
f7ef9339 10517 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10518
613e1657
KB
10519 This document is only a draft and is subject to change. (So beware.)
10520
f7ef9339 10521 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10522 determined empirically by examining 64-bit ELF files produced by
10523 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10524
10525 - Kevin, July 16, 2002
613e1657
KB
10526 ] */
10527
10528static LONGEST
c764a876 10529read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10530{
fe1b8b76 10531 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10532
dd373385 10533 if (length == 0xffffffff)
613e1657 10534 {
fe1b8b76 10535 length = bfd_get_64 (abfd, buf + 4);
613e1657 10536 *bytes_read = 12;
613e1657 10537 }
dd373385 10538 else if (length == 0)
f7ef9339 10539 {
dd373385 10540 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10541 length = bfd_get_64 (abfd, buf);
f7ef9339 10542 *bytes_read = 8;
f7ef9339 10543 }
613e1657
KB
10544 else
10545 {
10546 *bytes_read = 4;
613e1657
KB
10547 }
10548
c764a876
DE
10549 return length;
10550}
dd373385 10551
c764a876
DE
10552/* Cover function for read_initial_length.
10553 Returns the length of the object at BUF, and stores the size of the
10554 initial length in *BYTES_READ and stores the size that offsets will be in
10555 *OFFSET_SIZE.
10556 If the initial length size is not equivalent to that specified in
10557 CU_HEADER then issue a complaint.
10558 This is useful when reading non-comp-unit headers. */
dd373385 10559
c764a876
DE
10560static LONGEST
10561read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10562 const struct comp_unit_head *cu_header,
10563 unsigned int *bytes_read,
10564 unsigned int *offset_size)
10565{
10566 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10567
10568 gdb_assert (cu_header->initial_length_size == 4
10569 || cu_header->initial_length_size == 8
10570 || cu_header->initial_length_size == 12);
10571
10572 if (cu_header->initial_length_size != *bytes_read)
10573 complaint (&symfile_complaints,
10574 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10575
c764a876 10576 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10577 return length;
613e1657
KB
10578}
10579
10580/* Read an offset from the data stream. The size of the offset is
917c78fc 10581 given by cu_header->offset_size. */
613e1657
KB
10582
10583static LONGEST
fe1b8b76 10584read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10585 unsigned int *bytes_read)
c764a876
DE
10586{
10587 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10588
c764a876
DE
10589 *bytes_read = cu_header->offset_size;
10590 return offset;
10591}
10592
10593/* Read an offset from the data stream. */
10594
10595static LONGEST
10596read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10597{
10598 LONGEST retval = 0;
10599
c764a876 10600 switch (offset_size)
613e1657
KB
10601 {
10602 case 4:
fe1b8b76 10603 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10604 break;
10605 case 8:
fe1b8b76 10606 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10607 break;
10608 default:
8e65ff28 10609 internal_error (__FILE__, __LINE__,
c764a876 10610 _("read_offset_1: bad switch [in module %s]"),
659b0389 10611 bfd_get_filename (abfd));
613e1657
KB
10612 }
10613
917c78fc 10614 return retval;
613e1657
KB
10615}
10616
fe1b8b76
JB
10617static gdb_byte *
10618read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10619{
10620 /* If the size of a host char is 8 bits, we can return a pointer
10621 to the buffer, otherwise we have to copy the data to a buffer
10622 allocated on the temporary obstack. */
4bdf3d34 10623 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10624 return buf;
c906108c
SS
10625}
10626
10627static char *
9b1c24c8 10628read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10629{
10630 /* If the size of a host char is 8 bits, we can return a pointer
10631 to the string, otherwise we have to copy the string to a buffer
10632 allocated on the temporary obstack. */
4bdf3d34 10633 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10634 if (*buf == '\0')
10635 {
10636 *bytes_read_ptr = 1;
10637 return NULL;
10638 }
fe1b8b76
JB
10639 *bytes_read_ptr = strlen ((char *) buf) + 1;
10640 return (char *) buf;
4bdf3d34
JJ
10641}
10642
10643static char *
cf2c3c16 10644read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10645{
be391dca 10646 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10647 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10648 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10649 bfd_get_filename (abfd));
dce234bc 10650 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10651 error (_("DW_FORM_strp pointing outside of "
10652 ".debug_str section [in module %s]"),
10653 bfd_get_filename (abfd));
4bdf3d34 10654 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10655 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10656 return NULL;
dce234bc 10657 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10658}
10659
cf2c3c16
TT
10660static char *
10661read_indirect_string (bfd *abfd, gdb_byte *buf,
10662 const struct comp_unit_head *cu_header,
10663 unsigned int *bytes_read_ptr)
10664{
10665 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10666
10667 return read_indirect_string_at_offset (abfd, str_offset);
10668}
10669
ce5d95e1 10670static unsigned long
fe1b8b76 10671read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10672{
ce5d95e1
JB
10673 unsigned long result;
10674 unsigned int num_read;
c906108c
SS
10675 int i, shift;
10676 unsigned char byte;
10677
10678 result = 0;
10679 shift = 0;
10680 num_read = 0;
10681 i = 0;
10682 while (1)
10683 {
fe1b8b76 10684 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10685 buf++;
10686 num_read++;
ce5d95e1 10687 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10688 if ((byte & 128) == 0)
10689 {
10690 break;
10691 }
10692 shift += 7;
10693 }
10694 *bytes_read_ptr = num_read;
10695 return result;
10696}
10697
ce5d95e1 10698static long
fe1b8b76 10699read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10700{
ce5d95e1 10701 long result;
77e0b926 10702 int i, shift, num_read;
c906108c
SS
10703 unsigned char byte;
10704
10705 result = 0;
10706 shift = 0;
c906108c
SS
10707 num_read = 0;
10708 i = 0;
10709 while (1)
10710 {
fe1b8b76 10711 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10712 buf++;
10713 num_read++;
ce5d95e1 10714 result |= ((long)(byte & 127) << shift);
c906108c
SS
10715 shift += 7;
10716 if ((byte & 128) == 0)
10717 {
10718 break;
10719 }
10720 }
77e0b926
DJ
10721 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10722 result |= -(((long)1) << shift);
c906108c
SS
10723 *bytes_read_ptr = num_read;
10724 return result;
10725}
10726
4bb7a0a7
DJ
10727/* Return a pointer to just past the end of an LEB128 number in BUF. */
10728
fe1b8b76
JB
10729static gdb_byte *
10730skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10731{
10732 int byte;
10733
10734 while (1)
10735 {
fe1b8b76 10736 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10737 buf++;
10738 if ((byte & 128) == 0)
10739 return buf;
10740 }
10741}
10742
c906108c 10743static void
e142c38c 10744set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10745{
10746 switch (lang)
10747 {
10748 case DW_LANG_C89:
76bee0cc 10749 case DW_LANG_C99:
c906108c 10750 case DW_LANG_C:
e142c38c 10751 cu->language = language_c;
c906108c
SS
10752 break;
10753 case DW_LANG_C_plus_plus:
e142c38c 10754 cu->language = language_cplus;
c906108c 10755 break;
6aecb9c2
JB
10756 case DW_LANG_D:
10757 cu->language = language_d;
10758 break;
c906108c
SS
10759 case DW_LANG_Fortran77:
10760 case DW_LANG_Fortran90:
b21b22e0 10761 case DW_LANG_Fortran95:
e142c38c 10762 cu->language = language_fortran;
c906108c
SS
10763 break;
10764 case DW_LANG_Mips_Assembler:
e142c38c 10765 cu->language = language_asm;
c906108c 10766 break;
bebd888e 10767 case DW_LANG_Java:
e142c38c 10768 cu->language = language_java;
bebd888e 10769 break;
c906108c 10770 case DW_LANG_Ada83:
8aaf0b47 10771 case DW_LANG_Ada95:
bc5f45f8
JB
10772 cu->language = language_ada;
10773 break;
72019c9c
GM
10774 case DW_LANG_Modula2:
10775 cu->language = language_m2;
10776 break;
fe8e67fd
PM
10777 case DW_LANG_Pascal83:
10778 cu->language = language_pascal;
10779 break;
22566fbd
DJ
10780 case DW_LANG_ObjC:
10781 cu->language = language_objc;
10782 break;
c906108c
SS
10783 case DW_LANG_Cobol74:
10784 case DW_LANG_Cobol85:
c906108c 10785 default:
e142c38c 10786 cu->language = language_minimal;
c906108c
SS
10787 break;
10788 }
e142c38c 10789 cu->language_defn = language_def (cu->language);
c906108c
SS
10790}
10791
10792/* Return the named attribute or NULL if not there. */
10793
10794static struct attribute *
e142c38c 10795dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10796{
10797 unsigned int i;
10798 struct attribute *spec = NULL;
10799
10800 for (i = 0; i < die->num_attrs; ++i)
10801 {
10802 if (die->attrs[i].name == name)
10b3939b 10803 return &die->attrs[i];
c906108c
SS
10804 if (die->attrs[i].name == DW_AT_specification
10805 || die->attrs[i].name == DW_AT_abstract_origin)
10806 spec = &die->attrs[i];
10807 }
c906108c 10808
10b3939b 10809 if (spec)
f2f0e013
DJ
10810 {
10811 die = follow_die_ref (die, spec, &cu);
10812 return dwarf2_attr (die, name, cu);
10813 }
c5aa993b 10814
c906108c
SS
10815 return NULL;
10816}
10817
348e048f
DE
10818/* Return the named attribute or NULL if not there,
10819 but do not follow DW_AT_specification, etc.
10820 This is for use in contexts where we're reading .debug_types dies.
10821 Following DW_AT_specification, DW_AT_abstract_origin will take us
10822 back up the chain, and we want to go down. */
10823
10824static struct attribute *
10825dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10826 struct dwarf2_cu *cu)
10827{
10828 unsigned int i;
10829
10830 for (i = 0; i < die->num_attrs; ++i)
10831 if (die->attrs[i].name == name)
10832 return &die->attrs[i];
10833
10834 return NULL;
10835}
10836
05cf31d1
JB
10837/* Return non-zero iff the attribute NAME is defined for the given DIE,
10838 and holds a non-zero value. This function should only be used for
2dc7f7b3 10839 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10840
10841static int
10842dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10843{
10844 struct attribute *attr = dwarf2_attr (die, name, cu);
10845
10846 return (attr && DW_UNSND (attr));
10847}
10848
3ca72b44 10849static int
e142c38c 10850die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10851{
05cf31d1
JB
10852 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10853 which value is non-zero. However, we have to be careful with
10854 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10855 (via dwarf2_flag_true_p) follows this attribute. So we may
10856 end up accidently finding a declaration attribute that belongs
10857 to a different DIE referenced by the specification attribute,
10858 even though the given DIE does not have a declaration attribute. */
10859 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10860 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10861}
10862
63d06c5c 10863/* Return the die giving the specification for DIE, if there is
f2f0e013 10864 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10865 containing the return value on output. If there is no
10866 specification, but there is an abstract origin, that is
10867 returned. */
63d06c5c
DC
10868
10869static struct die_info *
f2f0e013 10870die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10871{
f2f0e013
DJ
10872 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10873 *spec_cu);
63d06c5c 10874
edb3359d
DJ
10875 if (spec_attr == NULL)
10876 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10877
63d06c5c
DC
10878 if (spec_attr == NULL)
10879 return NULL;
10880 else
f2f0e013 10881 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10882}
c906108c 10883
debd256d 10884/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10885 refers to.
10886 NOTE: This is also used as a "cleanup" function. */
10887
debd256d
JB
10888static void
10889free_line_header (struct line_header *lh)
10890{
10891 if (lh->standard_opcode_lengths)
a8bc7b56 10892 xfree (lh->standard_opcode_lengths);
debd256d
JB
10893
10894 /* Remember that all the lh->file_names[i].name pointers are
10895 pointers into debug_line_buffer, and don't need to be freed. */
10896 if (lh->file_names)
a8bc7b56 10897 xfree (lh->file_names);
debd256d
JB
10898
10899 /* Similarly for the include directory names. */
10900 if (lh->include_dirs)
a8bc7b56 10901 xfree (lh->include_dirs);
debd256d 10902
a8bc7b56 10903 xfree (lh);
debd256d
JB
10904}
10905
debd256d 10906/* Add an entry to LH's include directory table. */
ae2de4f8 10907
debd256d
JB
10908static void
10909add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10910{
debd256d
JB
10911 /* Grow the array if necessary. */
10912 if (lh->include_dirs_size == 0)
c5aa993b 10913 {
debd256d
JB
10914 lh->include_dirs_size = 1; /* for testing */
10915 lh->include_dirs = xmalloc (lh->include_dirs_size
10916 * sizeof (*lh->include_dirs));
10917 }
10918 else if (lh->num_include_dirs >= lh->include_dirs_size)
10919 {
10920 lh->include_dirs_size *= 2;
10921 lh->include_dirs = xrealloc (lh->include_dirs,
10922 (lh->include_dirs_size
10923 * sizeof (*lh->include_dirs)));
c5aa993b 10924 }
c906108c 10925
debd256d
JB
10926 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10927}
6e70227d 10928
debd256d 10929/* Add an entry to LH's file name table. */
ae2de4f8 10930
debd256d
JB
10931static void
10932add_file_name (struct line_header *lh,
10933 char *name,
10934 unsigned int dir_index,
10935 unsigned int mod_time,
10936 unsigned int length)
10937{
10938 struct file_entry *fe;
10939
10940 /* Grow the array if necessary. */
10941 if (lh->file_names_size == 0)
10942 {
10943 lh->file_names_size = 1; /* for testing */
10944 lh->file_names = xmalloc (lh->file_names_size
10945 * sizeof (*lh->file_names));
10946 }
10947 else if (lh->num_file_names >= lh->file_names_size)
10948 {
10949 lh->file_names_size *= 2;
10950 lh->file_names = xrealloc (lh->file_names,
10951 (lh->file_names_size
10952 * sizeof (*lh->file_names)));
10953 }
10954
10955 fe = &lh->file_names[lh->num_file_names++];
10956 fe->name = name;
10957 fe->dir_index = dir_index;
10958 fe->mod_time = mod_time;
10959 fe->length = length;
aaa75496 10960 fe->included_p = 0;
cb1df416 10961 fe->symtab = NULL;
debd256d 10962}
6e70227d 10963
debd256d 10964/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10965 .debug_line, according to the endianness of ABFD. Return a pointer
10966 to a struct line_header, allocated using xmalloc.
debd256d
JB
10967
10968 NOTE: the strings in the include directory and file name tables of
10969 the returned object point into debug_line_buffer, and must not be
10970 freed. */
ae2de4f8 10971
debd256d
JB
10972static struct line_header *
10973dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10974 struct dwarf2_cu *cu)
debd256d
JB
10975{
10976 struct cleanup *back_to;
10977 struct line_header *lh;
fe1b8b76 10978 gdb_byte *line_ptr;
c764a876 10979 unsigned int bytes_read, offset_size;
debd256d
JB
10980 int i;
10981 char *cur_dir, *cur_file;
10982
be391dca 10983 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10984 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10985 {
e2e0b3e5 10986 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10987 return 0;
10988 }
10989
a738430d
MK
10990 /* Make sure that at least there's room for the total_length field.
10991 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10992 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10993 {
4d3c2250 10994 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10995 return 0;
10996 }
10997
10998 lh = xmalloc (sizeof (*lh));
10999 memset (lh, 0, sizeof (*lh));
11000 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
11001 (void *) lh);
11002
dce234bc 11003 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 11004
a738430d 11005 /* Read in the header. */
6e70227d 11006 lh->total_length =
c764a876
DE
11007 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
11008 &bytes_read, &offset_size);
debd256d 11009 line_ptr += bytes_read;
dce234bc
PP
11010 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
11011 + dwarf2_per_objfile->line.size))
debd256d 11012 {
4d3c2250 11013 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
11014 return 0;
11015 }
11016 lh->statement_program_end = line_ptr + lh->total_length;
11017 lh->version = read_2_bytes (abfd, line_ptr);
11018 line_ptr += 2;
c764a876
DE
11019 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
11020 line_ptr += offset_size;
debd256d
JB
11021 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
11022 line_ptr += 1;
2dc7f7b3
TT
11023 if (lh->version >= 4)
11024 {
11025 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
11026 line_ptr += 1;
11027 }
11028 else
11029 lh->maximum_ops_per_instruction = 1;
11030
11031 if (lh->maximum_ops_per_instruction == 0)
11032 {
11033 lh->maximum_ops_per_instruction = 1;
11034 complaint (&symfile_complaints,
3e43a32a
MS
11035 _("invalid maximum_ops_per_instruction "
11036 "in `.debug_line' section"));
2dc7f7b3
TT
11037 }
11038
debd256d
JB
11039 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11040 line_ptr += 1;
11041 lh->line_base = read_1_signed_byte (abfd, line_ptr);
11042 line_ptr += 1;
11043 lh->line_range = read_1_byte (abfd, line_ptr);
11044 line_ptr += 1;
11045 lh->opcode_base = read_1_byte (abfd, line_ptr);
11046 line_ptr += 1;
11047 lh->standard_opcode_lengths
fe1b8b76 11048 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
11049
11050 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11051 for (i = 1; i < lh->opcode_base; ++i)
11052 {
11053 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11054 line_ptr += 1;
11055 }
11056
a738430d 11057 /* Read directory table. */
9b1c24c8 11058 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11059 {
11060 line_ptr += bytes_read;
11061 add_include_dir (lh, cur_dir);
11062 }
11063 line_ptr += bytes_read;
11064
a738430d 11065 /* Read file name table. */
9b1c24c8 11066 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11067 {
11068 unsigned int dir_index, mod_time, length;
11069
11070 line_ptr += bytes_read;
11071 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11072 line_ptr += bytes_read;
11073 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11074 line_ptr += bytes_read;
11075 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11076 line_ptr += bytes_read;
11077
11078 add_file_name (lh, cur_file, dir_index, mod_time, length);
11079 }
11080 line_ptr += bytes_read;
6e70227d 11081 lh->statement_program_start = line_ptr;
debd256d 11082
dce234bc
PP
11083 if (line_ptr > (dwarf2_per_objfile->line.buffer
11084 + dwarf2_per_objfile->line.size))
4d3c2250 11085 complaint (&symfile_complaints,
3e43a32a
MS
11086 _("line number info header doesn't "
11087 "fit in `.debug_line' section"));
debd256d
JB
11088
11089 discard_cleanups (back_to);
11090 return lh;
11091}
c906108c 11092
5fb290d7
DJ
11093/* This function exists to work around a bug in certain compilers
11094 (particularly GCC 2.95), in which the first line number marker of a
11095 function does not show up until after the prologue, right before
11096 the second line number marker. This function shifts ADDRESS down
11097 to the beginning of the function if necessary, and is called on
11098 addresses passed to record_line. */
11099
11100static CORE_ADDR
e142c38c 11101check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
11102{
11103 struct function_range *fn;
11104
11105 /* Find the function_range containing address. */
e142c38c 11106 if (!cu->first_fn)
5fb290d7
DJ
11107 return address;
11108
e142c38c
DJ
11109 if (!cu->cached_fn)
11110 cu->cached_fn = cu->first_fn;
5fb290d7 11111
e142c38c 11112 fn = cu->cached_fn;
5fb290d7
DJ
11113 while (fn)
11114 if (fn->lowpc <= address && fn->highpc > address)
11115 goto found;
11116 else
11117 fn = fn->next;
11118
e142c38c
DJ
11119 fn = cu->first_fn;
11120 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
11121 if (fn->lowpc <= address && fn->highpc > address)
11122 goto found;
11123 else
11124 fn = fn->next;
11125
11126 return address;
11127
11128 found:
11129 if (fn->seen_line)
11130 return address;
11131 if (address != fn->lowpc)
4d3c2250 11132 complaint (&symfile_complaints,
e2e0b3e5 11133 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 11134 (unsigned long) address, fn->name);
5fb290d7
DJ
11135 fn->seen_line = 1;
11136 return fn->lowpc;
11137}
11138
c6da4cef
DE
11139/* Subroutine of dwarf_decode_lines to simplify it.
11140 Return the file name of the psymtab for included file FILE_INDEX
11141 in line header LH of PST.
11142 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11143 If space for the result is malloc'd, it will be freed by a cleanup.
11144 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11145
11146static char *
11147psymtab_include_file_name (const struct line_header *lh, int file_index,
11148 const struct partial_symtab *pst,
11149 const char *comp_dir)
11150{
11151 const struct file_entry fe = lh->file_names [file_index];
11152 char *include_name = fe.name;
11153 char *include_name_to_compare = include_name;
11154 char *dir_name = NULL;
72b9f47f
TT
11155 const char *pst_filename;
11156 char *copied_name = NULL;
c6da4cef
DE
11157 int file_is_pst;
11158
11159 if (fe.dir_index)
11160 dir_name = lh->include_dirs[fe.dir_index - 1];
11161
11162 if (!IS_ABSOLUTE_PATH (include_name)
11163 && (dir_name != NULL || comp_dir != NULL))
11164 {
11165 /* Avoid creating a duplicate psymtab for PST.
11166 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11167 Before we do the comparison, however, we need to account
11168 for DIR_NAME and COMP_DIR.
11169 First prepend dir_name (if non-NULL). If we still don't
11170 have an absolute path prepend comp_dir (if non-NULL).
11171 However, the directory we record in the include-file's
11172 psymtab does not contain COMP_DIR (to match the
11173 corresponding symtab(s)).
11174
11175 Example:
11176
11177 bash$ cd /tmp
11178 bash$ gcc -g ./hello.c
11179 include_name = "hello.c"
11180 dir_name = "."
11181 DW_AT_comp_dir = comp_dir = "/tmp"
11182 DW_AT_name = "./hello.c" */
11183
11184 if (dir_name != NULL)
11185 {
11186 include_name = concat (dir_name, SLASH_STRING,
11187 include_name, (char *)NULL);
11188 include_name_to_compare = include_name;
11189 make_cleanup (xfree, include_name);
11190 }
11191 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11192 {
11193 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11194 include_name, (char *)NULL);
11195 }
11196 }
11197
11198 pst_filename = pst->filename;
11199 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11200 {
72b9f47f
TT
11201 copied_name = concat (pst->dirname, SLASH_STRING,
11202 pst_filename, (char *)NULL);
11203 pst_filename = copied_name;
c6da4cef
DE
11204 }
11205
1e3fad37 11206 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11207
11208 if (include_name_to_compare != include_name)
11209 xfree (include_name_to_compare);
72b9f47f
TT
11210 if (copied_name != NULL)
11211 xfree (copied_name);
c6da4cef
DE
11212
11213 if (file_is_pst)
11214 return NULL;
11215 return include_name;
11216}
11217
c91513d8
PP
11218/* Ignore this record_line request. */
11219
11220static void
11221noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11222{
11223 return;
11224}
11225
aaa75496
JB
11226/* Decode the Line Number Program (LNP) for the given line_header
11227 structure and CU. The actual information extracted and the type
11228 of structures created from the LNP depends on the value of PST.
11229
11230 1. If PST is NULL, then this procedure uses the data from the program
11231 to create all necessary symbol tables, and their linetables.
6e70227d 11232
aaa75496
JB
11233 2. If PST is not NULL, this procedure reads the program to determine
11234 the list of files included by the unit represented by PST, and
c6da4cef
DE
11235 builds all the associated partial symbol tables.
11236
11237 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11238 It is used for relative paths in the line table.
11239 NOTE: When processing partial symtabs (pst != NULL),
11240 comp_dir == pst->dirname.
11241
11242 NOTE: It is important that psymtabs have the same file name (via strcmp)
11243 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11244 symtab we don't use it in the name of the psymtabs we create.
11245 E.g. expand_line_sal requires this when finding psymtabs to expand.
11246 A good testcase for this is mb-inline.exp. */
debd256d 11247
c906108c 11248static void
72b9f47f 11249dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 11250 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11251{
a8c50c1f 11252 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11253 gdb_byte *line_end;
a8c50c1f 11254 unsigned int bytes_read, extended_len;
c906108c 11255 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11256 CORE_ADDR baseaddr;
11257 struct objfile *objfile = cu->objfile;
fbf65064 11258 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11259 const int decode_for_pst_p = (pst != NULL);
cb1df416 11260 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
11261 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11262 = record_line;
e142c38c
DJ
11263
11264 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11265
debd256d
JB
11266 line_ptr = lh->statement_program_start;
11267 line_end = lh->statement_program_end;
c906108c
SS
11268
11269 /* Read the statement sequences until there's nothing left. */
11270 while (line_ptr < line_end)
11271 {
11272 /* state machine registers */
11273 CORE_ADDR address = 0;
11274 unsigned int file = 1;
11275 unsigned int line = 1;
11276 unsigned int column = 0;
debd256d 11277 int is_stmt = lh->default_is_stmt;
c906108c
SS
11278 int basic_block = 0;
11279 int end_sequence = 0;
fbf65064 11280 CORE_ADDR addr;
2dc7f7b3 11281 unsigned char op_index = 0;
c906108c 11282
aaa75496 11283 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11284 {
aaa75496 11285 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11286 /* lh->include_dirs and lh->file_names are 0-based, but the
11287 directory and file name numbers in the statement program
11288 are 1-based. */
11289 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11290 char *dir = NULL;
a738430d 11291
debd256d
JB
11292 if (fe->dir_index)
11293 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11294
11295 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11296 }
11297
a738430d 11298 /* Decode the table. */
c5aa993b 11299 while (!end_sequence)
c906108c
SS
11300 {
11301 op_code = read_1_byte (abfd, line_ptr);
11302 line_ptr += 1;
59205f5a
JB
11303 if (line_ptr > line_end)
11304 {
11305 dwarf2_debug_line_missing_end_sequence_complaint ();
11306 break;
11307 }
9aa1fe7e 11308
debd256d 11309 if (op_code >= lh->opcode_base)
6e70227d 11310 {
a738430d 11311 /* Special operand. */
debd256d 11312 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11313 address += (((op_index + (adj_opcode / lh->line_range))
11314 / lh->maximum_ops_per_instruction)
11315 * lh->minimum_instruction_length);
11316 op_index = ((op_index + (adj_opcode / lh->line_range))
11317 % lh->maximum_ops_per_instruction);
debd256d 11318 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11319 if (lh->num_file_names < file || file == 0)
25e43795 11320 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11321 /* For now we ignore lines not starting on an
11322 instruction boundary. */
11323 else if (op_index == 0)
25e43795
DJ
11324 {
11325 lh->file_names[file - 1].included_p = 1;
ca5f395d 11326 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11327 {
11328 if (last_subfile != current_subfile)
11329 {
11330 addr = gdbarch_addr_bits_remove (gdbarch, address);
11331 if (last_subfile)
c91513d8 11332 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11333 last_subfile = current_subfile;
11334 }
25e43795 11335 /* Append row to matrix using current values. */
fbf65064
UW
11336 addr = check_cu_functions (address, cu);
11337 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 11338 (*p_record_line) (current_subfile, line, addr);
366da635 11339 }
25e43795 11340 }
ca5f395d 11341 basic_block = 0;
9aa1fe7e
GK
11342 }
11343 else switch (op_code)
c906108c
SS
11344 {
11345 case DW_LNS_extended_op:
3e43a32a
MS
11346 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11347 &bytes_read);
473b7be6 11348 line_ptr += bytes_read;
a8c50c1f 11349 extended_end = line_ptr + extended_len;
c906108c
SS
11350 extended_op = read_1_byte (abfd, line_ptr);
11351 line_ptr += 1;
11352 switch (extended_op)
11353 {
11354 case DW_LNE_end_sequence:
c91513d8 11355 p_record_line = record_line;
c906108c 11356 end_sequence = 1;
c906108c
SS
11357 break;
11358 case DW_LNE_set_address:
e7c27a73 11359 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11360
11361 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11362 {
11363 /* This line table is for a function which has been
11364 GCd by the linker. Ignore it. PR gdb/12528 */
11365
11366 long line_offset
11367 = line_ptr - dwarf2_per_objfile->line.buffer;
11368
11369 complaint (&symfile_complaints,
11370 _(".debug_line address at offset 0x%lx is 0 "
11371 "[in module %s]"),
11372 line_offset, cu->objfile->name);
11373 p_record_line = noop_record_line;
11374 }
11375
2dc7f7b3 11376 op_index = 0;
107d2387
AC
11377 line_ptr += bytes_read;
11378 address += baseaddr;
c906108c
SS
11379 break;
11380 case DW_LNE_define_file:
debd256d
JB
11381 {
11382 char *cur_file;
11383 unsigned int dir_index, mod_time, length;
6e70227d 11384
3e43a32a
MS
11385 cur_file = read_direct_string (abfd, line_ptr,
11386 &bytes_read);
debd256d
JB
11387 line_ptr += bytes_read;
11388 dir_index =
11389 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11390 line_ptr += bytes_read;
11391 mod_time =
11392 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11393 line_ptr += bytes_read;
11394 length =
11395 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11396 line_ptr += bytes_read;
11397 add_file_name (lh, cur_file, dir_index, mod_time, length);
11398 }
c906108c 11399 break;
d0c6ba3d
CC
11400 case DW_LNE_set_discriminator:
11401 /* The discriminator is not interesting to the debugger;
11402 just ignore it. */
11403 line_ptr = extended_end;
11404 break;
c906108c 11405 default:
4d3c2250 11406 complaint (&symfile_complaints,
e2e0b3e5 11407 _("mangled .debug_line section"));
debd256d 11408 return;
c906108c 11409 }
a8c50c1f
DJ
11410 /* Make sure that we parsed the extended op correctly. If e.g.
11411 we expected a different address size than the producer used,
11412 we may have read the wrong number of bytes. */
11413 if (line_ptr != extended_end)
11414 {
11415 complaint (&symfile_complaints,
11416 _("mangled .debug_line section"));
11417 return;
11418 }
c906108c
SS
11419 break;
11420 case DW_LNS_copy:
59205f5a 11421 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11422 dwarf2_debug_line_missing_file_complaint ();
11423 else
366da635 11424 {
25e43795 11425 lh->file_names[file - 1].included_p = 1;
ca5f395d 11426 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11427 {
11428 if (last_subfile != current_subfile)
11429 {
11430 addr = gdbarch_addr_bits_remove (gdbarch, address);
11431 if (last_subfile)
c91513d8 11432 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11433 last_subfile = current_subfile;
11434 }
11435 addr = check_cu_functions (address, cu);
11436 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 11437 (*p_record_line) (current_subfile, line, addr);
fbf65064 11438 }
366da635 11439 }
c906108c
SS
11440 basic_block = 0;
11441 break;
11442 case DW_LNS_advance_pc:
2dc7f7b3
TT
11443 {
11444 CORE_ADDR adjust
11445 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11446
11447 address += (((op_index + adjust)
11448 / lh->maximum_ops_per_instruction)
11449 * lh->minimum_instruction_length);
11450 op_index = ((op_index + adjust)
11451 % lh->maximum_ops_per_instruction);
11452 line_ptr += bytes_read;
11453 }
c906108c
SS
11454 break;
11455 case DW_LNS_advance_line:
11456 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11457 line_ptr += bytes_read;
11458 break;
11459 case DW_LNS_set_file:
debd256d 11460 {
a738430d
MK
11461 /* The arrays lh->include_dirs and lh->file_names are
11462 0-based, but the directory and file name numbers in
11463 the statement program are 1-based. */
debd256d 11464 struct file_entry *fe;
4f1520fb 11465 char *dir = NULL;
a738430d 11466
debd256d
JB
11467 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11468 line_ptr += bytes_read;
59205f5a 11469 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11470 dwarf2_debug_line_missing_file_complaint ();
11471 else
11472 {
11473 fe = &lh->file_names[file - 1];
11474 if (fe->dir_index)
11475 dir = lh->include_dirs[fe->dir_index - 1];
11476 if (!decode_for_pst_p)
11477 {
11478 last_subfile = current_subfile;
11479 dwarf2_start_subfile (fe->name, dir, comp_dir);
11480 }
11481 }
debd256d 11482 }
c906108c
SS
11483 break;
11484 case DW_LNS_set_column:
11485 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11486 line_ptr += bytes_read;
11487 break;
11488 case DW_LNS_negate_stmt:
11489 is_stmt = (!is_stmt);
11490 break;
11491 case DW_LNS_set_basic_block:
11492 basic_block = 1;
11493 break;
c2c6d25f
JM
11494 /* Add to the address register of the state machine the
11495 address increment value corresponding to special opcode
a738430d
MK
11496 255. I.e., this value is scaled by the minimum
11497 instruction length since special opcode 255 would have
b021a221 11498 scaled the increment. */
c906108c 11499 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11500 {
11501 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11502
11503 address += (((op_index + adjust)
11504 / lh->maximum_ops_per_instruction)
11505 * lh->minimum_instruction_length);
11506 op_index = ((op_index + adjust)
11507 % lh->maximum_ops_per_instruction);
11508 }
c906108c
SS
11509 break;
11510 case DW_LNS_fixed_advance_pc:
11511 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11512 op_index = 0;
c906108c
SS
11513 line_ptr += 2;
11514 break;
9aa1fe7e 11515 default:
a738430d
MK
11516 {
11517 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11518 int i;
a738430d 11519
debd256d 11520 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11521 {
11522 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11523 line_ptr += bytes_read;
11524 }
11525 }
c906108c
SS
11526 }
11527 }
59205f5a
JB
11528 if (lh->num_file_names < file || file == 0)
11529 dwarf2_debug_line_missing_file_complaint ();
11530 else
11531 {
11532 lh->file_names[file - 1].included_p = 1;
11533 if (!decode_for_pst_p)
fbf65064
UW
11534 {
11535 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11536 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11537 }
59205f5a 11538 }
c906108c 11539 }
aaa75496
JB
11540
11541 if (decode_for_pst_p)
11542 {
11543 int file_index;
11544
11545 /* Now that we're done scanning the Line Header Program, we can
11546 create the psymtab of each included file. */
11547 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11548 if (lh->file_names[file_index].included_p == 1)
11549 {
c6da4cef
DE
11550 char *include_name =
11551 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11552 if (include_name != NULL)
aaa75496
JB
11553 dwarf2_create_include_psymtab (include_name, pst, objfile);
11554 }
11555 }
cb1df416
DJ
11556 else
11557 {
11558 /* Make sure a symtab is created for every file, even files
11559 which contain only variables (i.e. no code with associated
11560 line numbers). */
11561
11562 int i;
11563 struct file_entry *fe;
11564
11565 for (i = 0; i < lh->num_file_names; i++)
11566 {
11567 char *dir = NULL;
9a619af0 11568
cb1df416
DJ
11569 fe = &lh->file_names[i];
11570 if (fe->dir_index)
11571 dir = lh->include_dirs[fe->dir_index - 1];
11572 dwarf2_start_subfile (fe->name, dir, comp_dir);
11573
11574 /* Skip the main file; we don't need it, and it must be
11575 allocated last, so that it will show up before the
11576 non-primary symtabs in the objfile's symtab list. */
11577 if (current_subfile == first_subfile)
11578 continue;
11579
11580 if (current_subfile->symtab == NULL)
11581 current_subfile->symtab = allocate_symtab (current_subfile->name,
11582 cu->objfile);
11583 fe->symtab = current_subfile->symtab;
11584 }
11585 }
c906108c
SS
11586}
11587
11588/* Start a subfile for DWARF. FILENAME is the name of the file and
11589 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11590 or NULL if not known. COMP_DIR is the compilation directory for the
11591 linetable's compilation unit or NULL if not known.
c906108c
SS
11592 This routine tries to keep line numbers from identical absolute and
11593 relative file names in a common subfile.
11594
11595 Using the `list' example from the GDB testsuite, which resides in
11596 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11597 of /srcdir/list0.c yields the following debugging information for list0.c:
11598
c5aa993b
JM
11599 DW_AT_name: /srcdir/list0.c
11600 DW_AT_comp_dir: /compdir
357e46e7 11601 files.files[0].name: list0.h
c5aa993b 11602 files.files[0].dir: /srcdir
357e46e7 11603 files.files[1].name: list0.c
c5aa993b 11604 files.files[1].dir: /srcdir
c906108c
SS
11605
11606 The line number information for list0.c has to end up in a single
4f1520fb
FR
11607 subfile, so that `break /srcdir/list0.c:1' works as expected.
11608 start_subfile will ensure that this happens provided that we pass the
11609 concatenation of files.files[1].dir and files.files[1].name as the
11610 subfile's name. */
c906108c
SS
11611
11612static void
3e43a32a
MS
11613dwarf2_start_subfile (char *filename, const char *dirname,
11614 const char *comp_dir)
c906108c 11615{
4f1520fb
FR
11616 char *fullname;
11617
11618 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11619 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11620 second argument to start_subfile. To be consistent, we do the
11621 same here. In order not to lose the line information directory,
11622 we concatenate it to the filename when it makes sense.
11623 Note that the Dwarf3 standard says (speaking of filenames in line
11624 information): ``The directory index is ignored for file names
11625 that represent full path names''. Thus ignoring dirname in the
11626 `else' branch below isn't an issue. */
c906108c 11627
d5166ae1 11628 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11629 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11630 else
11631 fullname = filename;
c906108c 11632
4f1520fb
FR
11633 start_subfile (fullname, comp_dir);
11634
11635 if (fullname != filename)
11636 xfree (fullname);
c906108c
SS
11637}
11638
4c2df51b
DJ
11639static void
11640var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11641 struct dwarf2_cu *cu)
4c2df51b 11642{
e7c27a73
DJ
11643 struct objfile *objfile = cu->objfile;
11644 struct comp_unit_head *cu_header = &cu->header;
11645
4c2df51b
DJ
11646 /* NOTE drow/2003-01-30: There used to be a comment and some special
11647 code here to turn a symbol with DW_AT_external and a
11648 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11649 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11650 with some versions of binutils) where shared libraries could have
11651 relocations against symbols in their debug information - the
11652 minimal symbol would have the right address, but the debug info
11653 would not. It's no longer necessary, because we will explicitly
11654 apply relocations when we read in the debug information now. */
11655
11656 /* A DW_AT_location attribute with no contents indicates that a
11657 variable has been optimized away. */
11658 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11659 {
11660 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11661 return;
11662 }
11663
11664 /* Handle one degenerate form of location expression specially, to
11665 preserve GDB's previous behavior when section offsets are
11666 specified. If this is just a DW_OP_addr then mark this symbol
11667 as LOC_STATIC. */
11668
11669 if (attr_form_is_block (attr)
11670 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11671 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11672 {
891d2f0b 11673 unsigned int dummy;
4c2df51b
DJ
11674
11675 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11676 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11677 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11678 fixup_symbol_section (sym, objfile);
11679 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11680 SYMBOL_SECTION (sym));
4c2df51b
DJ
11681 return;
11682 }
11683
11684 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11685 expression evaluator, and use LOC_COMPUTED only when necessary
11686 (i.e. when the value of a register or memory location is
11687 referenced, or a thread-local block, etc.). Then again, it might
11688 not be worthwhile. I'm assuming that it isn't unless performance
11689 or memory numbers show me otherwise. */
11690
e7c27a73 11691 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11692 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11693
11694 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11695 cu->has_loclist = 1;
4c2df51b
DJ
11696}
11697
c906108c
SS
11698/* Given a pointer to a DWARF information entry, figure out if we need
11699 to make a symbol table entry for it, and if so, create a new entry
11700 and return a pointer to it.
11701 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11702 used the passed type.
11703 If SPACE is not NULL, use it to hold the new symbol. If it is
11704 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11705
11706static struct symbol *
34eaf542
TT
11707new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11708 struct symbol *space)
c906108c 11709{
e7c27a73 11710 struct objfile *objfile = cu->objfile;
c906108c
SS
11711 struct symbol *sym = NULL;
11712 char *name;
11713 struct attribute *attr = NULL;
11714 struct attribute *attr2 = NULL;
e142c38c 11715 CORE_ADDR baseaddr;
e37fd15a
SW
11716 struct pending **list_to_add = NULL;
11717
edb3359d 11718 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11719
11720 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11721
94af9270 11722 name = dwarf2_name (die, cu);
c906108c
SS
11723 if (name)
11724 {
94af9270 11725 const char *linkagename;
34eaf542 11726 int suppress_add = 0;
94af9270 11727
34eaf542
TT
11728 if (space)
11729 sym = space;
11730 else
11731 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11732 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11733
11734 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11735 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11736 linkagename = dwarf2_physname (name, die, cu);
11737 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11738
f55ee35c
JK
11739 /* Fortran does not have mangling standard and the mangling does differ
11740 between gfortran, iFort etc. */
11741 if (cu->language == language_fortran
b250c185 11742 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11743 symbol_set_demangled_name (&(sym->ginfo),
11744 (char *) dwarf2_full_name (name, die, cu),
11745 NULL);
f55ee35c 11746
c906108c 11747 /* Default assumptions.
c5aa993b 11748 Use the passed type or decode it from the die. */
176620f1 11749 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11750 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11751 if (type != NULL)
11752 SYMBOL_TYPE (sym) = type;
11753 else
e7c27a73 11754 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11755 attr = dwarf2_attr (die,
11756 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11757 cu);
c906108c
SS
11758 if (attr)
11759 {
11760 SYMBOL_LINE (sym) = DW_UNSND (attr);
11761 }
cb1df416 11762
edb3359d
DJ
11763 attr = dwarf2_attr (die,
11764 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11765 cu);
cb1df416
DJ
11766 if (attr)
11767 {
11768 int file_index = DW_UNSND (attr);
9a619af0 11769
cb1df416
DJ
11770 if (cu->line_header == NULL
11771 || file_index > cu->line_header->num_file_names)
11772 complaint (&symfile_complaints,
11773 _("file index out of range"));
1c3d648d 11774 else if (file_index > 0)
cb1df416
DJ
11775 {
11776 struct file_entry *fe;
9a619af0 11777
cb1df416
DJ
11778 fe = &cu->line_header->file_names[file_index - 1];
11779 SYMBOL_SYMTAB (sym) = fe->symtab;
11780 }
11781 }
11782
c906108c
SS
11783 switch (die->tag)
11784 {
11785 case DW_TAG_label:
e142c38c 11786 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11787 if (attr)
11788 {
11789 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11790 }
0f5238ed
TT
11791 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11792 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11793 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11794 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11795 break;
11796 case DW_TAG_subprogram:
11797 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11798 finish_block. */
11799 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11800 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11801 if ((attr2 && (DW_UNSND (attr2) != 0))
11802 || cu->language == language_ada)
c906108c 11803 {
2cfa0c8d
JB
11804 /* Subprograms marked external are stored as a global symbol.
11805 Ada subprograms, whether marked external or not, are always
11806 stored as a global symbol, because we want to be able to
11807 access them globally. For instance, we want to be able
11808 to break on a nested subprogram without having to
11809 specify the context. */
e37fd15a 11810 list_to_add = &global_symbols;
c906108c
SS
11811 }
11812 else
11813 {
e37fd15a 11814 list_to_add = cu->list_in_scope;
c906108c
SS
11815 }
11816 break;
edb3359d
DJ
11817 case DW_TAG_inlined_subroutine:
11818 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11819 finish_block. */
11820 SYMBOL_CLASS (sym) = LOC_BLOCK;
11821 SYMBOL_INLINED (sym) = 1;
11822 /* Do not add the symbol to any lists. It will be found via
11823 BLOCK_FUNCTION from the blockvector. */
11824 break;
34eaf542
TT
11825 case DW_TAG_template_value_param:
11826 suppress_add = 1;
11827 /* Fall through. */
72929c62 11828 case DW_TAG_constant:
c906108c 11829 case DW_TAG_variable:
254e6b9e 11830 case DW_TAG_member:
0963b4bd
MS
11831 /* Compilation with minimal debug info may result in
11832 variables with missing type entries. Change the
11833 misleading `void' type to something sensible. */
c906108c 11834 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11835 SYMBOL_TYPE (sym)
46bf5051 11836 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11837
e142c38c 11838 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11839 /* In the case of DW_TAG_member, we should only be called for
11840 static const members. */
11841 if (die->tag == DW_TAG_member)
11842 {
3863f96c
DE
11843 /* dwarf2_add_field uses die_is_declaration,
11844 so we do the same. */
254e6b9e
DE
11845 gdb_assert (die_is_declaration (die, cu));
11846 gdb_assert (attr);
11847 }
c906108c
SS
11848 if (attr)
11849 {
e7c27a73 11850 dwarf2_const_value (attr, sym, cu);
e142c38c 11851 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11852 if (!suppress_add)
34eaf542
TT
11853 {
11854 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11855 list_to_add = &global_symbols;
34eaf542 11856 else
e37fd15a 11857 list_to_add = cu->list_in_scope;
34eaf542 11858 }
c906108c
SS
11859 break;
11860 }
e142c38c 11861 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11862 if (attr)
11863 {
e7c27a73 11864 var_decode_location (attr, sym, cu);
e142c38c 11865 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11866 if (SYMBOL_CLASS (sym) == LOC_STATIC
11867 && SYMBOL_VALUE_ADDRESS (sym) == 0
11868 && !dwarf2_per_objfile->has_section_at_zero)
11869 {
11870 /* When a static variable is eliminated by the linker,
11871 the corresponding debug information is not stripped
11872 out, but the variable address is set to null;
11873 do not add such variables into symbol table. */
11874 }
11875 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11876 {
f55ee35c
JK
11877 /* Workaround gfortran PR debug/40040 - it uses
11878 DW_AT_location for variables in -fPIC libraries which may
11879 get overriden by other libraries/executable and get
11880 a different address. Resolve it by the minimal symbol
11881 which may come from inferior's executable using copy
11882 relocation. Make this workaround only for gfortran as for
11883 other compilers GDB cannot guess the minimal symbol
11884 Fortran mangling kind. */
11885 if (cu->language == language_fortran && die->parent
11886 && die->parent->tag == DW_TAG_module
11887 && cu->producer
11888 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11889 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11890
1c809c68
TT
11891 /* A variable with DW_AT_external is never static,
11892 but it may be block-scoped. */
11893 list_to_add = (cu->list_in_scope == &file_symbols
11894 ? &global_symbols : cu->list_in_scope);
1c809c68 11895 }
c906108c 11896 else
e37fd15a 11897 list_to_add = cu->list_in_scope;
c906108c
SS
11898 }
11899 else
11900 {
11901 /* We do not know the address of this symbol.
c5aa993b
JM
11902 If it is an external symbol and we have type information
11903 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11904 The address of the variable will then be determined from
11905 the minimal symbol table whenever the variable is
11906 referenced. */
e142c38c 11907 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11908 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11909 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11910 {
0fe7935b
DJ
11911 /* A variable with DW_AT_external is never static, but it
11912 may be block-scoped. */
11913 list_to_add = (cu->list_in_scope == &file_symbols
11914 ? &global_symbols : cu->list_in_scope);
11915
c906108c 11916 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11917 }
442ddf59
JK
11918 else if (!die_is_declaration (die, cu))
11919 {
11920 /* Use the default LOC_OPTIMIZED_OUT class. */
11921 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11922 if (!suppress_add)
11923 list_to_add = cu->list_in_scope;
442ddf59 11924 }
c906108c
SS
11925 }
11926 break;
11927 case DW_TAG_formal_parameter:
edb3359d
DJ
11928 /* If we are inside a function, mark this as an argument. If
11929 not, we might be looking at an argument to an inlined function
11930 when we do not have enough information to show inlined frames;
11931 pretend it's a local variable in that case so that the user can
11932 still see it. */
11933 if (context_stack_depth > 0
11934 && context_stack[context_stack_depth - 1].name != NULL)
11935 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11936 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11937 if (attr)
11938 {
e7c27a73 11939 var_decode_location (attr, sym, cu);
c906108c 11940 }
e142c38c 11941 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11942 if (attr)
11943 {
e7c27a73 11944 dwarf2_const_value (attr, sym, cu);
c906108c 11945 }
f346a30d 11946
e37fd15a 11947 list_to_add = cu->list_in_scope;
c906108c
SS
11948 break;
11949 case DW_TAG_unspecified_parameters:
11950 /* From varargs functions; gdb doesn't seem to have any
11951 interest in this information, so just ignore it for now.
11952 (FIXME?) */
11953 break;
34eaf542
TT
11954 case DW_TAG_template_type_param:
11955 suppress_add = 1;
11956 /* Fall through. */
c906108c 11957 case DW_TAG_class_type:
680b30c7 11958 case DW_TAG_interface_type:
c906108c
SS
11959 case DW_TAG_structure_type:
11960 case DW_TAG_union_type:
72019c9c 11961 case DW_TAG_set_type:
c906108c
SS
11962 case DW_TAG_enumeration_type:
11963 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11964 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11965
63d06c5c 11966 {
987504bb 11967 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11968 really ever be static objects: otherwise, if you try
11969 to, say, break of a class's method and you're in a file
11970 which doesn't mention that class, it won't work unless
11971 the check for all static symbols in lookup_symbol_aux
11972 saves you. See the OtherFileClass tests in
11973 gdb.c++/namespace.exp. */
11974
e37fd15a 11975 if (!suppress_add)
34eaf542 11976 {
34eaf542
TT
11977 list_to_add = (cu->list_in_scope == &file_symbols
11978 && (cu->language == language_cplus
11979 || cu->language == language_java)
11980 ? &global_symbols : cu->list_in_scope);
63d06c5c 11981
64382290
TT
11982 /* The semantics of C++ state that "struct foo {
11983 ... }" also defines a typedef for "foo". A Java
11984 class declaration also defines a typedef for the
11985 class. */
11986 if (cu->language == language_cplus
11987 || cu->language == language_java
11988 || cu->language == language_ada)
11989 {
11990 /* The symbol's name is already allocated along
11991 with this objfile, so we don't need to
11992 duplicate it for the type. */
11993 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11994 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11995 }
63d06c5c
DC
11996 }
11997 }
c906108c
SS
11998 break;
11999 case DW_TAG_typedef:
63d06c5c
DC
12000 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
12001 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 12002 list_to_add = cu->list_in_scope;
63d06c5c 12003 break;
c906108c 12004 case DW_TAG_base_type:
a02abb62 12005 case DW_TAG_subrange_type:
c906108c 12006 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 12007 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 12008 list_to_add = cu->list_in_scope;
c906108c
SS
12009 break;
12010 case DW_TAG_enumerator:
e142c38c 12011 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
12012 if (attr)
12013 {
e7c27a73 12014 dwarf2_const_value (attr, sym, cu);
c906108c 12015 }
63d06c5c
DC
12016 {
12017 /* NOTE: carlton/2003-11-10: See comment above in the
12018 DW_TAG_class_type, etc. block. */
12019
e142c38c 12020 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
12021 && (cu->language == language_cplus
12022 || cu->language == language_java)
e142c38c 12023 ? &global_symbols : cu->list_in_scope);
63d06c5c 12024 }
c906108c 12025 break;
5c4e30ca
DC
12026 case DW_TAG_namespace:
12027 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 12028 list_to_add = &global_symbols;
5c4e30ca 12029 break;
c906108c
SS
12030 default:
12031 /* Not a tag we recognize. Hopefully we aren't processing
12032 trash data, but since we must specifically ignore things
12033 we don't recognize, there is nothing else we should do at
0963b4bd 12034 this point. */
e2e0b3e5 12035 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 12036 dwarf_tag_name (die->tag));
c906108c
SS
12037 break;
12038 }
df8a16a1 12039
e37fd15a
SW
12040 if (suppress_add)
12041 {
12042 sym->hash_next = objfile->template_symbols;
12043 objfile->template_symbols = sym;
12044 list_to_add = NULL;
12045 }
12046
12047 if (list_to_add != NULL)
12048 add_symbol_to_list (sym, list_to_add);
12049
df8a16a1
DJ
12050 /* For the benefit of old versions of GCC, check for anonymous
12051 namespaces based on the demangled name. */
12052 if (!processing_has_namespace_info
94af9270 12053 && cu->language == language_cplus)
a10964d1 12054 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
12055 }
12056 return (sym);
12057}
12058
34eaf542
TT
12059/* A wrapper for new_symbol_full that always allocates a new symbol. */
12060
12061static struct symbol *
12062new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12063{
12064 return new_symbol_full (die, type, cu, NULL);
12065}
12066
98bfdba5
PA
12067/* Given an attr with a DW_FORM_dataN value in host byte order,
12068 zero-extend it as appropriate for the symbol's type. The DWARF
12069 standard (v4) is not entirely clear about the meaning of using
12070 DW_FORM_dataN for a constant with a signed type, where the type is
12071 wider than the data. The conclusion of a discussion on the DWARF
12072 list was that this is unspecified. We choose to always zero-extend
12073 because that is the interpretation long in use by GCC. */
c906108c 12074
98bfdba5
PA
12075static gdb_byte *
12076dwarf2_const_value_data (struct attribute *attr, struct type *type,
12077 const char *name, struct obstack *obstack,
12078 struct dwarf2_cu *cu, long *value, int bits)
c906108c 12079{
e7c27a73 12080 struct objfile *objfile = cu->objfile;
e17a4113
UW
12081 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12082 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
12083 LONGEST l = DW_UNSND (attr);
12084
12085 if (bits < sizeof (*value) * 8)
12086 {
12087 l &= ((LONGEST) 1 << bits) - 1;
12088 *value = l;
12089 }
12090 else if (bits == sizeof (*value) * 8)
12091 *value = l;
12092 else
12093 {
12094 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12095 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12096 return bytes;
12097 }
12098
12099 return NULL;
12100}
12101
12102/* Read a constant value from an attribute. Either set *VALUE, or if
12103 the value does not fit in *VALUE, set *BYTES - either already
12104 allocated on the objfile obstack, or newly allocated on OBSTACK,
12105 or, set *BATON, if we translated the constant to a location
12106 expression. */
12107
12108static void
12109dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12110 const char *name, struct obstack *obstack,
12111 struct dwarf2_cu *cu,
12112 long *value, gdb_byte **bytes,
12113 struct dwarf2_locexpr_baton **baton)
12114{
12115 struct objfile *objfile = cu->objfile;
12116 struct comp_unit_head *cu_header = &cu->header;
c906108c 12117 struct dwarf_block *blk;
98bfdba5
PA
12118 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12119 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12120
12121 *value = 0;
12122 *bytes = NULL;
12123 *baton = NULL;
c906108c
SS
12124
12125 switch (attr->form)
12126 {
12127 case DW_FORM_addr:
ac56253d 12128 {
ac56253d
TT
12129 gdb_byte *data;
12130
98bfdba5
PA
12131 if (TYPE_LENGTH (type) != cu_header->addr_size)
12132 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12133 cu_header->addr_size,
98bfdba5 12134 TYPE_LENGTH (type));
ac56253d
TT
12135 /* Symbols of this form are reasonably rare, so we just
12136 piggyback on the existing location code rather than writing
12137 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12138 *baton = obstack_alloc (&objfile->objfile_obstack,
12139 sizeof (struct dwarf2_locexpr_baton));
12140 (*baton)->per_cu = cu->per_cu;
12141 gdb_assert ((*baton)->per_cu);
ac56253d 12142
98bfdba5
PA
12143 (*baton)->size = 2 + cu_header->addr_size;
12144 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12145 (*baton)->data = data;
ac56253d
TT
12146
12147 data[0] = DW_OP_addr;
12148 store_unsigned_integer (&data[1], cu_header->addr_size,
12149 byte_order, DW_ADDR (attr));
12150 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12151 }
c906108c 12152 break;
4ac36638 12153 case DW_FORM_string:
93b5768b 12154 case DW_FORM_strp:
98bfdba5
PA
12155 /* DW_STRING is already allocated on the objfile obstack, point
12156 directly to it. */
12157 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12158 break;
c906108c
SS
12159 case DW_FORM_block1:
12160 case DW_FORM_block2:
12161 case DW_FORM_block4:
12162 case DW_FORM_block:
2dc7f7b3 12163 case DW_FORM_exprloc:
c906108c 12164 blk = DW_BLOCK (attr);
98bfdba5
PA
12165 if (TYPE_LENGTH (type) != blk->size)
12166 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12167 TYPE_LENGTH (type));
12168 *bytes = blk->data;
c906108c 12169 break;
2df3850c
JM
12170
12171 /* The DW_AT_const_value attributes are supposed to carry the
12172 symbol's value "represented as it would be on the target
12173 architecture." By the time we get here, it's already been
12174 converted to host endianness, so we just need to sign- or
12175 zero-extend it as appropriate. */
12176 case DW_FORM_data1:
3e43a32a
MS
12177 *bytes = dwarf2_const_value_data (attr, type, name,
12178 obstack, cu, value, 8);
2df3850c 12179 break;
c906108c 12180 case DW_FORM_data2:
3e43a32a
MS
12181 *bytes = dwarf2_const_value_data (attr, type, name,
12182 obstack, cu, value, 16);
2df3850c 12183 break;
c906108c 12184 case DW_FORM_data4:
3e43a32a
MS
12185 *bytes = dwarf2_const_value_data (attr, type, name,
12186 obstack, cu, value, 32);
2df3850c 12187 break;
c906108c 12188 case DW_FORM_data8:
3e43a32a
MS
12189 *bytes = dwarf2_const_value_data (attr, type, name,
12190 obstack, cu, value, 64);
2df3850c
JM
12191 break;
12192
c906108c 12193 case DW_FORM_sdata:
98bfdba5 12194 *value = DW_SND (attr);
2df3850c
JM
12195 break;
12196
c906108c 12197 case DW_FORM_udata:
98bfdba5 12198 *value = DW_UNSND (attr);
c906108c 12199 break;
2df3850c 12200
c906108c 12201 default:
4d3c2250 12202 complaint (&symfile_complaints,
e2e0b3e5 12203 _("unsupported const value attribute form: '%s'"),
4d3c2250 12204 dwarf_form_name (attr->form));
98bfdba5 12205 *value = 0;
c906108c
SS
12206 break;
12207 }
12208}
12209
2df3850c 12210
98bfdba5
PA
12211/* Copy constant value from an attribute to a symbol. */
12212
2df3850c 12213static void
98bfdba5
PA
12214dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12215 struct dwarf2_cu *cu)
2df3850c 12216{
98bfdba5
PA
12217 struct objfile *objfile = cu->objfile;
12218 struct comp_unit_head *cu_header = &cu->header;
12219 long value;
12220 gdb_byte *bytes;
12221 struct dwarf2_locexpr_baton *baton;
2df3850c 12222
98bfdba5
PA
12223 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12224 SYMBOL_PRINT_NAME (sym),
12225 &objfile->objfile_obstack, cu,
12226 &value, &bytes, &baton);
2df3850c 12227
98bfdba5
PA
12228 if (baton != NULL)
12229 {
12230 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12231 SYMBOL_LOCATION_BATON (sym) = baton;
12232 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12233 }
12234 else if (bytes != NULL)
12235 {
12236 SYMBOL_VALUE_BYTES (sym) = bytes;
12237 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12238 }
12239 else
12240 {
12241 SYMBOL_VALUE (sym) = value;
12242 SYMBOL_CLASS (sym) = LOC_CONST;
12243 }
2df3850c
JM
12244}
12245
c906108c
SS
12246/* Return the type of the die in question using its DW_AT_type attribute. */
12247
12248static struct type *
e7c27a73 12249die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12250{
c906108c 12251 struct attribute *type_attr;
c906108c 12252
e142c38c 12253 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12254 if (!type_attr)
12255 {
12256 /* A missing DW_AT_type represents a void type. */
46bf5051 12257 return objfile_type (cu->objfile)->builtin_void;
c906108c 12258 }
348e048f 12259
673bfd45 12260 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12261}
12262
b4ba55a1
JB
12263/* True iff CU's producer generates GNAT Ada auxiliary information
12264 that allows to find parallel types through that information instead
12265 of having to do expensive parallel lookups by type name. */
12266
12267static int
12268need_gnat_info (struct dwarf2_cu *cu)
12269{
12270 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12271 of GNAT produces this auxiliary information, without any indication
12272 that it is produced. Part of enhancing the FSF version of GNAT
12273 to produce that information will be to put in place an indicator
12274 that we can use in order to determine whether the descriptive type
12275 info is available or not. One suggestion that has been made is
12276 to use a new attribute, attached to the CU die. For now, assume
12277 that the descriptive type info is not available. */
12278 return 0;
12279}
12280
b4ba55a1
JB
12281/* Return the auxiliary type of the die in question using its
12282 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12283 attribute is not present. */
12284
12285static struct type *
12286die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12287{
b4ba55a1 12288 struct attribute *type_attr;
b4ba55a1
JB
12289
12290 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12291 if (!type_attr)
12292 return NULL;
12293
673bfd45 12294 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12295}
12296
12297/* If DIE has a descriptive_type attribute, then set the TYPE's
12298 descriptive type accordingly. */
12299
12300static void
12301set_descriptive_type (struct type *type, struct die_info *die,
12302 struct dwarf2_cu *cu)
12303{
12304 struct type *descriptive_type = die_descriptive_type (die, cu);
12305
12306 if (descriptive_type)
12307 {
12308 ALLOCATE_GNAT_AUX_TYPE (type);
12309 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12310 }
12311}
12312
c906108c
SS
12313/* Return the containing type of the die in question using its
12314 DW_AT_containing_type attribute. */
12315
12316static struct type *
e7c27a73 12317die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12318{
c906108c 12319 struct attribute *type_attr;
c906108c 12320
e142c38c 12321 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12322 if (!type_attr)
12323 error (_("Dwarf Error: Problem turning containing type into gdb type "
12324 "[in module %s]"), cu->objfile->name);
12325
673bfd45 12326 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12327}
12328
673bfd45
DE
12329/* Look up the type of DIE in CU using its type attribute ATTR.
12330 If there is no type substitute an error marker. */
12331
c906108c 12332static struct type *
673bfd45
DE
12333lookup_die_type (struct die_info *die, struct attribute *attr,
12334 struct dwarf2_cu *cu)
c906108c 12335{
f792889a
DJ
12336 struct type *this_type;
12337
673bfd45
DE
12338 /* First see if we have it cached. */
12339
12340 if (is_ref_attr (attr))
12341 {
12342 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12343
12344 this_type = get_die_type_at_offset (offset, cu->per_cu);
12345 }
55f1336d 12346 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12347 {
12348 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12349 struct dwarf2_cu *sig_cu;
12350 unsigned int offset;
12351
12352 /* sig_type will be NULL if the signatured type is missing from
12353 the debug info. */
12354 if (sig_type == NULL)
12355 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12356 "at 0x%x [in module %s]"),
12357 die->offset, cu->objfile->name);
12358
b0df02fd 12359 gdb_assert (sig_type->per_cu.debug_types_section);
b3c8eb43 12360 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
12361 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12362 }
12363 else
12364 {
12365 dump_die_for_error (die);
12366 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12367 dwarf_attr_name (attr->name), cu->objfile->name);
12368 }
12369
12370 /* If not cached we need to read it in. */
12371
12372 if (this_type == NULL)
12373 {
12374 struct die_info *type_die;
12375 struct dwarf2_cu *type_cu = cu;
12376
12377 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12378 /* If the type is cached, we should have found it above. */
12379 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12380 this_type = read_type_die_1 (type_die, type_cu);
12381 }
12382
12383 /* If we still don't have a type use an error marker. */
12384
12385 if (this_type == NULL)
c906108c 12386 {
b00fdb78
TT
12387 char *message, *saved;
12388
12389 /* read_type_die already issued a complaint. */
12390 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12391 cu->objfile->name,
12392 cu->header.offset,
12393 die->offset);
12394 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12395 message, strlen (message));
12396 xfree (message);
12397
12398 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 12399 }
673bfd45 12400
f792889a 12401 return this_type;
c906108c
SS
12402}
12403
673bfd45
DE
12404/* Return the type in DIE, CU.
12405 Returns NULL for invalid types.
12406
12407 This first does a lookup in the appropriate type_hash table,
12408 and only reads the die in if necessary.
12409
12410 NOTE: This can be called when reading in partial or full symbols. */
12411
f792889a 12412static struct type *
e7c27a73 12413read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12414{
f792889a
DJ
12415 struct type *this_type;
12416
12417 this_type = get_die_type (die, cu);
12418 if (this_type)
12419 return this_type;
12420
673bfd45
DE
12421 return read_type_die_1 (die, cu);
12422}
12423
12424/* Read the type in DIE, CU.
12425 Returns NULL for invalid types. */
12426
12427static struct type *
12428read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12429{
12430 struct type *this_type = NULL;
12431
c906108c
SS
12432 switch (die->tag)
12433 {
12434 case DW_TAG_class_type:
680b30c7 12435 case DW_TAG_interface_type:
c906108c
SS
12436 case DW_TAG_structure_type:
12437 case DW_TAG_union_type:
f792889a 12438 this_type = read_structure_type (die, cu);
c906108c
SS
12439 break;
12440 case DW_TAG_enumeration_type:
f792889a 12441 this_type = read_enumeration_type (die, cu);
c906108c
SS
12442 break;
12443 case DW_TAG_subprogram:
12444 case DW_TAG_subroutine_type:
edb3359d 12445 case DW_TAG_inlined_subroutine:
f792889a 12446 this_type = read_subroutine_type (die, cu);
c906108c
SS
12447 break;
12448 case DW_TAG_array_type:
f792889a 12449 this_type = read_array_type (die, cu);
c906108c 12450 break;
72019c9c 12451 case DW_TAG_set_type:
f792889a 12452 this_type = read_set_type (die, cu);
72019c9c 12453 break;
c906108c 12454 case DW_TAG_pointer_type:
f792889a 12455 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12456 break;
12457 case DW_TAG_ptr_to_member_type:
f792889a 12458 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12459 break;
12460 case DW_TAG_reference_type:
f792889a 12461 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12462 break;
12463 case DW_TAG_const_type:
f792889a 12464 this_type = read_tag_const_type (die, cu);
c906108c
SS
12465 break;
12466 case DW_TAG_volatile_type:
f792889a 12467 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12468 break;
12469 case DW_TAG_string_type:
f792889a 12470 this_type = read_tag_string_type (die, cu);
c906108c
SS
12471 break;
12472 case DW_TAG_typedef:
f792889a 12473 this_type = read_typedef (die, cu);
c906108c 12474 break;
a02abb62 12475 case DW_TAG_subrange_type:
f792889a 12476 this_type = read_subrange_type (die, cu);
a02abb62 12477 break;
c906108c 12478 case DW_TAG_base_type:
f792889a 12479 this_type = read_base_type (die, cu);
c906108c 12480 break;
81a17f79 12481 case DW_TAG_unspecified_type:
f792889a 12482 this_type = read_unspecified_type (die, cu);
81a17f79 12483 break;
0114d602
DJ
12484 case DW_TAG_namespace:
12485 this_type = read_namespace_type (die, cu);
12486 break;
f55ee35c
JK
12487 case DW_TAG_module:
12488 this_type = read_module_type (die, cu);
12489 break;
c906108c 12490 default:
3e43a32a
MS
12491 complaint (&symfile_complaints,
12492 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12493 dwarf_tag_name (die->tag));
c906108c
SS
12494 break;
12495 }
63d06c5c 12496
f792889a 12497 return this_type;
63d06c5c
DC
12498}
12499
abc72ce4
DE
12500/* See if we can figure out if the class lives in a namespace. We do
12501 this by looking for a member function; its demangled name will
12502 contain namespace info, if there is any.
12503 Return the computed name or NULL.
12504 Space for the result is allocated on the objfile's obstack.
12505 This is the full-die version of guess_partial_die_structure_name.
12506 In this case we know DIE has no useful parent. */
12507
12508static char *
12509guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12510{
12511 struct die_info *spec_die;
12512 struct dwarf2_cu *spec_cu;
12513 struct die_info *child;
12514
12515 spec_cu = cu;
12516 spec_die = die_specification (die, &spec_cu);
12517 if (spec_die != NULL)
12518 {
12519 die = spec_die;
12520 cu = spec_cu;
12521 }
12522
12523 for (child = die->child;
12524 child != NULL;
12525 child = child->sibling)
12526 {
12527 if (child->tag == DW_TAG_subprogram)
12528 {
12529 struct attribute *attr;
12530
12531 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12532 if (attr == NULL)
12533 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12534 if (attr != NULL)
12535 {
12536 char *actual_name
12537 = language_class_name_from_physname (cu->language_defn,
12538 DW_STRING (attr));
12539 char *name = NULL;
12540
12541 if (actual_name != NULL)
12542 {
12543 char *die_name = dwarf2_name (die, cu);
12544
12545 if (die_name != NULL
12546 && strcmp (die_name, actual_name) != 0)
12547 {
12548 /* Strip off the class name from the full name.
12549 We want the prefix. */
12550 int die_name_len = strlen (die_name);
12551 int actual_name_len = strlen (actual_name);
12552
12553 /* Test for '::' as a sanity check. */
12554 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12555 && actual_name[actual_name_len
12556 - die_name_len - 1] == ':')
abc72ce4
DE
12557 name =
12558 obsavestring (actual_name,
12559 actual_name_len - die_name_len - 2,
12560 &cu->objfile->objfile_obstack);
12561 }
12562 }
12563 xfree (actual_name);
12564 return name;
12565 }
12566 }
12567 }
12568
12569 return NULL;
12570}
12571
96408a79
SA
12572/* GCC might emit a nameless typedef that has a linkage name. Determine the
12573 prefix part in such case. See
12574 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12575
12576static char *
12577anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12578{
12579 struct attribute *attr;
12580 char *base;
12581
12582 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12583 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12584 return NULL;
12585
12586 attr = dwarf2_attr (die, DW_AT_name, cu);
12587 if (attr != NULL && DW_STRING (attr) != NULL)
12588 return NULL;
12589
12590 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12591 if (attr == NULL)
12592 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12593 if (attr == NULL || DW_STRING (attr) == NULL)
12594 return NULL;
12595
12596 /* dwarf2_name had to be already called. */
12597 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12598
12599 /* Strip the base name, keep any leading namespaces/classes. */
12600 base = strrchr (DW_STRING (attr), ':');
12601 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12602 return "";
12603
12604 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12605 &cu->objfile->objfile_obstack);
12606}
12607
fdde2d81 12608/* Return the name of the namespace/class that DIE is defined within,
0114d602 12609 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12610
0114d602
DJ
12611 For example, if we're within the method foo() in the following
12612 code:
12613
12614 namespace N {
12615 class C {
12616 void foo () {
12617 }
12618 };
12619 }
12620
12621 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12622
12623static char *
e142c38c 12624determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12625{
0114d602
DJ
12626 struct die_info *parent, *spec_die;
12627 struct dwarf2_cu *spec_cu;
12628 struct type *parent_type;
96408a79 12629 char *retval;
63d06c5c 12630
f55ee35c
JK
12631 if (cu->language != language_cplus && cu->language != language_java
12632 && cu->language != language_fortran)
0114d602
DJ
12633 return "";
12634
96408a79
SA
12635 retval = anonymous_struct_prefix (die, cu);
12636 if (retval)
12637 return retval;
12638
0114d602
DJ
12639 /* We have to be careful in the presence of DW_AT_specification.
12640 For example, with GCC 3.4, given the code
12641
12642 namespace N {
12643 void foo() {
12644 // Definition of N::foo.
12645 }
12646 }
12647
12648 then we'll have a tree of DIEs like this:
12649
12650 1: DW_TAG_compile_unit
12651 2: DW_TAG_namespace // N
12652 3: DW_TAG_subprogram // declaration of N::foo
12653 4: DW_TAG_subprogram // definition of N::foo
12654 DW_AT_specification // refers to die #3
12655
12656 Thus, when processing die #4, we have to pretend that we're in
12657 the context of its DW_AT_specification, namely the contex of die
12658 #3. */
12659 spec_cu = cu;
12660 spec_die = die_specification (die, &spec_cu);
12661 if (spec_die == NULL)
12662 parent = die->parent;
12663 else
63d06c5c 12664 {
0114d602
DJ
12665 parent = spec_die->parent;
12666 cu = spec_cu;
63d06c5c 12667 }
0114d602
DJ
12668
12669 if (parent == NULL)
12670 return "";
98bfdba5
PA
12671 else if (parent->building_fullname)
12672 {
12673 const char *name;
12674 const char *parent_name;
12675
12676 /* It has been seen on RealView 2.2 built binaries,
12677 DW_TAG_template_type_param types actually _defined_ as
12678 children of the parent class:
12679
12680 enum E {};
12681 template class <class Enum> Class{};
12682 Class<enum E> class_e;
12683
12684 1: DW_TAG_class_type (Class)
12685 2: DW_TAG_enumeration_type (E)
12686 3: DW_TAG_enumerator (enum1:0)
12687 3: DW_TAG_enumerator (enum2:1)
12688 ...
12689 2: DW_TAG_template_type_param
12690 DW_AT_type DW_FORM_ref_udata (E)
12691
12692 Besides being broken debug info, it can put GDB into an
12693 infinite loop. Consider:
12694
12695 When we're building the full name for Class<E>, we'll start
12696 at Class, and go look over its template type parameters,
12697 finding E. We'll then try to build the full name of E, and
12698 reach here. We're now trying to build the full name of E,
12699 and look over the parent DIE for containing scope. In the
12700 broken case, if we followed the parent DIE of E, we'd again
12701 find Class, and once again go look at its template type
12702 arguments, etc., etc. Simply don't consider such parent die
12703 as source-level parent of this die (it can't be, the language
12704 doesn't allow it), and break the loop here. */
12705 name = dwarf2_name (die, cu);
12706 parent_name = dwarf2_name (parent, cu);
12707 complaint (&symfile_complaints,
12708 _("template param type '%s' defined within parent '%s'"),
12709 name ? name : "<unknown>",
12710 parent_name ? parent_name : "<unknown>");
12711 return "";
12712 }
63d06c5c 12713 else
0114d602
DJ
12714 switch (parent->tag)
12715 {
63d06c5c 12716 case DW_TAG_namespace:
0114d602 12717 parent_type = read_type_die (parent, cu);
acebe513
UW
12718 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12719 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12720 Work around this problem here. */
12721 if (cu->language == language_cplus
12722 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12723 return "";
0114d602
DJ
12724 /* We give a name to even anonymous namespaces. */
12725 return TYPE_TAG_NAME (parent_type);
63d06c5c 12726 case DW_TAG_class_type:
680b30c7 12727 case DW_TAG_interface_type:
63d06c5c 12728 case DW_TAG_structure_type:
0114d602 12729 case DW_TAG_union_type:
f55ee35c 12730 case DW_TAG_module:
0114d602
DJ
12731 parent_type = read_type_die (parent, cu);
12732 if (TYPE_TAG_NAME (parent_type) != NULL)
12733 return TYPE_TAG_NAME (parent_type);
12734 else
12735 /* An anonymous structure is only allowed non-static data
12736 members; no typedefs, no member functions, et cetera.
12737 So it does not need a prefix. */
12738 return "";
abc72ce4
DE
12739 case DW_TAG_compile_unit:
12740 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12741 if (cu->language == language_cplus
8b70b953 12742 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12743 && die->child != NULL
12744 && (die->tag == DW_TAG_class_type
12745 || die->tag == DW_TAG_structure_type
12746 || die->tag == DW_TAG_union_type))
12747 {
12748 char *name = guess_full_die_structure_name (die, cu);
12749 if (name != NULL)
12750 return name;
12751 }
12752 return "";
63d06c5c 12753 default:
8176b9b8 12754 return determine_prefix (parent, cu);
63d06c5c 12755 }
63d06c5c
DC
12756}
12757
3e43a32a
MS
12758/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12759 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12760 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12761 an obconcat, otherwise allocate storage for the result. The CU argument is
12762 used to determine the language and hence, the appropriate separator. */
987504bb 12763
f55ee35c 12764#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12765
12766static char *
f55ee35c
JK
12767typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12768 int physname, struct dwarf2_cu *cu)
63d06c5c 12769{
f55ee35c 12770 const char *lead = "";
5c315b68 12771 const char *sep;
63d06c5c 12772
3e43a32a
MS
12773 if (suffix == NULL || suffix[0] == '\0'
12774 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12775 sep = "";
12776 else if (cu->language == language_java)
12777 sep = ".";
f55ee35c
JK
12778 else if (cu->language == language_fortran && physname)
12779 {
12780 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12781 DW_AT_MIPS_linkage_name is preferred and used instead. */
12782
12783 lead = "__";
12784 sep = "_MOD_";
12785 }
987504bb
JJ
12786 else
12787 sep = "::";
63d06c5c 12788
6dd47d34
DE
12789 if (prefix == NULL)
12790 prefix = "";
12791 if (suffix == NULL)
12792 suffix = "";
12793
987504bb
JJ
12794 if (obs == NULL)
12795 {
3e43a32a
MS
12796 char *retval
12797 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12798
f55ee35c
JK
12799 strcpy (retval, lead);
12800 strcat (retval, prefix);
6dd47d34
DE
12801 strcat (retval, sep);
12802 strcat (retval, suffix);
63d06c5c
DC
12803 return retval;
12804 }
987504bb
JJ
12805 else
12806 {
12807 /* We have an obstack. */
f55ee35c 12808 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12809 }
63d06c5c
DC
12810}
12811
c906108c
SS
12812/* Return sibling of die, NULL if no sibling. */
12813
f9aca02d 12814static struct die_info *
fba45db2 12815sibling_die (struct die_info *die)
c906108c 12816{
639d11d3 12817 return die->sibling;
c906108c
SS
12818}
12819
71c25dea
TT
12820/* Get name of a die, return NULL if not found. */
12821
12822static char *
12823dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12824 struct obstack *obstack)
12825{
12826 if (name && cu->language == language_cplus)
12827 {
12828 char *canon_name = cp_canonicalize_string (name);
12829
12830 if (canon_name != NULL)
12831 {
12832 if (strcmp (canon_name, name) != 0)
12833 name = obsavestring (canon_name, strlen (canon_name),
12834 obstack);
12835 xfree (canon_name);
12836 }
12837 }
12838
12839 return name;
c906108c
SS
12840}
12841
9219021c
DC
12842/* Get name of a die, return NULL if not found. */
12843
12844static char *
e142c38c 12845dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12846{
12847 struct attribute *attr;
12848
e142c38c 12849 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12850 if ((!attr || !DW_STRING (attr))
12851 && die->tag != DW_TAG_class_type
12852 && die->tag != DW_TAG_interface_type
12853 && die->tag != DW_TAG_structure_type
12854 && die->tag != DW_TAG_union_type)
71c25dea
TT
12855 return NULL;
12856
12857 switch (die->tag)
12858 {
12859 case DW_TAG_compile_unit:
12860 /* Compilation units have a DW_AT_name that is a filename, not
12861 a source language identifier. */
12862 case DW_TAG_enumeration_type:
12863 case DW_TAG_enumerator:
12864 /* These tags always have simple identifiers already; no need
12865 to canonicalize them. */
12866 return DW_STRING (attr);
907af001 12867
418835cc
KS
12868 case DW_TAG_subprogram:
12869 /* Java constructors will all be named "<init>", so return
12870 the class name when we see this special case. */
12871 if (cu->language == language_java
12872 && DW_STRING (attr) != NULL
12873 && strcmp (DW_STRING (attr), "<init>") == 0)
12874 {
12875 struct dwarf2_cu *spec_cu = cu;
12876 struct die_info *spec_die;
12877
12878 /* GCJ will output '<init>' for Java constructor names.
12879 For this special case, return the name of the parent class. */
12880
12881 /* GCJ may output suprogram DIEs with AT_specification set.
12882 If so, use the name of the specified DIE. */
12883 spec_die = die_specification (die, &spec_cu);
12884 if (spec_die != NULL)
12885 return dwarf2_name (spec_die, spec_cu);
12886
12887 do
12888 {
12889 die = die->parent;
12890 if (die->tag == DW_TAG_class_type)
12891 return dwarf2_name (die, cu);
12892 }
12893 while (die->tag != DW_TAG_compile_unit);
12894 }
907af001
UW
12895 break;
12896
12897 case DW_TAG_class_type:
12898 case DW_TAG_interface_type:
12899 case DW_TAG_structure_type:
12900 case DW_TAG_union_type:
12901 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12902 structures or unions. These were of the form "._%d" in GCC 4.1,
12903 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12904 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12905 if (attr && DW_STRING (attr)
12906 && (strncmp (DW_STRING (attr), "._", 2) == 0
12907 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12908 return NULL;
53832f31
TT
12909
12910 /* GCC might emit a nameless typedef that has a linkage name. See
12911 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12912 if (!attr || DW_STRING (attr) == NULL)
12913 {
df5c6c50 12914 char *demangled = NULL;
53832f31
TT
12915
12916 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12917 if (attr == NULL)
12918 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12919
12920 if (attr == NULL || DW_STRING (attr) == NULL)
12921 return NULL;
12922
df5c6c50
JK
12923 /* Avoid demangling DW_STRING (attr) the second time on a second
12924 call for the same DIE. */
12925 if (!DW_STRING_IS_CANONICAL (attr))
12926 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12927
12928 if (demangled)
12929 {
96408a79
SA
12930 char *base;
12931
53832f31 12932 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12933 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12934 &cu->objfile->objfile_obstack);
53832f31
TT
12935 DW_STRING_IS_CANONICAL (attr) = 1;
12936 xfree (demangled);
96408a79
SA
12937
12938 /* Strip any leading namespaces/classes, keep only the base name.
12939 DW_AT_name for named DIEs does not contain the prefixes. */
12940 base = strrchr (DW_STRING (attr), ':');
12941 if (base && base > DW_STRING (attr) && base[-1] == ':')
12942 return &base[1];
12943 else
12944 return DW_STRING (attr);
53832f31
TT
12945 }
12946 }
907af001
UW
12947 break;
12948
71c25dea 12949 default:
907af001
UW
12950 break;
12951 }
12952
12953 if (!DW_STRING_IS_CANONICAL (attr))
12954 {
12955 DW_STRING (attr)
12956 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12957 &cu->objfile->objfile_obstack);
12958 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12959 }
907af001 12960 return DW_STRING (attr);
9219021c
DC
12961}
12962
12963/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12964 is none. *EXT_CU is the CU containing DIE on input, and the CU
12965 containing the return value on output. */
9219021c
DC
12966
12967static struct die_info *
f2f0e013 12968dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12969{
12970 struct attribute *attr;
9219021c 12971
f2f0e013 12972 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12973 if (attr == NULL)
12974 return NULL;
12975
f2f0e013 12976 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12977}
12978
c906108c
SS
12979/* Convert a DIE tag into its string name. */
12980
12981static char *
aa1ee363 12982dwarf_tag_name (unsigned tag)
c906108c
SS
12983{
12984 switch (tag)
12985 {
12986 case DW_TAG_padding:
12987 return "DW_TAG_padding";
12988 case DW_TAG_array_type:
12989 return "DW_TAG_array_type";
12990 case DW_TAG_class_type:
12991 return "DW_TAG_class_type";
12992 case DW_TAG_entry_point:
12993 return "DW_TAG_entry_point";
12994 case DW_TAG_enumeration_type:
12995 return "DW_TAG_enumeration_type";
12996 case DW_TAG_formal_parameter:
12997 return "DW_TAG_formal_parameter";
12998 case DW_TAG_imported_declaration:
12999 return "DW_TAG_imported_declaration";
13000 case DW_TAG_label:
13001 return "DW_TAG_label";
13002 case DW_TAG_lexical_block:
13003 return "DW_TAG_lexical_block";
13004 case DW_TAG_member:
13005 return "DW_TAG_member";
13006 case DW_TAG_pointer_type:
13007 return "DW_TAG_pointer_type";
13008 case DW_TAG_reference_type:
13009 return "DW_TAG_reference_type";
13010 case DW_TAG_compile_unit:
13011 return "DW_TAG_compile_unit";
13012 case DW_TAG_string_type:
13013 return "DW_TAG_string_type";
13014 case DW_TAG_structure_type:
13015 return "DW_TAG_structure_type";
13016 case DW_TAG_subroutine_type:
13017 return "DW_TAG_subroutine_type";
13018 case DW_TAG_typedef:
13019 return "DW_TAG_typedef";
13020 case DW_TAG_union_type:
13021 return "DW_TAG_union_type";
13022 case DW_TAG_unspecified_parameters:
13023 return "DW_TAG_unspecified_parameters";
13024 case DW_TAG_variant:
13025 return "DW_TAG_variant";
13026 case DW_TAG_common_block:
13027 return "DW_TAG_common_block";
13028 case DW_TAG_common_inclusion:
13029 return "DW_TAG_common_inclusion";
13030 case DW_TAG_inheritance:
13031 return "DW_TAG_inheritance";
13032 case DW_TAG_inlined_subroutine:
13033 return "DW_TAG_inlined_subroutine";
13034 case DW_TAG_module:
13035 return "DW_TAG_module";
13036 case DW_TAG_ptr_to_member_type:
13037 return "DW_TAG_ptr_to_member_type";
13038 case DW_TAG_set_type:
13039 return "DW_TAG_set_type";
13040 case DW_TAG_subrange_type:
13041 return "DW_TAG_subrange_type";
13042 case DW_TAG_with_stmt:
13043 return "DW_TAG_with_stmt";
13044 case DW_TAG_access_declaration:
13045 return "DW_TAG_access_declaration";
13046 case DW_TAG_base_type:
13047 return "DW_TAG_base_type";
13048 case DW_TAG_catch_block:
13049 return "DW_TAG_catch_block";
13050 case DW_TAG_const_type:
13051 return "DW_TAG_const_type";
13052 case DW_TAG_constant:
13053 return "DW_TAG_constant";
13054 case DW_TAG_enumerator:
13055 return "DW_TAG_enumerator";
13056 case DW_TAG_file_type:
13057 return "DW_TAG_file_type";
13058 case DW_TAG_friend:
13059 return "DW_TAG_friend";
13060 case DW_TAG_namelist:
13061 return "DW_TAG_namelist";
13062 case DW_TAG_namelist_item:
13063 return "DW_TAG_namelist_item";
13064 case DW_TAG_packed_type:
13065 return "DW_TAG_packed_type";
13066 case DW_TAG_subprogram:
13067 return "DW_TAG_subprogram";
13068 case DW_TAG_template_type_param:
13069 return "DW_TAG_template_type_param";
13070 case DW_TAG_template_value_param:
13071 return "DW_TAG_template_value_param";
13072 case DW_TAG_thrown_type:
13073 return "DW_TAG_thrown_type";
13074 case DW_TAG_try_block:
13075 return "DW_TAG_try_block";
13076 case DW_TAG_variant_part:
13077 return "DW_TAG_variant_part";
13078 case DW_TAG_variable:
13079 return "DW_TAG_variable";
13080 case DW_TAG_volatile_type:
13081 return "DW_TAG_volatile_type";
d9fa45fe
DC
13082 case DW_TAG_dwarf_procedure:
13083 return "DW_TAG_dwarf_procedure";
13084 case DW_TAG_restrict_type:
13085 return "DW_TAG_restrict_type";
13086 case DW_TAG_interface_type:
13087 return "DW_TAG_interface_type";
13088 case DW_TAG_namespace:
13089 return "DW_TAG_namespace";
13090 case DW_TAG_imported_module:
13091 return "DW_TAG_imported_module";
13092 case DW_TAG_unspecified_type:
13093 return "DW_TAG_unspecified_type";
13094 case DW_TAG_partial_unit:
13095 return "DW_TAG_partial_unit";
13096 case DW_TAG_imported_unit:
13097 return "DW_TAG_imported_unit";
b7619582
GF
13098 case DW_TAG_condition:
13099 return "DW_TAG_condition";
13100 case DW_TAG_shared_type:
13101 return "DW_TAG_shared_type";
348e048f
DE
13102 case DW_TAG_type_unit:
13103 return "DW_TAG_type_unit";
c906108c
SS
13104 case DW_TAG_MIPS_loop:
13105 return "DW_TAG_MIPS_loop";
b7619582
GF
13106 case DW_TAG_HP_array_descriptor:
13107 return "DW_TAG_HP_array_descriptor";
c906108c
SS
13108 case DW_TAG_format_label:
13109 return "DW_TAG_format_label";
13110 case DW_TAG_function_template:
13111 return "DW_TAG_function_template";
13112 case DW_TAG_class_template:
13113 return "DW_TAG_class_template";
b7619582
GF
13114 case DW_TAG_GNU_BINCL:
13115 return "DW_TAG_GNU_BINCL";
13116 case DW_TAG_GNU_EINCL:
13117 return "DW_TAG_GNU_EINCL";
13118 case DW_TAG_upc_shared_type:
13119 return "DW_TAG_upc_shared_type";
13120 case DW_TAG_upc_strict_type:
13121 return "DW_TAG_upc_strict_type";
13122 case DW_TAG_upc_relaxed_type:
13123 return "DW_TAG_upc_relaxed_type";
13124 case DW_TAG_PGI_kanji_type:
13125 return "DW_TAG_PGI_kanji_type";
13126 case DW_TAG_PGI_interface_block:
13127 return "DW_TAG_PGI_interface_block";
96408a79
SA
13128 case DW_TAG_GNU_call_site:
13129 return "DW_TAG_GNU_call_site";
c906108c
SS
13130 default:
13131 return "DW_TAG_<unknown>";
13132 }
13133}
13134
13135/* Convert a DWARF attribute code into its string name. */
13136
13137static char *
aa1ee363 13138dwarf_attr_name (unsigned attr)
c906108c
SS
13139{
13140 switch (attr)
13141 {
13142 case DW_AT_sibling:
13143 return "DW_AT_sibling";
13144 case DW_AT_location:
13145 return "DW_AT_location";
13146 case DW_AT_name:
13147 return "DW_AT_name";
13148 case DW_AT_ordering:
13149 return "DW_AT_ordering";
13150 case DW_AT_subscr_data:
13151 return "DW_AT_subscr_data";
13152 case DW_AT_byte_size:
13153 return "DW_AT_byte_size";
13154 case DW_AT_bit_offset:
13155 return "DW_AT_bit_offset";
13156 case DW_AT_bit_size:
13157 return "DW_AT_bit_size";
13158 case DW_AT_element_list:
13159 return "DW_AT_element_list";
13160 case DW_AT_stmt_list:
13161 return "DW_AT_stmt_list";
13162 case DW_AT_low_pc:
13163 return "DW_AT_low_pc";
13164 case DW_AT_high_pc:
13165 return "DW_AT_high_pc";
13166 case DW_AT_language:
13167 return "DW_AT_language";
13168 case DW_AT_member:
13169 return "DW_AT_member";
13170 case DW_AT_discr:
13171 return "DW_AT_discr";
13172 case DW_AT_discr_value:
13173 return "DW_AT_discr_value";
13174 case DW_AT_visibility:
13175 return "DW_AT_visibility";
13176 case DW_AT_import:
13177 return "DW_AT_import";
13178 case DW_AT_string_length:
13179 return "DW_AT_string_length";
13180 case DW_AT_common_reference:
13181 return "DW_AT_common_reference";
13182 case DW_AT_comp_dir:
13183 return "DW_AT_comp_dir";
13184 case DW_AT_const_value:
13185 return "DW_AT_const_value";
13186 case DW_AT_containing_type:
13187 return "DW_AT_containing_type";
13188 case DW_AT_default_value:
13189 return "DW_AT_default_value";
13190 case DW_AT_inline:
13191 return "DW_AT_inline";
13192 case DW_AT_is_optional:
13193 return "DW_AT_is_optional";
13194 case DW_AT_lower_bound:
13195 return "DW_AT_lower_bound";
13196 case DW_AT_producer:
13197 return "DW_AT_producer";
13198 case DW_AT_prototyped:
13199 return "DW_AT_prototyped";
13200 case DW_AT_return_addr:
13201 return "DW_AT_return_addr";
13202 case DW_AT_start_scope:
13203 return "DW_AT_start_scope";
09fa0d7c
JK
13204 case DW_AT_bit_stride:
13205 return "DW_AT_bit_stride";
c906108c
SS
13206 case DW_AT_upper_bound:
13207 return "DW_AT_upper_bound";
13208 case DW_AT_abstract_origin:
13209 return "DW_AT_abstract_origin";
13210 case DW_AT_accessibility:
13211 return "DW_AT_accessibility";
13212 case DW_AT_address_class:
13213 return "DW_AT_address_class";
13214 case DW_AT_artificial:
13215 return "DW_AT_artificial";
13216 case DW_AT_base_types:
13217 return "DW_AT_base_types";
13218 case DW_AT_calling_convention:
13219 return "DW_AT_calling_convention";
13220 case DW_AT_count:
13221 return "DW_AT_count";
13222 case DW_AT_data_member_location:
13223 return "DW_AT_data_member_location";
13224 case DW_AT_decl_column:
13225 return "DW_AT_decl_column";
13226 case DW_AT_decl_file:
13227 return "DW_AT_decl_file";
13228 case DW_AT_decl_line:
13229 return "DW_AT_decl_line";
13230 case DW_AT_declaration:
13231 return "DW_AT_declaration";
13232 case DW_AT_discr_list:
13233 return "DW_AT_discr_list";
13234 case DW_AT_encoding:
13235 return "DW_AT_encoding";
13236 case DW_AT_external:
13237 return "DW_AT_external";
13238 case DW_AT_frame_base:
13239 return "DW_AT_frame_base";
13240 case DW_AT_friend:
13241 return "DW_AT_friend";
13242 case DW_AT_identifier_case:
13243 return "DW_AT_identifier_case";
13244 case DW_AT_macro_info:
13245 return "DW_AT_macro_info";
13246 case DW_AT_namelist_items:
13247 return "DW_AT_namelist_items";
13248 case DW_AT_priority:
13249 return "DW_AT_priority";
13250 case DW_AT_segment:
13251 return "DW_AT_segment";
13252 case DW_AT_specification:
13253 return "DW_AT_specification";
13254 case DW_AT_static_link:
13255 return "DW_AT_static_link";
13256 case DW_AT_type:
13257 return "DW_AT_type";
13258 case DW_AT_use_location:
13259 return "DW_AT_use_location";
13260 case DW_AT_variable_parameter:
13261 return "DW_AT_variable_parameter";
13262 case DW_AT_virtuality:
13263 return "DW_AT_virtuality";
13264 case DW_AT_vtable_elem_location:
13265 return "DW_AT_vtable_elem_location";
b7619582 13266 /* DWARF 3 values. */
d9fa45fe
DC
13267 case DW_AT_allocated:
13268 return "DW_AT_allocated";
13269 case DW_AT_associated:
13270 return "DW_AT_associated";
13271 case DW_AT_data_location:
13272 return "DW_AT_data_location";
09fa0d7c
JK
13273 case DW_AT_byte_stride:
13274 return "DW_AT_byte_stride";
d9fa45fe
DC
13275 case DW_AT_entry_pc:
13276 return "DW_AT_entry_pc";
13277 case DW_AT_use_UTF8:
13278 return "DW_AT_use_UTF8";
13279 case DW_AT_extension:
13280 return "DW_AT_extension";
13281 case DW_AT_ranges:
13282 return "DW_AT_ranges";
13283 case DW_AT_trampoline:
13284 return "DW_AT_trampoline";
13285 case DW_AT_call_column:
13286 return "DW_AT_call_column";
13287 case DW_AT_call_file:
13288 return "DW_AT_call_file";
13289 case DW_AT_call_line:
13290 return "DW_AT_call_line";
b7619582
GF
13291 case DW_AT_description:
13292 return "DW_AT_description";
13293 case DW_AT_binary_scale:
13294 return "DW_AT_binary_scale";
13295 case DW_AT_decimal_scale:
13296 return "DW_AT_decimal_scale";
13297 case DW_AT_small:
13298 return "DW_AT_small";
13299 case DW_AT_decimal_sign:
13300 return "DW_AT_decimal_sign";
13301 case DW_AT_digit_count:
13302 return "DW_AT_digit_count";
13303 case DW_AT_picture_string:
13304 return "DW_AT_picture_string";
13305 case DW_AT_mutable:
13306 return "DW_AT_mutable";
13307 case DW_AT_threads_scaled:
13308 return "DW_AT_threads_scaled";
13309 case DW_AT_explicit:
13310 return "DW_AT_explicit";
13311 case DW_AT_object_pointer:
13312 return "DW_AT_object_pointer";
13313 case DW_AT_endianity:
13314 return "DW_AT_endianity";
13315 case DW_AT_elemental:
13316 return "DW_AT_elemental";
13317 case DW_AT_pure:
13318 return "DW_AT_pure";
13319 case DW_AT_recursive:
13320 return "DW_AT_recursive";
348e048f
DE
13321 /* DWARF 4 values. */
13322 case DW_AT_signature:
13323 return "DW_AT_signature";
31ef98ae
TT
13324 case DW_AT_linkage_name:
13325 return "DW_AT_linkage_name";
b7619582 13326 /* SGI/MIPS extensions. */
c764a876 13327#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13328 case DW_AT_MIPS_fde:
13329 return "DW_AT_MIPS_fde";
c764a876 13330#endif
c906108c
SS
13331 case DW_AT_MIPS_loop_begin:
13332 return "DW_AT_MIPS_loop_begin";
13333 case DW_AT_MIPS_tail_loop_begin:
13334 return "DW_AT_MIPS_tail_loop_begin";
13335 case DW_AT_MIPS_epilog_begin:
13336 return "DW_AT_MIPS_epilog_begin";
13337 case DW_AT_MIPS_loop_unroll_factor:
13338 return "DW_AT_MIPS_loop_unroll_factor";
13339 case DW_AT_MIPS_software_pipeline_depth:
13340 return "DW_AT_MIPS_software_pipeline_depth";
13341 case DW_AT_MIPS_linkage_name:
13342 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13343 case DW_AT_MIPS_stride:
13344 return "DW_AT_MIPS_stride";
13345 case DW_AT_MIPS_abstract_name:
13346 return "DW_AT_MIPS_abstract_name";
13347 case DW_AT_MIPS_clone_origin:
13348 return "DW_AT_MIPS_clone_origin";
13349 case DW_AT_MIPS_has_inlines:
13350 return "DW_AT_MIPS_has_inlines";
b7619582 13351 /* HP extensions. */
c764a876 13352#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13353 case DW_AT_HP_block_index:
13354 return "DW_AT_HP_block_index";
c764a876 13355#endif
b7619582
GF
13356 case DW_AT_HP_unmodifiable:
13357 return "DW_AT_HP_unmodifiable";
13358 case DW_AT_HP_actuals_stmt_list:
13359 return "DW_AT_HP_actuals_stmt_list";
13360 case DW_AT_HP_proc_per_section:
13361 return "DW_AT_HP_proc_per_section";
13362 case DW_AT_HP_raw_data_ptr:
13363 return "DW_AT_HP_raw_data_ptr";
13364 case DW_AT_HP_pass_by_reference:
13365 return "DW_AT_HP_pass_by_reference";
13366 case DW_AT_HP_opt_level:
13367 return "DW_AT_HP_opt_level";
13368 case DW_AT_HP_prof_version_id:
13369 return "DW_AT_HP_prof_version_id";
13370 case DW_AT_HP_opt_flags:
13371 return "DW_AT_HP_opt_flags";
13372 case DW_AT_HP_cold_region_low_pc:
13373 return "DW_AT_HP_cold_region_low_pc";
13374 case DW_AT_HP_cold_region_high_pc:
13375 return "DW_AT_HP_cold_region_high_pc";
13376 case DW_AT_HP_all_variables_modifiable:
13377 return "DW_AT_HP_all_variables_modifiable";
13378 case DW_AT_HP_linkage_name:
13379 return "DW_AT_HP_linkage_name";
13380 case DW_AT_HP_prof_flags:
13381 return "DW_AT_HP_prof_flags";
13382 /* GNU extensions. */
c906108c
SS
13383 case DW_AT_sf_names:
13384 return "DW_AT_sf_names";
13385 case DW_AT_src_info:
13386 return "DW_AT_src_info";
13387 case DW_AT_mac_info:
13388 return "DW_AT_mac_info";
13389 case DW_AT_src_coords:
13390 return "DW_AT_src_coords";
13391 case DW_AT_body_begin:
13392 return "DW_AT_body_begin";
13393 case DW_AT_body_end:
13394 return "DW_AT_body_end";
f5f8a009
EZ
13395 case DW_AT_GNU_vector:
13396 return "DW_AT_GNU_vector";
2de00c64
DE
13397 case DW_AT_GNU_odr_signature:
13398 return "DW_AT_GNU_odr_signature";
b7619582
GF
13399 /* VMS extensions. */
13400 case DW_AT_VMS_rtnbeg_pd_address:
13401 return "DW_AT_VMS_rtnbeg_pd_address";
13402 /* UPC extension. */
13403 case DW_AT_upc_threads_scaled:
13404 return "DW_AT_upc_threads_scaled";
13405 /* PGI (STMicroelectronics) extensions. */
13406 case DW_AT_PGI_lbase:
13407 return "DW_AT_PGI_lbase";
13408 case DW_AT_PGI_soffset:
13409 return "DW_AT_PGI_soffset";
13410 case DW_AT_PGI_lstride:
13411 return "DW_AT_PGI_lstride";
c906108c
SS
13412 default:
13413 return "DW_AT_<unknown>";
13414 }
13415}
13416
13417/* Convert a DWARF value form code into its string name. */
13418
13419static char *
aa1ee363 13420dwarf_form_name (unsigned form)
c906108c
SS
13421{
13422 switch (form)
13423 {
13424 case DW_FORM_addr:
13425 return "DW_FORM_addr";
13426 case DW_FORM_block2:
13427 return "DW_FORM_block2";
13428 case DW_FORM_block4:
13429 return "DW_FORM_block4";
13430 case DW_FORM_data2:
13431 return "DW_FORM_data2";
13432 case DW_FORM_data4:
13433 return "DW_FORM_data4";
13434 case DW_FORM_data8:
13435 return "DW_FORM_data8";
13436 case DW_FORM_string:
13437 return "DW_FORM_string";
13438 case DW_FORM_block:
13439 return "DW_FORM_block";
13440 case DW_FORM_block1:
13441 return "DW_FORM_block1";
13442 case DW_FORM_data1:
13443 return "DW_FORM_data1";
13444 case DW_FORM_flag:
13445 return "DW_FORM_flag";
13446 case DW_FORM_sdata:
13447 return "DW_FORM_sdata";
13448 case DW_FORM_strp:
13449 return "DW_FORM_strp";
13450 case DW_FORM_udata:
13451 return "DW_FORM_udata";
13452 case DW_FORM_ref_addr:
13453 return "DW_FORM_ref_addr";
13454 case DW_FORM_ref1:
13455 return "DW_FORM_ref1";
13456 case DW_FORM_ref2:
13457 return "DW_FORM_ref2";
13458 case DW_FORM_ref4:
13459 return "DW_FORM_ref4";
13460 case DW_FORM_ref8:
13461 return "DW_FORM_ref8";
13462 case DW_FORM_ref_udata:
13463 return "DW_FORM_ref_udata";
13464 case DW_FORM_indirect:
13465 return "DW_FORM_indirect";
348e048f
DE
13466 case DW_FORM_sec_offset:
13467 return "DW_FORM_sec_offset";
13468 case DW_FORM_exprloc:
13469 return "DW_FORM_exprloc";
13470 case DW_FORM_flag_present:
13471 return "DW_FORM_flag_present";
55f1336d
TT
13472 case DW_FORM_ref_sig8:
13473 return "DW_FORM_ref_sig8";
c906108c
SS
13474 default:
13475 return "DW_FORM_<unknown>";
13476 }
13477}
13478
13479/* Convert a DWARF stack opcode into its string name. */
13480
9eae7c52 13481const char *
b1bfef65 13482dwarf_stack_op_name (unsigned op)
c906108c
SS
13483{
13484 switch (op)
13485 {
13486 case DW_OP_addr:
13487 return "DW_OP_addr";
13488 case DW_OP_deref:
13489 return "DW_OP_deref";
13490 case DW_OP_const1u:
13491 return "DW_OP_const1u";
13492 case DW_OP_const1s:
13493 return "DW_OP_const1s";
13494 case DW_OP_const2u:
13495 return "DW_OP_const2u";
13496 case DW_OP_const2s:
13497 return "DW_OP_const2s";
13498 case DW_OP_const4u:
13499 return "DW_OP_const4u";
13500 case DW_OP_const4s:
13501 return "DW_OP_const4s";
13502 case DW_OP_const8u:
13503 return "DW_OP_const8u";
13504 case DW_OP_const8s:
13505 return "DW_OP_const8s";
13506 case DW_OP_constu:
13507 return "DW_OP_constu";
13508 case DW_OP_consts:
13509 return "DW_OP_consts";
13510 case DW_OP_dup:
13511 return "DW_OP_dup";
13512 case DW_OP_drop:
13513 return "DW_OP_drop";
13514 case DW_OP_over:
13515 return "DW_OP_over";
13516 case DW_OP_pick:
13517 return "DW_OP_pick";
13518 case DW_OP_swap:
13519 return "DW_OP_swap";
13520 case DW_OP_rot:
13521 return "DW_OP_rot";
13522 case DW_OP_xderef:
13523 return "DW_OP_xderef";
13524 case DW_OP_abs:
13525 return "DW_OP_abs";
13526 case DW_OP_and:
13527 return "DW_OP_and";
13528 case DW_OP_div:
13529 return "DW_OP_div";
13530 case DW_OP_minus:
13531 return "DW_OP_minus";
13532 case DW_OP_mod:
13533 return "DW_OP_mod";
13534 case DW_OP_mul:
13535 return "DW_OP_mul";
13536 case DW_OP_neg:
13537 return "DW_OP_neg";
13538 case DW_OP_not:
13539 return "DW_OP_not";
13540 case DW_OP_or:
13541 return "DW_OP_or";
13542 case DW_OP_plus:
13543 return "DW_OP_plus";
13544 case DW_OP_plus_uconst:
13545 return "DW_OP_plus_uconst";
13546 case DW_OP_shl:
13547 return "DW_OP_shl";
13548 case DW_OP_shr:
13549 return "DW_OP_shr";
13550 case DW_OP_shra:
13551 return "DW_OP_shra";
13552 case DW_OP_xor:
13553 return "DW_OP_xor";
13554 case DW_OP_bra:
13555 return "DW_OP_bra";
13556 case DW_OP_eq:
13557 return "DW_OP_eq";
13558 case DW_OP_ge:
13559 return "DW_OP_ge";
13560 case DW_OP_gt:
13561 return "DW_OP_gt";
13562 case DW_OP_le:
13563 return "DW_OP_le";
13564 case DW_OP_lt:
13565 return "DW_OP_lt";
13566 case DW_OP_ne:
13567 return "DW_OP_ne";
13568 case DW_OP_skip:
13569 return "DW_OP_skip";
13570 case DW_OP_lit0:
13571 return "DW_OP_lit0";
13572 case DW_OP_lit1:
13573 return "DW_OP_lit1";
13574 case DW_OP_lit2:
13575 return "DW_OP_lit2";
13576 case DW_OP_lit3:
13577 return "DW_OP_lit3";
13578 case DW_OP_lit4:
13579 return "DW_OP_lit4";
13580 case DW_OP_lit5:
13581 return "DW_OP_lit5";
13582 case DW_OP_lit6:
13583 return "DW_OP_lit6";
13584 case DW_OP_lit7:
13585 return "DW_OP_lit7";
13586 case DW_OP_lit8:
13587 return "DW_OP_lit8";
13588 case DW_OP_lit9:
13589 return "DW_OP_lit9";
13590 case DW_OP_lit10:
13591 return "DW_OP_lit10";
13592 case DW_OP_lit11:
13593 return "DW_OP_lit11";
13594 case DW_OP_lit12:
13595 return "DW_OP_lit12";
13596 case DW_OP_lit13:
13597 return "DW_OP_lit13";
13598 case DW_OP_lit14:
13599 return "DW_OP_lit14";
13600 case DW_OP_lit15:
13601 return "DW_OP_lit15";
13602 case DW_OP_lit16:
13603 return "DW_OP_lit16";
13604 case DW_OP_lit17:
13605 return "DW_OP_lit17";
13606 case DW_OP_lit18:
13607 return "DW_OP_lit18";
13608 case DW_OP_lit19:
13609 return "DW_OP_lit19";
13610 case DW_OP_lit20:
13611 return "DW_OP_lit20";
13612 case DW_OP_lit21:
13613 return "DW_OP_lit21";
13614 case DW_OP_lit22:
13615 return "DW_OP_lit22";
13616 case DW_OP_lit23:
13617 return "DW_OP_lit23";
13618 case DW_OP_lit24:
13619 return "DW_OP_lit24";
13620 case DW_OP_lit25:
13621 return "DW_OP_lit25";
13622 case DW_OP_lit26:
13623 return "DW_OP_lit26";
13624 case DW_OP_lit27:
13625 return "DW_OP_lit27";
13626 case DW_OP_lit28:
13627 return "DW_OP_lit28";
13628 case DW_OP_lit29:
13629 return "DW_OP_lit29";
13630 case DW_OP_lit30:
13631 return "DW_OP_lit30";
13632 case DW_OP_lit31:
13633 return "DW_OP_lit31";
13634 case DW_OP_reg0:
13635 return "DW_OP_reg0";
13636 case DW_OP_reg1:
13637 return "DW_OP_reg1";
13638 case DW_OP_reg2:
13639 return "DW_OP_reg2";
13640 case DW_OP_reg3:
13641 return "DW_OP_reg3";
13642 case DW_OP_reg4:
13643 return "DW_OP_reg4";
13644 case DW_OP_reg5:
13645 return "DW_OP_reg5";
13646 case DW_OP_reg6:
13647 return "DW_OP_reg6";
13648 case DW_OP_reg7:
13649 return "DW_OP_reg7";
13650 case DW_OP_reg8:
13651 return "DW_OP_reg8";
13652 case DW_OP_reg9:
13653 return "DW_OP_reg9";
13654 case DW_OP_reg10:
13655 return "DW_OP_reg10";
13656 case DW_OP_reg11:
13657 return "DW_OP_reg11";
13658 case DW_OP_reg12:
13659 return "DW_OP_reg12";
13660 case DW_OP_reg13:
13661 return "DW_OP_reg13";
13662 case DW_OP_reg14:
13663 return "DW_OP_reg14";
13664 case DW_OP_reg15:
13665 return "DW_OP_reg15";
13666 case DW_OP_reg16:
13667 return "DW_OP_reg16";
13668 case DW_OP_reg17:
13669 return "DW_OP_reg17";
13670 case DW_OP_reg18:
13671 return "DW_OP_reg18";
13672 case DW_OP_reg19:
13673 return "DW_OP_reg19";
13674 case DW_OP_reg20:
13675 return "DW_OP_reg20";
13676 case DW_OP_reg21:
13677 return "DW_OP_reg21";
13678 case DW_OP_reg22:
13679 return "DW_OP_reg22";
13680 case DW_OP_reg23:
13681 return "DW_OP_reg23";
13682 case DW_OP_reg24:
13683 return "DW_OP_reg24";
13684 case DW_OP_reg25:
13685 return "DW_OP_reg25";
13686 case DW_OP_reg26:
13687 return "DW_OP_reg26";
13688 case DW_OP_reg27:
13689 return "DW_OP_reg27";
13690 case DW_OP_reg28:
13691 return "DW_OP_reg28";
13692 case DW_OP_reg29:
13693 return "DW_OP_reg29";
13694 case DW_OP_reg30:
13695 return "DW_OP_reg30";
13696 case DW_OP_reg31:
13697 return "DW_OP_reg31";
13698 case DW_OP_breg0:
13699 return "DW_OP_breg0";
13700 case DW_OP_breg1:
13701 return "DW_OP_breg1";
13702 case DW_OP_breg2:
13703 return "DW_OP_breg2";
13704 case DW_OP_breg3:
13705 return "DW_OP_breg3";
13706 case DW_OP_breg4:
13707 return "DW_OP_breg4";
13708 case DW_OP_breg5:
13709 return "DW_OP_breg5";
13710 case DW_OP_breg6:
13711 return "DW_OP_breg6";
13712 case DW_OP_breg7:
13713 return "DW_OP_breg7";
13714 case DW_OP_breg8:
13715 return "DW_OP_breg8";
13716 case DW_OP_breg9:
13717 return "DW_OP_breg9";
13718 case DW_OP_breg10:
13719 return "DW_OP_breg10";
13720 case DW_OP_breg11:
13721 return "DW_OP_breg11";
13722 case DW_OP_breg12:
13723 return "DW_OP_breg12";
13724 case DW_OP_breg13:
13725 return "DW_OP_breg13";
13726 case DW_OP_breg14:
13727 return "DW_OP_breg14";
13728 case DW_OP_breg15:
13729 return "DW_OP_breg15";
13730 case DW_OP_breg16:
13731 return "DW_OP_breg16";
13732 case DW_OP_breg17:
13733 return "DW_OP_breg17";
13734 case DW_OP_breg18:
13735 return "DW_OP_breg18";
13736 case DW_OP_breg19:
13737 return "DW_OP_breg19";
13738 case DW_OP_breg20:
13739 return "DW_OP_breg20";
13740 case DW_OP_breg21:
13741 return "DW_OP_breg21";
13742 case DW_OP_breg22:
13743 return "DW_OP_breg22";
13744 case DW_OP_breg23:
13745 return "DW_OP_breg23";
13746 case DW_OP_breg24:
13747 return "DW_OP_breg24";
13748 case DW_OP_breg25:
13749 return "DW_OP_breg25";
13750 case DW_OP_breg26:
13751 return "DW_OP_breg26";
13752 case DW_OP_breg27:
13753 return "DW_OP_breg27";
13754 case DW_OP_breg28:
13755 return "DW_OP_breg28";
13756 case DW_OP_breg29:
13757 return "DW_OP_breg29";
13758 case DW_OP_breg30:
13759 return "DW_OP_breg30";
13760 case DW_OP_breg31:
13761 return "DW_OP_breg31";
13762 case DW_OP_regx:
13763 return "DW_OP_regx";
13764 case DW_OP_fbreg:
13765 return "DW_OP_fbreg";
13766 case DW_OP_bregx:
13767 return "DW_OP_bregx";
13768 case DW_OP_piece:
13769 return "DW_OP_piece";
13770 case DW_OP_deref_size:
13771 return "DW_OP_deref_size";
13772 case DW_OP_xderef_size:
13773 return "DW_OP_xderef_size";
13774 case DW_OP_nop:
13775 return "DW_OP_nop";
b7619582 13776 /* DWARF 3 extensions. */
ed348acc
EZ
13777 case DW_OP_push_object_address:
13778 return "DW_OP_push_object_address";
13779 case DW_OP_call2:
13780 return "DW_OP_call2";
13781 case DW_OP_call4:
13782 return "DW_OP_call4";
13783 case DW_OP_call_ref:
13784 return "DW_OP_call_ref";
b7619582
GF
13785 case DW_OP_form_tls_address:
13786 return "DW_OP_form_tls_address";
13787 case DW_OP_call_frame_cfa:
13788 return "DW_OP_call_frame_cfa";
13789 case DW_OP_bit_piece:
13790 return "DW_OP_bit_piece";
9eae7c52
TT
13791 /* DWARF 4 extensions. */
13792 case DW_OP_implicit_value:
13793 return "DW_OP_implicit_value";
13794 case DW_OP_stack_value:
13795 return "DW_OP_stack_value";
13796 /* GNU extensions. */
ed348acc
EZ
13797 case DW_OP_GNU_push_tls_address:
13798 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13799 case DW_OP_GNU_uninit:
13800 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13801 case DW_OP_GNU_implicit_pointer:
13802 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13803 case DW_OP_GNU_entry_value:
13804 return "DW_OP_GNU_entry_value";
13805 case DW_OP_GNU_const_type:
13806 return "DW_OP_GNU_const_type";
13807 case DW_OP_GNU_regval_type:
13808 return "DW_OP_GNU_regval_type";
13809 case DW_OP_GNU_deref_type:
13810 return "DW_OP_GNU_deref_type";
13811 case DW_OP_GNU_convert:
13812 return "DW_OP_GNU_convert";
13813 case DW_OP_GNU_reinterpret:
13814 return "DW_OP_GNU_reinterpret";
c906108c 13815 default:
b1bfef65 13816 return NULL;
c906108c
SS
13817 }
13818}
13819
13820static char *
fba45db2 13821dwarf_bool_name (unsigned mybool)
c906108c
SS
13822{
13823 if (mybool)
13824 return "TRUE";
13825 else
13826 return "FALSE";
13827}
13828
13829/* Convert a DWARF type code into its string name. */
13830
13831static char *
aa1ee363 13832dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13833{
13834 switch (enc)
13835 {
b7619582
GF
13836 case DW_ATE_void:
13837 return "DW_ATE_void";
c906108c
SS
13838 case DW_ATE_address:
13839 return "DW_ATE_address";
13840 case DW_ATE_boolean:
13841 return "DW_ATE_boolean";
13842 case DW_ATE_complex_float:
13843 return "DW_ATE_complex_float";
13844 case DW_ATE_float:
13845 return "DW_ATE_float";
13846 case DW_ATE_signed:
13847 return "DW_ATE_signed";
13848 case DW_ATE_signed_char:
13849 return "DW_ATE_signed_char";
13850 case DW_ATE_unsigned:
13851 return "DW_ATE_unsigned";
13852 case DW_ATE_unsigned_char:
13853 return "DW_ATE_unsigned_char";
b7619582 13854 /* DWARF 3. */
d9fa45fe
DC
13855 case DW_ATE_imaginary_float:
13856 return "DW_ATE_imaginary_float";
b7619582
GF
13857 case DW_ATE_packed_decimal:
13858 return "DW_ATE_packed_decimal";
13859 case DW_ATE_numeric_string:
13860 return "DW_ATE_numeric_string";
13861 case DW_ATE_edited:
13862 return "DW_ATE_edited";
13863 case DW_ATE_signed_fixed:
13864 return "DW_ATE_signed_fixed";
13865 case DW_ATE_unsigned_fixed:
13866 return "DW_ATE_unsigned_fixed";
13867 case DW_ATE_decimal_float:
13868 return "DW_ATE_decimal_float";
75079b2b
TT
13869 /* DWARF 4. */
13870 case DW_ATE_UTF:
13871 return "DW_ATE_UTF";
b7619582
GF
13872 /* HP extensions. */
13873 case DW_ATE_HP_float80:
13874 return "DW_ATE_HP_float80";
13875 case DW_ATE_HP_complex_float80:
13876 return "DW_ATE_HP_complex_float80";
13877 case DW_ATE_HP_float128:
13878 return "DW_ATE_HP_float128";
13879 case DW_ATE_HP_complex_float128:
13880 return "DW_ATE_HP_complex_float128";
13881 case DW_ATE_HP_floathpintel:
13882 return "DW_ATE_HP_floathpintel";
13883 case DW_ATE_HP_imaginary_float80:
13884 return "DW_ATE_HP_imaginary_float80";
13885 case DW_ATE_HP_imaginary_float128:
13886 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13887 default:
13888 return "DW_ATE_<unknown>";
13889 }
13890}
13891
0963b4bd 13892/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13893
13894#if 0
13895static char *
aa1ee363 13896dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13897{
13898 switch (cfi_opc)
13899 {
13900 case DW_CFA_advance_loc:
13901 return "DW_CFA_advance_loc";
13902 case DW_CFA_offset:
13903 return "DW_CFA_offset";
13904 case DW_CFA_restore:
13905 return "DW_CFA_restore";
13906 case DW_CFA_nop:
13907 return "DW_CFA_nop";
13908 case DW_CFA_set_loc:
13909 return "DW_CFA_set_loc";
13910 case DW_CFA_advance_loc1:
13911 return "DW_CFA_advance_loc1";
13912 case DW_CFA_advance_loc2:
13913 return "DW_CFA_advance_loc2";
13914 case DW_CFA_advance_loc4:
13915 return "DW_CFA_advance_loc4";
13916 case DW_CFA_offset_extended:
13917 return "DW_CFA_offset_extended";
13918 case DW_CFA_restore_extended:
13919 return "DW_CFA_restore_extended";
13920 case DW_CFA_undefined:
13921 return "DW_CFA_undefined";
13922 case DW_CFA_same_value:
13923 return "DW_CFA_same_value";
13924 case DW_CFA_register:
13925 return "DW_CFA_register";
13926 case DW_CFA_remember_state:
13927 return "DW_CFA_remember_state";
13928 case DW_CFA_restore_state:
13929 return "DW_CFA_restore_state";
13930 case DW_CFA_def_cfa:
13931 return "DW_CFA_def_cfa";
13932 case DW_CFA_def_cfa_register:
13933 return "DW_CFA_def_cfa_register";
13934 case DW_CFA_def_cfa_offset:
13935 return "DW_CFA_def_cfa_offset";
b7619582 13936 /* DWARF 3. */
985cb1a3
JM
13937 case DW_CFA_def_cfa_expression:
13938 return "DW_CFA_def_cfa_expression";
13939 case DW_CFA_expression:
13940 return "DW_CFA_expression";
13941 case DW_CFA_offset_extended_sf:
13942 return "DW_CFA_offset_extended_sf";
13943 case DW_CFA_def_cfa_sf:
13944 return "DW_CFA_def_cfa_sf";
13945 case DW_CFA_def_cfa_offset_sf:
13946 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13947 case DW_CFA_val_offset:
13948 return "DW_CFA_val_offset";
13949 case DW_CFA_val_offset_sf:
13950 return "DW_CFA_val_offset_sf";
13951 case DW_CFA_val_expression:
13952 return "DW_CFA_val_expression";
13953 /* SGI/MIPS specific. */
c906108c
SS
13954 case DW_CFA_MIPS_advance_loc8:
13955 return "DW_CFA_MIPS_advance_loc8";
b7619582 13956 /* GNU extensions. */
985cb1a3
JM
13957 case DW_CFA_GNU_window_save:
13958 return "DW_CFA_GNU_window_save";
13959 case DW_CFA_GNU_args_size:
13960 return "DW_CFA_GNU_args_size";
13961 case DW_CFA_GNU_negative_offset_extended:
13962 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13963 default:
13964 return "DW_CFA_<unknown>";
13965 }
13966}
13967#endif
13968
f9aca02d 13969static void
d97bc12b 13970dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13971{
13972 unsigned int i;
13973
d97bc12b
DE
13974 print_spaces (indent, f);
13975 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13976 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13977
13978 if (die->parent != NULL)
13979 {
13980 print_spaces (indent, f);
13981 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13982 die->parent->offset);
13983 }
13984
13985 print_spaces (indent, f);
13986 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13987 dwarf_bool_name (die->child != NULL));
c906108c 13988
d97bc12b
DE
13989 print_spaces (indent, f);
13990 fprintf_unfiltered (f, " attributes:\n");
13991
c906108c
SS
13992 for (i = 0; i < die->num_attrs; ++i)
13993 {
d97bc12b
DE
13994 print_spaces (indent, f);
13995 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13996 dwarf_attr_name (die->attrs[i].name),
13997 dwarf_form_name (die->attrs[i].form));
d97bc12b 13998
c906108c
SS
13999 switch (die->attrs[i].form)
14000 {
14001 case DW_FORM_ref_addr:
14002 case DW_FORM_addr:
d97bc12b 14003 fprintf_unfiltered (f, "address: ");
5af949e3 14004 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
14005 break;
14006 case DW_FORM_block2:
14007 case DW_FORM_block4:
14008 case DW_FORM_block:
14009 case DW_FORM_block1:
3e43a32a
MS
14010 fprintf_unfiltered (f, "block: size %d",
14011 DW_BLOCK (&die->attrs[i])->size);
c906108c 14012 break;
2dc7f7b3
TT
14013 case DW_FORM_exprloc:
14014 fprintf_unfiltered (f, "expression: size %u",
14015 DW_BLOCK (&die->attrs[i])->size);
14016 break;
10b3939b
DJ
14017 case DW_FORM_ref1:
14018 case DW_FORM_ref2:
14019 case DW_FORM_ref4:
d97bc12b 14020 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
14021 (long) (DW_ADDR (&die->attrs[i])));
14022 break;
c906108c
SS
14023 case DW_FORM_data1:
14024 case DW_FORM_data2:
14025 case DW_FORM_data4:
ce5d95e1 14026 case DW_FORM_data8:
c906108c
SS
14027 case DW_FORM_udata:
14028 case DW_FORM_sdata:
43bbcdc2
PH
14029 fprintf_unfiltered (f, "constant: %s",
14030 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 14031 break;
2dc7f7b3
TT
14032 case DW_FORM_sec_offset:
14033 fprintf_unfiltered (f, "section offset: %s",
14034 pulongest (DW_UNSND (&die->attrs[i])));
14035 break;
55f1336d 14036 case DW_FORM_ref_sig8:
348e048f
DE
14037 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14038 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 14039 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
14040 else
14041 fprintf_unfiltered (f, "signatured type, offset: unknown");
14042 break;
c906108c 14043 case DW_FORM_string:
4bdf3d34 14044 case DW_FORM_strp:
8285870a 14045 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14046 DW_STRING (&die->attrs[i])
8285870a
JK
14047 ? DW_STRING (&die->attrs[i]) : "",
14048 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14049 break;
14050 case DW_FORM_flag:
14051 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14052 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14053 else
d97bc12b 14054 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14055 break;
2dc7f7b3
TT
14056 case DW_FORM_flag_present:
14057 fprintf_unfiltered (f, "flag: TRUE");
14058 break;
a8329558 14059 case DW_FORM_indirect:
0963b4bd
MS
14060 /* The reader will have reduced the indirect form to
14061 the "base form" so this form should not occur. */
3e43a32a
MS
14062 fprintf_unfiltered (f,
14063 "unexpected attribute form: DW_FORM_indirect");
a8329558 14064 break;
c906108c 14065 default:
d97bc12b 14066 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14067 die->attrs[i].form);
d97bc12b 14068 break;
c906108c 14069 }
d97bc12b 14070 fprintf_unfiltered (f, "\n");
c906108c
SS
14071 }
14072}
14073
f9aca02d 14074static void
d97bc12b 14075dump_die_for_error (struct die_info *die)
c906108c 14076{
d97bc12b
DE
14077 dump_die_shallow (gdb_stderr, 0, die);
14078}
14079
14080static void
14081dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14082{
14083 int indent = level * 4;
14084
14085 gdb_assert (die != NULL);
14086
14087 if (level >= max_level)
14088 return;
14089
14090 dump_die_shallow (f, indent, die);
14091
14092 if (die->child != NULL)
c906108c 14093 {
d97bc12b
DE
14094 print_spaces (indent, f);
14095 fprintf_unfiltered (f, " Children:");
14096 if (level + 1 < max_level)
14097 {
14098 fprintf_unfiltered (f, "\n");
14099 dump_die_1 (f, level + 1, max_level, die->child);
14100 }
14101 else
14102 {
3e43a32a
MS
14103 fprintf_unfiltered (f,
14104 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14105 }
14106 }
14107
14108 if (die->sibling != NULL && level > 0)
14109 {
14110 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14111 }
14112}
14113
d97bc12b
DE
14114/* This is called from the pdie macro in gdbinit.in.
14115 It's not static so gcc will keep a copy callable from gdb. */
14116
14117void
14118dump_die (struct die_info *die, int max_level)
14119{
14120 dump_die_1 (gdb_stdlog, 0, max_level, die);
14121}
14122
f9aca02d 14123static void
51545339 14124store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14125{
51545339 14126 void **slot;
c906108c 14127
51545339
DJ
14128 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14129
14130 *slot = die;
c906108c
SS
14131}
14132
93311388
DE
14133static int
14134is_ref_attr (struct attribute *attr)
c906108c 14135{
c906108c
SS
14136 switch (attr->form)
14137 {
14138 case DW_FORM_ref_addr:
c906108c
SS
14139 case DW_FORM_ref1:
14140 case DW_FORM_ref2:
14141 case DW_FORM_ref4:
613e1657 14142 case DW_FORM_ref8:
c906108c 14143 case DW_FORM_ref_udata:
93311388 14144 return 1;
c906108c 14145 default:
93311388 14146 return 0;
c906108c 14147 }
93311388
DE
14148}
14149
14150static unsigned int
14151dwarf2_get_ref_die_offset (struct attribute *attr)
14152{
14153 if (is_ref_attr (attr))
14154 return DW_ADDR (attr);
14155
14156 complaint (&symfile_complaints,
14157 _("unsupported die ref attribute form: '%s'"),
14158 dwarf_form_name (attr->form));
14159 return 0;
c906108c
SS
14160}
14161
43bbcdc2
PH
14162/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14163 * the value held by the attribute is not constant. */
a02abb62 14164
43bbcdc2 14165static LONGEST
a02abb62
JB
14166dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14167{
14168 if (attr->form == DW_FORM_sdata)
14169 return DW_SND (attr);
14170 else if (attr->form == DW_FORM_udata
14171 || attr->form == DW_FORM_data1
14172 || attr->form == DW_FORM_data2
14173 || attr->form == DW_FORM_data4
14174 || attr->form == DW_FORM_data8)
14175 return DW_UNSND (attr);
14176 else
14177 {
3e43a32a
MS
14178 complaint (&symfile_complaints,
14179 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14180 dwarf_form_name (attr->form));
14181 return default_value;
14182 }
14183}
14184
03dd20cc 14185/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14186 unit and add it to our queue.
14187 The result is non-zero if PER_CU was queued, otherwise the result is zero
14188 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14189
348e048f 14190static int
03dd20cc
DJ
14191maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14192 struct dwarf2_per_cu_data *per_cu)
14193{
98bfdba5
PA
14194 /* We may arrive here during partial symbol reading, if we need full
14195 DIEs to process an unusual case (e.g. template arguments). Do
14196 not queue PER_CU, just tell our caller to load its DIEs. */
14197 if (dwarf2_per_objfile->reading_partial_symbols)
14198 {
14199 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14200 return 1;
14201 return 0;
14202 }
14203
03dd20cc
DJ
14204 /* Mark the dependence relation so that we don't flush PER_CU
14205 too early. */
14206 dwarf2_add_dependence (this_cu, per_cu);
14207
14208 /* If it's already on the queue, we have nothing to do. */
14209 if (per_cu->queued)
348e048f 14210 return 0;
03dd20cc
DJ
14211
14212 /* If the compilation unit is already loaded, just mark it as
14213 used. */
14214 if (per_cu->cu != NULL)
14215 {
14216 per_cu->cu->last_used = 0;
348e048f 14217 return 0;
03dd20cc
DJ
14218 }
14219
14220 /* Add it to the queue. */
14221 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
14222
14223 return 1;
14224}
14225
14226/* Follow reference or signature attribute ATTR of SRC_DIE.
14227 On entry *REF_CU is the CU of SRC_DIE.
14228 On exit *REF_CU is the CU of the result. */
14229
14230static struct die_info *
14231follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14232 struct dwarf2_cu **ref_cu)
14233{
14234 struct die_info *die;
14235
14236 if (is_ref_attr (attr))
14237 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14238 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14239 die = follow_die_sig (src_die, attr, ref_cu);
14240 else
14241 {
14242 dump_die_for_error (src_die);
14243 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14244 (*ref_cu)->objfile->name);
14245 }
14246
14247 return die;
03dd20cc
DJ
14248}
14249
5c631832 14250/* Follow reference OFFSET.
673bfd45
DE
14251 On entry *REF_CU is the CU of the source die referencing OFFSET.
14252 On exit *REF_CU is the CU of the result.
14253 Returns NULL if OFFSET is invalid. */
f504f079 14254
f9aca02d 14255static struct die_info *
5c631832 14256follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 14257{
10b3939b 14258 struct die_info temp_die;
f2f0e013 14259 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14260
348e048f
DE
14261 gdb_assert (cu->per_cu != NULL);
14262
98bfdba5
PA
14263 target_cu = cu;
14264
b0df02fd 14265 if (cu->per_cu->debug_types_section)
348e048f
DE
14266 {
14267 /* .debug_types CUs cannot reference anything outside their CU.
14268 If they need to, they have to reference a signatured type via
55f1336d 14269 DW_FORM_ref_sig8. */
348e048f 14270 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14271 return NULL;
348e048f
DE
14272 }
14273 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14274 {
14275 struct dwarf2_per_cu_data *per_cu;
9a619af0 14276
45452591 14277 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14278
14279 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
14280 if (maybe_queue_comp_unit (cu, per_cu))
14281 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 14282
10b3939b
DJ
14283 target_cu = per_cu->cu;
14284 }
98bfdba5
PA
14285 else if (cu->dies == NULL)
14286 {
14287 /* We're loading full DIEs during partial symbol reading. */
14288 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14289 load_full_comp_unit (cu->per_cu, cu->objfile);
14290 }
c906108c 14291
f2f0e013 14292 *ref_cu = target_cu;
51545339 14293 temp_die.offset = offset;
5c631832
JK
14294 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14295}
10b3939b 14296
5c631832
JK
14297/* Follow reference attribute ATTR of SRC_DIE.
14298 On entry *REF_CU is the CU of SRC_DIE.
14299 On exit *REF_CU is the CU of the result. */
14300
14301static struct die_info *
14302follow_die_ref (struct die_info *src_die, struct attribute *attr,
14303 struct dwarf2_cu **ref_cu)
14304{
14305 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14306 struct dwarf2_cu *cu = *ref_cu;
14307 struct die_info *die;
14308
14309 die = follow_die_offset (offset, ref_cu);
14310 if (!die)
14311 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14312 "at 0x%x [in module %s]"),
14313 offset, src_die->offset, cu->objfile->name);
348e048f 14314
5c631832
JK
14315 return die;
14316}
14317
d83e736b
JK
14318/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14319 Returned value is intended for DW_OP_call*. Returned
14320 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14321
14322struct dwarf2_locexpr_baton
14323dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
14324 struct dwarf2_per_cu_data *per_cu,
14325 CORE_ADDR (*get_frame_pc) (void *baton),
14326 void *baton)
5c631832 14327{
918dd910 14328 struct dwarf2_cu *cu;
5c631832
JK
14329 struct die_info *die;
14330 struct attribute *attr;
14331 struct dwarf2_locexpr_baton retval;
14332
8cf6f0b1
TT
14333 dw2_setup (per_cu->objfile);
14334
918dd910
JK
14335 if (per_cu->cu == NULL)
14336 load_cu (per_cu);
14337 cu = per_cu->cu;
14338
5c631832
JK
14339 die = follow_die_offset (offset, &cu);
14340 if (!die)
14341 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14342 offset, per_cu->cu->objfile->name);
14343
14344 attr = dwarf2_attr (die, DW_AT_location, cu);
14345 if (!attr)
14346 {
e103e986
JK
14347 /* DWARF: "If there is no such attribute, then there is no effect.".
14348 DATA is ignored if SIZE is 0. */
5c631832 14349
e103e986 14350 retval.data = NULL;
5c631832
JK
14351 retval.size = 0;
14352 }
8cf6f0b1
TT
14353 else if (attr_form_is_section_offset (attr))
14354 {
14355 struct dwarf2_loclist_baton loclist_baton;
14356 CORE_ADDR pc = (*get_frame_pc) (baton);
14357 size_t size;
14358
14359 fill_in_loclist_baton (cu, &loclist_baton, attr);
14360
14361 retval.data = dwarf2_find_location_expression (&loclist_baton,
14362 &size, pc);
14363 retval.size = size;
14364 }
5c631832
JK
14365 else
14366 {
14367 if (!attr_form_is_block (attr))
14368 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14369 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14370 offset, per_cu->cu->objfile->name);
14371
14372 retval.data = DW_BLOCK (attr)->data;
14373 retval.size = DW_BLOCK (attr)->size;
14374 }
14375 retval.per_cu = cu->per_cu;
918dd910 14376
918dd910
JK
14377 age_cached_comp_units ();
14378
5c631832 14379 return retval;
348e048f
DE
14380}
14381
8a9b8146
TT
14382/* Return the type of the DIE at DIE_OFFSET in the CU named by
14383 PER_CU. */
14384
14385struct type *
14386dwarf2_get_die_type (unsigned int die_offset,
14387 struct dwarf2_per_cu_data *per_cu)
14388{
8a9b8146 14389 dw2_setup (per_cu->objfile);
9ff3b74f 14390 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
14391}
14392
348e048f
DE
14393/* Follow the signature attribute ATTR in SRC_DIE.
14394 On entry *REF_CU is the CU of SRC_DIE.
14395 On exit *REF_CU is the CU of the result. */
14396
14397static struct die_info *
14398follow_die_sig (struct die_info *src_die, struct attribute *attr,
14399 struct dwarf2_cu **ref_cu)
14400{
14401 struct objfile *objfile = (*ref_cu)->objfile;
14402 struct die_info temp_die;
14403 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14404 struct dwarf2_cu *sig_cu;
14405 struct die_info *die;
14406
14407 /* sig_type will be NULL if the signatured type is missing from
14408 the debug info. */
14409 if (sig_type == NULL)
14410 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14411 "at 0x%x [in module %s]"),
14412 src_die->offset, objfile->name);
14413
14414 /* If necessary, add it to the queue and load its DIEs. */
14415
14416 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14417 read_signatured_type (objfile, sig_type);
14418
14419 gdb_assert (sig_type->per_cu.cu != NULL);
14420
14421 sig_cu = sig_type->per_cu.cu;
14422 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14423 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14424 if (die)
14425 {
14426 *ref_cu = sig_cu;
14427 return die;
14428 }
14429
3e43a32a
MS
14430 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14431 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14432 sig_type->type_offset, src_die->offset, objfile->name);
14433}
14434
14435/* Given an offset of a signatured type, return its signatured_type. */
14436
14437static struct signatured_type *
8b70b953
TT
14438lookup_signatured_type_at_offset (struct objfile *objfile,
14439 struct dwarf2_section_info *section,
14440 unsigned int offset)
348e048f 14441{
8b70b953 14442 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14443 unsigned int length, initial_length_size;
14444 unsigned int sig_offset;
14445 struct signatured_type find_entry, *type_sig;
14446
14447 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14448 sig_offset = (initial_length_size
14449 + 2 /*version*/
14450 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14451 + 1 /*address_size*/);
14452 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14453 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14454
14455 /* This is only used to lookup previously recorded types.
14456 If we didn't find it, it's our bug. */
14457 gdb_assert (type_sig != NULL);
b3c8eb43 14458 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14459
14460 return type_sig;
14461}
14462
14463/* Read in signatured type at OFFSET and build its CU and die(s). */
14464
14465static void
14466read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 14467 struct dwarf2_section_info *sect,
348e048f
DE
14468 unsigned int offset)
14469{
14470 struct signatured_type *type_sig;
14471
8b70b953 14472 dwarf2_read_section (objfile, sect);
be391dca 14473
348e048f
DE
14474 /* We have the section offset, but we need the signature to do the
14475 hash table lookup. */
8b70b953 14476 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14477
14478 gdb_assert (type_sig->per_cu.cu == NULL);
14479
14480 read_signatured_type (objfile, type_sig);
14481
14482 gdb_assert (type_sig->per_cu.cu != NULL);
14483}
14484
14485/* Read in a signatured type and build its CU and DIEs. */
14486
14487static void
14488read_signatured_type (struct objfile *objfile,
14489 struct signatured_type *type_sig)
14490{
1fd400ff 14491 gdb_byte *types_ptr;
348e048f
DE
14492 struct die_reader_specs reader_specs;
14493 struct dwarf2_cu *cu;
14494 ULONGEST signature;
14495 struct cleanup *back_to, *free_cu_cleanup;
b0df02fd 14496 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
348e048f 14497
8b70b953
TT
14498 dwarf2_read_section (objfile, section);
14499 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14500
348e048f
DE
14501 gdb_assert (type_sig->per_cu.cu == NULL);
14502
9816fde3
JK
14503 cu = xmalloc (sizeof (*cu));
14504 init_one_comp_unit (cu, objfile);
14505
348e048f
DE
14506 type_sig->per_cu.cu = cu;
14507 cu->per_cu = &type_sig->per_cu;
14508
14509 /* If an error occurs while loading, release our storage. */
14510 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14511
8b70b953 14512 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
348e048f
DE
14513 types_ptr, objfile->obfd);
14514 gdb_assert (signature == type_sig->signature);
14515
14516 cu->die_hash
14517 = htab_create_alloc_ex (cu->header.length / 12,
14518 die_hash,
14519 die_eq,
14520 NULL,
14521 &cu->comp_unit_obstack,
14522 hashtab_obstack_allocate,
14523 dummy_obstack_deallocate);
14524
14525 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14526 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14527
14528 init_cu_die_reader (&reader_specs, cu);
14529
14530 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14531 NULL /*parent*/);
14532
14533 /* We try not to read any attributes in this function, because not
14534 all objfiles needed for references have been loaded yet, and symbol
14535 table processing isn't initialized. But we have to set the CU language,
14536 or we won't be able to build types correctly. */
9816fde3 14537 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14538
14539 do_cleanups (back_to);
14540
14541 /* We've successfully allocated this compilation unit. Let our caller
14542 clean it up when finished with it. */
14543 discard_cleanups (free_cu_cleanup);
14544
14545 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14546 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14547}
14548
c906108c
SS
14549/* Decode simple location descriptions.
14550 Given a pointer to a dwarf block that defines a location, compute
14551 the location and return the value.
14552
4cecd739
DJ
14553 NOTE drow/2003-11-18: This function is called in two situations
14554 now: for the address of static or global variables (partial symbols
14555 only) and for offsets into structures which are expected to be
14556 (more or less) constant. The partial symbol case should go away,
14557 and only the constant case should remain. That will let this
14558 function complain more accurately. A few special modes are allowed
14559 without complaint for global variables (for instance, global
14560 register values and thread-local values).
c906108c
SS
14561
14562 A location description containing no operations indicates that the
4cecd739 14563 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14564 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14565 callers will only want a very basic result and this can become a
21ae7a4d
JK
14566 complaint.
14567
14568 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14569
14570static CORE_ADDR
e7c27a73 14571decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14572{
e7c27a73 14573 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14574 int i;
14575 int size = blk->size;
14576 gdb_byte *data = blk->data;
14577 CORE_ADDR stack[64];
14578 int stacki;
14579 unsigned int bytes_read, unsnd;
14580 gdb_byte op;
c906108c 14581
21ae7a4d
JK
14582 i = 0;
14583 stacki = 0;
14584 stack[stacki] = 0;
14585 stack[++stacki] = 0;
14586
14587 while (i < size)
14588 {
14589 op = data[i++];
14590 switch (op)
14591 {
14592 case DW_OP_lit0:
14593 case DW_OP_lit1:
14594 case DW_OP_lit2:
14595 case DW_OP_lit3:
14596 case DW_OP_lit4:
14597 case DW_OP_lit5:
14598 case DW_OP_lit6:
14599 case DW_OP_lit7:
14600 case DW_OP_lit8:
14601 case DW_OP_lit9:
14602 case DW_OP_lit10:
14603 case DW_OP_lit11:
14604 case DW_OP_lit12:
14605 case DW_OP_lit13:
14606 case DW_OP_lit14:
14607 case DW_OP_lit15:
14608 case DW_OP_lit16:
14609 case DW_OP_lit17:
14610 case DW_OP_lit18:
14611 case DW_OP_lit19:
14612 case DW_OP_lit20:
14613 case DW_OP_lit21:
14614 case DW_OP_lit22:
14615 case DW_OP_lit23:
14616 case DW_OP_lit24:
14617 case DW_OP_lit25:
14618 case DW_OP_lit26:
14619 case DW_OP_lit27:
14620 case DW_OP_lit28:
14621 case DW_OP_lit29:
14622 case DW_OP_lit30:
14623 case DW_OP_lit31:
14624 stack[++stacki] = op - DW_OP_lit0;
14625 break;
f1bea926 14626
21ae7a4d
JK
14627 case DW_OP_reg0:
14628 case DW_OP_reg1:
14629 case DW_OP_reg2:
14630 case DW_OP_reg3:
14631 case DW_OP_reg4:
14632 case DW_OP_reg5:
14633 case DW_OP_reg6:
14634 case DW_OP_reg7:
14635 case DW_OP_reg8:
14636 case DW_OP_reg9:
14637 case DW_OP_reg10:
14638 case DW_OP_reg11:
14639 case DW_OP_reg12:
14640 case DW_OP_reg13:
14641 case DW_OP_reg14:
14642 case DW_OP_reg15:
14643 case DW_OP_reg16:
14644 case DW_OP_reg17:
14645 case DW_OP_reg18:
14646 case DW_OP_reg19:
14647 case DW_OP_reg20:
14648 case DW_OP_reg21:
14649 case DW_OP_reg22:
14650 case DW_OP_reg23:
14651 case DW_OP_reg24:
14652 case DW_OP_reg25:
14653 case DW_OP_reg26:
14654 case DW_OP_reg27:
14655 case DW_OP_reg28:
14656 case DW_OP_reg29:
14657 case DW_OP_reg30:
14658 case DW_OP_reg31:
14659 stack[++stacki] = op - DW_OP_reg0;
14660 if (i < size)
14661 dwarf2_complex_location_expr_complaint ();
14662 break;
c906108c 14663
21ae7a4d
JK
14664 case DW_OP_regx:
14665 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14666 i += bytes_read;
14667 stack[++stacki] = unsnd;
14668 if (i < size)
14669 dwarf2_complex_location_expr_complaint ();
14670 break;
c906108c 14671
21ae7a4d
JK
14672 case DW_OP_addr:
14673 stack[++stacki] = read_address (objfile->obfd, &data[i],
14674 cu, &bytes_read);
14675 i += bytes_read;
14676 break;
d53d4ac5 14677
21ae7a4d
JK
14678 case DW_OP_const1u:
14679 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14680 i += 1;
14681 break;
14682
14683 case DW_OP_const1s:
14684 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14685 i += 1;
14686 break;
14687
14688 case DW_OP_const2u:
14689 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14690 i += 2;
14691 break;
14692
14693 case DW_OP_const2s:
14694 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14695 i += 2;
14696 break;
d53d4ac5 14697
21ae7a4d
JK
14698 case DW_OP_const4u:
14699 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14700 i += 4;
14701 break;
14702
14703 case DW_OP_const4s:
14704 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14705 i += 4;
14706 break;
14707
585861ea
JK
14708 case DW_OP_const8u:
14709 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14710 i += 8;
14711 break;
14712
21ae7a4d
JK
14713 case DW_OP_constu:
14714 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14715 &bytes_read);
14716 i += bytes_read;
14717 break;
14718
14719 case DW_OP_consts:
14720 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14721 i += bytes_read;
14722 break;
14723
14724 case DW_OP_dup:
14725 stack[stacki + 1] = stack[stacki];
14726 stacki++;
14727 break;
14728
14729 case DW_OP_plus:
14730 stack[stacki - 1] += stack[stacki];
14731 stacki--;
14732 break;
14733
14734 case DW_OP_plus_uconst:
14735 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14736 &bytes_read);
14737 i += bytes_read;
14738 break;
14739
14740 case DW_OP_minus:
14741 stack[stacki - 1] -= stack[stacki];
14742 stacki--;
14743 break;
14744
14745 case DW_OP_deref:
14746 /* If we're not the last op, then we definitely can't encode
14747 this using GDB's address_class enum. This is valid for partial
14748 global symbols, although the variable's address will be bogus
14749 in the psymtab. */
14750 if (i < size)
14751 dwarf2_complex_location_expr_complaint ();
14752 break;
14753
14754 case DW_OP_GNU_push_tls_address:
14755 /* The top of the stack has the offset from the beginning
14756 of the thread control block at which the variable is located. */
14757 /* Nothing should follow this operator, so the top of stack would
14758 be returned. */
14759 /* This is valid for partial global symbols, but the variable's
585861ea
JK
14760 address will be bogus in the psymtab. Make it always at least
14761 non-zero to not look as a variable garbage collected by linker
14762 which have DW_OP_addr 0. */
21ae7a4d
JK
14763 if (i < size)
14764 dwarf2_complex_location_expr_complaint ();
585861ea 14765 stack[stacki]++;
21ae7a4d
JK
14766 break;
14767
14768 case DW_OP_GNU_uninit:
14769 break;
14770
14771 default:
14772 {
14773 const char *name = dwarf_stack_op_name (op);
14774
14775 if (name)
14776 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14777 name);
14778 else
14779 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14780 op);
14781 }
14782
14783 return (stack[stacki]);
d53d4ac5 14784 }
3c6e0cb3 14785
21ae7a4d
JK
14786 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14787 outside of the allocated space. Also enforce minimum>0. */
14788 if (stacki >= ARRAY_SIZE (stack) - 1)
14789 {
14790 complaint (&symfile_complaints,
14791 _("location description stack overflow"));
14792 return 0;
14793 }
14794
14795 if (stacki <= 0)
14796 {
14797 complaint (&symfile_complaints,
14798 _("location description stack underflow"));
14799 return 0;
14800 }
14801 }
14802 return (stack[stacki]);
c906108c
SS
14803}
14804
14805/* memory allocation interface */
14806
c906108c 14807static struct dwarf_block *
7b5a2f43 14808dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14809{
14810 struct dwarf_block *blk;
14811
14812 blk = (struct dwarf_block *)
7b5a2f43 14813 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14814 return (blk);
14815}
14816
14817static struct abbrev_info *
f3dd6933 14818dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14819{
14820 struct abbrev_info *abbrev;
14821
f3dd6933
DJ
14822 abbrev = (struct abbrev_info *)
14823 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14824 memset (abbrev, 0, sizeof (struct abbrev_info));
14825 return (abbrev);
14826}
14827
14828static struct die_info *
b60c80d6 14829dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14830{
14831 struct die_info *die;
b60c80d6
DJ
14832 size_t size = sizeof (struct die_info);
14833
14834 if (num_attrs > 1)
14835 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14836
b60c80d6 14837 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14838 memset (die, 0, sizeof (struct die_info));
14839 return (die);
14840}
2e276125
JB
14841
14842\f
14843/* Macro support. */
14844
2e276125
JB
14845/* Return the full name of file number I in *LH's file name table.
14846 Use COMP_DIR as the name of the current directory of the
14847 compilation. The result is allocated using xmalloc; the caller is
14848 responsible for freeing it. */
14849static char *
14850file_full_name (int file, struct line_header *lh, const char *comp_dir)
14851{
6a83a1e6
EZ
14852 /* Is the file number a valid index into the line header's file name
14853 table? Remember that file numbers start with one, not zero. */
14854 if (1 <= file && file <= lh->num_file_names)
14855 {
14856 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14857
6a83a1e6
EZ
14858 if (IS_ABSOLUTE_PATH (fe->name))
14859 return xstrdup (fe->name);
14860 else
14861 {
14862 const char *dir;
14863 int dir_len;
14864 char *full_name;
14865
14866 if (fe->dir_index)
14867 dir = lh->include_dirs[fe->dir_index - 1];
14868 else
14869 dir = comp_dir;
14870
14871 if (dir)
14872 {
14873 dir_len = strlen (dir);
14874 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14875 strcpy (full_name, dir);
14876 full_name[dir_len] = '/';
14877 strcpy (full_name + dir_len + 1, fe->name);
14878 return full_name;
14879 }
14880 else
14881 return xstrdup (fe->name);
14882 }
14883 }
2e276125
JB
14884 else
14885 {
6a83a1e6
EZ
14886 /* The compiler produced a bogus file number. We can at least
14887 record the macro definitions made in the file, even if we
14888 won't be able to find the file by name. */
14889 char fake_name[80];
9a619af0 14890
6a83a1e6 14891 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14892
6e70227d 14893 complaint (&symfile_complaints,
6a83a1e6
EZ
14894 _("bad file number in macro information (%d)"),
14895 file);
2e276125 14896
6a83a1e6 14897 return xstrdup (fake_name);
2e276125
JB
14898 }
14899}
14900
14901
14902static struct macro_source_file *
14903macro_start_file (int file, int line,
14904 struct macro_source_file *current_file,
14905 const char *comp_dir,
14906 struct line_header *lh, struct objfile *objfile)
14907{
14908 /* The full name of this source file. */
14909 char *full_name = file_full_name (file, lh, comp_dir);
14910
14911 /* We don't create a macro table for this compilation unit
14912 at all until we actually get a filename. */
14913 if (! pending_macros)
4a146b47 14914 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14915 objfile->macro_cache);
2e276125
JB
14916
14917 if (! current_file)
14918 /* If we have no current file, then this must be the start_file
14919 directive for the compilation unit's main source file. */
14920 current_file = macro_set_main (pending_macros, full_name);
14921 else
14922 current_file = macro_include (current_file, line, full_name);
14923
14924 xfree (full_name);
6e70227d 14925
2e276125
JB
14926 return current_file;
14927}
14928
14929
14930/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14931 followed by a null byte. */
14932static char *
14933copy_string (const char *buf, int len)
14934{
14935 char *s = xmalloc (len + 1);
9a619af0 14936
2e276125
JB
14937 memcpy (s, buf, len);
14938 s[len] = '\0';
2e276125
JB
14939 return s;
14940}
14941
14942
14943static const char *
14944consume_improper_spaces (const char *p, const char *body)
14945{
14946 if (*p == ' ')
14947 {
4d3c2250 14948 complaint (&symfile_complaints,
3e43a32a
MS
14949 _("macro definition contains spaces "
14950 "in formal argument list:\n`%s'"),
4d3c2250 14951 body);
2e276125
JB
14952
14953 while (*p == ' ')
14954 p++;
14955 }
14956
14957 return p;
14958}
14959
14960
14961static void
14962parse_macro_definition (struct macro_source_file *file, int line,
14963 const char *body)
14964{
14965 const char *p;
14966
14967 /* The body string takes one of two forms. For object-like macro
14968 definitions, it should be:
14969
14970 <macro name> " " <definition>
14971
14972 For function-like macro definitions, it should be:
14973
14974 <macro name> "() " <definition>
14975 or
14976 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14977
14978 Spaces may appear only where explicitly indicated, and in the
14979 <definition>.
14980
14981 The Dwarf 2 spec says that an object-like macro's name is always
14982 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14983 the space when the macro's definition is the empty string.
2e276125
JB
14984
14985 The Dwarf 2 spec says that there should be no spaces between the
14986 formal arguments in a function-like macro's formal argument list,
14987 but versions of GCC around March 2002 include spaces after the
14988 commas. */
14989
14990
14991 /* Find the extent of the macro name. The macro name is terminated
14992 by either a space or null character (for an object-like macro) or
14993 an opening paren (for a function-like macro). */
14994 for (p = body; *p; p++)
14995 if (*p == ' ' || *p == '(')
14996 break;
14997
14998 if (*p == ' ' || *p == '\0')
14999 {
15000 /* It's an object-like macro. */
15001 int name_len = p - body;
15002 char *name = copy_string (body, name_len);
15003 const char *replacement;
15004
15005 if (*p == ' ')
15006 replacement = body + name_len + 1;
15007 else
15008 {
4d3c2250 15009 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15010 replacement = body + name_len;
15011 }
6e70227d 15012
2e276125
JB
15013 macro_define_object (file, line, name, replacement);
15014
15015 xfree (name);
15016 }
15017 else if (*p == '(')
15018 {
15019 /* It's a function-like macro. */
15020 char *name = copy_string (body, p - body);
15021 int argc = 0;
15022 int argv_size = 1;
15023 char **argv = xmalloc (argv_size * sizeof (*argv));
15024
15025 p++;
15026
15027 p = consume_improper_spaces (p, body);
15028
15029 /* Parse the formal argument list. */
15030 while (*p && *p != ')')
15031 {
15032 /* Find the extent of the current argument name. */
15033 const char *arg_start = p;
15034
15035 while (*p && *p != ',' && *p != ')' && *p != ' ')
15036 p++;
15037
15038 if (! *p || p == arg_start)
4d3c2250 15039 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15040 else
15041 {
15042 /* Make sure argv has room for the new argument. */
15043 if (argc >= argv_size)
15044 {
15045 argv_size *= 2;
15046 argv = xrealloc (argv, argv_size * sizeof (*argv));
15047 }
15048
15049 argv[argc++] = copy_string (arg_start, p - arg_start);
15050 }
15051
15052 p = consume_improper_spaces (p, body);
15053
15054 /* Consume the comma, if present. */
15055 if (*p == ',')
15056 {
15057 p++;
15058
15059 p = consume_improper_spaces (p, body);
15060 }
15061 }
15062
15063 if (*p == ')')
15064 {
15065 p++;
15066
15067 if (*p == ' ')
15068 /* Perfectly formed definition, no complaints. */
15069 macro_define_function (file, line, name,
6e70227d 15070 argc, (const char **) argv,
2e276125
JB
15071 p + 1);
15072 else if (*p == '\0')
15073 {
15074 /* Complain, but do define it. */
4d3c2250 15075 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15076 macro_define_function (file, line, name,
6e70227d 15077 argc, (const char **) argv,
2e276125
JB
15078 p);
15079 }
15080 else
15081 /* Just complain. */
4d3c2250 15082 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15083 }
15084 else
15085 /* Just complain. */
4d3c2250 15086 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15087
15088 xfree (name);
15089 {
15090 int i;
15091
15092 for (i = 0; i < argc; i++)
15093 xfree (argv[i]);
15094 }
15095 xfree (argv);
15096 }
15097 else
4d3c2250 15098 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15099}
15100
cf2c3c16
TT
15101/* Skip some bytes from BYTES according to the form given in FORM.
15102 Returns the new pointer. */
2e276125 15103
cf2c3c16
TT
15104static gdb_byte *
15105skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15106 enum dwarf_form form,
15107 unsigned int offset_size,
15108 struct dwarf2_section_info *section)
2e276125 15109{
cf2c3c16 15110 unsigned int bytes_read;
2e276125 15111
cf2c3c16 15112 switch (form)
2e276125 15113 {
cf2c3c16
TT
15114 case DW_FORM_data1:
15115 case DW_FORM_flag:
15116 ++bytes;
15117 break;
15118
15119 case DW_FORM_data2:
15120 bytes += 2;
15121 break;
15122
15123 case DW_FORM_data4:
15124 bytes += 4;
15125 break;
15126
15127 case DW_FORM_data8:
15128 bytes += 8;
15129 break;
15130
15131 case DW_FORM_string:
15132 read_direct_string (abfd, bytes, &bytes_read);
15133 bytes += bytes_read;
15134 break;
15135
15136 case DW_FORM_sec_offset:
15137 case DW_FORM_strp:
15138 bytes += offset_size;
15139 break;
15140
15141 case DW_FORM_block:
15142 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15143 bytes += bytes_read;
15144 break;
15145
15146 case DW_FORM_block1:
15147 bytes += 1 + read_1_byte (abfd, bytes);
15148 break;
15149 case DW_FORM_block2:
15150 bytes += 2 + read_2_bytes (abfd, bytes);
15151 break;
15152 case DW_FORM_block4:
15153 bytes += 4 + read_4_bytes (abfd, bytes);
15154 break;
15155
15156 case DW_FORM_sdata:
15157 case DW_FORM_udata:
15158 bytes = skip_leb128 (abfd, bytes);
15159 break;
15160
15161 default:
15162 {
15163 complain:
15164 complaint (&symfile_complaints,
15165 _("invalid form 0x%x in `%s'"),
15166 form,
15167 section->asection->name);
15168 return NULL;
15169 }
2e276125
JB
15170 }
15171
cf2c3c16
TT
15172 return bytes;
15173}
757a13d0 15174
cf2c3c16
TT
15175/* A helper for dwarf_decode_macros that handles skipping an unknown
15176 opcode. Returns an updated pointer to the macro data buffer; or,
15177 on error, issues a complaint and returns NULL. */
757a13d0 15178
cf2c3c16
TT
15179static gdb_byte *
15180skip_unknown_opcode (unsigned int opcode,
15181 gdb_byte **opcode_definitions,
15182 gdb_byte *mac_ptr,
15183 bfd *abfd,
15184 unsigned int offset_size,
15185 struct dwarf2_section_info *section)
15186{
15187 unsigned int bytes_read, i;
15188 unsigned long arg;
15189 gdb_byte *defn;
2e276125 15190
cf2c3c16 15191 if (opcode_definitions[opcode] == NULL)
2e276125 15192 {
cf2c3c16
TT
15193 complaint (&symfile_complaints,
15194 _("unrecognized DW_MACFINO opcode 0x%x"),
15195 opcode);
15196 return NULL;
15197 }
2e276125 15198
cf2c3c16
TT
15199 defn = opcode_definitions[opcode];
15200 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15201 defn += bytes_read;
2e276125 15202
cf2c3c16
TT
15203 for (i = 0; i < arg; ++i)
15204 {
15205 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15206 if (mac_ptr == NULL)
15207 {
15208 /* skip_form_bytes already issued the complaint. */
15209 return NULL;
15210 }
15211 }
757a13d0 15212
cf2c3c16
TT
15213 return mac_ptr;
15214}
757a13d0 15215
cf2c3c16
TT
15216/* A helper function which parses the header of a macro section.
15217 If the macro section is the extended (for now called "GNU") type,
15218 then this updates *OFFSET_SIZE. Returns a pointer to just after
15219 the header, or issues a complaint and returns NULL on error. */
757a13d0 15220
cf2c3c16
TT
15221static gdb_byte *
15222dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15223 bfd *abfd,
15224 gdb_byte *mac_ptr,
15225 unsigned int *offset_size,
15226 int section_is_gnu)
15227{
15228 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15229
cf2c3c16
TT
15230 if (section_is_gnu)
15231 {
15232 unsigned int version, flags;
757a13d0 15233
cf2c3c16
TT
15234 version = read_2_bytes (abfd, mac_ptr);
15235 if (version != 4)
15236 {
15237 complaint (&symfile_complaints,
15238 _("unrecognized version `%d' in .debug_macro section"),
15239 version);
15240 return NULL;
15241 }
15242 mac_ptr += 2;
757a13d0 15243
cf2c3c16
TT
15244 flags = read_1_byte (abfd, mac_ptr);
15245 ++mac_ptr;
15246 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15247
cf2c3c16
TT
15248 if ((flags & 2) != 0)
15249 /* We don't need the line table offset. */
15250 mac_ptr += *offset_size;
757a13d0 15251
cf2c3c16
TT
15252 /* Vendor opcode descriptions. */
15253 if ((flags & 4) != 0)
15254 {
15255 unsigned int i, count;
757a13d0 15256
cf2c3c16
TT
15257 count = read_1_byte (abfd, mac_ptr);
15258 ++mac_ptr;
15259 for (i = 0; i < count; ++i)
15260 {
15261 unsigned int opcode, bytes_read;
15262 unsigned long arg;
15263
15264 opcode = read_1_byte (abfd, mac_ptr);
15265 ++mac_ptr;
15266 opcode_definitions[opcode] = mac_ptr;
15267 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15268 mac_ptr += bytes_read;
15269 mac_ptr += arg;
15270 }
757a13d0 15271 }
cf2c3c16 15272 }
757a13d0 15273
cf2c3c16
TT
15274 return mac_ptr;
15275}
757a13d0 15276
cf2c3c16
TT
15277/* A helper for dwarf_decode_macros that handles the GNU extensions,
15278 including DW_GNU_MACINFO_transparent_include. */
15279
15280static void
15281dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15282 struct macro_source_file *current_file,
15283 struct line_header *lh, char *comp_dir,
15284 struct dwarf2_section_info *section,
15285 int section_is_gnu,
15286 unsigned int offset_size,
15287 struct objfile *objfile)
15288{
15289 enum dwarf_macro_record_type macinfo_type;
15290 int at_commandline;
15291 gdb_byte *opcode_definitions[256];
757a13d0 15292
cf2c3c16
TT
15293 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15294 &offset_size, section_is_gnu);
15295 if (mac_ptr == NULL)
15296 {
15297 /* We already issued a complaint. */
15298 return;
15299 }
757a13d0
JK
15300
15301 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15302 GDB is still reading the definitions from command line. First
15303 DW_MACINFO_start_file will need to be ignored as it was already executed
15304 to create CURRENT_FILE for the main source holding also the command line
15305 definitions. On first met DW_MACINFO_start_file this flag is reset to
15306 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15307
15308 at_commandline = 1;
15309
15310 do
15311 {
15312 /* Do we at least have room for a macinfo type byte? */
15313 if (mac_ptr >= mac_end)
15314 {
cf2c3c16 15315 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15316 break;
15317 }
15318
15319 macinfo_type = read_1_byte (abfd, mac_ptr);
15320 mac_ptr++;
15321
cf2c3c16
TT
15322 /* Note that we rely on the fact that the corresponding GNU and
15323 DWARF constants are the same. */
757a13d0
JK
15324 switch (macinfo_type)
15325 {
15326 /* A zero macinfo type indicates the end of the macro
15327 information. */
15328 case 0:
15329 break;
2e276125 15330
cf2c3c16
TT
15331 case DW_MACRO_GNU_define:
15332 case DW_MACRO_GNU_undef:
15333 case DW_MACRO_GNU_define_indirect:
15334 case DW_MACRO_GNU_undef_indirect:
2e276125 15335 {
891d2f0b 15336 unsigned int bytes_read;
2e276125
JB
15337 int line;
15338 char *body;
cf2c3c16 15339 int is_define;
2e276125 15340
cf2c3c16
TT
15341 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15342 mac_ptr += bytes_read;
15343
15344 if (macinfo_type == DW_MACRO_GNU_define
15345 || macinfo_type == DW_MACRO_GNU_undef)
15346 {
15347 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15348 mac_ptr += bytes_read;
15349 }
15350 else
15351 {
15352 LONGEST str_offset;
15353
15354 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15355 mac_ptr += offset_size;
2e276125 15356
cf2c3c16
TT
15357 body = read_indirect_string_at_offset (abfd, str_offset);
15358 }
15359
15360 is_define = (macinfo_type == DW_MACRO_GNU_define
15361 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15362 if (! current_file)
757a13d0
JK
15363 {
15364 /* DWARF violation as no main source is present. */
15365 complaint (&symfile_complaints,
15366 _("debug info with no main source gives macro %s "
15367 "on line %d: %s"),
cf2c3c16
TT
15368 is_define ? _("definition") : _("undefinition"),
15369 line, body);
757a13d0
JK
15370 break;
15371 }
3e43a32a
MS
15372 if ((line == 0 && !at_commandline)
15373 || (line != 0 && at_commandline))
4d3c2250 15374 complaint (&symfile_complaints,
757a13d0
JK
15375 _("debug info gives %s macro %s with %s line %d: %s"),
15376 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15377 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15378 line == 0 ? _("zero") : _("non-zero"), line, body);
15379
cf2c3c16 15380 if (is_define)
757a13d0 15381 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15382 else
15383 {
15384 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15385 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15386 macro_undef (current_file, line, body);
15387 }
2e276125
JB
15388 }
15389 break;
15390
cf2c3c16 15391 case DW_MACRO_GNU_start_file:
2e276125 15392 {
891d2f0b 15393 unsigned int bytes_read;
2e276125
JB
15394 int line, file;
15395
15396 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15397 mac_ptr += bytes_read;
15398 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15399 mac_ptr += bytes_read;
15400
3e43a32a
MS
15401 if ((line == 0 && !at_commandline)
15402 || (line != 0 && at_commandline))
757a13d0
JK
15403 complaint (&symfile_complaints,
15404 _("debug info gives source %d included "
15405 "from %s at %s line %d"),
15406 file, at_commandline ? _("command-line") : _("file"),
15407 line == 0 ? _("zero") : _("non-zero"), line);
15408
15409 if (at_commandline)
15410 {
cf2c3c16
TT
15411 /* This DW_MACRO_GNU_start_file was executed in the
15412 pass one. */
757a13d0
JK
15413 at_commandline = 0;
15414 }
15415 else
15416 current_file = macro_start_file (file, line,
15417 current_file, comp_dir,
cf2c3c16 15418 lh, objfile);
2e276125
JB
15419 }
15420 break;
15421
cf2c3c16 15422 case DW_MACRO_GNU_end_file:
2e276125 15423 if (! current_file)
4d3c2250 15424 complaint (&symfile_complaints,
3e43a32a
MS
15425 _("macro debug info has an unmatched "
15426 "`close_file' directive"));
2e276125
JB
15427 else
15428 {
15429 current_file = current_file->included_by;
15430 if (! current_file)
15431 {
cf2c3c16 15432 enum dwarf_macro_record_type next_type;
2e276125
JB
15433
15434 /* GCC circa March 2002 doesn't produce the zero
15435 type byte marking the end of the compilation
15436 unit. Complain if it's not there, but exit no
15437 matter what. */
15438
15439 /* Do we at least have room for a macinfo type byte? */
15440 if (mac_ptr >= mac_end)
15441 {
cf2c3c16 15442 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15443 return;
15444 }
15445
15446 /* We don't increment mac_ptr here, so this is just
15447 a look-ahead. */
15448 next_type = read_1_byte (abfd, mac_ptr);
15449 if (next_type != 0)
4d3c2250 15450 complaint (&symfile_complaints,
3e43a32a
MS
15451 _("no terminating 0-type entry for "
15452 "macros in `.debug_macinfo' section"));
2e276125
JB
15453
15454 return;
15455 }
15456 }
15457 break;
15458
cf2c3c16
TT
15459 case DW_MACRO_GNU_transparent_include:
15460 {
15461 LONGEST offset;
15462
15463 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15464 mac_ptr += offset_size;
15465
15466 dwarf_decode_macro_bytes (abfd,
15467 section->buffer + offset,
15468 mac_end, current_file,
15469 lh, comp_dir,
15470 section, section_is_gnu,
15471 offset_size, objfile);
15472 }
15473 break;
15474
2e276125 15475 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15476 if (!section_is_gnu)
15477 {
15478 unsigned int bytes_read;
15479 int constant;
2e276125 15480
cf2c3c16
TT
15481 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15482 mac_ptr += bytes_read;
15483 read_direct_string (abfd, mac_ptr, &bytes_read);
15484 mac_ptr += bytes_read;
2e276125 15485
cf2c3c16
TT
15486 /* We don't recognize any vendor extensions. */
15487 break;
15488 }
15489 /* FALLTHROUGH */
15490
15491 default:
15492 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15493 mac_ptr, abfd, offset_size,
15494 section);
15495 if (mac_ptr == NULL)
15496 return;
15497 break;
2e276125 15498 }
757a13d0 15499 } while (macinfo_type != 0);
2e276125 15500}
8e19ed76 15501
cf2c3c16
TT
15502static void
15503dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15504 char *comp_dir, bfd *abfd,
15505 struct dwarf2_cu *cu,
15506 struct dwarf2_section_info *section,
15507 int section_is_gnu)
15508{
15509 gdb_byte *mac_ptr, *mac_end;
15510 struct macro_source_file *current_file = 0;
15511 enum dwarf_macro_record_type macinfo_type;
15512 unsigned int offset_size = cu->header.offset_size;
15513 gdb_byte *opcode_definitions[256];
15514
15515 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15516 if (section->buffer == NULL)
15517 {
15518 complaint (&symfile_complaints, _("missing %s section"),
15519 section->asection->name);
15520 return;
15521 }
15522
15523 /* First pass: Find the name of the base filename.
15524 This filename is needed in order to process all macros whose definition
15525 (or undefinition) comes from the command line. These macros are defined
15526 before the first DW_MACINFO_start_file entry, and yet still need to be
15527 associated to the base file.
15528
15529 To determine the base file name, we scan the macro definitions until we
15530 reach the first DW_MACINFO_start_file entry. We then initialize
15531 CURRENT_FILE accordingly so that any macro definition found before the
15532 first DW_MACINFO_start_file can still be associated to the base file. */
15533
15534 mac_ptr = section->buffer + offset;
15535 mac_end = section->buffer + section->size;
15536
15537 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15538 &offset_size, section_is_gnu);
15539 if (mac_ptr == NULL)
15540 {
15541 /* We already issued a complaint. */
15542 return;
15543 }
15544
15545 do
15546 {
15547 /* Do we at least have room for a macinfo type byte? */
15548 if (mac_ptr >= mac_end)
15549 {
15550 /* Complaint is printed during the second pass as GDB will probably
15551 stop the first pass earlier upon finding
15552 DW_MACINFO_start_file. */
15553 break;
15554 }
15555
15556 macinfo_type = read_1_byte (abfd, mac_ptr);
15557 mac_ptr++;
15558
15559 /* Note that we rely on the fact that the corresponding GNU and
15560 DWARF constants are the same. */
15561 switch (macinfo_type)
15562 {
15563 /* A zero macinfo type indicates the end of the macro
15564 information. */
15565 case 0:
15566 break;
15567
15568 case DW_MACRO_GNU_define:
15569 case DW_MACRO_GNU_undef:
15570 /* Only skip the data by MAC_PTR. */
15571 {
15572 unsigned int bytes_read;
15573
15574 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15575 mac_ptr += bytes_read;
15576 read_direct_string (abfd, mac_ptr, &bytes_read);
15577 mac_ptr += bytes_read;
15578 }
15579 break;
15580
15581 case DW_MACRO_GNU_start_file:
15582 {
15583 unsigned int bytes_read;
15584 int line, file;
15585
15586 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15587 mac_ptr += bytes_read;
15588 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15589 mac_ptr += bytes_read;
15590
15591 current_file = macro_start_file (file, line, current_file,
15592 comp_dir, lh, cu->objfile);
15593 }
15594 break;
15595
15596 case DW_MACRO_GNU_end_file:
15597 /* No data to skip by MAC_PTR. */
15598 break;
15599
15600 case DW_MACRO_GNU_define_indirect:
15601 case DW_MACRO_GNU_undef_indirect:
15602 {
15603 unsigned int bytes_read;
15604
15605 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15606 mac_ptr += bytes_read;
15607 mac_ptr += offset_size;
15608 }
15609 break;
15610
15611 case DW_MACRO_GNU_transparent_include:
15612 /* Note that, according to the spec, a transparent include
15613 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15614 skip this opcode. */
15615 mac_ptr += offset_size;
15616 break;
15617
15618 case DW_MACINFO_vendor_ext:
15619 /* Only skip the data by MAC_PTR. */
15620 if (!section_is_gnu)
15621 {
15622 unsigned int bytes_read;
15623
15624 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15625 mac_ptr += bytes_read;
15626 read_direct_string (abfd, mac_ptr, &bytes_read);
15627 mac_ptr += bytes_read;
15628 }
15629 /* FALLTHROUGH */
15630
15631 default:
15632 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15633 mac_ptr, abfd, offset_size,
15634 section);
15635 if (mac_ptr == NULL)
15636 return;
15637 break;
15638 }
15639 } while (macinfo_type != 0 && current_file == NULL);
15640
15641 /* Second pass: Process all entries.
15642
15643 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15644 command-line macro definitions/undefinitions. This flag is unset when we
15645 reach the first DW_MACINFO_start_file entry. */
15646
15647 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15648 current_file, lh, comp_dir, section, section_is_gnu,
15649 offset_size, cu->objfile);
15650}
15651
8e19ed76 15652/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15653 if so return true else false. */
8e19ed76
PS
15654static int
15655attr_form_is_block (struct attribute *attr)
15656{
15657 return (attr == NULL ? 0 :
15658 attr->form == DW_FORM_block1
15659 || attr->form == DW_FORM_block2
15660 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15661 || attr->form == DW_FORM_block
15662 || attr->form == DW_FORM_exprloc);
8e19ed76 15663}
4c2df51b 15664
c6a0999f
JB
15665/* Return non-zero if ATTR's value is a section offset --- classes
15666 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15667 You may use DW_UNSND (attr) to retrieve such offsets.
15668
15669 Section 7.5.4, "Attribute Encodings", explains that no attribute
15670 may have a value that belongs to more than one of these classes; it
15671 would be ambiguous if we did, because we use the same forms for all
15672 of them. */
3690dd37
JB
15673static int
15674attr_form_is_section_offset (struct attribute *attr)
15675{
15676 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15677 || attr->form == DW_FORM_data8
15678 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15679}
15680
15681
15682/* Return non-zero if ATTR's value falls in the 'constant' class, or
15683 zero otherwise. When this function returns true, you can apply
15684 dwarf2_get_attr_constant_value to it.
15685
15686 However, note that for some attributes you must check
15687 attr_form_is_section_offset before using this test. DW_FORM_data4
15688 and DW_FORM_data8 are members of both the constant class, and of
15689 the classes that contain offsets into other debug sections
15690 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15691 that, if an attribute's can be either a constant or one of the
15692 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15693 taken as section offsets, not constants. */
15694static int
15695attr_form_is_constant (struct attribute *attr)
15696{
15697 switch (attr->form)
15698 {
15699 case DW_FORM_sdata:
15700 case DW_FORM_udata:
15701 case DW_FORM_data1:
15702 case DW_FORM_data2:
15703 case DW_FORM_data4:
15704 case DW_FORM_data8:
15705 return 1;
15706 default:
15707 return 0;
15708 }
15709}
15710
8cf6f0b1
TT
15711/* A helper function that fills in a dwarf2_loclist_baton. */
15712
15713static void
15714fill_in_loclist_baton (struct dwarf2_cu *cu,
15715 struct dwarf2_loclist_baton *baton,
15716 struct attribute *attr)
15717{
15718 dwarf2_read_section (dwarf2_per_objfile->objfile,
15719 &dwarf2_per_objfile->loc);
15720
15721 baton->per_cu = cu->per_cu;
15722 gdb_assert (baton->per_cu);
15723 /* We don't know how long the location list is, but make sure we
15724 don't run off the edge of the section. */
15725 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15726 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15727 baton->base_address = cu->base_address;
15728}
15729
4c2df51b
DJ
15730static void
15731dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15732 struct dwarf2_cu *cu)
4c2df51b 15733{
3690dd37 15734 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15735 /* ".debug_loc" may not exist at all, or the offset may be outside
15736 the section. If so, fall through to the complaint in the
15737 other branch. */
9e0ac564
TT
15738 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15739 &dwarf2_per_objfile->loc))
4c2df51b 15740 {
0d53c4c4 15741 struct dwarf2_loclist_baton *baton;
4c2df51b 15742
4a146b47 15743 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15744 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15745
8cf6f0b1 15746 fill_in_loclist_baton (cu, baton, attr);
be391dca 15747
d00adf39 15748 if (cu->base_known == 0)
0d53c4c4 15749 complaint (&symfile_complaints,
3e43a32a
MS
15750 _("Location list used without "
15751 "specifying the CU base address."));
4c2df51b 15752
768a979c 15753 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15754 SYMBOL_LOCATION_BATON (sym) = baton;
15755 }
15756 else
15757 {
15758 struct dwarf2_locexpr_baton *baton;
15759
4a146b47 15760 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15761 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15762 baton->per_cu = cu->per_cu;
15763 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15764
15765 if (attr_form_is_block (attr))
15766 {
15767 /* Note that we're just copying the block's data pointer
15768 here, not the actual data. We're still pointing into the
6502dd73
DJ
15769 info_buffer for SYM's objfile; right now we never release
15770 that buffer, but when we do clean up properly this may
15771 need to change. */
0d53c4c4
DJ
15772 baton->size = DW_BLOCK (attr)->size;
15773 baton->data = DW_BLOCK (attr)->data;
15774 }
15775 else
15776 {
15777 dwarf2_invalid_attrib_class_complaint ("location description",
15778 SYMBOL_NATURAL_NAME (sym));
15779 baton->size = 0;
0d53c4c4 15780 }
6e70227d 15781
768a979c 15782 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15783 SYMBOL_LOCATION_BATON (sym) = baton;
15784 }
4c2df51b 15785}
6502dd73 15786
9aa1f1e3
TT
15787/* Return the OBJFILE associated with the compilation unit CU. If CU
15788 came from a separate debuginfo file, then the master objfile is
15789 returned. */
ae0d2f24
UW
15790
15791struct objfile *
15792dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15793{
9291a0cd 15794 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15795
15796 /* Return the master objfile, so that we can report and look up the
15797 correct file containing this variable. */
15798 if (objfile->separate_debug_objfile_backlink)
15799 objfile = objfile->separate_debug_objfile_backlink;
15800
15801 return objfile;
15802}
15803
96408a79
SA
15804/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15805 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15806 CU_HEADERP first. */
15807
15808static const struct comp_unit_head *
15809per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15810 struct dwarf2_per_cu_data *per_cu)
15811{
15812 struct objfile *objfile;
15813 struct dwarf2_per_objfile *per_objfile;
15814 gdb_byte *info_ptr;
15815
15816 if (per_cu->cu)
15817 return &per_cu->cu->header;
15818
15819 objfile = per_cu->objfile;
15820 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15821 info_ptr = per_objfile->info.buffer + per_cu->offset;
15822
15823 memset (cu_headerp, 0, sizeof (*cu_headerp));
15824 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15825
15826 return cu_headerp;
15827}
15828
ae0d2f24
UW
15829/* Return the address size given in the compilation unit header for CU. */
15830
15831CORE_ADDR
15832dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15833{
96408a79
SA
15834 struct comp_unit_head cu_header_local;
15835 const struct comp_unit_head *cu_headerp;
c471e790 15836
96408a79
SA
15837 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15838
15839 return cu_headerp->addr_size;
ae0d2f24
UW
15840}
15841
9eae7c52
TT
15842/* Return the offset size given in the compilation unit header for CU. */
15843
15844int
15845dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15846{
96408a79
SA
15847 struct comp_unit_head cu_header_local;
15848 const struct comp_unit_head *cu_headerp;
9c6c53f7 15849
96408a79
SA
15850 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15851
15852 return cu_headerp->offset_size;
15853}
15854
15855/* See its dwarf2loc.h declaration. */
15856
15857int
15858dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15859{
15860 struct comp_unit_head cu_header_local;
15861 const struct comp_unit_head *cu_headerp;
15862
15863 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15864
15865 if (cu_headerp->version == 2)
15866 return cu_headerp->addr_size;
15867 else
15868 return cu_headerp->offset_size;
181cebd4
JK
15869}
15870
9aa1f1e3
TT
15871/* Return the text offset of the CU. The returned offset comes from
15872 this CU's objfile. If this objfile came from a separate debuginfo
15873 file, then the offset may be different from the corresponding
15874 offset in the parent objfile. */
15875
15876CORE_ADDR
15877dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15878{
bb3fa9d0 15879 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15880
15881 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15882}
15883
348e048f
DE
15884/* Locate the .debug_info compilation unit from CU's objfile which contains
15885 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15886
15887static struct dwarf2_per_cu_data *
c764a876 15888dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15889 struct objfile *objfile)
15890{
15891 struct dwarf2_per_cu_data *this_cu;
15892 int low, high;
15893
ae038cb0
DJ
15894 low = 0;
15895 high = dwarf2_per_objfile->n_comp_units - 1;
15896 while (high > low)
15897 {
15898 int mid = low + (high - low) / 2;
9a619af0 15899
ae038cb0
DJ
15900 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15901 high = mid;
15902 else
15903 low = mid + 1;
15904 }
15905 gdb_assert (low == high);
15906 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15907 {
10b3939b 15908 if (low == 0)
8a3fe4f8
AC
15909 error (_("Dwarf Error: could not find partial DIE containing "
15910 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15911 (long) offset, bfd_get_filename (objfile->obfd));
15912
ae038cb0
DJ
15913 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15914 return dwarf2_per_objfile->all_comp_units[low-1];
15915 }
15916 else
15917 {
15918 this_cu = dwarf2_per_objfile->all_comp_units[low];
15919 if (low == dwarf2_per_objfile->n_comp_units - 1
15920 && offset >= this_cu->offset + this_cu->length)
c764a876 15921 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15922 gdb_assert (offset < this_cu->offset + this_cu->length);
15923 return this_cu;
15924 }
15925}
15926
10b3939b
DJ
15927/* Locate the compilation unit from OBJFILE which is located at exactly
15928 OFFSET. Raises an error on failure. */
15929
ae038cb0 15930static struct dwarf2_per_cu_data *
c764a876 15931dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
15932{
15933 struct dwarf2_per_cu_data *this_cu;
9a619af0 15934
ae038cb0
DJ
15935 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15936 if (this_cu->offset != offset)
c764a876 15937 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
15938 return this_cu;
15939}
15940
9816fde3 15941/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 15942
9816fde3
JK
15943static void
15944init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 15945{
9816fde3 15946 memset (cu, 0, sizeof (*cu));
93311388
DE
15947 cu->objfile = objfile;
15948 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15949}
15950
15951/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15952
15953static void
15954prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15955{
15956 struct attribute *attr;
15957
15958 /* Set the language we're debugging. */
15959 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15960 if (attr)
15961 set_cu_language (DW_UNSND (attr), cu);
15962 else
9cded63f
TT
15963 {
15964 cu->language = language_minimal;
15965 cu->language_defn = language_def (cu->language);
15966 }
93311388
DE
15967}
15968
ae038cb0
DJ
15969/* Release one cached compilation unit, CU. We unlink it from the tree
15970 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15971 the caller is responsible for that.
15972 NOTE: DATA is a void * because this function is also used as a
15973 cleanup routine. */
ae038cb0
DJ
15974
15975static void
15976free_one_comp_unit (void *data)
15977{
15978 struct dwarf2_cu *cu = data;
15979
15980 if (cu->per_cu != NULL)
15981 cu->per_cu->cu = NULL;
15982 cu->per_cu = NULL;
15983
15984 obstack_free (&cu->comp_unit_obstack, NULL);
15985
15986 xfree (cu);
15987}
15988
72bf9492 15989/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15990 when we're finished with it. We can't free the pointer itself, but be
15991 sure to unlink it from the cache. Also release any associated storage
15992 and perform cache maintenance.
72bf9492
DJ
15993
15994 Only used during partial symbol parsing. */
15995
15996static void
15997free_stack_comp_unit (void *data)
15998{
15999 struct dwarf2_cu *cu = data;
16000
16001 obstack_free (&cu->comp_unit_obstack, NULL);
16002 cu->partial_dies = NULL;
ae038cb0
DJ
16003
16004 if (cu->per_cu != NULL)
16005 {
16006 /* This compilation unit is on the stack in our caller, so we
16007 should not xfree it. Just unlink it. */
16008 cu->per_cu->cu = NULL;
16009 cu->per_cu = NULL;
16010
16011 /* If we had a per-cu pointer, then we may have other compilation
16012 units loaded, so age them now. */
16013 age_cached_comp_units ();
16014 }
16015}
16016
16017/* Free all cached compilation units. */
16018
16019static void
16020free_cached_comp_units (void *data)
16021{
16022 struct dwarf2_per_cu_data *per_cu, **last_chain;
16023
16024 per_cu = dwarf2_per_objfile->read_in_chain;
16025 last_chain = &dwarf2_per_objfile->read_in_chain;
16026 while (per_cu != NULL)
16027 {
16028 struct dwarf2_per_cu_data *next_cu;
16029
16030 next_cu = per_cu->cu->read_in_chain;
16031
16032 free_one_comp_unit (per_cu->cu);
16033 *last_chain = next_cu;
16034
16035 per_cu = next_cu;
16036 }
16037}
16038
16039/* Increase the age counter on each cached compilation unit, and free
16040 any that are too old. */
16041
16042static void
16043age_cached_comp_units (void)
16044{
16045 struct dwarf2_per_cu_data *per_cu, **last_chain;
16046
16047 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16048 per_cu = dwarf2_per_objfile->read_in_chain;
16049 while (per_cu != NULL)
16050 {
16051 per_cu->cu->last_used ++;
16052 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16053 dwarf2_mark (per_cu->cu);
16054 per_cu = per_cu->cu->read_in_chain;
16055 }
16056
16057 per_cu = dwarf2_per_objfile->read_in_chain;
16058 last_chain = &dwarf2_per_objfile->read_in_chain;
16059 while (per_cu != NULL)
16060 {
16061 struct dwarf2_per_cu_data *next_cu;
16062
16063 next_cu = per_cu->cu->read_in_chain;
16064
16065 if (!per_cu->cu->mark)
16066 {
16067 free_one_comp_unit (per_cu->cu);
16068 *last_chain = next_cu;
16069 }
16070 else
16071 last_chain = &per_cu->cu->read_in_chain;
16072
16073 per_cu = next_cu;
16074 }
16075}
16076
16077/* Remove a single compilation unit from the cache. */
16078
16079static void
16080free_one_cached_comp_unit (void *target_cu)
16081{
16082 struct dwarf2_per_cu_data *per_cu, **last_chain;
16083
16084 per_cu = dwarf2_per_objfile->read_in_chain;
16085 last_chain = &dwarf2_per_objfile->read_in_chain;
16086 while (per_cu != NULL)
16087 {
16088 struct dwarf2_per_cu_data *next_cu;
16089
16090 next_cu = per_cu->cu->read_in_chain;
16091
16092 if (per_cu->cu == target_cu)
16093 {
16094 free_one_comp_unit (per_cu->cu);
16095 *last_chain = next_cu;
16096 break;
16097 }
16098 else
16099 last_chain = &per_cu->cu->read_in_chain;
16100
16101 per_cu = next_cu;
16102 }
16103}
16104
fe3e1990
DJ
16105/* Release all extra memory associated with OBJFILE. */
16106
16107void
16108dwarf2_free_objfile (struct objfile *objfile)
16109{
16110 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16111
16112 if (dwarf2_per_objfile == NULL)
16113 return;
16114
16115 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16116 free_cached_comp_units (NULL);
16117
7b9f3c50
DE
16118 if (dwarf2_per_objfile->quick_file_names_table)
16119 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16120
fe3e1990
DJ
16121 /* Everything else should be on the objfile obstack. */
16122}
16123
1c379e20
DJ
16124/* A pair of DIE offset and GDB type pointer. We store these
16125 in a hash table separate from the DIEs, and preserve them
16126 when the DIEs are flushed out of cache. */
16127
16128struct dwarf2_offset_and_type
16129{
16130 unsigned int offset;
16131 struct type *type;
16132};
16133
16134/* Hash function for a dwarf2_offset_and_type. */
16135
16136static hashval_t
16137offset_and_type_hash (const void *item)
16138{
16139 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 16140
1c379e20
DJ
16141 return ofs->offset;
16142}
16143
16144/* Equality function for a dwarf2_offset_and_type. */
16145
16146static int
16147offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16148{
16149 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16150 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16151
1c379e20
DJ
16152 return ofs_lhs->offset == ofs_rhs->offset;
16153}
16154
16155/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16156 table if necessary. For convenience, return TYPE.
16157
16158 The DIEs reading must have careful ordering to:
16159 * Not cause infite loops trying to read in DIEs as a prerequisite for
16160 reading current DIE.
16161 * Not trying to dereference contents of still incompletely read in types
16162 while reading in other DIEs.
16163 * Enable referencing still incompletely read in types just by a pointer to
16164 the type without accessing its fields.
16165
16166 Therefore caller should follow these rules:
16167 * Try to fetch any prerequisite types we may need to build this DIE type
16168 before building the type and calling set_die_type.
e71ec853 16169 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16170 possible before fetching more types to complete the current type.
16171 * Make the type as complete as possible before fetching more types. */
1c379e20 16172
f792889a 16173static struct type *
1c379e20
DJ
16174set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16175{
16176 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
16177 struct objfile *objfile = cu->objfile;
16178 htab_t *type_hash_ptr;
1c379e20 16179
b4ba55a1
JB
16180 /* For Ada types, make sure that the gnat-specific data is always
16181 initialized (if not already set). There are a few types where
16182 we should not be doing so, because the type-specific area is
16183 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16184 where the type-specific area is used to store the floatformat).
16185 But this is not a problem, because the gnat-specific information
16186 is actually not needed for these types. */
16187 if (need_gnat_info (cu)
16188 && TYPE_CODE (type) != TYPE_CODE_FUNC
16189 && TYPE_CODE (type) != TYPE_CODE_FLT
16190 && !HAVE_GNAT_AUX_INFO (type))
16191 INIT_GNAT_SPECIFIC (type);
16192
b0df02fd 16193 if (cu->per_cu->debug_types_section)
673bfd45
DE
16194 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16195 else
16196 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16197
16198 if (*type_hash_ptr == NULL)
f792889a 16199 {
673bfd45
DE
16200 *type_hash_ptr
16201 = htab_create_alloc_ex (127,
f792889a
DJ
16202 offset_and_type_hash,
16203 offset_and_type_eq,
16204 NULL,
673bfd45 16205 &objfile->objfile_obstack,
f792889a
DJ
16206 hashtab_obstack_allocate,
16207 dummy_obstack_deallocate);
f792889a 16208 }
1c379e20
DJ
16209
16210 ofs.offset = die->offset;
16211 ofs.type = type;
16212 slot = (struct dwarf2_offset_and_type **)
673bfd45 16213 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
16214 if (*slot)
16215 complaint (&symfile_complaints,
16216 _("A problem internal to GDB: DIE 0x%x has type already set"),
16217 die->offset);
673bfd45 16218 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16219 **slot = ofs;
f792889a 16220 return type;
1c379e20
DJ
16221}
16222
673bfd45
DE
16223/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16224 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16225
16226static struct type *
673bfd45
DE
16227get_die_type_at_offset (unsigned int offset,
16228 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16229{
16230 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16231 htab_t type_hash;
f792889a 16232
b0df02fd 16233 if (per_cu->debug_types_section)
673bfd45
DE
16234 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16235 else
16236 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16237 if (type_hash == NULL)
16238 return NULL;
1c379e20 16239
673bfd45 16240 ofs.offset = offset;
1c379e20
DJ
16241 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16242 if (slot)
16243 return slot->type;
16244 else
16245 return NULL;
16246}
16247
673bfd45
DE
16248/* Look up the type for DIE in the appropriate type_hash table,
16249 or return NULL if DIE does not have a saved type. */
16250
16251static struct type *
16252get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16253{
16254 return get_die_type_at_offset (die->offset, cu->per_cu);
16255}
16256
10b3939b
DJ
16257/* Add a dependence relationship from CU to REF_PER_CU. */
16258
16259static void
16260dwarf2_add_dependence (struct dwarf2_cu *cu,
16261 struct dwarf2_per_cu_data *ref_per_cu)
16262{
16263 void **slot;
16264
16265 if (cu->dependencies == NULL)
16266 cu->dependencies
16267 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16268 NULL, &cu->comp_unit_obstack,
16269 hashtab_obstack_allocate,
16270 dummy_obstack_deallocate);
16271
16272 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16273 if (*slot == NULL)
16274 *slot = ref_per_cu;
16275}
1c379e20 16276
f504f079
DE
16277/* Subroutine of dwarf2_mark to pass to htab_traverse.
16278 Set the mark field in every compilation unit in the
ae038cb0
DJ
16279 cache that we must keep because we are keeping CU. */
16280
10b3939b
DJ
16281static int
16282dwarf2_mark_helper (void **slot, void *data)
16283{
16284 struct dwarf2_per_cu_data *per_cu;
16285
16286 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16287
16288 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16289 reading of the chain. As such dependencies remain valid it is not much
16290 useful to track and undo them during QUIT cleanups. */
16291 if (per_cu->cu == NULL)
16292 return 1;
16293
10b3939b
DJ
16294 if (per_cu->cu->mark)
16295 return 1;
16296 per_cu->cu->mark = 1;
16297
16298 if (per_cu->cu->dependencies != NULL)
16299 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16300
16301 return 1;
16302}
16303
f504f079
DE
16304/* Set the mark field in CU and in every other compilation unit in the
16305 cache that we must keep because we are keeping CU. */
16306
ae038cb0
DJ
16307static void
16308dwarf2_mark (struct dwarf2_cu *cu)
16309{
16310 if (cu->mark)
16311 return;
16312 cu->mark = 1;
10b3939b
DJ
16313 if (cu->dependencies != NULL)
16314 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16315}
16316
16317static void
16318dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16319{
16320 while (per_cu)
16321 {
16322 per_cu->cu->mark = 0;
16323 per_cu = per_cu->cu->read_in_chain;
16324 }
72bf9492
DJ
16325}
16326
72bf9492
DJ
16327/* Trivial hash function for partial_die_info: the hash value of a DIE
16328 is its offset in .debug_info for this objfile. */
16329
16330static hashval_t
16331partial_die_hash (const void *item)
16332{
16333 const struct partial_die_info *part_die = item;
9a619af0 16334
72bf9492
DJ
16335 return part_die->offset;
16336}
16337
16338/* Trivial comparison function for partial_die_info structures: two DIEs
16339 are equal if they have the same offset. */
16340
16341static int
16342partial_die_eq (const void *item_lhs, const void *item_rhs)
16343{
16344 const struct partial_die_info *part_die_lhs = item_lhs;
16345 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16346
72bf9492
DJ
16347 return part_die_lhs->offset == part_die_rhs->offset;
16348}
16349
ae038cb0
DJ
16350static struct cmd_list_element *set_dwarf2_cmdlist;
16351static struct cmd_list_element *show_dwarf2_cmdlist;
16352
16353static void
16354set_dwarf2_cmd (char *args, int from_tty)
16355{
16356 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16357}
16358
16359static void
16360show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16361{
ae038cb0
DJ
16362 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16363}
16364
dce234bc
PP
16365/* If section described by INFO was mmapped, munmap it now. */
16366
16367static void
16368munmap_section_buffer (struct dwarf2_section_info *info)
16369{
b315ab21 16370 if (info->map_addr != NULL)
dce234bc
PP
16371 {
16372#ifdef HAVE_MMAP
b315ab21 16373 int res;
9a619af0 16374
b315ab21
TG
16375 res = munmap (info->map_addr, info->map_len);
16376 gdb_assert (res == 0);
dce234bc
PP
16377#else
16378 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16379 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16380#endif
16381 }
16382}
16383
16384/* munmap debug sections for OBJFILE, if necessary. */
16385
16386static void
c1bd65d0 16387dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16388{
16389 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16390 int ix;
16391 struct dwarf2_section_info *section;
9a619af0 16392
16be1145
DE
16393 /* This is sorted according to the order they're defined in to make it easier
16394 to keep in sync. */
dce234bc
PP
16395 munmap_section_buffer (&data->info);
16396 munmap_section_buffer (&data->abbrev);
16397 munmap_section_buffer (&data->line);
16be1145 16398 munmap_section_buffer (&data->loc);
dce234bc 16399 munmap_section_buffer (&data->macinfo);
cf2c3c16 16400 munmap_section_buffer (&data->macro);
16be1145 16401 munmap_section_buffer (&data->str);
dce234bc 16402 munmap_section_buffer (&data->ranges);
dce234bc
PP
16403 munmap_section_buffer (&data->frame);
16404 munmap_section_buffer (&data->eh_frame);
9291a0cd 16405 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16406
16407 for (ix = 0;
16408 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16409 ++ix)
16410 munmap_section_buffer (section);
16411
16412 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16413}
16414
16415\f
ae2de4f8 16416/* The "save gdb-index" command. */
9291a0cd
TT
16417
16418/* The contents of the hash table we create when building the string
16419 table. */
16420struct strtab_entry
16421{
16422 offset_type offset;
16423 const char *str;
16424};
16425
559a7a62
JK
16426/* Hash function for a strtab_entry.
16427
16428 Function is used only during write_hash_table so no index format backward
16429 compatibility is needed. */
b89be57b 16430
9291a0cd
TT
16431static hashval_t
16432hash_strtab_entry (const void *e)
16433{
16434 const struct strtab_entry *entry = e;
559a7a62 16435 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16436}
16437
16438/* Equality function for a strtab_entry. */
b89be57b 16439
9291a0cd
TT
16440static int
16441eq_strtab_entry (const void *a, const void *b)
16442{
16443 const struct strtab_entry *ea = a;
16444 const struct strtab_entry *eb = b;
16445 return !strcmp (ea->str, eb->str);
16446}
16447
16448/* Create a strtab_entry hash table. */
b89be57b 16449
9291a0cd
TT
16450static htab_t
16451create_strtab (void)
16452{
16453 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16454 xfree, xcalloc, xfree);
16455}
16456
16457/* Add a string to the constant pool. Return the string's offset in
16458 host order. */
b89be57b 16459
9291a0cd
TT
16460static offset_type
16461add_string (htab_t table, struct obstack *cpool, const char *str)
16462{
16463 void **slot;
16464 struct strtab_entry entry;
16465 struct strtab_entry *result;
16466
16467 entry.str = str;
16468 slot = htab_find_slot (table, &entry, INSERT);
16469 if (*slot)
16470 result = *slot;
16471 else
16472 {
16473 result = XNEW (struct strtab_entry);
16474 result->offset = obstack_object_size (cpool);
16475 result->str = str;
16476 obstack_grow_str0 (cpool, str);
16477 *slot = result;
16478 }
16479 return result->offset;
16480}
16481
16482/* An entry in the symbol table. */
16483struct symtab_index_entry
16484{
16485 /* The name of the symbol. */
16486 const char *name;
16487 /* The offset of the name in the constant pool. */
16488 offset_type index_offset;
16489 /* A sorted vector of the indices of all the CUs that hold an object
16490 of this name. */
16491 VEC (offset_type) *cu_indices;
16492};
16493
16494/* The symbol table. This is a power-of-2-sized hash table. */
16495struct mapped_symtab
16496{
16497 offset_type n_elements;
16498 offset_type size;
16499 struct symtab_index_entry **data;
16500};
16501
16502/* Hash function for a symtab_index_entry. */
b89be57b 16503
9291a0cd
TT
16504static hashval_t
16505hash_symtab_entry (const void *e)
16506{
16507 const struct symtab_index_entry *entry = e;
16508 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16509 sizeof (offset_type) * VEC_length (offset_type,
16510 entry->cu_indices),
16511 0);
16512}
16513
16514/* Equality function for a symtab_index_entry. */
b89be57b 16515
9291a0cd
TT
16516static int
16517eq_symtab_entry (const void *a, const void *b)
16518{
16519 const struct symtab_index_entry *ea = a;
16520 const struct symtab_index_entry *eb = b;
16521 int len = VEC_length (offset_type, ea->cu_indices);
16522 if (len != VEC_length (offset_type, eb->cu_indices))
16523 return 0;
16524 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16525 VEC_address (offset_type, eb->cu_indices),
16526 sizeof (offset_type) * len);
16527}
16528
16529/* Destroy a symtab_index_entry. */
b89be57b 16530
9291a0cd
TT
16531static void
16532delete_symtab_entry (void *p)
16533{
16534 struct symtab_index_entry *entry = p;
16535 VEC_free (offset_type, entry->cu_indices);
16536 xfree (entry);
16537}
16538
16539/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16540
9291a0cd 16541static htab_t
3876f04e 16542create_symbol_hash_table (void)
9291a0cd
TT
16543{
16544 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16545 delete_symtab_entry, xcalloc, xfree);
16546}
16547
16548/* Create a new mapped symtab object. */
b89be57b 16549
9291a0cd
TT
16550static struct mapped_symtab *
16551create_mapped_symtab (void)
16552{
16553 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16554 symtab->n_elements = 0;
16555 symtab->size = 1024;
16556 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16557 return symtab;
16558}
16559
16560/* Destroy a mapped_symtab. */
b89be57b 16561
9291a0cd
TT
16562static void
16563cleanup_mapped_symtab (void *p)
16564{
16565 struct mapped_symtab *symtab = p;
16566 /* The contents of the array are freed when the other hash table is
16567 destroyed. */
16568 xfree (symtab->data);
16569 xfree (symtab);
16570}
16571
16572/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16573 the slot.
16574
16575 Function is used only during write_hash_table so no index format backward
16576 compatibility is needed. */
b89be57b 16577
9291a0cd
TT
16578static struct symtab_index_entry **
16579find_slot (struct mapped_symtab *symtab, const char *name)
16580{
559a7a62 16581 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16582
16583 index = hash & (symtab->size - 1);
16584 step = ((hash * 17) & (symtab->size - 1)) | 1;
16585
16586 for (;;)
16587 {
16588 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16589 return &symtab->data[index];
16590 index = (index + step) & (symtab->size - 1);
16591 }
16592}
16593
16594/* Expand SYMTAB's hash table. */
b89be57b 16595
9291a0cd
TT
16596static void
16597hash_expand (struct mapped_symtab *symtab)
16598{
16599 offset_type old_size = symtab->size;
16600 offset_type i;
16601 struct symtab_index_entry **old_entries = symtab->data;
16602
16603 symtab->size *= 2;
16604 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16605
16606 for (i = 0; i < old_size; ++i)
16607 {
16608 if (old_entries[i])
16609 {
16610 struct symtab_index_entry **slot = find_slot (symtab,
16611 old_entries[i]->name);
16612 *slot = old_entries[i];
16613 }
16614 }
16615
16616 xfree (old_entries);
16617}
16618
16619/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16620 is the index of the CU in which the symbol appears. */
b89be57b 16621
9291a0cd
TT
16622static void
16623add_index_entry (struct mapped_symtab *symtab, const char *name,
16624 offset_type cu_index)
16625{
16626 struct symtab_index_entry **slot;
16627
16628 ++symtab->n_elements;
16629 if (4 * symtab->n_elements / 3 >= symtab->size)
16630 hash_expand (symtab);
16631
16632 slot = find_slot (symtab, name);
16633 if (!*slot)
16634 {
16635 *slot = XNEW (struct symtab_index_entry);
16636 (*slot)->name = name;
16637 (*slot)->cu_indices = NULL;
16638 }
16639 /* Don't push an index twice. Due to how we add entries we only
16640 have to check the last one. */
16641 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16642 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16643 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16644}
16645
16646/* Add a vector of indices to the constant pool. */
b89be57b 16647
9291a0cd 16648static offset_type
3876f04e 16649add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16650 struct symtab_index_entry *entry)
16651{
16652 void **slot;
16653
3876f04e 16654 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16655 if (!*slot)
16656 {
16657 offset_type len = VEC_length (offset_type, entry->cu_indices);
16658 offset_type val = MAYBE_SWAP (len);
16659 offset_type iter;
16660 int i;
16661
16662 *slot = entry;
16663 entry->index_offset = obstack_object_size (cpool);
16664
16665 obstack_grow (cpool, &val, sizeof (val));
16666 for (i = 0;
16667 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16668 ++i)
16669 {
16670 val = MAYBE_SWAP (iter);
16671 obstack_grow (cpool, &val, sizeof (val));
16672 }
16673 }
16674 else
16675 {
16676 struct symtab_index_entry *old_entry = *slot;
16677 entry->index_offset = old_entry->index_offset;
16678 entry = old_entry;
16679 }
16680 return entry->index_offset;
16681}
16682
16683/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16684 constant pool entries going into the obstack CPOOL. */
b89be57b 16685
9291a0cd
TT
16686static void
16687write_hash_table (struct mapped_symtab *symtab,
16688 struct obstack *output, struct obstack *cpool)
16689{
16690 offset_type i;
3876f04e 16691 htab_t symbol_hash_table;
9291a0cd
TT
16692 htab_t str_table;
16693
3876f04e 16694 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16695 str_table = create_strtab ();
3876f04e 16696
9291a0cd
TT
16697 /* We add all the index vectors to the constant pool first, to
16698 ensure alignment is ok. */
16699 for (i = 0; i < symtab->size; ++i)
16700 {
16701 if (symtab->data[i])
3876f04e 16702 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16703 }
16704
16705 /* Now write out the hash table. */
16706 for (i = 0; i < symtab->size; ++i)
16707 {
16708 offset_type str_off, vec_off;
16709
16710 if (symtab->data[i])
16711 {
16712 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16713 vec_off = symtab->data[i]->index_offset;
16714 }
16715 else
16716 {
16717 /* While 0 is a valid constant pool index, it is not valid
16718 to have 0 for both offsets. */
16719 str_off = 0;
16720 vec_off = 0;
16721 }
16722
16723 str_off = MAYBE_SWAP (str_off);
16724 vec_off = MAYBE_SWAP (vec_off);
16725
16726 obstack_grow (output, &str_off, sizeof (str_off));
16727 obstack_grow (output, &vec_off, sizeof (vec_off));
16728 }
16729
16730 htab_delete (str_table);
3876f04e 16731 htab_delete (symbol_hash_table);
9291a0cd
TT
16732}
16733
0a5429f6
DE
16734/* Struct to map psymtab to CU index in the index file. */
16735struct psymtab_cu_index_map
16736{
16737 struct partial_symtab *psymtab;
16738 unsigned int cu_index;
16739};
16740
16741static hashval_t
16742hash_psymtab_cu_index (const void *item)
16743{
16744 const struct psymtab_cu_index_map *map = item;
16745
16746 return htab_hash_pointer (map->psymtab);
16747}
16748
16749static int
16750eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16751{
16752 const struct psymtab_cu_index_map *lhs = item_lhs;
16753 const struct psymtab_cu_index_map *rhs = item_rhs;
16754
16755 return lhs->psymtab == rhs->psymtab;
16756}
16757
16758/* Helper struct for building the address table. */
16759struct addrmap_index_data
16760{
16761 struct objfile *objfile;
16762 struct obstack *addr_obstack;
16763 htab_t cu_index_htab;
16764
16765 /* Non-zero if the previous_* fields are valid.
16766 We can't write an entry until we see the next entry (since it is only then
16767 that we know the end of the entry). */
16768 int previous_valid;
16769 /* Index of the CU in the table of all CUs in the index file. */
16770 unsigned int previous_cu_index;
0963b4bd 16771 /* Start address of the CU. */
0a5429f6
DE
16772 CORE_ADDR previous_cu_start;
16773};
16774
16775/* Write an address entry to OBSTACK. */
b89be57b 16776
9291a0cd 16777static void
0a5429f6
DE
16778add_address_entry (struct objfile *objfile, struct obstack *obstack,
16779 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16780{
0a5429f6 16781 offset_type cu_index_to_write;
9291a0cd
TT
16782 char addr[8];
16783 CORE_ADDR baseaddr;
16784
16785 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16786
0a5429f6
DE
16787 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16788 obstack_grow (obstack, addr, 8);
16789 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16790 obstack_grow (obstack, addr, 8);
16791 cu_index_to_write = MAYBE_SWAP (cu_index);
16792 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16793}
16794
16795/* Worker function for traversing an addrmap to build the address table. */
16796
16797static int
16798add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16799{
16800 struct addrmap_index_data *data = datap;
16801 struct partial_symtab *pst = obj;
16802 offset_type cu_index;
16803 void **slot;
16804
16805 if (data->previous_valid)
16806 add_address_entry (data->objfile, data->addr_obstack,
16807 data->previous_cu_start, start_addr,
16808 data->previous_cu_index);
16809
16810 data->previous_cu_start = start_addr;
16811 if (pst != NULL)
16812 {
16813 struct psymtab_cu_index_map find_map, *map;
16814 find_map.psymtab = pst;
16815 map = htab_find (data->cu_index_htab, &find_map);
16816 gdb_assert (map != NULL);
16817 data->previous_cu_index = map->cu_index;
16818 data->previous_valid = 1;
16819 }
16820 else
16821 data->previous_valid = 0;
16822
16823 return 0;
16824}
16825
16826/* Write OBJFILE's address map to OBSTACK.
16827 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16828 in the index file. */
16829
16830static void
16831write_address_map (struct objfile *objfile, struct obstack *obstack,
16832 htab_t cu_index_htab)
16833{
16834 struct addrmap_index_data addrmap_index_data;
16835
16836 /* When writing the address table, we have to cope with the fact that
16837 the addrmap iterator only provides the start of a region; we have to
16838 wait until the next invocation to get the start of the next region. */
16839
16840 addrmap_index_data.objfile = objfile;
16841 addrmap_index_data.addr_obstack = obstack;
16842 addrmap_index_data.cu_index_htab = cu_index_htab;
16843 addrmap_index_data.previous_valid = 0;
16844
16845 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16846 &addrmap_index_data);
16847
16848 /* It's highly unlikely the last entry (end address = 0xff...ff)
16849 is valid, but we should still handle it.
16850 The end address is recorded as the start of the next region, but that
16851 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16852 anyway. */
16853 if (addrmap_index_data.previous_valid)
16854 add_address_entry (objfile, obstack,
16855 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16856 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16857}
16858
16859/* Add a list of partial symbols to SYMTAB. */
b89be57b 16860
9291a0cd
TT
16861static void
16862write_psymbols (struct mapped_symtab *symtab,
987d643c 16863 htab_t psyms_seen,
9291a0cd
TT
16864 struct partial_symbol **psymp,
16865 int count,
987d643c
TT
16866 offset_type cu_index,
16867 int is_static)
9291a0cd
TT
16868{
16869 for (; count-- > 0; ++psymp)
16870 {
987d643c
TT
16871 void **slot, *lookup;
16872
9291a0cd
TT
16873 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16874 error (_("Ada is not currently supported by the index"));
987d643c
TT
16875
16876 /* We only want to add a given psymbol once. However, we also
16877 want to account for whether it is global or static. So, we
16878 may add it twice, using slightly different values. */
16879 if (is_static)
16880 {
16881 uintptr_t val = 1 | (uintptr_t) *psymp;
16882
16883 lookup = (void *) val;
16884 }
16885 else
16886 lookup = *psymp;
16887
16888 /* Only add a given psymbol once. */
16889 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16890 if (!*slot)
16891 {
16892 *slot = lookup;
bb2f58dc 16893 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 16894 }
9291a0cd
TT
16895 }
16896}
16897
16898/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16899 exception if there is an error. */
b89be57b 16900
9291a0cd
TT
16901static void
16902write_obstack (FILE *file, struct obstack *obstack)
16903{
16904 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16905 file)
16906 != obstack_object_size (obstack))
16907 error (_("couldn't data write to file"));
16908}
16909
16910/* Unlink a file if the argument is not NULL. */
b89be57b 16911
9291a0cd
TT
16912static void
16913unlink_if_set (void *p)
16914{
16915 char **filename = p;
16916 if (*filename)
16917 unlink (*filename);
16918}
16919
1fd400ff
TT
16920/* A helper struct used when iterating over debug_types. */
16921struct signatured_type_index_data
16922{
16923 struct objfile *objfile;
16924 struct mapped_symtab *symtab;
16925 struct obstack *types_list;
987d643c 16926 htab_t psyms_seen;
1fd400ff
TT
16927 int cu_index;
16928};
16929
16930/* A helper function that writes a single signatured_type to an
16931 obstack. */
b89be57b 16932
1fd400ff
TT
16933static int
16934write_one_signatured_type (void **slot, void *d)
16935{
16936 struct signatured_type_index_data *info = d;
16937 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16938 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16939 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16940 gdb_byte val[8];
16941
16942 write_psymbols (info->symtab,
987d643c 16943 info->psyms_seen,
3e43a32a
MS
16944 info->objfile->global_psymbols.list
16945 + psymtab->globals_offset,
987d643c
TT
16946 psymtab->n_global_syms, info->cu_index,
16947 0);
1fd400ff 16948 write_psymbols (info->symtab,
987d643c 16949 info->psyms_seen,
3e43a32a
MS
16950 info->objfile->static_psymbols.list
16951 + psymtab->statics_offset,
987d643c
TT
16952 psymtab->n_static_syms, info->cu_index,
16953 1);
1fd400ff 16954
b3c8eb43 16955 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16956 obstack_grow (info->types_list, val, 8);
16957 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16958 obstack_grow (info->types_list, val, 8);
16959 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16960 obstack_grow (info->types_list, val, 8);
16961
16962 ++info->cu_index;
16963
16964 return 1;
16965}
16966
9291a0cd 16967/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16968
9291a0cd
TT
16969static void
16970write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16971{
16972 struct cleanup *cleanup;
16973 char *filename, *cleanup_filename;
1fd400ff
TT
16974 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16975 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16976 int i;
16977 FILE *out_file;
16978 struct mapped_symtab *symtab;
16979 offset_type val, size_of_contents, total_len;
16980 struct stat st;
16981 char buf[8];
987d643c 16982 htab_t psyms_seen;
0a5429f6
DE
16983 htab_t cu_index_htab;
16984 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16985
b4f2f049 16986 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16987 return;
b4f2f049 16988
9291a0cd
TT
16989 if (dwarf2_per_objfile->using_index)
16990 error (_("Cannot use an index to create the index"));
16991
8b70b953
TT
16992 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16993 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16994
9291a0cd 16995 if (stat (objfile->name, &st) < 0)
7e17e088 16996 perror_with_name (objfile->name);
9291a0cd
TT
16997
16998 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16999 INDEX_SUFFIX, (char *) NULL);
17000 cleanup = make_cleanup (xfree, filename);
17001
17002 out_file = fopen (filename, "wb");
17003 if (!out_file)
17004 error (_("Can't open `%s' for writing"), filename);
17005
17006 cleanup_filename = filename;
17007 make_cleanup (unlink_if_set, &cleanup_filename);
17008
17009 symtab = create_mapped_symtab ();
17010 make_cleanup (cleanup_mapped_symtab, symtab);
17011
17012 obstack_init (&addr_obstack);
17013 make_cleanup_obstack_free (&addr_obstack);
17014
17015 obstack_init (&cu_list);
17016 make_cleanup_obstack_free (&cu_list);
17017
1fd400ff
TT
17018 obstack_init (&types_cu_list);
17019 make_cleanup_obstack_free (&types_cu_list);
17020
987d643c
TT
17021 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17022 NULL, xcalloc, xfree);
96408a79 17023 make_cleanup_htab_delete (psyms_seen);
987d643c 17024
0a5429f6
DE
17025 /* While we're scanning CU's create a table that maps a psymtab pointer
17026 (which is what addrmap records) to its index (which is what is recorded
17027 in the index file). This will later be needed to write the address
17028 table. */
17029 cu_index_htab = htab_create_alloc (100,
17030 hash_psymtab_cu_index,
17031 eq_psymtab_cu_index,
17032 NULL, xcalloc, xfree);
96408a79 17033 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
17034 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17035 xmalloc (sizeof (struct psymtab_cu_index_map)
17036 * dwarf2_per_objfile->n_comp_units);
17037 make_cleanup (xfree, psymtab_cu_index_map);
17038
17039 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
17040 work here. Also, the debug_types entries do not appear in
17041 all_comp_units, but only in their own hash table. */
9291a0cd
TT
17042 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17043 {
3e43a32a
MS
17044 struct dwarf2_per_cu_data *per_cu
17045 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 17046 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 17047 gdb_byte val[8];
0a5429f6
DE
17048 struct psymtab_cu_index_map *map;
17049 void **slot;
9291a0cd
TT
17050
17051 write_psymbols (symtab,
987d643c 17052 psyms_seen,
9291a0cd 17053 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
17054 psymtab->n_global_syms, i,
17055 0);
9291a0cd 17056 write_psymbols (symtab,
987d643c 17057 psyms_seen,
9291a0cd 17058 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
17059 psymtab->n_static_syms, i,
17060 1);
9291a0cd 17061
0a5429f6
DE
17062 map = &psymtab_cu_index_map[i];
17063 map->psymtab = psymtab;
17064 map->cu_index = i;
17065 slot = htab_find_slot (cu_index_htab, map, INSERT);
17066 gdb_assert (slot != NULL);
17067 gdb_assert (*slot == NULL);
17068 *slot = map;
9291a0cd 17069
e254ef6a 17070 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 17071 obstack_grow (&cu_list, val, 8);
e254ef6a 17072 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17073 obstack_grow (&cu_list, val, 8);
17074 }
17075
0a5429f6
DE
17076 /* Dump the address map. */
17077 write_address_map (objfile, &addr_obstack, cu_index_htab);
17078
1fd400ff
TT
17079 /* Write out the .debug_type entries, if any. */
17080 if (dwarf2_per_objfile->signatured_types)
17081 {
17082 struct signatured_type_index_data sig_data;
17083
17084 sig_data.objfile = objfile;
17085 sig_data.symtab = symtab;
17086 sig_data.types_list = &types_cu_list;
987d643c 17087 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17088 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17089 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17090 write_one_signatured_type, &sig_data);
17091 }
17092
9291a0cd
TT
17093 obstack_init (&constant_pool);
17094 make_cleanup_obstack_free (&constant_pool);
17095 obstack_init (&symtab_obstack);
17096 make_cleanup_obstack_free (&symtab_obstack);
17097 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17098
17099 obstack_init (&contents);
17100 make_cleanup_obstack_free (&contents);
1fd400ff 17101 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17102 total_len = size_of_contents;
17103
17104 /* The version number. */
559a7a62 17105 val = MAYBE_SWAP (5);
9291a0cd
TT
17106 obstack_grow (&contents, &val, sizeof (val));
17107
17108 /* The offset of the CU list from the start of the file. */
17109 val = MAYBE_SWAP (total_len);
17110 obstack_grow (&contents, &val, sizeof (val));
17111 total_len += obstack_object_size (&cu_list);
17112
1fd400ff
TT
17113 /* The offset of the types CU list from the start of the file. */
17114 val = MAYBE_SWAP (total_len);
17115 obstack_grow (&contents, &val, sizeof (val));
17116 total_len += obstack_object_size (&types_cu_list);
17117
9291a0cd
TT
17118 /* The offset of the address table from the start of the file. */
17119 val = MAYBE_SWAP (total_len);
17120 obstack_grow (&contents, &val, sizeof (val));
17121 total_len += obstack_object_size (&addr_obstack);
17122
17123 /* The offset of the symbol table from the start of the file. */
17124 val = MAYBE_SWAP (total_len);
17125 obstack_grow (&contents, &val, sizeof (val));
17126 total_len += obstack_object_size (&symtab_obstack);
17127
17128 /* The offset of the constant pool from the start of the file. */
17129 val = MAYBE_SWAP (total_len);
17130 obstack_grow (&contents, &val, sizeof (val));
17131 total_len += obstack_object_size (&constant_pool);
17132
17133 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17134
17135 write_obstack (out_file, &contents);
17136 write_obstack (out_file, &cu_list);
1fd400ff 17137 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17138 write_obstack (out_file, &addr_obstack);
17139 write_obstack (out_file, &symtab_obstack);
17140 write_obstack (out_file, &constant_pool);
17141
17142 fclose (out_file);
17143
17144 /* We want to keep the file, so we set cleanup_filename to NULL
17145 here. See unlink_if_set. */
17146 cleanup_filename = NULL;
17147
17148 do_cleanups (cleanup);
17149}
17150
90476074
TT
17151/* Implementation of the `save gdb-index' command.
17152
17153 Note that the file format used by this command is documented in the
17154 GDB manual. Any changes here must be documented there. */
11570e71 17155
9291a0cd
TT
17156static void
17157save_gdb_index_command (char *arg, int from_tty)
17158{
17159 struct objfile *objfile;
17160
17161 if (!arg || !*arg)
96d19272 17162 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17163
17164 ALL_OBJFILES (objfile)
17165 {
17166 struct stat st;
17167
17168 /* If the objfile does not correspond to an actual file, skip it. */
17169 if (stat (objfile->name, &st) < 0)
17170 continue;
17171
17172 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17173 if (dwarf2_per_objfile)
17174 {
17175 volatile struct gdb_exception except;
17176
17177 TRY_CATCH (except, RETURN_MASK_ERROR)
17178 {
17179 write_psymtabs_to_index (objfile, arg);
17180 }
17181 if (except.reason < 0)
17182 exception_fprintf (gdb_stderr, except,
17183 _("Error while writing index for `%s': "),
17184 objfile->name);
17185 }
17186 }
dce234bc
PP
17187}
17188
9291a0cd
TT
17189\f
17190
9eae7c52
TT
17191int dwarf2_always_disassemble;
17192
17193static void
17194show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17195 struct cmd_list_element *c, const char *value)
17196{
3e43a32a
MS
17197 fprintf_filtered (file,
17198 _("Whether to always disassemble "
17199 "DWARF expressions is %s.\n"),
9eae7c52
TT
17200 value);
17201}
17202
900e11f9
JK
17203static void
17204show_check_physname (struct ui_file *file, int from_tty,
17205 struct cmd_list_element *c, const char *value)
17206{
17207 fprintf_filtered (file,
17208 _("Whether to check \"physname\" is %s.\n"),
17209 value);
17210}
17211
6502dd73
DJ
17212void _initialize_dwarf2_read (void);
17213
17214void
17215_initialize_dwarf2_read (void)
17216{
96d19272
JK
17217 struct cmd_list_element *c;
17218
dce234bc 17219 dwarf2_objfile_data_key
c1bd65d0 17220 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17221
1bedd215
AC
17222 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17223Set DWARF 2 specific variables.\n\
17224Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17225 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17226 0/*allow-unknown*/, &maintenance_set_cmdlist);
17227
1bedd215
AC
17228 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17229Show DWARF 2 specific variables\n\
17230Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17231 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17232 0/*allow-unknown*/, &maintenance_show_cmdlist);
17233
17234 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17235 &dwarf2_max_cache_age, _("\
17236Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17237Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17238A higher limit means that cached compilation units will be stored\n\
17239in memory longer, and more total memory will be used. Zero disables\n\
17240caching, which can slow down startup."),
2c5b56ce 17241 NULL,
920d2a44 17242 show_dwarf2_max_cache_age,
2c5b56ce 17243 &set_dwarf2_cmdlist,
ae038cb0 17244 &show_dwarf2_cmdlist);
d97bc12b 17245
9eae7c52
TT
17246 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17247 &dwarf2_always_disassemble, _("\
17248Set whether `info address' always disassembles DWARF expressions."), _("\
17249Show whether `info address' always disassembles DWARF expressions."), _("\
17250When enabled, DWARF expressions are always printed in an assembly-like\n\
17251syntax. When disabled, expressions will be printed in a more\n\
17252conversational style, when possible."),
17253 NULL,
17254 show_dwarf2_always_disassemble,
17255 &set_dwarf2_cmdlist,
17256 &show_dwarf2_cmdlist);
17257
d97bc12b
DE
17258 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17259Set debugging of the dwarf2 DIE reader."), _("\
17260Show debugging of the dwarf2 DIE reader."), _("\
17261When enabled (non-zero), DIEs are dumped after they are read in.\n\
17262The value is the maximum depth to print."),
17263 NULL,
17264 NULL,
17265 &setdebuglist, &showdebuglist);
9291a0cd 17266
900e11f9
JK
17267 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17268Set cross-checking of \"physname\" code against demangler."), _("\
17269Show cross-checking of \"physname\" code against demangler."), _("\
17270When enabled, GDB's internal \"physname\" code is checked against\n\
17271the demangler."),
17272 NULL, show_check_physname,
17273 &setdebuglist, &showdebuglist);
17274
96d19272 17275 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17276 _("\
fc1a9d6e 17277Save a gdb-index file.\n\
11570e71 17278Usage: save gdb-index DIRECTORY"),
96d19272
JK
17279 &save_cmdlist);
17280 set_cmd_completer (c, filename_completer);
6502dd73 17281}
This page took 3.225371 seconds and 4 git commands to generate.