Minor reformatting in ada-lang.c:assign_aggregate
[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. */
463 struct dwarf2_section_info *debug_type_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
CC
1340 bfd *abfd,
1341 int is_debug_type_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{
8b70b953
TT
1839 if (per_cu->debug_type_section)
1840 read_signatured_type_at_offset (per_cu->objfile,
1841 per_cu->debug_type_section,
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;
8b70b953 2003 type_sig->per_cu.debug_type_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
8b70b953
TT
2301 if (this_cu->debug_type_section)
2302 sec = this_cu->debug_type_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
CC
2312 abfd,
2313 this_cu->debug_type_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
2442static int
2443dw2_lookup_symtab (struct objfile *objfile, const char *name,
2444 const char *full_path, const char *real_path,
2445 struct symtab **result)
2446{
2447 int i;
c011a4f4
DE
2448 const char *name_basename = lbasename (name);
2449 int check_basename = name_basename == name;
9291a0cd
TT
2450 struct dwarf2_per_cu_data *base_cu = NULL;
2451
2452 dw2_setup (objfile);
ae2de4f8 2453
1fd400ff
TT
2454 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2455 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2456 {
2457 int j;
e254ef6a 2458 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2459 struct quick_file_names *file_data;
9291a0cd 2460
e254ef6a 2461 if (per_cu->v.quick->symtab)
9291a0cd
TT
2462 continue;
2463
7b9f3c50
DE
2464 file_data = dw2_get_file_names (objfile, per_cu);
2465 if (file_data == NULL)
9291a0cd
TT
2466 continue;
2467
7b9f3c50 2468 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2469 {
7b9f3c50 2470 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2471
2472 if (FILENAME_CMP (name, this_name) == 0)
2473 {
e254ef6a 2474 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2475 return 1;
2476 }
2477
2478 if (check_basename && ! base_cu
2479 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2480 base_cu = per_cu;
9291a0cd 2481
c011a4f4
DE
2482 /* Before we invoke realpath, which can get expensive when many
2483 files are involved, do a quick comparison of the basenames. */
2484 if (! basenames_may_differ
2485 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2486 continue;
2487
9291a0cd
TT
2488 if (full_path != NULL)
2489 {
7b9f3c50
DE
2490 const char *this_real_name = dw2_get_real_path (objfile,
2491 file_data, j);
9291a0cd 2492
7b9f3c50
DE
2493 if (this_real_name != NULL
2494 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2495 {
e254ef6a 2496 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2497 return 1;
2498 }
2499 }
2500
2501 if (real_path != NULL)
2502 {
7b9f3c50
DE
2503 const char *this_real_name = dw2_get_real_path (objfile,
2504 file_data, j);
9291a0cd 2505
7b9f3c50
DE
2506 if (this_real_name != NULL
2507 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2508 {
74dd2ca6
DE
2509 *result = dw2_instantiate_symtab (objfile, per_cu);
2510 return 1;
9291a0cd
TT
2511 }
2512 }
2513 }
2514 }
2515
2516 if (base_cu)
2517 {
2518 *result = dw2_instantiate_symtab (objfile, base_cu);
2519 return 1;
2520 }
2521
2522 return 0;
2523}
2524
2525static struct symtab *
2526dw2_lookup_symbol (struct objfile *objfile, int block_index,
2527 const char *name, domain_enum domain)
2528{
774b6a14 2529 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2530 instead. */
2531 return NULL;
2532}
2533
2534/* A helper function that expands all symtabs that hold an object
2535 named NAME. */
2fdf6df6 2536
9291a0cd
TT
2537static void
2538dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2539{
2540 dw2_setup (objfile);
2541
ae2de4f8 2542 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2543 if (dwarf2_per_objfile->index_table)
2544 {
2545 offset_type *vec;
2546
2547 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2548 name, &vec))
2549 {
2550 offset_type i, len = MAYBE_SWAP (*vec);
2551 for (i = 0; i < len; ++i)
2552 {
2553 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2554 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2555
e254ef6a 2556 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2557 }
2558 }
2559 }
2560}
2561
774b6a14
TT
2562static void
2563dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2564 enum block_enum block_kind, const char *name,
774b6a14 2565 domain_enum domain)
9291a0cd 2566{
774b6a14 2567 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2568}
2569
2570static void
2571dw2_print_stats (struct objfile *objfile)
2572{
2573 int i, count;
2574
2575 dw2_setup (objfile);
2576 count = 0;
1fd400ff
TT
2577 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2578 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2579 {
e254ef6a 2580 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2581
e254ef6a 2582 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2583 ++count;
2584 }
2585 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2586}
2587
2588static void
2589dw2_dump (struct objfile *objfile)
2590{
2591 /* Nothing worth printing. */
2592}
2593
2594static void
2595dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2596 struct section_offsets *delta)
2597{
2598 /* There's nothing to relocate here. */
2599}
2600
2601static void
2602dw2_expand_symtabs_for_function (struct objfile *objfile,
2603 const char *func_name)
2604{
2605 dw2_do_expand_symtabs_matching (objfile, func_name);
2606}
2607
2608static void
2609dw2_expand_all_symtabs (struct objfile *objfile)
2610{
2611 int i;
2612
2613 dw2_setup (objfile);
1fd400ff
TT
2614
2615 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2616 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2617 {
e254ef6a 2618 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2619
e254ef6a 2620 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2621 }
2622}
2623
2624static void
2625dw2_expand_symtabs_with_filename (struct objfile *objfile,
2626 const char *filename)
2627{
2628 int i;
2629
2630 dw2_setup (objfile);
d4637a04
DE
2631
2632 /* We don't need to consider type units here.
2633 This is only called for examining code, e.g. expand_line_sal.
2634 There can be an order of magnitude (or more) more type units
2635 than comp units, and we avoid them if we can. */
2636
2637 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2638 {
2639 int j;
e254ef6a 2640 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2641 struct quick_file_names *file_data;
9291a0cd 2642
e254ef6a 2643 if (per_cu->v.quick->symtab)
9291a0cd
TT
2644 continue;
2645
7b9f3c50
DE
2646 file_data = dw2_get_file_names (objfile, per_cu);
2647 if (file_data == NULL)
9291a0cd
TT
2648 continue;
2649
7b9f3c50 2650 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2651 {
7b9f3c50 2652 const char *this_name = file_data->file_names[j];
1ef75ecc 2653 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2654 {
e254ef6a 2655 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2656 break;
2657 }
2658 }
2659 }
2660}
2661
dd786858 2662static const char *
9291a0cd
TT
2663dw2_find_symbol_file (struct objfile *objfile, const char *name)
2664{
e254ef6a 2665 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2666 offset_type *vec;
7b9f3c50 2667 struct quick_file_names *file_data;
9291a0cd
TT
2668
2669 dw2_setup (objfile);
2670
ae2de4f8 2671 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2672 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2673 {
2674 struct symtab *s;
2675
2676 ALL_OBJFILE_SYMTABS (objfile, s)
2677 if (s->primary)
2678 {
2679 struct blockvector *bv = BLOCKVECTOR (s);
2680 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2681 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2682
2683 if (sym)
2684 return sym->symtab->filename;
2685 }
2686 return NULL;
2687 }
9291a0cd
TT
2688
2689 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2690 name, &vec))
2691 return NULL;
2692
2693 /* Note that this just looks at the very first one named NAME -- but
2694 actually we are looking for a function. find_main_filename
2695 should be rewritten so that it doesn't require a custom hook. It
2696 could just use the ordinary symbol tables. */
2697 /* vec[0] is the length, which must always be >0. */
e254ef6a 2698 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2699
7b9f3c50
DE
2700 file_data = dw2_get_file_names (objfile, per_cu);
2701 if (file_data == NULL)
9291a0cd
TT
2702 return NULL;
2703
7b9f3c50 2704 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2705}
2706
2707static void
40658b94
PH
2708dw2_map_matching_symbols (const char * name, domain_enum namespace,
2709 struct objfile *objfile, int global,
2710 int (*callback) (struct block *,
2711 struct symbol *, void *),
2edb89d3
JK
2712 void *data, symbol_compare_ftype *match,
2713 symbol_compare_ftype *ordered_compare)
9291a0cd 2714{
40658b94 2715 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2716 current language is Ada for a non-Ada objfile using GNU index. As Ada
2717 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2718}
2719
2720static void
2721dw2_expand_symtabs_matching (struct objfile *objfile,
2722 int (*file_matcher) (const char *, void *),
2723 int (*name_matcher) (const char *, void *),
8903c50d 2724 enum search_domain kind,
9291a0cd
TT
2725 void *data)
2726{
2727 int i;
2728 offset_type iter;
4b5246aa 2729 struct mapped_index *index;
9291a0cd
TT
2730
2731 dw2_setup (objfile);
ae2de4f8
DE
2732
2733 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2734 if (!dwarf2_per_objfile->index_table)
2735 return;
4b5246aa 2736 index = dwarf2_per_objfile->index_table;
9291a0cd 2737
7b08b9eb
JK
2738 if (file_matcher != NULL)
2739 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2740 + dwarf2_per_objfile->n_type_comp_units); ++i)
2741 {
2742 int j;
2743 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2744 struct quick_file_names *file_data;
2745
2746 per_cu->v.quick->mark = 0;
2747 if (per_cu->v.quick->symtab)
2748 continue;
2749
2750 file_data = dw2_get_file_names (objfile, per_cu);
2751 if (file_data == NULL)
2752 continue;
2753
2754 for (j = 0; j < file_data->num_file_names; ++j)
2755 {
2756 if (file_matcher (file_data->file_names[j], data))
2757 {
2758 per_cu->v.quick->mark = 1;
2759 break;
2760 }
2761 }
2762 }
9291a0cd 2763
3876f04e 2764 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2765 {
2766 offset_type idx = 2 * iter;
2767 const char *name;
2768 offset_type *vec, vec_len, vec_idx;
2769
3876f04e 2770 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2771 continue;
2772
3876f04e 2773 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2774
2775 if (! (*name_matcher) (name, data))
2776 continue;
2777
2778 /* The name was matched, now expand corresponding CUs that were
2779 marked. */
4b5246aa 2780 vec = (offset_type *) (index->constant_pool
3876f04e 2781 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2782 vec_len = MAYBE_SWAP (vec[0]);
2783 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2784 {
e254ef6a 2785 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2786
e254ef6a 2787 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2788 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2789 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2790 }
2791 }
2792}
2793
2794static struct symtab *
2795dw2_find_pc_sect_symtab (struct objfile *objfile,
2796 struct minimal_symbol *msymbol,
2797 CORE_ADDR pc,
2798 struct obj_section *section,
2799 int warn_if_readin)
2800{
2801 struct dwarf2_per_cu_data *data;
2802
2803 dw2_setup (objfile);
2804
2805 if (!objfile->psymtabs_addrmap)
2806 return NULL;
2807
2808 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2809 if (!data)
2810 return NULL;
2811
2812 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2813 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2814 paddress (get_objfile_arch (objfile), pc));
2815
2816 return dw2_instantiate_symtab (objfile, data);
2817}
2818
9291a0cd 2819static void
44b13c5a 2820dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2821 void *data, int need_fullname)
9291a0cd
TT
2822{
2823 int i;
2824
2825 dw2_setup (objfile);
ae2de4f8 2826
1fd400ff
TT
2827 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2828 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2829 {
2830 int j;
e254ef6a 2831 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2832 struct quick_file_names *file_data;
9291a0cd 2833
e254ef6a 2834 if (per_cu->v.quick->symtab)
9291a0cd
TT
2835 continue;
2836
7b9f3c50
DE
2837 file_data = dw2_get_file_names (objfile, per_cu);
2838 if (file_data == NULL)
9291a0cd
TT
2839 continue;
2840
7b9f3c50 2841 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2842 {
74e2f255
DE
2843 const char *this_real_name;
2844
2845 if (need_fullname)
2846 this_real_name = dw2_get_real_path (objfile, file_data, j);
2847 else
2848 this_real_name = NULL;
7b9f3c50 2849 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2850 }
2851 }
2852}
2853
2854static int
2855dw2_has_symbols (struct objfile *objfile)
2856{
2857 return 1;
2858}
2859
2860const struct quick_symbol_functions dwarf2_gdb_index_functions =
2861{
2862 dw2_has_symbols,
2863 dw2_find_last_source_symtab,
2864 dw2_forget_cached_source_info,
2865 dw2_lookup_symtab,
2866 dw2_lookup_symbol,
774b6a14 2867 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2868 dw2_print_stats,
2869 dw2_dump,
2870 dw2_relocate,
2871 dw2_expand_symtabs_for_function,
2872 dw2_expand_all_symtabs,
2873 dw2_expand_symtabs_with_filename,
2874 dw2_find_symbol_file,
40658b94 2875 dw2_map_matching_symbols,
9291a0cd
TT
2876 dw2_expand_symtabs_matching,
2877 dw2_find_pc_sect_symtab,
9291a0cd
TT
2878 dw2_map_symbol_filenames
2879};
2880
2881/* Initialize for reading DWARF for this objfile. Return 0 if this
2882 file will use psymtabs, or 1 if using the GNU index. */
2883
2884int
2885dwarf2_initialize_objfile (struct objfile *objfile)
2886{
2887 /* If we're about to read full symbols, don't bother with the
2888 indices. In this case we also don't care if some other debug
2889 format is making psymtabs, because they are all about to be
2890 expanded anyway. */
2891 if ((objfile->flags & OBJF_READNOW))
2892 {
2893 int i;
2894
2895 dwarf2_per_objfile->using_index = 1;
2896 create_all_comp_units (objfile);
1fd400ff 2897 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2898 dwarf2_per_objfile->quick_file_names_table =
2899 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2900
1fd400ff
TT
2901 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2902 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2903 {
e254ef6a 2904 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2905
e254ef6a
DE
2906 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2907 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2908 }
2909
2910 /* Return 1 so that gdb sees the "quick" functions. However,
2911 these functions will be no-ops because we will have expanded
2912 all symtabs. */
2913 return 1;
2914 }
2915
2916 if (dwarf2_read_index (objfile))
2917 return 1;
2918
9291a0cd
TT
2919 return 0;
2920}
2921
2922\f
2923
dce234bc
PP
2924/* Build a partial symbol table. */
2925
2926void
f29dff0a 2927dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2928{
f29dff0a 2929 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2930 {
2931 init_psymbol_list (objfile, 1024);
2932 }
2933
d146bf1e 2934 dwarf2_build_psymtabs_hard (objfile);
c906108c 2935}
c906108c 2936
45452591
DE
2937/* Return TRUE if OFFSET is within CU_HEADER. */
2938
2939static inline int
2940offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2941{
2942 unsigned int bottom = cu_header->offset;
2943 unsigned int top = (cu_header->offset
2944 + cu_header->length
2945 + cu_header->initial_length_size);
9a619af0 2946
45452591
DE
2947 return (offset >= bottom && offset < top);
2948}
2949
93311388
DE
2950/* Read in the comp unit header information from the debug_info at info_ptr.
2951 NOTE: This leaves members offset, first_die_offset to be filled in
2952 by the caller. */
107d2387 2953
fe1b8b76 2954static gdb_byte *
107d2387 2955read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2956 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2957{
2958 int signed_addr;
891d2f0b 2959 unsigned int bytes_read;
c764a876
DE
2960
2961 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2962 cu_header->initial_length_size = bytes_read;
2963 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2964 info_ptr += bytes_read;
107d2387
AC
2965 cu_header->version = read_2_bytes (abfd, info_ptr);
2966 info_ptr += 2;
613e1657 2967 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2968 &bytes_read);
613e1657 2969 info_ptr += bytes_read;
107d2387
AC
2970 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2971 info_ptr += 1;
2972 signed_addr = bfd_get_sign_extend_vma (abfd);
2973 if (signed_addr < 0)
8e65ff28 2974 internal_error (__FILE__, __LINE__,
e2e0b3e5 2975 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2976 cu_header->signed_addr_p = signed_addr;
c764a876 2977
107d2387
AC
2978 return info_ptr;
2979}
2980
adabb602
DE
2981/* Read in a CU header and perform some basic error checking. */
2982
fe1b8b76
JB
2983static gdb_byte *
2984partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2985 gdb_byte *buffer, unsigned int buffer_size,
460c1c54 2986 bfd *abfd, int is_debug_type_section)
72bf9492 2987{
fe1b8b76 2988 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 2989
adabb602
DE
2990 header->offset = beg_of_comp_unit - buffer;
2991
72bf9492
DJ
2992 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2993
460c1c54
CC
2994 /* If we're reading a type unit, skip over the signature and
2995 type_offset fields. */
2996 if (is_debug_type_section)
2997 info_ptr += 8 /*signature*/ + header->offset_size;
2998
adabb602
DE
2999 header->first_die_offset = info_ptr - beg_of_comp_unit;
3000
2dc7f7b3 3001 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 3002 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
3003 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3004 bfd_get_filename (abfd));
72bf9492 3005
9e0ac564
TT
3006 if (header->abbrev_offset
3007 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3008 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
3009 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3010 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 3011 (long) header->abbrev_offset,
93311388 3012 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
3013 bfd_get_filename (abfd));
3014
3015 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 3016 > buffer + buffer_size)
8a3fe4f8
AC
3017 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3018 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 3019 (long) header->length,
93311388 3020 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
3021 bfd_get_filename (abfd));
3022
3023 return info_ptr;
3024}
3025
348e048f
DE
3026/* Read in the types comp unit header information from .debug_types entry at
3027 types_ptr. The result is a pointer to one past the end of the header. */
3028
3029static gdb_byte *
3030read_type_comp_unit_head (struct comp_unit_head *cu_header,
8b70b953 3031 struct dwarf2_section_info *section,
348e048f
DE
3032 ULONGEST *signature,
3033 gdb_byte *types_ptr, bfd *abfd)
3034{
348e048f
DE
3035 gdb_byte *initial_types_ptr = types_ptr;
3036
8b70b953
TT
3037 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3038 cu_header->offset = types_ptr - section->buffer;
348e048f
DE
3039
3040 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3041
3042 *signature = read_8_bytes (abfd, types_ptr);
3043 types_ptr += 8;
3044 types_ptr += cu_header->offset_size;
3045 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3046
3047 return types_ptr;
3048}
3049
aaa75496
JB
3050/* Allocate a new partial symtab for file named NAME and mark this new
3051 partial symtab as being an include of PST. */
3052
3053static void
3054dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3055 struct objfile *objfile)
3056{
3057 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3058
3059 subpst->section_offsets = pst->section_offsets;
3060 subpst->textlow = 0;
3061 subpst->texthigh = 0;
3062
3063 subpst->dependencies = (struct partial_symtab **)
3064 obstack_alloc (&objfile->objfile_obstack,
3065 sizeof (struct partial_symtab *));
3066 subpst->dependencies[0] = pst;
3067 subpst->number_of_dependencies = 1;
3068
3069 subpst->globals_offset = 0;
3070 subpst->n_global_syms = 0;
3071 subpst->statics_offset = 0;
3072 subpst->n_static_syms = 0;
3073 subpst->symtab = NULL;
3074 subpst->read_symtab = pst->read_symtab;
3075 subpst->readin = 0;
3076
3077 /* No private part is necessary for include psymtabs. This property
3078 can be used to differentiate between such include psymtabs and
10b3939b 3079 the regular ones. */
58a9656e 3080 subpst->read_symtab_private = NULL;
aaa75496
JB
3081}
3082
3083/* Read the Line Number Program data and extract the list of files
3084 included by the source file represented by PST. Build an include
d85a05f0 3085 partial symtab for each of these included files. */
aaa75496
JB
3086
3087static void
3088dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3089 struct die_info *die,
aaa75496
JB
3090 struct partial_symtab *pst)
3091{
3092 struct objfile *objfile = cu->objfile;
3093 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3094 struct line_header *lh = NULL;
3095 struct attribute *attr;
aaa75496 3096
d85a05f0
DJ
3097 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3098 if (attr)
3099 {
3100 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3101
d85a05f0
DJ
3102 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3103 }
aaa75496
JB
3104 if (lh == NULL)
3105 return; /* No linetable, so no includes. */
3106
c6da4cef
DE
3107 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3108 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
3109
3110 free_line_header (lh);
3111}
3112
348e048f
DE
3113static hashval_t
3114hash_type_signature (const void *item)
3115{
3116 const struct signatured_type *type_sig = item;
9a619af0 3117
348e048f
DE
3118 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3119 return type_sig->signature;
3120}
3121
3122static int
3123eq_type_signature (const void *item_lhs, const void *item_rhs)
3124{
3125 const struct signatured_type *lhs = item_lhs;
3126 const struct signatured_type *rhs = item_rhs;
9a619af0 3127
348e048f
DE
3128 return lhs->signature == rhs->signature;
3129}
3130
1fd400ff
TT
3131/* Allocate a hash table for signatured types. */
3132
3133static htab_t
673bfd45 3134allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3135{
3136 return htab_create_alloc_ex (41,
3137 hash_type_signature,
3138 eq_type_signature,
3139 NULL,
3140 &objfile->objfile_obstack,
3141 hashtab_obstack_allocate,
3142 dummy_obstack_deallocate);
3143}
3144
3145/* A helper function to add a signatured type CU to a list. */
3146
3147static int
3148add_signatured_type_cu_to_list (void **slot, void *datum)
3149{
3150 struct signatured_type *sigt = *slot;
3151 struct dwarf2_per_cu_data ***datap = datum;
3152
3153 **datap = &sigt->per_cu;
3154 ++*datap;
3155
3156 return 1;
3157}
3158
348e048f
DE
3159/* Create the hash table of all entries in the .debug_types section.
3160 The result is zero if there is an error (e.g. missing .debug_types section),
3161 otherwise non-zero. */
3162
3163static int
3164create_debug_types_hash_table (struct objfile *objfile)
3165{
8b70b953 3166 htab_t types_htab = NULL;
1fd400ff 3167 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3168 int ix;
3169 struct dwarf2_section_info *section;
348e048f 3170
8b70b953 3171 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3172 {
3173 dwarf2_per_objfile->signatured_types = NULL;
3174 return 0;
3175 }
3176
8b70b953
TT
3177 for (ix = 0;
3178 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3179 ix, section);
3180 ++ix)
3181 {
3182 gdb_byte *info_ptr, *end_ptr;
348e048f 3183
8b70b953
TT
3184 dwarf2_read_section (objfile, section);
3185 info_ptr = section->buffer;
348e048f 3186
8b70b953
TT
3187 if (info_ptr == NULL)
3188 continue;
348e048f 3189
8b70b953
TT
3190 if (types_htab == NULL)
3191 types_htab = allocate_signatured_type_table (objfile);
348e048f 3192
8b70b953
TT
3193 if (dwarf2_die_debug)
3194 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3195
3196 end_ptr = info_ptr + section->size;
3197 while (info_ptr < end_ptr)
3198 {
3199 unsigned int offset;
3200 unsigned int offset_size;
3201 unsigned int type_offset;
3202 unsigned int length, initial_length_size;
3203 unsigned short version;
3204 ULONGEST signature;
3205 struct signatured_type *type_sig;
3206 void **slot;
3207 gdb_byte *ptr = info_ptr;
348e048f 3208
8b70b953 3209 offset = ptr - section->buffer;
348e048f 3210
8b70b953
TT
3211 /* We need to read the type's signature in order to build the hash
3212 table, but we don't need to read anything else just yet. */
348e048f 3213
8b70b953
TT
3214 /* Sanity check to ensure entire cu is present. */
3215 length = read_initial_length (objfile->obfd, ptr,
3216 &initial_length_size);
3217 if (ptr + length + initial_length_size > end_ptr)
3218 {
3219 complaint (&symfile_complaints,
3220 _("debug type entry runs off end "
3221 "of `.debug_types' section, ignored"));
3222 break;
3223 }
348e048f 3224
8b70b953
TT
3225 offset_size = initial_length_size == 4 ? 4 : 8;
3226 ptr += initial_length_size;
3227 version = bfd_get_16 (objfile->obfd, ptr);
3228 ptr += 2;
3229 ptr += offset_size; /* abbrev offset */
3230 ptr += 1; /* address size */
3231 signature = bfd_get_64 (objfile->obfd, ptr);
3232 ptr += 8;
3233 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
4743b735 3234 ptr += offset_size;
6caca83c
CC
3235
3236 /* Skip dummy type units. */
3237 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3238 {
3239 info_ptr = info_ptr + initial_length_size + length;
3240 continue;
3241 }
8b70b953
TT
3242
3243 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3244 memset (type_sig, 0, sizeof (*type_sig));
3245 type_sig->signature = signature;
3246 type_sig->type_offset = type_offset;
3247 type_sig->per_cu.objfile = objfile;
3248 type_sig->per_cu.debug_type_section = section;
3249 type_sig->per_cu.offset = offset;
3250
3251 slot = htab_find_slot (types_htab, type_sig, INSERT);
3252 gdb_assert (slot != NULL);
3253 if (*slot != NULL)
3254 {
3255 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3256
8b70b953
TT
3257 complaint (&symfile_complaints,
3258 _("debug type entry at offset 0x%x is duplicate to the "
3259 "entry at offset 0x%x, signature 0x%s"),
3260 offset, dup_sig->per_cu.offset,
3261 phex (signature, sizeof (signature)));
3262 gdb_assert (signature == dup_sig->signature);
3263 }
3264 *slot = type_sig;
348e048f 3265
8b70b953
TT
3266 if (dwarf2_die_debug)
3267 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3268 offset, phex (signature, sizeof (signature)));
348e048f 3269
8b70b953
TT
3270 info_ptr = info_ptr + initial_length_size + length;
3271 }
348e048f
DE
3272 }
3273
3274 dwarf2_per_objfile->signatured_types = types_htab;
3275
1fd400ff
TT
3276 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3277 dwarf2_per_objfile->type_comp_units
3278 = obstack_alloc (&objfile->objfile_obstack,
3279 dwarf2_per_objfile->n_type_comp_units
3280 * sizeof (struct dwarf2_per_cu_data *));
3281 iter = &dwarf2_per_objfile->type_comp_units[0];
3282 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3283 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3284 == dwarf2_per_objfile->n_type_comp_units);
3285
348e048f
DE
3286 return 1;
3287}
3288
3289/* Lookup a signature based type.
3290 Returns NULL if SIG is not present in the table. */
3291
3292static struct signatured_type *
3293lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3294{
3295 struct signatured_type find_entry, *entry;
3296
3297 if (dwarf2_per_objfile->signatured_types == NULL)
3298 {
3299 complaint (&symfile_complaints,
55f1336d 3300 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3301 return 0;
3302 }
3303
3304 find_entry.signature = sig;
3305 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3306 return entry;
3307}
3308
d85a05f0
DJ
3309/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3310
3311static void
3312init_cu_die_reader (struct die_reader_specs *reader,
3313 struct dwarf2_cu *cu)
3314{
3315 reader->abfd = cu->objfile->obfd;
3316 reader->cu = cu;
8b70b953 3317 if (cu->per_cu->debug_type_section)
be391dca 3318 {
8b70b953
TT
3319 gdb_assert (cu->per_cu->debug_type_section->readin);
3320 reader->buffer = cu->per_cu->debug_type_section->buffer;
be391dca 3321 }
d85a05f0 3322 else
be391dca
TT
3323 {
3324 gdb_assert (dwarf2_per_objfile->info.readin);
3325 reader->buffer = dwarf2_per_objfile->info.buffer;
3326 }
d85a05f0
DJ
3327}
3328
3329/* Find the base address of the compilation unit for range lists and
3330 location lists. It will normally be specified by DW_AT_low_pc.
3331 In DWARF-3 draft 4, the base address could be overridden by
3332 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3333 compilation units with discontinuous ranges. */
3334
3335static void
3336dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3337{
3338 struct attribute *attr;
3339
3340 cu->base_known = 0;
3341 cu->base_address = 0;
3342
3343 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3344 if (attr)
3345 {
3346 cu->base_address = DW_ADDR (attr);
3347 cu->base_known = 1;
3348 }
3349 else
3350 {
3351 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3352 if (attr)
3353 {
3354 cu->base_address = DW_ADDR (attr);
3355 cu->base_known = 1;
3356 }
3357 }
3358}
3359
348e048f
DE
3360/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3361 to combine the common parts.
93311388 3362 Process a compilation unit for a psymtab.
348e048f
DE
3363 BUFFER is a pointer to the beginning of the dwarf section buffer,
3364 either .debug_info or debug_types.
93311388
DE
3365 INFO_PTR is a pointer to the start of the CU.
3366 Returns a pointer to the next CU. */
aaa75496 3367
93311388
DE
3368static gdb_byte *
3369process_psymtab_comp_unit (struct objfile *objfile,
3370 struct dwarf2_per_cu_data *this_cu,
3371 gdb_byte *buffer, gdb_byte *info_ptr,
3372 unsigned int buffer_size)
c906108c 3373{
c906108c 3374 bfd *abfd = objfile->obfd;
93311388 3375 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3376 struct die_info *comp_unit_die;
c906108c 3377 struct partial_symtab *pst;
5734ee8b 3378 CORE_ADDR baseaddr;
93311388
DE
3379 struct cleanup *back_to_inner;
3380 struct dwarf2_cu cu;
d85a05f0
DJ
3381 int has_children, has_pc_info;
3382 struct attribute *attr;
d85a05f0
DJ
3383 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3384 struct die_reader_specs reader_specs;
3e2a0cee 3385 const char *filename;
c906108c 3386
9816fde3 3387 init_one_comp_unit (&cu, objfile);
93311388 3388 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3389
93311388
DE
3390 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3391 buffer, buffer_size,
460c1c54
CC
3392 abfd,
3393 this_cu->debug_type_section != NULL);
10b3939b 3394
6caca83c
CC
3395 /* Skip dummy compilation units. */
3396 if (info_ptr >= buffer + buffer_size
3397 || peek_abbrev_code (abfd, info_ptr) == 0)
3398 {
3399 info_ptr = (beg_of_comp_unit + cu.header.length
3400 + cu.header.initial_length_size);
3401 do_cleanups (back_to_inner);
3402 return info_ptr;
3403 }
3404
93311388 3405 cu.list_in_scope = &file_symbols;
af703f96 3406
328c9494
DJ
3407 /* If this compilation unit was already read in, free the
3408 cached copy in order to read it in again. This is
3409 necessary because we skipped some symbols when we first
3410 read in the compilation unit (see load_partial_dies).
3411 This problem could be avoided, but the benefit is
3412 unclear. */
3413 if (this_cu->cu != NULL)
3414 free_one_cached_comp_unit (this_cu->cu);
3415
3416 /* Note that this is a pointer to our stack frame, being
3417 added to a global data structure. It will be cleaned up
3418 in free_stack_comp_unit when we finish with this
3419 compilation unit. */
3420 this_cu->cu = &cu;
d85a05f0
DJ
3421 cu.per_cu = this_cu;
3422
93311388
DE
3423 /* Read the abbrevs for this compilation unit into a table. */
3424 dwarf2_read_abbrevs (abfd, &cu);
3425 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3426
93311388 3427 /* Read the compilation unit die. */
d85a05f0
DJ
3428 init_cu_die_reader (&reader_specs, &cu);
3429 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3430 &has_children);
93311388 3431
8b70b953 3432 if (this_cu->debug_type_section)
348e048f 3433 {
b3c8eb43
JK
3434 /* LENGTH has not been set yet for type units. */
3435 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3436 this_cu->length = cu.header.length + cu.header.initial_length_size;
3437 }
d85a05f0 3438 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3439 {
93311388
DE
3440 info_ptr = (beg_of_comp_unit + cu.header.length
3441 + cu.header.initial_length_size);
3442 do_cleanups (back_to_inner);
3443 return info_ptr;
3444 }
72bf9492 3445
9816fde3 3446 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3447
93311388 3448 /* Allocate a new partial symbol table structure. */
d85a05f0 3449 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3450 if (attr == NULL || !DW_STRING (attr))
3451 filename = "";
3452 else
3453 filename = DW_STRING (attr);
93311388 3454 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3455 filename,
93311388
DE
3456 /* TEXTLOW and TEXTHIGH are set below. */
3457 0,
3458 objfile->global_psymbols.next,
3459 objfile->static_psymbols.next);
72bf9492 3460
d85a05f0
DJ
3461 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3462 if (attr != NULL)
3463 pst->dirname = DW_STRING (attr);
72bf9492 3464
e38df1d0 3465 pst->read_symtab_private = this_cu;
72bf9492 3466
93311388 3467 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3468
0963b4bd 3469 /* Store the function that reads in the rest of the symbol table. */
93311388 3470 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3471
9291a0cd 3472 this_cu->v.psymtab = pst;
c906108c 3473
d85a05f0
DJ
3474 dwarf2_find_base_address (comp_unit_die, &cu);
3475
93311388
DE
3476 /* Possibly set the default values of LOWPC and HIGHPC from
3477 `DW_AT_ranges'. */
d85a05f0
DJ
3478 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3479 &best_highpc, &cu, pst);
3480 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3481 /* Store the contiguous range if it is not empty; it can be empty for
3482 CUs with no code. */
3483 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3484 best_lowpc + baseaddr,
3485 best_highpc + baseaddr - 1, pst);
93311388
DE
3486
3487 /* Check if comp unit has_children.
3488 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3489 If not, there's no more debug_info for this comp unit. */
d85a05f0 3490 if (has_children)
93311388
DE
3491 {
3492 struct partial_die_info *first_die;
3493 CORE_ADDR lowpc, highpc;
31ffec48 3494
93311388
DE
3495 lowpc = ((CORE_ADDR) -1);
3496 highpc = ((CORE_ADDR) 0);
c906108c 3497
93311388 3498 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3499
93311388 3500 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3501 ! has_pc_info, &cu);
57c22c6c 3502
93311388
DE
3503 /* If we didn't find a lowpc, set it to highpc to avoid
3504 complaints from `maint check'. */
3505 if (lowpc == ((CORE_ADDR) -1))
3506 lowpc = highpc;
10b3939b 3507
93311388
DE
3508 /* If the compilation unit didn't have an explicit address range,
3509 then use the information extracted from its child dies. */
d85a05f0 3510 if (! has_pc_info)
93311388 3511 {
d85a05f0
DJ
3512 best_lowpc = lowpc;
3513 best_highpc = highpc;
93311388
DE
3514 }
3515 }
d85a05f0
DJ
3516 pst->textlow = best_lowpc + baseaddr;
3517 pst->texthigh = best_highpc + baseaddr;
c906108c 3518
93311388
DE
3519 pst->n_global_syms = objfile->global_psymbols.next -
3520 (objfile->global_psymbols.list + pst->globals_offset);
3521 pst->n_static_syms = objfile->static_psymbols.next -
3522 (objfile->static_psymbols.list + pst->statics_offset);
3523 sort_pst_symbols (pst);
c906108c 3524
93311388
DE
3525 info_ptr = (beg_of_comp_unit + cu.header.length
3526 + cu.header.initial_length_size);
ae038cb0 3527
8b70b953 3528 if (this_cu->debug_type_section)
348e048f
DE
3529 {
3530 /* It's not clear we want to do anything with stmt lists here.
3531 Waiting to see what gcc ultimately does. */
3532 }
d85a05f0 3533 else
93311388
DE
3534 {
3535 /* Get the list of files included in the current compilation unit,
3536 and build a psymtab for each of them. */
d85a05f0 3537 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3538 }
ae038cb0 3539
93311388 3540 do_cleanups (back_to_inner);
ae038cb0 3541
93311388
DE
3542 return info_ptr;
3543}
ff013f42 3544
348e048f
DE
3545/* Traversal function for htab_traverse_noresize.
3546 Process one .debug_types comp-unit. */
3547
3548static int
3549process_type_comp_unit (void **slot, void *info)
3550{
3551 struct signatured_type *entry = (struct signatured_type *) *slot;
3552 struct objfile *objfile = (struct objfile *) info;
3553 struct dwarf2_per_cu_data *this_cu;
3554
3555 this_cu = &entry->per_cu;
348e048f 3556
8b70b953 3557 gdb_assert (this_cu->debug_type_section->readin);
348e048f 3558 process_psymtab_comp_unit (objfile, this_cu,
8b70b953
TT
3559 this_cu->debug_type_section->buffer,
3560 (this_cu->debug_type_section->buffer
3561 + this_cu->offset),
3562 this_cu->debug_type_section->size);
348e048f
DE
3563
3564 return 1;
3565}
3566
3567/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3568 Build partial symbol tables for the .debug_types comp-units. */
3569
3570static void
3571build_type_psymtabs (struct objfile *objfile)
3572{
3573 if (! create_debug_types_hash_table (objfile))
3574 return;
3575
3576 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3577 process_type_comp_unit, objfile);
3578}
3579
60606b2c
TT
3580/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3581
3582static void
3583psymtabs_addrmap_cleanup (void *o)
3584{
3585 struct objfile *objfile = o;
ec61707d 3586
60606b2c
TT
3587 objfile->psymtabs_addrmap = NULL;
3588}
3589
93311388
DE
3590/* Build the partial symbol table by doing a quick pass through the
3591 .debug_info and .debug_abbrev sections. */
72bf9492 3592
93311388 3593static void
c67a9c90 3594dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3595{
93311388 3596 gdb_byte *info_ptr;
60606b2c
TT
3597 struct cleanup *back_to, *addrmap_cleanup;
3598 struct obstack temp_obstack;
93311388 3599
98bfdba5
PA
3600 dwarf2_per_objfile->reading_partial_symbols = 1;
3601
be391dca 3602 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3603 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3604
93311388
DE
3605 /* Any cached compilation units will be linked by the per-objfile
3606 read_in_chain. Make sure to free them when we're done. */
3607 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3608
348e048f
DE
3609 build_type_psymtabs (objfile);
3610
93311388 3611 create_all_comp_units (objfile);
c906108c 3612
60606b2c
TT
3613 /* Create a temporary address map on a temporary obstack. We later
3614 copy this to the final obstack. */
3615 obstack_init (&temp_obstack);
3616 make_cleanup_obstack_free (&temp_obstack);
3617 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3618 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3619
93311388
DE
3620 /* Since the objects we're extracting from .debug_info vary in
3621 length, only the individual functions to extract them (like
3622 read_comp_unit_head and load_partial_die) can really know whether
3623 the buffer is large enough to hold another complete object.
c906108c 3624
93311388
DE
3625 At the moment, they don't actually check that. If .debug_info
3626 holds just one extra byte after the last compilation unit's dies,
3627 then read_comp_unit_head will happily read off the end of the
3628 buffer. read_partial_die is similarly casual. Those functions
3629 should be fixed.
c906108c 3630
93311388
DE
3631 For this loop condition, simply checking whether there's any data
3632 left at all should be sufficient. */
c906108c 3633
93311388
DE
3634 while (info_ptr < (dwarf2_per_objfile->info.buffer
3635 + dwarf2_per_objfile->info.size))
3636 {
3637 struct dwarf2_per_cu_data *this_cu;
dd373385 3638
3e43a32a
MS
3639 this_cu = dwarf2_find_comp_unit (info_ptr
3640 - dwarf2_per_objfile->info.buffer,
93311388 3641 objfile);
aaa75496 3642
93311388
DE
3643 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3644 dwarf2_per_objfile->info.buffer,
3645 info_ptr,
3646 dwarf2_per_objfile->info.size);
c906108c 3647 }
ff013f42
JK
3648
3649 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3650 &objfile->objfile_obstack);
60606b2c 3651 discard_cleanups (addrmap_cleanup);
ff013f42 3652
ae038cb0
DJ
3653 do_cleanups (back_to);
3654}
3655
93311388 3656/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3657
3658static void
93311388
DE
3659load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3660 struct objfile *objfile)
ae038cb0
DJ
3661{
3662 bfd *abfd = objfile->obfd;
adabb602 3663 gdb_byte *info_ptr;
d85a05f0 3664 struct die_info *comp_unit_die;
ae038cb0 3665 struct dwarf2_cu *cu;
1d9ec526 3666 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3667 int has_children;
3668 struct die_reader_specs reader_specs;
98bfdba5 3669 int read_cu = 0;
ae038cb0 3670
8b70b953 3671 gdb_assert (! this_cu->debug_type_section);
348e048f 3672
be391dca 3673 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3674 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0 3675
98bfdba5
PA
3676 if (this_cu->cu == NULL)
3677 {
9816fde3
JK
3678 cu = xmalloc (sizeof (*cu));
3679 init_one_comp_unit (cu, objfile);
ae038cb0 3680
98bfdba5 3681 read_cu = 1;
ae038cb0 3682
98bfdba5
PA
3683 /* If an error occurs while loading, release our storage. */
3684 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3685
98bfdba5
PA
3686 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3687 dwarf2_per_objfile->info.buffer,
3688 dwarf2_per_objfile->info.size,
460c1c54 3689 abfd, 0);
ae038cb0 3690
6caca83c
CC
3691 /* Skip dummy compilation units. */
3692 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3693 + dwarf2_per_objfile->info.size)
3694 || peek_abbrev_code (abfd, info_ptr) == 0)
3695 {
3696 do_cleanups (free_cu_cleanup);
3697 return;
3698 }
3699
98bfdba5
PA
3700 /* Link this compilation unit into the compilation unit tree. */
3701 this_cu->cu = cu;
3702 cu->per_cu = this_cu;
98bfdba5
PA
3703
3704 /* Link this CU into read_in_chain. */
3705 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3706 dwarf2_per_objfile->read_in_chain = this_cu;
3707 }
3708 else
3709 {
3710 cu = this_cu->cu;
3711 info_ptr += cu->header.first_die_offset;
3712 }
ae038cb0
DJ
3713
3714 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3715 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3716 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3717 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3718
3719 /* Read the compilation unit die. */
d85a05f0
DJ
3720 init_cu_die_reader (&reader_specs, cu);
3721 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3722 &has_children);
ae038cb0 3723
9816fde3 3724 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3725
ae038cb0
DJ
3726 /* Check if comp unit has_children.
3727 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3728 If not, there's no more debug_info for this comp unit. */
d85a05f0 3729 if (has_children)
93311388 3730 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3731
98bfdba5
PA
3732 do_cleanups (free_abbrevs_cleanup);
3733
3734 if (read_cu)
3735 {
3736 /* We've successfully allocated this compilation unit. Let our
3737 caller clean it up when finished with it. */
3738 discard_cleanups (free_cu_cleanup);
3739 }
ae038cb0
DJ
3740}
3741
3742/* Create a list of all compilation units in OBJFILE. We do this only
3743 if an inter-comp-unit reference is found; presumably if there is one,
3744 there will be many, and one will occur early in the .debug_info section.
3745 So there's no point in building this list incrementally. */
3746
3747static void
3748create_all_comp_units (struct objfile *objfile)
3749{
3750 int n_allocated;
3751 int n_comp_units;
3752 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3753 gdb_byte *info_ptr;
3754
3755 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3756 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3757
3758 n_comp_units = 0;
3759 n_allocated = 10;
3760 all_comp_units = xmalloc (n_allocated
3761 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3762
3e43a32a
MS
3763 while (info_ptr < dwarf2_per_objfile->info.buffer
3764 + dwarf2_per_objfile->info.size)
ae038cb0 3765 {
c764a876 3766 unsigned int length, initial_length_size;
ae038cb0 3767 struct dwarf2_per_cu_data *this_cu;
c764a876 3768 unsigned int offset;
ae038cb0 3769
dce234bc 3770 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3771
3772 /* Read just enough information to find out where the next
3773 compilation unit is. */
c764a876
DE
3774 length = read_initial_length (objfile->obfd, info_ptr,
3775 &initial_length_size);
ae038cb0
DJ
3776
3777 /* Save the compilation unit for later lookup. */
3778 this_cu = obstack_alloc (&objfile->objfile_obstack,
3779 sizeof (struct dwarf2_per_cu_data));
3780 memset (this_cu, 0, sizeof (*this_cu));
3781 this_cu->offset = offset;
c764a876 3782 this_cu->length = length + initial_length_size;
9291a0cd 3783 this_cu->objfile = objfile;
ae038cb0
DJ
3784
3785 if (n_comp_units == n_allocated)
3786 {
3787 n_allocated *= 2;
3788 all_comp_units = xrealloc (all_comp_units,
3789 n_allocated
3790 * sizeof (struct dwarf2_per_cu_data *));
3791 }
3792 all_comp_units[n_comp_units++] = this_cu;
3793
3794 info_ptr = info_ptr + this_cu->length;
3795 }
3796
3797 dwarf2_per_objfile->all_comp_units
3798 = obstack_alloc (&objfile->objfile_obstack,
3799 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3800 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3801 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3802 xfree (all_comp_units);
3803 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3804}
3805
5734ee8b
DJ
3806/* Process all loaded DIEs for compilation unit CU, starting at
3807 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3808 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3809 DW_AT_ranges). If NEED_PC is set, then this function will set
3810 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3811 and record the covered ranges in the addrmap. */
c906108c 3812
72bf9492
DJ
3813static void
3814scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3815 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3816{
72bf9492 3817 struct partial_die_info *pdi;
c906108c 3818
91c24f0a
DC
3819 /* Now, march along the PDI's, descending into ones which have
3820 interesting children but skipping the children of the other ones,
3821 until we reach the end of the compilation unit. */
c906108c 3822
72bf9492 3823 pdi = first_die;
91c24f0a 3824
72bf9492
DJ
3825 while (pdi != NULL)
3826 {
3827 fixup_partial_die (pdi, cu);
c906108c 3828
f55ee35c 3829 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3830 children, so we need to look at them. Ditto for anonymous
3831 enums. */
933c6fe4 3832
72bf9492 3833 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3834 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3835 {
72bf9492 3836 switch (pdi->tag)
c906108c
SS
3837 {
3838 case DW_TAG_subprogram:
5734ee8b 3839 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3840 break;
72929c62 3841 case DW_TAG_constant:
c906108c
SS
3842 case DW_TAG_variable:
3843 case DW_TAG_typedef:
91c24f0a 3844 case DW_TAG_union_type:
72bf9492 3845 if (!pdi->is_declaration)
63d06c5c 3846 {
72bf9492 3847 add_partial_symbol (pdi, cu);
63d06c5c
DC
3848 }
3849 break;
c906108c 3850 case DW_TAG_class_type:
680b30c7 3851 case DW_TAG_interface_type:
c906108c 3852 case DW_TAG_structure_type:
72bf9492 3853 if (!pdi->is_declaration)
c906108c 3854 {
72bf9492 3855 add_partial_symbol (pdi, cu);
c906108c
SS
3856 }
3857 break;
91c24f0a 3858 case DW_TAG_enumeration_type:
72bf9492
DJ
3859 if (!pdi->is_declaration)
3860 add_partial_enumeration (pdi, cu);
c906108c
SS
3861 break;
3862 case DW_TAG_base_type:
a02abb62 3863 case DW_TAG_subrange_type:
c906108c 3864 /* File scope base type definitions are added to the partial
c5aa993b 3865 symbol table. */
72bf9492 3866 add_partial_symbol (pdi, cu);
c906108c 3867 break;
d9fa45fe 3868 case DW_TAG_namespace:
5734ee8b 3869 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3870 break;
5d7cb8df
JK
3871 case DW_TAG_module:
3872 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3873 break;
c906108c
SS
3874 default:
3875 break;
3876 }
3877 }
3878
72bf9492
DJ
3879 /* If the die has a sibling, skip to the sibling. */
3880
3881 pdi = pdi->die_sibling;
3882 }
3883}
3884
3885/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3886
72bf9492 3887 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3888 name is concatenated with "::" and the partial DIE's name. For
3889 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3890 Enumerators are an exception; they use the scope of their parent
3891 enumeration type, i.e. the name of the enumeration type is not
3892 prepended to the enumerator.
91c24f0a 3893
72bf9492
DJ
3894 There are two complexities. One is DW_AT_specification; in this
3895 case "parent" means the parent of the target of the specification,
3896 instead of the direct parent of the DIE. The other is compilers
3897 which do not emit DW_TAG_namespace; in this case we try to guess
3898 the fully qualified name of structure types from their members'
3899 linkage names. This must be done using the DIE's children rather
3900 than the children of any DW_AT_specification target. We only need
3901 to do this for structures at the top level, i.e. if the target of
3902 any DW_AT_specification (if any; otherwise the DIE itself) does not
3903 have a parent. */
3904
3905/* Compute the scope prefix associated with PDI's parent, in
3906 compilation unit CU. The result will be allocated on CU's
3907 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3908 field. NULL is returned if no prefix is necessary. */
3909static char *
3910partial_die_parent_scope (struct partial_die_info *pdi,
3911 struct dwarf2_cu *cu)
3912{
3913 char *grandparent_scope;
3914 struct partial_die_info *parent, *real_pdi;
91c24f0a 3915
72bf9492
DJ
3916 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3917 then this means the parent of the specification DIE. */
3918
3919 real_pdi = pdi;
72bf9492 3920 while (real_pdi->has_specification)
10b3939b 3921 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3922
3923 parent = real_pdi->die_parent;
3924 if (parent == NULL)
3925 return NULL;
3926
3927 if (parent->scope_set)
3928 return parent->scope;
3929
3930 fixup_partial_die (parent, cu);
3931
10b3939b 3932 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3933
acebe513
UW
3934 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3935 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3936 Work around this problem here. */
3937 if (cu->language == language_cplus
6e70227d 3938 && parent->tag == DW_TAG_namespace
acebe513
UW
3939 && strcmp (parent->name, "::") == 0
3940 && grandparent_scope == NULL)
3941 {
3942 parent->scope = NULL;
3943 parent->scope_set = 1;
3944 return NULL;
3945 }
3946
9c6c53f7
SA
3947 if (pdi->tag == DW_TAG_enumerator)
3948 /* Enumerators should not get the name of the enumeration as a prefix. */
3949 parent->scope = grandparent_scope;
3950 else if (parent->tag == DW_TAG_namespace
f55ee35c 3951 || parent->tag == DW_TAG_module
72bf9492
DJ
3952 || parent->tag == DW_TAG_structure_type
3953 || parent->tag == DW_TAG_class_type
680b30c7 3954 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3955 || parent->tag == DW_TAG_union_type
3956 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3957 {
3958 if (grandparent_scope == NULL)
3959 parent->scope = parent->name;
3960 else
3e43a32a
MS
3961 parent->scope = typename_concat (&cu->comp_unit_obstack,
3962 grandparent_scope,
f55ee35c 3963 parent->name, 0, cu);
72bf9492 3964 }
72bf9492
DJ
3965 else
3966 {
3967 /* FIXME drow/2004-04-01: What should we be doing with
3968 function-local names? For partial symbols, we should probably be
3969 ignoring them. */
3970 complaint (&symfile_complaints,
e2e0b3e5 3971 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3972 parent->tag, pdi->offset);
3973 parent->scope = grandparent_scope;
c906108c
SS
3974 }
3975
72bf9492
DJ
3976 parent->scope_set = 1;
3977 return parent->scope;
3978}
3979
3980/* Return the fully scoped name associated with PDI, from compilation unit
3981 CU. The result will be allocated with malloc. */
3982static char *
3983partial_die_full_name (struct partial_die_info *pdi,
3984 struct dwarf2_cu *cu)
3985{
3986 char *parent_scope;
3987
98bfdba5
PA
3988 /* If this is a template instantiation, we can not work out the
3989 template arguments from partial DIEs. So, unfortunately, we have
3990 to go through the full DIEs. At least any work we do building
3991 types here will be reused if full symbols are loaded later. */
3992 if (pdi->has_template_arguments)
3993 {
3994 fixup_partial_die (pdi, cu);
3995
3996 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3997 {
3998 struct die_info *die;
3999 struct attribute attr;
4000 struct dwarf2_cu *ref_cu = cu;
4001
4002 attr.name = 0;
4003 attr.form = DW_FORM_ref_addr;
4004 attr.u.addr = pdi->offset;
4005 die = follow_die_ref (NULL, &attr, &ref_cu);
4006
4007 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4008 }
4009 }
4010
72bf9492
DJ
4011 parent_scope = partial_die_parent_scope (pdi, cu);
4012 if (parent_scope == NULL)
4013 return NULL;
4014 else
f55ee35c 4015 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4016}
4017
4018static void
72bf9492 4019add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4020{
e7c27a73 4021 struct objfile *objfile = cu->objfile;
c906108c 4022 CORE_ADDR addr = 0;
decbce07 4023 char *actual_name = NULL;
5c4e30ca 4024 const struct partial_symbol *psym = NULL;
e142c38c 4025 CORE_ADDR baseaddr;
72bf9492 4026 int built_actual_name = 0;
e142c38c
DJ
4027
4028 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4029
94af9270
KS
4030 actual_name = partial_die_full_name (pdi, cu);
4031 if (actual_name)
4032 built_actual_name = 1;
63d06c5c 4033
72bf9492
DJ
4034 if (actual_name == NULL)
4035 actual_name = pdi->name;
4036
c906108c
SS
4037 switch (pdi->tag)
4038 {
4039 case DW_TAG_subprogram:
2cfa0c8d 4040 if (pdi->is_external || cu->language == language_ada)
c906108c 4041 {
2cfa0c8d
JB
4042 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4043 of the global scope. But in Ada, we want to be able to access
4044 nested procedures globally. So all Ada subprograms are stored
4045 in the global scope. */
f47fb265 4046 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4047 mst_text, objfile); */
f47fb265
MS
4048 add_psymbol_to_list (actual_name, strlen (actual_name),
4049 built_actual_name,
4050 VAR_DOMAIN, LOC_BLOCK,
4051 &objfile->global_psymbols,
4052 0, pdi->lowpc + baseaddr,
4053 cu->language, objfile);
c906108c
SS
4054 }
4055 else
4056 {
f47fb265 4057 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4058 mst_file_text, objfile); */
f47fb265
MS
4059 add_psymbol_to_list (actual_name, strlen (actual_name),
4060 built_actual_name,
4061 VAR_DOMAIN, LOC_BLOCK,
4062 &objfile->static_psymbols,
4063 0, pdi->lowpc + baseaddr,
4064 cu->language, objfile);
c906108c
SS
4065 }
4066 break;
72929c62
JB
4067 case DW_TAG_constant:
4068 {
4069 struct psymbol_allocation_list *list;
4070
4071 if (pdi->is_external)
4072 list = &objfile->global_psymbols;
4073 else
4074 list = &objfile->static_psymbols;
f47fb265
MS
4075 add_psymbol_to_list (actual_name, strlen (actual_name),
4076 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4077 list, 0, 0, cu->language, objfile);
72929c62
JB
4078 }
4079 break;
c906108c 4080 case DW_TAG_variable:
caac4577
JG
4081 if (pdi->locdesc)
4082 addr = decode_locdesc (pdi->locdesc, cu);
4083
4084 if (pdi->locdesc
4085 && addr == 0
4086 && !dwarf2_per_objfile->has_section_at_zero)
4087 {
4088 /* A global or static variable may also have been stripped
4089 out by the linker if unused, in which case its address
4090 will be nullified; do not add such variables into partial
4091 symbol table then. */
4092 }
4093 else if (pdi->is_external)
c906108c
SS
4094 {
4095 /* Global Variable.
4096 Don't enter into the minimal symbol tables as there is
4097 a minimal symbol table entry from the ELF symbols already.
4098 Enter into partial symbol table if it has a location
4099 descriptor or a type.
4100 If the location descriptor is missing, new_symbol will create
4101 a LOC_UNRESOLVED symbol, the address of the variable will then
4102 be determined from the minimal symbol table whenever the variable
4103 is referenced.
4104 The address for the partial symbol table entry is not
4105 used by GDB, but it comes in handy for debugging partial symbol
4106 table building. */
4107
c906108c 4108 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4109 add_psymbol_to_list (actual_name, strlen (actual_name),
4110 built_actual_name,
4111 VAR_DOMAIN, LOC_STATIC,
4112 &objfile->global_psymbols,
4113 0, addr + baseaddr,
4114 cu->language, objfile);
c906108c
SS
4115 }
4116 else
4117 {
0963b4bd 4118 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4119 if (pdi->locdesc == NULL)
decbce07
MS
4120 {
4121 if (built_actual_name)
4122 xfree (actual_name);
4123 return;
4124 }
f47fb265 4125 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4126 mst_file_data, objfile); */
f47fb265
MS
4127 add_psymbol_to_list (actual_name, strlen (actual_name),
4128 built_actual_name,
4129 VAR_DOMAIN, LOC_STATIC,
4130 &objfile->static_psymbols,
4131 0, addr + baseaddr,
4132 cu->language, objfile);
c906108c
SS
4133 }
4134 break;
4135 case DW_TAG_typedef:
4136 case DW_TAG_base_type:
a02abb62 4137 case DW_TAG_subrange_type:
38d518c9 4138 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4139 built_actual_name,
176620f1 4140 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4141 &objfile->static_psymbols,
e142c38c 4142 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4143 break;
72bf9492
DJ
4144 case DW_TAG_namespace:
4145 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4146 built_actual_name,
72bf9492
DJ
4147 VAR_DOMAIN, LOC_TYPEDEF,
4148 &objfile->global_psymbols,
4149 0, (CORE_ADDR) 0, cu->language, objfile);
4150 break;
c906108c 4151 case DW_TAG_class_type:
680b30c7 4152 case DW_TAG_interface_type:
c906108c
SS
4153 case DW_TAG_structure_type:
4154 case DW_TAG_union_type:
4155 case DW_TAG_enumeration_type:
fa4028e9
JB
4156 /* Skip external references. The DWARF standard says in the section
4157 about "Structure, Union, and Class Type Entries": "An incomplete
4158 structure, union or class type is represented by a structure,
4159 union or class entry that does not have a byte size attribute
4160 and that has a DW_AT_declaration attribute." */
4161 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4162 {
4163 if (built_actual_name)
4164 xfree (actual_name);
4165 return;
4166 }
fa4028e9 4167
63d06c5c
DC
4168 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4169 static vs. global. */
38d518c9 4170 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4171 built_actual_name,
176620f1 4172 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4173 (cu->language == language_cplus
4174 || cu->language == language_java)
63d06c5c
DC
4175 ? &objfile->global_psymbols
4176 : &objfile->static_psymbols,
e142c38c 4177 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4178
c906108c
SS
4179 break;
4180 case DW_TAG_enumerator:
38d518c9 4181 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4182 built_actual_name,
176620f1 4183 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4184 (cu->language == language_cplus
4185 || cu->language == language_java)
f6fe98ef
DJ
4186 ? &objfile->global_psymbols
4187 : &objfile->static_psymbols,
e142c38c 4188 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4189 break;
4190 default:
4191 break;
4192 }
5c4e30ca 4193
72bf9492
DJ
4194 if (built_actual_name)
4195 xfree (actual_name);
c906108c
SS
4196}
4197
5c4e30ca
DC
4198/* Read a partial die corresponding to a namespace; also, add a symbol
4199 corresponding to that namespace to the symbol table. NAMESPACE is
4200 the name of the enclosing namespace. */
91c24f0a 4201
72bf9492
DJ
4202static void
4203add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4204 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4205 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4206{
72bf9492 4207 /* Add a symbol for the namespace. */
e7c27a73 4208
72bf9492 4209 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4210
4211 /* Now scan partial symbols in that namespace. */
4212
91c24f0a 4213 if (pdi->has_children)
5734ee8b 4214 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4215}
4216
5d7cb8df
JK
4217/* Read a partial die corresponding to a Fortran module. */
4218
4219static void
4220add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4221 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4222{
f55ee35c 4223 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4224
4225 if (pdi->has_children)
4226 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4227}
4228
bc30ff58
JB
4229/* Read a partial die corresponding to a subprogram and create a partial
4230 symbol for that subprogram. When the CU language allows it, this
4231 routine also defines a partial symbol for each nested subprogram
4232 that this subprogram contains.
6e70227d 4233
bc30ff58
JB
4234 DIE my also be a lexical block, in which case we simply search
4235 recursively for suprograms defined inside that lexical block.
4236 Again, this is only performed when the CU language allows this
4237 type of definitions. */
4238
4239static void
4240add_partial_subprogram (struct partial_die_info *pdi,
4241 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4242 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4243{
4244 if (pdi->tag == DW_TAG_subprogram)
4245 {
4246 if (pdi->has_pc_info)
4247 {
4248 if (pdi->lowpc < *lowpc)
4249 *lowpc = pdi->lowpc;
4250 if (pdi->highpc > *highpc)
4251 *highpc = pdi->highpc;
5734ee8b
DJ
4252 if (need_pc)
4253 {
4254 CORE_ADDR baseaddr;
4255 struct objfile *objfile = cu->objfile;
4256
4257 baseaddr = ANOFFSET (objfile->section_offsets,
4258 SECT_OFF_TEXT (objfile));
4259 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4260 pdi->lowpc + baseaddr,
4261 pdi->highpc - 1 + baseaddr,
9291a0cd 4262 cu->per_cu->v.psymtab);
5734ee8b 4263 }
bc30ff58 4264 if (!pdi->is_declaration)
e8d05480
JB
4265 /* Ignore subprogram DIEs that do not have a name, they are
4266 illegal. Do not emit a complaint at this point, we will
4267 do so when we convert this psymtab into a symtab. */
4268 if (pdi->name)
4269 add_partial_symbol (pdi, cu);
bc30ff58
JB
4270 }
4271 }
6e70227d 4272
bc30ff58
JB
4273 if (! pdi->has_children)
4274 return;
4275
4276 if (cu->language == language_ada)
4277 {
4278 pdi = pdi->die_child;
4279 while (pdi != NULL)
4280 {
4281 fixup_partial_die (pdi, cu);
4282 if (pdi->tag == DW_TAG_subprogram
4283 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4284 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4285 pdi = pdi->die_sibling;
4286 }
4287 }
4288}
4289
91c24f0a
DC
4290/* Read a partial die corresponding to an enumeration type. */
4291
72bf9492
DJ
4292static void
4293add_partial_enumeration (struct partial_die_info *enum_pdi,
4294 struct dwarf2_cu *cu)
91c24f0a 4295{
72bf9492 4296 struct partial_die_info *pdi;
91c24f0a
DC
4297
4298 if (enum_pdi->name != NULL)
72bf9492
DJ
4299 add_partial_symbol (enum_pdi, cu);
4300
4301 pdi = enum_pdi->die_child;
4302 while (pdi)
91c24f0a 4303 {
72bf9492 4304 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4305 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4306 else
72bf9492
DJ
4307 add_partial_symbol (pdi, cu);
4308 pdi = pdi->die_sibling;
91c24f0a 4309 }
91c24f0a
DC
4310}
4311
6caca83c
CC
4312/* Return the initial uleb128 in the die at INFO_PTR. */
4313
4314static unsigned int
4315peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4316{
4317 unsigned int bytes_read;
4318
4319 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4320}
4321
4bb7a0a7
DJ
4322/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4323 Return the corresponding abbrev, or NULL if the number is zero (indicating
4324 an empty DIE). In either case *BYTES_READ will be set to the length of
4325 the initial number. */
4326
4327static struct abbrev_info *
fe1b8b76 4328peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4329 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4330{
4331 bfd *abfd = cu->objfile->obfd;
4332 unsigned int abbrev_number;
4333 struct abbrev_info *abbrev;
4334
4335 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4336
4337 if (abbrev_number == 0)
4338 return NULL;
4339
4340 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4341 if (!abbrev)
4342 {
3e43a32a
MS
4343 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4344 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4345 }
4346
4347 return abbrev;
4348}
4349
93311388
DE
4350/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4351 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4352 DIE. Any children of the skipped DIEs will also be skipped. */
4353
fe1b8b76 4354static gdb_byte *
93311388 4355skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4356{
4357 struct abbrev_info *abbrev;
4358 unsigned int bytes_read;
4359
4360 while (1)
4361 {
4362 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4363 if (abbrev == NULL)
4364 return info_ptr + bytes_read;
4365 else
93311388 4366 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4367 }
4368}
4369
93311388
DE
4370/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4371 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4372 abbrev corresponding to that skipped uleb128 should be passed in
4373 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4374 children. */
4375
fe1b8b76 4376static gdb_byte *
93311388
DE
4377skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4378 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4379{
4380 unsigned int bytes_read;
4381 struct attribute attr;
4382 bfd *abfd = cu->objfile->obfd;
4383 unsigned int form, i;
4384
4385 for (i = 0; i < abbrev->num_attrs; i++)
4386 {
4387 /* The only abbrev we care about is DW_AT_sibling. */
4388 if (abbrev->attrs[i].name == DW_AT_sibling)
4389 {
4390 read_attribute (&attr, &abbrev->attrs[i],
4391 abfd, info_ptr, cu);
4392 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4393 complaint (&symfile_complaints,
4394 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4395 else
93311388 4396 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4397 }
4398
4399 /* If it isn't DW_AT_sibling, skip this attribute. */
4400 form = abbrev->attrs[i].form;
4401 skip_attribute:
4402 switch (form)
4403 {
4bb7a0a7 4404 case DW_FORM_ref_addr:
ae411497
TT
4405 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4406 and later it is offset sized. */
4407 if (cu->header.version == 2)
4408 info_ptr += cu->header.addr_size;
4409 else
4410 info_ptr += cu->header.offset_size;
4411 break;
4412 case DW_FORM_addr:
4bb7a0a7
DJ
4413 info_ptr += cu->header.addr_size;
4414 break;
4415 case DW_FORM_data1:
4416 case DW_FORM_ref1:
4417 case DW_FORM_flag:
4418 info_ptr += 1;
4419 break;
2dc7f7b3
TT
4420 case DW_FORM_flag_present:
4421 break;
4bb7a0a7
DJ
4422 case DW_FORM_data2:
4423 case DW_FORM_ref2:
4424 info_ptr += 2;
4425 break;
4426 case DW_FORM_data4:
4427 case DW_FORM_ref4:
4428 info_ptr += 4;
4429 break;
4430 case DW_FORM_data8:
4431 case DW_FORM_ref8:
55f1336d 4432 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4433 info_ptr += 8;
4434 break;
4435 case DW_FORM_string:
9b1c24c8 4436 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4437 info_ptr += bytes_read;
4438 break;
2dc7f7b3 4439 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4440 case DW_FORM_strp:
4441 info_ptr += cu->header.offset_size;
4442 break;
2dc7f7b3 4443 case DW_FORM_exprloc:
4bb7a0a7
DJ
4444 case DW_FORM_block:
4445 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4446 info_ptr += bytes_read;
4447 break;
4448 case DW_FORM_block1:
4449 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4450 break;
4451 case DW_FORM_block2:
4452 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4453 break;
4454 case DW_FORM_block4:
4455 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4456 break;
4457 case DW_FORM_sdata:
4458 case DW_FORM_udata:
4459 case DW_FORM_ref_udata:
4460 info_ptr = skip_leb128 (abfd, info_ptr);
4461 break;
4462 case DW_FORM_indirect:
4463 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4464 info_ptr += bytes_read;
4465 /* We need to continue parsing from here, so just go back to
4466 the top. */
4467 goto skip_attribute;
4468
4469 default:
3e43a32a
MS
4470 error (_("Dwarf Error: Cannot handle %s "
4471 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4472 dwarf_form_name (form),
4473 bfd_get_filename (abfd));
4474 }
4475 }
4476
4477 if (abbrev->has_children)
93311388 4478 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4479 else
4480 return info_ptr;
4481}
4482
93311388
DE
4483/* Locate ORIG_PDI's sibling.
4484 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4485 in BUFFER. */
91c24f0a 4486
fe1b8b76 4487static gdb_byte *
93311388
DE
4488locate_pdi_sibling (struct partial_die_info *orig_pdi,
4489 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4490 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4491{
4492 /* Do we know the sibling already? */
72bf9492 4493
91c24f0a
DC
4494 if (orig_pdi->sibling)
4495 return orig_pdi->sibling;
4496
4497 /* Are there any children to deal with? */
4498
4499 if (!orig_pdi->has_children)
4500 return info_ptr;
4501
4bb7a0a7 4502 /* Skip the children the long way. */
91c24f0a 4503
93311388 4504 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4505}
4506
c906108c
SS
4507/* Expand this partial symbol table into a full symbol table. */
4508
4509static void
fba45db2 4510dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4511{
c906108c
SS
4512 if (pst != NULL)
4513 {
4514 if (pst->readin)
4515 {
3e43a32a
MS
4516 warning (_("bug: psymtab for %s is already read in."),
4517 pst->filename);
c906108c
SS
4518 }
4519 else
4520 {
4521 if (info_verbose)
4522 {
3e43a32a
MS
4523 printf_filtered (_("Reading in symbols for %s..."),
4524 pst->filename);
c906108c
SS
4525 gdb_flush (gdb_stdout);
4526 }
4527
10b3939b
DJ
4528 /* Restore our global data. */
4529 dwarf2_per_objfile = objfile_data (pst->objfile,
4530 dwarf2_objfile_data_key);
4531
b2ab525c
KB
4532 /* If this psymtab is constructed from a debug-only objfile, the
4533 has_section_at_zero flag will not necessarily be correct. We
4534 can get the correct value for this flag by looking at the data
4535 associated with the (presumably stripped) associated objfile. */
4536 if (pst->objfile->separate_debug_objfile_backlink)
4537 {
4538 struct dwarf2_per_objfile *dpo_backlink
4539 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4540 dwarf2_objfile_data_key);
9a619af0 4541
b2ab525c
KB
4542 dwarf2_per_objfile->has_section_at_zero
4543 = dpo_backlink->has_section_at_zero;
4544 }
4545
98bfdba5
PA
4546 dwarf2_per_objfile->reading_partial_symbols = 0;
4547
c906108c
SS
4548 psymtab_to_symtab_1 (pst);
4549
4550 /* Finish up the debug error message. */
4551 if (info_verbose)
a3f17187 4552 printf_filtered (_("done.\n"));
c906108c
SS
4553 }
4554 }
4555}
4556
10b3939b
DJ
4557/* Add PER_CU to the queue. */
4558
4559static void
03dd20cc 4560queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4561{
4562 struct dwarf2_queue_item *item;
4563
4564 per_cu->queued = 1;
4565 item = xmalloc (sizeof (*item));
4566 item->per_cu = per_cu;
4567 item->next = NULL;
4568
4569 if (dwarf2_queue == NULL)
4570 dwarf2_queue = item;
4571 else
4572 dwarf2_queue_tail->next = item;
4573
4574 dwarf2_queue_tail = item;
4575}
4576
4577/* Process the queue. */
4578
4579static void
4580process_queue (struct objfile *objfile)
4581{
4582 struct dwarf2_queue_item *item, *next_item;
4583
03dd20cc
DJ
4584 /* The queue starts out with one item, but following a DIE reference
4585 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4586 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4587 {
9291a0cd
TT
4588 if (dwarf2_per_objfile->using_index
4589 ? !item->per_cu->v.quick->symtab
4590 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4591 process_full_comp_unit (item->per_cu);
4592
4593 item->per_cu->queued = 0;
4594 next_item = item->next;
4595 xfree (item);
4596 }
4597
4598 dwarf2_queue_tail = NULL;
4599}
4600
4601/* Free all allocated queue entries. This function only releases anything if
4602 an error was thrown; if the queue was processed then it would have been
4603 freed as we went along. */
4604
4605static void
4606dwarf2_release_queue (void *dummy)
4607{
4608 struct dwarf2_queue_item *item, *last;
4609
4610 item = dwarf2_queue;
4611 while (item)
4612 {
4613 /* Anything still marked queued is likely to be in an
4614 inconsistent state, so discard it. */
4615 if (item->per_cu->queued)
4616 {
4617 if (item->per_cu->cu != NULL)
4618 free_one_cached_comp_unit (item->per_cu->cu);
4619 item->per_cu->queued = 0;
4620 }
4621
4622 last = item;
4623 item = item->next;
4624 xfree (last);
4625 }
4626
4627 dwarf2_queue = dwarf2_queue_tail = NULL;
4628}
4629
4630/* Read in full symbols for PST, and anything it depends on. */
4631
c906108c 4632static void
fba45db2 4633psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4634{
10b3939b 4635 struct dwarf2_per_cu_data *per_cu;
c906108c 4636 struct cleanup *back_to;
aaa75496
JB
4637 int i;
4638
4639 for (i = 0; i < pst->number_of_dependencies; i++)
4640 if (!pst->dependencies[i]->readin)
4641 {
4642 /* Inform about additional files that need to be read in. */
4643 if (info_verbose)
4644 {
a3f17187 4645 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4646 fputs_filtered (" ", gdb_stdout);
4647 wrap_here ("");
4648 fputs_filtered ("and ", gdb_stdout);
4649 wrap_here ("");
4650 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4651 wrap_here (""); /* Flush output. */
aaa75496
JB
4652 gdb_flush (gdb_stdout);
4653 }
4654 psymtab_to_symtab_1 (pst->dependencies[i]);
4655 }
4656
e38df1d0 4657 per_cu = pst->read_symtab_private;
10b3939b
DJ
4658
4659 if (per_cu == NULL)
aaa75496
JB
4660 {
4661 /* It's an include file, no symbols to read for it.
4662 Everything is in the parent symtab. */
4663 pst->readin = 1;
4664 return;
4665 }
c906108c 4666
9291a0cd 4667 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4668}
4669
93311388 4670/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4671
93311388 4672static void
3e43a32a
MS
4673load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4674 struct objfile *objfile)
10b3939b 4675{
31ffec48 4676 bfd *abfd = objfile->obfd;
10b3939b 4677 struct dwarf2_cu *cu;
c764a876 4678 unsigned int offset;
93311388 4679 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4680 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4681 struct attribute *attr;
98bfdba5 4682 int read_cu = 0;
6502dd73 4683
8b70b953 4684 gdb_assert (! per_cu->debug_type_section);
348e048f 4685
c906108c 4686 /* Set local variables from the partial symbol table info. */
10b3939b 4687 offset = per_cu->offset;
6502dd73 4688
be391dca 4689 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4690 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4691 beg_of_comp_unit = info_ptr;
63d06c5c 4692
98bfdba5
PA
4693 if (per_cu->cu == NULL)
4694 {
9816fde3
JK
4695 cu = xmalloc (sizeof (*cu));
4696 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4697
4698 read_cu = 1;
c906108c 4699
98bfdba5
PA
4700 /* If an error occurs while loading, release our storage. */
4701 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4702
98bfdba5
PA
4703 /* Read in the comp_unit header. */
4704 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4705
6caca83c
CC
4706 /* Skip dummy compilation units. */
4707 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4708 + dwarf2_per_objfile->info.size)
4709 || peek_abbrev_code (abfd, info_ptr) == 0)
4710 {
4711 do_cleanups (free_cu_cleanup);
4712 return;
4713 }
4714
98bfdba5
PA
4715 /* Complete the cu_header. */
4716 cu->header.offset = offset;
4717 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4718
98bfdba5
PA
4719 /* Read the abbrevs for this compilation unit. */
4720 dwarf2_read_abbrevs (abfd, cu);
4721 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4722
98bfdba5
PA
4723 /* Link this compilation unit into the compilation unit tree. */
4724 per_cu->cu = cu;
4725 cu->per_cu = per_cu;
98bfdba5
PA
4726
4727 /* Link this CU into read_in_chain. */
4728 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4729 dwarf2_per_objfile->read_in_chain = per_cu;
4730 }
4731 else
4732 {
4733 cu = per_cu->cu;
4734 info_ptr += cu->header.first_die_offset;
4735 }
e142c38c 4736
93311388 4737 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4738
4739 /* We try not to read any attributes in this function, because not
4740 all objfiles needed for references have been loaded yet, and symbol
4741 table processing isn't initialized. But we have to set the CU language,
4742 or we won't be able to build types correctly. */
9816fde3 4743 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4744
a6c727b2
DJ
4745 /* Similarly, if we do not read the producer, we can not apply
4746 producer-specific interpretation. */
4747 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4748 if (attr)
4749 cu->producer = DW_STRING (attr);
4750
98bfdba5
PA
4751 if (read_cu)
4752 {
4753 do_cleanups (free_abbrevs_cleanup);
e142c38c 4754
98bfdba5
PA
4755 /* We've successfully allocated this compilation unit. Let our
4756 caller clean it up when finished with it. */
4757 discard_cleanups (free_cu_cleanup);
4758 }
10b3939b
DJ
4759}
4760
3da10d80
KS
4761/* Add a DIE to the delayed physname list. */
4762
4763static void
4764add_to_method_list (struct type *type, int fnfield_index, int index,
4765 const char *name, struct die_info *die,
4766 struct dwarf2_cu *cu)
4767{
4768 struct delayed_method_info mi;
4769 mi.type = type;
4770 mi.fnfield_index = fnfield_index;
4771 mi.index = index;
4772 mi.name = name;
4773 mi.die = die;
4774 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4775}
4776
4777/* A cleanup for freeing the delayed method list. */
4778
4779static void
4780free_delayed_list (void *ptr)
4781{
4782 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4783 if (cu->method_list != NULL)
4784 {
4785 VEC_free (delayed_method_info, cu->method_list);
4786 cu->method_list = NULL;
4787 }
4788}
4789
4790/* Compute the physnames of any methods on the CU's method list.
4791
4792 The computation of method physnames is delayed in order to avoid the
4793 (bad) condition that one of the method's formal parameters is of an as yet
4794 incomplete type. */
4795
4796static void
4797compute_delayed_physnames (struct dwarf2_cu *cu)
4798{
4799 int i;
4800 struct delayed_method_info *mi;
4801 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4802 {
1d06ead6 4803 const char *physname;
3da10d80
KS
4804 struct fn_fieldlist *fn_flp
4805 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4806 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4807 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4808 }
4809}
4810
10b3939b
DJ
4811/* Generate full symbol information for PST and CU, whose DIEs have
4812 already been loaded into memory. */
4813
4814static void
4815process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4816{
10b3939b 4817 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4818 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4819 CORE_ADDR lowpc, highpc;
4820 struct symtab *symtab;
3da10d80 4821 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4822 CORE_ADDR baseaddr;
4823
4824 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4825
10b3939b
DJ
4826 buildsym_init ();
4827 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4828 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4829
4830 cu->list_in_scope = &file_symbols;
c906108c
SS
4831
4832 /* Do line number decoding in read_file_scope () */
10b3939b 4833 process_die (cu->dies, cu);
c906108c 4834
3da10d80
KS
4835 /* Now that we have processed all the DIEs in the CU, all the types
4836 should be complete, and it should now be safe to compute all of the
4837 physnames. */
4838 compute_delayed_physnames (cu);
4839 do_cleanups (delayed_list_cleanup);
4840
fae299cd
DC
4841 /* Some compilers don't define a DW_AT_high_pc attribute for the
4842 compilation unit. If the DW_AT_high_pc is missing, synthesize
4843 it, by scanning the DIE's below the compilation unit. */
10b3939b 4844 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4845
613e1657 4846 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4847
8be455d7 4848 if (symtab != NULL)
c906108c 4849 {
df15bd07 4850 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4851
8be455d7
JK
4852 /* Set symtab language to language from DW_AT_language. If the
4853 compilation is from a C file generated by language preprocessors, do
4854 not set the language if it was already deduced by start_subfile. */
4855 if (!(cu->language == language_c && symtab->language != language_c))
4856 symtab->language = cu->language;
4857
4858 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4859 produce DW_AT_location with location lists but it can be possibly
4860 invalid without -fvar-tracking.
4861
4862 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4863 needed, it would be wrong due to missing DW_AT_producer there.
4864
4865 Still one can confuse GDB by using non-standard GCC compilation
4866 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4867 */
4632c0d0 4868 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4869 symtab->locations_valid = 1;
e0d00bc7
JK
4870
4871 if (gcc_4_minor >= 5)
4872 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4873
4874 symtab->call_site_htab = cu->call_site_htab;
c906108c 4875 }
9291a0cd
TT
4876
4877 if (dwarf2_per_objfile->using_index)
4878 per_cu->v.quick->symtab = symtab;
4879 else
4880 {
4881 struct partial_symtab *pst = per_cu->v.psymtab;
4882 pst->symtab = symtab;
4883 pst->readin = 1;
4884 }
c906108c
SS
4885
4886 do_cleanups (back_to);
4887}
4888
4889/* Process a die and its children. */
4890
4891static void
e7c27a73 4892process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4893{
4894 switch (die->tag)
4895 {
4896 case DW_TAG_padding:
4897 break;
4898 case DW_TAG_compile_unit:
e7c27a73 4899 read_file_scope (die, cu);
c906108c 4900 break;
348e048f
DE
4901 case DW_TAG_type_unit:
4902 read_type_unit_scope (die, cu);
4903 break;
c906108c 4904 case DW_TAG_subprogram:
c906108c 4905 case DW_TAG_inlined_subroutine:
edb3359d 4906 read_func_scope (die, cu);
c906108c
SS
4907 break;
4908 case DW_TAG_lexical_block:
14898363
L
4909 case DW_TAG_try_block:
4910 case DW_TAG_catch_block:
e7c27a73 4911 read_lexical_block_scope (die, cu);
c906108c 4912 break;
96408a79
SA
4913 case DW_TAG_GNU_call_site:
4914 read_call_site_scope (die, cu);
4915 break;
c906108c 4916 case DW_TAG_class_type:
680b30c7 4917 case DW_TAG_interface_type:
c906108c
SS
4918 case DW_TAG_structure_type:
4919 case DW_TAG_union_type:
134d01f1 4920 process_structure_scope (die, cu);
c906108c
SS
4921 break;
4922 case DW_TAG_enumeration_type:
134d01f1 4923 process_enumeration_scope (die, cu);
c906108c 4924 break;
134d01f1 4925
f792889a
DJ
4926 /* These dies have a type, but processing them does not create
4927 a symbol or recurse to process the children. Therefore we can
4928 read them on-demand through read_type_die. */
c906108c 4929 case DW_TAG_subroutine_type:
72019c9c 4930 case DW_TAG_set_type:
c906108c 4931 case DW_TAG_array_type:
c906108c 4932 case DW_TAG_pointer_type:
c906108c 4933 case DW_TAG_ptr_to_member_type:
c906108c 4934 case DW_TAG_reference_type:
c906108c 4935 case DW_TAG_string_type:
c906108c 4936 break;
134d01f1 4937
c906108c 4938 case DW_TAG_base_type:
a02abb62 4939 case DW_TAG_subrange_type:
cb249c71 4940 case DW_TAG_typedef:
134d01f1
DJ
4941 /* Add a typedef symbol for the type definition, if it has a
4942 DW_AT_name. */
f792889a 4943 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4944 break;
c906108c 4945 case DW_TAG_common_block:
e7c27a73 4946 read_common_block (die, cu);
c906108c
SS
4947 break;
4948 case DW_TAG_common_inclusion:
4949 break;
d9fa45fe 4950 case DW_TAG_namespace:
63d06c5c 4951 processing_has_namespace_info = 1;
e7c27a73 4952 read_namespace (die, cu);
d9fa45fe 4953 break;
5d7cb8df 4954 case DW_TAG_module:
f55ee35c 4955 processing_has_namespace_info = 1;
5d7cb8df
JK
4956 read_module (die, cu);
4957 break;
d9fa45fe
DC
4958 case DW_TAG_imported_declaration:
4959 case DW_TAG_imported_module:
63d06c5c 4960 processing_has_namespace_info = 1;
27aa8d6a
SW
4961 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4962 || cu->language != language_fortran))
4963 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4964 dwarf_tag_name (die->tag));
4965 read_import_statement (die, cu);
d9fa45fe 4966 break;
c906108c 4967 default:
e7c27a73 4968 new_symbol (die, NULL, cu);
c906108c
SS
4969 break;
4970 }
4971}
4972
94af9270
KS
4973/* A helper function for dwarf2_compute_name which determines whether DIE
4974 needs to have the name of the scope prepended to the name listed in the
4975 die. */
4976
4977static int
4978die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4979{
1c809c68
TT
4980 struct attribute *attr;
4981
94af9270
KS
4982 switch (die->tag)
4983 {
4984 case DW_TAG_namespace:
4985 case DW_TAG_typedef:
4986 case DW_TAG_class_type:
4987 case DW_TAG_interface_type:
4988 case DW_TAG_structure_type:
4989 case DW_TAG_union_type:
4990 case DW_TAG_enumeration_type:
4991 case DW_TAG_enumerator:
4992 case DW_TAG_subprogram:
4993 case DW_TAG_member:
4994 return 1;
4995
4996 case DW_TAG_variable:
c2b0a229 4997 case DW_TAG_constant:
94af9270
KS
4998 /* We only need to prefix "globally" visible variables. These include
4999 any variable marked with DW_AT_external or any variable that
5000 lives in a namespace. [Variables in anonymous namespaces
5001 require prefixing, but they are not DW_AT_external.] */
5002
5003 if (dwarf2_attr (die, DW_AT_specification, cu))
5004 {
5005 struct dwarf2_cu *spec_cu = cu;
9a619af0 5006
94af9270
KS
5007 return die_needs_namespace (die_specification (die, &spec_cu),
5008 spec_cu);
5009 }
5010
1c809c68 5011 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5012 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5013 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5014 return 0;
5015 /* A variable in a lexical block of some kind does not need a
5016 namespace, even though in C++ such variables may be external
5017 and have a mangled name. */
5018 if (die->parent->tag == DW_TAG_lexical_block
5019 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5020 || die->parent->tag == DW_TAG_catch_block
5021 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5022 return 0;
5023 return 1;
94af9270
KS
5024
5025 default:
5026 return 0;
5027 }
5028}
5029
98bfdba5
PA
5030/* Retrieve the last character from a mem_file. */
5031
5032static void
5033do_ui_file_peek_last (void *object, const char *buffer, long length)
5034{
5035 char *last_char_p = (char *) object;
5036
5037 if (length > 0)
5038 *last_char_p = buffer[length - 1];
5039}
5040
94af9270
KS
5041/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5042 compute the physname for the object, which include a method's
5043 formal parameters (C++/Java) and return type (Java).
5044
af6b7be1
JB
5045 For Ada, return the DIE's linkage name rather than the fully qualified
5046 name. PHYSNAME is ignored..
5047
94af9270
KS
5048 The result is allocated on the objfile_obstack and canonicalized. */
5049
5050static const char *
5051dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5052 int physname)
5053{
5054 if (name == NULL)
5055 name = dwarf2_name (die, cu);
5056
f55ee35c
JK
5057 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5058 compute it by typename_concat inside GDB. */
5059 if (cu->language == language_ada
5060 || (cu->language == language_fortran && physname))
5061 {
5062 /* For Ada unit, we prefer the linkage name over the name, as
5063 the former contains the exported name, which the user expects
5064 to be able to reference. Ideally, we want the user to be able
5065 to reference this entity using either natural or linkage name,
5066 but we haven't started looking at this enhancement yet. */
5067 struct attribute *attr;
5068
5069 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5070 if (attr == NULL)
5071 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5072 if (attr && DW_STRING (attr))
5073 return DW_STRING (attr);
5074 }
5075
94af9270
KS
5076 /* These are the only languages we know how to qualify names in. */
5077 if (name != NULL
f55ee35c
JK
5078 && (cu->language == language_cplus || cu->language == language_java
5079 || cu->language == language_fortran))
94af9270
KS
5080 {
5081 if (die_needs_namespace (die, cu))
5082 {
5083 long length;
5084 char *prefix;
5085 struct ui_file *buf;
5086
5087 prefix = determine_prefix (die, cu);
5088 buf = mem_fileopen ();
5089 if (*prefix != '\0')
5090 {
f55ee35c
JK
5091 char *prefixed_name = typename_concat (NULL, prefix, name,
5092 physname, cu);
9a619af0 5093
94af9270
KS
5094 fputs_unfiltered (prefixed_name, buf);
5095 xfree (prefixed_name);
5096 }
5097 else
62d5b8da 5098 fputs_unfiltered (name, buf);
94af9270 5099
98bfdba5
PA
5100 /* Template parameters may be specified in the DIE's DW_AT_name, or
5101 as children with DW_TAG_template_type_param or
5102 DW_TAG_value_type_param. If the latter, add them to the name
5103 here. If the name already has template parameters, then
5104 skip this step; some versions of GCC emit both, and
5105 it is more efficient to use the pre-computed name.
5106
5107 Something to keep in mind about this process: it is very
5108 unlikely, or in some cases downright impossible, to produce
5109 something that will match the mangled name of a function.
5110 If the definition of the function has the same debug info,
5111 we should be able to match up with it anyway. But fallbacks
5112 using the minimal symbol, for instance to find a method
5113 implemented in a stripped copy of libstdc++, will not work.
5114 If we do not have debug info for the definition, we will have to
5115 match them up some other way.
5116
5117 When we do name matching there is a related problem with function
5118 templates; two instantiated function templates are allowed to
5119 differ only by their return types, which we do not add here. */
5120
5121 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5122 {
5123 struct attribute *attr;
5124 struct die_info *child;
5125 int first = 1;
5126
5127 die->building_fullname = 1;
5128
5129 for (child = die->child; child != NULL; child = child->sibling)
5130 {
5131 struct type *type;
5132 long value;
5133 gdb_byte *bytes;
5134 struct dwarf2_locexpr_baton *baton;
5135 struct value *v;
5136
5137 if (child->tag != DW_TAG_template_type_param
5138 && child->tag != DW_TAG_template_value_param)
5139 continue;
5140
5141 if (first)
5142 {
5143 fputs_unfiltered ("<", buf);
5144 first = 0;
5145 }
5146 else
5147 fputs_unfiltered (", ", buf);
5148
5149 attr = dwarf2_attr (child, DW_AT_type, cu);
5150 if (attr == NULL)
5151 {
5152 complaint (&symfile_complaints,
5153 _("template parameter missing DW_AT_type"));
5154 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5155 continue;
5156 }
5157 type = die_type (child, cu);
5158
5159 if (child->tag == DW_TAG_template_type_param)
5160 {
5161 c_print_type (type, "", buf, -1, 0);
5162 continue;
5163 }
5164
5165 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5166 if (attr == NULL)
5167 {
5168 complaint (&symfile_complaints,
3e43a32a
MS
5169 _("template parameter missing "
5170 "DW_AT_const_value"));
98bfdba5
PA
5171 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5172 continue;
5173 }
5174
5175 dwarf2_const_value_attr (attr, type, name,
5176 &cu->comp_unit_obstack, cu,
5177 &value, &bytes, &baton);
5178
5179 if (TYPE_NOSIGN (type))
5180 /* GDB prints characters as NUMBER 'CHAR'. If that's
5181 changed, this can use value_print instead. */
5182 c_printchar (value, type, buf);
5183 else
5184 {
5185 struct value_print_options opts;
5186
5187 if (baton != NULL)
5188 v = dwarf2_evaluate_loc_desc (type, NULL,
5189 baton->data,
5190 baton->size,
5191 baton->per_cu);
5192 else if (bytes != NULL)
5193 {
5194 v = allocate_value (type);
5195 memcpy (value_contents_writeable (v), bytes,
5196 TYPE_LENGTH (type));
5197 }
5198 else
5199 v = value_from_longest (type, value);
5200
3e43a32a
MS
5201 /* Specify decimal so that we do not depend on
5202 the radix. */
98bfdba5
PA
5203 get_formatted_print_options (&opts, 'd');
5204 opts.raw = 1;
5205 value_print (v, buf, &opts);
5206 release_value (v);
5207 value_free (v);
5208 }
5209 }
5210
5211 die->building_fullname = 0;
5212
5213 if (!first)
5214 {
5215 /* Close the argument list, with a space if necessary
5216 (nested templates). */
5217 char last_char = '\0';
5218 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5219 if (last_char == '>')
5220 fputs_unfiltered (" >", buf);
5221 else
5222 fputs_unfiltered (">", buf);
5223 }
5224 }
5225
94af9270
KS
5226 /* For Java and C++ methods, append formal parameter type
5227 information, if PHYSNAME. */
6e70227d 5228
94af9270
KS
5229 if (physname && die->tag == DW_TAG_subprogram
5230 && (cu->language == language_cplus
5231 || cu->language == language_java))
5232 {
5233 struct type *type = read_type_die (die, cu);
5234
3167638f 5235 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5236
5237 if (cu->language == language_java)
5238 {
5239 /* For java, we must append the return type to method
0963b4bd 5240 names. */
94af9270
KS
5241 if (die->tag == DW_TAG_subprogram)
5242 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5243 0, 0);
5244 }
5245 else if (cu->language == language_cplus)
5246 {
60430eff
DJ
5247 /* Assume that an artificial first parameter is
5248 "this", but do not crash if it is not. RealView
5249 marks unnamed (and thus unused) parameters as
5250 artificial; there is no way to differentiate
5251 the two cases. */
94af9270
KS
5252 if (TYPE_NFIELDS (type) > 0
5253 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5254 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5255 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5256 0))))
94af9270
KS
5257 fputs_unfiltered (" const", buf);
5258 }
5259 }
5260
5261 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5262 &length);
5263 ui_file_delete (buf);
5264
5265 if (cu->language == language_cplus)
5266 {
5267 char *cname
5268 = dwarf2_canonicalize_name (name, cu,
5269 &cu->objfile->objfile_obstack);
9a619af0 5270
94af9270
KS
5271 if (cname != NULL)
5272 name = cname;
5273 }
5274 }
5275 }
5276
5277 return name;
5278}
5279
0114d602
DJ
5280/* Return the fully qualified name of DIE, based on its DW_AT_name.
5281 If scope qualifiers are appropriate they will be added. The result
5282 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5283 not have a name. NAME may either be from a previous call to
5284 dwarf2_name or NULL.
5285
0963b4bd 5286 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5287
5288static const char *
94af9270 5289dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5290{
94af9270
KS
5291 return dwarf2_compute_name (name, die, cu, 0);
5292}
0114d602 5293
94af9270
KS
5294/* Construct a physname for the given DIE in CU. NAME may either be
5295 from a previous call to dwarf2_name or NULL. The result will be
5296 allocated on the objfile_objstack or NULL if the DIE does not have a
5297 name.
0114d602 5298
94af9270 5299 The output string will be canonicalized (if C++/Java). */
0114d602 5300
94af9270
KS
5301static const char *
5302dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5303{
900e11f9
JK
5304 struct attribute *attr;
5305 const char *retval, *mangled = NULL, *canon = NULL;
5306 struct cleanup *back_to;
5307 int need_copy = 1;
5308
5309 /* In this case dwarf2_compute_name is just a shortcut not building anything
5310 on its own. */
5311 if (!die_needs_namespace (die, cu))
5312 return dwarf2_compute_name (name, die, cu, 1);
5313
5314 back_to = make_cleanup (null_cleanup, NULL);
5315
5316 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5317 if (!attr)
5318 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5319
5320 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5321 has computed. */
5322 if (attr && DW_STRING (attr))
5323 {
5324 char *demangled;
5325
5326 mangled = DW_STRING (attr);
5327
5328 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5329 type. It is easier for GDB users to search for such functions as
5330 `name(params)' than `long name(params)'. In such case the minimal
5331 symbol names do not match the full symbol names but for template
5332 functions there is never a need to look up their definition from their
5333 declaration so the only disadvantage remains the minimal symbol
5334 variant `long name(params)' does not have the proper inferior type.
5335 */
5336
5337 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5338 | (cu->language == language_java
5339 ? DMGL_JAVA | DMGL_RET_POSTFIX
5340 : DMGL_RET_DROP)));
5341 if (demangled)
5342 {
5343 make_cleanup (xfree, demangled);
5344 canon = demangled;
5345 }
5346 else
5347 {
5348 canon = mangled;
5349 need_copy = 0;
5350 }
5351 }
5352
5353 if (canon == NULL || check_physname)
5354 {
5355 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5356
5357 if (canon != NULL && strcmp (physname, canon) != 0)
5358 {
5359 /* It may not mean a bug in GDB. The compiler could also
5360 compute DW_AT_linkage_name incorrectly. But in such case
5361 GDB would need to be bug-to-bug compatible. */
5362
5363 complaint (&symfile_complaints,
5364 _("Computed physname <%s> does not match demangled <%s> "
5365 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5366 physname, canon, mangled, die->offset, cu->objfile->name);
5367
5368 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5369 is available here - over computed PHYSNAME. It is safer
5370 against both buggy GDB and buggy compilers. */
5371
5372 retval = canon;
5373 }
5374 else
5375 {
5376 retval = physname;
5377 need_copy = 0;
5378 }
5379 }
5380 else
5381 retval = canon;
5382
5383 if (need_copy)
5384 retval = obsavestring (retval, strlen (retval),
5385 &cu->objfile->objfile_obstack);
5386
5387 do_cleanups (back_to);
5388 return retval;
0114d602
DJ
5389}
5390
27aa8d6a
SW
5391/* Read the import statement specified by the given die and record it. */
5392
5393static void
5394read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5395{
5396 struct attribute *import_attr;
32019081 5397 struct die_info *imported_die, *child_die;
de4affc9 5398 struct dwarf2_cu *imported_cu;
27aa8d6a 5399 const char *imported_name;
794684b6 5400 const char *imported_name_prefix;
13387711
SW
5401 const char *canonical_name;
5402 const char *import_alias;
5403 const char *imported_declaration = NULL;
794684b6 5404 const char *import_prefix;
32019081
JK
5405 VEC (const_char_ptr) *excludes = NULL;
5406 struct cleanup *cleanups;
13387711
SW
5407
5408 char *temp;
27aa8d6a
SW
5409
5410 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5411 if (import_attr == NULL)
5412 {
5413 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5414 dwarf_tag_name (die->tag));
5415 return;
5416 }
5417
de4affc9
CC
5418 imported_cu = cu;
5419 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5420 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5421 if (imported_name == NULL)
5422 {
5423 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5424
5425 The import in the following code:
5426 namespace A
5427 {
5428 typedef int B;
5429 }
5430
5431 int main ()
5432 {
5433 using A::B;
5434 B b;
5435 return b;
5436 }
5437
5438 ...
5439 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5440 <52> DW_AT_decl_file : 1
5441 <53> DW_AT_decl_line : 6
5442 <54> DW_AT_import : <0x75>
5443 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5444 <59> DW_AT_name : B
5445 <5b> DW_AT_decl_file : 1
5446 <5c> DW_AT_decl_line : 2
5447 <5d> DW_AT_type : <0x6e>
5448 ...
5449 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5450 <76> DW_AT_byte_size : 4
5451 <77> DW_AT_encoding : 5 (signed)
5452
5453 imports the wrong die ( 0x75 instead of 0x58 ).
5454 This case will be ignored until the gcc bug is fixed. */
5455 return;
5456 }
5457
82856980
SW
5458 /* Figure out the local name after import. */
5459 import_alias = dwarf2_name (die, cu);
27aa8d6a 5460
794684b6
SW
5461 /* Figure out where the statement is being imported to. */
5462 import_prefix = determine_prefix (die, cu);
5463
5464 /* Figure out what the scope of the imported die is and prepend it
5465 to the name of the imported die. */
de4affc9 5466 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5467
f55ee35c
JK
5468 if (imported_die->tag != DW_TAG_namespace
5469 && imported_die->tag != DW_TAG_module)
794684b6 5470 {
13387711
SW
5471 imported_declaration = imported_name;
5472 canonical_name = imported_name_prefix;
794684b6 5473 }
13387711 5474 else if (strlen (imported_name_prefix) > 0)
794684b6 5475 {
13387711
SW
5476 temp = alloca (strlen (imported_name_prefix)
5477 + 2 + strlen (imported_name) + 1);
5478 strcpy (temp, imported_name_prefix);
5479 strcat (temp, "::");
5480 strcat (temp, imported_name);
5481 canonical_name = temp;
794684b6 5482 }
13387711
SW
5483 else
5484 canonical_name = imported_name;
794684b6 5485
32019081
JK
5486 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5487
5488 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5489 for (child_die = die->child; child_die && child_die->tag;
5490 child_die = sibling_die (child_die))
5491 {
5492 /* DWARF-4: A Fortran use statement with a “rename list” may be
5493 represented by an imported module entry with an import attribute
5494 referring to the module and owned entries corresponding to those
5495 entities that are renamed as part of being imported. */
5496
5497 if (child_die->tag != DW_TAG_imported_declaration)
5498 {
5499 complaint (&symfile_complaints,
5500 _("child DW_TAG_imported_declaration expected "
5501 "- DIE at 0x%x [in module %s]"),
5502 child_die->offset, cu->objfile->name);
5503 continue;
5504 }
5505
5506 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5507 if (import_attr == NULL)
5508 {
5509 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5510 dwarf_tag_name (child_die->tag));
5511 continue;
5512 }
5513
5514 imported_cu = cu;
5515 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5516 &imported_cu);
5517 imported_name = dwarf2_name (imported_die, imported_cu);
5518 if (imported_name == NULL)
5519 {
5520 complaint (&symfile_complaints,
5521 _("child DW_TAG_imported_declaration has unknown "
5522 "imported name - DIE at 0x%x [in module %s]"),
5523 child_die->offset, cu->objfile->name);
5524 continue;
5525 }
5526
5527 VEC_safe_push (const_char_ptr, excludes, imported_name);
5528
5529 process_die (child_die, cu);
5530 }
5531
c0cc3a76
SW
5532 cp_add_using_directive (import_prefix,
5533 canonical_name,
5534 import_alias,
13387711 5535 imported_declaration,
32019081 5536 excludes,
c0cc3a76 5537 &cu->objfile->objfile_obstack);
32019081
JK
5538
5539 do_cleanups (cleanups);
27aa8d6a
SW
5540}
5541
5fb290d7 5542static void
e142c38c 5543initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5544{
e142c38c 5545 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5546}
5547
ae2de4f8
DE
5548/* Cleanup function for read_file_scope. */
5549
cb1df416
DJ
5550static void
5551free_cu_line_header (void *arg)
5552{
5553 struct dwarf2_cu *cu = arg;
5554
5555 free_line_header (cu->line_header);
5556 cu->line_header = NULL;
5557}
5558
9291a0cd
TT
5559static void
5560find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5561 char **name, char **comp_dir)
5562{
5563 struct attribute *attr;
5564
5565 *name = NULL;
5566 *comp_dir = NULL;
5567
5568 /* Find the filename. Do not use dwarf2_name here, since the filename
5569 is not a source language identifier. */
5570 attr = dwarf2_attr (die, DW_AT_name, cu);
5571 if (attr)
5572 {
5573 *name = DW_STRING (attr);
5574 }
5575
5576 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5577 if (attr)
5578 *comp_dir = DW_STRING (attr);
5579 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5580 {
5581 *comp_dir = ldirname (*name);
5582 if (*comp_dir != NULL)
5583 make_cleanup (xfree, *comp_dir);
5584 }
5585 if (*comp_dir != NULL)
5586 {
5587 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5588 directory, get rid of it. */
5589 char *cp = strchr (*comp_dir, ':');
5590
5591 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5592 *comp_dir = cp + 1;
5593 }
5594
5595 if (*name == NULL)
5596 *name = "<unknown>";
5597}
5598
2ab95328
TT
5599/* Handle DW_AT_stmt_list for a compilation unit. */
5600
5601static void
5602handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5603 const char *comp_dir)
5604{
5605 struct attribute *attr;
5606 struct objfile *objfile = cu->objfile;
5607 bfd *abfd = objfile->obfd;
5608
5609 /* Decode line number information if present. We do this before
5610 processing child DIEs, so that the line header table is available
5611 for DW_AT_decl_file. */
5612 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5613 if (attr)
5614 {
5615 unsigned int line_offset = DW_UNSND (attr);
5616 struct line_header *line_header
5617 = dwarf_decode_line_header (line_offset, abfd, cu);
5618
5619 if (line_header)
5620 {
5621 cu->line_header = line_header;
5622 make_cleanup (free_cu_line_header, cu);
5623 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5624 }
5625 }
5626}
5627
ae2de4f8
DE
5628/* Process DW_TAG_compile_unit. */
5629
c906108c 5630static void
e7c27a73 5631read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5632{
e7c27a73 5633 struct objfile *objfile = cu->objfile;
debd256d 5634 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5635 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5636 CORE_ADDR highpc = ((CORE_ADDR) 0);
5637 struct attribute *attr;
e1024ff1 5638 char *name = NULL;
c906108c
SS
5639 char *comp_dir = NULL;
5640 struct die_info *child_die;
5641 bfd *abfd = objfile->obfd;
e142c38c 5642 CORE_ADDR baseaddr;
6e70227d 5643
e142c38c 5644 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5645
fae299cd 5646 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5647
5648 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5649 from finish_block. */
2acceee2 5650 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5651 lowpc = highpc;
5652 lowpc += baseaddr;
5653 highpc += baseaddr;
5654
9291a0cd 5655 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5656
e142c38c 5657 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5658 if (attr)
5659 {
e142c38c 5660 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5661 }
5662
b0f35d58 5663 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5664 if (attr)
b0f35d58 5665 cu->producer = DW_STRING (attr);
303b6f5d 5666
f4b8a18d
KW
5667 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5668 standardised yet. As a workaround for the language detection we fall
5669 back to the DW_AT_producer string. */
5670 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5671 cu->language = language_opencl;
5672
0963b4bd 5673 /* We assume that we're processing GCC output. */
c906108c 5674 processing_gcc_compilation = 2;
c906108c 5675
df8a16a1
DJ
5676 processing_has_namespace_info = 0;
5677
c906108c
SS
5678 start_symtab (name, comp_dir, lowpc);
5679 record_debugformat ("DWARF 2");
303b6f5d 5680 record_producer (cu->producer);
c906108c 5681
e142c38c 5682 initialize_cu_func_list (cu);
c906108c 5683
2ab95328 5684 handle_DW_AT_stmt_list (die, cu, comp_dir);
debd256d 5685
cb1df416
DJ
5686 /* Process all dies in compilation unit. */
5687 if (die->child != NULL)
5688 {
5689 child_die = die->child;
5690 while (child_die && child_die->tag)
5691 {
5692 process_die (child_die, cu);
5693 child_die = sibling_die (child_die);
5694 }
5695 }
5696
2e276125
JB
5697 /* Decode macro information, if present. Dwarf 2 macro information
5698 refers to information in the line number info statement program
5699 header, so we can only read it if we've read the header
5700 successfully. */
cf2c3c16 5701 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5702 if (attr && cu->line_header)
2e276125 5703 {
cf2c3c16
TT
5704 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5705 complaint (&symfile_complaints,
5706 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5707
5708 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5709 comp_dir, abfd, cu,
5710 &dwarf2_per_objfile->macro, 1);
5711 }
5712 else
5713 {
5714 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5715 if (attr && cu->line_header)
5716 {
5717 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5718
cf2c3c16
TT
5719 dwarf_decode_macros (cu->line_header, macro_offset,
5720 comp_dir, abfd, cu,
5721 &dwarf2_per_objfile->macinfo, 0);
5722 }
2e276125 5723 }
debd256d 5724 do_cleanups (back_to);
5fb290d7
DJ
5725}
5726
ae2de4f8
DE
5727/* Process DW_TAG_type_unit.
5728 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5729 actual type being defined by this TU. In this case the first top
5730 level sibling is there to provide context only. */
5731
5732static void
5733read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5734{
5735 struct objfile *objfile = cu->objfile;
5736 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5737 CORE_ADDR lowpc;
5738 struct attribute *attr;
5739 char *name = NULL;
5740 char *comp_dir = NULL;
5741 struct die_info *child_die;
5742 bfd *abfd = objfile->obfd;
348e048f
DE
5743
5744 /* start_symtab needs a low pc, but we don't really have one.
5745 Do what read_file_scope would do in the absence of such info. */
5746 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5747
5748 /* Find the filename. Do not use dwarf2_name here, since the filename
5749 is not a source language identifier. */
5750 attr = dwarf2_attr (die, DW_AT_name, cu);
5751 if (attr)
5752 name = DW_STRING (attr);
5753
5754 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5755 if (attr)
5756 comp_dir = DW_STRING (attr);
5757 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5758 {
5759 comp_dir = ldirname (name);
5760 if (comp_dir != NULL)
5761 make_cleanup (xfree, comp_dir);
5762 }
5763
5764 if (name == NULL)
5765 name = "<unknown>";
5766
5767 attr = dwarf2_attr (die, DW_AT_language, cu);
5768 if (attr)
5769 set_cu_language (DW_UNSND (attr), cu);
5770
5771 /* This isn't technically needed today. It is done for symmetry
5772 with read_file_scope. */
5773 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5774 if (attr)
348e048f
DE
5775 cu->producer = DW_STRING (attr);
5776
0963b4bd 5777 /* We assume that we're processing GCC output. */
348e048f
DE
5778 processing_gcc_compilation = 2;
5779
5780 processing_has_namespace_info = 0;
5781
5782 start_symtab (name, comp_dir, lowpc);
5783 record_debugformat ("DWARF 2");
5784 record_producer (cu->producer);
5785
2ab95328
TT
5786 handle_DW_AT_stmt_list (die, cu, comp_dir);
5787
348e048f
DE
5788 /* Process the dies in the type unit. */
5789 if (die->child == NULL)
5790 {
5791 dump_die_for_error (die);
5792 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5793 bfd_get_filename (abfd));
5794 }
5795
5796 child_die = die->child;
5797
5798 while (child_die && child_die->tag)
5799 {
5800 process_die (child_die, cu);
5801
5802 child_die = sibling_die (child_die);
5803 }
5804
5805 do_cleanups (back_to);
5806}
5807
5fb290d7 5808static void
e142c38c
DJ
5809add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5810 struct dwarf2_cu *cu)
5fb290d7
DJ
5811{
5812 struct function_range *thisfn;
5813
5814 thisfn = (struct function_range *)
7b5a2f43 5815 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5816 thisfn->name = name;
5817 thisfn->lowpc = lowpc;
5818 thisfn->highpc = highpc;
5819 thisfn->seen_line = 0;
5820 thisfn->next = NULL;
5821
e142c38c
DJ
5822 if (cu->last_fn == NULL)
5823 cu->first_fn = thisfn;
5fb290d7 5824 else
e142c38c 5825 cu->last_fn->next = thisfn;
5fb290d7 5826
e142c38c 5827 cu->last_fn = thisfn;
c906108c
SS
5828}
5829
d389af10
JK
5830/* qsort helper for inherit_abstract_dies. */
5831
5832static int
5833unsigned_int_compar (const void *ap, const void *bp)
5834{
5835 unsigned int a = *(unsigned int *) ap;
5836 unsigned int b = *(unsigned int *) bp;
5837
5838 return (a > b) - (b > a);
5839}
5840
5841/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5842 Inherit only the children of the DW_AT_abstract_origin DIE not being
5843 already referenced by DW_AT_abstract_origin from the children of the
5844 current DIE. */
d389af10
JK
5845
5846static void
5847inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5848{
5849 struct die_info *child_die;
5850 unsigned die_children_count;
5851 /* CU offsets which were referenced by children of the current DIE. */
5852 unsigned *offsets;
5853 unsigned *offsets_end, *offsetp;
5854 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5855 struct die_info *origin_die;
5856 /* Iterator of the ORIGIN_DIE children. */
5857 struct die_info *origin_child_die;
5858 struct cleanup *cleanups;
5859 struct attribute *attr;
cd02d79d
PA
5860 struct dwarf2_cu *origin_cu;
5861 struct pending **origin_previous_list_in_scope;
d389af10
JK
5862
5863 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5864 if (!attr)
5865 return;
5866
cd02d79d
PA
5867 /* Note that following die references may follow to a die in a
5868 different cu. */
5869
5870 origin_cu = cu;
5871 origin_die = follow_die_ref (die, attr, &origin_cu);
5872
5873 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5874 symbols in. */
5875 origin_previous_list_in_scope = origin_cu->list_in_scope;
5876 origin_cu->list_in_scope = cu->list_in_scope;
5877
edb3359d
DJ
5878 if (die->tag != origin_die->tag
5879 && !(die->tag == DW_TAG_inlined_subroutine
5880 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5881 complaint (&symfile_complaints,
5882 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5883 die->offset, origin_die->offset);
5884
5885 child_die = die->child;
5886 die_children_count = 0;
5887 while (child_die && child_die->tag)
5888 {
5889 child_die = sibling_die (child_die);
5890 die_children_count++;
5891 }
5892 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5893 cleanups = make_cleanup (xfree, offsets);
5894
5895 offsets_end = offsets;
5896 child_die = die->child;
5897 while (child_die && child_die->tag)
5898 {
c38f313d
DJ
5899 /* For each CHILD_DIE, find the corresponding child of
5900 ORIGIN_DIE. If there is more than one layer of
5901 DW_AT_abstract_origin, follow them all; there shouldn't be,
5902 but GCC versions at least through 4.4 generate this (GCC PR
5903 40573). */
5904 struct die_info *child_origin_die = child_die;
cd02d79d 5905 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5906
c38f313d
DJ
5907 while (1)
5908 {
cd02d79d
PA
5909 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5910 child_origin_cu);
c38f313d
DJ
5911 if (attr == NULL)
5912 break;
cd02d79d
PA
5913 child_origin_die = follow_die_ref (child_origin_die, attr,
5914 &child_origin_cu);
c38f313d
DJ
5915 }
5916
d389af10
JK
5917 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5918 counterpart may exist. */
c38f313d 5919 if (child_origin_die != child_die)
d389af10 5920 {
edb3359d
DJ
5921 if (child_die->tag != child_origin_die->tag
5922 && !(child_die->tag == DW_TAG_inlined_subroutine
5923 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5924 complaint (&symfile_complaints,
5925 _("Child DIE 0x%x and its abstract origin 0x%x have "
5926 "different tags"), child_die->offset,
5927 child_origin_die->offset);
c38f313d
DJ
5928 if (child_origin_die->parent != origin_die)
5929 complaint (&symfile_complaints,
5930 _("Child DIE 0x%x and its abstract origin 0x%x have "
5931 "different parents"), child_die->offset,
5932 child_origin_die->offset);
5933 else
5934 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5935 }
5936 child_die = sibling_die (child_die);
5937 }
5938 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5939 unsigned_int_compar);
5940 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5941 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5942 complaint (&symfile_complaints,
5943 _("Multiple children of DIE 0x%x refer "
5944 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5945 die->offset, *offsetp);
5946
5947 offsetp = offsets;
5948 origin_child_die = origin_die->child;
5949 while (origin_child_die && origin_child_die->tag)
5950 {
5951 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5952 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5953 offsetp++;
5954 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5955 {
5956 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5957 process_die (origin_child_die, origin_cu);
d389af10
JK
5958 }
5959 origin_child_die = sibling_die (origin_child_die);
5960 }
cd02d79d 5961 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5962
5963 do_cleanups (cleanups);
5964}
5965
c906108c 5966static void
e7c27a73 5967read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5968{
e7c27a73 5969 struct objfile *objfile = cu->objfile;
52f0bd74 5970 struct context_stack *new;
c906108c
SS
5971 CORE_ADDR lowpc;
5972 CORE_ADDR highpc;
5973 struct die_info *child_die;
edb3359d 5974 struct attribute *attr, *call_line, *call_file;
c906108c 5975 char *name;
e142c38c 5976 CORE_ADDR baseaddr;
801e3a5b 5977 struct block *block;
edb3359d 5978 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5979 VEC (symbolp) *template_args = NULL;
5980 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5981
5982 if (inlined_func)
5983 {
5984 /* If we do not have call site information, we can't show the
5985 caller of this inlined function. That's too confusing, so
5986 only use the scope for local variables. */
5987 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5988 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5989 if (call_line == NULL || call_file == NULL)
5990 {
5991 read_lexical_block_scope (die, cu);
5992 return;
5993 }
5994 }
c906108c 5995
e142c38c
DJ
5996 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5997
94af9270 5998 name = dwarf2_name (die, cu);
c906108c 5999
e8d05480
JB
6000 /* Ignore functions with missing or empty names. These are actually
6001 illegal according to the DWARF standard. */
6002 if (name == NULL)
6003 {
6004 complaint (&symfile_complaints,
6005 _("missing name for subprogram DIE at %d"), die->offset);
6006 return;
6007 }
6008
6009 /* Ignore functions with missing or invalid low and high pc attributes. */
6010 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6011 {
ae4d0c03
PM
6012 attr = dwarf2_attr (die, DW_AT_external, cu);
6013 if (!attr || !DW_UNSND (attr))
6014 complaint (&symfile_complaints,
3e43a32a
MS
6015 _("cannot get low and high bounds "
6016 "for subprogram DIE at %d"),
ae4d0c03 6017 die->offset);
e8d05480
JB
6018 return;
6019 }
c906108c
SS
6020
6021 lowpc += baseaddr;
6022 highpc += baseaddr;
6023
5fb290d7 6024 /* Record the function range for dwarf_decode_lines. */
e142c38c 6025 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 6026
34eaf542
TT
6027 /* If we have any template arguments, then we must allocate a
6028 different sort of symbol. */
6029 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6030 {
6031 if (child_die->tag == DW_TAG_template_type_param
6032 || child_die->tag == DW_TAG_template_value_param)
6033 {
6034 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6035 struct template_symbol);
6036 templ_func->base.is_cplus_template_function = 1;
6037 break;
6038 }
6039 }
6040
c906108c 6041 new = push_context (0, lowpc);
34eaf542
TT
6042 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6043 (struct symbol *) templ_func);
4c2df51b 6044
4cecd739
DJ
6045 /* If there is a location expression for DW_AT_frame_base, record
6046 it. */
e142c38c 6047 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 6048 if (attr)
c034e007
AC
6049 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6050 expression is being recorded directly in the function's symbol
6051 and not in a separate frame-base object. I guess this hack is
6052 to avoid adding some sort of frame-base adjunct/annex to the
6053 function's symbol :-(. The problem with doing this is that it
6054 results in a function symbol with a location expression that
6055 has nothing to do with the location of the function, ouch! The
6056 relationship should be: a function's symbol has-a frame base; a
6057 frame-base has-a location expression. */
e7c27a73 6058 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 6059
e142c38c 6060 cu->list_in_scope = &local_symbols;
c906108c 6061
639d11d3 6062 if (die->child != NULL)
c906108c 6063 {
639d11d3 6064 child_die = die->child;
c906108c
SS
6065 while (child_die && child_die->tag)
6066 {
34eaf542
TT
6067 if (child_die->tag == DW_TAG_template_type_param
6068 || child_die->tag == DW_TAG_template_value_param)
6069 {
6070 struct symbol *arg = new_symbol (child_die, NULL, cu);
6071
f1078f66
DJ
6072 if (arg != NULL)
6073 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6074 }
6075 else
6076 process_die (child_die, cu);
c906108c
SS
6077 child_die = sibling_die (child_die);
6078 }
6079 }
6080
d389af10
JK
6081 inherit_abstract_dies (die, cu);
6082
4a811a97
UW
6083 /* If we have a DW_AT_specification, we might need to import using
6084 directives from the context of the specification DIE. See the
6085 comment in determine_prefix. */
6086 if (cu->language == language_cplus
6087 && dwarf2_attr (die, DW_AT_specification, cu))
6088 {
6089 struct dwarf2_cu *spec_cu = cu;
6090 struct die_info *spec_die = die_specification (die, &spec_cu);
6091
6092 while (spec_die)
6093 {
6094 child_die = spec_die->child;
6095 while (child_die && child_die->tag)
6096 {
6097 if (child_die->tag == DW_TAG_imported_module)
6098 process_die (child_die, spec_cu);
6099 child_die = sibling_die (child_die);
6100 }
6101
6102 /* In some cases, GCC generates specification DIEs that
6103 themselves contain DW_AT_specification attributes. */
6104 spec_die = die_specification (spec_die, &spec_cu);
6105 }
6106 }
6107
c906108c
SS
6108 new = pop_context ();
6109 /* Make a block for the local symbols within. */
801e3a5b
JB
6110 block = finish_block (new->name, &local_symbols, new->old_blocks,
6111 lowpc, highpc, objfile);
6112
df8a16a1 6113 /* For C++, set the block's scope. */
f55ee35c 6114 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6115 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6116 determine_prefix (die, cu),
df8a16a1
DJ
6117 processing_has_namespace_info);
6118
801e3a5b
JB
6119 /* If we have address ranges, record them. */
6120 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6121
34eaf542
TT
6122 /* Attach template arguments to function. */
6123 if (! VEC_empty (symbolp, template_args))
6124 {
6125 gdb_assert (templ_func != NULL);
6126
6127 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6128 templ_func->template_arguments
6129 = obstack_alloc (&objfile->objfile_obstack,
6130 (templ_func->n_template_arguments
6131 * sizeof (struct symbol *)));
6132 memcpy (templ_func->template_arguments,
6133 VEC_address (symbolp, template_args),
6134 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6135 VEC_free (symbolp, template_args);
6136 }
6137
208d8187
JB
6138 /* In C++, we can have functions nested inside functions (e.g., when
6139 a function declares a class that has methods). This means that
6140 when we finish processing a function scope, we may need to go
6141 back to building a containing block's symbol lists. */
6142 local_symbols = new->locals;
6143 param_symbols = new->params;
27aa8d6a 6144 using_directives = new->using_directives;
208d8187 6145
921e78cf
JB
6146 /* If we've finished processing a top-level function, subsequent
6147 symbols go in the file symbol list. */
6148 if (outermost_context_p ())
e142c38c 6149 cu->list_in_scope = &file_symbols;
c906108c
SS
6150}
6151
6152/* Process all the DIES contained within a lexical block scope. Start
6153 a new scope, process the dies, and then close the scope. */
6154
6155static void
e7c27a73 6156read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6157{
e7c27a73 6158 struct objfile *objfile = cu->objfile;
52f0bd74 6159 struct context_stack *new;
c906108c
SS
6160 CORE_ADDR lowpc, highpc;
6161 struct die_info *child_die;
e142c38c
DJ
6162 CORE_ADDR baseaddr;
6163
6164 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6165
6166 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6167 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6168 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6169 be nasty. Might be easier to properly extend generic blocks to
af34e669 6170 describe ranges. */
d85a05f0 6171 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6172 return;
6173 lowpc += baseaddr;
6174 highpc += baseaddr;
6175
6176 push_context (0, lowpc);
639d11d3 6177 if (die->child != NULL)
c906108c 6178 {
639d11d3 6179 child_die = die->child;
c906108c
SS
6180 while (child_die && child_die->tag)
6181 {
e7c27a73 6182 process_die (child_die, cu);
c906108c
SS
6183 child_die = sibling_die (child_die);
6184 }
6185 }
6186 new = pop_context ();
6187
8540c487 6188 if (local_symbols != NULL || using_directives != NULL)
c906108c 6189 {
801e3a5b
JB
6190 struct block *block
6191 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6192 highpc, objfile);
6193
6194 /* Note that recording ranges after traversing children, as we
6195 do here, means that recording a parent's ranges entails
6196 walking across all its children's ranges as they appear in
6197 the address map, which is quadratic behavior.
6198
6199 It would be nicer to record the parent's ranges before
6200 traversing its children, simply overriding whatever you find
6201 there. But since we don't even decide whether to create a
6202 block until after we've traversed its children, that's hard
6203 to do. */
6204 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6205 }
6206 local_symbols = new->locals;
27aa8d6a 6207 using_directives = new->using_directives;
c906108c
SS
6208}
6209
96408a79
SA
6210/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6211
6212static void
6213read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6214{
6215 struct objfile *objfile = cu->objfile;
6216 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6217 CORE_ADDR pc, baseaddr;
6218 struct attribute *attr;
6219 struct call_site *call_site, call_site_local;
6220 void **slot;
6221 int nparams;
6222 struct die_info *child_die;
6223
6224 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6225
6226 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6227 if (!attr)
6228 {
6229 complaint (&symfile_complaints,
6230 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6231 "DIE 0x%x [in module %s]"),
6232 die->offset, cu->objfile->name);
6233 return;
6234 }
6235 pc = DW_ADDR (attr) + baseaddr;
6236
6237 if (cu->call_site_htab == NULL)
6238 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6239 NULL, &objfile->objfile_obstack,
6240 hashtab_obstack_allocate, NULL);
6241 call_site_local.pc = pc;
6242 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6243 if (*slot != NULL)
6244 {
6245 complaint (&symfile_complaints,
6246 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6247 "DIE 0x%x [in module %s]"),
6248 paddress (gdbarch, pc), die->offset, cu->objfile->name);
6249 return;
6250 }
6251
6252 /* Count parameters at the caller. */
6253
6254 nparams = 0;
6255 for (child_die = die->child; child_die && child_die->tag;
6256 child_die = sibling_die (child_die))
6257 {
6258 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6259 {
6260 complaint (&symfile_complaints,
6261 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6262 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6263 child_die->tag, child_die->offset, cu->objfile->name);
6264 continue;
6265 }
6266
6267 nparams++;
6268 }
6269
6270 call_site = obstack_alloc (&objfile->objfile_obstack,
6271 (sizeof (*call_site)
6272 + (sizeof (*call_site->parameter)
6273 * (nparams - 1))));
6274 *slot = call_site;
6275 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6276 call_site->pc = pc;
6277
6278 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6279 {
6280 struct die_info *func_die;
6281
6282 /* Skip also over DW_TAG_inlined_subroutine. */
6283 for (func_die = die->parent;
6284 func_die && func_die->tag != DW_TAG_subprogram
6285 && func_die->tag != DW_TAG_subroutine_type;
6286 func_die = func_die->parent);
6287
6288 /* DW_AT_GNU_all_call_sites is a superset
6289 of DW_AT_GNU_all_tail_call_sites. */
6290 if (func_die
6291 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6292 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6293 {
6294 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6295 not complete. But keep CALL_SITE for look ups via call_site_htab,
6296 both the initial caller containing the real return address PC and
6297 the final callee containing the current PC of a chain of tail
6298 calls do not need to have the tail call list complete. But any
6299 function candidate for a virtual tail call frame searched via
6300 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6301 determined unambiguously. */
6302 }
6303 else
6304 {
6305 struct type *func_type = NULL;
6306
6307 if (func_die)
6308 func_type = get_die_type (func_die, cu);
6309 if (func_type != NULL)
6310 {
6311 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6312
6313 /* Enlist this call site to the function. */
6314 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6315 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6316 }
6317 else
6318 complaint (&symfile_complaints,
6319 _("Cannot find function owning DW_TAG_GNU_call_site "
6320 "DIE 0x%x [in module %s]"),
6321 die->offset, cu->objfile->name);
6322 }
6323 }
6324
6325 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6326 if (attr == NULL)
6327 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6328 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6329 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6330 /* Keep NULL DWARF_BLOCK. */;
6331 else if (attr_form_is_block (attr))
6332 {
6333 struct dwarf2_locexpr_baton *dlbaton;
6334
6335 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6336 dlbaton->data = DW_BLOCK (attr)->data;
6337 dlbaton->size = DW_BLOCK (attr)->size;
6338 dlbaton->per_cu = cu->per_cu;
6339
6340 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6341 }
6342 else if (is_ref_attr (attr))
6343 {
6344 struct objfile *objfile = cu->objfile;
6345 struct dwarf2_cu *target_cu = cu;
6346 struct die_info *target_die;
6347
6348 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6349 gdb_assert (target_cu->objfile == objfile);
6350 if (die_is_declaration (target_die, target_cu))
6351 {
6352 const char *target_physname;
6353
6354 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6355 if (target_physname == NULL)
6356 complaint (&symfile_complaints,
6357 _("DW_AT_GNU_call_site_target target DIE has invalid "
6358 "physname, for referencing DIE 0x%x [in module %s]"),
6359 die->offset, cu->objfile->name);
6360 else
6361 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6362 }
6363 else
6364 {
6365 CORE_ADDR lowpc;
6366
6367 /* DW_AT_entry_pc should be preferred. */
6368 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6369 complaint (&symfile_complaints,
6370 _("DW_AT_GNU_call_site_target target DIE has invalid "
6371 "low pc, for referencing DIE 0x%x [in module %s]"),
6372 die->offset, cu->objfile->name);
6373 else
6374 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6375 }
6376 }
6377 else
6378 complaint (&symfile_complaints,
6379 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6380 "block nor reference, for DIE 0x%x [in module %s]"),
6381 die->offset, cu->objfile->name);
6382
6383 call_site->per_cu = cu->per_cu;
6384
6385 for (child_die = die->child;
6386 child_die && child_die->tag;
6387 child_die = sibling_die (child_die))
6388 {
6389 struct dwarf2_locexpr_baton *dlbaton;
6390 struct call_site_parameter *parameter;
6391
6392 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6393 {
6394 /* Already printed the complaint above. */
6395 continue;
6396 }
6397
6398 gdb_assert (call_site->parameter_count < nparams);
6399 parameter = &call_site->parameter[call_site->parameter_count];
6400
6401 /* DW_AT_location specifies the register number. Value of the data
6402 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6403
6404 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6405 if (!attr || !attr_form_is_block (attr))
6406 {
6407 complaint (&symfile_complaints,
6408 _("No DW_FORM_block* DW_AT_location for "
6409 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6410 child_die->offset, cu->objfile->name);
6411 continue;
6412 }
6413 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6414 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6415 if (parameter->dwarf_reg == -1
6416 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6417 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6418 &parameter->fb_offset))
6419 {
6420 complaint (&symfile_complaints,
6421 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6422 "for DW_FORM_block* DW_AT_location for "
6423 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6424 child_die->offset, cu->objfile->name);
6425 continue;
6426 }
6427
6428 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6429 if (!attr_form_is_block (attr))
6430 {
6431 complaint (&symfile_complaints,
6432 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6433 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6434 child_die->offset, cu->objfile->name);
6435 continue;
6436 }
6437 parameter->value = DW_BLOCK (attr)->data;
6438 parameter->value_size = DW_BLOCK (attr)->size;
6439
6440 /* Parameters are not pre-cleared by memset above. */
6441 parameter->data_value = NULL;
6442 parameter->data_value_size = 0;
6443 call_site->parameter_count++;
6444
6445 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6446 if (attr)
6447 {
6448 if (!attr_form_is_block (attr))
6449 complaint (&symfile_complaints,
6450 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6451 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6452 child_die->offset, cu->objfile->name);
6453 else
6454 {
6455 parameter->data_value = DW_BLOCK (attr)->data;
6456 parameter->data_value_size = DW_BLOCK (attr)->size;
6457 }
6458 }
6459 }
6460}
6461
43039443 6462/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6463 Return 1 if the attributes are present and valid, otherwise, return 0.
6464 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6465
6466static int
6467dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6468 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6469 struct partial_symtab *ranges_pst)
43039443
JK
6470{
6471 struct objfile *objfile = cu->objfile;
6472 struct comp_unit_head *cu_header = &cu->header;
6473 bfd *obfd = objfile->obfd;
6474 unsigned int addr_size = cu_header->addr_size;
6475 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6476 /* Base address selection entry. */
6477 CORE_ADDR base;
6478 int found_base;
6479 unsigned int dummy;
6480 gdb_byte *buffer;
6481 CORE_ADDR marker;
6482 int low_set;
6483 CORE_ADDR low = 0;
6484 CORE_ADDR high = 0;
ff013f42 6485 CORE_ADDR baseaddr;
43039443 6486
d00adf39
DE
6487 found_base = cu->base_known;
6488 base = cu->base_address;
43039443 6489
be391dca 6490 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6491 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6492 {
6493 complaint (&symfile_complaints,
6494 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6495 offset);
6496 return 0;
6497 }
dce234bc 6498 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6499
6500 /* Read in the largest possible address. */
6501 marker = read_address (obfd, buffer, cu, &dummy);
6502 if ((marker & mask) == mask)
6503 {
6504 /* If we found the largest possible address, then
6505 read the base address. */
6506 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6507 buffer += 2 * addr_size;
6508 offset += 2 * addr_size;
6509 found_base = 1;
6510 }
6511
6512 low_set = 0;
6513
e7030f15 6514 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6515
43039443
JK
6516 while (1)
6517 {
6518 CORE_ADDR range_beginning, range_end;
6519
6520 range_beginning = read_address (obfd, buffer, cu, &dummy);
6521 buffer += addr_size;
6522 range_end = read_address (obfd, buffer, cu, &dummy);
6523 buffer += addr_size;
6524 offset += 2 * addr_size;
6525
6526 /* An end of list marker is a pair of zero addresses. */
6527 if (range_beginning == 0 && range_end == 0)
6528 /* Found the end of list entry. */
6529 break;
6530
6531 /* Each base address selection entry is a pair of 2 values.
6532 The first is the largest possible address, the second is
6533 the base address. Check for a base address here. */
6534 if ((range_beginning & mask) == mask)
6535 {
6536 /* If we found the largest possible address, then
6537 read the base address. */
6538 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6539 found_base = 1;
6540 continue;
6541 }
6542
6543 if (!found_base)
6544 {
6545 /* We have no valid base address for the ranges
6546 data. */
6547 complaint (&symfile_complaints,
6548 _("Invalid .debug_ranges data (no base address)"));
6549 return 0;
6550 }
6551
9277c30c
UW
6552 if (range_beginning > range_end)
6553 {
6554 /* Inverted range entries are invalid. */
6555 complaint (&symfile_complaints,
6556 _("Invalid .debug_ranges data (inverted range)"));
6557 return 0;
6558 }
6559
6560 /* Empty range entries have no effect. */
6561 if (range_beginning == range_end)
6562 continue;
6563
43039443
JK
6564 range_beginning += base;
6565 range_end += base;
6566
9277c30c 6567 if (ranges_pst != NULL)
ff013f42 6568 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6569 range_beginning + baseaddr,
6570 range_end - 1 + baseaddr,
ff013f42
JK
6571 ranges_pst);
6572
43039443
JK
6573 /* FIXME: This is recording everything as a low-high
6574 segment of consecutive addresses. We should have a
6575 data structure for discontiguous block ranges
6576 instead. */
6577 if (! low_set)
6578 {
6579 low = range_beginning;
6580 high = range_end;
6581 low_set = 1;
6582 }
6583 else
6584 {
6585 if (range_beginning < low)
6586 low = range_beginning;
6587 if (range_end > high)
6588 high = range_end;
6589 }
6590 }
6591
6592 if (! low_set)
6593 /* If the first entry is an end-of-list marker, the range
6594 describes an empty scope, i.e. no instructions. */
6595 return 0;
6596
6597 if (low_return)
6598 *low_return = low;
6599 if (high_return)
6600 *high_return = high;
6601 return 1;
6602}
6603
af34e669
DJ
6604/* Get low and high pc attributes from a die. Return 1 if the attributes
6605 are present and valid, otherwise, return 0. Return -1 if the range is
6606 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6607static int
af34e669 6608dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6609 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6610 struct partial_symtab *pst)
c906108c
SS
6611{
6612 struct attribute *attr;
af34e669
DJ
6613 CORE_ADDR low = 0;
6614 CORE_ADDR high = 0;
6615 int ret = 0;
c906108c 6616
e142c38c 6617 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6618 if (attr)
af34e669
DJ
6619 {
6620 high = DW_ADDR (attr);
e142c38c 6621 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6622 if (attr)
6623 low = DW_ADDR (attr);
6624 else
6625 /* Found high w/o low attribute. */
6626 return 0;
6627
6628 /* Found consecutive range of addresses. */
6629 ret = 1;
6630 }
c906108c 6631 else
af34e669 6632 {
e142c38c 6633 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6634 if (attr != NULL)
6635 {
af34e669 6636 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6637 .debug_ranges section. */
d85a05f0 6638 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6639 return 0;
43039443 6640 /* Found discontinuous range of addresses. */
af34e669
DJ
6641 ret = -1;
6642 }
6643 }
c906108c 6644
9373cf26
JK
6645 /* read_partial_die has also the strict LOW < HIGH requirement. */
6646 if (high <= low)
c906108c
SS
6647 return 0;
6648
6649 /* When using the GNU linker, .gnu.linkonce. sections are used to
6650 eliminate duplicate copies of functions and vtables and such.
6651 The linker will arbitrarily choose one and discard the others.
6652 The AT_*_pc values for such functions refer to local labels in
6653 these sections. If the section from that file was discarded, the
6654 labels are not in the output, so the relocs get a value of 0.
6655 If this is a discarded function, mark the pc bounds as invalid,
6656 so that GDB will ignore it. */
72dca2f5 6657 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6658 return 0;
6659
6660 *lowpc = low;
96408a79
SA
6661 if (highpc)
6662 *highpc = high;
af34e669 6663 return ret;
c906108c
SS
6664}
6665
b084d499
JB
6666/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6667 its low and high PC addresses. Do nothing if these addresses could not
6668 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6669 and HIGHPC to the high address if greater than HIGHPC. */
6670
6671static void
6672dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6673 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6674 struct dwarf2_cu *cu)
6675{
6676 CORE_ADDR low, high;
6677 struct die_info *child = die->child;
6678
d85a05f0 6679 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6680 {
6681 *lowpc = min (*lowpc, low);
6682 *highpc = max (*highpc, high);
6683 }
6684
6685 /* If the language does not allow nested subprograms (either inside
6686 subprograms or lexical blocks), we're done. */
6687 if (cu->language != language_ada)
6688 return;
6e70227d 6689
b084d499
JB
6690 /* Check all the children of the given DIE. If it contains nested
6691 subprograms, then check their pc bounds. Likewise, we need to
6692 check lexical blocks as well, as they may also contain subprogram
6693 definitions. */
6694 while (child && child->tag)
6695 {
6696 if (child->tag == DW_TAG_subprogram
6697 || child->tag == DW_TAG_lexical_block)
6698 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6699 child = sibling_die (child);
6700 }
6701}
6702
fae299cd
DC
6703/* Get the low and high pc's represented by the scope DIE, and store
6704 them in *LOWPC and *HIGHPC. If the correct values can't be
6705 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6706
6707static void
6708get_scope_pc_bounds (struct die_info *die,
6709 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6710 struct dwarf2_cu *cu)
6711{
6712 CORE_ADDR best_low = (CORE_ADDR) -1;
6713 CORE_ADDR best_high = (CORE_ADDR) 0;
6714 CORE_ADDR current_low, current_high;
6715
d85a05f0 6716 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6717 {
6718 best_low = current_low;
6719 best_high = current_high;
6720 }
6721 else
6722 {
6723 struct die_info *child = die->child;
6724
6725 while (child && child->tag)
6726 {
6727 switch (child->tag) {
6728 case DW_TAG_subprogram:
b084d499 6729 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6730 break;
6731 case DW_TAG_namespace:
f55ee35c 6732 case DW_TAG_module:
fae299cd
DC
6733 /* FIXME: carlton/2004-01-16: Should we do this for
6734 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6735 that current GCC's always emit the DIEs corresponding
6736 to definitions of methods of classes as children of a
6737 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6738 the DIEs giving the declarations, which could be
6739 anywhere). But I don't see any reason why the
6740 standards says that they have to be there. */
6741 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6742
6743 if (current_low != ((CORE_ADDR) -1))
6744 {
6745 best_low = min (best_low, current_low);
6746 best_high = max (best_high, current_high);
6747 }
6748 break;
6749 default:
0963b4bd 6750 /* Ignore. */
fae299cd
DC
6751 break;
6752 }
6753
6754 child = sibling_die (child);
6755 }
6756 }
6757
6758 *lowpc = best_low;
6759 *highpc = best_high;
6760}
6761
801e3a5b
JB
6762/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6763 in DIE. */
6764static void
6765dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6766 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6767{
6768 struct attribute *attr;
6769
6770 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6771 if (attr)
6772 {
6773 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6774
801e3a5b
JB
6775 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6776 if (attr)
6777 {
6778 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6779
801e3a5b
JB
6780 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6781 }
6782 }
6783
6784 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6785 if (attr)
6786 {
6787 bfd *obfd = cu->objfile->obfd;
6788
6789 /* The value of the DW_AT_ranges attribute is the offset of the
6790 address range list in the .debug_ranges section. */
6791 unsigned long offset = DW_UNSND (attr);
dce234bc 6792 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6793
6794 /* For some target architectures, but not others, the
6795 read_address function sign-extends the addresses it returns.
6796 To recognize base address selection entries, we need a
6797 mask. */
6798 unsigned int addr_size = cu->header.addr_size;
6799 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6800
6801 /* The base address, to which the next pair is relative. Note
6802 that this 'base' is a DWARF concept: most entries in a range
6803 list are relative, to reduce the number of relocs against the
6804 debugging information. This is separate from this function's
6805 'baseaddr' argument, which GDB uses to relocate debugging
6806 information from a shared library based on the address at
6807 which the library was loaded. */
d00adf39
DE
6808 CORE_ADDR base = cu->base_address;
6809 int base_known = cu->base_known;
801e3a5b 6810
be391dca 6811 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6812 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6813 {
6814 complaint (&symfile_complaints,
6815 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6816 offset);
6817 return;
6818 }
6819
6820 for (;;)
6821 {
6822 unsigned int bytes_read;
6823 CORE_ADDR start, end;
6824
6825 start = read_address (obfd, buffer, cu, &bytes_read);
6826 buffer += bytes_read;
6827 end = read_address (obfd, buffer, cu, &bytes_read);
6828 buffer += bytes_read;
6829
6830 /* Did we find the end of the range list? */
6831 if (start == 0 && end == 0)
6832 break;
6833
6834 /* Did we find a base address selection entry? */
6835 else if ((start & base_select_mask) == base_select_mask)
6836 {
6837 base = end;
6838 base_known = 1;
6839 }
6840
6841 /* We found an ordinary address range. */
6842 else
6843 {
6844 if (!base_known)
6845 {
6846 complaint (&symfile_complaints,
3e43a32a
MS
6847 _("Invalid .debug_ranges data "
6848 "(no base address)"));
801e3a5b
JB
6849 return;
6850 }
6851
9277c30c
UW
6852 if (start > end)
6853 {
6854 /* Inverted range entries are invalid. */
6855 complaint (&symfile_complaints,
6856 _("Invalid .debug_ranges data "
6857 "(inverted range)"));
6858 return;
6859 }
6860
6861 /* Empty range entries have no effect. */
6862 if (start == end)
6863 continue;
6864
6e70227d
DE
6865 record_block_range (block,
6866 baseaddr + base + start,
801e3a5b
JB
6867 baseaddr + base + end - 1);
6868 }
6869 }
6870 }
6871}
6872
60d5a603
JK
6873/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6874 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6875 during 4.6.0 experimental. */
6876
6877static int
6878producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6879{
6880 const char *cs;
6881 int major, minor, release;
6882
6883 if (cu->producer == NULL)
6884 {
6885 /* For unknown compilers expect their behavior is DWARF version
6886 compliant.
6887
6888 GCC started to support .debug_types sections by -gdwarf-4 since
6889 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6890 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6891 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6892 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6893
6894 return 0;
6895 }
6896
6897 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6898
6899 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6900 {
6901 /* For non-GCC compilers expect their behavior is DWARF version
6902 compliant. */
6903
6904 return 0;
6905 }
6906 cs = &cu->producer[strlen ("GNU ")];
6907 while (*cs && !isdigit (*cs))
6908 cs++;
6909 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6910 {
6911 /* Not recognized as GCC. */
6912
6913 return 0;
6914 }
6915
6916 return major < 4 || (major == 4 && minor < 6);
6917}
6918
6919/* Return the default accessibility type if it is not overriden by
6920 DW_AT_accessibility. */
6921
6922static enum dwarf_access_attribute
6923dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6924{
6925 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6926 {
6927 /* The default DWARF 2 accessibility for members is public, the default
6928 accessibility for inheritance is private. */
6929
6930 if (die->tag != DW_TAG_inheritance)
6931 return DW_ACCESS_public;
6932 else
6933 return DW_ACCESS_private;
6934 }
6935 else
6936 {
6937 /* DWARF 3+ defines the default accessibility a different way. The same
6938 rules apply now for DW_TAG_inheritance as for the members and it only
6939 depends on the container kind. */
6940
6941 if (die->parent->tag == DW_TAG_class_type)
6942 return DW_ACCESS_private;
6943 else
6944 return DW_ACCESS_public;
6945 }
6946}
6947
74ac6d43
TT
6948/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6949 offset. If the attribute was not found return 0, otherwise return
6950 1. If it was found but could not properly be handled, set *OFFSET
6951 to 0. */
6952
6953static int
6954handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6955 LONGEST *offset)
6956{
6957 struct attribute *attr;
6958
6959 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6960 if (attr != NULL)
6961 {
6962 *offset = 0;
6963
6964 /* Note that we do not check for a section offset first here.
6965 This is because DW_AT_data_member_location is new in DWARF 4,
6966 so if we see it, we can assume that a constant form is really
6967 a constant and not a section offset. */
6968 if (attr_form_is_constant (attr))
6969 *offset = dwarf2_get_attr_constant_value (attr, 0);
6970 else if (attr_form_is_section_offset (attr))
6971 dwarf2_complex_location_expr_complaint ();
6972 else if (attr_form_is_block (attr))
6973 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6974 else
6975 dwarf2_complex_location_expr_complaint ();
6976
6977 return 1;
6978 }
6979
6980 return 0;
6981}
6982
c906108c
SS
6983/* Add an aggregate field to the field list. */
6984
6985static void
107d2387 6986dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6987 struct dwarf2_cu *cu)
6e70227d 6988{
e7c27a73 6989 struct objfile *objfile = cu->objfile;
5e2b427d 6990 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6991 struct nextfield *new_field;
6992 struct attribute *attr;
6993 struct field *fp;
6994 char *fieldname = "";
6995
6996 /* Allocate a new field list entry and link it in. */
6997 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6998 make_cleanup (xfree, new_field);
c906108c 6999 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
7000
7001 if (die->tag == DW_TAG_inheritance)
7002 {
7003 new_field->next = fip->baseclasses;
7004 fip->baseclasses = new_field;
7005 }
7006 else
7007 {
7008 new_field->next = fip->fields;
7009 fip->fields = new_field;
7010 }
c906108c
SS
7011 fip->nfields++;
7012
e142c38c 7013 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
7014 if (attr)
7015 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
7016 else
7017 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
7018 if (new_field->accessibility != DW_ACCESS_public)
7019 fip->non_public_fields = 1;
60d5a603 7020
e142c38c 7021 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
7022 if (attr)
7023 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
7024 else
7025 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
7026
7027 fp = &new_field->field;
a9a9bd0f 7028
e142c38c 7029 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 7030 {
74ac6d43
TT
7031 LONGEST offset;
7032
a9a9bd0f 7033 /* Data member other than a C++ static data member. */
6e70227d 7034
c906108c 7035 /* Get type of field. */
e7c27a73 7036 fp->type = die_type (die, cu);
c906108c 7037
d6a843b5 7038 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 7039
c906108c 7040 /* Get bit size of field (zero if none). */
e142c38c 7041 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
7042 if (attr)
7043 {
7044 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7045 }
7046 else
7047 {
7048 FIELD_BITSIZE (*fp) = 0;
7049 }
7050
7051 /* Get bit offset of field. */
74ac6d43
TT
7052 if (handle_data_member_location (die, cu, &offset))
7053 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 7054 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
7055 if (attr)
7056 {
5e2b427d 7057 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
7058 {
7059 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
7060 additional bit offset from the MSB of the containing
7061 anonymous object to the MSB of the field. We don't
7062 have to do anything special since we don't need to
7063 know the size of the anonymous object. */
c906108c
SS
7064 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7065 }
7066 else
7067 {
7068 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
7069 MSB of the anonymous object, subtract off the number of
7070 bits from the MSB of the field to the MSB of the
7071 object, and then subtract off the number of bits of
7072 the field itself. The result is the bit offset of
7073 the LSB of the field. */
c906108c
SS
7074 int anonymous_size;
7075 int bit_offset = DW_UNSND (attr);
7076
e142c38c 7077 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7078 if (attr)
7079 {
7080 /* The size of the anonymous object containing
7081 the bit field is explicit, so use the
7082 indicated size (in bytes). */
7083 anonymous_size = DW_UNSND (attr);
7084 }
7085 else
7086 {
7087 /* The size of the anonymous object containing
7088 the bit field must be inferred from the type
7089 attribute of the data member containing the
7090 bit field. */
7091 anonymous_size = TYPE_LENGTH (fp->type);
7092 }
7093 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7094 - bit_offset - FIELD_BITSIZE (*fp);
7095 }
7096 }
7097
7098 /* Get name of field. */
39cbfefa
DJ
7099 fieldname = dwarf2_name (die, cu);
7100 if (fieldname == NULL)
7101 fieldname = "";
d8151005
DJ
7102
7103 /* The name is already allocated along with this objfile, so we don't
7104 need to duplicate it for the type. */
7105 fp->name = fieldname;
c906108c
SS
7106
7107 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7108 pointer or virtual base class pointer) to private. */
e142c38c 7109 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7110 {
d48cc9dd 7111 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7112 new_field->accessibility = DW_ACCESS_private;
7113 fip->non_public_fields = 1;
7114 }
7115 }
a9a9bd0f 7116 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7117 {
a9a9bd0f
DC
7118 /* C++ static member. */
7119
7120 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7121 is a declaration, but all versions of G++ as of this writing
7122 (so through at least 3.2.1) incorrectly generate
7123 DW_TAG_variable tags. */
6e70227d 7124
ff355380 7125 const char *physname;
c906108c 7126
a9a9bd0f 7127 /* Get name of field. */
39cbfefa
DJ
7128 fieldname = dwarf2_name (die, cu);
7129 if (fieldname == NULL)
c906108c
SS
7130 return;
7131
254e6b9e 7132 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7133 if (attr
7134 /* Only create a symbol if this is an external value.
7135 new_symbol checks this and puts the value in the global symbol
7136 table, which we want. If it is not external, new_symbol
7137 will try to put the value in cu->list_in_scope which is wrong. */
7138 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7139 {
7140 /* A static const member, not much different than an enum as far as
7141 we're concerned, except that we can support more types. */
7142 new_symbol (die, NULL, cu);
7143 }
7144
2df3850c 7145 /* Get physical name. */
ff355380 7146 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7147
d8151005
DJ
7148 /* The name is already allocated along with this objfile, so we don't
7149 need to duplicate it for the type. */
7150 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7151 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7152 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7153 }
7154 else if (die->tag == DW_TAG_inheritance)
7155 {
74ac6d43 7156 LONGEST offset;
d4b96c9a 7157
74ac6d43
TT
7158 /* C++ base class field. */
7159 if (handle_data_member_location (die, cu, &offset))
7160 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7161 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7162 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7163 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7164 fip->nbaseclasses++;
7165 }
7166}
7167
98751a41
JK
7168/* Add a typedef defined in the scope of the FIP's class. */
7169
7170static void
7171dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7172 struct dwarf2_cu *cu)
6e70227d 7173{
98751a41 7174 struct objfile *objfile = cu->objfile;
98751a41
JK
7175 struct typedef_field_list *new_field;
7176 struct attribute *attr;
7177 struct typedef_field *fp;
7178 char *fieldname = "";
7179
7180 /* Allocate a new field list entry and link it in. */
7181 new_field = xzalloc (sizeof (*new_field));
7182 make_cleanup (xfree, new_field);
7183
7184 gdb_assert (die->tag == DW_TAG_typedef);
7185
7186 fp = &new_field->field;
7187
7188 /* Get name of field. */
7189 fp->name = dwarf2_name (die, cu);
7190 if (fp->name == NULL)
7191 return;
7192
7193 fp->type = read_type_die (die, cu);
7194
7195 new_field->next = fip->typedef_field_list;
7196 fip->typedef_field_list = new_field;
7197 fip->typedef_field_list_count++;
7198}
7199
c906108c
SS
7200/* Create the vector of fields, and attach it to the type. */
7201
7202static void
fba45db2 7203dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7204 struct dwarf2_cu *cu)
c906108c
SS
7205{
7206 int nfields = fip->nfields;
7207
7208 /* Record the field count, allocate space for the array of fields,
7209 and create blank accessibility bitfields if necessary. */
7210 TYPE_NFIELDS (type) = nfields;
7211 TYPE_FIELDS (type) = (struct field *)
7212 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7213 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7214
b4ba55a1 7215 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7216 {
7217 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7218
7219 TYPE_FIELD_PRIVATE_BITS (type) =
7220 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7221 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7222
7223 TYPE_FIELD_PROTECTED_BITS (type) =
7224 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7225 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7226
774b6a14
TT
7227 TYPE_FIELD_IGNORE_BITS (type) =
7228 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7229 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7230 }
7231
7232 /* If the type has baseclasses, allocate and clear a bit vector for
7233 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7234 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7235 {
7236 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7237 unsigned char *pointer;
c906108c
SS
7238
7239 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7240 pointer = TYPE_ALLOC (type, num_bytes);
7241 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7242 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7243 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7244 }
7245
3e43a32a
MS
7246 /* Copy the saved-up fields into the field vector. Start from the head of
7247 the list, adding to the tail of the field array, so that they end up in
7248 the same order in the array in which they were added to the list. */
c906108c
SS
7249 while (nfields-- > 0)
7250 {
7d0ccb61
DJ
7251 struct nextfield *fieldp;
7252
7253 if (fip->fields)
7254 {
7255 fieldp = fip->fields;
7256 fip->fields = fieldp->next;
7257 }
7258 else
7259 {
7260 fieldp = fip->baseclasses;
7261 fip->baseclasses = fieldp->next;
7262 }
7263
7264 TYPE_FIELD (type, nfields) = fieldp->field;
7265 switch (fieldp->accessibility)
c906108c 7266 {
c5aa993b 7267 case DW_ACCESS_private:
b4ba55a1
JB
7268 if (cu->language != language_ada)
7269 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7270 break;
c906108c 7271
c5aa993b 7272 case DW_ACCESS_protected:
b4ba55a1
JB
7273 if (cu->language != language_ada)
7274 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7275 break;
c906108c 7276
c5aa993b
JM
7277 case DW_ACCESS_public:
7278 break;
c906108c 7279
c5aa993b
JM
7280 default:
7281 /* Unknown accessibility. Complain and treat it as public. */
7282 {
e2e0b3e5 7283 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7284 fieldp->accessibility);
c5aa993b
JM
7285 }
7286 break;
c906108c
SS
7287 }
7288 if (nfields < fip->nbaseclasses)
7289 {
7d0ccb61 7290 switch (fieldp->virtuality)
c906108c 7291 {
c5aa993b
JM
7292 case DW_VIRTUALITY_virtual:
7293 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7294 if (cu->language == language_ada)
a73c6dcd 7295 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7296 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7297 break;
c906108c
SS
7298 }
7299 }
c906108c
SS
7300 }
7301}
7302
c906108c
SS
7303/* Add a member function to the proper fieldlist. */
7304
7305static void
107d2387 7306dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7307 struct type *type, struct dwarf2_cu *cu)
c906108c 7308{
e7c27a73 7309 struct objfile *objfile = cu->objfile;
c906108c
SS
7310 struct attribute *attr;
7311 struct fnfieldlist *flp;
7312 int i;
7313 struct fn_field *fnp;
7314 char *fieldname;
c906108c 7315 struct nextfnfield *new_fnfield;
f792889a 7316 struct type *this_type;
60d5a603 7317 enum dwarf_access_attribute accessibility;
c906108c 7318
b4ba55a1 7319 if (cu->language == language_ada)
a73c6dcd 7320 error (_("unexpected member function in Ada type"));
b4ba55a1 7321
2df3850c 7322 /* Get name of member function. */
39cbfefa
DJ
7323 fieldname = dwarf2_name (die, cu);
7324 if (fieldname == NULL)
2df3850c 7325 return;
c906108c 7326
c906108c
SS
7327 /* Look up member function name in fieldlist. */
7328 for (i = 0; i < fip->nfnfields; i++)
7329 {
27bfe10e 7330 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7331 break;
7332 }
7333
7334 /* Create new list element if necessary. */
7335 if (i < fip->nfnfields)
7336 flp = &fip->fnfieldlists[i];
7337 else
7338 {
7339 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7340 {
7341 fip->fnfieldlists = (struct fnfieldlist *)
7342 xrealloc (fip->fnfieldlists,
7343 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7344 * sizeof (struct fnfieldlist));
c906108c 7345 if (fip->nfnfields == 0)
c13c43fd 7346 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7347 }
7348 flp = &fip->fnfieldlists[fip->nfnfields];
7349 flp->name = fieldname;
7350 flp->length = 0;
7351 flp->head = NULL;
3da10d80 7352 i = fip->nfnfields++;
c906108c
SS
7353 }
7354
7355 /* Create a new member function field and chain it to the field list
0963b4bd 7356 entry. */
c906108c 7357 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7358 make_cleanup (xfree, new_fnfield);
c906108c
SS
7359 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7360 new_fnfield->next = flp->head;
7361 flp->head = new_fnfield;
7362 flp->length++;
7363
7364 /* Fill in the member function field info. */
7365 fnp = &new_fnfield->fnfield;
3da10d80
KS
7366
7367 /* Delay processing of the physname until later. */
7368 if (cu->language == language_cplus || cu->language == language_java)
7369 {
7370 add_to_method_list (type, i, flp->length - 1, fieldname,
7371 die, cu);
7372 }
7373 else
7374 {
1d06ead6 7375 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7376 fnp->physname = physname ? physname : "";
7377 }
7378
c906108c 7379 fnp->type = alloc_type (objfile);
f792889a
DJ
7380 this_type = read_type_die (die, cu);
7381 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7382 {
f792889a 7383 int nparams = TYPE_NFIELDS (this_type);
c906108c 7384
f792889a 7385 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7386 of the method itself (TYPE_CODE_METHOD). */
7387 smash_to_method_type (fnp->type, type,
f792889a
DJ
7388 TYPE_TARGET_TYPE (this_type),
7389 TYPE_FIELDS (this_type),
7390 TYPE_NFIELDS (this_type),
7391 TYPE_VARARGS (this_type));
c906108c
SS
7392
7393 /* Handle static member functions.
c5aa993b 7394 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7395 member functions. G++ helps GDB by marking the first
7396 parameter for non-static member functions (which is the this
7397 pointer) as artificial. We obtain this information from
7398 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7399 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7400 fnp->voffset = VOFFSET_STATIC;
7401 }
7402 else
e2e0b3e5 7403 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7404 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7405
7406 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7407 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7408 fnp->fcontext = die_containing_type (die, cu);
c906108c 7409
3e43a32a
MS
7410 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7411 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7412
7413 /* Get accessibility. */
e142c38c 7414 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7415 if (attr)
60d5a603
JK
7416 accessibility = DW_UNSND (attr);
7417 else
7418 accessibility = dwarf2_default_access_attribute (die, cu);
7419 switch (accessibility)
c906108c 7420 {
60d5a603
JK
7421 case DW_ACCESS_private:
7422 fnp->is_private = 1;
7423 break;
7424 case DW_ACCESS_protected:
7425 fnp->is_protected = 1;
7426 break;
c906108c
SS
7427 }
7428
b02dede2 7429 /* Check for artificial methods. */
e142c38c 7430 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7431 if (attr && DW_UNSND (attr) != 0)
7432 fnp->is_artificial = 1;
7433
0d564a31 7434 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7435 function. For older versions of GCC, this is an offset in the
7436 appropriate virtual table, as specified by DW_AT_containing_type.
7437 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7438 to the object address. */
7439
e142c38c 7440 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7441 if (attr)
8e19ed76 7442 {
aec5aa8b 7443 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7444 {
aec5aa8b
TT
7445 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7446 {
7447 /* Old-style GCC. */
7448 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7449 }
7450 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7451 || (DW_BLOCK (attr)->size > 1
7452 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7453 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7454 {
7455 struct dwarf_block blk;
7456 int offset;
7457
7458 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7459 ? 1 : 2);
7460 blk.size = DW_BLOCK (attr)->size - offset;
7461 blk.data = DW_BLOCK (attr)->data + offset;
7462 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7463 if ((fnp->voffset % cu->header.addr_size) != 0)
7464 dwarf2_complex_location_expr_complaint ();
7465 else
7466 fnp->voffset /= cu->header.addr_size;
7467 fnp->voffset += 2;
7468 }
7469 else
7470 dwarf2_complex_location_expr_complaint ();
7471
7472 if (!fnp->fcontext)
7473 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7474 }
3690dd37 7475 else if (attr_form_is_section_offset (attr))
8e19ed76 7476 {
4d3c2250 7477 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7478 }
7479 else
7480 {
4d3c2250
KB
7481 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7482 fieldname);
8e19ed76 7483 }
0d564a31 7484 }
d48cc9dd
DJ
7485 else
7486 {
7487 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7488 if (attr && DW_UNSND (attr))
7489 {
7490 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7491 complaint (&symfile_complaints,
3e43a32a
MS
7492 _("Member function \"%s\" (offset %d) is virtual "
7493 "but the vtable offset is not specified"),
d48cc9dd 7494 fieldname, die->offset);
9655fd1a 7495 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7496 TYPE_CPLUS_DYNAMIC (type) = 1;
7497 }
7498 }
c906108c
SS
7499}
7500
7501/* Create the vector of member function fields, and attach it to the type. */
7502
7503static void
fba45db2 7504dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7505 struct dwarf2_cu *cu)
c906108c
SS
7506{
7507 struct fnfieldlist *flp;
7508 int total_length = 0;
7509 int i;
7510
b4ba55a1 7511 if (cu->language == language_ada)
a73c6dcd 7512 error (_("unexpected member functions in Ada type"));
b4ba55a1 7513
c906108c
SS
7514 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7515 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7516 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7517
7518 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7519 {
7520 struct nextfnfield *nfp = flp->head;
7521 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7522 int k;
7523
7524 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7525 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7526 fn_flp->fn_fields = (struct fn_field *)
7527 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7528 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7529 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7530
7531 total_length += flp->length;
7532 }
7533
7534 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7535 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7536}
7537
1168df01
JB
7538/* Returns non-zero if NAME is the name of a vtable member in CU's
7539 language, zero otherwise. */
7540static int
7541is_vtable_name (const char *name, struct dwarf2_cu *cu)
7542{
7543 static const char vptr[] = "_vptr";
987504bb 7544 static const char vtable[] = "vtable";
1168df01 7545
987504bb
JJ
7546 /* Look for the C++ and Java forms of the vtable. */
7547 if ((cu->language == language_java
7548 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7549 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7550 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7551 return 1;
7552
7553 return 0;
7554}
7555
c0dd20ea 7556/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7557 functions, with the ABI-specified layout. If TYPE describes
7558 such a structure, smash it into a member function type.
61049d3b
DJ
7559
7560 GCC shouldn't do this; it should just output pointer to member DIEs.
7561 This is GCC PR debug/28767. */
c0dd20ea 7562
0b92b5bb
TT
7563static void
7564quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7565{
0b92b5bb 7566 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7567
7568 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7569 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7570 return;
c0dd20ea
DJ
7571
7572 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7573 if (TYPE_FIELD_NAME (type, 0) == NULL
7574 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7575 || TYPE_FIELD_NAME (type, 1) == NULL
7576 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7577 return;
c0dd20ea
DJ
7578
7579 /* Find the type of the method. */
0b92b5bb 7580 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7581 if (pfn_type == NULL
7582 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7583 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7584 return;
c0dd20ea
DJ
7585
7586 /* Look for the "this" argument. */
7587 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7588 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7589 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7590 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7591 return;
c0dd20ea
DJ
7592
7593 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7594 new_type = alloc_type (objfile);
7595 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7596 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7597 TYPE_VARARGS (pfn_type));
0b92b5bb 7598 smash_to_methodptr_type (type, new_type);
c0dd20ea 7599}
1168df01 7600
c906108c 7601/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7602 (definition) to create a type for the structure or union. Fill in
7603 the type's name and general properties; the members will not be
7604 processed until process_structure_type.
c906108c 7605
c767944b
DJ
7606 NOTE: we need to call these functions regardless of whether or not the
7607 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7608 structure or union. This gets the type entered into our set of
7609 user defined types.
7610
7611 However, if the structure is incomplete (an opaque struct/union)
7612 then suppress creating a symbol table entry for it since gdb only
7613 wants to find the one with the complete definition. Note that if
7614 it is complete, we just call new_symbol, which does it's own
7615 checking about whether the struct/union is anonymous or not (and
7616 suppresses creating a symbol table entry itself). */
7617
f792889a 7618static struct type *
134d01f1 7619read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7620{
e7c27a73 7621 struct objfile *objfile = cu->objfile;
c906108c
SS
7622 struct type *type;
7623 struct attribute *attr;
39cbfefa 7624 char *name;
c906108c 7625
348e048f
DE
7626 /* If the definition of this type lives in .debug_types, read that type.
7627 Don't follow DW_AT_specification though, that will take us back up
7628 the chain and we want to go down. */
7629 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7630 if (attr)
7631 {
7632 struct dwarf2_cu *type_cu = cu;
7633 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7634
348e048f
DE
7635 /* We could just recurse on read_structure_type, but we need to call
7636 get_die_type to ensure only one type for this DIE is created.
7637 This is important, for example, because for c++ classes we need
7638 TYPE_NAME set which is only done by new_symbol. Blech. */
7639 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7640
7641 /* TYPE_CU may not be the same as CU.
7642 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7643 return set_die_type (die, type, cu);
7644 }
7645
c0dd20ea 7646 type = alloc_type (objfile);
c906108c 7647 INIT_CPLUS_SPECIFIC (type);
93311388 7648
39cbfefa
DJ
7649 name = dwarf2_name (die, cu);
7650 if (name != NULL)
c906108c 7651 {
987504bb
JJ
7652 if (cu->language == language_cplus
7653 || cu->language == language_java)
63d06c5c 7654 {
3da10d80
KS
7655 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7656
7657 /* dwarf2_full_name might have already finished building the DIE's
7658 type. If so, there is no need to continue. */
7659 if (get_die_type (die, cu) != NULL)
7660 return get_die_type (die, cu);
7661
7662 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7663 if (die->tag == DW_TAG_structure_type
7664 || die->tag == DW_TAG_class_type)
7665 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7666 }
7667 else
7668 {
d8151005
DJ
7669 /* The name is already allocated along with this objfile, so
7670 we don't need to duplicate it for the type. */
94af9270
KS
7671 TYPE_TAG_NAME (type) = (char *) name;
7672 if (die->tag == DW_TAG_class_type)
7673 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7674 }
c906108c
SS
7675 }
7676
7677 if (die->tag == DW_TAG_structure_type)
7678 {
7679 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7680 }
7681 else if (die->tag == DW_TAG_union_type)
7682 {
7683 TYPE_CODE (type) = TYPE_CODE_UNION;
7684 }
7685 else
7686 {
c906108c
SS
7687 TYPE_CODE (type) = TYPE_CODE_CLASS;
7688 }
7689
0cc2414c
TT
7690 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7691 TYPE_DECLARED_CLASS (type) = 1;
7692
e142c38c 7693 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7694 if (attr)
7695 {
7696 TYPE_LENGTH (type) = DW_UNSND (attr);
7697 }
7698 else
7699 {
7700 TYPE_LENGTH (type) = 0;
7701 }
7702
876cecd0 7703 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7704 if (die_is_declaration (die, cu))
876cecd0 7705 TYPE_STUB (type) = 1;
a6c727b2
DJ
7706 else if (attr == NULL && die->child == NULL
7707 && producer_is_realview (cu->producer))
7708 /* RealView does not output the required DW_AT_declaration
7709 on incomplete types. */
7710 TYPE_STUB (type) = 1;
dc718098 7711
c906108c
SS
7712 /* We need to add the type field to the die immediately so we don't
7713 infinitely recurse when dealing with pointers to the structure
0963b4bd 7714 type within the structure itself. */
1c379e20 7715 set_die_type (die, type, cu);
c906108c 7716
7e314c57
JK
7717 /* set_die_type should be already done. */
7718 set_descriptive_type (type, die, cu);
7719
c767944b
DJ
7720 return type;
7721}
7722
7723/* Finish creating a structure or union type, including filling in
7724 its members and creating a symbol for it. */
7725
7726static void
7727process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7728{
7729 struct objfile *objfile = cu->objfile;
7730 struct die_info *child_die = die->child;
7731 struct type *type;
7732
7733 type = get_die_type (die, cu);
7734 if (type == NULL)
7735 type = read_structure_type (die, cu);
7736
e142c38c 7737 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7738 {
7739 struct field_info fi;
7740 struct die_info *child_die;
34eaf542 7741 VEC (symbolp) *template_args = NULL;
c767944b 7742 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7743
7744 memset (&fi, 0, sizeof (struct field_info));
7745
639d11d3 7746 child_die = die->child;
c906108c
SS
7747
7748 while (child_die && child_die->tag)
7749 {
a9a9bd0f
DC
7750 if (child_die->tag == DW_TAG_member
7751 || child_die->tag == DW_TAG_variable)
c906108c 7752 {
a9a9bd0f
DC
7753 /* NOTE: carlton/2002-11-05: A C++ static data member
7754 should be a DW_TAG_member that is a declaration, but
7755 all versions of G++ as of this writing (so through at
7756 least 3.2.1) incorrectly generate DW_TAG_variable
7757 tags for them instead. */
e7c27a73 7758 dwarf2_add_field (&fi, child_die, cu);
c906108c 7759 }
8713b1b1 7760 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7761 {
0963b4bd 7762 /* C++ member function. */
e7c27a73 7763 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7764 }
7765 else if (child_die->tag == DW_TAG_inheritance)
7766 {
7767 /* C++ base class field. */
e7c27a73 7768 dwarf2_add_field (&fi, child_die, cu);
c906108c 7769 }
98751a41
JK
7770 else if (child_die->tag == DW_TAG_typedef)
7771 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7772 else if (child_die->tag == DW_TAG_template_type_param
7773 || child_die->tag == DW_TAG_template_value_param)
7774 {
7775 struct symbol *arg = new_symbol (child_die, NULL, cu);
7776
f1078f66
DJ
7777 if (arg != NULL)
7778 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7779 }
7780
c906108c
SS
7781 child_die = sibling_die (child_die);
7782 }
7783
34eaf542
TT
7784 /* Attach template arguments to type. */
7785 if (! VEC_empty (symbolp, template_args))
7786 {
7787 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7788 TYPE_N_TEMPLATE_ARGUMENTS (type)
7789 = VEC_length (symbolp, template_args);
7790 TYPE_TEMPLATE_ARGUMENTS (type)
7791 = obstack_alloc (&objfile->objfile_obstack,
7792 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7793 * sizeof (struct symbol *)));
7794 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7795 VEC_address (symbolp, template_args),
7796 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7797 * sizeof (struct symbol *)));
7798 VEC_free (symbolp, template_args);
7799 }
7800
c906108c
SS
7801 /* Attach fields and member functions to the type. */
7802 if (fi.nfields)
e7c27a73 7803 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7804 if (fi.nfnfields)
7805 {
e7c27a73 7806 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7807
c5aa993b 7808 /* Get the type which refers to the base class (possibly this
c906108c 7809 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7810 class from the DW_AT_containing_type attribute. This use of
7811 DW_AT_containing_type is a GNU extension. */
c906108c 7812
e142c38c 7813 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7814 {
e7c27a73 7815 struct type *t = die_containing_type (die, cu);
c906108c
SS
7816
7817 TYPE_VPTR_BASETYPE (type) = t;
7818 if (type == t)
7819 {
c906108c
SS
7820 int i;
7821
7822 /* Our own class provides vtbl ptr. */
7823 for (i = TYPE_NFIELDS (t) - 1;
7824 i >= TYPE_N_BASECLASSES (t);
7825 --i)
7826 {
7827 char *fieldname = TYPE_FIELD_NAME (t, i);
7828
1168df01 7829 if (is_vtable_name (fieldname, cu))
c906108c
SS
7830 {
7831 TYPE_VPTR_FIELDNO (type) = i;
7832 break;
7833 }
7834 }
7835
7836 /* Complain if virtual function table field not found. */
7837 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7838 complaint (&symfile_complaints,
3e43a32a
MS
7839 _("virtual function table pointer "
7840 "not found when defining class '%s'"),
4d3c2250
KB
7841 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7842 "");
c906108c
SS
7843 }
7844 else
7845 {
7846 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7847 }
7848 }
f6235d4c
EZ
7849 else if (cu->producer
7850 && strncmp (cu->producer,
7851 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7852 {
7853 /* The IBM XLC compiler does not provide direct indication
7854 of the containing type, but the vtable pointer is
7855 always named __vfp. */
7856
7857 int i;
7858
7859 for (i = TYPE_NFIELDS (type) - 1;
7860 i >= TYPE_N_BASECLASSES (type);
7861 --i)
7862 {
7863 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7864 {
7865 TYPE_VPTR_FIELDNO (type) = i;
7866 TYPE_VPTR_BASETYPE (type) = type;
7867 break;
7868 }
7869 }
7870 }
c906108c 7871 }
98751a41
JK
7872
7873 /* Copy fi.typedef_field_list linked list elements content into the
7874 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7875 if (fi.typedef_field_list)
7876 {
7877 int i = fi.typedef_field_list_count;
7878
a0d7a4ff 7879 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7880 TYPE_TYPEDEF_FIELD_ARRAY (type)
7881 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7882 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7883
7884 /* Reverse the list order to keep the debug info elements order. */
7885 while (--i >= 0)
7886 {
7887 struct typedef_field *dest, *src;
6e70227d 7888
98751a41
JK
7889 dest = &TYPE_TYPEDEF_FIELD (type, i);
7890 src = &fi.typedef_field_list->field;
7891 fi.typedef_field_list = fi.typedef_field_list->next;
7892 *dest = *src;
7893 }
7894 }
c767944b
DJ
7895
7896 do_cleanups (back_to);
eb2a6f42
TT
7897
7898 if (HAVE_CPLUS_STRUCT (type))
7899 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7900 }
63d06c5c 7901
0b92b5bb
TT
7902 quirk_gcc_member_function_pointer (type, cu->objfile);
7903
90aeadfc
DC
7904 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7905 snapshots) has been known to create a die giving a declaration
7906 for a class that has, as a child, a die giving a definition for a
7907 nested class. So we have to process our children even if the
7908 current die is a declaration. Normally, of course, a declaration
7909 won't have any children at all. */
134d01f1 7910
90aeadfc
DC
7911 while (child_die != NULL && child_die->tag)
7912 {
7913 if (child_die->tag == DW_TAG_member
7914 || child_die->tag == DW_TAG_variable
34eaf542
TT
7915 || child_die->tag == DW_TAG_inheritance
7916 || child_die->tag == DW_TAG_template_value_param
7917 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7918 {
90aeadfc 7919 /* Do nothing. */
134d01f1 7920 }
90aeadfc
DC
7921 else
7922 process_die (child_die, cu);
134d01f1 7923
90aeadfc 7924 child_die = sibling_die (child_die);
134d01f1
DJ
7925 }
7926
fa4028e9
JB
7927 /* Do not consider external references. According to the DWARF standard,
7928 these DIEs are identified by the fact that they have no byte_size
7929 attribute, and a declaration attribute. */
7930 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7931 || !die_is_declaration (die, cu))
c767944b 7932 new_symbol (die, type, cu);
134d01f1
DJ
7933}
7934
7935/* Given a DW_AT_enumeration_type die, set its type. We do not
7936 complete the type's fields yet, or create any symbols. */
c906108c 7937
f792889a 7938static struct type *
134d01f1 7939read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7940{
e7c27a73 7941 struct objfile *objfile = cu->objfile;
c906108c 7942 struct type *type;
c906108c 7943 struct attribute *attr;
0114d602 7944 const char *name;
134d01f1 7945
348e048f
DE
7946 /* If the definition of this type lives in .debug_types, read that type.
7947 Don't follow DW_AT_specification though, that will take us back up
7948 the chain and we want to go down. */
7949 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7950 if (attr)
7951 {
7952 struct dwarf2_cu *type_cu = cu;
7953 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7954
348e048f 7955 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7956
7957 /* TYPE_CU may not be the same as CU.
7958 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7959 return set_die_type (die, type, cu);
7960 }
7961
c906108c
SS
7962 type = alloc_type (objfile);
7963
7964 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7965 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7966 if (name != NULL)
0114d602 7967 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7968
e142c38c 7969 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7970 if (attr)
7971 {
7972 TYPE_LENGTH (type) = DW_UNSND (attr);
7973 }
7974 else
7975 {
7976 TYPE_LENGTH (type) = 0;
7977 }
7978
137033e9
JB
7979 /* The enumeration DIE can be incomplete. In Ada, any type can be
7980 declared as private in the package spec, and then defined only
7981 inside the package body. Such types are known as Taft Amendment
7982 Types. When another package uses such a type, an incomplete DIE
7983 may be generated by the compiler. */
02eb380e 7984 if (die_is_declaration (die, cu))
876cecd0 7985 TYPE_STUB (type) = 1;
02eb380e 7986
f792889a 7987 return set_die_type (die, type, cu);
134d01f1
DJ
7988}
7989
7990/* Given a pointer to a die which begins an enumeration, process all
7991 the dies that define the members of the enumeration, and create the
7992 symbol for the enumeration type.
7993
7994 NOTE: We reverse the order of the element list. */
7995
7996static void
7997process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7998{
f792889a 7999 struct type *this_type;
134d01f1 8000
f792889a
DJ
8001 this_type = get_die_type (die, cu);
8002 if (this_type == NULL)
8003 this_type = read_enumeration_type (die, cu);
9dc481d3 8004
639d11d3 8005 if (die->child != NULL)
c906108c 8006 {
9dc481d3
DE
8007 struct die_info *child_die;
8008 struct symbol *sym;
8009 struct field *fields = NULL;
8010 int num_fields = 0;
8011 int unsigned_enum = 1;
8012 char *name;
8013
639d11d3 8014 child_die = die->child;
c906108c
SS
8015 while (child_die && child_die->tag)
8016 {
8017 if (child_die->tag != DW_TAG_enumerator)
8018 {
e7c27a73 8019 process_die (child_die, cu);
c906108c
SS
8020 }
8021 else
8022 {
39cbfefa
DJ
8023 name = dwarf2_name (child_die, cu);
8024 if (name)
c906108c 8025 {
f792889a 8026 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
8027 if (SYMBOL_VALUE (sym) < 0)
8028 unsigned_enum = 0;
8029
8030 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8031 {
8032 fields = (struct field *)
8033 xrealloc (fields,
8034 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8035 * sizeof (struct field));
c906108c
SS
8036 }
8037
3567439c 8038 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 8039 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 8040 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
8041 FIELD_BITSIZE (fields[num_fields]) = 0;
8042
8043 num_fields++;
8044 }
8045 }
8046
8047 child_die = sibling_die (child_die);
8048 }
8049
8050 if (num_fields)
8051 {
f792889a
DJ
8052 TYPE_NFIELDS (this_type) = num_fields;
8053 TYPE_FIELDS (this_type) = (struct field *)
8054 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8055 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 8056 sizeof (struct field) * num_fields);
b8c9b27d 8057 xfree (fields);
c906108c
SS
8058 }
8059 if (unsigned_enum)
876cecd0 8060 TYPE_UNSIGNED (this_type) = 1;
c906108c 8061 }
134d01f1 8062
6c83ed52
TT
8063 /* If we are reading an enum from a .debug_types unit, and the enum
8064 is a declaration, and the enum is not the signatured type in the
8065 unit, then we do not want to add a symbol for it. Adding a
8066 symbol would in some cases obscure the true definition of the
8067 enum, giving users an incomplete type when the definition is
8068 actually available. Note that we do not want to do this for all
8069 enums which are just declarations, because C++0x allows forward
8070 enum declarations. */
8071 if (cu->per_cu->debug_type_section
8072 && die_is_declaration (die, cu))
8073 {
8074 struct signatured_type *type_sig;
8075
8076 type_sig
8077 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8078 cu->per_cu->debug_type_section,
8079 cu->per_cu->offset);
8080 if (type_sig->type_offset != die->offset)
8081 return;
8082 }
8083
f792889a 8084 new_symbol (die, this_type, cu);
c906108c
SS
8085}
8086
8087/* Extract all information from a DW_TAG_array_type DIE and put it in
8088 the DIE's type field. For now, this only handles one dimensional
8089 arrays. */
8090
f792889a 8091static struct type *
e7c27a73 8092read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8093{
e7c27a73 8094 struct objfile *objfile = cu->objfile;
c906108c 8095 struct die_info *child_die;
7e314c57 8096 struct type *type;
c906108c
SS
8097 struct type *element_type, *range_type, *index_type;
8098 struct type **range_types = NULL;
8099 struct attribute *attr;
8100 int ndim = 0;
8101 struct cleanup *back_to;
39cbfefa 8102 char *name;
c906108c 8103
e7c27a73 8104 element_type = die_type (die, cu);
c906108c 8105
7e314c57
JK
8106 /* The die_type call above may have already set the type for this DIE. */
8107 type = get_die_type (die, cu);
8108 if (type)
8109 return type;
8110
c906108c
SS
8111 /* Irix 6.2 native cc creates array types without children for
8112 arrays with unspecified length. */
639d11d3 8113 if (die->child == NULL)
c906108c 8114 {
46bf5051 8115 index_type = objfile_type (objfile)->builtin_int;
c906108c 8116 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8117 type = create_array_type (NULL, element_type, range_type);
8118 return set_die_type (die, type, cu);
c906108c
SS
8119 }
8120
8121 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8122 child_die = die->child;
c906108c
SS
8123 while (child_die && child_die->tag)
8124 {
8125 if (child_die->tag == DW_TAG_subrange_type)
8126 {
f792889a 8127 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8128
f792889a 8129 if (child_type != NULL)
a02abb62 8130 {
0963b4bd
MS
8131 /* The range type was succesfully read. Save it for the
8132 array type creation. */
a02abb62
JB
8133 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8134 {
8135 range_types = (struct type **)
8136 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8137 * sizeof (struct type *));
8138 if (ndim == 0)
8139 make_cleanup (free_current_contents, &range_types);
8140 }
f792889a 8141 range_types[ndim++] = child_type;
a02abb62 8142 }
c906108c
SS
8143 }
8144 child_die = sibling_die (child_die);
8145 }
8146
8147 /* Dwarf2 dimensions are output from left to right, create the
8148 necessary array types in backwards order. */
7ca2d3a3 8149
c906108c 8150 type = element_type;
7ca2d3a3
DL
8151
8152 if (read_array_order (die, cu) == DW_ORD_col_major)
8153 {
8154 int i = 0;
9a619af0 8155
7ca2d3a3
DL
8156 while (i < ndim)
8157 type = create_array_type (NULL, type, range_types[i++]);
8158 }
8159 else
8160 {
8161 while (ndim-- > 0)
8162 type = create_array_type (NULL, type, range_types[ndim]);
8163 }
c906108c 8164
f5f8a009
EZ
8165 /* Understand Dwarf2 support for vector types (like they occur on
8166 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8167 array type. This is not part of the Dwarf2/3 standard yet, but a
8168 custom vendor extension. The main difference between a regular
8169 array and the vector variant is that vectors are passed by value
8170 to functions. */
e142c38c 8171 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8172 if (attr)
ea37ba09 8173 make_vector_type (type);
f5f8a009 8174
dbc98a8b
KW
8175 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8176 implementation may choose to implement triple vectors using this
8177 attribute. */
8178 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8179 if (attr)
8180 {
8181 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8182 TYPE_LENGTH (type) = DW_UNSND (attr);
8183 else
3e43a32a
MS
8184 complaint (&symfile_complaints,
8185 _("DW_AT_byte_size for array type smaller "
8186 "than the total size of elements"));
dbc98a8b
KW
8187 }
8188
39cbfefa
DJ
8189 name = dwarf2_name (die, cu);
8190 if (name)
8191 TYPE_NAME (type) = name;
6e70227d 8192
0963b4bd 8193 /* Install the type in the die. */
7e314c57
JK
8194 set_die_type (die, type, cu);
8195
8196 /* set_die_type should be already done. */
b4ba55a1
JB
8197 set_descriptive_type (type, die, cu);
8198
c906108c
SS
8199 do_cleanups (back_to);
8200
7e314c57 8201 return type;
c906108c
SS
8202}
8203
7ca2d3a3 8204static enum dwarf_array_dim_ordering
6e70227d 8205read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8206{
8207 struct attribute *attr;
8208
8209 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8210
8211 if (attr) return DW_SND (attr);
8212
0963b4bd
MS
8213 /* GNU F77 is a special case, as at 08/2004 array type info is the
8214 opposite order to the dwarf2 specification, but data is still
8215 laid out as per normal fortran.
7ca2d3a3 8216
0963b4bd
MS
8217 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8218 version checking. */
7ca2d3a3 8219
905e0470
PM
8220 if (cu->language == language_fortran
8221 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8222 {
8223 return DW_ORD_row_major;
8224 }
8225
6e70227d 8226 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8227 {
8228 case array_column_major:
8229 return DW_ORD_col_major;
8230 case array_row_major:
8231 default:
8232 return DW_ORD_row_major;
8233 };
8234}
8235
72019c9c 8236/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8237 the DIE's type field. */
72019c9c 8238
f792889a 8239static struct type *
72019c9c
GM
8240read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8241{
7e314c57
JK
8242 struct type *domain_type, *set_type;
8243 struct attribute *attr;
f792889a 8244
7e314c57
JK
8245 domain_type = die_type (die, cu);
8246
8247 /* The die_type call above may have already set the type for this DIE. */
8248 set_type = get_die_type (die, cu);
8249 if (set_type)
8250 return set_type;
8251
8252 set_type = create_set_type (NULL, domain_type);
8253
8254 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8255 if (attr)
8256 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8257
f792889a 8258 return set_die_type (die, set_type, cu);
72019c9c 8259}
7ca2d3a3 8260
c906108c
SS
8261/* First cut: install each common block member as a global variable. */
8262
8263static void
e7c27a73 8264read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8265{
8266 struct die_info *child_die;
8267 struct attribute *attr;
8268 struct symbol *sym;
8269 CORE_ADDR base = (CORE_ADDR) 0;
8270
e142c38c 8271 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8272 if (attr)
8273 {
0963b4bd 8274 /* Support the .debug_loc offsets. */
8e19ed76
PS
8275 if (attr_form_is_block (attr))
8276 {
e7c27a73 8277 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8278 }
3690dd37 8279 else if (attr_form_is_section_offset (attr))
8e19ed76 8280 {
4d3c2250 8281 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8282 }
8283 else
8284 {
4d3c2250
KB
8285 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8286 "common block member");
8e19ed76 8287 }
c906108c 8288 }
639d11d3 8289 if (die->child != NULL)
c906108c 8290 {
639d11d3 8291 child_die = die->child;
c906108c
SS
8292 while (child_die && child_die->tag)
8293 {
74ac6d43
TT
8294 LONGEST offset;
8295
e7c27a73 8296 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8297 if (sym != NULL
8298 && handle_data_member_location (child_die, cu, &offset))
c906108c 8299 {
74ac6d43 8300 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8301 add_symbol_to_list (sym, &global_symbols);
8302 }
8303 child_die = sibling_die (child_die);
8304 }
8305 }
8306}
8307
0114d602 8308/* Create a type for a C++ namespace. */
d9fa45fe 8309
0114d602
DJ
8310static struct type *
8311read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8312{
e7c27a73 8313 struct objfile *objfile = cu->objfile;
0114d602 8314 const char *previous_prefix, *name;
9219021c 8315 int is_anonymous;
0114d602
DJ
8316 struct type *type;
8317
8318 /* For extensions, reuse the type of the original namespace. */
8319 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8320 {
8321 struct die_info *ext_die;
8322 struct dwarf2_cu *ext_cu = cu;
9a619af0 8323
0114d602
DJ
8324 ext_die = dwarf2_extension (die, &ext_cu);
8325 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8326
8327 /* EXT_CU may not be the same as CU.
8328 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8329 return set_die_type (die, type, cu);
8330 }
9219021c 8331
e142c38c 8332 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8333
8334 /* Now build the name of the current namespace. */
8335
0114d602
DJ
8336 previous_prefix = determine_prefix (die, cu);
8337 if (previous_prefix[0] != '\0')
8338 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8339 previous_prefix, name, 0, cu);
0114d602
DJ
8340
8341 /* Create the type. */
8342 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8343 objfile);
8344 TYPE_NAME (type) = (char *) name;
8345 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8346
60531b24 8347 return set_die_type (die, type, cu);
0114d602
DJ
8348}
8349
8350/* Read a C++ namespace. */
8351
8352static void
8353read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8354{
8355 struct objfile *objfile = cu->objfile;
0114d602 8356 int is_anonymous;
9219021c 8357
5c4e30ca
DC
8358 /* Add a symbol associated to this if we haven't seen the namespace
8359 before. Also, add a using directive if it's an anonymous
8360 namespace. */
9219021c 8361
f2f0e013 8362 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8363 {
8364 struct type *type;
8365
0114d602 8366 type = read_type_die (die, cu);
e7c27a73 8367 new_symbol (die, type, cu);
5c4e30ca 8368
e8e80198 8369 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8370 if (is_anonymous)
0114d602
DJ
8371 {
8372 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8373
c0cc3a76 8374 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8375 NULL, NULL, &objfile->objfile_obstack);
0114d602 8376 }
5c4e30ca 8377 }
9219021c 8378
639d11d3 8379 if (die->child != NULL)
d9fa45fe 8380 {
639d11d3 8381 struct die_info *child_die = die->child;
6e70227d 8382
d9fa45fe
DC
8383 while (child_die && child_die->tag)
8384 {
e7c27a73 8385 process_die (child_die, cu);
d9fa45fe
DC
8386 child_die = sibling_die (child_die);
8387 }
8388 }
38d518c9
EZ
8389}
8390
f55ee35c
JK
8391/* Read a Fortran module as type. This DIE can be only a declaration used for
8392 imported module. Still we need that type as local Fortran "use ... only"
8393 declaration imports depend on the created type in determine_prefix. */
8394
8395static struct type *
8396read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8397{
8398 struct objfile *objfile = cu->objfile;
8399 char *module_name;
8400 struct type *type;
8401
8402 module_name = dwarf2_name (die, cu);
8403 if (!module_name)
3e43a32a
MS
8404 complaint (&symfile_complaints,
8405 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8406 die->offset);
8407 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8408
8409 /* determine_prefix uses TYPE_TAG_NAME. */
8410 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8411
8412 return set_die_type (die, type, cu);
8413}
8414
5d7cb8df
JK
8415/* Read a Fortran module. */
8416
8417static void
8418read_module (struct die_info *die, struct dwarf2_cu *cu)
8419{
8420 struct die_info *child_die = die->child;
8421
5d7cb8df
JK
8422 while (child_die && child_die->tag)
8423 {
8424 process_die (child_die, cu);
8425 child_die = sibling_die (child_die);
8426 }
8427}
8428
38d518c9
EZ
8429/* Return the name of the namespace represented by DIE. Set
8430 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8431 namespace. */
8432
8433static const char *
e142c38c 8434namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8435{
8436 struct die_info *current_die;
8437 const char *name = NULL;
8438
8439 /* Loop through the extensions until we find a name. */
8440
8441 for (current_die = die;
8442 current_die != NULL;
f2f0e013 8443 current_die = dwarf2_extension (die, &cu))
38d518c9 8444 {
e142c38c 8445 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8446 if (name != NULL)
8447 break;
8448 }
8449
8450 /* Is it an anonymous namespace? */
8451
8452 *is_anonymous = (name == NULL);
8453 if (*is_anonymous)
2b1dbab0 8454 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8455
8456 return name;
d9fa45fe
DC
8457}
8458
c906108c
SS
8459/* Extract all information from a DW_TAG_pointer_type DIE and add to
8460 the user defined type vector. */
8461
f792889a 8462static struct type *
e7c27a73 8463read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8464{
5e2b427d 8465 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8466 struct comp_unit_head *cu_header = &cu->header;
c906108c 8467 struct type *type;
8b2dbe47
KB
8468 struct attribute *attr_byte_size;
8469 struct attribute *attr_address_class;
8470 int byte_size, addr_class;
7e314c57
JK
8471 struct type *target_type;
8472
8473 target_type = die_type (die, cu);
c906108c 8474
7e314c57
JK
8475 /* The die_type call above may have already set the type for this DIE. */
8476 type = get_die_type (die, cu);
8477 if (type)
8478 return type;
8479
8480 type = lookup_pointer_type (target_type);
8b2dbe47 8481
e142c38c 8482 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8483 if (attr_byte_size)
8484 byte_size = DW_UNSND (attr_byte_size);
c906108c 8485 else
8b2dbe47
KB
8486 byte_size = cu_header->addr_size;
8487
e142c38c 8488 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8489 if (attr_address_class)
8490 addr_class = DW_UNSND (attr_address_class);
8491 else
8492 addr_class = DW_ADDR_none;
8493
8494 /* If the pointer size or address class is different than the
8495 default, create a type variant marked as such and set the
8496 length accordingly. */
8497 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8498 {
5e2b427d 8499 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8500 {
8501 int type_flags;
8502
849957d9 8503 type_flags = gdbarch_address_class_type_flags
5e2b427d 8504 (gdbarch, byte_size, addr_class);
876cecd0
TT
8505 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8506 == 0);
8b2dbe47
KB
8507 type = make_type_with_address_space (type, type_flags);
8508 }
8509 else if (TYPE_LENGTH (type) != byte_size)
8510 {
3e43a32a
MS
8511 complaint (&symfile_complaints,
8512 _("invalid pointer size %d"), byte_size);
8b2dbe47 8513 }
6e70227d 8514 else
9a619af0
MS
8515 {
8516 /* Should we also complain about unhandled address classes? */
8517 }
c906108c 8518 }
8b2dbe47
KB
8519
8520 TYPE_LENGTH (type) = byte_size;
f792889a 8521 return set_die_type (die, type, cu);
c906108c
SS
8522}
8523
8524/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8525 the user defined type vector. */
8526
f792889a 8527static struct type *
e7c27a73 8528read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8529{
8530 struct type *type;
8531 struct type *to_type;
8532 struct type *domain;
8533
e7c27a73
DJ
8534 to_type = die_type (die, cu);
8535 domain = die_containing_type (die, cu);
0d5de010 8536
7e314c57
JK
8537 /* The calls above may have already set the type for this DIE. */
8538 type = get_die_type (die, cu);
8539 if (type)
8540 return type;
8541
0d5de010
DJ
8542 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8543 type = lookup_methodptr_type (to_type);
8544 else
8545 type = lookup_memberptr_type (to_type, domain);
c906108c 8546
f792889a 8547 return set_die_type (die, type, cu);
c906108c
SS
8548}
8549
8550/* Extract all information from a DW_TAG_reference_type DIE and add to
8551 the user defined type vector. */
8552
f792889a 8553static struct type *
e7c27a73 8554read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8555{
e7c27a73 8556 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8557 struct type *type, *target_type;
c906108c
SS
8558 struct attribute *attr;
8559
7e314c57
JK
8560 target_type = die_type (die, cu);
8561
8562 /* The die_type call above may have already set the type for this DIE. */
8563 type = get_die_type (die, cu);
8564 if (type)
8565 return type;
8566
8567 type = lookup_reference_type (target_type);
e142c38c 8568 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8569 if (attr)
8570 {
8571 TYPE_LENGTH (type) = DW_UNSND (attr);
8572 }
8573 else
8574 {
107d2387 8575 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8576 }
f792889a 8577 return set_die_type (die, type, cu);
c906108c
SS
8578}
8579
f792889a 8580static struct type *
e7c27a73 8581read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8582{
f792889a 8583 struct type *base_type, *cv_type;
c906108c 8584
e7c27a73 8585 base_type = die_type (die, cu);
7e314c57
JK
8586
8587 /* The die_type call above may have already set the type for this DIE. */
8588 cv_type = get_die_type (die, cu);
8589 if (cv_type)
8590 return cv_type;
8591
2f608a3a
KW
8592 /* In case the const qualifier is applied to an array type, the element type
8593 is so qualified, not the array type (section 6.7.3 of C99). */
8594 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8595 {
8596 struct type *el_type, *inner_array;
8597
8598 base_type = copy_type (base_type);
8599 inner_array = base_type;
8600
8601 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8602 {
8603 TYPE_TARGET_TYPE (inner_array) =
8604 copy_type (TYPE_TARGET_TYPE (inner_array));
8605 inner_array = TYPE_TARGET_TYPE (inner_array);
8606 }
8607
8608 el_type = TYPE_TARGET_TYPE (inner_array);
8609 TYPE_TARGET_TYPE (inner_array) =
8610 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8611
8612 return set_die_type (die, base_type, cu);
8613 }
8614
f792889a
DJ
8615 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8616 return set_die_type (die, cv_type, cu);
c906108c
SS
8617}
8618
f792889a 8619static struct type *
e7c27a73 8620read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8621{
f792889a 8622 struct type *base_type, *cv_type;
c906108c 8623
e7c27a73 8624 base_type = die_type (die, cu);
7e314c57
JK
8625
8626 /* The die_type call above may have already set the type for this DIE. */
8627 cv_type = get_die_type (die, cu);
8628 if (cv_type)
8629 return cv_type;
8630
f792889a
DJ
8631 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8632 return set_die_type (die, cv_type, cu);
c906108c
SS
8633}
8634
8635/* Extract all information from a DW_TAG_string_type DIE and add to
8636 the user defined type vector. It isn't really a user defined type,
8637 but it behaves like one, with other DIE's using an AT_user_def_type
8638 attribute to reference it. */
8639
f792889a 8640static struct type *
e7c27a73 8641read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8642{
e7c27a73 8643 struct objfile *objfile = cu->objfile;
3b7538c0 8644 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8645 struct type *type, *range_type, *index_type, *char_type;
8646 struct attribute *attr;
8647 unsigned int length;
8648
e142c38c 8649 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8650 if (attr)
8651 {
8652 length = DW_UNSND (attr);
8653 }
8654 else
8655 {
0963b4bd 8656 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8657 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8658 if (attr)
8659 {
8660 length = DW_UNSND (attr);
8661 }
8662 else
8663 {
8664 length = 1;
8665 }
c906108c 8666 }
6ccb9162 8667
46bf5051 8668 index_type = objfile_type (objfile)->builtin_int;
c906108c 8669 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8670 char_type = language_string_char_type (cu->language_defn, gdbarch);
8671 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8672
f792889a 8673 return set_die_type (die, type, cu);
c906108c
SS
8674}
8675
8676/* Handle DIES due to C code like:
8677
8678 struct foo
c5aa993b
JM
8679 {
8680 int (*funcp)(int a, long l);
8681 int b;
8682 };
c906108c 8683
0963b4bd 8684 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8685
f792889a 8686static struct type *
e7c27a73 8687read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8688{
0963b4bd
MS
8689 struct type *type; /* Type that this function returns. */
8690 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8691 struct attribute *attr;
8692
e7c27a73 8693 type = die_type (die, cu);
7e314c57
JK
8694
8695 /* The die_type call above may have already set the type for this DIE. */
8696 ftype = get_die_type (die, cu);
8697 if (ftype)
8698 return ftype;
8699
0c8b41f1 8700 ftype = lookup_function_type (type);
c906108c 8701
5b8101ae 8702 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8703 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8704 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8705 || cu->language == language_cplus
5b8101ae
PM
8706 || cu->language == language_java
8707 || cu->language == language_pascal)
876cecd0 8708 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8709 else if (producer_is_realview (cu->producer))
8710 /* RealView does not emit DW_AT_prototyped. We can not
8711 distinguish prototyped and unprototyped functions; default to
8712 prototyped, since that is more common in modern code (and
8713 RealView warns about unprototyped functions). */
8714 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8715
c055b101
CV
8716 /* Store the calling convention in the type if it's available in
8717 the subroutine die. Otherwise set the calling convention to
8718 the default value DW_CC_normal. */
8719 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8720 if (attr)
8721 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8722 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8723 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8724 else
8725 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8726
8727 /* We need to add the subroutine type to the die immediately so
8728 we don't infinitely recurse when dealing with parameters
0963b4bd 8729 declared as the same subroutine type. */
76c10ea2 8730 set_die_type (die, ftype, cu);
6e70227d 8731
639d11d3 8732 if (die->child != NULL)
c906108c 8733 {
8072405b 8734 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8735 struct die_info *child_die;
8072405b 8736 int nparams, iparams;
c906108c
SS
8737
8738 /* Count the number of parameters.
8739 FIXME: GDB currently ignores vararg functions, but knows about
8740 vararg member functions. */
8072405b 8741 nparams = 0;
639d11d3 8742 child_die = die->child;
c906108c
SS
8743 while (child_die && child_die->tag)
8744 {
8745 if (child_die->tag == DW_TAG_formal_parameter)
8746 nparams++;
8747 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8748 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8749 child_die = sibling_die (child_die);
8750 }
8751
8752 /* Allocate storage for parameters and fill them in. */
8753 TYPE_NFIELDS (ftype) = nparams;
8754 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8755 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8756
8072405b
JK
8757 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8758 even if we error out during the parameters reading below. */
8759 for (iparams = 0; iparams < nparams; iparams++)
8760 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8761
8762 iparams = 0;
639d11d3 8763 child_die = die->child;
c906108c
SS
8764 while (child_die && child_die->tag)
8765 {
8766 if (child_die->tag == DW_TAG_formal_parameter)
8767 {
3ce3b1ba
PA
8768 struct type *arg_type;
8769
8770 /* DWARF version 2 has no clean way to discern C++
8771 static and non-static member functions. G++ helps
8772 GDB by marking the first parameter for non-static
8773 member functions (which is the this pointer) as
8774 artificial. We pass this information to
8775 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8776
8777 DWARF version 3 added DW_AT_object_pointer, which GCC
8778 4.5 does not yet generate. */
e142c38c 8779 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8780 if (attr)
8781 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8782 else
418835cc
KS
8783 {
8784 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8785
8786 /* GCC/43521: In java, the formal parameter
8787 "this" is sometimes not marked with DW_AT_artificial. */
8788 if (cu->language == language_java)
8789 {
8790 const char *name = dwarf2_name (child_die, cu);
9a619af0 8791
418835cc
KS
8792 if (name && !strcmp (name, "this"))
8793 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8794 }
8795 }
3ce3b1ba
PA
8796 arg_type = die_type (child_die, cu);
8797
8798 /* RealView does not mark THIS as const, which the testsuite
8799 expects. GCC marks THIS as const in method definitions,
8800 but not in the class specifications (GCC PR 43053). */
8801 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8802 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8803 {
8804 int is_this = 0;
8805 struct dwarf2_cu *arg_cu = cu;
8806 const char *name = dwarf2_name (child_die, cu);
8807
8808 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8809 if (attr)
8810 {
8811 /* If the compiler emits this, use it. */
8812 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8813 is_this = 1;
8814 }
8815 else if (name && strcmp (name, "this") == 0)
8816 /* Function definitions will have the argument names. */
8817 is_this = 1;
8818 else if (name == NULL && iparams == 0)
8819 /* Declarations may not have the names, so like
8820 elsewhere in GDB, assume an artificial first
8821 argument is "this". */
8822 is_this = 1;
8823
8824 if (is_this)
8825 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8826 arg_type, 0);
8827 }
8828
8829 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8830 iparams++;
8831 }
8832 child_die = sibling_die (child_die);
8833 }
8834 }
8835
76c10ea2 8836 return ftype;
c906108c
SS
8837}
8838
f792889a 8839static struct type *
e7c27a73 8840read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8841{
e7c27a73 8842 struct objfile *objfile = cu->objfile;
0114d602 8843 const char *name = NULL;
f792889a 8844 struct type *this_type;
c906108c 8845
94af9270 8846 name = dwarf2_full_name (NULL, die, cu);
f792889a 8847 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8848 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8849 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8850 set_die_type (die, this_type, cu);
8851 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8852 return this_type;
c906108c
SS
8853}
8854
8855/* Find a representation of a given base type and install
8856 it in the TYPE field of the die. */
8857
f792889a 8858static struct type *
e7c27a73 8859read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8860{
e7c27a73 8861 struct objfile *objfile = cu->objfile;
c906108c
SS
8862 struct type *type;
8863 struct attribute *attr;
8864 int encoding = 0, size = 0;
39cbfefa 8865 char *name;
6ccb9162
UW
8866 enum type_code code = TYPE_CODE_INT;
8867 int type_flags = 0;
8868 struct type *target_type = NULL;
c906108c 8869
e142c38c 8870 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8871 if (attr)
8872 {
8873 encoding = DW_UNSND (attr);
8874 }
e142c38c 8875 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8876 if (attr)
8877 {
8878 size = DW_UNSND (attr);
8879 }
39cbfefa 8880 name = dwarf2_name (die, cu);
6ccb9162 8881 if (!name)
c906108c 8882 {
6ccb9162
UW
8883 complaint (&symfile_complaints,
8884 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8885 }
6ccb9162
UW
8886
8887 switch (encoding)
c906108c 8888 {
6ccb9162
UW
8889 case DW_ATE_address:
8890 /* Turn DW_ATE_address into a void * pointer. */
8891 code = TYPE_CODE_PTR;
8892 type_flags |= TYPE_FLAG_UNSIGNED;
8893 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8894 break;
8895 case DW_ATE_boolean:
8896 code = TYPE_CODE_BOOL;
8897 type_flags |= TYPE_FLAG_UNSIGNED;
8898 break;
8899 case DW_ATE_complex_float:
8900 code = TYPE_CODE_COMPLEX;
8901 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8902 break;
8903 case DW_ATE_decimal_float:
8904 code = TYPE_CODE_DECFLOAT;
8905 break;
8906 case DW_ATE_float:
8907 code = TYPE_CODE_FLT;
8908 break;
8909 case DW_ATE_signed:
8910 break;
8911 case DW_ATE_unsigned:
8912 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8913 if (cu->language == language_fortran
8914 && name
8915 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8916 code = TYPE_CODE_CHAR;
6ccb9162
UW
8917 break;
8918 case DW_ATE_signed_char:
6e70227d 8919 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8920 || cu->language == language_pascal
8921 || cu->language == language_fortran)
6ccb9162
UW
8922 code = TYPE_CODE_CHAR;
8923 break;
8924 case DW_ATE_unsigned_char:
868a0084 8925 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8926 || cu->language == language_pascal
8927 || cu->language == language_fortran)
6ccb9162
UW
8928 code = TYPE_CODE_CHAR;
8929 type_flags |= TYPE_FLAG_UNSIGNED;
8930 break;
75079b2b
TT
8931 case DW_ATE_UTF:
8932 /* We just treat this as an integer and then recognize the
8933 type by name elsewhere. */
8934 break;
8935
6ccb9162
UW
8936 default:
8937 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8938 dwarf_type_encoding_name (encoding));
8939 break;
c906108c 8940 }
6ccb9162 8941
0114d602
DJ
8942 type = init_type (code, size, type_flags, NULL, objfile);
8943 TYPE_NAME (type) = name;
6ccb9162
UW
8944 TYPE_TARGET_TYPE (type) = target_type;
8945
0114d602 8946 if (name && strcmp (name, "char") == 0)
876cecd0 8947 TYPE_NOSIGN (type) = 1;
0114d602 8948
f792889a 8949 return set_die_type (die, type, cu);
c906108c
SS
8950}
8951
a02abb62
JB
8952/* Read the given DW_AT_subrange DIE. */
8953
f792889a 8954static struct type *
a02abb62
JB
8955read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8956{
8957 struct type *base_type;
8958 struct type *range_type;
8959 struct attribute *attr;
43bbcdc2
PH
8960 LONGEST low = 0;
8961 LONGEST high = -1;
39cbfefa 8962 char *name;
43bbcdc2 8963 LONGEST negative_mask;
e77813c8 8964
a02abb62 8965 base_type = die_type (die, cu);
953ac07e
JK
8966 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8967 check_typedef (base_type);
a02abb62 8968
7e314c57
JK
8969 /* The die_type call above may have already set the type for this DIE. */
8970 range_type = get_die_type (die, cu);
8971 if (range_type)
8972 return range_type;
8973
e142c38c 8974 if (cu->language == language_fortran)
6e70227d 8975 {
a02abb62
JB
8976 /* FORTRAN implies a lower bound of 1, if not given. */
8977 low = 1;
8978 }
8979
dd5e6932
DJ
8980 /* FIXME: For variable sized arrays either of these could be
8981 a variable rather than a constant value. We'll allow it,
8982 but we don't know how to handle it. */
e142c38c 8983 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8984 if (attr)
8985 low = dwarf2_get_attr_constant_value (attr, 0);
8986
e142c38c 8987 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8988 if (attr)
6e70227d 8989 {
d48323d8 8990 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
8991 {
8992 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8993 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8994 FIXME: GDB does not yet know how to handle dynamic
8995 arrays properly, treat them as arrays with unspecified
8996 length for now.
8997
8998 FIXME: jimb/2003-09-22: GDB does not really know
8999 how to handle arrays of unspecified length
9000 either; we just represent them as zero-length
9001 arrays. Choose an appropriate upper bound given
9002 the lower bound we've computed above. */
9003 high = low - 1;
9004 }
9005 else
9006 high = dwarf2_get_attr_constant_value (attr, 1);
9007 }
e77813c8
PM
9008 else
9009 {
9010 attr = dwarf2_attr (die, DW_AT_count, cu);
9011 if (attr)
9012 {
9013 int count = dwarf2_get_attr_constant_value (attr, 1);
9014 high = low + count - 1;
9015 }
c2ff108b
JK
9016 else
9017 {
9018 /* Unspecified array length. */
9019 high = low - 1;
9020 }
e77813c8
PM
9021 }
9022
9023 /* Dwarf-2 specifications explicitly allows to create subrange types
9024 without specifying a base type.
9025 In that case, the base type must be set to the type of
9026 the lower bound, upper bound or count, in that order, if any of these
9027 three attributes references an object that has a type.
9028 If no base type is found, the Dwarf-2 specifications say that
9029 a signed integer type of size equal to the size of an address should
9030 be used.
9031 For the following C code: `extern char gdb_int [];'
9032 GCC produces an empty range DIE.
9033 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 9034 high bound or count are not yet handled by this code. */
e77813c8
PM
9035 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9036 {
9037 struct objfile *objfile = cu->objfile;
9038 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9039 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9040 struct type *int_type = objfile_type (objfile)->builtin_int;
9041
9042 /* Test "int", "long int", and "long long int" objfile types,
9043 and select the first one having a size above or equal to the
9044 architecture address size. */
9045 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9046 base_type = int_type;
9047 else
9048 {
9049 int_type = objfile_type (objfile)->builtin_long;
9050 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9051 base_type = int_type;
9052 else
9053 {
9054 int_type = objfile_type (objfile)->builtin_long_long;
9055 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9056 base_type = int_type;
9057 }
9058 }
9059 }
a02abb62 9060
6e70227d 9061 negative_mask =
43bbcdc2
PH
9062 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9063 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9064 low |= negative_mask;
9065 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9066 high |= negative_mask;
9067
a02abb62
JB
9068 range_type = create_range_type (NULL, base_type, low, high);
9069
bbb0eef6
JK
9070 /* Mark arrays with dynamic length at least as an array of unspecified
9071 length. GDB could check the boundary but before it gets implemented at
9072 least allow accessing the array elements. */
d48323d8 9073 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9074 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9075
c2ff108b
JK
9076 /* Ada expects an empty array on no boundary attributes. */
9077 if (attr == NULL && cu->language != language_ada)
9078 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9079
39cbfefa
DJ
9080 name = dwarf2_name (die, cu);
9081 if (name)
9082 TYPE_NAME (range_type) = name;
6e70227d 9083
e142c38c 9084 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9085 if (attr)
9086 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9087
7e314c57
JK
9088 set_die_type (die, range_type, cu);
9089
9090 /* set_die_type should be already done. */
b4ba55a1
JB
9091 set_descriptive_type (range_type, die, cu);
9092
7e314c57 9093 return range_type;
a02abb62 9094}
6e70227d 9095
f792889a 9096static struct type *
81a17f79
JB
9097read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9098{
9099 struct type *type;
81a17f79 9100
81a17f79
JB
9101 /* For now, we only support the C meaning of an unspecified type: void. */
9102
0114d602
DJ
9103 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9104 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9105
f792889a 9106 return set_die_type (die, type, cu);
81a17f79 9107}
a02abb62 9108
51545339
DJ
9109/* Trivial hash function for die_info: the hash value of a DIE
9110 is its offset in .debug_info for this objfile. */
9111
9112static hashval_t
9113die_hash (const void *item)
9114{
9115 const struct die_info *die = item;
9a619af0 9116
51545339
DJ
9117 return die->offset;
9118}
9119
9120/* Trivial comparison function for die_info structures: two DIEs
9121 are equal if they have the same offset. */
9122
9123static int
9124die_eq (const void *item_lhs, const void *item_rhs)
9125{
9126 const struct die_info *die_lhs = item_lhs;
9127 const struct die_info *die_rhs = item_rhs;
9a619af0 9128
51545339
DJ
9129 return die_lhs->offset == die_rhs->offset;
9130}
9131
c906108c
SS
9132/* Read a whole compilation unit into a linked list of dies. */
9133
f9aca02d 9134static struct die_info *
93311388 9135read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9136{
93311388 9137 struct die_reader_specs reader_specs;
98bfdba5 9138 int read_abbrevs = 0;
1d9ec526 9139 struct cleanup *back_to = NULL;
98bfdba5
PA
9140 struct die_info *die;
9141
9142 if (cu->dwarf2_abbrevs == NULL)
9143 {
9144 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9145 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9146 read_abbrevs = 1;
9147 }
93311388 9148
348e048f 9149 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9150 cu->die_hash
9151 = htab_create_alloc_ex (cu->header.length / 12,
9152 die_hash,
9153 die_eq,
9154 NULL,
9155 &cu->comp_unit_obstack,
9156 hashtab_obstack_allocate,
9157 dummy_obstack_deallocate);
9158
93311388
DE
9159 init_cu_die_reader (&reader_specs, cu);
9160
98bfdba5
PA
9161 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9162
9163 if (read_abbrevs)
9164 do_cleanups (back_to);
9165
9166 return die;
639d11d3
DC
9167}
9168
d97bc12b
DE
9169/* Main entry point for reading a DIE and all children.
9170 Read the DIE and dump it if requested. */
9171
9172static struct die_info *
93311388
DE
9173read_die_and_children (const struct die_reader_specs *reader,
9174 gdb_byte *info_ptr,
d97bc12b
DE
9175 gdb_byte **new_info_ptr,
9176 struct die_info *parent)
9177{
93311388 9178 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9179 new_info_ptr, parent);
9180
9181 if (dwarf2_die_debug)
9182 {
348e048f
DE
9183 fprintf_unfiltered (gdb_stdlog,
9184 "\nRead die from %s of %s:\n",
8b70b953
TT
9185 (reader->cu->per_cu->debug_type_section
9186 ? ".debug_types"
9187 : ".debug_info"),
348e048f 9188 reader->abfd->filename);
d97bc12b
DE
9189 dump_die (result, dwarf2_die_debug);
9190 }
9191
9192 return result;
9193}
9194
639d11d3
DC
9195/* Read a single die and all its descendents. Set the die's sibling
9196 field to NULL; set other fields in the die correctly, and set all
9197 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9198 location of the info_ptr after reading all of those dies. PARENT
9199 is the parent of the die in question. */
9200
9201static struct die_info *
93311388
DE
9202read_die_and_children_1 (const struct die_reader_specs *reader,
9203 gdb_byte *info_ptr,
d97bc12b
DE
9204 gdb_byte **new_info_ptr,
9205 struct die_info *parent)
639d11d3
DC
9206{
9207 struct die_info *die;
fe1b8b76 9208 gdb_byte *cur_ptr;
639d11d3
DC
9209 int has_children;
9210
93311388 9211 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9212 if (die == NULL)
9213 {
9214 *new_info_ptr = cur_ptr;
9215 return NULL;
9216 }
93311388 9217 store_in_ref_table (die, reader->cu);
639d11d3
DC
9218
9219 if (has_children)
348e048f 9220 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9221 else
9222 {
9223 die->child = NULL;
9224 *new_info_ptr = cur_ptr;
9225 }
9226
9227 die->sibling = NULL;
9228 die->parent = parent;
9229 return die;
9230}
9231
9232/* Read a die, all of its descendents, and all of its siblings; set
9233 all of the fields of all of the dies correctly. Arguments are as
9234 in read_die_and_children. */
9235
9236static struct die_info *
93311388
DE
9237read_die_and_siblings (const struct die_reader_specs *reader,
9238 gdb_byte *info_ptr,
fe1b8b76 9239 gdb_byte **new_info_ptr,
639d11d3
DC
9240 struct die_info *parent)
9241{
9242 struct die_info *first_die, *last_sibling;
fe1b8b76 9243 gdb_byte *cur_ptr;
639d11d3 9244
c906108c 9245 cur_ptr = info_ptr;
639d11d3
DC
9246 first_die = last_sibling = NULL;
9247
9248 while (1)
c906108c 9249 {
639d11d3 9250 struct die_info *die
93311388 9251 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9252
1d325ec1 9253 if (die == NULL)
c906108c 9254 {
639d11d3
DC
9255 *new_info_ptr = cur_ptr;
9256 return first_die;
c906108c 9257 }
1d325ec1
DJ
9258
9259 if (!first_die)
9260 first_die = die;
c906108c 9261 else
1d325ec1
DJ
9262 last_sibling->sibling = die;
9263
9264 last_sibling = die;
c906108c 9265 }
c906108c
SS
9266}
9267
93311388
DE
9268/* Read the die from the .debug_info section buffer. Set DIEP to
9269 point to a newly allocated die with its information, except for its
9270 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9271 whether the die has children or not. */
9272
9273static gdb_byte *
9274read_full_die (const struct die_reader_specs *reader,
9275 struct die_info **diep, gdb_byte *info_ptr,
9276 int *has_children)
9277{
9278 unsigned int abbrev_number, bytes_read, i, offset;
9279 struct abbrev_info *abbrev;
9280 struct die_info *die;
9281 struct dwarf2_cu *cu = reader->cu;
9282 bfd *abfd = reader->abfd;
9283
9284 offset = info_ptr - reader->buffer;
9285 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9286 info_ptr += bytes_read;
9287 if (!abbrev_number)
9288 {
9289 *diep = NULL;
9290 *has_children = 0;
9291 return info_ptr;
9292 }
9293
9294 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9295 if (!abbrev)
348e048f
DE
9296 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9297 abbrev_number,
9298 bfd_get_filename (abfd));
9299
93311388
DE
9300 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9301 die->offset = offset;
9302 die->tag = abbrev->tag;
9303 die->abbrev = abbrev_number;
9304
9305 die->num_attrs = abbrev->num_attrs;
9306
9307 for (i = 0; i < abbrev->num_attrs; ++i)
9308 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9309 abfd, info_ptr, cu);
9310
9311 *diep = die;
9312 *has_children = abbrev->has_children;
9313 return info_ptr;
9314}
9315
c906108c
SS
9316/* In DWARF version 2, the description of the debugging information is
9317 stored in a separate .debug_abbrev section. Before we read any
9318 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9319 in a hash table. This function also sets flags in CU describing
9320 the data found in the abbrev table. */
c906108c
SS
9321
9322static void
e7c27a73 9323dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 9324{
e7c27a73 9325 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9326 gdb_byte *abbrev_ptr;
c906108c
SS
9327 struct abbrev_info *cur_abbrev;
9328 unsigned int abbrev_number, bytes_read, abbrev_name;
9329 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9330 struct attr_abbrev *cur_attrs;
9331 unsigned int allocated_attrs;
c906108c 9332
0963b4bd 9333 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9334 obstack_init (&cu->abbrev_obstack);
9335 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9336 (ABBREV_HASH_SIZE
9337 * sizeof (struct abbrev_info *)));
9338 memset (cu->dwarf2_abbrevs, 0,
9339 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9340
be391dca
TT
9341 dwarf2_read_section (dwarf2_per_objfile->objfile,
9342 &dwarf2_per_objfile->abbrev);
dce234bc 9343 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9344 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9345 abbrev_ptr += bytes_read;
9346
f3dd6933
DJ
9347 allocated_attrs = ATTR_ALLOC_CHUNK;
9348 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9349
0963b4bd 9350 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9351 while (abbrev_number)
9352 {
f3dd6933 9353 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9354
9355 /* read in abbrev header */
9356 cur_abbrev->number = abbrev_number;
9357 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9358 abbrev_ptr += bytes_read;
9359 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9360 abbrev_ptr += 1;
9361
72bf9492
DJ
9362 if (cur_abbrev->tag == DW_TAG_namespace)
9363 cu->has_namespace_info = 1;
9364
c906108c
SS
9365 /* now read in declarations */
9366 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9367 abbrev_ptr += bytes_read;
9368 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9369 abbrev_ptr += bytes_read;
9370 while (abbrev_name)
9371 {
f3dd6933 9372 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9373 {
f3dd6933
DJ
9374 allocated_attrs += ATTR_ALLOC_CHUNK;
9375 cur_attrs
9376 = xrealloc (cur_attrs, (allocated_attrs
9377 * sizeof (struct attr_abbrev)));
c906108c 9378 }
ae038cb0
DJ
9379
9380 /* Record whether this compilation unit might have
9381 inter-compilation-unit references. If we don't know what form
9382 this attribute will have, then it might potentially be a
9383 DW_FORM_ref_addr, so we conservatively expect inter-CU
9384 references. */
9385
9386 if (abbrev_form == DW_FORM_ref_addr
9387 || abbrev_form == DW_FORM_indirect)
9388 cu->has_form_ref_addr = 1;
9389
f3dd6933
DJ
9390 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9391 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9392 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9393 abbrev_ptr += bytes_read;
9394 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9395 abbrev_ptr += bytes_read;
9396 }
9397
f3dd6933
DJ
9398 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9399 (cur_abbrev->num_attrs
9400 * sizeof (struct attr_abbrev)));
9401 memcpy (cur_abbrev->attrs, cur_attrs,
9402 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9403
c906108c 9404 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9405 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9406 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9407
9408 /* Get next abbreviation.
9409 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9410 always properly terminated with an abbrev number of 0.
9411 Exit loop if we encounter an abbreviation which we have
9412 already read (which means we are about to read the abbreviations
9413 for the next compile unit) or if the end of the abbreviation
9414 table is reached. */
dce234bc
PP
9415 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9416 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9417 break;
9418 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9419 abbrev_ptr += bytes_read;
e7c27a73 9420 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9421 break;
9422 }
f3dd6933
DJ
9423
9424 xfree (cur_attrs);
c906108c
SS
9425}
9426
f3dd6933 9427/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9428
c906108c 9429static void
f3dd6933 9430dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9431{
f3dd6933 9432 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9433
f3dd6933
DJ
9434 obstack_free (&cu->abbrev_obstack, NULL);
9435 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9436}
9437
9438/* Lookup an abbrev_info structure in the abbrev hash table. */
9439
9440static struct abbrev_info *
e7c27a73 9441dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9442{
9443 unsigned int hash_number;
9444 struct abbrev_info *abbrev;
9445
9446 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9447 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9448
9449 while (abbrev)
9450 {
9451 if (abbrev->number == number)
9452 return abbrev;
9453 else
9454 abbrev = abbrev->next;
9455 }
9456 return NULL;
9457}
9458
72bf9492
DJ
9459/* Returns nonzero if TAG represents a type that we might generate a partial
9460 symbol for. */
9461
9462static int
9463is_type_tag_for_partial (int tag)
9464{
9465 switch (tag)
9466 {
9467#if 0
9468 /* Some types that would be reasonable to generate partial symbols for,
9469 that we don't at present. */
9470 case DW_TAG_array_type:
9471 case DW_TAG_file_type:
9472 case DW_TAG_ptr_to_member_type:
9473 case DW_TAG_set_type:
9474 case DW_TAG_string_type:
9475 case DW_TAG_subroutine_type:
9476#endif
9477 case DW_TAG_base_type:
9478 case DW_TAG_class_type:
680b30c7 9479 case DW_TAG_interface_type:
72bf9492
DJ
9480 case DW_TAG_enumeration_type:
9481 case DW_TAG_structure_type:
9482 case DW_TAG_subrange_type:
9483 case DW_TAG_typedef:
9484 case DW_TAG_union_type:
9485 return 1;
9486 default:
9487 return 0;
9488 }
9489}
9490
9491/* Load all DIEs that are interesting for partial symbols into memory. */
9492
9493static struct partial_die_info *
93311388
DE
9494load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9495 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
9496{
9497 struct partial_die_info *part_die;
9498 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9499 struct abbrev_info *abbrev;
9500 unsigned int bytes_read;
5afb4e99 9501 unsigned int load_all = 0;
72bf9492
DJ
9502
9503 int nesting_level = 1;
9504
9505 parent_die = NULL;
9506 last_die = NULL;
9507
5afb4e99
DJ
9508 if (cu->per_cu && cu->per_cu->load_all_dies)
9509 load_all = 1;
9510
72bf9492
DJ
9511 cu->partial_dies
9512 = htab_create_alloc_ex (cu->header.length / 12,
9513 partial_die_hash,
9514 partial_die_eq,
9515 NULL,
9516 &cu->comp_unit_obstack,
9517 hashtab_obstack_allocate,
9518 dummy_obstack_deallocate);
9519
9520 part_die = obstack_alloc (&cu->comp_unit_obstack,
9521 sizeof (struct partial_die_info));
9522
9523 while (1)
9524 {
9525 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9526
9527 /* A NULL abbrev means the end of a series of children. */
9528 if (abbrev == NULL)
9529 {
9530 if (--nesting_level == 0)
9531 {
9532 /* PART_DIE was probably the last thing allocated on the
9533 comp_unit_obstack, so we could call obstack_free
9534 here. We don't do that because the waste is small,
9535 and will be cleaned up when we're done with this
9536 compilation unit. This way, we're also more robust
9537 against other users of the comp_unit_obstack. */
9538 return first_die;
9539 }
9540 info_ptr += bytes_read;
9541 last_die = parent_die;
9542 parent_die = parent_die->die_parent;
9543 continue;
9544 }
9545
98bfdba5
PA
9546 /* Check for template arguments. We never save these; if
9547 they're seen, we just mark the parent, and go on our way. */
9548 if (parent_die != NULL
9549 && cu->language == language_cplus
9550 && (abbrev->tag == DW_TAG_template_type_param
9551 || abbrev->tag == DW_TAG_template_value_param))
9552 {
9553 parent_die->has_template_arguments = 1;
9554
9555 if (!load_all)
9556 {
9557 /* We don't need a partial DIE for the template argument. */
9558 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9559 cu);
9560 continue;
9561 }
9562 }
9563
9564 /* We only recurse into subprograms looking for template arguments.
9565 Skip their other children. */
9566 if (!load_all
9567 && cu->language == language_cplus
9568 && parent_die != NULL
9569 && parent_die->tag == DW_TAG_subprogram)
9570 {
9571 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9572 continue;
9573 }
9574
5afb4e99
DJ
9575 /* Check whether this DIE is interesting enough to save. Normally
9576 we would not be interested in members here, but there may be
9577 later variables referencing them via DW_AT_specification (for
9578 static members). */
9579 if (!load_all
9580 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9581 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9582 && abbrev->tag != DW_TAG_enumerator
9583 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9584 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9585 && abbrev->tag != DW_TAG_variable
5afb4e99 9586 && abbrev->tag != DW_TAG_namespace
f55ee35c 9587 && abbrev->tag != DW_TAG_module
5afb4e99 9588 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9589 {
9590 /* Otherwise we skip to the next sibling, if any. */
93311388 9591 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9592 continue;
9593 }
9594
93311388
DE
9595 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9596 buffer, info_ptr, cu);
72bf9492
DJ
9597
9598 /* This two-pass algorithm for processing partial symbols has a
9599 high cost in cache pressure. Thus, handle some simple cases
9600 here which cover the majority of C partial symbols. DIEs
9601 which neither have specification tags in them, nor could have
9602 specification tags elsewhere pointing at them, can simply be
9603 processed and discarded.
9604
9605 This segment is also optional; scan_partial_symbols and
9606 add_partial_symbol will handle these DIEs if we chain
9607 them in normally. When compilers which do not emit large
9608 quantities of duplicate debug information are more common,
9609 this code can probably be removed. */
9610
9611 /* Any complete simple types at the top level (pretty much all
9612 of them, for a language without namespaces), can be processed
9613 directly. */
9614 if (parent_die == NULL
9615 && part_die->has_specification == 0
9616 && part_die->is_declaration == 0
d8228535 9617 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9618 || part_die->tag == DW_TAG_base_type
9619 || part_die->tag == DW_TAG_subrange_type))
9620 {
9621 if (building_psymtab && part_die->name != NULL)
04a679b8 9622 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
9623 VAR_DOMAIN, LOC_TYPEDEF,
9624 &cu->objfile->static_psymbols,
9625 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 9626 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9627 continue;
9628 }
9629
d8228535
JK
9630 /* The exception for DW_TAG_typedef with has_children above is
9631 a workaround of GCC PR debug/47510. In the case of this complaint
9632 type_name_no_tag_or_error will error on such types later.
9633
9634 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9635 it could not find the child DIEs referenced later, this is checked
9636 above. In correct DWARF DW_TAG_typedef should have no children. */
9637
9638 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9639 complaint (&symfile_complaints,
9640 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9641 "- DIE at 0x%x [in module %s]"),
9642 part_die->offset, cu->objfile->name);
9643
72bf9492
DJ
9644 /* If we're at the second level, and we're an enumerator, and
9645 our parent has no specification (meaning possibly lives in a
9646 namespace elsewhere), then we can add the partial symbol now
9647 instead of queueing it. */
9648 if (part_die->tag == DW_TAG_enumerator
9649 && parent_die != NULL
9650 && parent_die->die_parent == NULL
9651 && parent_die->tag == DW_TAG_enumeration_type
9652 && parent_die->has_specification == 0)
9653 {
9654 if (part_die->name == NULL)
3e43a32a
MS
9655 complaint (&symfile_complaints,
9656 _("malformed enumerator DIE ignored"));
72bf9492 9657 else if (building_psymtab)
04a679b8 9658 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9659 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9660 (cu->language == language_cplus
9661 || cu->language == language_java)
72bf9492
DJ
9662 ? &cu->objfile->global_psymbols
9663 : &cu->objfile->static_psymbols,
9664 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9665
93311388 9666 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9667 continue;
9668 }
9669
9670 /* We'll save this DIE so link it in. */
9671 part_die->die_parent = parent_die;
9672 part_die->die_sibling = NULL;
9673 part_die->die_child = NULL;
9674
9675 if (last_die && last_die == parent_die)
9676 last_die->die_child = part_die;
9677 else if (last_die)
9678 last_die->die_sibling = part_die;
9679
9680 last_die = part_die;
9681
9682 if (first_die == NULL)
9683 first_die = part_die;
9684
9685 /* Maybe add the DIE to the hash table. Not all DIEs that we
9686 find interesting need to be in the hash table, because we
9687 also have the parent/sibling/child chains; only those that we
9688 might refer to by offset later during partial symbol reading.
9689
9690 For now this means things that might have be the target of a
9691 DW_AT_specification, DW_AT_abstract_origin, or
9692 DW_AT_extension. DW_AT_extension will refer only to
9693 namespaces; DW_AT_abstract_origin refers to functions (and
9694 many things under the function DIE, but we do not recurse
9695 into function DIEs during partial symbol reading) and
9696 possibly variables as well; DW_AT_specification refers to
9697 declarations. Declarations ought to have the DW_AT_declaration
9698 flag. It happens that GCC forgets to put it in sometimes, but
9699 only for functions, not for types.
9700
9701 Adding more things than necessary to the hash table is harmless
9702 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9703 wasted time in find_partial_die, when we reread the compilation
9704 unit with load_all_dies set. */
72bf9492 9705
5afb4e99 9706 if (load_all
72929c62 9707 || abbrev->tag == DW_TAG_constant
5afb4e99 9708 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9709 || abbrev->tag == DW_TAG_variable
9710 || abbrev->tag == DW_TAG_namespace
9711 || part_die->is_declaration)
9712 {
9713 void **slot;
9714
9715 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9716 part_die->offset, INSERT);
9717 *slot = part_die;
9718 }
9719
9720 part_die = obstack_alloc (&cu->comp_unit_obstack,
9721 sizeof (struct partial_die_info));
9722
9723 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9724 we have no reason to follow the children of structures; for other
98bfdba5
PA
9725 languages we have to, so that we can get at method physnames
9726 to infer fully qualified class names, for DW_AT_specification,
9727 and for C++ template arguments. For C++, we also look one level
9728 inside functions to find template arguments (if the name of the
9729 function does not already contain the template arguments).
bc30ff58
JB
9730
9731 For Ada, we need to scan the children of subprograms and lexical
9732 blocks as well because Ada allows the definition of nested
9733 entities that could be interesting for the debugger, such as
9734 nested subprograms for instance. */
72bf9492 9735 if (last_die->has_children
5afb4e99
DJ
9736 && (load_all
9737 || last_die->tag == DW_TAG_namespace
f55ee35c 9738 || last_die->tag == DW_TAG_module
72bf9492 9739 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9740 || (cu->language == language_cplus
9741 && last_die->tag == DW_TAG_subprogram
9742 && (last_die->name == NULL
9743 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9744 || (cu->language != language_c
9745 && (last_die->tag == DW_TAG_class_type
680b30c7 9746 || last_die->tag == DW_TAG_interface_type
72bf9492 9747 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9748 || last_die->tag == DW_TAG_union_type))
9749 || (cu->language == language_ada
9750 && (last_die->tag == DW_TAG_subprogram
9751 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9752 {
9753 nesting_level++;
9754 parent_die = last_die;
9755 continue;
9756 }
9757
9758 /* Otherwise we skip to the next sibling, if any. */
93311388 9759 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9760
9761 /* Back to the top, do it again. */
9762 }
9763}
9764
c906108c
SS
9765/* Read a minimal amount of information into the minimal die structure. */
9766
fe1b8b76 9767static gdb_byte *
72bf9492
DJ
9768read_partial_die (struct partial_die_info *part_die,
9769 struct abbrev_info *abbrev,
9770 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9771 gdb_byte *buffer, gdb_byte *info_ptr,
9772 struct dwarf2_cu *cu)
c906108c 9773{
fa238c03 9774 unsigned int i;
c906108c 9775 struct attribute attr;
c5aa993b 9776 int has_low_pc_attr = 0;
c906108c
SS
9777 int has_high_pc_attr = 0;
9778
72bf9492 9779 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9780
93311388 9781 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9782
9783 info_ptr += abbrev_len;
9784
9785 if (abbrev == NULL)
9786 return info_ptr;
9787
c906108c
SS
9788 part_die->tag = abbrev->tag;
9789 part_die->has_children = abbrev->has_children;
c906108c
SS
9790
9791 for (i = 0; i < abbrev->num_attrs; ++i)
9792 {
e7c27a73 9793 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9794
9795 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9796 partial symbol table. */
c906108c
SS
9797 switch (attr.name)
9798 {
9799 case DW_AT_name:
71c25dea
TT
9800 switch (part_die->tag)
9801 {
9802 case DW_TAG_compile_unit:
348e048f 9803 case DW_TAG_type_unit:
71c25dea
TT
9804 /* Compilation units have a DW_AT_name that is a filename, not
9805 a source language identifier. */
9806 case DW_TAG_enumeration_type:
9807 case DW_TAG_enumerator:
9808 /* These tags always have simple identifiers already; no need
9809 to canonicalize them. */
9810 part_die->name = DW_STRING (&attr);
9811 break;
9812 default:
9813 part_die->name
9814 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9815 &cu->objfile->objfile_obstack);
71c25dea
TT
9816 break;
9817 }
c906108c 9818 break;
31ef98ae 9819 case DW_AT_linkage_name:
c906108c 9820 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9821 /* Note that both forms of linkage name might appear. We
9822 assume they will be the same, and we only store the last
9823 one we see. */
94af9270
KS
9824 if (cu->language == language_ada)
9825 part_die->name = DW_STRING (&attr);
abc72ce4 9826 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9827 break;
9828 case DW_AT_low_pc:
9829 has_low_pc_attr = 1;
9830 part_die->lowpc = DW_ADDR (&attr);
9831 break;
9832 case DW_AT_high_pc:
9833 has_high_pc_attr = 1;
9834 part_die->highpc = DW_ADDR (&attr);
9835 break;
9836 case DW_AT_location:
0963b4bd 9837 /* Support the .debug_loc offsets. */
8e19ed76
PS
9838 if (attr_form_is_block (&attr))
9839 {
9840 part_die->locdesc = DW_BLOCK (&attr);
9841 }
3690dd37 9842 else if (attr_form_is_section_offset (&attr))
8e19ed76 9843 {
4d3c2250 9844 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9845 }
9846 else
9847 {
4d3c2250
KB
9848 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9849 "partial symbol information");
8e19ed76 9850 }
c906108c 9851 break;
c906108c
SS
9852 case DW_AT_external:
9853 part_die->is_external = DW_UNSND (&attr);
9854 break;
9855 case DW_AT_declaration:
9856 part_die->is_declaration = DW_UNSND (&attr);
9857 break;
9858 case DW_AT_type:
9859 part_die->has_type = 1;
9860 break;
9861 case DW_AT_abstract_origin:
9862 case DW_AT_specification:
72bf9492
DJ
9863 case DW_AT_extension:
9864 part_die->has_specification = 1;
c764a876 9865 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9866 break;
9867 case DW_AT_sibling:
9868 /* Ignore absolute siblings, they might point outside of
9869 the current compile unit. */
9870 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9871 complaint (&symfile_complaints,
9872 _("ignoring absolute DW_AT_sibling"));
c906108c 9873 else
93311388 9874 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9875 break;
fa4028e9
JB
9876 case DW_AT_byte_size:
9877 part_die->has_byte_size = 1;
9878 break;
68511cec
CES
9879 case DW_AT_calling_convention:
9880 /* DWARF doesn't provide a way to identify a program's source-level
9881 entry point. DW_AT_calling_convention attributes are only meant
9882 to describe functions' calling conventions.
9883
9884 However, because it's a necessary piece of information in
9885 Fortran, and because DW_CC_program is the only piece of debugging
9886 information whose definition refers to a 'main program' at all,
9887 several compilers have begun marking Fortran main programs with
9888 DW_CC_program --- even when those functions use the standard
9889 calling conventions.
9890
9891 So until DWARF specifies a way to provide this information and
9892 compilers pick up the new representation, we'll support this
9893 practice. */
9894 if (DW_UNSND (&attr) == DW_CC_program
9895 && cu->language == language_fortran)
01f8c46d
JK
9896 {
9897 set_main_name (part_die->name);
9898
9899 /* As this DIE has a static linkage the name would be difficult
9900 to look up later. */
9901 language_of_main = language_fortran;
9902 }
68511cec 9903 break;
c906108c
SS
9904 default:
9905 break;
9906 }
9907 }
9908
9373cf26
JK
9909 if (has_low_pc_attr && has_high_pc_attr)
9910 {
9911 /* When using the GNU linker, .gnu.linkonce. sections are used to
9912 eliminate duplicate copies of functions and vtables and such.
9913 The linker will arbitrarily choose one and discard the others.
9914 The AT_*_pc values for such functions refer to local labels in
9915 these sections. If the section from that file was discarded, the
9916 labels are not in the output, so the relocs get a value of 0.
9917 If this is a discarded function, mark the pc bounds as invalid,
9918 so that GDB will ignore it. */
9919 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9920 {
9921 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9922
9923 complaint (&symfile_complaints,
9924 _("DW_AT_low_pc %s is zero "
9925 "for DIE at 0x%x [in module %s]"),
9926 paddress (gdbarch, part_die->lowpc),
9927 part_die->offset, cu->objfile->name);
9928 }
9929 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9930 else if (part_die->lowpc >= part_die->highpc)
9931 {
9932 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9933
9934 complaint (&symfile_complaints,
9935 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9936 "for DIE at 0x%x [in module %s]"),
9937 paddress (gdbarch, part_die->lowpc),
9938 paddress (gdbarch, part_die->highpc),
9939 part_die->offset, cu->objfile->name);
9940 }
9941 else
9942 part_die->has_pc_info = 1;
9943 }
85cbf3d3 9944
c906108c
SS
9945 return info_ptr;
9946}
9947
72bf9492
DJ
9948/* Find a cached partial DIE at OFFSET in CU. */
9949
9950static struct partial_die_info *
c764a876 9951find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9952{
9953 struct partial_die_info *lookup_die = NULL;
9954 struct partial_die_info part_die;
9955
9956 part_die.offset = offset;
9957 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9958
72bf9492
DJ
9959 return lookup_die;
9960}
9961
348e048f
DE
9962/* Find a partial DIE at OFFSET, which may or may not be in CU,
9963 except in the case of .debug_types DIEs which do not reference
9964 outside their CU (they do however referencing other types via
55f1336d 9965 DW_FORM_ref_sig8). */
72bf9492
DJ
9966
9967static struct partial_die_info *
c764a876 9968find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9969{
5afb4e99
DJ
9970 struct dwarf2_per_cu_data *per_cu = NULL;
9971 struct partial_die_info *pd = NULL;
72bf9492 9972
8b70b953 9973 if (cu->per_cu->debug_type_section)
348e048f
DE
9974 {
9975 pd = find_partial_die_in_comp_unit (offset, cu);
9976 if (pd != NULL)
9977 return pd;
9978 goto not_found;
9979 }
9980
45452591 9981 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9982 {
9983 pd = find_partial_die_in_comp_unit (offset, cu);
9984 if (pd != NULL)
9985 return pd;
9986 }
72bf9492 9987
ae038cb0
DJ
9988 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9989
98bfdba5
PA
9990 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9991 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9992
9993 per_cu->cu->last_used = 0;
5afb4e99
DJ
9994 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9995
9996 if (pd == NULL && per_cu->load_all_dies == 0)
9997 {
9998 struct cleanup *back_to;
9999 struct partial_die_info comp_unit_die;
10000 struct abbrev_info *abbrev;
10001 unsigned int bytes_read;
10002 char *info_ptr;
10003
10004 per_cu->load_all_dies = 1;
10005
10006 /* Re-read the DIEs. */
10007 back_to = make_cleanup (null_cleanup, 0);
10008 if (per_cu->cu->dwarf2_abbrevs == NULL)
10009 {
10010 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 10011 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 10012 }
dce234bc 10013 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
10014 + per_cu->cu->header.offset
10015 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
10016 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10017 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
10018 per_cu->cu->objfile->obfd,
10019 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
10020 per_cu->cu);
10021 if (comp_unit_die.has_children)
93311388
DE
10022 load_partial_dies (per_cu->cu->objfile->obfd,
10023 dwarf2_per_objfile->info.buffer, info_ptr,
10024 0, per_cu->cu);
5afb4e99
DJ
10025 do_cleanups (back_to);
10026
10027 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10028 }
10029
348e048f
DE
10030 not_found:
10031
5afb4e99
DJ
10032 if (pd == NULL)
10033 internal_error (__FILE__, __LINE__,
3e43a32a
MS
10034 _("could not find partial DIE 0x%x "
10035 "in cache [from module %s]\n"),
5afb4e99
DJ
10036 offset, bfd_get_filename (cu->objfile->obfd));
10037 return pd;
72bf9492
DJ
10038}
10039
abc72ce4
DE
10040/* See if we can figure out if the class lives in a namespace. We do
10041 this by looking for a member function; its demangled name will
10042 contain namespace info, if there is any. */
10043
10044static void
10045guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10046 struct dwarf2_cu *cu)
10047{
10048 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10049 what template types look like, because the demangler
10050 frequently doesn't give the same name as the debug info. We
10051 could fix this by only using the demangled name to get the
10052 prefix (but see comment in read_structure_type). */
10053
10054 struct partial_die_info *real_pdi;
10055 struct partial_die_info *child_pdi;
10056
10057 /* If this DIE (this DIE's specification, if any) has a parent, then
10058 we should not do this. We'll prepend the parent's fully qualified
10059 name when we create the partial symbol. */
10060
10061 real_pdi = struct_pdi;
10062 while (real_pdi->has_specification)
10063 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10064
10065 if (real_pdi->die_parent != NULL)
10066 return;
10067
10068 for (child_pdi = struct_pdi->die_child;
10069 child_pdi != NULL;
10070 child_pdi = child_pdi->die_sibling)
10071 {
10072 if (child_pdi->tag == DW_TAG_subprogram
10073 && child_pdi->linkage_name != NULL)
10074 {
10075 char *actual_class_name
10076 = language_class_name_from_physname (cu->language_defn,
10077 child_pdi->linkage_name);
10078 if (actual_class_name != NULL)
10079 {
10080 struct_pdi->name
10081 = obsavestring (actual_class_name,
10082 strlen (actual_class_name),
10083 &cu->objfile->objfile_obstack);
10084 xfree (actual_class_name);
10085 }
10086 break;
10087 }
10088 }
10089}
10090
72bf9492
DJ
10091/* Adjust PART_DIE before generating a symbol for it. This function
10092 may set the is_external flag or change the DIE's name. */
10093
10094static void
10095fixup_partial_die (struct partial_die_info *part_die,
10096 struct dwarf2_cu *cu)
10097{
abc72ce4
DE
10098 /* Once we've fixed up a die, there's no point in doing so again.
10099 This also avoids a memory leak if we were to call
10100 guess_partial_die_structure_name multiple times. */
10101 if (part_die->fixup_called)
10102 return;
10103
72bf9492
DJ
10104 /* If we found a reference attribute and the DIE has no name, try
10105 to find a name in the referred to DIE. */
10106
10107 if (part_die->name == NULL && part_die->has_specification)
10108 {
10109 struct partial_die_info *spec_die;
72bf9492 10110
10b3939b 10111 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10112
10b3939b 10113 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10114
10115 if (spec_die->name)
10116 {
10117 part_die->name = spec_die->name;
10118
10119 /* Copy DW_AT_external attribute if it is set. */
10120 if (spec_die->is_external)
10121 part_die->is_external = spec_die->is_external;
10122 }
10123 }
10124
10125 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10126
10127 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10128 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10129
abc72ce4
DE
10130 /* If there is no parent die to provide a namespace, and there are
10131 children, see if we can determine the namespace from their linkage
10132 name.
10133 NOTE: We need to do this even if cu->has_namespace_info != 0.
10134 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10135 if (cu->language == language_cplus
8b70b953 10136 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10137 && part_die->die_parent == NULL
10138 && part_die->has_children
10139 && (part_die->tag == DW_TAG_class_type
10140 || part_die->tag == DW_TAG_structure_type
10141 || part_die->tag == DW_TAG_union_type))
10142 guess_partial_die_structure_name (part_die, cu);
10143
53832f31
TT
10144 /* GCC might emit a nameless struct or union that has a linkage
10145 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10146 if (part_die->name == NULL
96408a79
SA
10147 && (part_die->tag == DW_TAG_class_type
10148 || part_die->tag == DW_TAG_interface_type
10149 || part_die->tag == DW_TAG_structure_type
10150 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10151 && part_die->linkage_name != NULL)
10152 {
10153 char *demangled;
10154
10155 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10156 if (demangled)
10157 {
96408a79
SA
10158 const char *base;
10159
10160 /* Strip any leading namespaces/classes, keep only the base name.
10161 DW_AT_name for named DIEs does not contain the prefixes. */
10162 base = strrchr (demangled, ':');
10163 if (base && base > demangled && base[-1] == ':')
10164 base++;
10165 else
10166 base = demangled;
10167
10168 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10169 &cu->objfile->objfile_obstack);
10170 xfree (demangled);
10171 }
10172 }
10173
abc72ce4 10174 part_die->fixup_called = 1;
72bf9492
DJ
10175}
10176
a8329558 10177/* Read an attribute value described by an attribute form. */
c906108c 10178
fe1b8b76 10179static gdb_byte *
a8329558 10180read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10181 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10182 struct dwarf2_cu *cu)
c906108c 10183{
e7c27a73 10184 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10185 unsigned int bytes_read;
10186 struct dwarf_block *blk;
10187
a8329558
KW
10188 attr->form = form;
10189 switch (form)
c906108c 10190 {
c906108c 10191 case DW_FORM_ref_addr:
ae411497
TT
10192 if (cu->header.version == 2)
10193 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10194 else
3e43a32a
MS
10195 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10196 &cu->header, &bytes_read);
ae411497
TT
10197 info_ptr += bytes_read;
10198 break;
10199 case DW_FORM_addr:
e7c27a73 10200 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10201 info_ptr += bytes_read;
c906108c
SS
10202 break;
10203 case DW_FORM_block2:
7b5a2f43 10204 blk = dwarf_alloc_block (cu);
c906108c
SS
10205 blk->size = read_2_bytes (abfd, info_ptr);
10206 info_ptr += 2;
10207 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10208 info_ptr += blk->size;
10209 DW_BLOCK (attr) = blk;
10210 break;
10211 case DW_FORM_block4:
7b5a2f43 10212 blk = dwarf_alloc_block (cu);
c906108c
SS
10213 blk->size = read_4_bytes (abfd, info_ptr);
10214 info_ptr += 4;
10215 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10216 info_ptr += blk->size;
10217 DW_BLOCK (attr) = blk;
10218 break;
10219 case DW_FORM_data2:
10220 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10221 info_ptr += 2;
10222 break;
10223 case DW_FORM_data4:
10224 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10225 info_ptr += 4;
10226 break;
10227 case DW_FORM_data8:
10228 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10229 info_ptr += 8;
10230 break;
2dc7f7b3
TT
10231 case DW_FORM_sec_offset:
10232 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10233 info_ptr += bytes_read;
10234 break;
c906108c 10235 case DW_FORM_string:
9b1c24c8 10236 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10237 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10238 info_ptr += bytes_read;
10239 break;
4bdf3d34
JJ
10240 case DW_FORM_strp:
10241 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10242 &bytes_read);
8285870a 10243 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10244 info_ptr += bytes_read;
10245 break;
2dc7f7b3 10246 case DW_FORM_exprloc:
c906108c 10247 case DW_FORM_block:
7b5a2f43 10248 blk = dwarf_alloc_block (cu);
c906108c
SS
10249 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10250 info_ptr += bytes_read;
10251 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10252 info_ptr += blk->size;
10253 DW_BLOCK (attr) = blk;
10254 break;
10255 case DW_FORM_block1:
7b5a2f43 10256 blk = dwarf_alloc_block (cu);
c906108c
SS
10257 blk->size = read_1_byte (abfd, info_ptr);
10258 info_ptr += 1;
10259 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10260 info_ptr += blk->size;
10261 DW_BLOCK (attr) = blk;
10262 break;
10263 case DW_FORM_data1:
10264 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10265 info_ptr += 1;
10266 break;
10267 case DW_FORM_flag:
10268 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10269 info_ptr += 1;
10270 break;
2dc7f7b3
TT
10271 case DW_FORM_flag_present:
10272 DW_UNSND (attr) = 1;
10273 break;
c906108c
SS
10274 case DW_FORM_sdata:
10275 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10276 info_ptr += bytes_read;
10277 break;
10278 case DW_FORM_udata:
10279 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10280 info_ptr += bytes_read;
10281 break;
10282 case DW_FORM_ref1:
10b3939b 10283 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
10284 info_ptr += 1;
10285 break;
10286 case DW_FORM_ref2:
10b3939b 10287 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
10288 info_ptr += 2;
10289 break;
10290 case DW_FORM_ref4:
10b3939b 10291 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
10292 info_ptr += 4;
10293 break;
613e1657 10294 case DW_FORM_ref8:
10b3939b 10295 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
10296 info_ptr += 8;
10297 break;
55f1336d 10298 case DW_FORM_ref_sig8:
348e048f
DE
10299 /* Convert the signature to something we can record in DW_UNSND
10300 for later lookup.
10301 NOTE: This is NULL if the type wasn't found. */
10302 DW_SIGNATURED_TYPE (attr) =
10303 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10304 info_ptr += 8;
10305 break;
c906108c 10306 case DW_FORM_ref_udata:
10b3939b
DJ
10307 DW_ADDR (attr) = (cu->header.offset
10308 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10309 info_ptr += bytes_read;
10310 break;
c906108c 10311 case DW_FORM_indirect:
a8329558
KW
10312 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10313 info_ptr += bytes_read;
e7c27a73 10314 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10315 break;
c906108c 10316 default:
8a3fe4f8 10317 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10318 dwarf_form_name (form),
10319 bfd_get_filename (abfd));
c906108c 10320 }
28e94949
JB
10321
10322 /* We have seen instances where the compiler tried to emit a byte
10323 size attribute of -1 which ended up being encoded as an unsigned
10324 0xffffffff. Although 0xffffffff is technically a valid size value,
10325 an object of this size seems pretty unlikely so we can relatively
10326 safely treat these cases as if the size attribute was invalid and
10327 treat them as zero by default. */
10328 if (attr->name == DW_AT_byte_size
10329 && form == DW_FORM_data4
10330 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10331 {
10332 complaint
10333 (&symfile_complaints,
43bbcdc2
PH
10334 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10335 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10336 DW_UNSND (attr) = 0;
10337 }
28e94949 10338
c906108c
SS
10339 return info_ptr;
10340}
10341
a8329558
KW
10342/* Read an attribute described by an abbreviated attribute. */
10343
fe1b8b76 10344static gdb_byte *
a8329558 10345read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10346 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10347{
10348 attr->name = abbrev->name;
e7c27a73 10349 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10350}
10351
0963b4bd 10352/* Read dwarf information from a buffer. */
c906108c
SS
10353
10354static unsigned int
fe1b8b76 10355read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10356{
fe1b8b76 10357 return bfd_get_8 (abfd, buf);
c906108c
SS
10358}
10359
10360static int
fe1b8b76 10361read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10362{
fe1b8b76 10363 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10364}
10365
10366static unsigned int
fe1b8b76 10367read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10368{
fe1b8b76 10369 return bfd_get_16 (abfd, buf);
c906108c
SS
10370}
10371
21ae7a4d
JK
10372static int
10373read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10374{
10375 return bfd_get_signed_16 (abfd, buf);
10376}
10377
c906108c 10378static unsigned int
fe1b8b76 10379read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10380{
fe1b8b76 10381 return bfd_get_32 (abfd, buf);
c906108c
SS
10382}
10383
21ae7a4d
JK
10384static int
10385read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10386{
10387 return bfd_get_signed_32 (abfd, buf);
10388}
10389
93311388 10390static ULONGEST
fe1b8b76 10391read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10392{
fe1b8b76 10393 return bfd_get_64 (abfd, buf);
c906108c
SS
10394}
10395
10396static CORE_ADDR
fe1b8b76 10397read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10398 unsigned int *bytes_read)
c906108c 10399{
e7c27a73 10400 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10401 CORE_ADDR retval = 0;
10402
107d2387 10403 if (cu_header->signed_addr_p)
c906108c 10404 {
107d2387
AC
10405 switch (cu_header->addr_size)
10406 {
10407 case 2:
fe1b8b76 10408 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10409 break;
10410 case 4:
fe1b8b76 10411 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10412 break;
10413 case 8:
fe1b8b76 10414 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10415 break;
10416 default:
8e65ff28 10417 internal_error (__FILE__, __LINE__,
e2e0b3e5 10418 _("read_address: bad switch, signed [in module %s]"),
659b0389 10419 bfd_get_filename (abfd));
107d2387
AC
10420 }
10421 }
10422 else
10423 {
10424 switch (cu_header->addr_size)
10425 {
10426 case 2:
fe1b8b76 10427 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10428 break;
10429 case 4:
fe1b8b76 10430 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10431 break;
10432 case 8:
fe1b8b76 10433 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10434 break;
10435 default:
8e65ff28 10436 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10437 _("read_address: bad switch, "
10438 "unsigned [in module %s]"),
659b0389 10439 bfd_get_filename (abfd));
107d2387 10440 }
c906108c 10441 }
64367e0a 10442
107d2387
AC
10443 *bytes_read = cu_header->addr_size;
10444 return retval;
c906108c
SS
10445}
10446
f7ef9339 10447/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10448 specification allows the initial length to take up either 4 bytes
10449 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10450 bytes describe the length and all offsets will be 8 bytes in length
10451 instead of 4.
10452
f7ef9339
KB
10453 An older, non-standard 64-bit format is also handled by this
10454 function. The older format in question stores the initial length
10455 as an 8-byte quantity without an escape value. Lengths greater
10456 than 2^32 aren't very common which means that the initial 4 bytes
10457 is almost always zero. Since a length value of zero doesn't make
10458 sense for the 32-bit format, this initial zero can be considered to
10459 be an escape value which indicates the presence of the older 64-bit
10460 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10461 greater than 4GB. If it becomes necessary to handle lengths
10462 somewhat larger than 4GB, we could allow other small values (such
10463 as the non-sensical values of 1, 2, and 3) to also be used as
10464 escape values indicating the presence of the old format.
f7ef9339 10465
917c78fc
MK
10466 The value returned via bytes_read should be used to increment the
10467 relevant pointer after calling read_initial_length().
c764a876 10468
613e1657
KB
10469 [ Note: read_initial_length() and read_offset() are based on the
10470 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10471 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10472 from:
10473
f7ef9339 10474 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10475
613e1657
KB
10476 This document is only a draft and is subject to change. (So beware.)
10477
f7ef9339 10478 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10479 determined empirically by examining 64-bit ELF files produced by
10480 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10481
10482 - Kevin, July 16, 2002
613e1657
KB
10483 ] */
10484
10485static LONGEST
c764a876 10486read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10487{
fe1b8b76 10488 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10489
dd373385 10490 if (length == 0xffffffff)
613e1657 10491 {
fe1b8b76 10492 length = bfd_get_64 (abfd, buf + 4);
613e1657 10493 *bytes_read = 12;
613e1657 10494 }
dd373385 10495 else if (length == 0)
f7ef9339 10496 {
dd373385 10497 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10498 length = bfd_get_64 (abfd, buf);
f7ef9339 10499 *bytes_read = 8;
f7ef9339 10500 }
613e1657
KB
10501 else
10502 {
10503 *bytes_read = 4;
613e1657
KB
10504 }
10505
c764a876
DE
10506 return length;
10507}
dd373385 10508
c764a876
DE
10509/* Cover function for read_initial_length.
10510 Returns the length of the object at BUF, and stores the size of the
10511 initial length in *BYTES_READ and stores the size that offsets will be in
10512 *OFFSET_SIZE.
10513 If the initial length size is not equivalent to that specified in
10514 CU_HEADER then issue a complaint.
10515 This is useful when reading non-comp-unit headers. */
dd373385 10516
c764a876
DE
10517static LONGEST
10518read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10519 const struct comp_unit_head *cu_header,
10520 unsigned int *bytes_read,
10521 unsigned int *offset_size)
10522{
10523 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10524
10525 gdb_assert (cu_header->initial_length_size == 4
10526 || cu_header->initial_length_size == 8
10527 || cu_header->initial_length_size == 12);
10528
10529 if (cu_header->initial_length_size != *bytes_read)
10530 complaint (&symfile_complaints,
10531 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10532
c764a876 10533 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10534 return length;
613e1657
KB
10535}
10536
10537/* Read an offset from the data stream. The size of the offset is
917c78fc 10538 given by cu_header->offset_size. */
613e1657
KB
10539
10540static LONGEST
fe1b8b76 10541read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10542 unsigned int *bytes_read)
c764a876
DE
10543{
10544 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10545
c764a876
DE
10546 *bytes_read = cu_header->offset_size;
10547 return offset;
10548}
10549
10550/* Read an offset from the data stream. */
10551
10552static LONGEST
10553read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10554{
10555 LONGEST retval = 0;
10556
c764a876 10557 switch (offset_size)
613e1657
KB
10558 {
10559 case 4:
fe1b8b76 10560 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10561 break;
10562 case 8:
fe1b8b76 10563 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10564 break;
10565 default:
8e65ff28 10566 internal_error (__FILE__, __LINE__,
c764a876 10567 _("read_offset_1: bad switch [in module %s]"),
659b0389 10568 bfd_get_filename (abfd));
613e1657
KB
10569 }
10570
917c78fc 10571 return retval;
613e1657
KB
10572}
10573
fe1b8b76
JB
10574static gdb_byte *
10575read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10576{
10577 /* If the size of a host char is 8 bits, we can return a pointer
10578 to the buffer, otherwise we have to copy the data to a buffer
10579 allocated on the temporary obstack. */
4bdf3d34 10580 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10581 return buf;
c906108c
SS
10582}
10583
10584static char *
9b1c24c8 10585read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10586{
10587 /* If the size of a host char is 8 bits, we can return a pointer
10588 to the string, otherwise we have to copy the string to a buffer
10589 allocated on the temporary obstack. */
4bdf3d34 10590 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10591 if (*buf == '\0')
10592 {
10593 *bytes_read_ptr = 1;
10594 return NULL;
10595 }
fe1b8b76
JB
10596 *bytes_read_ptr = strlen ((char *) buf) + 1;
10597 return (char *) buf;
4bdf3d34
JJ
10598}
10599
10600static char *
cf2c3c16 10601read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10602{
be391dca 10603 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10604 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10605 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10606 bfd_get_filename (abfd));
dce234bc 10607 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10608 error (_("DW_FORM_strp pointing outside of "
10609 ".debug_str section [in module %s]"),
10610 bfd_get_filename (abfd));
4bdf3d34 10611 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10612 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10613 return NULL;
dce234bc 10614 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10615}
10616
cf2c3c16
TT
10617static char *
10618read_indirect_string (bfd *abfd, gdb_byte *buf,
10619 const struct comp_unit_head *cu_header,
10620 unsigned int *bytes_read_ptr)
10621{
10622 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10623
10624 return read_indirect_string_at_offset (abfd, str_offset);
10625}
10626
ce5d95e1 10627static unsigned long
fe1b8b76 10628read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10629{
ce5d95e1
JB
10630 unsigned long result;
10631 unsigned int num_read;
c906108c
SS
10632 int i, shift;
10633 unsigned char byte;
10634
10635 result = 0;
10636 shift = 0;
10637 num_read = 0;
10638 i = 0;
10639 while (1)
10640 {
fe1b8b76 10641 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10642 buf++;
10643 num_read++;
ce5d95e1 10644 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10645 if ((byte & 128) == 0)
10646 {
10647 break;
10648 }
10649 shift += 7;
10650 }
10651 *bytes_read_ptr = num_read;
10652 return result;
10653}
10654
ce5d95e1 10655static long
fe1b8b76 10656read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10657{
ce5d95e1 10658 long result;
77e0b926 10659 int i, shift, num_read;
c906108c
SS
10660 unsigned char byte;
10661
10662 result = 0;
10663 shift = 0;
c906108c
SS
10664 num_read = 0;
10665 i = 0;
10666 while (1)
10667 {
fe1b8b76 10668 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10669 buf++;
10670 num_read++;
ce5d95e1 10671 result |= ((long)(byte & 127) << shift);
c906108c
SS
10672 shift += 7;
10673 if ((byte & 128) == 0)
10674 {
10675 break;
10676 }
10677 }
77e0b926
DJ
10678 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10679 result |= -(((long)1) << shift);
c906108c
SS
10680 *bytes_read_ptr = num_read;
10681 return result;
10682}
10683
4bb7a0a7
DJ
10684/* Return a pointer to just past the end of an LEB128 number in BUF. */
10685
fe1b8b76
JB
10686static gdb_byte *
10687skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10688{
10689 int byte;
10690
10691 while (1)
10692 {
fe1b8b76 10693 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10694 buf++;
10695 if ((byte & 128) == 0)
10696 return buf;
10697 }
10698}
10699
c906108c 10700static void
e142c38c 10701set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10702{
10703 switch (lang)
10704 {
10705 case DW_LANG_C89:
76bee0cc 10706 case DW_LANG_C99:
c906108c 10707 case DW_LANG_C:
e142c38c 10708 cu->language = language_c;
c906108c
SS
10709 break;
10710 case DW_LANG_C_plus_plus:
e142c38c 10711 cu->language = language_cplus;
c906108c 10712 break;
6aecb9c2
JB
10713 case DW_LANG_D:
10714 cu->language = language_d;
10715 break;
c906108c
SS
10716 case DW_LANG_Fortran77:
10717 case DW_LANG_Fortran90:
b21b22e0 10718 case DW_LANG_Fortran95:
e142c38c 10719 cu->language = language_fortran;
c906108c
SS
10720 break;
10721 case DW_LANG_Mips_Assembler:
e142c38c 10722 cu->language = language_asm;
c906108c 10723 break;
bebd888e 10724 case DW_LANG_Java:
e142c38c 10725 cu->language = language_java;
bebd888e 10726 break;
c906108c 10727 case DW_LANG_Ada83:
8aaf0b47 10728 case DW_LANG_Ada95:
bc5f45f8
JB
10729 cu->language = language_ada;
10730 break;
72019c9c
GM
10731 case DW_LANG_Modula2:
10732 cu->language = language_m2;
10733 break;
fe8e67fd
PM
10734 case DW_LANG_Pascal83:
10735 cu->language = language_pascal;
10736 break;
22566fbd
DJ
10737 case DW_LANG_ObjC:
10738 cu->language = language_objc;
10739 break;
c906108c
SS
10740 case DW_LANG_Cobol74:
10741 case DW_LANG_Cobol85:
c906108c 10742 default:
e142c38c 10743 cu->language = language_minimal;
c906108c
SS
10744 break;
10745 }
e142c38c 10746 cu->language_defn = language_def (cu->language);
c906108c
SS
10747}
10748
10749/* Return the named attribute or NULL if not there. */
10750
10751static struct attribute *
e142c38c 10752dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10753{
10754 unsigned int i;
10755 struct attribute *spec = NULL;
10756
10757 for (i = 0; i < die->num_attrs; ++i)
10758 {
10759 if (die->attrs[i].name == name)
10b3939b 10760 return &die->attrs[i];
c906108c
SS
10761 if (die->attrs[i].name == DW_AT_specification
10762 || die->attrs[i].name == DW_AT_abstract_origin)
10763 spec = &die->attrs[i];
10764 }
c906108c 10765
10b3939b 10766 if (spec)
f2f0e013
DJ
10767 {
10768 die = follow_die_ref (die, spec, &cu);
10769 return dwarf2_attr (die, name, cu);
10770 }
c5aa993b 10771
c906108c
SS
10772 return NULL;
10773}
10774
348e048f
DE
10775/* Return the named attribute or NULL if not there,
10776 but do not follow DW_AT_specification, etc.
10777 This is for use in contexts where we're reading .debug_types dies.
10778 Following DW_AT_specification, DW_AT_abstract_origin will take us
10779 back up the chain, and we want to go down. */
10780
10781static struct attribute *
10782dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10783 struct dwarf2_cu *cu)
10784{
10785 unsigned int i;
10786
10787 for (i = 0; i < die->num_attrs; ++i)
10788 if (die->attrs[i].name == name)
10789 return &die->attrs[i];
10790
10791 return NULL;
10792}
10793
05cf31d1
JB
10794/* Return non-zero iff the attribute NAME is defined for the given DIE,
10795 and holds a non-zero value. This function should only be used for
2dc7f7b3 10796 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10797
10798static int
10799dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10800{
10801 struct attribute *attr = dwarf2_attr (die, name, cu);
10802
10803 return (attr && DW_UNSND (attr));
10804}
10805
3ca72b44 10806static int
e142c38c 10807die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10808{
05cf31d1
JB
10809 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10810 which value is non-zero. However, we have to be careful with
10811 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10812 (via dwarf2_flag_true_p) follows this attribute. So we may
10813 end up accidently finding a declaration attribute that belongs
10814 to a different DIE referenced by the specification attribute,
10815 even though the given DIE does not have a declaration attribute. */
10816 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10817 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10818}
10819
63d06c5c 10820/* Return the die giving the specification for DIE, if there is
f2f0e013 10821 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10822 containing the return value on output. If there is no
10823 specification, but there is an abstract origin, that is
10824 returned. */
63d06c5c
DC
10825
10826static struct die_info *
f2f0e013 10827die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10828{
f2f0e013
DJ
10829 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10830 *spec_cu);
63d06c5c 10831
edb3359d
DJ
10832 if (spec_attr == NULL)
10833 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10834
63d06c5c
DC
10835 if (spec_attr == NULL)
10836 return NULL;
10837 else
f2f0e013 10838 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10839}
c906108c 10840
debd256d 10841/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10842 refers to.
10843 NOTE: This is also used as a "cleanup" function. */
10844
debd256d
JB
10845static void
10846free_line_header (struct line_header *lh)
10847{
10848 if (lh->standard_opcode_lengths)
a8bc7b56 10849 xfree (lh->standard_opcode_lengths);
debd256d
JB
10850
10851 /* Remember that all the lh->file_names[i].name pointers are
10852 pointers into debug_line_buffer, and don't need to be freed. */
10853 if (lh->file_names)
a8bc7b56 10854 xfree (lh->file_names);
debd256d
JB
10855
10856 /* Similarly for the include directory names. */
10857 if (lh->include_dirs)
a8bc7b56 10858 xfree (lh->include_dirs);
debd256d 10859
a8bc7b56 10860 xfree (lh);
debd256d
JB
10861}
10862
debd256d 10863/* Add an entry to LH's include directory table. */
ae2de4f8 10864
debd256d
JB
10865static void
10866add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10867{
debd256d
JB
10868 /* Grow the array if necessary. */
10869 if (lh->include_dirs_size == 0)
c5aa993b 10870 {
debd256d
JB
10871 lh->include_dirs_size = 1; /* for testing */
10872 lh->include_dirs = xmalloc (lh->include_dirs_size
10873 * sizeof (*lh->include_dirs));
10874 }
10875 else if (lh->num_include_dirs >= lh->include_dirs_size)
10876 {
10877 lh->include_dirs_size *= 2;
10878 lh->include_dirs = xrealloc (lh->include_dirs,
10879 (lh->include_dirs_size
10880 * sizeof (*lh->include_dirs)));
c5aa993b 10881 }
c906108c 10882
debd256d
JB
10883 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10884}
6e70227d 10885
debd256d 10886/* Add an entry to LH's file name table. */
ae2de4f8 10887
debd256d
JB
10888static void
10889add_file_name (struct line_header *lh,
10890 char *name,
10891 unsigned int dir_index,
10892 unsigned int mod_time,
10893 unsigned int length)
10894{
10895 struct file_entry *fe;
10896
10897 /* Grow the array if necessary. */
10898 if (lh->file_names_size == 0)
10899 {
10900 lh->file_names_size = 1; /* for testing */
10901 lh->file_names = xmalloc (lh->file_names_size
10902 * sizeof (*lh->file_names));
10903 }
10904 else if (lh->num_file_names >= lh->file_names_size)
10905 {
10906 lh->file_names_size *= 2;
10907 lh->file_names = xrealloc (lh->file_names,
10908 (lh->file_names_size
10909 * sizeof (*lh->file_names)));
10910 }
10911
10912 fe = &lh->file_names[lh->num_file_names++];
10913 fe->name = name;
10914 fe->dir_index = dir_index;
10915 fe->mod_time = mod_time;
10916 fe->length = length;
aaa75496 10917 fe->included_p = 0;
cb1df416 10918 fe->symtab = NULL;
debd256d 10919}
6e70227d 10920
debd256d 10921/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10922 .debug_line, according to the endianness of ABFD. Return a pointer
10923 to a struct line_header, allocated using xmalloc.
debd256d
JB
10924
10925 NOTE: the strings in the include directory and file name tables of
10926 the returned object point into debug_line_buffer, and must not be
10927 freed. */
ae2de4f8 10928
debd256d
JB
10929static struct line_header *
10930dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10931 struct dwarf2_cu *cu)
debd256d
JB
10932{
10933 struct cleanup *back_to;
10934 struct line_header *lh;
fe1b8b76 10935 gdb_byte *line_ptr;
c764a876 10936 unsigned int bytes_read, offset_size;
debd256d
JB
10937 int i;
10938 char *cur_dir, *cur_file;
10939
be391dca 10940 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10941 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10942 {
e2e0b3e5 10943 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10944 return 0;
10945 }
10946
a738430d
MK
10947 /* Make sure that at least there's room for the total_length field.
10948 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10949 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10950 {
4d3c2250 10951 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10952 return 0;
10953 }
10954
10955 lh = xmalloc (sizeof (*lh));
10956 memset (lh, 0, sizeof (*lh));
10957 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10958 (void *) lh);
10959
dce234bc 10960 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10961
a738430d 10962 /* Read in the header. */
6e70227d 10963 lh->total_length =
c764a876
DE
10964 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10965 &bytes_read, &offset_size);
debd256d 10966 line_ptr += bytes_read;
dce234bc
PP
10967 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10968 + dwarf2_per_objfile->line.size))
debd256d 10969 {
4d3c2250 10970 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10971 return 0;
10972 }
10973 lh->statement_program_end = line_ptr + lh->total_length;
10974 lh->version = read_2_bytes (abfd, line_ptr);
10975 line_ptr += 2;
c764a876
DE
10976 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10977 line_ptr += offset_size;
debd256d
JB
10978 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10979 line_ptr += 1;
2dc7f7b3
TT
10980 if (lh->version >= 4)
10981 {
10982 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10983 line_ptr += 1;
10984 }
10985 else
10986 lh->maximum_ops_per_instruction = 1;
10987
10988 if (lh->maximum_ops_per_instruction == 0)
10989 {
10990 lh->maximum_ops_per_instruction = 1;
10991 complaint (&symfile_complaints,
3e43a32a
MS
10992 _("invalid maximum_ops_per_instruction "
10993 "in `.debug_line' section"));
2dc7f7b3
TT
10994 }
10995
debd256d
JB
10996 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10997 line_ptr += 1;
10998 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10999 line_ptr += 1;
11000 lh->line_range = read_1_byte (abfd, line_ptr);
11001 line_ptr += 1;
11002 lh->opcode_base = read_1_byte (abfd, line_ptr);
11003 line_ptr += 1;
11004 lh->standard_opcode_lengths
fe1b8b76 11005 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
11006
11007 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11008 for (i = 1; i < lh->opcode_base; ++i)
11009 {
11010 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11011 line_ptr += 1;
11012 }
11013
a738430d 11014 /* Read directory table. */
9b1c24c8 11015 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11016 {
11017 line_ptr += bytes_read;
11018 add_include_dir (lh, cur_dir);
11019 }
11020 line_ptr += bytes_read;
11021
a738430d 11022 /* Read file name table. */
9b1c24c8 11023 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11024 {
11025 unsigned int dir_index, mod_time, length;
11026
11027 line_ptr += bytes_read;
11028 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11029 line_ptr += bytes_read;
11030 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11031 line_ptr += bytes_read;
11032 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11033 line_ptr += bytes_read;
11034
11035 add_file_name (lh, cur_file, dir_index, mod_time, length);
11036 }
11037 line_ptr += bytes_read;
6e70227d 11038 lh->statement_program_start = line_ptr;
debd256d 11039
dce234bc
PP
11040 if (line_ptr > (dwarf2_per_objfile->line.buffer
11041 + dwarf2_per_objfile->line.size))
4d3c2250 11042 complaint (&symfile_complaints,
3e43a32a
MS
11043 _("line number info header doesn't "
11044 "fit in `.debug_line' section"));
debd256d
JB
11045
11046 discard_cleanups (back_to);
11047 return lh;
11048}
c906108c 11049
5fb290d7
DJ
11050/* This function exists to work around a bug in certain compilers
11051 (particularly GCC 2.95), in which the first line number marker of a
11052 function does not show up until after the prologue, right before
11053 the second line number marker. This function shifts ADDRESS down
11054 to the beginning of the function if necessary, and is called on
11055 addresses passed to record_line. */
11056
11057static CORE_ADDR
e142c38c 11058check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
11059{
11060 struct function_range *fn;
11061
11062 /* Find the function_range containing address. */
e142c38c 11063 if (!cu->first_fn)
5fb290d7
DJ
11064 return address;
11065
e142c38c
DJ
11066 if (!cu->cached_fn)
11067 cu->cached_fn = cu->first_fn;
5fb290d7 11068
e142c38c 11069 fn = cu->cached_fn;
5fb290d7
DJ
11070 while (fn)
11071 if (fn->lowpc <= address && fn->highpc > address)
11072 goto found;
11073 else
11074 fn = fn->next;
11075
e142c38c
DJ
11076 fn = cu->first_fn;
11077 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
11078 if (fn->lowpc <= address && fn->highpc > address)
11079 goto found;
11080 else
11081 fn = fn->next;
11082
11083 return address;
11084
11085 found:
11086 if (fn->seen_line)
11087 return address;
11088 if (address != fn->lowpc)
4d3c2250 11089 complaint (&symfile_complaints,
e2e0b3e5 11090 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 11091 (unsigned long) address, fn->name);
5fb290d7
DJ
11092 fn->seen_line = 1;
11093 return fn->lowpc;
11094}
11095
c6da4cef
DE
11096/* Subroutine of dwarf_decode_lines to simplify it.
11097 Return the file name of the psymtab for included file FILE_INDEX
11098 in line header LH of PST.
11099 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11100 If space for the result is malloc'd, it will be freed by a cleanup.
11101 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11102
11103static char *
11104psymtab_include_file_name (const struct line_header *lh, int file_index,
11105 const struct partial_symtab *pst,
11106 const char *comp_dir)
11107{
11108 const struct file_entry fe = lh->file_names [file_index];
11109 char *include_name = fe.name;
11110 char *include_name_to_compare = include_name;
11111 char *dir_name = NULL;
72b9f47f
TT
11112 const char *pst_filename;
11113 char *copied_name = NULL;
c6da4cef
DE
11114 int file_is_pst;
11115
11116 if (fe.dir_index)
11117 dir_name = lh->include_dirs[fe.dir_index - 1];
11118
11119 if (!IS_ABSOLUTE_PATH (include_name)
11120 && (dir_name != NULL || comp_dir != NULL))
11121 {
11122 /* Avoid creating a duplicate psymtab for PST.
11123 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11124 Before we do the comparison, however, we need to account
11125 for DIR_NAME and COMP_DIR.
11126 First prepend dir_name (if non-NULL). If we still don't
11127 have an absolute path prepend comp_dir (if non-NULL).
11128 However, the directory we record in the include-file's
11129 psymtab does not contain COMP_DIR (to match the
11130 corresponding symtab(s)).
11131
11132 Example:
11133
11134 bash$ cd /tmp
11135 bash$ gcc -g ./hello.c
11136 include_name = "hello.c"
11137 dir_name = "."
11138 DW_AT_comp_dir = comp_dir = "/tmp"
11139 DW_AT_name = "./hello.c" */
11140
11141 if (dir_name != NULL)
11142 {
11143 include_name = concat (dir_name, SLASH_STRING,
11144 include_name, (char *)NULL);
11145 include_name_to_compare = include_name;
11146 make_cleanup (xfree, include_name);
11147 }
11148 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11149 {
11150 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11151 include_name, (char *)NULL);
11152 }
11153 }
11154
11155 pst_filename = pst->filename;
11156 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11157 {
72b9f47f
TT
11158 copied_name = concat (pst->dirname, SLASH_STRING,
11159 pst_filename, (char *)NULL);
11160 pst_filename = copied_name;
c6da4cef
DE
11161 }
11162
1e3fad37 11163 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11164
11165 if (include_name_to_compare != include_name)
11166 xfree (include_name_to_compare);
72b9f47f
TT
11167 if (copied_name != NULL)
11168 xfree (copied_name);
c6da4cef
DE
11169
11170 if (file_is_pst)
11171 return NULL;
11172 return include_name;
11173}
11174
c91513d8
PP
11175/* Ignore this record_line request. */
11176
11177static void
11178noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11179{
11180 return;
11181}
11182
aaa75496
JB
11183/* Decode the Line Number Program (LNP) for the given line_header
11184 structure and CU. The actual information extracted and the type
11185 of structures created from the LNP depends on the value of PST.
11186
11187 1. If PST is NULL, then this procedure uses the data from the program
11188 to create all necessary symbol tables, and their linetables.
6e70227d 11189
aaa75496
JB
11190 2. If PST is not NULL, this procedure reads the program to determine
11191 the list of files included by the unit represented by PST, and
c6da4cef
DE
11192 builds all the associated partial symbol tables.
11193
11194 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11195 It is used for relative paths in the line table.
11196 NOTE: When processing partial symtabs (pst != NULL),
11197 comp_dir == pst->dirname.
11198
11199 NOTE: It is important that psymtabs have the same file name (via strcmp)
11200 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11201 symtab we don't use it in the name of the psymtabs we create.
11202 E.g. expand_line_sal requires this when finding psymtabs to expand.
11203 A good testcase for this is mb-inline.exp. */
debd256d 11204
c906108c 11205static void
72b9f47f 11206dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 11207 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11208{
a8c50c1f 11209 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11210 gdb_byte *line_end;
a8c50c1f 11211 unsigned int bytes_read, extended_len;
c906108c 11212 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11213 CORE_ADDR baseaddr;
11214 struct objfile *objfile = cu->objfile;
fbf65064 11215 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11216 const int decode_for_pst_p = (pst != NULL);
cb1df416 11217 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
11218 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11219 = record_line;
e142c38c
DJ
11220
11221 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11222
debd256d
JB
11223 line_ptr = lh->statement_program_start;
11224 line_end = lh->statement_program_end;
c906108c
SS
11225
11226 /* Read the statement sequences until there's nothing left. */
11227 while (line_ptr < line_end)
11228 {
11229 /* state machine registers */
11230 CORE_ADDR address = 0;
11231 unsigned int file = 1;
11232 unsigned int line = 1;
11233 unsigned int column = 0;
debd256d 11234 int is_stmt = lh->default_is_stmt;
c906108c
SS
11235 int basic_block = 0;
11236 int end_sequence = 0;
fbf65064 11237 CORE_ADDR addr;
2dc7f7b3 11238 unsigned char op_index = 0;
c906108c 11239
aaa75496 11240 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11241 {
aaa75496 11242 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11243 /* lh->include_dirs and lh->file_names are 0-based, but the
11244 directory and file name numbers in the statement program
11245 are 1-based. */
11246 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11247 char *dir = NULL;
a738430d 11248
debd256d
JB
11249 if (fe->dir_index)
11250 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11251
11252 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11253 }
11254
a738430d 11255 /* Decode the table. */
c5aa993b 11256 while (!end_sequence)
c906108c
SS
11257 {
11258 op_code = read_1_byte (abfd, line_ptr);
11259 line_ptr += 1;
59205f5a
JB
11260 if (line_ptr > line_end)
11261 {
11262 dwarf2_debug_line_missing_end_sequence_complaint ();
11263 break;
11264 }
9aa1fe7e 11265
debd256d 11266 if (op_code >= lh->opcode_base)
6e70227d 11267 {
a738430d 11268 /* Special operand. */
debd256d 11269 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11270 address += (((op_index + (adj_opcode / lh->line_range))
11271 / lh->maximum_ops_per_instruction)
11272 * lh->minimum_instruction_length);
11273 op_index = ((op_index + (adj_opcode / lh->line_range))
11274 % lh->maximum_ops_per_instruction);
debd256d 11275 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11276 if (lh->num_file_names < file || file == 0)
25e43795 11277 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11278 /* For now we ignore lines not starting on an
11279 instruction boundary. */
11280 else if (op_index == 0)
25e43795
DJ
11281 {
11282 lh->file_names[file - 1].included_p = 1;
ca5f395d 11283 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11284 {
11285 if (last_subfile != current_subfile)
11286 {
11287 addr = gdbarch_addr_bits_remove (gdbarch, address);
11288 if (last_subfile)
c91513d8 11289 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11290 last_subfile = current_subfile;
11291 }
25e43795 11292 /* Append row to matrix using current values. */
fbf65064
UW
11293 addr = check_cu_functions (address, cu);
11294 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 11295 (*p_record_line) (current_subfile, line, addr);
366da635 11296 }
25e43795 11297 }
ca5f395d 11298 basic_block = 0;
9aa1fe7e
GK
11299 }
11300 else switch (op_code)
c906108c
SS
11301 {
11302 case DW_LNS_extended_op:
3e43a32a
MS
11303 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11304 &bytes_read);
473b7be6 11305 line_ptr += bytes_read;
a8c50c1f 11306 extended_end = line_ptr + extended_len;
c906108c
SS
11307 extended_op = read_1_byte (abfd, line_ptr);
11308 line_ptr += 1;
11309 switch (extended_op)
11310 {
11311 case DW_LNE_end_sequence:
c91513d8 11312 p_record_line = record_line;
c906108c 11313 end_sequence = 1;
c906108c
SS
11314 break;
11315 case DW_LNE_set_address:
e7c27a73 11316 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11317
11318 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11319 {
11320 /* This line table is for a function which has been
11321 GCd by the linker. Ignore it. PR gdb/12528 */
11322
11323 long line_offset
11324 = line_ptr - dwarf2_per_objfile->line.buffer;
11325
11326 complaint (&symfile_complaints,
11327 _(".debug_line address at offset 0x%lx is 0 "
11328 "[in module %s]"),
11329 line_offset, cu->objfile->name);
11330 p_record_line = noop_record_line;
11331 }
11332
2dc7f7b3 11333 op_index = 0;
107d2387
AC
11334 line_ptr += bytes_read;
11335 address += baseaddr;
c906108c
SS
11336 break;
11337 case DW_LNE_define_file:
debd256d
JB
11338 {
11339 char *cur_file;
11340 unsigned int dir_index, mod_time, length;
6e70227d 11341
3e43a32a
MS
11342 cur_file = read_direct_string (abfd, line_ptr,
11343 &bytes_read);
debd256d
JB
11344 line_ptr += bytes_read;
11345 dir_index =
11346 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11347 line_ptr += bytes_read;
11348 mod_time =
11349 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11350 line_ptr += bytes_read;
11351 length =
11352 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11353 line_ptr += bytes_read;
11354 add_file_name (lh, cur_file, dir_index, mod_time, length);
11355 }
c906108c 11356 break;
d0c6ba3d
CC
11357 case DW_LNE_set_discriminator:
11358 /* The discriminator is not interesting to the debugger;
11359 just ignore it. */
11360 line_ptr = extended_end;
11361 break;
c906108c 11362 default:
4d3c2250 11363 complaint (&symfile_complaints,
e2e0b3e5 11364 _("mangled .debug_line section"));
debd256d 11365 return;
c906108c 11366 }
a8c50c1f
DJ
11367 /* Make sure that we parsed the extended op correctly. If e.g.
11368 we expected a different address size than the producer used,
11369 we may have read the wrong number of bytes. */
11370 if (line_ptr != extended_end)
11371 {
11372 complaint (&symfile_complaints,
11373 _("mangled .debug_line section"));
11374 return;
11375 }
c906108c
SS
11376 break;
11377 case DW_LNS_copy:
59205f5a 11378 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11379 dwarf2_debug_line_missing_file_complaint ();
11380 else
366da635 11381 {
25e43795 11382 lh->file_names[file - 1].included_p = 1;
ca5f395d 11383 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11384 {
11385 if (last_subfile != current_subfile)
11386 {
11387 addr = gdbarch_addr_bits_remove (gdbarch, address);
11388 if (last_subfile)
c91513d8 11389 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11390 last_subfile = current_subfile;
11391 }
11392 addr = check_cu_functions (address, cu);
11393 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 11394 (*p_record_line) (current_subfile, line, addr);
fbf65064 11395 }
366da635 11396 }
c906108c
SS
11397 basic_block = 0;
11398 break;
11399 case DW_LNS_advance_pc:
2dc7f7b3
TT
11400 {
11401 CORE_ADDR adjust
11402 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11403
11404 address += (((op_index + adjust)
11405 / lh->maximum_ops_per_instruction)
11406 * lh->minimum_instruction_length);
11407 op_index = ((op_index + adjust)
11408 % lh->maximum_ops_per_instruction);
11409 line_ptr += bytes_read;
11410 }
c906108c
SS
11411 break;
11412 case DW_LNS_advance_line:
11413 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11414 line_ptr += bytes_read;
11415 break;
11416 case DW_LNS_set_file:
debd256d 11417 {
a738430d
MK
11418 /* The arrays lh->include_dirs and lh->file_names are
11419 0-based, but the directory and file name numbers in
11420 the statement program are 1-based. */
debd256d 11421 struct file_entry *fe;
4f1520fb 11422 char *dir = NULL;
a738430d 11423
debd256d
JB
11424 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11425 line_ptr += bytes_read;
59205f5a 11426 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11427 dwarf2_debug_line_missing_file_complaint ();
11428 else
11429 {
11430 fe = &lh->file_names[file - 1];
11431 if (fe->dir_index)
11432 dir = lh->include_dirs[fe->dir_index - 1];
11433 if (!decode_for_pst_p)
11434 {
11435 last_subfile = current_subfile;
11436 dwarf2_start_subfile (fe->name, dir, comp_dir);
11437 }
11438 }
debd256d 11439 }
c906108c
SS
11440 break;
11441 case DW_LNS_set_column:
11442 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11443 line_ptr += bytes_read;
11444 break;
11445 case DW_LNS_negate_stmt:
11446 is_stmt = (!is_stmt);
11447 break;
11448 case DW_LNS_set_basic_block:
11449 basic_block = 1;
11450 break;
c2c6d25f
JM
11451 /* Add to the address register of the state machine the
11452 address increment value corresponding to special opcode
a738430d
MK
11453 255. I.e., this value is scaled by the minimum
11454 instruction length since special opcode 255 would have
b021a221 11455 scaled the increment. */
c906108c 11456 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11457 {
11458 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11459
11460 address += (((op_index + adjust)
11461 / lh->maximum_ops_per_instruction)
11462 * lh->minimum_instruction_length);
11463 op_index = ((op_index + adjust)
11464 % lh->maximum_ops_per_instruction);
11465 }
c906108c
SS
11466 break;
11467 case DW_LNS_fixed_advance_pc:
11468 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11469 op_index = 0;
c906108c
SS
11470 line_ptr += 2;
11471 break;
9aa1fe7e 11472 default:
a738430d
MK
11473 {
11474 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11475 int i;
a738430d 11476
debd256d 11477 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11478 {
11479 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11480 line_ptr += bytes_read;
11481 }
11482 }
c906108c
SS
11483 }
11484 }
59205f5a
JB
11485 if (lh->num_file_names < file || file == 0)
11486 dwarf2_debug_line_missing_file_complaint ();
11487 else
11488 {
11489 lh->file_names[file - 1].included_p = 1;
11490 if (!decode_for_pst_p)
fbf65064
UW
11491 {
11492 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11493 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11494 }
59205f5a 11495 }
c906108c 11496 }
aaa75496
JB
11497
11498 if (decode_for_pst_p)
11499 {
11500 int file_index;
11501
11502 /* Now that we're done scanning the Line Header Program, we can
11503 create the psymtab of each included file. */
11504 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11505 if (lh->file_names[file_index].included_p == 1)
11506 {
c6da4cef
DE
11507 char *include_name =
11508 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11509 if (include_name != NULL)
aaa75496
JB
11510 dwarf2_create_include_psymtab (include_name, pst, objfile);
11511 }
11512 }
cb1df416
DJ
11513 else
11514 {
11515 /* Make sure a symtab is created for every file, even files
11516 which contain only variables (i.e. no code with associated
11517 line numbers). */
11518
11519 int i;
11520 struct file_entry *fe;
11521
11522 for (i = 0; i < lh->num_file_names; i++)
11523 {
11524 char *dir = NULL;
9a619af0 11525
cb1df416
DJ
11526 fe = &lh->file_names[i];
11527 if (fe->dir_index)
11528 dir = lh->include_dirs[fe->dir_index - 1];
11529 dwarf2_start_subfile (fe->name, dir, comp_dir);
11530
11531 /* Skip the main file; we don't need it, and it must be
11532 allocated last, so that it will show up before the
11533 non-primary symtabs in the objfile's symtab list. */
11534 if (current_subfile == first_subfile)
11535 continue;
11536
11537 if (current_subfile->symtab == NULL)
11538 current_subfile->symtab = allocate_symtab (current_subfile->name,
11539 cu->objfile);
11540 fe->symtab = current_subfile->symtab;
11541 }
11542 }
c906108c
SS
11543}
11544
11545/* Start a subfile for DWARF. FILENAME is the name of the file and
11546 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11547 or NULL if not known. COMP_DIR is the compilation directory for the
11548 linetable's compilation unit or NULL if not known.
c906108c
SS
11549 This routine tries to keep line numbers from identical absolute and
11550 relative file names in a common subfile.
11551
11552 Using the `list' example from the GDB testsuite, which resides in
11553 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11554 of /srcdir/list0.c yields the following debugging information for list0.c:
11555
c5aa993b
JM
11556 DW_AT_name: /srcdir/list0.c
11557 DW_AT_comp_dir: /compdir
357e46e7 11558 files.files[0].name: list0.h
c5aa993b 11559 files.files[0].dir: /srcdir
357e46e7 11560 files.files[1].name: list0.c
c5aa993b 11561 files.files[1].dir: /srcdir
c906108c
SS
11562
11563 The line number information for list0.c has to end up in a single
4f1520fb
FR
11564 subfile, so that `break /srcdir/list0.c:1' works as expected.
11565 start_subfile will ensure that this happens provided that we pass the
11566 concatenation of files.files[1].dir and files.files[1].name as the
11567 subfile's name. */
c906108c
SS
11568
11569static void
3e43a32a
MS
11570dwarf2_start_subfile (char *filename, const char *dirname,
11571 const char *comp_dir)
c906108c 11572{
4f1520fb
FR
11573 char *fullname;
11574
11575 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11576 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11577 second argument to start_subfile. To be consistent, we do the
11578 same here. In order not to lose the line information directory,
11579 we concatenate it to the filename when it makes sense.
11580 Note that the Dwarf3 standard says (speaking of filenames in line
11581 information): ``The directory index is ignored for file names
11582 that represent full path names''. Thus ignoring dirname in the
11583 `else' branch below isn't an issue. */
c906108c 11584
d5166ae1 11585 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11586 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11587 else
11588 fullname = filename;
c906108c 11589
4f1520fb
FR
11590 start_subfile (fullname, comp_dir);
11591
11592 if (fullname != filename)
11593 xfree (fullname);
c906108c
SS
11594}
11595
4c2df51b
DJ
11596static void
11597var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11598 struct dwarf2_cu *cu)
4c2df51b 11599{
e7c27a73
DJ
11600 struct objfile *objfile = cu->objfile;
11601 struct comp_unit_head *cu_header = &cu->header;
11602
4c2df51b
DJ
11603 /* NOTE drow/2003-01-30: There used to be a comment and some special
11604 code here to turn a symbol with DW_AT_external and a
11605 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11606 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11607 with some versions of binutils) where shared libraries could have
11608 relocations against symbols in their debug information - the
11609 minimal symbol would have the right address, but the debug info
11610 would not. It's no longer necessary, because we will explicitly
11611 apply relocations when we read in the debug information now. */
11612
11613 /* A DW_AT_location attribute with no contents indicates that a
11614 variable has been optimized away. */
11615 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11616 {
11617 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11618 return;
11619 }
11620
11621 /* Handle one degenerate form of location expression specially, to
11622 preserve GDB's previous behavior when section offsets are
11623 specified. If this is just a DW_OP_addr then mark this symbol
11624 as LOC_STATIC. */
11625
11626 if (attr_form_is_block (attr)
11627 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11628 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11629 {
891d2f0b 11630 unsigned int dummy;
4c2df51b
DJ
11631
11632 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11633 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11634 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11635 fixup_symbol_section (sym, objfile);
11636 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11637 SYMBOL_SECTION (sym));
4c2df51b
DJ
11638 return;
11639 }
11640
11641 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11642 expression evaluator, and use LOC_COMPUTED only when necessary
11643 (i.e. when the value of a register or memory location is
11644 referenced, or a thread-local block, etc.). Then again, it might
11645 not be worthwhile. I'm assuming that it isn't unless performance
11646 or memory numbers show me otherwise. */
11647
e7c27a73 11648 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11649 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11650
11651 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11652 cu->has_loclist = 1;
4c2df51b
DJ
11653}
11654
c906108c
SS
11655/* Given a pointer to a DWARF information entry, figure out if we need
11656 to make a symbol table entry for it, and if so, create a new entry
11657 and return a pointer to it.
11658 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11659 used the passed type.
11660 If SPACE is not NULL, use it to hold the new symbol. If it is
11661 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11662
11663static struct symbol *
34eaf542
TT
11664new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11665 struct symbol *space)
c906108c 11666{
e7c27a73 11667 struct objfile *objfile = cu->objfile;
c906108c
SS
11668 struct symbol *sym = NULL;
11669 char *name;
11670 struct attribute *attr = NULL;
11671 struct attribute *attr2 = NULL;
e142c38c 11672 CORE_ADDR baseaddr;
e37fd15a
SW
11673 struct pending **list_to_add = NULL;
11674
edb3359d 11675 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11676
11677 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11678
94af9270 11679 name = dwarf2_name (die, cu);
c906108c
SS
11680 if (name)
11681 {
94af9270 11682 const char *linkagename;
34eaf542 11683 int suppress_add = 0;
94af9270 11684
34eaf542
TT
11685 if (space)
11686 sym = space;
11687 else
11688 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11689 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11690
11691 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11692 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11693 linkagename = dwarf2_physname (name, die, cu);
11694 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11695
f55ee35c
JK
11696 /* Fortran does not have mangling standard and the mangling does differ
11697 between gfortran, iFort etc. */
11698 if (cu->language == language_fortran
b250c185 11699 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11700 symbol_set_demangled_name (&(sym->ginfo),
11701 (char *) dwarf2_full_name (name, die, cu),
11702 NULL);
f55ee35c 11703
c906108c 11704 /* Default assumptions.
c5aa993b 11705 Use the passed type or decode it from the die. */
176620f1 11706 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11707 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11708 if (type != NULL)
11709 SYMBOL_TYPE (sym) = type;
11710 else
e7c27a73 11711 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11712 attr = dwarf2_attr (die,
11713 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11714 cu);
c906108c
SS
11715 if (attr)
11716 {
11717 SYMBOL_LINE (sym) = DW_UNSND (attr);
11718 }
cb1df416 11719
edb3359d
DJ
11720 attr = dwarf2_attr (die,
11721 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11722 cu);
cb1df416
DJ
11723 if (attr)
11724 {
11725 int file_index = DW_UNSND (attr);
9a619af0 11726
cb1df416
DJ
11727 if (cu->line_header == NULL
11728 || file_index > cu->line_header->num_file_names)
11729 complaint (&symfile_complaints,
11730 _("file index out of range"));
1c3d648d 11731 else if (file_index > 0)
cb1df416
DJ
11732 {
11733 struct file_entry *fe;
9a619af0 11734
cb1df416
DJ
11735 fe = &cu->line_header->file_names[file_index - 1];
11736 SYMBOL_SYMTAB (sym) = fe->symtab;
11737 }
11738 }
11739
c906108c
SS
11740 switch (die->tag)
11741 {
11742 case DW_TAG_label:
e142c38c 11743 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11744 if (attr)
11745 {
11746 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11747 }
0f5238ed
TT
11748 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11749 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11750 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11751 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11752 break;
11753 case DW_TAG_subprogram:
11754 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11755 finish_block. */
11756 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11757 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11758 if ((attr2 && (DW_UNSND (attr2) != 0))
11759 || cu->language == language_ada)
c906108c 11760 {
2cfa0c8d
JB
11761 /* Subprograms marked external are stored as a global symbol.
11762 Ada subprograms, whether marked external or not, are always
11763 stored as a global symbol, because we want to be able to
11764 access them globally. For instance, we want to be able
11765 to break on a nested subprogram without having to
11766 specify the context. */
e37fd15a 11767 list_to_add = &global_symbols;
c906108c
SS
11768 }
11769 else
11770 {
e37fd15a 11771 list_to_add = cu->list_in_scope;
c906108c
SS
11772 }
11773 break;
edb3359d
DJ
11774 case DW_TAG_inlined_subroutine:
11775 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11776 finish_block. */
11777 SYMBOL_CLASS (sym) = LOC_BLOCK;
11778 SYMBOL_INLINED (sym) = 1;
11779 /* Do not add the symbol to any lists. It will be found via
11780 BLOCK_FUNCTION from the blockvector. */
11781 break;
34eaf542
TT
11782 case DW_TAG_template_value_param:
11783 suppress_add = 1;
11784 /* Fall through. */
72929c62 11785 case DW_TAG_constant:
c906108c 11786 case DW_TAG_variable:
254e6b9e 11787 case DW_TAG_member:
0963b4bd
MS
11788 /* Compilation with minimal debug info may result in
11789 variables with missing type entries. Change the
11790 misleading `void' type to something sensible. */
c906108c 11791 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11792 SYMBOL_TYPE (sym)
46bf5051 11793 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11794
e142c38c 11795 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11796 /* In the case of DW_TAG_member, we should only be called for
11797 static const members. */
11798 if (die->tag == DW_TAG_member)
11799 {
3863f96c
DE
11800 /* dwarf2_add_field uses die_is_declaration,
11801 so we do the same. */
254e6b9e
DE
11802 gdb_assert (die_is_declaration (die, cu));
11803 gdb_assert (attr);
11804 }
c906108c
SS
11805 if (attr)
11806 {
e7c27a73 11807 dwarf2_const_value (attr, sym, cu);
e142c38c 11808 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11809 if (!suppress_add)
34eaf542
TT
11810 {
11811 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11812 list_to_add = &global_symbols;
34eaf542 11813 else
e37fd15a 11814 list_to_add = cu->list_in_scope;
34eaf542 11815 }
c906108c
SS
11816 break;
11817 }
e142c38c 11818 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11819 if (attr)
11820 {
e7c27a73 11821 var_decode_location (attr, sym, cu);
e142c38c 11822 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11823 if (SYMBOL_CLASS (sym) == LOC_STATIC
11824 && SYMBOL_VALUE_ADDRESS (sym) == 0
11825 && !dwarf2_per_objfile->has_section_at_zero)
11826 {
11827 /* When a static variable is eliminated by the linker,
11828 the corresponding debug information is not stripped
11829 out, but the variable address is set to null;
11830 do not add such variables into symbol table. */
11831 }
11832 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11833 {
f55ee35c
JK
11834 /* Workaround gfortran PR debug/40040 - it uses
11835 DW_AT_location for variables in -fPIC libraries which may
11836 get overriden by other libraries/executable and get
11837 a different address. Resolve it by the minimal symbol
11838 which may come from inferior's executable using copy
11839 relocation. Make this workaround only for gfortran as for
11840 other compilers GDB cannot guess the minimal symbol
11841 Fortran mangling kind. */
11842 if (cu->language == language_fortran && die->parent
11843 && die->parent->tag == DW_TAG_module
11844 && cu->producer
11845 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11846 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11847
1c809c68
TT
11848 /* A variable with DW_AT_external is never static,
11849 but it may be block-scoped. */
11850 list_to_add = (cu->list_in_scope == &file_symbols
11851 ? &global_symbols : cu->list_in_scope);
1c809c68 11852 }
c906108c 11853 else
e37fd15a 11854 list_to_add = cu->list_in_scope;
c906108c
SS
11855 }
11856 else
11857 {
11858 /* We do not know the address of this symbol.
c5aa993b
JM
11859 If it is an external symbol and we have type information
11860 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11861 The address of the variable will then be determined from
11862 the minimal symbol table whenever the variable is
11863 referenced. */
e142c38c 11864 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11865 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11866 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11867 {
0fe7935b
DJ
11868 /* A variable with DW_AT_external is never static, but it
11869 may be block-scoped. */
11870 list_to_add = (cu->list_in_scope == &file_symbols
11871 ? &global_symbols : cu->list_in_scope);
11872
c906108c 11873 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11874 }
442ddf59
JK
11875 else if (!die_is_declaration (die, cu))
11876 {
11877 /* Use the default LOC_OPTIMIZED_OUT class. */
11878 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11879 if (!suppress_add)
11880 list_to_add = cu->list_in_scope;
442ddf59 11881 }
c906108c
SS
11882 }
11883 break;
11884 case DW_TAG_formal_parameter:
edb3359d
DJ
11885 /* If we are inside a function, mark this as an argument. If
11886 not, we might be looking at an argument to an inlined function
11887 when we do not have enough information to show inlined frames;
11888 pretend it's a local variable in that case so that the user can
11889 still see it. */
11890 if (context_stack_depth > 0
11891 && context_stack[context_stack_depth - 1].name != NULL)
11892 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11893 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11894 if (attr)
11895 {
e7c27a73 11896 var_decode_location (attr, sym, cu);
c906108c 11897 }
e142c38c 11898 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11899 if (attr)
11900 {
e7c27a73 11901 dwarf2_const_value (attr, sym, cu);
c906108c 11902 }
f346a30d 11903
e37fd15a 11904 list_to_add = cu->list_in_scope;
c906108c
SS
11905 break;
11906 case DW_TAG_unspecified_parameters:
11907 /* From varargs functions; gdb doesn't seem to have any
11908 interest in this information, so just ignore it for now.
11909 (FIXME?) */
11910 break;
34eaf542
TT
11911 case DW_TAG_template_type_param:
11912 suppress_add = 1;
11913 /* Fall through. */
c906108c 11914 case DW_TAG_class_type:
680b30c7 11915 case DW_TAG_interface_type:
c906108c
SS
11916 case DW_TAG_structure_type:
11917 case DW_TAG_union_type:
72019c9c 11918 case DW_TAG_set_type:
c906108c
SS
11919 case DW_TAG_enumeration_type:
11920 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11921 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11922
63d06c5c 11923 {
987504bb 11924 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11925 really ever be static objects: otherwise, if you try
11926 to, say, break of a class's method and you're in a file
11927 which doesn't mention that class, it won't work unless
11928 the check for all static symbols in lookup_symbol_aux
11929 saves you. See the OtherFileClass tests in
11930 gdb.c++/namespace.exp. */
11931
e37fd15a 11932 if (!suppress_add)
34eaf542 11933 {
34eaf542
TT
11934 list_to_add = (cu->list_in_scope == &file_symbols
11935 && (cu->language == language_cplus
11936 || cu->language == language_java)
11937 ? &global_symbols : cu->list_in_scope);
63d06c5c 11938
64382290
TT
11939 /* The semantics of C++ state that "struct foo {
11940 ... }" also defines a typedef for "foo". A Java
11941 class declaration also defines a typedef for the
11942 class. */
11943 if (cu->language == language_cplus
11944 || cu->language == language_java
11945 || cu->language == language_ada)
11946 {
11947 /* The symbol's name is already allocated along
11948 with this objfile, so we don't need to
11949 duplicate it for the type. */
11950 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11951 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11952 }
63d06c5c
DC
11953 }
11954 }
c906108c
SS
11955 break;
11956 case DW_TAG_typedef:
63d06c5c
DC
11957 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11958 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11959 list_to_add = cu->list_in_scope;
63d06c5c 11960 break;
c906108c 11961 case DW_TAG_base_type:
a02abb62 11962 case DW_TAG_subrange_type:
c906108c 11963 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11964 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11965 list_to_add = cu->list_in_scope;
c906108c
SS
11966 break;
11967 case DW_TAG_enumerator:
e142c38c 11968 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11969 if (attr)
11970 {
e7c27a73 11971 dwarf2_const_value (attr, sym, cu);
c906108c 11972 }
63d06c5c
DC
11973 {
11974 /* NOTE: carlton/2003-11-10: See comment above in the
11975 DW_TAG_class_type, etc. block. */
11976
e142c38c 11977 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11978 && (cu->language == language_cplus
11979 || cu->language == language_java)
e142c38c 11980 ? &global_symbols : cu->list_in_scope);
63d06c5c 11981 }
c906108c 11982 break;
5c4e30ca
DC
11983 case DW_TAG_namespace:
11984 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11985 list_to_add = &global_symbols;
5c4e30ca 11986 break;
c906108c
SS
11987 default:
11988 /* Not a tag we recognize. Hopefully we aren't processing
11989 trash data, but since we must specifically ignore things
11990 we don't recognize, there is nothing else we should do at
0963b4bd 11991 this point. */
e2e0b3e5 11992 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11993 dwarf_tag_name (die->tag));
c906108c
SS
11994 break;
11995 }
df8a16a1 11996
e37fd15a
SW
11997 if (suppress_add)
11998 {
11999 sym->hash_next = objfile->template_symbols;
12000 objfile->template_symbols = sym;
12001 list_to_add = NULL;
12002 }
12003
12004 if (list_to_add != NULL)
12005 add_symbol_to_list (sym, list_to_add);
12006
df8a16a1
DJ
12007 /* For the benefit of old versions of GCC, check for anonymous
12008 namespaces based on the demangled name. */
12009 if (!processing_has_namespace_info
94af9270 12010 && cu->language == language_cplus)
a10964d1 12011 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
12012 }
12013 return (sym);
12014}
12015
34eaf542
TT
12016/* A wrapper for new_symbol_full that always allocates a new symbol. */
12017
12018static struct symbol *
12019new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12020{
12021 return new_symbol_full (die, type, cu, NULL);
12022}
12023
98bfdba5
PA
12024/* Given an attr with a DW_FORM_dataN value in host byte order,
12025 zero-extend it as appropriate for the symbol's type. The DWARF
12026 standard (v4) is not entirely clear about the meaning of using
12027 DW_FORM_dataN for a constant with a signed type, where the type is
12028 wider than the data. The conclusion of a discussion on the DWARF
12029 list was that this is unspecified. We choose to always zero-extend
12030 because that is the interpretation long in use by GCC. */
c906108c 12031
98bfdba5
PA
12032static gdb_byte *
12033dwarf2_const_value_data (struct attribute *attr, struct type *type,
12034 const char *name, struct obstack *obstack,
12035 struct dwarf2_cu *cu, long *value, int bits)
c906108c 12036{
e7c27a73 12037 struct objfile *objfile = cu->objfile;
e17a4113
UW
12038 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12039 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
12040 LONGEST l = DW_UNSND (attr);
12041
12042 if (bits < sizeof (*value) * 8)
12043 {
12044 l &= ((LONGEST) 1 << bits) - 1;
12045 *value = l;
12046 }
12047 else if (bits == sizeof (*value) * 8)
12048 *value = l;
12049 else
12050 {
12051 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12052 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12053 return bytes;
12054 }
12055
12056 return NULL;
12057}
12058
12059/* Read a constant value from an attribute. Either set *VALUE, or if
12060 the value does not fit in *VALUE, set *BYTES - either already
12061 allocated on the objfile obstack, or newly allocated on OBSTACK,
12062 or, set *BATON, if we translated the constant to a location
12063 expression. */
12064
12065static void
12066dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12067 const char *name, struct obstack *obstack,
12068 struct dwarf2_cu *cu,
12069 long *value, gdb_byte **bytes,
12070 struct dwarf2_locexpr_baton **baton)
12071{
12072 struct objfile *objfile = cu->objfile;
12073 struct comp_unit_head *cu_header = &cu->header;
c906108c 12074 struct dwarf_block *blk;
98bfdba5
PA
12075 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12076 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12077
12078 *value = 0;
12079 *bytes = NULL;
12080 *baton = NULL;
c906108c
SS
12081
12082 switch (attr->form)
12083 {
12084 case DW_FORM_addr:
ac56253d 12085 {
ac56253d
TT
12086 gdb_byte *data;
12087
98bfdba5
PA
12088 if (TYPE_LENGTH (type) != cu_header->addr_size)
12089 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12090 cu_header->addr_size,
98bfdba5 12091 TYPE_LENGTH (type));
ac56253d
TT
12092 /* Symbols of this form are reasonably rare, so we just
12093 piggyback on the existing location code rather than writing
12094 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12095 *baton = obstack_alloc (&objfile->objfile_obstack,
12096 sizeof (struct dwarf2_locexpr_baton));
12097 (*baton)->per_cu = cu->per_cu;
12098 gdb_assert ((*baton)->per_cu);
ac56253d 12099
98bfdba5
PA
12100 (*baton)->size = 2 + cu_header->addr_size;
12101 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12102 (*baton)->data = data;
ac56253d
TT
12103
12104 data[0] = DW_OP_addr;
12105 store_unsigned_integer (&data[1], cu_header->addr_size,
12106 byte_order, DW_ADDR (attr));
12107 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12108 }
c906108c 12109 break;
4ac36638 12110 case DW_FORM_string:
93b5768b 12111 case DW_FORM_strp:
98bfdba5
PA
12112 /* DW_STRING is already allocated on the objfile obstack, point
12113 directly to it. */
12114 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12115 break;
c906108c
SS
12116 case DW_FORM_block1:
12117 case DW_FORM_block2:
12118 case DW_FORM_block4:
12119 case DW_FORM_block:
2dc7f7b3 12120 case DW_FORM_exprloc:
c906108c 12121 blk = DW_BLOCK (attr);
98bfdba5
PA
12122 if (TYPE_LENGTH (type) != blk->size)
12123 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12124 TYPE_LENGTH (type));
12125 *bytes = blk->data;
c906108c 12126 break;
2df3850c
JM
12127
12128 /* The DW_AT_const_value attributes are supposed to carry the
12129 symbol's value "represented as it would be on the target
12130 architecture." By the time we get here, it's already been
12131 converted to host endianness, so we just need to sign- or
12132 zero-extend it as appropriate. */
12133 case DW_FORM_data1:
3e43a32a
MS
12134 *bytes = dwarf2_const_value_data (attr, type, name,
12135 obstack, cu, value, 8);
2df3850c 12136 break;
c906108c 12137 case DW_FORM_data2:
3e43a32a
MS
12138 *bytes = dwarf2_const_value_data (attr, type, name,
12139 obstack, cu, value, 16);
2df3850c 12140 break;
c906108c 12141 case DW_FORM_data4:
3e43a32a
MS
12142 *bytes = dwarf2_const_value_data (attr, type, name,
12143 obstack, cu, value, 32);
2df3850c 12144 break;
c906108c 12145 case DW_FORM_data8:
3e43a32a
MS
12146 *bytes = dwarf2_const_value_data (attr, type, name,
12147 obstack, cu, value, 64);
2df3850c
JM
12148 break;
12149
c906108c 12150 case DW_FORM_sdata:
98bfdba5 12151 *value = DW_SND (attr);
2df3850c
JM
12152 break;
12153
c906108c 12154 case DW_FORM_udata:
98bfdba5 12155 *value = DW_UNSND (attr);
c906108c 12156 break;
2df3850c 12157
c906108c 12158 default:
4d3c2250 12159 complaint (&symfile_complaints,
e2e0b3e5 12160 _("unsupported const value attribute form: '%s'"),
4d3c2250 12161 dwarf_form_name (attr->form));
98bfdba5 12162 *value = 0;
c906108c
SS
12163 break;
12164 }
12165}
12166
2df3850c 12167
98bfdba5
PA
12168/* Copy constant value from an attribute to a symbol. */
12169
2df3850c 12170static void
98bfdba5
PA
12171dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12172 struct dwarf2_cu *cu)
2df3850c 12173{
98bfdba5
PA
12174 struct objfile *objfile = cu->objfile;
12175 struct comp_unit_head *cu_header = &cu->header;
12176 long value;
12177 gdb_byte *bytes;
12178 struct dwarf2_locexpr_baton *baton;
2df3850c 12179
98bfdba5
PA
12180 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12181 SYMBOL_PRINT_NAME (sym),
12182 &objfile->objfile_obstack, cu,
12183 &value, &bytes, &baton);
2df3850c 12184
98bfdba5
PA
12185 if (baton != NULL)
12186 {
12187 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12188 SYMBOL_LOCATION_BATON (sym) = baton;
12189 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12190 }
12191 else if (bytes != NULL)
12192 {
12193 SYMBOL_VALUE_BYTES (sym) = bytes;
12194 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12195 }
12196 else
12197 {
12198 SYMBOL_VALUE (sym) = value;
12199 SYMBOL_CLASS (sym) = LOC_CONST;
12200 }
2df3850c
JM
12201}
12202
c906108c
SS
12203/* Return the type of the die in question using its DW_AT_type attribute. */
12204
12205static struct type *
e7c27a73 12206die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12207{
c906108c 12208 struct attribute *type_attr;
c906108c 12209
e142c38c 12210 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12211 if (!type_attr)
12212 {
12213 /* A missing DW_AT_type represents a void type. */
46bf5051 12214 return objfile_type (cu->objfile)->builtin_void;
c906108c 12215 }
348e048f 12216
673bfd45 12217 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12218}
12219
b4ba55a1
JB
12220/* True iff CU's producer generates GNAT Ada auxiliary information
12221 that allows to find parallel types through that information instead
12222 of having to do expensive parallel lookups by type name. */
12223
12224static int
12225need_gnat_info (struct dwarf2_cu *cu)
12226{
12227 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12228 of GNAT produces this auxiliary information, without any indication
12229 that it is produced. Part of enhancing the FSF version of GNAT
12230 to produce that information will be to put in place an indicator
12231 that we can use in order to determine whether the descriptive type
12232 info is available or not. One suggestion that has been made is
12233 to use a new attribute, attached to the CU die. For now, assume
12234 that the descriptive type info is not available. */
12235 return 0;
12236}
12237
b4ba55a1
JB
12238/* Return the auxiliary type of the die in question using its
12239 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12240 attribute is not present. */
12241
12242static struct type *
12243die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12244{
b4ba55a1 12245 struct attribute *type_attr;
b4ba55a1
JB
12246
12247 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12248 if (!type_attr)
12249 return NULL;
12250
673bfd45 12251 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12252}
12253
12254/* If DIE has a descriptive_type attribute, then set the TYPE's
12255 descriptive type accordingly. */
12256
12257static void
12258set_descriptive_type (struct type *type, struct die_info *die,
12259 struct dwarf2_cu *cu)
12260{
12261 struct type *descriptive_type = die_descriptive_type (die, cu);
12262
12263 if (descriptive_type)
12264 {
12265 ALLOCATE_GNAT_AUX_TYPE (type);
12266 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12267 }
12268}
12269
c906108c
SS
12270/* Return the containing type of the die in question using its
12271 DW_AT_containing_type attribute. */
12272
12273static struct type *
e7c27a73 12274die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12275{
c906108c 12276 struct attribute *type_attr;
c906108c 12277
e142c38c 12278 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12279 if (!type_attr)
12280 error (_("Dwarf Error: Problem turning containing type into gdb type "
12281 "[in module %s]"), cu->objfile->name);
12282
673bfd45 12283 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12284}
12285
673bfd45
DE
12286/* Look up the type of DIE in CU using its type attribute ATTR.
12287 If there is no type substitute an error marker. */
12288
c906108c 12289static struct type *
673bfd45
DE
12290lookup_die_type (struct die_info *die, struct attribute *attr,
12291 struct dwarf2_cu *cu)
c906108c 12292{
f792889a
DJ
12293 struct type *this_type;
12294
673bfd45
DE
12295 /* First see if we have it cached. */
12296
12297 if (is_ref_attr (attr))
12298 {
12299 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12300
12301 this_type = get_die_type_at_offset (offset, cu->per_cu);
12302 }
55f1336d 12303 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12304 {
12305 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12306 struct dwarf2_cu *sig_cu;
12307 unsigned int offset;
12308
12309 /* sig_type will be NULL if the signatured type is missing from
12310 the debug info. */
12311 if (sig_type == NULL)
12312 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12313 "at 0x%x [in module %s]"),
12314 die->offset, cu->objfile->name);
12315
8b70b953 12316 gdb_assert (sig_type->per_cu.debug_type_section);
b3c8eb43 12317 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
12318 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12319 }
12320 else
12321 {
12322 dump_die_for_error (die);
12323 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12324 dwarf_attr_name (attr->name), cu->objfile->name);
12325 }
12326
12327 /* If not cached we need to read it in. */
12328
12329 if (this_type == NULL)
12330 {
12331 struct die_info *type_die;
12332 struct dwarf2_cu *type_cu = cu;
12333
12334 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12335 /* If the type is cached, we should have found it above. */
12336 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12337 this_type = read_type_die_1 (type_die, type_cu);
12338 }
12339
12340 /* If we still don't have a type use an error marker. */
12341
12342 if (this_type == NULL)
c906108c 12343 {
b00fdb78
TT
12344 char *message, *saved;
12345
12346 /* read_type_die already issued a complaint. */
12347 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12348 cu->objfile->name,
12349 cu->header.offset,
12350 die->offset);
12351 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12352 message, strlen (message));
12353 xfree (message);
12354
12355 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 12356 }
673bfd45 12357
f792889a 12358 return this_type;
c906108c
SS
12359}
12360
673bfd45
DE
12361/* Return the type in DIE, CU.
12362 Returns NULL for invalid types.
12363
12364 This first does a lookup in the appropriate type_hash table,
12365 and only reads the die in if necessary.
12366
12367 NOTE: This can be called when reading in partial or full symbols. */
12368
f792889a 12369static struct type *
e7c27a73 12370read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12371{
f792889a
DJ
12372 struct type *this_type;
12373
12374 this_type = get_die_type (die, cu);
12375 if (this_type)
12376 return this_type;
12377
673bfd45
DE
12378 return read_type_die_1 (die, cu);
12379}
12380
12381/* Read the type in DIE, CU.
12382 Returns NULL for invalid types. */
12383
12384static struct type *
12385read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12386{
12387 struct type *this_type = NULL;
12388
c906108c
SS
12389 switch (die->tag)
12390 {
12391 case DW_TAG_class_type:
680b30c7 12392 case DW_TAG_interface_type:
c906108c
SS
12393 case DW_TAG_structure_type:
12394 case DW_TAG_union_type:
f792889a 12395 this_type = read_structure_type (die, cu);
c906108c
SS
12396 break;
12397 case DW_TAG_enumeration_type:
f792889a 12398 this_type = read_enumeration_type (die, cu);
c906108c
SS
12399 break;
12400 case DW_TAG_subprogram:
12401 case DW_TAG_subroutine_type:
edb3359d 12402 case DW_TAG_inlined_subroutine:
f792889a 12403 this_type = read_subroutine_type (die, cu);
c906108c
SS
12404 break;
12405 case DW_TAG_array_type:
f792889a 12406 this_type = read_array_type (die, cu);
c906108c 12407 break;
72019c9c 12408 case DW_TAG_set_type:
f792889a 12409 this_type = read_set_type (die, cu);
72019c9c 12410 break;
c906108c 12411 case DW_TAG_pointer_type:
f792889a 12412 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12413 break;
12414 case DW_TAG_ptr_to_member_type:
f792889a 12415 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12416 break;
12417 case DW_TAG_reference_type:
f792889a 12418 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12419 break;
12420 case DW_TAG_const_type:
f792889a 12421 this_type = read_tag_const_type (die, cu);
c906108c
SS
12422 break;
12423 case DW_TAG_volatile_type:
f792889a 12424 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12425 break;
12426 case DW_TAG_string_type:
f792889a 12427 this_type = read_tag_string_type (die, cu);
c906108c
SS
12428 break;
12429 case DW_TAG_typedef:
f792889a 12430 this_type = read_typedef (die, cu);
c906108c 12431 break;
a02abb62 12432 case DW_TAG_subrange_type:
f792889a 12433 this_type = read_subrange_type (die, cu);
a02abb62 12434 break;
c906108c 12435 case DW_TAG_base_type:
f792889a 12436 this_type = read_base_type (die, cu);
c906108c 12437 break;
81a17f79 12438 case DW_TAG_unspecified_type:
f792889a 12439 this_type = read_unspecified_type (die, cu);
81a17f79 12440 break;
0114d602
DJ
12441 case DW_TAG_namespace:
12442 this_type = read_namespace_type (die, cu);
12443 break;
f55ee35c
JK
12444 case DW_TAG_module:
12445 this_type = read_module_type (die, cu);
12446 break;
c906108c 12447 default:
3e43a32a
MS
12448 complaint (&symfile_complaints,
12449 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12450 dwarf_tag_name (die->tag));
c906108c
SS
12451 break;
12452 }
63d06c5c 12453
f792889a 12454 return this_type;
63d06c5c
DC
12455}
12456
abc72ce4
DE
12457/* See if we can figure out if the class lives in a namespace. We do
12458 this by looking for a member function; its demangled name will
12459 contain namespace info, if there is any.
12460 Return the computed name or NULL.
12461 Space for the result is allocated on the objfile's obstack.
12462 This is the full-die version of guess_partial_die_structure_name.
12463 In this case we know DIE has no useful parent. */
12464
12465static char *
12466guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12467{
12468 struct die_info *spec_die;
12469 struct dwarf2_cu *spec_cu;
12470 struct die_info *child;
12471
12472 spec_cu = cu;
12473 spec_die = die_specification (die, &spec_cu);
12474 if (spec_die != NULL)
12475 {
12476 die = spec_die;
12477 cu = spec_cu;
12478 }
12479
12480 for (child = die->child;
12481 child != NULL;
12482 child = child->sibling)
12483 {
12484 if (child->tag == DW_TAG_subprogram)
12485 {
12486 struct attribute *attr;
12487
12488 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12489 if (attr == NULL)
12490 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12491 if (attr != NULL)
12492 {
12493 char *actual_name
12494 = language_class_name_from_physname (cu->language_defn,
12495 DW_STRING (attr));
12496 char *name = NULL;
12497
12498 if (actual_name != NULL)
12499 {
12500 char *die_name = dwarf2_name (die, cu);
12501
12502 if (die_name != NULL
12503 && strcmp (die_name, actual_name) != 0)
12504 {
12505 /* Strip off the class name from the full name.
12506 We want the prefix. */
12507 int die_name_len = strlen (die_name);
12508 int actual_name_len = strlen (actual_name);
12509
12510 /* Test for '::' as a sanity check. */
12511 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12512 && actual_name[actual_name_len
12513 - die_name_len - 1] == ':')
abc72ce4
DE
12514 name =
12515 obsavestring (actual_name,
12516 actual_name_len - die_name_len - 2,
12517 &cu->objfile->objfile_obstack);
12518 }
12519 }
12520 xfree (actual_name);
12521 return name;
12522 }
12523 }
12524 }
12525
12526 return NULL;
12527}
12528
96408a79
SA
12529/* GCC might emit a nameless typedef that has a linkage name. Determine the
12530 prefix part in such case. See
12531 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12532
12533static char *
12534anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12535{
12536 struct attribute *attr;
12537 char *base;
12538
12539 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12540 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12541 return NULL;
12542
12543 attr = dwarf2_attr (die, DW_AT_name, cu);
12544 if (attr != NULL && DW_STRING (attr) != NULL)
12545 return NULL;
12546
12547 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12548 if (attr == NULL)
12549 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12550 if (attr == NULL || DW_STRING (attr) == NULL)
12551 return NULL;
12552
12553 /* dwarf2_name had to be already called. */
12554 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12555
12556 /* Strip the base name, keep any leading namespaces/classes. */
12557 base = strrchr (DW_STRING (attr), ':');
12558 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12559 return "";
12560
12561 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12562 &cu->objfile->objfile_obstack);
12563}
12564
fdde2d81 12565/* Return the name of the namespace/class that DIE is defined within,
0114d602 12566 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12567
0114d602
DJ
12568 For example, if we're within the method foo() in the following
12569 code:
12570
12571 namespace N {
12572 class C {
12573 void foo () {
12574 }
12575 };
12576 }
12577
12578 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12579
12580static char *
e142c38c 12581determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12582{
0114d602
DJ
12583 struct die_info *parent, *spec_die;
12584 struct dwarf2_cu *spec_cu;
12585 struct type *parent_type;
96408a79 12586 char *retval;
63d06c5c 12587
f55ee35c
JK
12588 if (cu->language != language_cplus && cu->language != language_java
12589 && cu->language != language_fortran)
0114d602
DJ
12590 return "";
12591
96408a79
SA
12592 retval = anonymous_struct_prefix (die, cu);
12593 if (retval)
12594 return retval;
12595
0114d602
DJ
12596 /* We have to be careful in the presence of DW_AT_specification.
12597 For example, with GCC 3.4, given the code
12598
12599 namespace N {
12600 void foo() {
12601 // Definition of N::foo.
12602 }
12603 }
12604
12605 then we'll have a tree of DIEs like this:
12606
12607 1: DW_TAG_compile_unit
12608 2: DW_TAG_namespace // N
12609 3: DW_TAG_subprogram // declaration of N::foo
12610 4: DW_TAG_subprogram // definition of N::foo
12611 DW_AT_specification // refers to die #3
12612
12613 Thus, when processing die #4, we have to pretend that we're in
12614 the context of its DW_AT_specification, namely the contex of die
12615 #3. */
12616 spec_cu = cu;
12617 spec_die = die_specification (die, &spec_cu);
12618 if (spec_die == NULL)
12619 parent = die->parent;
12620 else
63d06c5c 12621 {
0114d602
DJ
12622 parent = spec_die->parent;
12623 cu = spec_cu;
63d06c5c 12624 }
0114d602
DJ
12625
12626 if (parent == NULL)
12627 return "";
98bfdba5
PA
12628 else if (parent->building_fullname)
12629 {
12630 const char *name;
12631 const char *parent_name;
12632
12633 /* It has been seen on RealView 2.2 built binaries,
12634 DW_TAG_template_type_param types actually _defined_ as
12635 children of the parent class:
12636
12637 enum E {};
12638 template class <class Enum> Class{};
12639 Class<enum E> class_e;
12640
12641 1: DW_TAG_class_type (Class)
12642 2: DW_TAG_enumeration_type (E)
12643 3: DW_TAG_enumerator (enum1:0)
12644 3: DW_TAG_enumerator (enum2:1)
12645 ...
12646 2: DW_TAG_template_type_param
12647 DW_AT_type DW_FORM_ref_udata (E)
12648
12649 Besides being broken debug info, it can put GDB into an
12650 infinite loop. Consider:
12651
12652 When we're building the full name for Class<E>, we'll start
12653 at Class, and go look over its template type parameters,
12654 finding E. We'll then try to build the full name of E, and
12655 reach here. We're now trying to build the full name of E,
12656 and look over the parent DIE for containing scope. In the
12657 broken case, if we followed the parent DIE of E, we'd again
12658 find Class, and once again go look at its template type
12659 arguments, etc., etc. Simply don't consider such parent die
12660 as source-level parent of this die (it can't be, the language
12661 doesn't allow it), and break the loop here. */
12662 name = dwarf2_name (die, cu);
12663 parent_name = dwarf2_name (parent, cu);
12664 complaint (&symfile_complaints,
12665 _("template param type '%s' defined within parent '%s'"),
12666 name ? name : "<unknown>",
12667 parent_name ? parent_name : "<unknown>");
12668 return "";
12669 }
63d06c5c 12670 else
0114d602
DJ
12671 switch (parent->tag)
12672 {
63d06c5c 12673 case DW_TAG_namespace:
0114d602 12674 parent_type = read_type_die (parent, cu);
acebe513
UW
12675 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12676 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12677 Work around this problem here. */
12678 if (cu->language == language_cplus
12679 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12680 return "";
0114d602
DJ
12681 /* We give a name to even anonymous namespaces. */
12682 return TYPE_TAG_NAME (parent_type);
63d06c5c 12683 case DW_TAG_class_type:
680b30c7 12684 case DW_TAG_interface_type:
63d06c5c 12685 case DW_TAG_structure_type:
0114d602 12686 case DW_TAG_union_type:
f55ee35c 12687 case DW_TAG_module:
0114d602
DJ
12688 parent_type = read_type_die (parent, cu);
12689 if (TYPE_TAG_NAME (parent_type) != NULL)
12690 return TYPE_TAG_NAME (parent_type);
12691 else
12692 /* An anonymous structure is only allowed non-static data
12693 members; no typedefs, no member functions, et cetera.
12694 So it does not need a prefix. */
12695 return "";
abc72ce4
DE
12696 case DW_TAG_compile_unit:
12697 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12698 if (cu->language == language_cplus
8b70b953 12699 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12700 && die->child != NULL
12701 && (die->tag == DW_TAG_class_type
12702 || die->tag == DW_TAG_structure_type
12703 || die->tag == DW_TAG_union_type))
12704 {
12705 char *name = guess_full_die_structure_name (die, cu);
12706 if (name != NULL)
12707 return name;
12708 }
12709 return "";
63d06c5c 12710 default:
8176b9b8 12711 return determine_prefix (parent, cu);
63d06c5c 12712 }
63d06c5c
DC
12713}
12714
3e43a32a
MS
12715/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12716 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12717 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12718 an obconcat, otherwise allocate storage for the result. The CU argument is
12719 used to determine the language and hence, the appropriate separator. */
987504bb 12720
f55ee35c 12721#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12722
12723static char *
f55ee35c
JK
12724typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12725 int physname, struct dwarf2_cu *cu)
63d06c5c 12726{
f55ee35c 12727 const char *lead = "";
5c315b68 12728 const char *sep;
63d06c5c 12729
3e43a32a
MS
12730 if (suffix == NULL || suffix[0] == '\0'
12731 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12732 sep = "";
12733 else if (cu->language == language_java)
12734 sep = ".";
f55ee35c
JK
12735 else if (cu->language == language_fortran && physname)
12736 {
12737 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12738 DW_AT_MIPS_linkage_name is preferred and used instead. */
12739
12740 lead = "__";
12741 sep = "_MOD_";
12742 }
987504bb
JJ
12743 else
12744 sep = "::";
63d06c5c 12745
6dd47d34
DE
12746 if (prefix == NULL)
12747 prefix = "";
12748 if (suffix == NULL)
12749 suffix = "";
12750
987504bb
JJ
12751 if (obs == NULL)
12752 {
3e43a32a
MS
12753 char *retval
12754 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12755
f55ee35c
JK
12756 strcpy (retval, lead);
12757 strcat (retval, prefix);
6dd47d34
DE
12758 strcat (retval, sep);
12759 strcat (retval, suffix);
63d06c5c
DC
12760 return retval;
12761 }
987504bb
JJ
12762 else
12763 {
12764 /* We have an obstack. */
f55ee35c 12765 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12766 }
63d06c5c
DC
12767}
12768
c906108c
SS
12769/* Return sibling of die, NULL if no sibling. */
12770
f9aca02d 12771static struct die_info *
fba45db2 12772sibling_die (struct die_info *die)
c906108c 12773{
639d11d3 12774 return die->sibling;
c906108c
SS
12775}
12776
71c25dea
TT
12777/* Get name of a die, return NULL if not found. */
12778
12779static char *
12780dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12781 struct obstack *obstack)
12782{
12783 if (name && cu->language == language_cplus)
12784 {
12785 char *canon_name = cp_canonicalize_string (name);
12786
12787 if (canon_name != NULL)
12788 {
12789 if (strcmp (canon_name, name) != 0)
12790 name = obsavestring (canon_name, strlen (canon_name),
12791 obstack);
12792 xfree (canon_name);
12793 }
12794 }
12795
12796 return name;
c906108c
SS
12797}
12798
9219021c
DC
12799/* Get name of a die, return NULL if not found. */
12800
12801static char *
e142c38c 12802dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12803{
12804 struct attribute *attr;
12805
e142c38c 12806 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12807 if ((!attr || !DW_STRING (attr))
12808 && die->tag != DW_TAG_class_type
12809 && die->tag != DW_TAG_interface_type
12810 && die->tag != DW_TAG_structure_type
12811 && die->tag != DW_TAG_union_type)
71c25dea
TT
12812 return NULL;
12813
12814 switch (die->tag)
12815 {
12816 case DW_TAG_compile_unit:
12817 /* Compilation units have a DW_AT_name that is a filename, not
12818 a source language identifier. */
12819 case DW_TAG_enumeration_type:
12820 case DW_TAG_enumerator:
12821 /* These tags always have simple identifiers already; no need
12822 to canonicalize them. */
12823 return DW_STRING (attr);
907af001 12824
418835cc
KS
12825 case DW_TAG_subprogram:
12826 /* Java constructors will all be named "<init>", so return
12827 the class name when we see this special case. */
12828 if (cu->language == language_java
12829 && DW_STRING (attr) != NULL
12830 && strcmp (DW_STRING (attr), "<init>") == 0)
12831 {
12832 struct dwarf2_cu *spec_cu = cu;
12833 struct die_info *spec_die;
12834
12835 /* GCJ will output '<init>' for Java constructor names.
12836 For this special case, return the name of the parent class. */
12837
12838 /* GCJ may output suprogram DIEs with AT_specification set.
12839 If so, use the name of the specified DIE. */
12840 spec_die = die_specification (die, &spec_cu);
12841 if (spec_die != NULL)
12842 return dwarf2_name (spec_die, spec_cu);
12843
12844 do
12845 {
12846 die = die->parent;
12847 if (die->tag == DW_TAG_class_type)
12848 return dwarf2_name (die, cu);
12849 }
12850 while (die->tag != DW_TAG_compile_unit);
12851 }
907af001
UW
12852 break;
12853
12854 case DW_TAG_class_type:
12855 case DW_TAG_interface_type:
12856 case DW_TAG_structure_type:
12857 case DW_TAG_union_type:
12858 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12859 structures or unions. These were of the form "._%d" in GCC 4.1,
12860 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12861 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12862 if (attr && DW_STRING (attr)
12863 && (strncmp (DW_STRING (attr), "._", 2) == 0
12864 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12865 return NULL;
53832f31
TT
12866
12867 /* GCC might emit a nameless typedef that has a linkage name. See
12868 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12869 if (!attr || DW_STRING (attr) == NULL)
12870 {
df5c6c50 12871 char *demangled = NULL;
53832f31
TT
12872
12873 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12874 if (attr == NULL)
12875 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12876
12877 if (attr == NULL || DW_STRING (attr) == NULL)
12878 return NULL;
12879
df5c6c50
JK
12880 /* Avoid demangling DW_STRING (attr) the second time on a second
12881 call for the same DIE. */
12882 if (!DW_STRING_IS_CANONICAL (attr))
12883 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12884
12885 if (demangled)
12886 {
96408a79
SA
12887 char *base;
12888
53832f31 12889 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12890 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12891 &cu->objfile->objfile_obstack);
53832f31
TT
12892 DW_STRING_IS_CANONICAL (attr) = 1;
12893 xfree (demangled);
96408a79
SA
12894
12895 /* Strip any leading namespaces/classes, keep only the base name.
12896 DW_AT_name for named DIEs does not contain the prefixes. */
12897 base = strrchr (DW_STRING (attr), ':');
12898 if (base && base > DW_STRING (attr) && base[-1] == ':')
12899 return &base[1];
12900 else
12901 return DW_STRING (attr);
53832f31
TT
12902 }
12903 }
907af001
UW
12904 break;
12905
71c25dea 12906 default:
907af001
UW
12907 break;
12908 }
12909
12910 if (!DW_STRING_IS_CANONICAL (attr))
12911 {
12912 DW_STRING (attr)
12913 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12914 &cu->objfile->objfile_obstack);
12915 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12916 }
907af001 12917 return DW_STRING (attr);
9219021c
DC
12918}
12919
12920/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12921 is none. *EXT_CU is the CU containing DIE on input, and the CU
12922 containing the return value on output. */
9219021c
DC
12923
12924static struct die_info *
f2f0e013 12925dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12926{
12927 struct attribute *attr;
9219021c 12928
f2f0e013 12929 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12930 if (attr == NULL)
12931 return NULL;
12932
f2f0e013 12933 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12934}
12935
c906108c
SS
12936/* Convert a DIE tag into its string name. */
12937
12938static char *
aa1ee363 12939dwarf_tag_name (unsigned tag)
c906108c
SS
12940{
12941 switch (tag)
12942 {
12943 case DW_TAG_padding:
12944 return "DW_TAG_padding";
12945 case DW_TAG_array_type:
12946 return "DW_TAG_array_type";
12947 case DW_TAG_class_type:
12948 return "DW_TAG_class_type";
12949 case DW_TAG_entry_point:
12950 return "DW_TAG_entry_point";
12951 case DW_TAG_enumeration_type:
12952 return "DW_TAG_enumeration_type";
12953 case DW_TAG_formal_parameter:
12954 return "DW_TAG_formal_parameter";
12955 case DW_TAG_imported_declaration:
12956 return "DW_TAG_imported_declaration";
12957 case DW_TAG_label:
12958 return "DW_TAG_label";
12959 case DW_TAG_lexical_block:
12960 return "DW_TAG_lexical_block";
12961 case DW_TAG_member:
12962 return "DW_TAG_member";
12963 case DW_TAG_pointer_type:
12964 return "DW_TAG_pointer_type";
12965 case DW_TAG_reference_type:
12966 return "DW_TAG_reference_type";
12967 case DW_TAG_compile_unit:
12968 return "DW_TAG_compile_unit";
12969 case DW_TAG_string_type:
12970 return "DW_TAG_string_type";
12971 case DW_TAG_structure_type:
12972 return "DW_TAG_structure_type";
12973 case DW_TAG_subroutine_type:
12974 return "DW_TAG_subroutine_type";
12975 case DW_TAG_typedef:
12976 return "DW_TAG_typedef";
12977 case DW_TAG_union_type:
12978 return "DW_TAG_union_type";
12979 case DW_TAG_unspecified_parameters:
12980 return "DW_TAG_unspecified_parameters";
12981 case DW_TAG_variant:
12982 return "DW_TAG_variant";
12983 case DW_TAG_common_block:
12984 return "DW_TAG_common_block";
12985 case DW_TAG_common_inclusion:
12986 return "DW_TAG_common_inclusion";
12987 case DW_TAG_inheritance:
12988 return "DW_TAG_inheritance";
12989 case DW_TAG_inlined_subroutine:
12990 return "DW_TAG_inlined_subroutine";
12991 case DW_TAG_module:
12992 return "DW_TAG_module";
12993 case DW_TAG_ptr_to_member_type:
12994 return "DW_TAG_ptr_to_member_type";
12995 case DW_TAG_set_type:
12996 return "DW_TAG_set_type";
12997 case DW_TAG_subrange_type:
12998 return "DW_TAG_subrange_type";
12999 case DW_TAG_with_stmt:
13000 return "DW_TAG_with_stmt";
13001 case DW_TAG_access_declaration:
13002 return "DW_TAG_access_declaration";
13003 case DW_TAG_base_type:
13004 return "DW_TAG_base_type";
13005 case DW_TAG_catch_block:
13006 return "DW_TAG_catch_block";
13007 case DW_TAG_const_type:
13008 return "DW_TAG_const_type";
13009 case DW_TAG_constant:
13010 return "DW_TAG_constant";
13011 case DW_TAG_enumerator:
13012 return "DW_TAG_enumerator";
13013 case DW_TAG_file_type:
13014 return "DW_TAG_file_type";
13015 case DW_TAG_friend:
13016 return "DW_TAG_friend";
13017 case DW_TAG_namelist:
13018 return "DW_TAG_namelist";
13019 case DW_TAG_namelist_item:
13020 return "DW_TAG_namelist_item";
13021 case DW_TAG_packed_type:
13022 return "DW_TAG_packed_type";
13023 case DW_TAG_subprogram:
13024 return "DW_TAG_subprogram";
13025 case DW_TAG_template_type_param:
13026 return "DW_TAG_template_type_param";
13027 case DW_TAG_template_value_param:
13028 return "DW_TAG_template_value_param";
13029 case DW_TAG_thrown_type:
13030 return "DW_TAG_thrown_type";
13031 case DW_TAG_try_block:
13032 return "DW_TAG_try_block";
13033 case DW_TAG_variant_part:
13034 return "DW_TAG_variant_part";
13035 case DW_TAG_variable:
13036 return "DW_TAG_variable";
13037 case DW_TAG_volatile_type:
13038 return "DW_TAG_volatile_type";
d9fa45fe
DC
13039 case DW_TAG_dwarf_procedure:
13040 return "DW_TAG_dwarf_procedure";
13041 case DW_TAG_restrict_type:
13042 return "DW_TAG_restrict_type";
13043 case DW_TAG_interface_type:
13044 return "DW_TAG_interface_type";
13045 case DW_TAG_namespace:
13046 return "DW_TAG_namespace";
13047 case DW_TAG_imported_module:
13048 return "DW_TAG_imported_module";
13049 case DW_TAG_unspecified_type:
13050 return "DW_TAG_unspecified_type";
13051 case DW_TAG_partial_unit:
13052 return "DW_TAG_partial_unit";
13053 case DW_TAG_imported_unit:
13054 return "DW_TAG_imported_unit";
b7619582
GF
13055 case DW_TAG_condition:
13056 return "DW_TAG_condition";
13057 case DW_TAG_shared_type:
13058 return "DW_TAG_shared_type";
348e048f
DE
13059 case DW_TAG_type_unit:
13060 return "DW_TAG_type_unit";
c906108c
SS
13061 case DW_TAG_MIPS_loop:
13062 return "DW_TAG_MIPS_loop";
b7619582
GF
13063 case DW_TAG_HP_array_descriptor:
13064 return "DW_TAG_HP_array_descriptor";
c906108c
SS
13065 case DW_TAG_format_label:
13066 return "DW_TAG_format_label";
13067 case DW_TAG_function_template:
13068 return "DW_TAG_function_template";
13069 case DW_TAG_class_template:
13070 return "DW_TAG_class_template";
b7619582
GF
13071 case DW_TAG_GNU_BINCL:
13072 return "DW_TAG_GNU_BINCL";
13073 case DW_TAG_GNU_EINCL:
13074 return "DW_TAG_GNU_EINCL";
13075 case DW_TAG_upc_shared_type:
13076 return "DW_TAG_upc_shared_type";
13077 case DW_TAG_upc_strict_type:
13078 return "DW_TAG_upc_strict_type";
13079 case DW_TAG_upc_relaxed_type:
13080 return "DW_TAG_upc_relaxed_type";
13081 case DW_TAG_PGI_kanji_type:
13082 return "DW_TAG_PGI_kanji_type";
13083 case DW_TAG_PGI_interface_block:
13084 return "DW_TAG_PGI_interface_block";
96408a79
SA
13085 case DW_TAG_GNU_call_site:
13086 return "DW_TAG_GNU_call_site";
c906108c
SS
13087 default:
13088 return "DW_TAG_<unknown>";
13089 }
13090}
13091
13092/* Convert a DWARF attribute code into its string name. */
13093
13094static char *
aa1ee363 13095dwarf_attr_name (unsigned attr)
c906108c
SS
13096{
13097 switch (attr)
13098 {
13099 case DW_AT_sibling:
13100 return "DW_AT_sibling";
13101 case DW_AT_location:
13102 return "DW_AT_location";
13103 case DW_AT_name:
13104 return "DW_AT_name";
13105 case DW_AT_ordering:
13106 return "DW_AT_ordering";
13107 case DW_AT_subscr_data:
13108 return "DW_AT_subscr_data";
13109 case DW_AT_byte_size:
13110 return "DW_AT_byte_size";
13111 case DW_AT_bit_offset:
13112 return "DW_AT_bit_offset";
13113 case DW_AT_bit_size:
13114 return "DW_AT_bit_size";
13115 case DW_AT_element_list:
13116 return "DW_AT_element_list";
13117 case DW_AT_stmt_list:
13118 return "DW_AT_stmt_list";
13119 case DW_AT_low_pc:
13120 return "DW_AT_low_pc";
13121 case DW_AT_high_pc:
13122 return "DW_AT_high_pc";
13123 case DW_AT_language:
13124 return "DW_AT_language";
13125 case DW_AT_member:
13126 return "DW_AT_member";
13127 case DW_AT_discr:
13128 return "DW_AT_discr";
13129 case DW_AT_discr_value:
13130 return "DW_AT_discr_value";
13131 case DW_AT_visibility:
13132 return "DW_AT_visibility";
13133 case DW_AT_import:
13134 return "DW_AT_import";
13135 case DW_AT_string_length:
13136 return "DW_AT_string_length";
13137 case DW_AT_common_reference:
13138 return "DW_AT_common_reference";
13139 case DW_AT_comp_dir:
13140 return "DW_AT_comp_dir";
13141 case DW_AT_const_value:
13142 return "DW_AT_const_value";
13143 case DW_AT_containing_type:
13144 return "DW_AT_containing_type";
13145 case DW_AT_default_value:
13146 return "DW_AT_default_value";
13147 case DW_AT_inline:
13148 return "DW_AT_inline";
13149 case DW_AT_is_optional:
13150 return "DW_AT_is_optional";
13151 case DW_AT_lower_bound:
13152 return "DW_AT_lower_bound";
13153 case DW_AT_producer:
13154 return "DW_AT_producer";
13155 case DW_AT_prototyped:
13156 return "DW_AT_prototyped";
13157 case DW_AT_return_addr:
13158 return "DW_AT_return_addr";
13159 case DW_AT_start_scope:
13160 return "DW_AT_start_scope";
09fa0d7c
JK
13161 case DW_AT_bit_stride:
13162 return "DW_AT_bit_stride";
c906108c
SS
13163 case DW_AT_upper_bound:
13164 return "DW_AT_upper_bound";
13165 case DW_AT_abstract_origin:
13166 return "DW_AT_abstract_origin";
13167 case DW_AT_accessibility:
13168 return "DW_AT_accessibility";
13169 case DW_AT_address_class:
13170 return "DW_AT_address_class";
13171 case DW_AT_artificial:
13172 return "DW_AT_artificial";
13173 case DW_AT_base_types:
13174 return "DW_AT_base_types";
13175 case DW_AT_calling_convention:
13176 return "DW_AT_calling_convention";
13177 case DW_AT_count:
13178 return "DW_AT_count";
13179 case DW_AT_data_member_location:
13180 return "DW_AT_data_member_location";
13181 case DW_AT_decl_column:
13182 return "DW_AT_decl_column";
13183 case DW_AT_decl_file:
13184 return "DW_AT_decl_file";
13185 case DW_AT_decl_line:
13186 return "DW_AT_decl_line";
13187 case DW_AT_declaration:
13188 return "DW_AT_declaration";
13189 case DW_AT_discr_list:
13190 return "DW_AT_discr_list";
13191 case DW_AT_encoding:
13192 return "DW_AT_encoding";
13193 case DW_AT_external:
13194 return "DW_AT_external";
13195 case DW_AT_frame_base:
13196 return "DW_AT_frame_base";
13197 case DW_AT_friend:
13198 return "DW_AT_friend";
13199 case DW_AT_identifier_case:
13200 return "DW_AT_identifier_case";
13201 case DW_AT_macro_info:
13202 return "DW_AT_macro_info";
13203 case DW_AT_namelist_items:
13204 return "DW_AT_namelist_items";
13205 case DW_AT_priority:
13206 return "DW_AT_priority";
13207 case DW_AT_segment:
13208 return "DW_AT_segment";
13209 case DW_AT_specification:
13210 return "DW_AT_specification";
13211 case DW_AT_static_link:
13212 return "DW_AT_static_link";
13213 case DW_AT_type:
13214 return "DW_AT_type";
13215 case DW_AT_use_location:
13216 return "DW_AT_use_location";
13217 case DW_AT_variable_parameter:
13218 return "DW_AT_variable_parameter";
13219 case DW_AT_virtuality:
13220 return "DW_AT_virtuality";
13221 case DW_AT_vtable_elem_location:
13222 return "DW_AT_vtable_elem_location";
b7619582 13223 /* DWARF 3 values. */
d9fa45fe
DC
13224 case DW_AT_allocated:
13225 return "DW_AT_allocated";
13226 case DW_AT_associated:
13227 return "DW_AT_associated";
13228 case DW_AT_data_location:
13229 return "DW_AT_data_location";
09fa0d7c
JK
13230 case DW_AT_byte_stride:
13231 return "DW_AT_byte_stride";
d9fa45fe
DC
13232 case DW_AT_entry_pc:
13233 return "DW_AT_entry_pc";
13234 case DW_AT_use_UTF8:
13235 return "DW_AT_use_UTF8";
13236 case DW_AT_extension:
13237 return "DW_AT_extension";
13238 case DW_AT_ranges:
13239 return "DW_AT_ranges";
13240 case DW_AT_trampoline:
13241 return "DW_AT_trampoline";
13242 case DW_AT_call_column:
13243 return "DW_AT_call_column";
13244 case DW_AT_call_file:
13245 return "DW_AT_call_file";
13246 case DW_AT_call_line:
13247 return "DW_AT_call_line";
b7619582
GF
13248 case DW_AT_description:
13249 return "DW_AT_description";
13250 case DW_AT_binary_scale:
13251 return "DW_AT_binary_scale";
13252 case DW_AT_decimal_scale:
13253 return "DW_AT_decimal_scale";
13254 case DW_AT_small:
13255 return "DW_AT_small";
13256 case DW_AT_decimal_sign:
13257 return "DW_AT_decimal_sign";
13258 case DW_AT_digit_count:
13259 return "DW_AT_digit_count";
13260 case DW_AT_picture_string:
13261 return "DW_AT_picture_string";
13262 case DW_AT_mutable:
13263 return "DW_AT_mutable";
13264 case DW_AT_threads_scaled:
13265 return "DW_AT_threads_scaled";
13266 case DW_AT_explicit:
13267 return "DW_AT_explicit";
13268 case DW_AT_object_pointer:
13269 return "DW_AT_object_pointer";
13270 case DW_AT_endianity:
13271 return "DW_AT_endianity";
13272 case DW_AT_elemental:
13273 return "DW_AT_elemental";
13274 case DW_AT_pure:
13275 return "DW_AT_pure";
13276 case DW_AT_recursive:
13277 return "DW_AT_recursive";
348e048f
DE
13278 /* DWARF 4 values. */
13279 case DW_AT_signature:
13280 return "DW_AT_signature";
31ef98ae
TT
13281 case DW_AT_linkage_name:
13282 return "DW_AT_linkage_name";
b7619582 13283 /* SGI/MIPS extensions. */
c764a876 13284#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13285 case DW_AT_MIPS_fde:
13286 return "DW_AT_MIPS_fde";
c764a876 13287#endif
c906108c
SS
13288 case DW_AT_MIPS_loop_begin:
13289 return "DW_AT_MIPS_loop_begin";
13290 case DW_AT_MIPS_tail_loop_begin:
13291 return "DW_AT_MIPS_tail_loop_begin";
13292 case DW_AT_MIPS_epilog_begin:
13293 return "DW_AT_MIPS_epilog_begin";
13294 case DW_AT_MIPS_loop_unroll_factor:
13295 return "DW_AT_MIPS_loop_unroll_factor";
13296 case DW_AT_MIPS_software_pipeline_depth:
13297 return "DW_AT_MIPS_software_pipeline_depth";
13298 case DW_AT_MIPS_linkage_name:
13299 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13300 case DW_AT_MIPS_stride:
13301 return "DW_AT_MIPS_stride";
13302 case DW_AT_MIPS_abstract_name:
13303 return "DW_AT_MIPS_abstract_name";
13304 case DW_AT_MIPS_clone_origin:
13305 return "DW_AT_MIPS_clone_origin";
13306 case DW_AT_MIPS_has_inlines:
13307 return "DW_AT_MIPS_has_inlines";
b7619582 13308 /* HP extensions. */
c764a876 13309#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13310 case DW_AT_HP_block_index:
13311 return "DW_AT_HP_block_index";
c764a876 13312#endif
b7619582
GF
13313 case DW_AT_HP_unmodifiable:
13314 return "DW_AT_HP_unmodifiable";
13315 case DW_AT_HP_actuals_stmt_list:
13316 return "DW_AT_HP_actuals_stmt_list";
13317 case DW_AT_HP_proc_per_section:
13318 return "DW_AT_HP_proc_per_section";
13319 case DW_AT_HP_raw_data_ptr:
13320 return "DW_AT_HP_raw_data_ptr";
13321 case DW_AT_HP_pass_by_reference:
13322 return "DW_AT_HP_pass_by_reference";
13323 case DW_AT_HP_opt_level:
13324 return "DW_AT_HP_opt_level";
13325 case DW_AT_HP_prof_version_id:
13326 return "DW_AT_HP_prof_version_id";
13327 case DW_AT_HP_opt_flags:
13328 return "DW_AT_HP_opt_flags";
13329 case DW_AT_HP_cold_region_low_pc:
13330 return "DW_AT_HP_cold_region_low_pc";
13331 case DW_AT_HP_cold_region_high_pc:
13332 return "DW_AT_HP_cold_region_high_pc";
13333 case DW_AT_HP_all_variables_modifiable:
13334 return "DW_AT_HP_all_variables_modifiable";
13335 case DW_AT_HP_linkage_name:
13336 return "DW_AT_HP_linkage_name";
13337 case DW_AT_HP_prof_flags:
13338 return "DW_AT_HP_prof_flags";
13339 /* GNU extensions. */
c906108c
SS
13340 case DW_AT_sf_names:
13341 return "DW_AT_sf_names";
13342 case DW_AT_src_info:
13343 return "DW_AT_src_info";
13344 case DW_AT_mac_info:
13345 return "DW_AT_mac_info";
13346 case DW_AT_src_coords:
13347 return "DW_AT_src_coords";
13348 case DW_AT_body_begin:
13349 return "DW_AT_body_begin";
13350 case DW_AT_body_end:
13351 return "DW_AT_body_end";
f5f8a009
EZ
13352 case DW_AT_GNU_vector:
13353 return "DW_AT_GNU_vector";
2de00c64
DE
13354 case DW_AT_GNU_odr_signature:
13355 return "DW_AT_GNU_odr_signature";
b7619582
GF
13356 /* VMS extensions. */
13357 case DW_AT_VMS_rtnbeg_pd_address:
13358 return "DW_AT_VMS_rtnbeg_pd_address";
13359 /* UPC extension. */
13360 case DW_AT_upc_threads_scaled:
13361 return "DW_AT_upc_threads_scaled";
13362 /* PGI (STMicroelectronics) extensions. */
13363 case DW_AT_PGI_lbase:
13364 return "DW_AT_PGI_lbase";
13365 case DW_AT_PGI_soffset:
13366 return "DW_AT_PGI_soffset";
13367 case DW_AT_PGI_lstride:
13368 return "DW_AT_PGI_lstride";
c906108c
SS
13369 default:
13370 return "DW_AT_<unknown>";
13371 }
13372}
13373
13374/* Convert a DWARF value form code into its string name. */
13375
13376static char *
aa1ee363 13377dwarf_form_name (unsigned form)
c906108c
SS
13378{
13379 switch (form)
13380 {
13381 case DW_FORM_addr:
13382 return "DW_FORM_addr";
13383 case DW_FORM_block2:
13384 return "DW_FORM_block2";
13385 case DW_FORM_block4:
13386 return "DW_FORM_block4";
13387 case DW_FORM_data2:
13388 return "DW_FORM_data2";
13389 case DW_FORM_data4:
13390 return "DW_FORM_data4";
13391 case DW_FORM_data8:
13392 return "DW_FORM_data8";
13393 case DW_FORM_string:
13394 return "DW_FORM_string";
13395 case DW_FORM_block:
13396 return "DW_FORM_block";
13397 case DW_FORM_block1:
13398 return "DW_FORM_block1";
13399 case DW_FORM_data1:
13400 return "DW_FORM_data1";
13401 case DW_FORM_flag:
13402 return "DW_FORM_flag";
13403 case DW_FORM_sdata:
13404 return "DW_FORM_sdata";
13405 case DW_FORM_strp:
13406 return "DW_FORM_strp";
13407 case DW_FORM_udata:
13408 return "DW_FORM_udata";
13409 case DW_FORM_ref_addr:
13410 return "DW_FORM_ref_addr";
13411 case DW_FORM_ref1:
13412 return "DW_FORM_ref1";
13413 case DW_FORM_ref2:
13414 return "DW_FORM_ref2";
13415 case DW_FORM_ref4:
13416 return "DW_FORM_ref4";
13417 case DW_FORM_ref8:
13418 return "DW_FORM_ref8";
13419 case DW_FORM_ref_udata:
13420 return "DW_FORM_ref_udata";
13421 case DW_FORM_indirect:
13422 return "DW_FORM_indirect";
348e048f
DE
13423 case DW_FORM_sec_offset:
13424 return "DW_FORM_sec_offset";
13425 case DW_FORM_exprloc:
13426 return "DW_FORM_exprloc";
13427 case DW_FORM_flag_present:
13428 return "DW_FORM_flag_present";
55f1336d
TT
13429 case DW_FORM_ref_sig8:
13430 return "DW_FORM_ref_sig8";
c906108c
SS
13431 default:
13432 return "DW_FORM_<unknown>";
13433 }
13434}
13435
13436/* Convert a DWARF stack opcode into its string name. */
13437
9eae7c52 13438const char *
b1bfef65 13439dwarf_stack_op_name (unsigned op)
c906108c
SS
13440{
13441 switch (op)
13442 {
13443 case DW_OP_addr:
13444 return "DW_OP_addr";
13445 case DW_OP_deref:
13446 return "DW_OP_deref";
13447 case DW_OP_const1u:
13448 return "DW_OP_const1u";
13449 case DW_OP_const1s:
13450 return "DW_OP_const1s";
13451 case DW_OP_const2u:
13452 return "DW_OP_const2u";
13453 case DW_OP_const2s:
13454 return "DW_OP_const2s";
13455 case DW_OP_const4u:
13456 return "DW_OP_const4u";
13457 case DW_OP_const4s:
13458 return "DW_OP_const4s";
13459 case DW_OP_const8u:
13460 return "DW_OP_const8u";
13461 case DW_OP_const8s:
13462 return "DW_OP_const8s";
13463 case DW_OP_constu:
13464 return "DW_OP_constu";
13465 case DW_OP_consts:
13466 return "DW_OP_consts";
13467 case DW_OP_dup:
13468 return "DW_OP_dup";
13469 case DW_OP_drop:
13470 return "DW_OP_drop";
13471 case DW_OP_over:
13472 return "DW_OP_over";
13473 case DW_OP_pick:
13474 return "DW_OP_pick";
13475 case DW_OP_swap:
13476 return "DW_OP_swap";
13477 case DW_OP_rot:
13478 return "DW_OP_rot";
13479 case DW_OP_xderef:
13480 return "DW_OP_xderef";
13481 case DW_OP_abs:
13482 return "DW_OP_abs";
13483 case DW_OP_and:
13484 return "DW_OP_and";
13485 case DW_OP_div:
13486 return "DW_OP_div";
13487 case DW_OP_minus:
13488 return "DW_OP_minus";
13489 case DW_OP_mod:
13490 return "DW_OP_mod";
13491 case DW_OP_mul:
13492 return "DW_OP_mul";
13493 case DW_OP_neg:
13494 return "DW_OP_neg";
13495 case DW_OP_not:
13496 return "DW_OP_not";
13497 case DW_OP_or:
13498 return "DW_OP_or";
13499 case DW_OP_plus:
13500 return "DW_OP_plus";
13501 case DW_OP_plus_uconst:
13502 return "DW_OP_plus_uconst";
13503 case DW_OP_shl:
13504 return "DW_OP_shl";
13505 case DW_OP_shr:
13506 return "DW_OP_shr";
13507 case DW_OP_shra:
13508 return "DW_OP_shra";
13509 case DW_OP_xor:
13510 return "DW_OP_xor";
13511 case DW_OP_bra:
13512 return "DW_OP_bra";
13513 case DW_OP_eq:
13514 return "DW_OP_eq";
13515 case DW_OP_ge:
13516 return "DW_OP_ge";
13517 case DW_OP_gt:
13518 return "DW_OP_gt";
13519 case DW_OP_le:
13520 return "DW_OP_le";
13521 case DW_OP_lt:
13522 return "DW_OP_lt";
13523 case DW_OP_ne:
13524 return "DW_OP_ne";
13525 case DW_OP_skip:
13526 return "DW_OP_skip";
13527 case DW_OP_lit0:
13528 return "DW_OP_lit0";
13529 case DW_OP_lit1:
13530 return "DW_OP_lit1";
13531 case DW_OP_lit2:
13532 return "DW_OP_lit2";
13533 case DW_OP_lit3:
13534 return "DW_OP_lit3";
13535 case DW_OP_lit4:
13536 return "DW_OP_lit4";
13537 case DW_OP_lit5:
13538 return "DW_OP_lit5";
13539 case DW_OP_lit6:
13540 return "DW_OP_lit6";
13541 case DW_OP_lit7:
13542 return "DW_OP_lit7";
13543 case DW_OP_lit8:
13544 return "DW_OP_lit8";
13545 case DW_OP_lit9:
13546 return "DW_OP_lit9";
13547 case DW_OP_lit10:
13548 return "DW_OP_lit10";
13549 case DW_OP_lit11:
13550 return "DW_OP_lit11";
13551 case DW_OP_lit12:
13552 return "DW_OP_lit12";
13553 case DW_OP_lit13:
13554 return "DW_OP_lit13";
13555 case DW_OP_lit14:
13556 return "DW_OP_lit14";
13557 case DW_OP_lit15:
13558 return "DW_OP_lit15";
13559 case DW_OP_lit16:
13560 return "DW_OP_lit16";
13561 case DW_OP_lit17:
13562 return "DW_OP_lit17";
13563 case DW_OP_lit18:
13564 return "DW_OP_lit18";
13565 case DW_OP_lit19:
13566 return "DW_OP_lit19";
13567 case DW_OP_lit20:
13568 return "DW_OP_lit20";
13569 case DW_OP_lit21:
13570 return "DW_OP_lit21";
13571 case DW_OP_lit22:
13572 return "DW_OP_lit22";
13573 case DW_OP_lit23:
13574 return "DW_OP_lit23";
13575 case DW_OP_lit24:
13576 return "DW_OP_lit24";
13577 case DW_OP_lit25:
13578 return "DW_OP_lit25";
13579 case DW_OP_lit26:
13580 return "DW_OP_lit26";
13581 case DW_OP_lit27:
13582 return "DW_OP_lit27";
13583 case DW_OP_lit28:
13584 return "DW_OP_lit28";
13585 case DW_OP_lit29:
13586 return "DW_OP_lit29";
13587 case DW_OP_lit30:
13588 return "DW_OP_lit30";
13589 case DW_OP_lit31:
13590 return "DW_OP_lit31";
13591 case DW_OP_reg0:
13592 return "DW_OP_reg0";
13593 case DW_OP_reg1:
13594 return "DW_OP_reg1";
13595 case DW_OP_reg2:
13596 return "DW_OP_reg2";
13597 case DW_OP_reg3:
13598 return "DW_OP_reg3";
13599 case DW_OP_reg4:
13600 return "DW_OP_reg4";
13601 case DW_OP_reg5:
13602 return "DW_OP_reg5";
13603 case DW_OP_reg6:
13604 return "DW_OP_reg6";
13605 case DW_OP_reg7:
13606 return "DW_OP_reg7";
13607 case DW_OP_reg8:
13608 return "DW_OP_reg8";
13609 case DW_OP_reg9:
13610 return "DW_OP_reg9";
13611 case DW_OP_reg10:
13612 return "DW_OP_reg10";
13613 case DW_OP_reg11:
13614 return "DW_OP_reg11";
13615 case DW_OP_reg12:
13616 return "DW_OP_reg12";
13617 case DW_OP_reg13:
13618 return "DW_OP_reg13";
13619 case DW_OP_reg14:
13620 return "DW_OP_reg14";
13621 case DW_OP_reg15:
13622 return "DW_OP_reg15";
13623 case DW_OP_reg16:
13624 return "DW_OP_reg16";
13625 case DW_OP_reg17:
13626 return "DW_OP_reg17";
13627 case DW_OP_reg18:
13628 return "DW_OP_reg18";
13629 case DW_OP_reg19:
13630 return "DW_OP_reg19";
13631 case DW_OP_reg20:
13632 return "DW_OP_reg20";
13633 case DW_OP_reg21:
13634 return "DW_OP_reg21";
13635 case DW_OP_reg22:
13636 return "DW_OP_reg22";
13637 case DW_OP_reg23:
13638 return "DW_OP_reg23";
13639 case DW_OP_reg24:
13640 return "DW_OP_reg24";
13641 case DW_OP_reg25:
13642 return "DW_OP_reg25";
13643 case DW_OP_reg26:
13644 return "DW_OP_reg26";
13645 case DW_OP_reg27:
13646 return "DW_OP_reg27";
13647 case DW_OP_reg28:
13648 return "DW_OP_reg28";
13649 case DW_OP_reg29:
13650 return "DW_OP_reg29";
13651 case DW_OP_reg30:
13652 return "DW_OP_reg30";
13653 case DW_OP_reg31:
13654 return "DW_OP_reg31";
13655 case DW_OP_breg0:
13656 return "DW_OP_breg0";
13657 case DW_OP_breg1:
13658 return "DW_OP_breg1";
13659 case DW_OP_breg2:
13660 return "DW_OP_breg2";
13661 case DW_OP_breg3:
13662 return "DW_OP_breg3";
13663 case DW_OP_breg4:
13664 return "DW_OP_breg4";
13665 case DW_OP_breg5:
13666 return "DW_OP_breg5";
13667 case DW_OP_breg6:
13668 return "DW_OP_breg6";
13669 case DW_OP_breg7:
13670 return "DW_OP_breg7";
13671 case DW_OP_breg8:
13672 return "DW_OP_breg8";
13673 case DW_OP_breg9:
13674 return "DW_OP_breg9";
13675 case DW_OP_breg10:
13676 return "DW_OP_breg10";
13677 case DW_OP_breg11:
13678 return "DW_OP_breg11";
13679 case DW_OP_breg12:
13680 return "DW_OP_breg12";
13681 case DW_OP_breg13:
13682 return "DW_OP_breg13";
13683 case DW_OP_breg14:
13684 return "DW_OP_breg14";
13685 case DW_OP_breg15:
13686 return "DW_OP_breg15";
13687 case DW_OP_breg16:
13688 return "DW_OP_breg16";
13689 case DW_OP_breg17:
13690 return "DW_OP_breg17";
13691 case DW_OP_breg18:
13692 return "DW_OP_breg18";
13693 case DW_OP_breg19:
13694 return "DW_OP_breg19";
13695 case DW_OP_breg20:
13696 return "DW_OP_breg20";
13697 case DW_OP_breg21:
13698 return "DW_OP_breg21";
13699 case DW_OP_breg22:
13700 return "DW_OP_breg22";
13701 case DW_OP_breg23:
13702 return "DW_OP_breg23";
13703 case DW_OP_breg24:
13704 return "DW_OP_breg24";
13705 case DW_OP_breg25:
13706 return "DW_OP_breg25";
13707 case DW_OP_breg26:
13708 return "DW_OP_breg26";
13709 case DW_OP_breg27:
13710 return "DW_OP_breg27";
13711 case DW_OP_breg28:
13712 return "DW_OP_breg28";
13713 case DW_OP_breg29:
13714 return "DW_OP_breg29";
13715 case DW_OP_breg30:
13716 return "DW_OP_breg30";
13717 case DW_OP_breg31:
13718 return "DW_OP_breg31";
13719 case DW_OP_regx:
13720 return "DW_OP_regx";
13721 case DW_OP_fbreg:
13722 return "DW_OP_fbreg";
13723 case DW_OP_bregx:
13724 return "DW_OP_bregx";
13725 case DW_OP_piece:
13726 return "DW_OP_piece";
13727 case DW_OP_deref_size:
13728 return "DW_OP_deref_size";
13729 case DW_OP_xderef_size:
13730 return "DW_OP_xderef_size";
13731 case DW_OP_nop:
13732 return "DW_OP_nop";
b7619582 13733 /* DWARF 3 extensions. */
ed348acc
EZ
13734 case DW_OP_push_object_address:
13735 return "DW_OP_push_object_address";
13736 case DW_OP_call2:
13737 return "DW_OP_call2";
13738 case DW_OP_call4:
13739 return "DW_OP_call4";
13740 case DW_OP_call_ref:
13741 return "DW_OP_call_ref";
b7619582
GF
13742 case DW_OP_form_tls_address:
13743 return "DW_OP_form_tls_address";
13744 case DW_OP_call_frame_cfa:
13745 return "DW_OP_call_frame_cfa";
13746 case DW_OP_bit_piece:
13747 return "DW_OP_bit_piece";
9eae7c52
TT
13748 /* DWARF 4 extensions. */
13749 case DW_OP_implicit_value:
13750 return "DW_OP_implicit_value";
13751 case DW_OP_stack_value:
13752 return "DW_OP_stack_value";
13753 /* GNU extensions. */
ed348acc
EZ
13754 case DW_OP_GNU_push_tls_address:
13755 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13756 case DW_OP_GNU_uninit:
13757 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13758 case DW_OP_GNU_implicit_pointer:
13759 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13760 case DW_OP_GNU_entry_value:
13761 return "DW_OP_GNU_entry_value";
13762 case DW_OP_GNU_const_type:
13763 return "DW_OP_GNU_const_type";
13764 case DW_OP_GNU_regval_type:
13765 return "DW_OP_GNU_regval_type";
13766 case DW_OP_GNU_deref_type:
13767 return "DW_OP_GNU_deref_type";
13768 case DW_OP_GNU_convert:
13769 return "DW_OP_GNU_convert";
13770 case DW_OP_GNU_reinterpret:
13771 return "DW_OP_GNU_reinterpret";
c906108c 13772 default:
b1bfef65 13773 return NULL;
c906108c
SS
13774 }
13775}
13776
13777static char *
fba45db2 13778dwarf_bool_name (unsigned mybool)
c906108c
SS
13779{
13780 if (mybool)
13781 return "TRUE";
13782 else
13783 return "FALSE";
13784}
13785
13786/* Convert a DWARF type code into its string name. */
13787
13788static char *
aa1ee363 13789dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13790{
13791 switch (enc)
13792 {
b7619582
GF
13793 case DW_ATE_void:
13794 return "DW_ATE_void";
c906108c
SS
13795 case DW_ATE_address:
13796 return "DW_ATE_address";
13797 case DW_ATE_boolean:
13798 return "DW_ATE_boolean";
13799 case DW_ATE_complex_float:
13800 return "DW_ATE_complex_float";
13801 case DW_ATE_float:
13802 return "DW_ATE_float";
13803 case DW_ATE_signed:
13804 return "DW_ATE_signed";
13805 case DW_ATE_signed_char:
13806 return "DW_ATE_signed_char";
13807 case DW_ATE_unsigned:
13808 return "DW_ATE_unsigned";
13809 case DW_ATE_unsigned_char:
13810 return "DW_ATE_unsigned_char";
b7619582 13811 /* DWARF 3. */
d9fa45fe
DC
13812 case DW_ATE_imaginary_float:
13813 return "DW_ATE_imaginary_float";
b7619582
GF
13814 case DW_ATE_packed_decimal:
13815 return "DW_ATE_packed_decimal";
13816 case DW_ATE_numeric_string:
13817 return "DW_ATE_numeric_string";
13818 case DW_ATE_edited:
13819 return "DW_ATE_edited";
13820 case DW_ATE_signed_fixed:
13821 return "DW_ATE_signed_fixed";
13822 case DW_ATE_unsigned_fixed:
13823 return "DW_ATE_unsigned_fixed";
13824 case DW_ATE_decimal_float:
13825 return "DW_ATE_decimal_float";
75079b2b
TT
13826 /* DWARF 4. */
13827 case DW_ATE_UTF:
13828 return "DW_ATE_UTF";
b7619582
GF
13829 /* HP extensions. */
13830 case DW_ATE_HP_float80:
13831 return "DW_ATE_HP_float80";
13832 case DW_ATE_HP_complex_float80:
13833 return "DW_ATE_HP_complex_float80";
13834 case DW_ATE_HP_float128:
13835 return "DW_ATE_HP_float128";
13836 case DW_ATE_HP_complex_float128:
13837 return "DW_ATE_HP_complex_float128";
13838 case DW_ATE_HP_floathpintel:
13839 return "DW_ATE_HP_floathpintel";
13840 case DW_ATE_HP_imaginary_float80:
13841 return "DW_ATE_HP_imaginary_float80";
13842 case DW_ATE_HP_imaginary_float128:
13843 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13844 default:
13845 return "DW_ATE_<unknown>";
13846 }
13847}
13848
0963b4bd 13849/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13850
13851#if 0
13852static char *
aa1ee363 13853dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13854{
13855 switch (cfi_opc)
13856 {
13857 case DW_CFA_advance_loc:
13858 return "DW_CFA_advance_loc";
13859 case DW_CFA_offset:
13860 return "DW_CFA_offset";
13861 case DW_CFA_restore:
13862 return "DW_CFA_restore";
13863 case DW_CFA_nop:
13864 return "DW_CFA_nop";
13865 case DW_CFA_set_loc:
13866 return "DW_CFA_set_loc";
13867 case DW_CFA_advance_loc1:
13868 return "DW_CFA_advance_loc1";
13869 case DW_CFA_advance_loc2:
13870 return "DW_CFA_advance_loc2";
13871 case DW_CFA_advance_loc4:
13872 return "DW_CFA_advance_loc4";
13873 case DW_CFA_offset_extended:
13874 return "DW_CFA_offset_extended";
13875 case DW_CFA_restore_extended:
13876 return "DW_CFA_restore_extended";
13877 case DW_CFA_undefined:
13878 return "DW_CFA_undefined";
13879 case DW_CFA_same_value:
13880 return "DW_CFA_same_value";
13881 case DW_CFA_register:
13882 return "DW_CFA_register";
13883 case DW_CFA_remember_state:
13884 return "DW_CFA_remember_state";
13885 case DW_CFA_restore_state:
13886 return "DW_CFA_restore_state";
13887 case DW_CFA_def_cfa:
13888 return "DW_CFA_def_cfa";
13889 case DW_CFA_def_cfa_register:
13890 return "DW_CFA_def_cfa_register";
13891 case DW_CFA_def_cfa_offset:
13892 return "DW_CFA_def_cfa_offset";
b7619582 13893 /* DWARF 3. */
985cb1a3
JM
13894 case DW_CFA_def_cfa_expression:
13895 return "DW_CFA_def_cfa_expression";
13896 case DW_CFA_expression:
13897 return "DW_CFA_expression";
13898 case DW_CFA_offset_extended_sf:
13899 return "DW_CFA_offset_extended_sf";
13900 case DW_CFA_def_cfa_sf:
13901 return "DW_CFA_def_cfa_sf";
13902 case DW_CFA_def_cfa_offset_sf:
13903 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13904 case DW_CFA_val_offset:
13905 return "DW_CFA_val_offset";
13906 case DW_CFA_val_offset_sf:
13907 return "DW_CFA_val_offset_sf";
13908 case DW_CFA_val_expression:
13909 return "DW_CFA_val_expression";
13910 /* SGI/MIPS specific. */
c906108c
SS
13911 case DW_CFA_MIPS_advance_loc8:
13912 return "DW_CFA_MIPS_advance_loc8";
b7619582 13913 /* GNU extensions. */
985cb1a3
JM
13914 case DW_CFA_GNU_window_save:
13915 return "DW_CFA_GNU_window_save";
13916 case DW_CFA_GNU_args_size:
13917 return "DW_CFA_GNU_args_size";
13918 case DW_CFA_GNU_negative_offset_extended:
13919 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13920 default:
13921 return "DW_CFA_<unknown>";
13922 }
13923}
13924#endif
13925
f9aca02d 13926static void
d97bc12b 13927dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13928{
13929 unsigned int i;
13930
d97bc12b
DE
13931 print_spaces (indent, f);
13932 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13933 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13934
13935 if (die->parent != NULL)
13936 {
13937 print_spaces (indent, f);
13938 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13939 die->parent->offset);
13940 }
13941
13942 print_spaces (indent, f);
13943 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13944 dwarf_bool_name (die->child != NULL));
c906108c 13945
d97bc12b
DE
13946 print_spaces (indent, f);
13947 fprintf_unfiltered (f, " attributes:\n");
13948
c906108c
SS
13949 for (i = 0; i < die->num_attrs; ++i)
13950 {
d97bc12b
DE
13951 print_spaces (indent, f);
13952 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13953 dwarf_attr_name (die->attrs[i].name),
13954 dwarf_form_name (die->attrs[i].form));
d97bc12b 13955
c906108c
SS
13956 switch (die->attrs[i].form)
13957 {
13958 case DW_FORM_ref_addr:
13959 case DW_FORM_addr:
d97bc12b 13960 fprintf_unfiltered (f, "address: ");
5af949e3 13961 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13962 break;
13963 case DW_FORM_block2:
13964 case DW_FORM_block4:
13965 case DW_FORM_block:
13966 case DW_FORM_block1:
3e43a32a
MS
13967 fprintf_unfiltered (f, "block: size %d",
13968 DW_BLOCK (&die->attrs[i])->size);
c906108c 13969 break;
2dc7f7b3
TT
13970 case DW_FORM_exprloc:
13971 fprintf_unfiltered (f, "expression: size %u",
13972 DW_BLOCK (&die->attrs[i])->size);
13973 break;
10b3939b
DJ
13974 case DW_FORM_ref1:
13975 case DW_FORM_ref2:
13976 case DW_FORM_ref4:
d97bc12b 13977 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13978 (long) (DW_ADDR (&die->attrs[i])));
13979 break;
c906108c
SS
13980 case DW_FORM_data1:
13981 case DW_FORM_data2:
13982 case DW_FORM_data4:
ce5d95e1 13983 case DW_FORM_data8:
c906108c
SS
13984 case DW_FORM_udata:
13985 case DW_FORM_sdata:
43bbcdc2
PH
13986 fprintf_unfiltered (f, "constant: %s",
13987 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13988 break;
2dc7f7b3
TT
13989 case DW_FORM_sec_offset:
13990 fprintf_unfiltered (f, "section offset: %s",
13991 pulongest (DW_UNSND (&die->attrs[i])));
13992 break;
55f1336d 13993 case DW_FORM_ref_sig8:
348e048f
DE
13994 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13995 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13996 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13997 else
13998 fprintf_unfiltered (f, "signatured type, offset: unknown");
13999 break;
c906108c 14000 case DW_FORM_string:
4bdf3d34 14001 case DW_FORM_strp:
8285870a 14002 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14003 DW_STRING (&die->attrs[i])
8285870a
JK
14004 ? DW_STRING (&die->attrs[i]) : "",
14005 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14006 break;
14007 case DW_FORM_flag:
14008 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14009 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14010 else
d97bc12b 14011 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14012 break;
2dc7f7b3
TT
14013 case DW_FORM_flag_present:
14014 fprintf_unfiltered (f, "flag: TRUE");
14015 break;
a8329558 14016 case DW_FORM_indirect:
0963b4bd
MS
14017 /* The reader will have reduced the indirect form to
14018 the "base form" so this form should not occur. */
3e43a32a
MS
14019 fprintf_unfiltered (f,
14020 "unexpected attribute form: DW_FORM_indirect");
a8329558 14021 break;
c906108c 14022 default:
d97bc12b 14023 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14024 die->attrs[i].form);
d97bc12b 14025 break;
c906108c 14026 }
d97bc12b 14027 fprintf_unfiltered (f, "\n");
c906108c
SS
14028 }
14029}
14030
f9aca02d 14031static void
d97bc12b 14032dump_die_for_error (struct die_info *die)
c906108c 14033{
d97bc12b
DE
14034 dump_die_shallow (gdb_stderr, 0, die);
14035}
14036
14037static void
14038dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14039{
14040 int indent = level * 4;
14041
14042 gdb_assert (die != NULL);
14043
14044 if (level >= max_level)
14045 return;
14046
14047 dump_die_shallow (f, indent, die);
14048
14049 if (die->child != NULL)
c906108c 14050 {
d97bc12b
DE
14051 print_spaces (indent, f);
14052 fprintf_unfiltered (f, " Children:");
14053 if (level + 1 < max_level)
14054 {
14055 fprintf_unfiltered (f, "\n");
14056 dump_die_1 (f, level + 1, max_level, die->child);
14057 }
14058 else
14059 {
3e43a32a
MS
14060 fprintf_unfiltered (f,
14061 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14062 }
14063 }
14064
14065 if (die->sibling != NULL && level > 0)
14066 {
14067 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14068 }
14069}
14070
d97bc12b
DE
14071/* This is called from the pdie macro in gdbinit.in.
14072 It's not static so gcc will keep a copy callable from gdb. */
14073
14074void
14075dump_die (struct die_info *die, int max_level)
14076{
14077 dump_die_1 (gdb_stdlog, 0, max_level, die);
14078}
14079
f9aca02d 14080static void
51545339 14081store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14082{
51545339 14083 void **slot;
c906108c 14084
51545339
DJ
14085 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14086
14087 *slot = die;
c906108c
SS
14088}
14089
93311388
DE
14090static int
14091is_ref_attr (struct attribute *attr)
c906108c 14092{
c906108c
SS
14093 switch (attr->form)
14094 {
14095 case DW_FORM_ref_addr:
c906108c
SS
14096 case DW_FORM_ref1:
14097 case DW_FORM_ref2:
14098 case DW_FORM_ref4:
613e1657 14099 case DW_FORM_ref8:
c906108c 14100 case DW_FORM_ref_udata:
93311388 14101 return 1;
c906108c 14102 default:
93311388 14103 return 0;
c906108c 14104 }
93311388
DE
14105}
14106
14107static unsigned int
14108dwarf2_get_ref_die_offset (struct attribute *attr)
14109{
14110 if (is_ref_attr (attr))
14111 return DW_ADDR (attr);
14112
14113 complaint (&symfile_complaints,
14114 _("unsupported die ref attribute form: '%s'"),
14115 dwarf_form_name (attr->form));
14116 return 0;
c906108c
SS
14117}
14118
43bbcdc2
PH
14119/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14120 * the value held by the attribute is not constant. */
a02abb62 14121
43bbcdc2 14122static LONGEST
a02abb62
JB
14123dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14124{
14125 if (attr->form == DW_FORM_sdata)
14126 return DW_SND (attr);
14127 else if (attr->form == DW_FORM_udata
14128 || attr->form == DW_FORM_data1
14129 || attr->form == DW_FORM_data2
14130 || attr->form == DW_FORM_data4
14131 || attr->form == DW_FORM_data8)
14132 return DW_UNSND (attr);
14133 else
14134 {
3e43a32a
MS
14135 complaint (&symfile_complaints,
14136 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14137 dwarf_form_name (attr->form));
14138 return default_value;
14139 }
14140}
14141
03dd20cc 14142/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14143 unit and add it to our queue.
14144 The result is non-zero if PER_CU was queued, otherwise the result is zero
14145 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14146
348e048f 14147static int
03dd20cc
DJ
14148maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14149 struct dwarf2_per_cu_data *per_cu)
14150{
98bfdba5
PA
14151 /* We may arrive here during partial symbol reading, if we need full
14152 DIEs to process an unusual case (e.g. template arguments). Do
14153 not queue PER_CU, just tell our caller to load its DIEs. */
14154 if (dwarf2_per_objfile->reading_partial_symbols)
14155 {
14156 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14157 return 1;
14158 return 0;
14159 }
14160
03dd20cc
DJ
14161 /* Mark the dependence relation so that we don't flush PER_CU
14162 too early. */
14163 dwarf2_add_dependence (this_cu, per_cu);
14164
14165 /* If it's already on the queue, we have nothing to do. */
14166 if (per_cu->queued)
348e048f 14167 return 0;
03dd20cc
DJ
14168
14169 /* If the compilation unit is already loaded, just mark it as
14170 used. */
14171 if (per_cu->cu != NULL)
14172 {
14173 per_cu->cu->last_used = 0;
348e048f 14174 return 0;
03dd20cc
DJ
14175 }
14176
14177 /* Add it to the queue. */
14178 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
14179
14180 return 1;
14181}
14182
14183/* Follow reference or signature attribute ATTR of SRC_DIE.
14184 On entry *REF_CU is the CU of SRC_DIE.
14185 On exit *REF_CU is the CU of the result. */
14186
14187static struct die_info *
14188follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14189 struct dwarf2_cu **ref_cu)
14190{
14191 struct die_info *die;
14192
14193 if (is_ref_attr (attr))
14194 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14195 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14196 die = follow_die_sig (src_die, attr, ref_cu);
14197 else
14198 {
14199 dump_die_for_error (src_die);
14200 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14201 (*ref_cu)->objfile->name);
14202 }
14203
14204 return die;
03dd20cc
DJ
14205}
14206
5c631832 14207/* Follow reference OFFSET.
673bfd45
DE
14208 On entry *REF_CU is the CU of the source die referencing OFFSET.
14209 On exit *REF_CU is the CU of the result.
14210 Returns NULL if OFFSET is invalid. */
f504f079 14211
f9aca02d 14212static struct die_info *
5c631832 14213follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 14214{
10b3939b 14215 struct die_info temp_die;
f2f0e013 14216 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14217
348e048f
DE
14218 gdb_assert (cu->per_cu != NULL);
14219
98bfdba5
PA
14220 target_cu = cu;
14221
8b70b953 14222 if (cu->per_cu->debug_type_section)
348e048f
DE
14223 {
14224 /* .debug_types CUs cannot reference anything outside their CU.
14225 If they need to, they have to reference a signatured type via
55f1336d 14226 DW_FORM_ref_sig8. */
348e048f 14227 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14228 return NULL;
348e048f
DE
14229 }
14230 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14231 {
14232 struct dwarf2_per_cu_data *per_cu;
9a619af0 14233
45452591 14234 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14235
14236 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
14237 if (maybe_queue_comp_unit (cu, per_cu))
14238 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 14239
10b3939b
DJ
14240 target_cu = per_cu->cu;
14241 }
98bfdba5
PA
14242 else if (cu->dies == NULL)
14243 {
14244 /* We're loading full DIEs during partial symbol reading. */
14245 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14246 load_full_comp_unit (cu->per_cu, cu->objfile);
14247 }
c906108c 14248
f2f0e013 14249 *ref_cu = target_cu;
51545339 14250 temp_die.offset = offset;
5c631832
JK
14251 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14252}
10b3939b 14253
5c631832
JK
14254/* Follow reference attribute ATTR of SRC_DIE.
14255 On entry *REF_CU is the CU of SRC_DIE.
14256 On exit *REF_CU is the CU of the result. */
14257
14258static struct die_info *
14259follow_die_ref (struct die_info *src_die, struct attribute *attr,
14260 struct dwarf2_cu **ref_cu)
14261{
14262 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14263 struct dwarf2_cu *cu = *ref_cu;
14264 struct die_info *die;
14265
14266 die = follow_die_offset (offset, ref_cu);
14267 if (!die)
14268 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14269 "at 0x%x [in module %s]"),
14270 offset, src_die->offset, cu->objfile->name);
348e048f 14271
5c631832
JK
14272 return die;
14273}
14274
d83e736b
JK
14275/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14276 Returned value is intended for DW_OP_call*. Returned
14277 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14278
14279struct dwarf2_locexpr_baton
14280dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
14281 struct dwarf2_per_cu_data *per_cu,
14282 CORE_ADDR (*get_frame_pc) (void *baton),
14283 void *baton)
5c631832 14284{
918dd910 14285 struct dwarf2_cu *cu;
5c631832
JK
14286 struct die_info *die;
14287 struct attribute *attr;
14288 struct dwarf2_locexpr_baton retval;
14289
8cf6f0b1
TT
14290 dw2_setup (per_cu->objfile);
14291
918dd910
JK
14292 if (per_cu->cu == NULL)
14293 load_cu (per_cu);
14294 cu = per_cu->cu;
14295
5c631832
JK
14296 die = follow_die_offset (offset, &cu);
14297 if (!die)
14298 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14299 offset, per_cu->cu->objfile->name);
14300
14301 attr = dwarf2_attr (die, DW_AT_location, cu);
14302 if (!attr)
14303 {
e103e986
JK
14304 /* DWARF: "If there is no such attribute, then there is no effect.".
14305 DATA is ignored if SIZE is 0. */
5c631832 14306
e103e986 14307 retval.data = NULL;
5c631832
JK
14308 retval.size = 0;
14309 }
8cf6f0b1
TT
14310 else if (attr_form_is_section_offset (attr))
14311 {
14312 struct dwarf2_loclist_baton loclist_baton;
14313 CORE_ADDR pc = (*get_frame_pc) (baton);
14314 size_t size;
14315
14316 fill_in_loclist_baton (cu, &loclist_baton, attr);
14317
14318 retval.data = dwarf2_find_location_expression (&loclist_baton,
14319 &size, pc);
14320 retval.size = size;
14321 }
5c631832
JK
14322 else
14323 {
14324 if (!attr_form_is_block (attr))
14325 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14326 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14327 offset, per_cu->cu->objfile->name);
14328
14329 retval.data = DW_BLOCK (attr)->data;
14330 retval.size = DW_BLOCK (attr)->size;
14331 }
14332 retval.per_cu = cu->per_cu;
918dd910 14333
918dd910
JK
14334 age_cached_comp_units ();
14335
5c631832 14336 return retval;
348e048f
DE
14337}
14338
8a9b8146
TT
14339/* Return the type of the DIE at DIE_OFFSET in the CU named by
14340 PER_CU. */
14341
14342struct type *
14343dwarf2_get_die_type (unsigned int die_offset,
14344 struct dwarf2_per_cu_data *per_cu)
14345{
8a9b8146 14346 dw2_setup (per_cu->objfile);
9ff3b74f 14347 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
14348}
14349
348e048f
DE
14350/* Follow the signature attribute ATTR in SRC_DIE.
14351 On entry *REF_CU is the CU of SRC_DIE.
14352 On exit *REF_CU is the CU of the result. */
14353
14354static struct die_info *
14355follow_die_sig (struct die_info *src_die, struct attribute *attr,
14356 struct dwarf2_cu **ref_cu)
14357{
14358 struct objfile *objfile = (*ref_cu)->objfile;
14359 struct die_info temp_die;
14360 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14361 struct dwarf2_cu *sig_cu;
14362 struct die_info *die;
14363
14364 /* sig_type will be NULL if the signatured type is missing from
14365 the debug info. */
14366 if (sig_type == NULL)
14367 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14368 "at 0x%x [in module %s]"),
14369 src_die->offset, objfile->name);
14370
14371 /* If necessary, add it to the queue and load its DIEs. */
14372
14373 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14374 read_signatured_type (objfile, sig_type);
14375
14376 gdb_assert (sig_type->per_cu.cu != NULL);
14377
14378 sig_cu = sig_type->per_cu.cu;
14379 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14380 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14381 if (die)
14382 {
14383 *ref_cu = sig_cu;
14384 return die;
14385 }
14386
3e43a32a
MS
14387 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14388 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14389 sig_type->type_offset, src_die->offset, objfile->name);
14390}
14391
14392/* Given an offset of a signatured type, return its signatured_type. */
14393
14394static struct signatured_type *
8b70b953
TT
14395lookup_signatured_type_at_offset (struct objfile *objfile,
14396 struct dwarf2_section_info *section,
14397 unsigned int offset)
348e048f 14398{
8b70b953 14399 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14400 unsigned int length, initial_length_size;
14401 unsigned int sig_offset;
14402 struct signatured_type find_entry, *type_sig;
14403
14404 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14405 sig_offset = (initial_length_size
14406 + 2 /*version*/
14407 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14408 + 1 /*address_size*/);
14409 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14410 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14411
14412 /* This is only used to lookup previously recorded types.
14413 If we didn't find it, it's our bug. */
14414 gdb_assert (type_sig != NULL);
b3c8eb43 14415 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14416
14417 return type_sig;
14418}
14419
14420/* Read in signatured type at OFFSET and build its CU and die(s). */
14421
14422static void
14423read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 14424 struct dwarf2_section_info *sect,
348e048f
DE
14425 unsigned int offset)
14426{
14427 struct signatured_type *type_sig;
14428
8b70b953 14429 dwarf2_read_section (objfile, sect);
be391dca 14430
348e048f
DE
14431 /* We have the section offset, but we need the signature to do the
14432 hash table lookup. */
8b70b953 14433 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14434
14435 gdb_assert (type_sig->per_cu.cu == NULL);
14436
14437 read_signatured_type (objfile, type_sig);
14438
14439 gdb_assert (type_sig->per_cu.cu != NULL);
14440}
14441
14442/* Read in a signatured type and build its CU and DIEs. */
14443
14444static void
14445read_signatured_type (struct objfile *objfile,
14446 struct signatured_type *type_sig)
14447{
1fd400ff 14448 gdb_byte *types_ptr;
348e048f
DE
14449 struct die_reader_specs reader_specs;
14450 struct dwarf2_cu *cu;
14451 ULONGEST signature;
14452 struct cleanup *back_to, *free_cu_cleanup;
8b70b953 14453 struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
348e048f 14454
8b70b953
TT
14455 dwarf2_read_section (objfile, section);
14456 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14457
348e048f
DE
14458 gdb_assert (type_sig->per_cu.cu == NULL);
14459
9816fde3
JK
14460 cu = xmalloc (sizeof (*cu));
14461 init_one_comp_unit (cu, objfile);
14462
348e048f
DE
14463 type_sig->per_cu.cu = cu;
14464 cu->per_cu = &type_sig->per_cu;
14465
14466 /* If an error occurs while loading, release our storage. */
14467 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14468
8b70b953 14469 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
348e048f
DE
14470 types_ptr, objfile->obfd);
14471 gdb_assert (signature == type_sig->signature);
14472
14473 cu->die_hash
14474 = htab_create_alloc_ex (cu->header.length / 12,
14475 die_hash,
14476 die_eq,
14477 NULL,
14478 &cu->comp_unit_obstack,
14479 hashtab_obstack_allocate,
14480 dummy_obstack_deallocate);
14481
14482 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14483 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14484
14485 init_cu_die_reader (&reader_specs, cu);
14486
14487 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14488 NULL /*parent*/);
14489
14490 /* We try not to read any attributes in this function, because not
14491 all objfiles needed for references have been loaded yet, and symbol
14492 table processing isn't initialized. But we have to set the CU language,
14493 or we won't be able to build types correctly. */
9816fde3 14494 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14495
14496 do_cleanups (back_to);
14497
14498 /* We've successfully allocated this compilation unit. Let our caller
14499 clean it up when finished with it. */
14500 discard_cleanups (free_cu_cleanup);
14501
14502 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14503 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14504}
14505
c906108c
SS
14506/* Decode simple location descriptions.
14507 Given a pointer to a dwarf block that defines a location, compute
14508 the location and return the value.
14509
4cecd739
DJ
14510 NOTE drow/2003-11-18: This function is called in two situations
14511 now: for the address of static or global variables (partial symbols
14512 only) and for offsets into structures which are expected to be
14513 (more or less) constant. The partial symbol case should go away,
14514 and only the constant case should remain. That will let this
14515 function complain more accurately. A few special modes are allowed
14516 without complaint for global variables (for instance, global
14517 register values and thread-local values).
c906108c
SS
14518
14519 A location description containing no operations indicates that the
4cecd739 14520 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14521 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14522 callers will only want a very basic result and this can become a
21ae7a4d
JK
14523 complaint.
14524
14525 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14526
14527static CORE_ADDR
e7c27a73 14528decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14529{
e7c27a73 14530 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14531 int i;
14532 int size = blk->size;
14533 gdb_byte *data = blk->data;
14534 CORE_ADDR stack[64];
14535 int stacki;
14536 unsigned int bytes_read, unsnd;
14537 gdb_byte op;
c906108c 14538
21ae7a4d
JK
14539 i = 0;
14540 stacki = 0;
14541 stack[stacki] = 0;
14542 stack[++stacki] = 0;
14543
14544 while (i < size)
14545 {
14546 op = data[i++];
14547 switch (op)
14548 {
14549 case DW_OP_lit0:
14550 case DW_OP_lit1:
14551 case DW_OP_lit2:
14552 case DW_OP_lit3:
14553 case DW_OP_lit4:
14554 case DW_OP_lit5:
14555 case DW_OP_lit6:
14556 case DW_OP_lit7:
14557 case DW_OP_lit8:
14558 case DW_OP_lit9:
14559 case DW_OP_lit10:
14560 case DW_OP_lit11:
14561 case DW_OP_lit12:
14562 case DW_OP_lit13:
14563 case DW_OP_lit14:
14564 case DW_OP_lit15:
14565 case DW_OP_lit16:
14566 case DW_OP_lit17:
14567 case DW_OP_lit18:
14568 case DW_OP_lit19:
14569 case DW_OP_lit20:
14570 case DW_OP_lit21:
14571 case DW_OP_lit22:
14572 case DW_OP_lit23:
14573 case DW_OP_lit24:
14574 case DW_OP_lit25:
14575 case DW_OP_lit26:
14576 case DW_OP_lit27:
14577 case DW_OP_lit28:
14578 case DW_OP_lit29:
14579 case DW_OP_lit30:
14580 case DW_OP_lit31:
14581 stack[++stacki] = op - DW_OP_lit0;
14582 break;
f1bea926 14583
21ae7a4d
JK
14584 case DW_OP_reg0:
14585 case DW_OP_reg1:
14586 case DW_OP_reg2:
14587 case DW_OP_reg3:
14588 case DW_OP_reg4:
14589 case DW_OP_reg5:
14590 case DW_OP_reg6:
14591 case DW_OP_reg7:
14592 case DW_OP_reg8:
14593 case DW_OP_reg9:
14594 case DW_OP_reg10:
14595 case DW_OP_reg11:
14596 case DW_OP_reg12:
14597 case DW_OP_reg13:
14598 case DW_OP_reg14:
14599 case DW_OP_reg15:
14600 case DW_OP_reg16:
14601 case DW_OP_reg17:
14602 case DW_OP_reg18:
14603 case DW_OP_reg19:
14604 case DW_OP_reg20:
14605 case DW_OP_reg21:
14606 case DW_OP_reg22:
14607 case DW_OP_reg23:
14608 case DW_OP_reg24:
14609 case DW_OP_reg25:
14610 case DW_OP_reg26:
14611 case DW_OP_reg27:
14612 case DW_OP_reg28:
14613 case DW_OP_reg29:
14614 case DW_OP_reg30:
14615 case DW_OP_reg31:
14616 stack[++stacki] = op - DW_OP_reg0;
14617 if (i < size)
14618 dwarf2_complex_location_expr_complaint ();
14619 break;
c906108c 14620
21ae7a4d
JK
14621 case DW_OP_regx:
14622 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14623 i += bytes_read;
14624 stack[++stacki] = unsnd;
14625 if (i < size)
14626 dwarf2_complex_location_expr_complaint ();
14627 break;
c906108c 14628
21ae7a4d
JK
14629 case DW_OP_addr:
14630 stack[++stacki] = read_address (objfile->obfd, &data[i],
14631 cu, &bytes_read);
14632 i += bytes_read;
14633 break;
d53d4ac5 14634
21ae7a4d
JK
14635 case DW_OP_const1u:
14636 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14637 i += 1;
14638 break;
14639
14640 case DW_OP_const1s:
14641 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14642 i += 1;
14643 break;
14644
14645 case DW_OP_const2u:
14646 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14647 i += 2;
14648 break;
14649
14650 case DW_OP_const2s:
14651 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14652 i += 2;
14653 break;
d53d4ac5 14654
21ae7a4d
JK
14655 case DW_OP_const4u:
14656 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14657 i += 4;
14658 break;
14659
14660 case DW_OP_const4s:
14661 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14662 i += 4;
14663 break;
14664
14665 case DW_OP_constu:
14666 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14667 &bytes_read);
14668 i += bytes_read;
14669 break;
14670
14671 case DW_OP_consts:
14672 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14673 i += bytes_read;
14674 break;
14675
14676 case DW_OP_dup:
14677 stack[stacki + 1] = stack[stacki];
14678 stacki++;
14679 break;
14680
14681 case DW_OP_plus:
14682 stack[stacki - 1] += stack[stacki];
14683 stacki--;
14684 break;
14685
14686 case DW_OP_plus_uconst:
14687 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14688 &bytes_read);
14689 i += bytes_read;
14690 break;
14691
14692 case DW_OP_minus:
14693 stack[stacki - 1] -= stack[stacki];
14694 stacki--;
14695 break;
14696
14697 case DW_OP_deref:
14698 /* If we're not the last op, then we definitely can't encode
14699 this using GDB's address_class enum. This is valid for partial
14700 global symbols, although the variable's address will be bogus
14701 in the psymtab. */
14702 if (i < size)
14703 dwarf2_complex_location_expr_complaint ();
14704 break;
14705
14706 case DW_OP_GNU_push_tls_address:
14707 /* The top of the stack has the offset from the beginning
14708 of the thread control block at which the variable is located. */
14709 /* Nothing should follow this operator, so the top of stack would
14710 be returned. */
14711 /* This is valid for partial global symbols, but the variable's
14712 address will be bogus in the psymtab. */
14713 if (i < size)
14714 dwarf2_complex_location_expr_complaint ();
14715 break;
14716
14717 case DW_OP_GNU_uninit:
14718 break;
14719
14720 default:
14721 {
14722 const char *name = dwarf_stack_op_name (op);
14723
14724 if (name)
14725 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14726 name);
14727 else
14728 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14729 op);
14730 }
14731
14732 return (stack[stacki]);
d53d4ac5 14733 }
3c6e0cb3 14734
21ae7a4d
JK
14735 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14736 outside of the allocated space. Also enforce minimum>0. */
14737 if (stacki >= ARRAY_SIZE (stack) - 1)
14738 {
14739 complaint (&symfile_complaints,
14740 _("location description stack overflow"));
14741 return 0;
14742 }
14743
14744 if (stacki <= 0)
14745 {
14746 complaint (&symfile_complaints,
14747 _("location description stack underflow"));
14748 return 0;
14749 }
14750 }
14751 return (stack[stacki]);
c906108c
SS
14752}
14753
14754/* memory allocation interface */
14755
c906108c 14756static struct dwarf_block *
7b5a2f43 14757dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14758{
14759 struct dwarf_block *blk;
14760
14761 blk = (struct dwarf_block *)
7b5a2f43 14762 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14763 return (blk);
14764}
14765
14766static struct abbrev_info *
f3dd6933 14767dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14768{
14769 struct abbrev_info *abbrev;
14770
f3dd6933
DJ
14771 abbrev = (struct abbrev_info *)
14772 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14773 memset (abbrev, 0, sizeof (struct abbrev_info));
14774 return (abbrev);
14775}
14776
14777static struct die_info *
b60c80d6 14778dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14779{
14780 struct die_info *die;
b60c80d6
DJ
14781 size_t size = sizeof (struct die_info);
14782
14783 if (num_attrs > 1)
14784 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14785
b60c80d6 14786 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14787 memset (die, 0, sizeof (struct die_info));
14788 return (die);
14789}
2e276125
JB
14790
14791\f
14792/* Macro support. */
14793
2e276125
JB
14794/* Return the full name of file number I in *LH's file name table.
14795 Use COMP_DIR as the name of the current directory of the
14796 compilation. The result is allocated using xmalloc; the caller is
14797 responsible for freeing it. */
14798static char *
14799file_full_name (int file, struct line_header *lh, const char *comp_dir)
14800{
6a83a1e6
EZ
14801 /* Is the file number a valid index into the line header's file name
14802 table? Remember that file numbers start with one, not zero. */
14803 if (1 <= file && file <= lh->num_file_names)
14804 {
14805 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14806
6a83a1e6
EZ
14807 if (IS_ABSOLUTE_PATH (fe->name))
14808 return xstrdup (fe->name);
14809 else
14810 {
14811 const char *dir;
14812 int dir_len;
14813 char *full_name;
14814
14815 if (fe->dir_index)
14816 dir = lh->include_dirs[fe->dir_index - 1];
14817 else
14818 dir = comp_dir;
14819
14820 if (dir)
14821 {
14822 dir_len = strlen (dir);
14823 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14824 strcpy (full_name, dir);
14825 full_name[dir_len] = '/';
14826 strcpy (full_name + dir_len + 1, fe->name);
14827 return full_name;
14828 }
14829 else
14830 return xstrdup (fe->name);
14831 }
14832 }
2e276125
JB
14833 else
14834 {
6a83a1e6
EZ
14835 /* The compiler produced a bogus file number. We can at least
14836 record the macro definitions made in the file, even if we
14837 won't be able to find the file by name. */
14838 char fake_name[80];
9a619af0 14839
6a83a1e6 14840 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14841
6e70227d 14842 complaint (&symfile_complaints,
6a83a1e6
EZ
14843 _("bad file number in macro information (%d)"),
14844 file);
2e276125 14845
6a83a1e6 14846 return xstrdup (fake_name);
2e276125
JB
14847 }
14848}
14849
14850
14851static struct macro_source_file *
14852macro_start_file (int file, int line,
14853 struct macro_source_file *current_file,
14854 const char *comp_dir,
14855 struct line_header *lh, struct objfile *objfile)
14856{
14857 /* The full name of this source file. */
14858 char *full_name = file_full_name (file, lh, comp_dir);
14859
14860 /* We don't create a macro table for this compilation unit
14861 at all until we actually get a filename. */
14862 if (! pending_macros)
4a146b47 14863 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14864 objfile->macro_cache);
2e276125
JB
14865
14866 if (! current_file)
14867 /* If we have no current file, then this must be the start_file
14868 directive for the compilation unit's main source file. */
14869 current_file = macro_set_main (pending_macros, full_name);
14870 else
14871 current_file = macro_include (current_file, line, full_name);
14872
14873 xfree (full_name);
6e70227d 14874
2e276125
JB
14875 return current_file;
14876}
14877
14878
14879/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14880 followed by a null byte. */
14881static char *
14882copy_string (const char *buf, int len)
14883{
14884 char *s = xmalloc (len + 1);
9a619af0 14885
2e276125
JB
14886 memcpy (s, buf, len);
14887 s[len] = '\0';
2e276125
JB
14888 return s;
14889}
14890
14891
14892static const char *
14893consume_improper_spaces (const char *p, const char *body)
14894{
14895 if (*p == ' ')
14896 {
4d3c2250 14897 complaint (&symfile_complaints,
3e43a32a
MS
14898 _("macro definition contains spaces "
14899 "in formal argument list:\n`%s'"),
4d3c2250 14900 body);
2e276125
JB
14901
14902 while (*p == ' ')
14903 p++;
14904 }
14905
14906 return p;
14907}
14908
14909
14910static void
14911parse_macro_definition (struct macro_source_file *file, int line,
14912 const char *body)
14913{
14914 const char *p;
14915
14916 /* The body string takes one of two forms. For object-like macro
14917 definitions, it should be:
14918
14919 <macro name> " " <definition>
14920
14921 For function-like macro definitions, it should be:
14922
14923 <macro name> "() " <definition>
14924 or
14925 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14926
14927 Spaces may appear only where explicitly indicated, and in the
14928 <definition>.
14929
14930 The Dwarf 2 spec says that an object-like macro's name is always
14931 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14932 the space when the macro's definition is the empty string.
2e276125
JB
14933
14934 The Dwarf 2 spec says that there should be no spaces between the
14935 formal arguments in a function-like macro's formal argument list,
14936 but versions of GCC around March 2002 include spaces after the
14937 commas. */
14938
14939
14940 /* Find the extent of the macro name. The macro name is terminated
14941 by either a space or null character (for an object-like macro) or
14942 an opening paren (for a function-like macro). */
14943 for (p = body; *p; p++)
14944 if (*p == ' ' || *p == '(')
14945 break;
14946
14947 if (*p == ' ' || *p == '\0')
14948 {
14949 /* It's an object-like macro. */
14950 int name_len = p - body;
14951 char *name = copy_string (body, name_len);
14952 const char *replacement;
14953
14954 if (*p == ' ')
14955 replacement = body + name_len + 1;
14956 else
14957 {
4d3c2250 14958 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14959 replacement = body + name_len;
14960 }
6e70227d 14961
2e276125
JB
14962 macro_define_object (file, line, name, replacement);
14963
14964 xfree (name);
14965 }
14966 else if (*p == '(')
14967 {
14968 /* It's a function-like macro. */
14969 char *name = copy_string (body, p - body);
14970 int argc = 0;
14971 int argv_size = 1;
14972 char **argv = xmalloc (argv_size * sizeof (*argv));
14973
14974 p++;
14975
14976 p = consume_improper_spaces (p, body);
14977
14978 /* Parse the formal argument list. */
14979 while (*p && *p != ')')
14980 {
14981 /* Find the extent of the current argument name. */
14982 const char *arg_start = p;
14983
14984 while (*p && *p != ',' && *p != ')' && *p != ' ')
14985 p++;
14986
14987 if (! *p || p == arg_start)
4d3c2250 14988 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14989 else
14990 {
14991 /* Make sure argv has room for the new argument. */
14992 if (argc >= argv_size)
14993 {
14994 argv_size *= 2;
14995 argv = xrealloc (argv, argv_size * sizeof (*argv));
14996 }
14997
14998 argv[argc++] = copy_string (arg_start, p - arg_start);
14999 }
15000
15001 p = consume_improper_spaces (p, body);
15002
15003 /* Consume the comma, if present. */
15004 if (*p == ',')
15005 {
15006 p++;
15007
15008 p = consume_improper_spaces (p, body);
15009 }
15010 }
15011
15012 if (*p == ')')
15013 {
15014 p++;
15015
15016 if (*p == ' ')
15017 /* Perfectly formed definition, no complaints. */
15018 macro_define_function (file, line, name,
6e70227d 15019 argc, (const char **) argv,
2e276125
JB
15020 p + 1);
15021 else if (*p == '\0')
15022 {
15023 /* Complain, but do define it. */
4d3c2250 15024 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15025 macro_define_function (file, line, name,
6e70227d 15026 argc, (const char **) argv,
2e276125
JB
15027 p);
15028 }
15029 else
15030 /* Just complain. */
4d3c2250 15031 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15032 }
15033 else
15034 /* Just complain. */
4d3c2250 15035 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15036
15037 xfree (name);
15038 {
15039 int i;
15040
15041 for (i = 0; i < argc; i++)
15042 xfree (argv[i]);
15043 }
15044 xfree (argv);
15045 }
15046 else
4d3c2250 15047 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15048}
15049
cf2c3c16
TT
15050/* Skip some bytes from BYTES according to the form given in FORM.
15051 Returns the new pointer. */
2e276125 15052
cf2c3c16
TT
15053static gdb_byte *
15054skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15055 enum dwarf_form form,
15056 unsigned int offset_size,
15057 struct dwarf2_section_info *section)
2e276125 15058{
cf2c3c16 15059 unsigned int bytes_read;
2e276125 15060
cf2c3c16 15061 switch (form)
2e276125 15062 {
cf2c3c16
TT
15063 case DW_FORM_data1:
15064 case DW_FORM_flag:
15065 ++bytes;
15066 break;
15067
15068 case DW_FORM_data2:
15069 bytes += 2;
15070 break;
15071
15072 case DW_FORM_data4:
15073 bytes += 4;
15074 break;
15075
15076 case DW_FORM_data8:
15077 bytes += 8;
15078 break;
15079
15080 case DW_FORM_string:
15081 read_direct_string (abfd, bytes, &bytes_read);
15082 bytes += bytes_read;
15083 break;
15084
15085 case DW_FORM_sec_offset:
15086 case DW_FORM_strp:
15087 bytes += offset_size;
15088 break;
15089
15090 case DW_FORM_block:
15091 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15092 bytes += bytes_read;
15093 break;
15094
15095 case DW_FORM_block1:
15096 bytes += 1 + read_1_byte (abfd, bytes);
15097 break;
15098 case DW_FORM_block2:
15099 bytes += 2 + read_2_bytes (abfd, bytes);
15100 break;
15101 case DW_FORM_block4:
15102 bytes += 4 + read_4_bytes (abfd, bytes);
15103 break;
15104
15105 case DW_FORM_sdata:
15106 case DW_FORM_udata:
15107 bytes = skip_leb128 (abfd, bytes);
15108 break;
15109
15110 default:
15111 {
15112 complain:
15113 complaint (&symfile_complaints,
15114 _("invalid form 0x%x in `%s'"),
15115 form,
15116 section->asection->name);
15117 return NULL;
15118 }
2e276125
JB
15119 }
15120
cf2c3c16
TT
15121 return bytes;
15122}
757a13d0 15123
cf2c3c16
TT
15124/* A helper for dwarf_decode_macros that handles skipping an unknown
15125 opcode. Returns an updated pointer to the macro data buffer; or,
15126 on error, issues a complaint and returns NULL. */
757a13d0 15127
cf2c3c16
TT
15128static gdb_byte *
15129skip_unknown_opcode (unsigned int opcode,
15130 gdb_byte **opcode_definitions,
15131 gdb_byte *mac_ptr,
15132 bfd *abfd,
15133 unsigned int offset_size,
15134 struct dwarf2_section_info *section)
15135{
15136 unsigned int bytes_read, i;
15137 unsigned long arg;
15138 gdb_byte *defn;
2e276125 15139
cf2c3c16 15140 if (opcode_definitions[opcode] == NULL)
2e276125 15141 {
cf2c3c16
TT
15142 complaint (&symfile_complaints,
15143 _("unrecognized DW_MACFINO opcode 0x%x"),
15144 opcode);
15145 return NULL;
15146 }
2e276125 15147
cf2c3c16
TT
15148 defn = opcode_definitions[opcode];
15149 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15150 defn += bytes_read;
2e276125 15151
cf2c3c16
TT
15152 for (i = 0; i < arg; ++i)
15153 {
15154 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15155 if (mac_ptr == NULL)
15156 {
15157 /* skip_form_bytes already issued the complaint. */
15158 return NULL;
15159 }
15160 }
757a13d0 15161
cf2c3c16
TT
15162 return mac_ptr;
15163}
757a13d0 15164
cf2c3c16
TT
15165/* A helper function which parses the header of a macro section.
15166 If the macro section is the extended (for now called "GNU") type,
15167 then this updates *OFFSET_SIZE. Returns a pointer to just after
15168 the header, or issues a complaint and returns NULL on error. */
757a13d0 15169
cf2c3c16
TT
15170static gdb_byte *
15171dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15172 bfd *abfd,
15173 gdb_byte *mac_ptr,
15174 unsigned int *offset_size,
15175 int section_is_gnu)
15176{
15177 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15178
cf2c3c16
TT
15179 if (section_is_gnu)
15180 {
15181 unsigned int version, flags;
757a13d0 15182
cf2c3c16
TT
15183 version = read_2_bytes (abfd, mac_ptr);
15184 if (version != 4)
15185 {
15186 complaint (&symfile_complaints,
15187 _("unrecognized version `%d' in .debug_macro section"),
15188 version);
15189 return NULL;
15190 }
15191 mac_ptr += 2;
757a13d0 15192
cf2c3c16
TT
15193 flags = read_1_byte (abfd, mac_ptr);
15194 ++mac_ptr;
15195 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15196
cf2c3c16
TT
15197 if ((flags & 2) != 0)
15198 /* We don't need the line table offset. */
15199 mac_ptr += *offset_size;
757a13d0 15200
cf2c3c16
TT
15201 /* Vendor opcode descriptions. */
15202 if ((flags & 4) != 0)
15203 {
15204 unsigned int i, count;
757a13d0 15205
cf2c3c16
TT
15206 count = read_1_byte (abfd, mac_ptr);
15207 ++mac_ptr;
15208 for (i = 0; i < count; ++i)
15209 {
15210 unsigned int opcode, bytes_read;
15211 unsigned long arg;
15212
15213 opcode = read_1_byte (abfd, mac_ptr);
15214 ++mac_ptr;
15215 opcode_definitions[opcode] = mac_ptr;
15216 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15217 mac_ptr += bytes_read;
15218 mac_ptr += arg;
15219 }
757a13d0 15220 }
cf2c3c16 15221 }
757a13d0 15222
cf2c3c16
TT
15223 return mac_ptr;
15224}
757a13d0 15225
cf2c3c16
TT
15226/* A helper for dwarf_decode_macros that handles the GNU extensions,
15227 including DW_GNU_MACINFO_transparent_include. */
15228
15229static void
15230dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15231 struct macro_source_file *current_file,
15232 struct line_header *lh, char *comp_dir,
15233 struct dwarf2_section_info *section,
15234 int section_is_gnu,
15235 unsigned int offset_size,
15236 struct objfile *objfile)
15237{
15238 enum dwarf_macro_record_type macinfo_type;
15239 int at_commandline;
15240 gdb_byte *opcode_definitions[256];
757a13d0 15241
cf2c3c16
TT
15242 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15243 &offset_size, section_is_gnu);
15244 if (mac_ptr == NULL)
15245 {
15246 /* We already issued a complaint. */
15247 return;
15248 }
757a13d0
JK
15249
15250 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15251 GDB is still reading the definitions from command line. First
15252 DW_MACINFO_start_file will need to be ignored as it was already executed
15253 to create CURRENT_FILE for the main source holding also the command line
15254 definitions. On first met DW_MACINFO_start_file this flag is reset to
15255 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15256
15257 at_commandline = 1;
15258
15259 do
15260 {
15261 /* Do we at least have room for a macinfo type byte? */
15262 if (mac_ptr >= mac_end)
15263 {
cf2c3c16 15264 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15265 break;
15266 }
15267
15268 macinfo_type = read_1_byte (abfd, mac_ptr);
15269 mac_ptr++;
15270
cf2c3c16
TT
15271 /* Note that we rely on the fact that the corresponding GNU and
15272 DWARF constants are the same. */
757a13d0
JK
15273 switch (macinfo_type)
15274 {
15275 /* A zero macinfo type indicates the end of the macro
15276 information. */
15277 case 0:
15278 break;
2e276125 15279
cf2c3c16
TT
15280 case DW_MACRO_GNU_define:
15281 case DW_MACRO_GNU_undef:
15282 case DW_MACRO_GNU_define_indirect:
15283 case DW_MACRO_GNU_undef_indirect:
2e276125 15284 {
891d2f0b 15285 unsigned int bytes_read;
2e276125
JB
15286 int line;
15287 char *body;
cf2c3c16 15288 int is_define;
2e276125 15289
cf2c3c16
TT
15290 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15291 mac_ptr += bytes_read;
15292
15293 if (macinfo_type == DW_MACRO_GNU_define
15294 || macinfo_type == DW_MACRO_GNU_undef)
15295 {
15296 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15297 mac_ptr += bytes_read;
15298 }
15299 else
15300 {
15301 LONGEST str_offset;
15302
15303 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15304 mac_ptr += offset_size;
2e276125 15305
cf2c3c16
TT
15306 body = read_indirect_string_at_offset (abfd, str_offset);
15307 }
15308
15309 is_define = (macinfo_type == DW_MACRO_GNU_define
15310 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15311 if (! current_file)
757a13d0
JK
15312 {
15313 /* DWARF violation as no main source is present. */
15314 complaint (&symfile_complaints,
15315 _("debug info with no main source gives macro %s "
15316 "on line %d: %s"),
cf2c3c16
TT
15317 is_define ? _("definition") : _("undefinition"),
15318 line, body);
757a13d0
JK
15319 break;
15320 }
3e43a32a
MS
15321 if ((line == 0 && !at_commandline)
15322 || (line != 0 && at_commandline))
4d3c2250 15323 complaint (&symfile_complaints,
757a13d0
JK
15324 _("debug info gives %s macro %s with %s line %d: %s"),
15325 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15326 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15327 line == 0 ? _("zero") : _("non-zero"), line, body);
15328
cf2c3c16 15329 if (is_define)
757a13d0 15330 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15331 else
15332 {
15333 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15334 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15335 macro_undef (current_file, line, body);
15336 }
2e276125
JB
15337 }
15338 break;
15339
cf2c3c16 15340 case DW_MACRO_GNU_start_file:
2e276125 15341 {
891d2f0b 15342 unsigned int bytes_read;
2e276125
JB
15343 int line, file;
15344
15345 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15346 mac_ptr += bytes_read;
15347 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15348 mac_ptr += bytes_read;
15349
3e43a32a
MS
15350 if ((line == 0 && !at_commandline)
15351 || (line != 0 && at_commandline))
757a13d0
JK
15352 complaint (&symfile_complaints,
15353 _("debug info gives source %d included "
15354 "from %s at %s line %d"),
15355 file, at_commandline ? _("command-line") : _("file"),
15356 line == 0 ? _("zero") : _("non-zero"), line);
15357
15358 if (at_commandline)
15359 {
cf2c3c16
TT
15360 /* This DW_MACRO_GNU_start_file was executed in the
15361 pass one. */
757a13d0
JK
15362 at_commandline = 0;
15363 }
15364 else
15365 current_file = macro_start_file (file, line,
15366 current_file, comp_dir,
cf2c3c16 15367 lh, objfile);
2e276125
JB
15368 }
15369 break;
15370
cf2c3c16 15371 case DW_MACRO_GNU_end_file:
2e276125 15372 if (! current_file)
4d3c2250 15373 complaint (&symfile_complaints,
3e43a32a
MS
15374 _("macro debug info has an unmatched "
15375 "`close_file' directive"));
2e276125
JB
15376 else
15377 {
15378 current_file = current_file->included_by;
15379 if (! current_file)
15380 {
cf2c3c16 15381 enum dwarf_macro_record_type next_type;
2e276125
JB
15382
15383 /* GCC circa March 2002 doesn't produce the zero
15384 type byte marking the end of the compilation
15385 unit. Complain if it's not there, but exit no
15386 matter what. */
15387
15388 /* Do we at least have room for a macinfo type byte? */
15389 if (mac_ptr >= mac_end)
15390 {
cf2c3c16 15391 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15392 return;
15393 }
15394
15395 /* We don't increment mac_ptr here, so this is just
15396 a look-ahead. */
15397 next_type = read_1_byte (abfd, mac_ptr);
15398 if (next_type != 0)
4d3c2250 15399 complaint (&symfile_complaints,
3e43a32a
MS
15400 _("no terminating 0-type entry for "
15401 "macros in `.debug_macinfo' section"));
2e276125
JB
15402
15403 return;
15404 }
15405 }
15406 break;
15407
cf2c3c16
TT
15408 case DW_MACRO_GNU_transparent_include:
15409 {
15410 LONGEST offset;
15411
15412 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15413 mac_ptr += offset_size;
15414
15415 dwarf_decode_macro_bytes (abfd,
15416 section->buffer + offset,
15417 mac_end, current_file,
15418 lh, comp_dir,
15419 section, section_is_gnu,
15420 offset_size, objfile);
15421 }
15422 break;
15423
2e276125 15424 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15425 if (!section_is_gnu)
15426 {
15427 unsigned int bytes_read;
15428 int constant;
2e276125 15429
cf2c3c16
TT
15430 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15431 mac_ptr += bytes_read;
15432 read_direct_string (abfd, mac_ptr, &bytes_read);
15433 mac_ptr += bytes_read;
2e276125 15434
cf2c3c16
TT
15435 /* We don't recognize any vendor extensions. */
15436 break;
15437 }
15438 /* FALLTHROUGH */
15439
15440 default:
15441 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15442 mac_ptr, abfd, offset_size,
15443 section);
15444 if (mac_ptr == NULL)
15445 return;
15446 break;
2e276125 15447 }
757a13d0 15448 } while (macinfo_type != 0);
2e276125 15449}
8e19ed76 15450
cf2c3c16
TT
15451static void
15452dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15453 char *comp_dir, bfd *abfd,
15454 struct dwarf2_cu *cu,
15455 struct dwarf2_section_info *section,
15456 int section_is_gnu)
15457{
15458 gdb_byte *mac_ptr, *mac_end;
15459 struct macro_source_file *current_file = 0;
15460 enum dwarf_macro_record_type macinfo_type;
15461 unsigned int offset_size = cu->header.offset_size;
15462 gdb_byte *opcode_definitions[256];
15463
15464 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15465 if (section->buffer == NULL)
15466 {
15467 complaint (&symfile_complaints, _("missing %s section"),
15468 section->asection->name);
15469 return;
15470 }
15471
15472 /* First pass: Find the name of the base filename.
15473 This filename is needed in order to process all macros whose definition
15474 (or undefinition) comes from the command line. These macros are defined
15475 before the first DW_MACINFO_start_file entry, and yet still need to be
15476 associated to the base file.
15477
15478 To determine the base file name, we scan the macro definitions until we
15479 reach the first DW_MACINFO_start_file entry. We then initialize
15480 CURRENT_FILE accordingly so that any macro definition found before the
15481 first DW_MACINFO_start_file can still be associated to the base file. */
15482
15483 mac_ptr = section->buffer + offset;
15484 mac_end = section->buffer + section->size;
15485
15486 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15487 &offset_size, section_is_gnu);
15488 if (mac_ptr == NULL)
15489 {
15490 /* We already issued a complaint. */
15491 return;
15492 }
15493
15494 do
15495 {
15496 /* Do we at least have room for a macinfo type byte? */
15497 if (mac_ptr >= mac_end)
15498 {
15499 /* Complaint is printed during the second pass as GDB will probably
15500 stop the first pass earlier upon finding
15501 DW_MACINFO_start_file. */
15502 break;
15503 }
15504
15505 macinfo_type = read_1_byte (abfd, mac_ptr);
15506 mac_ptr++;
15507
15508 /* Note that we rely on the fact that the corresponding GNU and
15509 DWARF constants are the same. */
15510 switch (macinfo_type)
15511 {
15512 /* A zero macinfo type indicates the end of the macro
15513 information. */
15514 case 0:
15515 break;
15516
15517 case DW_MACRO_GNU_define:
15518 case DW_MACRO_GNU_undef:
15519 /* Only skip the data by MAC_PTR. */
15520 {
15521 unsigned int bytes_read;
15522
15523 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15524 mac_ptr += bytes_read;
15525 read_direct_string (abfd, mac_ptr, &bytes_read);
15526 mac_ptr += bytes_read;
15527 }
15528 break;
15529
15530 case DW_MACRO_GNU_start_file:
15531 {
15532 unsigned int bytes_read;
15533 int line, file;
15534
15535 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15536 mac_ptr += bytes_read;
15537 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15538 mac_ptr += bytes_read;
15539
15540 current_file = macro_start_file (file, line, current_file,
15541 comp_dir, lh, cu->objfile);
15542 }
15543 break;
15544
15545 case DW_MACRO_GNU_end_file:
15546 /* No data to skip by MAC_PTR. */
15547 break;
15548
15549 case DW_MACRO_GNU_define_indirect:
15550 case DW_MACRO_GNU_undef_indirect:
15551 {
15552 unsigned int bytes_read;
15553
15554 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15555 mac_ptr += bytes_read;
15556 mac_ptr += offset_size;
15557 }
15558 break;
15559
15560 case DW_MACRO_GNU_transparent_include:
15561 /* Note that, according to the spec, a transparent include
15562 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15563 skip this opcode. */
15564 mac_ptr += offset_size;
15565 break;
15566
15567 case DW_MACINFO_vendor_ext:
15568 /* Only skip the data by MAC_PTR. */
15569 if (!section_is_gnu)
15570 {
15571 unsigned int bytes_read;
15572
15573 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15574 mac_ptr += bytes_read;
15575 read_direct_string (abfd, mac_ptr, &bytes_read);
15576 mac_ptr += bytes_read;
15577 }
15578 /* FALLTHROUGH */
15579
15580 default:
15581 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15582 mac_ptr, abfd, offset_size,
15583 section);
15584 if (mac_ptr == NULL)
15585 return;
15586 break;
15587 }
15588 } while (macinfo_type != 0 && current_file == NULL);
15589
15590 /* Second pass: Process all entries.
15591
15592 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15593 command-line macro definitions/undefinitions. This flag is unset when we
15594 reach the first DW_MACINFO_start_file entry. */
15595
15596 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15597 current_file, lh, comp_dir, section, section_is_gnu,
15598 offset_size, cu->objfile);
15599}
15600
8e19ed76 15601/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15602 if so return true else false. */
8e19ed76
PS
15603static int
15604attr_form_is_block (struct attribute *attr)
15605{
15606 return (attr == NULL ? 0 :
15607 attr->form == DW_FORM_block1
15608 || attr->form == DW_FORM_block2
15609 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15610 || attr->form == DW_FORM_block
15611 || attr->form == DW_FORM_exprloc);
8e19ed76 15612}
4c2df51b 15613
c6a0999f
JB
15614/* Return non-zero if ATTR's value is a section offset --- classes
15615 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15616 You may use DW_UNSND (attr) to retrieve such offsets.
15617
15618 Section 7.5.4, "Attribute Encodings", explains that no attribute
15619 may have a value that belongs to more than one of these classes; it
15620 would be ambiguous if we did, because we use the same forms for all
15621 of them. */
3690dd37
JB
15622static int
15623attr_form_is_section_offset (struct attribute *attr)
15624{
15625 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15626 || attr->form == DW_FORM_data8
15627 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15628}
15629
15630
15631/* Return non-zero if ATTR's value falls in the 'constant' class, or
15632 zero otherwise. When this function returns true, you can apply
15633 dwarf2_get_attr_constant_value to it.
15634
15635 However, note that for some attributes you must check
15636 attr_form_is_section_offset before using this test. DW_FORM_data4
15637 and DW_FORM_data8 are members of both the constant class, and of
15638 the classes that contain offsets into other debug sections
15639 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15640 that, if an attribute's can be either a constant or one of the
15641 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15642 taken as section offsets, not constants. */
15643static int
15644attr_form_is_constant (struct attribute *attr)
15645{
15646 switch (attr->form)
15647 {
15648 case DW_FORM_sdata:
15649 case DW_FORM_udata:
15650 case DW_FORM_data1:
15651 case DW_FORM_data2:
15652 case DW_FORM_data4:
15653 case DW_FORM_data8:
15654 return 1;
15655 default:
15656 return 0;
15657 }
15658}
15659
8cf6f0b1
TT
15660/* A helper function that fills in a dwarf2_loclist_baton. */
15661
15662static void
15663fill_in_loclist_baton (struct dwarf2_cu *cu,
15664 struct dwarf2_loclist_baton *baton,
15665 struct attribute *attr)
15666{
15667 dwarf2_read_section (dwarf2_per_objfile->objfile,
15668 &dwarf2_per_objfile->loc);
15669
15670 baton->per_cu = cu->per_cu;
15671 gdb_assert (baton->per_cu);
15672 /* We don't know how long the location list is, but make sure we
15673 don't run off the edge of the section. */
15674 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15675 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15676 baton->base_address = cu->base_address;
15677}
15678
4c2df51b
DJ
15679static void
15680dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15681 struct dwarf2_cu *cu)
4c2df51b 15682{
3690dd37 15683 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15684 /* ".debug_loc" may not exist at all, or the offset may be outside
15685 the section. If so, fall through to the complaint in the
15686 other branch. */
9e0ac564
TT
15687 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15688 &dwarf2_per_objfile->loc))
4c2df51b 15689 {
0d53c4c4 15690 struct dwarf2_loclist_baton *baton;
4c2df51b 15691
4a146b47 15692 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15693 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15694
8cf6f0b1 15695 fill_in_loclist_baton (cu, baton, attr);
be391dca 15696
d00adf39 15697 if (cu->base_known == 0)
0d53c4c4 15698 complaint (&symfile_complaints,
3e43a32a
MS
15699 _("Location list used without "
15700 "specifying the CU base address."));
4c2df51b 15701
768a979c 15702 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15703 SYMBOL_LOCATION_BATON (sym) = baton;
15704 }
15705 else
15706 {
15707 struct dwarf2_locexpr_baton *baton;
15708
4a146b47 15709 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15710 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15711 baton->per_cu = cu->per_cu;
15712 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15713
15714 if (attr_form_is_block (attr))
15715 {
15716 /* Note that we're just copying the block's data pointer
15717 here, not the actual data. We're still pointing into the
6502dd73
DJ
15718 info_buffer for SYM's objfile; right now we never release
15719 that buffer, but when we do clean up properly this may
15720 need to change. */
0d53c4c4
DJ
15721 baton->size = DW_BLOCK (attr)->size;
15722 baton->data = DW_BLOCK (attr)->data;
15723 }
15724 else
15725 {
15726 dwarf2_invalid_attrib_class_complaint ("location description",
15727 SYMBOL_NATURAL_NAME (sym));
15728 baton->size = 0;
0d53c4c4 15729 }
6e70227d 15730
768a979c 15731 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15732 SYMBOL_LOCATION_BATON (sym) = baton;
15733 }
4c2df51b 15734}
6502dd73 15735
9aa1f1e3
TT
15736/* Return the OBJFILE associated with the compilation unit CU. If CU
15737 came from a separate debuginfo file, then the master objfile is
15738 returned. */
ae0d2f24
UW
15739
15740struct objfile *
15741dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15742{
9291a0cd 15743 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15744
15745 /* Return the master objfile, so that we can report and look up the
15746 correct file containing this variable. */
15747 if (objfile->separate_debug_objfile_backlink)
15748 objfile = objfile->separate_debug_objfile_backlink;
15749
15750 return objfile;
15751}
15752
96408a79
SA
15753/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15754 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15755 CU_HEADERP first. */
15756
15757static const struct comp_unit_head *
15758per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15759 struct dwarf2_per_cu_data *per_cu)
15760{
15761 struct objfile *objfile;
15762 struct dwarf2_per_objfile *per_objfile;
15763 gdb_byte *info_ptr;
15764
15765 if (per_cu->cu)
15766 return &per_cu->cu->header;
15767
15768 objfile = per_cu->objfile;
15769 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15770 info_ptr = per_objfile->info.buffer + per_cu->offset;
15771
15772 memset (cu_headerp, 0, sizeof (*cu_headerp));
15773 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15774
15775 return cu_headerp;
15776}
15777
ae0d2f24
UW
15778/* Return the address size given in the compilation unit header for CU. */
15779
15780CORE_ADDR
15781dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15782{
96408a79
SA
15783 struct comp_unit_head cu_header_local;
15784 const struct comp_unit_head *cu_headerp;
c471e790 15785
96408a79
SA
15786 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15787
15788 return cu_headerp->addr_size;
ae0d2f24
UW
15789}
15790
9eae7c52
TT
15791/* Return the offset size given in the compilation unit header for CU. */
15792
15793int
15794dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15795{
96408a79
SA
15796 struct comp_unit_head cu_header_local;
15797 const struct comp_unit_head *cu_headerp;
9c6c53f7 15798
96408a79
SA
15799 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15800
15801 return cu_headerp->offset_size;
15802}
15803
15804/* See its dwarf2loc.h declaration. */
15805
15806int
15807dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15808{
15809 struct comp_unit_head cu_header_local;
15810 const struct comp_unit_head *cu_headerp;
15811
15812 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15813
15814 if (cu_headerp->version == 2)
15815 return cu_headerp->addr_size;
15816 else
15817 return cu_headerp->offset_size;
181cebd4
JK
15818}
15819
9aa1f1e3
TT
15820/* Return the text offset of the CU. The returned offset comes from
15821 this CU's objfile. If this objfile came from a separate debuginfo
15822 file, then the offset may be different from the corresponding
15823 offset in the parent objfile. */
15824
15825CORE_ADDR
15826dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15827{
bb3fa9d0 15828 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15829
15830 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15831}
15832
348e048f
DE
15833/* Locate the .debug_info compilation unit from CU's objfile which contains
15834 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15835
15836static struct dwarf2_per_cu_data *
c764a876 15837dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15838 struct objfile *objfile)
15839{
15840 struct dwarf2_per_cu_data *this_cu;
15841 int low, high;
15842
ae038cb0
DJ
15843 low = 0;
15844 high = dwarf2_per_objfile->n_comp_units - 1;
15845 while (high > low)
15846 {
15847 int mid = low + (high - low) / 2;
9a619af0 15848
ae038cb0
DJ
15849 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15850 high = mid;
15851 else
15852 low = mid + 1;
15853 }
15854 gdb_assert (low == high);
15855 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15856 {
10b3939b 15857 if (low == 0)
8a3fe4f8
AC
15858 error (_("Dwarf Error: could not find partial DIE containing "
15859 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15860 (long) offset, bfd_get_filename (objfile->obfd));
15861
ae038cb0
DJ
15862 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15863 return dwarf2_per_objfile->all_comp_units[low-1];
15864 }
15865 else
15866 {
15867 this_cu = dwarf2_per_objfile->all_comp_units[low];
15868 if (low == dwarf2_per_objfile->n_comp_units - 1
15869 && offset >= this_cu->offset + this_cu->length)
c764a876 15870 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15871 gdb_assert (offset < this_cu->offset + this_cu->length);
15872 return this_cu;
15873 }
15874}
15875
10b3939b
DJ
15876/* Locate the compilation unit from OBJFILE which is located at exactly
15877 OFFSET. Raises an error on failure. */
15878
ae038cb0 15879static struct dwarf2_per_cu_data *
c764a876 15880dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
15881{
15882 struct dwarf2_per_cu_data *this_cu;
9a619af0 15883
ae038cb0
DJ
15884 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15885 if (this_cu->offset != offset)
c764a876 15886 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
15887 return this_cu;
15888}
15889
9816fde3 15890/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 15891
9816fde3
JK
15892static void
15893init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 15894{
9816fde3 15895 memset (cu, 0, sizeof (*cu));
93311388
DE
15896 cu->objfile = objfile;
15897 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15898}
15899
15900/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15901
15902static void
15903prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15904{
15905 struct attribute *attr;
15906
15907 /* Set the language we're debugging. */
15908 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15909 if (attr)
15910 set_cu_language (DW_UNSND (attr), cu);
15911 else
9cded63f
TT
15912 {
15913 cu->language = language_minimal;
15914 cu->language_defn = language_def (cu->language);
15915 }
93311388
DE
15916}
15917
ae038cb0
DJ
15918/* Release one cached compilation unit, CU. We unlink it from the tree
15919 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15920 the caller is responsible for that.
15921 NOTE: DATA is a void * because this function is also used as a
15922 cleanup routine. */
ae038cb0
DJ
15923
15924static void
15925free_one_comp_unit (void *data)
15926{
15927 struct dwarf2_cu *cu = data;
15928
15929 if (cu->per_cu != NULL)
15930 cu->per_cu->cu = NULL;
15931 cu->per_cu = NULL;
15932
15933 obstack_free (&cu->comp_unit_obstack, NULL);
15934
15935 xfree (cu);
15936}
15937
72bf9492 15938/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15939 when we're finished with it. We can't free the pointer itself, but be
15940 sure to unlink it from the cache. Also release any associated storage
15941 and perform cache maintenance.
72bf9492
DJ
15942
15943 Only used during partial symbol parsing. */
15944
15945static void
15946free_stack_comp_unit (void *data)
15947{
15948 struct dwarf2_cu *cu = data;
15949
15950 obstack_free (&cu->comp_unit_obstack, NULL);
15951 cu->partial_dies = NULL;
ae038cb0
DJ
15952
15953 if (cu->per_cu != NULL)
15954 {
15955 /* This compilation unit is on the stack in our caller, so we
15956 should not xfree it. Just unlink it. */
15957 cu->per_cu->cu = NULL;
15958 cu->per_cu = NULL;
15959
15960 /* If we had a per-cu pointer, then we may have other compilation
15961 units loaded, so age them now. */
15962 age_cached_comp_units ();
15963 }
15964}
15965
15966/* Free all cached compilation units. */
15967
15968static void
15969free_cached_comp_units (void *data)
15970{
15971 struct dwarf2_per_cu_data *per_cu, **last_chain;
15972
15973 per_cu = dwarf2_per_objfile->read_in_chain;
15974 last_chain = &dwarf2_per_objfile->read_in_chain;
15975 while (per_cu != NULL)
15976 {
15977 struct dwarf2_per_cu_data *next_cu;
15978
15979 next_cu = per_cu->cu->read_in_chain;
15980
15981 free_one_comp_unit (per_cu->cu);
15982 *last_chain = next_cu;
15983
15984 per_cu = next_cu;
15985 }
15986}
15987
15988/* Increase the age counter on each cached compilation unit, and free
15989 any that are too old. */
15990
15991static void
15992age_cached_comp_units (void)
15993{
15994 struct dwarf2_per_cu_data *per_cu, **last_chain;
15995
15996 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15997 per_cu = dwarf2_per_objfile->read_in_chain;
15998 while (per_cu != NULL)
15999 {
16000 per_cu->cu->last_used ++;
16001 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16002 dwarf2_mark (per_cu->cu);
16003 per_cu = per_cu->cu->read_in_chain;
16004 }
16005
16006 per_cu = dwarf2_per_objfile->read_in_chain;
16007 last_chain = &dwarf2_per_objfile->read_in_chain;
16008 while (per_cu != NULL)
16009 {
16010 struct dwarf2_per_cu_data *next_cu;
16011
16012 next_cu = per_cu->cu->read_in_chain;
16013
16014 if (!per_cu->cu->mark)
16015 {
16016 free_one_comp_unit (per_cu->cu);
16017 *last_chain = next_cu;
16018 }
16019 else
16020 last_chain = &per_cu->cu->read_in_chain;
16021
16022 per_cu = next_cu;
16023 }
16024}
16025
16026/* Remove a single compilation unit from the cache. */
16027
16028static void
16029free_one_cached_comp_unit (void *target_cu)
16030{
16031 struct dwarf2_per_cu_data *per_cu, **last_chain;
16032
16033 per_cu = dwarf2_per_objfile->read_in_chain;
16034 last_chain = &dwarf2_per_objfile->read_in_chain;
16035 while (per_cu != NULL)
16036 {
16037 struct dwarf2_per_cu_data *next_cu;
16038
16039 next_cu = per_cu->cu->read_in_chain;
16040
16041 if (per_cu->cu == target_cu)
16042 {
16043 free_one_comp_unit (per_cu->cu);
16044 *last_chain = next_cu;
16045 break;
16046 }
16047 else
16048 last_chain = &per_cu->cu->read_in_chain;
16049
16050 per_cu = next_cu;
16051 }
16052}
16053
fe3e1990
DJ
16054/* Release all extra memory associated with OBJFILE. */
16055
16056void
16057dwarf2_free_objfile (struct objfile *objfile)
16058{
16059 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16060
16061 if (dwarf2_per_objfile == NULL)
16062 return;
16063
16064 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16065 free_cached_comp_units (NULL);
16066
7b9f3c50
DE
16067 if (dwarf2_per_objfile->quick_file_names_table)
16068 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16069
fe3e1990
DJ
16070 /* Everything else should be on the objfile obstack. */
16071}
16072
1c379e20
DJ
16073/* A pair of DIE offset and GDB type pointer. We store these
16074 in a hash table separate from the DIEs, and preserve them
16075 when the DIEs are flushed out of cache. */
16076
16077struct dwarf2_offset_and_type
16078{
16079 unsigned int offset;
16080 struct type *type;
16081};
16082
16083/* Hash function for a dwarf2_offset_and_type. */
16084
16085static hashval_t
16086offset_and_type_hash (const void *item)
16087{
16088 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 16089
1c379e20
DJ
16090 return ofs->offset;
16091}
16092
16093/* Equality function for a dwarf2_offset_and_type. */
16094
16095static int
16096offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16097{
16098 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16099 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16100
1c379e20
DJ
16101 return ofs_lhs->offset == ofs_rhs->offset;
16102}
16103
16104/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16105 table if necessary. For convenience, return TYPE.
16106
16107 The DIEs reading must have careful ordering to:
16108 * Not cause infite loops trying to read in DIEs as a prerequisite for
16109 reading current DIE.
16110 * Not trying to dereference contents of still incompletely read in types
16111 while reading in other DIEs.
16112 * Enable referencing still incompletely read in types just by a pointer to
16113 the type without accessing its fields.
16114
16115 Therefore caller should follow these rules:
16116 * Try to fetch any prerequisite types we may need to build this DIE type
16117 before building the type and calling set_die_type.
e71ec853 16118 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16119 possible before fetching more types to complete the current type.
16120 * Make the type as complete as possible before fetching more types. */
1c379e20 16121
f792889a 16122static struct type *
1c379e20
DJ
16123set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16124{
16125 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
16126 struct objfile *objfile = cu->objfile;
16127 htab_t *type_hash_ptr;
1c379e20 16128
b4ba55a1
JB
16129 /* For Ada types, make sure that the gnat-specific data is always
16130 initialized (if not already set). There are a few types where
16131 we should not be doing so, because the type-specific area is
16132 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16133 where the type-specific area is used to store the floatformat).
16134 But this is not a problem, because the gnat-specific information
16135 is actually not needed for these types. */
16136 if (need_gnat_info (cu)
16137 && TYPE_CODE (type) != TYPE_CODE_FUNC
16138 && TYPE_CODE (type) != TYPE_CODE_FLT
16139 && !HAVE_GNAT_AUX_INFO (type))
16140 INIT_GNAT_SPECIFIC (type);
16141
8b70b953 16142 if (cu->per_cu->debug_type_section)
673bfd45
DE
16143 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16144 else
16145 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16146
16147 if (*type_hash_ptr == NULL)
f792889a 16148 {
673bfd45
DE
16149 *type_hash_ptr
16150 = htab_create_alloc_ex (127,
f792889a
DJ
16151 offset_and_type_hash,
16152 offset_and_type_eq,
16153 NULL,
673bfd45 16154 &objfile->objfile_obstack,
f792889a
DJ
16155 hashtab_obstack_allocate,
16156 dummy_obstack_deallocate);
f792889a 16157 }
1c379e20
DJ
16158
16159 ofs.offset = die->offset;
16160 ofs.type = type;
16161 slot = (struct dwarf2_offset_and_type **)
673bfd45 16162 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
16163 if (*slot)
16164 complaint (&symfile_complaints,
16165 _("A problem internal to GDB: DIE 0x%x has type already set"),
16166 die->offset);
673bfd45 16167 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16168 **slot = ofs;
f792889a 16169 return type;
1c379e20
DJ
16170}
16171
673bfd45
DE
16172/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16173 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16174
16175static struct type *
673bfd45
DE
16176get_die_type_at_offset (unsigned int offset,
16177 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16178{
16179 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16180 htab_t type_hash;
f792889a 16181
8b70b953 16182 if (per_cu->debug_type_section)
673bfd45
DE
16183 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16184 else
16185 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16186 if (type_hash == NULL)
16187 return NULL;
1c379e20 16188
673bfd45 16189 ofs.offset = offset;
1c379e20
DJ
16190 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16191 if (slot)
16192 return slot->type;
16193 else
16194 return NULL;
16195}
16196
673bfd45
DE
16197/* Look up the type for DIE in the appropriate type_hash table,
16198 or return NULL if DIE does not have a saved type. */
16199
16200static struct type *
16201get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16202{
16203 return get_die_type_at_offset (die->offset, cu->per_cu);
16204}
16205
10b3939b
DJ
16206/* Add a dependence relationship from CU to REF_PER_CU. */
16207
16208static void
16209dwarf2_add_dependence (struct dwarf2_cu *cu,
16210 struct dwarf2_per_cu_data *ref_per_cu)
16211{
16212 void **slot;
16213
16214 if (cu->dependencies == NULL)
16215 cu->dependencies
16216 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16217 NULL, &cu->comp_unit_obstack,
16218 hashtab_obstack_allocate,
16219 dummy_obstack_deallocate);
16220
16221 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16222 if (*slot == NULL)
16223 *slot = ref_per_cu;
16224}
1c379e20 16225
f504f079
DE
16226/* Subroutine of dwarf2_mark to pass to htab_traverse.
16227 Set the mark field in every compilation unit in the
ae038cb0
DJ
16228 cache that we must keep because we are keeping CU. */
16229
10b3939b
DJ
16230static int
16231dwarf2_mark_helper (void **slot, void *data)
16232{
16233 struct dwarf2_per_cu_data *per_cu;
16234
16235 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16236
16237 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16238 reading of the chain. As such dependencies remain valid it is not much
16239 useful to track and undo them during QUIT cleanups. */
16240 if (per_cu->cu == NULL)
16241 return 1;
16242
10b3939b
DJ
16243 if (per_cu->cu->mark)
16244 return 1;
16245 per_cu->cu->mark = 1;
16246
16247 if (per_cu->cu->dependencies != NULL)
16248 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16249
16250 return 1;
16251}
16252
f504f079
DE
16253/* Set the mark field in CU and in every other compilation unit in the
16254 cache that we must keep because we are keeping CU. */
16255
ae038cb0
DJ
16256static void
16257dwarf2_mark (struct dwarf2_cu *cu)
16258{
16259 if (cu->mark)
16260 return;
16261 cu->mark = 1;
10b3939b
DJ
16262 if (cu->dependencies != NULL)
16263 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16264}
16265
16266static void
16267dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16268{
16269 while (per_cu)
16270 {
16271 per_cu->cu->mark = 0;
16272 per_cu = per_cu->cu->read_in_chain;
16273 }
72bf9492
DJ
16274}
16275
72bf9492
DJ
16276/* Trivial hash function for partial_die_info: the hash value of a DIE
16277 is its offset in .debug_info for this objfile. */
16278
16279static hashval_t
16280partial_die_hash (const void *item)
16281{
16282 const struct partial_die_info *part_die = item;
9a619af0 16283
72bf9492
DJ
16284 return part_die->offset;
16285}
16286
16287/* Trivial comparison function for partial_die_info structures: two DIEs
16288 are equal if they have the same offset. */
16289
16290static int
16291partial_die_eq (const void *item_lhs, const void *item_rhs)
16292{
16293 const struct partial_die_info *part_die_lhs = item_lhs;
16294 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16295
72bf9492
DJ
16296 return part_die_lhs->offset == part_die_rhs->offset;
16297}
16298
ae038cb0
DJ
16299static struct cmd_list_element *set_dwarf2_cmdlist;
16300static struct cmd_list_element *show_dwarf2_cmdlist;
16301
16302static void
16303set_dwarf2_cmd (char *args, int from_tty)
16304{
16305 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16306}
16307
16308static void
16309show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16310{
ae038cb0
DJ
16311 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16312}
16313
dce234bc
PP
16314/* If section described by INFO was mmapped, munmap it now. */
16315
16316static void
16317munmap_section_buffer (struct dwarf2_section_info *info)
16318{
b315ab21 16319 if (info->map_addr != NULL)
dce234bc
PP
16320 {
16321#ifdef HAVE_MMAP
b315ab21 16322 int res;
9a619af0 16323
b315ab21
TG
16324 res = munmap (info->map_addr, info->map_len);
16325 gdb_assert (res == 0);
dce234bc
PP
16326#else
16327 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16328 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16329#endif
16330 }
16331}
16332
16333/* munmap debug sections for OBJFILE, if necessary. */
16334
16335static void
c1bd65d0 16336dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16337{
16338 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16339 int ix;
16340 struct dwarf2_section_info *section;
9a619af0 16341
16be1145
DE
16342 /* This is sorted according to the order they're defined in to make it easier
16343 to keep in sync. */
dce234bc
PP
16344 munmap_section_buffer (&data->info);
16345 munmap_section_buffer (&data->abbrev);
16346 munmap_section_buffer (&data->line);
16be1145 16347 munmap_section_buffer (&data->loc);
dce234bc 16348 munmap_section_buffer (&data->macinfo);
cf2c3c16 16349 munmap_section_buffer (&data->macro);
16be1145 16350 munmap_section_buffer (&data->str);
dce234bc 16351 munmap_section_buffer (&data->ranges);
dce234bc
PP
16352 munmap_section_buffer (&data->frame);
16353 munmap_section_buffer (&data->eh_frame);
9291a0cd 16354 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16355
16356 for (ix = 0;
16357 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16358 ++ix)
16359 munmap_section_buffer (section);
16360
16361 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16362}
16363
16364\f
ae2de4f8 16365/* The "save gdb-index" command. */
9291a0cd
TT
16366
16367/* The contents of the hash table we create when building the string
16368 table. */
16369struct strtab_entry
16370{
16371 offset_type offset;
16372 const char *str;
16373};
16374
559a7a62
JK
16375/* Hash function for a strtab_entry.
16376
16377 Function is used only during write_hash_table so no index format backward
16378 compatibility is needed. */
b89be57b 16379
9291a0cd
TT
16380static hashval_t
16381hash_strtab_entry (const void *e)
16382{
16383 const struct strtab_entry *entry = e;
559a7a62 16384 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16385}
16386
16387/* Equality function for a strtab_entry. */
b89be57b 16388
9291a0cd
TT
16389static int
16390eq_strtab_entry (const void *a, const void *b)
16391{
16392 const struct strtab_entry *ea = a;
16393 const struct strtab_entry *eb = b;
16394 return !strcmp (ea->str, eb->str);
16395}
16396
16397/* Create a strtab_entry hash table. */
b89be57b 16398
9291a0cd
TT
16399static htab_t
16400create_strtab (void)
16401{
16402 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16403 xfree, xcalloc, xfree);
16404}
16405
16406/* Add a string to the constant pool. Return the string's offset in
16407 host order. */
b89be57b 16408
9291a0cd
TT
16409static offset_type
16410add_string (htab_t table, struct obstack *cpool, const char *str)
16411{
16412 void **slot;
16413 struct strtab_entry entry;
16414 struct strtab_entry *result;
16415
16416 entry.str = str;
16417 slot = htab_find_slot (table, &entry, INSERT);
16418 if (*slot)
16419 result = *slot;
16420 else
16421 {
16422 result = XNEW (struct strtab_entry);
16423 result->offset = obstack_object_size (cpool);
16424 result->str = str;
16425 obstack_grow_str0 (cpool, str);
16426 *slot = result;
16427 }
16428 return result->offset;
16429}
16430
16431/* An entry in the symbol table. */
16432struct symtab_index_entry
16433{
16434 /* The name of the symbol. */
16435 const char *name;
16436 /* The offset of the name in the constant pool. */
16437 offset_type index_offset;
16438 /* A sorted vector of the indices of all the CUs that hold an object
16439 of this name. */
16440 VEC (offset_type) *cu_indices;
16441};
16442
16443/* The symbol table. This is a power-of-2-sized hash table. */
16444struct mapped_symtab
16445{
16446 offset_type n_elements;
16447 offset_type size;
16448 struct symtab_index_entry **data;
16449};
16450
16451/* Hash function for a symtab_index_entry. */
b89be57b 16452
9291a0cd
TT
16453static hashval_t
16454hash_symtab_entry (const void *e)
16455{
16456 const struct symtab_index_entry *entry = e;
16457 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16458 sizeof (offset_type) * VEC_length (offset_type,
16459 entry->cu_indices),
16460 0);
16461}
16462
16463/* Equality function for a symtab_index_entry. */
b89be57b 16464
9291a0cd
TT
16465static int
16466eq_symtab_entry (const void *a, const void *b)
16467{
16468 const struct symtab_index_entry *ea = a;
16469 const struct symtab_index_entry *eb = b;
16470 int len = VEC_length (offset_type, ea->cu_indices);
16471 if (len != VEC_length (offset_type, eb->cu_indices))
16472 return 0;
16473 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16474 VEC_address (offset_type, eb->cu_indices),
16475 sizeof (offset_type) * len);
16476}
16477
16478/* Destroy a symtab_index_entry. */
b89be57b 16479
9291a0cd
TT
16480static void
16481delete_symtab_entry (void *p)
16482{
16483 struct symtab_index_entry *entry = p;
16484 VEC_free (offset_type, entry->cu_indices);
16485 xfree (entry);
16486}
16487
16488/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16489
9291a0cd 16490static htab_t
3876f04e 16491create_symbol_hash_table (void)
9291a0cd
TT
16492{
16493 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16494 delete_symtab_entry, xcalloc, xfree);
16495}
16496
16497/* Create a new mapped symtab object. */
b89be57b 16498
9291a0cd
TT
16499static struct mapped_symtab *
16500create_mapped_symtab (void)
16501{
16502 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16503 symtab->n_elements = 0;
16504 symtab->size = 1024;
16505 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16506 return symtab;
16507}
16508
16509/* Destroy a mapped_symtab. */
b89be57b 16510
9291a0cd
TT
16511static void
16512cleanup_mapped_symtab (void *p)
16513{
16514 struct mapped_symtab *symtab = p;
16515 /* The contents of the array are freed when the other hash table is
16516 destroyed. */
16517 xfree (symtab->data);
16518 xfree (symtab);
16519}
16520
16521/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16522 the slot.
16523
16524 Function is used only during write_hash_table so no index format backward
16525 compatibility is needed. */
b89be57b 16526
9291a0cd
TT
16527static struct symtab_index_entry **
16528find_slot (struct mapped_symtab *symtab, const char *name)
16529{
559a7a62 16530 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16531
16532 index = hash & (symtab->size - 1);
16533 step = ((hash * 17) & (symtab->size - 1)) | 1;
16534
16535 for (;;)
16536 {
16537 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16538 return &symtab->data[index];
16539 index = (index + step) & (symtab->size - 1);
16540 }
16541}
16542
16543/* Expand SYMTAB's hash table. */
b89be57b 16544
9291a0cd
TT
16545static void
16546hash_expand (struct mapped_symtab *symtab)
16547{
16548 offset_type old_size = symtab->size;
16549 offset_type i;
16550 struct symtab_index_entry **old_entries = symtab->data;
16551
16552 symtab->size *= 2;
16553 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16554
16555 for (i = 0; i < old_size; ++i)
16556 {
16557 if (old_entries[i])
16558 {
16559 struct symtab_index_entry **slot = find_slot (symtab,
16560 old_entries[i]->name);
16561 *slot = old_entries[i];
16562 }
16563 }
16564
16565 xfree (old_entries);
16566}
16567
16568/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16569 is the index of the CU in which the symbol appears. */
b89be57b 16570
9291a0cd
TT
16571static void
16572add_index_entry (struct mapped_symtab *symtab, const char *name,
16573 offset_type cu_index)
16574{
16575 struct symtab_index_entry **slot;
16576
16577 ++symtab->n_elements;
16578 if (4 * symtab->n_elements / 3 >= symtab->size)
16579 hash_expand (symtab);
16580
16581 slot = find_slot (symtab, name);
16582 if (!*slot)
16583 {
16584 *slot = XNEW (struct symtab_index_entry);
16585 (*slot)->name = name;
16586 (*slot)->cu_indices = NULL;
16587 }
16588 /* Don't push an index twice. Due to how we add entries we only
16589 have to check the last one. */
16590 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16591 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16592 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16593}
16594
16595/* Add a vector of indices to the constant pool. */
b89be57b 16596
9291a0cd 16597static offset_type
3876f04e 16598add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16599 struct symtab_index_entry *entry)
16600{
16601 void **slot;
16602
3876f04e 16603 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16604 if (!*slot)
16605 {
16606 offset_type len = VEC_length (offset_type, entry->cu_indices);
16607 offset_type val = MAYBE_SWAP (len);
16608 offset_type iter;
16609 int i;
16610
16611 *slot = entry;
16612 entry->index_offset = obstack_object_size (cpool);
16613
16614 obstack_grow (cpool, &val, sizeof (val));
16615 for (i = 0;
16616 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16617 ++i)
16618 {
16619 val = MAYBE_SWAP (iter);
16620 obstack_grow (cpool, &val, sizeof (val));
16621 }
16622 }
16623 else
16624 {
16625 struct symtab_index_entry *old_entry = *slot;
16626 entry->index_offset = old_entry->index_offset;
16627 entry = old_entry;
16628 }
16629 return entry->index_offset;
16630}
16631
16632/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16633 constant pool entries going into the obstack CPOOL. */
b89be57b 16634
9291a0cd
TT
16635static void
16636write_hash_table (struct mapped_symtab *symtab,
16637 struct obstack *output, struct obstack *cpool)
16638{
16639 offset_type i;
3876f04e 16640 htab_t symbol_hash_table;
9291a0cd
TT
16641 htab_t str_table;
16642
3876f04e 16643 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16644 str_table = create_strtab ();
3876f04e 16645
9291a0cd
TT
16646 /* We add all the index vectors to the constant pool first, to
16647 ensure alignment is ok. */
16648 for (i = 0; i < symtab->size; ++i)
16649 {
16650 if (symtab->data[i])
3876f04e 16651 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16652 }
16653
16654 /* Now write out the hash table. */
16655 for (i = 0; i < symtab->size; ++i)
16656 {
16657 offset_type str_off, vec_off;
16658
16659 if (symtab->data[i])
16660 {
16661 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16662 vec_off = symtab->data[i]->index_offset;
16663 }
16664 else
16665 {
16666 /* While 0 is a valid constant pool index, it is not valid
16667 to have 0 for both offsets. */
16668 str_off = 0;
16669 vec_off = 0;
16670 }
16671
16672 str_off = MAYBE_SWAP (str_off);
16673 vec_off = MAYBE_SWAP (vec_off);
16674
16675 obstack_grow (output, &str_off, sizeof (str_off));
16676 obstack_grow (output, &vec_off, sizeof (vec_off));
16677 }
16678
16679 htab_delete (str_table);
3876f04e 16680 htab_delete (symbol_hash_table);
9291a0cd
TT
16681}
16682
0a5429f6
DE
16683/* Struct to map psymtab to CU index in the index file. */
16684struct psymtab_cu_index_map
16685{
16686 struct partial_symtab *psymtab;
16687 unsigned int cu_index;
16688};
16689
16690static hashval_t
16691hash_psymtab_cu_index (const void *item)
16692{
16693 const struct psymtab_cu_index_map *map = item;
16694
16695 return htab_hash_pointer (map->psymtab);
16696}
16697
16698static int
16699eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16700{
16701 const struct psymtab_cu_index_map *lhs = item_lhs;
16702 const struct psymtab_cu_index_map *rhs = item_rhs;
16703
16704 return lhs->psymtab == rhs->psymtab;
16705}
16706
16707/* Helper struct for building the address table. */
16708struct addrmap_index_data
16709{
16710 struct objfile *objfile;
16711 struct obstack *addr_obstack;
16712 htab_t cu_index_htab;
16713
16714 /* Non-zero if the previous_* fields are valid.
16715 We can't write an entry until we see the next entry (since it is only then
16716 that we know the end of the entry). */
16717 int previous_valid;
16718 /* Index of the CU in the table of all CUs in the index file. */
16719 unsigned int previous_cu_index;
0963b4bd 16720 /* Start address of the CU. */
0a5429f6
DE
16721 CORE_ADDR previous_cu_start;
16722};
16723
16724/* Write an address entry to OBSTACK. */
b89be57b 16725
9291a0cd 16726static void
0a5429f6
DE
16727add_address_entry (struct objfile *objfile, struct obstack *obstack,
16728 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16729{
0a5429f6 16730 offset_type cu_index_to_write;
9291a0cd
TT
16731 char addr[8];
16732 CORE_ADDR baseaddr;
16733
16734 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16735
0a5429f6
DE
16736 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16737 obstack_grow (obstack, addr, 8);
16738 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16739 obstack_grow (obstack, addr, 8);
16740 cu_index_to_write = MAYBE_SWAP (cu_index);
16741 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16742}
16743
16744/* Worker function for traversing an addrmap to build the address table. */
16745
16746static int
16747add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16748{
16749 struct addrmap_index_data *data = datap;
16750 struct partial_symtab *pst = obj;
16751 offset_type cu_index;
16752 void **slot;
16753
16754 if (data->previous_valid)
16755 add_address_entry (data->objfile, data->addr_obstack,
16756 data->previous_cu_start, start_addr,
16757 data->previous_cu_index);
16758
16759 data->previous_cu_start = start_addr;
16760 if (pst != NULL)
16761 {
16762 struct psymtab_cu_index_map find_map, *map;
16763 find_map.psymtab = pst;
16764 map = htab_find (data->cu_index_htab, &find_map);
16765 gdb_assert (map != NULL);
16766 data->previous_cu_index = map->cu_index;
16767 data->previous_valid = 1;
16768 }
16769 else
16770 data->previous_valid = 0;
16771
16772 return 0;
16773}
16774
16775/* Write OBJFILE's address map to OBSTACK.
16776 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16777 in the index file. */
16778
16779static void
16780write_address_map (struct objfile *objfile, struct obstack *obstack,
16781 htab_t cu_index_htab)
16782{
16783 struct addrmap_index_data addrmap_index_data;
16784
16785 /* When writing the address table, we have to cope with the fact that
16786 the addrmap iterator only provides the start of a region; we have to
16787 wait until the next invocation to get the start of the next region. */
16788
16789 addrmap_index_data.objfile = objfile;
16790 addrmap_index_data.addr_obstack = obstack;
16791 addrmap_index_data.cu_index_htab = cu_index_htab;
16792 addrmap_index_data.previous_valid = 0;
16793
16794 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16795 &addrmap_index_data);
16796
16797 /* It's highly unlikely the last entry (end address = 0xff...ff)
16798 is valid, but we should still handle it.
16799 The end address is recorded as the start of the next region, but that
16800 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16801 anyway. */
16802 if (addrmap_index_data.previous_valid)
16803 add_address_entry (objfile, obstack,
16804 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16805 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16806}
16807
16808/* Add a list of partial symbols to SYMTAB. */
b89be57b 16809
9291a0cd
TT
16810static void
16811write_psymbols (struct mapped_symtab *symtab,
987d643c 16812 htab_t psyms_seen,
9291a0cd
TT
16813 struct partial_symbol **psymp,
16814 int count,
987d643c
TT
16815 offset_type cu_index,
16816 int is_static)
9291a0cd
TT
16817{
16818 for (; count-- > 0; ++psymp)
16819 {
987d643c
TT
16820 void **slot, *lookup;
16821
9291a0cd
TT
16822 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16823 error (_("Ada is not currently supported by the index"));
987d643c
TT
16824
16825 /* We only want to add a given psymbol once. However, we also
16826 want to account for whether it is global or static. So, we
16827 may add it twice, using slightly different values. */
16828 if (is_static)
16829 {
16830 uintptr_t val = 1 | (uintptr_t) *psymp;
16831
16832 lookup = (void *) val;
16833 }
16834 else
16835 lookup = *psymp;
16836
16837 /* Only add a given psymbol once. */
16838 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16839 if (!*slot)
16840 {
16841 *slot = lookup;
16842 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16843 }
9291a0cd
TT
16844 }
16845}
16846
16847/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16848 exception if there is an error. */
b89be57b 16849
9291a0cd
TT
16850static void
16851write_obstack (FILE *file, struct obstack *obstack)
16852{
16853 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16854 file)
16855 != obstack_object_size (obstack))
16856 error (_("couldn't data write to file"));
16857}
16858
16859/* Unlink a file if the argument is not NULL. */
b89be57b 16860
9291a0cd
TT
16861static void
16862unlink_if_set (void *p)
16863{
16864 char **filename = p;
16865 if (*filename)
16866 unlink (*filename);
16867}
16868
1fd400ff
TT
16869/* A helper struct used when iterating over debug_types. */
16870struct signatured_type_index_data
16871{
16872 struct objfile *objfile;
16873 struct mapped_symtab *symtab;
16874 struct obstack *types_list;
987d643c 16875 htab_t psyms_seen;
1fd400ff
TT
16876 int cu_index;
16877};
16878
16879/* A helper function that writes a single signatured_type to an
16880 obstack. */
b89be57b 16881
1fd400ff
TT
16882static int
16883write_one_signatured_type (void **slot, void *d)
16884{
16885 struct signatured_type_index_data *info = d;
16886 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16887 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16888 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16889 gdb_byte val[8];
16890
16891 write_psymbols (info->symtab,
987d643c 16892 info->psyms_seen,
3e43a32a
MS
16893 info->objfile->global_psymbols.list
16894 + psymtab->globals_offset,
987d643c
TT
16895 psymtab->n_global_syms, info->cu_index,
16896 0);
1fd400ff 16897 write_psymbols (info->symtab,
987d643c 16898 info->psyms_seen,
3e43a32a
MS
16899 info->objfile->static_psymbols.list
16900 + psymtab->statics_offset,
987d643c
TT
16901 psymtab->n_static_syms, info->cu_index,
16902 1);
1fd400ff 16903
b3c8eb43 16904 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16905 obstack_grow (info->types_list, val, 8);
16906 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16907 obstack_grow (info->types_list, val, 8);
16908 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16909 obstack_grow (info->types_list, val, 8);
16910
16911 ++info->cu_index;
16912
16913 return 1;
16914}
16915
9291a0cd 16916/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16917
9291a0cd
TT
16918static void
16919write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16920{
16921 struct cleanup *cleanup;
16922 char *filename, *cleanup_filename;
1fd400ff
TT
16923 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16924 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16925 int i;
16926 FILE *out_file;
16927 struct mapped_symtab *symtab;
16928 offset_type val, size_of_contents, total_len;
16929 struct stat st;
16930 char buf[8];
987d643c 16931 htab_t psyms_seen;
0a5429f6
DE
16932 htab_t cu_index_htab;
16933 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16934
b4f2f049 16935 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16936 return;
b4f2f049 16937
9291a0cd
TT
16938 if (dwarf2_per_objfile->using_index)
16939 error (_("Cannot use an index to create the index"));
16940
8b70b953
TT
16941 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16942 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16943
9291a0cd 16944 if (stat (objfile->name, &st) < 0)
7e17e088 16945 perror_with_name (objfile->name);
9291a0cd
TT
16946
16947 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16948 INDEX_SUFFIX, (char *) NULL);
16949 cleanup = make_cleanup (xfree, filename);
16950
16951 out_file = fopen (filename, "wb");
16952 if (!out_file)
16953 error (_("Can't open `%s' for writing"), filename);
16954
16955 cleanup_filename = filename;
16956 make_cleanup (unlink_if_set, &cleanup_filename);
16957
16958 symtab = create_mapped_symtab ();
16959 make_cleanup (cleanup_mapped_symtab, symtab);
16960
16961 obstack_init (&addr_obstack);
16962 make_cleanup_obstack_free (&addr_obstack);
16963
16964 obstack_init (&cu_list);
16965 make_cleanup_obstack_free (&cu_list);
16966
1fd400ff
TT
16967 obstack_init (&types_cu_list);
16968 make_cleanup_obstack_free (&types_cu_list);
16969
987d643c
TT
16970 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16971 NULL, xcalloc, xfree);
96408a79 16972 make_cleanup_htab_delete (psyms_seen);
987d643c 16973
0a5429f6
DE
16974 /* While we're scanning CU's create a table that maps a psymtab pointer
16975 (which is what addrmap records) to its index (which is what is recorded
16976 in the index file). This will later be needed to write the address
16977 table. */
16978 cu_index_htab = htab_create_alloc (100,
16979 hash_psymtab_cu_index,
16980 eq_psymtab_cu_index,
16981 NULL, xcalloc, xfree);
96408a79 16982 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
16983 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16984 xmalloc (sizeof (struct psymtab_cu_index_map)
16985 * dwarf2_per_objfile->n_comp_units);
16986 make_cleanup (xfree, psymtab_cu_index_map);
16987
16988 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16989 work here. Also, the debug_types entries do not appear in
16990 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16991 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16992 {
3e43a32a
MS
16993 struct dwarf2_per_cu_data *per_cu
16994 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16995 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16996 gdb_byte val[8];
0a5429f6
DE
16997 struct psymtab_cu_index_map *map;
16998 void **slot;
9291a0cd
TT
16999
17000 write_psymbols (symtab,
987d643c 17001 psyms_seen,
9291a0cd 17002 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
17003 psymtab->n_global_syms, i,
17004 0);
9291a0cd 17005 write_psymbols (symtab,
987d643c 17006 psyms_seen,
9291a0cd 17007 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
17008 psymtab->n_static_syms, i,
17009 1);
9291a0cd 17010
0a5429f6
DE
17011 map = &psymtab_cu_index_map[i];
17012 map->psymtab = psymtab;
17013 map->cu_index = i;
17014 slot = htab_find_slot (cu_index_htab, map, INSERT);
17015 gdb_assert (slot != NULL);
17016 gdb_assert (*slot == NULL);
17017 *slot = map;
9291a0cd 17018
e254ef6a 17019 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 17020 obstack_grow (&cu_list, val, 8);
e254ef6a 17021 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17022 obstack_grow (&cu_list, val, 8);
17023 }
17024
0a5429f6
DE
17025 /* Dump the address map. */
17026 write_address_map (objfile, &addr_obstack, cu_index_htab);
17027
1fd400ff
TT
17028 /* Write out the .debug_type entries, if any. */
17029 if (dwarf2_per_objfile->signatured_types)
17030 {
17031 struct signatured_type_index_data sig_data;
17032
17033 sig_data.objfile = objfile;
17034 sig_data.symtab = symtab;
17035 sig_data.types_list = &types_cu_list;
987d643c 17036 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17037 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17038 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17039 write_one_signatured_type, &sig_data);
17040 }
17041
9291a0cd
TT
17042 obstack_init (&constant_pool);
17043 make_cleanup_obstack_free (&constant_pool);
17044 obstack_init (&symtab_obstack);
17045 make_cleanup_obstack_free (&symtab_obstack);
17046 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17047
17048 obstack_init (&contents);
17049 make_cleanup_obstack_free (&contents);
1fd400ff 17050 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17051 total_len = size_of_contents;
17052
17053 /* The version number. */
559a7a62 17054 val = MAYBE_SWAP (5);
9291a0cd
TT
17055 obstack_grow (&contents, &val, sizeof (val));
17056
17057 /* The offset of the CU list from the start of the file. */
17058 val = MAYBE_SWAP (total_len);
17059 obstack_grow (&contents, &val, sizeof (val));
17060 total_len += obstack_object_size (&cu_list);
17061
1fd400ff
TT
17062 /* The offset of the types CU list from the start of the file. */
17063 val = MAYBE_SWAP (total_len);
17064 obstack_grow (&contents, &val, sizeof (val));
17065 total_len += obstack_object_size (&types_cu_list);
17066
9291a0cd
TT
17067 /* The offset of the address table from the start of the file. */
17068 val = MAYBE_SWAP (total_len);
17069 obstack_grow (&contents, &val, sizeof (val));
17070 total_len += obstack_object_size (&addr_obstack);
17071
17072 /* The offset of the symbol table from the start of the file. */
17073 val = MAYBE_SWAP (total_len);
17074 obstack_grow (&contents, &val, sizeof (val));
17075 total_len += obstack_object_size (&symtab_obstack);
17076
17077 /* The offset of the constant pool from the start of the file. */
17078 val = MAYBE_SWAP (total_len);
17079 obstack_grow (&contents, &val, sizeof (val));
17080 total_len += obstack_object_size (&constant_pool);
17081
17082 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17083
17084 write_obstack (out_file, &contents);
17085 write_obstack (out_file, &cu_list);
1fd400ff 17086 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17087 write_obstack (out_file, &addr_obstack);
17088 write_obstack (out_file, &symtab_obstack);
17089 write_obstack (out_file, &constant_pool);
17090
17091 fclose (out_file);
17092
17093 /* We want to keep the file, so we set cleanup_filename to NULL
17094 here. See unlink_if_set. */
17095 cleanup_filename = NULL;
17096
17097 do_cleanups (cleanup);
17098}
17099
90476074
TT
17100/* Implementation of the `save gdb-index' command.
17101
17102 Note that the file format used by this command is documented in the
17103 GDB manual. Any changes here must be documented there. */
11570e71 17104
9291a0cd
TT
17105static void
17106save_gdb_index_command (char *arg, int from_tty)
17107{
17108 struct objfile *objfile;
17109
17110 if (!arg || !*arg)
96d19272 17111 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17112
17113 ALL_OBJFILES (objfile)
17114 {
17115 struct stat st;
17116
17117 /* If the objfile does not correspond to an actual file, skip it. */
17118 if (stat (objfile->name, &st) < 0)
17119 continue;
17120
17121 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17122 if (dwarf2_per_objfile)
17123 {
17124 volatile struct gdb_exception except;
17125
17126 TRY_CATCH (except, RETURN_MASK_ERROR)
17127 {
17128 write_psymtabs_to_index (objfile, arg);
17129 }
17130 if (except.reason < 0)
17131 exception_fprintf (gdb_stderr, except,
17132 _("Error while writing index for `%s': "),
17133 objfile->name);
17134 }
17135 }
dce234bc
PP
17136}
17137
9291a0cd
TT
17138\f
17139
9eae7c52
TT
17140int dwarf2_always_disassemble;
17141
17142static void
17143show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17144 struct cmd_list_element *c, const char *value)
17145{
3e43a32a
MS
17146 fprintf_filtered (file,
17147 _("Whether to always disassemble "
17148 "DWARF expressions is %s.\n"),
9eae7c52
TT
17149 value);
17150}
17151
900e11f9
JK
17152static void
17153show_check_physname (struct ui_file *file, int from_tty,
17154 struct cmd_list_element *c, const char *value)
17155{
17156 fprintf_filtered (file,
17157 _("Whether to check \"physname\" is %s.\n"),
17158 value);
17159}
17160
6502dd73
DJ
17161void _initialize_dwarf2_read (void);
17162
17163void
17164_initialize_dwarf2_read (void)
17165{
96d19272
JK
17166 struct cmd_list_element *c;
17167
dce234bc 17168 dwarf2_objfile_data_key
c1bd65d0 17169 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17170
1bedd215
AC
17171 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17172Set DWARF 2 specific variables.\n\
17173Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17174 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17175 0/*allow-unknown*/, &maintenance_set_cmdlist);
17176
1bedd215
AC
17177 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17178Show DWARF 2 specific variables\n\
17179Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17180 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17181 0/*allow-unknown*/, &maintenance_show_cmdlist);
17182
17183 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17184 &dwarf2_max_cache_age, _("\
17185Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17186Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17187A higher limit means that cached compilation units will be stored\n\
17188in memory longer, and more total memory will be used. Zero disables\n\
17189caching, which can slow down startup."),
2c5b56ce 17190 NULL,
920d2a44 17191 show_dwarf2_max_cache_age,
2c5b56ce 17192 &set_dwarf2_cmdlist,
ae038cb0 17193 &show_dwarf2_cmdlist);
d97bc12b 17194
9eae7c52
TT
17195 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17196 &dwarf2_always_disassemble, _("\
17197Set whether `info address' always disassembles DWARF expressions."), _("\
17198Show whether `info address' always disassembles DWARF expressions."), _("\
17199When enabled, DWARF expressions are always printed in an assembly-like\n\
17200syntax. When disabled, expressions will be printed in a more\n\
17201conversational style, when possible."),
17202 NULL,
17203 show_dwarf2_always_disassemble,
17204 &set_dwarf2_cmdlist,
17205 &show_dwarf2_cmdlist);
17206
d97bc12b
DE
17207 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17208Set debugging of the dwarf2 DIE reader."), _("\
17209Show debugging of the dwarf2 DIE reader."), _("\
17210When enabled (non-zero), DIEs are dumped after they are read in.\n\
17211The value is the maximum depth to print."),
17212 NULL,
17213 NULL,
17214 &setdebuglist, &showdebuglist);
9291a0cd 17215
900e11f9
JK
17216 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17217Set cross-checking of \"physname\" code against demangler."), _("\
17218Show cross-checking of \"physname\" code against demangler."), _("\
17219When enabled, GDB's internal \"physname\" code is checked against\n\
17220the demangler."),
17221 NULL, show_check_physname,
17222 &setdebuglist, &showdebuglist);
17223
96d19272 17224 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17225 _("\
fc1a9d6e 17226Save a gdb-index file.\n\
11570e71 17227Usage: save gdb-index DIRECTORY"),
96d19272
JK
17228 &save_cmdlist);
17229 set_cmd_completer (c, filename_completer);
6502dd73 17230}
This page took 4.351149 seconds and 4 git commands to generate.