PR ld/13049
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
6aba47ca 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
7b6bb8da 4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
0fb0cc75 5 Free Software Foundation, Inc.
c906108c
SS
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 12 support.
c906108c 13
c5aa993b 14 This file is part of GDB.
c906108c 15
c5aa993b
JM
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
a9762ec7
JB
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
c906108c 20
a9762ec7
JB
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
c906108c 25
c5aa993b 26 You should have received a copy of the GNU General Public License
a9762ec7 27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
28
29#include "defs.h"
30#include "bfd.h"
c906108c
SS
31#include "symtab.h"
32#include "gdbtypes.h"
c906108c 33#include "objfiles.h"
fa8f86ff 34#include "dwarf2.h"
c906108c
SS
35#include "buildsym.h"
36#include "demangle.h"
37#include "expression.h"
d5166ae1 38#include "filenames.h" /* for DOSish file names */
2e276125 39#include "macrotab.h"
c906108c
SS
40#include "language.h"
41#include "complaints.h"
357e46e7 42#include "bcache.h"
4c2df51b
DJ
43#include "dwarf2expr.h"
44#include "dwarf2loc.h"
9219021c 45#include "cp-support.h"
72bf9492 46#include "hashtab.h"
ae038cb0
DJ
47#include "command.h"
48#include "gdbcmd.h"
edb3359d 49#include "block.h"
ff013f42 50#include "addrmap.h"
94af9270
KS
51#include "typeprint.h"
52#include "jv-lang.h"
ccefe4c4 53#include "psympriv.h"
9291a0cd
TT
54#include "exceptions.h"
55#include "gdb_stat.h"
96d19272 56#include "completer.h"
34eaf542 57#include "vec.h"
98bfdba5
PA
58#include "c-lang.h"
59#include "valprint.h"
60d5a603 60#include <ctype.h>
4c2df51b 61
c906108c
SS
62#include <fcntl.h>
63#include "gdb_string.h"
4bdf3d34 64#include "gdb_assert.h"
c906108c 65#include <sys/types.h>
233a11ab
CS
66#ifdef HAVE_ZLIB_H
67#include <zlib.h>
68#endif
dce234bc
PP
69#ifdef HAVE_MMAP
70#include <sys/mman.h>
85d9bd0e
TT
71#ifndef MAP_FAILED
72#define MAP_FAILED ((void *) -1)
73#endif
dce234bc 74#endif
d8151005 75
34eaf542
TT
76typedef struct symbol *symbolp;
77DEF_VEC_P (symbolp);
78
107d2387 79#if 0
357e46e7 80/* .debug_info header for a compilation unit
c906108c
SS
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83typedef struct comp_unit_header
84 {
85 unsigned int length; /* length of the .debug_info
86 contribution */
87 unsigned short version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90 unsigned char addr_size; /* byte size of an address -- 4 */
91 }
92_COMP_UNIT_HEADER;
93#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 94#endif
c906108c 95
c906108c
SS
96/* .debug_line statement program prologue
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99typedef struct statement_prologue
100 {
101 unsigned int total_length; /* byte length of the statement
102 information */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int prologue_length; /* # bytes between prologue &
106 stmt program */
107 unsigned char minimum_instruction_length; /* byte size of
108 smallest instr */
109 unsigned char default_is_stmt; /* initial value of is_stmt
110 register */
111 char line_base;
112 unsigned char line_range;
113 unsigned char opcode_base; /* number assigned to first special
114 opcode */
115 unsigned char *standard_opcode_lengths;
116 }
117_STATEMENT_PROLOGUE;
118
d97bc12b
DE
119/* When non-zero, dump DIEs after they are read in. */
120static int dwarf2_die_debug = 0;
121
900e11f9
JK
122/* When non-zero, cross-check physname against demangler. */
123static int check_physname = 0;
124
dce234bc
PP
125static int pagesize;
126
df8a16a1
DJ
127/* When set, the file that we're processing is known to have debugging
128 info for C++ namespaces. GCC 3.3.x did not produce this information,
129 but later versions do. */
130
131static int processing_has_namespace_info;
132
6502dd73
DJ
133static const struct objfile_data *dwarf2_objfile_data_key;
134
dce234bc
PP
135struct dwarf2_section_info
136{
137 asection *asection;
138 gdb_byte *buffer;
139 bfd_size_type size;
b315ab21
TG
140 /* Not NULL if the section was actually mmapped. */
141 void *map_addr;
142 /* Page aligned size of mmapped area. */
143 bfd_size_type map_len;
be391dca
TT
144 /* True if we have tried to read this section. */
145 int readin;
dce234bc
PP
146};
147
8b70b953
TT
148typedef struct dwarf2_section_info dwarf2_section_info_def;
149DEF_VEC_O (dwarf2_section_info_def);
150
9291a0cd
TT
151/* All offsets in the index are of this type. It must be
152 architecture-independent. */
153typedef uint32_t offset_type;
154
155DEF_VEC_I (offset_type);
156
157/* A description of the mapped index. The file format is described in
158 a comment by the code that writes the index. */
159struct mapped_index
160{
559a7a62
JK
161 /* Index data format version. */
162 int version;
163
9291a0cd
TT
164 /* The total length of the buffer. */
165 off_t total_size;
b11b1f88 166
9291a0cd
TT
167 /* A pointer to the address table data. */
168 const gdb_byte *address_table;
b11b1f88 169
9291a0cd
TT
170 /* Size of the address table data in bytes. */
171 offset_type address_table_size;
b11b1f88 172
3876f04e
DE
173 /* The symbol table, implemented as a hash table. */
174 const offset_type *symbol_table;
b11b1f88 175
9291a0cd 176 /* Size in slots, each slot is 2 offset_types. */
3876f04e 177 offset_type symbol_table_slots;
b11b1f88 178
9291a0cd
TT
179 /* A pointer to the constant pool. */
180 const char *constant_pool;
181};
182
6502dd73
DJ
183struct dwarf2_per_objfile
184{
dce234bc
PP
185 struct dwarf2_section_info info;
186 struct dwarf2_section_info abbrev;
187 struct dwarf2_section_info line;
dce234bc
PP
188 struct dwarf2_section_info loc;
189 struct dwarf2_section_info macinfo;
cf2c3c16 190 struct dwarf2_section_info macro;
dce234bc
PP
191 struct dwarf2_section_info str;
192 struct dwarf2_section_info ranges;
193 struct dwarf2_section_info frame;
194 struct dwarf2_section_info eh_frame;
9291a0cd 195 struct dwarf2_section_info gdb_index;
ae038cb0 196
8b70b953
TT
197 VEC (dwarf2_section_info_def) *types;
198
be391dca
TT
199 /* Back link. */
200 struct objfile *objfile;
201
10b3939b
DJ
202 /* A list of all the compilation units. This is used to locate
203 the target compilation unit of a particular reference. */
ae038cb0
DJ
204 struct dwarf2_per_cu_data **all_comp_units;
205
206 /* The number of compilation units in ALL_COMP_UNITS. */
207 int n_comp_units;
208
1fd400ff
TT
209 /* The number of .debug_types-related CUs. */
210 int n_type_comp_units;
211
212 /* The .debug_types-related CUs. */
213 struct dwarf2_per_cu_data **type_comp_units;
214
ae038cb0
DJ
215 /* A chain of compilation units that are currently read in, so that
216 they can be freed later. */
217 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 218
348e048f
DE
219 /* A table mapping .debug_types signatures to its signatured_type entry.
220 This is NULL if the .debug_types section hasn't been read in yet. */
221 htab_t signatured_types;
222
72dca2f5
FR
223 /* A flag indicating wether this objfile has a section loaded at a
224 VMA of 0. */
225 int has_section_at_zero;
9291a0cd 226
ae2de4f8
DE
227 /* True if we are using the mapped index,
228 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
229 unsigned char using_index;
230
ae2de4f8 231 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 232 struct mapped_index *index_table;
98bfdba5 233
7b9f3c50
DE
234 /* When using index_table, this keeps track of all quick_file_names entries.
235 TUs can share line table entries with CUs or other TUs, and there can be
236 a lot more TUs than unique line tables, so we maintain a separate table
237 of all line table entries to support the sharing. */
238 htab_t quick_file_names_table;
239
98bfdba5
PA
240 /* Set during partial symbol reading, to prevent queueing of full
241 symbols. */
242 int reading_partial_symbols;
673bfd45
DE
243
244 /* Table mapping type .debug_info DIE offsets to types.
245 This is NULL if not allocated yet.
246 It (currently) makes sense to allocate debug_types_type_hash lazily.
247 To keep things simple we allocate both lazily. */
248 htab_t debug_info_type_hash;
249
250 /* Table mapping type .debug_types DIE offsets to types.
251 This is NULL if not allocated yet. */
252 htab_t debug_types_type_hash;
6502dd73
DJ
253};
254
255static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 256
251d32d9 257/* Default names of the debugging sections. */
c906108c 258
233a11ab
CS
259/* Note that if the debugging section has been compressed, it might
260 have a name like .zdebug_info. */
261
251d32d9
TG
262static const struct dwarf2_debug_sections dwarf2_elf_names = {
263 { ".debug_info", ".zdebug_info" },
264 { ".debug_abbrev", ".zdebug_abbrev" },
265 { ".debug_line", ".zdebug_line" },
266 { ".debug_loc", ".zdebug_loc" },
267 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 268 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
269 { ".debug_str", ".zdebug_str" },
270 { ".debug_ranges", ".zdebug_ranges" },
271 { ".debug_types", ".zdebug_types" },
272 { ".debug_frame", ".zdebug_frame" },
273 { ".eh_frame", NULL },
24d3216f
TT
274 { ".gdb_index", ".zgdb_index" },
275 23
251d32d9 276};
c906108c
SS
277
278/* local data types */
279
0963b4bd 280/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
281#ifndef ABBREV_HASH_SIZE
282#define ABBREV_HASH_SIZE 121
283#endif
284
107d2387
AC
285/* The data in a compilation unit header, after target2host
286 translation, looks like this. */
c906108c 287struct comp_unit_head
a738430d 288{
c764a876 289 unsigned int length;
a738430d 290 short version;
a738430d
MK
291 unsigned char addr_size;
292 unsigned char signed_addr_p;
9cbfa09e 293 unsigned int abbrev_offset;
57349743 294
a738430d
MK
295 /* Size of file offsets; either 4 or 8. */
296 unsigned int offset_size;
57349743 297
a738430d
MK
298 /* Size of the length field; either 4 or 12. */
299 unsigned int initial_length_size;
57349743 300
a738430d
MK
301 /* Offset to the first byte of this compilation unit header in the
302 .debug_info section, for resolving relative reference dies. */
303 unsigned int offset;
57349743 304
d00adf39
DE
305 /* Offset to first die in this cu from the start of the cu.
306 This will be the first byte following the compilation unit header. */
307 unsigned int first_die_offset;
a738430d 308};
c906108c 309
3da10d80
KS
310/* Type used for delaying computation of method physnames.
311 See comments for compute_delayed_physnames. */
312struct delayed_method_info
313{
314 /* The type to which the method is attached, i.e., its parent class. */
315 struct type *type;
316
317 /* The index of the method in the type's function fieldlists. */
318 int fnfield_index;
319
320 /* The index of the method in the fieldlist. */
321 int index;
322
323 /* The name of the DIE. */
324 const char *name;
325
326 /* The DIE associated with this method. */
327 struct die_info *die;
328};
329
330typedef struct delayed_method_info delayed_method_info;
331DEF_VEC_O (delayed_method_info);
332
e7c27a73
DJ
333/* Internal state when decoding a particular compilation unit. */
334struct dwarf2_cu
335{
336 /* The objfile containing this compilation unit. */
337 struct objfile *objfile;
338
d00adf39 339 /* The header of the compilation unit. */
e7c27a73 340 struct comp_unit_head header;
e142c38c 341
d00adf39
DE
342 /* Base address of this compilation unit. */
343 CORE_ADDR base_address;
344
345 /* Non-zero if base_address has been set. */
346 int base_known;
347
e142c38c
DJ
348 struct function_range *first_fn, *last_fn, *cached_fn;
349
350 /* The language we are debugging. */
351 enum language language;
352 const struct language_defn *language_defn;
353
b0f35d58
DL
354 const char *producer;
355
e142c38c
DJ
356 /* The generic symbol table building routines have separate lists for
357 file scope symbols and all all other scopes (local scopes). So
358 we need to select the right one to pass to add_symbol_to_list().
359 We do it by keeping a pointer to the correct list in list_in_scope.
360
361 FIXME: The original dwarf code just treated the file scope as the
362 first local scope, and all other local scopes as nested local
363 scopes, and worked fine. Check to see if we really need to
364 distinguish these in buildsym.c. */
365 struct pending **list_in_scope;
366
f3dd6933
DJ
367 /* DWARF abbreviation table associated with this compilation unit. */
368 struct abbrev_info **dwarf2_abbrevs;
369
370 /* Storage for the abbrev table. */
371 struct obstack abbrev_obstack;
72bf9492
DJ
372
373 /* Hash table holding all the loaded partial DIEs. */
374 htab_t partial_dies;
375
376 /* Storage for things with the same lifetime as this read-in compilation
377 unit, including partial DIEs. */
378 struct obstack comp_unit_obstack;
379
ae038cb0
DJ
380 /* When multiple dwarf2_cu structures are living in memory, this field
381 chains them all together, so that they can be released efficiently.
382 We will probably also want a generation counter so that most-recently-used
383 compilation units are cached... */
384 struct dwarf2_per_cu_data *read_in_chain;
385
386 /* Backchain to our per_cu entry if the tree has been built. */
387 struct dwarf2_per_cu_data *per_cu;
388
389 /* How many compilation units ago was this CU last referenced? */
390 int last_used;
391
10b3939b 392 /* A hash table of die offsets for following references. */
51545339 393 htab_t die_hash;
10b3939b
DJ
394
395 /* Full DIEs if read in. */
396 struct die_info *dies;
397
398 /* A set of pointers to dwarf2_per_cu_data objects for compilation
399 units referenced by this one. Only set during full symbol processing;
400 partial symbol tables do not have dependencies. */
401 htab_t dependencies;
402
cb1df416
DJ
403 /* Header data from the line table, during full symbol processing. */
404 struct line_header *line_header;
405
3da10d80
KS
406 /* A list of methods which need to have physnames computed
407 after all type information has been read. */
408 VEC (delayed_method_info) *method_list;
409
96408a79
SA
410 /* To be copied to symtab->call_site_htab. */
411 htab_t call_site_htab;
412
ae038cb0
DJ
413 /* Mark used when releasing cached dies. */
414 unsigned int mark : 1;
415
416 /* This flag will be set if this compilation unit might include
417 inter-compilation-unit references. */
418 unsigned int has_form_ref_addr : 1;
419
72bf9492
DJ
420 /* This flag will be set if this compilation unit includes any
421 DW_TAG_namespace DIEs. If we know that there are explicit
422 DIEs for namespaces, we don't need to try to infer them
423 from mangled names. */
424 unsigned int has_namespace_info : 1;
8be455d7
JK
425
426 /* This CU references .debug_loc. See the symtab->locations_valid field.
427 This test is imperfect as there may exist optimized debug code not using
428 any location list and still facing inlining issues if handled as
429 unoptimized code. For a future better test see GCC PR other/32998. */
430
431 unsigned int has_loclist : 1;
e7c27a73
DJ
432};
433
10b3939b
DJ
434/* Persistent data held for a compilation unit, even when not
435 processing it. We put a pointer to this structure in the
436 read_symtab_private field of the psymtab. If we encounter
437 inter-compilation-unit references, we also maintain a sorted
438 list of all compilation units. */
439
ae038cb0
DJ
440struct dwarf2_per_cu_data
441{
348e048f 442 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 443 bytes should suffice to store the length of any compilation unit
45452591
DE
444 - if it doesn't, GDB will fall over anyway.
445 NOTE: Unlike comp_unit_head.length, this length includes
446 initial_length_size. */
c764a876 447 unsigned int offset;
348e048f 448 unsigned int length : 29;
ae038cb0
DJ
449
450 /* Flag indicating this compilation unit will be read in before
451 any of the current compilation units are processed. */
c764a876 452 unsigned int queued : 1;
ae038cb0 453
5afb4e99
DJ
454 /* This flag will be set if we need to load absolutely all DIEs
455 for this compilation unit, instead of just the ones we think
456 are interesting. It gets set if we look for a DIE in the
457 hash table and don't find it. */
458 unsigned int load_all_dies : 1;
459
8b70b953
TT
460 /* Non-null if this CU is from .debug_types; in which case it points
461 to the section. Otherwise it's from .debug_info. */
462 struct dwarf2_section_info *debug_type_section;
348e048f 463
17ea53c3
JK
464 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
465 of the CU cache it gets reset to NULL again. */
ae038cb0 466 struct dwarf2_cu *cu;
1c379e20 467
9291a0cd
TT
468 /* The corresponding objfile. */
469 struct objfile *objfile;
470
471 /* When using partial symbol tables, the 'psymtab' field is active.
472 Otherwise the 'quick' field is active. */
473 union
474 {
475 /* The partial symbol table associated with this compilation unit,
476 or NULL for partial units (which do not have an associated
477 symtab). */
478 struct partial_symtab *psymtab;
479
480 /* Data needed by the "quick" functions. */
481 struct dwarf2_per_cu_quick_data *quick;
482 } v;
ae038cb0
DJ
483};
484
348e048f
DE
485/* Entry in the signatured_types hash table. */
486
487struct signatured_type
488{
489 ULONGEST signature;
490
348e048f
DE
491 /* Offset in .debug_types of the type defined by this TU. */
492 unsigned int type_offset;
493
494 /* The CU(/TU) of this type. */
495 struct dwarf2_per_cu_data per_cu;
496};
497
0963b4bd
MS
498/* Struct used to pass misc. parameters to read_die_and_children, et
499 al. which are used for both .debug_info and .debug_types dies.
500 All parameters here are unchanging for the life of the call. This
501 struct exists to abstract away the constant parameters of die
502 reading. */
93311388
DE
503
504struct die_reader_specs
505{
506 /* The bfd of this objfile. */
507 bfd* abfd;
508
509 /* The CU of the DIE we are parsing. */
510 struct dwarf2_cu *cu;
511
512 /* Pointer to start of section buffer.
513 This is either the start of .debug_info or .debug_types. */
514 const gdb_byte *buffer;
515};
516
debd256d
JB
517/* The line number information for a compilation unit (found in the
518 .debug_line section) begins with a "statement program header",
519 which contains the following information. */
520struct line_header
521{
522 unsigned int total_length;
523 unsigned short version;
524 unsigned int header_length;
525 unsigned char minimum_instruction_length;
2dc7f7b3 526 unsigned char maximum_ops_per_instruction;
debd256d
JB
527 unsigned char default_is_stmt;
528 int line_base;
529 unsigned char line_range;
530 unsigned char opcode_base;
531
532 /* standard_opcode_lengths[i] is the number of operands for the
533 standard opcode whose value is i. This means that
534 standard_opcode_lengths[0] is unused, and the last meaningful
535 element is standard_opcode_lengths[opcode_base - 1]. */
536 unsigned char *standard_opcode_lengths;
537
538 /* The include_directories table. NOTE! These strings are not
539 allocated with xmalloc; instead, they are pointers into
540 debug_line_buffer. If you try to free them, `free' will get
541 indigestion. */
542 unsigned int num_include_dirs, include_dirs_size;
543 char **include_dirs;
544
545 /* The file_names table. NOTE! These strings are not allocated
546 with xmalloc; instead, they are pointers into debug_line_buffer.
547 Don't try to free them directly. */
548 unsigned int num_file_names, file_names_size;
549 struct file_entry
c906108c 550 {
debd256d
JB
551 char *name;
552 unsigned int dir_index;
553 unsigned int mod_time;
554 unsigned int length;
aaa75496 555 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 556 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
557 } *file_names;
558
559 /* The start and end of the statement program following this
6502dd73 560 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 561 gdb_byte *statement_program_start, *statement_program_end;
debd256d 562};
c906108c
SS
563
564/* When we construct a partial symbol table entry we only
0963b4bd 565 need this much information. */
c906108c
SS
566struct partial_die_info
567 {
72bf9492 568 /* Offset of this DIE. */
c906108c 569 unsigned int offset;
72bf9492
DJ
570
571 /* DWARF-2 tag for this DIE. */
572 ENUM_BITFIELD(dwarf_tag) tag : 16;
573
72bf9492
DJ
574 /* Assorted flags describing the data found in this DIE. */
575 unsigned int has_children : 1;
576 unsigned int is_external : 1;
577 unsigned int is_declaration : 1;
578 unsigned int has_type : 1;
579 unsigned int has_specification : 1;
580 unsigned int has_pc_info : 1;
581
582 /* Flag set if the SCOPE field of this structure has been
583 computed. */
584 unsigned int scope_set : 1;
585
fa4028e9
JB
586 /* Flag set if the DIE has a byte_size attribute. */
587 unsigned int has_byte_size : 1;
588
98bfdba5
PA
589 /* Flag set if any of the DIE's children are template arguments. */
590 unsigned int has_template_arguments : 1;
591
abc72ce4
DE
592 /* Flag set if fixup_partial_die has been called on this die. */
593 unsigned int fixup_called : 1;
594
72bf9492 595 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 596 sometimes a default name for unnamed DIEs. */
c906108c 597 char *name;
72bf9492 598
abc72ce4
DE
599 /* The linkage name, if present. */
600 const char *linkage_name;
601
72bf9492
DJ
602 /* The scope to prepend to our children. This is generally
603 allocated on the comp_unit_obstack, so will disappear
604 when this compilation unit leaves the cache. */
605 char *scope;
606
607 /* The location description associated with this DIE, if any. */
608 struct dwarf_block *locdesc;
609
610 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
611 CORE_ADDR lowpc;
612 CORE_ADDR highpc;
72bf9492 613
93311388 614 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 615 DW_AT_sibling, if any. */
abc72ce4
DE
616 /* NOTE: This member isn't strictly necessary, read_partial_die could
617 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 618 gdb_byte *sibling;
72bf9492
DJ
619
620 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
621 DW_AT_specification (or DW_AT_abstract_origin or
622 DW_AT_extension). */
623 unsigned int spec_offset;
624
625 /* Pointers to this DIE's parent, first child, and next sibling,
626 if any. */
627 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
628 };
629
0963b4bd 630/* This data structure holds the information of an abbrev. */
c906108c
SS
631struct abbrev_info
632 {
633 unsigned int number; /* number identifying abbrev */
634 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
635 unsigned short has_children; /* boolean */
636 unsigned short num_attrs; /* number of attributes */
c906108c
SS
637 struct attr_abbrev *attrs; /* an array of attribute descriptions */
638 struct abbrev_info *next; /* next in chain */
639 };
640
641struct attr_abbrev
642 {
9d25dd43
DE
643 ENUM_BITFIELD(dwarf_attribute) name : 16;
644 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
645 };
646
0963b4bd 647/* Attributes have a name and a value. */
b60c80d6
DJ
648struct attribute
649 {
9d25dd43 650 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
651 ENUM_BITFIELD(dwarf_form) form : 15;
652
653 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
654 field should be in u.str (existing only for DW_STRING) but it is kept
655 here for better struct attribute alignment. */
656 unsigned int string_is_canonical : 1;
657
b60c80d6
DJ
658 union
659 {
660 char *str;
661 struct dwarf_block *blk;
43bbcdc2
PH
662 ULONGEST unsnd;
663 LONGEST snd;
b60c80d6 664 CORE_ADDR addr;
348e048f 665 struct signatured_type *signatured_type;
b60c80d6
DJ
666 }
667 u;
668 };
669
0963b4bd 670/* This data structure holds a complete die structure. */
c906108c
SS
671struct die_info
672 {
76815b17
DE
673 /* DWARF-2 tag for this DIE. */
674 ENUM_BITFIELD(dwarf_tag) tag : 16;
675
676 /* Number of attributes */
98bfdba5
PA
677 unsigned char num_attrs;
678
679 /* True if we're presently building the full type name for the
680 type derived from this DIE. */
681 unsigned char building_fullname : 1;
76815b17
DE
682
683 /* Abbrev number */
684 unsigned int abbrev;
685
93311388 686 /* Offset in .debug_info or .debug_types section. */
76815b17 687 unsigned int offset;
78ba4af6
JB
688
689 /* The dies in a compilation unit form an n-ary tree. PARENT
690 points to this die's parent; CHILD points to the first child of
691 this node; and all the children of a given node are chained
4950bc1c 692 together via their SIBLING fields. */
639d11d3
DC
693 struct die_info *child; /* Its first child, if any. */
694 struct die_info *sibling; /* Its next sibling, if any. */
695 struct die_info *parent; /* Its parent, if any. */
c906108c 696
b60c80d6
DJ
697 /* An array of attributes, with NUM_ATTRS elements. There may be
698 zero, but it's not common and zero-sized arrays are not
699 sufficiently portable C. */
700 struct attribute attrs[1];
c906108c
SS
701 };
702
5fb290d7
DJ
703struct function_range
704{
705 const char *name;
706 CORE_ADDR lowpc, highpc;
707 int seen_line;
708 struct function_range *next;
709};
710
0963b4bd 711/* Get at parts of an attribute structure. */
c906108c
SS
712
713#define DW_STRING(attr) ((attr)->u.str)
8285870a 714#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
715#define DW_UNSND(attr) ((attr)->u.unsnd)
716#define DW_BLOCK(attr) ((attr)->u.blk)
717#define DW_SND(attr) ((attr)->u.snd)
718#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 719#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 720
0963b4bd 721/* Blocks are a bunch of untyped bytes. */
c906108c
SS
722struct dwarf_block
723 {
724 unsigned int size;
1d6edc3c
JK
725
726 /* Valid only if SIZE is not zero. */
fe1b8b76 727 gdb_byte *data;
c906108c
SS
728 };
729
c906108c
SS
730#ifndef ATTR_ALLOC_CHUNK
731#define ATTR_ALLOC_CHUNK 4
732#endif
733
c906108c
SS
734/* Allocate fields for structs, unions and enums in this size. */
735#ifndef DW_FIELD_ALLOC_CHUNK
736#define DW_FIELD_ALLOC_CHUNK 4
737#endif
738
c906108c
SS
739/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
740 but this would require a corresponding change in unpack_field_as_long
741 and friends. */
742static int bits_per_byte = 8;
743
744/* The routines that read and process dies for a C struct or C++ class
745 pass lists of data member fields and lists of member function fields
746 in an instance of a field_info structure, as defined below. */
747struct field_info
c5aa993b 748 {
0963b4bd 749 /* List of data member and baseclasses fields. */
c5aa993b
JM
750 struct nextfield
751 {
752 struct nextfield *next;
753 int accessibility;
754 int virtuality;
755 struct field field;
756 }
7d0ccb61 757 *fields, *baseclasses;
c906108c 758
7d0ccb61 759 /* Number of fields (including baseclasses). */
c5aa993b 760 int nfields;
c906108c 761
c5aa993b
JM
762 /* Number of baseclasses. */
763 int nbaseclasses;
c906108c 764
c5aa993b
JM
765 /* Set if the accesibility of one of the fields is not public. */
766 int non_public_fields;
c906108c 767
c5aa993b
JM
768 /* Member function fields array, entries are allocated in the order they
769 are encountered in the object file. */
770 struct nextfnfield
771 {
772 struct nextfnfield *next;
773 struct fn_field fnfield;
774 }
775 *fnfields;
c906108c 776
c5aa993b
JM
777 /* Member function fieldlist array, contains name of possibly overloaded
778 member function, number of overloaded member functions and a pointer
779 to the head of the member function field chain. */
780 struct fnfieldlist
781 {
782 char *name;
783 int length;
784 struct nextfnfield *head;
785 }
786 *fnfieldlists;
c906108c 787
c5aa993b
JM
788 /* Number of entries in the fnfieldlists array. */
789 int nfnfields;
98751a41
JK
790
791 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
792 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
793 struct typedef_field_list
794 {
795 struct typedef_field field;
796 struct typedef_field_list *next;
797 }
798 *typedef_field_list;
799 unsigned typedef_field_list_count;
c5aa993b 800 };
c906108c 801
10b3939b
DJ
802/* One item on the queue of compilation units to read in full symbols
803 for. */
804struct dwarf2_queue_item
805{
806 struct dwarf2_per_cu_data *per_cu;
807 struct dwarf2_queue_item *next;
808};
809
810/* The current queue. */
811static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
812
ae038cb0
DJ
813/* Loaded secondary compilation units are kept in memory until they
814 have not been referenced for the processing of this many
815 compilation units. Set this to zero to disable caching. Cache
816 sizes of up to at least twenty will improve startup time for
817 typical inter-CU-reference binaries, at an obvious memory cost. */
818static int dwarf2_max_cache_age = 5;
920d2a44
AC
819static void
820show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
821 struct cmd_list_element *c, const char *value)
822{
3e43a32a
MS
823 fprintf_filtered (file, _("The upper bound on the age of cached "
824 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
825 value);
826}
827
ae038cb0 828
0963b4bd 829/* Various complaints about symbol reading that don't abort the process. */
c906108c 830
4d3c2250
KB
831static void
832dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 833{
4d3c2250 834 complaint (&symfile_complaints,
e2e0b3e5 835 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
836}
837
25e43795
DJ
838static void
839dwarf2_debug_line_missing_file_complaint (void)
840{
841 complaint (&symfile_complaints,
842 _(".debug_line section has line data without a file"));
843}
844
59205f5a
JB
845static void
846dwarf2_debug_line_missing_end_sequence_complaint (void)
847{
848 complaint (&symfile_complaints,
3e43a32a
MS
849 _(".debug_line section has line "
850 "program sequence without an end"));
59205f5a
JB
851}
852
4d3c2250
KB
853static void
854dwarf2_complex_location_expr_complaint (void)
2e276125 855{
e2e0b3e5 856 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
857}
858
4d3c2250
KB
859static void
860dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
861 int arg3)
2e276125 862{
4d3c2250 863 complaint (&symfile_complaints,
3e43a32a
MS
864 _("const value length mismatch for '%s', got %d, expected %d"),
865 arg1, arg2, arg3);
4d3c2250
KB
866}
867
868static void
cf2c3c16 869dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 870{
4d3c2250 871 complaint (&symfile_complaints,
cf2c3c16
TT
872 _("macro info runs off end of `%s' section"),
873 section->asection->name);
4d3c2250
KB
874}
875
876static void
877dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 878{
4d3c2250 879 complaint (&symfile_complaints,
3e43a32a
MS
880 _("macro debug info contains a "
881 "malformed macro definition:\n`%s'"),
4d3c2250
KB
882 arg1);
883}
884
885static void
886dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 887{
4d3c2250 888 complaint (&symfile_complaints,
3e43a32a
MS
889 _("invalid attribute class or form for '%s' in '%s'"),
890 arg1, arg2);
4d3c2250 891}
c906108c 892
c906108c
SS
893/* local function prototypes */
894
4efb68b1 895static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 896
aaa75496
JB
897static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
898 struct objfile *);
899
918dd910
JK
900static void dwarf2_find_base_address (struct die_info *die,
901 struct dwarf2_cu *cu);
902
c67a9c90 903static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 904
72bf9492
DJ
905static void scan_partial_symbols (struct partial_die_info *,
906 CORE_ADDR *, CORE_ADDR *,
5734ee8b 907 int, struct dwarf2_cu *);
c906108c 908
72bf9492
DJ
909static void add_partial_symbol (struct partial_die_info *,
910 struct dwarf2_cu *);
63d06c5c 911
72bf9492
DJ
912static void add_partial_namespace (struct partial_die_info *pdi,
913 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 914 int need_pc, struct dwarf2_cu *cu);
63d06c5c 915
5d7cb8df
JK
916static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
917 CORE_ADDR *highpc, int need_pc,
918 struct dwarf2_cu *cu);
919
72bf9492
DJ
920static void add_partial_enumeration (struct partial_die_info *enum_pdi,
921 struct dwarf2_cu *cu);
91c24f0a 922
bc30ff58
JB
923static void add_partial_subprogram (struct partial_die_info *pdi,
924 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 925 int need_pc, struct dwarf2_cu *cu);
bc30ff58 926
fe1b8b76 927static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
928 gdb_byte *buffer, gdb_byte *info_ptr,
929 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 930
a14ed312 931static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 932
a14ed312 933static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 934
e7c27a73 935static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 936
f3dd6933 937static void dwarf2_free_abbrev_table (void *);
c906108c 938
6caca83c
CC
939static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
940
fe1b8b76 941static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 942 struct dwarf2_cu *);
72bf9492 943
57349743 944static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 945 struct dwarf2_cu *);
c906108c 946
93311388
DE
947static struct partial_die_info *load_partial_dies (bfd *,
948 gdb_byte *, gdb_byte *,
949 int, struct dwarf2_cu *);
72bf9492 950
fe1b8b76 951static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
952 struct abbrev_info *abbrev,
953 unsigned int, bfd *,
954 gdb_byte *, gdb_byte *,
955 struct dwarf2_cu *);
c906108c 956
c764a876 957static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 958 struct dwarf2_cu *);
72bf9492
DJ
959
960static void fixup_partial_die (struct partial_die_info *,
961 struct dwarf2_cu *);
962
fe1b8b76
JB
963static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
964 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 965
fe1b8b76
JB
966static gdb_byte *read_attribute_value (struct attribute *, unsigned,
967 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 968
fe1b8b76 969static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 970
fe1b8b76 971static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 972
fe1b8b76 973static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 974
fe1b8b76 975static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 976
93311388 977static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 978
fe1b8b76 979static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 980 unsigned int *);
c906108c 981
c764a876
DE
982static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
983
984static LONGEST read_checked_initial_length_and_offset
985 (bfd *, gdb_byte *, const struct comp_unit_head *,
986 unsigned int *, unsigned int *);
613e1657 987
fe1b8b76 988static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
989 unsigned int *);
990
991static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 992
fe1b8b76 993static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 994
9b1c24c8 995static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 996
fe1b8b76
JB
997static char *read_indirect_string (bfd *, gdb_byte *,
998 const struct comp_unit_head *,
999 unsigned int *);
4bdf3d34 1000
fe1b8b76 1001static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1002
fe1b8b76 1003static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1004
fe1b8b76 1005static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 1006
e142c38c 1007static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1008
e142c38c
DJ
1009static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1010 struct dwarf2_cu *);
c906108c 1011
348e048f
DE
1012static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1013 unsigned int,
1014 struct dwarf2_cu *);
1015
05cf31d1
JB
1016static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1017 struct dwarf2_cu *cu);
1018
e142c38c 1019static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1020
e142c38c 1021static struct die_info *die_specification (struct die_info *die,
f2f0e013 1022 struct dwarf2_cu **);
63d06c5c 1023
debd256d
JB
1024static void free_line_header (struct line_header *lh);
1025
aaa75496
JB
1026static void add_file_name (struct line_header *, char *, unsigned int,
1027 unsigned int, unsigned int);
1028
debd256d
JB
1029static struct line_header *(dwarf_decode_line_header
1030 (unsigned int offset,
e7c27a73 1031 bfd *abfd, struct dwarf2_cu *cu));
debd256d 1032
72b9f47f 1033static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
aaa75496 1034 struct dwarf2_cu *, struct partial_symtab *);
c906108c 1035
72b9f47f 1036static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1037
a14ed312 1038static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1039 struct dwarf2_cu *);
c906108c 1040
34eaf542
TT
1041static struct symbol *new_symbol_full (struct die_info *, struct type *,
1042 struct dwarf2_cu *, struct symbol *);
1043
a14ed312 1044static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1045 struct dwarf2_cu *);
c906108c 1046
98bfdba5
PA
1047static void dwarf2_const_value_attr (struct attribute *attr,
1048 struct type *type,
1049 const char *name,
1050 struct obstack *obstack,
1051 struct dwarf2_cu *cu, long *value,
1052 gdb_byte **bytes,
1053 struct dwarf2_locexpr_baton **baton);
2df3850c 1054
e7c27a73 1055static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1056
b4ba55a1
JB
1057static int need_gnat_info (struct dwarf2_cu *);
1058
3e43a32a
MS
1059static struct type *die_descriptive_type (struct die_info *,
1060 struct dwarf2_cu *);
b4ba55a1
JB
1061
1062static void set_descriptive_type (struct type *, struct die_info *,
1063 struct dwarf2_cu *);
1064
e7c27a73
DJ
1065static struct type *die_containing_type (struct die_info *,
1066 struct dwarf2_cu *);
c906108c 1067
673bfd45
DE
1068static struct type *lookup_die_type (struct die_info *, struct attribute *,
1069 struct dwarf2_cu *);
c906108c 1070
f792889a 1071static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1072
673bfd45
DE
1073static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1074
086ed43d 1075static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1076
6e70227d 1077static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1078 const char *suffix, int physname,
1079 struct dwarf2_cu *cu);
63d06c5c 1080
e7c27a73 1081static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1082
348e048f
DE
1083static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1084
e7c27a73 1085static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1086
e7c27a73 1087static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1088
96408a79
SA
1089static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1090
ff013f42
JK
1091static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1092 struct dwarf2_cu *, struct partial_symtab *);
1093
a14ed312 1094static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1095 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1096 struct partial_symtab *);
c906108c 1097
fae299cd
DC
1098static void get_scope_pc_bounds (struct die_info *,
1099 CORE_ADDR *, CORE_ADDR *,
1100 struct dwarf2_cu *);
1101
801e3a5b
JB
1102static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1103 CORE_ADDR, struct dwarf2_cu *);
1104
a14ed312 1105static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1106 struct dwarf2_cu *);
c906108c 1107
a14ed312 1108static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1109 struct type *, struct dwarf2_cu *);
c906108c 1110
a14ed312 1111static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1112 struct die_info *, struct type *,
e7c27a73 1113 struct dwarf2_cu *);
c906108c 1114
a14ed312 1115static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1116 struct type *,
1117 struct dwarf2_cu *);
c906108c 1118
134d01f1 1119static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1120
e7c27a73 1121static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1122
e7c27a73 1123static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1124
5d7cb8df
JK
1125static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1126
27aa8d6a
SW
1127static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1128
f55ee35c
JK
1129static struct type *read_module_type (struct die_info *die,
1130 struct dwarf2_cu *cu);
1131
38d518c9 1132static const char *namespace_name (struct die_info *die,
e142c38c 1133 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1134
134d01f1 1135static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1136
e7c27a73 1137static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1138
6e70227d 1139static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1140 struct dwarf2_cu *);
1141
93311388 1142static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1143
93311388
DE
1144static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1145 gdb_byte *info_ptr,
d97bc12b
DE
1146 gdb_byte **new_info_ptr,
1147 struct die_info *parent);
1148
93311388
DE
1149static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1150 gdb_byte *info_ptr,
fe1b8b76 1151 gdb_byte **new_info_ptr,
639d11d3
DC
1152 struct die_info *parent);
1153
93311388
DE
1154static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1155 gdb_byte *info_ptr,
fe1b8b76 1156 gdb_byte **new_info_ptr,
639d11d3
DC
1157 struct die_info *parent);
1158
93311388
DE
1159static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1160 struct die_info **, gdb_byte *,
1161 int *);
1162
e7c27a73 1163static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1164
71c25dea
TT
1165static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1166 struct obstack *);
1167
e142c38c 1168static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1169
98bfdba5
PA
1170static const char *dwarf2_full_name (char *name,
1171 struct die_info *die,
1172 struct dwarf2_cu *cu);
1173
e142c38c 1174static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1175 struct dwarf2_cu **);
9219021c 1176
a14ed312 1177static char *dwarf_tag_name (unsigned int);
c906108c 1178
a14ed312 1179static char *dwarf_attr_name (unsigned int);
c906108c 1180
a14ed312 1181static char *dwarf_form_name (unsigned int);
c906108c 1182
a14ed312 1183static char *dwarf_bool_name (unsigned int);
c906108c 1184
a14ed312 1185static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1186
1187#if 0
a14ed312 1188static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1189#endif
1190
f9aca02d 1191static struct die_info *sibling_die (struct die_info *);
c906108c 1192
d97bc12b
DE
1193static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1194
1195static void dump_die_for_error (struct die_info *);
1196
1197static void dump_die_1 (struct ui_file *, int level, int max_level,
1198 struct die_info *);
c906108c 1199
d97bc12b 1200/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1201
51545339 1202static void store_in_ref_table (struct die_info *,
10b3939b 1203 struct dwarf2_cu *);
c906108c 1204
93311388
DE
1205static int is_ref_attr (struct attribute *);
1206
c764a876 1207static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1208
43bbcdc2 1209static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1210
348e048f
DE
1211static struct die_info *follow_die_ref_or_sig (struct die_info *,
1212 struct attribute *,
1213 struct dwarf2_cu **);
1214
10b3939b
DJ
1215static struct die_info *follow_die_ref (struct die_info *,
1216 struct attribute *,
f2f0e013 1217 struct dwarf2_cu **);
c906108c 1218
348e048f
DE
1219static struct die_info *follow_die_sig (struct die_info *,
1220 struct attribute *,
1221 struct dwarf2_cu **);
1222
6c83ed52
TT
1223static struct signatured_type *lookup_signatured_type_at_offset
1224 (struct objfile *objfile,
1225 struct dwarf2_section_info *section,
1226 unsigned int offset);
1227
348e048f 1228static void read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 1229 struct dwarf2_section_info *sect,
348e048f
DE
1230 unsigned int offset);
1231
1232static void read_signatured_type (struct objfile *,
1233 struct signatured_type *type_sig);
1234
c906108c
SS
1235/* memory allocation interface */
1236
7b5a2f43 1237static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1238
f3dd6933 1239static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1240
b60c80d6 1241static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1242
e142c38c 1243static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1244
e142c38c
DJ
1245static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1246 struct dwarf2_cu *);
5fb290d7 1247
2e276125 1248static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1249 char *, bfd *, struct dwarf2_cu *,
1250 struct dwarf2_section_info *,
1251 int);
2e276125 1252
8e19ed76
PS
1253static int attr_form_is_block (struct attribute *);
1254
3690dd37
JB
1255static int attr_form_is_section_offset (struct attribute *);
1256
1257static int attr_form_is_constant (struct attribute *);
1258
8cf6f0b1
TT
1259static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1260 struct dwarf2_loclist_baton *baton,
1261 struct attribute *attr);
1262
93e7bd98
DJ
1263static void dwarf2_symbol_mark_computed (struct attribute *attr,
1264 struct symbol *sym,
1265 struct dwarf2_cu *cu);
4c2df51b 1266
93311388
DE
1267static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1268 struct abbrev_info *abbrev,
1269 struct dwarf2_cu *cu);
4bb7a0a7 1270
72bf9492
DJ
1271static void free_stack_comp_unit (void *);
1272
72bf9492
DJ
1273static hashval_t partial_die_hash (const void *item);
1274
1275static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1276
ae038cb0 1277static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1278 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1279
1280static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1281 (unsigned int offset, struct objfile *objfile);
ae038cb0 1282
9816fde3
JK
1283static void init_one_comp_unit (struct dwarf2_cu *cu,
1284 struct objfile *objfile);
1285
1286static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1287 struct die_info *comp_unit_die);
93311388 1288
ae038cb0
DJ
1289static void free_one_comp_unit (void *);
1290
1291static void free_cached_comp_units (void *);
1292
1293static void age_cached_comp_units (void);
1294
1295static void free_one_cached_comp_unit (void *);
1296
f792889a
DJ
1297static struct type *set_die_type (struct die_info *, struct type *,
1298 struct dwarf2_cu *);
1c379e20 1299
ae038cb0
DJ
1300static void create_all_comp_units (struct objfile *);
1301
1fd400ff
TT
1302static int create_debug_types_hash_table (struct objfile *objfile);
1303
93311388
DE
1304static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1305 struct objfile *);
10b3939b
DJ
1306
1307static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1308
1309static void dwarf2_add_dependence (struct dwarf2_cu *,
1310 struct dwarf2_per_cu_data *);
1311
ae038cb0
DJ
1312static void dwarf2_mark (struct dwarf2_cu *);
1313
1314static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1315
673bfd45
DE
1316static struct type *get_die_type_at_offset (unsigned int,
1317 struct dwarf2_per_cu_data *per_cu);
1318
f792889a 1319static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1320
9291a0cd
TT
1321static void dwarf2_release_queue (void *dummy);
1322
1323static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1324 struct objfile *objfile);
1325
1326static void process_queue (struct objfile *objfile);
1327
1328static void find_file_and_directory (struct die_info *die,
1329 struct dwarf2_cu *cu,
1330 char **name, char **comp_dir);
1331
1332static char *file_full_name (int file, struct line_header *lh,
1333 const char *comp_dir);
1334
1335static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1336 gdb_byte *info_ptr,
1337 gdb_byte *buffer,
1338 unsigned int buffer_size,
1339 bfd *abfd);
1340
1341static void init_cu_die_reader (struct die_reader_specs *reader,
1342 struct dwarf2_cu *cu);
1343
673bfd45 1344static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1345
9291a0cd
TT
1346#if WORDS_BIGENDIAN
1347
1348/* Convert VALUE between big- and little-endian. */
1349static offset_type
1350byte_swap (offset_type value)
1351{
1352 offset_type result;
1353
1354 result = (value & 0xff) << 24;
1355 result |= (value & 0xff00) << 8;
1356 result |= (value & 0xff0000) >> 8;
1357 result |= (value & 0xff000000) >> 24;
1358 return result;
1359}
1360
1361#define MAYBE_SWAP(V) byte_swap (V)
1362
1363#else
1364#define MAYBE_SWAP(V) (V)
1365#endif /* WORDS_BIGENDIAN */
1366
1367/* The suffix for an index file. */
1368#define INDEX_SUFFIX ".gdb-index"
1369
3da10d80
KS
1370static const char *dwarf2_physname (char *name, struct die_info *die,
1371 struct dwarf2_cu *cu);
1372
c906108c 1373/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1374 information and return true if we have enough to do something.
1375 NAMES points to the dwarf2 section names, or is NULL if the standard
1376 ELF names are used. */
c906108c
SS
1377
1378int
251d32d9
TG
1379dwarf2_has_info (struct objfile *objfile,
1380 const struct dwarf2_debug_sections *names)
c906108c 1381{
be391dca
TT
1382 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1383 if (!dwarf2_per_objfile)
1384 {
1385 /* Initialize per-objfile state. */
1386 struct dwarf2_per_objfile *data
1387 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1388
be391dca
TT
1389 memset (data, 0, sizeof (*data));
1390 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1391 dwarf2_per_objfile = data;
6502dd73 1392
251d32d9
TG
1393 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1394 (void *) names);
be391dca
TT
1395 dwarf2_per_objfile->objfile = objfile;
1396 }
1397 return (dwarf2_per_objfile->info.asection != NULL
1398 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1399}
1400
251d32d9
TG
1401/* When loading sections, we look either for uncompressed section or for
1402 compressed section names. */
233a11ab
CS
1403
1404static int
251d32d9
TG
1405section_is_p (const char *section_name,
1406 const struct dwarf2_section_names *names)
233a11ab 1407{
251d32d9
TG
1408 if (names->normal != NULL
1409 && strcmp (section_name, names->normal) == 0)
1410 return 1;
1411 if (names->compressed != NULL
1412 && strcmp (section_name, names->compressed) == 0)
1413 return 1;
1414 return 0;
233a11ab
CS
1415}
1416
c906108c
SS
1417/* This function is mapped across the sections and remembers the
1418 offset and size of each of the debugging sections we are interested
1419 in. */
1420
1421static void
251d32d9 1422dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1423{
251d32d9
TG
1424 const struct dwarf2_debug_sections *names;
1425
1426 if (vnames == NULL)
1427 names = &dwarf2_elf_names;
1428 else
1429 names = (const struct dwarf2_debug_sections *) vnames;
1430
1431 if (section_is_p (sectp->name, &names->info))
c906108c 1432 {
dce234bc
PP
1433 dwarf2_per_objfile->info.asection = sectp;
1434 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1435 }
251d32d9 1436 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1437 {
dce234bc
PP
1438 dwarf2_per_objfile->abbrev.asection = sectp;
1439 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1440 }
251d32d9 1441 else if (section_is_p (sectp->name, &names->line))
c906108c 1442 {
dce234bc
PP
1443 dwarf2_per_objfile->line.asection = sectp;
1444 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1445 }
251d32d9 1446 else if (section_is_p (sectp->name, &names->loc))
c906108c 1447 {
dce234bc
PP
1448 dwarf2_per_objfile->loc.asection = sectp;
1449 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1450 }
251d32d9 1451 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1452 {
dce234bc
PP
1453 dwarf2_per_objfile->macinfo.asection = sectp;
1454 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1455 }
cf2c3c16
TT
1456 else if (section_is_p (sectp->name, &names->macro))
1457 {
1458 dwarf2_per_objfile->macro.asection = sectp;
1459 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1460 }
251d32d9 1461 else if (section_is_p (sectp->name, &names->str))
c906108c 1462 {
dce234bc
PP
1463 dwarf2_per_objfile->str.asection = sectp;
1464 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1465 }
251d32d9 1466 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1467 {
dce234bc
PP
1468 dwarf2_per_objfile->frame.asection = sectp;
1469 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1470 }
251d32d9 1471 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1472 {
3799ccc6 1473 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1474
3799ccc6
EZ
1475 if (aflag & SEC_HAS_CONTENTS)
1476 {
dce234bc
PP
1477 dwarf2_per_objfile->eh_frame.asection = sectp;
1478 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1479 }
b6af0555 1480 }
251d32d9 1481 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1482 {
dce234bc
PP
1483 dwarf2_per_objfile->ranges.asection = sectp;
1484 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1485 }
251d32d9 1486 else if (section_is_p (sectp->name, &names->types))
348e048f 1487 {
8b70b953
TT
1488 struct dwarf2_section_info type_section;
1489
1490 memset (&type_section, 0, sizeof (type_section));
1491 type_section.asection = sectp;
1492 type_section.size = bfd_get_section_size (sectp);
1493
1494 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1495 &type_section);
348e048f 1496 }
251d32d9 1497 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1498 {
1499 dwarf2_per_objfile->gdb_index.asection = sectp;
1500 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1501 }
dce234bc 1502
72dca2f5
FR
1503 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1504 && bfd_section_vma (abfd, sectp) == 0)
1505 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1506}
1507
dce234bc
PP
1508/* Decompress a section that was compressed using zlib. Store the
1509 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1510
1511static void
dce234bc
PP
1512zlib_decompress_section (struct objfile *objfile, asection *sectp,
1513 gdb_byte **outbuf, bfd_size_type *outsize)
1514{
1515 bfd *abfd = objfile->obfd;
1516#ifndef HAVE_ZLIB_H
1517 error (_("Support for zlib-compressed DWARF data (from '%s') "
1518 "is disabled in this copy of GDB"),
1519 bfd_get_filename (abfd));
1520#else
1521 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1522 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1523 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1524 bfd_size_type uncompressed_size;
1525 gdb_byte *uncompressed_buffer;
1526 z_stream strm;
1527 int rc;
1528 int header_size = 12;
1529
1530 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1531 || bfd_bread (compressed_buffer,
1532 compressed_size, abfd) != compressed_size)
dce234bc
PP
1533 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1534 bfd_get_filename (abfd));
1535
1536 /* Read the zlib header. In this case, it should be "ZLIB" followed
1537 by the uncompressed section size, 8 bytes in big-endian order. */
1538 if (compressed_size < header_size
1539 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1540 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1541 bfd_get_filename (abfd));
1542 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1543 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1544 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1545 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1546 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1547 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1548 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1549 uncompressed_size += compressed_buffer[11];
1550
1551 /* It is possible the section consists of several compressed
1552 buffers concatenated together, so we uncompress in a loop. */
1553 strm.zalloc = NULL;
1554 strm.zfree = NULL;
1555 strm.opaque = NULL;
1556 strm.avail_in = compressed_size - header_size;
1557 strm.next_in = (Bytef*) compressed_buffer + header_size;
1558 strm.avail_out = uncompressed_size;
1559 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1560 uncompressed_size);
1561 rc = inflateInit (&strm);
1562 while (strm.avail_in > 0)
1563 {
1564 if (rc != Z_OK)
1565 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1566 bfd_get_filename (abfd), rc);
1567 strm.next_out = ((Bytef*) uncompressed_buffer
1568 + (uncompressed_size - strm.avail_out));
1569 rc = inflate (&strm, Z_FINISH);
1570 if (rc != Z_STREAM_END)
1571 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1572 bfd_get_filename (abfd), rc);
1573 rc = inflateReset (&strm);
1574 }
1575 rc = inflateEnd (&strm);
1576 if (rc != Z_OK
1577 || strm.avail_out != 0)
1578 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1579 bfd_get_filename (abfd), rc);
1580
affddf13 1581 do_cleanups (cleanup);
dce234bc
PP
1582 *outbuf = uncompressed_buffer;
1583 *outsize = uncompressed_size;
1584#endif
233a11ab
CS
1585}
1586
9e0ac564
TT
1587/* A helper function that decides whether a section is empty. */
1588
1589static int
1590dwarf2_section_empty_p (struct dwarf2_section_info *info)
1591{
1592 return info->asection == NULL || info->size == 0;
1593}
1594
dce234bc
PP
1595/* Read the contents of the section SECTP from object file specified by
1596 OBJFILE, store info about the section into INFO.
1597 If the section is compressed, uncompress it before returning. */
c906108c 1598
dce234bc
PP
1599static void
1600dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1601{
dce234bc
PP
1602 bfd *abfd = objfile->obfd;
1603 asection *sectp = info->asection;
1604 gdb_byte *buf, *retbuf;
1605 unsigned char header[4];
c906108c 1606
be391dca
TT
1607 if (info->readin)
1608 return;
dce234bc 1609 info->buffer = NULL;
b315ab21 1610 info->map_addr = NULL;
be391dca 1611 info->readin = 1;
188dd5d6 1612
9e0ac564 1613 if (dwarf2_section_empty_p (info))
dce234bc 1614 return;
c906108c 1615
dce234bc
PP
1616 /* Check if the file has a 4-byte header indicating compression. */
1617 if (info->size > sizeof (header)
1618 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1619 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1620 {
1621 /* Upon decompression, update the buffer and its size. */
1622 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1623 {
1624 zlib_decompress_section (objfile, sectp, &info->buffer,
1625 &info->size);
1626 return;
1627 }
1628 }
4bdf3d34 1629
dce234bc
PP
1630#ifdef HAVE_MMAP
1631 if (pagesize == 0)
1632 pagesize = getpagesize ();
2e276125 1633
dce234bc
PP
1634 /* Only try to mmap sections which are large enough: we don't want to
1635 waste space due to fragmentation. Also, only try mmap for sections
1636 without relocations. */
1637
1638 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1639 {
b315ab21
TG
1640 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1641 MAP_PRIVATE, sectp->filepos,
1642 &info->map_addr, &info->map_len);
dce234bc 1643
b315ab21 1644 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1645 {
be391dca 1646#if HAVE_POSIX_MADVISE
b315ab21 1647 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1648#endif
dce234bc
PP
1649 return;
1650 }
1651 }
1652#endif
1653
1654 /* If we get here, we are a normal, not-compressed section. */
1655 info->buffer = buf
1656 = obstack_alloc (&objfile->objfile_obstack, info->size);
1657
1658 /* When debugging .o files, we may need to apply relocations; see
1659 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1660 We never compress sections in .o files, so we only need to
1661 try this when the section is not compressed. */
ac8035ab 1662 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1663 if (retbuf != NULL)
1664 {
1665 info->buffer = retbuf;
1666 return;
1667 }
1668
1669 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1670 || bfd_bread (buf, info->size, abfd) != info->size)
1671 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1672 bfd_get_filename (abfd));
1673}
1674
9e0ac564
TT
1675/* A helper function that returns the size of a section in a safe way.
1676 If you are positive that the section has been read before using the
1677 size, then it is safe to refer to the dwarf2_section_info object's
1678 "size" field directly. In other cases, you must call this
1679 function, because for compressed sections the size field is not set
1680 correctly until the section has been read. */
1681
1682static bfd_size_type
1683dwarf2_section_size (struct objfile *objfile,
1684 struct dwarf2_section_info *info)
1685{
1686 if (!info->readin)
1687 dwarf2_read_section (objfile, info);
1688 return info->size;
1689}
1690
dce234bc 1691/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1692 SECTION_NAME. */
af34e669 1693
dce234bc 1694void
3017a003
TG
1695dwarf2_get_section_info (struct objfile *objfile,
1696 enum dwarf2_section_enum sect,
dce234bc
PP
1697 asection **sectp, gdb_byte **bufp,
1698 bfd_size_type *sizep)
1699{
1700 struct dwarf2_per_objfile *data
1701 = objfile_data (objfile, dwarf2_objfile_data_key);
1702 struct dwarf2_section_info *info;
a3b2a86b
TT
1703
1704 /* We may see an objfile without any DWARF, in which case we just
1705 return nothing. */
1706 if (data == NULL)
1707 {
1708 *sectp = NULL;
1709 *bufp = NULL;
1710 *sizep = 0;
1711 return;
1712 }
3017a003
TG
1713 switch (sect)
1714 {
1715 case DWARF2_DEBUG_FRAME:
1716 info = &data->frame;
1717 break;
1718 case DWARF2_EH_FRAME:
1719 info = &data->eh_frame;
1720 break;
1721 default:
1722 gdb_assert_not_reached ("unexpected section");
1723 }
dce234bc 1724
9e0ac564 1725 dwarf2_read_section (objfile, info);
dce234bc
PP
1726
1727 *sectp = info->asection;
1728 *bufp = info->buffer;
1729 *sizep = info->size;
1730}
1731
9291a0cd 1732\f
7b9f3c50
DE
1733/* DWARF quick_symbols_functions support. */
1734
1735/* TUs can share .debug_line entries, and there can be a lot more TUs than
1736 unique line tables, so we maintain a separate table of all .debug_line
1737 derived entries to support the sharing.
1738 All the quick functions need is the list of file names. We discard the
1739 line_header when we're done and don't need to record it here. */
1740struct quick_file_names
1741{
1742 /* The offset in .debug_line of the line table. We hash on this. */
1743 unsigned int offset;
1744
1745 /* The number of entries in file_names, real_names. */
1746 unsigned int num_file_names;
1747
1748 /* The file names from the line table, after being run through
1749 file_full_name. */
1750 const char **file_names;
1751
1752 /* The file names from the line table after being run through
1753 gdb_realpath. These are computed lazily. */
1754 const char **real_names;
1755};
1756
1757/* When using the index (and thus not using psymtabs), each CU has an
1758 object of this type. This is used to hold information needed by
1759 the various "quick" methods. */
1760struct dwarf2_per_cu_quick_data
1761{
1762 /* The file table. This can be NULL if there was no file table
1763 or it's currently not read in.
1764 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1765 struct quick_file_names *file_names;
1766
1767 /* The corresponding symbol table. This is NULL if symbols for this
1768 CU have not yet been read. */
1769 struct symtab *symtab;
1770
1771 /* A temporary mark bit used when iterating over all CUs in
1772 expand_symtabs_matching. */
1773 unsigned int mark : 1;
1774
1775 /* True if we've tried to read the file table and found there isn't one.
1776 There will be no point in trying to read it again next time. */
1777 unsigned int no_file_data : 1;
1778};
1779
1780/* Hash function for a quick_file_names. */
1781
1782static hashval_t
1783hash_file_name_entry (const void *e)
1784{
1785 const struct quick_file_names *file_data = e;
1786
1787 return file_data->offset;
1788}
1789
1790/* Equality function for a quick_file_names. */
1791
1792static int
1793eq_file_name_entry (const void *a, const void *b)
1794{
1795 const struct quick_file_names *ea = a;
1796 const struct quick_file_names *eb = b;
1797
1798 return ea->offset == eb->offset;
1799}
1800
1801/* Delete function for a quick_file_names. */
1802
1803static void
1804delete_file_name_entry (void *e)
1805{
1806 struct quick_file_names *file_data = e;
1807 int i;
1808
1809 for (i = 0; i < file_data->num_file_names; ++i)
1810 {
1811 xfree ((void*) file_data->file_names[i]);
1812 if (file_data->real_names)
1813 xfree ((void*) file_data->real_names[i]);
1814 }
1815
1816 /* The space for the struct itself lives on objfile_obstack,
1817 so we don't free it here. */
1818}
1819
1820/* Create a quick_file_names hash table. */
1821
1822static htab_t
1823create_quick_file_names_table (unsigned int nr_initial_entries)
1824{
1825 return htab_create_alloc (nr_initial_entries,
1826 hash_file_name_entry, eq_file_name_entry,
1827 delete_file_name_entry, xcalloc, xfree);
1828}
9291a0cd 1829
918dd910
JK
1830/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1831 have to be created afterwards. You should call age_cached_comp_units after
1832 processing PER_CU->CU. dw2_setup must have been already called. */
1833
1834static void
1835load_cu (struct dwarf2_per_cu_data *per_cu)
1836{
8b70b953
TT
1837 if (per_cu->debug_type_section)
1838 read_signatured_type_at_offset (per_cu->objfile,
1839 per_cu->debug_type_section,
1840 per_cu->offset);
918dd910
JK
1841 else
1842 load_full_comp_unit (per_cu, per_cu->objfile);
1843
1844 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1845
1846 gdb_assert (per_cu->cu != NULL);
1847}
1848
9291a0cd
TT
1849/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1850 this CU came. */
2fdf6df6 1851
9291a0cd
TT
1852static void
1853dw2_do_instantiate_symtab (struct objfile *objfile,
1854 struct dwarf2_per_cu_data *per_cu)
1855{
1856 struct cleanup *back_to;
1857
1858 back_to = make_cleanup (dwarf2_release_queue, NULL);
1859
1860 queue_comp_unit (per_cu, objfile);
1861
918dd910 1862 load_cu (per_cu);
9291a0cd
TT
1863
1864 process_queue (objfile);
1865
1866 /* Age the cache, releasing compilation units that have not
1867 been used recently. */
1868 age_cached_comp_units ();
1869
1870 do_cleanups (back_to);
1871}
1872
1873/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1874 the objfile from which this CU came. Returns the resulting symbol
1875 table. */
2fdf6df6 1876
9291a0cd
TT
1877static struct symtab *
1878dw2_instantiate_symtab (struct objfile *objfile,
1879 struct dwarf2_per_cu_data *per_cu)
1880{
1881 if (!per_cu->v.quick->symtab)
1882 {
1883 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1884 increment_reading_symtab ();
1885 dw2_do_instantiate_symtab (objfile, per_cu);
1886 do_cleanups (back_to);
1887 }
1888 return per_cu->v.quick->symtab;
1889}
1890
1fd400ff 1891/* Return the CU given its index. */
2fdf6df6 1892
1fd400ff
TT
1893static struct dwarf2_per_cu_data *
1894dw2_get_cu (int index)
1895{
1896 if (index >= dwarf2_per_objfile->n_comp_units)
1897 {
1898 index -= dwarf2_per_objfile->n_comp_units;
1899 return dwarf2_per_objfile->type_comp_units[index];
1900 }
1901 return dwarf2_per_objfile->all_comp_units[index];
1902}
1903
9291a0cd
TT
1904/* A helper function that knows how to read a 64-bit value in a way
1905 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1906 otherwise. */
2fdf6df6 1907
9291a0cd
TT
1908static int
1909extract_cu_value (const char *bytes, ULONGEST *result)
1910{
1911 if (sizeof (ULONGEST) < 8)
1912 {
1913 int i;
1914
1915 /* Ignore the upper 4 bytes if they are all zero. */
1916 for (i = 0; i < 4; ++i)
1917 if (bytes[i + 4] != 0)
1918 return 0;
1919
1920 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1921 }
1922 else
1923 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1924 return 1;
1925}
1926
1927/* Read the CU list from the mapped index, and use it to create all
1928 the CU objects for this objfile. Return 0 if something went wrong,
1929 1 if everything went ok. */
2fdf6df6 1930
9291a0cd 1931static int
1fd400ff
TT
1932create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1933 offset_type cu_list_elements)
9291a0cd
TT
1934{
1935 offset_type i;
9291a0cd
TT
1936
1937 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1938 dwarf2_per_objfile->all_comp_units
1939 = obstack_alloc (&objfile->objfile_obstack,
1940 dwarf2_per_objfile->n_comp_units
1941 * sizeof (struct dwarf2_per_cu_data *));
1942
1943 for (i = 0; i < cu_list_elements; i += 2)
1944 {
1945 struct dwarf2_per_cu_data *the_cu;
1946 ULONGEST offset, length;
1947
1948 if (!extract_cu_value (cu_list, &offset)
1949 || !extract_cu_value (cu_list + 8, &length))
1950 return 0;
1951 cu_list += 2 * 8;
1952
1953 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1954 struct dwarf2_per_cu_data);
1955 the_cu->offset = offset;
1956 the_cu->length = length;
1957 the_cu->objfile = objfile;
1958 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1959 struct dwarf2_per_cu_quick_data);
1960 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1961 }
1962
1963 return 1;
1964}
1965
1fd400ff 1966/* Create the signatured type hash table from the index. */
673bfd45 1967
1fd400ff 1968static int
673bfd45 1969create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 1970 struct dwarf2_section_info *section,
673bfd45
DE
1971 const gdb_byte *bytes,
1972 offset_type elements)
1fd400ff
TT
1973{
1974 offset_type i;
673bfd45 1975 htab_t sig_types_hash;
1fd400ff
TT
1976
1977 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1978 dwarf2_per_objfile->type_comp_units
1979 = obstack_alloc (&objfile->objfile_obstack,
1980 dwarf2_per_objfile->n_type_comp_units
1981 * sizeof (struct dwarf2_per_cu_data *));
1982
673bfd45 1983 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
1984
1985 for (i = 0; i < elements; i += 3)
1986 {
1987 struct signatured_type *type_sig;
1988 ULONGEST offset, type_offset, signature;
1989 void **slot;
1990
1991 if (!extract_cu_value (bytes, &offset)
1992 || !extract_cu_value (bytes + 8, &type_offset))
1993 return 0;
1994 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1995 bytes += 3 * 8;
1996
1997 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1998 struct signatured_type);
1999 type_sig->signature = signature;
1fd400ff 2000 type_sig->type_offset = type_offset;
8b70b953 2001 type_sig->per_cu.debug_type_section = section;
1fd400ff
TT
2002 type_sig->per_cu.offset = offset;
2003 type_sig->per_cu.objfile = objfile;
2004 type_sig->per_cu.v.quick
2005 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2006 struct dwarf2_per_cu_quick_data);
2007
673bfd45 2008 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1fd400ff
TT
2009 *slot = type_sig;
2010
2011 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
2012 }
2013
673bfd45 2014 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2015
2016 return 1;
2017}
2018
9291a0cd
TT
2019/* Read the address map data from the mapped index, and use it to
2020 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2021
9291a0cd
TT
2022static void
2023create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2024{
2025 const gdb_byte *iter, *end;
2026 struct obstack temp_obstack;
2027 struct addrmap *mutable_map;
2028 struct cleanup *cleanup;
2029 CORE_ADDR baseaddr;
2030
2031 obstack_init (&temp_obstack);
2032 cleanup = make_cleanup_obstack_free (&temp_obstack);
2033 mutable_map = addrmap_create_mutable (&temp_obstack);
2034
2035 iter = index->address_table;
2036 end = iter + index->address_table_size;
2037
2038 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2039
2040 while (iter < end)
2041 {
2042 ULONGEST hi, lo, cu_index;
2043 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2044 iter += 8;
2045 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2046 iter += 8;
2047 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2048 iter += 4;
2049
2050 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2051 dw2_get_cu (cu_index));
9291a0cd
TT
2052 }
2053
2054 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2055 &objfile->objfile_obstack);
2056 do_cleanups (cleanup);
2057}
2058
59d7bcaf
JK
2059/* The hash function for strings in the mapped index. This is the same as
2060 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2061 implementation. This is necessary because the hash function is tied to the
2062 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2063 SYMBOL_HASH_NEXT.
2064
2065 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2066
9291a0cd 2067static hashval_t
559a7a62 2068mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2069{
2070 const unsigned char *str = (const unsigned char *) p;
2071 hashval_t r = 0;
2072 unsigned char c;
2073
2074 while ((c = *str++) != 0)
559a7a62
JK
2075 {
2076 if (index_version >= 5)
2077 c = tolower (c);
2078 r = r * 67 + c - 113;
2079 }
9291a0cd
TT
2080
2081 return r;
2082}
2083
2084/* Find a slot in the mapped index INDEX for the object named NAME.
2085 If NAME is found, set *VEC_OUT to point to the CU vector in the
2086 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2087
9291a0cd
TT
2088static int
2089find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2090 offset_type **vec_out)
2091{
0cf03b49
JK
2092 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2093 offset_type hash;
9291a0cd 2094 offset_type slot, step;
559a7a62 2095 int (*cmp) (const char *, const char *);
9291a0cd 2096
0cf03b49
JK
2097 if (current_language->la_language == language_cplus
2098 || current_language->la_language == language_java
2099 || current_language->la_language == language_fortran)
2100 {
2101 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2102 not contain any. */
2103 const char *paren = strchr (name, '(');
2104
2105 if (paren)
2106 {
2107 char *dup;
2108
2109 dup = xmalloc (paren - name + 1);
2110 memcpy (dup, name, paren - name);
2111 dup[paren - name] = 0;
2112
2113 make_cleanup (xfree, dup);
2114 name = dup;
2115 }
2116 }
2117
559a7a62
JK
2118 /* Index version 4 did not support case insensitive searches. But the
2119 indexes for case insensitive languages are built in lowercase, therefore
2120 simulate our NAME being searched is also lowercased. */
2121 hash = mapped_index_string_hash ((index->version == 4
2122 && case_sensitivity == case_sensitive_off
2123 ? 5 : index->version),
2124 name);
2125
3876f04e
DE
2126 slot = hash & (index->symbol_table_slots - 1);
2127 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2128 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2129
2130 for (;;)
2131 {
2132 /* Convert a slot number to an offset into the table. */
2133 offset_type i = 2 * slot;
2134 const char *str;
3876f04e 2135 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2136 {
2137 do_cleanups (back_to);
2138 return 0;
2139 }
9291a0cd 2140
3876f04e 2141 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2142 if (!cmp (name, str))
9291a0cd
TT
2143 {
2144 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2145 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2146 do_cleanups (back_to);
9291a0cd
TT
2147 return 1;
2148 }
2149
3876f04e 2150 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2151 }
2152}
2153
2154/* Read the index file. If everything went ok, initialize the "quick"
2155 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2156
9291a0cd
TT
2157static int
2158dwarf2_read_index (struct objfile *objfile)
2159{
9291a0cd
TT
2160 char *addr;
2161 struct mapped_index *map;
b3b272e1 2162 offset_type *metadata;
ac0b195c
KW
2163 const gdb_byte *cu_list;
2164 const gdb_byte *types_list = NULL;
2165 offset_type version, cu_list_elements;
2166 offset_type types_list_elements = 0;
1fd400ff 2167 int i;
9291a0cd 2168
9e0ac564 2169 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2170 return 0;
82430852
JK
2171
2172 /* Older elfutils strip versions could keep the section in the main
2173 executable while splitting it for the separate debug info file. */
2174 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2175 & SEC_HAS_CONTENTS) == 0)
2176 return 0;
2177
9291a0cd
TT
2178 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2179
2180 addr = dwarf2_per_objfile->gdb_index.buffer;
2181 /* Version check. */
1fd400ff 2182 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2183 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2184 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2185 contained incomplete addrmap. So, it seems better to just ignore such
559a7a62
JK
2186 indices. Index version 4 uses a different hash function than index
2187 version 5 and later. */
831adc1f 2188 if (version < 4)
9291a0cd 2189 return 0;
594e8718
JK
2190 /* Indexes with higher version than the one supported by GDB may be no
2191 longer backward compatible. */
559a7a62 2192 if (version > 5)
594e8718 2193 return 0;
9291a0cd
TT
2194
2195 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2196 map->version = version;
b3b272e1 2197 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2198
2199 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2200
2201 i = 0;
2202 cu_list = addr + MAYBE_SWAP (metadata[i]);
2203 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2204 / 8);
1fd400ff
TT
2205 ++i;
2206
987d643c
TT
2207 types_list = addr + MAYBE_SWAP (metadata[i]);
2208 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2209 - MAYBE_SWAP (metadata[i]))
2210 / 8);
2211 ++i;
1fd400ff
TT
2212
2213 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2214 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2215 - MAYBE_SWAP (metadata[i]));
2216 ++i;
2217
3876f04e
DE
2218 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2219 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2220 - MAYBE_SWAP (metadata[i]))
2221 / (2 * sizeof (offset_type)));
1fd400ff 2222 ++i;
9291a0cd 2223
1fd400ff
TT
2224 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2225
2226 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2227 return 0;
2228
8b70b953
TT
2229 if (types_list_elements)
2230 {
2231 struct dwarf2_section_info *section;
2232
2233 /* We can only handle a single .debug_types when we have an
2234 index. */
2235 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2236 return 0;
2237
2238 section = VEC_index (dwarf2_section_info_def,
2239 dwarf2_per_objfile->types, 0);
2240
2241 if (!create_signatured_type_table_from_index (objfile, section,
2242 types_list,
2243 types_list_elements))
2244 return 0;
2245 }
9291a0cd
TT
2246
2247 create_addrmap_from_index (objfile, map);
2248
2249 dwarf2_per_objfile->index_table = map;
2250 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2251 dwarf2_per_objfile->quick_file_names_table =
2252 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2253
2254 return 1;
2255}
2256
2257/* A helper for the "quick" functions which sets the global
2258 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2259
9291a0cd
TT
2260static void
2261dw2_setup (struct objfile *objfile)
2262{
2263 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2264 gdb_assert (dwarf2_per_objfile);
2265}
2266
2267/* A helper for the "quick" functions which attempts to read the line
2268 table for THIS_CU. */
2fdf6df6 2269
7b9f3c50
DE
2270static struct quick_file_names *
2271dw2_get_file_names (struct objfile *objfile,
2272 struct dwarf2_per_cu_data *this_cu)
9291a0cd
TT
2273{
2274 bfd *abfd = objfile->obfd;
7b9f3c50 2275 struct line_header *lh;
9291a0cd
TT
2276 struct attribute *attr;
2277 struct cleanup *cleanups;
2278 struct die_info *comp_unit_die;
36374493 2279 struct dwarf2_section_info* sec;
adabb602 2280 gdb_byte *info_ptr, *buffer;
9291a0cd
TT
2281 int has_children, i;
2282 struct dwarf2_cu cu;
2283 unsigned int bytes_read, buffer_size;
2284 struct die_reader_specs reader_specs;
2285 char *name, *comp_dir;
7b9f3c50
DE
2286 void **slot;
2287 struct quick_file_names *qfn;
2288 unsigned int line_offset;
9291a0cd 2289
7b9f3c50
DE
2290 if (this_cu->v.quick->file_names != NULL)
2291 return this_cu->v.quick->file_names;
2292 /* If we know there is no line data, no point in looking again. */
2293 if (this_cu->v.quick->no_file_data)
2294 return NULL;
9291a0cd 2295
9816fde3 2296 init_one_comp_unit (&cu, objfile);
9291a0cd
TT
2297 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2298
8b70b953
TT
2299 if (this_cu->debug_type_section)
2300 sec = this_cu->debug_type_section;
36374493
DE
2301 else
2302 sec = &dwarf2_per_objfile->info;
2303 dwarf2_read_section (objfile, sec);
2304 buffer_size = sec->size;
2305 buffer = sec->buffer;
9291a0cd 2306 info_ptr = buffer + this_cu->offset;
9291a0cd
TT
2307
2308 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2309 buffer, buffer_size,
2310 abfd);
2311
6caca83c
CC
2312 /* Skip dummy compilation units. */
2313 if (info_ptr >= buffer + buffer_size
2314 || peek_abbrev_code (abfd, info_ptr) == 0)
2315 {
2316 do_cleanups (cleanups);
2317 return NULL;
2318 }
2319
9291a0cd
TT
2320 this_cu->cu = &cu;
2321 cu.per_cu = this_cu;
2322
2323 dwarf2_read_abbrevs (abfd, &cu);
2324 make_cleanup (dwarf2_free_abbrev_table, &cu);
2325
8b70b953 2326 if (this_cu->debug_type_section)
9291a0cd
TT
2327 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2328 init_cu_die_reader (&reader_specs, &cu);
e8e80198
MS
2329 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2330 &has_children);
9291a0cd 2331
7b9f3c50
DE
2332 lh = NULL;
2333 slot = NULL;
2334 line_offset = 0;
9291a0cd
TT
2335 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2336 if (attr)
2337 {
7b9f3c50
DE
2338 struct quick_file_names find_entry;
2339
2340 line_offset = DW_UNSND (attr);
2341
2342 /* We may have already read in this line header (TU line header sharing).
2343 If we have we're done. */
2344 find_entry.offset = line_offset;
2345 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2346 &find_entry, INSERT);
2347 if (*slot != NULL)
2348 {
2349 do_cleanups (cleanups);
2350 this_cu->v.quick->file_names = *slot;
2351 return *slot;
2352 }
2353
9291a0cd
TT
2354 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2355 }
2356 if (lh == NULL)
2357 {
2358 do_cleanups (cleanups);
7b9f3c50
DE
2359 this_cu->v.quick->no_file_data = 1;
2360 return NULL;
9291a0cd
TT
2361 }
2362
7b9f3c50
DE
2363 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2364 qfn->offset = line_offset;
2365 gdb_assert (slot != NULL);
2366 *slot = qfn;
9291a0cd 2367
7b9f3c50 2368 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
9291a0cd 2369
7b9f3c50
DE
2370 qfn->num_file_names = lh->num_file_names;
2371 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2372 lh->num_file_names * sizeof (char *));
9291a0cd 2373 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2374 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2375 qfn->real_names = NULL;
9291a0cd 2376
7b9f3c50 2377 free_line_header (lh);
9291a0cd 2378 do_cleanups (cleanups);
7b9f3c50
DE
2379
2380 this_cu->v.quick->file_names = qfn;
2381 return qfn;
9291a0cd
TT
2382}
2383
2384/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2385 real path for a given file name from the line table. */
2fdf6df6 2386
9291a0cd 2387static const char *
7b9f3c50
DE
2388dw2_get_real_path (struct objfile *objfile,
2389 struct quick_file_names *qfn, int index)
9291a0cd 2390{
7b9f3c50
DE
2391 if (qfn->real_names == NULL)
2392 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2393 qfn->num_file_names, sizeof (char *));
9291a0cd 2394
7b9f3c50
DE
2395 if (qfn->real_names[index] == NULL)
2396 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2397
7b9f3c50 2398 return qfn->real_names[index];
9291a0cd
TT
2399}
2400
2401static struct symtab *
2402dw2_find_last_source_symtab (struct objfile *objfile)
2403{
2404 int index;
ae2de4f8 2405
9291a0cd
TT
2406 dw2_setup (objfile);
2407 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2408 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2409}
2410
7b9f3c50
DE
2411/* Traversal function for dw2_forget_cached_source_info. */
2412
2413static int
2414dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2415{
7b9f3c50 2416 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2417
7b9f3c50 2418 if (file_data->real_names)
9291a0cd 2419 {
7b9f3c50 2420 int i;
9291a0cd 2421
7b9f3c50 2422 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2423 {
7b9f3c50
DE
2424 xfree ((void*) file_data->real_names[i]);
2425 file_data->real_names[i] = NULL;
9291a0cd
TT
2426 }
2427 }
7b9f3c50
DE
2428
2429 return 1;
2430}
2431
2432static void
2433dw2_forget_cached_source_info (struct objfile *objfile)
2434{
2435 dw2_setup (objfile);
2436
2437 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2438 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2439}
2440
2441static int
2442dw2_lookup_symtab (struct objfile *objfile, const char *name,
2443 const char *full_path, const char *real_path,
2444 struct symtab **result)
2445{
2446 int i;
2447 int check_basename = lbasename (name) == name;
2448 struct dwarf2_per_cu_data *base_cu = NULL;
2449
2450 dw2_setup (objfile);
ae2de4f8 2451
1fd400ff
TT
2452 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2453 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2454 {
2455 int j;
e254ef6a 2456 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2457 struct quick_file_names *file_data;
9291a0cd 2458
e254ef6a 2459 if (per_cu->v.quick->symtab)
9291a0cd
TT
2460 continue;
2461
7b9f3c50
DE
2462 file_data = dw2_get_file_names (objfile, per_cu);
2463 if (file_data == NULL)
9291a0cd
TT
2464 continue;
2465
7b9f3c50 2466 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2467 {
7b9f3c50 2468 const char *this_name = file_data->file_names[j];
9291a0cd
TT
2469
2470 if (FILENAME_CMP (name, this_name) == 0)
2471 {
e254ef6a 2472 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2473 return 1;
2474 }
2475
2476 if (check_basename && ! base_cu
2477 && FILENAME_CMP (lbasename (this_name), name) == 0)
e254ef6a 2478 base_cu = per_cu;
9291a0cd
TT
2479
2480 if (full_path != NULL)
2481 {
7b9f3c50
DE
2482 const char *this_real_name = dw2_get_real_path (objfile,
2483 file_data, j);
9291a0cd 2484
7b9f3c50
DE
2485 if (this_real_name != NULL
2486 && FILENAME_CMP (full_path, this_real_name) == 0)
9291a0cd 2487 {
e254ef6a 2488 *result = dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2489 return 1;
2490 }
2491 }
2492
2493 if (real_path != NULL)
2494 {
7b9f3c50
DE
2495 const char *this_real_name = dw2_get_real_path (objfile,
2496 file_data, j);
9291a0cd 2497
7b9f3c50
DE
2498 if (this_real_name != NULL
2499 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 2500 {
74dd2ca6
DE
2501 *result = dw2_instantiate_symtab (objfile, per_cu);
2502 return 1;
9291a0cd
TT
2503 }
2504 }
2505 }
2506 }
2507
2508 if (base_cu)
2509 {
2510 *result = dw2_instantiate_symtab (objfile, base_cu);
2511 return 1;
2512 }
2513
2514 return 0;
2515}
2516
2517static struct symtab *
2518dw2_lookup_symbol (struct objfile *objfile, int block_index,
2519 const char *name, domain_enum domain)
2520{
774b6a14 2521 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2522 instead. */
2523 return NULL;
2524}
2525
2526/* A helper function that expands all symtabs that hold an object
2527 named NAME. */
2fdf6df6 2528
9291a0cd
TT
2529static void
2530dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2531{
2532 dw2_setup (objfile);
2533
ae2de4f8 2534 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2535 if (dwarf2_per_objfile->index_table)
2536 {
2537 offset_type *vec;
2538
2539 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2540 name, &vec))
2541 {
2542 offset_type i, len = MAYBE_SWAP (*vec);
2543 for (i = 0; i < len; ++i)
2544 {
2545 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2546 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2547
e254ef6a 2548 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2549 }
2550 }
2551 }
2552}
2553
774b6a14
TT
2554static void
2555dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2556 enum block_enum block_kind, const char *name,
774b6a14 2557 domain_enum domain)
9291a0cd 2558{
774b6a14 2559 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2560}
2561
2562static void
2563dw2_print_stats (struct objfile *objfile)
2564{
2565 int i, count;
2566
2567 dw2_setup (objfile);
2568 count = 0;
1fd400ff
TT
2569 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2570 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2571 {
e254ef6a 2572 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2573
e254ef6a 2574 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2575 ++count;
2576 }
2577 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2578}
2579
2580static void
2581dw2_dump (struct objfile *objfile)
2582{
2583 /* Nothing worth printing. */
2584}
2585
2586static void
2587dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2588 struct section_offsets *delta)
2589{
2590 /* There's nothing to relocate here. */
2591}
2592
2593static void
2594dw2_expand_symtabs_for_function (struct objfile *objfile,
2595 const char *func_name)
2596{
2597 dw2_do_expand_symtabs_matching (objfile, func_name);
2598}
2599
2600static void
2601dw2_expand_all_symtabs (struct objfile *objfile)
2602{
2603 int i;
2604
2605 dw2_setup (objfile);
1fd400ff
TT
2606
2607 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2608 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2609 {
e254ef6a 2610 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2611
e254ef6a 2612 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2613 }
2614}
2615
2616static void
2617dw2_expand_symtabs_with_filename (struct objfile *objfile,
2618 const char *filename)
2619{
2620 int i;
2621
2622 dw2_setup (objfile);
d4637a04
DE
2623
2624 /* We don't need to consider type units here.
2625 This is only called for examining code, e.g. expand_line_sal.
2626 There can be an order of magnitude (or more) more type units
2627 than comp units, and we avoid them if we can. */
2628
2629 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2630 {
2631 int j;
e254ef6a 2632 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2633 struct quick_file_names *file_data;
9291a0cd 2634
e254ef6a 2635 if (per_cu->v.quick->symtab)
9291a0cd
TT
2636 continue;
2637
7b9f3c50
DE
2638 file_data = dw2_get_file_names (objfile, per_cu);
2639 if (file_data == NULL)
9291a0cd
TT
2640 continue;
2641
7b9f3c50 2642 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2643 {
7b9f3c50 2644 const char *this_name = file_data->file_names[j];
1ef75ecc 2645 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2646 {
e254ef6a 2647 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2648 break;
2649 }
2650 }
2651 }
2652}
2653
dd786858 2654static const char *
9291a0cd
TT
2655dw2_find_symbol_file (struct objfile *objfile, const char *name)
2656{
e254ef6a 2657 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2658 offset_type *vec;
7b9f3c50 2659 struct quick_file_names *file_data;
9291a0cd
TT
2660
2661 dw2_setup (objfile);
2662
ae2de4f8 2663 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2664 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2665 {
2666 struct symtab *s;
2667
2668 ALL_OBJFILE_SYMTABS (objfile, s)
2669 if (s->primary)
2670 {
2671 struct blockvector *bv = BLOCKVECTOR (s);
2672 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2673 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2674
2675 if (sym)
2676 return sym->symtab->filename;
2677 }
2678 return NULL;
2679 }
9291a0cd
TT
2680
2681 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2682 name, &vec))
2683 return NULL;
2684
2685 /* Note that this just looks at the very first one named NAME -- but
2686 actually we are looking for a function. find_main_filename
2687 should be rewritten so that it doesn't require a custom hook. It
2688 could just use the ordinary symbol tables. */
2689 /* vec[0] is the length, which must always be >0. */
e254ef6a 2690 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2691
7b9f3c50
DE
2692 file_data = dw2_get_file_names (objfile, per_cu);
2693 if (file_data == NULL)
9291a0cd
TT
2694 return NULL;
2695
7b9f3c50 2696 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2697}
2698
2699static void
40658b94
PH
2700dw2_map_matching_symbols (const char * name, domain_enum namespace,
2701 struct objfile *objfile, int global,
2702 int (*callback) (struct block *,
2703 struct symbol *, void *),
2edb89d3
JK
2704 void *data, symbol_compare_ftype *match,
2705 symbol_compare_ftype *ordered_compare)
9291a0cd 2706{
40658b94 2707 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2708 current language is Ada for a non-Ada objfile using GNU index. As Ada
2709 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2710}
2711
2712static void
2713dw2_expand_symtabs_matching (struct objfile *objfile,
2714 int (*file_matcher) (const char *, void *),
2715 int (*name_matcher) (const char *, void *),
8903c50d 2716 enum search_domain kind,
9291a0cd
TT
2717 void *data)
2718{
2719 int i;
2720 offset_type iter;
4b5246aa 2721 struct mapped_index *index;
9291a0cd
TT
2722
2723 dw2_setup (objfile);
ae2de4f8
DE
2724
2725 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2726 if (!dwarf2_per_objfile->index_table)
2727 return;
4b5246aa 2728 index = dwarf2_per_objfile->index_table;
9291a0cd 2729
7b08b9eb
JK
2730 if (file_matcher != NULL)
2731 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2732 + dwarf2_per_objfile->n_type_comp_units); ++i)
2733 {
2734 int j;
2735 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2736 struct quick_file_names *file_data;
2737
2738 per_cu->v.quick->mark = 0;
2739 if (per_cu->v.quick->symtab)
2740 continue;
2741
2742 file_data = dw2_get_file_names (objfile, per_cu);
2743 if (file_data == NULL)
2744 continue;
2745
2746 for (j = 0; j < file_data->num_file_names; ++j)
2747 {
2748 if (file_matcher (file_data->file_names[j], data))
2749 {
2750 per_cu->v.quick->mark = 1;
2751 break;
2752 }
2753 }
2754 }
9291a0cd 2755
3876f04e 2756 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2757 {
2758 offset_type idx = 2 * iter;
2759 const char *name;
2760 offset_type *vec, vec_len, vec_idx;
2761
3876f04e 2762 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2763 continue;
2764
3876f04e 2765 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd
TT
2766
2767 if (! (*name_matcher) (name, data))
2768 continue;
2769
2770 /* The name was matched, now expand corresponding CUs that were
2771 marked. */
4b5246aa 2772 vec = (offset_type *) (index->constant_pool
3876f04e 2773 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2774 vec_len = MAYBE_SWAP (vec[0]);
2775 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2776 {
e254ef6a 2777 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2778
e254ef6a 2779 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2780 if (file_matcher == NULL || per_cu->v.quick->mark)
e254ef6a 2781 dw2_instantiate_symtab (objfile, per_cu);
9291a0cd
TT
2782 }
2783 }
2784}
2785
2786static struct symtab *
2787dw2_find_pc_sect_symtab (struct objfile *objfile,
2788 struct minimal_symbol *msymbol,
2789 CORE_ADDR pc,
2790 struct obj_section *section,
2791 int warn_if_readin)
2792{
2793 struct dwarf2_per_cu_data *data;
2794
2795 dw2_setup (objfile);
2796
2797 if (!objfile->psymtabs_addrmap)
2798 return NULL;
2799
2800 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2801 if (!data)
2802 return NULL;
2803
2804 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2805 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2806 paddress (get_objfile_arch (objfile), pc));
2807
2808 return dw2_instantiate_symtab (objfile, data);
2809}
2810
9291a0cd 2811static void
44b13c5a 2812dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
9291a0cd
TT
2813 void *data)
2814{
2815 int i;
2816
2817 dw2_setup (objfile);
ae2de4f8 2818
1fd400ff
TT
2819 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2820 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2821 {
2822 int j;
e254ef6a 2823 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2824 struct quick_file_names *file_data;
9291a0cd 2825
e254ef6a 2826 if (per_cu->v.quick->symtab)
9291a0cd
TT
2827 continue;
2828
7b9f3c50
DE
2829 file_data = dw2_get_file_names (objfile, per_cu);
2830 if (file_data == NULL)
9291a0cd
TT
2831 continue;
2832
7b9f3c50 2833 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2834 {
7b9f3c50
DE
2835 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2836 j);
2837 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
2838 }
2839 }
2840}
2841
2842static int
2843dw2_has_symbols (struct objfile *objfile)
2844{
2845 return 1;
2846}
2847
2848const struct quick_symbol_functions dwarf2_gdb_index_functions =
2849{
2850 dw2_has_symbols,
2851 dw2_find_last_source_symtab,
2852 dw2_forget_cached_source_info,
2853 dw2_lookup_symtab,
2854 dw2_lookup_symbol,
774b6a14 2855 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
2856 dw2_print_stats,
2857 dw2_dump,
2858 dw2_relocate,
2859 dw2_expand_symtabs_for_function,
2860 dw2_expand_all_symtabs,
2861 dw2_expand_symtabs_with_filename,
2862 dw2_find_symbol_file,
40658b94 2863 dw2_map_matching_symbols,
9291a0cd
TT
2864 dw2_expand_symtabs_matching,
2865 dw2_find_pc_sect_symtab,
9291a0cd
TT
2866 dw2_map_symbol_filenames
2867};
2868
2869/* Initialize for reading DWARF for this objfile. Return 0 if this
2870 file will use psymtabs, or 1 if using the GNU index. */
2871
2872int
2873dwarf2_initialize_objfile (struct objfile *objfile)
2874{
2875 /* If we're about to read full symbols, don't bother with the
2876 indices. In this case we also don't care if some other debug
2877 format is making psymtabs, because they are all about to be
2878 expanded anyway. */
2879 if ((objfile->flags & OBJF_READNOW))
2880 {
2881 int i;
2882
2883 dwarf2_per_objfile->using_index = 1;
2884 create_all_comp_units (objfile);
1fd400ff 2885 create_debug_types_hash_table (objfile);
7b9f3c50
DE
2886 dwarf2_per_objfile->quick_file_names_table =
2887 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 2888
1fd400ff
TT
2889 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2890 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2891 {
e254ef6a 2892 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2893
e254ef6a
DE
2894 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2895 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
2896 }
2897
2898 /* Return 1 so that gdb sees the "quick" functions. However,
2899 these functions will be no-ops because we will have expanded
2900 all symtabs. */
2901 return 1;
2902 }
2903
2904 if (dwarf2_read_index (objfile))
2905 return 1;
2906
9291a0cd
TT
2907 return 0;
2908}
2909
2910\f
2911
dce234bc
PP
2912/* Build a partial symbol table. */
2913
2914void
f29dff0a 2915dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2916{
f29dff0a 2917 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2918 {
2919 init_psymbol_list (objfile, 1024);
2920 }
2921
d146bf1e 2922 dwarf2_build_psymtabs_hard (objfile);
c906108c 2923}
c906108c 2924
45452591
DE
2925/* Return TRUE if OFFSET is within CU_HEADER. */
2926
2927static inline int
2928offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2929{
2930 unsigned int bottom = cu_header->offset;
2931 unsigned int top = (cu_header->offset
2932 + cu_header->length
2933 + cu_header->initial_length_size);
9a619af0 2934
45452591
DE
2935 return (offset >= bottom && offset < top);
2936}
2937
93311388
DE
2938/* Read in the comp unit header information from the debug_info at info_ptr.
2939 NOTE: This leaves members offset, first_die_offset to be filled in
2940 by the caller. */
107d2387 2941
fe1b8b76 2942static gdb_byte *
107d2387 2943read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2944 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2945{
2946 int signed_addr;
891d2f0b 2947 unsigned int bytes_read;
c764a876
DE
2948
2949 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2950 cu_header->initial_length_size = bytes_read;
2951 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2952 info_ptr += bytes_read;
107d2387
AC
2953 cu_header->version = read_2_bytes (abfd, info_ptr);
2954 info_ptr += 2;
613e1657 2955 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2956 &bytes_read);
613e1657 2957 info_ptr += bytes_read;
107d2387
AC
2958 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2959 info_ptr += 1;
2960 signed_addr = bfd_get_sign_extend_vma (abfd);
2961 if (signed_addr < 0)
8e65ff28 2962 internal_error (__FILE__, __LINE__,
e2e0b3e5 2963 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2964 cu_header->signed_addr_p = signed_addr;
c764a876 2965
107d2387
AC
2966 return info_ptr;
2967}
2968
adabb602
DE
2969/* Read in a CU header and perform some basic error checking. */
2970
fe1b8b76
JB
2971static gdb_byte *
2972partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2973 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2974 bfd *abfd)
2975{
fe1b8b76 2976 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 2977
adabb602
DE
2978 header->offset = beg_of_comp_unit - buffer;
2979
72bf9492
DJ
2980 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2981
adabb602
DE
2982 header->first_die_offset = info_ptr - beg_of_comp_unit;
2983
2dc7f7b3 2984 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2985 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2986 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2987 bfd_get_filename (abfd));
72bf9492 2988
9e0ac564
TT
2989 if (header->abbrev_offset
2990 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2991 &dwarf2_per_objfile->abbrev))
8a3fe4f8
AC
2992 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2993 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2994 (long) header->abbrev_offset,
93311388 2995 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2996 bfd_get_filename (abfd));
2997
2998 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2999 > buffer + buffer_size)
8a3fe4f8
AC
3000 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3001 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 3002 (long) header->length,
93311388 3003 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
3004 bfd_get_filename (abfd));
3005
3006 return info_ptr;
3007}
3008
348e048f
DE
3009/* Read in the types comp unit header information from .debug_types entry at
3010 types_ptr. The result is a pointer to one past the end of the header. */
3011
3012static gdb_byte *
3013read_type_comp_unit_head (struct comp_unit_head *cu_header,
8b70b953 3014 struct dwarf2_section_info *section,
348e048f
DE
3015 ULONGEST *signature,
3016 gdb_byte *types_ptr, bfd *abfd)
3017{
348e048f
DE
3018 gdb_byte *initial_types_ptr = types_ptr;
3019
8b70b953
TT
3020 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3021 cu_header->offset = types_ptr - section->buffer;
348e048f
DE
3022
3023 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3024
3025 *signature = read_8_bytes (abfd, types_ptr);
3026 types_ptr += 8;
3027 types_ptr += cu_header->offset_size;
3028 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3029
3030 return types_ptr;
3031}
3032
aaa75496
JB
3033/* Allocate a new partial symtab for file named NAME and mark this new
3034 partial symtab as being an include of PST. */
3035
3036static void
3037dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3038 struct objfile *objfile)
3039{
3040 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3041
3042 subpst->section_offsets = pst->section_offsets;
3043 subpst->textlow = 0;
3044 subpst->texthigh = 0;
3045
3046 subpst->dependencies = (struct partial_symtab **)
3047 obstack_alloc (&objfile->objfile_obstack,
3048 sizeof (struct partial_symtab *));
3049 subpst->dependencies[0] = pst;
3050 subpst->number_of_dependencies = 1;
3051
3052 subpst->globals_offset = 0;
3053 subpst->n_global_syms = 0;
3054 subpst->statics_offset = 0;
3055 subpst->n_static_syms = 0;
3056 subpst->symtab = NULL;
3057 subpst->read_symtab = pst->read_symtab;
3058 subpst->readin = 0;
3059
3060 /* No private part is necessary for include psymtabs. This property
3061 can be used to differentiate between such include psymtabs and
10b3939b 3062 the regular ones. */
58a9656e 3063 subpst->read_symtab_private = NULL;
aaa75496
JB
3064}
3065
3066/* Read the Line Number Program data and extract the list of files
3067 included by the source file represented by PST. Build an include
d85a05f0 3068 partial symtab for each of these included files. */
aaa75496
JB
3069
3070static void
3071dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 3072 struct die_info *die,
aaa75496
JB
3073 struct partial_symtab *pst)
3074{
3075 struct objfile *objfile = cu->objfile;
3076 bfd *abfd = objfile->obfd;
d85a05f0
DJ
3077 struct line_header *lh = NULL;
3078 struct attribute *attr;
aaa75496 3079
d85a05f0
DJ
3080 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3081 if (attr)
3082 {
3083 unsigned int line_offset = DW_UNSND (attr);
9a619af0 3084
d85a05f0
DJ
3085 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3086 }
aaa75496
JB
3087 if (lh == NULL)
3088 return; /* No linetable, so no includes. */
3089
c6da4cef
DE
3090 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3091 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
aaa75496
JB
3092
3093 free_line_header (lh);
3094}
3095
348e048f
DE
3096static hashval_t
3097hash_type_signature (const void *item)
3098{
3099 const struct signatured_type *type_sig = item;
9a619af0 3100
348e048f
DE
3101 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3102 return type_sig->signature;
3103}
3104
3105static int
3106eq_type_signature (const void *item_lhs, const void *item_rhs)
3107{
3108 const struct signatured_type *lhs = item_lhs;
3109 const struct signatured_type *rhs = item_rhs;
9a619af0 3110
348e048f
DE
3111 return lhs->signature == rhs->signature;
3112}
3113
1fd400ff
TT
3114/* Allocate a hash table for signatured types. */
3115
3116static htab_t
673bfd45 3117allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3118{
3119 return htab_create_alloc_ex (41,
3120 hash_type_signature,
3121 eq_type_signature,
3122 NULL,
3123 &objfile->objfile_obstack,
3124 hashtab_obstack_allocate,
3125 dummy_obstack_deallocate);
3126}
3127
3128/* A helper function to add a signatured type CU to a list. */
3129
3130static int
3131add_signatured_type_cu_to_list (void **slot, void *datum)
3132{
3133 struct signatured_type *sigt = *slot;
3134 struct dwarf2_per_cu_data ***datap = datum;
3135
3136 **datap = &sigt->per_cu;
3137 ++*datap;
3138
3139 return 1;
3140}
3141
348e048f
DE
3142/* Create the hash table of all entries in the .debug_types section.
3143 The result is zero if there is an error (e.g. missing .debug_types section),
3144 otherwise non-zero. */
3145
3146static int
3147create_debug_types_hash_table (struct objfile *objfile)
3148{
8b70b953 3149 htab_t types_htab = NULL;
1fd400ff 3150 struct dwarf2_per_cu_data **iter;
8b70b953
TT
3151 int ix;
3152 struct dwarf2_section_info *section;
348e048f 3153
8b70b953 3154 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
348e048f
DE
3155 {
3156 dwarf2_per_objfile->signatured_types = NULL;
3157 return 0;
3158 }
3159
8b70b953
TT
3160 for (ix = 0;
3161 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3162 ix, section);
3163 ++ix)
3164 {
3165 gdb_byte *info_ptr, *end_ptr;
348e048f 3166
8b70b953
TT
3167 dwarf2_read_section (objfile, section);
3168 info_ptr = section->buffer;
348e048f 3169
8b70b953
TT
3170 if (info_ptr == NULL)
3171 continue;
348e048f 3172
8b70b953
TT
3173 if (types_htab == NULL)
3174 types_htab = allocate_signatured_type_table (objfile);
348e048f 3175
8b70b953
TT
3176 if (dwarf2_die_debug)
3177 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3178
3179 end_ptr = info_ptr + section->size;
3180 while (info_ptr < end_ptr)
3181 {
3182 unsigned int offset;
3183 unsigned int offset_size;
3184 unsigned int type_offset;
3185 unsigned int length, initial_length_size;
3186 unsigned short version;
3187 ULONGEST signature;
3188 struct signatured_type *type_sig;
3189 void **slot;
3190 gdb_byte *ptr = info_ptr;
348e048f 3191
8b70b953 3192 offset = ptr - section->buffer;
348e048f 3193
8b70b953
TT
3194 /* We need to read the type's signature in order to build the hash
3195 table, but we don't need to read anything else just yet. */
348e048f 3196
8b70b953
TT
3197 /* Sanity check to ensure entire cu is present. */
3198 length = read_initial_length (objfile->obfd, ptr,
3199 &initial_length_size);
3200 if (ptr + length + initial_length_size > end_ptr)
3201 {
3202 complaint (&symfile_complaints,
3203 _("debug type entry runs off end "
3204 "of `.debug_types' section, ignored"));
3205 break;
3206 }
348e048f 3207
8b70b953
TT
3208 offset_size = initial_length_size == 4 ? 4 : 8;
3209 ptr += initial_length_size;
3210 version = bfd_get_16 (objfile->obfd, ptr);
3211 ptr += 2;
3212 ptr += offset_size; /* abbrev offset */
3213 ptr += 1; /* address size */
3214 signature = bfd_get_64 (objfile->obfd, ptr);
3215 ptr += 8;
3216 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
4743b735 3217 ptr += offset_size;
6caca83c
CC
3218
3219 /* Skip dummy type units. */
3220 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3221 {
3222 info_ptr = info_ptr + initial_length_size + length;
3223 continue;
3224 }
8b70b953
TT
3225
3226 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3227 memset (type_sig, 0, sizeof (*type_sig));
3228 type_sig->signature = signature;
3229 type_sig->type_offset = type_offset;
3230 type_sig->per_cu.objfile = objfile;
3231 type_sig->per_cu.debug_type_section = section;
3232 type_sig->per_cu.offset = offset;
3233
3234 slot = htab_find_slot (types_htab, type_sig, INSERT);
3235 gdb_assert (slot != NULL);
3236 if (*slot != NULL)
3237 {
3238 const struct signatured_type *dup_sig = *slot;
b3c8eb43 3239
8b70b953
TT
3240 complaint (&symfile_complaints,
3241 _("debug type entry at offset 0x%x is duplicate to the "
3242 "entry at offset 0x%x, signature 0x%s"),
3243 offset, dup_sig->per_cu.offset,
3244 phex (signature, sizeof (signature)));
3245 gdb_assert (signature == dup_sig->signature);
3246 }
3247 *slot = type_sig;
348e048f 3248
8b70b953
TT
3249 if (dwarf2_die_debug)
3250 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3251 offset, phex (signature, sizeof (signature)));
348e048f 3252
8b70b953
TT
3253 info_ptr = info_ptr + initial_length_size + length;
3254 }
348e048f
DE
3255 }
3256
3257 dwarf2_per_objfile->signatured_types = types_htab;
3258
1fd400ff
TT
3259 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3260 dwarf2_per_objfile->type_comp_units
3261 = obstack_alloc (&objfile->objfile_obstack,
3262 dwarf2_per_objfile->n_type_comp_units
3263 * sizeof (struct dwarf2_per_cu_data *));
3264 iter = &dwarf2_per_objfile->type_comp_units[0];
3265 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3266 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3267 == dwarf2_per_objfile->n_type_comp_units);
3268
348e048f
DE
3269 return 1;
3270}
3271
3272/* Lookup a signature based type.
3273 Returns NULL if SIG is not present in the table. */
3274
3275static struct signatured_type *
3276lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3277{
3278 struct signatured_type find_entry, *entry;
3279
3280 if (dwarf2_per_objfile->signatured_types == NULL)
3281 {
3282 complaint (&symfile_complaints,
55f1336d 3283 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
348e048f
DE
3284 return 0;
3285 }
3286
3287 find_entry.signature = sig;
3288 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3289 return entry;
3290}
3291
d85a05f0
DJ
3292/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3293
3294static void
3295init_cu_die_reader (struct die_reader_specs *reader,
3296 struct dwarf2_cu *cu)
3297{
3298 reader->abfd = cu->objfile->obfd;
3299 reader->cu = cu;
8b70b953 3300 if (cu->per_cu->debug_type_section)
be391dca 3301 {
8b70b953
TT
3302 gdb_assert (cu->per_cu->debug_type_section->readin);
3303 reader->buffer = cu->per_cu->debug_type_section->buffer;
be391dca 3304 }
d85a05f0 3305 else
be391dca
TT
3306 {
3307 gdb_assert (dwarf2_per_objfile->info.readin);
3308 reader->buffer = dwarf2_per_objfile->info.buffer;
3309 }
d85a05f0
DJ
3310}
3311
3312/* Find the base address of the compilation unit for range lists and
3313 location lists. It will normally be specified by DW_AT_low_pc.
3314 In DWARF-3 draft 4, the base address could be overridden by
3315 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3316 compilation units with discontinuous ranges. */
3317
3318static void
3319dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3320{
3321 struct attribute *attr;
3322
3323 cu->base_known = 0;
3324 cu->base_address = 0;
3325
3326 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3327 if (attr)
3328 {
3329 cu->base_address = DW_ADDR (attr);
3330 cu->base_known = 1;
3331 }
3332 else
3333 {
3334 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3335 if (attr)
3336 {
3337 cu->base_address = DW_ADDR (attr);
3338 cu->base_known = 1;
3339 }
3340 }
3341}
3342
348e048f
DE
3343/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3344 to combine the common parts.
93311388 3345 Process a compilation unit for a psymtab.
348e048f
DE
3346 BUFFER is a pointer to the beginning of the dwarf section buffer,
3347 either .debug_info or debug_types.
93311388
DE
3348 INFO_PTR is a pointer to the start of the CU.
3349 Returns a pointer to the next CU. */
aaa75496 3350
93311388
DE
3351static gdb_byte *
3352process_psymtab_comp_unit (struct objfile *objfile,
3353 struct dwarf2_per_cu_data *this_cu,
3354 gdb_byte *buffer, gdb_byte *info_ptr,
3355 unsigned int buffer_size)
c906108c 3356{
c906108c 3357 bfd *abfd = objfile->obfd;
93311388 3358 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 3359 struct die_info *comp_unit_die;
c906108c 3360 struct partial_symtab *pst;
5734ee8b 3361 CORE_ADDR baseaddr;
93311388
DE
3362 struct cleanup *back_to_inner;
3363 struct dwarf2_cu cu;
d85a05f0
DJ
3364 int has_children, has_pc_info;
3365 struct attribute *attr;
d85a05f0
DJ
3366 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3367 struct die_reader_specs reader_specs;
3e2a0cee 3368 const char *filename;
c906108c 3369
9816fde3 3370 init_one_comp_unit (&cu, objfile);
93311388 3371 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 3372
93311388
DE
3373 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3374 buffer, buffer_size,
3375 abfd);
10b3939b 3376
6caca83c
CC
3377 /* Skip dummy compilation units. */
3378 if (info_ptr >= buffer + buffer_size
3379 || peek_abbrev_code (abfd, info_ptr) == 0)
3380 {
3381 info_ptr = (beg_of_comp_unit + cu.header.length
3382 + cu.header.initial_length_size);
3383 do_cleanups (back_to_inner);
3384 return info_ptr;
3385 }
3386
93311388 3387 cu.list_in_scope = &file_symbols;
af703f96 3388
328c9494
DJ
3389 /* If this compilation unit was already read in, free the
3390 cached copy in order to read it in again. This is
3391 necessary because we skipped some symbols when we first
3392 read in the compilation unit (see load_partial_dies).
3393 This problem could be avoided, but the benefit is
3394 unclear. */
3395 if (this_cu->cu != NULL)
3396 free_one_cached_comp_unit (this_cu->cu);
3397
3398 /* Note that this is a pointer to our stack frame, being
3399 added to a global data structure. It will be cleaned up
3400 in free_stack_comp_unit when we finish with this
3401 compilation unit. */
3402 this_cu->cu = &cu;
d85a05f0
DJ
3403 cu.per_cu = this_cu;
3404
93311388
DE
3405 /* Read the abbrevs for this compilation unit into a table. */
3406 dwarf2_read_abbrevs (abfd, &cu);
3407 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 3408
93311388 3409 /* Read the compilation unit die. */
8b70b953 3410 if (this_cu->debug_type_section)
348e048f 3411 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
3412 init_cu_die_reader (&reader_specs, &cu);
3413 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3414 &has_children);
93311388 3415
8b70b953 3416 if (this_cu->debug_type_section)
348e048f 3417 {
b3c8eb43
JK
3418 /* LENGTH has not been set yet for type units. */
3419 gdb_assert (this_cu->offset == cu.header.offset);
348e048f
DE
3420 this_cu->length = cu.header.length + cu.header.initial_length_size;
3421 }
d85a05f0 3422 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 3423 {
93311388
DE
3424 info_ptr = (beg_of_comp_unit + cu.header.length
3425 + cu.header.initial_length_size);
3426 do_cleanups (back_to_inner);
3427 return info_ptr;
3428 }
72bf9492 3429
9816fde3 3430 prepare_one_comp_unit (&cu, comp_unit_die);
c906108c 3431
93311388 3432 /* Allocate a new partial symbol table structure. */
d85a05f0 3433 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3e2a0cee
TT
3434 if (attr == NULL || !DW_STRING (attr))
3435 filename = "";
3436 else
3437 filename = DW_STRING (attr);
93311388 3438 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 3439 filename,
93311388
DE
3440 /* TEXTLOW and TEXTHIGH are set below. */
3441 0,
3442 objfile->global_psymbols.next,
3443 objfile->static_psymbols.next);
72bf9492 3444
d85a05f0
DJ
3445 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3446 if (attr != NULL)
3447 pst->dirname = DW_STRING (attr);
72bf9492 3448
e38df1d0 3449 pst->read_symtab_private = this_cu;
72bf9492 3450
93311388 3451 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3452
0963b4bd 3453 /* Store the function that reads in the rest of the symbol table. */
93311388 3454 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3455
9291a0cd 3456 this_cu->v.psymtab = pst;
c906108c 3457
d85a05f0
DJ
3458 dwarf2_find_base_address (comp_unit_die, &cu);
3459
93311388
DE
3460 /* Possibly set the default values of LOWPC and HIGHPC from
3461 `DW_AT_ranges'. */
d85a05f0
DJ
3462 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3463 &best_highpc, &cu, pst);
3464 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3465 /* Store the contiguous range if it is not empty; it can be empty for
3466 CUs with no code. */
3467 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3468 best_lowpc + baseaddr,
3469 best_highpc + baseaddr - 1, pst);
93311388
DE
3470
3471 /* Check if comp unit has_children.
3472 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3473 If not, there's no more debug_info for this comp unit. */
d85a05f0 3474 if (has_children)
93311388
DE
3475 {
3476 struct partial_die_info *first_die;
3477 CORE_ADDR lowpc, highpc;
31ffec48 3478
93311388
DE
3479 lowpc = ((CORE_ADDR) -1);
3480 highpc = ((CORE_ADDR) 0);
c906108c 3481
93311388 3482 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3483
93311388 3484 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3485 ! has_pc_info, &cu);
57c22c6c 3486
93311388
DE
3487 /* If we didn't find a lowpc, set it to highpc to avoid
3488 complaints from `maint check'. */
3489 if (lowpc == ((CORE_ADDR) -1))
3490 lowpc = highpc;
10b3939b 3491
93311388
DE
3492 /* If the compilation unit didn't have an explicit address range,
3493 then use the information extracted from its child dies. */
d85a05f0 3494 if (! has_pc_info)
93311388 3495 {
d85a05f0
DJ
3496 best_lowpc = lowpc;
3497 best_highpc = highpc;
93311388
DE
3498 }
3499 }
d85a05f0
DJ
3500 pst->textlow = best_lowpc + baseaddr;
3501 pst->texthigh = best_highpc + baseaddr;
c906108c 3502
93311388
DE
3503 pst->n_global_syms = objfile->global_psymbols.next -
3504 (objfile->global_psymbols.list + pst->globals_offset);
3505 pst->n_static_syms = objfile->static_psymbols.next -
3506 (objfile->static_psymbols.list + pst->statics_offset);
3507 sort_pst_symbols (pst);
c906108c 3508
93311388
DE
3509 info_ptr = (beg_of_comp_unit + cu.header.length
3510 + cu.header.initial_length_size);
ae038cb0 3511
8b70b953 3512 if (this_cu->debug_type_section)
348e048f
DE
3513 {
3514 /* It's not clear we want to do anything with stmt lists here.
3515 Waiting to see what gcc ultimately does. */
3516 }
d85a05f0 3517 else
93311388
DE
3518 {
3519 /* Get the list of files included in the current compilation unit,
3520 and build a psymtab for each of them. */
d85a05f0 3521 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3522 }
ae038cb0 3523
93311388 3524 do_cleanups (back_to_inner);
ae038cb0 3525
93311388
DE
3526 return info_ptr;
3527}
ff013f42 3528
348e048f
DE
3529/* Traversal function for htab_traverse_noresize.
3530 Process one .debug_types comp-unit. */
3531
3532static int
3533process_type_comp_unit (void **slot, void *info)
3534{
3535 struct signatured_type *entry = (struct signatured_type *) *slot;
3536 struct objfile *objfile = (struct objfile *) info;
3537 struct dwarf2_per_cu_data *this_cu;
3538
3539 this_cu = &entry->per_cu;
348e048f 3540
8b70b953 3541 gdb_assert (this_cu->debug_type_section->readin);
348e048f 3542 process_psymtab_comp_unit (objfile, this_cu,
8b70b953
TT
3543 this_cu->debug_type_section->buffer,
3544 (this_cu->debug_type_section->buffer
3545 + this_cu->offset),
3546 this_cu->debug_type_section->size);
348e048f
DE
3547
3548 return 1;
3549}
3550
3551/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3552 Build partial symbol tables for the .debug_types comp-units. */
3553
3554static void
3555build_type_psymtabs (struct objfile *objfile)
3556{
3557 if (! create_debug_types_hash_table (objfile))
3558 return;
3559
3560 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3561 process_type_comp_unit, objfile);
3562}
3563
60606b2c
TT
3564/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3565
3566static void
3567psymtabs_addrmap_cleanup (void *o)
3568{
3569 struct objfile *objfile = o;
ec61707d 3570
60606b2c
TT
3571 objfile->psymtabs_addrmap = NULL;
3572}
3573
93311388
DE
3574/* Build the partial symbol table by doing a quick pass through the
3575 .debug_info and .debug_abbrev sections. */
72bf9492 3576
93311388 3577static void
c67a9c90 3578dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3579{
93311388 3580 gdb_byte *info_ptr;
60606b2c
TT
3581 struct cleanup *back_to, *addrmap_cleanup;
3582 struct obstack temp_obstack;
93311388 3583
98bfdba5
PA
3584 dwarf2_per_objfile->reading_partial_symbols = 1;
3585
be391dca 3586 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3587 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3588
93311388
DE
3589 /* Any cached compilation units will be linked by the per-objfile
3590 read_in_chain. Make sure to free them when we're done. */
3591 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3592
348e048f
DE
3593 build_type_psymtabs (objfile);
3594
93311388 3595 create_all_comp_units (objfile);
c906108c 3596
60606b2c
TT
3597 /* Create a temporary address map on a temporary obstack. We later
3598 copy this to the final obstack. */
3599 obstack_init (&temp_obstack);
3600 make_cleanup_obstack_free (&temp_obstack);
3601 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3602 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3603
93311388
DE
3604 /* Since the objects we're extracting from .debug_info vary in
3605 length, only the individual functions to extract them (like
3606 read_comp_unit_head and load_partial_die) can really know whether
3607 the buffer is large enough to hold another complete object.
c906108c 3608
93311388
DE
3609 At the moment, they don't actually check that. If .debug_info
3610 holds just one extra byte after the last compilation unit's dies,
3611 then read_comp_unit_head will happily read off the end of the
3612 buffer. read_partial_die is similarly casual. Those functions
3613 should be fixed.
c906108c 3614
93311388
DE
3615 For this loop condition, simply checking whether there's any data
3616 left at all should be sufficient. */
c906108c 3617
93311388
DE
3618 while (info_ptr < (dwarf2_per_objfile->info.buffer
3619 + dwarf2_per_objfile->info.size))
3620 {
3621 struct dwarf2_per_cu_data *this_cu;
dd373385 3622
3e43a32a
MS
3623 this_cu = dwarf2_find_comp_unit (info_ptr
3624 - dwarf2_per_objfile->info.buffer,
93311388 3625 objfile);
aaa75496 3626
93311388
DE
3627 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3628 dwarf2_per_objfile->info.buffer,
3629 info_ptr,
3630 dwarf2_per_objfile->info.size);
c906108c 3631 }
ff013f42
JK
3632
3633 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3634 &objfile->objfile_obstack);
60606b2c 3635 discard_cleanups (addrmap_cleanup);
ff013f42 3636
ae038cb0
DJ
3637 do_cleanups (back_to);
3638}
3639
93311388 3640/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3641
3642static void
93311388
DE
3643load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3644 struct objfile *objfile)
ae038cb0
DJ
3645{
3646 bfd *abfd = objfile->obfd;
adabb602 3647 gdb_byte *info_ptr;
d85a05f0 3648 struct die_info *comp_unit_die;
ae038cb0 3649 struct dwarf2_cu *cu;
1d9ec526 3650 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
d85a05f0
DJ
3651 int has_children;
3652 struct die_reader_specs reader_specs;
98bfdba5 3653 int read_cu = 0;
ae038cb0 3654
8b70b953 3655 gdb_assert (! this_cu->debug_type_section);
348e048f 3656
be391dca 3657 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3658 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0 3659
98bfdba5
PA
3660 if (this_cu->cu == NULL)
3661 {
9816fde3
JK
3662 cu = xmalloc (sizeof (*cu));
3663 init_one_comp_unit (cu, objfile);
ae038cb0 3664
98bfdba5 3665 read_cu = 1;
ae038cb0 3666
98bfdba5
PA
3667 /* If an error occurs while loading, release our storage. */
3668 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
328c9494 3669
98bfdba5
PA
3670 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3671 dwarf2_per_objfile->info.buffer,
3672 dwarf2_per_objfile->info.size,
3673 abfd);
ae038cb0 3674
6caca83c
CC
3675 /* Skip dummy compilation units. */
3676 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3677 + dwarf2_per_objfile->info.size)
3678 || peek_abbrev_code (abfd, info_ptr) == 0)
3679 {
3680 do_cleanups (free_cu_cleanup);
3681 return;
3682 }
3683
98bfdba5
PA
3684 /* Link this compilation unit into the compilation unit tree. */
3685 this_cu->cu = cu;
3686 cu->per_cu = this_cu;
98bfdba5
PA
3687
3688 /* Link this CU into read_in_chain. */
3689 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3690 dwarf2_per_objfile->read_in_chain = this_cu;
3691 }
3692 else
3693 {
3694 cu = this_cu->cu;
3695 info_ptr += cu->header.first_die_offset;
3696 }
ae038cb0
DJ
3697
3698 /* Read the abbrevs for this compilation unit into a table. */
98bfdba5 3699 gdb_assert (cu->dwarf2_abbrevs == NULL);
ae038cb0 3700 dwarf2_read_abbrevs (abfd, cu);
98bfdba5 3701 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
ae038cb0
DJ
3702
3703 /* Read the compilation unit die. */
d85a05f0
DJ
3704 init_cu_die_reader (&reader_specs, cu);
3705 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3706 &has_children);
ae038cb0 3707
9816fde3 3708 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 3709
ae038cb0
DJ
3710 /* Check if comp unit has_children.
3711 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 3712 If not, there's no more debug_info for this comp unit. */
d85a05f0 3713 if (has_children)
93311388 3714 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0 3715
98bfdba5
PA
3716 do_cleanups (free_abbrevs_cleanup);
3717
3718 if (read_cu)
3719 {
3720 /* We've successfully allocated this compilation unit. Let our
3721 caller clean it up when finished with it. */
3722 discard_cleanups (free_cu_cleanup);
3723 }
ae038cb0
DJ
3724}
3725
3726/* Create a list of all compilation units in OBJFILE. We do this only
3727 if an inter-comp-unit reference is found; presumably if there is one,
3728 there will be many, and one will occur early in the .debug_info section.
3729 So there's no point in building this list incrementally. */
3730
3731static void
3732create_all_comp_units (struct objfile *objfile)
3733{
3734 int n_allocated;
3735 int n_comp_units;
3736 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3737 gdb_byte *info_ptr;
3738
3739 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3740 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3741
3742 n_comp_units = 0;
3743 n_allocated = 10;
3744 all_comp_units = xmalloc (n_allocated
3745 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3746
3e43a32a
MS
3747 while (info_ptr < dwarf2_per_objfile->info.buffer
3748 + dwarf2_per_objfile->info.size)
ae038cb0 3749 {
c764a876 3750 unsigned int length, initial_length_size;
ae038cb0 3751 struct dwarf2_per_cu_data *this_cu;
c764a876 3752 unsigned int offset;
ae038cb0 3753
dce234bc 3754 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3755
3756 /* Read just enough information to find out where the next
3757 compilation unit is. */
c764a876
DE
3758 length = read_initial_length (objfile->obfd, info_ptr,
3759 &initial_length_size);
ae038cb0
DJ
3760
3761 /* Save the compilation unit for later lookup. */
3762 this_cu = obstack_alloc (&objfile->objfile_obstack,
3763 sizeof (struct dwarf2_per_cu_data));
3764 memset (this_cu, 0, sizeof (*this_cu));
3765 this_cu->offset = offset;
c764a876 3766 this_cu->length = length + initial_length_size;
9291a0cd 3767 this_cu->objfile = objfile;
ae038cb0
DJ
3768
3769 if (n_comp_units == n_allocated)
3770 {
3771 n_allocated *= 2;
3772 all_comp_units = xrealloc (all_comp_units,
3773 n_allocated
3774 * sizeof (struct dwarf2_per_cu_data *));
3775 }
3776 all_comp_units[n_comp_units++] = this_cu;
3777
3778 info_ptr = info_ptr + this_cu->length;
3779 }
3780
3781 dwarf2_per_objfile->all_comp_units
3782 = obstack_alloc (&objfile->objfile_obstack,
3783 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3784 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3785 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3786 xfree (all_comp_units);
3787 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3788}
3789
5734ee8b
DJ
3790/* Process all loaded DIEs for compilation unit CU, starting at
3791 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3792 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3793 DW_AT_ranges). If NEED_PC is set, then this function will set
3794 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3795 and record the covered ranges in the addrmap. */
c906108c 3796
72bf9492
DJ
3797static void
3798scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3799 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3800{
72bf9492 3801 struct partial_die_info *pdi;
c906108c 3802
91c24f0a
DC
3803 /* Now, march along the PDI's, descending into ones which have
3804 interesting children but skipping the children of the other ones,
3805 until we reach the end of the compilation unit. */
c906108c 3806
72bf9492 3807 pdi = first_die;
91c24f0a 3808
72bf9492
DJ
3809 while (pdi != NULL)
3810 {
3811 fixup_partial_die (pdi, cu);
c906108c 3812
f55ee35c 3813 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3814 children, so we need to look at them. Ditto for anonymous
3815 enums. */
933c6fe4 3816
72bf9492 3817 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3818 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3819 {
72bf9492 3820 switch (pdi->tag)
c906108c
SS
3821 {
3822 case DW_TAG_subprogram:
5734ee8b 3823 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 3824 break;
72929c62 3825 case DW_TAG_constant:
c906108c
SS
3826 case DW_TAG_variable:
3827 case DW_TAG_typedef:
91c24f0a 3828 case DW_TAG_union_type:
72bf9492 3829 if (!pdi->is_declaration)
63d06c5c 3830 {
72bf9492 3831 add_partial_symbol (pdi, cu);
63d06c5c
DC
3832 }
3833 break;
c906108c 3834 case DW_TAG_class_type:
680b30c7 3835 case DW_TAG_interface_type:
c906108c 3836 case DW_TAG_structure_type:
72bf9492 3837 if (!pdi->is_declaration)
c906108c 3838 {
72bf9492 3839 add_partial_symbol (pdi, cu);
c906108c
SS
3840 }
3841 break;
91c24f0a 3842 case DW_TAG_enumeration_type:
72bf9492
DJ
3843 if (!pdi->is_declaration)
3844 add_partial_enumeration (pdi, cu);
c906108c
SS
3845 break;
3846 case DW_TAG_base_type:
a02abb62 3847 case DW_TAG_subrange_type:
c906108c 3848 /* File scope base type definitions are added to the partial
c5aa993b 3849 symbol table. */
72bf9492 3850 add_partial_symbol (pdi, cu);
c906108c 3851 break;
d9fa45fe 3852 case DW_TAG_namespace:
5734ee8b 3853 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3854 break;
5d7cb8df
JK
3855 case DW_TAG_module:
3856 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3857 break;
c906108c
SS
3858 default:
3859 break;
3860 }
3861 }
3862
72bf9492
DJ
3863 /* If the die has a sibling, skip to the sibling. */
3864
3865 pdi = pdi->die_sibling;
3866 }
3867}
3868
3869/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3870
72bf9492 3871 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3872 name is concatenated with "::" and the partial DIE's name. For
3873 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3874 Enumerators are an exception; they use the scope of their parent
3875 enumeration type, i.e. the name of the enumeration type is not
3876 prepended to the enumerator.
91c24f0a 3877
72bf9492
DJ
3878 There are two complexities. One is DW_AT_specification; in this
3879 case "parent" means the parent of the target of the specification,
3880 instead of the direct parent of the DIE. The other is compilers
3881 which do not emit DW_TAG_namespace; in this case we try to guess
3882 the fully qualified name of structure types from their members'
3883 linkage names. This must be done using the DIE's children rather
3884 than the children of any DW_AT_specification target. We only need
3885 to do this for structures at the top level, i.e. if the target of
3886 any DW_AT_specification (if any; otherwise the DIE itself) does not
3887 have a parent. */
3888
3889/* Compute the scope prefix associated with PDI's parent, in
3890 compilation unit CU. The result will be allocated on CU's
3891 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3892 field. NULL is returned if no prefix is necessary. */
3893static char *
3894partial_die_parent_scope (struct partial_die_info *pdi,
3895 struct dwarf2_cu *cu)
3896{
3897 char *grandparent_scope;
3898 struct partial_die_info *parent, *real_pdi;
91c24f0a 3899
72bf9492
DJ
3900 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3901 then this means the parent of the specification DIE. */
3902
3903 real_pdi = pdi;
72bf9492 3904 while (real_pdi->has_specification)
10b3939b 3905 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3906
3907 parent = real_pdi->die_parent;
3908 if (parent == NULL)
3909 return NULL;
3910
3911 if (parent->scope_set)
3912 return parent->scope;
3913
3914 fixup_partial_die (parent, cu);
3915
10b3939b 3916 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3917
acebe513
UW
3918 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3919 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3920 Work around this problem here. */
3921 if (cu->language == language_cplus
6e70227d 3922 && parent->tag == DW_TAG_namespace
acebe513
UW
3923 && strcmp (parent->name, "::") == 0
3924 && grandparent_scope == NULL)
3925 {
3926 parent->scope = NULL;
3927 parent->scope_set = 1;
3928 return NULL;
3929 }
3930
9c6c53f7
SA
3931 if (pdi->tag == DW_TAG_enumerator)
3932 /* Enumerators should not get the name of the enumeration as a prefix. */
3933 parent->scope = grandparent_scope;
3934 else if (parent->tag == DW_TAG_namespace
f55ee35c 3935 || parent->tag == DW_TAG_module
72bf9492
DJ
3936 || parent->tag == DW_TAG_structure_type
3937 || parent->tag == DW_TAG_class_type
680b30c7 3938 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3939 || parent->tag == DW_TAG_union_type
3940 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3941 {
3942 if (grandparent_scope == NULL)
3943 parent->scope = parent->name;
3944 else
3e43a32a
MS
3945 parent->scope = typename_concat (&cu->comp_unit_obstack,
3946 grandparent_scope,
f55ee35c 3947 parent->name, 0, cu);
72bf9492 3948 }
72bf9492
DJ
3949 else
3950 {
3951 /* FIXME drow/2004-04-01: What should we be doing with
3952 function-local names? For partial symbols, we should probably be
3953 ignoring them. */
3954 complaint (&symfile_complaints,
e2e0b3e5 3955 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3956 parent->tag, pdi->offset);
3957 parent->scope = grandparent_scope;
c906108c
SS
3958 }
3959
72bf9492
DJ
3960 parent->scope_set = 1;
3961 return parent->scope;
3962}
3963
3964/* Return the fully scoped name associated with PDI, from compilation unit
3965 CU. The result will be allocated with malloc. */
3966static char *
3967partial_die_full_name (struct partial_die_info *pdi,
3968 struct dwarf2_cu *cu)
3969{
3970 char *parent_scope;
3971
98bfdba5
PA
3972 /* If this is a template instantiation, we can not work out the
3973 template arguments from partial DIEs. So, unfortunately, we have
3974 to go through the full DIEs. At least any work we do building
3975 types here will be reused if full symbols are loaded later. */
3976 if (pdi->has_template_arguments)
3977 {
3978 fixup_partial_die (pdi, cu);
3979
3980 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3981 {
3982 struct die_info *die;
3983 struct attribute attr;
3984 struct dwarf2_cu *ref_cu = cu;
3985
3986 attr.name = 0;
3987 attr.form = DW_FORM_ref_addr;
3988 attr.u.addr = pdi->offset;
3989 die = follow_die_ref (NULL, &attr, &ref_cu);
3990
3991 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3992 }
3993 }
3994
72bf9492
DJ
3995 parent_scope = partial_die_parent_scope (pdi, cu);
3996 if (parent_scope == NULL)
3997 return NULL;
3998 else
f55ee35c 3999 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4000}
4001
4002static void
72bf9492 4003add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4004{
e7c27a73 4005 struct objfile *objfile = cu->objfile;
c906108c 4006 CORE_ADDR addr = 0;
decbce07 4007 char *actual_name = NULL;
5c4e30ca 4008 const struct partial_symbol *psym = NULL;
e142c38c 4009 CORE_ADDR baseaddr;
72bf9492 4010 int built_actual_name = 0;
e142c38c
DJ
4011
4012 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4013
94af9270
KS
4014 actual_name = partial_die_full_name (pdi, cu);
4015 if (actual_name)
4016 built_actual_name = 1;
63d06c5c 4017
72bf9492
DJ
4018 if (actual_name == NULL)
4019 actual_name = pdi->name;
4020
c906108c
SS
4021 switch (pdi->tag)
4022 {
4023 case DW_TAG_subprogram:
2cfa0c8d 4024 if (pdi->is_external || cu->language == language_ada)
c906108c 4025 {
2cfa0c8d
JB
4026 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4027 of the global scope. But in Ada, we want to be able to access
4028 nested procedures globally. So all Ada subprograms are stored
4029 in the global scope. */
f47fb265 4030 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4031 mst_text, objfile); */
f47fb265
MS
4032 add_psymbol_to_list (actual_name, strlen (actual_name),
4033 built_actual_name,
4034 VAR_DOMAIN, LOC_BLOCK,
4035 &objfile->global_psymbols,
4036 0, pdi->lowpc + baseaddr,
4037 cu->language, objfile);
c906108c
SS
4038 }
4039 else
4040 {
f47fb265 4041 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4042 mst_file_text, objfile); */
f47fb265
MS
4043 add_psymbol_to_list (actual_name, strlen (actual_name),
4044 built_actual_name,
4045 VAR_DOMAIN, LOC_BLOCK,
4046 &objfile->static_psymbols,
4047 0, pdi->lowpc + baseaddr,
4048 cu->language, objfile);
c906108c
SS
4049 }
4050 break;
72929c62
JB
4051 case DW_TAG_constant:
4052 {
4053 struct psymbol_allocation_list *list;
4054
4055 if (pdi->is_external)
4056 list = &objfile->global_psymbols;
4057 else
4058 list = &objfile->static_psymbols;
f47fb265
MS
4059 add_psymbol_to_list (actual_name, strlen (actual_name),
4060 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4061 list, 0, 0, cu->language, objfile);
72929c62
JB
4062 }
4063 break;
c906108c 4064 case DW_TAG_variable:
caac4577
JG
4065 if (pdi->locdesc)
4066 addr = decode_locdesc (pdi->locdesc, cu);
4067
4068 if (pdi->locdesc
4069 && addr == 0
4070 && !dwarf2_per_objfile->has_section_at_zero)
4071 {
4072 /* A global or static variable may also have been stripped
4073 out by the linker if unused, in which case its address
4074 will be nullified; do not add such variables into partial
4075 symbol table then. */
4076 }
4077 else if (pdi->is_external)
c906108c
SS
4078 {
4079 /* Global Variable.
4080 Don't enter into the minimal symbol tables as there is
4081 a minimal symbol table entry from the ELF symbols already.
4082 Enter into partial symbol table if it has a location
4083 descriptor or a type.
4084 If the location descriptor is missing, new_symbol will create
4085 a LOC_UNRESOLVED symbol, the address of the variable will then
4086 be determined from the minimal symbol table whenever the variable
4087 is referenced.
4088 The address for the partial symbol table entry is not
4089 used by GDB, but it comes in handy for debugging partial symbol
4090 table building. */
4091
c906108c 4092 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4093 add_psymbol_to_list (actual_name, strlen (actual_name),
4094 built_actual_name,
4095 VAR_DOMAIN, LOC_STATIC,
4096 &objfile->global_psymbols,
4097 0, addr + baseaddr,
4098 cu->language, objfile);
c906108c
SS
4099 }
4100 else
4101 {
0963b4bd 4102 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4103 if (pdi->locdesc == NULL)
decbce07
MS
4104 {
4105 if (built_actual_name)
4106 xfree (actual_name);
4107 return;
4108 }
f47fb265 4109 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4110 mst_file_data, objfile); */
f47fb265
MS
4111 add_psymbol_to_list (actual_name, strlen (actual_name),
4112 built_actual_name,
4113 VAR_DOMAIN, LOC_STATIC,
4114 &objfile->static_psymbols,
4115 0, addr + baseaddr,
4116 cu->language, objfile);
c906108c
SS
4117 }
4118 break;
4119 case DW_TAG_typedef:
4120 case DW_TAG_base_type:
a02abb62 4121 case DW_TAG_subrange_type:
38d518c9 4122 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4123 built_actual_name,
176620f1 4124 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4125 &objfile->static_psymbols,
e142c38c 4126 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4127 break;
72bf9492
DJ
4128 case DW_TAG_namespace:
4129 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4130 built_actual_name,
72bf9492
DJ
4131 VAR_DOMAIN, LOC_TYPEDEF,
4132 &objfile->global_psymbols,
4133 0, (CORE_ADDR) 0, cu->language, objfile);
4134 break;
c906108c 4135 case DW_TAG_class_type:
680b30c7 4136 case DW_TAG_interface_type:
c906108c
SS
4137 case DW_TAG_structure_type:
4138 case DW_TAG_union_type:
4139 case DW_TAG_enumeration_type:
fa4028e9
JB
4140 /* Skip external references. The DWARF standard says in the section
4141 about "Structure, Union, and Class Type Entries": "An incomplete
4142 structure, union or class type is represented by a structure,
4143 union or class entry that does not have a byte size attribute
4144 and that has a DW_AT_declaration attribute." */
4145 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4146 {
4147 if (built_actual_name)
4148 xfree (actual_name);
4149 return;
4150 }
fa4028e9 4151
63d06c5c
DC
4152 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4153 static vs. global. */
38d518c9 4154 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4155 built_actual_name,
176620f1 4156 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4157 (cu->language == language_cplus
4158 || cu->language == language_java)
63d06c5c
DC
4159 ? &objfile->global_psymbols
4160 : &objfile->static_psymbols,
e142c38c 4161 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4162
c906108c
SS
4163 break;
4164 case DW_TAG_enumerator:
38d518c9 4165 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4166 built_actual_name,
176620f1 4167 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4168 (cu->language == language_cplus
4169 || cu->language == language_java)
f6fe98ef
DJ
4170 ? &objfile->global_psymbols
4171 : &objfile->static_psymbols,
e142c38c 4172 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4173 break;
4174 default:
4175 break;
4176 }
5c4e30ca 4177
72bf9492
DJ
4178 if (built_actual_name)
4179 xfree (actual_name);
c906108c
SS
4180}
4181
5c4e30ca
DC
4182/* Read a partial die corresponding to a namespace; also, add a symbol
4183 corresponding to that namespace to the symbol table. NAMESPACE is
4184 the name of the enclosing namespace. */
91c24f0a 4185
72bf9492
DJ
4186static void
4187add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4188 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4189 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4190{
72bf9492 4191 /* Add a symbol for the namespace. */
e7c27a73 4192
72bf9492 4193 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4194
4195 /* Now scan partial symbols in that namespace. */
4196
91c24f0a 4197 if (pdi->has_children)
5734ee8b 4198 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4199}
4200
5d7cb8df
JK
4201/* Read a partial die corresponding to a Fortran module. */
4202
4203static void
4204add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4205 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4206{
f55ee35c 4207 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4208
4209 if (pdi->has_children)
4210 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4211}
4212
bc30ff58
JB
4213/* Read a partial die corresponding to a subprogram and create a partial
4214 symbol for that subprogram. When the CU language allows it, this
4215 routine also defines a partial symbol for each nested subprogram
4216 that this subprogram contains.
6e70227d 4217
bc30ff58
JB
4218 DIE my also be a lexical block, in which case we simply search
4219 recursively for suprograms defined inside that lexical block.
4220 Again, this is only performed when the CU language allows this
4221 type of definitions. */
4222
4223static void
4224add_partial_subprogram (struct partial_die_info *pdi,
4225 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4226 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4227{
4228 if (pdi->tag == DW_TAG_subprogram)
4229 {
4230 if (pdi->has_pc_info)
4231 {
4232 if (pdi->lowpc < *lowpc)
4233 *lowpc = pdi->lowpc;
4234 if (pdi->highpc > *highpc)
4235 *highpc = pdi->highpc;
5734ee8b
DJ
4236 if (need_pc)
4237 {
4238 CORE_ADDR baseaddr;
4239 struct objfile *objfile = cu->objfile;
4240
4241 baseaddr = ANOFFSET (objfile->section_offsets,
4242 SECT_OFF_TEXT (objfile));
4243 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4244 pdi->lowpc + baseaddr,
4245 pdi->highpc - 1 + baseaddr,
9291a0cd 4246 cu->per_cu->v.psymtab);
5734ee8b 4247 }
bc30ff58 4248 if (!pdi->is_declaration)
e8d05480
JB
4249 /* Ignore subprogram DIEs that do not have a name, they are
4250 illegal. Do not emit a complaint at this point, we will
4251 do so when we convert this psymtab into a symtab. */
4252 if (pdi->name)
4253 add_partial_symbol (pdi, cu);
bc30ff58
JB
4254 }
4255 }
6e70227d 4256
bc30ff58
JB
4257 if (! pdi->has_children)
4258 return;
4259
4260 if (cu->language == language_ada)
4261 {
4262 pdi = pdi->die_child;
4263 while (pdi != NULL)
4264 {
4265 fixup_partial_die (pdi, cu);
4266 if (pdi->tag == DW_TAG_subprogram
4267 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4268 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4269 pdi = pdi->die_sibling;
4270 }
4271 }
4272}
4273
91c24f0a
DC
4274/* Read a partial die corresponding to an enumeration type. */
4275
72bf9492
DJ
4276static void
4277add_partial_enumeration (struct partial_die_info *enum_pdi,
4278 struct dwarf2_cu *cu)
91c24f0a 4279{
72bf9492 4280 struct partial_die_info *pdi;
91c24f0a
DC
4281
4282 if (enum_pdi->name != NULL)
72bf9492
DJ
4283 add_partial_symbol (enum_pdi, cu);
4284
4285 pdi = enum_pdi->die_child;
4286 while (pdi)
91c24f0a 4287 {
72bf9492 4288 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4289 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4290 else
72bf9492
DJ
4291 add_partial_symbol (pdi, cu);
4292 pdi = pdi->die_sibling;
91c24f0a 4293 }
91c24f0a
DC
4294}
4295
6caca83c
CC
4296/* Return the initial uleb128 in the die at INFO_PTR. */
4297
4298static unsigned int
4299peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4300{
4301 unsigned int bytes_read;
4302
4303 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4304}
4305
4bb7a0a7
DJ
4306/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4307 Return the corresponding abbrev, or NULL if the number is zero (indicating
4308 an empty DIE). In either case *BYTES_READ will be set to the length of
4309 the initial number. */
4310
4311static struct abbrev_info *
fe1b8b76 4312peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4313 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4314{
4315 bfd *abfd = cu->objfile->obfd;
4316 unsigned int abbrev_number;
4317 struct abbrev_info *abbrev;
4318
4319 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4320
4321 if (abbrev_number == 0)
4322 return NULL;
4323
4324 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4325 if (!abbrev)
4326 {
3e43a32a
MS
4327 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4328 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4329 }
4330
4331 return abbrev;
4332}
4333
93311388
DE
4334/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4335 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4336 DIE. Any children of the skipped DIEs will also be skipped. */
4337
fe1b8b76 4338static gdb_byte *
93311388 4339skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4340{
4341 struct abbrev_info *abbrev;
4342 unsigned int bytes_read;
4343
4344 while (1)
4345 {
4346 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4347 if (abbrev == NULL)
4348 return info_ptr + bytes_read;
4349 else
93311388 4350 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
4351 }
4352}
4353
93311388
DE
4354/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4355 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4356 abbrev corresponding to that skipped uleb128 should be passed in
4357 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4358 children. */
4359
fe1b8b76 4360static gdb_byte *
93311388
DE
4361skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4362 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
4363{
4364 unsigned int bytes_read;
4365 struct attribute attr;
4366 bfd *abfd = cu->objfile->obfd;
4367 unsigned int form, i;
4368
4369 for (i = 0; i < abbrev->num_attrs; i++)
4370 {
4371 /* The only abbrev we care about is DW_AT_sibling. */
4372 if (abbrev->attrs[i].name == DW_AT_sibling)
4373 {
4374 read_attribute (&attr, &abbrev->attrs[i],
4375 abfd, info_ptr, cu);
4376 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4377 complaint (&symfile_complaints,
4378 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4379 else
93311388 4380 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
4381 }
4382
4383 /* If it isn't DW_AT_sibling, skip this attribute. */
4384 form = abbrev->attrs[i].form;
4385 skip_attribute:
4386 switch (form)
4387 {
4bb7a0a7 4388 case DW_FORM_ref_addr:
ae411497
TT
4389 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4390 and later it is offset sized. */
4391 if (cu->header.version == 2)
4392 info_ptr += cu->header.addr_size;
4393 else
4394 info_ptr += cu->header.offset_size;
4395 break;
4396 case DW_FORM_addr:
4bb7a0a7
DJ
4397 info_ptr += cu->header.addr_size;
4398 break;
4399 case DW_FORM_data1:
4400 case DW_FORM_ref1:
4401 case DW_FORM_flag:
4402 info_ptr += 1;
4403 break;
2dc7f7b3
TT
4404 case DW_FORM_flag_present:
4405 break;
4bb7a0a7
DJ
4406 case DW_FORM_data2:
4407 case DW_FORM_ref2:
4408 info_ptr += 2;
4409 break;
4410 case DW_FORM_data4:
4411 case DW_FORM_ref4:
4412 info_ptr += 4;
4413 break;
4414 case DW_FORM_data8:
4415 case DW_FORM_ref8:
55f1336d 4416 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4417 info_ptr += 8;
4418 break;
4419 case DW_FORM_string:
9b1c24c8 4420 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4421 info_ptr += bytes_read;
4422 break;
2dc7f7b3 4423 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4424 case DW_FORM_strp:
4425 info_ptr += cu->header.offset_size;
4426 break;
2dc7f7b3 4427 case DW_FORM_exprloc:
4bb7a0a7
DJ
4428 case DW_FORM_block:
4429 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4430 info_ptr += bytes_read;
4431 break;
4432 case DW_FORM_block1:
4433 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4434 break;
4435 case DW_FORM_block2:
4436 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4437 break;
4438 case DW_FORM_block4:
4439 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4440 break;
4441 case DW_FORM_sdata:
4442 case DW_FORM_udata:
4443 case DW_FORM_ref_udata:
4444 info_ptr = skip_leb128 (abfd, info_ptr);
4445 break;
4446 case DW_FORM_indirect:
4447 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4448 info_ptr += bytes_read;
4449 /* We need to continue parsing from here, so just go back to
4450 the top. */
4451 goto skip_attribute;
4452
4453 default:
3e43a32a
MS
4454 error (_("Dwarf Error: Cannot handle %s "
4455 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
4456 dwarf_form_name (form),
4457 bfd_get_filename (abfd));
4458 }
4459 }
4460
4461 if (abbrev->has_children)
93311388 4462 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
4463 else
4464 return info_ptr;
4465}
4466
93311388
DE
4467/* Locate ORIG_PDI's sibling.
4468 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4469 in BUFFER. */
91c24f0a 4470
fe1b8b76 4471static gdb_byte *
93311388
DE
4472locate_pdi_sibling (struct partial_die_info *orig_pdi,
4473 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4474 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4475{
4476 /* Do we know the sibling already? */
72bf9492 4477
91c24f0a
DC
4478 if (orig_pdi->sibling)
4479 return orig_pdi->sibling;
4480
4481 /* Are there any children to deal with? */
4482
4483 if (!orig_pdi->has_children)
4484 return info_ptr;
4485
4bb7a0a7 4486 /* Skip the children the long way. */
91c24f0a 4487
93311388 4488 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4489}
4490
c906108c
SS
4491/* Expand this partial symbol table into a full symbol table. */
4492
4493static void
fba45db2 4494dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4495{
c906108c
SS
4496 if (pst != NULL)
4497 {
4498 if (pst->readin)
4499 {
3e43a32a
MS
4500 warning (_("bug: psymtab for %s is already read in."),
4501 pst->filename);
c906108c
SS
4502 }
4503 else
4504 {
4505 if (info_verbose)
4506 {
3e43a32a
MS
4507 printf_filtered (_("Reading in symbols for %s..."),
4508 pst->filename);
c906108c
SS
4509 gdb_flush (gdb_stdout);
4510 }
4511
10b3939b
DJ
4512 /* Restore our global data. */
4513 dwarf2_per_objfile = objfile_data (pst->objfile,
4514 dwarf2_objfile_data_key);
4515
b2ab525c
KB
4516 /* If this psymtab is constructed from a debug-only objfile, the
4517 has_section_at_zero flag will not necessarily be correct. We
4518 can get the correct value for this flag by looking at the data
4519 associated with the (presumably stripped) associated objfile. */
4520 if (pst->objfile->separate_debug_objfile_backlink)
4521 {
4522 struct dwarf2_per_objfile *dpo_backlink
4523 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4524 dwarf2_objfile_data_key);
9a619af0 4525
b2ab525c
KB
4526 dwarf2_per_objfile->has_section_at_zero
4527 = dpo_backlink->has_section_at_zero;
4528 }
4529
98bfdba5
PA
4530 dwarf2_per_objfile->reading_partial_symbols = 0;
4531
c906108c
SS
4532 psymtab_to_symtab_1 (pst);
4533
4534 /* Finish up the debug error message. */
4535 if (info_verbose)
a3f17187 4536 printf_filtered (_("done.\n"));
c906108c
SS
4537 }
4538 }
4539}
4540
10b3939b
DJ
4541/* Add PER_CU to the queue. */
4542
4543static void
03dd20cc 4544queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4545{
4546 struct dwarf2_queue_item *item;
4547
4548 per_cu->queued = 1;
4549 item = xmalloc (sizeof (*item));
4550 item->per_cu = per_cu;
4551 item->next = NULL;
4552
4553 if (dwarf2_queue == NULL)
4554 dwarf2_queue = item;
4555 else
4556 dwarf2_queue_tail->next = item;
4557
4558 dwarf2_queue_tail = item;
4559}
4560
4561/* Process the queue. */
4562
4563static void
4564process_queue (struct objfile *objfile)
4565{
4566 struct dwarf2_queue_item *item, *next_item;
4567
03dd20cc
DJ
4568 /* The queue starts out with one item, but following a DIE reference
4569 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4570 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4571 {
9291a0cd
TT
4572 if (dwarf2_per_objfile->using_index
4573 ? !item->per_cu->v.quick->symtab
4574 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4575 process_full_comp_unit (item->per_cu);
4576
4577 item->per_cu->queued = 0;
4578 next_item = item->next;
4579 xfree (item);
4580 }
4581
4582 dwarf2_queue_tail = NULL;
4583}
4584
4585/* Free all allocated queue entries. This function only releases anything if
4586 an error was thrown; if the queue was processed then it would have been
4587 freed as we went along. */
4588
4589static void
4590dwarf2_release_queue (void *dummy)
4591{
4592 struct dwarf2_queue_item *item, *last;
4593
4594 item = dwarf2_queue;
4595 while (item)
4596 {
4597 /* Anything still marked queued is likely to be in an
4598 inconsistent state, so discard it. */
4599 if (item->per_cu->queued)
4600 {
4601 if (item->per_cu->cu != NULL)
4602 free_one_cached_comp_unit (item->per_cu->cu);
4603 item->per_cu->queued = 0;
4604 }
4605
4606 last = item;
4607 item = item->next;
4608 xfree (last);
4609 }
4610
4611 dwarf2_queue = dwarf2_queue_tail = NULL;
4612}
4613
4614/* Read in full symbols for PST, and anything it depends on. */
4615
c906108c 4616static void
fba45db2 4617psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4618{
10b3939b 4619 struct dwarf2_per_cu_data *per_cu;
c906108c 4620 struct cleanup *back_to;
aaa75496
JB
4621 int i;
4622
4623 for (i = 0; i < pst->number_of_dependencies; i++)
4624 if (!pst->dependencies[i]->readin)
4625 {
4626 /* Inform about additional files that need to be read in. */
4627 if (info_verbose)
4628 {
a3f17187 4629 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4630 fputs_filtered (" ", gdb_stdout);
4631 wrap_here ("");
4632 fputs_filtered ("and ", gdb_stdout);
4633 wrap_here ("");
4634 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 4635 wrap_here (""); /* Flush output. */
aaa75496
JB
4636 gdb_flush (gdb_stdout);
4637 }
4638 psymtab_to_symtab_1 (pst->dependencies[i]);
4639 }
4640
e38df1d0 4641 per_cu = pst->read_symtab_private;
10b3939b
DJ
4642
4643 if (per_cu == NULL)
aaa75496
JB
4644 {
4645 /* It's an include file, no symbols to read for it.
4646 Everything is in the parent symtab. */
4647 pst->readin = 1;
4648 return;
4649 }
c906108c 4650
9291a0cd 4651 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4652}
4653
93311388 4654/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4655
93311388 4656static void
3e43a32a
MS
4657load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4658 struct objfile *objfile)
10b3939b 4659{
31ffec48 4660 bfd *abfd = objfile->obfd;
10b3939b 4661 struct dwarf2_cu *cu;
c764a876 4662 unsigned int offset;
93311388 4663 gdb_byte *info_ptr, *beg_of_comp_unit;
98bfdba5 4664 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
10b3939b 4665 struct attribute *attr;
98bfdba5 4666 int read_cu = 0;
6502dd73 4667
8b70b953 4668 gdb_assert (! per_cu->debug_type_section);
348e048f 4669
c906108c 4670 /* Set local variables from the partial symbol table info. */
10b3939b 4671 offset = per_cu->offset;
6502dd73 4672
be391dca 4673 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4674 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4675 beg_of_comp_unit = info_ptr;
63d06c5c 4676
98bfdba5
PA
4677 if (per_cu->cu == NULL)
4678 {
9816fde3
JK
4679 cu = xmalloc (sizeof (*cu));
4680 init_one_comp_unit (cu, objfile);
98bfdba5
PA
4681
4682 read_cu = 1;
c906108c 4683
98bfdba5
PA
4684 /* If an error occurs while loading, release our storage. */
4685 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4686
98bfdba5
PA
4687 /* Read in the comp_unit header. */
4688 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4689
6caca83c
CC
4690 /* Skip dummy compilation units. */
4691 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4692 + dwarf2_per_objfile->info.size)
4693 || peek_abbrev_code (abfd, info_ptr) == 0)
4694 {
4695 do_cleanups (free_cu_cleanup);
4696 return;
4697 }
4698
98bfdba5
PA
4699 /* Complete the cu_header. */
4700 cu->header.offset = offset;
4701 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
93311388 4702
98bfdba5
PA
4703 /* Read the abbrevs for this compilation unit. */
4704 dwarf2_read_abbrevs (abfd, cu);
4705 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
10b3939b 4706
98bfdba5
PA
4707 /* Link this compilation unit into the compilation unit tree. */
4708 per_cu->cu = cu;
4709 cu->per_cu = per_cu;
98bfdba5
PA
4710
4711 /* Link this CU into read_in_chain. */
4712 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4713 dwarf2_per_objfile->read_in_chain = per_cu;
4714 }
4715 else
4716 {
4717 cu = per_cu->cu;
4718 info_ptr += cu->header.first_die_offset;
4719 }
e142c38c 4720
93311388 4721 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4722
4723 /* We try not to read any attributes in this function, because not
4724 all objfiles needed for references have been loaded yet, and symbol
4725 table processing isn't initialized. But we have to set the CU language,
4726 or we won't be able to build types correctly. */
9816fde3 4727 prepare_one_comp_unit (cu, cu->dies);
10b3939b 4728
a6c727b2
DJ
4729 /* Similarly, if we do not read the producer, we can not apply
4730 producer-specific interpretation. */
4731 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4732 if (attr)
4733 cu->producer = DW_STRING (attr);
4734
98bfdba5
PA
4735 if (read_cu)
4736 {
4737 do_cleanups (free_abbrevs_cleanup);
e142c38c 4738
98bfdba5
PA
4739 /* We've successfully allocated this compilation unit. Let our
4740 caller clean it up when finished with it. */
4741 discard_cleanups (free_cu_cleanup);
4742 }
10b3939b
DJ
4743}
4744
3da10d80
KS
4745/* Add a DIE to the delayed physname list. */
4746
4747static void
4748add_to_method_list (struct type *type, int fnfield_index, int index,
4749 const char *name, struct die_info *die,
4750 struct dwarf2_cu *cu)
4751{
4752 struct delayed_method_info mi;
4753 mi.type = type;
4754 mi.fnfield_index = fnfield_index;
4755 mi.index = index;
4756 mi.name = name;
4757 mi.die = die;
4758 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4759}
4760
4761/* A cleanup for freeing the delayed method list. */
4762
4763static void
4764free_delayed_list (void *ptr)
4765{
4766 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4767 if (cu->method_list != NULL)
4768 {
4769 VEC_free (delayed_method_info, cu->method_list);
4770 cu->method_list = NULL;
4771 }
4772}
4773
4774/* Compute the physnames of any methods on the CU's method list.
4775
4776 The computation of method physnames is delayed in order to avoid the
4777 (bad) condition that one of the method's formal parameters is of an as yet
4778 incomplete type. */
4779
4780static void
4781compute_delayed_physnames (struct dwarf2_cu *cu)
4782{
4783 int i;
4784 struct delayed_method_info *mi;
4785 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4786 {
1d06ead6 4787 const char *physname;
3da10d80
KS
4788 struct fn_fieldlist *fn_flp
4789 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 4790 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
4791 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4792 }
4793}
4794
10b3939b
DJ
4795/* Generate full symbol information for PST and CU, whose DIEs have
4796 already been loaded into memory. */
4797
4798static void
4799process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4800{
10b3939b 4801 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4802 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4803 CORE_ADDR lowpc, highpc;
4804 struct symtab *symtab;
3da10d80 4805 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
4806 CORE_ADDR baseaddr;
4807
4808 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4809
10b3939b
DJ
4810 buildsym_init ();
4811 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 4812 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
4813
4814 cu->list_in_scope = &file_symbols;
c906108c
SS
4815
4816 /* Do line number decoding in read_file_scope () */
10b3939b 4817 process_die (cu->dies, cu);
c906108c 4818
3da10d80
KS
4819 /* Now that we have processed all the DIEs in the CU, all the types
4820 should be complete, and it should now be safe to compute all of the
4821 physnames. */
4822 compute_delayed_physnames (cu);
4823 do_cleanups (delayed_list_cleanup);
4824
fae299cd
DC
4825 /* Some compilers don't define a DW_AT_high_pc attribute for the
4826 compilation unit. If the DW_AT_high_pc is missing, synthesize
4827 it, by scanning the DIE's below the compilation unit. */
10b3939b 4828 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4829
613e1657 4830 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 4831
8be455d7 4832 if (symtab != NULL)
c906108c 4833 {
df15bd07 4834 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 4835
8be455d7
JK
4836 /* Set symtab language to language from DW_AT_language. If the
4837 compilation is from a C file generated by language preprocessors, do
4838 not set the language if it was already deduced by start_subfile. */
4839 if (!(cu->language == language_c && symtab->language != language_c))
4840 symtab->language = cu->language;
4841
4842 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4843 produce DW_AT_location with location lists but it can be possibly
4844 invalid without -fvar-tracking.
4845
4846 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4847 needed, it would be wrong due to missing DW_AT_producer there.
4848
4849 Still one can confuse GDB by using non-standard GCC compilation
4850 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4851 */
4632c0d0 4852 if (cu->has_loclist && gcc_4_minor >= 0)
8be455d7 4853 symtab->locations_valid = 1;
e0d00bc7
JK
4854
4855 if (gcc_4_minor >= 5)
4856 symtab->epilogue_unwind_valid = 1;
96408a79
SA
4857
4858 symtab->call_site_htab = cu->call_site_htab;
c906108c 4859 }
9291a0cd
TT
4860
4861 if (dwarf2_per_objfile->using_index)
4862 per_cu->v.quick->symtab = symtab;
4863 else
4864 {
4865 struct partial_symtab *pst = per_cu->v.psymtab;
4866 pst->symtab = symtab;
4867 pst->readin = 1;
4868 }
c906108c
SS
4869
4870 do_cleanups (back_to);
4871}
4872
4873/* Process a die and its children. */
4874
4875static void
e7c27a73 4876process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4877{
4878 switch (die->tag)
4879 {
4880 case DW_TAG_padding:
4881 break;
4882 case DW_TAG_compile_unit:
e7c27a73 4883 read_file_scope (die, cu);
c906108c 4884 break;
348e048f
DE
4885 case DW_TAG_type_unit:
4886 read_type_unit_scope (die, cu);
4887 break;
c906108c 4888 case DW_TAG_subprogram:
c906108c 4889 case DW_TAG_inlined_subroutine:
edb3359d 4890 read_func_scope (die, cu);
c906108c
SS
4891 break;
4892 case DW_TAG_lexical_block:
14898363
L
4893 case DW_TAG_try_block:
4894 case DW_TAG_catch_block:
e7c27a73 4895 read_lexical_block_scope (die, cu);
c906108c 4896 break;
96408a79
SA
4897 case DW_TAG_GNU_call_site:
4898 read_call_site_scope (die, cu);
4899 break;
c906108c 4900 case DW_TAG_class_type:
680b30c7 4901 case DW_TAG_interface_type:
c906108c
SS
4902 case DW_TAG_structure_type:
4903 case DW_TAG_union_type:
134d01f1 4904 process_structure_scope (die, cu);
c906108c
SS
4905 break;
4906 case DW_TAG_enumeration_type:
134d01f1 4907 process_enumeration_scope (die, cu);
c906108c 4908 break;
134d01f1 4909
f792889a
DJ
4910 /* These dies have a type, but processing them does not create
4911 a symbol or recurse to process the children. Therefore we can
4912 read them on-demand through read_type_die. */
c906108c 4913 case DW_TAG_subroutine_type:
72019c9c 4914 case DW_TAG_set_type:
c906108c 4915 case DW_TAG_array_type:
c906108c 4916 case DW_TAG_pointer_type:
c906108c 4917 case DW_TAG_ptr_to_member_type:
c906108c 4918 case DW_TAG_reference_type:
c906108c 4919 case DW_TAG_string_type:
c906108c 4920 break;
134d01f1 4921
c906108c 4922 case DW_TAG_base_type:
a02abb62 4923 case DW_TAG_subrange_type:
cb249c71 4924 case DW_TAG_typedef:
134d01f1
DJ
4925 /* Add a typedef symbol for the type definition, if it has a
4926 DW_AT_name. */
f792889a 4927 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4928 break;
c906108c 4929 case DW_TAG_common_block:
e7c27a73 4930 read_common_block (die, cu);
c906108c
SS
4931 break;
4932 case DW_TAG_common_inclusion:
4933 break;
d9fa45fe 4934 case DW_TAG_namespace:
63d06c5c 4935 processing_has_namespace_info = 1;
e7c27a73 4936 read_namespace (die, cu);
d9fa45fe 4937 break;
5d7cb8df 4938 case DW_TAG_module:
f55ee35c 4939 processing_has_namespace_info = 1;
5d7cb8df
JK
4940 read_module (die, cu);
4941 break;
d9fa45fe
DC
4942 case DW_TAG_imported_declaration:
4943 case DW_TAG_imported_module:
63d06c5c 4944 processing_has_namespace_info = 1;
27aa8d6a
SW
4945 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4946 || cu->language != language_fortran))
4947 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4948 dwarf_tag_name (die->tag));
4949 read_import_statement (die, cu);
d9fa45fe 4950 break;
c906108c 4951 default:
e7c27a73 4952 new_symbol (die, NULL, cu);
c906108c
SS
4953 break;
4954 }
4955}
4956
94af9270
KS
4957/* A helper function for dwarf2_compute_name which determines whether DIE
4958 needs to have the name of the scope prepended to the name listed in the
4959 die. */
4960
4961static int
4962die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4963{
1c809c68
TT
4964 struct attribute *attr;
4965
94af9270
KS
4966 switch (die->tag)
4967 {
4968 case DW_TAG_namespace:
4969 case DW_TAG_typedef:
4970 case DW_TAG_class_type:
4971 case DW_TAG_interface_type:
4972 case DW_TAG_structure_type:
4973 case DW_TAG_union_type:
4974 case DW_TAG_enumeration_type:
4975 case DW_TAG_enumerator:
4976 case DW_TAG_subprogram:
4977 case DW_TAG_member:
4978 return 1;
4979
4980 case DW_TAG_variable:
c2b0a229 4981 case DW_TAG_constant:
94af9270
KS
4982 /* We only need to prefix "globally" visible variables. These include
4983 any variable marked with DW_AT_external or any variable that
4984 lives in a namespace. [Variables in anonymous namespaces
4985 require prefixing, but they are not DW_AT_external.] */
4986
4987 if (dwarf2_attr (die, DW_AT_specification, cu))
4988 {
4989 struct dwarf2_cu *spec_cu = cu;
9a619af0 4990
94af9270
KS
4991 return die_needs_namespace (die_specification (die, &spec_cu),
4992 spec_cu);
4993 }
4994
1c809c68 4995 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4996 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4997 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4998 return 0;
4999 /* A variable in a lexical block of some kind does not need a
5000 namespace, even though in C++ such variables may be external
5001 and have a mangled name. */
5002 if (die->parent->tag == DW_TAG_lexical_block
5003 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5004 || die->parent->tag == DW_TAG_catch_block
5005 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5006 return 0;
5007 return 1;
94af9270
KS
5008
5009 default:
5010 return 0;
5011 }
5012}
5013
98bfdba5
PA
5014/* Retrieve the last character from a mem_file. */
5015
5016static void
5017do_ui_file_peek_last (void *object, const char *buffer, long length)
5018{
5019 char *last_char_p = (char *) object;
5020
5021 if (length > 0)
5022 *last_char_p = buffer[length - 1];
5023}
5024
94af9270
KS
5025/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5026 compute the physname for the object, which include a method's
5027 formal parameters (C++/Java) and return type (Java).
5028
af6b7be1
JB
5029 For Ada, return the DIE's linkage name rather than the fully qualified
5030 name. PHYSNAME is ignored..
5031
94af9270
KS
5032 The result is allocated on the objfile_obstack and canonicalized. */
5033
5034static const char *
5035dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5036 int physname)
5037{
5038 if (name == NULL)
5039 name = dwarf2_name (die, cu);
5040
f55ee35c
JK
5041 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5042 compute it by typename_concat inside GDB. */
5043 if (cu->language == language_ada
5044 || (cu->language == language_fortran && physname))
5045 {
5046 /* For Ada unit, we prefer the linkage name over the name, as
5047 the former contains the exported name, which the user expects
5048 to be able to reference. Ideally, we want the user to be able
5049 to reference this entity using either natural or linkage name,
5050 but we haven't started looking at this enhancement yet. */
5051 struct attribute *attr;
5052
5053 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5054 if (attr == NULL)
5055 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5056 if (attr && DW_STRING (attr))
5057 return DW_STRING (attr);
5058 }
5059
94af9270
KS
5060 /* These are the only languages we know how to qualify names in. */
5061 if (name != NULL
f55ee35c
JK
5062 && (cu->language == language_cplus || cu->language == language_java
5063 || cu->language == language_fortran))
94af9270
KS
5064 {
5065 if (die_needs_namespace (die, cu))
5066 {
5067 long length;
5068 char *prefix;
5069 struct ui_file *buf;
5070
5071 prefix = determine_prefix (die, cu);
5072 buf = mem_fileopen ();
5073 if (*prefix != '\0')
5074 {
f55ee35c
JK
5075 char *prefixed_name = typename_concat (NULL, prefix, name,
5076 physname, cu);
9a619af0 5077
94af9270
KS
5078 fputs_unfiltered (prefixed_name, buf);
5079 xfree (prefixed_name);
5080 }
5081 else
62d5b8da 5082 fputs_unfiltered (name, buf);
94af9270 5083
98bfdba5
PA
5084 /* Template parameters may be specified in the DIE's DW_AT_name, or
5085 as children with DW_TAG_template_type_param or
5086 DW_TAG_value_type_param. If the latter, add them to the name
5087 here. If the name already has template parameters, then
5088 skip this step; some versions of GCC emit both, and
5089 it is more efficient to use the pre-computed name.
5090
5091 Something to keep in mind about this process: it is very
5092 unlikely, or in some cases downright impossible, to produce
5093 something that will match the mangled name of a function.
5094 If the definition of the function has the same debug info,
5095 we should be able to match up with it anyway. But fallbacks
5096 using the minimal symbol, for instance to find a method
5097 implemented in a stripped copy of libstdc++, will not work.
5098 If we do not have debug info for the definition, we will have to
5099 match them up some other way.
5100
5101 When we do name matching there is a related problem with function
5102 templates; two instantiated function templates are allowed to
5103 differ only by their return types, which we do not add here. */
5104
5105 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5106 {
5107 struct attribute *attr;
5108 struct die_info *child;
5109 int first = 1;
5110
5111 die->building_fullname = 1;
5112
5113 for (child = die->child; child != NULL; child = child->sibling)
5114 {
5115 struct type *type;
5116 long value;
5117 gdb_byte *bytes;
5118 struct dwarf2_locexpr_baton *baton;
5119 struct value *v;
5120
5121 if (child->tag != DW_TAG_template_type_param
5122 && child->tag != DW_TAG_template_value_param)
5123 continue;
5124
5125 if (first)
5126 {
5127 fputs_unfiltered ("<", buf);
5128 first = 0;
5129 }
5130 else
5131 fputs_unfiltered (", ", buf);
5132
5133 attr = dwarf2_attr (child, DW_AT_type, cu);
5134 if (attr == NULL)
5135 {
5136 complaint (&symfile_complaints,
5137 _("template parameter missing DW_AT_type"));
5138 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5139 continue;
5140 }
5141 type = die_type (child, cu);
5142
5143 if (child->tag == DW_TAG_template_type_param)
5144 {
5145 c_print_type (type, "", buf, -1, 0);
5146 continue;
5147 }
5148
5149 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5150 if (attr == NULL)
5151 {
5152 complaint (&symfile_complaints,
3e43a32a
MS
5153 _("template parameter missing "
5154 "DW_AT_const_value"));
98bfdba5
PA
5155 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5156 continue;
5157 }
5158
5159 dwarf2_const_value_attr (attr, type, name,
5160 &cu->comp_unit_obstack, cu,
5161 &value, &bytes, &baton);
5162
5163 if (TYPE_NOSIGN (type))
5164 /* GDB prints characters as NUMBER 'CHAR'. If that's
5165 changed, this can use value_print instead. */
5166 c_printchar (value, type, buf);
5167 else
5168 {
5169 struct value_print_options opts;
5170
5171 if (baton != NULL)
5172 v = dwarf2_evaluate_loc_desc (type, NULL,
5173 baton->data,
5174 baton->size,
5175 baton->per_cu);
5176 else if (bytes != NULL)
5177 {
5178 v = allocate_value (type);
5179 memcpy (value_contents_writeable (v), bytes,
5180 TYPE_LENGTH (type));
5181 }
5182 else
5183 v = value_from_longest (type, value);
5184
3e43a32a
MS
5185 /* Specify decimal so that we do not depend on
5186 the radix. */
98bfdba5
PA
5187 get_formatted_print_options (&opts, 'd');
5188 opts.raw = 1;
5189 value_print (v, buf, &opts);
5190 release_value (v);
5191 value_free (v);
5192 }
5193 }
5194
5195 die->building_fullname = 0;
5196
5197 if (!first)
5198 {
5199 /* Close the argument list, with a space if necessary
5200 (nested templates). */
5201 char last_char = '\0';
5202 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5203 if (last_char == '>')
5204 fputs_unfiltered (" >", buf);
5205 else
5206 fputs_unfiltered (">", buf);
5207 }
5208 }
5209
94af9270
KS
5210 /* For Java and C++ methods, append formal parameter type
5211 information, if PHYSNAME. */
6e70227d 5212
94af9270
KS
5213 if (physname && die->tag == DW_TAG_subprogram
5214 && (cu->language == language_cplus
5215 || cu->language == language_java))
5216 {
5217 struct type *type = read_type_die (die, cu);
5218
3167638f 5219 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5220
5221 if (cu->language == language_java)
5222 {
5223 /* For java, we must append the return type to method
0963b4bd 5224 names. */
94af9270
KS
5225 if (die->tag == DW_TAG_subprogram)
5226 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5227 0, 0);
5228 }
5229 else if (cu->language == language_cplus)
5230 {
60430eff
DJ
5231 /* Assume that an artificial first parameter is
5232 "this", but do not crash if it is not. RealView
5233 marks unnamed (and thus unused) parameters as
5234 artificial; there is no way to differentiate
5235 the two cases. */
94af9270
KS
5236 if (TYPE_NFIELDS (type) > 0
5237 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5238 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5239 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5240 0))))
94af9270
KS
5241 fputs_unfiltered (" const", buf);
5242 }
5243 }
5244
5245 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5246 &length);
5247 ui_file_delete (buf);
5248
5249 if (cu->language == language_cplus)
5250 {
5251 char *cname
5252 = dwarf2_canonicalize_name (name, cu,
5253 &cu->objfile->objfile_obstack);
9a619af0 5254
94af9270
KS
5255 if (cname != NULL)
5256 name = cname;
5257 }
5258 }
5259 }
5260
5261 return name;
5262}
5263
0114d602
DJ
5264/* Return the fully qualified name of DIE, based on its DW_AT_name.
5265 If scope qualifiers are appropriate they will be added. The result
5266 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5267 not have a name. NAME may either be from a previous call to
5268 dwarf2_name or NULL.
5269
0963b4bd 5270 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5271
5272static const char *
94af9270 5273dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5274{
94af9270
KS
5275 return dwarf2_compute_name (name, die, cu, 0);
5276}
0114d602 5277
94af9270
KS
5278/* Construct a physname for the given DIE in CU. NAME may either be
5279 from a previous call to dwarf2_name or NULL. The result will be
5280 allocated on the objfile_objstack or NULL if the DIE does not have a
5281 name.
0114d602 5282
94af9270 5283 The output string will be canonicalized (if C++/Java). */
0114d602 5284
94af9270
KS
5285static const char *
5286dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5287{
900e11f9
JK
5288 struct attribute *attr;
5289 const char *retval, *mangled = NULL, *canon = NULL;
5290 struct cleanup *back_to;
5291 int need_copy = 1;
5292
5293 /* In this case dwarf2_compute_name is just a shortcut not building anything
5294 on its own. */
5295 if (!die_needs_namespace (die, cu))
5296 return dwarf2_compute_name (name, die, cu, 1);
5297
5298 back_to = make_cleanup (null_cleanup, NULL);
5299
5300 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5301 if (!attr)
5302 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5303
5304 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5305 has computed. */
5306 if (attr && DW_STRING (attr))
5307 {
5308 char *demangled;
5309
5310 mangled = DW_STRING (attr);
5311
5312 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5313 type. It is easier for GDB users to search for such functions as
5314 `name(params)' than `long name(params)'. In such case the minimal
5315 symbol names do not match the full symbol names but for template
5316 functions there is never a need to look up their definition from their
5317 declaration so the only disadvantage remains the minimal symbol
5318 variant `long name(params)' does not have the proper inferior type.
5319 */
5320
5321 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5322 | (cu->language == language_java
5323 ? DMGL_JAVA | DMGL_RET_POSTFIX
5324 : DMGL_RET_DROP)));
5325 if (demangled)
5326 {
5327 make_cleanup (xfree, demangled);
5328 canon = demangled;
5329 }
5330 else
5331 {
5332 canon = mangled;
5333 need_copy = 0;
5334 }
5335 }
5336
5337 if (canon == NULL || check_physname)
5338 {
5339 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5340
5341 if (canon != NULL && strcmp (physname, canon) != 0)
5342 {
5343 /* It may not mean a bug in GDB. The compiler could also
5344 compute DW_AT_linkage_name incorrectly. But in such case
5345 GDB would need to be bug-to-bug compatible. */
5346
5347 complaint (&symfile_complaints,
5348 _("Computed physname <%s> does not match demangled <%s> "
5349 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5350 physname, canon, mangled, die->offset, cu->objfile->name);
5351
5352 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5353 is available here - over computed PHYSNAME. It is safer
5354 against both buggy GDB and buggy compilers. */
5355
5356 retval = canon;
5357 }
5358 else
5359 {
5360 retval = physname;
5361 need_copy = 0;
5362 }
5363 }
5364 else
5365 retval = canon;
5366
5367 if (need_copy)
5368 retval = obsavestring (retval, strlen (retval),
5369 &cu->objfile->objfile_obstack);
5370
5371 do_cleanups (back_to);
5372 return retval;
0114d602
DJ
5373}
5374
27aa8d6a
SW
5375/* Read the import statement specified by the given die and record it. */
5376
5377static void
5378read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5379{
5380 struct attribute *import_attr;
32019081 5381 struct die_info *imported_die, *child_die;
de4affc9 5382 struct dwarf2_cu *imported_cu;
27aa8d6a 5383 const char *imported_name;
794684b6 5384 const char *imported_name_prefix;
13387711
SW
5385 const char *canonical_name;
5386 const char *import_alias;
5387 const char *imported_declaration = NULL;
794684b6 5388 const char *import_prefix;
32019081
JK
5389 VEC (const_char_ptr) *excludes = NULL;
5390 struct cleanup *cleanups;
13387711
SW
5391
5392 char *temp;
27aa8d6a
SW
5393
5394 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5395 if (import_attr == NULL)
5396 {
5397 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5398 dwarf_tag_name (die->tag));
5399 return;
5400 }
5401
de4affc9
CC
5402 imported_cu = cu;
5403 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5404 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
5405 if (imported_name == NULL)
5406 {
5407 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5408
5409 The import in the following code:
5410 namespace A
5411 {
5412 typedef int B;
5413 }
5414
5415 int main ()
5416 {
5417 using A::B;
5418 B b;
5419 return b;
5420 }
5421
5422 ...
5423 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5424 <52> DW_AT_decl_file : 1
5425 <53> DW_AT_decl_line : 6
5426 <54> DW_AT_import : <0x75>
5427 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5428 <59> DW_AT_name : B
5429 <5b> DW_AT_decl_file : 1
5430 <5c> DW_AT_decl_line : 2
5431 <5d> DW_AT_type : <0x6e>
5432 ...
5433 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5434 <76> DW_AT_byte_size : 4
5435 <77> DW_AT_encoding : 5 (signed)
5436
5437 imports the wrong die ( 0x75 instead of 0x58 ).
5438 This case will be ignored until the gcc bug is fixed. */
5439 return;
5440 }
5441
82856980
SW
5442 /* Figure out the local name after import. */
5443 import_alias = dwarf2_name (die, cu);
27aa8d6a 5444
794684b6
SW
5445 /* Figure out where the statement is being imported to. */
5446 import_prefix = determine_prefix (die, cu);
5447
5448 /* Figure out what the scope of the imported die is and prepend it
5449 to the name of the imported die. */
de4affc9 5450 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 5451
f55ee35c
JK
5452 if (imported_die->tag != DW_TAG_namespace
5453 && imported_die->tag != DW_TAG_module)
794684b6 5454 {
13387711
SW
5455 imported_declaration = imported_name;
5456 canonical_name = imported_name_prefix;
794684b6 5457 }
13387711 5458 else if (strlen (imported_name_prefix) > 0)
794684b6 5459 {
13387711
SW
5460 temp = alloca (strlen (imported_name_prefix)
5461 + 2 + strlen (imported_name) + 1);
5462 strcpy (temp, imported_name_prefix);
5463 strcat (temp, "::");
5464 strcat (temp, imported_name);
5465 canonical_name = temp;
794684b6 5466 }
13387711
SW
5467 else
5468 canonical_name = imported_name;
794684b6 5469
32019081
JK
5470 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5471
5472 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5473 for (child_die = die->child; child_die && child_die->tag;
5474 child_die = sibling_die (child_die))
5475 {
5476 /* DWARF-4: A Fortran use statement with a “rename list” may be
5477 represented by an imported module entry with an import attribute
5478 referring to the module and owned entries corresponding to those
5479 entities that are renamed as part of being imported. */
5480
5481 if (child_die->tag != DW_TAG_imported_declaration)
5482 {
5483 complaint (&symfile_complaints,
5484 _("child DW_TAG_imported_declaration expected "
5485 "- DIE at 0x%x [in module %s]"),
5486 child_die->offset, cu->objfile->name);
5487 continue;
5488 }
5489
5490 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5491 if (import_attr == NULL)
5492 {
5493 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5494 dwarf_tag_name (child_die->tag));
5495 continue;
5496 }
5497
5498 imported_cu = cu;
5499 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5500 &imported_cu);
5501 imported_name = dwarf2_name (imported_die, imported_cu);
5502 if (imported_name == NULL)
5503 {
5504 complaint (&symfile_complaints,
5505 _("child DW_TAG_imported_declaration has unknown "
5506 "imported name - DIE at 0x%x [in module %s]"),
5507 child_die->offset, cu->objfile->name);
5508 continue;
5509 }
5510
5511 VEC_safe_push (const_char_ptr, excludes, imported_name);
5512
5513 process_die (child_die, cu);
5514 }
5515
c0cc3a76
SW
5516 cp_add_using_directive (import_prefix,
5517 canonical_name,
5518 import_alias,
13387711 5519 imported_declaration,
32019081 5520 excludes,
c0cc3a76 5521 &cu->objfile->objfile_obstack);
32019081
JK
5522
5523 do_cleanups (cleanups);
27aa8d6a
SW
5524}
5525
5fb290d7 5526static void
e142c38c 5527initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 5528{
e142c38c 5529 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
5530}
5531
ae2de4f8
DE
5532/* Cleanup function for read_file_scope. */
5533
cb1df416
DJ
5534static void
5535free_cu_line_header (void *arg)
5536{
5537 struct dwarf2_cu *cu = arg;
5538
5539 free_line_header (cu->line_header);
5540 cu->line_header = NULL;
5541}
5542
9291a0cd
TT
5543static void
5544find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5545 char **name, char **comp_dir)
5546{
5547 struct attribute *attr;
5548
5549 *name = NULL;
5550 *comp_dir = NULL;
5551
5552 /* Find the filename. Do not use dwarf2_name here, since the filename
5553 is not a source language identifier. */
5554 attr = dwarf2_attr (die, DW_AT_name, cu);
5555 if (attr)
5556 {
5557 *name = DW_STRING (attr);
5558 }
5559
5560 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5561 if (attr)
5562 *comp_dir = DW_STRING (attr);
5563 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5564 {
5565 *comp_dir = ldirname (*name);
5566 if (*comp_dir != NULL)
5567 make_cleanup (xfree, *comp_dir);
5568 }
5569 if (*comp_dir != NULL)
5570 {
5571 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5572 directory, get rid of it. */
5573 char *cp = strchr (*comp_dir, ':');
5574
5575 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5576 *comp_dir = cp + 1;
5577 }
5578
5579 if (*name == NULL)
5580 *name = "<unknown>";
5581}
5582
2ab95328
TT
5583/* Handle DW_AT_stmt_list for a compilation unit. */
5584
5585static void
5586handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5587 const char *comp_dir)
5588{
5589 struct attribute *attr;
5590 struct objfile *objfile = cu->objfile;
5591 bfd *abfd = objfile->obfd;
5592
5593 /* Decode line number information if present. We do this before
5594 processing child DIEs, so that the line header table is available
5595 for DW_AT_decl_file. */
5596 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5597 if (attr)
5598 {
5599 unsigned int line_offset = DW_UNSND (attr);
5600 struct line_header *line_header
5601 = dwarf_decode_line_header (line_offset, abfd, cu);
5602
5603 if (line_header)
5604 {
5605 cu->line_header = line_header;
5606 make_cleanup (free_cu_line_header, cu);
5607 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5608 }
5609 }
5610}
5611
ae2de4f8
DE
5612/* Process DW_TAG_compile_unit. */
5613
c906108c 5614static void
e7c27a73 5615read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5616{
e7c27a73 5617 struct objfile *objfile = cu->objfile;
debd256d 5618 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 5619 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
5620 CORE_ADDR highpc = ((CORE_ADDR) 0);
5621 struct attribute *attr;
e1024ff1 5622 char *name = NULL;
c906108c
SS
5623 char *comp_dir = NULL;
5624 struct die_info *child_die;
5625 bfd *abfd = objfile->obfd;
e142c38c 5626 CORE_ADDR baseaddr;
6e70227d 5627
e142c38c 5628 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 5629
fae299cd 5630 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
5631
5632 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5633 from finish_block. */
2acceee2 5634 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
5635 lowpc = highpc;
5636 lowpc += baseaddr;
5637 highpc += baseaddr;
5638
9291a0cd 5639 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 5640
e142c38c 5641 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
5642 if (attr)
5643 {
e142c38c 5644 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
5645 }
5646
b0f35d58 5647 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5648 if (attr)
b0f35d58 5649 cu->producer = DW_STRING (attr);
303b6f5d 5650
f4b8a18d
KW
5651 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5652 standardised yet. As a workaround for the language detection we fall
5653 back to the DW_AT_producer string. */
5654 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5655 cu->language = language_opencl;
5656
0963b4bd 5657 /* We assume that we're processing GCC output. */
c906108c 5658 processing_gcc_compilation = 2;
c906108c 5659
df8a16a1
DJ
5660 processing_has_namespace_info = 0;
5661
c906108c
SS
5662 start_symtab (name, comp_dir, lowpc);
5663 record_debugformat ("DWARF 2");
303b6f5d 5664 record_producer (cu->producer);
c906108c 5665
e142c38c 5666 initialize_cu_func_list (cu);
c906108c 5667
2ab95328 5668 handle_DW_AT_stmt_list (die, cu, comp_dir);
debd256d 5669
cb1df416
DJ
5670 /* Process all dies in compilation unit. */
5671 if (die->child != NULL)
5672 {
5673 child_die = die->child;
5674 while (child_die && child_die->tag)
5675 {
5676 process_die (child_die, cu);
5677 child_die = sibling_die (child_die);
5678 }
5679 }
5680
2e276125
JB
5681 /* Decode macro information, if present. Dwarf 2 macro information
5682 refers to information in the line number info statement program
5683 header, so we can only read it if we've read the header
5684 successfully. */
cf2c3c16 5685 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
2ab95328 5686 if (attr && cu->line_header)
2e276125 5687 {
cf2c3c16
TT
5688 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5689 complaint (&symfile_complaints,
5690 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5691
5692 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5693 comp_dir, abfd, cu,
5694 &dwarf2_per_objfile->macro, 1);
5695 }
5696 else
5697 {
5698 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5699 if (attr && cu->line_header)
5700 {
5701 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 5702
cf2c3c16
TT
5703 dwarf_decode_macros (cu->line_header, macro_offset,
5704 comp_dir, abfd, cu,
5705 &dwarf2_per_objfile->macinfo, 0);
5706 }
2e276125 5707 }
debd256d 5708 do_cleanups (back_to);
5fb290d7
DJ
5709}
5710
ae2de4f8
DE
5711/* Process DW_TAG_type_unit.
5712 For TUs we want to skip the first top level sibling if it's not the
348e048f
DE
5713 actual type being defined by this TU. In this case the first top
5714 level sibling is there to provide context only. */
5715
5716static void
5717read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5718{
5719 struct objfile *objfile = cu->objfile;
5720 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5721 CORE_ADDR lowpc;
5722 struct attribute *attr;
5723 char *name = NULL;
5724 char *comp_dir = NULL;
5725 struct die_info *child_die;
5726 bfd *abfd = objfile->obfd;
348e048f
DE
5727
5728 /* start_symtab needs a low pc, but we don't really have one.
5729 Do what read_file_scope would do in the absence of such info. */
5730 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5731
5732 /* Find the filename. Do not use dwarf2_name here, since the filename
5733 is not a source language identifier. */
5734 attr = dwarf2_attr (die, DW_AT_name, cu);
5735 if (attr)
5736 name = DW_STRING (attr);
5737
5738 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5739 if (attr)
5740 comp_dir = DW_STRING (attr);
5741 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5742 {
5743 comp_dir = ldirname (name);
5744 if (comp_dir != NULL)
5745 make_cleanup (xfree, comp_dir);
5746 }
5747
5748 if (name == NULL)
5749 name = "<unknown>";
5750
5751 attr = dwarf2_attr (die, DW_AT_language, cu);
5752 if (attr)
5753 set_cu_language (DW_UNSND (attr), cu);
5754
5755 /* This isn't technically needed today. It is done for symmetry
5756 with read_file_scope. */
5757 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 5758 if (attr)
348e048f
DE
5759 cu->producer = DW_STRING (attr);
5760
0963b4bd 5761 /* We assume that we're processing GCC output. */
348e048f
DE
5762 processing_gcc_compilation = 2;
5763
5764 processing_has_namespace_info = 0;
5765
5766 start_symtab (name, comp_dir, lowpc);
5767 record_debugformat ("DWARF 2");
5768 record_producer (cu->producer);
5769
2ab95328
TT
5770 handle_DW_AT_stmt_list (die, cu, comp_dir);
5771
348e048f
DE
5772 /* Process the dies in the type unit. */
5773 if (die->child == NULL)
5774 {
5775 dump_die_for_error (die);
5776 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5777 bfd_get_filename (abfd));
5778 }
5779
5780 child_die = die->child;
5781
5782 while (child_die && child_die->tag)
5783 {
5784 process_die (child_die, cu);
5785
5786 child_die = sibling_die (child_die);
5787 }
5788
5789 do_cleanups (back_to);
5790}
5791
5fb290d7 5792static void
e142c38c
DJ
5793add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5794 struct dwarf2_cu *cu)
5fb290d7
DJ
5795{
5796 struct function_range *thisfn;
5797
5798 thisfn = (struct function_range *)
7b5a2f43 5799 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
5800 thisfn->name = name;
5801 thisfn->lowpc = lowpc;
5802 thisfn->highpc = highpc;
5803 thisfn->seen_line = 0;
5804 thisfn->next = NULL;
5805
e142c38c
DJ
5806 if (cu->last_fn == NULL)
5807 cu->first_fn = thisfn;
5fb290d7 5808 else
e142c38c 5809 cu->last_fn->next = thisfn;
5fb290d7 5810
e142c38c 5811 cu->last_fn = thisfn;
c906108c
SS
5812}
5813
d389af10
JK
5814/* qsort helper for inherit_abstract_dies. */
5815
5816static int
5817unsigned_int_compar (const void *ap, const void *bp)
5818{
5819 unsigned int a = *(unsigned int *) ap;
5820 unsigned int b = *(unsigned int *) bp;
5821
5822 return (a > b) - (b > a);
5823}
5824
5825/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
5826 Inherit only the children of the DW_AT_abstract_origin DIE not being
5827 already referenced by DW_AT_abstract_origin from the children of the
5828 current DIE. */
d389af10
JK
5829
5830static void
5831inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5832{
5833 struct die_info *child_die;
5834 unsigned die_children_count;
5835 /* CU offsets which were referenced by children of the current DIE. */
5836 unsigned *offsets;
5837 unsigned *offsets_end, *offsetp;
5838 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5839 struct die_info *origin_die;
5840 /* Iterator of the ORIGIN_DIE children. */
5841 struct die_info *origin_child_die;
5842 struct cleanup *cleanups;
5843 struct attribute *attr;
cd02d79d
PA
5844 struct dwarf2_cu *origin_cu;
5845 struct pending **origin_previous_list_in_scope;
d389af10
JK
5846
5847 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5848 if (!attr)
5849 return;
5850
cd02d79d
PA
5851 /* Note that following die references may follow to a die in a
5852 different cu. */
5853
5854 origin_cu = cu;
5855 origin_die = follow_die_ref (die, attr, &origin_cu);
5856
5857 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5858 symbols in. */
5859 origin_previous_list_in_scope = origin_cu->list_in_scope;
5860 origin_cu->list_in_scope = cu->list_in_scope;
5861
edb3359d
DJ
5862 if (die->tag != origin_die->tag
5863 && !(die->tag == DW_TAG_inlined_subroutine
5864 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5865 complaint (&symfile_complaints,
5866 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5867 die->offset, origin_die->offset);
5868
5869 child_die = die->child;
5870 die_children_count = 0;
5871 while (child_die && child_die->tag)
5872 {
5873 child_die = sibling_die (child_die);
5874 die_children_count++;
5875 }
5876 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5877 cleanups = make_cleanup (xfree, offsets);
5878
5879 offsets_end = offsets;
5880 child_die = die->child;
5881 while (child_die && child_die->tag)
5882 {
c38f313d
DJ
5883 /* For each CHILD_DIE, find the corresponding child of
5884 ORIGIN_DIE. If there is more than one layer of
5885 DW_AT_abstract_origin, follow them all; there shouldn't be,
5886 but GCC versions at least through 4.4 generate this (GCC PR
5887 40573). */
5888 struct die_info *child_origin_die = child_die;
cd02d79d 5889 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 5890
c38f313d
DJ
5891 while (1)
5892 {
cd02d79d
PA
5893 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5894 child_origin_cu);
c38f313d
DJ
5895 if (attr == NULL)
5896 break;
cd02d79d
PA
5897 child_origin_die = follow_die_ref (child_origin_die, attr,
5898 &child_origin_cu);
c38f313d
DJ
5899 }
5900
d389af10
JK
5901 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5902 counterpart may exist. */
c38f313d 5903 if (child_origin_die != child_die)
d389af10 5904 {
edb3359d
DJ
5905 if (child_die->tag != child_origin_die->tag
5906 && !(child_die->tag == DW_TAG_inlined_subroutine
5907 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
5908 complaint (&symfile_complaints,
5909 _("Child DIE 0x%x and its abstract origin 0x%x have "
5910 "different tags"), child_die->offset,
5911 child_origin_die->offset);
c38f313d
DJ
5912 if (child_origin_die->parent != origin_die)
5913 complaint (&symfile_complaints,
5914 _("Child DIE 0x%x and its abstract origin 0x%x have "
5915 "different parents"), child_die->offset,
5916 child_origin_die->offset);
5917 else
5918 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5919 }
5920 child_die = sibling_die (child_die);
5921 }
5922 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5923 unsigned_int_compar);
5924 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5925 if (offsetp[-1] == *offsetp)
3e43a32a
MS
5926 complaint (&symfile_complaints,
5927 _("Multiple children of DIE 0x%x refer "
5928 "to DIE 0x%x as their abstract origin"),
d389af10
JK
5929 die->offset, *offsetp);
5930
5931 offsetp = offsets;
5932 origin_child_die = origin_die->child;
5933 while (origin_child_die && origin_child_die->tag)
5934 {
5935 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5936 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5937 offsetp++;
5938 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5939 {
5940 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 5941 process_die (origin_child_die, origin_cu);
d389af10
JK
5942 }
5943 origin_child_die = sibling_die (origin_child_die);
5944 }
cd02d79d 5945 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
5946
5947 do_cleanups (cleanups);
5948}
5949
c906108c 5950static void
e7c27a73 5951read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5952{
e7c27a73 5953 struct objfile *objfile = cu->objfile;
52f0bd74 5954 struct context_stack *new;
c906108c
SS
5955 CORE_ADDR lowpc;
5956 CORE_ADDR highpc;
5957 struct die_info *child_die;
edb3359d 5958 struct attribute *attr, *call_line, *call_file;
c906108c 5959 char *name;
e142c38c 5960 CORE_ADDR baseaddr;
801e3a5b 5961 struct block *block;
edb3359d 5962 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
5963 VEC (symbolp) *template_args = NULL;
5964 struct template_symbol *templ_func = NULL;
edb3359d
DJ
5965
5966 if (inlined_func)
5967 {
5968 /* If we do not have call site information, we can't show the
5969 caller of this inlined function. That's too confusing, so
5970 only use the scope for local variables. */
5971 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5972 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5973 if (call_line == NULL || call_file == NULL)
5974 {
5975 read_lexical_block_scope (die, cu);
5976 return;
5977 }
5978 }
c906108c 5979
e142c38c
DJ
5980 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5981
94af9270 5982 name = dwarf2_name (die, cu);
c906108c 5983
e8d05480
JB
5984 /* Ignore functions with missing or empty names. These are actually
5985 illegal according to the DWARF standard. */
5986 if (name == NULL)
5987 {
5988 complaint (&symfile_complaints,
5989 _("missing name for subprogram DIE at %d"), die->offset);
5990 return;
5991 }
5992
5993 /* Ignore functions with missing or invalid low and high pc attributes. */
5994 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5995 {
ae4d0c03
PM
5996 attr = dwarf2_attr (die, DW_AT_external, cu);
5997 if (!attr || !DW_UNSND (attr))
5998 complaint (&symfile_complaints,
3e43a32a
MS
5999 _("cannot get low and high bounds "
6000 "for subprogram DIE at %d"),
ae4d0c03 6001 die->offset);
e8d05480
JB
6002 return;
6003 }
c906108c
SS
6004
6005 lowpc += baseaddr;
6006 highpc += baseaddr;
6007
5fb290d7 6008 /* Record the function range for dwarf_decode_lines. */
e142c38c 6009 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 6010
34eaf542
TT
6011 /* If we have any template arguments, then we must allocate a
6012 different sort of symbol. */
6013 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6014 {
6015 if (child_die->tag == DW_TAG_template_type_param
6016 || child_die->tag == DW_TAG_template_value_param)
6017 {
6018 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6019 struct template_symbol);
6020 templ_func->base.is_cplus_template_function = 1;
6021 break;
6022 }
6023 }
6024
c906108c 6025 new = push_context (0, lowpc);
34eaf542
TT
6026 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6027 (struct symbol *) templ_func);
4c2df51b 6028
4cecd739
DJ
6029 /* If there is a location expression for DW_AT_frame_base, record
6030 it. */
e142c38c 6031 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 6032 if (attr)
c034e007
AC
6033 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6034 expression is being recorded directly in the function's symbol
6035 and not in a separate frame-base object. I guess this hack is
6036 to avoid adding some sort of frame-base adjunct/annex to the
6037 function's symbol :-(. The problem with doing this is that it
6038 results in a function symbol with a location expression that
6039 has nothing to do with the location of the function, ouch! The
6040 relationship should be: a function's symbol has-a frame base; a
6041 frame-base has-a location expression. */
e7c27a73 6042 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 6043
e142c38c 6044 cu->list_in_scope = &local_symbols;
c906108c 6045
639d11d3 6046 if (die->child != NULL)
c906108c 6047 {
639d11d3 6048 child_die = die->child;
c906108c
SS
6049 while (child_die && child_die->tag)
6050 {
34eaf542
TT
6051 if (child_die->tag == DW_TAG_template_type_param
6052 || child_die->tag == DW_TAG_template_value_param)
6053 {
6054 struct symbol *arg = new_symbol (child_die, NULL, cu);
6055
f1078f66
DJ
6056 if (arg != NULL)
6057 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
6058 }
6059 else
6060 process_die (child_die, cu);
c906108c
SS
6061 child_die = sibling_die (child_die);
6062 }
6063 }
6064
d389af10
JK
6065 inherit_abstract_dies (die, cu);
6066
4a811a97
UW
6067 /* If we have a DW_AT_specification, we might need to import using
6068 directives from the context of the specification DIE. See the
6069 comment in determine_prefix. */
6070 if (cu->language == language_cplus
6071 && dwarf2_attr (die, DW_AT_specification, cu))
6072 {
6073 struct dwarf2_cu *spec_cu = cu;
6074 struct die_info *spec_die = die_specification (die, &spec_cu);
6075
6076 while (spec_die)
6077 {
6078 child_die = spec_die->child;
6079 while (child_die && child_die->tag)
6080 {
6081 if (child_die->tag == DW_TAG_imported_module)
6082 process_die (child_die, spec_cu);
6083 child_die = sibling_die (child_die);
6084 }
6085
6086 /* In some cases, GCC generates specification DIEs that
6087 themselves contain DW_AT_specification attributes. */
6088 spec_die = die_specification (spec_die, &spec_cu);
6089 }
6090 }
6091
c906108c
SS
6092 new = pop_context ();
6093 /* Make a block for the local symbols within. */
801e3a5b
JB
6094 block = finish_block (new->name, &local_symbols, new->old_blocks,
6095 lowpc, highpc, objfile);
6096
df8a16a1 6097 /* For C++, set the block's scope. */
f55ee35c 6098 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 6099 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 6100 determine_prefix (die, cu),
df8a16a1
DJ
6101 processing_has_namespace_info);
6102
801e3a5b
JB
6103 /* If we have address ranges, record them. */
6104 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 6105
34eaf542
TT
6106 /* Attach template arguments to function. */
6107 if (! VEC_empty (symbolp, template_args))
6108 {
6109 gdb_assert (templ_func != NULL);
6110
6111 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6112 templ_func->template_arguments
6113 = obstack_alloc (&objfile->objfile_obstack,
6114 (templ_func->n_template_arguments
6115 * sizeof (struct symbol *)));
6116 memcpy (templ_func->template_arguments,
6117 VEC_address (symbolp, template_args),
6118 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6119 VEC_free (symbolp, template_args);
6120 }
6121
208d8187
JB
6122 /* In C++, we can have functions nested inside functions (e.g., when
6123 a function declares a class that has methods). This means that
6124 when we finish processing a function scope, we may need to go
6125 back to building a containing block's symbol lists. */
6126 local_symbols = new->locals;
6127 param_symbols = new->params;
27aa8d6a 6128 using_directives = new->using_directives;
208d8187 6129
921e78cf
JB
6130 /* If we've finished processing a top-level function, subsequent
6131 symbols go in the file symbol list. */
6132 if (outermost_context_p ())
e142c38c 6133 cu->list_in_scope = &file_symbols;
c906108c
SS
6134}
6135
6136/* Process all the DIES contained within a lexical block scope. Start
6137 a new scope, process the dies, and then close the scope. */
6138
6139static void
e7c27a73 6140read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6141{
e7c27a73 6142 struct objfile *objfile = cu->objfile;
52f0bd74 6143 struct context_stack *new;
c906108c
SS
6144 CORE_ADDR lowpc, highpc;
6145 struct die_info *child_die;
e142c38c
DJ
6146 CORE_ADDR baseaddr;
6147
6148 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
6149
6150 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
6151 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6152 as multiple lexical blocks? Handling children in a sane way would
6e70227d 6153 be nasty. Might be easier to properly extend generic blocks to
af34e669 6154 describe ranges. */
d85a05f0 6155 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
6156 return;
6157 lowpc += baseaddr;
6158 highpc += baseaddr;
6159
6160 push_context (0, lowpc);
639d11d3 6161 if (die->child != NULL)
c906108c 6162 {
639d11d3 6163 child_die = die->child;
c906108c
SS
6164 while (child_die && child_die->tag)
6165 {
e7c27a73 6166 process_die (child_die, cu);
c906108c
SS
6167 child_die = sibling_die (child_die);
6168 }
6169 }
6170 new = pop_context ();
6171
8540c487 6172 if (local_symbols != NULL || using_directives != NULL)
c906108c 6173 {
801e3a5b
JB
6174 struct block *block
6175 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6176 highpc, objfile);
6177
6178 /* Note that recording ranges after traversing children, as we
6179 do here, means that recording a parent's ranges entails
6180 walking across all its children's ranges as they appear in
6181 the address map, which is quadratic behavior.
6182
6183 It would be nicer to record the parent's ranges before
6184 traversing its children, simply overriding whatever you find
6185 there. But since we don't even decide whether to create a
6186 block until after we've traversed its children, that's hard
6187 to do. */
6188 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
6189 }
6190 local_symbols = new->locals;
27aa8d6a 6191 using_directives = new->using_directives;
c906108c
SS
6192}
6193
96408a79
SA
6194/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6195
6196static void
6197read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6198{
6199 struct objfile *objfile = cu->objfile;
6200 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6201 CORE_ADDR pc, baseaddr;
6202 struct attribute *attr;
6203 struct call_site *call_site, call_site_local;
6204 void **slot;
6205 int nparams;
6206 struct die_info *child_die;
6207
6208 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6209
6210 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6211 if (!attr)
6212 {
6213 complaint (&symfile_complaints,
6214 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6215 "DIE 0x%x [in module %s]"),
6216 die->offset, cu->objfile->name);
6217 return;
6218 }
6219 pc = DW_ADDR (attr) + baseaddr;
6220
6221 if (cu->call_site_htab == NULL)
6222 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6223 NULL, &objfile->objfile_obstack,
6224 hashtab_obstack_allocate, NULL);
6225 call_site_local.pc = pc;
6226 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6227 if (*slot != NULL)
6228 {
6229 complaint (&symfile_complaints,
6230 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6231 "DIE 0x%x [in module %s]"),
6232 paddress (gdbarch, pc), die->offset, cu->objfile->name);
6233 return;
6234 }
6235
6236 /* Count parameters at the caller. */
6237
6238 nparams = 0;
6239 for (child_die = die->child; child_die && child_die->tag;
6240 child_die = sibling_die (child_die))
6241 {
6242 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6243 {
6244 complaint (&symfile_complaints,
6245 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6246 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6247 child_die->tag, child_die->offset, cu->objfile->name);
6248 continue;
6249 }
6250
6251 nparams++;
6252 }
6253
6254 call_site = obstack_alloc (&objfile->objfile_obstack,
6255 (sizeof (*call_site)
6256 + (sizeof (*call_site->parameter)
6257 * (nparams - 1))));
6258 *slot = call_site;
6259 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6260 call_site->pc = pc;
6261
6262 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6263 {
6264 struct die_info *func_die;
6265
6266 /* Skip also over DW_TAG_inlined_subroutine. */
6267 for (func_die = die->parent;
6268 func_die && func_die->tag != DW_TAG_subprogram
6269 && func_die->tag != DW_TAG_subroutine_type;
6270 func_die = func_die->parent);
6271
6272 /* DW_AT_GNU_all_call_sites is a superset
6273 of DW_AT_GNU_all_tail_call_sites. */
6274 if (func_die
6275 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6276 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6277 {
6278 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6279 not complete. But keep CALL_SITE for look ups via call_site_htab,
6280 both the initial caller containing the real return address PC and
6281 the final callee containing the current PC of a chain of tail
6282 calls do not need to have the tail call list complete. But any
6283 function candidate for a virtual tail call frame searched via
6284 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6285 determined unambiguously. */
6286 }
6287 else
6288 {
6289 struct type *func_type = NULL;
6290
6291 if (func_die)
6292 func_type = get_die_type (func_die, cu);
6293 if (func_type != NULL)
6294 {
6295 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6296
6297 /* Enlist this call site to the function. */
6298 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6299 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6300 }
6301 else
6302 complaint (&symfile_complaints,
6303 _("Cannot find function owning DW_TAG_GNU_call_site "
6304 "DIE 0x%x [in module %s]"),
6305 die->offset, cu->objfile->name);
6306 }
6307 }
6308
6309 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6310 if (attr == NULL)
6311 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6312 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6313 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6314 /* Keep NULL DWARF_BLOCK. */;
6315 else if (attr_form_is_block (attr))
6316 {
6317 struct dwarf2_locexpr_baton *dlbaton;
6318
6319 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6320 dlbaton->data = DW_BLOCK (attr)->data;
6321 dlbaton->size = DW_BLOCK (attr)->size;
6322 dlbaton->per_cu = cu->per_cu;
6323
6324 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6325 }
6326 else if (is_ref_attr (attr))
6327 {
6328 struct objfile *objfile = cu->objfile;
6329 struct dwarf2_cu *target_cu = cu;
6330 struct die_info *target_die;
6331
6332 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6333 gdb_assert (target_cu->objfile == objfile);
6334 if (die_is_declaration (target_die, target_cu))
6335 {
6336 const char *target_physname;
6337
6338 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6339 if (target_physname == NULL)
6340 complaint (&symfile_complaints,
6341 _("DW_AT_GNU_call_site_target target DIE has invalid "
6342 "physname, for referencing DIE 0x%x [in module %s]"),
6343 die->offset, cu->objfile->name);
6344 else
6345 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6346 }
6347 else
6348 {
6349 CORE_ADDR lowpc;
6350
6351 /* DW_AT_entry_pc should be preferred. */
6352 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6353 complaint (&symfile_complaints,
6354 _("DW_AT_GNU_call_site_target target DIE has invalid "
6355 "low pc, for referencing DIE 0x%x [in module %s]"),
6356 die->offset, cu->objfile->name);
6357 else
6358 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6359 }
6360 }
6361 else
6362 complaint (&symfile_complaints,
6363 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6364 "block nor reference, for DIE 0x%x [in module %s]"),
6365 die->offset, cu->objfile->name);
6366
6367 call_site->per_cu = cu->per_cu;
6368
6369 for (child_die = die->child;
6370 child_die && child_die->tag;
6371 child_die = sibling_die (child_die))
6372 {
6373 struct dwarf2_locexpr_baton *dlbaton;
6374 struct call_site_parameter *parameter;
6375
6376 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6377 {
6378 /* Already printed the complaint above. */
6379 continue;
6380 }
6381
6382 gdb_assert (call_site->parameter_count < nparams);
6383 parameter = &call_site->parameter[call_site->parameter_count];
6384
6385 /* DW_AT_location specifies the register number. Value of the data
6386 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6387
6388 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6389 if (!attr || !attr_form_is_block (attr))
6390 {
6391 complaint (&symfile_complaints,
6392 _("No DW_FORM_block* DW_AT_location for "
6393 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6394 child_die->offset, cu->objfile->name);
6395 continue;
6396 }
6397 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6398 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6399 if (parameter->dwarf_reg == -1
6400 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6401 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6402 &parameter->fb_offset))
6403 {
6404 complaint (&symfile_complaints,
6405 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6406 "for DW_FORM_block* DW_AT_location for "
6407 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6408 child_die->offset, cu->objfile->name);
6409 continue;
6410 }
6411
6412 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6413 if (!attr_form_is_block (attr))
6414 {
6415 complaint (&symfile_complaints,
6416 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6417 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6418 child_die->offset, cu->objfile->name);
6419 continue;
6420 }
6421 parameter->value = DW_BLOCK (attr)->data;
6422 parameter->value_size = DW_BLOCK (attr)->size;
6423
6424 /* Parameters are not pre-cleared by memset above. */
6425 parameter->data_value = NULL;
6426 parameter->data_value_size = 0;
6427 call_site->parameter_count++;
6428
6429 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6430 if (attr)
6431 {
6432 if (!attr_form_is_block (attr))
6433 complaint (&symfile_complaints,
6434 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6435 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6436 child_die->offset, cu->objfile->name);
6437 else
6438 {
6439 parameter->data_value = DW_BLOCK (attr)->data;
6440 parameter->data_value_size = DW_BLOCK (attr)->size;
6441 }
6442 }
6443 }
6444}
6445
43039443 6446/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
6447 Return 1 if the attributes are present and valid, otherwise, return 0.
6448 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
6449
6450static int
6451dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
6452 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6453 struct partial_symtab *ranges_pst)
43039443
JK
6454{
6455 struct objfile *objfile = cu->objfile;
6456 struct comp_unit_head *cu_header = &cu->header;
6457 bfd *obfd = objfile->obfd;
6458 unsigned int addr_size = cu_header->addr_size;
6459 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6460 /* Base address selection entry. */
6461 CORE_ADDR base;
6462 int found_base;
6463 unsigned int dummy;
6464 gdb_byte *buffer;
6465 CORE_ADDR marker;
6466 int low_set;
6467 CORE_ADDR low = 0;
6468 CORE_ADDR high = 0;
ff013f42 6469 CORE_ADDR baseaddr;
43039443 6470
d00adf39
DE
6471 found_base = cu->base_known;
6472 base = cu->base_address;
43039443 6473
be391dca 6474 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 6475 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
6476 {
6477 complaint (&symfile_complaints,
6478 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6479 offset);
6480 return 0;
6481 }
dce234bc 6482 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
6483
6484 /* Read in the largest possible address. */
6485 marker = read_address (obfd, buffer, cu, &dummy);
6486 if ((marker & mask) == mask)
6487 {
6488 /* If we found the largest possible address, then
6489 read the base address. */
6490 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6491 buffer += 2 * addr_size;
6492 offset += 2 * addr_size;
6493 found_base = 1;
6494 }
6495
6496 low_set = 0;
6497
e7030f15 6498 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 6499
43039443
JK
6500 while (1)
6501 {
6502 CORE_ADDR range_beginning, range_end;
6503
6504 range_beginning = read_address (obfd, buffer, cu, &dummy);
6505 buffer += addr_size;
6506 range_end = read_address (obfd, buffer, cu, &dummy);
6507 buffer += addr_size;
6508 offset += 2 * addr_size;
6509
6510 /* An end of list marker is a pair of zero addresses. */
6511 if (range_beginning == 0 && range_end == 0)
6512 /* Found the end of list entry. */
6513 break;
6514
6515 /* Each base address selection entry is a pair of 2 values.
6516 The first is the largest possible address, the second is
6517 the base address. Check for a base address here. */
6518 if ((range_beginning & mask) == mask)
6519 {
6520 /* If we found the largest possible address, then
6521 read the base address. */
6522 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6523 found_base = 1;
6524 continue;
6525 }
6526
6527 if (!found_base)
6528 {
6529 /* We have no valid base address for the ranges
6530 data. */
6531 complaint (&symfile_complaints,
6532 _("Invalid .debug_ranges data (no base address)"));
6533 return 0;
6534 }
6535
9277c30c
UW
6536 if (range_beginning > range_end)
6537 {
6538 /* Inverted range entries are invalid. */
6539 complaint (&symfile_complaints,
6540 _("Invalid .debug_ranges data (inverted range)"));
6541 return 0;
6542 }
6543
6544 /* Empty range entries have no effect. */
6545 if (range_beginning == range_end)
6546 continue;
6547
43039443
JK
6548 range_beginning += base;
6549 range_end += base;
6550
9277c30c 6551 if (ranges_pst != NULL)
ff013f42 6552 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
6553 range_beginning + baseaddr,
6554 range_end - 1 + baseaddr,
ff013f42
JK
6555 ranges_pst);
6556
43039443
JK
6557 /* FIXME: This is recording everything as a low-high
6558 segment of consecutive addresses. We should have a
6559 data structure for discontiguous block ranges
6560 instead. */
6561 if (! low_set)
6562 {
6563 low = range_beginning;
6564 high = range_end;
6565 low_set = 1;
6566 }
6567 else
6568 {
6569 if (range_beginning < low)
6570 low = range_beginning;
6571 if (range_end > high)
6572 high = range_end;
6573 }
6574 }
6575
6576 if (! low_set)
6577 /* If the first entry is an end-of-list marker, the range
6578 describes an empty scope, i.e. no instructions. */
6579 return 0;
6580
6581 if (low_return)
6582 *low_return = low;
6583 if (high_return)
6584 *high_return = high;
6585 return 1;
6586}
6587
af34e669
DJ
6588/* Get low and high pc attributes from a die. Return 1 if the attributes
6589 are present and valid, otherwise, return 0. Return -1 if the range is
6590 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 6591static int
af34e669 6592dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
6593 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6594 struct partial_symtab *pst)
c906108c
SS
6595{
6596 struct attribute *attr;
af34e669
DJ
6597 CORE_ADDR low = 0;
6598 CORE_ADDR high = 0;
6599 int ret = 0;
c906108c 6600
e142c38c 6601 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 6602 if (attr)
af34e669
DJ
6603 {
6604 high = DW_ADDR (attr);
e142c38c 6605 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
6606 if (attr)
6607 low = DW_ADDR (attr);
6608 else
6609 /* Found high w/o low attribute. */
6610 return 0;
6611
6612 /* Found consecutive range of addresses. */
6613 ret = 1;
6614 }
c906108c 6615 else
af34e669 6616 {
e142c38c 6617 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
6618 if (attr != NULL)
6619 {
af34e669 6620 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 6621 .debug_ranges section. */
d85a05f0 6622 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 6623 return 0;
43039443 6624 /* Found discontinuous range of addresses. */
af34e669
DJ
6625 ret = -1;
6626 }
6627 }
c906108c 6628
9373cf26
JK
6629 /* read_partial_die has also the strict LOW < HIGH requirement. */
6630 if (high <= low)
c906108c
SS
6631 return 0;
6632
6633 /* When using the GNU linker, .gnu.linkonce. sections are used to
6634 eliminate duplicate copies of functions and vtables and such.
6635 The linker will arbitrarily choose one and discard the others.
6636 The AT_*_pc values for such functions refer to local labels in
6637 these sections. If the section from that file was discarded, the
6638 labels are not in the output, so the relocs get a value of 0.
6639 If this is a discarded function, mark the pc bounds as invalid,
6640 so that GDB will ignore it. */
72dca2f5 6641 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
6642 return 0;
6643
6644 *lowpc = low;
96408a79
SA
6645 if (highpc)
6646 *highpc = high;
af34e669 6647 return ret;
c906108c
SS
6648}
6649
b084d499
JB
6650/* Assuming that DIE represents a subprogram DIE or a lexical block, get
6651 its low and high PC addresses. Do nothing if these addresses could not
6652 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6653 and HIGHPC to the high address if greater than HIGHPC. */
6654
6655static void
6656dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6657 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6658 struct dwarf2_cu *cu)
6659{
6660 CORE_ADDR low, high;
6661 struct die_info *child = die->child;
6662
d85a05f0 6663 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
6664 {
6665 *lowpc = min (*lowpc, low);
6666 *highpc = max (*highpc, high);
6667 }
6668
6669 /* If the language does not allow nested subprograms (either inside
6670 subprograms or lexical blocks), we're done. */
6671 if (cu->language != language_ada)
6672 return;
6e70227d 6673
b084d499
JB
6674 /* Check all the children of the given DIE. If it contains nested
6675 subprograms, then check their pc bounds. Likewise, we need to
6676 check lexical blocks as well, as they may also contain subprogram
6677 definitions. */
6678 while (child && child->tag)
6679 {
6680 if (child->tag == DW_TAG_subprogram
6681 || child->tag == DW_TAG_lexical_block)
6682 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6683 child = sibling_die (child);
6684 }
6685}
6686
fae299cd
DC
6687/* Get the low and high pc's represented by the scope DIE, and store
6688 them in *LOWPC and *HIGHPC. If the correct values can't be
6689 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6690
6691static void
6692get_scope_pc_bounds (struct die_info *die,
6693 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6694 struct dwarf2_cu *cu)
6695{
6696 CORE_ADDR best_low = (CORE_ADDR) -1;
6697 CORE_ADDR best_high = (CORE_ADDR) 0;
6698 CORE_ADDR current_low, current_high;
6699
d85a05f0 6700 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
6701 {
6702 best_low = current_low;
6703 best_high = current_high;
6704 }
6705 else
6706 {
6707 struct die_info *child = die->child;
6708
6709 while (child && child->tag)
6710 {
6711 switch (child->tag) {
6712 case DW_TAG_subprogram:
b084d499 6713 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
6714 break;
6715 case DW_TAG_namespace:
f55ee35c 6716 case DW_TAG_module:
fae299cd
DC
6717 /* FIXME: carlton/2004-01-16: Should we do this for
6718 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6719 that current GCC's always emit the DIEs corresponding
6720 to definitions of methods of classes as children of a
6721 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6722 the DIEs giving the declarations, which could be
6723 anywhere). But I don't see any reason why the
6724 standards says that they have to be there. */
6725 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6726
6727 if (current_low != ((CORE_ADDR) -1))
6728 {
6729 best_low = min (best_low, current_low);
6730 best_high = max (best_high, current_high);
6731 }
6732 break;
6733 default:
0963b4bd 6734 /* Ignore. */
fae299cd
DC
6735 break;
6736 }
6737
6738 child = sibling_die (child);
6739 }
6740 }
6741
6742 *lowpc = best_low;
6743 *highpc = best_high;
6744}
6745
801e3a5b
JB
6746/* Record the address ranges for BLOCK, offset by BASEADDR, as given
6747 in DIE. */
6748static void
6749dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6750 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6751{
6752 struct attribute *attr;
6753
6754 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6755 if (attr)
6756 {
6757 CORE_ADDR high = DW_ADDR (attr);
9a619af0 6758
801e3a5b
JB
6759 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6760 if (attr)
6761 {
6762 CORE_ADDR low = DW_ADDR (attr);
9a619af0 6763
801e3a5b
JB
6764 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6765 }
6766 }
6767
6768 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6769 if (attr)
6770 {
6771 bfd *obfd = cu->objfile->obfd;
6772
6773 /* The value of the DW_AT_ranges attribute is the offset of the
6774 address range list in the .debug_ranges section. */
6775 unsigned long offset = DW_UNSND (attr);
dce234bc 6776 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
6777
6778 /* For some target architectures, but not others, the
6779 read_address function sign-extends the addresses it returns.
6780 To recognize base address selection entries, we need a
6781 mask. */
6782 unsigned int addr_size = cu->header.addr_size;
6783 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6784
6785 /* The base address, to which the next pair is relative. Note
6786 that this 'base' is a DWARF concept: most entries in a range
6787 list are relative, to reduce the number of relocs against the
6788 debugging information. This is separate from this function's
6789 'baseaddr' argument, which GDB uses to relocate debugging
6790 information from a shared library based on the address at
6791 which the library was loaded. */
d00adf39
DE
6792 CORE_ADDR base = cu->base_address;
6793 int base_known = cu->base_known;
801e3a5b 6794
be391dca 6795 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 6796 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
6797 {
6798 complaint (&symfile_complaints,
6799 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6800 offset);
6801 return;
6802 }
6803
6804 for (;;)
6805 {
6806 unsigned int bytes_read;
6807 CORE_ADDR start, end;
6808
6809 start = read_address (obfd, buffer, cu, &bytes_read);
6810 buffer += bytes_read;
6811 end = read_address (obfd, buffer, cu, &bytes_read);
6812 buffer += bytes_read;
6813
6814 /* Did we find the end of the range list? */
6815 if (start == 0 && end == 0)
6816 break;
6817
6818 /* Did we find a base address selection entry? */
6819 else if ((start & base_select_mask) == base_select_mask)
6820 {
6821 base = end;
6822 base_known = 1;
6823 }
6824
6825 /* We found an ordinary address range. */
6826 else
6827 {
6828 if (!base_known)
6829 {
6830 complaint (&symfile_complaints,
3e43a32a
MS
6831 _("Invalid .debug_ranges data "
6832 "(no base address)"));
801e3a5b
JB
6833 return;
6834 }
6835
9277c30c
UW
6836 if (start > end)
6837 {
6838 /* Inverted range entries are invalid. */
6839 complaint (&symfile_complaints,
6840 _("Invalid .debug_ranges data "
6841 "(inverted range)"));
6842 return;
6843 }
6844
6845 /* Empty range entries have no effect. */
6846 if (start == end)
6847 continue;
6848
6e70227d
DE
6849 record_block_range (block,
6850 baseaddr + base + start,
801e3a5b
JB
6851 baseaddr + base + end - 1);
6852 }
6853 }
6854 }
6855}
6856
60d5a603
JK
6857/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6858 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6859 during 4.6.0 experimental. */
6860
6861static int
6862producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6863{
6864 const char *cs;
6865 int major, minor, release;
6866
6867 if (cu->producer == NULL)
6868 {
6869 /* For unknown compilers expect their behavior is DWARF version
6870 compliant.
6871
6872 GCC started to support .debug_types sections by -gdwarf-4 since
6873 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6874 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6875 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6876 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6877
6878 return 0;
6879 }
6880
6881 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6882
6883 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6884 {
6885 /* For non-GCC compilers expect their behavior is DWARF version
6886 compliant. */
6887
6888 return 0;
6889 }
6890 cs = &cu->producer[strlen ("GNU ")];
6891 while (*cs && !isdigit (*cs))
6892 cs++;
6893 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6894 {
6895 /* Not recognized as GCC. */
6896
6897 return 0;
6898 }
6899
6900 return major < 4 || (major == 4 && minor < 6);
6901}
6902
6903/* Return the default accessibility type if it is not overriden by
6904 DW_AT_accessibility. */
6905
6906static enum dwarf_access_attribute
6907dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6908{
6909 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6910 {
6911 /* The default DWARF 2 accessibility for members is public, the default
6912 accessibility for inheritance is private. */
6913
6914 if (die->tag != DW_TAG_inheritance)
6915 return DW_ACCESS_public;
6916 else
6917 return DW_ACCESS_private;
6918 }
6919 else
6920 {
6921 /* DWARF 3+ defines the default accessibility a different way. The same
6922 rules apply now for DW_TAG_inheritance as for the members and it only
6923 depends on the container kind. */
6924
6925 if (die->parent->tag == DW_TAG_class_type)
6926 return DW_ACCESS_private;
6927 else
6928 return DW_ACCESS_public;
6929 }
6930}
6931
74ac6d43
TT
6932/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6933 offset. If the attribute was not found return 0, otherwise return
6934 1. If it was found but could not properly be handled, set *OFFSET
6935 to 0. */
6936
6937static int
6938handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6939 LONGEST *offset)
6940{
6941 struct attribute *attr;
6942
6943 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6944 if (attr != NULL)
6945 {
6946 *offset = 0;
6947
6948 /* Note that we do not check for a section offset first here.
6949 This is because DW_AT_data_member_location is new in DWARF 4,
6950 so if we see it, we can assume that a constant form is really
6951 a constant and not a section offset. */
6952 if (attr_form_is_constant (attr))
6953 *offset = dwarf2_get_attr_constant_value (attr, 0);
6954 else if (attr_form_is_section_offset (attr))
6955 dwarf2_complex_location_expr_complaint ();
6956 else if (attr_form_is_block (attr))
6957 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6958 else
6959 dwarf2_complex_location_expr_complaint ();
6960
6961 return 1;
6962 }
6963
6964 return 0;
6965}
6966
c906108c
SS
6967/* Add an aggregate field to the field list. */
6968
6969static void
107d2387 6970dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 6971 struct dwarf2_cu *cu)
6e70227d 6972{
e7c27a73 6973 struct objfile *objfile = cu->objfile;
5e2b427d 6974 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
6975 struct nextfield *new_field;
6976 struct attribute *attr;
6977 struct field *fp;
6978 char *fieldname = "";
6979
6980 /* Allocate a new field list entry and link it in. */
6981 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 6982 make_cleanup (xfree, new_field);
c906108c 6983 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
6984
6985 if (die->tag == DW_TAG_inheritance)
6986 {
6987 new_field->next = fip->baseclasses;
6988 fip->baseclasses = new_field;
6989 }
6990 else
6991 {
6992 new_field->next = fip->fields;
6993 fip->fields = new_field;
6994 }
c906108c
SS
6995 fip->nfields++;
6996
e142c38c 6997 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6998 if (attr)
6999 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
7000 else
7001 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
7002 if (new_field->accessibility != DW_ACCESS_public)
7003 fip->non_public_fields = 1;
60d5a603 7004
e142c38c 7005 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
7006 if (attr)
7007 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
7008 else
7009 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
7010
7011 fp = &new_field->field;
a9a9bd0f 7012
e142c38c 7013 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 7014 {
74ac6d43
TT
7015 LONGEST offset;
7016
a9a9bd0f 7017 /* Data member other than a C++ static data member. */
6e70227d 7018
c906108c 7019 /* Get type of field. */
e7c27a73 7020 fp->type = die_type (die, cu);
c906108c 7021
d6a843b5 7022 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 7023
c906108c 7024 /* Get bit size of field (zero if none). */
e142c38c 7025 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
7026 if (attr)
7027 {
7028 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7029 }
7030 else
7031 {
7032 FIELD_BITSIZE (*fp) = 0;
7033 }
7034
7035 /* Get bit offset of field. */
74ac6d43
TT
7036 if (handle_data_member_location (die, cu, &offset))
7037 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 7038 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
7039 if (attr)
7040 {
5e2b427d 7041 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
7042 {
7043 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
7044 additional bit offset from the MSB of the containing
7045 anonymous object to the MSB of the field. We don't
7046 have to do anything special since we don't need to
7047 know the size of the anonymous object. */
c906108c
SS
7048 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7049 }
7050 else
7051 {
7052 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
7053 MSB of the anonymous object, subtract off the number of
7054 bits from the MSB of the field to the MSB of the
7055 object, and then subtract off the number of bits of
7056 the field itself. The result is the bit offset of
7057 the LSB of the field. */
c906108c
SS
7058 int anonymous_size;
7059 int bit_offset = DW_UNSND (attr);
7060
e142c38c 7061 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7062 if (attr)
7063 {
7064 /* The size of the anonymous object containing
7065 the bit field is explicit, so use the
7066 indicated size (in bytes). */
7067 anonymous_size = DW_UNSND (attr);
7068 }
7069 else
7070 {
7071 /* The size of the anonymous object containing
7072 the bit field must be inferred from the type
7073 attribute of the data member containing the
7074 bit field. */
7075 anonymous_size = TYPE_LENGTH (fp->type);
7076 }
7077 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7078 - bit_offset - FIELD_BITSIZE (*fp);
7079 }
7080 }
7081
7082 /* Get name of field. */
39cbfefa
DJ
7083 fieldname = dwarf2_name (die, cu);
7084 if (fieldname == NULL)
7085 fieldname = "";
d8151005
DJ
7086
7087 /* The name is already allocated along with this objfile, so we don't
7088 need to duplicate it for the type. */
7089 fp->name = fieldname;
c906108c
SS
7090
7091 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 7092 pointer or virtual base class pointer) to private. */
e142c38c 7093 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 7094 {
d48cc9dd 7095 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
7096 new_field->accessibility = DW_ACCESS_private;
7097 fip->non_public_fields = 1;
7098 }
7099 }
a9a9bd0f 7100 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 7101 {
a9a9bd0f
DC
7102 /* C++ static member. */
7103
7104 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7105 is a declaration, but all versions of G++ as of this writing
7106 (so through at least 3.2.1) incorrectly generate
7107 DW_TAG_variable tags. */
6e70227d 7108
ff355380 7109 const char *physname;
c906108c 7110
a9a9bd0f 7111 /* Get name of field. */
39cbfefa
DJ
7112 fieldname = dwarf2_name (die, cu);
7113 if (fieldname == NULL)
c906108c
SS
7114 return;
7115
254e6b9e 7116 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
7117 if (attr
7118 /* Only create a symbol if this is an external value.
7119 new_symbol checks this and puts the value in the global symbol
7120 table, which we want. If it is not external, new_symbol
7121 will try to put the value in cu->list_in_scope which is wrong. */
7122 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
7123 {
7124 /* A static const member, not much different than an enum as far as
7125 we're concerned, except that we can support more types. */
7126 new_symbol (die, NULL, cu);
7127 }
7128
2df3850c 7129 /* Get physical name. */
ff355380 7130 physname = dwarf2_physname (fieldname, die, cu);
c906108c 7131
d8151005
DJ
7132 /* The name is already allocated along with this objfile, so we don't
7133 need to duplicate it for the type. */
7134 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 7135 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 7136 FIELD_NAME (*fp) = fieldname;
c906108c
SS
7137 }
7138 else if (die->tag == DW_TAG_inheritance)
7139 {
74ac6d43 7140 LONGEST offset;
d4b96c9a 7141
74ac6d43
TT
7142 /* C++ base class field. */
7143 if (handle_data_member_location (die, cu, &offset))
7144 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 7145 FIELD_BITSIZE (*fp) = 0;
e7c27a73 7146 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
7147 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7148 fip->nbaseclasses++;
7149 }
7150}
7151
98751a41
JK
7152/* Add a typedef defined in the scope of the FIP's class. */
7153
7154static void
7155dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7156 struct dwarf2_cu *cu)
6e70227d 7157{
98751a41 7158 struct objfile *objfile = cu->objfile;
98751a41
JK
7159 struct typedef_field_list *new_field;
7160 struct attribute *attr;
7161 struct typedef_field *fp;
7162 char *fieldname = "";
7163
7164 /* Allocate a new field list entry and link it in. */
7165 new_field = xzalloc (sizeof (*new_field));
7166 make_cleanup (xfree, new_field);
7167
7168 gdb_assert (die->tag == DW_TAG_typedef);
7169
7170 fp = &new_field->field;
7171
7172 /* Get name of field. */
7173 fp->name = dwarf2_name (die, cu);
7174 if (fp->name == NULL)
7175 return;
7176
7177 fp->type = read_type_die (die, cu);
7178
7179 new_field->next = fip->typedef_field_list;
7180 fip->typedef_field_list = new_field;
7181 fip->typedef_field_list_count++;
7182}
7183
c906108c
SS
7184/* Create the vector of fields, and attach it to the type. */
7185
7186static void
fba45db2 7187dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7188 struct dwarf2_cu *cu)
c906108c
SS
7189{
7190 int nfields = fip->nfields;
7191
7192 /* Record the field count, allocate space for the array of fields,
7193 and create blank accessibility bitfields if necessary. */
7194 TYPE_NFIELDS (type) = nfields;
7195 TYPE_FIELDS (type) = (struct field *)
7196 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7197 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7198
b4ba55a1 7199 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
7200 {
7201 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7202
7203 TYPE_FIELD_PRIVATE_BITS (type) =
7204 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7205 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7206
7207 TYPE_FIELD_PROTECTED_BITS (type) =
7208 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7209 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7210
774b6a14
TT
7211 TYPE_FIELD_IGNORE_BITS (type) =
7212 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7213 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
7214 }
7215
7216 /* If the type has baseclasses, allocate and clear a bit vector for
7217 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 7218 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
7219 {
7220 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 7221 unsigned char *pointer;
c906108c
SS
7222
7223 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
7224 pointer = TYPE_ALLOC (type, num_bytes);
7225 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
7226 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7227 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7228 }
7229
3e43a32a
MS
7230 /* Copy the saved-up fields into the field vector. Start from the head of
7231 the list, adding to the tail of the field array, so that they end up in
7232 the same order in the array in which they were added to the list. */
c906108c
SS
7233 while (nfields-- > 0)
7234 {
7d0ccb61
DJ
7235 struct nextfield *fieldp;
7236
7237 if (fip->fields)
7238 {
7239 fieldp = fip->fields;
7240 fip->fields = fieldp->next;
7241 }
7242 else
7243 {
7244 fieldp = fip->baseclasses;
7245 fip->baseclasses = fieldp->next;
7246 }
7247
7248 TYPE_FIELD (type, nfields) = fieldp->field;
7249 switch (fieldp->accessibility)
c906108c 7250 {
c5aa993b 7251 case DW_ACCESS_private:
b4ba55a1
JB
7252 if (cu->language != language_ada)
7253 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 7254 break;
c906108c 7255
c5aa993b 7256 case DW_ACCESS_protected:
b4ba55a1
JB
7257 if (cu->language != language_ada)
7258 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 7259 break;
c906108c 7260
c5aa993b
JM
7261 case DW_ACCESS_public:
7262 break;
c906108c 7263
c5aa993b
JM
7264 default:
7265 /* Unknown accessibility. Complain and treat it as public. */
7266 {
e2e0b3e5 7267 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 7268 fieldp->accessibility);
c5aa993b
JM
7269 }
7270 break;
c906108c
SS
7271 }
7272 if (nfields < fip->nbaseclasses)
7273 {
7d0ccb61 7274 switch (fieldp->virtuality)
c906108c 7275 {
c5aa993b
JM
7276 case DW_VIRTUALITY_virtual:
7277 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 7278 if (cu->language == language_ada)
a73c6dcd 7279 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
7280 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7281 break;
c906108c
SS
7282 }
7283 }
c906108c
SS
7284 }
7285}
7286
c906108c
SS
7287/* Add a member function to the proper fieldlist. */
7288
7289static void
107d2387 7290dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 7291 struct type *type, struct dwarf2_cu *cu)
c906108c 7292{
e7c27a73 7293 struct objfile *objfile = cu->objfile;
c906108c
SS
7294 struct attribute *attr;
7295 struct fnfieldlist *flp;
7296 int i;
7297 struct fn_field *fnp;
7298 char *fieldname;
c906108c 7299 struct nextfnfield *new_fnfield;
f792889a 7300 struct type *this_type;
60d5a603 7301 enum dwarf_access_attribute accessibility;
c906108c 7302
b4ba55a1 7303 if (cu->language == language_ada)
a73c6dcd 7304 error (_("unexpected member function in Ada type"));
b4ba55a1 7305
2df3850c 7306 /* Get name of member function. */
39cbfefa
DJ
7307 fieldname = dwarf2_name (die, cu);
7308 if (fieldname == NULL)
2df3850c 7309 return;
c906108c 7310
c906108c
SS
7311 /* Look up member function name in fieldlist. */
7312 for (i = 0; i < fip->nfnfields; i++)
7313 {
27bfe10e 7314 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
7315 break;
7316 }
7317
7318 /* Create new list element if necessary. */
7319 if (i < fip->nfnfields)
7320 flp = &fip->fnfieldlists[i];
7321 else
7322 {
7323 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7324 {
7325 fip->fnfieldlists = (struct fnfieldlist *)
7326 xrealloc (fip->fnfieldlists,
7327 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 7328 * sizeof (struct fnfieldlist));
c906108c 7329 if (fip->nfnfields == 0)
c13c43fd 7330 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
7331 }
7332 flp = &fip->fnfieldlists[fip->nfnfields];
7333 flp->name = fieldname;
7334 flp->length = 0;
7335 flp->head = NULL;
3da10d80 7336 i = fip->nfnfields++;
c906108c
SS
7337 }
7338
7339 /* Create a new member function field and chain it to the field list
0963b4bd 7340 entry. */
c906108c 7341 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 7342 make_cleanup (xfree, new_fnfield);
c906108c
SS
7343 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7344 new_fnfield->next = flp->head;
7345 flp->head = new_fnfield;
7346 flp->length++;
7347
7348 /* Fill in the member function field info. */
7349 fnp = &new_fnfield->fnfield;
3da10d80
KS
7350
7351 /* Delay processing of the physname until later. */
7352 if (cu->language == language_cplus || cu->language == language_java)
7353 {
7354 add_to_method_list (type, i, flp->length - 1, fieldname,
7355 die, cu);
7356 }
7357 else
7358 {
1d06ead6 7359 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
7360 fnp->physname = physname ? physname : "";
7361 }
7362
c906108c 7363 fnp->type = alloc_type (objfile);
f792889a
DJ
7364 this_type = read_type_die (die, cu);
7365 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 7366 {
f792889a 7367 int nparams = TYPE_NFIELDS (this_type);
c906108c 7368
f792889a 7369 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
7370 of the method itself (TYPE_CODE_METHOD). */
7371 smash_to_method_type (fnp->type, type,
f792889a
DJ
7372 TYPE_TARGET_TYPE (this_type),
7373 TYPE_FIELDS (this_type),
7374 TYPE_NFIELDS (this_type),
7375 TYPE_VARARGS (this_type));
c906108c
SS
7376
7377 /* Handle static member functions.
c5aa993b 7378 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
7379 member functions. G++ helps GDB by marking the first
7380 parameter for non-static member functions (which is the this
7381 pointer) as artificial. We obtain this information from
7382 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 7383 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
7384 fnp->voffset = VOFFSET_STATIC;
7385 }
7386 else
e2e0b3e5 7387 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 7388 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
7389
7390 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 7391 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 7392 fnp->fcontext = die_containing_type (die, cu);
c906108c 7393
3e43a32a
MS
7394 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7395 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
7396
7397 /* Get accessibility. */
e142c38c 7398 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 7399 if (attr)
60d5a603
JK
7400 accessibility = DW_UNSND (attr);
7401 else
7402 accessibility = dwarf2_default_access_attribute (die, cu);
7403 switch (accessibility)
c906108c 7404 {
60d5a603
JK
7405 case DW_ACCESS_private:
7406 fnp->is_private = 1;
7407 break;
7408 case DW_ACCESS_protected:
7409 fnp->is_protected = 1;
7410 break;
c906108c
SS
7411 }
7412
b02dede2 7413 /* Check for artificial methods. */
e142c38c 7414 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
7415 if (attr && DW_UNSND (attr) != 0)
7416 fnp->is_artificial = 1;
7417
0d564a31 7418 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
7419 function. For older versions of GCC, this is an offset in the
7420 appropriate virtual table, as specified by DW_AT_containing_type.
7421 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
7422 to the object address. */
7423
e142c38c 7424 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 7425 if (attr)
8e19ed76 7426 {
aec5aa8b 7427 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 7428 {
aec5aa8b
TT
7429 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7430 {
7431 /* Old-style GCC. */
7432 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7433 }
7434 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7435 || (DW_BLOCK (attr)->size > 1
7436 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7437 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7438 {
7439 struct dwarf_block blk;
7440 int offset;
7441
7442 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7443 ? 1 : 2);
7444 blk.size = DW_BLOCK (attr)->size - offset;
7445 blk.data = DW_BLOCK (attr)->data + offset;
7446 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7447 if ((fnp->voffset % cu->header.addr_size) != 0)
7448 dwarf2_complex_location_expr_complaint ();
7449 else
7450 fnp->voffset /= cu->header.addr_size;
7451 fnp->voffset += 2;
7452 }
7453 else
7454 dwarf2_complex_location_expr_complaint ();
7455
7456 if (!fnp->fcontext)
7457 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7458 }
3690dd37 7459 else if (attr_form_is_section_offset (attr))
8e19ed76 7460 {
4d3c2250 7461 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
7462 }
7463 else
7464 {
4d3c2250
KB
7465 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7466 fieldname);
8e19ed76 7467 }
0d564a31 7468 }
d48cc9dd
DJ
7469 else
7470 {
7471 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7472 if (attr && DW_UNSND (attr))
7473 {
7474 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7475 complaint (&symfile_complaints,
3e43a32a
MS
7476 _("Member function \"%s\" (offset %d) is virtual "
7477 "but the vtable offset is not specified"),
d48cc9dd 7478 fieldname, die->offset);
9655fd1a 7479 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
7480 TYPE_CPLUS_DYNAMIC (type) = 1;
7481 }
7482 }
c906108c
SS
7483}
7484
7485/* Create the vector of member function fields, and attach it to the type. */
7486
7487static void
fba45db2 7488dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 7489 struct dwarf2_cu *cu)
c906108c
SS
7490{
7491 struct fnfieldlist *flp;
7492 int total_length = 0;
7493 int i;
7494
b4ba55a1 7495 if (cu->language == language_ada)
a73c6dcd 7496 error (_("unexpected member functions in Ada type"));
b4ba55a1 7497
c906108c
SS
7498 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7499 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7500 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7501
7502 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7503 {
7504 struct nextfnfield *nfp = flp->head;
7505 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7506 int k;
7507
7508 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7509 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7510 fn_flp->fn_fields = (struct fn_field *)
7511 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7512 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 7513 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
7514
7515 total_length += flp->length;
7516 }
7517
7518 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7519 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7520}
7521
1168df01
JB
7522/* Returns non-zero if NAME is the name of a vtable member in CU's
7523 language, zero otherwise. */
7524static int
7525is_vtable_name (const char *name, struct dwarf2_cu *cu)
7526{
7527 static const char vptr[] = "_vptr";
987504bb 7528 static const char vtable[] = "vtable";
1168df01 7529
987504bb
JJ
7530 /* Look for the C++ and Java forms of the vtable. */
7531 if ((cu->language == language_java
7532 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7533 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7534 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
7535 return 1;
7536
7537 return 0;
7538}
7539
c0dd20ea 7540/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
7541 functions, with the ABI-specified layout. If TYPE describes
7542 such a structure, smash it into a member function type.
61049d3b
DJ
7543
7544 GCC shouldn't do this; it should just output pointer to member DIEs.
7545 This is GCC PR debug/28767. */
c0dd20ea 7546
0b92b5bb
TT
7547static void
7548quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 7549{
0b92b5bb 7550 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
7551
7552 /* Check for a structure with no name and two children. */
0b92b5bb
TT
7553 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7554 return;
c0dd20ea
DJ
7555
7556 /* Check for __pfn and __delta members. */
0b92b5bb
TT
7557 if (TYPE_FIELD_NAME (type, 0) == NULL
7558 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7559 || TYPE_FIELD_NAME (type, 1) == NULL
7560 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7561 return;
c0dd20ea
DJ
7562
7563 /* Find the type of the method. */
0b92b5bb 7564 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
7565 if (pfn_type == NULL
7566 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7567 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 7568 return;
c0dd20ea
DJ
7569
7570 /* Look for the "this" argument. */
7571 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7572 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 7573 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 7574 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 7575 return;
c0dd20ea
DJ
7576
7577 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
7578 new_type = alloc_type (objfile);
7579 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
7580 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7581 TYPE_VARARGS (pfn_type));
0b92b5bb 7582 smash_to_methodptr_type (type, new_type);
c0dd20ea 7583}
1168df01 7584
c906108c 7585/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
7586 (definition) to create a type for the structure or union. Fill in
7587 the type's name and general properties; the members will not be
7588 processed until process_structure_type.
c906108c 7589
c767944b
DJ
7590 NOTE: we need to call these functions regardless of whether or not the
7591 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
7592 structure or union. This gets the type entered into our set of
7593 user defined types.
7594
7595 However, if the structure is incomplete (an opaque struct/union)
7596 then suppress creating a symbol table entry for it since gdb only
7597 wants to find the one with the complete definition. Note that if
7598 it is complete, we just call new_symbol, which does it's own
7599 checking about whether the struct/union is anonymous or not (and
7600 suppresses creating a symbol table entry itself). */
7601
f792889a 7602static struct type *
134d01f1 7603read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7604{
e7c27a73 7605 struct objfile *objfile = cu->objfile;
c906108c
SS
7606 struct type *type;
7607 struct attribute *attr;
39cbfefa 7608 char *name;
c906108c 7609
348e048f
DE
7610 /* If the definition of this type lives in .debug_types, read that type.
7611 Don't follow DW_AT_specification though, that will take us back up
7612 the chain and we want to go down. */
7613 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7614 if (attr)
7615 {
7616 struct dwarf2_cu *type_cu = cu;
7617 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7618
348e048f
DE
7619 /* We could just recurse on read_structure_type, but we need to call
7620 get_die_type to ensure only one type for this DIE is created.
7621 This is important, for example, because for c++ classes we need
7622 TYPE_NAME set which is only done by new_symbol. Blech. */
7623 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7624
7625 /* TYPE_CU may not be the same as CU.
7626 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7627 return set_die_type (die, type, cu);
7628 }
7629
c0dd20ea 7630 type = alloc_type (objfile);
c906108c 7631 INIT_CPLUS_SPECIFIC (type);
93311388 7632
39cbfefa
DJ
7633 name = dwarf2_name (die, cu);
7634 if (name != NULL)
c906108c 7635 {
987504bb
JJ
7636 if (cu->language == language_cplus
7637 || cu->language == language_java)
63d06c5c 7638 {
3da10d80
KS
7639 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7640
7641 /* dwarf2_full_name might have already finished building the DIE's
7642 type. If so, there is no need to continue. */
7643 if (get_die_type (die, cu) != NULL)
7644 return get_die_type (die, cu);
7645
7646 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
7647 if (die->tag == DW_TAG_structure_type
7648 || die->tag == DW_TAG_class_type)
7649 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
7650 }
7651 else
7652 {
d8151005
DJ
7653 /* The name is already allocated along with this objfile, so
7654 we don't need to duplicate it for the type. */
94af9270
KS
7655 TYPE_TAG_NAME (type) = (char *) name;
7656 if (die->tag == DW_TAG_class_type)
7657 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 7658 }
c906108c
SS
7659 }
7660
7661 if (die->tag == DW_TAG_structure_type)
7662 {
7663 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7664 }
7665 else if (die->tag == DW_TAG_union_type)
7666 {
7667 TYPE_CODE (type) = TYPE_CODE_UNION;
7668 }
7669 else
7670 {
c906108c
SS
7671 TYPE_CODE (type) = TYPE_CODE_CLASS;
7672 }
7673
0cc2414c
TT
7674 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7675 TYPE_DECLARED_CLASS (type) = 1;
7676
e142c38c 7677 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7678 if (attr)
7679 {
7680 TYPE_LENGTH (type) = DW_UNSND (attr);
7681 }
7682 else
7683 {
7684 TYPE_LENGTH (type) = 0;
7685 }
7686
876cecd0 7687 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 7688 if (die_is_declaration (die, cu))
876cecd0 7689 TYPE_STUB (type) = 1;
a6c727b2
DJ
7690 else if (attr == NULL && die->child == NULL
7691 && producer_is_realview (cu->producer))
7692 /* RealView does not output the required DW_AT_declaration
7693 on incomplete types. */
7694 TYPE_STUB (type) = 1;
dc718098 7695
c906108c
SS
7696 /* We need to add the type field to the die immediately so we don't
7697 infinitely recurse when dealing with pointers to the structure
0963b4bd 7698 type within the structure itself. */
1c379e20 7699 set_die_type (die, type, cu);
c906108c 7700
7e314c57
JK
7701 /* set_die_type should be already done. */
7702 set_descriptive_type (type, die, cu);
7703
c767944b
DJ
7704 return type;
7705}
7706
7707/* Finish creating a structure or union type, including filling in
7708 its members and creating a symbol for it. */
7709
7710static void
7711process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7712{
7713 struct objfile *objfile = cu->objfile;
7714 struct die_info *child_die = die->child;
7715 struct type *type;
7716
7717 type = get_die_type (die, cu);
7718 if (type == NULL)
7719 type = read_structure_type (die, cu);
7720
e142c38c 7721 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
7722 {
7723 struct field_info fi;
7724 struct die_info *child_die;
34eaf542 7725 VEC (symbolp) *template_args = NULL;
c767944b 7726 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
7727
7728 memset (&fi, 0, sizeof (struct field_info));
7729
639d11d3 7730 child_die = die->child;
c906108c
SS
7731
7732 while (child_die && child_die->tag)
7733 {
a9a9bd0f
DC
7734 if (child_die->tag == DW_TAG_member
7735 || child_die->tag == DW_TAG_variable)
c906108c 7736 {
a9a9bd0f
DC
7737 /* NOTE: carlton/2002-11-05: A C++ static data member
7738 should be a DW_TAG_member that is a declaration, but
7739 all versions of G++ as of this writing (so through at
7740 least 3.2.1) incorrectly generate DW_TAG_variable
7741 tags for them instead. */
e7c27a73 7742 dwarf2_add_field (&fi, child_die, cu);
c906108c 7743 }
8713b1b1 7744 else if (child_die->tag == DW_TAG_subprogram)
c906108c 7745 {
0963b4bd 7746 /* C++ member function. */
e7c27a73 7747 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
7748 }
7749 else if (child_die->tag == DW_TAG_inheritance)
7750 {
7751 /* C++ base class field. */
e7c27a73 7752 dwarf2_add_field (&fi, child_die, cu);
c906108c 7753 }
98751a41
JK
7754 else if (child_die->tag == DW_TAG_typedef)
7755 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
7756 else if (child_die->tag == DW_TAG_template_type_param
7757 || child_die->tag == DW_TAG_template_value_param)
7758 {
7759 struct symbol *arg = new_symbol (child_die, NULL, cu);
7760
f1078f66
DJ
7761 if (arg != NULL)
7762 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7763 }
7764
c906108c
SS
7765 child_die = sibling_die (child_die);
7766 }
7767
34eaf542
TT
7768 /* Attach template arguments to type. */
7769 if (! VEC_empty (symbolp, template_args))
7770 {
7771 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7772 TYPE_N_TEMPLATE_ARGUMENTS (type)
7773 = VEC_length (symbolp, template_args);
7774 TYPE_TEMPLATE_ARGUMENTS (type)
7775 = obstack_alloc (&objfile->objfile_obstack,
7776 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7777 * sizeof (struct symbol *)));
7778 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7779 VEC_address (symbolp, template_args),
7780 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7781 * sizeof (struct symbol *)));
7782 VEC_free (symbolp, template_args);
7783 }
7784
c906108c
SS
7785 /* Attach fields and member functions to the type. */
7786 if (fi.nfields)
e7c27a73 7787 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
7788 if (fi.nfnfields)
7789 {
e7c27a73 7790 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 7791
c5aa993b 7792 /* Get the type which refers to the base class (possibly this
c906108c 7793 class itself) which contains the vtable pointer for the current
0d564a31
DJ
7794 class from the DW_AT_containing_type attribute. This use of
7795 DW_AT_containing_type is a GNU extension. */
c906108c 7796
e142c38c 7797 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 7798 {
e7c27a73 7799 struct type *t = die_containing_type (die, cu);
c906108c
SS
7800
7801 TYPE_VPTR_BASETYPE (type) = t;
7802 if (type == t)
7803 {
c906108c
SS
7804 int i;
7805
7806 /* Our own class provides vtbl ptr. */
7807 for (i = TYPE_NFIELDS (t) - 1;
7808 i >= TYPE_N_BASECLASSES (t);
7809 --i)
7810 {
7811 char *fieldname = TYPE_FIELD_NAME (t, i);
7812
1168df01 7813 if (is_vtable_name (fieldname, cu))
c906108c
SS
7814 {
7815 TYPE_VPTR_FIELDNO (type) = i;
7816 break;
7817 }
7818 }
7819
7820 /* Complain if virtual function table field not found. */
7821 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 7822 complaint (&symfile_complaints,
3e43a32a
MS
7823 _("virtual function table pointer "
7824 "not found when defining class '%s'"),
4d3c2250
KB
7825 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7826 "");
c906108c
SS
7827 }
7828 else
7829 {
7830 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7831 }
7832 }
f6235d4c
EZ
7833 else if (cu->producer
7834 && strncmp (cu->producer,
7835 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7836 {
7837 /* The IBM XLC compiler does not provide direct indication
7838 of the containing type, but the vtable pointer is
7839 always named __vfp. */
7840
7841 int i;
7842
7843 for (i = TYPE_NFIELDS (type) - 1;
7844 i >= TYPE_N_BASECLASSES (type);
7845 --i)
7846 {
7847 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7848 {
7849 TYPE_VPTR_FIELDNO (type) = i;
7850 TYPE_VPTR_BASETYPE (type) = type;
7851 break;
7852 }
7853 }
7854 }
c906108c 7855 }
98751a41
JK
7856
7857 /* Copy fi.typedef_field_list linked list elements content into the
7858 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7859 if (fi.typedef_field_list)
7860 {
7861 int i = fi.typedef_field_list_count;
7862
a0d7a4ff 7863 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
7864 TYPE_TYPEDEF_FIELD_ARRAY (type)
7865 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7866 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7867
7868 /* Reverse the list order to keep the debug info elements order. */
7869 while (--i >= 0)
7870 {
7871 struct typedef_field *dest, *src;
6e70227d 7872
98751a41
JK
7873 dest = &TYPE_TYPEDEF_FIELD (type, i);
7874 src = &fi.typedef_field_list->field;
7875 fi.typedef_field_list = fi.typedef_field_list->next;
7876 *dest = *src;
7877 }
7878 }
c767944b
DJ
7879
7880 do_cleanups (back_to);
eb2a6f42
TT
7881
7882 if (HAVE_CPLUS_STRUCT (type))
7883 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 7884 }
63d06c5c 7885
0b92b5bb
TT
7886 quirk_gcc_member_function_pointer (type, cu->objfile);
7887
90aeadfc
DC
7888 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7889 snapshots) has been known to create a die giving a declaration
7890 for a class that has, as a child, a die giving a definition for a
7891 nested class. So we have to process our children even if the
7892 current die is a declaration. Normally, of course, a declaration
7893 won't have any children at all. */
134d01f1 7894
90aeadfc
DC
7895 while (child_die != NULL && child_die->tag)
7896 {
7897 if (child_die->tag == DW_TAG_member
7898 || child_die->tag == DW_TAG_variable
34eaf542
TT
7899 || child_die->tag == DW_TAG_inheritance
7900 || child_die->tag == DW_TAG_template_value_param
7901 || child_die->tag == DW_TAG_template_type_param)
134d01f1 7902 {
90aeadfc 7903 /* Do nothing. */
134d01f1 7904 }
90aeadfc
DC
7905 else
7906 process_die (child_die, cu);
134d01f1 7907
90aeadfc 7908 child_die = sibling_die (child_die);
134d01f1
DJ
7909 }
7910
fa4028e9
JB
7911 /* Do not consider external references. According to the DWARF standard,
7912 these DIEs are identified by the fact that they have no byte_size
7913 attribute, and a declaration attribute. */
7914 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7915 || !die_is_declaration (die, cu))
c767944b 7916 new_symbol (die, type, cu);
134d01f1
DJ
7917}
7918
7919/* Given a DW_AT_enumeration_type die, set its type. We do not
7920 complete the type's fields yet, or create any symbols. */
c906108c 7921
f792889a 7922static struct type *
134d01f1 7923read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7924{
e7c27a73 7925 struct objfile *objfile = cu->objfile;
c906108c 7926 struct type *type;
c906108c 7927 struct attribute *attr;
0114d602 7928 const char *name;
134d01f1 7929
348e048f
DE
7930 /* If the definition of this type lives in .debug_types, read that type.
7931 Don't follow DW_AT_specification though, that will take us back up
7932 the chain and we want to go down. */
7933 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7934 if (attr)
7935 {
7936 struct dwarf2_cu *type_cu = cu;
7937 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 7938
348e048f 7939 type = read_type_die (type_die, type_cu);
9dc481d3
DE
7940
7941 /* TYPE_CU may not be the same as CU.
7942 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
7943 return set_die_type (die, type, cu);
7944 }
7945
c906108c
SS
7946 type = alloc_type (objfile);
7947
7948 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 7949 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 7950 if (name != NULL)
0114d602 7951 TYPE_TAG_NAME (type) = (char *) name;
c906108c 7952
e142c38c 7953 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7954 if (attr)
7955 {
7956 TYPE_LENGTH (type) = DW_UNSND (attr);
7957 }
7958 else
7959 {
7960 TYPE_LENGTH (type) = 0;
7961 }
7962
137033e9
JB
7963 /* The enumeration DIE can be incomplete. In Ada, any type can be
7964 declared as private in the package spec, and then defined only
7965 inside the package body. Such types are known as Taft Amendment
7966 Types. When another package uses such a type, an incomplete DIE
7967 may be generated by the compiler. */
02eb380e 7968 if (die_is_declaration (die, cu))
876cecd0 7969 TYPE_STUB (type) = 1;
02eb380e 7970
f792889a 7971 return set_die_type (die, type, cu);
134d01f1
DJ
7972}
7973
7974/* Given a pointer to a die which begins an enumeration, process all
7975 the dies that define the members of the enumeration, and create the
7976 symbol for the enumeration type.
7977
7978 NOTE: We reverse the order of the element list. */
7979
7980static void
7981process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7982{
f792889a 7983 struct type *this_type;
134d01f1 7984
f792889a
DJ
7985 this_type = get_die_type (die, cu);
7986 if (this_type == NULL)
7987 this_type = read_enumeration_type (die, cu);
9dc481d3 7988
639d11d3 7989 if (die->child != NULL)
c906108c 7990 {
9dc481d3
DE
7991 struct die_info *child_die;
7992 struct symbol *sym;
7993 struct field *fields = NULL;
7994 int num_fields = 0;
7995 int unsigned_enum = 1;
7996 char *name;
7997
639d11d3 7998 child_die = die->child;
c906108c
SS
7999 while (child_die && child_die->tag)
8000 {
8001 if (child_die->tag != DW_TAG_enumerator)
8002 {
e7c27a73 8003 process_die (child_die, cu);
c906108c
SS
8004 }
8005 else
8006 {
39cbfefa
DJ
8007 name = dwarf2_name (child_die, cu);
8008 if (name)
c906108c 8009 {
f792889a 8010 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
8011 if (SYMBOL_VALUE (sym) < 0)
8012 unsigned_enum = 0;
8013
8014 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8015 {
8016 fields = (struct field *)
8017 xrealloc (fields,
8018 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8019 * sizeof (struct field));
c906108c
SS
8020 }
8021
3567439c 8022 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 8023 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 8024 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
8025 FIELD_BITSIZE (fields[num_fields]) = 0;
8026
8027 num_fields++;
8028 }
8029 }
8030
8031 child_die = sibling_die (child_die);
8032 }
8033
8034 if (num_fields)
8035 {
f792889a
DJ
8036 TYPE_NFIELDS (this_type) = num_fields;
8037 TYPE_FIELDS (this_type) = (struct field *)
8038 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8039 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 8040 sizeof (struct field) * num_fields);
b8c9b27d 8041 xfree (fields);
c906108c
SS
8042 }
8043 if (unsigned_enum)
876cecd0 8044 TYPE_UNSIGNED (this_type) = 1;
c906108c 8045 }
134d01f1 8046
6c83ed52
TT
8047 /* If we are reading an enum from a .debug_types unit, and the enum
8048 is a declaration, and the enum is not the signatured type in the
8049 unit, then we do not want to add a symbol for it. Adding a
8050 symbol would in some cases obscure the true definition of the
8051 enum, giving users an incomplete type when the definition is
8052 actually available. Note that we do not want to do this for all
8053 enums which are just declarations, because C++0x allows forward
8054 enum declarations. */
8055 if (cu->per_cu->debug_type_section
8056 && die_is_declaration (die, cu))
8057 {
8058 struct signatured_type *type_sig;
8059
8060 type_sig
8061 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8062 cu->per_cu->debug_type_section,
8063 cu->per_cu->offset);
8064 if (type_sig->type_offset != die->offset)
8065 return;
8066 }
8067
f792889a 8068 new_symbol (die, this_type, cu);
c906108c
SS
8069}
8070
8071/* Extract all information from a DW_TAG_array_type DIE and put it in
8072 the DIE's type field. For now, this only handles one dimensional
8073 arrays. */
8074
f792889a 8075static struct type *
e7c27a73 8076read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8077{
e7c27a73 8078 struct objfile *objfile = cu->objfile;
c906108c 8079 struct die_info *child_die;
7e314c57 8080 struct type *type;
c906108c
SS
8081 struct type *element_type, *range_type, *index_type;
8082 struct type **range_types = NULL;
8083 struct attribute *attr;
8084 int ndim = 0;
8085 struct cleanup *back_to;
39cbfefa 8086 char *name;
c906108c 8087
e7c27a73 8088 element_type = die_type (die, cu);
c906108c 8089
7e314c57
JK
8090 /* The die_type call above may have already set the type for this DIE. */
8091 type = get_die_type (die, cu);
8092 if (type)
8093 return type;
8094
c906108c
SS
8095 /* Irix 6.2 native cc creates array types without children for
8096 arrays with unspecified length. */
639d11d3 8097 if (die->child == NULL)
c906108c 8098 {
46bf5051 8099 index_type = objfile_type (objfile)->builtin_int;
c906108c 8100 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
8101 type = create_array_type (NULL, element_type, range_type);
8102 return set_die_type (die, type, cu);
c906108c
SS
8103 }
8104
8105 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 8106 child_die = die->child;
c906108c
SS
8107 while (child_die && child_die->tag)
8108 {
8109 if (child_die->tag == DW_TAG_subrange_type)
8110 {
f792889a 8111 struct type *child_type = read_type_die (child_die, cu);
9a619af0 8112
f792889a 8113 if (child_type != NULL)
a02abb62 8114 {
0963b4bd
MS
8115 /* The range type was succesfully read. Save it for the
8116 array type creation. */
a02abb62
JB
8117 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8118 {
8119 range_types = (struct type **)
8120 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8121 * sizeof (struct type *));
8122 if (ndim == 0)
8123 make_cleanup (free_current_contents, &range_types);
8124 }
f792889a 8125 range_types[ndim++] = child_type;
a02abb62 8126 }
c906108c
SS
8127 }
8128 child_die = sibling_die (child_die);
8129 }
8130
8131 /* Dwarf2 dimensions are output from left to right, create the
8132 necessary array types in backwards order. */
7ca2d3a3 8133
c906108c 8134 type = element_type;
7ca2d3a3
DL
8135
8136 if (read_array_order (die, cu) == DW_ORD_col_major)
8137 {
8138 int i = 0;
9a619af0 8139
7ca2d3a3
DL
8140 while (i < ndim)
8141 type = create_array_type (NULL, type, range_types[i++]);
8142 }
8143 else
8144 {
8145 while (ndim-- > 0)
8146 type = create_array_type (NULL, type, range_types[ndim]);
8147 }
c906108c 8148
f5f8a009
EZ
8149 /* Understand Dwarf2 support for vector types (like they occur on
8150 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8151 array type. This is not part of the Dwarf2/3 standard yet, but a
8152 custom vendor extension. The main difference between a regular
8153 array and the vector variant is that vectors are passed by value
8154 to functions. */
e142c38c 8155 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 8156 if (attr)
ea37ba09 8157 make_vector_type (type);
f5f8a009 8158
dbc98a8b
KW
8159 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8160 implementation may choose to implement triple vectors using this
8161 attribute. */
8162 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8163 if (attr)
8164 {
8165 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8166 TYPE_LENGTH (type) = DW_UNSND (attr);
8167 else
3e43a32a
MS
8168 complaint (&symfile_complaints,
8169 _("DW_AT_byte_size for array type smaller "
8170 "than the total size of elements"));
dbc98a8b
KW
8171 }
8172
39cbfefa
DJ
8173 name = dwarf2_name (die, cu);
8174 if (name)
8175 TYPE_NAME (type) = name;
6e70227d 8176
0963b4bd 8177 /* Install the type in the die. */
7e314c57
JK
8178 set_die_type (die, type, cu);
8179
8180 /* set_die_type should be already done. */
b4ba55a1
JB
8181 set_descriptive_type (type, die, cu);
8182
c906108c
SS
8183 do_cleanups (back_to);
8184
7e314c57 8185 return type;
c906108c
SS
8186}
8187
7ca2d3a3 8188static enum dwarf_array_dim_ordering
6e70227d 8189read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
8190{
8191 struct attribute *attr;
8192
8193 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8194
8195 if (attr) return DW_SND (attr);
8196
0963b4bd
MS
8197 /* GNU F77 is a special case, as at 08/2004 array type info is the
8198 opposite order to the dwarf2 specification, but data is still
8199 laid out as per normal fortran.
7ca2d3a3 8200
0963b4bd
MS
8201 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8202 version checking. */
7ca2d3a3 8203
905e0470
PM
8204 if (cu->language == language_fortran
8205 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
8206 {
8207 return DW_ORD_row_major;
8208 }
8209
6e70227d 8210 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
8211 {
8212 case array_column_major:
8213 return DW_ORD_col_major;
8214 case array_row_major:
8215 default:
8216 return DW_ORD_row_major;
8217 };
8218}
8219
72019c9c 8220/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 8221 the DIE's type field. */
72019c9c 8222
f792889a 8223static struct type *
72019c9c
GM
8224read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8225{
7e314c57
JK
8226 struct type *domain_type, *set_type;
8227 struct attribute *attr;
f792889a 8228
7e314c57
JK
8229 domain_type = die_type (die, cu);
8230
8231 /* The die_type call above may have already set the type for this DIE. */
8232 set_type = get_die_type (die, cu);
8233 if (set_type)
8234 return set_type;
8235
8236 set_type = create_set_type (NULL, domain_type);
8237
8238 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
8239 if (attr)
8240 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 8241
f792889a 8242 return set_die_type (die, set_type, cu);
72019c9c 8243}
7ca2d3a3 8244
c906108c
SS
8245/* First cut: install each common block member as a global variable. */
8246
8247static void
e7c27a73 8248read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8249{
8250 struct die_info *child_die;
8251 struct attribute *attr;
8252 struct symbol *sym;
8253 CORE_ADDR base = (CORE_ADDR) 0;
8254
e142c38c 8255 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
8256 if (attr)
8257 {
0963b4bd 8258 /* Support the .debug_loc offsets. */
8e19ed76
PS
8259 if (attr_form_is_block (attr))
8260 {
e7c27a73 8261 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 8262 }
3690dd37 8263 else if (attr_form_is_section_offset (attr))
8e19ed76 8264 {
4d3c2250 8265 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8266 }
8267 else
8268 {
4d3c2250
KB
8269 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8270 "common block member");
8e19ed76 8271 }
c906108c 8272 }
639d11d3 8273 if (die->child != NULL)
c906108c 8274 {
639d11d3 8275 child_die = die->child;
c906108c
SS
8276 while (child_die && child_die->tag)
8277 {
74ac6d43
TT
8278 LONGEST offset;
8279
e7c27a73 8280 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
8281 if (sym != NULL
8282 && handle_data_member_location (child_die, cu, &offset))
c906108c 8283 {
74ac6d43 8284 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
8285 add_symbol_to_list (sym, &global_symbols);
8286 }
8287 child_die = sibling_die (child_die);
8288 }
8289 }
8290}
8291
0114d602 8292/* Create a type for a C++ namespace. */
d9fa45fe 8293
0114d602
DJ
8294static struct type *
8295read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 8296{
e7c27a73 8297 struct objfile *objfile = cu->objfile;
0114d602 8298 const char *previous_prefix, *name;
9219021c 8299 int is_anonymous;
0114d602
DJ
8300 struct type *type;
8301
8302 /* For extensions, reuse the type of the original namespace. */
8303 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8304 {
8305 struct die_info *ext_die;
8306 struct dwarf2_cu *ext_cu = cu;
9a619af0 8307
0114d602
DJ
8308 ext_die = dwarf2_extension (die, &ext_cu);
8309 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
8310
8311 /* EXT_CU may not be the same as CU.
8312 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
8313 return set_die_type (die, type, cu);
8314 }
9219021c 8315
e142c38c 8316 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
8317
8318 /* Now build the name of the current namespace. */
8319
0114d602
DJ
8320 previous_prefix = determine_prefix (die, cu);
8321 if (previous_prefix[0] != '\0')
8322 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 8323 previous_prefix, name, 0, cu);
0114d602
DJ
8324
8325 /* Create the type. */
8326 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8327 objfile);
8328 TYPE_NAME (type) = (char *) name;
8329 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8330
60531b24 8331 return set_die_type (die, type, cu);
0114d602
DJ
8332}
8333
8334/* Read a C++ namespace. */
8335
8336static void
8337read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8338{
8339 struct objfile *objfile = cu->objfile;
0114d602 8340 int is_anonymous;
9219021c 8341
5c4e30ca
DC
8342 /* Add a symbol associated to this if we haven't seen the namespace
8343 before. Also, add a using directive if it's an anonymous
8344 namespace. */
9219021c 8345
f2f0e013 8346 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
8347 {
8348 struct type *type;
8349
0114d602 8350 type = read_type_die (die, cu);
e7c27a73 8351 new_symbol (die, type, cu);
5c4e30ca 8352
e8e80198 8353 namespace_name (die, &is_anonymous, cu);
5c4e30ca 8354 if (is_anonymous)
0114d602
DJ
8355 {
8356 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 8357
c0cc3a76 8358 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 8359 NULL, NULL, &objfile->objfile_obstack);
0114d602 8360 }
5c4e30ca 8361 }
9219021c 8362
639d11d3 8363 if (die->child != NULL)
d9fa45fe 8364 {
639d11d3 8365 struct die_info *child_die = die->child;
6e70227d 8366
d9fa45fe
DC
8367 while (child_die && child_die->tag)
8368 {
e7c27a73 8369 process_die (child_die, cu);
d9fa45fe
DC
8370 child_die = sibling_die (child_die);
8371 }
8372 }
38d518c9
EZ
8373}
8374
f55ee35c
JK
8375/* Read a Fortran module as type. This DIE can be only a declaration used for
8376 imported module. Still we need that type as local Fortran "use ... only"
8377 declaration imports depend on the created type in determine_prefix. */
8378
8379static struct type *
8380read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8381{
8382 struct objfile *objfile = cu->objfile;
8383 char *module_name;
8384 struct type *type;
8385
8386 module_name = dwarf2_name (die, cu);
8387 if (!module_name)
3e43a32a
MS
8388 complaint (&symfile_complaints,
8389 _("DW_TAG_module has no name, offset 0x%x"),
f55ee35c
JK
8390 die->offset);
8391 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8392
8393 /* determine_prefix uses TYPE_TAG_NAME. */
8394 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8395
8396 return set_die_type (die, type, cu);
8397}
8398
5d7cb8df
JK
8399/* Read a Fortran module. */
8400
8401static void
8402read_module (struct die_info *die, struct dwarf2_cu *cu)
8403{
8404 struct die_info *child_die = die->child;
8405
5d7cb8df
JK
8406 while (child_die && child_die->tag)
8407 {
8408 process_die (child_die, cu);
8409 child_die = sibling_die (child_die);
8410 }
8411}
8412
38d518c9
EZ
8413/* Return the name of the namespace represented by DIE. Set
8414 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8415 namespace. */
8416
8417static const char *
e142c38c 8418namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
8419{
8420 struct die_info *current_die;
8421 const char *name = NULL;
8422
8423 /* Loop through the extensions until we find a name. */
8424
8425 for (current_die = die;
8426 current_die != NULL;
f2f0e013 8427 current_die = dwarf2_extension (die, &cu))
38d518c9 8428 {
e142c38c 8429 name = dwarf2_name (current_die, cu);
38d518c9
EZ
8430 if (name != NULL)
8431 break;
8432 }
8433
8434 /* Is it an anonymous namespace? */
8435
8436 *is_anonymous = (name == NULL);
8437 if (*is_anonymous)
2b1dbab0 8438 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
8439
8440 return name;
d9fa45fe
DC
8441}
8442
c906108c
SS
8443/* Extract all information from a DW_TAG_pointer_type DIE and add to
8444 the user defined type vector. */
8445
f792889a 8446static struct type *
e7c27a73 8447read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8448{
5e2b427d 8449 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 8450 struct comp_unit_head *cu_header = &cu->header;
c906108c 8451 struct type *type;
8b2dbe47
KB
8452 struct attribute *attr_byte_size;
8453 struct attribute *attr_address_class;
8454 int byte_size, addr_class;
7e314c57
JK
8455 struct type *target_type;
8456
8457 target_type = die_type (die, cu);
c906108c 8458
7e314c57
JK
8459 /* The die_type call above may have already set the type for this DIE. */
8460 type = get_die_type (die, cu);
8461 if (type)
8462 return type;
8463
8464 type = lookup_pointer_type (target_type);
8b2dbe47 8465
e142c38c 8466 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
8467 if (attr_byte_size)
8468 byte_size = DW_UNSND (attr_byte_size);
c906108c 8469 else
8b2dbe47
KB
8470 byte_size = cu_header->addr_size;
8471
e142c38c 8472 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
8473 if (attr_address_class)
8474 addr_class = DW_UNSND (attr_address_class);
8475 else
8476 addr_class = DW_ADDR_none;
8477
8478 /* If the pointer size or address class is different than the
8479 default, create a type variant marked as such and set the
8480 length accordingly. */
8481 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 8482 {
5e2b427d 8483 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
8484 {
8485 int type_flags;
8486
849957d9 8487 type_flags = gdbarch_address_class_type_flags
5e2b427d 8488 (gdbarch, byte_size, addr_class);
876cecd0
TT
8489 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8490 == 0);
8b2dbe47
KB
8491 type = make_type_with_address_space (type, type_flags);
8492 }
8493 else if (TYPE_LENGTH (type) != byte_size)
8494 {
3e43a32a
MS
8495 complaint (&symfile_complaints,
8496 _("invalid pointer size %d"), byte_size);
8b2dbe47 8497 }
6e70227d 8498 else
9a619af0
MS
8499 {
8500 /* Should we also complain about unhandled address classes? */
8501 }
c906108c 8502 }
8b2dbe47
KB
8503
8504 TYPE_LENGTH (type) = byte_size;
f792889a 8505 return set_die_type (die, type, cu);
c906108c
SS
8506}
8507
8508/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8509 the user defined type vector. */
8510
f792889a 8511static struct type *
e7c27a73 8512read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8513{
8514 struct type *type;
8515 struct type *to_type;
8516 struct type *domain;
8517
e7c27a73
DJ
8518 to_type = die_type (die, cu);
8519 domain = die_containing_type (die, cu);
0d5de010 8520
7e314c57
JK
8521 /* The calls above may have already set the type for this DIE. */
8522 type = get_die_type (die, cu);
8523 if (type)
8524 return type;
8525
0d5de010
DJ
8526 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8527 type = lookup_methodptr_type (to_type);
8528 else
8529 type = lookup_memberptr_type (to_type, domain);
c906108c 8530
f792889a 8531 return set_die_type (die, type, cu);
c906108c
SS
8532}
8533
8534/* Extract all information from a DW_TAG_reference_type DIE and add to
8535 the user defined type vector. */
8536
f792889a 8537static struct type *
e7c27a73 8538read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8539{
e7c27a73 8540 struct comp_unit_head *cu_header = &cu->header;
7e314c57 8541 struct type *type, *target_type;
c906108c
SS
8542 struct attribute *attr;
8543
7e314c57
JK
8544 target_type = die_type (die, cu);
8545
8546 /* The die_type call above may have already set the type for this DIE. */
8547 type = get_die_type (die, cu);
8548 if (type)
8549 return type;
8550
8551 type = lookup_reference_type (target_type);
e142c38c 8552 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8553 if (attr)
8554 {
8555 TYPE_LENGTH (type) = DW_UNSND (attr);
8556 }
8557 else
8558 {
107d2387 8559 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 8560 }
f792889a 8561 return set_die_type (die, type, cu);
c906108c
SS
8562}
8563
f792889a 8564static struct type *
e7c27a73 8565read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8566{
f792889a 8567 struct type *base_type, *cv_type;
c906108c 8568
e7c27a73 8569 base_type = die_type (die, cu);
7e314c57
JK
8570
8571 /* The die_type call above may have already set the type for this DIE. */
8572 cv_type = get_die_type (die, cu);
8573 if (cv_type)
8574 return cv_type;
8575
2f608a3a
KW
8576 /* In case the const qualifier is applied to an array type, the element type
8577 is so qualified, not the array type (section 6.7.3 of C99). */
8578 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8579 {
8580 struct type *el_type, *inner_array;
8581
8582 base_type = copy_type (base_type);
8583 inner_array = base_type;
8584
8585 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8586 {
8587 TYPE_TARGET_TYPE (inner_array) =
8588 copy_type (TYPE_TARGET_TYPE (inner_array));
8589 inner_array = TYPE_TARGET_TYPE (inner_array);
8590 }
8591
8592 el_type = TYPE_TARGET_TYPE (inner_array);
8593 TYPE_TARGET_TYPE (inner_array) =
8594 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8595
8596 return set_die_type (die, base_type, cu);
8597 }
8598
f792889a
DJ
8599 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8600 return set_die_type (die, cv_type, cu);
c906108c
SS
8601}
8602
f792889a 8603static struct type *
e7c27a73 8604read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8605{
f792889a 8606 struct type *base_type, *cv_type;
c906108c 8607
e7c27a73 8608 base_type = die_type (die, cu);
7e314c57
JK
8609
8610 /* The die_type call above may have already set the type for this DIE. */
8611 cv_type = get_die_type (die, cu);
8612 if (cv_type)
8613 return cv_type;
8614
f792889a
DJ
8615 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8616 return set_die_type (die, cv_type, cu);
c906108c
SS
8617}
8618
8619/* Extract all information from a DW_TAG_string_type DIE and add to
8620 the user defined type vector. It isn't really a user defined type,
8621 but it behaves like one, with other DIE's using an AT_user_def_type
8622 attribute to reference it. */
8623
f792889a 8624static struct type *
e7c27a73 8625read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8626{
e7c27a73 8627 struct objfile *objfile = cu->objfile;
3b7538c0 8628 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8629 struct type *type, *range_type, *index_type, *char_type;
8630 struct attribute *attr;
8631 unsigned int length;
8632
e142c38c 8633 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
8634 if (attr)
8635 {
8636 length = DW_UNSND (attr);
8637 }
8638 else
8639 {
0963b4bd 8640 /* Check for the DW_AT_byte_size attribute. */
e142c38c 8641 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
8642 if (attr)
8643 {
8644 length = DW_UNSND (attr);
8645 }
8646 else
8647 {
8648 length = 1;
8649 }
c906108c 8650 }
6ccb9162 8651
46bf5051 8652 index_type = objfile_type (objfile)->builtin_int;
c906108c 8653 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
8654 char_type = language_string_char_type (cu->language_defn, gdbarch);
8655 type = create_string_type (NULL, char_type, range_type);
6ccb9162 8656
f792889a 8657 return set_die_type (die, type, cu);
c906108c
SS
8658}
8659
8660/* Handle DIES due to C code like:
8661
8662 struct foo
c5aa993b
JM
8663 {
8664 int (*funcp)(int a, long l);
8665 int b;
8666 };
c906108c 8667
0963b4bd 8668 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 8669
f792889a 8670static struct type *
e7c27a73 8671read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8672{
0963b4bd
MS
8673 struct type *type; /* Type that this function returns. */
8674 struct type *ftype; /* Function that returns above type. */
c906108c
SS
8675 struct attribute *attr;
8676
e7c27a73 8677 type = die_type (die, cu);
7e314c57
JK
8678
8679 /* The die_type call above may have already set the type for this DIE. */
8680 ftype = get_die_type (die, cu);
8681 if (ftype)
8682 return ftype;
8683
0c8b41f1 8684 ftype = lookup_function_type (type);
c906108c 8685
5b8101ae 8686 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 8687 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 8688 if ((attr && (DW_UNSND (attr) != 0))
987504bb 8689 || cu->language == language_cplus
5b8101ae
PM
8690 || cu->language == language_java
8691 || cu->language == language_pascal)
876cecd0 8692 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
8693 else if (producer_is_realview (cu->producer))
8694 /* RealView does not emit DW_AT_prototyped. We can not
8695 distinguish prototyped and unprototyped functions; default to
8696 prototyped, since that is more common in modern code (and
8697 RealView warns about unprototyped functions). */
8698 TYPE_PROTOTYPED (ftype) = 1;
c906108c 8699
c055b101
CV
8700 /* Store the calling convention in the type if it's available in
8701 the subroutine die. Otherwise set the calling convention to
8702 the default value DW_CC_normal. */
8703 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
8704 if (attr)
8705 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8706 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8707 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8708 else
8709 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
8710
8711 /* We need to add the subroutine type to the die immediately so
8712 we don't infinitely recurse when dealing with parameters
0963b4bd 8713 declared as the same subroutine type. */
76c10ea2 8714 set_die_type (die, ftype, cu);
6e70227d 8715
639d11d3 8716 if (die->child != NULL)
c906108c 8717 {
8072405b 8718 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 8719 struct die_info *child_die;
8072405b 8720 int nparams, iparams;
c906108c
SS
8721
8722 /* Count the number of parameters.
8723 FIXME: GDB currently ignores vararg functions, but knows about
8724 vararg member functions. */
8072405b 8725 nparams = 0;
639d11d3 8726 child_die = die->child;
c906108c
SS
8727 while (child_die && child_die->tag)
8728 {
8729 if (child_die->tag == DW_TAG_formal_parameter)
8730 nparams++;
8731 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 8732 TYPE_VARARGS (ftype) = 1;
c906108c
SS
8733 child_die = sibling_die (child_die);
8734 }
8735
8736 /* Allocate storage for parameters and fill them in. */
8737 TYPE_NFIELDS (ftype) = nparams;
8738 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 8739 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 8740
8072405b
JK
8741 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8742 even if we error out during the parameters reading below. */
8743 for (iparams = 0; iparams < nparams; iparams++)
8744 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8745
8746 iparams = 0;
639d11d3 8747 child_die = die->child;
c906108c
SS
8748 while (child_die && child_die->tag)
8749 {
8750 if (child_die->tag == DW_TAG_formal_parameter)
8751 {
3ce3b1ba
PA
8752 struct type *arg_type;
8753
8754 /* DWARF version 2 has no clean way to discern C++
8755 static and non-static member functions. G++ helps
8756 GDB by marking the first parameter for non-static
8757 member functions (which is the this pointer) as
8758 artificial. We pass this information to
8759 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8760
8761 DWARF version 3 added DW_AT_object_pointer, which GCC
8762 4.5 does not yet generate. */
e142c38c 8763 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
8764 if (attr)
8765 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8766 else
418835cc
KS
8767 {
8768 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8769
8770 /* GCC/43521: In java, the formal parameter
8771 "this" is sometimes not marked with DW_AT_artificial. */
8772 if (cu->language == language_java)
8773 {
8774 const char *name = dwarf2_name (child_die, cu);
9a619af0 8775
418835cc
KS
8776 if (name && !strcmp (name, "this"))
8777 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8778 }
8779 }
3ce3b1ba
PA
8780 arg_type = die_type (child_die, cu);
8781
8782 /* RealView does not mark THIS as const, which the testsuite
8783 expects. GCC marks THIS as const in method definitions,
8784 but not in the class specifications (GCC PR 43053). */
8785 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8786 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8787 {
8788 int is_this = 0;
8789 struct dwarf2_cu *arg_cu = cu;
8790 const char *name = dwarf2_name (child_die, cu);
8791
8792 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8793 if (attr)
8794 {
8795 /* If the compiler emits this, use it. */
8796 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8797 is_this = 1;
8798 }
8799 else if (name && strcmp (name, "this") == 0)
8800 /* Function definitions will have the argument names. */
8801 is_this = 1;
8802 else if (name == NULL && iparams == 0)
8803 /* Declarations may not have the names, so like
8804 elsewhere in GDB, assume an artificial first
8805 argument is "this". */
8806 is_this = 1;
8807
8808 if (is_this)
8809 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8810 arg_type, 0);
8811 }
8812
8813 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
8814 iparams++;
8815 }
8816 child_die = sibling_die (child_die);
8817 }
8818 }
8819
76c10ea2 8820 return ftype;
c906108c
SS
8821}
8822
f792889a 8823static struct type *
e7c27a73 8824read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8825{
e7c27a73 8826 struct objfile *objfile = cu->objfile;
0114d602 8827 const char *name = NULL;
f792889a 8828 struct type *this_type;
c906108c 8829
94af9270 8830 name = dwarf2_full_name (NULL, die, cu);
f792889a 8831 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
8832 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8833 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
8834 set_die_type (die, this_type, cu);
8835 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8836 return this_type;
c906108c
SS
8837}
8838
8839/* Find a representation of a given base type and install
8840 it in the TYPE field of the die. */
8841
f792889a 8842static struct type *
e7c27a73 8843read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8844{
e7c27a73 8845 struct objfile *objfile = cu->objfile;
c906108c
SS
8846 struct type *type;
8847 struct attribute *attr;
8848 int encoding = 0, size = 0;
39cbfefa 8849 char *name;
6ccb9162
UW
8850 enum type_code code = TYPE_CODE_INT;
8851 int type_flags = 0;
8852 struct type *target_type = NULL;
c906108c 8853
e142c38c 8854 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
8855 if (attr)
8856 {
8857 encoding = DW_UNSND (attr);
8858 }
e142c38c 8859 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8860 if (attr)
8861 {
8862 size = DW_UNSND (attr);
8863 }
39cbfefa 8864 name = dwarf2_name (die, cu);
6ccb9162 8865 if (!name)
c906108c 8866 {
6ccb9162
UW
8867 complaint (&symfile_complaints,
8868 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 8869 }
6ccb9162
UW
8870
8871 switch (encoding)
c906108c 8872 {
6ccb9162
UW
8873 case DW_ATE_address:
8874 /* Turn DW_ATE_address into a void * pointer. */
8875 code = TYPE_CODE_PTR;
8876 type_flags |= TYPE_FLAG_UNSIGNED;
8877 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8878 break;
8879 case DW_ATE_boolean:
8880 code = TYPE_CODE_BOOL;
8881 type_flags |= TYPE_FLAG_UNSIGNED;
8882 break;
8883 case DW_ATE_complex_float:
8884 code = TYPE_CODE_COMPLEX;
8885 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8886 break;
8887 case DW_ATE_decimal_float:
8888 code = TYPE_CODE_DECFLOAT;
8889 break;
8890 case DW_ATE_float:
8891 code = TYPE_CODE_FLT;
8892 break;
8893 case DW_ATE_signed:
8894 break;
8895 case DW_ATE_unsigned:
8896 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
8897 if (cu->language == language_fortran
8898 && name
8899 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8900 code = TYPE_CODE_CHAR;
6ccb9162
UW
8901 break;
8902 case DW_ATE_signed_char:
6e70227d 8903 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8904 || cu->language == language_pascal
8905 || cu->language == language_fortran)
6ccb9162
UW
8906 code = TYPE_CODE_CHAR;
8907 break;
8908 case DW_ATE_unsigned_char:
868a0084 8909 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
8910 || cu->language == language_pascal
8911 || cu->language == language_fortran)
6ccb9162
UW
8912 code = TYPE_CODE_CHAR;
8913 type_flags |= TYPE_FLAG_UNSIGNED;
8914 break;
75079b2b
TT
8915 case DW_ATE_UTF:
8916 /* We just treat this as an integer and then recognize the
8917 type by name elsewhere. */
8918 break;
8919
6ccb9162
UW
8920 default:
8921 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8922 dwarf_type_encoding_name (encoding));
8923 break;
c906108c 8924 }
6ccb9162 8925
0114d602
DJ
8926 type = init_type (code, size, type_flags, NULL, objfile);
8927 TYPE_NAME (type) = name;
6ccb9162
UW
8928 TYPE_TARGET_TYPE (type) = target_type;
8929
0114d602 8930 if (name && strcmp (name, "char") == 0)
876cecd0 8931 TYPE_NOSIGN (type) = 1;
0114d602 8932
f792889a 8933 return set_die_type (die, type, cu);
c906108c
SS
8934}
8935
a02abb62
JB
8936/* Read the given DW_AT_subrange DIE. */
8937
f792889a 8938static struct type *
a02abb62
JB
8939read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8940{
8941 struct type *base_type;
8942 struct type *range_type;
8943 struct attribute *attr;
43bbcdc2
PH
8944 LONGEST low = 0;
8945 LONGEST high = -1;
39cbfefa 8946 char *name;
43bbcdc2 8947 LONGEST negative_mask;
e77813c8 8948
a02abb62 8949 base_type = die_type (die, cu);
953ac07e
JK
8950 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8951 check_typedef (base_type);
a02abb62 8952
7e314c57
JK
8953 /* The die_type call above may have already set the type for this DIE. */
8954 range_type = get_die_type (die, cu);
8955 if (range_type)
8956 return range_type;
8957
e142c38c 8958 if (cu->language == language_fortran)
6e70227d 8959 {
a02abb62
JB
8960 /* FORTRAN implies a lower bound of 1, if not given. */
8961 low = 1;
8962 }
8963
dd5e6932
DJ
8964 /* FIXME: For variable sized arrays either of these could be
8965 a variable rather than a constant value. We'll allow it,
8966 but we don't know how to handle it. */
e142c38c 8967 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
8968 if (attr)
8969 low = dwarf2_get_attr_constant_value (attr, 0);
8970
e142c38c 8971 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 8972 if (attr)
6e70227d 8973 {
d48323d8 8974 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
8975 {
8976 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 8977 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
8978 FIXME: GDB does not yet know how to handle dynamic
8979 arrays properly, treat them as arrays with unspecified
8980 length for now.
8981
8982 FIXME: jimb/2003-09-22: GDB does not really know
8983 how to handle arrays of unspecified length
8984 either; we just represent them as zero-length
8985 arrays. Choose an appropriate upper bound given
8986 the lower bound we've computed above. */
8987 high = low - 1;
8988 }
8989 else
8990 high = dwarf2_get_attr_constant_value (attr, 1);
8991 }
e77813c8
PM
8992 else
8993 {
8994 attr = dwarf2_attr (die, DW_AT_count, cu);
8995 if (attr)
8996 {
8997 int count = dwarf2_get_attr_constant_value (attr, 1);
8998 high = low + count - 1;
8999 }
c2ff108b
JK
9000 else
9001 {
9002 /* Unspecified array length. */
9003 high = low - 1;
9004 }
e77813c8
PM
9005 }
9006
9007 /* Dwarf-2 specifications explicitly allows to create subrange types
9008 without specifying a base type.
9009 In that case, the base type must be set to the type of
9010 the lower bound, upper bound or count, in that order, if any of these
9011 three attributes references an object that has a type.
9012 If no base type is found, the Dwarf-2 specifications say that
9013 a signed integer type of size equal to the size of an address should
9014 be used.
9015 For the following C code: `extern char gdb_int [];'
9016 GCC produces an empty range DIE.
9017 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 9018 high bound or count are not yet handled by this code. */
e77813c8
PM
9019 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9020 {
9021 struct objfile *objfile = cu->objfile;
9022 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9023 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9024 struct type *int_type = objfile_type (objfile)->builtin_int;
9025
9026 /* Test "int", "long int", and "long long int" objfile types,
9027 and select the first one having a size above or equal to the
9028 architecture address size. */
9029 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9030 base_type = int_type;
9031 else
9032 {
9033 int_type = objfile_type (objfile)->builtin_long;
9034 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9035 base_type = int_type;
9036 else
9037 {
9038 int_type = objfile_type (objfile)->builtin_long_long;
9039 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9040 base_type = int_type;
9041 }
9042 }
9043 }
a02abb62 9044
6e70227d 9045 negative_mask =
43bbcdc2
PH
9046 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9047 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9048 low |= negative_mask;
9049 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9050 high |= negative_mask;
9051
a02abb62
JB
9052 range_type = create_range_type (NULL, base_type, low, high);
9053
bbb0eef6
JK
9054 /* Mark arrays with dynamic length at least as an array of unspecified
9055 length. GDB could check the boundary but before it gets implemented at
9056 least allow accessing the array elements. */
d48323d8 9057 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
9058 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9059
c2ff108b
JK
9060 /* Ada expects an empty array on no boundary attributes. */
9061 if (attr == NULL && cu->language != language_ada)
9062 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9063
39cbfefa
DJ
9064 name = dwarf2_name (die, cu);
9065 if (name)
9066 TYPE_NAME (range_type) = name;
6e70227d 9067
e142c38c 9068 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
9069 if (attr)
9070 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9071
7e314c57
JK
9072 set_die_type (die, range_type, cu);
9073
9074 /* set_die_type should be already done. */
b4ba55a1
JB
9075 set_descriptive_type (range_type, die, cu);
9076
7e314c57 9077 return range_type;
a02abb62 9078}
6e70227d 9079
f792889a 9080static struct type *
81a17f79
JB
9081read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9082{
9083 struct type *type;
81a17f79 9084
81a17f79
JB
9085 /* For now, we only support the C meaning of an unspecified type: void. */
9086
0114d602
DJ
9087 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9088 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 9089
f792889a 9090 return set_die_type (die, type, cu);
81a17f79 9091}
a02abb62 9092
51545339
DJ
9093/* Trivial hash function for die_info: the hash value of a DIE
9094 is its offset in .debug_info for this objfile. */
9095
9096static hashval_t
9097die_hash (const void *item)
9098{
9099 const struct die_info *die = item;
9a619af0 9100
51545339
DJ
9101 return die->offset;
9102}
9103
9104/* Trivial comparison function for die_info structures: two DIEs
9105 are equal if they have the same offset. */
9106
9107static int
9108die_eq (const void *item_lhs, const void *item_rhs)
9109{
9110 const struct die_info *die_lhs = item_lhs;
9111 const struct die_info *die_rhs = item_rhs;
9a619af0 9112
51545339
DJ
9113 return die_lhs->offset == die_rhs->offset;
9114}
9115
c906108c
SS
9116/* Read a whole compilation unit into a linked list of dies. */
9117
f9aca02d 9118static struct die_info *
93311388 9119read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 9120{
93311388 9121 struct die_reader_specs reader_specs;
98bfdba5 9122 int read_abbrevs = 0;
1d9ec526 9123 struct cleanup *back_to = NULL;
98bfdba5
PA
9124 struct die_info *die;
9125
9126 if (cu->dwarf2_abbrevs == NULL)
9127 {
9128 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
9129 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9130 read_abbrevs = 1;
9131 }
93311388 9132
348e048f 9133 gdb_assert (cu->die_hash == NULL);
51545339
DJ
9134 cu->die_hash
9135 = htab_create_alloc_ex (cu->header.length / 12,
9136 die_hash,
9137 die_eq,
9138 NULL,
9139 &cu->comp_unit_obstack,
9140 hashtab_obstack_allocate,
9141 dummy_obstack_deallocate);
9142
93311388
DE
9143 init_cu_die_reader (&reader_specs, cu);
9144
98bfdba5
PA
9145 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9146
9147 if (read_abbrevs)
9148 do_cleanups (back_to);
9149
9150 return die;
639d11d3
DC
9151}
9152
d97bc12b
DE
9153/* Main entry point for reading a DIE and all children.
9154 Read the DIE and dump it if requested. */
9155
9156static struct die_info *
93311388
DE
9157read_die_and_children (const struct die_reader_specs *reader,
9158 gdb_byte *info_ptr,
d97bc12b
DE
9159 gdb_byte **new_info_ptr,
9160 struct die_info *parent)
9161{
93311388 9162 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
9163 new_info_ptr, parent);
9164
9165 if (dwarf2_die_debug)
9166 {
348e048f
DE
9167 fprintf_unfiltered (gdb_stdlog,
9168 "\nRead die from %s of %s:\n",
8b70b953
TT
9169 (reader->cu->per_cu->debug_type_section
9170 ? ".debug_types"
9171 : ".debug_info"),
348e048f 9172 reader->abfd->filename);
d97bc12b
DE
9173 dump_die (result, dwarf2_die_debug);
9174 }
9175
9176 return result;
9177}
9178
639d11d3
DC
9179/* Read a single die and all its descendents. Set the die's sibling
9180 field to NULL; set other fields in the die correctly, and set all
9181 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9182 location of the info_ptr after reading all of those dies. PARENT
9183 is the parent of the die in question. */
9184
9185static struct die_info *
93311388
DE
9186read_die_and_children_1 (const struct die_reader_specs *reader,
9187 gdb_byte *info_ptr,
d97bc12b
DE
9188 gdb_byte **new_info_ptr,
9189 struct die_info *parent)
639d11d3
DC
9190{
9191 struct die_info *die;
fe1b8b76 9192 gdb_byte *cur_ptr;
639d11d3
DC
9193 int has_children;
9194
93311388 9195 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
9196 if (die == NULL)
9197 {
9198 *new_info_ptr = cur_ptr;
9199 return NULL;
9200 }
93311388 9201 store_in_ref_table (die, reader->cu);
639d11d3
DC
9202
9203 if (has_children)
348e048f 9204 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
9205 else
9206 {
9207 die->child = NULL;
9208 *new_info_ptr = cur_ptr;
9209 }
9210
9211 die->sibling = NULL;
9212 die->parent = parent;
9213 return die;
9214}
9215
9216/* Read a die, all of its descendents, and all of its siblings; set
9217 all of the fields of all of the dies correctly. Arguments are as
9218 in read_die_and_children. */
9219
9220static struct die_info *
93311388
DE
9221read_die_and_siblings (const struct die_reader_specs *reader,
9222 gdb_byte *info_ptr,
fe1b8b76 9223 gdb_byte **new_info_ptr,
639d11d3
DC
9224 struct die_info *parent)
9225{
9226 struct die_info *first_die, *last_sibling;
fe1b8b76 9227 gdb_byte *cur_ptr;
639d11d3 9228
c906108c 9229 cur_ptr = info_ptr;
639d11d3
DC
9230 first_die = last_sibling = NULL;
9231
9232 while (1)
c906108c 9233 {
639d11d3 9234 struct die_info *die
93311388 9235 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 9236
1d325ec1 9237 if (die == NULL)
c906108c 9238 {
639d11d3
DC
9239 *new_info_ptr = cur_ptr;
9240 return first_die;
c906108c 9241 }
1d325ec1
DJ
9242
9243 if (!first_die)
9244 first_die = die;
c906108c 9245 else
1d325ec1
DJ
9246 last_sibling->sibling = die;
9247
9248 last_sibling = die;
c906108c 9249 }
c906108c
SS
9250}
9251
93311388
DE
9252/* Read the die from the .debug_info section buffer. Set DIEP to
9253 point to a newly allocated die with its information, except for its
9254 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9255 whether the die has children or not. */
9256
9257static gdb_byte *
9258read_full_die (const struct die_reader_specs *reader,
9259 struct die_info **diep, gdb_byte *info_ptr,
9260 int *has_children)
9261{
9262 unsigned int abbrev_number, bytes_read, i, offset;
9263 struct abbrev_info *abbrev;
9264 struct die_info *die;
9265 struct dwarf2_cu *cu = reader->cu;
9266 bfd *abfd = reader->abfd;
9267
9268 offset = info_ptr - reader->buffer;
9269 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9270 info_ptr += bytes_read;
9271 if (!abbrev_number)
9272 {
9273 *diep = NULL;
9274 *has_children = 0;
9275 return info_ptr;
9276 }
9277
9278 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9279 if (!abbrev)
348e048f
DE
9280 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9281 abbrev_number,
9282 bfd_get_filename (abfd));
9283
93311388
DE
9284 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9285 die->offset = offset;
9286 die->tag = abbrev->tag;
9287 die->abbrev = abbrev_number;
9288
9289 die->num_attrs = abbrev->num_attrs;
9290
9291 for (i = 0; i < abbrev->num_attrs; ++i)
9292 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9293 abfd, info_ptr, cu);
9294
9295 *diep = die;
9296 *has_children = abbrev->has_children;
9297 return info_ptr;
9298}
9299
c906108c
SS
9300/* In DWARF version 2, the description of the debugging information is
9301 stored in a separate .debug_abbrev section. Before we read any
9302 dies from a section we read in all abbreviations and install them
72bf9492
DJ
9303 in a hash table. This function also sets flags in CU describing
9304 the data found in the abbrev table. */
c906108c
SS
9305
9306static void
e7c27a73 9307dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 9308{
e7c27a73 9309 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 9310 gdb_byte *abbrev_ptr;
c906108c
SS
9311 struct abbrev_info *cur_abbrev;
9312 unsigned int abbrev_number, bytes_read, abbrev_name;
9313 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
9314 struct attr_abbrev *cur_attrs;
9315 unsigned int allocated_attrs;
c906108c 9316
0963b4bd 9317 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
9318 obstack_init (&cu->abbrev_obstack);
9319 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9320 (ABBREV_HASH_SIZE
9321 * sizeof (struct abbrev_info *)));
9322 memset (cu->dwarf2_abbrevs, 0,
9323 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 9324
be391dca
TT
9325 dwarf2_read_section (dwarf2_per_objfile->objfile,
9326 &dwarf2_per_objfile->abbrev);
dce234bc 9327 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
9328 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9329 abbrev_ptr += bytes_read;
9330
f3dd6933
DJ
9331 allocated_attrs = ATTR_ALLOC_CHUNK;
9332 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 9333
0963b4bd 9334 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
9335 while (abbrev_number)
9336 {
f3dd6933 9337 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
9338
9339 /* read in abbrev header */
9340 cur_abbrev->number = abbrev_number;
9341 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9342 abbrev_ptr += bytes_read;
9343 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9344 abbrev_ptr += 1;
9345
72bf9492
DJ
9346 if (cur_abbrev->tag == DW_TAG_namespace)
9347 cu->has_namespace_info = 1;
9348
c906108c
SS
9349 /* now read in declarations */
9350 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9351 abbrev_ptr += bytes_read;
9352 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9353 abbrev_ptr += bytes_read;
9354 while (abbrev_name)
9355 {
f3dd6933 9356 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 9357 {
f3dd6933
DJ
9358 allocated_attrs += ATTR_ALLOC_CHUNK;
9359 cur_attrs
9360 = xrealloc (cur_attrs, (allocated_attrs
9361 * sizeof (struct attr_abbrev)));
c906108c 9362 }
ae038cb0
DJ
9363
9364 /* Record whether this compilation unit might have
9365 inter-compilation-unit references. If we don't know what form
9366 this attribute will have, then it might potentially be a
9367 DW_FORM_ref_addr, so we conservatively expect inter-CU
9368 references. */
9369
9370 if (abbrev_form == DW_FORM_ref_addr
9371 || abbrev_form == DW_FORM_indirect)
9372 cu->has_form_ref_addr = 1;
9373
f3dd6933
DJ
9374 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9375 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
9376 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9377 abbrev_ptr += bytes_read;
9378 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9379 abbrev_ptr += bytes_read;
9380 }
9381
f3dd6933
DJ
9382 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9383 (cur_abbrev->num_attrs
9384 * sizeof (struct attr_abbrev)));
9385 memcpy (cur_abbrev->attrs, cur_attrs,
9386 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9387
c906108c 9388 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
9389 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9390 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
9391
9392 /* Get next abbreviation.
9393 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
9394 always properly terminated with an abbrev number of 0.
9395 Exit loop if we encounter an abbreviation which we have
9396 already read (which means we are about to read the abbreviations
9397 for the next compile unit) or if the end of the abbreviation
9398 table is reached. */
dce234bc
PP
9399 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9400 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
9401 break;
9402 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9403 abbrev_ptr += bytes_read;
e7c27a73 9404 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
9405 break;
9406 }
f3dd6933
DJ
9407
9408 xfree (cur_attrs);
c906108c
SS
9409}
9410
f3dd6933 9411/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 9412
c906108c 9413static void
f3dd6933 9414dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 9415{
f3dd6933 9416 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 9417
f3dd6933
DJ
9418 obstack_free (&cu->abbrev_obstack, NULL);
9419 cu->dwarf2_abbrevs = NULL;
c906108c
SS
9420}
9421
9422/* Lookup an abbrev_info structure in the abbrev hash table. */
9423
9424static struct abbrev_info *
e7c27a73 9425dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
9426{
9427 unsigned int hash_number;
9428 struct abbrev_info *abbrev;
9429
9430 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 9431 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
9432
9433 while (abbrev)
9434 {
9435 if (abbrev->number == number)
9436 return abbrev;
9437 else
9438 abbrev = abbrev->next;
9439 }
9440 return NULL;
9441}
9442
72bf9492
DJ
9443/* Returns nonzero if TAG represents a type that we might generate a partial
9444 symbol for. */
9445
9446static int
9447is_type_tag_for_partial (int tag)
9448{
9449 switch (tag)
9450 {
9451#if 0
9452 /* Some types that would be reasonable to generate partial symbols for,
9453 that we don't at present. */
9454 case DW_TAG_array_type:
9455 case DW_TAG_file_type:
9456 case DW_TAG_ptr_to_member_type:
9457 case DW_TAG_set_type:
9458 case DW_TAG_string_type:
9459 case DW_TAG_subroutine_type:
9460#endif
9461 case DW_TAG_base_type:
9462 case DW_TAG_class_type:
680b30c7 9463 case DW_TAG_interface_type:
72bf9492
DJ
9464 case DW_TAG_enumeration_type:
9465 case DW_TAG_structure_type:
9466 case DW_TAG_subrange_type:
9467 case DW_TAG_typedef:
9468 case DW_TAG_union_type:
9469 return 1;
9470 default:
9471 return 0;
9472 }
9473}
9474
9475/* Load all DIEs that are interesting for partial symbols into memory. */
9476
9477static struct partial_die_info *
93311388
DE
9478load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9479 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
9480{
9481 struct partial_die_info *part_die;
9482 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9483 struct abbrev_info *abbrev;
9484 unsigned int bytes_read;
5afb4e99 9485 unsigned int load_all = 0;
72bf9492
DJ
9486
9487 int nesting_level = 1;
9488
9489 parent_die = NULL;
9490 last_die = NULL;
9491
5afb4e99
DJ
9492 if (cu->per_cu && cu->per_cu->load_all_dies)
9493 load_all = 1;
9494
72bf9492
DJ
9495 cu->partial_dies
9496 = htab_create_alloc_ex (cu->header.length / 12,
9497 partial_die_hash,
9498 partial_die_eq,
9499 NULL,
9500 &cu->comp_unit_obstack,
9501 hashtab_obstack_allocate,
9502 dummy_obstack_deallocate);
9503
9504 part_die = obstack_alloc (&cu->comp_unit_obstack,
9505 sizeof (struct partial_die_info));
9506
9507 while (1)
9508 {
9509 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9510
9511 /* A NULL abbrev means the end of a series of children. */
9512 if (abbrev == NULL)
9513 {
9514 if (--nesting_level == 0)
9515 {
9516 /* PART_DIE was probably the last thing allocated on the
9517 comp_unit_obstack, so we could call obstack_free
9518 here. We don't do that because the waste is small,
9519 and will be cleaned up when we're done with this
9520 compilation unit. This way, we're also more robust
9521 against other users of the comp_unit_obstack. */
9522 return first_die;
9523 }
9524 info_ptr += bytes_read;
9525 last_die = parent_die;
9526 parent_die = parent_die->die_parent;
9527 continue;
9528 }
9529
98bfdba5
PA
9530 /* Check for template arguments. We never save these; if
9531 they're seen, we just mark the parent, and go on our way. */
9532 if (parent_die != NULL
9533 && cu->language == language_cplus
9534 && (abbrev->tag == DW_TAG_template_type_param
9535 || abbrev->tag == DW_TAG_template_value_param))
9536 {
9537 parent_die->has_template_arguments = 1;
9538
9539 if (!load_all)
9540 {
9541 /* We don't need a partial DIE for the template argument. */
9542 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9543 cu);
9544 continue;
9545 }
9546 }
9547
9548 /* We only recurse into subprograms looking for template arguments.
9549 Skip their other children. */
9550 if (!load_all
9551 && cu->language == language_cplus
9552 && parent_die != NULL
9553 && parent_die->tag == DW_TAG_subprogram)
9554 {
9555 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9556 continue;
9557 }
9558
5afb4e99
DJ
9559 /* Check whether this DIE is interesting enough to save. Normally
9560 we would not be interested in members here, but there may be
9561 later variables referencing them via DW_AT_specification (for
9562 static members). */
9563 if (!load_all
9564 && !is_type_tag_for_partial (abbrev->tag)
72929c62 9565 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
9566 && abbrev->tag != DW_TAG_enumerator
9567 && abbrev->tag != DW_TAG_subprogram
bc30ff58 9568 && abbrev->tag != DW_TAG_lexical_block
72bf9492 9569 && abbrev->tag != DW_TAG_variable
5afb4e99 9570 && abbrev->tag != DW_TAG_namespace
f55ee35c 9571 && abbrev->tag != DW_TAG_module
5afb4e99 9572 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
9573 {
9574 /* Otherwise we skip to the next sibling, if any. */
93311388 9575 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
9576 continue;
9577 }
9578
93311388
DE
9579 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9580 buffer, info_ptr, cu);
72bf9492
DJ
9581
9582 /* This two-pass algorithm for processing partial symbols has a
9583 high cost in cache pressure. Thus, handle some simple cases
9584 here which cover the majority of C partial symbols. DIEs
9585 which neither have specification tags in them, nor could have
9586 specification tags elsewhere pointing at them, can simply be
9587 processed and discarded.
9588
9589 This segment is also optional; scan_partial_symbols and
9590 add_partial_symbol will handle these DIEs if we chain
9591 them in normally. When compilers which do not emit large
9592 quantities of duplicate debug information are more common,
9593 this code can probably be removed. */
9594
9595 /* Any complete simple types at the top level (pretty much all
9596 of them, for a language without namespaces), can be processed
9597 directly. */
9598 if (parent_die == NULL
9599 && part_die->has_specification == 0
9600 && part_die->is_declaration == 0
d8228535 9601 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
9602 || part_die->tag == DW_TAG_base_type
9603 || part_die->tag == DW_TAG_subrange_type))
9604 {
9605 if (building_psymtab && part_die->name != NULL)
04a679b8 9606 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
9607 VAR_DOMAIN, LOC_TYPEDEF,
9608 &cu->objfile->static_psymbols,
9609 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 9610 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9611 continue;
9612 }
9613
d8228535
JK
9614 /* The exception for DW_TAG_typedef with has_children above is
9615 a workaround of GCC PR debug/47510. In the case of this complaint
9616 type_name_no_tag_or_error will error on such types later.
9617
9618 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9619 it could not find the child DIEs referenced later, this is checked
9620 above. In correct DWARF DW_TAG_typedef should have no children. */
9621
9622 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9623 complaint (&symfile_complaints,
9624 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9625 "- DIE at 0x%x [in module %s]"),
9626 part_die->offset, cu->objfile->name);
9627
72bf9492
DJ
9628 /* If we're at the second level, and we're an enumerator, and
9629 our parent has no specification (meaning possibly lives in a
9630 namespace elsewhere), then we can add the partial symbol now
9631 instead of queueing it. */
9632 if (part_die->tag == DW_TAG_enumerator
9633 && parent_die != NULL
9634 && parent_die->die_parent == NULL
9635 && parent_die->tag == DW_TAG_enumeration_type
9636 && parent_die->has_specification == 0)
9637 {
9638 if (part_die->name == NULL)
3e43a32a
MS
9639 complaint (&symfile_complaints,
9640 _("malformed enumerator DIE ignored"));
72bf9492 9641 else if (building_psymtab)
04a679b8 9642 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 9643 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
9644 (cu->language == language_cplus
9645 || cu->language == language_java)
72bf9492
DJ
9646 ? &cu->objfile->global_psymbols
9647 : &cu->objfile->static_psymbols,
9648 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9649
93311388 9650 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9651 continue;
9652 }
9653
9654 /* We'll save this DIE so link it in. */
9655 part_die->die_parent = parent_die;
9656 part_die->die_sibling = NULL;
9657 part_die->die_child = NULL;
9658
9659 if (last_die && last_die == parent_die)
9660 last_die->die_child = part_die;
9661 else if (last_die)
9662 last_die->die_sibling = part_die;
9663
9664 last_die = part_die;
9665
9666 if (first_die == NULL)
9667 first_die = part_die;
9668
9669 /* Maybe add the DIE to the hash table. Not all DIEs that we
9670 find interesting need to be in the hash table, because we
9671 also have the parent/sibling/child chains; only those that we
9672 might refer to by offset later during partial symbol reading.
9673
9674 For now this means things that might have be the target of a
9675 DW_AT_specification, DW_AT_abstract_origin, or
9676 DW_AT_extension. DW_AT_extension will refer only to
9677 namespaces; DW_AT_abstract_origin refers to functions (and
9678 many things under the function DIE, but we do not recurse
9679 into function DIEs during partial symbol reading) and
9680 possibly variables as well; DW_AT_specification refers to
9681 declarations. Declarations ought to have the DW_AT_declaration
9682 flag. It happens that GCC forgets to put it in sometimes, but
9683 only for functions, not for types.
9684
9685 Adding more things than necessary to the hash table is harmless
9686 except for the performance cost. Adding too few will result in
5afb4e99
DJ
9687 wasted time in find_partial_die, when we reread the compilation
9688 unit with load_all_dies set. */
72bf9492 9689
5afb4e99 9690 if (load_all
72929c62 9691 || abbrev->tag == DW_TAG_constant
5afb4e99 9692 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
9693 || abbrev->tag == DW_TAG_variable
9694 || abbrev->tag == DW_TAG_namespace
9695 || part_die->is_declaration)
9696 {
9697 void **slot;
9698
9699 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9700 part_die->offset, INSERT);
9701 *slot = part_die;
9702 }
9703
9704 part_die = obstack_alloc (&cu->comp_unit_obstack,
9705 sizeof (struct partial_die_info));
9706
9707 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 9708 we have no reason to follow the children of structures; for other
98bfdba5
PA
9709 languages we have to, so that we can get at method physnames
9710 to infer fully qualified class names, for DW_AT_specification,
9711 and for C++ template arguments. For C++, we also look one level
9712 inside functions to find template arguments (if the name of the
9713 function does not already contain the template arguments).
bc30ff58
JB
9714
9715 For Ada, we need to scan the children of subprograms and lexical
9716 blocks as well because Ada allows the definition of nested
9717 entities that could be interesting for the debugger, such as
9718 nested subprograms for instance. */
72bf9492 9719 if (last_die->has_children
5afb4e99
DJ
9720 && (load_all
9721 || last_die->tag == DW_TAG_namespace
f55ee35c 9722 || last_die->tag == DW_TAG_module
72bf9492 9723 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
9724 || (cu->language == language_cplus
9725 && last_die->tag == DW_TAG_subprogram
9726 && (last_die->name == NULL
9727 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
9728 || (cu->language != language_c
9729 && (last_die->tag == DW_TAG_class_type
680b30c7 9730 || last_die->tag == DW_TAG_interface_type
72bf9492 9731 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
9732 || last_die->tag == DW_TAG_union_type))
9733 || (cu->language == language_ada
9734 && (last_die->tag == DW_TAG_subprogram
9735 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
9736 {
9737 nesting_level++;
9738 parent_die = last_die;
9739 continue;
9740 }
9741
9742 /* Otherwise we skip to the next sibling, if any. */
93311388 9743 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
9744
9745 /* Back to the top, do it again. */
9746 }
9747}
9748
c906108c
SS
9749/* Read a minimal amount of information into the minimal die structure. */
9750
fe1b8b76 9751static gdb_byte *
72bf9492
DJ
9752read_partial_die (struct partial_die_info *part_die,
9753 struct abbrev_info *abbrev,
9754 unsigned int abbrev_len, bfd *abfd,
93311388
DE
9755 gdb_byte *buffer, gdb_byte *info_ptr,
9756 struct dwarf2_cu *cu)
c906108c 9757{
fa238c03 9758 unsigned int i;
c906108c 9759 struct attribute attr;
c5aa993b 9760 int has_low_pc_attr = 0;
c906108c
SS
9761 int has_high_pc_attr = 0;
9762
72bf9492 9763 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 9764
93311388 9765 part_die->offset = info_ptr - buffer;
72bf9492
DJ
9766
9767 info_ptr += abbrev_len;
9768
9769 if (abbrev == NULL)
9770 return info_ptr;
9771
c906108c
SS
9772 part_die->tag = abbrev->tag;
9773 part_die->has_children = abbrev->has_children;
c906108c
SS
9774
9775 for (i = 0; i < abbrev->num_attrs; ++i)
9776 {
e7c27a73 9777 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
9778
9779 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 9780 partial symbol table. */
c906108c
SS
9781 switch (attr.name)
9782 {
9783 case DW_AT_name:
71c25dea
TT
9784 switch (part_die->tag)
9785 {
9786 case DW_TAG_compile_unit:
348e048f 9787 case DW_TAG_type_unit:
71c25dea
TT
9788 /* Compilation units have a DW_AT_name that is a filename, not
9789 a source language identifier. */
9790 case DW_TAG_enumeration_type:
9791 case DW_TAG_enumerator:
9792 /* These tags always have simple identifiers already; no need
9793 to canonicalize them. */
9794 part_die->name = DW_STRING (&attr);
9795 break;
9796 default:
9797 part_die->name
9798 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 9799 &cu->objfile->objfile_obstack);
71c25dea
TT
9800 break;
9801 }
c906108c 9802 break;
31ef98ae 9803 case DW_AT_linkage_name:
c906108c 9804 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
9805 /* Note that both forms of linkage name might appear. We
9806 assume they will be the same, and we only store the last
9807 one we see. */
94af9270
KS
9808 if (cu->language == language_ada)
9809 part_die->name = DW_STRING (&attr);
abc72ce4 9810 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
9811 break;
9812 case DW_AT_low_pc:
9813 has_low_pc_attr = 1;
9814 part_die->lowpc = DW_ADDR (&attr);
9815 break;
9816 case DW_AT_high_pc:
9817 has_high_pc_attr = 1;
9818 part_die->highpc = DW_ADDR (&attr);
9819 break;
9820 case DW_AT_location:
0963b4bd 9821 /* Support the .debug_loc offsets. */
8e19ed76
PS
9822 if (attr_form_is_block (&attr))
9823 {
9824 part_die->locdesc = DW_BLOCK (&attr);
9825 }
3690dd37 9826 else if (attr_form_is_section_offset (&attr))
8e19ed76 9827 {
4d3c2250 9828 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9829 }
9830 else
9831 {
4d3c2250
KB
9832 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9833 "partial symbol information");
8e19ed76 9834 }
c906108c 9835 break;
c906108c
SS
9836 case DW_AT_external:
9837 part_die->is_external = DW_UNSND (&attr);
9838 break;
9839 case DW_AT_declaration:
9840 part_die->is_declaration = DW_UNSND (&attr);
9841 break;
9842 case DW_AT_type:
9843 part_die->has_type = 1;
9844 break;
9845 case DW_AT_abstract_origin:
9846 case DW_AT_specification:
72bf9492
DJ
9847 case DW_AT_extension:
9848 part_die->has_specification = 1;
c764a876 9849 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
9850 break;
9851 case DW_AT_sibling:
9852 /* Ignore absolute siblings, they might point outside of
9853 the current compile unit. */
9854 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
9855 complaint (&symfile_complaints,
9856 _("ignoring absolute DW_AT_sibling"));
c906108c 9857 else
93311388 9858 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 9859 break;
fa4028e9
JB
9860 case DW_AT_byte_size:
9861 part_die->has_byte_size = 1;
9862 break;
68511cec
CES
9863 case DW_AT_calling_convention:
9864 /* DWARF doesn't provide a way to identify a program's source-level
9865 entry point. DW_AT_calling_convention attributes are only meant
9866 to describe functions' calling conventions.
9867
9868 However, because it's a necessary piece of information in
9869 Fortran, and because DW_CC_program is the only piece of debugging
9870 information whose definition refers to a 'main program' at all,
9871 several compilers have begun marking Fortran main programs with
9872 DW_CC_program --- even when those functions use the standard
9873 calling conventions.
9874
9875 So until DWARF specifies a way to provide this information and
9876 compilers pick up the new representation, we'll support this
9877 practice. */
9878 if (DW_UNSND (&attr) == DW_CC_program
9879 && cu->language == language_fortran)
01f8c46d
JK
9880 {
9881 set_main_name (part_die->name);
9882
9883 /* As this DIE has a static linkage the name would be difficult
9884 to look up later. */
9885 language_of_main = language_fortran;
9886 }
68511cec 9887 break;
c906108c
SS
9888 default:
9889 break;
9890 }
9891 }
9892
9373cf26
JK
9893 if (has_low_pc_attr && has_high_pc_attr)
9894 {
9895 /* When using the GNU linker, .gnu.linkonce. sections are used to
9896 eliminate duplicate copies of functions and vtables and such.
9897 The linker will arbitrarily choose one and discard the others.
9898 The AT_*_pc values for such functions refer to local labels in
9899 these sections. If the section from that file was discarded, the
9900 labels are not in the output, so the relocs get a value of 0.
9901 If this is a discarded function, mark the pc bounds as invalid,
9902 so that GDB will ignore it. */
9903 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9904 {
9905 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9906
9907 complaint (&symfile_complaints,
9908 _("DW_AT_low_pc %s is zero "
9909 "for DIE at 0x%x [in module %s]"),
9910 paddress (gdbarch, part_die->lowpc),
9911 part_die->offset, cu->objfile->name);
9912 }
9913 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9914 else if (part_die->lowpc >= part_die->highpc)
9915 {
9916 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9917
9918 complaint (&symfile_complaints,
9919 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9920 "for DIE at 0x%x [in module %s]"),
9921 paddress (gdbarch, part_die->lowpc),
9922 paddress (gdbarch, part_die->highpc),
9923 part_die->offset, cu->objfile->name);
9924 }
9925 else
9926 part_die->has_pc_info = 1;
9927 }
85cbf3d3 9928
c906108c
SS
9929 return info_ptr;
9930}
9931
72bf9492
DJ
9932/* Find a cached partial DIE at OFFSET in CU. */
9933
9934static struct partial_die_info *
c764a876 9935find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
9936{
9937 struct partial_die_info *lookup_die = NULL;
9938 struct partial_die_info part_die;
9939
9940 part_die.offset = offset;
9941 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9942
72bf9492
DJ
9943 return lookup_die;
9944}
9945
348e048f
DE
9946/* Find a partial DIE at OFFSET, which may or may not be in CU,
9947 except in the case of .debug_types DIEs which do not reference
9948 outside their CU (they do however referencing other types via
55f1336d 9949 DW_FORM_ref_sig8). */
72bf9492
DJ
9950
9951static struct partial_die_info *
c764a876 9952find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 9953{
5afb4e99
DJ
9954 struct dwarf2_per_cu_data *per_cu = NULL;
9955 struct partial_die_info *pd = NULL;
72bf9492 9956
8b70b953 9957 if (cu->per_cu->debug_type_section)
348e048f
DE
9958 {
9959 pd = find_partial_die_in_comp_unit (offset, cu);
9960 if (pd != NULL)
9961 return pd;
9962 goto not_found;
9963 }
9964
45452591 9965 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
9966 {
9967 pd = find_partial_die_in_comp_unit (offset, cu);
9968 if (pd != NULL)
9969 return pd;
9970 }
72bf9492 9971
ae038cb0
DJ
9972 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9973
98bfdba5
PA
9974 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9975 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
9976
9977 per_cu->cu->last_used = 0;
5afb4e99
DJ
9978 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9979
9980 if (pd == NULL && per_cu->load_all_dies == 0)
9981 {
9982 struct cleanup *back_to;
9983 struct partial_die_info comp_unit_die;
9984 struct abbrev_info *abbrev;
9985 unsigned int bytes_read;
9986 char *info_ptr;
9987
9988 per_cu->load_all_dies = 1;
9989
9990 /* Re-read the DIEs. */
9991 back_to = make_cleanup (null_cleanup, 0);
9992 if (per_cu->cu->dwarf2_abbrevs == NULL)
9993 {
9994 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 9995 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 9996 }
dce234bc 9997 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
9998 + per_cu->cu->header.offset
9999 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
10000 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10001 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
10002 per_cu->cu->objfile->obfd,
10003 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
10004 per_cu->cu);
10005 if (comp_unit_die.has_children)
93311388
DE
10006 load_partial_dies (per_cu->cu->objfile->obfd,
10007 dwarf2_per_objfile->info.buffer, info_ptr,
10008 0, per_cu->cu);
5afb4e99
DJ
10009 do_cleanups (back_to);
10010
10011 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10012 }
10013
348e048f
DE
10014 not_found:
10015
5afb4e99
DJ
10016 if (pd == NULL)
10017 internal_error (__FILE__, __LINE__,
3e43a32a
MS
10018 _("could not find partial DIE 0x%x "
10019 "in cache [from module %s]\n"),
5afb4e99
DJ
10020 offset, bfd_get_filename (cu->objfile->obfd));
10021 return pd;
72bf9492
DJ
10022}
10023
abc72ce4
DE
10024/* See if we can figure out if the class lives in a namespace. We do
10025 this by looking for a member function; its demangled name will
10026 contain namespace info, if there is any. */
10027
10028static void
10029guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10030 struct dwarf2_cu *cu)
10031{
10032 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10033 what template types look like, because the demangler
10034 frequently doesn't give the same name as the debug info. We
10035 could fix this by only using the demangled name to get the
10036 prefix (but see comment in read_structure_type). */
10037
10038 struct partial_die_info *real_pdi;
10039 struct partial_die_info *child_pdi;
10040
10041 /* If this DIE (this DIE's specification, if any) has a parent, then
10042 we should not do this. We'll prepend the parent's fully qualified
10043 name when we create the partial symbol. */
10044
10045 real_pdi = struct_pdi;
10046 while (real_pdi->has_specification)
10047 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10048
10049 if (real_pdi->die_parent != NULL)
10050 return;
10051
10052 for (child_pdi = struct_pdi->die_child;
10053 child_pdi != NULL;
10054 child_pdi = child_pdi->die_sibling)
10055 {
10056 if (child_pdi->tag == DW_TAG_subprogram
10057 && child_pdi->linkage_name != NULL)
10058 {
10059 char *actual_class_name
10060 = language_class_name_from_physname (cu->language_defn,
10061 child_pdi->linkage_name);
10062 if (actual_class_name != NULL)
10063 {
10064 struct_pdi->name
10065 = obsavestring (actual_class_name,
10066 strlen (actual_class_name),
10067 &cu->objfile->objfile_obstack);
10068 xfree (actual_class_name);
10069 }
10070 break;
10071 }
10072 }
10073}
10074
72bf9492
DJ
10075/* Adjust PART_DIE before generating a symbol for it. This function
10076 may set the is_external flag or change the DIE's name. */
10077
10078static void
10079fixup_partial_die (struct partial_die_info *part_die,
10080 struct dwarf2_cu *cu)
10081{
abc72ce4
DE
10082 /* Once we've fixed up a die, there's no point in doing so again.
10083 This also avoids a memory leak if we were to call
10084 guess_partial_die_structure_name multiple times. */
10085 if (part_die->fixup_called)
10086 return;
10087
72bf9492
DJ
10088 /* If we found a reference attribute and the DIE has no name, try
10089 to find a name in the referred to DIE. */
10090
10091 if (part_die->name == NULL && part_die->has_specification)
10092 {
10093 struct partial_die_info *spec_die;
72bf9492 10094
10b3939b 10095 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 10096
10b3939b 10097 fixup_partial_die (spec_die, cu);
72bf9492
DJ
10098
10099 if (spec_die->name)
10100 {
10101 part_die->name = spec_die->name;
10102
10103 /* Copy DW_AT_external attribute if it is set. */
10104 if (spec_die->is_external)
10105 part_die->is_external = spec_die->is_external;
10106 }
10107 }
10108
10109 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
10110
10111 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 10112 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 10113
abc72ce4
DE
10114 /* If there is no parent die to provide a namespace, and there are
10115 children, see if we can determine the namespace from their linkage
10116 name.
10117 NOTE: We need to do this even if cu->has_namespace_info != 0.
10118 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10119 if (cu->language == language_cplus
8b70b953 10120 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
10121 && part_die->die_parent == NULL
10122 && part_die->has_children
10123 && (part_die->tag == DW_TAG_class_type
10124 || part_die->tag == DW_TAG_structure_type
10125 || part_die->tag == DW_TAG_union_type))
10126 guess_partial_die_structure_name (part_die, cu);
10127
53832f31
TT
10128 /* GCC might emit a nameless struct or union that has a linkage
10129 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10130 if (part_die->name == NULL
96408a79
SA
10131 && (part_die->tag == DW_TAG_class_type
10132 || part_die->tag == DW_TAG_interface_type
10133 || part_die->tag == DW_TAG_structure_type
10134 || part_die->tag == DW_TAG_union_type)
53832f31
TT
10135 && part_die->linkage_name != NULL)
10136 {
10137 char *demangled;
10138
10139 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10140 if (demangled)
10141 {
96408a79
SA
10142 const char *base;
10143
10144 /* Strip any leading namespaces/classes, keep only the base name.
10145 DW_AT_name for named DIEs does not contain the prefixes. */
10146 base = strrchr (demangled, ':');
10147 if (base && base > demangled && base[-1] == ':')
10148 base++;
10149 else
10150 base = demangled;
10151
10152 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
10153 &cu->objfile->objfile_obstack);
10154 xfree (demangled);
10155 }
10156 }
10157
abc72ce4 10158 part_die->fixup_called = 1;
72bf9492
DJ
10159}
10160
a8329558 10161/* Read an attribute value described by an attribute form. */
c906108c 10162
fe1b8b76 10163static gdb_byte *
a8329558 10164read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 10165 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 10166 struct dwarf2_cu *cu)
c906108c 10167{
e7c27a73 10168 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10169 unsigned int bytes_read;
10170 struct dwarf_block *blk;
10171
a8329558
KW
10172 attr->form = form;
10173 switch (form)
c906108c 10174 {
c906108c 10175 case DW_FORM_ref_addr:
ae411497
TT
10176 if (cu->header.version == 2)
10177 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10178 else
3e43a32a
MS
10179 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10180 &cu->header, &bytes_read);
ae411497
TT
10181 info_ptr += bytes_read;
10182 break;
10183 case DW_FORM_addr:
e7c27a73 10184 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 10185 info_ptr += bytes_read;
c906108c
SS
10186 break;
10187 case DW_FORM_block2:
7b5a2f43 10188 blk = dwarf_alloc_block (cu);
c906108c
SS
10189 blk->size = read_2_bytes (abfd, info_ptr);
10190 info_ptr += 2;
10191 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10192 info_ptr += blk->size;
10193 DW_BLOCK (attr) = blk;
10194 break;
10195 case DW_FORM_block4:
7b5a2f43 10196 blk = dwarf_alloc_block (cu);
c906108c
SS
10197 blk->size = read_4_bytes (abfd, info_ptr);
10198 info_ptr += 4;
10199 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10200 info_ptr += blk->size;
10201 DW_BLOCK (attr) = blk;
10202 break;
10203 case DW_FORM_data2:
10204 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10205 info_ptr += 2;
10206 break;
10207 case DW_FORM_data4:
10208 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10209 info_ptr += 4;
10210 break;
10211 case DW_FORM_data8:
10212 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10213 info_ptr += 8;
10214 break;
2dc7f7b3
TT
10215 case DW_FORM_sec_offset:
10216 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10217 info_ptr += bytes_read;
10218 break;
c906108c 10219 case DW_FORM_string:
9b1c24c8 10220 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 10221 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
10222 info_ptr += bytes_read;
10223 break;
4bdf3d34
JJ
10224 case DW_FORM_strp:
10225 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10226 &bytes_read);
8285870a 10227 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
10228 info_ptr += bytes_read;
10229 break;
2dc7f7b3 10230 case DW_FORM_exprloc:
c906108c 10231 case DW_FORM_block:
7b5a2f43 10232 blk = dwarf_alloc_block (cu);
c906108c
SS
10233 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10234 info_ptr += bytes_read;
10235 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10236 info_ptr += blk->size;
10237 DW_BLOCK (attr) = blk;
10238 break;
10239 case DW_FORM_block1:
7b5a2f43 10240 blk = dwarf_alloc_block (cu);
c906108c
SS
10241 blk->size = read_1_byte (abfd, info_ptr);
10242 info_ptr += 1;
10243 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10244 info_ptr += blk->size;
10245 DW_BLOCK (attr) = blk;
10246 break;
10247 case DW_FORM_data1:
10248 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10249 info_ptr += 1;
10250 break;
10251 case DW_FORM_flag:
10252 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10253 info_ptr += 1;
10254 break;
2dc7f7b3
TT
10255 case DW_FORM_flag_present:
10256 DW_UNSND (attr) = 1;
10257 break;
c906108c
SS
10258 case DW_FORM_sdata:
10259 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10260 info_ptr += bytes_read;
10261 break;
10262 case DW_FORM_udata:
10263 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10264 info_ptr += bytes_read;
10265 break;
10266 case DW_FORM_ref1:
10b3939b 10267 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
10268 info_ptr += 1;
10269 break;
10270 case DW_FORM_ref2:
10b3939b 10271 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
10272 info_ptr += 2;
10273 break;
10274 case DW_FORM_ref4:
10b3939b 10275 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
10276 info_ptr += 4;
10277 break;
613e1657 10278 case DW_FORM_ref8:
10b3939b 10279 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
10280 info_ptr += 8;
10281 break;
55f1336d 10282 case DW_FORM_ref_sig8:
348e048f
DE
10283 /* Convert the signature to something we can record in DW_UNSND
10284 for later lookup.
10285 NOTE: This is NULL if the type wasn't found. */
10286 DW_SIGNATURED_TYPE (attr) =
10287 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10288 info_ptr += 8;
10289 break;
c906108c 10290 case DW_FORM_ref_udata:
10b3939b
DJ
10291 DW_ADDR (attr) = (cu->header.offset
10292 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
10293 info_ptr += bytes_read;
10294 break;
c906108c 10295 case DW_FORM_indirect:
a8329558
KW
10296 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10297 info_ptr += bytes_read;
e7c27a73 10298 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 10299 break;
c906108c 10300 default:
8a3fe4f8 10301 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
10302 dwarf_form_name (form),
10303 bfd_get_filename (abfd));
c906108c 10304 }
28e94949
JB
10305
10306 /* We have seen instances where the compiler tried to emit a byte
10307 size attribute of -1 which ended up being encoded as an unsigned
10308 0xffffffff. Although 0xffffffff is technically a valid size value,
10309 an object of this size seems pretty unlikely so we can relatively
10310 safely treat these cases as if the size attribute was invalid and
10311 treat them as zero by default. */
10312 if (attr->name == DW_AT_byte_size
10313 && form == DW_FORM_data4
10314 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
10315 {
10316 complaint
10317 (&symfile_complaints,
43bbcdc2
PH
10318 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10319 hex_string (DW_UNSND (attr)));
01c66ae6
JB
10320 DW_UNSND (attr) = 0;
10321 }
28e94949 10322
c906108c
SS
10323 return info_ptr;
10324}
10325
a8329558
KW
10326/* Read an attribute described by an abbreviated attribute. */
10327
fe1b8b76 10328static gdb_byte *
a8329558 10329read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 10330 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
10331{
10332 attr->name = abbrev->name;
e7c27a73 10333 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
10334}
10335
0963b4bd 10336/* Read dwarf information from a buffer. */
c906108c
SS
10337
10338static unsigned int
fe1b8b76 10339read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 10340{
fe1b8b76 10341 return bfd_get_8 (abfd, buf);
c906108c
SS
10342}
10343
10344static int
fe1b8b76 10345read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 10346{
fe1b8b76 10347 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
10348}
10349
10350static unsigned int
fe1b8b76 10351read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10352{
fe1b8b76 10353 return bfd_get_16 (abfd, buf);
c906108c
SS
10354}
10355
21ae7a4d
JK
10356static int
10357read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10358{
10359 return bfd_get_signed_16 (abfd, buf);
10360}
10361
c906108c 10362static unsigned int
fe1b8b76 10363read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10364{
fe1b8b76 10365 return bfd_get_32 (abfd, buf);
c906108c
SS
10366}
10367
21ae7a4d
JK
10368static int
10369read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10370{
10371 return bfd_get_signed_32 (abfd, buf);
10372}
10373
93311388 10374static ULONGEST
fe1b8b76 10375read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 10376{
fe1b8b76 10377 return bfd_get_64 (abfd, buf);
c906108c
SS
10378}
10379
10380static CORE_ADDR
fe1b8b76 10381read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 10382 unsigned int *bytes_read)
c906108c 10383{
e7c27a73 10384 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
10385 CORE_ADDR retval = 0;
10386
107d2387 10387 if (cu_header->signed_addr_p)
c906108c 10388 {
107d2387
AC
10389 switch (cu_header->addr_size)
10390 {
10391 case 2:
fe1b8b76 10392 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
10393 break;
10394 case 4:
fe1b8b76 10395 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
10396 break;
10397 case 8:
fe1b8b76 10398 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
10399 break;
10400 default:
8e65ff28 10401 internal_error (__FILE__, __LINE__,
e2e0b3e5 10402 _("read_address: bad switch, signed [in module %s]"),
659b0389 10403 bfd_get_filename (abfd));
107d2387
AC
10404 }
10405 }
10406 else
10407 {
10408 switch (cu_header->addr_size)
10409 {
10410 case 2:
fe1b8b76 10411 retval = bfd_get_16 (abfd, buf);
107d2387
AC
10412 break;
10413 case 4:
fe1b8b76 10414 retval = bfd_get_32 (abfd, buf);
107d2387
AC
10415 break;
10416 case 8:
fe1b8b76 10417 retval = bfd_get_64 (abfd, buf);
107d2387
AC
10418 break;
10419 default:
8e65ff28 10420 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
10421 _("read_address: bad switch, "
10422 "unsigned [in module %s]"),
659b0389 10423 bfd_get_filename (abfd));
107d2387 10424 }
c906108c 10425 }
64367e0a 10426
107d2387
AC
10427 *bytes_read = cu_header->addr_size;
10428 return retval;
c906108c
SS
10429}
10430
f7ef9339 10431/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
10432 specification allows the initial length to take up either 4 bytes
10433 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10434 bytes describe the length and all offsets will be 8 bytes in length
10435 instead of 4.
10436
f7ef9339
KB
10437 An older, non-standard 64-bit format is also handled by this
10438 function. The older format in question stores the initial length
10439 as an 8-byte quantity without an escape value. Lengths greater
10440 than 2^32 aren't very common which means that the initial 4 bytes
10441 is almost always zero. Since a length value of zero doesn't make
10442 sense for the 32-bit format, this initial zero can be considered to
10443 be an escape value which indicates the presence of the older 64-bit
10444 format. As written, the code can't detect (old format) lengths
917c78fc
MK
10445 greater than 4GB. If it becomes necessary to handle lengths
10446 somewhat larger than 4GB, we could allow other small values (such
10447 as the non-sensical values of 1, 2, and 3) to also be used as
10448 escape values indicating the presence of the old format.
f7ef9339 10449
917c78fc
MK
10450 The value returned via bytes_read should be used to increment the
10451 relevant pointer after calling read_initial_length().
c764a876 10452
613e1657
KB
10453 [ Note: read_initial_length() and read_offset() are based on the
10454 document entitled "DWARF Debugging Information Format", revision
f7ef9339 10455 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
10456 from:
10457
f7ef9339 10458 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 10459
613e1657
KB
10460 This document is only a draft and is subject to change. (So beware.)
10461
f7ef9339 10462 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
10463 determined empirically by examining 64-bit ELF files produced by
10464 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
10465
10466 - Kevin, July 16, 2002
613e1657
KB
10467 ] */
10468
10469static LONGEST
c764a876 10470read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 10471{
fe1b8b76 10472 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 10473
dd373385 10474 if (length == 0xffffffff)
613e1657 10475 {
fe1b8b76 10476 length = bfd_get_64 (abfd, buf + 4);
613e1657 10477 *bytes_read = 12;
613e1657 10478 }
dd373385 10479 else if (length == 0)
f7ef9339 10480 {
dd373385 10481 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 10482 length = bfd_get_64 (abfd, buf);
f7ef9339 10483 *bytes_read = 8;
f7ef9339 10484 }
613e1657
KB
10485 else
10486 {
10487 *bytes_read = 4;
613e1657
KB
10488 }
10489
c764a876
DE
10490 return length;
10491}
dd373385 10492
c764a876
DE
10493/* Cover function for read_initial_length.
10494 Returns the length of the object at BUF, and stores the size of the
10495 initial length in *BYTES_READ and stores the size that offsets will be in
10496 *OFFSET_SIZE.
10497 If the initial length size is not equivalent to that specified in
10498 CU_HEADER then issue a complaint.
10499 This is useful when reading non-comp-unit headers. */
dd373385 10500
c764a876
DE
10501static LONGEST
10502read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10503 const struct comp_unit_head *cu_header,
10504 unsigned int *bytes_read,
10505 unsigned int *offset_size)
10506{
10507 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10508
10509 gdb_assert (cu_header->initial_length_size == 4
10510 || cu_header->initial_length_size == 8
10511 || cu_header->initial_length_size == 12);
10512
10513 if (cu_header->initial_length_size != *bytes_read)
10514 complaint (&symfile_complaints,
10515 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 10516
c764a876 10517 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 10518 return length;
613e1657
KB
10519}
10520
10521/* Read an offset from the data stream. The size of the offset is
917c78fc 10522 given by cu_header->offset_size. */
613e1657
KB
10523
10524static LONGEST
fe1b8b76 10525read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 10526 unsigned int *bytes_read)
c764a876
DE
10527{
10528 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 10529
c764a876
DE
10530 *bytes_read = cu_header->offset_size;
10531 return offset;
10532}
10533
10534/* Read an offset from the data stream. */
10535
10536static LONGEST
10537read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
10538{
10539 LONGEST retval = 0;
10540
c764a876 10541 switch (offset_size)
613e1657
KB
10542 {
10543 case 4:
fe1b8b76 10544 retval = bfd_get_32 (abfd, buf);
613e1657
KB
10545 break;
10546 case 8:
fe1b8b76 10547 retval = bfd_get_64 (abfd, buf);
613e1657
KB
10548 break;
10549 default:
8e65ff28 10550 internal_error (__FILE__, __LINE__,
c764a876 10551 _("read_offset_1: bad switch [in module %s]"),
659b0389 10552 bfd_get_filename (abfd));
613e1657
KB
10553 }
10554
917c78fc 10555 return retval;
613e1657
KB
10556}
10557
fe1b8b76
JB
10558static gdb_byte *
10559read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
10560{
10561 /* If the size of a host char is 8 bits, we can return a pointer
10562 to the buffer, otherwise we have to copy the data to a buffer
10563 allocated on the temporary obstack. */
4bdf3d34 10564 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 10565 return buf;
c906108c
SS
10566}
10567
10568static char *
9b1c24c8 10569read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
10570{
10571 /* If the size of a host char is 8 bits, we can return a pointer
10572 to the string, otherwise we have to copy the string to a buffer
10573 allocated on the temporary obstack. */
4bdf3d34 10574 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
10575 if (*buf == '\0')
10576 {
10577 *bytes_read_ptr = 1;
10578 return NULL;
10579 }
fe1b8b76
JB
10580 *bytes_read_ptr = strlen ((char *) buf) + 1;
10581 return (char *) buf;
4bdf3d34
JJ
10582}
10583
10584static char *
cf2c3c16 10585read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 10586{
be391dca 10587 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 10588 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
10589 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10590 bfd_get_filename (abfd));
dce234bc 10591 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
10592 error (_("DW_FORM_strp pointing outside of "
10593 ".debug_str section [in module %s]"),
10594 bfd_get_filename (abfd));
4bdf3d34 10595 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 10596 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 10597 return NULL;
dce234bc 10598 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
10599}
10600
cf2c3c16
TT
10601static char *
10602read_indirect_string (bfd *abfd, gdb_byte *buf,
10603 const struct comp_unit_head *cu_header,
10604 unsigned int *bytes_read_ptr)
10605{
10606 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10607
10608 return read_indirect_string_at_offset (abfd, str_offset);
10609}
10610
ce5d95e1 10611static unsigned long
fe1b8b76 10612read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10613{
ce5d95e1
JB
10614 unsigned long result;
10615 unsigned int num_read;
c906108c
SS
10616 int i, shift;
10617 unsigned char byte;
10618
10619 result = 0;
10620 shift = 0;
10621 num_read = 0;
10622 i = 0;
10623 while (1)
10624 {
fe1b8b76 10625 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10626 buf++;
10627 num_read++;
ce5d95e1 10628 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
10629 if ((byte & 128) == 0)
10630 {
10631 break;
10632 }
10633 shift += 7;
10634 }
10635 *bytes_read_ptr = num_read;
10636 return result;
10637}
10638
ce5d95e1 10639static long
fe1b8b76 10640read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 10641{
ce5d95e1 10642 long result;
77e0b926 10643 int i, shift, num_read;
c906108c
SS
10644 unsigned char byte;
10645
10646 result = 0;
10647 shift = 0;
c906108c
SS
10648 num_read = 0;
10649 i = 0;
10650 while (1)
10651 {
fe1b8b76 10652 byte = bfd_get_8 (abfd, buf);
c906108c
SS
10653 buf++;
10654 num_read++;
ce5d95e1 10655 result |= ((long)(byte & 127) << shift);
c906108c
SS
10656 shift += 7;
10657 if ((byte & 128) == 0)
10658 {
10659 break;
10660 }
10661 }
77e0b926
DJ
10662 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10663 result |= -(((long)1) << shift);
c906108c
SS
10664 *bytes_read_ptr = num_read;
10665 return result;
10666}
10667
4bb7a0a7
DJ
10668/* Return a pointer to just past the end of an LEB128 number in BUF. */
10669
fe1b8b76
JB
10670static gdb_byte *
10671skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
10672{
10673 int byte;
10674
10675 while (1)
10676 {
fe1b8b76 10677 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
10678 buf++;
10679 if ((byte & 128) == 0)
10680 return buf;
10681 }
10682}
10683
c906108c 10684static void
e142c38c 10685set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
10686{
10687 switch (lang)
10688 {
10689 case DW_LANG_C89:
76bee0cc 10690 case DW_LANG_C99:
c906108c 10691 case DW_LANG_C:
e142c38c 10692 cu->language = language_c;
c906108c
SS
10693 break;
10694 case DW_LANG_C_plus_plus:
e142c38c 10695 cu->language = language_cplus;
c906108c 10696 break;
6aecb9c2
JB
10697 case DW_LANG_D:
10698 cu->language = language_d;
10699 break;
c906108c
SS
10700 case DW_LANG_Fortran77:
10701 case DW_LANG_Fortran90:
b21b22e0 10702 case DW_LANG_Fortran95:
e142c38c 10703 cu->language = language_fortran;
c906108c
SS
10704 break;
10705 case DW_LANG_Mips_Assembler:
e142c38c 10706 cu->language = language_asm;
c906108c 10707 break;
bebd888e 10708 case DW_LANG_Java:
e142c38c 10709 cu->language = language_java;
bebd888e 10710 break;
c906108c 10711 case DW_LANG_Ada83:
8aaf0b47 10712 case DW_LANG_Ada95:
bc5f45f8
JB
10713 cu->language = language_ada;
10714 break;
72019c9c
GM
10715 case DW_LANG_Modula2:
10716 cu->language = language_m2;
10717 break;
fe8e67fd
PM
10718 case DW_LANG_Pascal83:
10719 cu->language = language_pascal;
10720 break;
22566fbd
DJ
10721 case DW_LANG_ObjC:
10722 cu->language = language_objc;
10723 break;
c906108c
SS
10724 case DW_LANG_Cobol74:
10725 case DW_LANG_Cobol85:
c906108c 10726 default:
e142c38c 10727 cu->language = language_minimal;
c906108c
SS
10728 break;
10729 }
e142c38c 10730 cu->language_defn = language_def (cu->language);
c906108c
SS
10731}
10732
10733/* Return the named attribute or NULL if not there. */
10734
10735static struct attribute *
e142c38c 10736dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
10737{
10738 unsigned int i;
10739 struct attribute *spec = NULL;
10740
10741 for (i = 0; i < die->num_attrs; ++i)
10742 {
10743 if (die->attrs[i].name == name)
10b3939b 10744 return &die->attrs[i];
c906108c
SS
10745 if (die->attrs[i].name == DW_AT_specification
10746 || die->attrs[i].name == DW_AT_abstract_origin)
10747 spec = &die->attrs[i];
10748 }
c906108c 10749
10b3939b 10750 if (spec)
f2f0e013
DJ
10751 {
10752 die = follow_die_ref (die, spec, &cu);
10753 return dwarf2_attr (die, name, cu);
10754 }
c5aa993b 10755
c906108c
SS
10756 return NULL;
10757}
10758
348e048f
DE
10759/* Return the named attribute or NULL if not there,
10760 but do not follow DW_AT_specification, etc.
10761 This is for use in contexts where we're reading .debug_types dies.
10762 Following DW_AT_specification, DW_AT_abstract_origin will take us
10763 back up the chain, and we want to go down. */
10764
10765static struct attribute *
10766dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10767 struct dwarf2_cu *cu)
10768{
10769 unsigned int i;
10770
10771 for (i = 0; i < die->num_attrs; ++i)
10772 if (die->attrs[i].name == name)
10773 return &die->attrs[i];
10774
10775 return NULL;
10776}
10777
05cf31d1
JB
10778/* Return non-zero iff the attribute NAME is defined for the given DIE,
10779 and holds a non-zero value. This function should only be used for
2dc7f7b3 10780 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
10781
10782static int
10783dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10784{
10785 struct attribute *attr = dwarf2_attr (die, name, cu);
10786
10787 return (attr && DW_UNSND (attr));
10788}
10789
3ca72b44 10790static int
e142c38c 10791die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 10792{
05cf31d1
JB
10793 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10794 which value is non-zero. However, we have to be careful with
10795 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10796 (via dwarf2_flag_true_p) follows this attribute. So we may
10797 end up accidently finding a declaration attribute that belongs
10798 to a different DIE referenced by the specification attribute,
10799 even though the given DIE does not have a declaration attribute. */
10800 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10801 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
10802}
10803
63d06c5c 10804/* Return the die giving the specification for DIE, if there is
f2f0e013 10805 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
10806 containing the return value on output. If there is no
10807 specification, but there is an abstract origin, that is
10808 returned. */
63d06c5c
DC
10809
10810static struct die_info *
f2f0e013 10811die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 10812{
f2f0e013
DJ
10813 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10814 *spec_cu);
63d06c5c 10815
edb3359d
DJ
10816 if (spec_attr == NULL)
10817 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10818
63d06c5c
DC
10819 if (spec_attr == NULL)
10820 return NULL;
10821 else
f2f0e013 10822 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 10823}
c906108c 10824
debd256d 10825/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
10826 refers to.
10827 NOTE: This is also used as a "cleanup" function. */
10828
debd256d
JB
10829static void
10830free_line_header (struct line_header *lh)
10831{
10832 if (lh->standard_opcode_lengths)
a8bc7b56 10833 xfree (lh->standard_opcode_lengths);
debd256d
JB
10834
10835 /* Remember that all the lh->file_names[i].name pointers are
10836 pointers into debug_line_buffer, and don't need to be freed. */
10837 if (lh->file_names)
a8bc7b56 10838 xfree (lh->file_names);
debd256d
JB
10839
10840 /* Similarly for the include directory names. */
10841 if (lh->include_dirs)
a8bc7b56 10842 xfree (lh->include_dirs);
debd256d 10843
a8bc7b56 10844 xfree (lh);
debd256d
JB
10845}
10846
debd256d 10847/* Add an entry to LH's include directory table. */
ae2de4f8 10848
debd256d
JB
10849static void
10850add_include_dir (struct line_header *lh, char *include_dir)
c906108c 10851{
debd256d
JB
10852 /* Grow the array if necessary. */
10853 if (lh->include_dirs_size == 0)
c5aa993b 10854 {
debd256d
JB
10855 lh->include_dirs_size = 1; /* for testing */
10856 lh->include_dirs = xmalloc (lh->include_dirs_size
10857 * sizeof (*lh->include_dirs));
10858 }
10859 else if (lh->num_include_dirs >= lh->include_dirs_size)
10860 {
10861 lh->include_dirs_size *= 2;
10862 lh->include_dirs = xrealloc (lh->include_dirs,
10863 (lh->include_dirs_size
10864 * sizeof (*lh->include_dirs)));
c5aa993b 10865 }
c906108c 10866
debd256d
JB
10867 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10868}
6e70227d 10869
debd256d 10870/* Add an entry to LH's file name table. */
ae2de4f8 10871
debd256d
JB
10872static void
10873add_file_name (struct line_header *lh,
10874 char *name,
10875 unsigned int dir_index,
10876 unsigned int mod_time,
10877 unsigned int length)
10878{
10879 struct file_entry *fe;
10880
10881 /* Grow the array if necessary. */
10882 if (lh->file_names_size == 0)
10883 {
10884 lh->file_names_size = 1; /* for testing */
10885 lh->file_names = xmalloc (lh->file_names_size
10886 * sizeof (*lh->file_names));
10887 }
10888 else if (lh->num_file_names >= lh->file_names_size)
10889 {
10890 lh->file_names_size *= 2;
10891 lh->file_names = xrealloc (lh->file_names,
10892 (lh->file_names_size
10893 * sizeof (*lh->file_names)));
10894 }
10895
10896 fe = &lh->file_names[lh->num_file_names++];
10897 fe->name = name;
10898 fe->dir_index = dir_index;
10899 fe->mod_time = mod_time;
10900 fe->length = length;
aaa75496 10901 fe->included_p = 0;
cb1df416 10902 fe->symtab = NULL;
debd256d 10903}
6e70227d 10904
debd256d 10905/* Read the statement program header starting at OFFSET in
6502dd73
DJ
10906 .debug_line, according to the endianness of ABFD. Return a pointer
10907 to a struct line_header, allocated using xmalloc.
debd256d
JB
10908
10909 NOTE: the strings in the include directory and file name tables of
10910 the returned object point into debug_line_buffer, and must not be
10911 freed. */
ae2de4f8 10912
debd256d
JB
10913static struct line_header *
10914dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 10915 struct dwarf2_cu *cu)
debd256d
JB
10916{
10917 struct cleanup *back_to;
10918 struct line_header *lh;
fe1b8b76 10919 gdb_byte *line_ptr;
c764a876 10920 unsigned int bytes_read, offset_size;
debd256d
JB
10921 int i;
10922 char *cur_dir, *cur_file;
10923
be391dca 10924 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 10925 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 10926 {
e2e0b3e5 10927 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
10928 return 0;
10929 }
10930
a738430d
MK
10931 /* Make sure that at least there's room for the total_length field.
10932 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 10933 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 10934 {
4d3c2250 10935 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10936 return 0;
10937 }
10938
10939 lh = xmalloc (sizeof (*lh));
10940 memset (lh, 0, sizeof (*lh));
10941 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10942 (void *) lh);
10943
dce234bc 10944 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 10945
a738430d 10946 /* Read in the header. */
6e70227d 10947 lh->total_length =
c764a876
DE
10948 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10949 &bytes_read, &offset_size);
debd256d 10950 line_ptr += bytes_read;
dce234bc
PP
10951 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10952 + dwarf2_per_objfile->line.size))
debd256d 10953 {
4d3c2250 10954 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
10955 return 0;
10956 }
10957 lh->statement_program_end = line_ptr + lh->total_length;
10958 lh->version = read_2_bytes (abfd, line_ptr);
10959 line_ptr += 2;
c764a876
DE
10960 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10961 line_ptr += offset_size;
debd256d
JB
10962 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10963 line_ptr += 1;
2dc7f7b3
TT
10964 if (lh->version >= 4)
10965 {
10966 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10967 line_ptr += 1;
10968 }
10969 else
10970 lh->maximum_ops_per_instruction = 1;
10971
10972 if (lh->maximum_ops_per_instruction == 0)
10973 {
10974 lh->maximum_ops_per_instruction = 1;
10975 complaint (&symfile_complaints,
3e43a32a
MS
10976 _("invalid maximum_ops_per_instruction "
10977 "in `.debug_line' section"));
2dc7f7b3
TT
10978 }
10979
debd256d
JB
10980 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10981 line_ptr += 1;
10982 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10983 line_ptr += 1;
10984 lh->line_range = read_1_byte (abfd, line_ptr);
10985 line_ptr += 1;
10986 lh->opcode_base = read_1_byte (abfd, line_ptr);
10987 line_ptr += 1;
10988 lh->standard_opcode_lengths
fe1b8b76 10989 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
10990
10991 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10992 for (i = 1; i < lh->opcode_base; ++i)
10993 {
10994 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10995 line_ptr += 1;
10996 }
10997
a738430d 10998 /* Read directory table. */
9b1c24c8 10999 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11000 {
11001 line_ptr += bytes_read;
11002 add_include_dir (lh, cur_dir);
11003 }
11004 line_ptr += bytes_read;
11005
a738430d 11006 /* Read file name table. */
9b1c24c8 11007 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
11008 {
11009 unsigned int dir_index, mod_time, length;
11010
11011 line_ptr += bytes_read;
11012 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11013 line_ptr += bytes_read;
11014 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11015 line_ptr += bytes_read;
11016 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11017 line_ptr += bytes_read;
11018
11019 add_file_name (lh, cur_file, dir_index, mod_time, length);
11020 }
11021 line_ptr += bytes_read;
6e70227d 11022 lh->statement_program_start = line_ptr;
debd256d 11023
dce234bc
PP
11024 if (line_ptr > (dwarf2_per_objfile->line.buffer
11025 + dwarf2_per_objfile->line.size))
4d3c2250 11026 complaint (&symfile_complaints,
3e43a32a
MS
11027 _("line number info header doesn't "
11028 "fit in `.debug_line' section"));
debd256d
JB
11029
11030 discard_cleanups (back_to);
11031 return lh;
11032}
c906108c 11033
5fb290d7
DJ
11034/* This function exists to work around a bug in certain compilers
11035 (particularly GCC 2.95), in which the first line number marker of a
11036 function does not show up until after the prologue, right before
11037 the second line number marker. This function shifts ADDRESS down
11038 to the beginning of the function if necessary, and is called on
11039 addresses passed to record_line. */
11040
11041static CORE_ADDR
e142c38c 11042check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
11043{
11044 struct function_range *fn;
11045
11046 /* Find the function_range containing address. */
e142c38c 11047 if (!cu->first_fn)
5fb290d7
DJ
11048 return address;
11049
e142c38c
DJ
11050 if (!cu->cached_fn)
11051 cu->cached_fn = cu->first_fn;
5fb290d7 11052
e142c38c 11053 fn = cu->cached_fn;
5fb290d7
DJ
11054 while (fn)
11055 if (fn->lowpc <= address && fn->highpc > address)
11056 goto found;
11057 else
11058 fn = fn->next;
11059
e142c38c
DJ
11060 fn = cu->first_fn;
11061 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
11062 if (fn->lowpc <= address && fn->highpc > address)
11063 goto found;
11064 else
11065 fn = fn->next;
11066
11067 return address;
11068
11069 found:
11070 if (fn->seen_line)
11071 return address;
11072 if (address != fn->lowpc)
4d3c2250 11073 complaint (&symfile_complaints,
e2e0b3e5 11074 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 11075 (unsigned long) address, fn->name);
5fb290d7
DJ
11076 fn->seen_line = 1;
11077 return fn->lowpc;
11078}
11079
c6da4cef
DE
11080/* Subroutine of dwarf_decode_lines to simplify it.
11081 Return the file name of the psymtab for included file FILE_INDEX
11082 in line header LH of PST.
11083 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11084 If space for the result is malloc'd, it will be freed by a cleanup.
11085 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11086
11087static char *
11088psymtab_include_file_name (const struct line_header *lh, int file_index,
11089 const struct partial_symtab *pst,
11090 const char *comp_dir)
11091{
11092 const struct file_entry fe = lh->file_names [file_index];
11093 char *include_name = fe.name;
11094 char *include_name_to_compare = include_name;
11095 char *dir_name = NULL;
72b9f47f
TT
11096 const char *pst_filename;
11097 char *copied_name = NULL;
c6da4cef
DE
11098 int file_is_pst;
11099
11100 if (fe.dir_index)
11101 dir_name = lh->include_dirs[fe.dir_index - 1];
11102
11103 if (!IS_ABSOLUTE_PATH (include_name)
11104 && (dir_name != NULL || comp_dir != NULL))
11105 {
11106 /* Avoid creating a duplicate psymtab for PST.
11107 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11108 Before we do the comparison, however, we need to account
11109 for DIR_NAME and COMP_DIR.
11110 First prepend dir_name (if non-NULL). If we still don't
11111 have an absolute path prepend comp_dir (if non-NULL).
11112 However, the directory we record in the include-file's
11113 psymtab does not contain COMP_DIR (to match the
11114 corresponding symtab(s)).
11115
11116 Example:
11117
11118 bash$ cd /tmp
11119 bash$ gcc -g ./hello.c
11120 include_name = "hello.c"
11121 dir_name = "."
11122 DW_AT_comp_dir = comp_dir = "/tmp"
11123 DW_AT_name = "./hello.c" */
11124
11125 if (dir_name != NULL)
11126 {
11127 include_name = concat (dir_name, SLASH_STRING,
11128 include_name, (char *)NULL);
11129 include_name_to_compare = include_name;
11130 make_cleanup (xfree, include_name);
11131 }
11132 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11133 {
11134 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11135 include_name, (char *)NULL);
11136 }
11137 }
11138
11139 pst_filename = pst->filename;
11140 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11141 {
72b9f47f
TT
11142 copied_name = concat (pst->dirname, SLASH_STRING,
11143 pst_filename, (char *)NULL);
11144 pst_filename = copied_name;
c6da4cef
DE
11145 }
11146
1e3fad37 11147 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
11148
11149 if (include_name_to_compare != include_name)
11150 xfree (include_name_to_compare);
72b9f47f
TT
11151 if (copied_name != NULL)
11152 xfree (copied_name);
c6da4cef
DE
11153
11154 if (file_is_pst)
11155 return NULL;
11156 return include_name;
11157}
11158
c91513d8
PP
11159/* Ignore this record_line request. */
11160
11161static void
11162noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11163{
11164 return;
11165}
11166
aaa75496
JB
11167/* Decode the Line Number Program (LNP) for the given line_header
11168 structure and CU. The actual information extracted and the type
11169 of structures created from the LNP depends on the value of PST.
11170
11171 1. If PST is NULL, then this procedure uses the data from the program
11172 to create all necessary symbol tables, and their linetables.
6e70227d 11173
aaa75496
JB
11174 2. If PST is not NULL, this procedure reads the program to determine
11175 the list of files included by the unit represented by PST, and
c6da4cef
DE
11176 builds all the associated partial symbol tables.
11177
11178 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11179 It is used for relative paths in the line table.
11180 NOTE: When processing partial symtabs (pst != NULL),
11181 comp_dir == pst->dirname.
11182
11183 NOTE: It is important that psymtabs have the same file name (via strcmp)
11184 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11185 symtab we don't use it in the name of the psymtabs we create.
11186 E.g. expand_line_sal requires this when finding psymtabs to expand.
11187 A good testcase for this is mb-inline.exp. */
debd256d 11188
c906108c 11189static void
72b9f47f 11190dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
aaa75496 11191 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 11192{
a8c50c1f 11193 gdb_byte *line_ptr, *extended_end;
fe1b8b76 11194 gdb_byte *line_end;
a8c50c1f 11195 unsigned int bytes_read, extended_len;
c906108c 11196 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
11197 CORE_ADDR baseaddr;
11198 struct objfile *objfile = cu->objfile;
fbf65064 11199 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 11200 const int decode_for_pst_p = (pst != NULL);
cb1df416 11201 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
c91513d8
PP
11202 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11203 = record_line;
e142c38c
DJ
11204
11205 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11206
debd256d
JB
11207 line_ptr = lh->statement_program_start;
11208 line_end = lh->statement_program_end;
c906108c
SS
11209
11210 /* Read the statement sequences until there's nothing left. */
11211 while (line_ptr < line_end)
11212 {
11213 /* state machine registers */
11214 CORE_ADDR address = 0;
11215 unsigned int file = 1;
11216 unsigned int line = 1;
11217 unsigned int column = 0;
debd256d 11218 int is_stmt = lh->default_is_stmt;
c906108c
SS
11219 int basic_block = 0;
11220 int end_sequence = 0;
fbf65064 11221 CORE_ADDR addr;
2dc7f7b3 11222 unsigned char op_index = 0;
c906108c 11223
aaa75496 11224 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 11225 {
aaa75496 11226 /* Start a subfile for the current file of the state machine. */
debd256d
JB
11227 /* lh->include_dirs and lh->file_names are 0-based, but the
11228 directory and file name numbers in the statement program
11229 are 1-based. */
11230 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 11231 char *dir = NULL;
a738430d 11232
debd256d
JB
11233 if (fe->dir_index)
11234 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
11235
11236 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
11237 }
11238
a738430d 11239 /* Decode the table. */
c5aa993b 11240 while (!end_sequence)
c906108c
SS
11241 {
11242 op_code = read_1_byte (abfd, line_ptr);
11243 line_ptr += 1;
59205f5a
JB
11244 if (line_ptr > line_end)
11245 {
11246 dwarf2_debug_line_missing_end_sequence_complaint ();
11247 break;
11248 }
9aa1fe7e 11249
debd256d 11250 if (op_code >= lh->opcode_base)
6e70227d 11251 {
a738430d 11252 /* Special operand. */
debd256d 11253 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
11254 address += (((op_index + (adj_opcode / lh->line_range))
11255 / lh->maximum_ops_per_instruction)
11256 * lh->minimum_instruction_length);
11257 op_index = ((op_index + (adj_opcode / lh->line_range))
11258 % lh->maximum_ops_per_instruction);
debd256d 11259 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 11260 if (lh->num_file_names < file || file == 0)
25e43795 11261 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
11262 /* For now we ignore lines not starting on an
11263 instruction boundary. */
11264 else if (op_index == 0)
25e43795
DJ
11265 {
11266 lh->file_names[file - 1].included_p = 1;
ca5f395d 11267 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11268 {
11269 if (last_subfile != current_subfile)
11270 {
11271 addr = gdbarch_addr_bits_remove (gdbarch, address);
11272 if (last_subfile)
c91513d8 11273 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11274 last_subfile = current_subfile;
11275 }
25e43795 11276 /* Append row to matrix using current values. */
fbf65064
UW
11277 addr = check_cu_functions (address, cu);
11278 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 11279 (*p_record_line) (current_subfile, line, addr);
366da635 11280 }
25e43795 11281 }
ca5f395d 11282 basic_block = 0;
9aa1fe7e
GK
11283 }
11284 else switch (op_code)
c906108c
SS
11285 {
11286 case DW_LNS_extended_op:
3e43a32a
MS
11287 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11288 &bytes_read);
473b7be6 11289 line_ptr += bytes_read;
a8c50c1f 11290 extended_end = line_ptr + extended_len;
c906108c
SS
11291 extended_op = read_1_byte (abfd, line_ptr);
11292 line_ptr += 1;
11293 switch (extended_op)
11294 {
11295 case DW_LNE_end_sequence:
c91513d8 11296 p_record_line = record_line;
c906108c 11297 end_sequence = 1;
c906108c
SS
11298 break;
11299 case DW_LNE_set_address:
e7c27a73 11300 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
11301
11302 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11303 {
11304 /* This line table is for a function which has been
11305 GCd by the linker. Ignore it. PR gdb/12528 */
11306
11307 long line_offset
11308 = line_ptr - dwarf2_per_objfile->line.buffer;
11309
11310 complaint (&symfile_complaints,
11311 _(".debug_line address at offset 0x%lx is 0 "
11312 "[in module %s]"),
11313 line_offset, cu->objfile->name);
11314 p_record_line = noop_record_line;
11315 }
11316
2dc7f7b3 11317 op_index = 0;
107d2387
AC
11318 line_ptr += bytes_read;
11319 address += baseaddr;
c906108c
SS
11320 break;
11321 case DW_LNE_define_file:
debd256d
JB
11322 {
11323 char *cur_file;
11324 unsigned int dir_index, mod_time, length;
6e70227d 11325
3e43a32a
MS
11326 cur_file = read_direct_string (abfd, line_ptr,
11327 &bytes_read);
debd256d
JB
11328 line_ptr += bytes_read;
11329 dir_index =
11330 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11331 line_ptr += bytes_read;
11332 mod_time =
11333 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11334 line_ptr += bytes_read;
11335 length =
11336 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11337 line_ptr += bytes_read;
11338 add_file_name (lh, cur_file, dir_index, mod_time, length);
11339 }
c906108c 11340 break;
d0c6ba3d
CC
11341 case DW_LNE_set_discriminator:
11342 /* The discriminator is not interesting to the debugger;
11343 just ignore it. */
11344 line_ptr = extended_end;
11345 break;
c906108c 11346 default:
4d3c2250 11347 complaint (&symfile_complaints,
e2e0b3e5 11348 _("mangled .debug_line section"));
debd256d 11349 return;
c906108c 11350 }
a8c50c1f
DJ
11351 /* Make sure that we parsed the extended op correctly. If e.g.
11352 we expected a different address size than the producer used,
11353 we may have read the wrong number of bytes. */
11354 if (line_ptr != extended_end)
11355 {
11356 complaint (&symfile_complaints,
11357 _("mangled .debug_line section"));
11358 return;
11359 }
c906108c
SS
11360 break;
11361 case DW_LNS_copy:
59205f5a 11362 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11363 dwarf2_debug_line_missing_file_complaint ();
11364 else
366da635 11365 {
25e43795 11366 lh->file_names[file - 1].included_p = 1;
ca5f395d 11367 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
11368 {
11369 if (last_subfile != current_subfile)
11370 {
11371 addr = gdbarch_addr_bits_remove (gdbarch, address);
11372 if (last_subfile)
c91513d8 11373 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
11374 last_subfile = current_subfile;
11375 }
11376 addr = check_cu_functions (address, cu);
11377 addr = gdbarch_addr_bits_remove (gdbarch, addr);
c91513d8 11378 (*p_record_line) (current_subfile, line, addr);
fbf65064 11379 }
366da635 11380 }
c906108c
SS
11381 basic_block = 0;
11382 break;
11383 case DW_LNS_advance_pc:
2dc7f7b3
TT
11384 {
11385 CORE_ADDR adjust
11386 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11387
11388 address += (((op_index + adjust)
11389 / lh->maximum_ops_per_instruction)
11390 * lh->minimum_instruction_length);
11391 op_index = ((op_index + adjust)
11392 % lh->maximum_ops_per_instruction);
11393 line_ptr += bytes_read;
11394 }
c906108c
SS
11395 break;
11396 case DW_LNS_advance_line:
11397 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11398 line_ptr += bytes_read;
11399 break;
11400 case DW_LNS_set_file:
debd256d 11401 {
a738430d
MK
11402 /* The arrays lh->include_dirs and lh->file_names are
11403 0-based, but the directory and file name numbers in
11404 the statement program are 1-based. */
debd256d 11405 struct file_entry *fe;
4f1520fb 11406 char *dir = NULL;
a738430d 11407
debd256d
JB
11408 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11409 line_ptr += bytes_read;
59205f5a 11410 if (lh->num_file_names < file || file == 0)
25e43795
DJ
11411 dwarf2_debug_line_missing_file_complaint ();
11412 else
11413 {
11414 fe = &lh->file_names[file - 1];
11415 if (fe->dir_index)
11416 dir = lh->include_dirs[fe->dir_index - 1];
11417 if (!decode_for_pst_p)
11418 {
11419 last_subfile = current_subfile;
11420 dwarf2_start_subfile (fe->name, dir, comp_dir);
11421 }
11422 }
debd256d 11423 }
c906108c
SS
11424 break;
11425 case DW_LNS_set_column:
11426 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11427 line_ptr += bytes_read;
11428 break;
11429 case DW_LNS_negate_stmt:
11430 is_stmt = (!is_stmt);
11431 break;
11432 case DW_LNS_set_basic_block:
11433 basic_block = 1;
11434 break;
c2c6d25f
JM
11435 /* Add to the address register of the state machine the
11436 address increment value corresponding to special opcode
a738430d
MK
11437 255. I.e., this value is scaled by the minimum
11438 instruction length since special opcode 255 would have
b021a221 11439 scaled the increment. */
c906108c 11440 case DW_LNS_const_add_pc:
2dc7f7b3
TT
11441 {
11442 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11443
11444 address += (((op_index + adjust)
11445 / lh->maximum_ops_per_instruction)
11446 * lh->minimum_instruction_length);
11447 op_index = ((op_index + adjust)
11448 % lh->maximum_ops_per_instruction);
11449 }
c906108c
SS
11450 break;
11451 case DW_LNS_fixed_advance_pc:
11452 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 11453 op_index = 0;
c906108c
SS
11454 line_ptr += 2;
11455 break;
9aa1fe7e 11456 default:
a738430d
MK
11457 {
11458 /* Unknown standard opcode, ignore it. */
9aa1fe7e 11459 int i;
a738430d 11460
debd256d 11461 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
11462 {
11463 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11464 line_ptr += bytes_read;
11465 }
11466 }
c906108c
SS
11467 }
11468 }
59205f5a
JB
11469 if (lh->num_file_names < file || file == 0)
11470 dwarf2_debug_line_missing_file_complaint ();
11471 else
11472 {
11473 lh->file_names[file - 1].included_p = 1;
11474 if (!decode_for_pst_p)
fbf65064
UW
11475 {
11476 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 11477 (*p_record_line) (current_subfile, 0, addr);
fbf65064 11478 }
59205f5a 11479 }
c906108c 11480 }
aaa75496
JB
11481
11482 if (decode_for_pst_p)
11483 {
11484 int file_index;
11485
11486 /* Now that we're done scanning the Line Header Program, we can
11487 create the psymtab of each included file. */
11488 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11489 if (lh->file_names[file_index].included_p == 1)
11490 {
c6da4cef
DE
11491 char *include_name =
11492 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11493 if (include_name != NULL)
aaa75496
JB
11494 dwarf2_create_include_psymtab (include_name, pst, objfile);
11495 }
11496 }
cb1df416
DJ
11497 else
11498 {
11499 /* Make sure a symtab is created for every file, even files
11500 which contain only variables (i.e. no code with associated
11501 line numbers). */
11502
11503 int i;
11504 struct file_entry *fe;
11505
11506 for (i = 0; i < lh->num_file_names; i++)
11507 {
11508 char *dir = NULL;
9a619af0 11509
cb1df416
DJ
11510 fe = &lh->file_names[i];
11511 if (fe->dir_index)
11512 dir = lh->include_dirs[fe->dir_index - 1];
11513 dwarf2_start_subfile (fe->name, dir, comp_dir);
11514
11515 /* Skip the main file; we don't need it, and it must be
11516 allocated last, so that it will show up before the
11517 non-primary symtabs in the objfile's symtab list. */
11518 if (current_subfile == first_subfile)
11519 continue;
11520
11521 if (current_subfile->symtab == NULL)
11522 current_subfile->symtab = allocate_symtab (current_subfile->name,
11523 cu->objfile);
11524 fe->symtab = current_subfile->symtab;
11525 }
11526 }
c906108c
SS
11527}
11528
11529/* Start a subfile for DWARF. FILENAME is the name of the file and
11530 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
11531 or NULL if not known. COMP_DIR is the compilation directory for the
11532 linetable's compilation unit or NULL if not known.
c906108c
SS
11533 This routine tries to keep line numbers from identical absolute and
11534 relative file names in a common subfile.
11535
11536 Using the `list' example from the GDB testsuite, which resides in
11537 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11538 of /srcdir/list0.c yields the following debugging information for list0.c:
11539
c5aa993b
JM
11540 DW_AT_name: /srcdir/list0.c
11541 DW_AT_comp_dir: /compdir
357e46e7 11542 files.files[0].name: list0.h
c5aa993b 11543 files.files[0].dir: /srcdir
357e46e7 11544 files.files[1].name: list0.c
c5aa993b 11545 files.files[1].dir: /srcdir
c906108c
SS
11546
11547 The line number information for list0.c has to end up in a single
4f1520fb
FR
11548 subfile, so that `break /srcdir/list0.c:1' works as expected.
11549 start_subfile will ensure that this happens provided that we pass the
11550 concatenation of files.files[1].dir and files.files[1].name as the
11551 subfile's name. */
c906108c
SS
11552
11553static void
3e43a32a
MS
11554dwarf2_start_subfile (char *filename, const char *dirname,
11555 const char *comp_dir)
c906108c 11556{
4f1520fb
FR
11557 char *fullname;
11558
11559 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11560 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11561 second argument to start_subfile. To be consistent, we do the
11562 same here. In order not to lose the line information directory,
11563 we concatenate it to the filename when it makes sense.
11564 Note that the Dwarf3 standard says (speaking of filenames in line
11565 information): ``The directory index is ignored for file names
11566 that represent full path names''. Thus ignoring dirname in the
11567 `else' branch below isn't an issue. */
c906108c 11568
d5166ae1 11569 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
11570 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11571 else
11572 fullname = filename;
c906108c 11573
4f1520fb
FR
11574 start_subfile (fullname, comp_dir);
11575
11576 if (fullname != filename)
11577 xfree (fullname);
c906108c
SS
11578}
11579
4c2df51b
DJ
11580static void
11581var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 11582 struct dwarf2_cu *cu)
4c2df51b 11583{
e7c27a73
DJ
11584 struct objfile *objfile = cu->objfile;
11585 struct comp_unit_head *cu_header = &cu->header;
11586
4c2df51b
DJ
11587 /* NOTE drow/2003-01-30: There used to be a comment and some special
11588 code here to turn a symbol with DW_AT_external and a
11589 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11590 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11591 with some versions of binutils) where shared libraries could have
11592 relocations against symbols in their debug information - the
11593 minimal symbol would have the right address, but the debug info
11594 would not. It's no longer necessary, because we will explicitly
11595 apply relocations when we read in the debug information now. */
11596
11597 /* A DW_AT_location attribute with no contents indicates that a
11598 variable has been optimized away. */
11599 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11600 {
11601 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11602 return;
11603 }
11604
11605 /* Handle one degenerate form of location expression specially, to
11606 preserve GDB's previous behavior when section offsets are
11607 specified. If this is just a DW_OP_addr then mark this symbol
11608 as LOC_STATIC. */
11609
11610 if (attr_form_is_block (attr)
11611 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11612 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11613 {
891d2f0b 11614 unsigned int dummy;
4c2df51b
DJ
11615
11616 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 11617 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 11618 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
11619 fixup_symbol_section (sym, objfile);
11620 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11621 SYMBOL_SECTION (sym));
4c2df51b
DJ
11622 return;
11623 }
11624
11625 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11626 expression evaluator, and use LOC_COMPUTED only when necessary
11627 (i.e. when the value of a register or memory location is
11628 referenced, or a thread-local block, etc.). Then again, it might
11629 not be worthwhile. I'm assuming that it isn't unless performance
11630 or memory numbers show me otherwise. */
11631
e7c27a73 11632 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 11633 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
11634
11635 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11636 cu->has_loclist = 1;
4c2df51b
DJ
11637}
11638
c906108c
SS
11639/* Given a pointer to a DWARF information entry, figure out if we need
11640 to make a symbol table entry for it, and if so, create a new entry
11641 and return a pointer to it.
11642 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
11643 used the passed type.
11644 If SPACE is not NULL, use it to hold the new symbol. If it is
11645 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
11646
11647static struct symbol *
34eaf542
TT
11648new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11649 struct symbol *space)
c906108c 11650{
e7c27a73 11651 struct objfile *objfile = cu->objfile;
c906108c
SS
11652 struct symbol *sym = NULL;
11653 char *name;
11654 struct attribute *attr = NULL;
11655 struct attribute *attr2 = NULL;
e142c38c 11656 CORE_ADDR baseaddr;
e37fd15a
SW
11657 struct pending **list_to_add = NULL;
11658
edb3359d 11659 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
11660
11661 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11662
94af9270 11663 name = dwarf2_name (die, cu);
c906108c
SS
11664 if (name)
11665 {
94af9270 11666 const char *linkagename;
34eaf542 11667 int suppress_add = 0;
94af9270 11668
34eaf542
TT
11669 if (space)
11670 sym = space;
11671 else
11672 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 11673 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
11674
11675 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 11676 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
11677 linkagename = dwarf2_physname (name, die, cu);
11678 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 11679
f55ee35c
JK
11680 /* Fortran does not have mangling standard and the mangling does differ
11681 between gfortran, iFort etc. */
11682 if (cu->language == language_fortran
b250c185 11683 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
11684 symbol_set_demangled_name (&(sym->ginfo),
11685 (char *) dwarf2_full_name (name, die, cu),
11686 NULL);
f55ee35c 11687
c906108c 11688 /* Default assumptions.
c5aa993b 11689 Use the passed type or decode it from the die. */
176620f1 11690 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 11691 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
11692 if (type != NULL)
11693 SYMBOL_TYPE (sym) = type;
11694 else
e7c27a73 11695 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
11696 attr = dwarf2_attr (die,
11697 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11698 cu);
c906108c
SS
11699 if (attr)
11700 {
11701 SYMBOL_LINE (sym) = DW_UNSND (attr);
11702 }
cb1df416 11703
edb3359d
DJ
11704 attr = dwarf2_attr (die,
11705 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11706 cu);
cb1df416
DJ
11707 if (attr)
11708 {
11709 int file_index = DW_UNSND (attr);
9a619af0 11710
cb1df416
DJ
11711 if (cu->line_header == NULL
11712 || file_index > cu->line_header->num_file_names)
11713 complaint (&symfile_complaints,
11714 _("file index out of range"));
1c3d648d 11715 else if (file_index > 0)
cb1df416
DJ
11716 {
11717 struct file_entry *fe;
9a619af0 11718
cb1df416
DJ
11719 fe = &cu->line_header->file_names[file_index - 1];
11720 SYMBOL_SYMTAB (sym) = fe->symtab;
11721 }
11722 }
11723
c906108c
SS
11724 switch (die->tag)
11725 {
11726 case DW_TAG_label:
e142c38c 11727 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
11728 if (attr)
11729 {
11730 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11731 }
0f5238ed
TT
11732 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11733 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 11734 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 11735 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
11736 break;
11737 case DW_TAG_subprogram:
11738 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11739 finish_block. */
11740 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 11741 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
11742 if ((attr2 && (DW_UNSND (attr2) != 0))
11743 || cu->language == language_ada)
c906108c 11744 {
2cfa0c8d
JB
11745 /* Subprograms marked external are stored as a global symbol.
11746 Ada subprograms, whether marked external or not, are always
11747 stored as a global symbol, because we want to be able to
11748 access them globally. For instance, we want to be able
11749 to break on a nested subprogram without having to
11750 specify the context. */
e37fd15a 11751 list_to_add = &global_symbols;
c906108c
SS
11752 }
11753 else
11754 {
e37fd15a 11755 list_to_add = cu->list_in_scope;
c906108c
SS
11756 }
11757 break;
edb3359d
DJ
11758 case DW_TAG_inlined_subroutine:
11759 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11760 finish_block. */
11761 SYMBOL_CLASS (sym) = LOC_BLOCK;
11762 SYMBOL_INLINED (sym) = 1;
11763 /* Do not add the symbol to any lists. It will be found via
11764 BLOCK_FUNCTION from the blockvector. */
11765 break;
34eaf542
TT
11766 case DW_TAG_template_value_param:
11767 suppress_add = 1;
11768 /* Fall through. */
72929c62 11769 case DW_TAG_constant:
c906108c 11770 case DW_TAG_variable:
254e6b9e 11771 case DW_TAG_member:
0963b4bd
MS
11772 /* Compilation with minimal debug info may result in
11773 variables with missing type entries. Change the
11774 misleading `void' type to something sensible. */
c906108c 11775 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 11776 SYMBOL_TYPE (sym)
46bf5051 11777 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 11778
e142c38c 11779 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
11780 /* In the case of DW_TAG_member, we should only be called for
11781 static const members. */
11782 if (die->tag == DW_TAG_member)
11783 {
3863f96c
DE
11784 /* dwarf2_add_field uses die_is_declaration,
11785 so we do the same. */
254e6b9e
DE
11786 gdb_assert (die_is_declaration (die, cu));
11787 gdb_assert (attr);
11788 }
c906108c
SS
11789 if (attr)
11790 {
e7c27a73 11791 dwarf2_const_value (attr, sym, cu);
e142c38c 11792 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 11793 if (!suppress_add)
34eaf542
TT
11794 {
11795 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 11796 list_to_add = &global_symbols;
34eaf542 11797 else
e37fd15a 11798 list_to_add = cu->list_in_scope;
34eaf542 11799 }
c906108c
SS
11800 break;
11801 }
e142c38c 11802 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11803 if (attr)
11804 {
e7c27a73 11805 var_decode_location (attr, sym, cu);
e142c38c 11806 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
11807 if (SYMBOL_CLASS (sym) == LOC_STATIC
11808 && SYMBOL_VALUE_ADDRESS (sym) == 0
11809 && !dwarf2_per_objfile->has_section_at_zero)
11810 {
11811 /* When a static variable is eliminated by the linker,
11812 the corresponding debug information is not stripped
11813 out, but the variable address is set to null;
11814 do not add such variables into symbol table. */
11815 }
11816 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 11817 {
f55ee35c
JK
11818 /* Workaround gfortran PR debug/40040 - it uses
11819 DW_AT_location for variables in -fPIC libraries which may
11820 get overriden by other libraries/executable and get
11821 a different address. Resolve it by the minimal symbol
11822 which may come from inferior's executable using copy
11823 relocation. Make this workaround only for gfortran as for
11824 other compilers GDB cannot guess the minimal symbol
11825 Fortran mangling kind. */
11826 if (cu->language == language_fortran && die->parent
11827 && die->parent->tag == DW_TAG_module
11828 && cu->producer
11829 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11830 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11831
1c809c68
TT
11832 /* A variable with DW_AT_external is never static,
11833 but it may be block-scoped. */
11834 list_to_add = (cu->list_in_scope == &file_symbols
11835 ? &global_symbols : cu->list_in_scope);
1c809c68 11836 }
c906108c 11837 else
e37fd15a 11838 list_to_add = cu->list_in_scope;
c906108c
SS
11839 }
11840 else
11841 {
11842 /* We do not know the address of this symbol.
c5aa993b
JM
11843 If it is an external symbol and we have type information
11844 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11845 The address of the variable will then be determined from
11846 the minimal symbol table whenever the variable is
11847 referenced. */
e142c38c 11848 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 11849 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 11850 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 11851 {
0fe7935b
DJ
11852 /* A variable with DW_AT_external is never static, but it
11853 may be block-scoped. */
11854 list_to_add = (cu->list_in_scope == &file_symbols
11855 ? &global_symbols : cu->list_in_scope);
11856
c906108c 11857 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 11858 }
442ddf59
JK
11859 else if (!die_is_declaration (die, cu))
11860 {
11861 /* Use the default LOC_OPTIMIZED_OUT class. */
11862 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
11863 if (!suppress_add)
11864 list_to_add = cu->list_in_scope;
442ddf59 11865 }
c906108c
SS
11866 }
11867 break;
11868 case DW_TAG_formal_parameter:
edb3359d
DJ
11869 /* If we are inside a function, mark this as an argument. If
11870 not, we might be looking at an argument to an inlined function
11871 when we do not have enough information to show inlined frames;
11872 pretend it's a local variable in that case so that the user can
11873 still see it. */
11874 if (context_stack_depth > 0
11875 && context_stack[context_stack_depth - 1].name != NULL)
11876 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 11877 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
11878 if (attr)
11879 {
e7c27a73 11880 var_decode_location (attr, sym, cu);
c906108c 11881 }
e142c38c 11882 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11883 if (attr)
11884 {
e7c27a73 11885 dwarf2_const_value (attr, sym, cu);
c906108c 11886 }
f346a30d 11887
e37fd15a 11888 list_to_add = cu->list_in_scope;
c906108c
SS
11889 break;
11890 case DW_TAG_unspecified_parameters:
11891 /* From varargs functions; gdb doesn't seem to have any
11892 interest in this information, so just ignore it for now.
11893 (FIXME?) */
11894 break;
34eaf542
TT
11895 case DW_TAG_template_type_param:
11896 suppress_add = 1;
11897 /* Fall through. */
c906108c 11898 case DW_TAG_class_type:
680b30c7 11899 case DW_TAG_interface_type:
c906108c
SS
11900 case DW_TAG_structure_type:
11901 case DW_TAG_union_type:
72019c9c 11902 case DW_TAG_set_type:
c906108c
SS
11903 case DW_TAG_enumeration_type:
11904 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11905 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 11906
63d06c5c 11907 {
987504bb 11908 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
11909 really ever be static objects: otherwise, if you try
11910 to, say, break of a class's method and you're in a file
11911 which doesn't mention that class, it won't work unless
11912 the check for all static symbols in lookup_symbol_aux
11913 saves you. See the OtherFileClass tests in
11914 gdb.c++/namespace.exp. */
11915
e37fd15a 11916 if (!suppress_add)
34eaf542 11917 {
34eaf542
TT
11918 list_to_add = (cu->list_in_scope == &file_symbols
11919 && (cu->language == language_cplus
11920 || cu->language == language_java)
11921 ? &global_symbols : cu->list_in_scope);
63d06c5c 11922
64382290
TT
11923 /* The semantics of C++ state that "struct foo {
11924 ... }" also defines a typedef for "foo". A Java
11925 class declaration also defines a typedef for the
11926 class. */
11927 if (cu->language == language_cplus
11928 || cu->language == language_java
11929 || cu->language == language_ada)
11930 {
11931 /* The symbol's name is already allocated along
11932 with this objfile, so we don't need to
11933 duplicate it for the type. */
11934 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11935 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11936 }
63d06c5c
DC
11937 }
11938 }
c906108c
SS
11939 break;
11940 case DW_TAG_typedef:
63d06c5c
DC
11941 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11942 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11943 list_to_add = cu->list_in_scope;
63d06c5c 11944 break;
c906108c 11945 case DW_TAG_base_type:
a02abb62 11946 case DW_TAG_subrange_type:
c906108c 11947 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 11948 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 11949 list_to_add = cu->list_in_scope;
c906108c
SS
11950 break;
11951 case DW_TAG_enumerator:
e142c38c 11952 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
11953 if (attr)
11954 {
e7c27a73 11955 dwarf2_const_value (attr, sym, cu);
c906108c 11956 }
63d06c5c
DC
11957 {
11958 /* NOTE: carlton/2003-11-10: See comment above in the
11959 DW_TAG_class_type, etc. block. */
11960
e142c38c 11961 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
11962 && (cu->language == language_cplus
11963 || cu->language == language_java)
e142c38c 11964 ? &global_symbols : cu->list_in_scope);
63d06c5c 11965 }
c906108c 11966 break;
5c4e30ca
DC
11967 case DW_TAG_namespace:
11968 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 11969 list_to_add = &global_symbols;
5c4e30ca 11970 break;
c906108c
SS
11971 default:
11972 /* Not a tag we recognize. Hopefully we aren't processing
11973 trash data, but since we must specifically ignore things
11974 we don't recognize, there is nothing else we should do at
0963b4bd 11975 this point. */
e2e0b3e5 11976 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 11977 dwarf_tag_name (die->tag));
c906108c
SS
11978 break;
11979 }
df8a16a1 11980
e37fd15a
SW
11981 if (suppress_add)
11982 {
11983 sym->hash_next = objfile->template_symbols;
11984 objfile->template_symbols = sym;
11985 list_to_add = NULL;
11986 }
11987
11988 if (list_to_add != NULL)
11989 add_symbol_to_list (sym, list_to_add);
11990
df8a16a1
DJ
11991 /* For the benefit of old versions of GCC, check for anonymous
11992 namespaces based on the demangled name. */
11993 if (!processing_has_namespace_info
94af9270 11994 && cu->language == language_cplus)
df8a16a1 11995 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
11996 }
11997 return (sym);
11998}
11999
34eaf542
TT
12000/* A wrapper for new_symbol_full that always allocates a new symbol. */
12001
12002static struct symbol *
12003new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12004{
12005 return new_symbol_full (die, type, cu, NULL);
12006}
12007
98bfdba5
PA
12008/* Given an attr with a DW_FORM_dataN value in host byte order,
12009 zero-extend it as appropriate for the symbol's type. The DWARF
12010 standard (v4) is not entirely clear about the meaning of using
12011 DW_FORM_dataN for a constant with a signed type, where the type is
12012 wider than the data. The conclusion of a discussion on the DWARF
12013 list was that this is unspecified. We choose to always zero-extend
12014 because that is the interpretation long in use by GCC. */
c906108c 12015
98bfdba5
PA
12016static gdb_byte *
12017dwarf2_const_value_data (struct attribute *attr, struct type *type,
12018 const char *name, struct obstack *obstack,
12019 struct dwarf2_cu *cu, long *value, int bits)
c906108c 12020{
e7c27a73 12021 struct objfile *objfile = cu->objfile;
e17a4113
UW
12022 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12023 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
12024 LONGEST l = DW_UNSND (attr);
12025
12026 if (bits < sizeof (*value) * 8)
12027 {
12028 l &= ((LONGEST) 1 << bits) - 1;
12029 *value = l;
12030 }
12031 else if (bits == sizeof (*value) * 8)
12032 *value = l;
12033 else
12034 {
12035 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12036 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12037 return bytes;
12038 }
12039
12040 return NULL;
12041}
12042
12043/* Read a constant value from an attribute. Either set *VALUE, or if
12044 the value does not fit in *VALUE, set *BYTES - either already
12045 allocated on the objfile obstack, or newly allocated on OBSTACK,
12046 or, set *BATON, if we translated the constant to a location
12047 expression. */
12048
12049static void
12050dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12051 const char *name, struct obstack *obstack,
12052 struct dwarf2_cu *cu,
12053 long *value, gdb_byte **bytes,
12054 struct dwarf2_locexpr_baton **baton)
12055{
12056 struct objfile *objfile = cu->objfile;
12057 struct comp_unit_head *cu_header = &cu->header;
c906108c 12058 struct dwarf_block *blk;
98bfdba5
PA
12059 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12060 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12061
12062 *value = 0;
12063 *bytes = NULL;
12064 *baton = NULL;
c906108c
SS
12065
12066 switch (attr->form)
12067 {
12068 case DW_FORM_addr:
ac56253d 12069 {
ac56253d
TT
12070 gdb_byte *data;
12071
98bfdba5
PA
12072 if (TYPE_LENGTH (type) != cu_header->addr_size)
12073 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 12074 cu_header->addr_size,
98bfdba5 12075 TYPE_LENGTH (type));
ac56253d
TT
12076 /* Symbols of this form are reasonably rare, so we just
12077 piggyback on the existing location code rather than writing
12078 a new implementation of symbol_computed_ops. */
98bfdba5
PA
12079 *baton = obstack_alloc (&objfile->objfile_obstack,
12080 sizeof (struct dwarf2_locexpr_baton));
12081 (*baton)->per_cu = cu->per_cu;
12082 gdb_assert ((*baton)->per_cu);
ac56253d 12083
98bfdba5
PA
12084 (*baton)->size = 2 + cu_header->addr_size;
12085 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12086 (*baton)->data = data;
ac56253d
TT
12087
12088 data[0] = DW_OP_addr;
12089 store_unsigned_integer (&data[1], cu_header->addr_size,
12090 byte_order, DW_ADDR (attr));
12091 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 12092 }
c906108c 12093 break;
4ac36638 12094 case DW_FORM_string:
93b5768b 12095 case DW_FORM_strp:
98bfdba5
PA
12096 /* DW_STRING is already allocated on the objfile obstack, point
12097 directly to it. */
12098 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 12099 break;
c906108c
SS
12100 case DW_FORM_block1:
12101 case DW_FORM_block2:
12102 case DW_FORM_block4:
12103 case DW_FORM_block:
2dc7f7b3 12104 case DW_FORM_exprloc:
c906108c 12105 blk = DW_BLOCK (attr);
98bfdba5
PA
12106 if (TYPE_LENGTH (type) != blk->size)
12107 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12108 TYPE_LENGTH (type));
12109 *bytes = blk->data;
c906108c 12110 break;
2df3850c
JM
12111
12112 /* The DW_AT_const_value attributes are supposed to carry the
12113 symbol's value "represented as it would be on the target
12114 architecture." By the time we get here, it's already been
12115 converted to host endianness, so we just need to sign- or
12116 zero-extend it as appropriate. */
12117 case DW_FORM_data1:
3e43a32a
MS
12118 *bytes = dwarf2_const_value_data (attr, type, name,
12119 obstack, cu, value, 8);
2df3850c 12120 break;
c906108c 12121 case DW_FORM_data2:
3e43a32a
MS
12122 *bytes = dwarf2_const_value_data (attr, type, name,
12123 obstack, cu, value, 16);
2df3850c 12124 break;
c906108c 12125 case DW_FORM_data4:
3e43a32a
MS
12126 *bytes = dwarf2_const_value_data (attr, type, name,
12127 obstack, cu, value, 32);
2df3850c 12128 break;
c906108c 12129 case DW_FORM_data8:
3e43a32a
MS
12130 *bytes = dwarf2_const_value_data (attr, type, name,
12131 obstack, cu, value, 64);
2df3850c
JM
12132 break;
12133
c906108c 12134 case DW_FORM_sdata:
98bfdba5 12135 *value = DW_SND (attr);
2df3850c
JM
12136 break;
12137
c906108c 12138 case DW_FORM_udata:
98bfdba5 12139 *value = DW_UNSND (attr);
c906108c 12140 break;
2df3850c 12141
c906108c 12142 default:
4d3c2250 12143 complaint (&symfile_complaints,
e2e0b3e5 12144 _("unsupported const value attribute form: '%s'"),
4d3c2250 12145 dwarf_form_name (attr->form));
98bfdba5 12146 *value = 0;
c906108c
SS
12147 break;
12148 }
12149}
12150
2df3850c 12151
98bfdba5
PA
12152/* Copy constant value from an attribute to a symbol. */
12153
2df3850c 12154static void
98bfdba5
PA
12155dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12156 struct dwarf2_cu *cu)
2df3850c 12157{
98bfdba5
PA
12158 struct objfile *objfile = cu->objfile;
12159 struct comp_unit_head *cu_header = &cu->header;
12160 long value;
12161 gdb_byte *bytes;
12162 struct dwarf2_locexpr_baton *baton;
2df3850c 12163
98bfdba5
PA
12164 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12165 SYMBOL_PRINT_NAME (sym),
12166 &objfile->objfile_obstack, cu,
12167 &value, &bytes, &baton);
2df3850c 12168
98bfdba5
PA
12169 if (baton != NULL)
12170 {
12171 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12172 SYMBOL_LOCATION_BATON (sym) = baton;
12173 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12174 }
12175 else if (bytes != NULL)
12176 {
12177 SYMBOL_VALUE_BYTES (sym) = bytes;
12178 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12179 }
12180 else
12181 {
12182 SYMBOL_VALUE (sym) = value;
12183 SYMBOL_CLASS (sym) = LOC_CONST;
12184 }
2df3850c
JM
12185}
12186
c906108c
SS
12187/* Return the type of the die in question using its DW_AT_type attribute. */
12188
12189static struct type *
e7c27a73 12190die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12191{
c906108c 12192 struct attribute *type_attr;
c906108c 12193
e142c38c 12194 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
12195 if (!type_attr)
12196 {
12197 /* A missing DW_AT_type represents a void type. */
46bf5051 12198 return objfile_type (cu->objfile)->builtin_void;
c906108c 12199 }
348e048f 12200
673bfd45 12201 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12202}
12203
b4ba55a1
JB
12204/* True iff CU's producer generates GNAT Ada auxiliary information
12205 that allows to find parallel types through that information instead
12206 of having to do expensive parallel lookups by type name. */
12207
12208static int
12209need_gnat_info (struct dwarf2_cu *cu)
12210{
12211 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12212 of GNAT produces this auxiliary information, without any indication
12213 that it is produced. Part of enhancing the FSF version of GNAT
12214 to produce that information will be to put in place an indicator
12215 that we can use in order to determine whether the descriptive type
12216 info is available or not. One suggestion that has been made is
12217 to use a new attribute, attached to the CU die. For now, assume
12218 that the descriptive type info is not available. */
12219 return 0;
12220}
12221
b4ba55a1
JB
12222/* Return the auxiliary type of the die in question using its
12223 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12224 attribute is not present. */
12225
12226static struct type *
12227die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12228{
b4ba55a1 12229 struct attribute *type_attr;
b4ba55a1
JB
12230
12231 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12232 if (!type_attr)
12233 return NULL;
12234
673bfd45 12235 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
12236}
12237
12238/* If DIE has a descriptive_type attribute, then set the TYPE's
12239 descriptive type accordingly. */
12240
12241static void
12242set_descriptive_type (struct type *type, struct die_info *die,
12243 struct dwarf2_cu *cu)
12244{
12245 struct type *descriptive_type = die_descriptive_type (die, cu);
12246
12247 if (descriptive_type)
12248 {
12249 ALLOCATE_GNAT_AUX_TYPE (type);
12250 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12251 }
12252}
12253
c906108c
SS
12254/* Return the containing type of the die in question using its
12255 DW_AT_containing_type attribute. */
12256
12257static struct type *
e7c27a73 12258die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12259{
c906108c 12260 struct attribute *type_attr;
c906108c 12261
e142c38c 12262 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
12263 if (!type_attr)
12264 error (_("Dwarf Error: Problem turning containing type into gdb type "
12265 "[in module %s]"), cu->objfile->name);
12266
673bfd45 12267 return lookup_die_type (die, type_attr, cu);
c906108c
SS
12268}
12269
673bfd45
DE
12270/* Look up the type of DIE in CU using its type attribute ATTR.
12271 If there is no type substitute an error marker. */
12272
c906108c 12273static struct type *
673bfd45
DE
12274lookup_die_type (struct die_info *die, struct attribute *attr,
12275 struct dwarf2_cu *cu)
c906108c 12276{
f792889a
DJ
12277 struct type *this_type;
12278
673bfd45
DE
12279 /* First see if we have it cached. */
12280
12281 if (is_ref_attr (attr))
12282 {
12283 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12284
12285 this_type = get_die_type_at_offset (offset, cu->per_cu);
12286 }
55f1336d 12287 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
12288 {
12289 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12290 struct dwarf2_cu *sig_cu;
12291 unsigned int offset;
12292
12293 /* sig_type will be NULL if the signatured type is missing from
12294 the debug info. */
12295 if (sig_type == NULL)
12296 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12297 "at 0x%x [in module %s]"),
12298 die->offset, cu->objfile->name);
12299
8b70b953 12300 gdb_assert (sig_type->per_cu.debug_type_section);
b3c8eb43 12301 offset = sig_type->per_cu.offset + sig_type->type_offset;
673bfd45
DE
12302 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12303 }
12304 else
12305 {
12306 dump_die_for_error (die);
12307 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12308 dwarf_attr_name (attr->name), cu->objfile->name);
12309 }
12310
12311 /* If not cached we need to read it in. */
12312
12313 if (this_type == NULL)
12314 {
12315 struct die_info *type_die;
12316 struct dwarf2_cu *type_cu = cu;
12317
12318 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12319 /* If the type is cached, we should have found it above. */
12320 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12321 this_type = read_type_die_1 (type_die, type_cu);
12322 }
12323
12324 /* If we still don't have a type use an error marker. */
12325
12326 if (this_type == NULL)
c906108c 12327 {
b00fdb78
TT
12328 char *message, *saved;
12329
12330 /* read_type_die already issued a complaint. */
12331 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12332 cu->objfile->name,
12333 cu->header.offset,
12334 die->offset);
12335 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12336 message, strlen (message));
12337 xfree (message);
12338
12339 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 12340 }
673bfd45 12341
f792889a 12342 return this_type;
c906108c
SS
12343}
12344
673bfd45
DE
12345/* Return the type in DIE, CU.
12346 Returns NULL for invalid types.
12347
12348 This first does a lookup in the appropriate type_hash table,
12349 and only reads the die in if necessary.
12350
12351 NOTE: This can be called when reading in partial or full symbols. */
12352
f792889a 12353static struct type *
e7c27a73 12354read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12355{
f792889a
DJ
12356 struct type *this_type;
12357
12358 this_type = get_die_type (die, cu);
12359 if (this_type)
12360 return this_type;
12361
673bfd45
DE
12362 return read_type_die_1 (die, cu);
12363}
12364
12365/* Read the type in DIE, CU.
12366 Returns NULL for invalid types. */
12367
12368static struct type *
12369read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12370{
12371 struct type *this_type = NULL;
12372
c906108c
SS
12373 switch (die->tag)
12374 {
12375 case DW_TAG_class_type:
680b30c7 12376 case DW_TAG_interface_type:
c906108c
SS
12377 case DW_TAG_structure_type:
12378 case DW_TAG_union_type:
f792889a 12379 this_type = read_structure_type (die, cu);
c906108c
SS
12380 break;
12381 case DW_TAG_enumeration_type:
f792889a 12382 this_type = read_enumeration_type (die, cu);
c906108c
SS
12383 break;
12384 case DW_TAG_subprogram:
12385 case DW_TAG_subroutine_type:
edb3359d 12386 case DW_TAG_inlined_subroutine:
f792889a 12387 this_type = read_subroutine_type (die, cu);
c906108c
SS
12388 break;
12389 case DW_TAG_array_type:
f792889a 12390 this_type = read_array_type (die, cu);
c906108c 12391 break;
72019c9c 12392 case DW_TAG_set_type:
f792889a 12393 this_type = read_set_type (die, cu);
72019c9c 12394 break;
c906108c 12395 case DW_TAG_pointer_type:
f792889a 12396 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
12397 break;
12398 case DW_TAG_ptr_to_member_type:
f792889a 12399 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
12400 break;
12401 case DW_TAG_reference_type:
f792889a 12402 this_type = read_tag_reference_type (die, cu);
c906108c
SS
12403 break;
12404 case DW_TAG_const_type:
f792889a 12405 this_type = read_tag_const_type (die, cu);
c906108c
SS
12406 break;
12407 case DW_TAG_volatile_type:
f792889a 12408 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
12409 break;
12410 case DW_TAG_string_type:
f792889a 12411 this_type = read_tag_string_type (die, cu);
c906108c
SS
12412 break;
12413 case DW_TAG_typedef:
f792889a 12414 this_type = read_typedef (die, cu);
c906108c 12415 break;
a02abb62 12416 case DW_TAG_subrange_type:
f792889a 12417 this_type = read_subrange_type (die, cu);
a02abb62 12418 break;
c906108c 12419 case DW_TAG_base_type:
f792889a 12420 this_type = read_base_type (die, cu);
c906108c 12421 break;
81a17f79 12422 case DW_TAG_unspecified_type:
f792889a 12423 this_type = read_unspecified_type (die, cu);
81a17f79 12424 break;
0114d602
DJ
12425 case DW_TAG_namespace:
12426 this_type = read_namespace_type (die, cu);
12427 break;
f55ee35c
JK
12428 case DW_TAG_module:
12429 this_type = read_module_type (die, cu);
12430 break;
c906108c 12431 default:
3e43a32a
MS
12432 complaint (&symfile_complaints,
12433 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 12434 dwarf_tag_name (die->tag));
c906108c
SS
12435 break;
12436 }
63d06c5c 12437
f792889a 12438 return this_type;
63d06c5c
DC
12439}
12440
abc72ce4
DE
12441/* See if we can figure out if the class lives in a namespace. We do
12442 this by looking for a member function; its demangled name will
12443 contain namespace info, if there is any.
12444 Return the computed name or NULL.
12445 Space for the result is allocated on the objfile's obstack.
12446 This is the full-die version of guess_partial_die_structure_name.
12447 In this case we know DIE has no useful parent. */
12448
12449static char *
12450guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12451{
12452 struct die_info *spec_die;
12453 struct dwarf2_cu *spec_cu;
12454 struct die_info *child;
12455
12456 spec_cu = cu;
12457 spec_die = die_specification (die, &spec_cu);
12458 if (spec_die != NULL)
12459 {
12460 die = spec_die;
12461 cu = spec_cu;
12462 }
12463
12464 for (child = die->child;
12465 child != NULL;
12466 child = child->sibling)
12467 {
12468 if (child->tag == DW_TAG_subprogram)
12469 {
12470 struct attribute *attr;
12471
12472 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12473 if (attr == NULL)
12474 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12475 if (attr != NULL)
12476 {
12477 char *actual_name
12478 = language_class_name_from_physname (cu->language_defn,
12479 DW_STRING (attr));
12480 char *name = NULL;
12481
12482 if (actual_name != NULL)
12483 {
12484 char *die_name = dwarf2_name (die, cu);
12485
12486 if (die_name != NULL
12487 && strcmp (die_name, actual_name) != 0)
12488 {
12489 /* Strip off the class name from the full name.
12490 We want the prefix. */
12491 int die_name_len = strlen (die_name);
12492 int actual_name_len = strlen (actual_name);
12493
12494 /* Test for '::' as a sanity check. */
12495 if (actual_name_len > die_name_len + 2
3e43a32a
MS
12496 && actual_name[actual_name_len
12497 - die_name_len - 1] == ':')
abc72ce4
DE
12498 name =
12499 obsavestring (actual_name,
12500 actual_name_len - die_name_len - 2,
12501 &cu->objfile->objfile_obstack);
12502 }
12503 }
12504 xfree (actual_name);
12505 return name;
12506 }
12507 }
12508 }
12509
12510 return NULL;
12511}
12512
96408a79
SA
12513/* GCC might emit a nameless typedef that has a linkage name. Determine the
12514 prefix part in such case. See
12515 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12516
12517static char *
12518anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12519{
12520 struct attribute *attr;
12521 char *base;
12522
12523 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12524 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12525 return NULL;
12526
12527 attr = dwarf2_attr (die, DW_AT_name, cu);
12528 if (attr != NULL && DW_STRING (attr) != NULL)
12529 return NULL;
12530
12531 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12532 if (attr == NULL)
12533 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12534 if (attr == NULL || DW_STRING (attr) == NULL)
12535 return NULL;
12536
12537 /* dwarf2_name had to be already called. */
12538 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12539
12540 /* Strip the base name, keep any leading namespaces/classes. */
12541 base = strrchr (DW_STRING (attr), ':');
12542 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12543 return "";
12544
12545 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12546 &cu->objfile->objfile_obstack);
12547}
12548
fdde2d81 12549/* Return the name of the namespace/class that DIE is defined within,
0114d602 12550 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 12551
0114d602
DJ
12552 For example, if we're within the method foo() in the following
12553 code:
12554
12555 namespace N {
12556 class C {
12557 void foo () {
12558 }
12559 };
12560 }
12561
12562 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
12563
12564static char *
e142c38c 12565determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 12566{
0114d602
DJ
12567 struct die_info *parent, *spec_die;
12568 struct dwarf2_cu *spec_cu;
12569 struct type *parent_type;
96408a79 12570 char *retval;
63d06c5c 12571
f55ee35c
JK
12572 if (cu->language != language_cplus && cu->language != language_java
12573 && cu->language != language_fortran)
0114d602
DJ
12574 return "";
12575
96408a79
SA
12576 retval = anonymous_struct_prefix (die, cu);
12577 if (retval)
12578 return retval;
12579
0114d602
DJ
12580 /* We have to be careful in the presence of DW_AT_specification.
12581 For example, with GCC 3.4, given the code
12582
12583 namespace N {
12584 void foo() {
12585 // Definition of N::foo.
12586 }
12587 }
12588
12589 then we'll have a tree of DIEs like this:
12590
12591 1: DW_TAG_compile_unit
12592 2: DW_TAG_namespace // N
12593 3: DW_TAG_subprogram // declaration of N::foo
12594 4: DW_TAG_subprogram // definition of N::foo
12595 DW_AT_specification // refers to die #3
12596
12597 Thus, when processing die #4, we have to pretend that we're in
12598 the context of its DW_AT_specification, namely the contex of die
12599 #3. */
12600 spec_cu = cu;
12601 spec_die = die_specification (die, &spec_cu);
12602 if (spec_die == NULL)
12603 parent = die->parent;
12604 else
63d06c5c 12605 {
0114d602
DJ
12606 parent = spec_die->parent;
12607 cu = spec_cu;
63d06c5c 12608 }
0114d602
DJ
12609
12610 if (parent == NULL)
12611 return "";
98bfdba5
PA
12612 else if (parent->building_fullname)
12613 {
12614 const char *name;
12615 const char *parent_name;
12616
12617 /* It has been seen on RealView 2.2 built binaries,
12618 DW_TAG_template_type_param types actually _defined_ as
12619 children of the parent class:
12620
12621 enum E {};
12622 template class <class Enum> Class{};
12623 Class<enum E> class_e;
12624
12625 1: DW_TAG_class_type (Class)
12626 2: DW_TAG_enumeration_type (E)
12627 3: DW_TAG_enumerator (enum1:0)
12628 3: DW_TAG_enumerator (enum2:1)
12629 ...
12630 2: DW_TAG_template_type_param
12631 DW_AT_type DW_FORM_ref_udata (E)
12632
12633 Besides being broken debug info, it can put GDB into an
12634 infinite loop. Consider:
12635
12636 When we're building the full name for Class<E>, we'll start
12637 at Class, and go look over its template type parameters,
12638 finding E. We'll then try to build the full name of E, and
12639 reach here. We're now trying to build the full name of E,
12640 and look over the parent DIE for containing scope. In the
12641 broken case, if we followed the parent DIE of E, we'd again
12642 find Class, and once again go look at its template type
12643 arguments, etc., etc. Simply don't consider such parent die
12644 as source-level parent of this die (it can't be, the language
12645 doesn't allow it), and break the loop here. */
12646 name = dwarf2_name (die, cu);
12647 parent_name = dwarf2_name (parent, cu);
12648 complaint (&symfile_complaints,
12649 _("template param type '%s' defined within parent '%s'"),
12650 name ? name : "<unknown>",
12651 parent_name ? parent_name : "<unknown>");
12652 return "";
12653 }
63d06c5c 12654 else
0114d602
DJ
12655 switch (parent->tag)
12656 {
63d06c5c 12657 case DW_TAG_namespace:
0114d602 12658 parent_type = read_type_die (parent, cu);
acebe513
UW
12659 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12660 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12661 Work around this problem here. */
12662 if (cu->language == language_cplus
12663 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12664 return "";
0114d602
DJ
12665 /* We give a name to even anonymous namespaces. */
12666 return TYPE_TAG_NAME (parent_type);
63d06c5c 12667 case DW_TAG_class_type:
680b30c7 12668 case DW_TAG_interface_type:
63d06c5c 12669 case DW_TAG_structure_type:
0114d602 12670 case DW_TAG_union_type:
f55ee35c 12671 case DW_TAG_module:
0114d602
DJ
12672 parent_type = read_type_die (parent, cu);
12673 if (TYPE_TAG_NAME (parent_type) != NULL)
12674 return TYPE_TAG_NAME (parent_type);
12675 else
12676 /* An anonymous structure is only allowed non-static data
12677 members; no typedefs, no member functions, et cetera.
12678 So it does not need a prefix. */
12679 return "";
abc72ce4
DE
12680 case DW_TAG_compile_unit:
12681 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12682 if (cu->language == language_cplus
8b70b953 12683 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
12684 && die->child != NULL
12685 && (die->tag == DW_TAG_class_type
12686 || die->tag == DW_TAG_structure_type
12687 || die->tag == DW_TAG_union_type))
12688 {
12689 char *name = guess_full_die_structure_name (die, cu);
12690 if (name != NULL)
12691 return name;
12692 }
12693 return "";
63d06c5c 12694 default:
8176b9b8 12695 return determine_prefix (parent, cu);
63d06c5c 12696 }
63d06c5c
DC
12697}
12698
3e43a32a
MS
12699/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12700 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12701 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12702 an obconcat, otherwise allocate storage for the result. The CU argument is
12703 used to determine the language and hence, the appropriate separator. */
987504bb 12704
f55ee35c 12705#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
12706
12707static char *
f55ee35c
JK
12708typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12709 int physname, struct dwarf2_cu *cu)
63d06c5c 12710{
f55ee35c 12711 const char *lead = "";
5c315b68 12712 const char *sep;
63d06c5c 12713
3e43a32a
MS
12714 if (suffix == NULL || suffix[0] == '\0'
12715 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
12716 sep = "";
12717 else if (cu->language == language_java)
12718 sep = ".";
f55ee35c
JK
12719 else if (cu->language == language_fortran && physname)
12720 {
12721 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12722 DW_AT_MIPS_linkage_name is preferred and used instead. */
12723
12724 lead = "__";
12725 sep = "_MOD_";
12726 }
987504bb
JJ
12727 else
12728 sep = "::";
63d06c5c 12729
6dd47d34
DE
12730 if (prefix == NULL)
12731 prefix = "";
12732 if (suffix == NULL)
12733 suffix = "";
12734
987504bb
JJ
12735 if (obs == NULL)
12736 {
3e43a32a
MS
12737 char *retval
12738 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 12739
f55ee35c
JK
12740 strcpy (retval, lead);
12741 strcat (retval, prefix);
6dd47d34
DE
12742 strcat (retval, sep);
12743 strcat (retval, suffix);
63d06c5c
DC
12744 return retval;
12745 }
987504bb
JJ
12746 else
12747 {
12748 /* We have an obstack. */
f55ee35c 12749 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 12750 }
63d06c5c
DC
12751}
12752
c906108c
SS
12753/* Return sibling of die, NULL if no sibling. */
12754
f9aca02d 12755static struct die_info *
fba45db2 12756sibling_die (struct die_info *die)
c906108c 12757{
639d11d3 12758 return die->sibling;
c906108c
SS
12759}
12760
71c25dea
TT
12761/* Get name of a die, return NULL if not found. */
12762
12763static char *
12764dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12765 struct obstack *obstack)
12766{
12767 if (name && cu->language == language_cplus)
12768 {
12769 char *canon_name = cp_canonicalize_string (name);
12770
12771 if (canon_name != NULL)
12772 {
12773 if (strcmp (canon_name, name) != 0)
12774 name = obsavestring (canon_name, strlen (canon_name),
12775 obstack);
12776 xfree (canon_name);
12777 }
12778 }
12779
12780 return name;
c906108c
SS
12781}
12782
9219021c
DC
12783/* Get name of a die, return NULL if not found. */
12784
12785static char *
e142c38c 12786dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
12787{
12788 struct attribute *attr;
12789
e142c38c 12790 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
12791 if ((!attr || !DW_STRING (attr))
12792 && die->tag != DW_TAG_class_type
12793 && die->tag != DW_TAG_interface_type
12794 && die->tag != DW_TAG_structure_type
12795 && die->tag != DW_TAG_union_type)
71c25dea
TT
12796 return NULL;
12797
12798 switch (die->tag)
12799 {
12800 case DW_TAG_compile_unit:
12801 /* Compilation units have a DW_AT_name that is a filename, not
12802 a source language identifier. */
12803 case DW_TAG_enumeration_type:
12804 case DW_TAG_enumerator:
12805 /* These tags always have simple identifiers already; no need
12806 to canonicalize them. */
12807 return DW_STRING (attr);
907af001 12808
418835cc
KS
12809 case DW_TAG_subprogram:
12810 /* Java constructors will all be named "<init>", so return
12811 the class name when we see this special case. */
12812 if (cu->language == language_java
12813 && DW_STRING (attr) != NULL
12814 && strcmp (DW_STRING (attr), "<init>") == 0)
12815 {
12816 struct dwarf2_cu *spec_cu = cu;
12817 struct die_info *spec_die;
12818
12819 /* GCJ will output '<init>' for Java constructor names.
12820 For this special case, return the name of the parent class. */
12821
12822 /* GCJ may output suprogram DIEs with AT_specification set.
12823 If so, use the name of the specified DIE. */
12824 spec_die = die_specification (die, &spec_cu);
12825 if (spec_die != NULL)
12826 return dwarf2_name (spec_die, spec_cu);
12827
12828 do
12829 {
12830 die = die->parent;
12831 if (die->tag == DW_TAG_class_type)
12832 return dwarf2_name (die, cu);
12833 }
12834 while (die->tag != DW_TAG_compile_unit);
12835 }
907af001
UW
12836 break;
12837
12838 case DW_TAG_class_type:
12839 case DW_TAG_interface_type:
12840 case DW_TAG_structure_type:
12841 case DW_TAG_union_type:
12842 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12843 structures or unions. These were of the form "._%d" in GCC 4.1,
12844 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12845 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
12846 if (attr && DW_STRING (attr)
12847 && (strncmp (DW_STRING (attr), "._", 2) == 0
12848 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 12849 return NULL;
53832f31
TT
12850
12851 /* GCC might emit a nameless typedef that has a linkage name. See
12852 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12853 if (!attr || DW_STRING (attr) == NULL)
12854 {
df5c6c50 12855 char *demangled = NULL;
53832f31
TT
12856
12857 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12858 if (attr == NULL)
12859 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12860
12861 if (attr == NULL || DW_STRING (attr) == NULL)
12862 return NULL;
12863
df5c6c50
JK
12864 /* Avoid demangling DW_STRING (attr) the second time on a second
12865 call for the same DIE. */
12866 if (!DW_STRING_IS_CANONICAL (attr))
12867 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
12868
12869 if (demangled)
12870 {
96408a79
SA
12871 char *base;
12872
53832f31 12873 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
12874 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12875 &cu->objfile->objfile_obstack);
53832f31
TT
12876 DW_STRING_IS_CANONICAL (attr) = 1;
12877 xfree (demangled);
96408a79
SA
12878
12879 /* Strip any leading namespaces/classes, keep only the base name.
12880 DW_AT_name for named DIEs does not contain the prefixes. */
12881 base = strrchr (DW_STRING (attr), ':');
12882 if (base && base > DW_STRING (attr) && base[-1] == ':')
12883 return &base[1];
12884 else
12885 return DW_STRING (attr);
53832f31
TT
12886 }
12887 }
907af001
UW
12888 break;
12889
71c25dea 12890 default:
907af001
UW
12891 break;
12892 }
12893
12894 if (!DW_STRING_IS_CANONICAL (attr))
12895 {
12896 DW_STRING (attr)
12897 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12898 &cu->objfile->objfile_obstack);
12899 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 12900 }
907af001 12901 return DW_STRING (attr);
9219021c
DC
12902}
12903
12904/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
12905 is none. *EXT_CU is the CU containing DIE on input, and the CU
12906 containing the return value on output. */
9219021c
DC
12907
12908static struct die_info *
f2f0e013 12909dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
12910{
12911 struct attribute *attr;
9219021c 12912
f2f0e013 12913 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
12914 if (attr == NULL)
12915 return NULL;
12916
f2f0e013 12917 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
12918}
12919
c906108c
SS
12920/* Convert a DIE tag into its string name. */
12921
12922static char *
aa1ee363 12923dwarf_tag_name (unsigned tag)
c906108c
SS
12924{
12925 switch (tag)
12926 {
12927 case DW_TAG_padding:
12928 return "DW_TAG_padding";
12929 case DW_TAG_array_type:
12930 return "DW_TAG_array_type";
12931 case DW_TAG_class_type:
12932 return "DW_TAG_class_type";
12933 case DW_TAG_entry_point:
12934 return "DW_TAG_entry_point";
12935 case DW_TAG_enumeration_type:
12936 return "DW_TAG_enumeration_type";
12937 case DW_TAG_formal_parameter:
12938 return "DW_TAG_formal_parameter";
12939 case DW_TAG_imported_declaration:
12940 return "DW_TAG_imported_declaration";
12941 case DW_TAG_label:
12942 return "DW_TAG_label";
12943 case DW_TAG_lexical_block:
12944 return "DW_TAG_lexical_block";
12945 case DW_TAG_member:
12946 return "DW_TAG_member";
12947 case DW_TAG_pointer_type:
12948 return "DW_TAG_pointer_type";
12949 case DW_TAG_reference_type:
12950 return "DW_TAG_reference_type";
12951 case DW_TAG_compile_unit:
12952 return "DW_TAG_compile_unit";
12953 case DW_TAG_string_type:
12954 return "DW_TAG_string_type";
12955 case DW_TAG_structure_type:
12956 return "DW_TAG_structure_type";
12957 case DW_TAG_subroutine_type:
12958 return "DW_TAG_subroutine_type";
12959 case DW_TAG_typedef:
12960 return "DW_TAG_typedef";
12961 case DW_TAG_union_type:
12962 return "DW_TAG_union_type";
12963 case DW_TAG_unspecified_parameters:
12964 return "DW_TAG_unspecified_parameters";
12965 case DW_TAG_variant:
12966 return "DW_TAG_variant";
12967 case DW_TAG_common_block:
12968 return "DW_TAG_common_block";
12969 case DW_TAG_common_inclusion:
12970 return "DW_TAG_common_inclusion";
12971 case DW_TAG_inheritance:
12972 return "DW_TAG_inheritance";
12973 case DW_TAG_inlined_subroutine:
12974 return "DW_TAG_inlined_subroutine";
12975 case DW_TAG_module:
12976 return "DW_TAG_module";
12977 case DW_TAG_ptr_to_member_type:
12978 return "DW_TAG_ptr_to_member_type";
12979 case DW_TAG_set_type:
12980 return "DW_TAG_set_type";
12981 case DW_TAG_subrange_type:
12982 return "DW_TAG_subrange_type";
12983 case DW_TAG_with_stmt:
12984 return "DW_TAG_with_stmt";
12985 case DW_TAG_access_declaration:
12986 return "DW_TAG_access_declaration";
12987 case DW_TAG_base_type:
12988 return "DW_TAG_base_type";
12989 case DW_TAG_catch_block:
12990 return "DW_TAG_catch_block";
12991 case DW_TAG_const_type:
12992 return "DW_TAG_const_type";
12993 case DW_TAG_constant:
12994 return "DW_TAG_constant";
12995 case DW_TAG_enumerator:
12996 return "DW_TAG_enumerator";
12997 case DW_TAG_file_type:
12998 return "DW_TAG_file_type";
12999 case DW_TAG_friend:
13000 return "DW_TAG_friend";
13001 case DW_TAG_namelist:
13002 return "DW_TAG_namelist";
13003 case DW_TAG_namelist_item:
13004 return "DW_TAG_namelist_item";
13005 case DW_TAG_packed_type:
13006 return "DW_TAG_packed_type";
13007 case DW_TAG_subprogram:
13008 return "DW_TAG_subprogram";
13009 case DW_TAG_template_type_param:
13010 return "DW_TAG_template_type_param";
13011 case DW_TAG_template_value_param:
13012 return "DW_TAG_template_value_param";
13013 case DW_TAG_thrown_type:
13014 return "DW_TAG_thrown_type";
13015 case DW_TAG_try_block:
13016 return "DW_TAG_try_block";
13017 case DW_TAG_variant_part:
13018 return "DW_TAG_variant_part";
13019 case DW_TAG_variable:
13020 return "DW_TAG_variable";
13021 case DW_TAG_volatile_type:
13022 return "DW_TAG_volatile_type";
d9fa45fe
DC
13023 case DW_TAG_dwarf_procedure:
13024 return "DW_TAG_dwarf_procedure";
13025 case DW_TAG_restrict_type:
13026 return "DW_TAG_restrict_type";
13027 case DW_TAG_interface_type:
13028 return "DW_TAG_interface_type";
13029 case DW_TAG_namespace:
13030 return "DW_TAG_namespace";
13031 case DW_TAG_imported_module:
13032 return "DW_TAG_imported_module";
13033 case DW_TAG_unspecified_type:
13034 return "DW_TAG_unspecified_type";
13035 case DW_TAG_partial_unit:
13036 return "DW_TAG_partial_unit";
13037 case DW_TAG_imported_unit:
13038 return "DW_TAG_imported_unit";
b7619582
GF
13039 case DW_TAG_condition:
13040 return "DW_TAG_condition";
13041 case DW_TAG_shared_type:
13042 return "DW_TAG_shared_type";
348e048f
DE
13043 case DW_TAG_type_unit:
13044 return "DW_TAG_type_unit";
c906108c
SS
13045 case DW_TAG_MIPS_loop:
13046 return "DW_TAG_MIPS_loop";
b7619582
GF
13047 case DW_TAG_HP_array_descriptor:
13048 return "DW_TAG_HP_array_descriptor";
c906108c
SS
13049 case DW_TAG_format_label:
13050 return "DW_TAG_format_label";
13051 case DW_TAG_function_template:
13052 return "DW_TAG_function_template";
13053 case DW_TAG_class_template:
13054 return "DW_TAG_class_template";
b7619582
GF
13055 case DW_TAG_GNU_BINCL:
13056 return "DW_TAG_GNU_BINCL";
13057 case DW_TAG_GNU_EINCL:
13058 return "DW_TAG_GNU_EINCL";
13059 case DW_TAG_upc_shared_type:
13060 return "DW_TAG_upc_shared_type";
13061 case DW_TAG_upc_strict_type:
13062 return "DW_TAG_upc_strict_type";
13063 case DW_TAG_upc_relaxed_type:
13064 return "DW_TAG_upc_relaxed_type";
13065 case DW_TAG_PGI_kanji_type:
13066 return "DW_TAG_PGI_kanji_type";
13067 case DW_TAG_PGI_interface_block:
13068 return "DW_TAG_PGI_interface_block";
96408a79
SA
13069 case DW_TAG_GNU_call_site:
13070 return "DW_TAG_GNU_call_site";
c906108c
SS
13071 default:
13072 return "DW_TAG_<unknown>";
13073 }
13074}
13075
13076/* Convert a DWARF attribute code into its string name. */
13077
13078static char *
aa1ee363 13079dwarf_attr_name (unsigned attr)
c906108c
SS
13080{
13081 switch (attr)
13082 {
13083 case DW_AT_sibling:
13084 return "DW_AT_sibling";
13085 case DW_AT_location:
13086 return "DW_AT_location";
13087 case DW_AT_name:
13088 return "DW_AT_name";
13089 case DW_AT_ordering:
13090 return "DW_AT_ordering";
13091 case DW_AT_subscr_data:
13092 return "DW_AT_subscr_data";
13093 case DW_AT_byte_size:
13094 return "DW_AT_byte_size";
13095 case DW_AT_bit_offset:
13096 return "DW_AT_bit_offset";
13097 case DW_AT_bit_size:
13098 return "DW_AT_bit_size";
13099 case DW_AT_element_list:
13100 return "DW_AT_element_list";
13101 case DW_AT_stmt_list:
13102 return "DW_AT_stmt_list";
13103 case DW_AT_low_pc:
13104 return "DW_AT_low_pc";
13105 case DW_AT_high_pc:
13106 return "DW_AT_high_pc";
13107 case DW_AT_language:
13108 return "DW_AT_language";
13109 case DW_AT_member:
13110 return "DW_AT_member";
13111 case DW_AT_discr:
13112 return "DW_AT_discr";
13113 case DW_AT_discr_value:
13114 return "DW_AT_discr_value";
13115 case DW_AT_visibility:
13116 return "DW_AT_visibility";
13117 case DW_AT_import:
13118 return "DW_AT_import";
13119 case DW_AT_string_length:
13120 return "DW_AT_string_length";
13121 case DW_AT_common_reference:
13122 return "DW_AT_common_reference";
13123 case DW_AT_comp_dir:
13124 return "DW_AT_comp_dir";
13125 case DW_AT_const_value:
13126 return "DW_AT_const_value";
13127 case DW_AT_containing_type:
13128 return "DW_AT_containing_type";
13129 case DW_AT_default_value:
13130 return "DW_AT_default_value";
13131 case DW_AT_inline:
13132 return "DW_AT_inline";
13133 case DW_AT_is_optional:
13134 return "DW_AT_is_optional";
13135 case DW_AT_lower_bound:
13136 return "DW_AT_lower_bound";
13137 case DW_AT_producer:
13138 return "DW_AT_producer";
13139 case DW_AT_prototyped:
13140 return "DW_AT_prototyped";
13141 case DW_AT_return_addr:
13142 return "DW_AT_return_addr";
13143 case DW_AT_start_scope:
13144 return "DW_AT_start_scope";
09fa0d7c
JK
13145 case DW_AT_bit_stride:
13146 return "DW_AT_bit_stride";
c906108c
SS
13147 case DW_AT_upper_bound:
13148 return "DW_AT_upper_bound";
13149 case DW_AT_abstract_origin:
13150 return "DW_AT_abstract_origin";
13151 case DW_AT_accessibility:
13152 return "DW_AT_accessibility";
13153 case DW_AT_address_class:
13154 return "DW_AT_address_class";
13155 case DW_AT_artificial:
13156 return "DW_AT_artificial";
13157 case DW_AT_base_types:
13158 return "DW_AT_base_types";
13159 case DW_AT_calling_convention:
13160 return "DW_AT_calling_convention";
13161 case DW_AT_count:
13162 return "DW_AT_count";
13163 case DW_AT_data_member_location:
13164 return "DW_AT_data_member_location";
13165 case DW_AT_decl_column:
13166 return "DW_AT_decl_column";
13167 case DW_AT_decl_file:
13168 return "DW_AT_decl_file";
13169 case DW_AT_decl_line:
13170 return "DW_AT_decl_line";
13171 case DW_AT_declaration:
13172 return "DW_AT_declaration";
13173 case DW_AT_discr_list:
13174 return "DW_AT_discr_list";
13175 case DW_AT_encoding:
13176 return "DW_AT_encoding";
13177 case DW_AT_external:
13178 return "DW_AT_external";
13179 case DW_AT_frame_base:
13180 return "DW_AT_frame_base";
13181 case DW_AT_friend:
13182 return "DW_AT_friend";
13183 case DW_AT_identifier_case:
13184 return "DW_AT_identifier_case";
13185 case DW_AT_macro_info:
13186 return "DW_AT_macro_info";
13187 case DW_AT_namelist_items:
13188 return "DW_AT_namelist_items";
13189 case DW_AT_priority:
13190 return "DW_AT_priority";
13191 case DW_AT_segment:
13192 return "DW_AT_segment";
13193 case DW_AT_specification:
13194 return "DW_AT_specification";
13195 case DW_AT_static_link:
13196 return "DW_AT_static_link";
13197 case DW_AT_type:
13198 return "DW_AT_type";
13199 case DW_AT_use_location:
13200 return "DW_AT_use_location";
13201 case DW_AT_variable_parameter:
13202 return "DW_AT_variable_parameter";
13203 case DW_AT_virtuality:
13204 return "DW_AT_virtuality";
13205 case DW_AT_vtable_elem_location:
13206 return "DW_AT_vtable_elem_location";
b7619582 13207 /* DWARF 3 values. */
d9fa45fe
DC
13208 case DW_AT_allocated:
13209 return "DW_AT_allocated";
13210 case DW_AT_associated:
13211 return "DW_AT_associated";
13212 case DW_AT_data_location:
13213 return "DW_AT_data_location";
09fa0d7c
JK
13214 case DW_AT_byte_stride:
13215 return "DW_AT_byte_stride";
d9fa45fe
DC
13216 case DW_AT_entry_pc:
13217 return "DW_AT_entry_pc";
13218 case DW_AT_use_UTF8:
13219 return "DW_AT_use_UTF8";
13220 case DW_AT_extension:
13221 return "DW_AT_extension";
13222 case DW_AT_ranges:
13223 return "DW_AT_ranges";
13224 case DW_AT_trampoline:
13225 return "DW_AT_trampoline";
13226 case DW_AT_call_column:
13227 return "DW_AT_call_column";
13228 case DW_AT_call_file:
13229 return "DW_AT_call_file";
13230 case DW_AT_call_line:
13231 return "DW_AT_call_line";
b7619582
GF
13232 case DW_AT_description:
13233 return "DW_AT_description";
13234 case DW_AT_binary_scale:
13235 return "DW_AT_binary_scale";
13236 case DW_AT_decimal_scale:
13237 return "DW_AT_decimal_scale";
13238 case DW_AT_small:
13239 return "DW_AT_small";
13240 case DW_AT_decimal_sign:
13241 return "DW_AT_decimal_sign";
13242 case DW_AT_digit_count:
13243 return "DW_AT_digit_count";
13244 case DW_AT_picture_string:
13245 return "DW_AT_picture_string";
13246 case DW_AT_mutable:
13247 return "DW_AT_mutable";
13248 case DW_AT_threads_scaled:
13249 return "DW_AT_threads_scaled";
13250 case DW_AT_explicit:
13251 return "DW_AT_explicit";
13252 case DW_AT_object_pointer:
13253 return "DW_AT_object_pointer";
13254 case DW_AT_endianity:
13255 return "DW_AT_endianity";
13256 case DW_AT_elemental:
13257 return "DW_AT_elemental";
13258 case DW_AT_pure:
13259 return "DW_AT_pure";
13260 case DW_AT_recursive:
13261 return "DW_AT_recursive";
348e048f
DE
13262 /* DWARF 4 values. */
13263 case DW_AT_signature:
13264 return "DW_AT_signature";
31ef98ae
TT
13265 case DW_AT_linkage_name:
13266 return "DW_AT_linkage_name";
b7619582 13267 /* SGI/MIPS extensions. */
c764a876 13268#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
13269 case DW_AT_MIPS_fde:
13270 return "DW_AT_MIPS_fde";
c764a876 13271#endif
c906108c
SS
13272 case DW_AT_MIPS_loop_begin:
13273 return "DW_AT_MIPS_loop_begin";
13274 case DW_AT_MIPS_tail_loop_begin:
13275 return "DW_AT_MIPS_tail_loop_begin";
13276 case DW_AT_MIPS_epilog_begin:
13277 return "DW_AT_MIPS_epilog_begin";
13278 case DW_AT_MIPS_loop_unroll_factor:
13279 return "DW_AT_MIPS_loop_unroll_factor";
13280 case DW_AT_MIPS_software_pipeline_depth:
13281 return "DW_AT_MIPS_software_pipeline_depth";
13282 case DW_AT_MIPS_linkage_name:
13283 return "DW_AT_MIPS_linkage_name";
b7619582
GF
13284 case DW_AT_MIPS_stride:
13285 return "DW_AT_MIPS_stride";
13286 case DW_AT_MIPS_abstract_name:
13287 return "DW_AT_MIPS_abstract_name";
13288 case DW_AT_MIPS_clone_origin:
13289 return "DW_AT_MIPS_clone_origin";
13290 case DW_AT_MIPS_has_inlines:
13291 return "DW_AT_MIPS_has_inlines";
b7619582 13292 /* HP extensions. */
c764a876 13293#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
13294 case DW_AT_HP_block_index:
13295 return "DW_AT_HP_block_index";
c764a876 13296#endif
b7619582
GF
13297 case DW_AT_HP_unmodifiable:
13298 return "DW_AT_HP_unmodifiable";
13299 case DW_AT_HP_actuals_stmt_list:
13300 return "DW_AT_HP_actuals_stmt_list";
13301 case DW_AT_HP_proc_per_section:
13302 return "DW_AT_HP_proc_per_section";
13303 case DW_AT_HP_raw_data_ptr:
13304 return "DW_AT_HP_raw_data_ptr";
13305 case DW_AT_HP_pass_by_reference:
13306 return "DW_AT_HP_pass_by_reference";
13307 case DW_AT_HP_opt_level:
13308 return "DW_AT_HP_opt_level";
13309 case DW_AT_HP_prof_version_id:
13310 return "DW_AT_HP_prof_version_id";
13311 case DW_AT_HP_opt_flags:
13312 return "DW_AT_HP_opt_flags";
13313 case DW_AT_HP_cold_region_low_pc:
13314 return "DW_AT_HP_cold_region_low_pc";
13315 case DW_AT_HP_cold_region_high_pc:
13316 return "DW_AT_HP_cold_region_high_pc";
13317 case DW_AT_HP_all_variables_modifiable:
13318 return "DW_AT_HP_all_variables_modifiable";
13319 case DW_AT_HP_linkage_name:
13320 return "DW_AT_HP_linkage_name";
13321 case DW_AT_HP_prof_flags:
13322 return "DW_AT_HP_prof_flags";
13323 /* GNU extensions. */
c906108c
SS
13324 case DW_AT_sf_names:
13325 return "DW_AT_sf_names";
13326 case DW_AT_src_info:
13327 return "DW_AT_src_info";
13328 case DW_AT_mac_info:
13329 return "DW_AT_mac_info";
13330 case DW_AT_src_coords:
13331 return "DW_AT_src_coords";
13332 case DW_AT_body_begin:
13333 return "DW_AT_body_begin";
13334 case DW_AT_body_end:
13335 return "DW_AT_body_end";
f5f8a009
EZ
13336 case DW_AT_GNU_vector:
13337 return "DW_AT_GNU_vector";
2de00c64
DE
13338 case DW_AT_GNU_odr_signature:
13339 return "DW_AT_GNU_odr_signature";
b7619582
GF
13340 /* VMS extensions. */
13341 case DW_AT_VMS_rtnbeg_pd_address:
13342 return "DW_AT_VMS_rtnbeg_pd_address";
13343 /* UPC extension. */
13344 case DW_AT_upc_threads_scaled:
13345 return "DW_AT_upc_threads_scaled";
13346 /* PGI (STMicroelectronics) extensions. */
13347 case DW_AT_PGI_lbase:
13348 return "DW_AT_PGI_lbase";
13349 case DW_AT_PGI_soffset:
13350 return "DW_AT_PGI_soffset";
13351 case DW_AT_PGI_lstride:
13352 return "DW_AT_PGI_lstride";
c906108c
SS
13353 default:
13354 return "DW_AT_<unknown>";
13355 }
13356}
13357
13358/* Convert a DWARF value form code into its string name. */
13359
13360static char *
aa1ee363 13361dwarf_form_name (unsigned form)
c906108c
SS
13362{
13363 switch (form)
13364 {
13365 case DW_FORM_addr:
13366 return "DW_FORM_addr";
13367 case DW_FORM_block2:
13368 return "DW_FORM_block2";
13369 case DW_FORM_block4:
13370 return "DW_FORM_block4";
13371 case DW_FORM_data2:
13372 return "DW_FORM_data2";
13373 case DW_FORM_data4:
13374 return "DW_FORM_data4";
13375 case DW_FORM_data8:
13376 return "DW_FORM_data8";
13377 case DW_FORM_string:
13378 return "DW_FORM_string";
13379 case DW_FORM_block:
13380 return "DW_FORM_block";
13381 case DW_FORM_block1:
13382 return "DW_FORM_block1";
13383 case DW_FORM_data1:
13384 return "DW_FORM_data1";
13385 case DW_FORM_flag:
13386 return "DW_FORM_flag";
13387 case DW_FORM_sdata:
13388 return "DW_FORM_sdata";
13389 case DW_FORM_strp:
13390 return "DW_FORM_strp";
13391 case DW_FORM_udata:
13392 return "DW_FORM_udata";
13393 case DW_FORM_ref_addr:
13394 return "DW_FORM_ref_addr";
13395 case DW_FORM_ref1:
13396 return "DW_FORM_ref1";
13397 case DW_FORM_ref2:
13398 return "DW_FORM_ref2";
13399 case DW_FORM_ref4:
13400 return "DW_FORM_ref4";
13401 case DW_FORM_ref8:
13402 return "DW_FORM_ref8";
13403 case DW_FORM_ref_udata:
13404 return "DW_FORM_ref_udata";
13405 case DW_FORM_indirect:
13406 return "DW_FORM_indirect";
348e048f
DE
13407 case DW_FORM_sec_offset:
13408 return "DW_FORM_sec_offset";
13409 case DW_FORM_exprloc:
13410 return "DW_FORM_exprloc";
13411 case DW_FORM_flag_present:
13412 return "DW_FORM_flag_present";
55f1336d
TT
13413 case DW_FORM_ref_sig8:
13414 return "DW_FORM_ref_sig8";
c906108c
SS
13415 default:
13416 return "DW_FORM_<unknown>";
13417 }
13418}
13419
13420/* Convert a DWARF stack opcode into its string name. */
13421
9eae7c52 13422const char *
b1bfef65 13423dwarf_stack_op_name (unsigned op)
c906108c
SS
13424{
13425 switch (op)
13426 {
13427 case DW_OP_addr:
13428 return "DW_OP_addr";
13429 case DW_OP_deref:
13430 return "DW_OP_deref";
13431 case DW_OP_const1u:
13432 return "DW_OP_const1u";
13433 case DW_OP_const1s:
13434 return "DW_OP_const1s";
13435 case DW_OP_const2u:
13436 return "DW_OP_const2u";
13437 case DW_OP_const2s:
13438 return "DW_OP_const2s";
13439 case DW_OP_const4u:
13440 return "DW_OP_const4u";
13441 case DW_OP_const4s:
13442 return "DW_OP_const4s";
13443 case DW_OP_const8u:
13444 return "DW_OP_const8u";
13445 case DW_OP_const8s:
13446 return "DW_OP_const8s";
13447 case DW_OP_constu:
13448 return "DW_OP_constu";
13449 case DW_OP_consts:
13450 return "DW_OP_consts";
13451 case DW_OP_dup:
13452 return "DW_OP_dup";
13453 case DW_OP_drop:
13454 return "DW_OP_drop";
13455 case DW_OP_over:
13456 return "DW_OP_over";
13457 case DW_OP_pick:
13458 return "DW_OP_pick";
13459 case DW_OP_swap:
13460 return "DW_OP_swap";
13461 case DW_OP_rot:
13462 return "DW_OP_rot";
13463 case DW_OP_xderef:
13464 return "DW_OP_xderef";
13465 case DW_OP_abs:
13466 return "DW_OP_abs";
13467 case DW_OP_and:
13468 return "DW_OP_and";
13469 case DW_OP_div:
13470 return "DW_OP_div";
13471 case DW_OP_minus:
13472 return "DW_OP_minus";
13473 case DW_OP_mod:
13474 return "DW_OP_mod";
13475 case DW_OP_mul:
13476 return "DW_OP_mul";
13477 case DW_OP_neg:
13478 return "DW_OP_neg";
13479 case DW_OP_not:
13480 return "DW_OP_not";
13481 case DW_OP_or:
13482 return "DW_OP_or";
13483 case DW_OP_plus:
13484 return "DW_OP_plus";
13485 case DW_OP_plus_uconst:
13486 return "DW_OP_plus_uconst";
13487 case DW_OP_shl:
13488 return "DW_OP_shl";
13489 case DW_OP_shr:
13490 return "DW_OP_shr";
13491 case DW_OP_shra:
13492 return "DW_OP_shra";
13493 case DW_OP_xor:
13494 return "DW_OP_xor";
13495 case DW_OP_bra:
13496 return "DW_OP_bra";
13497 case DW_OP_eq:
13498 return "DW_OP_eq";
13499 case DW_OP_ge:
13500 return "DW_OP_ge";
13501 case DW_OP_gt:
13502 return "DW_OP_gt";
13503 case DW_OP_le:
13504 return "DW_OP_le";
13505 case DW_OP_lt:
13506 return "DW_OP_lt";
13507 case DW_OP_ne:
13508 return "DW_OP_ne";
13509 case DW_OP_skip:
13510 return "DW_OP_skip";
13511 case DW_OP_lit0:
13512 return "DW_OP_lit0";
13513 case DW_OP_lit1:
13514 return "DW_OP_lit1";
13515 case DW_OP_lit2:
13516 return "DW_OP_lit2";
13517 case DW_OP_lit3:
13518 return "DW_OP_lit3";
13519 case DW_OP_lit4:
13520 return "DW_OP_lit4";
13521 case DW_OP_lit5:
13522 return "DW_OP_lit5";
13523 case DW_OP_lit6:
13524 return "DW_OP_lit6";
13525 case DW_OP_lit7:
13526 return "DW_OP_lit7";
13527 case DW_OP_lit8:
13528 return "DW_OP_lit8";
13529 case DW_OP_lit9:
13530 return "DW_OP_lit9";
13531 case DW_OP_lit10:
13532 return "DW_OP_lit10";
13533 case DW_OP_lit11:
13534 return "DW_OP_lit11";
13535 case DW_OP_lit12:
13536 return "DW_OP_lit12";
13537 case DW_OP_lit13:
13538 return "DW_OP_lit13";
13539 case DW_OP_lit14:
13540 return "DW_OP_lit14";
13541 case DW_OP_lit15:
13542 return "DW_OP_lit15";
13543 case DW_OP_lit16:
13544 return "DW_OP_lit16";
13545 case DW_OP_lit17:
13546 return "DW_OP_lit17";
13547 case DW_OP_lit18:
13548 return "DW_OP_lit18";
13549 case DW_OP_lit19:
13550 return "DW_OP_lit19";
13551 case DW_OP_lit20:
13552 return "DW_OP_lit20";
13553 case DW_OP_lit21:
13554 return "DW_OP_lit21";
13555 case DW_OP_lit22:
13556 return "DW_OP_lit22";
13557 case DW_OP_lit23:
13558 return "DW_OP_lit23";
13559 case DW_OP_lit24:
13560 return "DW_OP_lit24";
13561 case DW_OP_lit25:
13562 return "DW_OP_lit25";
13563 case DW_OP_lit26:
13564 return "DW_OP_lit26";
13565 case DW_OP_lit27:
13566 return "DW_OP_lit27";
13567 case DW_OP_lit28:
13568 return "DW_OP_lit28";
13569 case DW_OP_lit29:
13570 return "DW_OP_lit29";
13571 case DW_OP_lit30:
13572 return "DW_OP_lit30";
13573 case DW_OP_lit31:
13574 return "DW_OP_lit31";
13575 case DW_OP_reg0:
13576 return "DW_OP_reg0";
13577 case DW_OP_reg1:
13578 return "DW_OP_reg1";
13579 case DW_OP_reg2:
13580 return "DW_OP_reg2";
13581 case DW_OP_reg3:
13582 return "DW_OP_reg3";
13583 case DW_OP_reg4:
13584 return "DW_OP_reg4";
13585 case DW_OP_reg5:
13586 return "DW_OP_reg5";
13587 case DW_OP_reg6:
13588 return "DW_OP_reg6";
13589 case DW_OP_reg7:
13590 return "DW_OP_reg7";
13591 case DW_OP_reg8:
13592 return "DW_OP_reg8";
13593 case DW_OP_reg9:
13594 return "DW_OP_reg9";
13595 case DW_OP_reg10:
13596 return "DW_OP_reg10";
13597 case DW_OP_reg11:
13598 return "DW_OP_reg11";
13599 case DW_OP_reg12:
13600 return "DW_OP_reg12";
13601 case DW_OP_reg13:
13602 return "DW_OP_reg13";
13603 case DW_OP_reg14:
13604 return "DW_OP_reg14";
13605 case DW_OP_reg15:
13606 return "DW_OP_reg15";
13607 case DW_OP_reg16:
13608 return "DW_OP_reg16";
13609 case DW_OP_reg17:
13610 return "DW_OP_reg17";
13611 case DW_OP_reg18:
13612 return "DW_OP_reg18";
13613 case DW_OP_reg19:
13614 return "DW_OP_reg19";
13615 case DW_OP_reg20:
13616 return "DW_OP_reg20";
13617 case DW_OP_reg21:
13618 return "DW_OP_reg21";
13619 case DW_OP_reg22:
13620 return "DW_OP_reg22";
13621 case DW_OP_reg23:
13622 return "DW_OP_reg23";
13623 case DW_OP_reg24:
13624 return "DW_OP_reg24";
13625 case DW_OP_reg25:
13626 return "DW_OP_reg25";
13627 case DW_OP_reg26:
13628 return "DW_OP_reg26";
13629 case DW_OP_reg27:
13630 return "DW_OP_reg27";
13631 case DW_OP_reg28:
13632 return "DW_OP_reg28";
13633 case DW_OP_reg29:
13634 return "DW_OP_reg29";
13635 case DW_OP_reg30:
13636 return "DW_OP_reg30";
13637 case DW_OP_reg31:
13638 return "DW_OP_reg31";
13639 case DW_OP_breg0:
13640 return "DW_OP_breg0";
13641 case DW_OP_breg1:
13642 return "DW_OP_breg1";
13643 case DW_OP_breg2:
13644 return "DW_OP_breg2";
13645 case DW_OP_breg3:
13646 return "DW_OP_breg3";
13647 case DW_OP_breg4:
13648 return "DW_OP_breg4";
13649 case DW_OP_breg5:
13650 return "DW_OP_breg5";
13651 case DW_OP_breg6:
13652 return "DW_OP_breg6";
13653 case DW_OP_breg7:
13654 return "DW_OP_breg7";
13655 case DW_OP_breg8:
13656 return "DW_OP_breg8";
13657 case DW_OP_breg9:
13658 return "DW_OP_breg9";
13659 case DW_OP_breg10:
13660 return "DW_OP_breg10";
13661 case DW_OP_breg11:
13662 return "DW_OP_breg11";
13663 case DW_OP_breg12:
13664 return "DW_OP_breg12";
13665 case DW_OP_breg13:
13666 return "DW_OP_breg13";
13667 case DW_OP_breg14:
13668 return "DW_OP_breg14";
13669 case DW_OP_breg15:
13670 return "DW_OP_breg15";
13671 case DW_OP_breg16:
13672 return "DW_OP_breg16";
13673 case DW_OP_breg17:
13674 return "DW_OP_breg17";
13675 case DW_OP_breg18:
13676 return "DW_OP_breg18";
13677 case DW_OP_breg19:
13678 return "DW_OP_breg19";
13679 case DW_OP_breg20:
13680 return "DW_OP_breg20";
13681 case DW_OP_breg21:
13682 return "DW_OP_breg21";
13683 case DW_OP_breg22:
13684 return "DW_OP_breg22";
13685 case DW_OP_breg23:
13686 return "DW_OP_breg23";
13687 case DW_OP_breg24:
13688 return "DW_OP_breg24";
13689 case DW_OP_breg25:
13690 return "DW_OP_breg25";
13691 case DW_OP_breg26:
13692 return "DW_OP_breg26";
13693 case DW_OP_breg27:
13694 return "DW_OP_breg27";
13695 case DW_OP_breg28:
13696 return "DW_OP_breg28";
13697 case DW_OP_breg29:
13698 return "DW_OP_breg29";
13699 case DW_OP_breg30:
13700 return "DW_OP_breg30";
13701 case DW_OP_breg31:
13702 return "DW_OP_breg31";
13703 case DW_OP_regx:
13704 return "DW_OP_regx";
13705 case DW_OP_fbreg:
13706 return "DW_OP_fbreg";
13707 case DW_OP_bregx:
13708 return "DW_OP_bregx";
13709 case DW_OP_piece:
13710 return "DW_OP_piece";
13711 case DW_OP_deref_size:
13712 return "DW_OP_deref_size";
13713 case DW_OP_xderef_size:
13714 return "DW_OP_xderef_size";
13715 case DW_OP_nop:
13716 return "DW_OP_nop";
b7619582 13717 /* DWARF 3 extensions. */
ed348acc
EZ
13718 case DW_OP_push_object_address:
13719 return "DW_OP_push_object_address";
13720 case DW_OP_call2:
13721 return "DW_OP_call2";
13722 case DW_OP_call4:
13723 return "DW_OP_call4";
13724 case DW_OP_call_ref:
13725 return "DW_OP_call_ref";
b7619582
GF
13726 case DW_OP_form_tls_address:
13727 return "DW_OP_form_tls_address";
13728 case DW_OP_call_frame_cfa:
13729 return "DW_OP_call_frame_cfa";
13730 case DW_OP_bit_piece:
13731 return "DW_OP_bit_piece";
9eae7c52
TT
13732 /* DWARF 4 extensions. */
13733 case DW_OP_implicit_value:
13734 return "DW_OP_implicit_value";
13735 case DW_OP_stack_value:
13736 return "DW_OP_stack_value";
13737 /* GNU extensions. */
ed348acc
EZ
13738 case DW_OP_GNU_push_tls_address:
13739 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
13740 case DW_OP_GNU_uninit:
13741 return "DW_OP_GNU_uninit";
8cf6f0b1
TT
13742 case DW_OP_GNU_implicit_pointer:
13743 return "DW_OP_GNU_implicit_pointer";
8a9b8146
TT
13744 case DW_OP_GNU_entry_value:
13745 return "DW_OP_GNU_entry_value";
13746 case DW_OP_GNU_const_type:
13747 return "DW_OP_GNU_const_type";
13748 case DW_OP_GNU_regval_type:
13749 return "DW_OP_GNU_regval_type";
13750 case DW_OP_GNU_deref_type:
13751 return "DW_OP_GNU_deref_type";
13752 case DW_OP_GNU_convert:
13753 return "DW_OP_GNU_convert";
13754 case DW_OP_GNU_reinterpret:
13755 return "DW_OP_GNU_reinterpret";
c906108c 13756 default:
b1bfef65 13757 return NULL;
c906108c
SS
13758 }
13759}
13760
13761static char *
fba45db2 13762dwarf_bool_name (unsigned mybool)
c906108c
SS
13763{
13764 if (mybool)
13765 return "TRUE";
13766 else
13767 return "FALSE";
13768}
13769
13770/* Convert a DWARF type code into its string name. */
13771
13772static char *
aa1ee363 13773dwarf_type_encoding_name (unsigned enc)
c906108c
SS
13774{
13775 switch (enc)
13776 {
b7619582
GF
13777 case DW_ATE_void:
13778 return "DW_ATE_void";
c906108c
SS
13779 case DW_ATE_address:
13780 return "DW_ATE_address";
13781 case DW_ATE_boolean:
13782 return "DW_ATE_boolean";
13783 case DW_ATE_complex_float:
13784 return "DW_ATE_complex_float";
13785 case DW_ATE_float:
13786 return "DW_ATE_float";
13787 case DW_ATE_signed:
13788 return "DW_ATE_signed";
13789 case DW_ATE_signed_char:
13790 return "DW_ATE_signed_char";
13791 case DW_ATE_unsigned:
13792 return "DW_ATE_unsigned";
13793 case DW_ATE_unsigned_char:
13794 return "DW_ATE_unsigned_char";
b7619582 13795 /* DWARF 3. */
d9fa45fe
DC
13796 case DW_ATE_imaginary_float:
13797 return "DW_ATE_imaginary_float";
b7619582
GF
13798 case DW_ATE_packed_decimal:
13799 return "DW_ATE_packed_decimal";
13800 case DW_ATE_numeric_string:
13801 return "DW_ATE_numeric_string";
13802 case DW_ATE_edited:
13803 return "DW_ATE_edited";
13804 case DW_ATE_signed_fixed:
13805 return "DW_ATE_signed_fixed";
13806 case DW_ATE_unsigned_fixed:
13807 return "DW_ATE_unsigned_fixed";
13808 case DW_ATE_decimal_float:
13809 return "DW_ATE_decimal_float";
75079b2b
TT
13810 /* DWARF 4. */
13811 case DW_ATE_UTF:
13812 return "DW_ATE_UTF";
b7619582
GF
13813 /* HP extensions. */
13814 case DW_ATE_HP_float80:
13815 return "DW_ATE_HP_float80";
13816 case DW_ATE_HP_complex_float80:
13817 return "DW_ATE_HP_complex_float80";
13818 case DW_ATE_HP_float128:
13819 return "DW_ATE_HP_float128";
13820 case DW_ATE_HP_complex_float128:
13821 return "DW_ATE_HP_complex_float128";
13822 case DW_ATE_HP_floathpintel:
13823 return "DW_ATE_HP_floathpintel";
13824 case DW_ATE_HP_imaginary_float80:
13825 return "DW_ATE_HP_imaginary_float80";
13826 case DW_ATE_HP_imaginary_float128:
13827 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
13828 default:
13829 return "DW_ATE_<unknown>";
13830 }
13831}
13832
0963b4bd 13833/* Convert a DWARF call frame info operation to its string name. */
c906108c
SS
13834
13835#if 0
13836static char *
aa1ee363 13837dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
13838{
13839 switch (cfi_opc)
13840 {
13841 case DW_CFA_advance_loc:
13842 return "DW_CFA_advance_loc";
13843 case DW_CFA_offset:
13844 return "DW_CFA_offset";
13845 case DW_CFA_restore:
13846 return "DW_CFA_restore";
13847 case DW_CFA_nop:
13848 return "DW_CFA_nop";
13849 case DW_CFA_set_loc:
13850 return "DW_CFA_set_loc";
13851 case DW_CFA_advance_loc1:
13852 return "DW_CFA_advance_loc1";
13853 case DW_CFA_advance_loc2:
13854 return "DW_CFA_advance_loc2";
13855 case DW_CFA_advance_loc4:
13856 return "DW_CFA_advance_loc4";
13857 case DW_CFA_offset_extended:
13858 return "DW_CFA_offset_extended";
13859 case DW_CFA_restore_extended:
13860 return "DW_CFA_restore_extended";
13861 case DW_CFA_undefined:
13862 return "DW_CFA_undefined";
13863 case DW_CFA_same_value:
13864 return "DW_CFA_same_value";
13865 case DW_CFA_register:
13866 return "DW_CFA_register";
13867 case DW_CFA_remember_state:
13868 return "DW_CFA_remember_state";
13869 case DW_CFA_restore_state:
13870 return "DW_CFA_restore_state";
13871 case DW_CFA_def_cfa:
13872 return "DW_CFA_def_cfa";
13873 case DW_CFA_def_cfa_register:
13874 return "DW_CFA_def_cfa_register";
13875 case DW_CFA_def_cfa_offset:
13876 return "DW_CFA_def_cfa_offset";
b7619582 13877 /* DWARF 3. */
985cb1a3
JM
13878 case DW_CFA_def_cfa_expression:
13879 return "DW_CFA_def_cfa_expression";
13880 case DW_CFA_expression:
13881 return "DW_CFA_expression";
13882 case DW_CFA_offset_extended_sf:
13883 return "DW_CFA_offset_extended_sf";
13884 case DW_CFA_def_cfa_sf:
13885 return "DW_CFA_def_cfa_sf";
13886 case DW_CFA_def_cfa_offset_sf:
13887 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
13888 case DW_CFA_val_offset:
13889 return "DW_CFA_val_offset";
13890 case DW_CFA_val_offset_sf:
13891 return "DW_CFA_val_offset_sf";
13892 case DW_CFA_val_expression:
13893 return "DW_CFA_val_expression";
13894 /* SGI/MIPS specific. */
c906108c
SS
13895 case DW_CFA_MIPS_advance_loc8:
13896 return "DW_CFA_MIPS_advance_loc8";
b7619582 13897 /* GNU extensions. */
985cb1a3
JM
13898 case DW_CFA_GNU_window_save:
13899 return "DW_CFA_GNU_window_save";
13900 case DW_CFA_GNU_args_size:
13901 return "DW_CFA_GNU_args_size";
13902 case DW_CFA_GNU_negative_offset_extended:
13903 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
13904 default:
13905 return "DW_CFA_<unknown>";
13906 }
13907}
13908#endif
13909
f9aca02d 13910static void
d97bc12b 13911dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
13912{
13913 unsigned int i;
13914
d97bc12b
DE
13915 print_spaces (indent, f);
13916 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 13917 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
13918
13919 if (die->parent != NULL)
13920 {
13921 print_spaces (indent, f);
13922 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13923 die->parent->offset);
13924 }
13925
13926 print_spaces (indent, f);
13927 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 13928 dwarf_bool_name (die->child != NULL));
c906108c 13929
d97bc12b
DE
13930 print_spaces (indent, f);
13931 fprintf_unfiltered (f, " attributes:\n");
13932
c906108c
SS
13933 for (i = 0; i < die->num_attrs; ++i)
13934 {
d97bc12b
DE
13935 print_spaces (indent, f);
13936 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
13937 dwarf_attr_name (die->attrs[i].name),
13938 dwarf_form_name (die->attrs[i].form));
d97bc12b 13939
c906108c
SS
13940 switch (die->attrs[i].form)
13941 {
13942 case DW_FORM_ref_addr:
13943 case DW_FORM_addr:
d97bc12b 13944 fprintf_unfiltered (f, "address: ");
5af949e3 13945 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
13946 break;
13947 case DW_FORM_block2:
13948 case DW_FORM_block4:
13949 case DW_FORM_block:
13950 case DW_FORM_block1:
3e43a32a
MS
13951 fprintf_unfiltered (f, "block: size %d",
13952 DW_BLOCK (&die->attrs[i])->size);
c906108c 13953 break;
2dc7f7b3
TT
13954 case DW_FORM_exprloc:
13955 fprintf_unfiltered (f, "expression: size %u",
13956 DW_BLOCK (&die->attrs[i])->size);
13957 break;
10b3939b
DJ
13958 case DW_FORM_ref1:
13959 case DW_FORM_ref2:
13960 case DW_FORM_ref4:
d97bc12b 13961 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
13962 (long) (DW_ADDR (&die->attrs[i])));
13963 break;
c906108c
SS
13964 case DW_FORM_data1:
13965 case DW_FORM_data2:
13966 case DW_FORM_data4:
ce5d95e1 13967 case DW_FORM_data8:
c906108c
SS
13968 case DW_FORM_udata:
13969 case DW_FORM_sdata:
43bbcdc2
PH
13970 fprintf_unfiltered (f, "constant: %s",
13971 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 13972 break;
2dc7f7b3
TT
13973 case DW_FORM_sec_offset:
13974 fprintf_unfiltered (f, "section offset: %s",
13975 pulongest (DW_UNSND (&die->attrs[i])));
13976 break;
55f1336d 13977 case DW_FORM_ref_sig8:
348e048f
DE
13978 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13979 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b3c8eb43 13980 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
348e048f
DE
13981 else
13982 fprintf_unfiltered (f, "signatured type, offset: unknown");
13983 break;
c906108c 13984 case DW_FORM_string:
4bdf3d34 13985 case DW_FORM_strp:
8285870a 13986 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 13987 DW_STRING (&die->attrs[i])
8285870a
JK
13988 ? DW_STRING (&die->attrs[i]) : "",
13989 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
13990 break;
13991 case DW_FORM_flag:
13992 if (DW_UNSND (&die->attrs[i]))
d97bc12b 13993 fprintf_unfiltered (f, "flag: TRUE");
c906108c 13994 else
d97bc12b 13995 fprintf_unfiltered (f, "flag: FALSE");
c906108c 13996 break;
2dc7f7b3
TT
13997 case DW_FORM_flag_present:
13998 fprintf_unfiltered (f, "flag: TRUE");
13999 break;
a8329558 14000 case DW_FORM_indirect:
0963b4bd
MS
14001 /* The reader will have reduced the indirect form to
14002 the "base form" so this form should not occur. */
3e43a32a
MS
14003 fprintf_unfiltered (f,
14004 "unexpected attribute form: DW_FORM_indirect");
a8329558 14005 break;
c906108c 14006 default:
d97bc12b 14007 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14008 die->attrs[i].form);
d97bc12b 14009 break;
c906108c 14010 }
d97bc12b 14011 fprintf_unfiltered (f, "\n");
c906108c
SS
14012 }
14013}
14014
f9aca02d 14015static void
d97bc12b 14016dump_die_for_error (struct die_info *die)
c906108c 14017{
d97bc12b
DE
14018 dump_die_shallow (gdb_stderr, 0, die);
14019}
14020
14021static void
14022dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14023{
14024 int indent = level * 4;
14025
14026 gdb_assert (die != NULL);
14027
14028 if (level >= max_level)
14029 return;
14030
14031 dump_die_shallow (f, indent, die);
14032
14033 if (die->child != NULL)
c906108c 14034 {
d97bc12b
DE
14035 print_spaces (indent, f);
14036 fprintf_unfiltered (f, " Children:");
14037 if (level + 1 < max_level)
14038 {
14039 fprintf_unfiltered (f, "\n");
14040 dump_die_1 (f, level + 1, max_level, die->child);
14041 }
14042 else
14043 {
3e43a32a
MS
14044 fprintf_unfiltered (f,
14045 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14046 }
14047 }
14048
14049 if (die->sibling != NULL && level > 0)
14050 {
14051 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14052 }
14053}
14054
d97bc12b
DE
14055/* This is called from the pdie macro in gdbinit.in.
14056 It's not static so gcc will keep a copy callable from gdb. */
14057
14058void
14059dump_die (struct die_info *die, int max_level)
14060{
14061 dump_die_1 (gdb_stdlog, 0, max_level, die);
14062}
14063
f9aca02d 14064static void
51545339 14065store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14066{
51545339 14067 void **slot;
c906108c 14068
51545339
DJ
14069 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14070
14071 *slot = die;
c906108c
SS
14072}
14073
93311388
DE
14074static int
14075is_ref_attr (struct attribute *attr)
c906108c 14076{
c906108c
SS
14077 switch (attr->form)
14078 {
14079 case DW_FORM_ref_addr:
c906108c
SS
14080 case DW_FORM_ref1:
14081 case DW_FORM_ref2:
14082 case DW_FORM_ref4:
613e1657 14083 case DW_FORM_ref8:
c906108c 14084 case DW_FORM_ref_udata:
93311388 14085 return 1;
c906108c 14086 default:
93311388 14087 return 0;
c906108c 14088 }
93311388
DE
14089}
14090
14091static unsigned int
14092dwarf2_get_ref_die_offset (struct attribute *attr)
14093{
14094 if (is_ref_attr (attr))
14095 return DW_ADDR (attr);
14096
14097 complaint (&symfile_complaints,
14098 _("unsupported die ref attribute form: '%s'"),
14099 dwarf_form_name (attr->form));
14100 return 0;
c906108c
SS
14101}
14102
43bbcdc2
PH
14103/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14104 * the value held by the attribute is not constant. */
a02abb62 14105
43bbcdc2 14106static LONGEST
a02abb62
JB
14107dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14108{
14109 if (attr->form == DW_FORM_sdata)
14110 return DW_SND (attr);
14111 else if (attr->form == DW_FORM_udata
14112 || attr->form == DW_FORM_data1
14113 || attr->form == DW_FORM_data2
14114 || attr->form == DW_FORM_data4
14115 || attr->form == DW_FORM_data8)
14116 return DW_UNSND (attr);
14117 else
14118 {
3e43a32a
MS
14119 complaint (&symfile_complaints,
14120 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14121 dwarf_form_name (attr->form));
14122 return default_value;
14123 }
14124}
14125
03dd20cc 14126/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14127 unit and add it to our queue.
14128 The result is non-zero if PER_CU was queued, otherwise the result is zero
14129 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14130
348e048f 14131static int
03dd20cc
DJ
14132maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14133 struct dwarf2_per_cu_data *per_cu)
14134{
98bfdba5
PA
14135 /* We may arrive here during partial symbol reading, if we need full
14136 DIEs to process an unusual case (e.g. template arguments). Do
14137 not queue PER_CU, just tell our caller to load its DIEs. */
14138 if (dwarf2_per_objfile->reading_partial_symbols)
14139 {
14140 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14141 return 1;
14142 return 0;
14143 }
14144
03dd20cc
DJ
14145 /* Mark the dependence relation so that we don't flush PER_CU
14146 too early. */
14147 dwarf2_add_dependence (this_cu, per_cu);
14148
14149 /* If it's already on the queue, we have nothing to do. */
14150 if (per_cu->queued)
348e048f 14151 return 0;
03dd20cc
DJ
14152
14153 /* If the compilation unit is already loaded, just mark it as
14154 used. */
14155 if (per_cu->cu != NULL)
14156 {
14157 per_cu->cu->last_used = 0;
348e048f 14158 return 0;
03dd20cc
DJ
14159 }
14160
14161 /* Add it to the queue. */
14162 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
14163
14164 return 1;
14165}
14166
14167/* Follow reference or signature attribute ATTR of SRC_DIE.
14168 On entry *REF_CU is the CU of SRC_DIE.
14169 On exit *REF_CU is the CU of the result. */
14170
14171static struct die_info *
14172follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14173 struct dwarf2_cu **ref_cu)
14174{
14175 struct die_info *die;
14176
14177 if (is_ref_attr (attr))
14178 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14179 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14180 die = follow_die_sig (src_die, attr, ref_cu);
14181 else
14182 {
14183 dump_die_for_error (src_die);
14184 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14185 (*ref_cu)->objfile->name);
14186 }
14187
14188 return die;
03dd20cc
DJ
14189}
14190
5c631832 14191/* Follow reference OFFSET.
673bfd45
DE
14192 On entry *REF_CU is the CU of the source die referencing OFFSET.
14193 On exit *REF_CU is the CU of the result.
14194 Returns NULL if OFFSET is invalid. */
f504f079 14195
f9aca02d 14196static struct die_info *
5c631832 14197follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 14198{
10b3939b 14199 struct die_info temp_die;
f2f0e013 14200 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14201
348e048f
DE
14202 gdb_assert (cu->per_cu != NULL);
14203
98bfdba5
PA
14204 target_cu = cu;
14205
8b70b953 14206 if (cu->per_cu->debug_type_section)
348e048f
DE
14207 {
14208 /* .debug_types CUs cannot reference anything outside their CU.
14209 If they need to, they have to reference a signatured type via
55f1336d 14210 DW_FORM_ref_sig8. */
348e048f 14211 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14212 return NULL;
348e048f
DE
14213 }
14214 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14215 {
14216 struct dwarf2_per_cu_data *per_cu;
9a619af0 14217
45452591 14218 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14219
14220 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
14221 if (maybe_queue_comp_unit (cu, per_cu))
14222 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 14223
10b3939b
DJ
14224 target_cu = per_cu->cu;
14225 }
98bfdba5
PA
14226 else if (cu->dies == NULL)
14227 {
14228 /* We're loading full DIEs during partial symbol reading. */
14229 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14230 load_full_comp_unit (cu->per_cu, cu->objfile);
14231 }
c906108c 14232
f2f0e013 14233 *ref_cu = target_cu;
51545339 14234 temp_die.offset = offset;
5c631832
JK
14235 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14236}
10b3939b 14237
5c631832
JK
14238/* Follow reference attribute ATTR of SRC_DIE.
14239 On entry *REF_CU is the CU of SRC_DIE.
14240 On exit *REF_CU is the CU of the result. */
14241
14242static struct die_info *
14243follow_die_ref (struct die_info *src_die, struct attribute *attr,
14244 struct dwarf2_cu **ref_cu)
14245{
14246 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14247 struct dwarf2_cu *cu = *ref_cu;
14248 struct die_info *die;
14249
14250 die = follow_die_offset (offset, ref_cu);
14251 if (!die)
14252 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14253 "at 0x%x [in module %s]"),
14254 offset, src_die->offset, cu->objfile->name);
348e048f 14255
5c631832
JK
14256 return die;
14257}
14258
d83e736b
JK
14259/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14260 Returned value is intended for DW_OP_call*. Returned
14261 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14262
14263struct dwarf2_locexpr_baton
14264dwarf2_fetch_die_location_block (unsigned int offset,
8cf6f0b1
TT
14265 struct dwarf2_per_cu_data *per_cu,
14266 CORE_ADDR (*get_frame_pc) (void *baton),
14267 void *baton)
5c631832 14268{
918dd910 14269 struct dwarf2_cu *cu;
5c631832
JK
14270 struct die_info *die;
14271 struct attribute *attr;
14272 struct dwarf2_locexpr_baton retval;
14273
8cf6f0b1
TT
14274 dw2_setup (per_cu->objfile);
14275
918dd910
JK
14276 if (per_cu->cu == NULL)
14277 load_cu (per_cu);
14278 cu = per_cu->cu;
14279
5c631832
JK
14280 die = follow_die_offset (offset, &cu);
14281 if (!die)
14282 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14283 offset, per_cu->cu->objfile->name);
14284
14285 attr = dwarf2_attr (die, DW_AT_location, cu);
14286 if (!attr)
14287 {
e103e986
JK
14288 /* DWARF: "If there is no such attribute, then there is no effect.".
14289 DATA is ignored if SIZE is 0. */
5c631832 14290
e103e986 14291 retval.data = NULL;
5c631832
JK
14292 retval.size = 0;
14293 }
8cf6f0b1
TT
14294 else if (attr_form_is_section_offset (attr))
14295 {
14296 struct dwarf2_loclist_baton loclist_baton;
14297 CORE_ADDR pc = (*get_frame_pc) (baton);
14298 size_t size;
14299
14300 fill_in_loclist_baton (cu, &loclist_baton, attr);
14301
14302 retval.data = dwarf2_find_location_expression (&loclist_baton,
14303 &size, pc);
14304 retval.size = size;
14305 }
5c631832
JK
14306 else
14307 {
14308 if (!attr_form_is_block (attr))
14309 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14310 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14311 offset, per_cu->cu->objfile->name);
14312
14313 retval.data = DW_BLOCK (attr)->data;
14314 retval.size = DW_BLOCK (attr)->size;
14315 }
14316 retval.per_cu = cu->per_cu;
918dd910 14317
918dd910
JK
14318 age_cached_comp_units ();
14319
5c631832 14320 return retval;
348e048f
DE
14321}
14322
8a9b8146
TT
14323/* Return the type of the DIE at DIE_OFFSET in the CU named by
14324 PER_CU. */
14325
14326struct type *
14327dwarf2_get_die_type (unsigned int die_offset,
14328 struct dwarf2_per_cu_data *per_cu)
14329{
8a9b8146 14330 dw2_setup (per_cu->objfile);
9ff3b74f 14331 return get_die_type_at_offset (die_offset, per_cu);
8a9b8146
TT
14332}
14333
348e048f
DE
14334/* Follow the signature attribute ATTR in SRC_DIE.
14335 On entry *REF_CU is the CU of SRC_DIE.
14336 On exit *REF_CU is the CU of the result. */
14337
14338static struct die_info *
14339follow_die_sig (struct die_info *src_die, struct attribute *attr,
14340 struct dwarf2_cu **ref_cu)
14341{
14342 struct objfile *objfile = (*ref_cu)->objfile;
14343 struct die_info temp_die;
14344 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14345 struct dwarf2_cu *sig_cu;
14346 struct die_info *die;
14347
14348 /* sig_type will be NULL if the signatured type is missing from
14349 the debug info. */
14350 if (sig_type == NULL)
14351 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14352 "at 0x%x [in module %s]"),
14353 src_die->offset, objfile->name);
14354
14355 /* If necessary, add it to the queue and load its DIEs. */
14356
14357 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14358 read_signatured_type (objfile, sig_type);
14359
14360 gdb_assert (sig_type->per_cu.cu != NULL);
14361
14362 sig_cu = sig_type->per_cu.cu;
14363 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14364 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14365 if (die)
14366 {
14367 *ref_cu = sig_cu;
14368 return die;
14369 }
14370
3e43a32a
MS
14371 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14372 "from DIE at 0x%x [in module %s]"),
348e048f
DE
14373 sig_type->type_offset, src_die->offset, objfile->name);
14374}
14375
14376/* Given an offset of a signatured type, return its signatured_type. */
14377
14378static struct signatured_type *
8b70b953
TT
14379lookup_signatured_type_at_offset (struct objfile *objfile,
14380 struct dwarf2_section_info *section,
14381 unsigned int offset)
348e048f 14382{
8b70b953 14383 gdb_byte *info_ptr = section->buffer + offset;
348e048f
DE
14384 unsigned int length, initial_length_size;
14385 unsigned int sig_offset;
14386 struct signatured_type find_entry, *type_sig;
14387
14388 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14389 sig_offset = (initial_length_size
14390 + 2 /*version*/
14391 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14392 + 1 /*address_size*/);
14393 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14394 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14395
14396 /* This is only used to lookup previously recorded types.
14397 If we didn't find it, it's our bug. */
14398 gdb_assert (type_sig != NULL);
b3c8eb43 14399 gdb_assert (offset == type_sig->per_cu.offset);
348e048f
DE
14400
14401 return type_sig;
14402}
14403
14404/* Read in signatured type at OFFSET and build its CU and die(s). */
14405
14406static void
14407read_signatured_type_at_offset (struct objfile *objfile,
8b70b953 14408 struct dwarf2_section_info *sect,
348e048f
DE
14409 unsigned int offset)
14410{
14411 struct signatured_type *type_sig;
14412
8b70b953 14413 dwarf2_read_section (objfile, sect);
be391dca 14414
348e048f
DE
14415 /* We have the section offset, but we need the signature to do the
14416 hash table lookup. */
8b70b953 14417 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f
DE
14418
14419 gdb_assert (type_sig->per_cu.cu == NULL);
14420
14421 read_signatured_type (objfile, type_sig);
14422
14423 gdb_assert (type_sig->per_cu.cu != NULL);
14424}
14425
14426/* Read in a signatured type and build its CU and DIEs. */
14427
14428static void
14429read_signatured_type (struct objfile *objfile,
14430 struct signatured_type *type_sig)
14431{
1fd400ff 14432 gdb_byte *types_ptr;
348e048f
DE
14433 struct die_reader_specs reader_specs;
14434 struct dwarf2_cu *cu;
14435 ULONGEST signature;
14436 struct cleanup *back_to, *free_cu_cleanup;
8b70b953 14437 struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
348e048f 14438
8b70b953
TT
14439 dwarf2_read_section (objfile, section);
14440 types_ptr = section->buffer + type_sig->per_cu.offset;
1fd400ff 14441
348e048f
DE
14442 gdb_assert (type_sig->per_cu.cu == NULL);
14443
9816fde3
JK
14444 cu = xmalloc (sizeof (*cu));
14445 init_one_comp_unit (cu, objfile);
14446
348e048f
DE
14447 type_sig->per_cu.cu = cu;
14448 cu->per_cu = &type_sig->per_cu;
14449
14450 /* If an error occurs while loading, release our storage. */
14451 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14452
8b70b953 14453 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
348e048f
DE
14454 types_ptr, objfile->obfd);
14455 gdb_assert (signature == type_sig->signature);
14456
14457 cu->die_hash
14458 = htab_create_alloc_ex (cu->header.length / 12,
14459 die_hash,
14460 die_eq,
14461 NULL,
14462 &cu->comp_unit_obstack,
14463 hashtab_obstack_allocate,
14464 dummy_obstack_deallocate);
14465
14466 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14467 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14468
14469 init_cu_die_reader (&reader_specs, cu);
14470
14471 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14472 NULL /*parent*/);
14473
14474 /* We try not to read any attributes in this function, because not
14475 all objfiles needed for references have been loaded yet, and symbol
14476 table processing isn't initialized. But we have to set the CU language,
14477 or we won't be able to build types correctly. */
9816fde3 14478 prepare_one_comp_unit (cu, cu->dies);
348e048f
DE
14479
14480 do_cleanups (back_to);
14481
14482 /* We've successfully allocated this compilation unit. Let our caller
14483 clean it up when finished with it. */
14484 discard_cleanups (free_cu_cleanup);
14485
14486 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14487 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
14488}
14489
c906108c
SS
14490/* Decode simple location descriptions.
14491 Given a pointer to a dwarf block that defines a location, compute
14492 the location and return the value.
14493
4cecd739
DJ
14494 NOTE drow/2003-11-18: This function is called in two situations
14495 now: for the address of static or global variables (partial symbols
14496 only) and for offsets into structures which are expected to be
14497 (more or less) constant. The partial symbol case should go away,
14498 and only the constant case should remain. That will let this
14499 function complain more accurately. A few special modes are allowed
14500 without complaint for global variables (for instance, global
14501 register values and thread-local values).
c906108c
SS
14502
14503 A location description containing no operations indicates that the
4cecd739 14504 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14505 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14506 callers will only want a very basic result and this can become a
21ae7a4d
JK
14507 complaint.
14508
14509 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14510
14511static CORE_ADDR
e7c27a73 14512decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14513{
e7c27a73 14514 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14515 int i;
14516 int size = blk->size;
14517 gdb_byte *data = blk->data;
14518 CORE_ADDR stack[64];
14519 int stacki;
14520 unsigned int bytes_read, unsnd;
14521 gdb_byte op;
c906108c 14522
21ae7a4d
JK
14523 i = 0;
14524 stacki = 0;
14525 stack[stacki] = 0;
14526 stack[++stacki] = 0;
14527
14528 while (i < size)
14529 {
14530 op = data[i++];
14531 switch (op)
14532 {
14533 case DW_OP_lit0:
14534 case DW_OP_lit1:
14535 case DW_OP_lit2:
14536 case DW_OP_lit3:
14537 case DW_OP_lit4:
14538 case DW_OP_lit5:
14539 case DW_OP_lit6:
14540 case DW_OP_lit7:
14541 case DW_OP_lit8:
14542 case DW_OP_lit9:
14543 case DW_OP_lit10:
14544 case DW_OP_lit11:
14545 case DW_OP_lit12:
14546 case DW_OP_lit13:
14547 case DW_OP_lit14:
14548 case DW_OP_lit15:
14549 case DW_OP_lit16:
14550 case DW_OP_lit17:
14551 case DW_OP_lit18:
14552 case DW_OP_lit19:
14553 case DW_OP_lit20:
14554 case DW_OP_lit21:
14555 case DW_OP_lit22:
14556 case DW_OP_lit23:
14557 case DW_OP_lit24:
14558 case DW_OP_lit25:
14559 case DW_OP_lit26:
14560 case DW_OP_lit27:
14561 case DW_OP_lit28:
14562 case DW_OP_lit29:
14563 case DW_OP_lit30:
14564 case DW_OP_lit31:
14565 stack[++stacki] = op - DW_OP_lit0;
14566 break;
f1bea926 14567
21ae7a4d
JK
14568 case DW_OP_reg0:
14569 case DW_OP_reg1:
14570 case DW_OP_reg2:
14571 case DW_OP_reg3:
14572 case DW_OP_reg4:
14573 case DW_OP_reg5:
14574 case DW_OP_reg6:
14575 case DW_OP_reg7:
14576 case DW_OP_reg8:
14577 case DW_OP_reg9:
14578 case DW_OP_reg10:
14579 case DW_OP_reg11:
14580 case DW_OP_reg12:
14581 case DW_OP_reg13:
14582 case DW_OP_reg14:
14583 case DW_OP_reg15:
14584 case DW_OP_reg16:
14585 case DW_OP_reg17:
14586 case DW_OP_reg18:
14587 case DW_OP_reg19:
14588 case DW_OP_reg20:
14589 case DW_OP_reg21:
14590 case DW_OP_reg22:
14591 case DW_OP_reg23:
14592 case DW_OP_reg24:
14593 case DW_OP_reg25:
14594 case DW_OP_reg26:
14595 case DW_OP_reg27:
14596 case DW_OP_reg28:
14597 case DW_OP_reg29:
14598 case DW_OP_reg30:
14599 case DW_OP_reg31:
14600 stack[++stacki] = op - DW_OP_reg0;
14601 if (i < size)
14602 dwarf2_complex_location_expr_complaint ();
14603 break;
c906108c 14604
21ae7a4d
JK
14605 case DW_OP_regx:
14606 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14607 i += bytes_read;
14608 stack[++stacki] = unsnd;
14609 if (i < size)
14610 dwarf2_complex_location_expr_complaint ();
14611 break;
c906108c 14612
21ae7a4d
JK
14613 case DW_OP_addr:
14614 stack[++stacki] = read_address (objfile->obfd, &data[i],
14615 cu, &bytes_read);
14616 i += bytes_read;
14617 break;
d53d4ac5 14618
21ae7a4d
JK
14619 case DW_OP_const1u:
14620 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14621 i += 1;
14622 break;
14623
14624 case DW_OP_const1s:
14625 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14626 i += 1;
14627 break;
14628
14629 case DW_OP_const2u:
14630 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14631 i += 2;
14632 break;
14633
14634 case DW_OP_const2s:
14635 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14636 i += 2;
14637 break;
d53d4ac5 14638
21ae7a4d
JK
14639 case DW_OP_const4u:
14640 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14641 i += 4;
14642 break;
14643
14644 case DW_OP_const4s:
14645 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14646 i += 4;
14647 break;
14648
14649 case DW_OP_constu:
14650 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14651 &bytes_read);
14652 i += bytes_read;
14653 break;
14654
14655 case DW_OP_consts:
14656 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14657 i += bytes_read;
14658 break;
14659
14660 case DW_OP_dup:
14661 stack[stacki + 1] = stack[stacki];
14662 stacki++;
14663 break;
14664
14665 case DW_OP_plus:
14666 stack[stacki - 1] += stack[stacki];
14667 stacki--;
14668 break;
14669
14670 case DW_OP_plus_uconst:
14671 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14672 &bytes_read);
14673 i += bytes_read;
14674 break;
14675
14676 case DW_OP_minus:
14677 stack[stacki - 1] -= stack[stacki];
14678 stacki--;
14679 break;
14680
14681 case DW_OP_deref:
14682 /* If we're not the last op, then we definitely can't encode
14683 this using GDB's address_class enum. This is valid for partial
14684 global symbols, although the variable's address will be bogus
14685 in the psymtab. */
14686 if (i < size)
14687 dwarf2_complex_location_expr_complaint ();
14688 break;
14689
14690 case DW_OP_GNU_push_tls_address:
14691 /* The top of the stack has the offset from the beginning
14692 of the thread control block at which the variable is located. */
14693 /* Nothing should follow this operator, so the top of stack would
14694 be returned. */
14695 /* This is valid for partial global symbols, but the variable's
14696 address will be bogus in the psymtab. */
14697 if (i < size)
14698 dwarf2_complex_location_expr_complaint ();
14699 break;
14700
14701 case DW_OP_GNU_uninit:
14702 break;
14703
14704 default:
14705 {
14706 const char *name = dwarf_stack_op_name (op);
14707
14708 if (name)
14709 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14710 name);
14711 else
14712 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14713 op);
14714 }
14715
14716 return (stack[stacki]);
d53d4ac5 14717 }
3c6e0cb3 14718
21ae7a4d
JK
14719 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14720 outside of the allocated space. Also enforce minimum>0. */
14721 if (stacki >= ARRAY_SIZE (stack) - 1)
14722 {
14723 complaint (&symfile_complaints,
14724 _("location description stack overflow"));
14725 return 0;
14726 }
14727
14728 if (stacki <= 0)
14729 {
14730 complaint (&symfile_complaints,
14731 _("location description stack underflow"));
14732 return 0;
14733 }
14734 }
14735 return (stack[stacki]);
c906108c
SS
14736}
14737
14738/* memory allocation interface */
14739
c906108c 14740static struct dwarf_block *
7b5a2f43 14741dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
14742{
14743 struct dwarf_block *blk;
14744
14745 blk = (struct dwarf_block *)
7b5a2f43 14746 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
14747 return (blk);
14748}
14749
14750static struct abbrev_info *
f3dd6933 14751dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
14752{
14753 struct abbrev_info *abbrev;
14754
f3dd6933
DJ
14755 abbrev = (struct abbrev_info *)
14756 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
14757 memset (abbrev, 0, sizeof (struct abbrev_info));
14758 return (abbrev);
14759}
14760
14761static struct die_info *
b60c80d6 14762dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
14763{
14764 struct die_info *die;
b60c80d6
DJ
14765 size_t size = sizeof (struct die_info);
14766
14767 if (num_attrs > 1)
14768 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 14769
b60c80d6 14770 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
14771 memset (die, 0, sizeof (struct die_info));
14772 return (die);
14773}
2e276125
JB
14774
14775\f
14776/* Macro support. */
14777
2e276125
JB
14778/* Return the full name of file number I in *LH's file name table.
14779 Use COMP_DIR as the name of the current directory of the
14780 compilation. The result is allocated using xmalloc; the caller is
14781 responsible for freeing it. */
14782static char *
14783file_full_name (int file, struct line_header *lh, const char *comp_dir)
14784{
6a83a1e6
EZ
14785 /* Is the file number a valid index into the line header's file name
14786 table? Remember that file numbers start with one, not zero. */
14787 if (1 <= file && file <= lh->num_file_names)
14788 {
14789 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 14790
6a83a1e6
EZ
14791 if (IS_ABSOLUTE_PATH (fe->name))
14792 return xstrdup (fe->name);
14793 else
14794 {
14795 const char *dir;
14796 int dir_len;
14797 char *full_name;
14798
14799 if (fe->dir_index)
14800 dir = lh->include_dirs[fe->dir_index - 1];
14801 else
14802 dir = comp_dir;
14803
14804 if (dir)
14805 {
14806 dir_len = strlen (dir);
14807 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14808 strcpy (full_name, dir);
14809 full_name[dir_len] = '/';
14810 strcpy (full_name + dir_len + 1, fe->name);
14811 return full_name;
14812 }
14813 else
14814 return xstrdup (fe->name);
14815 }
14816 }
2e276125
JB
14817 else
14818 {
6a83a1e6
EZ
14819 /* The compiler produced a bogus file number. We can at least
14820 record the macro definitions made in the file, even if we
14821 won't be able to find the file by name. */
14822 char fake_name[80];
9a619af0 14823
6a83a1e6 14824 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 14825
6e70227d 14826 complaint (&symfile_complaints,
6a83a1e6
EZ
14827 _("bad file number in macro information (%d)"),
14828 file);
2e276125 14829
6a83a1e6 14830 return xstrdup (fake_name);
2e276125
JB
14831 }
14832}
14833
14834
14835static struct macro_source_file *
14836macro_start_file (int file, int line,
14837 struct macro_source_file *current_file,
14838 const char *comp_dir,
14839 struct line_header *lh, struct objfile *objfile)
14840{
14841 /* The full name of this source file. */
14842 char *full_name = file_full_name (file, lh, comp_dir);
14843
14844 /* We don't create a macro table for this compilation unit
14845 at all until we actually get a filename. */
14846 if (! pending_macros)
4a146b47 14847 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 14848 objfile->macro_cache);
2e276125
JB
14849
14850 if (! current_file)
14851 /* If we have no current file, then this must be the start_file
14852 directive for the compilation unit's main source file. */
14853 current_file = macro_set_main (pending_macros, full_name);
14854 else
14855 current_file = macro_include (current_file, line, full_name);
14856
14857 xfree (full_name);
6e70227d 14858
2e276125
JB
14859 return current_file;
14860}
14861
14862
14863/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14864 followed by a null byte. */
14865static char *
14866copy_string (const char *buf, int len)
14867{
14868 char *s = xmalloc (len + 1);
9a619af0 14869
2e276125
JB
14870 memcpy (s, buf, len);
14871 s[len] = '\0';
2e276125
JB
14872 return s;
14873}
14874
14875
14876static const char *
14877consume_improper_spaces (const char *p, const char *body)
14878{
14879 if (*p == ' ')
14880 {
4d3c2250 14881 complaint (&symfile_complaints,
3e43a32a
MS
14882 _("macro definition contains spaces "
14883 "in formal argument list:\n`%s'"),
4d3c2250 14884 body);
2e276125
JB
14885
14886 while (*p == ' ')
14887 p++;
14888 }
14889
14890 return p;
14891}
14892
14893
14894static void
14895parse_macro_definition (struct macro_source_file *file, int line,
14896 const char *body)
14897{
14898 const char *p;
14899
14900 /* The body string takes one of two forms. For object-like macro
14901 definitions, it should be:
14902
14903 <macro name> " " <definition>
14904
14905 For function-like macro definitions, it should be:
14906
14907 <macro name> "() " <definition>
14908 or
14909 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14910
14911 Spaces may appear only where explicitly indicated, and in the
14912 <definition>.
14913
14914 The Dwarf 2 spec says that an object-like macro's name is always
14915 followed by a space, but versions of GCC around March 2002 omit
6e70227d 14916 the space when the macro's definition is the empty string.
2e276125
JB
14917
14918 The Dwarf 2 spec says that there should be no spaces between the
14919 formal arguments in a function-like macro's formal argument list,
14920 but versions of GCC around March 2002 include spaces after the
14921 commas. */
14922
14923
14924 /* Find the extent of the macro name. The macro name is terminated
14925 by either a space or null character (for an object-like macro) or
14926 an opening paren (for a function-like macro). */
14927 for (p = body; *p; p++)
14928 if (*p == ' ' || *p == '(')
14929 break;
14930
14931 if (*p == ' ' || *p == '\0')
14932 {
14933 /* It's an object-like macro. */
14934 int name_len = p - body;
14935 char *name = copy_string (body, name_len);
14936 const char *replacement;
14937
14938 if (*p == ' ')
14939 replacement = body + name_len + 1;
14940 else
14941 {
4d3c2250 14942 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14943 replacement = body + name_len;
14944 }
6e70227d 14945
2e276125
JB
14946 macro_define_object (file, line, name, replacement);
14947
14948 xfree (name);
14949 }
14950 else if (*p == '(')
14951 {
14952 /* It's a function-like macro. */
14953 char *name = copy_string (body, p - body);
14954 int argc = 0;
14955 int argv_size = 1;
14956 char **argv = xmalloc (argv_size * sizeof (*argv));
14957
14958 p++;
14959
14960 p = consume_improper_spaces (p, body);
14961
14962 /* Parse the formal argument list. */
14963 while (*p && *p != ')')
14964 {
14965 /* Find the extent of the current argument name. */
14966 const char *arg_start = p;
14967
14968 while (*p && *p != ',' && *p != ')' && *p != ' ')
14969 p++;
14970
14971 if (! *p || p == arg_start)
4d3c2250 14972 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
14973 else
14974 {
14975 /* Make sure argv has room for the new argument. */
14976 if (argc >= argv_size)
14977 {
14978 argv_size *= 2;
14979 argv = xrealloc (argv, argv_size * sizeof (*argv));
14980 }
14981
14982 argv[argc++] = copy_string (arg_start, p - arg_start);
14983 }
14984
14985 p = consume_improper_spaces (p, body);
14986
14987 /* Consume the comma, if present. */
14988 if (*p == ',')
14989 {
14990 p++;
14991
14992 p = consume_improper_spaces (p, body);
14993 }
14994 }
14995
14996 if (*p == ')')
14997 {
14998 p++;
14999
15000 if (*p == ' ')
15001 /* Perfectly formed definition, no complaints. */
15002 macro_define_function (file, line, name,
6e70227d 15003 argc, (const char **) argv,
2e276125
JB
15004 p + 1);
15005 else if (*p == '\0')
15006 {
15007 /* Complain, but do define it. */
4d3c2250 15008 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15009 macro_define_function (file, line, name,
6e70227d 15010 argc, (const char **) argv,
2e276125
JB
15011 p);
15012 }
15013 else
15014 /* Just complain. */
4d3c2250 15015 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15016 }
15017 else
15018 /* Just complain. */
4d3c2250 15019 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15020
15021 xfree (name);
15022 {
15023 int i;
15024
15025 for (i = 0; i < argc; i++)
15026 xfree (argv[i]);
15027 }
15028 xfree (argv);
15029 }
15030 else
4d3c2250 15031 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15032}
15033
cf2c3c16
TT
15034/* Skip some bytes from BYTES according to the form given in FORM.
15035 Returns the new pointer. */
2e276125 15036
cf2c3c16
TT
15037static gdb_byte *
15038skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15039 enum dwarf_form form,
15040 unsigned int offset_size,
15041 struct dwarf2_section_info *section)
2e276125 15042{
cf2c3c16 15043 unsigned int bytes_read;
2e276125 15044
cf2c3c16 15045 switch (form)
2e276125 15046 {
cf2c3c16
TT
15047 case DW_FORM_data1:
15048 case DW_FORM_flag:
15049 ++bytes;
15050 break;
15051
15052 case DW_FORM_data2:
15053 bytes += 2;
15054 break;
15055
15056 case DW_FORM_data4:
15057 bytes += 4;
15058 break;
15059
15060 case DW_FORM_data8:
15061 bytes += 8;
15062 break;
15063
15064 case DW_FORM_string:
15065 read_direct_string (abfd, bytes, &bytes_read);
15066 bytes += bytes_read;
15067 break;
15068
15069 case DW_FORM_sec_offset:
15070 case DW_FORM_strp:
15071 bytes += offset_size;
15072 break;
15073
15074 case DW_FORM_block:
15075 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15076 bytes += bytes_read;
15077 break;
15078
15079 case DW_FORM_block1:
15080 bytes += 1 + read_1_byte (abfd, bytes);
15081 break;
15082 case DW_FORM_block2:
15083 bytes += 2 + read_2_bytes (abfd, bytes);
15084 break;
15085 case DW_FORM_block4:
15086 bytes += 4 + read_4_bytes (abfd, bytes);
15087 break;
15088
15089 case DW_FORM_sdata:
15090 case DW_FORM_udata:
15091 bytes = skip_leb128 (abfd, bytes);
15092 break;
15093
15094 default:
15095 {
15096 complain:
15097 complaint (&symfile_complaints,
15098 _("invalid form 0x%x in `%s'"),
15099 form,
15100 section->asection->name);
15101 return NULL;
15102 }
2e276125
JB
15103 }
15104
cf2c3c16
TT
15105 return bytes;
15106}
757a13d0 15107
cf2c3c16
TT
15108/* A helper for dwarf_decode_macros that handles skipping an unknown
15109 opcode. Returns an updated pointer to the macro data buffer; or,
15110 on error, issues a complaint and returns NULL. */
757a13d0 15111
cf2c3c16
TT
15112static gdb_byte *
15113skip_unknown_opcode (unsigned int opcode,
15114 gdb_byte **opcode_definitions,
15115 gdb_byte *mac_ptr,
15116 bfd *abfd,
15117 unsigned int offset_size,
15118 struct dwarf2_section_info *section)
15119{
15120 unsigned int bytes_read, i;
15121 unsigned long arg;
15122 gdb_byte *defn;
2e276125 15123
cf2c3c16 15124 if (opcode_definitions[opcode] == NULL)
2e276125 15125 {
cf2c3c16
TT
15126 complaint (&symfile_complaints,
15127 _("unrecognized DW_MACFINO opcode 0x%x"),
15128 opcode);
15129 return NULL;
15130 }
2e276125 15131
cf2c3c16
TT
15132 defn = opcode_definitions[opcode];
15133 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15134 defn += bytes_read;
2e276125 15135
cf2c3c16
TT
15136 for (i = 0; i < arg; ++i)
15137 {
15138 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15139 if (mac_ptr == NULL)
15140 {
15141 /* skip_form_bytes already issued the complaint. */
15142 return NULL;
15143 }
15144 }
757a13d0 15145
cf2c3c16
TT
15146 return mac_ptr;
15147}
757a13d0 15148
cf2c3c16
TT
15149/* A helper function which parses the header of a macro section.
15150 If the macro section is the extended (for now called "GNU") type,
15151 then this updates *OFFSET_SIZE. Returns a pointer to just after
15152 the header, or issues a complaint and returns NULL on error. */
757a13d0 15153
cf2c3c16
TT
15154static gdb_byte *
15155dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15156 bfd *abfd,
15157 gdb_byte *mac_ptr,
15158 unsigned int *offset_size,
15159 int section_is_gnu)
15160{
15161 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15162
cf2c3c16
TT
15163 if (section_is_gnu)
15164 {
15165 unsigned int version, flags;
757a13d0 15166
cf2c3c16
TT
15167 version = read_2_bytes (abfd, mac_ptr);
15168 if (version != 4)
15169 {
15170 complaint (&symfile_complaints,
15171 _("unrecognized version `%d' in .debug_macro section"),
15172 version);
15173 return NULL;
15174 }
15175 mac_ptr += 2;
757a13d0 15176
cf2c3c16
TT
15177 flags = read_1_byte (abfd, mac_ptr);
15178 ++mac_ptr;
15179 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15180
cf2c3c16
TT
15181 if ((flags & 2) != 0)
15182 /* We don't need the line table offset. */
15183 mac_ptr += *offset_size;
757a13d0 15184
cf2c3c16
TT
15185 /* Vendor opcode descriptions. */
15186 if ((flags & 4) != 0)
15187 {
15188 unsigned int i, count;
757a13d0 15189
cf2c3c16
TT
15190 count = read_1_byte (abfd, mac_ptr);
15191 ++mac_ptr;
15192 for (i = 0; i < count; ++i)
15193 {
15194 unsigned int opcode, bytes_read;
15195 unsigned long arg;
15196
15197 opcode = read_1_byte (abfd, mac_ptr);
15198 ++mac_ptr;
15199 opcode_definitions[opcode] = mac_ptr;
15200 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15201 mac_ptr += bytes_read;
15202 mac_ptr += arg;
15203 }
757a13d0 15204 }
cf2c3c16 15205 }
757a13d0 15206
cf2c3c16
TT
15207 return mac_ptr;
15208}
757a13d0 15209
cf2c3c16
TT
15210/* A helper for dwarf_decode_macros that handles the GNU extensions,
15211 including DW_GNU_MACINFO_transparent_include. */
15212
15213static void
15214dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15215 struct macro_source_file *current_file,
15216 struct line_header *lh, char *comp_dir,
15217 struct dwarf2_section_info *section,
15218 int section_is_gnu,
15219 unsigned int offset_size,
15220 struct objfile *objfile)
15221{
15222 enum dwarf_macro_record_type macinfo_type;
15223 int at_commandline;
15224 gdb_byte *opcode_definitions[256];
757a13d0 15225
cf2c3c16
TT
15226 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15227 &offset_size, section_is_gnu);
15228 if (mac_ptr == NULL)
15229 {
15230 /* We already issued a complaint. */
15231 return;
15232 }
757a13d0
JK
15233
15234 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15235 GDB is still reading the definitions from command line. First
15236 DW_MACINFO_start_file will need to be ignored as it was already executed
15237 to create CURRENT_FILE for the main source holding also the command line
15238 definitions. On first met DW_MACINFO_start_file this flag is reset to
15239 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15240
15241 at_commandline = 1;
15242
15243 do
15244 {
15245 /* Do we at least have room for a macinfo type byte? */
15246 if (mac_ptr >= mac_end)
15247 {
cf2c3c16 15248 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15249 break;
15250 }
15251
15252 macinfo_type = read_1_byte (abfd, mac_ptr);
15253 mac_ptr++;
15254
cf2c3c16
TT
15255 /* Note that we rely on the fact that the corresponding GNU and
15256 DWARF constants are the same. */
757a13d0
JK
15257 switch (macinfo_type)
15258 {
15259 /* A zero macinfo type indicates the end of the macro
15260 information. */
15261 case 0:
15262 break;
2e276125 15263
cf2c3c16
TT
15264 case DW_MACRO_GNU_define:
15265 case DW_MACRO_GNU_undef:
15266 case DW_MACRO_GNU_define_indirect:
15267 case DW_MACRO_GNU_undef_indirect:
2e276125 15268 {
891d2f0b 15269 unsigned int bytes_read;
2e276125
JB
15270 int line;
15271 char *body;
cf2c3c16 15272 int is_define;
2e276125 15273
cf2c3c16
TT
15274 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15275 mac_ptr += bytes_read;
15276
15277 if (macinfo_type == DW_MACRO_GNU_define
15278 || macinfo_type == DW_MACRO_GNU_undef)
15279 {
15280 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15281 mac_ptr += bytes_read;
15282 }
15283 else
15284 {
15285 LONGEST str_offset;
15286
15287 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15288 mac_ptr += offset_size;
2e276125 15289
cf2c3c16
TT
15290 body = read_indirect_string_at_offset (abfd, str_offset);
15291 }
15292
15293 is_define = (macinfo_type == DW_MACRO_GNU_define
15294 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15295 if (! current_file)
757a13d0
JK
15296 {
15297 /* DWARF violation as no main source is present. */
15298 complaint (&symfile_complaints,
15299 _("debug info with no main source gives macro %s "
15300 "on line %d: %s"),
cf2c3c16
TT
15301 is_define ? _("definition") : _("undefinition"),
15302 line, body);
757a13d0
JK
15303 break;
15304 }
3e43a32a
MS
15305 if ((line == 0 && !at_commandline)
15306 || (line != 0 && at_commandline))
4d3c2250 15307 complaint (&symfile_complaints,
757a13d0
JK
15308 _("debug info gives %s macro %s with %s line %d: %s"),
15309 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15310 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15311 line == 0 ? _("zero") : _("non-zero"), line, body);
15312
cf2c3c16 15313 if (is_define)
757a13d0 15314 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15315 else
15316 {
15317 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15318 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15319 macro_undef (current_file, line, body);
15320 }
2e276125
JB
15321 }
15322 break;
15323
cf2c3c16 15324 case DW_MACRO_GNU_start_file:
2e276125 15325 {
891d2f0b 15326 unsigned int bytes_read;
2e276125
JB
15327 int line, file;
15328
15329 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15330 mac_ptr += bytes_read;
15331 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15332 mac_ptr += bytes_read;
15333
3e43a32a
MS
15334 if ((line == 0 && !at_commandline)
15335 || (line != 0 && at_commandline))
757a13d0
JK
15336 complaint (&symfile_complaints,
15337 _("debug info gives source %d included "
15338 "from %s at %s line %d"),
15339 file, at_commandline ? _("command-line") : _("file"),
15340 line == 0 ? _("zero") : _("non-zero"), line);
15341
15342 if (at_commandline)
15343 {
cf2c3c16
TT
15344 /* This DW_MACRO_GNU_start_file was executed in the
15345 pass one. */
757a13d0
JK
15346 at_commandline = 0;
15347 }
15348 else
15349 current_file = macro_start_file (file, line,
15350 current_file, comp_dir,
cf2c3c16 15351 lh, objfile);
2e276125
JB
15352 }
15353 break;
15354
cf2c3c16 15355 case DW_MACRO_GNU_end_file:
2e276125 15356 if (! current_file)
4d3c2250 15357 complaint (&symfile_complaints,
3e43a32a
MS
15358 _("macro debug info has an unmatched "
15359 "`close_file' directive"));
2e276125
JB
15360 else
15361 {
15362 current_file = current_file->included_by;
15363 if (! current_file)
15364 {
cf2c3c16 15365 enum dwarf_macro_record_type next_type;
2e276125
JB
15366
15367 /* GCC circa March 2002 doesn't produce the zero
15368 type byte marking the end of the compilation
15369 unit. Complain if it's not there, but exit no
15370 matter what. */
15371
15372 /* Do we at least have room for a macinfo type byte? */
15373 if (mac_ptr >= mac_end)
15374 {
cf2c3c16 15375 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15376 return;
15377 }
15378
15379 /* We don't increment mac_ptr here, so this is just
15380 a look-ahead. */
15381 next_type = read_1_byte (abfd, mac_ptr);
15382 if (next_type != 0)
4d3c2250 15383 complaint (&symfile_complaints,
3e43a32a
MS
15384 _("no terminating 0-type entry for "
15385 "macros in `.debug_macinfo' section"));
2e276125
JB
15386
15387 return;
15388 }
15389 }
15390 break;
15391
cf2c3c16
TT
15392 case DW_MACRO_GNU_transparent_include:
15393 {
15394 LONGEST offset;
15395
15396 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15397 mac_ptr += offset_size;
15398
15399 dwarf_decode_macro_bytes (abfd,
15400 section->buffer + offset,
15401 mac_end, current_file,
15402 lh, comp_dir,
15403 section, section_is_gnu,
15404 offset_size, objfile);
15405 }
15406 break;
15407
2e276125 15408 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15409 if (!section_is_gnu)
15410 {
15411 unsigned int bytes_read;
15412 int constant;
2e276125 15413
cf2c3c16
TT
15414 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15415 mac_ptr += bytes_read;
15416 read_direct_string (abfd, mac_ptr, &bytes_read);
15417 mac_ptr += bytes_read;
2e276125 15418
cf2c3c16
TT
15419 /* We don't recognize any vendor extensions. */
15420 break;
15421 }
15422 /* FALLTHROUGH */
15423
15424 default:
15425 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15426 mac_ptr, abfd, offset_size,
15427 section);
15428 if (mac_ptr == NULL)
15429 return;
15430 break;
2e276125 15431 }
757a13d0 15432 } while (macinfo_type != 0);
2e276125 15433}
8e19ed76 15434
cf2c3c16
TT
15435static void
15436dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15437 char *comp_dir, bfd *abfd,
15438 struct dwarf2_cu *cu,
15439 struct dwarf2_section_info *section,
15440 int section_is_gnu)
15441{
15442 gdb_byte *mac_ptr, *mac_end;
15443 struct macro_source_file *current_file = 0;
15444 enum dwarf_macro_record_type macinfo_type;
15445 unsigned int offset_size = cu->header.offset_size;
15446 gdb_byte *opcode_definitions[256];
15447
15448 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15449 if (section->buffer == NULL)
15450 {
15451 complaint (&symfile_complaints, _("missing %s section"),
15452 section->asection->name);
15453 return;
15454 }
15455
15456 /* First pass: Find the name of the base filename.
15457 This filename is needed in order to process all macros whose definition
15458 (or undefinition) comes from the command line. These macros are defined
15459 before the first DW_MACINFO_start_file entry, and yet still need to be
15460 associated to the base file.
15461
15462 To determine the base file name, we scan the macro definitions until we
15463 reach the first DW_MACINFO_start_file entry. We then initialize
15464 CURRENT_FILE accordingly so that any macro definition found before the
15465 first DW_MACINFO_start_file can still be associated to the base file. */
15466
15467 mac_ptr = section->buffer + offset;
15468 mac_end = section->buffer + section->size;
15469
15470 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15471 &offset_size, section_is_gnu);
15472 if (mac_ptr == NULL)
15473 {
15474 /* We already issued a complaint. */
15475 return;
15476 }
15477
15478 do
15479 {
15480 /* Do we at least have room for a macinfo type byte? */
15481 if (mac_ptr >= mac_end)
15482 {
15483 /* Complaint is printed during the second pass as GDB will probably
15484 stop the first pass earlier upon finding
15485 DW_MACINFO_start_file. */
15486 break;
15487 }
15488
15489 macinfo_type = read_1_byte (abfd, mac_ptr);
15490 mac_ptr++;
15491
15492 /* Note that we rely on the fact that the corresponding GNU and
15493 DWARF constants are the same. */
15494 switch (macinfo_type)
15495 {
15496 /* A zero macinfo type indicates the end of the macro
15497 information. */
15498 case 0:
15499 break;
15500
15501 case DW_MACRO_GNU_define:
15502 case DW_MACRO_GNU_undef:
15503 /* Only skip the data by MAC_PTR. */
15504 {
15505 unsigned int bytes_read;
15506
15507 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15508 mac_ptr += bytes_read;
15509 read_direct_string (abfd, mac_ptr, &bytes_read);
15510 mac_ptr += bytes_read;
15511 }
15512 break;
15513
15514 case DW_MACRO_GNU_start_file:
15515 {
15516 unsigned int bytes_read;
15517 int line, file;
15518
15519 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15520 mac_ptr += bytes_read;
15521 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15522 mac_ptr += bytes_read;
15523
15524 current_file = macro_start_file (file, line, current_file,
15525 comp_dir, lh, cu->objfile);
15526 }
15527 break;
15528
15529 case DW_MACRO_GNU_end_file:
15530 /* No data to skip by MAC_PTR. */
15531 break;
15532
15533 case DW_MACRO_GNU_define_indirect:
15534 case DW_MACRO_GNU_undef_indirect:
15535 {
15536 unsigned int bytes_read;
15537
15538 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15539 mac_ptr += bytes_read;
15540 mac_ptr += offset_size;
15541 }
15542 break;
15543
15544 case DW_MACRO_GNU_transparent_include:
15545 /* Note that, according to the spec, a transparent include
15546 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15547 skip this opcode. */
15548 mac_ptr += offset_size;
15549 break;
15550
15551 case DW_MACINFO_vendor_ext:
15552 /* Only skip the data by MAC_PTR. */
15553 if (!section_is_gnu)
15554 {
15555 unsigned int bytes_read;
15556
15557 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15558 mac_ptr += bytes_read;
15559 read_direct_string (abfd, mac_ptr, &bytes_read);
15560 mac_ptr += bytes_read;
15561 }
15562 /* FALLTHROUGH */
15563
15564 default:
15565 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15566 mac_ptr, abfd, offset_size,
15567 section);
15568 if (mac_ptr == NULL)
15569 return;
15570 break;
15571 }
15572 } while (macinfo_type != 0 && current_file == NULL);
15573
15574 /* Second pass: Process all entries.
15575
15576 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15577 command-line macro definitions/undefinitions. This flag is unset when we
15578 reach the first DW_MACINFO_start_file entry. */
15579
15580 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15581 current_file, lh, comp_dir, section, section_is_gnu,
15582 offset_size, cu->objfile);
15583}
15584
8e19ed76 15585/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 15586 if so return true else false. */
8e19ed76
PS
15587static int
15588attr_form_is_block (struct attribute *attr)
15589{
15590 return (attr == NULL ? 0 :
15591 attr->form == DW_FORM_block1
15592 || attr->form == DW_FORM_block2
15593 || attr->form == DW_FORM_block4
2dc7f7b3
TT
15594 || attr->form == DW_FORM_block
15595 || attr->form == DW_FORM_exprloc);
8e19ed76 15596}
4c2df51b 15597
c6a0999f
JB
15598/* Return non-zero if ATTR's value is a section offset --- classes
15599 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15600 You may use DW_UNSND (attr) to retrieve such offsets.
15601
15602 Section 7.5.4, "Attribute Encodings", explains that no attribute
15603 may have a value that belongs to more than one of these classes; it
15604 would be ambiguous if we did, because we use the same forms for all
15605 of them. */
3690dd37
JB
15606static int
15607attr_form_is_section_offset (struct attribute *attr)
15608{
15609 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
15610 || attr->form == DW_FORM_data8
15611 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
15612}
15613
15614
15615/* Return non-zero if ATTR's value falls in the 'constant' class, or
15616 zero otherwise. When this function returns true, you can apply
15617 dwarf2_get_attr_constant_value to it.
15618
15619 However, note that for some attributes you must check
15620 attr_form_is_section_offset before using this test. DW_FORM_data4
15621 and DW_FORM_data8 are members of both the constant class, and of
15622 the classes that contain offsets into other debug sections
15623 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15624 that, if an attribute's can be either a constant or one of the
15625 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15626 taken as section offsets, not constants. */
15627static int
15628attr_form_is_constant (struct attribute *attr)
15629{
15630 switch (attr->form)
15631 {
15632 case DW_FORM_sdata:
15633 case DW_FORM_udata:
15634 case DW_FORM_data1:
15635 case DW_FORM_data2:
15636 case DW_FORM_data4:
15637 case DW_FORM_data8:
15638 return 1;
15639 default:
15640 return 0;
15641 }
15642}
15643
8cf6f0b1
TT
15644/* A helper function that fills in a dwarf2_loclist_baton. */
15645
15646static void
15647fill_in_loclist_baton (struct dwarf2_cu *cu,
15648 struct dwarf2_loclist_baton *baton,
15649 struct attribute *attr)
15650{
15651 dwarf2_read_section (dwarf2_per_objfile->objfile,
15652 &dwarf2_per_objfile->loc);
15653
15654 baton->per_cu = cu->per_cu;
15655 gdb_assert (baton->per_cu);
15656 /* We don't know how long the location list is, but make sure we
15657 don't run off the edge of the section. */
15658 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15659 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15660 baton->base_address = cu->base_address;
15661}
15662
4c2df51b
DJ
15663static void
15664dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 15665 struct dwarf2_cu *cu)
4c2df51b 15666{
3690dd37 15667 if (attr_form_is_section_offset (attr)
99bcc461
DJ
15668 /* ".debug_loc" may not exist at all, or the offset may be outside
15669 the section. If so, fall through to the complaint in the
15670 other branch. */
9e0ac564
TT
15671 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15672 &dwarf2_per_objfile->loc))
4c2df51b 15673 {
0d53c4c4 15674 struct dwarf2_loclist_baton *baton;
4c2df51b 15675
4a146b47 15676 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15677 sizeof (struct dwarf2_loclist_baton));
4c2df51b 15678
8cf6f0b1 15679 fill_in_loclist_baton (cu, baton, attr);
be391dca 15680
d00adf39 15681 if (cu->base_known == 0)
0d53c4c4 15682 complaint (&symfile_complaints,
3e43a32a
MS
15683 _("Location list used without "
15684 "specifying the CU base address."));
4c2df51b 15685
768a979c 15686 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
15687 SYMBOL_LOCATION_BATON (sym) = baton;
15688 }
15689 else
15690 {
15691 struct dwarf2_locexpr_baton *baton;
15692
4a146b47 15693 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 15694 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
15695 baton->per_cu = cu->per_cu;
15696 gdb_assert (baton->per_cu);
0d53c4c4
DJ
15697
15698 if (attr_form_is_block (attr))
15699 {
15700 /* Note that we're just copying the block's data pointer
15701 here, not the actual data. We're still pointing into the
6502dd73
DJ
15702 info_buffer for SYM's objfile; right now we never release
15703 that buffer, but when we do clean up properly this may
15704 need to change. */
0d53c4c4
DJ
15705 baton->size = DW_BLOCK (attr)->size;
15706 baton->data = DW_BLOCK (attr)->data;
15707 }
15708 else
15709 {
15710 dwarf2_invalid_attrib_class_complaint ("location description",
15711 SYMBOL_NATURAL_NAME (sym));
15712 baton->size = 0;
0d53c4c4 15713 }
6e70227d 15714
768a979c 15715 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
15716 SYMBOL_LOCATION_BATON (sym) = baton;
15717 }
4c2df51b 15718}
6502dd73 15719
9aa1f1e3
TT
15720/* Return the OBJFILE associated with the compilation unit CU. If CU
15721 came from a separate debuginfo file, then the master objfile is
15722 returned. */
ae0d2f24
UW
15723
15724struct objfile *
15725dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15726{
9291a0cd 15727 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
15728
15729 /* Return the master objfile, so that we can report and look up the
15730 correct file containing this variable. */
15731 if (objfile->separate_debug_objfile_backlink)
15732 objfile = objfile->separate_debug_objfile_backlink;
15733
15734 return objfile;
15735}
15736
96408a79
SA
15737/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15738 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15739 CU_HEADERP first. */
15740
15741static const struct comp_unit_head *
15742per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15743 struct dwarf2_per_cu_data *per_cu)
15744{
15745 struct objfile *objfile;
15746 struct dwarf2_per_objfile *per_objfile;
15747 gdb_byte *info_ptr;
15748
15749 if (per_cu->cu)
15750 return &per_cu->cu->header;
15751
15752 objfile = per_cu->objfile;
15753 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15754 info_ptr = per_objfile->info.buffer + per_cu->offset;
15755
15756 memset (cu_headerp, 0, sizeof (*cu_headerp));
15757 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15758
15759 return cu_headerp;
15760}
15761
ae0d2f24
UW
15762/* Return the address size given in the compilation unit header for CU. */
15763
15764CORE_ADDR
15765dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15766{
96408a79
SA
15767 struct comp_unit_head cu_header_local;
15768 const struct comp_unit_head *cu_headerp;
c471e790 15769
96408a79
SA
15770 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15771
15772 return cu_headerp->addr_size;
ae0d2f24
UW
15773}
15774
9eae7c52
TT
15775/* Return the offset size given in the compilation unit header for CU. */
15776
15777int
15778dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15779{
96408a79
SA
15780 struct comp_unit_head cu_header_local;
15781 const struct comp_unit_head *cu_headerp;
9c6c53f7 15782
96408a79
SA
15783 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15784
15785 return cu_headerp->offset_size;
15786}
15787
15788/* See its dwarf2loc.h declaration. */
15789
15790int
15791dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15792{
15793 struct comp_unit_head cu_header_local;
15794 const struct comp_unit_head *cu_headerp;
15795
15796 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15797
15798 if (cu_headerp->version == 2)
15799 return cu_headerp->addr_size;
15800 else
15801 return cu_headerp->offset_size;
181cebd4
JK
15802}
15803
9aa1f1e3
TT
15804/* Return the text offset of the CU. The returned offset comes from
15805 this CU's objfile. If this objfile came from a separate debuginfo
15806 file, then the offset may be different from the corresponding
15807 offset in the parent objfile. */
15808
15809CORE_ADDR
15810dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15811{
bb3fa9d0 15812 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
15813
15814 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15815}
15816
348e048f
DE
15817/* Locate the .debug_info compilation unit from CU's objfile which contains
15818 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
15819
15820static struct dwarf2_per_cu_data *
c764a876 15821dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
15822 struct objfile *objfile)
15823{
15824 struct dwarf2_per_cu_data *this_cu;
15825 int low, high;
15826
ae038cb0
DJ
15827 low = 0;
15828 high = dwarf2_per_objfile->n_comp_units - 1;
15829 while (high > low)
15830 {
15831 int mid = low + (high - low) / 2;
9a619af0 15832
ae038cb0
DJ
15833 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15834 high = mid;
15835 else
15836 low = mid + 1;
15837 }
15838 gdb_assert (low == high);
15839 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15840 {
10b3939b 15841 if (low == 0)
8a3fe4f8
AC
15842 error (_("Dwarf Error: could not find partial DIE containing "
15843 "offset 0x%lx [in module %s]"),
10b3939b
DJ
15844 (long) offset, bfd_get_filename (objfile->obfd));
15845
ae038cb0
DJ
15846 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15847 return dwarf2_per_objfile->all_comp_units[low-1];
15848 }
15849 else
15850 {
15851 this_cu = dwarf2_per_objfile->all_comp_units[low];
15852 if (low == dwarf2_per_objfile->n_comp_units - 1
15853 && offset >= this_cu->offset + this_cu->length)
c764a876 15854 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
15855 gdb_assert (offset < this_cu->offset + this_cu->length);
15856 return this_cu;
15857 }
15858}
15859
10b3939b
DJ
15860/* Locate the compilation unit from OBJFILE which is located at exactly
15861 OFFSET. Raises an error on failure. */
15862
ae038cb0 15863static struct dwarf2_per_cu_data *
c764a876 15864dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
15865{
15866 struct dwarf2_per_cu_data *this_cu;
9a619af0 15867
ae038cb0
DJ
15868 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15869 if (this_cu->offset != offset)
c764a876 15870 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
15871 return this_cu;
15872}
15873
9816fde3 15874/* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
93311388 15875
9816fde3
JK
15876static void
15877init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
93311388 15878{
9816fde3 15879 memset (cu, 0, sizeof (*cu));
93311388
DE
15880 cu->objfile = objfile;
15881 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
15882}
15883
15884/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15885
15886static void
15887prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15888{
15889 struct attribute *attr;
15890
15891 /* Set the language we're debugging. */
15892 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15893 if (attr)
15894 set_cu_language (DW_UNSND (attr), cu);
15895 else
9cded63f
TT
15896 {
15897 cu->language = language_minimal;
15898 cu->language_defn = language_def (cu->language);
15899 }
93311388
DE
15900}
15901
ae038cb0
DJ
15902/* Release one cached compilation unit, CU. We unlink it from the tree
15903 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
15904 the caller is responsible for that.
15905 NOTE: DATA is a void * because this function is also used as a
15906 cleanup routine. */
ae038cb0
DJ
15907
15908static void
15909free_one_comp_unit (void *data)
15910{
15911 struct dwarf2_cu *cu = data;
15912
15913 if (cu->per_cu != NULL)
15914 cu->per_cu->cu = NULL;
15915 cu->per_cu = NULL;
15916
15917 obstack_free (&cu->comp_unit_obstack, NULL);
15918
15919 xfree (cu);
15920}
15921
72bf9492 15922/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
15923 when we're finished with it. We can't free the pointer itself, but be
15924 sure to unlink it from the cache. Also release any associated storage
15925 and perform cache maintenance.
72bf9492
DJ
15926
15927 Only used during partial symbol parsing. */
15928
15929static void
15930free_stack_comp_unit (void *data)
15931{
15932 struct dwarf2_cu *cu = data;
15933
15934 obstack_free (&cu->comp_unit_obstack, NULL);
15935 cu->partial_dies = NULL;
ae038cb0
DJ
15936
15937 if (cu->per_cu != NULL)
15938 {
15939 /* This compilation unit is on the stack in our caller, so we
15940 should not xfree it. Just unlink it. */
15941 cu->per_cu->cu = NULL;
15942 cu->per_cu = NULL;
15943
15944 /* If we had a per-cu pointer, then we may have other compilation
15945 units loaded, so age them now. */
15946 age_cached_comp_units ();
15947 }
15948}
15949
15950/* Free all cached compilation units. */
15951
15952static void
15953free_cached_comp_units (void *data)
15954{
15955 struct dwarf2_per_cu_data *per_cu, **last_chain;
15956
15957 per_cu = dwarf2_per_objfile->read_in_chain;
15958 last_chain = &dwarf2_per_objfile->read_in_chain;
15959 while (per_cu != NULL)
15960 {
15961 struct dwarf2_per_cu_data *next_cu;
15962
15963 next_cu = per_cu->cu->read_in_chain;
15964
15965 free_one_comp_unit (per_cu->cu);
15966 *last_chain = next_cu;
15967
15968 per_cu = next_cu;
15969 }
15970}
15971
15972/* Increase the age counter on each cached compilation unit, and free
15973 any that are too old. */
15974
15975static void
15976age_cached_comp_units (void)
15977{
15978 struct dwarf2_per_cu_data *per_cu, **last_chain;
15979
15980 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15981 per_cu = dwarf2_per_objfile->read_in_chain;
15982 while (per_cu != NULL)
15983 {
15984 per_cu->cu->last_used ++;
15985 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15986 dwarf2_mark (per_cu->cu);
15987 per_cu = per_cu->cu->read_in_chain;
15988 }
15989
15990 per_cu = dwarf2_per_objfile->read_in_chain;
15991 last_chain = &dwarf2_per_objfile->read_in_chain;
15992 while (per_cu != NULL)
15993 {
15994 struct dwarf2_per_cu_data *next_cu;
15995
15996 next_cu = per_cu->cu->read_in_chain;
15997
15998 if (!per_cu->cu->mark)
15999 {
16000 free_one_comp_unit (per_cu->cu);
16001 *last_chain = next_cu;
16002 }
16003 else
16004 last_chain = &per_cu->cu->read_in_chain;
16005
16006 per_cu = next_cu;
16007 }
16008}
16009
16010/* Remove a single compilation unit from the cache. */
16011
16012static void
16013free_one_cached_comp_unit (void *target_cu)
16014{
16015 struct dwarf2_per_cu_data *per_cu, **last_chain;
16016
16017 per_cu = dwarf2_per_objfile->read_in_chain;
16018 last_chain = &dwarf2_per_objfile->read_in_chain;
16019 while (per_cu != NULL)
16020 {
16021 struct dwarf2_per_cu_data *next_cu;
16022
16023 next_cu = per_cu->cu->read_in_chain;
16024
16025 if (per_cu->cu == target_cu)
16026 {
16027 free_one_comp_unit (per_cu->cu);
16028 *last_chain = next_cu;
16029 break;
16030 }
16031 else
16032 last_chain = &per_cu->cu->read_in_chain;
16033
16034 per_cu = next_cu;
16035 }
16036}
16037
fe3e1990
DJ
16038/* Release all extra memory associated with OBJFILE. */
16039
16040void
16041dwarf2_free_objfile (struct objfile *objfile)
16042{
16043 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16044
16045 if (dwarf2_per_objfile == NULL)
16046 return;
16047
16048 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16049 free_cached_comp_units (NULL);
16050
7b9f3c50
DE
16051 if (dwarf2_per_objfile->quick_file_names_table)
16052 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16053
fe3e1990
DJ
16054 /* Everything else should be on the objfile obstack. */
16055}
16056
1c379e20
DJ
16057/* A pair of DIE offset and GDB type pointer. We store these
16058 in a hash table separate from the DIEs, and preserve them
16059 when the DIEs are flushed out of cache. */
16060
16061struct dwarf2_offset_and_type
16062{
16063 unsigned int offset;
16064 struct type *type;
16065};
16066
16067/* Hash function for a dwarf2_offset_and_type. */
16068
16069static hashval_t
16070offset_and_type_hash (const void *item)
16071{
16072 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 16073
1c379e20
DJ
16074 return ofs->offset;
16075}
16076
16077/* Equality function for a dwarf2_offset_and_type. */
16078
16079static int
16080offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16081{
16082 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16083 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16084
1c379e20
DJ
16085 return ofs_lhs->offset == ofs_rhs->offset;
16086}
16087
16088/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16089 table if necessary. For convenience, return TYPE.
16090
16091 The DIEs reading must have careful ordering to:
16092 * Not cause infite loops trying to read in DIEs as a prerequisite for
16093 reading current DIE.
16094 * Not trying to dereference contents of still incompletely read in types
16095 while reading in other DIEs.
16096 * Enable referencing still incompletely read in types just by a pointer to
16097 the type without accessing its fields.
16098
16099 Therefore caller should follow these rules:
16100 * Try to fetch any prerequisite types we may need to build this DIE type
16101 before building the type and calling set_die_type.
e71ec853 16102 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16103 possible before fetching more types to complete the current type.
16104 * Make the type as complete as possible before fetching more types. */
1c379e20 16105
f792889a 16106static struct type *
1c379e20
DJ
16107set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16108{
16109 struct dwarf2_offset_and_type **slot, ofs;
673bfd45
DE
16110 struct objfile *objfile = cu->objfile;
16111 htab_t *type_hash_ptr;
1c379e20 16112
b4ba55a1
JB
16113 /* For Ada types, make sure that the gnat-specific data is always
16114 initialized (if not already set). There are a few types where
16115 we should not be doing so, because the type-specific area is
16116 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16117 where the type-specific area is used to store the floatformat).
16118 But this is not a problem, because the gnat-specific information
16119 is actually not needed for these types. */
16120 if (need_gnat_info (cu)
16121 && TYPE_CODE (type) != TYPE_CODE_FUNC
16122 && TYPE_CODE (type) != TYPE_CODE_FLT
16123 && !HAVE_GNAT_AUX_INFO (type))
16124 INIT_GNAT_SPECIFIC (type);
16125
8b70b953 16126 if (cu->per_cu->debug_type_section)
673bfd45
DE
16127 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16128 else
16129 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16130
16131 if (*type_hash_ptr == NULL)
f792889a 16132 {
673bfd45
DE
16133 *type_hash_ptr
16134 = htab_create_alloc_ex (127,
f792889a
DJ
16135 offset_and_type_hash,
16136 offset_and_type_eq,
16137 NULL,
673bfd45 16138 &objfile->objfile_obstack,
f792889a
DJ
16139 hashtab_obstack_allocate,
16140 dummy_obstack_deallocate);
f792889a 16141 }
1c379e20
DJ
16142
16143 ofs.offset = die->offset;
16144 ofs.type = type;
16145 slot = (struct dwarf2_offset_and_type **)
673bfd45 16146 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
7e314c57
JK
16147 if (*slot)
16148 complaint (&symfile_complaints,
16149 _("A problem internal to GDB: DIE 0x%x has type already set"),
16150 die->offset);
673bfd45 16151 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16152 **slot = ofs;
f792889a 16153 return type;
1c379e20
DJ
16154}
16155
673bfd45
DE
16156/* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16157 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16158
16159static struct type *
673bfd45
DE
16160get_die_type_at_offset (unsigned int offset,
16161 struct dwarf2_per_cu_data *per_cu)
1c379e20
DJ
16162{
16163 struct dwarf2_offset_and_type *slot, ofs;
673bfd45 16164 htab_t type_hash;
f792889a 16165
8b70b953 16166 if (per_cu->debug_type_section)
673bfd45
DE
16167 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16168 else
16169 type_hash = dwarf2_per_objfile->debug_info_type_hash;
f792889a
DJ
16170 if (type_hash == NULL)
16171 return NULL;
1c379e20 16172
673bfd45 16173 ofs.offset = offset;
1c379e20
DJ
16174 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16175 if (slot)
16176 return slot->type;
16177 else
16178 return NULL;
16179}
16180
673bfd45
DE
16181/* Look up the type for DIE in the appropriate type_hash table,
16182 or return NULL if DIE does not have a saved type. */
16183
16184static struct type *
16185get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16186{
16187 return get_die_type_at_offset (die->offset, cu->per_cu);
16188}
16189
10b3939b
DJ
16190/* Add a dependence relationship from CU to REF_PER_CU. */
16191
16192static void
16193dwarf2_add_dependence (struct dwarf2_cu *cu,
16194 struct dwarf2_per_cu_data *ref_per_cu)
16195{
16196 void **slot;
16197
16198 if (cu->dependencies == NULL)
16199 cu->dependencies
16200 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16201 NULL, &cu->comp_unit_obstack,
16202 hashtab_obstack_allocate,
16203 dummy_obstack_deallocate);
16204
16205 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16206 if (*slot == NULL)
16207 *slot = ref_per_cu;
16208}
1c379e20 16209
f504f079
DE
16210/* Subroutine of dwarf2_mark to pass to htab_traverse.
16211 Set the mark field in every compilation unit in the
ae038cb0
DJ
16212 cache that we must keep because we are keeping CU. */
16213
10b3939b
DJ
16214static int
16215dwarf2_mark_helper (void **slot, void *data)
16216{
16217 struct dwarf2_per_cu_data *per_cu;
16218
16219 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16220
16221 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16222 reading of the chain. As such dependencies remain valid it is not much
16223 useful to track and undo them during QUIT cleanups. */
16224 if (per_cu->cu == NULL)
16225 return 1;
16226
10b3939b
DJ
16227 if (per_cu->cu->mark)
16228 return 1;
16229 per_cu->cu->mark = 1;
16230
16231 if (per_cu->cu->dependencies != NULL)
16232 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16233
16234 return 1;
16235}
16236
f504f079
DE
16237/* Set the mark field in CU and in every other compilation unit in the
16238 cache that we must keep because we are keeping CU. */
16239
ae038cb0
DJ
16240static void
16241dwarf2_mark (struct dwarf2_cu *cu)
16242{
16243 if (cu->mark)
16244 return;
16245 cu->mark = 1;
10b3939b
DJ
16246 if (cu->dependencies != NULL)
16247 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16248}
16249
16250static void
16251dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16252{
16253 while (per_cu)
16254 {
16255 per_cu->cu->mark = 0;
16256 per_cu = per_cu->cu->read_in_chain;
16257 }
72bf9492
DJ
16258}
16259
72bf9492
DJ
16260/* Trivial hash function for partial_die_info: the hash value of a DIE
16261 is its offset in .debug_info for this objfile. */
16262
16263static hashval_t
16264partial_die_hash (const void *item)
16265{
16266 const struct partial_die_info *part_die = item;
9a619af0 16267
72bf9492
DJ
16268 return part_die->offset;
16269}
16270
16271/* Trivial comparison function for partial_die_info structures: two DIEs
16272 are equal if they have the same offset. */
16273
16274static int
16275partial_die_eq (const void *item_lhs, const void *item_rhs)
16276{
16277 const struct partial_die_info *part_die_lhs = item_lhs;
16278 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16279
72bf9492
DJ
16280 return part_die_lhs->offset == part_die_rhs->offset;
16281}
16282
ae038cb0
DJ
16283static struct cmd_list_element *set_dwarf2_cmdlist;
16284static struct cmd_list_element *show_dwarf2_cmdlist;
16285
16286static void
16287set_dwarf2_cmd (char *args, int from_tty)
16288{
16289 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16290}
16291
16292static void
16293show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16294{
ae038cb0
DJ
16295 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16296}
16297
dce234bc
PP
16298/* If section described by INFO was mmapped, munmap it now. */
16299
16300static void
16301munmap_section_buffer (struct dwarf2_section_info *info)
16302{
b315ab21 16303 if (info->map_addr != NULL)
dce234bc
PP
16304 {
16305#ifdef HAVE_MMAP
b315ab21 16306 int res;
9a619af0 16307
b315ab21
TG
16308 res = munmap (info->map_addr, info->map_len);
16309 gdb_assert (res == 0);
dce234bc
PP
16310#else
16311 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16312 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16313#endif
16314 }
16315}
16316
16317/* munmap debug sections for OBJFILE, if necessary. */
16318
16319static void
c1bd65d0 16320dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16321{
16322 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16323 int ix;
16324 struct dwarf2_section_info *section;
9a619af0 16325
16be1145
DE
16326 /* This is sorted according to the order they're defined in to make it easier
16327 to keep in sync. */
dce234bc
PP
16328 munmap_section_buffer (&data->info);
16329 munmap_section_buffer (&data->abbrev);
16330 munmap_section_buffer (&data->line);
16be1145 16331 munmap_section_buffer (&data->loc);
dce234bc 16332 munmap_section_buffer (&data->macinfo);
cf2c3c16 16333 munmap_section_buffer (&data->macro);
16be1145 16334 munmap_section_buffer (&data->str);
dce234bc 16335 munmap_section_buffer (&data->ranges);
dce234bc
PP
16336 munmap_section_buffer (&data->frame);
16337 munmap_section_buffer (&data->eh_frame);
9291a0cd 16338 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16339
16340 for (ix = 0;
16341 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16342 ++ix)
16343 munmap_section_buffer (section);
16344
16345 VEC_free (dwarf2_section_info_def, data->types);
9291a0cd
TT
16346}
16347
16348\f
ae2de4f8 16349/* The "save gdb-index" command. */
9291a0cd
TT
16350
16351/* The contents of the hash table we create when building the string
16352 table. */
16353struct strtab_entry
16354{
16355 offset_type offset;
16356 const char *str;
16357};
16358
559a7a62
JK
16359/* Hash function for a strtab_entry.
16360
16361 Function is used only during write_hash_table so no index format backward
16362 compatibility is needed. */
b89be57b 16363
9291a0cd
TT
16364static hashval_t
16365hash_strtab_entry (const void *e)
16366{
16367 const struct strtab_entry *entry = e;
559a7a62 16368 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16369}
16370
16371/* Equality function for a strtab_entry. */
b89be57b 16372
9291a0cd
TT
16373static int
16374eq_strtab_entry (const void *a, const void *b)
16375{
16376 const struct strtab_entry *ea = a;
16377 const struct strtab_entry *eb = b;
16378 return !strcmp (ea->str, eb->str);
16379}
16380
16381/* Create a strtab_entry hash table. */
b89be57b 16382
9291a0cd
TT
16383static htab_t
16384create_strtab (void)
16385{
16386 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16387 xfree, xcalloc, xfree);
16388}
16389
16390/* Add a string to the constant pool. Return the string's offset in
16391 host order. */
b89be57b 16392
9291a0cd
TT
16393static offset_type
16394add_string (htab_t table, struct obstack *cpool, const char *str)
16395{
16396 void **slot;
16397 struct strtab_entry entry;
16398 struct strtab_entry *result;
16399
16400 entry.str = str;
16401 slot = htab_find_slot (table, &entry, INSERT);
16402 if (*slot)
16403 result = *slot;
16404 else
16405 {
16406 result = XNEW (struct strtab_entry);
16407 result->offset = obstack_object_size (cpool);
16408 result->str = str;
16409 obstack_grow_str0 (cpool, str);
16410 *slot = result;
16411 }
16412 return result->offset;
16413}
16414
16415/* An entry in the symbol table. */
16416struct symtab_index_entry
16417{
16418 /* The name of the symbol. */
16419 const char *name;
16420 /* The offset of the name in the constant pool. */
16421 offset_type index_offset;
16422 /* A sorted vector of the indices of all the CUs that hold an object
16423 of this name. */
16424 VEC (offset_type) *cu_indices;
16425};
16426
16427/* The symbol table. This is a power-of-2-sized hash table. */
16428struct mapped_symtab
16429{
16430 offset_type n_elements;
16431 offset_type size;
16432 struct symtab_index_entry **data;
16433};
16434
16435/* Hash function for a symtab_index_entry. */
b89be57b 16436
9291a0cd
TT
16437static hashval_t
16438hash_symtab_entry (const void *e)
16439{
16440 const struct symtab_index_entry *entry = e;
16441 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16442 sizeof (offset_type) * VEC_length (offset_type,
16443 entry->cu_indices),
16444 0);
16445}
16446
16447/* Equality function for a symtab_index_entry. */
b89be57b 16448
9291a0cd
TT
16449static int
16450eq_symtab_entry (const void *a, const void *b)
16451{
16452 const struct symtab_index_entry *ea = a;
16453 const struct symtab_index_entry *eb = b;
16454 int len = VEC_length (offset_type, ea->cu_indices);
16455 if (len != VEC_length (offset_type, eb->cu_indices))
16456 return 0;
16457 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16458 VEC_address (offset_type, eb->cu_indices),
16459 sizeof (offset_type) * len);
16460}
16461
16462/* Destroy a symtab_index_entry. */
b89be57b 16463
9291a0cd
TT
16464static void
16465delete_symtab_entry (void *p)
16466{
16467 struct symtab_index_entry *entry = p;
16468 VEC_free (offset_type, entry->cu_indices);
16469 xfree (entry);
16470}
16471
16472/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16473
9291a0cd 16474static htab_t
3876f04e 16475create_symbol_hash_table (void)
9291a0cd
TT
16476{
16477 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16478 delete_symtab_entry, xcalloc, xfree);
16479}
16480
16481/* Create a new mapped symtab object. */
b89be57b 16482
9291a0cd
TT
16483static struct mapped_symtab *
16484create_mapped_symtab (void)
16485{
16486 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16487 symtab->n_elements = 0;
16488 symtab->size = 1024;
16489 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16490 return symtab;
16491}
16492
16493/* Destroy a mapped_symtab. */
b89be57b 16494
9291a0cd
TT
16495static void
16496cleanup_mapped_symtab (void *p)
16497{
16498 struct mapped_symtab *symtab = p;
16499 /* The contents of the array are freed when the other hash table is
16500 destroyed. */
16501 xfree (symtab->data);
16502 xfree (symtab);
16503}
16504
16505/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
16506 the slot.
16507
16508 Function is used only during write_hash_table so no index format backward
16509 compatibility is needed. */
b89be57b 16510
9291a0cd
TT
16511static struct symtab_index_entry **
16512find_slot (struct mapped_symtab *symtab, const char *name)
16513{
559a7a62 16514 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
16515
16516 index = hash & (symtab->size - 1);
16517 step = ((hash * 17) & (symtab->size - 1)) | 1;
16518
16519 for (;;)
16520 {
16521 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16522 return &symtab->data[index];
16523 index = (index + step) & (symtab->size - 1);
16524 }
16525}
16526
16527/* Expand SYMTAB's hash table. */
b89be57b 16528
9291a0cd
TT
16529static void
16530hash_expand (struct mapped_symtab *symtab)
16531{
16532 offset_type old_size = symtab->size;
16533 offset_type i;
16534 struct symtab_index_entry **old_entries = symtab->data;
16535
16536 symtab->size *= 2;
16537 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16538
16539 for (i = 0; i < old_size; ++i)
16540 {
16541 if (old_entries[i])
16542 {
16543 struct symtab_index_entry **slot = find_slot (symtab,
16544 old_entries[i]->name);
16545 *slot = old_entries[i];
16546 }
16547 }
16548
16549 xfree (old_entries);
16550}
16551
16552/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16553 is the index of the CU in which the symbol appears. */
b89be57b 16554
9291a0cd
TT
16555static void
16556add_index_entry (struct mapped_symtab *symtab, const char *name,
16557 offset_type cu_index)
16558{
16559 struct symtab_index_entry **slot;
16560
16561 ++symtab->n_elements;
16562 if (4 * symtab->n_elements / 3 >= symtab->size)
16563 hash_expand (symtab);
16564
16565 slot = find_slot (symtab, name);
16566 if (!*slot)
16567 {
16568 *slot = XNEW (struct symtab_index_entry);
16569 (*slot)->name = name;
16570 (*slot)->cu_indices = NULL;
16571 }
16572 /* Don't push an index twice. Due to how we add entries we only
16573 have to check the last one. */
16574 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 16575 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
16576 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16577}
16578
16579/* Add a vector of indices to the constant pool. */
b89be57b 16580
9291a0cd 16581static offset_type
3876f04e 16582add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
16583 struct symtab_index_entry *entry)
16584{
16585 void **slot;
16586
3876f04e 16587 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
16588 if (!*slot)
16589 {
16590 offset_type len = VEC_length (offset_type, entry->cu_indices);
16591 offset_type val = MAYBE_SWAP (len);
16592 offset_type iter;
16593 int i;
16594
16595 *slot = entry;
16596 entry->index_offset = obstack_object_size (cpool);
16597
16598 obstack_grow (cpool, &val, sizeof (val));
16599 for (i = 0;
16600 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16601 ++i)
16602 {
16603 val = MAYBE_SWAP (iter);
16604 obstack_grow (cpool, &val, sizeof (val));
16605 }
16606 }
16607 else
16608 {
16609 struct symtab_index_entry *old_entry = *slot;
16610 entry->index_offset = old_entry->index_offset;
16611 entry = old_entry;
16612 }
16613 return entry->index_offset;
16614}
16615
16616/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16617 constant pool entries going into the obstack CPOOL. */
b89be57b 16618
9291a0cd
TT
16619static void
16620write_hash_table (struct mapped_symtab *symtab,
16621 struct obstack *output, struct obstack *cpool)
16622{
16623 offset_type i;
3876f04e 16624 htab_t symbol_hash_table;
9291a0cd
TT
16625 htab_t str_table;
16626
3876f04e 16627 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 16628 str_table = create_strtab ();
3876f04e 16629
9291a0cd
TT
16630 /* We add all the index vectors to the constant pool first, to
16631 ensure alignment is ok. */
16632 for (i = 0; i < symtab->size; ++i)
16633 {
16634 if (symtab->data[i])
3876f04e 16635 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
16636 }
16637
16638 /* Now write out the hash table. */
16639 for (i = 0; i < symtab->size; ++i)
16640 {
16641 offset_type str_off, vec_off;
16642
16643 if (symtab->data[i])
16644 {
16645 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16646 vec_off = symtab->data[i]->index_offset;
16647 }
16648 else
16649 {
16650 /* While 0 is a valid constant pool index, it is not valid
16651 to have 0 for both offsets. */
16652 str_off = 0;
16653 vec_off = 0;
16654 }
16655
16656 str_off = MAYBE_SWAP (str_off);
16657 vec_off = MAYBE_SWAP (vec_off);
16658
16659 obstack_grow (output, &str_off, sizeof (str_off));
16660 obstack_grow (output, &vec_off, sizeof (vec_off));
16661 }
16662
16663 htab_delete (str_table);
3876f04e 16664 htab_delete (symbol_hash_table);
9291a0cd
TT
16665}
16666
0a5429f6
DE
16667/* Struct to map psymtab to CU index in the index file. */
16668struct psymtab_cu_index_map
16669{
16670 struct partial_symtab *psymtab;
16671 unsigned int cu_index;
16672};
16673
16674static hashval_t
16675hash_psymtab_cu_index (const void *item)
16676{
16677 const struct psymtab_cu_index_map *map = item;
16678
16679 return htab_hash_pointer (map->psymtab);
16680}
16681
16682static int
16683eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16684{
16685 const struct psymtab_cu_index_map *lhs = item_lhs;
16686 const struct psymtab_cu_index_map *rhs = item_rhs;
16687
16688 return lhs->psymtab == rhs->psymtab;
16689}
16690
16691/* Helper struct for building the address table. */
16692struct addrmap_index_data
16693{
16694 struct objfile *objfile;
16695 struct obstack *addr_obstack;
16696 htab_t cu_index_htab;
16697
16698 /* Non-zero if the previous_* fields are valid.
16699 We can't write an entry until we see the next entry (since it is only then
16700 that we know the end of the entry). */
16701 int previous_valid;
16702 /* Index of the CU in the table of all CUs in the index file. */
16703 unsigned int previous_cu_index;
0963b4bd 16704 /* Start address of the CU. */
0a5429f6
DE
16705 CORE_ADDR previous_cu_start;
16706};
16707
16708/* Write an address entry to OBSTACK. */
b89be57b 16709
9291a0cd 16710static void
0a5429f6
DE
16711add_address_entry (struct objfile *objfile, struct obstack *obstack,
16712 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 16713{
0a5429f6 16714 offset_type cu_index_to_write;
9291a0cd
TT
16715 char addr[8];
16716 CORE_ADDR baseaddr;
16717
16718 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16719
0a5429f6
DE
16720 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16721 obstack_grow (obstack, addr, 8);
16722 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16723 obstack_grow (obstack, addr, 8);
16724 cu_index_to_write = MAYBE_SWAP (cu_index);
16725 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16726}
16727
16728/* Worker function for traversing an addrmap to build the address table. */
16729
16730static int
16731add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16732{
16733 struct addrmap_index_data *data = datap;
16734 struct partial_symtab *pst = obj;
16735 offset_type cu_index;
16736 void **slot;
16737
16738 if (data->previous_valid)
16739 add_address_entry (data->objfile, data->addr_obstack,
16740 data->previous_cu_start, start_addr,
16741 data->previous_cu_index);
16742
16743 data->previous_cu_start = start_addr;
16744 if (pst != NULL)
16745 {
16746 struct psymtab_cu_index_map find_map, *map;
16747 find_map.psymtab = pst;
16748 map = htab_find (data->cu_index_htab, &find_map);
16749 gdb_assert (map != NULL);
16750 data->previous_cu_index = map->cu_index;
16751 data->previous_valid = 1;
16752 }
16753 else
16754 data->previous_valid = 0;
16755
16756 return 0;
16757}
16758
16759/* Write OBJFILE's address map to OBSTACK.
16760 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16761 in the index file. */
16762
16763static void
16764write_address_map (struct objfile *objfile, struct obstack *obstack,
16765 htab_t cu_index_htab)
16766{
16767 struct addrmap_index_data addrmap_index_data;
16768
16769 /* When writing the address table, we have to cope with the fact that
16770 the addrmap iterator only provides the start of a region; we have to
16771 wait until the next invocation to get the start of the next region. */
16772
16773 addrmap_index_data.objfile = objfile;
16774 addrmap_index_data.addr_obstack = obstack;
16775 addrmap_index_data.cu_index_htab = cu_index_htab;
16776 addrmap_index_data.previous_valid = 0;
16777
16778 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16779 &addrmap_index_data);
16780
16781 /* It's highly unlikely the last entry (end address = 0xff...ff)
16782 is valid, but we should still handle it.
16783 The end address is recorded as the start of the next region, but that
16784 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16785 anyway. */
16786 if (addrmap_index_data.previous_valid)
16787 add_address_entry (objfile, obstack,
16788 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16789 addrmap_index_data.previous_cu_index);
9291a0cd
TT
16790}
16791
16792/* Add a list of partial symbols to SYMTAB. */
b89be57b 16793
9291a0cd
TT
16794static void
16795write_psymbols (struct mapped_symtab *symtab,
987d643c 16796 htab_t psyms_seen,
9291a0cd
TT
16797 struct partial_symbol **psymp,
16798 int count,
987d643c
TT
16799 offset_type cu_index,
16800 int is_static)
9291a0cd
TT
16801{
16802 for (; count-- > 0; ++psymp)
16803 {
987d643c
TT
16804 void **slot, *lookup;
16805
9291a0cd
TT
16806 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16807 error (_("Ada is not currently supported by the index"));
987d643c
TT
16808
16809 /* We only want to add a given psymbol once. However, we also
16810 want to account for whether it is global or static. So, we
16811 may add it twice, using slightly different values. */
16812 if (is_static)
16813 {
16814 uintptr_t val = 1 | (uintptr_t) *psymp;
16815
16816 lookup = (void *) val;
16817 }
16818 else
16819 lookup = *psymp;
16820
16821 /* Only add a given psymbol once. */
16822 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16823 if (!*slot)
16824 {
16825 *slot = lookup;
16826 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16827 }
9291a0cd
TT
16828 }
16829}
16830
16831/* Write the contents of an ("unfinished") obstack to FILE. Throw an
16832 exception if there is an error. */
b89be57b 16833
9291a0cd
TT
16834static void
16835write_obstack (FILE *file, struct obstack *obstack)
16836{
16837 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16838 file)
16839 != obstack_object_size (obstack))
16840 error (_("couldn't data write to file"));
16841}
16842
16843/* Unlink a file if the argument is not NULL. */
b89be57b 16844
9291a0cd
TT
16845static void
16846unlink_if_set (void *p)
16847{
16848 char **filename = p;
16849 if (*filename)
16850 unlink (*filename);
16851}
16852
1fd400ff
TT
16853/* A helper struct used when iterating over debug_types. */
16854struct signatured_type_index_data
16855{
16856 struct objfile *objfile;
16857 struct mapped_symtab *symtab;
16858 struct obstack *types_list;
987d643c 16859 htab_t psyms_seen;
1fd400ff
TT
16860 int cu_index;
16861};
16862
16863/* A helper function that writes a single signatured_type to an
16864 obstack. */
b89be57b 16865
1fd400ff
TT
16866static int
16867write_one_signatured_type (void **slot, void *d)
16868{
16869 struct signatured_type_index_data *info = d;
16870 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
16871 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16872 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
16873 gdb_byte val[8];
16874
16875 write_psymbols (info->symtab,
987d643c 16876 info->psyms_seen,
3e43a32a
MS
16877 info->objfile->global_psymbols.list
16878 + psymtab->globals_offset,
987d643c
TT
16879 psymtab->n_global_syms, info->cu_index,
16880 0);
1fd400ff 16881 write_psymbols (info->symtab,
987d643c 16882 info->psyms_seen,
3e43a32a
MS
16883 info->objfile->static_psymbols.list
16884 + psymtab->statics_offset,
987d643c
TT
16885 psymtab->n_static_syms, info->cu_index,
16886 1);
1fd400ff 16887
b3c8eb43 16888 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
1fd400ff
TT
16889 obstack_grow (info->types_list, val, 8);
16890 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16891 obstack_grow (info->types_list, val, 8);
16892 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16893 obstack_grow (info->types_list, val, 8);
16894
16895 ++info->cu_index;
16896
16897 return 1;
16898}
16899
9291a0cd 16900/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 16901
9291a0cd
TT
16902static void
16903write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16904{
16905 struct cleanup *cleanup;
16906 char *filename, *cleanup_filename;
1fd400ff
TT
16907 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16908 struct obstack cu_list, types_cu_list;
9291a0cd
TT
16909 int i;
16910 FILE *out_file;
16911 struct mapped_symtab *symtab;
16912 offset_type val, size_of_contents, total_len;
16913 struct stat st;
16914 char buf[8];
987d643c 16915 htab_t psyms_seen;
0a5429f6
DE
16916 htab_t cu_index_htab;
16917 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 16918
b4f2f049 16919 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 16920 return;
b4f2f049 16921
9291a0cd
TT
16922 if (dwarf2_per_objfile->using_index)
16923 error (_("Cannot use an index to create the index"));
16924
8b70b953
TT
16925 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16926 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16927
9291a0cd 16928 if (stat (objfile->name, &st) < 0)
7e17e088 16929 perror_with_name (objfile->name);
9291a0cd
TT
16930
16931 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16932 INDEX_SUFFIX, (char *) NULL);
16933 cleanup = make_cleanup (xfree, filename);
16934
16935 out_file = fopen (filename, "wb");
16936 if (!out_file)
16937 error (_("Can't open `%s' for writing"), filename);
16938
16939 cleanup_filename = filename;
16940 make_cleanup (unlink_if_set, &cleanup_filename);
16941
16942 symtab = create_mapped_symtab ();
16943 make_cleanup (cleanup_mapped_symtab, symtab);
16944
16945 obstack_init (&addr_obstack);
16946 make_cleanup_obstack_free (&addr_obstack);
16947
16948 obstack_init (&cu_list);
16949 make_cleanup_obstack_free (&cu_list);
16950
1fd400ff
TT
16951 obstack_init (&types_cu_list);
16952 make_cleanup_obstack_free (&types_cu_list);
16953
987d643c
TT
16954 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16955 NULL, xcalloc, xfree);
96408a79 16956 make_cleanup_htab_delete (psyms_seen);
987d643c 16957
0a5429f6
DE
16958 /* While we're scanning CU's create a table that maps a psymtab pointer
16959 (which is what addrmap records) to its index (which is what is recorded
16960 in the index file). This will later be needed to write the address
16961 table. */
16962 cu_index_htab = htab_create_alloc (100,
16963 hash_psymtab_cu_index,
16964 eq_psymtab_cu_index,
16965 NULL, xcalloc, xfree);
96408a79 16966 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
16967 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16968 xmalloc (sizeof (struct psymtab_cu_index_map)
16969 * dwarf2_per_objfile->n_comp_units);
16970 make_cleanup (xfree, psymtab_cu_index_map);
16971
16972 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
16973 work here. Also, the debug_types entries do not appear in
16974 all_comp_units, but only in their own hash table. */
9291a0cd
TT
16975 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16976 {
3e43a32a
MS
16977 struct dwarf2_per_cu_data *per_cu
16978 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 16979 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 16980 gdb_byte val[8];
0a5429f6
DE
16981 struct psymtab_cu_index_map *map;
16982 void **slot;
9291a0cd
TT
16983
16984 write_psymbols (symtab,
987d643c 16985 psyms_seen,
9291a0cd 16986 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
16987 psymtab->n_global_syms, i,
16988 0);
9291a0cd 16989 write_psymbols (symtab,
987d643c 16990 psyms_seen,
9291a0cd 16991 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
16992 psymtab->n_static_syms, i,
16993 1);
9291a0cd 16994
0a5429f6
DE
16995 map = &psymtab_cu_index_map[i];
16996 map->psymtab = psymtab;
16997 map->cu_index = i;
16998 slot = htab_find_slot (cu_index_htab, map, INSERT);
16999 gdb_assert (slot != NULL);
17000 gdb_assert (*slot == NULL);
17001 *slot = map;
9291a0cd 17002
e254ef6a 17003 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
9291a0cd 17004 obstack_grow (&cu_list, val, 8);
e254ef6a 17005 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17006 obstack_grow (&cu_list, val, 8);
17007 }
17008
0a5429f6
DE
17009 /* Dump the address map. */
17010 write_address_map (objfile, &addr_obstack, cu_index_htab);
17011
1fd400ff
TT
17012 /* Write out the .debug_type entries, if any. */
17013 if (dwarf2_per_objfile->signatured_types)
17014 {
17015 struct signatured_type_index_data sig_data;
17016
17017 sig_data.objfile = objfile;
17018 sig_data.symtab = symtab;
17019 sig_data.types_list = &types_cu_list;
987d643c 17020 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17021 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17022 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17023 write_one_signatured_type, &sig_data);
17024 }
17025
9291a0cd
TT
17026 obstack_init (&constant_pool);
17027 make_cleanup_obstack_free (&constant_pool);
17028 obstack_init (&symtab_obstack);
17029 make_cleanup_obstack_free (&symtab_obstack);
17030 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17031
17032 obstack_init (&contents);
17033 make_cleanup_obstack_free (&contents);
1fd400ff 17034 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17035 total_len = size_of_contents;
17036
17037 /* The version number. */
559a7a62 17038 val = MAYBE_SWAP (5);
9291a0cd
TT
17039 obstack_grow (&contents, &val, sizeof (val));
17040
17041 /* The offset of the CU list from the start of the file. */
17042 val = MAYBE_SWAP (total_len);
17043 obstack_grow (&contents, &val, sizeof (val));
17044 total_len += obstack_object_size (&cu_list);
17045
1fd400ff
TT
17046 /* The offset of the types CU list from the start of the file. */
17047 val = MAYBE_SWAP (total_len);
17048 obstack_grow (&contents, &val, sizeof (val));
17049 total_len += obstack_object_size (&types_cu_list);
17050
9291a0cd
TT
17051 /* The offset of the address table from the start of the file. */
17052 val = MAYBE_SWAP (total_len);
17053 obstack_grow (&contents, &val, sizeof (val));
17054 total_len += obstack_object_size (&addr_obstack);
17055
17056 /* The offset of the symbol table from the start of the file. */
17057 val = MAYBE_SWAP (total_len);
17058 obstack_grow (&contents, &val, sizeof (val));
17059 total_len += obstack_object_size (&symtab_obstack);
17060
17061 /* The offset of the constant pool from the start of the file. */
17062 val = MAYBE_SWAP (total_len);
17063 obstack_grow (&contents, &val, sizeof (val));
17064 total_len += obstack_object_size (&constant_pool);
17065
17066 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17067
17068 write_obstack (out_file, &contents);
17069 write_obstack (out_file, &cu_list);
1fd400ff 17070 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17071 write_obstack (out_file, &addr_obstack);
17072 write_obstack (out_file, &symtab_obstack);
17073 write_obstack (out_file, &constant_pool);
17074
17075 fclose (out_file);
17076
17077 /* We want to keep the file, so we set cleanup_filename to NULL
17078 here. See unlink_if_set. */
17079 cleanup_filename = NULL;
17080
17081 do_cleanups (cleanup);
17082}
17083
90476074
TT
17084/* Implementation of the `save gdb-index' command.
17085
17086 Note that the file format used by this command is documented in the
17087 GDB manual. Any changes here must be documented there. */
11570e71 17088
9291a0cd
TT
17089static void
17090save_gdb_index_command (char *arg, int from_tty)
17091{
17092 struct objfile *objfile;
17093
17094 if (!arg || !*arg)
96d19272 17095 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17096
17097 ALL_OBJFILES (objfile)
17098 {
17099 struct stat st;
17100
17101 /* If the objfile does not correspond to an actual file, skip it. */
17102 if (stat (objfile->name, &st) < 0)
17103 continue;
17104
17105 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17106 if (dwarf2_per_objfile)
17107 {
17108 volatile struct gdb_exception except;
17109
17110 TRY_CATCH (except, RETURN_MASK_ERROR)
17111 {
17112 write_psymtabs_to_index (objfile, arg);
17113 }
17114 if (except.reason < 0)
17115 exception_fprintf (gdb_stderr, except,
17116 _("Error while writing index for `%s': "),
17117 objfile->name);
17118 }
17119 }
dce234bc
PP
17120}
17121
9291a0cd
TT
17122\f
17123
9eae7c52
TT
17124int dwarf2_always_disassemble;
17125
17126static void
17127show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17128 struct cmd_list_element *c, const char *value)
17129{
3e43a32a
MS
17130 fprintf_filtered (file,
17131 _("Whether to always disassemble "
17132 "DWARF expressions is %s.\n"),
9eae7c52
TT
17133 value);
17134}
17135
900e11f9
JK
17136static void
17137show_check_physname (struct ui_file *file, int from_tty,
17138 struct cmd_list_element *c, const char *value)
17139{
17140 fprintf_filtered (file,
17141 _("Whether to check \"physname\" is %s.\n"),
17142 value);
17143}
17144
6502dd73
DJ
17145void _initialize_dwarf2_read (void);
17146
17147void
17148_initialize_dwarf2_read (void)
17149{
96d19272
JK
17150 struct cmd_list_element *c;
17151
dce234bc 17152 dwarf2_objfile_data_key
c1bd65d0 17153 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17154
1bedd215
AC
17155 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17156Set DWARF 2 specific variables.\n\
17157Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17158 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17159 0/*allow-unknown*/, &maintenance_set_cmdlist);
17160
1bedd215
AC
17161 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17162Show DWARF 2 specific variables\n\
17163Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17164 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17165 0/*allow-unknown*/, &maintenance_show_cmdlist);
17166
17167 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17168 &dwarf2_max_cache_age, _("\
17169Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17170Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17171A higher limit means that cached compilation units will be stored\n\
17172in memory longer, and more total memory will be used. Zero disables\n\
17173caching, which can slow down startup."),
2c5b56ce 17174 NULL,
920d2a44 17175 show_dwarf2_max_cache_age,
2c5b56ce 17176 &set_dwarf2_cmdlist,
ae038cb0 17177 &show_dwarf2_cmdlist);
d97bc12b 17178
9eae7c52
TT
17179 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17180 &dwarf2_always_disassemble, _("\
17181Set whether `info address' always disassembles DWARF expressions."), _("\
17182Show whether `info address' always disassembles DWARF expressions."), _("\
17183When enabled, DWARF expressions are always printed in an assembly-like\n\
17184syntax. When disabled, expressions will be printed in a more\n\
17185conversational style, when possible."),
17186 NULL,
17187 show_dwarf2_always_disassemble,
17188 &set_dwarf2_cmdlist,
17189 &show_dwarf2_cmdlist);
17190
d97bc12b
DE
17191 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17192Set debugging of the dwarf2 DIE reader."), _("\
17193Show debugging of the dwarf2 DIE reader."), _("\
17194When enabled (non-zero), DIEs are dumped after they are read in.\n\
17195The value is the maximum depth to print."),
17196 NULL,
17197 NULL,
17198 &setdebuglist, &showdebuglist);
9291a0cd 17199
900e11f9
JK
17200 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17201Set cross-checking of \"physname\" code against demangler."), _("\
17202Show cross-checking of \"physname\" code against demangler."), _("\
17203When enabled, GDB's internal \"physname\" code is checked against\n\
17204the demangler."),
17205 NULL, show_check_physname,
17206 &setdebuglist, &showdebuglist);
17207
96d19272 17208 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17209 _("\
fc1a9d6e 17210Save a gdb-index file.\n\
11570e71 17211Usage: save gdb-index DIRECTORY"),
96d19272
JK
17212 &save_cmdlist);
17213 set_cmd_completer (c, filename_completer);
6502dd73 17214}
This page took 2.330207 seconds and 4 git commands to generate.