*** empty log message ***
[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,
4c38e0a4 4 2004, 2005, 2006, 2007, 2008, 2009, 2010
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"
4c2df51b 57
c906108c
SS
58#include <fcntl.h>
59#include "gdb_string.h"
4bdf3d34 60#include "gdb_assert.h"
c906108c 61#include <sys/types.h>
233a11ab
CS
62#ifdef HAVE_ZLIB_H
63#include <zlib.h>
64#endif
dce234bc
PP
65#ifdef HAVE_MMAP
66#include <sys/mman.h>
85d9bd0e
TT
67#ifndef MAP_FAILED
68#define MAP_FAILED ((void *) -1)
69#endif
dce234bc 70#endif
d8151005 71
107d2387 72#if 0
357e46e7 73/* .debug_info header for a compilation unit
c906108c
SS
74 Because of alignment constraints, this structure has padding and cannot
75 be mapped directly onto the beginning of the .debug_info section. */
76typedef struct comp_unit_header
77 {
78 unsigned int length; /* length of the .debug_info
79 contribution */
80 unsigned short version; /* version number -- 2 for DWARF
81 version 2 */
82 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
83 unsigned char addr_size; /* byte size of an address -- 4 */
84 }
85_COMP_UNIT_HEADER;
86#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 87#endif
c906108c 88
c906108c
SS
89/* .debug_line statement program prologue
90 Because of alignment constraints, this structure has padding and cannot
91 be mapped directly onto the beginning of the .debug_info section. */
92typedef struct statement_prologue
93 {
94 unsigned int total_length; /* byte length of the statement
95 information */
96 unsigned short version; /* version number -- 2 for DWARF
97 version 2 */
98 unsigned int prologue_length; /* # bytes between prologue &
99 stmt program */
100 unsigned char minimum_instruction_length; /* byte size of
101 smallest instr */
102 unsigned char default_is_stmt; /* initial value of is_stmt
103 register */
104 char line_base;
105 unsigned char line_range;
106 unsigned char opcode_base; /* number assigned to first special
107 opcode */
108 unsigned char *standard_opcode_lengths;
109 }
110_STATEMENT_PROLOGUE;
111
d97bc12b
DE
112/* When non-zero, dump DIEs after they are read in. */
113static int dwarf2_die_debug = 0;
114
dce234bc
PP
115static int pagesize;
116
df8a16a1
DJ
117/* When set, the file that we're processing is known to have debugging
118 info for C++ namespaces. GCC 3.3.x did not produce this information,
119 but later versions do. */
120
121static int processing_has_namespace_info;
122
6502dd73
DJ
123static const struct objfile_data *dwarf2_objfile_data_key;
124
dce234bc
PP
125struct dwarf2_section_info
126{
127 asection *asection;
128 gdb_byte *buffer;
129 bfd_size_type size;
130 int was_mmapped;
be391dca
TT
131 /* True if we have tried to read this section. */
132 int readin;
dce234bc
PP
133};
134
9291a0cd
TT
135/* All offsets in the index are of this type. It must be
136 architecture-independent. */
137typedef uint32_t offset_type;
138
139DEF_VEC_I (offset_type);
140
141/* A description of the mapped index. The file format is described in
142 a comment by the code that writes the index. */
143struct mapped_index
144{
145 /* The total length of the buffer. */
146 off_t total_size;
147 /* A pointer to the address table data. */
148 const gdb_byte *address_table;
149 /* Size of the address table data in bytes. */
150 offset_type address_table_size;
151 /* The hash table. */
152 const offset_type *index_table;
153 /* Size in slots, each slot is 2 offset_types. */
154 offset_type index_table_slots;
155 /* A pointer to the constant pool. */
156 const char *constant_pool;
157};
158
6502dd73
DJ
159struct dwarf2_per_objfile
160{
dce234bc
PP
161 struct dwarf2_section_info info;
162 struct dwarf2_section_info abbrev;
163 struct dwarf2_section_info line;
dce234bc
PP
164 struct dwarf2_section_info loc;
165 struct dwarf2_section_info macinfo;
166 struct dwarf2_section_info str;
167 struct dwarf2_section_info ranges;
348e048f 168 struct dwarf2_section_info types;
dce234bc
PP
169 struct dwarf2_section_info frame;
170 struct dwarf2_section_info eh_frame;
9291a0cd 171 struct dwarf2_section_info gdb_index;
ae038cb0 172
be391dca
TT
173 /* Back link. */
174 struct objfile *objfile;
175
10b3939b
DJ
176 /* A list of all the compilation units. This is used to locate
177 the target compilation unit of a particular reference. */
ae038cb0
DJ
178 struct dwarf2_per_cu_data **all_comp_units;
179
180 /* The number of compilation units in ALL_COMP_UNITS. */
181 int n_comp_units;
182
1fd400ff
TT
183 /* The number of .debug_types-related CUs. */
184 int n_type_comp_units;
185
186 /* The .debug_types-related CUs. */
187 struct dwarf2_per_cu_data **type_comp_units;
188
ae038cb0
DJ
189 /* A chain of compilation units that are currently read in, so that
190 they can be freed later. */
191 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 192
348e048f
DE
193 /* A table mapping .debug_types signatures to its signatured_type entry.
194 This is NULL if the .debug_types section hasn't been read in yet. */
195 htab_t signatured_types;
196
72dca2f5
FR
197 /* A flag indicating wether this objfile has a section loaded at a
198 VMA of 0. */
199 int has_section_at_zero;
9291a0cd
TT
200
201 /* True if we are using the mapped index. */
202 unsigned char using_index;
203
204 /* The mapped index. */
205 struct mapped_index *index_table;
6502dd73
DJ
206};
207
208static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
209
210/* names of the debugging sections */
211
233a11ab
CS
212/* Note that if the debugging section has been compressed, it might
213 have a name like .zdebug_info. */
214
215#define INFO_SECTION "debug_info"
216#define ABBREV_SECTION "debug_abbrev"
217#define LINE_SECTION "debug_line"
233a11ab
CS
218#define LOC_SECTION "debug_loc"
219#define MACINFO_SECTION "debug_macinfo"
220#define STR_SECTION "debug_str"
221#define RANGES_SECTION "debug_ranges"
348e048f 222#define TYPES_SECTION "debug_types"
233a11ab
CS
223#define FRAME_SECTION "debug_frame"
224#define EH_FRAME_SECTION "eh_frame"
9291a0cd 225#define GDB_INDEX_SECTION "gdb_index"
c906108c
SS
226
227/* local data types */
228
57349743
JB
229/* We hold several abbreviation tables in memory at the same time. */
230#ifndef ABBREV_HASH_SIZE
231#define ABBREV_HASH_SIZE 121
232#endif
233
107d2387
AC
234/* The data in a compilation unit header, after target2host
235 translation, looks like this. */
c906108c 236struct comp_unit_head
a738430d 237{
c764a876 238 unsigned int length;
a738430d 239 short version;
a738430d
MK
240 unsigned char addr_size;
241 unsigned char signed_addr_p;
9cbfa09e 242 unsigned int abbrev_offset;
57349743 243
a738430d
MK
244 /* Size of file offsets; either 4 or 8. */
245 unsigned int offset_size;
57349743 246
a738430d
MK
247 /* Size of the length field; either 4 or 12. */
248 unsigned int initial_length_size;
57349743 249
a738430d
MK
250 /* Offset to the first byte of this compilation unit header in the
251 .debug_info section, for resolving relative reference dies. */
252 unsigned int offset;
57349743 253
d00adf39
DE
254 /* Offset to first die in this cu from the start of the cu.
255 This will be the first byte following the compilation unit header. */
256 unsigned int first_die_offset;
a738430d 257};
c906108c 258
e7c27a73
DJ
259/* Internal state when decoding a particular compilation unit. */
260struct dwarf2_cu
261{
262 /* The objfile containing this compilation unit. */
263 struct objfile *objfile;
264
d00adf39 265 /* The header of the compilation unit. */
e7c27a73 266 struct comp_unit_head header;
e142c38c 267
d00adf39
DE
268 /* Base address of this compilation unit. */
269 CORE_ADDR base_address;
270
271 /* Non-zero if base_address has been set. */
272 int base_known;
273
e142c38c
DJ
274 struct function_range *first_fn, *last_fn, *cached_fn;
275
276 /* The language we are debugging. */
277 enum language language;
278 const struct language_defn *language_defn;
279
b0f35d58
DL
280 const char *producer;
281
e142c38c
DJ
282 /* The generic symbol table building routines have separate lists for
283 file scope symbols and all all other scopes (local scopes). So
284 we need to select the right one to pass to add_symbol_to_list().
285 We do it by keeping a pointer to the correct list in list_in_scope.
286
287 FIXME: The original dwarf code just treated the file scope as the
288 first local scope, and all other local scopes as nested local
289 scopes, and worked fine. Check to see if we really need to
290 distinguish these in buildsym.c. */
291 struct pending **list_in_scope;
292
f3dd6933
DJ
293 /* DWARF abbreviation table associated with this compilation unit. */
294 struct abbrev_info **dwarf2_abbrevs;
295
296 /* Storage for the abbrev table. */
297 struct obstack abbrev_obstack;
72bf9492
DJ
298
299 /* Hash table holding all the loaded partial DIEs. */
300 htab_t partial_dies;
301
302 /* Storage for things with the same lifetime as this read-in compilation
303 unit, including partial DIEs. */
304 struct obstack comp_unit_obstack;
305
ae038cb0
DJ
306 /* When multiple dwarf2_cu structures are living in memory, this field
307 chains them all together, so that they can be released efficiently.
308 We will probably also want a generation counter so that most-recently-used
309 compilation units are cached... */
310 struct dwarf2_per_cu_data *read_in_chain;
311
312 /* Backchain to our per_cu entry if the tree has been built. */
313 struct dwarf2_per_cu_data *per_cu;
314
f792889a
DJ
315 /* Pointer to the die -> type map. Although it is stored
316 permanently in per_cu, we copy it here to avoid double
317 indirection. */
318 htab_t type_hash;
319
ae038cb0
DJ
320 /* How many compilation units ago was this CU last referenced? */
321 int last_used;
322
10b3939b 323 /* A hash table of die offsets for following references. */
51545339 324 htab_t die_hash;
10b3939b
DJ
325
326 /* Full DIEs if read in. */
327 struct die_info *dies;
328
329 /* A set of pointers to dwarf2_per_cu_data objects for compilation
330 units referenced by this one. Only set during full symbol processing;
331 partial symbol tables do not have dependencies. */
332 htab_t dependencies;
333
cb1df416
DJ
334 /* Header data from the line table, during full symbol processing. */
335 struct line_header *line_header;
336
ae038cb0
DJ
337 /* Mark used when releasing cached dies. */
338 unsigned int mark : 1;
339
340 /* This flag will be set if this compilation unit might include
341 inter-compilation-unit references. */
342 unsigned int has_form_ref_addr : 1;
343
72bf9492
DJ
344 /* This flag will be set if this compilation unit includes any
345 DW_TAG_namespace DIEs. If we know that there are explicit
346 DIEs for namespaces, we don't need to try to infer them
347 from mangled names. */
348 unsigned int has_namespace_info : 1;
e7c27a73
DJ
349};
350
9291a0cd
TT
351/* When using the index (and thus not using psymtabs), each CU has an
352 object of this type. This is used to hold information needed by
353 the various "quick" methods. */
354struct dwarf2_per_cu_quick_data
355{
356 /* The line table. This can be NULL if there was no line table. */
357 struct line_header *lines;
358
359 /* The file names from the line table. */
360 const char **file_names;
361 /* The file names from the line table after being run through
362 gdb_realpath. */
363 const char **full_names;
364
365 /* The corresponding symbol table. This is NULL if symbols for this
366 CU have not yet been read. */
367 struct symtab *symtab;
368
369 /* A temporary mark bit used when iterating over all CUs in
370 expand_symtabs_matching. */
371 unsigned int mark : 1;
372
373 /* True if we've tried to read the line table. */
374 unsigned int read_lines : 1;
375};
376
10b3939b
DJ
377/* Persistent data held for a compilation unit, even when not
378 processing it. We put a pointer to this structure in the
379 read_symtab_private field of the psymtab. If we encounter
380 inter-compilation-unit references, we also maintain a sorted
381 list of all compilation units. */
382
ae038cb0
DJ
383struct dwarf2_per_cu_data
384{
348e048f 385 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 386 bytes should suffice to store the length of any compilation unit
45452591
DE
387 - if it doesn't, GDB will fall over anyway.
388 NOTE: Unlike comp_unit_head.length, this length includes
389 initial_length_size. */
c764a876 390 unsigned int offset;
348e048f 391 unsigned int length : 29;
ae038cb0
DJ
392
393 /* Flag indicating this compilation unit will be read in before
394 any of the current compilation units are processed. */
c764a876 395 unsigned int queued : 1;
ae038cb0 396
5afb4e99
DJ
397 /* This flag will be set if we need to load absolutely all DIEs
398 for this compilation unit, instead of just the ones we think
399 are interesting. It gets set if we look for a DIE in the
400 hash table and don't find it. */
401 unsigned int load_all_dies : 1;
402
348e048f
DE
403 /* Non-zero if this CU is from .debug_types.
404 Otherwise it's from .debug_info. */
405 unsigned int from_debug_types : 1;
406
17ea53c3
JK
407 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
408 of the CU cache it gets reset to NULL again. */
ae038cb0 409 struct dwarf2_cu *cu;
1c379e20
DJ
410
411 /* If full symbols for this CU have been read in, then this field
412 holds a map of DIE offsets to types. It isn't always possible
413 to reconstruct this information later, so we have to preserve
414 it. */
1c379e20 415 htab_t type_hash;
10b3939b 416
9291a0cd
TT
417 /* The corresponding objfile. */
418 struct objfile *objfile;
419
420 /* When using partial symbol tables, the 'psymtab' field is active.
421 Otherwise the 'quick' field is active. */
422 union
423 {
424 /* The partial symbol table associated with this compilation unit,
425 or NULL for partial units (which do not have an associated
426 symtab). */
427 struct partial_symtab *psymtab;
428
429 /* Data needed by the "quick" functions. */
430 struct dwarf2_per_cu_quick_data *quick;
431 } v;
ae038cb0
DJ
432};
433
348e048f
DE
434/* Entry in the signatured_types hash table. */
435
436struct signatured_type
437{
438 ULONGEST signature;
439
440 /* Offset in .debug_types of the TU (type_unit) for this type. */
441 unsigned int offset;
442
443 /* Offset in .debug_types of the type defined by this TU. */
444 unsigned int type_offset;
445
446 /* The CU(/TU) of this type. */
447 struct dwarf2_per_cu_data per_cu;
448};
449
93311388
DE
450/* Struct used to pass misc. parameters to read_die_and_children, et. al.
451 which are used for both .debug_info and .debug_types dies.
452 All parameters here are unchanging for the life of the call.
453 This struct exists to abstract away the constant parameters of
454 die reading. */
455
456struct die_reader_specs
457{
458 /* The bfd of this objfile. */
459 bfd* abfd;
460
461 /* The CU of the DIE we are parsing. */
462 struct dwarf2_cu *cu;
463
464 /* Pointer to start of section buffer.
465 This is either the start of .debug_info or .debug_types. */
466 const gdb_byte *buffer;
467};
468
debd256d
JB
469/* The line number information for a compilation unit (found in the
470 .debug_line section) begins with a "statement program header",
471 which contains the following information. */
472struct line_header
473{
474 unsigned int total_length;
475 unsigned short version;
476 unsigned int header_length;
477 unsigned char minimum_instruction_length;
2dc7f7b3 478 unsigned char maximum_ops_per_instruction;
debd256d
JB
479 unsigned char default_is_stmt;
480 int line_base;
481 unsigned char line_range;
482 unsigned char opcode_base;
483
484 /* standard_opcode_lengths[i] is the number of operands for the
485 standard opcode whose value is i. This means that
486 standard_opcode_lengths[0] is unused, and the last meaningful
487 element is standard_opcode_lengths[opcode_base - 1]. */
488 unsigned char *standard_opcode_lengths;
489
490 /* The include_directories table. NOTE! These strings are not
491 allocated with xmalloc; instead, they are pointers into
492 debug_line_buffer. If you try to free them, `free' will get
493 indigestion. */
494 unsigned int num_include_dirs, include_dirs_size;
495 char **include_dirs;
496
497 /* The file_names table. NOTE! These strings are not allocated
498 with xmalloc; instead, they are pointers into debug_line_buffer.
499 Don't try to free them directly. */
500 unsigned int num_file_names, file_names_size;
501 struct file_entry
c906108c 502 {
debd256d
JB
503 char *name;
504 unsigned int dir_index;
505 unsigned int mod_time;
506 unsigned int length;
aaa75496 507 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 508 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
509 } *file_names;
510
511 /* The start and end of the statement program following this
6502dd73 512 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 513 gdb_byte *statement_program_start, *statement_program_end;
debd256d 514};
c906108c
SS
515
516/* When we construct a partial symbol table entry we only
517 need this much information. */
518struct partial_die_info
519 {
72bf9492 520 /* Offset of this DIE. */
c906108c 521 unsigned int offset;
72bf9492
DJ
522
523 /* DWARF-2 tag for this DIE. */
524 ENUM_BITFIELD(dwarf_tag) tag : 16;
525
72bf9492
DJ
526 /* Assorted flags describing the data found in this DIE. */
527 unsigned int has_children : 1;
528 unsigned int is_external : 1;
529 unsigned int is_declaration : 1;
530 unsigned int has_type : 1;
531 unsigned int has_specification : 1;
532 unsigned int has_pc_info : 1;
533
534 /* Flag set if the SCOPE field of this structure has been
535 computed. */
536 unsigned int scope_set : 1;
537
fa4028e9
JB
538 /* Flag set if the DIE has a byte_size attribute. */
539 unsigned int has_byte_size : 1;
540
72bf9492 541 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 542 sometimes a default name for unnamed DIEs. */
c906108c 543 char *name;
72bf9492
DJ
544
545 /* The scope to prepend to our children. This is generally
546 allocated on the comp_unit_obstack, so will disappear
547 when this compilation unit leaves the cache. */
548 char *scope;
549
550 /* The location description associated with this DIE, if any. */
551 struct dwarf_block *locdesc;
552
553 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
554 CORE_ADDR lowpc;
555 CORE_ADDR highpc;
72bf9492 556
93311388 557 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 558 DW_AT_sibling, if any. */
fe1b8b76 559 gdb_byte *sibling;
72bf9492
DJ
560
561 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
562 DW_AT_specification (or DW_AT_abstract_origin or
563 DW_AT_extension). */
564 unsigned int spec_offset;
565
566 /* Pointers to this DIE's parent, first child, and next sibling,
567 if any. */
568 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
569 };
570
571/* This data structure holds the information of an abbrev. */
572struct abbrev_info
573 {
574 unsigned int number; /* number identifying abbrev */
575 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
576 unsigned short has_children; /* boolean */
577 unsigned short num_attrs; /* number of attributes */
c906108c
SS
578 struct attr_abbrev *attrs; /* an array of attribute descriptions */
579 struct abbrev_info *next; /* next in chain */
580 };
581
582struct attr_abbrev
583 {
9d25dd43
DE
584 ENUM_BITFIELD(dwarf_attribute) name : 16;
585 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
586 };
587
b60c80d6
DJ
588/* Attributes have a name and a value */
589struct attribute
590 {
9d25dd43 591 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
592 ENUM_BITFIELD(dwarf_form) form : 15;
593
594 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
595 field should be in u.str (existing only for DW_STRING) but it is kept
596 here for better struct attribute alignment. */
597 unsigned int string_is_canonical : 1;
598
b60c80d6
DJ
599 union
600 {
601 char *str;
602 struct dwarf_block *blk;
43bbcdc2
PH
603 ULONGEST unsnd;
604 LONGEST snd;
b60c80d6 605 CORE_ADDR addr;
348e048f 606 struct signatured_type *signatured_type;
b60c80d6
DJ
607 }
608 u;
609 };
610
c906108c
SS
611/* This data structure holds a complete die structure. */
612struct die_info
613 {
76815b17
DE
614 /* DWARF-2 tag for this DIE. */
615 ENUM_BITFIELD(dwarf_tag) tag : 16;
616
617 /* Number of attributes */
618 unsigned short num_attrs;
619
620 /* Abbrev number */
621 unsigned int abbrev;
622
93311388 623 /* Offset in .debug_info or .debug_types section. */
76815b17 624 unsigned int offset;
78ba4af6
JB
625
626 /* The dies in a compilation unit form an n-ary tree. PARENT
627 points to this die's parent; CHILD points to the first child of
628 this node; and all the children of a given node are chained
629 together via their SIBLING fields, terminated by a die whose
630 tag is zero. */
639d11d3
DC
631 struct die_info *child; /* Its first child, if any. */
632 struct die_info *sibling; /* Its next sibling, if any. */
633 struct die_info *parent; /* Its parent, if any. */
c906108c 634
b60c80d6
DJ
635 /* An array of attributes, with NUM_ATTRS elements. There may be
636 zero, but it's not common and zero-sized arrays are not
637 sufficiently portable C. */
638 struct attribute attrs[1];
c906108c
SS
639 };
640
5fb290d7
DJ
641struct function_range
642{
643 const char *name;
644 CORE_ADDR lowpc, highpc;
645 int seen_line;
646 struct function_range *next;
647};
648
c906108c
SS
649/* Get at parts of an attribute structure */
650
651#define DW_STRING(attr) ((attr)->u.str)
8285870a 652#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
653#define DW_UNSND(attr) ((attr)->u.unsnd)
654#define DW_BLOCK(attr) ((attr)->u.blk)
655#define DW_SND(attr) ((attr)->u.snd)
656#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 657#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c
SS
658
659/* Blocks are a bunch of untyped bytes. */
660struct dwarf_block
661 {
662 unsigned int size;
fe1b8b76 663 gdb_byte *data;
c906108c
SS
664 };
665
c906108c
SS
666#ifndef ATTR_ALLOC_CHUNK
667#define ATTR_ALLOC_CHUNK 4
668#endif
669
c906108c
SS
670/* Allocate fields for structs, unions and enums in this size. */
671#ifndef DW_FIELD_ALLOC_CHUNK
672#define DW_FIELD_ALLOC_CHUNK 4
673#endif
674
c906108c
SS
675/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
676 but this would require a corresponding change in unpack_field_as_long
677 and friends. */
678static int bits_per_byte = 8;
679
680/* The routines that read and process dies for a C struct or C++ class
681 pass lists of data member fields and lists of member function fields
682 in an instance of a field_info structure, as defined below. */
683struct field_info
c5aa993b
JM
684 {
685 /* List of data member and baseclasses fields. */
686 struct nextfield
687 {
688 struct nextfield *next;
689 int accessibility;
690 int virtuality;
691 struct field field;
692 }
7d0ccb61 693 *fields, *baseclasses;
c906108c 694
7d0ccb61 695 /* Number of fields (including baseclasses). */
c5aa993b 696 int nfields;
c906108c 697
c5aa993b
JM
698 /* Number of baseclasses. */
699 int nbaseclasses;
c906108c 700
c5aa993b
JM
701 /* Set if the accesibility of one of the fields is not public. */
702 int non_public_fields;
c906108c 703
c5aa993b
JM
704 /* Member function fields array, entries are allocated in the order they
705 are encountered in the object file. */
706 struct nextfnfield
707 {
708 struct nextfnfield *next;
709 struct fn_field fnfield;
710 }
711 *fnfields;
c906108c 712
c5aa993b
JM
713 /* Member function fieldlist array, contains name of possibly overloaded
714 member function, number of overloaded member functions and a pointer
715 to the head of the member function field chain. */
716 struct fnfieldlist
717 {
718 char *name;
719 int length;
720 struct nextfnfield *head;
721 }
722 *fnfieldlists;
c906108c 723
c5aa993b
JM
724 /* Number of entries in the fnfieldlists array. */
725 int nfnfields;
98751a41
JK
726
727 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
728 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
729 struct typedef_field_list
730 {
731 struct typedef_field field;
732 struct typedef_field_list *next;
733 }
734 *typedef_field_list;
735 unsigned typedef_field_list_count;
c5aa993b 736 };
c906108c 737
10b3939b
DJ
738/* One item on the queue of compilation units to read in full symbols
739 for. */
740struct dwarf2_queue_item
741{
742 struct dwarf2_per_cu_data *per_cu;
743 struct dwarf2_queue_item *next;
744};
745
746/* The current queue. */
747static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
748
ae038cb0
DJ
749/* Loaded secondary compilation units are kept in memory until they
750 have not been referenced for the processing of this many
751 compilation units. Set this to zero to disable caching. Cache
752 sizes of up to at least twenty will improve startup time for
753 typical inter-CU-reference binaries, at an obvious memory cost. */
754static int dwarf2_max_cache_age = 5;
920d2a44
AC
755static void
756show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
757 struct cmd_list_element *c, const char *value)
758{
759 fprintf_filtered (file, _("\
760The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
761 value);
762}
763
ae038cb0 764
c906108c
SS
765/* Various complaints about symbol reading that don't abort the process */
766
4d3c2250
KB
767static void
768dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 769{
4d3c2250 770 complaint (&symfile_complaints,
e2e0b3e5 771 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
772}
773
25e43795
DJ
774static void
775dwarf2_debug_line_missing_file_complaint (void)
776{
777 complaint (&symfile_complaints,
778 _(".debug_line section has line data without a file"));
779}
780
59205f5a
JB
781static void
782dwarf2_debug_line_missing_end_sequence_complaint (void)
783{
784 complaint (&symfile_complaints,
785 _(".debug_line section has line program sequence without an end"));
786}
787
4d3c2250
KB
788static void
789dwarf2_complex_location_expr_complaint (void)
2e276125 790{
e2e0b3e5 791 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
792}
793
4d3c2250
KB
794static void
795dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
796 int arg3)
2e276125 797{
4d3c2250 798 complaint (&symfile_complaints,
e2e0b3e5 799 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
800 arg2, arg3);
801}
802
803static void
804dwarf2_macros_too_long_complaint (void)
2e276125 805{
4d3c2250 806 complaint (&symfile_complaints,
e2e0b3e5 807 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
808}
809
810static void
811dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 812{
4d3c2250 813 complaint (&symfile_complaints,
e2e0b3e5 814 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
815 arg1);
816}
817
818static void
819dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 820{
4d3c2250 821 complaint (&symfile_complaints,
e2e0b3e5 822 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 823}
c906108c 824
c906108c
SS
825/* local function prototypes */
826
4efb68b1 827static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 828
aaa75496
JB
829static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
830 struct objfile *);
831
832static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
d85a05f0 833 struct die_info *,
aaa75496
JB
834 struct partial_symtab *);
835
c67a9c90 836static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 837
72bf9492
DJ
838static void scan_partial_symbols (struct partial_die_info *,
839 CORE_ADDR *, CORE_ADDR *,
5734ee8b 840 int, struct dwarf2_cu *);
c906108c 841
72bf9492
DJ
842static void add_partial_symbol (struct partial_die_info *,
843 struct dwarf2_cu *);
63d06c5c 844
72bf9492
DJ
845static void add_partial_namespace (struct partial_die_info *pdi,
846 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 847 int need_pc, struct dwarf2_cu *cu);
63d06c5c 848
5d7cb8df
JK
849static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
850 CORE_ADDR *highpc, int need_pc,
851 struct dwarf2_cu *cu);
852
72bf9492
DJ
853static void add_partial_enumeration (struct partial_die_info *enum_pdi,
854 struct dwarf2_cu *cu);
91c24f0a 855
bc30ff58
JB
856static void add_partial_subprogram (struct partial_die_info *pdi,
857 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 858 int need_pc, struct dwarf2_cu *cu);
bc30ff58 859
fe1b8b76 860static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
93311388
DE
861 gdb_byte *buffer, gdb_byte *info_ptr,
862 bfd *abfd, struct dwarf2_cu *cu);
91c24f0a 863
a14ed312 864static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 865
a14ed312 866static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 867
e7c27a73 868static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 869
f3dd6933 870static void dwarf2_free_abbrev_table (void *);
c906108c 871
fe1b8b76 872static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 873 struct dwarf2_cu *);
72bf9492 874
57349743 875static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 876 struct dwarf2_cu *);
c906108c 877
93311388
DE
878static struct partial_die_info *load_partial_dies (bfd *,
879 gdb_byte *, gdb_byte *,
880 int, struct dwarf2_cu *);
72bf9492 881
fe1b8b76 882static gdb_byte *read_partial_die (struct partial_die_info *,
93311388
DE
883 struct abbrev_info *abbrev,
884 unsigned int, bfd *,
885 gdb_byte *, gdb_byte *,
886 struct dwarf2_cu *);
c906108c 887
c764a876 888static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 889 struct dwarf2_cu *);
72bf9492
DJ
890
891static void fixup_partial_die (struct partial_die_info *,
892 struct dwarf2_cu *);
893
fe1b8b76
JB
894static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
895 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 896
fe1b8b76
JB
897static gdb_byte *read_attribute_value (struct attribute *, unsigned,
898 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 899
fe1b8b76 900static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 901
fe1b8b76 902static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 903
fe1b8b76 904static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 905
fe1b8b76 906static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 907
93311388 908static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 909
fe1b8b76 910static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 911 unsigned int *);
c906108c 912
c764a876
DE
913static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
914
915static LONGEST read_checked_initial_length_and_offset
916 (bfd *, gdb_byte *, const struct comp_unit_head *,
917 unsigned int *, unsigned int *);
613e1657 918
fe1b8b76 919static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
920 unsigned int *);
921
922static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 923
fe1b8b76 924static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 925
fe1b8b76 926static char *read_string (bfd *, gdb_byte *, unsigned int *);
c906108c 927
fe1b8b76
JB
928static char *read_indirect_string (bfd *, gdb_byte *,
929 const struct comp_unit_head *,
930 unsigned int *);
4bdf3d34 931
fe1b8b76 932static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 933
fe1b8b76 934static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 935
fe1b8b76 936static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 937
e142c38c 938static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 939
e142c38c
DJ
940static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
941 struct dwarf2_cu *);
c906108c 942
348e048f
DE
943static struct attribute *dwarf2_attr_no_follow (struct die_info *,
944 unsigned int,
945 struct dwarf2_cu *);
946
05cf31d1
JB
947static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
948 struct dwarf2_cu *cu);
949
e142c38c 950static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 951
e142c38c 952static struct die_info *die_specification (struct die_info *die,
f2f0e013 953 struct dwarf2_cu **);
63d06c5c 954
debd256d
JB
955static void free_line_header (struct line_header *lh);
956
aaa75496
JB
957static void add_file_name (struct line_header *, char *, unsigned int,
958 unsigned int, unsigned int);
959
debd256d
JB
960static struct line_header *(dwarf_decode_line_header
961 (unsigned int offset,
e7c27a73 962 bfd *abfd, struct dwarf2_cu *cu));
debd256d
JB
963
964static void dwarf_decode_lines (struct line_header *, char *, bfd *,
aaa75496 965 struct dwarf2_cu *, struct partial_symtab *);
c906108c 966
4f1520fb 967static void dwarf2_start_subfile (char *, char *, char *);
c906108c 968
a14ed312 969static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 970 struct dwarf2_cu *);
c906108c 971
a14ed312 972static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 973 struct dwarf2_cu *);
c906108c 974
2df3850c
JM
975static void dwarf2_const_value_data (struct attribute *attr,
976 struct symbol *sym,
977 int bits);
978
e7c27a73 979static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 980
b4ba55a1
JB
981static int need_gnat_info (struct dwarf2_cu *);
982
983static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
984
985static void set_descriptive_type (struct type *, struct die_info *,
986 struct dwarf2_cu *);
987
e7c27a73
DJ
988static struct type *die_containing_type (struct die_info *,
989 struct dwarf2_cu *);
c906108c 990
e7c27a73 991static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
c906108c 992
f792889a 993static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 994
086ed43d 995static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 996
6e70227d 997static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
998 const char *suffix, int physname,
999 struct dwarf2_cu *cu);
63d06c5c 1000
e7c27a73 1001static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1002
348e048f
DE
1003static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1004
e7c27a73 1005static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1006
e7c27a73 1007static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1008
ff013f42
JK
1009static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1010 struct dwarf2_cu *, struct partial_symtab *);
1011
a14ed312 1012static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1013 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1014 struct partial_symtab *);
c906108c 1015
fae299cd
DC
1016static void get_scope_pc_bounds (struct die_info *,
1017 CORE_ADDR *, CORE_ADDR *,
1018 struct dwarf2_cu *);
1019
801e3a5b
JB
1020static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1021 CORE_ADDR, struct dwarf2_cu *);
1022
a14ed312 1023static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1024 struct dwarf2_cu *);
c906108c 1025
a14ed312 1026static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1027 struct type *, struct dwarf2_cu *);
c906108c 1028
a14ed312 1029static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1030 struct die_info *, struct type *,
e7c27a73 1031 struct dwarf2_cu *);
c906108c 1032
a14ed312 1033static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 1034 struct type *, struct dwarf2_cu *);
c906108c 1035
134d01f1 1036static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1037
e7c27a73 1038static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1039
e7c27a73 1040static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1041
5d7cb8df
JK
1042static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1043
27aa8d6a
SW
1044static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1045
f55ee35c
JK
1046static struct type *read_module_type (struct die_info *die,
1047 struct dwarf2_cu *cu);
1048
38d518c9 1049static const char *namespace_name (struct die_info *die,
e142c38c 1050 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1051
134d01f1 1052static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1053
e7c27a73 1054static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1055
6e70227d 1056static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1057 struct dwarf2_cu *);
1058
93311388 1059static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
c906108c 1060
93311388
DE
1061static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1062 gdb_byte *info_ptr,
d97bc12b
DE
1063 gdb_byte **new_info_ptr,
1064 struct die_info *parent);
1065
93311388
DE
1066static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1067 gdb_byte *info_ptr,
fe1b8b76 1068 gdb_byte **new_info_ptr,
639d11d3
DC
1069 struct die_info *parent);
1070
93311388
DE
1071static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1072 gdb_byte *info_ptr,
fe1b8b76 1073 gdb_byte **new_info_ptr,
639d11d3
DC
1074 struct die_info *parent);
1075
93311388
DE
1076static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1077 struct die_info **, gdb_byte *,
1078 int *);
1079
e7c27a73 1080static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1081
71c25dea
TT
1082static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1083 struct obstack *);
1084
e142c38c 1085static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1086
e142c38c 1087static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1088 struct dwarf2_cu **);
9219021c 1089
a14ed312 1090static char *dwarf_tag_name (unsigned int);
c906108c 1091
a14ed312 1092static char *dwarf_attr_name (unsigned int);
c906108c 1093
a14ed312 1094static char *dwarf_form_name (unsigned int);
c906108c 1095
a14ed312 1096static char *dwarf_bool_name (unsigned int);
c906108c 1097
a14ed312 1098static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1099
1100#if 0
a14ed312 1101static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1102#endif
1103
f9aca02d 1104static struct die_info *sibling_die (struct die_info *);
c906108c 1105
d97bc12b
DE
1106static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1107
1108static void dump_die_for_error (struct die_info *);
1109
1110static void dump_die_1 (struct ui_file *, int level, int max_level,
1111 struct die_info *);
c906108c 1112
d97bc12b 1113/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1114
51545339 1115static void store_in_ref_table (struct die_info *,
10b3939b 1116 struct dwarf2_cu *);
c906108c 1117
93311388
DE
1118static int is_ref_attr (struct attribute *);
1119
c764a876 1120static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1121
43bbcdc2 1122static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1123
348e048f
DE
1124static struct die_info *follow_die_ref_or_sig (struct die_info *,
1125 struct attribute *,
1126 struct dwarf2_cu **);
1127
10b3939b
DJ
1128static struct die_info *follow_die_ref (struct die_info *,
1129 struct attribute *,
f2f0e013 1130 struct dwarf2_cu **);
c906108c 1131
348e048f
DE
1132static struct die_info *follow_die_sig (struct die_info *,
1133 struct attribute *,
1134 struct dwarf2_cu **);
1135
1136static void read_signatured_type_at_offset (struct objfile *objfile,
1137 unsigned int offset);
1138
1139static void read_signatured_type (struct objfile *,
1140 struct signatured_type *type_sig);
1141
c906108c
SS
1142/* memory allocation interface */
1143
7b5a2f43 1144static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1145
f3dd6933 1146static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1147
b60c80d6 1148static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1149
e142c38c 1150static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1151
e142c38c
DJ
1152static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1153 struct dwarf2_cu *);
5fb290d7 1154
2e276125 1155static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1156 char *, bfd *, struct dwarf2_cu *);
2e276125 1157
8e19ed76
PS
1158static int attr_form_is_block (struct attribute *);
1159
3690dd37
JB
1160static int attr_form_is_section_offset (struct attribute *);
1161
1162static int attr_form_is_constant (struct attribute *);
1163
93e7bd98
DJ
1164static void dwarf2_symbol_mark_computed (struct attribute *attr,
1165 struct symbol *sym,
1166 struct dwarf2_cu *cu);
4c2df51b 1167
93311388
DE
1168static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1169 struct abbrev_info *abbrev,
1170 struct dwarf2_cu *cu);
4bb7a0a7 1171
72bf9492
DJ
1172static void free_stack_comp_unit (void *);
1173
72bf9492
DJ
1174static hashval_t partial_die_hash (const void *item);
1175
1176static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1177
ae038cb0 1178static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1179 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1180
1181static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1182 (unsigned int offset, struct objfile *objfile);
ae038cb0 1183
93311388
DE
1184static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1185
ae038cb0
DJ
1186static void free_one_comp_unit (void *);
1187
1188static void free_cached_comp_units (void *);
1189
1190static void age_cached_comp_units (void);
1191
1192static void free_one_cached_comp_unit (void *);
1193
f792889a
DJ
1194static struct type *set_die_type (struct die_info *, struct type *,
1195 struct dwarf2_cu *);
1c379e20 1196
ae038cb0
DJ
1197static void create_all_comp_units (struct objfile *);
1198
1fd400ff
TT
1199static int create_debug_types_hash_table (struct objfile *objfile);
1200
93311388
DE
1201static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1202 struct objfile *);
10b3939b
DJ
1203
1204static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1205
1206static void dwarf2_add_dependence (struct dwarf2_cu *,
1207 struct dwarf2_per_cu_data *);
1208
ae038cb0
DJ
1209static void dwarf2_mark (struct dwarf2_cu *);
1210
1211static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1212
f792889a 1213static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1214
9291a0cd
TT
1215static void dwarf2_release_queue (void *dummy);
1216
1217static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1218 struct objfile *objfile);
1219
1220static void process_queue (struct objfile *objfile);
1221
1222static void find_file_and_directory (struct die_info *die,
1223 struct dwarf2_cu *cu,
1224 char **name, char **comp_dir);
1225
1226static char *file_full_name (int file, struct line_header *lh,
1227 const char *comp_dir);
1228
1229static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1230 gdb_byte *info_ptr,
1231 gdb_byte *buffer,
1232 unsigned int buffer_size,
1233 bfd *abfd);
1234
1235static void init_cu_die_reader (struct die_reader_specs *reader,
1236 struct dwarf2_cu *cu);
1237
1fd400ff
TT
1238static htab_t allocate_signatured_type_hash_table (struct objfile *objfile);
1239
9291a0cd
TT
1240#if WORDS_BIGENDIAN
1241
1242/* Convert VALUE between big- and little-endian. */
1243static offset_type
1244byte_swap (offset_type value)
1245{
1246 offset_type result;
1247
1248 result = (value & 0xff) << 24;
1249 result |= (value & 0xff00) << 8;
1250 result |= (value & 0xff0000) >> 8;
1251 result |= (value & 0xff000000) >> 24;
1252 return result;
1253}
1254
1255#define MAYBE_SWAP(V) byte_swap (V)
1256
1257#else
1258#define MAYBE_SWAP(V) (V)
1259#endif /* WORDS_BIGENDIAN */
1260
1261/* The suffix for an index file. */
1262#define INDEX_SUFFIX ".gdb-index"
1263
c906108c
SS
1264/* Try to locate the sections we need for DWARF 2 debugging
1265 information and return true if we have enough to do something. */
1266
1267int
6502dd73 1268dwarf2_has_info (struct objfile *objfile)
c906108c 1269{
be391dca
TT
1270 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1271 if (!dwarf2_per_objfile)
1272 {
1273 /* Initialize per-objfile state. */
1274 struct dwarf2_per_objfile *data
1275 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1276
be391dca
TT
1277 memset (data, 0, sizeof (*data));
1278 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1279 dwarf2_per_objfile = data;
6502dd73 1280
be391dca
TT
1281 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1282 dwarf2_per_objfile->objfile = objfile;
1283 }
1284 return (dwarf2_per_objfile->info.asection != NULL
1285 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1286}
1287
233a11ab
CS
1288/* When loading sections, we can either look for ".<name>", or for
1289 * ".z<name>", which indicates a compressed section. */
1290
1291static int
dce234bc 1292section_is_p (const char *section_name, const char *name)
233a11ab 1293{
dce234bc
PP
1294 return (section_name[0] == '.'
1295 && (strcmp (section_name + 1, name) == 0
1296 || (section_name[1] == 'z'
1297 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1298}
1299
c906108c
SS
1300/* This function is mapped across the sections and remembers the
1301 offset and size of each of the debugging sections we are interested
1302 in. */
1303
1304static void
72dca2f5 1305dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1306{
dce234bc 1307 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1308 {
dce234bc
PP
1309 dwarf2_per_objfile->info.asection = sectp;
1310 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1311 }
dce234bc 1312 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1313 {
dce234bc
PP
1314 dwarf2_per_objfile->abbrev.asection = sectp;
1315 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1316 }
dce234bc 1317 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1318 {
dce234bc
PP
1319 dwarf2_per_objfile->line.asection = sectp;
1320 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1321 }
dce234bc 1322 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1323 {
dce234bc
PP
1324 dwarf2_per_objfile->loc.asection = sectp;
1325 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1326 }
dce234bc 1327 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1328 {
dce234bc
PP
1329 dwarf2_per_objfile->macinfo.asection = sectp;
1330 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1331 }
dce234bc 1332 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1333 {
dce234bc
PP
1334 dwarf2_per_objfile->str.asection = sectp;
1335 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1336 }
dce234bc 1337 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1338 {
dce234bc
PP
1339 dwarf2_per_objfile->frame.asection = sectp;
1340 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1341 }
dce234bc 1342 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1343 {
3799ccc6 1344 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1345
3799ccc6
EZ
1346 if (aflag & SEC_HAS_CONTENTS)
1347 {
dce234bc
PP
1348 dwarf2_per_objfile->eh_frame.asection = sectp;
1349 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1350 }
b6af0555 1351 }
dce234bc 1352 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1353 {
dce234bc
PP
1354 dwarf2_per_objfile->ranges.asection = sectp;
1355 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1356 }
348e048f
DE
1357 else if (section_is_p (sectp->name, TYPES_SECTION))
1358 {
1359 dwarf2_per_objfile->types.asection = sectp;
1360 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1361 }
9291a0cd
TT
1362 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1363 {
1364 dwarf2_per_objfile->gdb_index.asection = sectp;
1365 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1366 }
dce234bc 1367
72dca2f5
FR
1368 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1369 && bfd_section_vma (abfd, sectp) == 0)
1370 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1371}
1372
dce234bc
PP
1373/* Decompress a section that was compressed using zlib. Store the
1374 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1375
1376static void
dce234bc
PP
1377zlib_decompress_section (struct objfile *objfile, asection *sectp,
1378 gdb_byte **outbuf, bfd_size_type *outsize)
1379{
1380 bfd *abfd = objfile->obfd;
1381#ifndef HAVE_ZLIB_H
1382 error (_("Support for zlib-compressed DWARF data (from '%s') "
1383 "is disabled in this copy of GDB"),
1384 bfd_get_filename (abfd));
1385#else
1386 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1387 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1388 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1389 bfd_size_type uncompressed_size;
1390 gdb_byte *uncompressed_buffer;
1391 z_stream strm;
1392 int rc;
1393 int header_size = 12;
1394
1395 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1396 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1397 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1398 bfd_get_filename (abfd));
1399
1400 /* Read the zlib header. In this case, it should be "ZLIB" followed
1401 by the uncompressed section size, 8 bytes in big-endian order. */
1402 if (compressed_size < header_size
1403 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1404 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1405 bfd_get_filename (abfd));
1406 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1407 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1408 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1409 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1410 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1411 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1412 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1413 uncompressed_size += compressed_buffer[11];
1414
1415 /* It is possible the section consists of several compressed
1416 buffers concatenated together, so we uncompress in a loop. */
1417 strm.zalloc = NULL;
1418 strm.zfree = NULL;
1419 strm.opaque = NULL;
1420 strm.avail_in = compressed_size - header_size;
1421 strm.next_in = (Bytef*) compressed_buffer + header_size;
1422 strm.avail_out = uncompressed_size;
1423 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1424 uncompressed_size);
1425 rc = inflateInit (&strm);
1426 while (strm.avail_in > 0)
1427 {
1428 if (rc != Z_OK)
1429 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1430 bfd_get_filename (abfd), rc);
1431 strm.next_out = ((Bytef*) uncompressed_buffer
1432 + (uncompressed_size - strm.avail_out));
1433 rc = inflate (&strm, Z_FINISH);
1434 if (rc != Z_STREAM_END)
1435 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1436 bfd_get_filename (abfd), rc);
1437 rc = inflateReset (&strm);
1438 }
1439 rc = inflateEnd (&strm);
1440 if (rc != Z_OK
1441 || strm.avail_out != 0)
1442 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1443 bfd_get_filename (abfd), rc);
1444
affddf13 1445 do_cleanups (cleanup);
dce234bc
PP
1446 *outbuf = uncompressed_buffer;
1447 *outsize = uncompressed_size;
1448#endif
233a11ab
CS
1449}
1450
dce234bc
PP
1451/* Read the contents of the section SECTP from object file specified by
1452 OBJFILE, store info about the section into INFO.
1453 If the section is compressed, uncompress it before returning. */
c906108c 1454
dce234bc
PP
1455static void
1456dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1457{
dce234bc
PP
1458 bfd *abfd = objfile->obfd;
1459 asection *sectp = info->asection;
1460 gdb_byte *buf, *retbuf;
1461 unsigned char header[4];
c906108c 1462
be391dca
TT
1463 if (info->readin)
1464 return;
dce234bc
PP
1465 info->buffer = NULL;
1466 info->was_mmapped = 0;
be391dca 1467 info->readin = 1;
188dd5d6 1468
dce234bc
PP
1469 if (info->asection == NULL || info->size == 0)
1470 return;
c906108c 1471
dce234bc
PP
1472 /* Check if the file has a 4-byte header indicating compression. */
1473 if (info->size > sizeof (header)
1474 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1475 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1476 {
1477 /* Upon decompression, update the buffer and its size. */
1478 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1479 {
1480 zlib_decompress_section (objfile, sectp, &info->buffer,
1481 &info->size);
1482 return;
1483 }
1484 }
4bdf3d34 1485
dce234bc
PP
1486#ifdef HAVE_MMAP
1487 if (pagesize == 0)
1488 pagesize = getpagesize ();
2e276125 1489
dce234bc
PP
1490 /* Only try to mmap sections which are large enough: we don't want to
1491 waste space due to fragmentation. Also, only try mmap for sections
1492 without relocations. */
1493
1494 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1495 {
1496 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1497 size_t map_length = info->size + sectp->filepos - pg_offset;
1498 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1499 MAP_PRIVATE, pg_offset);
1500
1501 if (retbuf != MAP_FAILED)
1502 {
1503 info->was_mmapped = 1;
1504 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
be391dca
TT
1505#if HAVE_POSIX_MADVISE
1506 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1507#endif
dce234bc
PP
1508 return;
1509 }
1510 }
1511#endif
1512
1513 /* If we get here, we are a normal, not-compressed section. */
1514 info->buffer = buf
1515 = obstack_alloc (&objfile->objfile_obstack, info->size);
1516
1517 /* When debugging .o files, we may need to apply relocations; see
1518 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1519 We never compress sections in .o files, so we only need to
1520 try this when the section is not compressed. */
ac8035ab 1521 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1522 if (retbuf != NULL)
1523 {
1524 info->buffer = retbuf;
1525 return;
1526 }
1527
1528 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1529 || bfd_bread (buf, info->size, abfd) != info->size)
1530 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1531 bfd_get_filename (abfd));
1532}
1533
1534/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1535 SECTION_NAME. */
af34e669 1536
dce234bc
PP
1537void
1538dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1539 asection **sectp, gdb_byte **bufp,
1540 bfd_size_type *sizep)
1541{
1542 struct dwarf2_per_objfile *data
1543 = objfile_data (objfile, dwarf2_objfile_data_key);
1544 struct dwarf2_section_info *info;
a3b2a86b
TT
1545
1546 /* We may see an objfile without any DWARF, in which case we just
1547 return nothing. */
1548 if (data == NULL)
1549 {
1550 *sectp = NULL;
1551 *bufp = NULL;
1552 *sizep = 0;
1553 return;
1554 }
dce234bc
PP
1555 if (section_is_p (section_name, EH_FRAME_SECTION))
1556 info = &data->eh_frame;
1557 else if (section_is_p (section_name, FRAME_SECTION))
1558 info = &data->frame;
0d53c4c4 1559 else
dce234bc
PP
1560 gdb_assert (0);
1561
1562 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1563 /* We haven't read this section in yet. Do it now. */
1564 dwarf2_read_section (objfile, info);
1565
1566 *sectp = info->asection;
1567 *bufp = info->buffer;
1568 *sizep = info->size;
1569}
1570
9291a0cd
TT
1571\f
1572
1573/* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1574 this CU came. */
1575static void
1576dw2_do_instantiate_symtab (struct objfile *objfile,
1577 struct dwarf2_per_cu_data *per_cu)
1578{
1579 struct cleanup *back_to;
1580
1581 back_to = make_cleanup (dwarf2_release_queue, NULL);
1582
1583 queue_comp_unit (per_cu, objfile);
1584
1585 if (per_cu->from_debug_types)
1586 read_signatured_type_at_offset (objfile, per_cu->offset);
1587 else
1588 load_full_comp_unit (per_cu, objfile);
1589
1590 process_queue (objfile);
1591
1592 /* Age the cache, releasing compilation units that have not
1593 been used recently. */
1594 age_cached_comp_units ();
1595
1596 do_cleanups (back_to);
1597}
1598
1599/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1600 the objfile from which this CU came. Returns the resulting symbol
1601 table. */
1602static struct symtab *
1603dw2_instantiate_symtab (struct objfile *objfile,
1604 struct dwarf2_per_cu_data *per_cu)
1605{
1606 if (!per_cu->v.quick->symtab)
1607 {
1608 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1609 increment_reading_symtab ();
1610 dw2_do_instantiate_symtab (objfile, per_cu);
1611 do_cleanups (back_to);
1612 }
1613 return per_cu->v.quick->symtab;
1614}
1615
1fd400ff
TT
1616/* Return the CU given its index. */
1617static struct dwarf2_per_cu_data *
1618dw2_get_cu (int index)
1619{
1620 if (index >= dwarf2_per_objfile->n_comp_units)
1621 {
1622 index -= dwarf2_per_objfile->n_comp_units;
1623 return dwarf2_per_objfile->type_comp_units[index];
1624 }
1625 return dwarf2_per_objfile->all_comp_units[index];
1626}
1627
9291a0cd
TT
1628/* A helper function that knows how to read a 64-bit value in a way
1629 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1630 otherwise. */
1631static int
1632extract_cu_value (const char *bytes, ULONGEST *result)
1633{
1634 if (sizeof (ULONGEST) < 8)
1635 {
1636 int i;
1637
1638 /* Ignore the upper 4 bytes if they are all zero. */
1639 for (i = 0; i < 4; ++i)
1640 if (bytes[i + 4] != 0)
1641 return 0;
1642
1643 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1644 }
1645 else
1646 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1647 return 1;
1648}
1649
1650/* Read the CU list from the mapped index, and use it to create all
1651 the CU objects for this objfile. Return 0 if something went wrong,
1652 1 if everything went ok. */
1653static int
1fd400ff
TT
1654create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1655 offset_type cu_list_elements)
9291a0cd
TT
1656{
1657 offset_type i;
9291a0cd
TT
1658
1659 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1660 dwarf2_per_objfile->all_comp_units
1661 = obstack_alloc (&objfile->objfile_obstack,
1662 dwarf2_per_objfile->n_comp_units
1663 * sizeof (struct dwarf2_per_cu_data *));
1664
1665 for (i = 0; i < cu_list_elements; i += 2)
1666 {
1667 struct dwarf2_per_cu_data *the_cu;
1668 ULONGEST offset, length;
1669
1670 if (!extract_cu_value (cu_list, &offset)
1671 || !extract_cu_value (cu_list + 8, &length))
1672 return 0;
1673 cu_list += 2 * 8;
1674
1675 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1676 struct dwarf2_per_cu_data);
1677 the_cu->offset = offset;
1678 the_cu->length = length;
1679 the_cu->objfile = objfile;
1680 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1681 struct dwarf2_per_cu_quick_data);
1682 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1683 }
1684
1685 return 1;
1686}
1687
1fd400ff
TT
1688/* Create the signatured type hash table from the index. */
1689static int
1690create_signatured_type_hash_from_index (struct objfile *objfile,
1691 const gdb_byte *bytes,
1692 offset_type elements)
1693{
1694 offset_type i;
1695 htab_t type_hash;
1696
1697 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1698 dwarf2_per_objfile->type_comp_units
1699 = obstack_alloc (&objfile->objfile_obstack,
1700 dwarf2_per_objfile->n_type_comp_units
1701 * sizeof (struct dwarf2_per_cu_data *));
1702
1703 type_hash = allocate_signatured_type_hash_table (objfile);
1704
1705 for (i = 0; i < elements; i += 3)
1706 {
1707 struct signatured_type *type_sig;
1708 ULONGEST offset, type_offset, signature;
1709 void **slot;
1710
1711 if (!extract_cu_value (bytes, &offset)
1712 || !extract_cu_value (bytes + 8, &type_offset))
1713 return 0;
1714 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1715 bytes += 3 * 8;
1716
1717 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1718 struct signatured_type);
1719 type_sig->signature = signature;
1720 type_sig->offset = offset;
1721 type_sig->type_offset = type_offset;
1722 type_sig->per_cu.from_debug_types = 1;
1723 type_sig->per_cu.offset = offset;
1724 type_sig->per_cu.objfile = objfile;
1725 type_sig->per_cu.v.quick
1726 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1727 struct dwarf2_per_cu_quick_data);
1728
1729 slot = htab_find_slot (type_hash, type_sig, INSERT);
1730 *slot = type_sig;
1731
1732 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1733 }
1734
1735 dwarf2_per_objfile->signatured_types = type_hash;
1736
1737 return 1;
1738}
1739
9291a0cd
TT
1740/* Read the address map data from the mapped index, and use it to
1741 populate the objfile's psymtabs_addrmap. */
1742static void
1743create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1744{
1745 const gdb_byte *iter, *end;
1746 struct obstack temp_obstack;
1747 struct addrmap *mutable_map;
1748 struct cleanup *cleanup;
1749 CORE_ADDR baseaddr;
1750
1751 obstack_init (&temp_obstack);
1752 cleanup = make_cleanup_obstack_free (&temp_obstack);
1753 mutable_map = addrmap_create_mutable (&temp_obstack);
1754
1755 iter = index->address_table;
1756 end = iter + index->address_table_size;
1757
1758 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1759
1760 while (iter < end)
1761 {
1762 ULONGEST hi, lo, cu_index;
1763 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1764 iter += 8;
1765 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1766 iter += 8;
1767 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1768 iter += 4;
1769
1770 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 1771 dw2_get_cu (cu_index));
9291a0cd
TT
1772 }
1773
1774 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1775 &objfile->objfile_obstack);
1776 do_cleanups (cleanup);
1777}
1778
1779/* The hash function for strings in the mapped index. This is the
1780 same as the hashtab.c hash function, but we keep a separate copy to
1781 maintain control over the implementation. This is necessary
1782 because the hash function is tied to the format of the mapped index
1783 file. */
1784static hashval_t
1785mapped_index_string_hash (const void *p)
1786{
1787 const unsigned char *str = (const unsigned char *) p;
1788 hashval_t r = 0;
1789 unsigned char c;
1790
1791 while ((c = *str++) != 0)
1792 r = r * 67 + c - 113;
1793
1794 return r;
1795}
1796
1797/* Find a slot in the mapped index INDEX for the object named NAME.
1798 If NAME is found, set *VEC_OUT to point to the CU vector in the
1799 constant pool and return 1. If NAME cannot be found, return 0. */
1800static int
1801find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1802 offset_type **vec_out)
1803{
1804 offset_type hash = mapped_index_string_hash (name);
1805 offset_type slot, step;
1806
1807 slot = hash & (index->index_table_slots - 1);
1808 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1809
1810 for (;;)
1811 {
1812 /* Convert a slot number to an offset into the table. */
1813 offset_type i = 2 * slot;
1814 const char *str;
1815 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1816 return 0;
1817
1818 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1819 if (!strcmp (name, str))
1820 {
1821 *vec_out = (offset_type *) (index->constant_pool
1822 + MAYBE_SWAP (index->index_table[i + 1]));
1823 return 1;
1824 }
1825
1826 slot = (slot + step) & (index->index_table_slots - 1);
1827 }
1828}
1829
1830/* Read the index file. If everything went ok, initialize the "quick"
1831 elements of all the CUs and return 1. Otherwise, return 0. */
1832static int
1833dwarf2_read_index (struct objfile *objfile)
1834{
9291a0cd
TT
1835 char *addr;
1836 struct mapped_index *map;
b3b272e1 1837 offset_type *metadata;
1fd400ff
TT
1838 const gdb_byte *cu_list, *types_list;
1839 offset_type version, cu_list_elements, types_list_elements;
1840 int i;
9291a0cd
TT
1841
1842 if (dwarf2_per_objfile->gdb_index.asection == NULL
1843 || dwarf2_per_objfile->gdb_index.size == 0)
1844 return 0;
1845 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1846
1847 addr = dwarf2_per_objfile->gdb_index.buffer;
1848 /* Version check. */
1fd400ff
TT
1849 version = MAYBE_SWAP (*(offset_type *) addr);
1850 if (version == 1)
1851 {
1852 /* Index version 1 neglected to account for .debug_types. So,
1853 if we see .debug_types, we cannot use this index. */
1854 if (dwarf2_per_objfile->types.asection != NULL
1855 && dwarf2_per_objfile->types.size != 0)
1856 return 0;
1857 }
1858 else if (version != 2)
9291a0cd
TT
1859 return 0;
1860
1861 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
b3b272e1 1862 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
1863
1864 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
1865
1866 i = 0;
1867 cu_list = addr + MAYBE_SWAP (metadata[i]);
1868 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 1869 / 8);
1fd400ff
TT
1870 ++i;
1871
1872 if (version == 2)
1873 {
1874 types_list = addr + MAYBE_SWAP (metadata[i]);
1875 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1876 - MAYBE_SWAP (metadata[i]))
1877 / 8);
1878 ++i;
1879 }
1880
1881 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1882 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1883 - MAYBE_SWAP (metadata[i]));
1884 ++i;
1885
1886 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1887 map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1888 - MAYBE_SWAP (metadata[i]))
9291a0cd 1889 / (2 * sizeof (offset_type)));
1fd400ff 1890 ++i;
9291a0cd 1891
1fd400ff
TT
1892 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1893
1894 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1895 return 0;
1896
1897 if (version == 2
1898 && types_list_elements
1899 && !create_signatured_type_hash_from_index (objfile, types_list,
1900 types_list_elements))
9291a0cd
TT
1901 return 0;
1902
1903 create_addrmap_from_index (objfile, map);
1904
1905 dwarf2_per_objfile->index_table = map;
1906 dwarf2_per_objfile->using_index = 1;
1907
1908 return 1;
1909}
1910
1911/* A helper for the "quick" functions which sets the global
1912 dwarf2_per_objfile according to OBJFILE. */
1913static void
1914dw2_setup (struct objfile *objfile)
1915{
1916 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1917 gdb_assert (dwarf2_per_objfile);
1918}
1919
1920/* A helper for the "quick" functions which attempts to read the line
1921 table for THIS_CU. */
1922static void
1923dw2_require_line_header (struct objfile *objfile,
1924 struct dwarf2_per_cu_data *this_cu)
1925{
1926 bfd *abfd = objfile->obfd;
1927 struct line_header *lh = NULL;
1928 struct attribute *attr;
1929 struct cleanup *cleanups;
1930 struct die_info *comp_unit_die;
1931 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1932 int has_children, i;
1933 struct dwarf2_cu cu;
1934 unsigned int bytes_read, buffer_size;
1935 struct die_reader_specs reader_specs;
1936 char *name, *comp_dir;
1937
1938 if (this_cu->v.quick->read_lines)
1939 return;
1940 this_cu->v.quick->read_lines = 1;
1941
1942 memset (&cu, 0, sizeof (cu));
1943 cu.objfile = objfile;
1944 obstack_init (&cu.comp_unit_obstack);
1945
1946 cleanups = make_cleanup (free_stack_comp_unit, &cu);
1947
1948 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
1949 buffer_size = dwarf2_per_objfile->info.size;
1950 buffer = dwarf2_per_objfile->info.buffer;
1951 info_ptr = buffer + this_cu->offset;
1952 beg_of_comp_unit = info_ptr;
1953
1954 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
1955 buffer, buffer_size,
1956 abfd);
1957
1958 /* Complete the cu_header. */
1959 cu.header.offset = beg_of_comp_unit - buffer;
1960 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
1961
1962 this_cu->cu = &cu;
1963 cu.per_cu = this_cu;
1964
1965 dwarf2_read_abbrevs (abfd, &cu);
1966 make_cleanup (dwarf2_free_abbrev_table, &cu);
1967
1968 if (this_cu->from_debug_types)
1969 info_ptr += 8 /*signature*/ + cu.header.offset_size;
1970 init_cu_die_reader (&reader_specs, &cu);
1971 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
1972 &has_children);
1973
1974 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
1975 if (attr)
1976 {
1977 unsigned int line_offset = DW_UNSND (attr);
1978 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
1979 }
1980 if (lh == NULL)
1981 {
1982 do_cleanups (cleanups);
1983 return;
1984 }
1985
1986 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
1987
1988 this_cu->v.quick->lines = lh;
1989
1990 this_cu->v.quick->file_names
1991 = obstack_alloc (&objfile->objfile_obstack,
1992 lh->num_file_names * sizeof (char *));
1993 for (i = 0; i < lh->num_file_names; ++i)
1994 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
1995
1996 do_cleanups (cleanups);
1997}
1998
1999/* A helper for the "quick" functions which computes and caches the
2000 real path for a given file name from the line table.
2001 dw2_require_line_header must have been called before this is
2002 invoked. */
2003static const char *
2004dw2_require_full_path (struct objfile *objfile,
2005 struct dwarf2_per_cu_data *cu,
2006 int index)
2007{
2008 if (!cu->v.quick->full_names)
2009 cu->v.quick->full_names
2010 = OBSTACK_CALLOC (&objfile->objfile_obstack,
2011 cu->v.quick->lines->num_file_names,
2012 sizeof (char *));
2013
2014 if (!cu->v.quick->full_names[index])
2015 cu->v.quick->full_names[index]
2016 = gdb_realpath (cu->v.quick->file_names[index]);
2017
2018 return cu->v.quick->full_names[index];
2019}
2020
2021static struct symtab *
2022dw2_find_last_source_symtab (struct objfile *objfile)
2023{
2024 int index;
2025 dw2_setup (objfile);
2026 index = dwarf2_per_objfile->n_comp_units - 1;
1fd400ff 2027 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
9291a0cd
TT
2028}
2029
2030static void
2031dw2_forget_cached_source_info (struct objfile *objfile)
2032{
2033 int i;
2034
2035 dw2_setup (objfile);
1fd400ff
TT
2036 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2037 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2038 {
1fd400ff 2039 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2040
2041 if (cu->v.quick->full_names)
2042 {
2043 int j;
2044
2045 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2046 xfree ((void *) cu->v.quick->full_names[j]);
2047 }
2048 }
2049}
2050
2051static int
2052dw2_lookup_symtab (struct objfile *objfile, const char *name,
2053 const char *full_path, const char *real_path,
2054 struct symtab **result)
2055{
2056 int i;
2057 int check_basename = lbasename (name) == name;
2058 struct dwarf2_per_cu_data *base_cu = NULL;
2059
2060 dw2_setup (objfile);
1fd400ff
TT
2061 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2062 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2063 {
2064 int j;
1fd400ff 2065 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2066
2067 if (cu->v.quick->symtab)
2068 continue;
2069
2070 dw2_require_line_header (objfile, cu);
2071 if (!cu->v.quick->lines)
2072 continue;
2073
2074 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2075 {
2076 const char *this_name = cu->v.quick->file_names[j];
2077
2078 if (FILENAME_CMP (name, this_name) == 0)
2079 {
2080 *result = dw2_instantiate_symtab (objfile, cu);
2081 return 1;
2082 }
2083
2084 if (check_basename && ! base_cu
2085 && FILENAME_CMP (lbasename (this_name), name) == 0)
2086 base_cu = cu;
2087
2088 if (full_path != NULL)
2089 {
2090 const char *this_full_name = dw2_require_full_path (objfile,
2091 cu, j);
2092
2093 if (this_full_name
2094 && FILENAME_CMP (full_path, this_full_name) == 0)
2095 {
2096 *result = dw2_instantiate_symtab (objfile, cu);
2097 return 1;
2098 }
2099 }
2100
2101 if (real_path != NULL)
2102 {
2103 const char *this_full_name = dw2_require_full_path (objfile,
2104 cu, j);
2105
2106 if (this_full_name != NULL)
2107 {
2108 char *rp = gdb_realpath (this_full_name);
2109 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2110 {
2111 xfree (rp);
2112 *result = dw2_instantiate_symtab (objfile, cu);
2113 return 1;
2114 }
2115 xfree (rp);
2116 }
2117 }
2118 }
2119 }
2120
2121 if (base_cu)
2122 {
2123 *result = dw2_instantiate_symtab (objfile, base_cu);
2124 return 1;
2125 }
2126
2127 return 0;
2128}
2129
2130static struct symtab *
2131dw2_lookup_symbol (struct objfile *objfile, int block_index,
2132 const char *name, domain_enum domain)
2133{
2134 /* We do all the work in the pre_expand_symtabs_matching hook
2135 instead. */
2136 return NULL;
2137}
2138
2139/* A helper function that expands all symtabs that hold an object
2140 named NAME. */
2141static void
2142dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2143{
2144 dw2_setup (objfile);
2145
2146 if (dwarf2_per_objfile->index_table)
2147 {
2148 offset_type *vec;
2149
2150 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2151 name, &vec))
2152 {
2153 offset_type i, len = MAYBE_SWAP (*vec);
2154 for (i = 0; i < len; ++i)
2155 {
2156 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
1fd400ff
TT
2157 struct dwarf2_per_cu_data *cu = dw2_get_cu (cu_index);
2158
9291a0cd
TT
2159 dw2_instantiate_symtab (objfile, cu);
2160 }
2161 }
2162 }
2163}
2164
2165static void
2166dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2167 int kind, const char *name,
2168 domain_enum domain)
2169{
2170 dw2_do_expand_symtabs_matching (objfile, name);
2171}
2172
2173static void
2174dw2_print_stats (struct objfile *objfile)
2175{
2176 int i, count;
2177
2178 dw2_setup (objfile);
2179 count = 0;
1fd400ff
TT
2180 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2181 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2182 {
1fd400ff 2183 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2184
2185 if (!cu->v.quick->symtab)
2186 ++count;
2187 }
2188 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2189}
2190
2191static void
2192dw2_dump (struct objfile *objfile)
2193{
2194 /* Nothing worth printing. */
2195}
2196
2197static void
2198dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2199 struct section_offsets *delta)
2200{
2201 /* There's nothing to relocate here. */
2202}
2203
2204static void
2205dw2_expand_symtabs_for_function (struct objfile *objfile,
2206 const char *func_name)
2207{
2208 dw2_do_expand_symtabs_matching (objfile, func_name);
2209}
2210
2211static void
2212dw2_expand_all_symtabs (struct objfile *objfile)
2213{
2214 int i;
2215
2216 dw2_setup (objfile);
1fd400ff
TT
2217
2218 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2219 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2220 {
1fd400ff 2221 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2222
2223 dw2_instantiate_symtab (objfile, cu);
2224 }
2225}
2226
2227static void
2228dw2_expand_symtabs_with_filename (struct objfile *objfile,
2229 const char *filename)
2230{
2231 int i;
2232
2233 dw2_setup (objfile);
1fd400ff
TT
2234 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2235 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2236 {
2237 int j;
1fd400ff 2238 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2239
2240 if (cu->v.quick->symtab)
2241 continue;
2242
2243 dw2_require_line_header (objfile, cu);
2244 if (!cu->v.quick->lines)
2245 continue;
2246
2247 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2248 {
2249 const char *this_name = cu->v.quick->file_names[j];
2250 if (strcmp (this_name, filename) == 0)
2251 {
2252 dw2_instantiate_symtab (objfile, cu);
2253 break;
2254 }
2255 }
2256 }
2257}
2258
dd786858 2259static const char *
9291a0cd
TT
2260dw2_find_symbol_file (struct objfile *objfile, const char *name)
2261{
2262 struct dwarf2_per_cu_data *cu;
2263 offset_type *vec;
2264
2265 dw2_setup (objfile);
2266
2267 if (!dwarf2_per_objfile->index_table)
2268 return NULL;
2269
2270 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2271 name, &vec))
2272 return NULL;
2273
2274 /* Note that this just looks at the very first one named NAME -- but
2275 actually we are looking for a function. find_main_filename
2276 should be rewritten so that it doesn't require a custom hook. It
2277 could just use the ordinary symbol tables. */
2278 /* vec[0] is the length, which must always be >0. */
1fd400ff 2279 cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd
TT
2280
2281 dw2_require_line_header (objfile, cu);
2282 if (!cu->v.quick->lines)
2283 return NULL;
2284
dd786858 2285 return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
9291a0cd
TT
2286}
2287
2288static void
2289dw2_map_ada_symtabs (struct objfile *objfile,
2290 int (*wild_match) (const char *, int, const char *),
2291 int (*is_name_suffix) (const char *),
2292 void (*callback) (struct objfile *,
2293 struct symtab *, void *),
2294 const char *name, int global,
2295 domain_enum namespace, int wild,
2296 void *data)
2297{
2298 /* For now, we don't support Ada, so this function can't be
2299 reached. */
2300 internal_error (__FILE__, __LINE__,
2301 _("map_ada_symtabs called via index method"));
2302}
2303
2304static void
2305dw2_expand_symtabs_matching (struct objfile *objfile,
2306 int (*file_matcher) (const char *, void *),
2307 int (*name_matcher) (const char *, void *),
2308 domain_enum kind,
2309 void *data)
2310{
2311 int i;
2312 offset_type iter;
2313
2314 dw2_setup (objfile);
2315 if (!dwarf2_per_objfile->index_table)
2316 return;
2317
1fd400ff
TT
2318 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2319 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2320 {
2321 int j;
1fd400ff 2322 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2323
2324 cu->v.quick->mark = 0;
2325 if (cu->v.quick->symtab)
2326 continue;
2327
2328 dw2_require_line_header (objfile, cu);
2329 if (!cu->v.quick->lines)
2330 continue;
2331
2332 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2333 {
2334 if (file_matcher (cu->v.quick->file_names[j], data))
2335 {
2336 cu->v.quick->mark = 1;
2337 break;
2338 }
2339 }
2340 }
2341
2342 for (iter = 0;
2343 iter < dwarf2_per_objfile->index_table->index_table_slots;
2344 ++iter)
2345 {
2346 offset_type idx = 2 * iter;
2347 const char *name;
2348 offset_type *vec, vec_len, vec_idx;
2349
2350 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2351 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2352 continue;
2353
2354 name = (dwarf2_per_objfile->index_table->constant_pool
2355 + dwarf2_per_objfile->index_table->index_table[idx]);
2356
2357 if (! (*name_matcher) (name, data))
2358 continue;
2359
2360 /* The name was matched, now expand corresponding CUs that were
2361 marked. */
2362 vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2363 + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2364 vec_len = MAYBE_SWAP (vec[0]);
2365 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2366 {
1fd400ff
TT
2367 struct dwarf2_per_cu_data *cu;
2368
2369 cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
9291a0cd
TT
2370 if (cu->v.quick->mark)
2371 dw2_instantiate_symtab (objfile, cu);
2372 }
2373 }
2374}
2375
2376static struct symtab *
2377dw2_find_pc_sect_symtab (struct objfile *objfile,
2378 struct minimal_symbol *msymbol,
2379 CORE_ADDR pc,
2380 struct obj_section *section,
2381 int warn_if_readin)
2382{
2383 struct dwarf2_per_cu_data *data;
2384
2385 dw2_setup (objfile);
2386
2387 if (!objfile->psymtabs_addrmap)
2388 return NULL;
2389
2390 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2391 if (!data)
2392 return NULL;
2393
2394 if (warn_if_readin && data->v.quick->symtab)
2395 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)\n"),
2396 paddress (get_objfile_arch (objfile), pc));
2397
2398 return dw2_instantiate_symtab (objfile, data);
2399}
2400
2401static void
2402dw2_map_symbol_names (struct objfile *objfile,
2403 void (*fun) (const char *, void *),
2404 void *data)
2405{
2406 offset_type iter;
2407 dw2_setup (objfile);
2408
2409 if (!dwarf2_per_objfile->index_table)
2410 return;
2411
2412 for (iter = 0;
2413 iter < dwarf2_per_objfile->index_table->index_table_slots;
2414 ++iter)
2415 {
2416 offset_type idx = 2 * iter;
2417 const char *name;
2418 offset_type *vec, vec_len, vec_idx;
2419
2420 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2421 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2422 continue;
2423
2424 name = (dwarf2_per_objfile->index_table->constant_pool
2425 + dwarf2_per_objfile->index_table->index_table[idx]);
2426
2427 (*fun) (name, data);
2428 }
2429}
2430
2431static void
2432dw2_map_symbol_filenames (struct objfile *objfile,
2433 void (*fun) (const char *, const char *, void *),
2434 void *data)
2435{
2436 int i;
2437
2438 dw2_setup (objfile);
1fd400ff
TT
2439 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2440 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd
TT
2441 {
2442 int j;
1fd400ff 2443 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2444
2445 if (cu->v.quick->symtab)
2446 continue;
2447
2448 dw2_require_line_header (objfile, cu);
2449 if (!cu->v.quick->lines)
2450 continue;
2451
2452 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2453 {
2454 const char *this_full_name = dw2_require_full_path (objfile, cu, j);
2455 (*fun) (cu->v.quick->file_names[j], this_full_name, data);
2456 }
2457 }
2458}
2459
2460static int
2461dw2_has_symbols (struct objfile *objfile)
2462{
2463 return 1;
2464}
2465
2466const struct quick_symbol_functions dwarf2_gdb_index_functions =
2467{
2468 dw2_has_symbols,
2469 dw2_find_last_source_symtab,
2470 dw2_forget_cached_source_info,
2471 dw2_lookup_symtab,
2472 dw2_lookup_symbol,
2473 dw2_pre_expand_symtabs_matching,
2474 dw2_print_stats,
2475 dw2_dump,
2476 dw2_relocate,
2477 dw2_expand_symtabs_for_function,
2478 dw2_expand_all_symtabs,
2479 dw2_expand_symtabs_with_filename,
2480 dw2_find_symbol_file,
2481 dw2_map_ada_symtabs,
2482 dw2_expand_symtabs_matching,
2483 dw2_find_pc_sect_symtab,
2484 dw2_map_symbol_names,
2485 dw2_map_symbol_filenames
2486};
2487
2488/* Initialize for reading DWARF for this objfile. Return 0 if this
2489 file will use psymtabs, or 1 if using the GNU index. */
2490
2491int
2492dwarf2_initialize_objfile (struct objfile *objfile)
2493{
2494 /* If we're about to read full symbols, don't bother with the
2495 indices. In this case we also don't care if some other debug
2496 format is making psymtabs, because they are all about to be
2497 expanded anyway. */
2498 if ((objfile->flags & OBJF_READNOW))
2499 {
2500 int i;
2501
2502 dwarf2_per_objfile->using_index = 1;
2503 create_all_comp_units (objfile);
1fd400ff 2504 create_debug_types_hash_table (objfile);
9291a0cd 2505
1fd400ff
TT
2506 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2507 + dwarf2_per_objfile->n_type_comp_units); ++i)
9291a0cd 2508 {
1fd400ff 2509 struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
9291a0cd
TT
2510
2511 cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2512 struct dwarf2_per_cu_quick_data);
2513 }
2514
2515 /* Return 1 so that gdb sees the "quick" functions. However,
2516 these functions will be no-ops because we will have expanded
2517 all symtabs. */
2518 return 1;
2519 }
2520
2521 if (dwarf2_read_index (objfile))
2522 return 1;
2523
2524 dwarf2_build_psymtabs (objfile);
2525 return 0;
2526}
2527
2528\f
2529
dce234bc
PP
2530/* Build a partial symbol table. */
2531
2532void
f29dff0a 2533dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 2534{
f29dff0a 2535 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
2536 {
2537 init_psymbol_list (objfile, 1024);
2538 }
2539
d146bf1e 2540 dwarf2_build_psymtabs_hard (objfile);
c906108c 2541}
c906108c 2542
45452591
DE
2543/* Return TRUE if OFFSET is within CU_HEADER. */
2544
2545static inline int
2546offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2547{
2548 unsigned int bottom = cu_header->offset;
2549 unsigned int top = (cu_header->offset
2550 + cu_header->length
2551 + cu_header->initial_length_size);
9a619af0 2552
45452591
DE
2553 return (offset >= bottom && offset < top);
2554}
2555
93311388
DE
2556/* Read in the comp unit header information from the debug_info at info_ptr.
2557 NOTE: This leaves members offset, first_die_offset to be filled in
2558 by the caller. */
107d2387 2559
fe1b8b76 2560static gdb_byte *
107d2387 2561read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 2562 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
2563{
2564 int signed_addr;
891d2f0b 2565 unsigned int bytes_read;
c764a876
DE
2566
2567 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2568 cu_header->initial_length_size = bytes_read;
2569 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 2570 info_ptr += bytes_read;
107d2387
AC
2571 cu_header->version = read_2_bytes (abfd, info_ptr);
2572 info_ptr += 2;
613e1657 2573 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 2574 &bytes_read);
613e1657 2575 info_ptr += bytes_read;
107d2387
AC
2576 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2577 info_ptr += 1;
2578 signed_addr = bfd_get_sign_extend_vma (abfd);
2579 if (signed_addr < 0)
8e65ff28 2580 internal_error (__FILE__, __LINE__,
e2e0b3e5 2581 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 2582 cu_header->signed_addr_p = signed_addr;
c764a876 2583
107d2387
AC
2584 return info_ptr;
2585}
2586
fe1b8b76
JB
2587static gdb_byte *
2588partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
93311388 2589 gdb_byte *buffer, unsigned int buffer_size,
72bf9492
DJ
2590 bfd *abfd)
2591{
fe1b8b76 2592 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
2593
2594 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2595
2dc7f7b3 2596 if (header->version != 2 && header->version != 3 && header->version != 4)
8a3fe4f8 2597 error (_("Dwarf Error: wrong version in compilation unit header "
2dc7f7b3
TT
2598 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2599 bfd_get_filename (abfd));
72bf9492 2600
dce234bc 2601 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
2602 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2603 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 2604 (long) header->abbrev_offset,
93311388 2605 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2606 bfd_get_filename (abfd));
2607
2608 if (beg_of_comp_unit + header->length + header->initial_length_size
93311388 2609 > buffer + buffer_size)
8a3fe4f8
AC
2610 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2611 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 2612 (long) header->length,
93311388 2613 (long) (beg_of_comp_unit - buffer),
72bf9492
DJ
2614 bfd_get_filename (abfd));
2615
2616 return info_ptr;
2617}
2618
348e048f
DE
2619/* Read in the types comp unit header information from .debug_types entry at
2620 types_ptr. The result is a pointer to one past the end of the header. */
2621
2622static gdb_byte *
2623read_type_comp_unit_head (struct comp_unit_head *cu_header,
2624 ULONGEST *signature,
2625 gdb_byte *types_ptr, bfd *abfd)
2626{
348e048f
DE
2627 gdb_byte *initial_types_ptr = types_ptr;
2628
6e70227d 2629 dwarf2_read_section (dwarf2_per_objfile->objfile,
fa238c03 2630 &dwarf2_per_objfile->types);
348e048f
DE
2631 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2632
2633 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2634
2635 *signature = read_8_bytes (abfd, types_ptr);
2636 types_ptr += 8;
2637 types_ptr += cu_header->offset_size;
2638 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2639
2640 return types_ptr;
2641}
2642
aaa75496
JB
2643/* Allocate a new partial symtab for file named NAME and mark this new
2644 partial symtab as being an include of PST. */
2645
2646static void
2647dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2648 struct objfile *objfile)
2649{
2650 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2651
2652 subpst->section_offsets = pst->section_offsets;
2653 subpst->textlow = 0;
2654 subpst->texthigh = 0;
2655
2656 subpst->dependencies = (struct partial_symtab **)
2657 obstack_alloc (&objfile->objfile_obstack,
2658 sizeof (struct partial_symtab *));
2659 subpst->dependencies[0] = pst;
2660 subpst->number_of_dependencies = 1;
2661
2662 subpst->globals_offset = 0;
2663 subpst->n_global_syms = 0;
2664 subpst->statics_offset = 0;
2665 subpst->n_static_syms = 0;
2666 subpst->symtab = NULL;
2667 subpst->read_symtab = pst->read_symtab;
2668 subpst->readin = 0;
2669
2670 /* No private part is necessary for include psymtabs. This property
2671 can be used to differentiate between such include psymtabs and
10b3939b 2672 the regular ones. */
58a9656e 2673 subpst->read_symtab_private = NULL;
aaa75496
JB
2674}
2675
2676/* Read the Line Number Program data and extract the list of files
2677 included by the source file represented by PST. Build an include
d85a05f0 2678 partial symtab for each of these included files. */
aaa75496
JB
2679
2680static void
2681dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
d85a05f0 2682 struct die_info *die,
aaa75496
JB
2683 struct partial_symtab *pst)
2684{
2685 struct objfile *objfile = cu->objfile;
2686 bfd *abfd = objfile->obfd;
d85a05f0
DJ
2687 struct line_header *lh = NULL;
2688 struct attribute *attr;
aaa75496 2689
d85a05f0
DJ
2690 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2691 if (attr)
2692 {
2693 unsigned int line_offset = DW_UNSND (attr);
9a619af0 2694
d85a05f0
DJ
2695 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2696 }
aaa75496
JB
2697 if (lh == NULL)
2698 return; /* No linetable, so no includes. */
2699
2700 dwarf_decode_lines (lh, NULL, abfd, cu, pst);
2701
2702 free_line_header (lh);
2703}
2704
348e048f
DE
2705static hashval_t
2706hash_type_signature (const void *item)
2707{
2708 const struct signatured_type *type_sig = item;
9a619af0 2709
348e048f
DE
2710 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2711 return type_sig->signature;
2712}
2713
2714static int
2715eq_type_signature (const void *item_lhs, const void *item_rhs)
2716{
2717 const struct signatured_type *lhs = item_lhs;
2718 const struct signatured_type *rhs = item_rhs;
9a619af0 2719
348e048f
DE
2720 return lhs->signature == rhs->signature;
2721}
2722
1fd400ff
TT
2723/* Allocate a hash table for signatured types. */
2724
2725static htab_t
2726allocate_signatured_type_hash_table (struct objfile *objfile)
2727{
2728 return htab_create_alloc_ex (41,
2729 hash_type_signature,
2730 eq_type_signature,
2731 NULL,
2732 &objfile->objfile_obstack,
2733 hashtab_obstack_allocate,
2734 dummy_obstack_deallocate);
2735}
2736
2737/* A helper function to add a signatured type CU to a list. */
2738
2739static int
2740add_signatured_type_cu_to_list (void **slot, void *datum)
2741{
2742 struct signatured_type *sigt = *slot;
2743 struct dwarf2_per_cu_data ***datap = datum;
2744
2745 **datap = &sigt->per_cu;
2746 ++*datap;
2747
2748 return 1;
2749}
2750
348e048f
DE
2751/* Create the hash table of all entries in the .debug_types section.
2752 The result is zero if there is an error (e.g. missing .debug_types section),
2753 otherwise non-zero. */
2754
2755static int
2756create_debug_types_hash_table (struct objfile *objfile)
2757{
be391dca 2758 gdb_byte *info_ptr;
348e048f 2759 htab_t types_htab;
1fd400ff 2760 struct dwarf2_per_cu_data **iter;
348e048f 2761
be391dca
TT
2762 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2763 info_ptr = dwarf2_per_objfile->types.buffer;
2764
348e048f
DE
2765 if (info_ptr == NULL)
2766 {
2767 dwarf2_per_objfile->signatured_types = NULL;
2768 return 0;
2769 }
2770
1fd400ff 2771 types_htab = allocate_signatured_type_hash_table (objfile);
348e048f
DE
2772
2773 if (dwarf2_die_debug)
2774 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2775
2776 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2777 {
2778 unsigned int offset;
2779 unsigned int offset_size;
2780 unsigned int type_offset;
2781 unsigned int length, initial_length_size;
2782 unsigned short version;
2783 ULONGEST signature;
2784 struct signatured_type *type_sig;
2785 void **slot;
2786 gdb_byte *ptr = info_ptr;
2787
2788 offset = ptr - dwarf2_per_objfile->types.buffer;
2789
2790 /* We need to read the type's signature in order to build the hash
2791 table, but we don't need to read anything else just yet. */
2792
2793 /* Sanity check to ensure entire cu is present. */
2794 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2795 if (ptr + length + initial_length_size
2796 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2797 {
2798 complaint (&symfile_complaints,
2799 _("debug type entry runs off end of `.debug_types' section, ignored"));
2800 break;
2801 }
2802
2803 offset_size = initial_length_size == 4 ? 4 : 8;
2804 ptr += initial_length_size;
2805 version = bfd_get_16 (objfile->obfd, ptr);
2806 ptr += 2;
2807 ptr += offset_size; /* abbrev offset */
2808 ptr += 1; /* address size */
2809 signature = bfd_get_64 (objfile->obfd, ptr);
2810 ptr += 8;
2811 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2812
2813 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2814 memset (type_sig, 0, sizeof (*type_sig));
2815 type_sig->signature = signature;
2816 type_sig->offset = offset;
2817 type_sig->type_offset = type_offset;
ca1f3406 2818 type_sig->per_cu.objfile = objfile;
1fd400ff 2819 type_sig->per_cu.from_debug_types = 1;
348e048f
DE
2820
2821 slot = htab_find_slot (types_htab, type_sig, INSERT);
2822 gdb_assert (slot != NULL);
2823 *slot = type_sig;
2824
2825 if (dwarf2_die_debug)
2826 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2827 offset, phex (signature, sizeof (signature)));
2828
2829 info_ptr = info_ptr + initial_length_size + length;
2830 }
2831
2832 dwarf2_per_objfile->signatured_types = types_htab;
2833
1fd400ff
TT
2834 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2835 dwarf2_per_objfile->type_comp_units
2836 = obstack_alloc (&objfile->objfile_obstack,
2837 dwarf2_per_objfile->n_type_comp_units
2838 * sizeof (struct dwarf2_per_cu_data *));
2839 iter = &dwarf2_per_objfile->type_comp_units[0];
2840 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2841 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2842 == dwarf2_per_objfile->n_type_comp_units);
2843
348e048f
DE
2844 return 1;
2845}
2846
2847/* Lookup a signature based type.
2848 Returns NULL if SIG is not present in the table. */
2849
2850static struct signatured_type *
2851lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2852{
2853 struct signatured_type find_entry, *entry;
2854
2855 if (dwarf2_per_objfile->signatured_types == NULL)
2856 {
2857 complaint (&symfile_complaints,
2858 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2859 return 0;
2860 }
2861
2862 find_entry.signature = sig;
2863 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2864 return entry;
2865}
2866
d85a05f0
DJ
2867/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2868
2869static void
2870init_cu_die_reader (struct die_reader_specs *reader,
2871 struct dwarf2_cu *cu)
2872{
2873 reader->abfd = cu->objfile->obfd;
2874 reader->cu = cu;
2875 if (cu->per_cu->from_debug_types)
be391dca
TT
2876 {
2877 gdb_assert (dwarf2_per_objfile->types.readin);
2878 reader->buffer = dwarf2_per_objfile->types.buffer;
2879 }
d85a05f0 2880 else
be391dca
TT
2881 {
2882 gdb_assert (dwarf2_per_objfile->info.readin);
2883 reader->buffer = dwarf2_per_objfile->info.buffer;
2884 }
d85a05f0
DJ
2885}
2886
2887/* Find the base address of the compilation unit for range lists and
2888 location lists. It will normally be specified by DW_AT_low_pc.
2889 In DWARF-3 draft 4, the base address could be overridden by
2890 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2891 compilation units with discontinuous ranges. */
2892
2893static void
2894dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2895{
2896 struct attribute *attr;
2897
2898 cu->base_known = 0;
2899 cu->base_address = 0;
2900
2901 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2902 if (attr)
2903 {
2904 cu->base_address = DW_ADDR (attr);
2905 cu->base_known = 1;
2906 }
2907 else
2908 {
2909 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2910 if (attr)
2911 {
2912 cu->base_address = DW_ADDR (attr);
2913 cu->base_known = 1;
2914 }
2915 }
2916}
2917
348e048f
DE
2918/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2919 to combine the common parts.
93311388 2920 Process a compilation unit for a psymtab.
348e048f
DE
2921 BUFFER is a pointer to the beginning of the dwarf section buffer,
2922 either .debug_info or debug_types.
93311388
DE
2923 INFO_PTR is a pointer to the start of the CU.
2924 Returns a pointer to the next CU. */
aaa75496 2925
93311388
DE
2926static gdb_byte *
2927process_psymtab_comp_unit (struct objfile *objfile,
2928 struct dwarf2_per_cu_data *this_cu,
2929 gdb_byte *buffer, gdb_byte *info_ptr,
2930 unsigned int buffer_size)
c906108c 2931{
c906108c 2932 bfd *abfd = objfile->obfd;
93311388 2933 gdb_byte *beg_of_comp_unit = info_ptr;
d85a05f0 2934 struct die_info *comp_unit_die;
c906108c 2935 struct partial_symtab *pst;
5734ee8b 2936 CORE_ADDR baseaddr;
93311388
DE
2937 struct cleanup *back_to_inner;
2938 struct dwarf2_cu cu;
d85a05f0
DJ
2939 int has_children, has_pc_info;
2940 struct attribute *attr;
d85a05f0
DJ
2941 CORE_ADDR best_lowpc = 0, best_highpc = 0;
2942 struct die_reader_specs reader_specs;
c906108c 2943
93311388
DE
2944 memset (&cu, 0, sizeof (cu));
2945 cu.objfile = objfile;
2946 obstack_init (&cu.comp_unit_obstack);
c906108c 2947
93311388 2948 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
ae038cb0 2949
93311388
DE
2950 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2951 buffer, buffer_size,
2952 abfd);
10b3939b 2953
93311388
DE
2954 /* Complete the cu_header. */
2955 cu.header.offset = beg_of_comp_unit - buffer;
2956 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
ff013f42 2957
93311388 2958 cu.list_in_scope = &file_symbols;
af703f96 2959
328c9494
DJ
2960 /* If this compilation unit was already read in, free the
2961 cached copy in order to read it in again. This is
2962 necessary because we skipped some symbols when we first
2963 read in the compilation unit (see load_partial_dies).
2964 This problem could be avoided, but the benefit is
2965 unclear. */
2966 if (this_cu->cu != NULL)
2967 free_one_cached_comp_unit (this_cu->cu);
2968
2969 /* Note that this is a pointer to our stack frame, being
2970 added to a global data structure. It will be cleaned up
2971 in free_stack_comp_unit when we finish with this
2972 compilation unit. */
2973 this_cu->cu = &cu;
d85a05f0
DJ
2974 cu.per_cu = this_cu;
2975
93311388
DE
2976 /* Read the abbrevs for this compilation unit into a table. */
2977 dwarf2_read_abbrevs (abfd, &cu);
2978 make_cleanup (dwarf2_free_abbrev_table, &cu);
af703f96 2979
93311388 2980 /* Read the compilation unit die. */
348e048f
DE
2981 if (this_cu->from_debug_types)
2982 info_ptr += 8 /*signature*/ + cu.header.offset_size;
d85a05f0
DJ
2983 init_cu_die_reader (&reader_specs, &cu);
2984 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2985 &has_children);
93311388 2986
348e048f
DE
2987 if (this_cu->from_debug_types)
2988 {
2989 /* offset,length haven't been set yet for type units. */
2990 this_cu->offset = cu.header.offset;
2991 this_cu->length = cu.header.length + cu.header.initial_length_size;
2992 }
d85a05f0 2993 else if (comp_unit_die->tag == DW_TAG_partial_unit)
c906108c 2994 {
93311388
DE
2995 info_ptr = (beg_of_comp_unit + cu.header.length
2996 + cu.header.initial_length_size);
2997 do_cleanups (back_to_inner);
2998 return info_ptr;
2999 }
72bf9492 3000
93311388 3001 /* Set the language we're debugging. */
d85a05f0
DJ
3002 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3003 if (attr)
3004 set_cu_language (DW_UNSND (attr), &cu);
3005 else
3006 set_cu_language (language_minimal, &cu);
c906108c 3007
93311388 3008 /* Allocate a new partial symbol table structure. */
d85a05f0 3009 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
93311388 3010 pst = start_psymtab_common (objfile, objfile->section_offsets,
d85a05f0 3011 (attr != NULL) ? DW_STRING (attr) : "",
93311388
DE
3012 /* TEXTLOW and TEXTHIGH are set below. */
3013 0,
3014 objfile->global_psymbols.next,
3015 objfile->static_psymbols.next);
72bf9492 3016
d85a05f0
DJ
3017 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3018 if (attr != NULL)
3019 pst->dirname = DW_STRING (attr);
72bf9492 3020
e38df1d0 3021 pst->read_symtab_private = this_cu;
72bf9492 3022
93311388 3023 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 3024
93311388
DE
3025 /* Store the function that reads in the rest of the symbol table */
3026 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 3027
9291a0cd 3028 this_cu->v.psymtab = pst;
c906108c 3029
d85a05f0
DJ
3030 dwarf2_find_base_address (comp_unit_die, &cu);
3031
93311388
DE
3032 /* Possibly set the default values of LOWPC and HIGHPC from
3033 `DW_AT_ranges'. */
d85a05f0
DJ
3034 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3035 &best_highpc, &cu, pst);
3036 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
3037 /* Store the contiguous range if it is not empty; it can be empty for
3038 CUs with no code. */
3039 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
3040 best_lowpc + baseaddr,
3041 best_highpc + baseaddr - 1, pst);
93311388
DE
3042
3043 /* Check if comp unit has_children.
3044 If so, read the rest of the partial symbols from this comp unit.
3045 If not, there's no more debug_info for this comp unit. */
d85a05f0 3046 if (has_children)
93311388
DE
3047 {
3048 struct partial_die_info *first_die;
3049 CORE_ADDR lowpc, highpc;
31ffec48 3050
93311388
DE
3051 lowpc = ((CORE_ADDR) -1);
3052 highpc = ((CORE_ADDR) 0);
c906108c 3053
93311388 3054 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
c906108c 3055
93311388 3056 scan_partial_symbols (first_die, &lowpc, &highpc,
d85a05f0 3057 ! has_pc_info, &cu);
57c22c6c 3058
93311388
DE
3059 /* If we didn't find a lowpc, set it to highpc to avoid
3060 complaints from `maint check'. */
3061 if (lowpc == ((CORE_ADDR) -1))
3062 lowpc = highpc;
10b3939b 3063
93311388
DE
3064 /* If the compilation unit didn't have an explicit address range,
3065 then use the information extracted from its child dies. */
d85a05f0 3066 if (! has_pc_info)
93311388 3067 {
d85a05f0
DJ
3068 best_lowpc = lowpc;
3069 best_highpc = highpc;
93311388
DE
3070 }
3071 }
d85a05f0
DJ
3072 pst->textlow = best_lowpc + baseaddr;
3073 pst->texthigh = best_highpc + baseaddr;
c906108c 3074
93311388
DE
3075 pst->n_global_syms = objfile->global_psymbols.next -
3076 (objfile->global_psymbols.list + pst->globals_offset);
3077 pst->n_static_syms = objfile->static_psymbols.next -
3078 (objfile->static_psymbols.list + pst->statics_offset);
3079 sort_pst_symbols (pst);
c906108c 3080
93311388
DE
3081 info_ptr = (beg_of_comp_unit + cu.header.length
3082 + cu.header.initial_length_size);
ae038cb0 3083
348e048f
DE
3084 if (this_cu->from_debug_types)
3085 {
3086 /* It's not clear we want to do anything with stmt lists here.
3087 Waiting to see what gcc ultimately does. */
3088 }
d85a05f0 3089 else
93311388
DE
3090 {
3091 /* Get the list of files included in the current compilation unit,
3092 and build a psymtab for each of them. */
d85a05f0 3093 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
93311388 3094 }
ae038cb0 3095
93311388 3096 do_cleanups (back_to_inner);
ae038cb0 3097
93311388
DE
3098 return info_ptr;
3099}
ff013f42 3100
348e048f
DE
3101/* Traversal function for htab_traverse_noresize.
3102 Process one .debug_types comp-unit. */
3103
3104static int
3105process_type_comp_unit (void **slot, void *info)
3106{
3107 struct signatured_type *entry = (struct signatured_type *) *slot;
3108 struct objfile *objfile = (struct objfile *) info;
3109 struct dwarf2_per_cu_data *this_cu;
3110
3111 this_cu = &entry->per_cu;
348e048f 3112
be391dca 3113 gdb_assert (dwarf2_per_objfile->types.readin);
348e048f
DE
3114 process_psymtab_comp_unit (objfile, this_cu,
3115 dwarf2_per_objfile->types.buffer,
3116 dwarf2_per_objfile->types.buffer + entry->offset,
3117 dwarf2_per_objfile->types.size);
3118
3119 return 1;
3120}
3121
3122/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3123 Build partial symbol tables for the .debug_types comp-units. */
3124
3125static void
3126build_type_psymtabs (struct objfile *objfile)
3127{
3128 if (! create_debug_types_hash_table (objfile))
3129 return;
3130
3131 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3132 process_type_comp_unit, objfile);
3133}
3134
60606b2c
TT
3135/* A cleanup function that clears objfile's psymtabs_addrmap field. */
3136
3137static void
3138psymtabs_addrmap_cleanup (void *o)
3139{
3140 struct objfile *objfile = o;
ec61707d 3141
60606b2c
TT
3142 objfile->psymtabs_addrmap = NULL;
3143}
3144
93311388
DE
3145/* Build the partial symbol table by doing a quick pass through the
3146 .debug_info and .debug_abbrev sections. */
72bf9492 3147
93311388 3148static void
c67a9c90 3149dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 3150{
93311388 3151 gdb_byte *info_ptr;
60606b2c
TT
3152 struct cleanup *back_to, *addrmap_cleanup;
3153 struct obstack temp_obstack;
93311388 3154
be391dca 3155 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
93311388 3156 info_ptr = dwarf2_per_objfile->info.buffer;
91c24f0a 3157
93311388
DE
3158 /* Any cached compilation units will be linked by the per-objfile
3159 read_in_chain. Make sure to free them when we're done. */
3160 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 3161
348e048f
DE
3162 build_type_psymtabs (objfile);
3163
93311388 3164 create_all_comp_units (objfile);
c906108c 3165
60606b2c
TT
3166 /* Create a temporary address map on a temporary obstack. We later
3167 copy this to the final obstack. */
3168 obstack_init (&temp_obstack);
3169 make_cleanup_obstack_free (&temp_obstack);
3170 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3171 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 3172
93311388
DE
3173 /* Since the objects we're extracting from .debug_info vary in
3174 length, only the individual functions to extract them (like
3175 read_comp_unit_head and load_partial_die) can really know whether
3176 the buffer is large enough to hold another complete object.
c906108c 3177
93311388
DE
3178 At the moment, they don't actually check that. If .debug_info
3179 holds just one extra byte after the last compilation unit's dies,
3180 then read_comp_unit_head will happily read off the end of the
3181 buffer. read_partial_die is similarly casual. Those functions
3182 should be fixed.
c906108c 3183
93311388
DE
3184 For this loop condition, simply checking whether there's any data
3185 left at all should be sufficient. */
c906108c 3186
93311388
DE
3187 while (info_ptr < (dwarf2_per_objfile->info.buffer
3188 + dwarf2_per_objfile->info.size))
3189 {
3190 struct dwarf2_per_cu_data *this_cu;
dd373385 3191
93311388
DE
3192 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3193 objfile);
aaa75496 3194
93311388
DE
3195 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3196 dwarf2_per_objfile->info.buffer,
3197 info_ptr,
3198 dwarf2_per_objfile->info.size);
c906108c 3199 }
ff013f42
JK
3200
3201 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3202 &objfile->objfile_obstack);
60606b2c 3203 discard_cleanups (addrmap_cleanup);
ff013f42 3204
ae038cb0
DJ
3205 do_cleanups (back_to);
3206}
3207
93311388 3208/* Load the partial DIEs for a secondary CU into memory. */
ae038cb0
DJ
3209
3210static void
93311388
DE
3211load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3212 struct objfile *objfile)
ae038cb0
DJ
3213{
3214 bfd *abfd = objfile->obfd;
fe1b8b76 3215 gdb_byte *info_ptr, *beg_of_comp_unit;
d85a05f0 3216 struct die_info *comp_unit_die;
ae038cb0 3217 struct dwarf2_cu *cu;
ae038cb0 3218 struct cleanup *back_to;
d85a05f0
DJ
3219 struct attribute *attr;
3220 int has_children;
3221 struct die_reader_specs reader_specs;
ae038cb0 3222
348e048f
DE
3223 gdb_assert (! this_cu->from_debug_types);
3224
be391dca 3225 gdb_assert (dwarf2_per_objfile->info.readin);
dce234bc 3226 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
3227 beg_of_comp_unit = info_ptr;
3228
93311388 3229 cu = alloc_one_comp_unit (objfile);
ae038cb0 3230
93311388 3231 /* ??? Missing cleanup for CU? */
ae038cb0 3232
328c9494
DJ
3233 /* Link this compilation unit into the compilation unit tree. */
3234 this_cu->cu = cu;
3235 cu->per_cu = this_cu;
3236 cu->type_hash = this_cu->type_hash;
3237
93311388
DE
3238 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3239 dwarf2_per_objfile->info.buffer,
3240 dwarf2_per_objfile->info.size,
3241 abfd);
ae038cb0
DJ
3242
3243 /* Complete the cu_header. */
93311388 3244 cu->header.offset = this_cu->offset;
d00adf39 3245 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
ae038cb0
DJ
3246
3247 /* Read the abbrevs for this compilation unit into a table. */
3248 dwarf2_read_abbrevs (abfd, cu);
3249 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
3250
3251 /* Read the compilation unit die. */
d85a05f0
DJ
3252 init_cu_die_reader (&reader_specs, cu);
3253 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3254 &has_children);
ae038cb0
DJ
3255
3256 /* Set the language we're debugging. */
d85a05f0
DJ
3257 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3258 if (attr)
3259 set_cu_language (DW_UNSND (attr), cu);
3260 else
3261 set_cu_language (language_minimal, cu);
ae038cb0 3262
ae038cb0
DJ
3263 /* Check if comp unit has_children.
3264 If so, read the rest of the partial symbols from this comp unit.
3265 If not, there's no more debug_info for this comp unit. */
d85a05f0 3266 if (has_children)
93311388 3267 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
ae038cb0
DJ
3268
3269 do_cleanups (back_to);
3270}
3271
3272/* Create a list of all compilation units in OBJFILE. We do this only
3273 if an inter-comp-unit reference is found; presumably if there is one,
3274 there will be many, and one will occur early in the .debug_info section.
3275 So there's no point in building this list incrementally. */
3276
3277static void
3278create_all_comp_units (struct objfile *objfile)
3279{
3280 int n_allocated;
3281 int n_comp_units;
3282 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
3283 gdb_byte *info_ptr;
3284
3285 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3286 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3287
3288 n_comp_units = 0;
3289 n_allocated = 10;
3290 all_comp_units = xmalloc (n_allocated
3291 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 3292
dce234bc 3293 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 3294 {
c764a876 3295 unsigned int length, initial_length_size;
ae038cb0 3296 struct dwarf2_per_cu_data *this_cu;
c764a876 3297 unsigned int offset;
ae038cb0 3298
dce234bc 3299 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
3300
3301 /* Read just enough information to find out where the next
3302 compilation unit is. */
c764a876
DE
3303 length = read_initial_length (objfile->obfd, info_ptr,
3304 &initial_length_size);
ae038cb0
DJ
3305
3306 /* Save the compilation unit for later lookup. */
3307 this_cu = obstack_alloc (&objfile->objfile_obstack,
3308 sizeof (struct dwarf2_per_cu_data));
3309 memset (this_cu, 0, sizeof (*this_cu));
3310 this_cu->offset = offset;
c764a876 3311 this_cu->length = length + initial_length_size;
9291a0cd 3312 this_cu->objfile = objfile;
ae038cb0
DJ
3313
3314 if (n_comp_units == n_allocated)
3315 {
3316 n_allocated *= 2;
3317 all_comp_units = xrealloc (all_comp_units,
3318 n_allocated
3319 * sizeof (struct dwarf2_per_cu_data *));
3320 }
3321 all_comp_units[n_comp_units++] = this_cu;
3322
3323 info_ptr = info_ptr + this_cu->length;
3324 }
3325
3326 dwarf2_per_objfile->all_comp_units
3327 = obstack_alloc (&objfile->objfile_obstack,
3328 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3329 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3330 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3331 xfree (all_comp_units);
3332 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
3333}
3334
5734ee8b
DJ
3335/* Process all loaded DIEs for compilation unit CU, starting at
3336 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3337 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3338 DW_AT_ranges). If NEED_PC is set, then this function will set
3339 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3340 and record the covered ranges in the addrmap. */
c906108c 3341
72bf9492
DJ
3342static void
3343scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 3344 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 3345{
72bf9492 3346 struct partial_die_info *pdi;
c906108c 3347
91c24f0a
DC
3348 /* Now, march along the PDI's, descending into ones which have
3349 interesting children but skipping the children of the other ones,
3350 until we reach the end of the compilation unit. */
c906108c 3351
72bf9492 3352 pdi = first_die;
91c24f0a 3353
72bf9492
DJ
3354 while (pdi != NULL)
3355 {
3356 fixup_partial_die (pdi, cu);
c906108c 3357
f55ee35c 3358 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
3359 children, so we need to look at them. Ditto for anonymous
3360 enums. */
933c6fe4 3361
72bf9492 3362 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 3363 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 3364 {
72bf9492 3365 switch (pdi->tag)
c906108c
SS
3366 {
3367 case DW_TAG_subprogram:
5734ee8b 3368 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c
SS
3369 break;
3370 case DW_TAG_variable:
3371 case DW_TAG_typedef:
91c24f0a 3372 case DW_TAG_union_type:
72bf9492 3373 if (!pdi->is_declaration)
63d06c5c 3374 {
72bf9492 3375 add_partial_symbol (pdi, cu);
63d06c5c
DC
3376 }
3377 break;
c906108c 3378 case DW_TAG_class_type:
680b30c7 3379 case DW_TAG_interface_type:
c906108c 3380 case DW_TAG_structure_type:
72bf9492 3381 if (!pdi->is_declaration)
c906108c 3382 {
72bf9492 3383 add_partial_symbol (pdi, cu);
c906108c
SS
3384 }
3385 break;
91c24f0a 3386 case DW_TAG_enumeration_type:
72bf9492
DJ
3387 if (!pdi->is_declaration)
3388 add_partial_enumeration (pdi, cu);
c906108c
SS
3389 break;
3390 case DW_TAG_base_type:
a02abb62 3391 case DW_TAG_subrange_type:
c906108c 3392 /* File scope base type definitions are added to the partial
c5aa993b 3393 symbol table. */
72bf9492 3394 add_partial_symbol (pdi, cu);
c906108c 3395 break;
d9fa45fe 3396 case DW_TAG_namespace:
5734ee8b 3397 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 3398 break;
5d7cb8df
JK
3399 case DW_TAG_module:
3400 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3401 break;
c906108c
SS
3402 default:
3403 break;
3404 }
3405 }
3406
72bf9492
DJ
3407 /* If the die has a sibling, skip to the sibling. */
3408
3409 pdi = pdi->die_sibling;
3410 }
3411}
3412
3413/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 3414
72bf9492 3415 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
3416 name is concatenated with "::" and the partial DIE's name. For
3417 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
3418 Enumerators are an exception; they use the scope of their parent
3419 enumeration type, i.e. the name of the enumeration type is not
3420 prepended to the enumerator.
91c24f0a 3421
72bf9492
DJ
3422 There are two complexities. One is DW_AT_specification; in this
3423 case "parent" means the parent of the target of the specification,
3424 instead of the direct parent of the DIE. The other is compilers
3425 which do not emit DW_TAG_namespace; in this case we try to guess
3426 the fully qualified name of structure types from their members'
3427 linkage names. This must be done using the DIE's children rather
3428 than the children of any DW_AT_specification target. We only need
3429 to do this for structures at the top level, i.e. if the target of
3430 any DW_AT_specification (if any; otherwise the DIE itself) does not
3431 have a parent. */
3432
3433/* Compute the scope prefix associated with PDI's parent, in
3434 compilation unit CU. The result will be allocated on CU's
3435 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3436 field. NULL is returned if no prefix is necessary. */
3437static char *
3438partial_die_parent_scope (struct partial_die_info *pdi,
3439 struct dwarf2_cu *cu)
3440{
3441 char *grandparent_scope;
3442 struct partial_die_info *parent, *real_pdi;
91c24f0a 3443
72bf9492
DJ
3444 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3445 then this means the parent of the specification DIE. */
3446
3447 real_pdi = pdi;
72bf9492 3448 while (real_pdi->has_specification)
10b3939b 3449 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
3450
3451 parent = real_pdi->die_parent;
3452 if (parent == NULL)
3453 return NULL;
3454
3455 if (parent->scope_set)
3456 return parent->scope;
3457
3458 fixup_partial_die (parent, cu);
3459
10b3939b 3460 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 3461
acebe513
UW
3462 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3463 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3464 Work around this problem here. */
3465 if (cu->language == language_cplus
6e70227d 3466 && parent->tag == DW_TAG_namespace
acebe513
UW
3467 && strcmp (parent->name, "::") == 0
3468 && grandparent_scope == NULL)
3469 {
3470 parent->scope = NULL;
3471 parent->scope_set = 1;
3472 return NULL;
3473 }
3474
72bf9492 3475 if (parent->tag == DW_TAG_namespace
f55ee35c 3476 || parent->tag == DW_TAG_module
72bf9492
DJ
3477 || parent->tag == DW_TAG_structure_type
3478 || parent->tag == DW_TAG_class_type
680b30c7 3479 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
3480 || parent->tag == DW_TAG_union_type
3481 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
3482 {
3483 if (grandparent_scope == NULL)
3484 parent->scope = parent->name;
3485 else
987504bb 3486 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
f55ee35c 3487 parent->name, 0, cu);
72bf9492 3488 }
ceeb3d5a 3489 else if (parent->tag == DW_TAG_enumerator)
72bf9492
DJ
3490 /* Enumerators should not get the name of the enumeration as a prefix. */
3491 parent->scope = grandparent_scope;
3492 else
3493 {
3494 /* FIXME drow/2004-04-01: What should we be doing with
3495 function-local names? For partial symbols, we should probably be
3496 ignoring them. */
3497 complaint (&symfile_complaints,
e2e0b3e5 3498 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
3499 parent->tag, pdi->offset);
3500 parent->scope = grandparent_scope;
c906108c
SS
3501 }
3502
72bf9492
DJ
3503 parent->scope_set = 1;
3504 return parent->scope;
3505}
3506
3507/* Return the fully scoped name associated with PDI, from compilation unit
3508 CU. The result will be allocated with malloc. */
3509static char *
3510partial_die_full_name (struct partial_die_info *pdi,
3511 struct dwarf2_cu *cu)
3512{
3513 char *parent_scope;
3514
3515 parent_scope = partial_die_parent_scope (pdi, cu);
3516 if (parent_scope == NULL)
3517 return NULL;
3518 else
f55ee35c 3519 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
3520}
3521
3522static void
72bf9492 3523add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 3524{
e7c27a73 3525 struct objfile *objfile = cu->objfile;
c906108c 3526 CORE_ADDR addr = 0;
decbce07 3527 char *actual_name = NULL;
5c4e30ca 3528 const struct partial_symbol *psym = NULL;
e142c38c 3529 CORE_ADDR baseaddr;
72bf9492 3530 int built_actual_name = 0;
e142c38c
DJ
3531
3532 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3533
94af9270
KS
3534 actual_name = partial_die_full_name (pdi, cu);
3535 if (actual_name)
3536 built_actual_name = 1;
63d06c5c 3537
72bf9492
DJ
3538 if (actual_name == NULL)
3539 actual_name = pdi->name;
3540
c906108c
SS
3541 switch (pdi->tag)
3542 {
3543 case DW_TAG_subprogram:
2cfa0c8d 3544 if (pdi->is_external || cu->language == language_ada)
c906108c 3545 {
2cfa0c8d
JB
3546 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3547 of the global scope. But in Ada, we want to be able to access
3548 nested procedures globally. So all Ada subprograms are stored
3549 in the global scope. */
38d518c9 3550 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3551 mst_text, objfile); */
38d518c9 3552 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3553 built_actual_name,
5c4e30ca
DC
3554 VAR_DOMAIN, LOC_BLOCK,
3555 &objfile->global_psymbols,
3556 0, pdi->lowpc + baseaddr,
e142c38c 3557 cu->language, objfile);
c906108c
SS
3558 }
3559 else
3560 {
38d518c9 3561 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 3562 mst_file_text, objfile); */
38d518c9 3563 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3564 built_actual_name,
5c4e30ca
DC
3565 VAR_DOMAIN, LOC_BLOCK,
3566 &objfile->static_psymbols,
3567 0, pdi->lowpc + baseaddr,
e142c38c 3568 cu->language, objfile);
c906108c
SS
3569 }
3570 break;
3571 case DW_TAG_variable:
caac4577
JG
3572 if (pdi->locdesc)
3573 addr = decode_locdesc (pdi->locdesc, cu);
3574
3575 if (pdi->locdesc
3576 && addr == 0
3577 && !dwarf2_per_objfile->has_section_at_zero)
3578 {
3579 /* A global or static variable may also have been stripped
3580 out by the linker if unused, in which case its address
3581 will be nullified; do not add such variables into partial
3582 symbol table then. */
3583 }
3584 else if (pdi->is_external)
c906108c
SS
3585 {
3586 /* Global Variable.
3587 Don't enter into the minimal symbol tables as there is
3588 a minimal symbol table entry from the ELF symbols already.
3589 Enter into partial symbol table if it has a location
3590 descriptor or a type.
3591 If the location descriptor is missing, new_symbol will create
3592 a LOC_UNRESOLVED symbol, the address of the variable will then
3593 be determined from the minimal symbol table whenever the variable
3594 is referenced.
3595 The address for the partial symbol table entry is not
3596 used by GDB, but it comes in handy for debugging partial symbol
3597 table building. */
3598
c906108c 3599 if (pdi->locdesc || pdi->has_type)
38d518c9 3600 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3601 built_actual_name,
5c4e30ca
DC
3602 VAR_DOMAIN, LOC_STATIC,
3603 &objfile->global_psymbols,
3604 0, addr + baseaddr,
e142c38c 3605 cu->language, objfile);
c906108c
SS
3606 }
3607 else
3608 {
3609 /* Static Variable. Skip symbols without location descriptors. */
3610 if (pdi->locdesc == NULL)
decbce07
MS
3611 {
3612 if (built_actual_name)
3613 xfree (actual_name);
3614 return;
3615 }
38d518c9 3616 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 3617 mst_file_data, objfile); */
38d518c9 3618 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3619 built_actual_name,
5c4e30ca
DC
3620 VAR_DOMAIN, LOC_STATIC,
3621 &objfile->static_psymbols,
3622 0, addr + baseaddr,
e142c38c 3623 cu->language, objfile);
c906108c
SS
3624 }
3625 break;
3626 case DW_TAG_typedef:
3627 case DW_TAG_base_type:
a02abb62 3628 case DW_TAG_subrange_type:
38d518c9 3629 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3630 built_actual_name,
176620f1 3631 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 3632 &objfile->static_psymbols,
e142c38c 3633 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3634 break;
72bf9492
DJ
3635 case DW_TAG_namespace:
3636 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3637 built_actual_name,
72bf9492
DJ
3638 VAR_DOMAIN, LOC_TYPEDEF,
3639 &objfile->global_psymbols,
3640 0, (CORE_ADDR) 0, cu->language, objfile);
3641 break;
c906108c 3642 case DW_TAG_class_type:
680b30c7 3643 case DW_TAG_interface_type:
c906108c
SS
3644 case DW_TAG_structure_type:
3645 case DW_TAG_union_type:
3646 case DW_TAG_enumeration_type:
fa4028e9
JB
3647 /* Skip external references. The DWARF standard says in the section
3648 about "Structure, Union, and Class Type Entries": "An incomplete
3649 structure, union or class type is represented by a structure,
3650 union or class entry that does not have a byte size attribute
3651 and that has a DW_AT_declaration attribute." */
3652 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
3653 {
3654 if (built_actual_name)
3655 xfree (actual_name);
3656 return;
3657 }
fa4028e9 3658
63d06c5c
DC
3659 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3660 static vs. global. */
38d518c9 3661 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3662 built_actual_name,
176620f1 3663 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
3664 (cu->language == language_cplus
3665 || cu->language == language_java)
63d06c5c
DC
3666 ? &objfile->global_psymbols
3667 : &objfile->static_psymbols,
e142c38c 3668 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 3669
c906108c
SS
3670 break;
3671 case DW_TAG_enumerator:
38d518c9 3672 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 3673 built_actual_name,
176620f1 3674 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
3675 (cu->language == language_cplus
3676 || cu->language == language_java)
f6fe98ef
DJ
3677 ? &objfile->global_psymbols
3678 : &objfile->static_psymbols,
e142c38c 3679 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
3680 break;
3681 default:
3682 break;
3683 }
5c4e30ca 3684
72bf9492
DJ
3685 if (built_actual_name)
3686 xfree (actual_name);
c906108c
SS
3687}
3688
5c4e30ca
DC
3689/* Read a partial die corresponding to a namespace; also, add a symbol
3690 corresponding to that namespace to the symbol table. NAMESPACE is
3691 the name of the enclosing namespace. */
91c24f0a 3692
72bf9492
DJ
3693static void
3694add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 3695 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3696 int need_pc, struct dwarf2_cu *cu)
91c24f0a 3697{
72bf9492 3698 /* Add a symbol for the namespace. */
e7c27a73 3699
72bf9492 3700 add_partial_symbol (pdi, cu);
5c4e30ca
DC
3701
3702 /* Now scan partial symbols in that namespace. */
3703
91c24f0a 3704 if (pdi->has_children)
5734ee8b 3705 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
3706}
3707
5d7cb8df
JK
3708/* Read a partial die corresponding to a Fortran module. */
3709
3710static void
3711add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3712 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3713{
f55ee35c 3714 /* Now scan partial symbols in that module. */
5d7cb8df
JK
3715
3716 if (pdi->has_children)
3717 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3718}
3719
bc30ff58
JB
3720/* Read a partial die corresponding to a subprogram and create a partial
3721 symbol for that subprogram. When the CU language allows it, this
3722 routine also defines a partial symbol for each nested subprogram
3723 that this subprogram contains.
6e70227d 3724
bc30ff58
JB
3725 DIE my also be a lexical block, in which case we simply search
3726 recursively for suprograms defined inside that lexical block.
3727 Again, this is only performed when the CU language allows this
3728 type of definitions. */
3729
3730static void
3731add_partial_subprogram (struct partial_die_info *pdi,
3732 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 3733 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
3734{
3735 if (pdi->tag == DW_TAG_subprogram)
3736 {
3737 if (pdi->has_pc_info)
3738 {
3739 if (pdi->lowpc < *lowpc)
3740 *lowpc = pdi->lowpc;
3741 if (pdi->highpc > *highpc)
3742 *highpc = pdi->highpc;
5734ee8b
DJ
3743 if (need_pc)
3744 {
3745 CORE_ADDR baseaddr;
3746 struct objfile *objfile = cu->objfile;
3747
3748 baseaddr = ANOFFSET (objfile->section_offsets,
3749 SECT_OFF_TEXT (objfile));
3750 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
3751 pdi->lowpc + baseaddr,
3752 pdi->highpc - 1 + baseaddr,
9291a0cd 3753 cu->per_cu->v.psymtab);
5734ee8b 3754 }
bc30ff58 3755 if (!pdi->is_declaration)
e8d05480
JB
3756 /* Ignore subprogram DIEs that do not have a name, they are
3757 illegal. Do not emit a complaint at this point, we will
3758 do so when we convert this psymtab into a symtab. */
3759 if (pdi->name)
3760 add_partial_symbol (pdi, cu);
bc30ff58
JB
3761 }
3762 }
6e70227d 3763
bc30ff58
JB
3764 if (! pdi->has_children)
3765 return;
3766
3767 if (cu->language == language_ada)
3768 {
3769 pdi = pdi->die_child;
3770 while (pdi != NULL)
3771 {
3772 fixup_partial_die (pdi, cu);
3773 if (pdi->tag == DW_TAG_subprogram
3774 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 3775 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
3776 pdi = pdi->die_sibling;
3777 }
3778 }
3779}
3780
72bf9492
DJ
3781/* See if we can figure out if the class lives in a namespace. We do
3782 this by looking for a member function; its demangled name will
3783 contain namespace info, if there is any. */
63d06c5c 3784
72bf9492
DJ
3785static void
3786guess_structure_name (struct partial_die_info *struct_pdi,
3787 struct dwarf2_cu *cu)
63d06c5c 3788{
987504bb
JJ
3789 if ((cu->language == language_cplus
3790 || cu->language == language_java)
72bf9492 3791 && cu->has_namespace_info == 0
63d06c5c
DC
3792 && struct_pdi->has_children)
3793 {
63d06c5c
DC
3794 /* NOTE: carlton/2003-10-07: Getting the info this way changes
3795 what template types look like, because the demangler
3796 frequently doesn't give the same name as the debug info. We
3797 could fix this by only using the demangled name to get the
134d01f1 3798 prefix (but see comment in read_structure_type). */
63d06c5c 3799
72bf9492 3800 struct partial_die_info *real_pdi;
5d51ca54 3801
72bf9492
DJ
3802 /* If this DIE (this DIE's specification, if any) has a parent, then
3803 we should not do this. We'll prepend the parent's fully qualified
3804 name when we create the partial symbol. */
5d51ca54 3805
72bf9492 3806 real_pdi = struct_pdi;
72bf9492 3807 while (real_pdi->has_specification)
10b3939b 3808 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
63d06c5c 3809
72bf9492
DJ
3810 if (real_pdi->die_parent != NULL)
3811 return;
63d06c5c 3812 }
63d06c5c
DC
3813}
3814
91c24f0a
DC
3815/* Read a partial die corresponding to an enumeration type. */
3816
72bf9492
DJ
3817static void
3818add_partial_enumeration (struct partial_die_info *enum_pdi,
3819 struct dwarf2_cu *cu)
91c24f0a 3820{
72bf9492 3821 struct partial_die_info *pdi;
91c24f0a
DC
3822
3823 if (enum_pdi->name != NULL)
72bf9492
DJ
3824 add_partial_symbol (enum_pdi, cu);
3825
3826 pdi = enum_pdi->die_child;
3827 while (pdi)
91c24f0a 3828 {
72bf9492 3829 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 3830 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 3831 else
72bf9492
DJ
3832 add_partial_symbol (pdi, cu);
3833 pdi = pdi->die_sibling;
91c24f0a 3834 }
91c24f0a
DC
3835}
3836
4bb7a0a7
DJ
3837/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3838 Return the corresponding abbrev, or NULL if the number is zero (indicating
3839 an empty DIE). In either case *BYTES_READ will be set to the length of
3840 the initial number. */
3841
3842static struct abbrev_info *
fe1b8b76 3843peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 3844 struct dwarf2_cu *cu)
4bb7a0a7
DJ
3845{
3846 bfd *abfd = cu->objfile->obfd;
3847 unsigned int abbrev_number;
3848 struct abbrev_info *abbrev;
3849
3850 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3851
3852 if (abbrev_number == 0)
3853 return NULL;
3854
3855 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3856 if (!abbrev)
3857 {
8a3fe4f8 3858 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
3859 bfd_get_filename (abfd));
3860 }
3861
3862 return abbrev;
3863}
3864
93311388
DE
3865/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3866 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
3867 DIE. Any children of the skipped DIEs will also be skipped. */
3868
fe1b8b76 3869static gdb_byte *
93311388 3870skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3871{
3872 struct abbrev_info *abbrev;
3873 unsigned int bytes_read;
3874
3875 while (1)
3876 {
3877 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3878 if (abbrev == NULL)
3879 return info_ptr + bytes_read;
3880 else
93311388 3881 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4bb7a0a7
DJ
3882 }
3883}
3884
93311388
DE
3885/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3886 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
3887 abbrev corresponding to that skipped uleb128 should be passed in
3888 ABBREV. Returns a pointer to this DIE's sibling, skipping any
3889 children. */
3890
fe1b8b76 3891static gdb_byte *
93311388
DE
3892skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
3893 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4bb7a0a7
DJ
3894{
3895 unsigned int bytes_read;
3896 struct attribute attr;
3897 bfd *abfd = cu->objfile->obfd;
3898 unsigned int form, i;
3899
3900 for (i = 0; i < abbrev->num_attrs; i++)
3901 {
3902 /* The only abbrev we care about is DW_AT_sibling. */
3903 if (abbrev->attrs[i].name == DW_AT_sibling)
3904 {
3905 read_attribute (&attr, &abbrev->attrs[i],
3906 abfd, info_ptr, cu);
3907 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 3908 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 3909 else
93311388 3910 return buffer + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
3911 }
3912
3913 /* If it isn't DW_AT_sibling, skip this attribute. */
3914 form = abbrev->attrs[i].form;
3915 skip_attribute:
3916 switch (form)
3917 {
4bb7a0a7 3918 case DW_FORM_ref_addr:
ae411497
TT
3919 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
3920 and later it is offset sized. */
3921 if (cu->header.version == 2)
3922 info_ptr += cu->header.addr_size;
3923 else
3924 info_ptr += cu->header.offset_size;
3925 break;
3926 case DW_FORM_addr:
4bb7a0a7
DJ
3927 info_ptr += cu->header.addr_size;
3928 break;
3929 case DW_FORM_data1:
3930 case DW_FORM_ref1:
3931 case DW_FORM_flag:
3932 info_ptr += 1;
3933 break;
2dc7f7b3
TT
3934 case DW_FORM_flag_present:
3935 break;
4bb7a0a7
DJ
3936 case DW_FORM_data2:
3937 case DW_FORM_ref2:
3938 info_ptr += 2;
3939 break;
3940 case DW_FORM_data4:
3941 case DW_FORM_ref4:
3942 info_ptr += 4;
3943 break;
3944 case DW_FORM_data8:
3945 case DW_FORM_ref8:
348e048f 3946 case DW_FORM_sig8:
4bb7a0a7
DJ
3947 info_ptr += 8;
3948 break;
3949 case DW_FORM_string:
3950 read_string (abfd, info_ptr, &bytes_read);
3951 info_ptr += bytes_read;
3952 break;
2dc7f7b3 3953 case DW_FORM_sec_offset:
4bb7a0a7
DJ
3954 case DW_FORM_strp:
3955 info_ptr += cu->header.offset_size;
3956 break;
2dc7f7b3 3957 case DW_FORM_exprloc:
4bb7a0a7
DJ
3958 case DW_FORM_block:
3959 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3960 info_ptr += bytes_read;
3961 break;
3962 case DW_FORM_block1:
3963 info_ptr += 1 + read_1_byte (abfd, info_ptr);
3964 break;
3965 case DW_FORM_block2:
3966 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
3967 break;
3968 case DW_FORM_block4:
3969 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
3970 break;
3971 case DW_FORM_sdata:
3972 case DW_FORM_udata:
3973 case DW_FORM_ref_udata:
3974 info_ptr = skip_leb128 (abfd, info_ptr);
3975 break;
3976 case DW_FORM_indirect:
3977 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3978 info_ptr += bytes_read;
3979 /* We need to continue parsing from here, so just go back to
3980 the top. */
3981 goto skip_attribute;
3982
3983 default:
8a3fe4f8 3984 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
3985 dwarf_form_name (form),
3986 bfd_get_filename (abfd));
3987 }
3988 }
3989
3990 if (abbrev->has_children)
93311388 3991 return skip_children (buffer, info_ptr, cu);
4bb7a0a7
DJ
3992 else
3993 return info_ptr;
3994}
3995
93311388
DE
3996/* Locate ORIG_PDI's sibling.
3997 INFO_PTR should point to the start of the next DIE after ORIG_PDI
3998 in BUFFER. */
91c24f0a 3999
fe1b8b76 4000static gdb_byte *
93311388
DE
4001locate_pdi_sibling (struct partial_die_info *orig_pdi,
4002 gdb_byte *buffer, gdb_byte *info_ptr,
e7c27a73 4003 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
4004{
4005 /* Do we know the sibling already? */
72bf9492 4006
91c24f0a
DC
4007 if (orig_pdi->sibling)
4008 return orig_pdi->sibling;
4009
4010 /* Are there any children to deal with? */
4011
4012 if (!orig_pdi->has_children)
4013 return info_ptr;
4014
4bb7a0a7 4015 /* Skip the children the long way. */
91c24f0a 4016
93311388 4017 return skip_children (buffer, info_ptr, cu);
91c24f0a
DC
4018}
4019
c906108c
SS
4020/* Expand this partial symbol table into a full symbol table. */
4021
4022static void
fba45db2 4023dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 4024{
c906108c
SS
4025 if (pst != NULL)
4026 {
4027 if (pst->readin)
4028 {
8a3fe4f8 4029 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
4030 }
4031 else
4032 {
4033 if (info_verbose)
4034 {
a3f17187 4035 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
4036 gdb_flush (gdb_stdout);
4037 }
4038
10b3939b
DJ
4039 /* Restore our global data. */
4040 dwarf2_per_objfile = objfile_data (pst->objfile,
4041 dwarf2_objfile_data_key);
4042
b2ab525c
KB
4043 /* If this psymtab is constructed from a debug-only objfile, the
4044 has_section_at_zero flag will not necessarily be correct. We
4045 can get the correct value for this flag by looking at the data
4046 associated with the (presumably stripped) associated objfile. */
4047 if (pst->objfile->separate_debug_objfile_backlink)
4048 {
4049 struct dwarf2_per_objfile *dpo_backlink
4050 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4051 dwarf2_objfile_data_key);
9a619af0 4052
b2ab525c
KB
4053 dwarf2_per_objfile->has_section_at_zero
4054 = dpo_backlink->has_section_at_zero;
4055 }
4056
c906108c
SS
4057 psymtab_to_symtab_1 (pst);
4058
4059 /* Finish up the debug error message. */
4060 if (info_verbose)
a3f17187 4061 printf_filtered (_("done.\n"));
c906108c
SS
4062 }
4063 }
4064}
4065
10b3939b
DJ
4066/* Add PER_CU to the queue. */
4067
4068static void
03dd20cc 4069queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
4070{
4071 struct dwarf2_queue_item *item;
4072
4073 per_cu->queued = 1;
4074 item = xmalloc (sizeof (*item));
4075 item->per_cu = per_cu;
4076 item->next = NULL;
4077
4078 if (dwarf2_queue == NULL)
4079 dwarf2_queue = item;
4080 else
4081 dwarf2_queue_tail->next = item;
4082
4083 dwarf2_queue_tail = item;
4084}
4085
4086/* Process the queue. */
4087
4088static void
4089process_queue (struct objfile *objfile)
4090{
4091 struct dwarf2_queue_item *item, *next_item;
4092
03dd20cc
DJ
4093 /* The queue starts out with one item, but following a DIE reference
4094 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
4095 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4096 {
9291a0cd
TT
4097 if (dwarf2_per_objfile->using_index
4098 ? !item->per_cu->v.quick->symtab
4099 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
4100 process_full_comp_unit (item->per_cu);
4101
4102 item->per_cu->queued = 0;
4103 next_item = item->next;
4104 xfree (item);
4105 }
4106
4107 dwarf2_queue_tail = NULL;
4108}
4109
4110/* Free all allocated queue entries. This function only releases anything if
4111 an error was thrown; if the queue was processed then it would have been
4112 freed as we went along. */
4113
4114static void
4115dwarf2_release_queue (void *dummy)
4116{
4117 struct dwarf2_queue_item *item, *last;
4118
4119 item = dwarf2_queue;
4120 while (item)
4121 {
4122 /* Anything still marked queued is likely to be in an
4123 inconsistent state, so discard it. */
4124 if (item->per_cu->queued)
4125 {
4126 if (item->per_cu->cu != NULL)
4127 free_one_cached_comp_unit (item->per_cu->cu);
4128 item->per_cu->queued = 0;
4129 }
4130
4131 last = item;
4132 item = item->next;
4133 xfree (last);
4134 }
4135
4136 dwarf2_queue = dwarf2_queue_tail = NULL;
4137}
4138
4139/* Read in full symbols for PST, and anything it depends on. */
4140
c906108c 4141static void
fba45db2 4142psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 4143{
10b3939b 4144 struct dwarf2_per_cu_data *per_cu;
c906108c 4145 struct cleanup *back_to;
aaa75496
JB
4146 int i;
4147
4148 for (i = 0; i < pst->number_of_dependencies; i++)
4149 if (!pst->dependencies[i]->readin)
4150 {
4151 /* Inform about additional files that need to be read in. */
4152 if (info_verbose)
4153 {
a3f17187 4154 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
4155 fputs_filtered (" ", gdb_stdout);
4156 wrap_here ("");
4157 fputs_filtered ("and ", gdb_stdout);
4158 wrap_here ("");
4159 printf_filtered ("%s...", pst->dependencies[i]->filename);
4160 wrap_here (""); /* Flush output */
4161 gdb_flush (gdb_stdout);
4162 }
4163 psymtab_to_symtab_1 (pst->dependencies[i]);
4164 }
4165
e38df1d0 4166 per_cu = pst->read_symtab_private;
10b3939b
DJ
4167
4168 if (per_cu == NULL)
aaa75496
JB
4169 {
4170 /* It's an include file, no symbols to read for it.
4171 Everything is in the parent symtab. */
4172 pst->readin = 1;
4173 return;
4174 }
c906108c 4175
9291a0cd 4176 dw2_do_instantiate_symtab (pst->objfile, per_cu);
10b3939b
DJ
4177}
4178
93311388 4179/* Load the DIEs associated with PER_CU into memory. */
10b3939b 4180
93311388 4181static void
31ffec48 4182load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 4183{
31ffec48 4184 bfd *abfd = objfile->obfd;
10b3939b 4185 struct dwarf2_cu *cu;
c764a876 4186 unsigned int offset;
93311388 4187 gdb_byte *info_ptr, *beg_of_comp_unit;
10b3939b
DJ
4188 struct cleanup *back_to, *free_cu_cleanup;
4189 struct attribute *attr;
6502dd73 4190
348e048f
DE
4191 gdb_assert (! per_cu->from_debug_types);
4192
c906108c 4193 /* Set local variables from the partial symbol table info. */
10b3939b 4194 offset = per_cu->offset;
6502dd73 4195
be391dca 4196 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
dce234bc 4197 info_ptr = dwarf2_per_objfile->info.buffer + offset;
93311388 4198 beg_of_comp_unit = info_ptr;
63d06c5c 4199
93311388 4200 cu = alloc_one_comp_unit (objfile);
c906108c 4201
10b3939b
DJ
4202 /* If an error occurs while loading, release our storage. */
4203 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 4204
93311388 4205 /* Read in the comp_unit header. */
10b3939b 4206 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c 4207
93311388
DE
4208 /* Complete the cu_header. */
4209 cu->header.offset = offset;
4210 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4211
4212 /* Read the abbrevs for this compilation unit. */
10b3939b
DJ
4213 dwarf2_read_abbrevs (abfd, cu);
4214 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
4215
93311388 4216 /* Link this compilation unit into the compilation unit tree. */
10b3939b 4217 per_cu->cu = cu;
93311388 4218 cu->per_cu = per_cu;
f792889a 4219 cu->type_hash = per_cu->type_hash;
e142c38c 4220
93311388 4221 cu->dies = read_comp_unit (info_ptr, cu);
10b3939b
DJ
4222
4223 /* We try not to read any attributes in this function, because not
4224 all objfiles needed for references have been loaded yet, and symbol
4225 table processing isn't initialized. But we have to set the CU language,
4226 or we won't be able to build types correctly. */
4227 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4228 if (attr)
4229 set_cu_language (DW_UNSND (attr), cu);
4230 else
4231 set_cu_language (language_minimal, cu);
4232
a6c727b2
DJ
4233 /* Similarly, if we do not read the producer, we can not apply
4234 producer-specific interpretation. */
4235 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4236 if (attr)
4237 cu->producer = DW_STRING (attr);
4238
348e048f
DE
4239 /* Link this CU into read_in_chain. */
4240 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4241 dwarf2_per_objfile->read_in_chain = per_cu;
4242
10b3939b 4243 do_cleanups (back_to);
e142c38c 4244
10b3939b
DJ
4245 /* We've successfully allocated this compilation unit. Let our caller
4246 clean it up when finished with it. */
4247 discard_cleanups (free_cu_cleanup);
10b3939b
DJ
4248}
4249
4250/* Generate full symbol information for PST and CU, whose DIEs have
4251 already been loaded into memory. */
4252
4253static void
4254process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4255{
10b3939b 4256 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 4257 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
4258 CORE_ADDR lowpc, highpc;
4259 struct symtab *symtab;
4260 struct cleanup *back_to;
10b3939b
DJ
4261 CORE_ADDR baseaddr;
4262
4263 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4264
10b3939b
DJ
4265 buildsym_init ();
4266 back_to = make_cleanup (really_free_pendings, NULL);
4267
4268 cu->list_in_scope = &file_symbols;
c906108c 4269
d85a05f0 4270 dwarf2_find_base_address (cu->dies, cu);
0d53c4c4 4271
c906108c 4272 /* Do line number decoding in read_file_scope () */
10b3939b 4273 process_die (cu->dies, cu);
c906108c 4274
fae299cd
DC
4275 /* Some compilers don't define a DW_AT_high_pc attribute for the
4276 compilation unit. If the DW_AT_high_pc is missing, synthesize
4277 it, by scanning the DIE's below the compilation unit. */
10b3939b 4278 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 4279
613e1657 4280 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
4281
4282 /* Set symtab language to language from DW_AT_language.
4283 If the compilation is from a C file generated by language preprocessors,
4284 do not set the language if it was already deduced by start_subfile. */
4285 if (symtab != NULL
10b3939b 4286 && !(cu->language == language_c && symtab->language != language_c))
c906108c 4287 {
10b3939b 4288 symtab->language = cu->language;
c906108c 4289 }
9291a0cd
TT
4290
4291 if (dwarf2_per_objfile->using_index)
4292 per_cu->v.quick->symtab = symtab;
4293 else
4294 {
4295 struct partial_symtab *pst = per_cu->v.psymtab;
4296 pst->symtab = symtab;
4297 pst->readin = 1;
4298 }
c906108c
SS
4299
4300 do_cleanups (back_to);
4301}
4302
4303/* Process a die and its children. */
4304
4305static void
e7c27a73 4306process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4307{
4308 switch (die->tag)
4309 {
4310 case DW_TAG_padding:
4311 break;
4312 case DW_TAG_compile_unit:
e7c27a73 4313 read_file_scope (die, cu);
c906108c 4314 break;
348e048f
DE
4315 case DW_TAG_type_unit:
4316 read_type_unit_scope (die, cu);
4317 break;
c906108c 4318 case DW_TAG_subprogram:
c906108c 4319 case DW_TAG_inlined_subroutine:
edb3359d 4320 read_func_scope (die, cu);
c906108c
SS
4321 break;
4322 case DW_TAG_lexical_block:
14898363
L
4323 case DW_TAG_try_block:
4324 case DW_TAG_catch_block:
e7c27a73 4325 read_lexical_block_scope (die, cu);
c906108c
SS
4326 break;
4327 case DW_TAG_class_type:
680b30c7 4328 case DW_TAG_interface_type:
c906108c
SS
4329 case DW_TAG_structure_type:
4330 case DW_TAG_union_type:
134d01f1 4331 process_structure_scope (die, cu);
c906108c
SS
4332 break;
4333 case DW_TAG_enumeration_type:
134d01f1 4334 process_enumeration_scope (die, cu);
c906108c 4335 break;
134d01f1 4336
f792889a
DJ
4337 /* These dies have a type, but processing them does not create
4338 a symbol or recurse to process the children. Therefore we can
4339 read them on-demand through read_type_die. */
c906108c 4340 case DW_TAG_subroutine_type:
72019c9c 4341 case DW_TAG_set_type:
c906108c 4342 case DW_TAG_array_type:
c906108c 4343 case DW_TAG_pointer_type:
c906108c 4344 case DW_TAG_ptr_to_member_type:
c906108c 4345 case DW_TAG_reference_type:
c906108c 4346 case DW_TAG_string_type:
c906108c 4347 break;
134d01f1 4348
c906108c 4349 case DW_TAG_base_type:
a02abb62 4350 case DW_TAG_subrange_type:
cb249c71 4351 case DW_TAG_typedef:
134d01f1
DJ
4352 /* Add a typedef symbol for the type definition, if it has a
4353 DW_AT_name. */
f792889a 4354 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 4355 break;
c906108c 4356 case DW_TAG_common_block:
e7c27a73 4357 read_common_block (die, cu);
c906108c
SS
4358 break;
4359 case DW_TAG_common_inclusion:
4360 break;
d9fa45fe 4361 case DW_TAG_namespace:
63d06c5c 4362 processing_has_namespace_info = 1;
e7c27a73 4363 read_namespace (die, cu);
d9fa45fe 4364 break;
5d7cb8df 4365 case DW_TAG_module:
f55ee35c 4366 processing_has_namespace_info = 1;
5d7cb8df
JK
4367 read_module (die, cu);
4368 break;
d9fa45fe
DC
4369 case DW_TAG_imported_declaration:
4370 case DW_TAG_imported_module:
63d06c5c 4371 processing_has_namespace_info = 1;
27aa8d6a
SW
4372 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4373 || cu->language != language_fortran))
4374 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4375 dwarf_tag_name (die->tag));
4376 read_import_statement (die, cu);
d9fa45fe 4377 break;
c906108c 4378 default:
e7c27a73 4379 new_symbol (die, NULL, cu);
c906108c
SS
4380 break;
4381 }
4382}
4383
94af9270
KS
4384/* A helper function for dwarf2_compute_name which determines whether DIE
4385 needs to have the name of the scope prepended to the name listed in the
4386 die. */
4387
4388static int
4389die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4390{
1c809c68
TT
4391 struct attribute *attr;
4392
94af9270
KS
4393 switch (die->tag)
4394 {
4395 case DW_TAG_namespace:
4396 case DW_TAG_typedef:
4397 case DW_TAG_class_type:
4398 case DW_TAG_interface_type:
4399 case DW_TAG_structure_type:
4400 case DW_TAG_union_type:
4401 case DW_TAG_enumeration_type:
4402 case DW_TAG_enumerator:
4403 case DW_TAG_subprogram:
4404 case DW_TAG_member:
4405 return 1;
4406
4407 case DW_TAG_variable:
4408 /* We only need to prefix "globally" visible variables. These include
4409 any variable marked with DW_AT_external or any variable that
4410 lives in a namespace. [Variables in anonymous namespaces
4411 require prefixing, but they are not DW_AT_external.] */
4412
4413 if (dwarf2_attr (die, DW_AT_specification, cu))
4414 {
4415 struct dwarf2_cu *spec_cu = cu;
9a619af0 4416
94af9270
KS
4417 return die_needs_namespace (die_specification (die, &spec_cu),
4418 spec_cu);
4419 }
4420
1c809c68 4421 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
4422 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4423 && die->parent->tag != DW_TAG_module)
1c809c68
TT
4424 return 0;
4425 /* A variable in a lexical block of some kind does not need a
4426 namespace, even though in C++ such variables may be external
4427 and have a mangled name. */
4428 if (die->parent->tag == DW_TAG_lexical_block
4429 || die->parent->tag == DW_TAG_try_block
1054b214
TT
4430 || die->parent->tag == DW_TAG_catch_block
4431 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
4432 return 0;
4433 return 1;
94af9270
KS
4434
4435 default:
4436 return 0;
4437 }
4438}
4439
4440/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4441 compute the physname for the object, which include a method's
4442 formal parameters (C++/Java) and return type (Java).
4443
af6b7be1
JB
4444 For Ada, return the DIE's linkage name rather than the fully qualified
4445 name. PHYSNAME is ignored..
4446
94af9270
KS
4447 The result is allocated on the objfile_obstack and canonicalized. */
4448
4449static const char *
4450dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4451 int physname)
4452{
4453 if (name == NULL)
4454 name = dwarf2_name (die, cu);
4455
f55ee35c
JK
4456 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4457 compute it by typename_concat inside GDB. */
4458 if (cu->language == language_ada
4459 || (cu->language == language_fortran && physname))
4460 {
4461 /* For Ada unit, we prefer the linkage name over the name, as
4462 the former contains the exported name, which the user expects
4463 to be able to reference. Ideally, we want the user to be able
4464 to reference this entity using either natural or linkage name,
4465 but we haven't started looking at this enhancement yet. */
4466 struct attribute *attr;
4467
4468 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4469 if (attr == NULL)
4470 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4471 if (attr && DW_STRING (attr))
4472 return DW_STRING (attr);
4473 }
4474
94af9270
KS
4475 /* These are the only languages we know how to qualify names in. */
4476 if (name != NULL
f55ee35c
JK
4477 && (cu->language == language_cplus || cu->language == language_java
4478 || cu->language == language_fortran))
94af9270
KS
4479 {
4480 if (die_needs_namespace (die, cu))
4481 {
4482 long length;
4483 char *prefix;
4484 struct ui_file *buf;
4485
4486 prefix = determine_prefix (die, cu);
4487 buf = mem_fileopen ();
4488 if (*prefix != '\0')
4489 {
f55ee35c
JK
4490 char *prefixed_name = typename_concat (NULL, prefix, name,
4491 physname, cu);
9a619af0 4492
94af9270
KS
4493 fputs_unfiltered (prefixed_name, buf);
4494 xfree (prefixed_name);
4495 }
4496 else
4497 fputs_unfiltered (name ? name : "", buf);
4498
4499 /* For Java and C++ methods, append formal parameter type
4500 information, if PHYSNAME. */
6e70227d 4501
94af9270
KS
4502 if (physname && die->tag == DW_TAG_subprogram
4503 && (cu->language == language_cplus
4504 || cu->language == language_java))
4505 {
4506 struct type *type = read_type_die (die, cu);
4507
4508 c_type_print_args (type, buf, 0, cu->language);
4509
4510 if (cu->language == language_java)
4511 {
4512 /* For java, we must append the return type to method
4513 names. */
4514 if (die->tag == DW_TAG_subprogram)
4515 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4516 0, 0);
4517 }
4518 else if (cu->language == language_cplus)
4519 {
4520 if (TYPE_NFIELDS (type) > 0
4521 && TYPE_FIELD_ARTIFICIAL (type, 0)
4522 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4523 fputs_unfiltered (" const", buf);
4524 }
4525 }
4526
4527 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4528 &length);
4529 ui_file_delete (buf);
4530
4531 if (cu->language == language_cplus)
4532 {
4533 char *cname
4534 = dwarf2_canonicalize_name (name, cu,
4535 &cu->objfile->objfile_obstack);
9a619af0 4536
94af9270
KS
4537 if (cname != NULL)
4538 name = cname;
4539 }
4540 }
4541 }
4542
4543 return name;
4544}
4545
0114d602
DJ
4546/* Return the fully qualified name of DIE, based on its DW_AT_name.
4547 If scope qualifiers are appropriate they will be added. The result
4548 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
4549 not have a name. NAME may either be from a previous call to
4550 dwarf2_name or NULL.
4551
4552 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
4553
4554static const char *
94af9270 4555dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 4556{
94af9270
KS
4557 return dwarf2_compute_name (name, die, cu, 0);
4558}
0114d602 4559
94af9270
KS
4560/* Construct a physname for the given DIE in CU. NAME may either be
4561 from a previous call to dwarf2_name or NULL. The result will be
4562 allocated on the objfile_objstack or NULL if the DIE does not have a
4563 name.
0114d602 4564
94af9270 4565 The output string will be canonicalized (if C++/Java). */
0114d602 4566
94af9270
KS
4567static const char *
4568dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4569{
4570 return dwarf2_compute_name (name, die, cu, 1);
0114d602
DJ
4571}
4572
27aa8d6a
SW
4573/* Read the import statement specified by the given die and record it. */
4574
4575static void
4576read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4577{
4578 struct attribute *import_attr;
4579 struct die_info *imported_die;
de4affc9 4580 struct dwarf2_cu *imported_cu;
27aa8d6a 4581 const char *imported_name;
794684b6 4582 const char *imported_name_prefix;
13387711
SW
4583 const char *canonical_name;
4584 const char *import_alias;
4585 const char *imported_declaration = NULL;
794684b6 4586 const char *import_prefix;
13387711
SW
4587
4588 char *temp;
27aa8d6a
SW
4589
4590 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4591 if (import_attr == NULL)
4592 {
4593 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4594 dwarf_tag_name (die->tag));
4595 return;
4596 }
4597
de4affc9
CC
4598 imported_cu = cu;
4599 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4600 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
4601 if (imported_name == NULL)
4602 {
4603 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4604
4605 The import in the following code:
4606 namespace A
4607 {
4608 typedef int B;
4609 }
4610
4611 int main ()
4612 {
4613 using A::B;
4614 B b;
4615 return b;
4616 }
4617
4618 ...
4619 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4620 <52> DW_AT_decl_file : 1
4621 <53> DW_AT_decl_line : 6
4622 <54> DW_AT_import : <0x75>
4623 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4624 <59> DW_AT_name : B
4625 <5b> DW_AT_decl_file : 1
4626 <5c> DW_AT_decl_line : 2
4627 <5d> DW_AT_type : <0x6e>
4628 ...
4629 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4630 <76> DW_AT_byte_size : 4
4631 <77> DW_AT_encoding : 5 (signed)
4632
4633 imports the wrong die ( 0x75 instead of 0x58 ).
4634 This case will be ignored until the gcc bug is fixed. */
4635 return;
4636 }
4637
82856980
SW
4638 /* Figure out the local name after import. */
4639 import_alias = dwarf2_name (die, cu);
27aa8d6a 4640
794684b6
SW
4641 /* Figure out where the statement is being imported to. */
4642 import_prefix = determine_prefix (die, cu);
4643
4644 /* Figure out what the scope of the imported die is and prepend it
4645 to the name of the imported die. */
de4affc9 4646 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 4647
f55ee35c
JK
4648 if (imported_die->tag != DW_TAG_namespace
4649 && imported_die->tag != DW_TAG_module)
794684b6 4650 {
13387711
SW
4651 imported_declaration = imported_name;
4652 canonical_name = imported_name_prefix;
794684b6 4653 }
13387711 4654 else if (strlen (imported_name_prefix) > 0)
794684b6 4655 {
13387711
SW
4656 temp = alloca (strlen (imported_name_prefix)
4657 + 2 + strlen (imported_name) + 1);
4658 strcpy (temp, imported_name_prefix);
4659 strcat (temp, "::");
4660 strcat (temp, imported_name);
4661 canonical_name = temp;
794684b6 4662 }
13387711
SW
4663 else
4664 canonical_name = imported_name;
794684b6 4665
c0cc3a76
SW
4666 cp_add_using_directive (import_prefix,
4667 canonical_name,
4668 import_alias,
13387711 4669 imported_declaration,
c0cc3a76 4670 &cu->objfile->objfile_obstack);
27aa8d6a
SW
4671}
4672
5fb290d7 4673static void
e142c38c 4674initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 4675{
e142c38c 4676 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
4677}
4678
cb1df416
DJ
4679static void
4680free_cu_line_header (void *arg)
4681{
4682 struct dwarf2_cu *cu = arg;
4683
4684 free_line_header (cu->line_header);
4685 cu->line_header = NULL;
4686}
4687
9291a0cd
TT
4688static void
4689find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
4690 char **name, char **comp_dir)
4691{
4692 struct attribute *attr;
4693
4694 *name = NULL;
4695 *comp_dir = NULL;
4696
4697 /* Find the filename. Do not use dwarf2_name here, since the filename
4698 is not a source language identifier. */
4699 attr = dwarf2_attr (die, DW_AT_name, cu);
4700 if (attr)
4701 {
4702 *name = DW_STRING (attr);
4703 }
4704
4705 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4706 if (attr)
4707 *comp_dir = DW_STRING (attr);
4708 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
4709 {
4710 *comp_dir = ldirname (*name);
4711 if (*comp_dir != NULL)
4712 make_cleanup (xfree, *comp_dir);
4713 }
4714 if (*comp_dir != NULL)
4715 {
4716 /* Irix 6.2 native cc prepends <machine>.: to the compilation
4717 directory, get rid of it. */
4718 char *cp = strchr (*comp_dir, ':');
4719
4720 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
4721 *comp_dir = cp + 1;
4722 }
4723
4724 if (*name == NULL)
4725 *name = "<unknown>";
4726}
4727
c906108c 4728static void
e7c27a73 4729read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4730{
e7c27a73 4731 struct objfile *objfile = cu->objfile;
debd256d 4732 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 4733 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
4734 CORE_ADDR highpc = ((CORE_ADDR) 0);
4735 struct attribute *attr;
e1024ff1 4736 char *name = NULL;
c906108c
SS
4737 char *comp_dir = NULL;
4738 struct die_info *child_die;
4739 bfd *abfd = objfile->obfd;
debd256d 4740 struct line_header *line_header = 0;
e142c38c 4741 CORE_ADDR baseaddr;
6e70227d 4742
e142c38c 4743 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4744
fae299cd 4745 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
4746
4747 /* If we didn't find a lowpc, set it to highpc to avoid complaints
4748 from finish_block. */
2acceee2 4749 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
4750 lowpc = highpc;
4751 lowpc += baseaddr;
4752 highpc += baseaddr;
4753
9291a0cd 4754 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 4755
e142c38c 4756 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
4757 if (attr)
4758 {
e142c38c 4759 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
4760 }
4761
b0f35d58 4762 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 4763 if (attr)
b0f35d58 4764 cu->producer = DW_STRING (attr);
303b6f5d 4765
c906108c
SS
4766 /* We assume that we're processing GCC output. */
4767 processing_gcc_compilation = 2;
c906108c 4768
df8a16a1
DJ
4769 processing_has_namespace_info = 0;
4770
c906108c
SS
4771 start_symtab (name, comp_dir, lowpc);
4772 record_debugformat ("DWARF 2");
303b6f5d 4773 record_producer (cu->producer);
c906108c 4774
e142c38c 4775 initialize_cu_func_list (cu);
c906108c 4776
cb1df416
DJ
4777 /* Decode line number information if present. We do this before
4778 processing child DIEs, so that the line header table is available
4779 for DW_AT_decl_file. */
e142c38c 4780 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
4781 if (attr)
4782 {
debd256d 4783 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 4784 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
4785 if (line_header)
4786 {
cb1df416
DJ
4787 cu->line_header = line_header;
4788 make_cleanup (free_cu_line_header, cu);
aaa75496 4789 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 4790 }
5fb290d7 4791 }
debd256d 4792
cb1df416
DJ
4793 /* Process all dies in compilation unit. */
4794 if (die->child != NULL)
4795 {
4796 child_die = die->child;
4797 while (child_die && child_die->tag)
4798 {
4799 process_die (child_die, cu);
4800 child_die = sibling_die (child_die);
4801 }
4802 }
4803
2e276125
JB
4804 /* Decode macro information, if present. Dwarf 2 macro information
4805 refers to information in the line number info statement program
4806 header, so we can only read it if we've read the header
4807 successfully. */
e142c38c 4808 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 4809 if (attr && line_header)
2e276125
JB
4810 {
4811 unsigned int macro_offset = DW_UNSND (attr);
9a619af0 4812
2e276125 4813 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 4814 comp_dir, abfd, cu);
2e276125 4815 }
debd256d 4816 do_cleanups (back_to);
5fb290d7
DJ
4817}
4818
348e048f
DE
4819/* For TUs we want to skip the first top level sibling if it's not the
4820 actual type being defined by this TU. In this case the first top
4821 level sibling is there to provide context only. */
4822
4823static void
4824read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
4825{
4826 struct objfile *objfile = cu->objfile;
4827 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4828 CORE_ADDR lowpc;
4829 struct attribute *attr;
4830 char *name = NULL;
4831 char *comp_dir = NULL;
4832 struct die_info *child_die;
4833 bfd *abfd = objfile->obfd;
348e048f
DE
4834
4835 /* start_symtab needs a low pc, but we don't really have one.
4836 Do what read_file_scope would do in the absence of such info. */
4837 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4838
4839 /* Find the filename. Do not use dwarf2_name here, since the filename
4840 is not a source language identifier. */
4841 attr = dwarf2_attr (die, DW_AT_name, cu);
4842 if (attr)
4843 name = DW_STRING (attr);
4844
4845 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4846 if (attr)
4847 comp_dir = DW_STRING (attr);
4848 else if (name != NULL && IS_ABSOLUTE_PATH (name))
4849 {
4850 comp_dir = ldirname (name);
4851 if (comp_dir != NULL)
4852 make_cleanup (xfree, comp_dir);
4853 }
4854
4855 if (name == NULL)
4856 name = "<unknown>";
4857
4858 attr = dwarf2_attr (die, DW_AT_language, cu);
4859 if (attr)
4860 set_cu_language (DW_UNSND (attr), cu);
4861
4862 /* This isn't technically needed today. It is done for symmetry
4863 with read_file_scope. */
4864 attr = dwarf2_attr (die, DW_AT_producer, cu);
6e70227d 4865 if (attr)
348e048f
DE
4866 cu->producer = DW_STRING (attr);
4867
4868 /* We assume that we're processing GCC output. */
4869 processing_gcc_compilation = 2;
4870
4871 processing_has_namespace_info = 0;
4872
4873 start_symtab (name, comp_dir, lowpc);
4874 record_debugformat ("DWARF 2");
4875 record_producer (cu->producer);
4876
4877 /* Process the dies in the type unit. */
4878 if (die->child == NULL)
4879 {
4880 dump_die_for_error (die);
4881 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
4882 bfd_get_filename (abfd));
4883 }
4884
4885 child_die = die->child;
4886
4887 while (child_die && child_die->tag)
4888 {
4889 process_die (child_die, cu);
4890
4891 child_die = sibling_die (child_die);
4892 }
4893
4894 do_cleanups (back_to);
4895}
4896
5fb290d7 4897static void
e142c38c
DJ
4898add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
4899 struct dwarf2_cu *cu)
5fb290d7
DJ
4900{
4901 struct function_range *thisfn;
4902
4903 thisfn = (struct function_range *)
7b5a2f43 4904 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
4905 thisfn->name = name;
4906 thisfn->lowpc = lowpc;
4907 thisfn->highpc = highpc;
4908 thisfn->seen_line = 0;
4909 thisfn->next = NULL;
4910
e142c38c
DJ
4911 if (cu->last_fn == NULL)
4912 cu->first_fn = thisfn;
5fb290d7 4913 else
e142c38c 4914 cu->last_fn->next = thisfn;
5fb290d7 4915
e142c38c 4916 cu->last_fn = thisfn;
c906108c
SS
4917}
4918
d389af10
JK
4919/* qsort helper for inherit_abstract_dies. */
4920
4921static int
4922unsigned_int_compar (const void *ap, const void *bp)
4923{
4924 unsigned int a = *(unsigned int *) ap;
4925 unsigned int b = *(unsigned int *) bp;
4926
4927 return (a > b) - (b > a);
4928}
4929
4930/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
4931 Inherit only the children of the DW_AT_abstract_origin DIE not being already
4932 referenced by DW_AT_abstract_origin from the children of the current DIE. */
4933
4934static void
4935inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
4936{
4937 struct die_info *child_die;
4938 unsigned die_children_count;
4939 /* CU offsets which were referenced by children of the current DIE. */
4940 unsigned *offsets;
4941 unsigned *offsets_end, *offsetp;
4942 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
4943 struct die_info *origin_die;
4944 /* Iterator of the ORIGIN_DIE children. */
4945 struct die_info *origin_child_die;
4946 struct cleanup *cleanups;
4947 struct attribute *attr;
4948
4949 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
4950 if (!attr)
4951 return;
4952
4953 origin_die = follow_die_ref (die, attr, &cu);
edb3359d
DJ
4954 if (die->tag != origin_die->tag
4955 && !(die->tag == DW_TAG_inlined_subroutine
4956 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
4957 complaint (&symfile_complaints,
4958 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
4959 die->offset, origin_die->offset);
4960
4961 child_die = die->child;
4962 die_children_count = 0;
4963 while (child_die && child_die->tag)
4964 {
4965 child_die = sibling_die (child_die);
4966 die_children_count++;
4967 }
4968 offsets = xmalloc (sizeof (*offsets) * die_children_count);
4969 cleanups = make_cleanup (xfree, offsets);
4970
4971 offsets_end = offsets;
4972 child_die = die->child;
4973 while (child_die && child_die->tag)
4974 {
c38f313d
DJ
4975 /* For each CHILD_DIE, find the corresponding child of
4976 ORIGIN_DIE. If there is more than one layer of
4977 DW_AT_abstract_origin, follow them all; there shouldn't be,
4978 but GCC versions at least through 4.4 generate this (GCC PR
4979 40573). */
4980 struct die_info *child_origin_die = child_die;
9a619af0 4981
c38f313d
DJ
4982 while (1)
4983 {
4984 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
4985 if (attr == NULL)
4986 break;
4987 child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
4988 }
4989
d389af10
JK
4990 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
4991 counterpart may exist. */
c38f313d 4992 if (child_origin_die != child_die)
d389af10 4993 {
edb3359d
DJ
4994 if (child_die->tag != child_origin_die->tag
4995 && !(child_die->tag == DW_TAG_inlined_subroutine
4996 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
4997 complaint (&symfile_complaints,
4998 _("Child DIE 0x%x and its abstract origin 0x%x have "
4999 "different tags"), child_die->offset,
5000 child_origin_die->offset);
c38f313d
DJ
5001 if (child_origin_die->parent != origin_die)
5002 complaint (&symfile_complaints,
5003 _("Child DIE 0x%x and its abstract origin 0x%x have "
5004 "different parents"), child_die->offset,
5005 child_origin_die->offset);
5006 else
5007 *offsets_end++ = child_origin_die->offset;
d389af10
JK
5008 }
5009 child_die = sibling_die (child_die);
5010 }
5011 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5012 unsigned_int_compar);
5013 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5014 if (offsetp[-1] == *offsetp)
5015 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5016 "to DIE 0x%x as their abstract origin"),
5017 die->offset, *offsetp);
5018
5019 offsetp = offsets;
5020 origin_child_die = origin_die->child;
5021 while (origin_child_die && origin_child_die->tag)
5022 {
5023 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5024 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5025 offsetp++;
5026 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5027 {
5028 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5029 process_die (origin_child_die, cu);
5030 }
5031 origin_child_die = sibling_die (origin_child_die);
5032 }
5033
5034 do_cleanups (cleanups);
5035}
5036
c906108c 5037static void
e7c27a73 5038read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5039{
e7c27a73 5040 struct objfile *objfile = cu->objfile;
52f0bd74 5041 struct context_stack *new;
c906108c
SS
5042 CORE_ADDR lowpc;
5043 CORE_ADDR highpc;
5044 struct die_info *child_die;
edb3359d 5045 struct attribute *attr, *call_line, *call_file;
c906108c 5046 char *name;
e142c38c 5047 CORE_ADDR baseaddr;
801e3a5b 5048 struct block *block;
edb3359d
DJ
5049 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5050
5051 if (inlined_func)
5052 {
5053 /* If we do not have call site information, we can't show the
5054 caller of this inlined function. That's too confusing, so
5055 only use the scope for local variables. */
5056 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5057 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5058 if (call_line == NULL || call_file == NULL)
5059 {
5060 read_lexical_block_scope (die, cu);
5061 return;
5062 }
5063 }
c906108c 5064
e142c38c
DJ
5065 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5066
94af9270 5067 name = dwarf2_name (die, cu);
c906108c 5068
e8d05480
JB
5069 /* Ignore functions with missing or empty names. These are actually
5070 illegal according to the DWARF standard. */
5071 if (name == NULL)
5072 {
5073 complaint (&symfile_complaints,
5074 _("missing name for subprogram DIE at %d"), die->offset);
5075 return;
5076 }
5077
5078 /* Ignore functions with missing or invalid low and high pc attributes. */
5079 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5080 {
ae4d0c03
PM
5081 attr = dwarf2_attr (die, DW_AT_external, cu);
5082 if (!attr || !DW_UNSND (attr))
5083 complaint (&symfile_complaints,
5084 _("cannot get low and high bounds for subprogram DIE at %d"),
5085 die->offset);
e8d05480
JB
5086 return;
5087 }
c906108c
SS
5088
5089 lowpc += baseaddr;
5090 highpc += baseaddr;
5091
5fb290d7 5092 /* Record the function range for dwarf_decode_lines. */
e142c38c 5093 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 5094
c906108c 5095 new = push_context (0, lowpc);
f792889a 5096 new->name = new_symbol (die, read_type_die (die, cu), cu);
4c2df51b 5097
4cecd739
DJ
5098 /* If there is a location expression for DW_AT_frame_base, record
5099 it. */
e142c38c 5100 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 5101 if (attr)
c034e007
AC
5102 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5103 expression is being recorded directly in the function's symbol
5104 and not in a separate frame-base object. I guess this hack is
5105 to avoid adding some sort of frame-base adjunct/annex to the
5106 function's symbol :-(. The problem with doing this is that it
5107 results in a function symbol with a location expression that
5108 has nothing to do with the location of the function, ouch! The
5109 relationship should be: a function's symbol has-a frame base; a
5110 frame-base has-a location expression. */
e7c27a73 5111 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 5112
e142c38c 5113 cu->list_in_scope = &local_symbols;
c906108c 5114
639d11d3 5115 if (die->child != NULL)
c906108c 5116 {
639d11d3 5117 child_die = die->child;
c906108c
SS
5118 while (child_die && child_die->tag)
5119 {
e7c27a73 5120 process_die (child_die, cu);
c906108c
SS
5121 child_die = sibling_die (child_die);
5122 }
5123 }
5124
d389af10
JK
5125 inherit_abstract_dies (die, cu);
5126
4a811a97
UW
5127 /* If we have a DW_AT_specification, we might need to import using
5128 directives from the context of the specification DIE. See the
5129 comment in determine_prefix. */
5130 if (cu->language == language_cplus
5131 && dwarf2_attr (die, DW_AT_specification, cu))
5132 {
5133 struct dwarf2_cu *spec_cu = cu;
5134 struct die_info *spec_die = die_specification (die, &spec_cu);
5135
5136 while (spec_die)
5137 {
5138 child_die = spec_die->child;
5139 while (child_die && child_die->tag)
5140 {
5141 if (child_die->tag == DW_TAG_imported_module)
5142 process_die (child_die, spec_cu);
5143 child_die = sibling_die (child_die);
5144 }
5145
5146 /* In some cases, GCC generates specification DIEs that
5147 themselves contain DW_AT_specification attributes. */
5148 spec_die = die_specification (spec_die, &spec_cu);
5149 }
5150 }
5151
c906108c
SS
5152 new = pop_context ();
5153 /* Make a block for the local symbols within. */
801e3a5b
JB
5154 block = finish_block (new->name, &local_symbols, new->old_blocks,
5155 lowpc, highpc, objfile);
5156
df8a16a1 5157 /* For C++, set the block's scope. */
f55ee35c 5158 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 5159 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 5160 determine_prefix (die, cu),
df8a16a1
DJ
5161 processing_has_namespace_info);
5162
801e3a5b
JB
5163 /* If we have address ranges, record them. */
5164 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 5165
208d8187
JB
5166 /* In C++, we can have functions nested inside functions (e.g., when
5167 a function declares a class that has methods). This means that
5168 when we finish processing a function scope, we may need to go
5169 back to building a containing block's symbol lists. */
5170 local_symbols = new->locals;
5171 param_symbols = new->params;
27aa8d6a 5172 using_directives = new->using_directives;
208d8187 5173
921e78cf
JB
5174 /* If we've finished processing a top-level function, subsequent
5175 symbols go in the file symbol list. */
5176 if (outermost_context_p ())
e142c38c 5177 cu->list_in_scope = &file_symbols;
c906108c
SS
5178}
5179
5180/* Process all the DIES contained within a lexical block scope. Start
5181 a new scope, process the dies, and then close the scope. */
5182
5183static void
e7c27a73 5184read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5185{
e7c27a73 5186 struct objfile *objfile = cu->objfile;
52f0bd74 5187 struct context_stack *new;
c906108c
SS
5188 CORE_ADDR lowpc, highpc;
5189 struct die_info *child_die;
e142c38c
DJ
5190 CORE_ADDR baseaddr;
5191
5192 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
5193
5194 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
5195 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5196 as multiple lexical blocks? Handling children in a sane way would
6e70227d 5197 be nasty. Might be easier to properly extend generic blocks to
af34e669 5198 describe ranges. */
d85a05f0 5199 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
5200 return;
5201 lowpc += baseaddr;
5202 highpc += baseaddr;
5203
5204 push_context (0, lowpc);
639d11d3 5205 if (die->child != NULL)
c906108c 5206 {
639d11d3 5207 child_die = die->child;
c906108c
SS
5208 while (child_die && child_die->tag)
5209 {
e7c27a73 5210 process_die (child_die, cu);
c906108c
SS
5211 child_die = sibling_die (child_die);
5212 }
5213 }
5214 new = pop_context ();
5215
8540c487 5216 if (local_symbols != NULL || using_directives != NULL)
c906108c 5217 {
801e3a5b
JB
5218 struct block *block
5219 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5220 highpc, objfile);
5221
5222 /* Note that recording ranges after traversing children, as we
5223 do here, means that recording a parent's ranges entails
5224 walking across all its children's ranges as they appear in
5225 the address map, which is quadratic behavior.
5226
5227 It would be nicer to record the parent's ranges before
5228 traversing its children, simply overriding whatever you find
5229 there. But since we don't even decide whether to create a
5230 block until after we've traversed its children, that's hard
5231 to do. */
5232 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
5233 }
5234 local_symbols = new->locals;
27aa8d6a 5235 using_directives = new->using_directives;
c906108c
SS
5236}
5237
43039443 5238/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
5239 Return 1 if the attributes are present and valid, otherwise, return 0.
5240 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
5241
5242static int
5243dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
5244 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5245 struct partial_symtab *ranges_pst)
43039443
JK
5246{
5247 struct objfile *objfile = cu->objfile;
5248 struct comp_unit_head *cu_header = &cu->header;
5249 bfd *obfd = objfile->obfd;
5250 unsigned int addr_size = cu_header->addr_size;
5251 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5252 /* Base address selection entry. */
5253 CORE_ADDR base;
5254 int found_base;
5255 unsigned int dummy;
5256 gdb_byte *buffer;
5257 CORE_ADDR marker;
5258 int low_set;
5259 CORE_ADDR low = 0;
5260 CORE_ADDR high = 0;
ff013f42 5261 CORE_ADDR baseaddr;
43039443 5262
d00adf39
DE
5263 found_base = cu->base_known;
5264 base = cu->base_address;
43039443 5265
be391dca 5266 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 5267 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
5268 {
5269 complaint (&symfile_complaints,
5270 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5271 offset);
5272 return 0;
5273 }
dce234bc 5274 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
5275
5276 /* Read in the largest possible address. */
5277 marker = read_address (obfd, buffer, cu, &dummy);
5278 if ((marker & mask) == mask)
5279 {
5280 /* If we found the largest possible address, then
5281 read the base address. */
5282 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5283 buffer += 2 * addr_size;
5284 offset += 2 * addr_size;
5285 found_base = 1;
5286 }
5287
5288 low_set = 0;
5289
e7030f15 5290 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 5291
43039443
JK
5292 while (1)
5293 {
5294 CORE_ADDR range_beginning, range_end;
5295
5296 range_beginning = read_address (obfd, buffer, cu, &dummy);
5297 buffer += addr_size;
5298 range_end = read_address (obfd, buffer, cu, &dummy);
5299 buffer += addr_size;
5300 offset += 2 * addr_size;
5301
5302 /* An end of list marker is a pair of zero addresses. */
5303 if (range_beginning == 0 && range_end == 0)
5304 /* Found the end of list entry. */
5305 break;
5306
5307 /* Each base address selection entry is a pair of 2 values.
5308 The first is the largest possible address, the second is
5309 the base address. Check for a base address here. */
5310 if ((range_beginning & mask) == mask)
5311 {
5312 /* If we found the largest possible address, then
5313 read the base address. */
5314 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5315 found_base = 1;
5316 continue;
5317 }
5318
5319 if (!found_base)
5320 {
5321 /* We have no valid base address for the ranges
5322 data. */
5323 complaint (&symfile_complaints,
5324 _("Invalid .debug_ranges data (no base address)"));
5325 return 0;
5326 }
5327
5328 range_beginning += base;
5329 range_end += base;
5330
ff013f42
JK
5331 if (ranges_pst != NULL && range_beginning < range_end)
5332 addrmap_set_empty (objfile->psymtabs_addrmap,
5333 range_beginning + baseaddr, range_end - 1 + baseaddr,
5334 ranges_pst);
5335
43039443
JK
5336 /* FIXME: This is recording everything as a low-high
5337 segment of consecutive addresses. We should have a
5338 data structure for discontiguous block ranges
5339 instead. */
5340 if (! low_set)
5341 {
5342 low = range_beginning;
5343 high = range_end;
5344 low_set = 1;
5345 }
5346 else
5347 {
5348 if (range_beginning < low)
5349 low = range_beginning;
5350 if (range_end > high)
5351 high = range_end;
5352 }
5353 }
5354
5355 if (! low_set)
5356 /* If the first entry is an end-of-list marker, the range
5357 describes an empty scope, i.e. no instructions. */
5358 return 0;
5359
5360 if (low_return)
5361 *low_return = low;
5362 if (high_return)
5363 *high_return = high;
5364 return 1;
5365}
5366
af34e669
DJ
5367/* Get low and high pc attributes from a die. Return 1 if the attributes
5368 are present and valid, otherwise, return 0. Return -1 if the range is
5369 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 5370static int
af34e669 5371dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
5372 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5373 struct partial_symtab *pst)
c906108c
SS
5374{
5375 struct attribute *attr;
af34e669
DJ
5376 CORE_ADDR low = 0;
5377 CORE_ADDR high = 0;
5378 int ret = 0;
c906108c 5379
e142c38c 5380 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 5381 if (attr)
af34e669
DJ
5382 {
5383 high = DW_ADDR (attr);
e142c38c 5384 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
5385 if (attr)
5386 low = DW_ADDR (attr);
5387 else
5388 /* Found high w/o low attribute. */
5389 return 0;
5390
5391 /* Found consecutive range of addresses. */
5392 ret = 1;
5393 }
c906108c 5394 else
af34e669 5395 {
e142c38c 5396 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
5397 if (attr != NULL)
5398 {
af34e669 5399 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 5400 .debug_ranges section. */
d85a05f0 5401 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 5402 return 0;
43039443 5403 /* Found discontinuous range of addresses. */
af34e669
DJ
5404 ret = -1;
5405 }
5406 }
c906108c
SS
5407
5408 if (high < low)
5409 return 0;
5410
5411 /* When using the GNU linker, .gnu.linkonce. sections are used to
5412 eliminate duplicate copies of functions and vtables and such.
5413 The linker will arbitrarily choose one and discard the others.
5414 The AT_*_pc values for such functions refer to local labels in
5415 these sections. If the section from that file was discarded, the
5416 labels are not in the output, so the relocs get a value of 0.
5417 If this is a discarded function, mark the pc bounds as invalid,
5418 so that GDB will ignore it. */
72dca2f5 5419 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
5420 return 0;
5421
5422 *lowpc = low;
5423 *highpc = high;
af34e669 5424 return ret;
c906108c
SS
5425}
5426
b084d499
JB
5427/* Assuming that DIE represents a subprogram DIE or a lexical block, get
5428 its low and high PC addresses. Do nothing if these addresses could not
5429 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5430 and HIGHPC to the high address if greater than HIGHPC. */
5431
5432static void
5433dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5434 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5435 struct dwarf2_cu *cu)
5436{
5437 CORE_ADDR low, high;
5438 struct die_info *child = die->child;
5439
d85a05f0 5440 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
5441 {
5442 *lowpc = min (*lowpc, low);
5443 *highpc = max (*highpc, high);
5444 }
5445
5446 /* If the language does not allow nested subprograms (either inside
5447 subprograms or lexical blocks), we're done. */
5448 if (cu->language != language_ada)
5449 return;
6e70227d 5450
b084d499
JB
5451 /* Check all the children of the given DIE. If it contains nested
5452 subprograms, then check their pc bounds. Likewise, we need to
5453 check lexical blocks as well, as they may also contain subprogram
5454 definitions. */
5455 while (child && child->tag)
5456 {
5457 if (child->tag == DW_TAG_subprogram
5458 || child->tag == DW_TAG_lexical_block)
5459 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5460 child = sibling_die (child);
5461 }
5462}
5463
fae299cd
DC
5464/* Get the low and high pc's represented by the scope DIE, and store
5465 them in *LOWPC and *HIGHPC. If the correct values can't be
5466 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5467
5468static void
5469get_scope_pc_bounds (struct die_info *die,
5470 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5471 struct dwarf2_cu *cu)
5472{
5473 CORE_ADDR best_low = (CORE_ADDR) -1;
5474 CORE_ADDR best_high = (CORE_ADDR) 0;
5475 CORE_ADDR current_low, current_high;
5476
d85a05f0 5477 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
5478 {
5479 best_low = current_low;
5480 best_high = current_high;
5481 }
5482 else
5483 {
5484 struct die_info *child = die->child;
5485
5486 while (child && child->tag)
5487 {
5488 switch (child->tag) {
5489 case DW_TAG_subprogram:
b084d499 5490 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
5491 break;
5492 case DW_TAG_namespace:
f55ee35c 5493 case DW_TAG_module:
fae299cd
DC
5494 /* FIXME: carlton/2004-01-16: Should we do this for
5495 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5496 that current GCC's always emit the DIEs corresponding
5497 to definitions of methods of classes as children of a
5498 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5499 the DIEs giving the declarations, which could be
5500 anywhere). But I don't see any reason why the
5501 standards says that they have to be there. */
5502 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5503
5504 if (current_low != ((CORE_ADDR) -1))
5505 {
5506 best_low = min (best_low, current_low);
5507 best_high = max (best_high, current_high);
5508 }
5509 break;
5510 default:
5511 /* Ignore. */
5512 break;
5513 }
5514
5515 child = sibling_die (child);
5516 }
5517 }
5518
5519 *lowpc = best_low;
5520 *highpc = best_high;
5521}
5522
801e3a5b
JB
5523/* Record the address ranges for BLOCK, offset by BASEADDR, as given
5524 in DIE. */
5525static void
5526dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5527 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5528{
5529 struct attribute *attr;
5530
5531 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5532 if (attr)
5533 {
5534 CORE_ADDR high = DW_ADDR (attr);
9a619af0 5535
801e3a5b
JB
5536 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5537 if (attr)
5538 {
5539 CORE_ADDR low = DW_ADDR (attr);
9a619af0 5540
801e3a5b
JB
5541 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5542 }
5543 }
5544
5545 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5546 if (attr)
5547 {
5548 bfd *obfd = cu->objfile->obfd;
5549
5550 /* The value of the DW_AT_ranges attribute is the offset of the
5551 address range list in the .debug_ranges section. */
5552 unsigned long offset = DW_UNSND (attr);
dce234bc 5553 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
5554
5555 /* For some target architectures, but not others, the
5556 read_address function sign-extends the addresses it returns.
5557 To recognize base address selection entries, we need a
5558 mask. */
5559 unsigned int addr_size = cu->header.addr_size;
5560 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5561
5562 /* The base address, to which the next pair is relative. Note
5563 that this 'base' is a DWARF concept: most entries in a range
5564 list are relative, to reduce the number of relocs against the
5565 debugging information. This is separate from this function's
5566 'baseaddr' argument, which GDB uses to relocate debugging
5567 information from a shared library based on the address at
5568 which the library was loaded. */
d00adf39
DE
5569 CORE_ADDR base = cu->base_address;
5570 int base_known = cu->base_known;
801e3a5b 5571
be391dca 5572 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 5573 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
5574 {
5575 complaint (&symfile_complaints,
5576 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5577 offset);
5578 return;
5579 }
5580
5581 for (;;)
5582 {
5583 unsigned int bytes_read;
5584 CORE_ADDR start, end;
5585
5586 start = read_address (obfd, buffer, cu, &bytes_read);
5587 buffer += bytes_read;
5588 end = read_address (obfd, buffer, cu, &bytes_read);
5589 buffer += bytes_read;
5590
5591 /* Did we find the end of the range list? */
5592 if (start == 0 && end == 0)
5593 break;
5594
5595 /* Did we find a base address selection entry? */
5596 else if ((start & base_select_mask) == base_select_mask)
5597 {
5598 base = end;
5599 base_known = 1;
5600 }
5601
5602 /* We found an ordinary address range. */
5603 else
5604 {
5605 if (!base_known)
5606 {
5607 complaint (&symfile_complaints,
5608 _("Invalid .debug_ranges data (no base address)"));
5609 return;
5610 }
5611
6e70227d
DE
5612 record_block_range (block,
5613 baseaddr + base + start,
801e3a5b
JB
5614 baseaddr + base + end - 1);
5615 }
5616 }
5617 }
5618}
5619
c906108c
SS
5620/* Add an aggregate field to the field list. */
5621
5622static void
107d2387 5623dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 5624 struct dwarf2_cu *cu)
6e70227d 5625{
e7c27a73 5626 struct objfile *objfile = cu->objfile;
5e2b427d 5627 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
5628 struct nextfield *new_field;
5629 struct attribute *attr;
5630 struct field *fp;
5631 char *fieldname = "";
5632
5633 /* Allocate a new field list entry and link it in. */
5634 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 5635 make_cleanup (xfree, new_field);
c906108c 5636 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
5637
5638 if (die->tag == DW_TAG_inheritance)
5639 {
5640 new_field->next = fip->baseclasses;
5641 fip->baseclasses = new_field;
5642 }
5643 else
5644 {
5645 new_field->next = fip->fields;
5646 fip->fields = new_field;
5647 }
c906108c
SS
5648 fip->nfields++;
5649
5650 /* Handle accessibility and virtuality of field.
5651 The default accessibility for members is public, the default
5652 accessibility for inheritance is private. */
5653 if (die->tag != DW_TAG_inheritance)
5654 new_field->accessibility = DW_ACCESS_public;
5655 else
5656 new_field->accessibility = DW_ACCESS_private;
5657 new_field->virtuality = DW_VIRTUALITY_none;
5658
e142c38c 5659 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
5660 if (attr)
5661 new_field->accessibility = DW_UNSND (attr);
5662 if (new_field->accessibility != DW_ACCESS_public)
5663 fip->non_public_fields = 1;
e142c38c 5664 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
5665 if (attr)
5666 new_field->virtuality = DW_UNSND (attr);
5667
5668 fp = &new_field->field;
a9a9bd0f 5669
e142c38c 5670 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 5671 {
a9a9bd0f 5672 /* Data member other than a C++ static data member. */
6e70227d 5673
c906108c 5674 /* Get type of field. */
e7c27a73 5675 fp->type = die_type (die, cu);
c906108c 5676
d6a843b5 5677 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 5678
c906108c 5679 /* Get bit size of field (zero if none). */
e142c38c 5680 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
5681 if (attr)
5682 {
5683 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
5684 }
5685 else
5686 {
5687 FIELD_BITSIZE (*fp) = 0;
5688 }
5689
5690 /* Get bit offset of field. */
e142c38c 5691 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
5692 if (attr)
5693 {
d4b96c9a 5694 int byte_offset = 0;
c6a0999f 5695
3690dd37 5696 if (attr_form_is_section_offset (attr))
d4b96c9a 5697 dwarf2_complex_location_expr_complaint ();
3690dd37 5698 else if (attr_form_is_constant (attr))
c6a0999f 5699 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
d4b96c9a 5700 else if (attr_form_is_block (attr))
c6a0999f 5701 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
d4b96c9a
JK
5702 else
5703 dwarf2_complex_location_expr_complaint ();
c6a0999f 5704
d6a843b5 5705 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 5706 }
e142c38c 5707 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
5708 if (attr)
5709 {
5e2b427d 5710 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
5711 {
5712 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
5713 additional bit offset from the MSB of the containing
5714 anonymous object to the MSB of the field. We don't
5715 have to do anything special since we don't need to
5716 know the size of the anonymous object. */
c906108c
SS
5717 FIELD_BITPOS (*fp) += DW_UNSND (attr);
5718 }
5719 else
5720 {
5721 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
5722 MSB of the anonymous object, subtract off the number of
5723 bits from the MSB of the field to the MSB of the
5724 object, and then subtract off the number of bits of
5725 the field itself. The result is the bit offset of
5726 the LSB of the field. */
c906108c
SS
5727 int anonymous_size;
5728 int bit_offset = DW_UNSND (attr);
5729
e142c38c 5730 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
5731 if (attr)
5732 {
5733 /* The size of the anonymous object containing
5734 the bit field is explicit, so use the
5735 indicated size (in bytes). */
5736 anonymous_size = DW_UNSND (attr);
5737 }
5738 else
5739 {
5740 /* The size of the anonymous object containing
5741 the bit field must be inferred from the type
5742 attribute of the data member containing the
5743 bit field. */
5744 anonymous_size = TYPE_LENGTH (fp->type);
5745 }
5746 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
5747 - bit_offset - FIELD_BITSIZE (*fp);
5748 }
5749 }
5750
5751 /* Get name of field. */
39cbfefa
DJ
5752 fieldname = dwarf2_name (die, cu);
5753 if (fieldname == NULL)
5754 fieldname = "";
d8151005
DJ
5755
5756 /* The name is already allocated along with this objfile, so we don't
5757 need to duplicate it for the type. */
5758 fp->name = fieldname;
c906108c
SS
5759
5760 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 5761 pointer or virtual base class pointer) to private. */
e142c38c 5762 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 5763 {
d48cc9dd 5764 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
5765 new_field->accessibility = DW_ACCESS_private;
5766 fip->non_public_fields = 1;
5767 }
5768 }
a9a9bd0f 5769 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 5770 {
a9a9bd0f
DC
5771 /* C++ static member. */
5772
5773 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
5774 is a declaration, but all versions of G++ as of this writing
5775 (so through at least 3.2.1) incorrectly generate
5776 DW_TAG_variable tags. */
6e70227d 5777
c906108c 5778 char *physname;
c906108c 5779
a9a9bd0f 5780 /* Get name of field. */
39cbfefa
DJ
5781 fieldname = dwarf2_name (die, cu);
5782 if (fieldname == NULL)
c906108c
SS
5783 return;
5784
254e6b9e 5785 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
5786 if (attr
5787 /* Only create a symbol if this is an external value.
5788 new_symbol checks this and puts the value in the global symbol
5789 table, which we want. If it is not external, new_symbol
5790 will try to put the value in cu->list_in_scope which is wrong. */
5791 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
5792 {
5793 /* A static const member, not much different than an enum as far as
5794 we're concerned, except that we can support more types. */
5795 new_symbol (die, NULL, cu);
5796 }
5797
2df3850c 5798 /* Get physical name. */
94af9270 5799 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c 5800
d8151005
DJ
5801 /* The name is already allocated along with this objfile, so we don't
5802 need to duplicate it for the type. */
5803 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 5804 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 5805 FIELD_NAME (*fp) = fieldname;
c906108c
SS
5806 }
5807 else if (die->tag == DW_TAG_inheritance)
5808 {
5809 /* C++ base class field. */
e142c38c 5810 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 5811 if (attr)
d4b96c9a
JK
5812 {
5813 int byte_offset = 0;
5814
5815 if (attr_form_is_section_offset (attr))
5816 dwarf2_complex_location_expr_complaint ();
5817 else if (attr_form_is_constant (attr))
5818 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5819 else if (attr_form_is_block (attr))
5820 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5821 else
5822 dwarf2_complex_location_expr_complaint ();
5823
5824 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
5825 }
c906108c 5826 FIELD_BITSIZE (*fp) = 0;
e7c27a73 5827 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
5828 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
5829 fip->nbaseclasses++;
5830 }
5831}
5832
98751a41
JK
5833/* Add a typedef defined in the scope of the FIP's class. */
5834
5835static void
5836dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
5837 struct dwarf2_cu *cu)
6e70227d 5838{
98751a41
JK
5839 struct objfile *objfile = cu->objfile;
5840 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5841 struct typedef_field_list *new_field;
5842 struct attribute *attr;
5843 struct typedef_field *fp;
5844 char *fieldname = "";
5845
5846 /* Allocate a new field list entry and link it in. */
5847 new_field = xzalloc (sizeof (*new_field));
5848 make_cleanup (xfree, new_field);
5849
5850 gdb_assert (die->tag == DW_TAG_typedef);
5851
5852 fp = &new_field->field;
5853
5854 /* Get name of field. */
5855 fp->name = dwarf2_name (die, cu);
5856 if (fp->name == NULL)
5857 return;
5858
5859 fp->type = read_type_die (die, cu);
5860
5861 new_field->next = fip->typedef_field_list;
5862 fip->typedef_field_list = new_field;
5863 fip->typedef_field_list_count++;
5864}
5865
c906108c
SS
5866/* Create the vector of fields, and attach it to the type. */
5867
5868static void
fba45db2 5869dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 5870 struct dwarf2_cu *cu)
c906108c
SS
5871{
5872 int nfields = fip->nfields;
5873
5874 /* Record the field count, allocate space for the array of fields,
5875 and create blank accessibility bitfields if necessary. */
5876 TYPE_NFIELDS (type) = nfields;
5877 TYPE_FIELDS (type) = (struct field *)
5878 TYPE_ALLOC (type, sizeof (struct field) * nfields);
5879 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
5880
b4ba55a1 5881 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
5882 {
5883 ALLOCATE_CPLUS_STRUCT_TYPE (type);
5884
5885 TYPE_FIELD_PRIVATE_BITS (type) =
5886 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5887 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
5888
5889 TYPE_FIELD_PROTECTED_BITS (type) =
5890 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5891 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
5892
5893 TYPE_FIELD_IGNORE_BITS (type) =
5894 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5895 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
5896 }
5897
5898 /* If the type has baseclasses, allocate and clear a bit vector for
5899 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 5900 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
5901 {
5902 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 5903 unsigned char *pointer;
c906108c
SS
5904
5905 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
5906 pointer = TYPE_ALLOC (type, num_bytes);
5907 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
5908 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
5909 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
5910 }
5911
5912 /* Copy the saved-up fields into the field vector. Start from the head
5913 of the list, adding to the tail of the field array, so that they end
5914 up in the same order in the array in which they were added to the list. */
5915 while (nfields-- > 0)
5916 {
7d0ccb61
DJ
5917 struct nextfield *fieldp;
5918
5919 if (fip->fields)
5920 {
5921 fieldp = fip->fields;
5922 fip->fields = fieldp->next;
5923 }
5924 else
5925 {
5926 fieldp = fip->baseclasses;
5927 fip->baseclasses = fieldp->next;
5928 }
5929
5930 TYPE_FIELD (type, nfields) = fieldp->field;
5931 switch (fieldp->accessibility)
c906108c 5932 {
c5aa993b 5933 case DW_ACCESS_private:
b4ba55a1
JB
5934 if (cu->language != language_ada)
5935 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 5936 break;
c906108c 5937
c5aa993b 5938 case DW_ACCESS_protected:
b4ba55a1
JB
5939 if (cu->language != language_ada)
5940 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 5941 break;
c906108c 5942
c5aa993b
JM
5943 case DW_ACCESS_public:
5944 break;
c906108c 5945
c5aa993b
JM
5946 default:
5947 /* Unknown accessibility. Complain and treat it as public. */
5948 {
e2e0b3e5 5949 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 5950 fieldp->accessibility);
c5aa993b
JM
5951 }
5952 break;
c906108c
SS
5953 }
5954 if (nfields < fip->nbaseclasses)
5955 {
7d0ccb61 5956 switch (fieldp->virtuality)
c906108c 5957 {
c5aa993b
JM
5958 case DW_VIRTUALITY_virtual:
5959 case DW_VIRTUALITY_pure_virtual:
b4ba55a1
JB
5960 if (cu->language == language_ada)
5961 error ("unexpected virtuality in component of Ada type");
c5aa993b
JM
5962 SET_TYPE_FIELD_VIRTUAL (type, nfields);
5963 break;
c906108c
SS
5964 }
5965 }
c906108c
SS
5966 }
5967}
5968
c906108c
SS
5969/* Add a member function to the proper fieldlist. */
5970
5971static void
107d2387 5972dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 5973 struct type *type, struct dwarf2_cu *cu)
c906108c 5974{
e7c27a73 5975 struct objfile *objfile = cu->objfile;
c906108c
SS
5976 struct attribute *attr;
5977 struct fnfieldlist *flp;
5978 int i;
5979 struct fn_field *fnp;
5980 char *fieldname;
5981 char *physname;
5982 struct nextfnfield *new_fnfield;
f792889a 5983 struct type *this_type;
c906108c 5984
b4ba55a1
JB
5985 if (cu->language == language_ada)
5986 error ("unexpected member function in Ada type");
5987
2df3850c 5988 /* Get name of member function. */
39cbfefa
DJ
5989 fieldname = dwarf2_name (die, cu);
5990 if (fieldname == NULL)
2df3850c 5991 return;
c906108c 5992
2df3850c 5993 /* Get the mangled name. */
94af9270 5994 physname = (char *) dwarf2_physname (fieldname, die, cu);
c906108c
SS
5995
5996 /* Look up member function name in fieldlist. */
5997 for (i = 0; i < fip->nfnfields; i++)
5998 {
27bfe10e 5999 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
6000 break;
6001 }
6002
6003 /* Create new list element if necessary. */
6004 if (i < fip->nfnfields)
6005 flp = &fip->fnfieldlists[i];
6006 else
6007 {
6008 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6009 {
6010 fip->fnfieldlists = (struct fnfieldlist *)
6011 xrealloc (fip->fnfieldlists,
6012 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6013 * sizeof (struct fnfieldlist));
c906108c 6014 if (fip->nfnfields == 0)
c13c43fd 6015 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
6016 }
6017 flp = &fip->fnfieldlists[fip->nfnfields];
6018 flp->name = fieldname;
6019 flp->length = 0;
6020 flp->head = NULL;
6021 fip->nfnfields++;
6022 }
6023
6024 /* Create a new member function field and chain it to the field list
6025 entry. */
6026 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 6027 make_cleanup (xfree, new_fnfield);
c906108c
SS
6028 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6029 new_fnfield->next = flp->head;
6030 flp->head = new_fnfield;
6031 flp->length++;
6032
6033 /* Fill in the member function field info. */
6034 fnp = &new_fnfield->fnfield;
d8151005
DJ
6035 /* The name is already allocated along with this objfile, so we don't
6036 need to duplicate it for the type. */
6037 fnp->physname = physname ? physname : "";
c906108c 6038 fnp->type = alloc_type (objfile);
f792889a
DJ
6039 this_type = read_type_die (die, cu);
6040 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 6041 {
f792889a 6042 int nparams = TYPE_NFIELDS (this_type);
c906108c 6043
f792889a 6044 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
6045 of the method itself (TYPE_CODE_METHOD). */
6046 smash_to_method_type (fnp->type, type,
f792889a
DJ
6047 TYPE_TARGET_TYPE (this_type),
6048 TYPE_FIELDS (this_type),
6049 TYPE_NFIELDS (this_type),
6050 TYPE_VARARGS (this_type));
c906108c
SS
6051
6052 /* Handle static member functions.
c5aa993b
JM
6053 Dwarf2 has no clean way to discern C++ static and non-static
6054 member functions. G++ helps GDB by marking the first
6055 parameter for non-static member functions (which is the
6056 this pointer) as artificial. We obtain this information
6057 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 6058 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
6059 fnp->voffset = VOFFSET_STATIC;
6060 }
6061 else
e2e0b3e5 6062 complaint (&symfile_complaints, _("member function type missing for '%s'"),
4d3c2250 6063 physname);
c906108c
SS
6064
6065 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 6066 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 6067 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
6068
6069 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6070 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6071
6072 /* Get accessibility. */
e142c38c 6073 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
6074 if (attr)
6075 {
6076 switch (DW_UNSND (attr))
6077 {
c5aa993b
JM
6078 case DW_ACCESS_private:
6079 fnp->is_private = 1;
6080 break;
6081 case DW_ACCESS_protected:
6082 fnp->is_protected = 1;
6083 break;
c906108c
SS
6084 }
6085 }
6086
b02dede2 6087 /* Check for artificial methods. */
e142c38c 6088 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
6089 if (attr && DW_UNSND (attr) != 0)
6090 fnp->is_artificial = 1;
6091
0d564a31 6092 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
6093 function. For older versions of GCC, this is an offset in the
6094 appropriate virtual table, as specified by DW_AT_containing_type.
6095 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
6096 to the object address. */
6097
e142c38c 6098 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 6099 if (attr)
8e19ed76 6100 {
aec5aa8b 6101 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 6102 {
aec5aa8b
TT
6103 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6104 {
6105 /* Old-style GCC. */
6106 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6107 }
6108 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6109 || (DW_BLOCK (attr)->size > 1
6110 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6111 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6112 {
6113 struct dwarf_block blk;
6114 int offset;
6115
6116 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6117 ? 1 : 2);
6118 blk.size = DW_BLOCK (attr)->size - offset;
6119 blk.data = DW_BLOCK (attr)->data + offset;
6120 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6121 if ((fnp->voffset % cu->header.addr_size) != 0)
6122 dwarf2_complex_location_expr_complaint ();
6123 else
6124 fnp->voffset /= cu->header.addr_size;
6125 fnp->voffset += 2;
6126 }
6127 else
6128 dwarf2_complex_location_expr_complaint ();
6129
6130 if (!fnp->fcontext)
6131 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6132 }
3690dd37 6133 else if (attr_form_is_section_offset (attr))
8e19ed76 6134 {
4d3c2250 6135 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6136 }
6137 else
6138 {
4d3c2250
KB
6139 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6140 fieldname);
8e19ed76 6141 }
0d564a31 6142 }
d48cc9dd
DJ
6143 else
6144 {
6145 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6146 if (attr && DW_UNSND (attr))
6147 {
6148 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6149 complaint (&symfile_complaints,
6150 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6151 fieldname, die->offset);
9655fd1a 6152 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
6153 TYPE_CPLUS_DYNAMIC (type) = 1;
6154 }
6155 }
c906108c
SS
6156}
6157
6158/* Create the vector of member function fields, and attach it to the type. */
6159
6160static void
fba45db2 6161dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 6162 struct dwarf2_cu *cu)
c906108c
SS
6163{
6164 struct fnfieldlist *flp;
6165 int total_length = 0;
6166 int i;
6167
b4ba55a1
JB
6168 if (cu->language == language_ada)
6169 error ("unexpected member functions in Ada type");
6170
c906108c
SS
6171 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6172 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6173 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6174
6175 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6176 {
6177 struct nextfnfield *nfp = flp->head;
6178 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6179 int k;
6180
6181 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6182 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6183 fn_flp->fn_fields = (struct fn_field *)
6184 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6185 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 6186 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
6187
6188 total_length += flp->length;
6189 }
6190
6191 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6192 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6193}
6194
1168df01
JB
6195/* Returns non-zero if NAME is the name of a vtable member in CU's
6196 language, zero otherwise. */
6197static int
6198is_vtable_name (const char *name, struct dwarf2_cu *cu)
6199{
6200 static const char vptr[] = "_vptr";
987504bb 6201 static const char vtable[] = "vtable";
1168df01 6202
987504bb
JJ
6203 /* Look for the C++ and Java forms of the vtable. */
6204 if ((cu->language == language_java
6205 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6206 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6207 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
6208 return 1;
6209
6210 return 0;
6211}
6212
c0dd20ea 6213/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
6214 functions, with the ABI-specified layout. If TYPE describes
6215 such a structure, smash it into a member function type.
61049d3b
DJ
6216
6217 GCC shouldn't do this; it should just output pointer to member DIEs.
6218 This is GCC PR debug/28767. */
c0dd20ea 6219
0b92b5bb
TT
6220static void
6221quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 6222{
0b92b5bb 6223 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
6224
6225 /* Check for a structure with no name and two children. */
0b92b5bb
TT
6226 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6227 return;
c0dd20ea
DJ
6228
6229 /* Check for __pfn and __delta members. */
0b92b5bb
TT
6230 if (TYPE_FIELD_NAME (type, 0) == NULL
6231 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6232 || TYPE_FIELD_NAME (type, 1) == NULL
6233 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6234 return;
c0dd20ea
DJ
6235
6236 /* Find the type of the method. */
0b92b5bb 6237 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
6238 if (pfn_type == NULL
6239 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6240 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 6241 return;
c0dd20ea
DJ
6242
6243 /* Look for the "this" argument. */
6244 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6245 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 6246 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 6247 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 6248 return;
c0dd20ea
DJ
6249
6250 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
6251 new_type = alloc_type (objfile);
6252 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
6253 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6254 TYPE_VARARGS (pfn_type));
0b92b5bb 6255 smash_to_methodptr_type (type, new_type);
c0dd20ea 6256}
1168df01 6257
c906108c
SS
6258/* Called when we find the DIE that starts a structure or union scope
6259 (definition) to process all dies that define the members of the
6260 structure or union.
6261
6262 NOTE: we need to call struct_type regardless of whether or not the
6263 DIE has an at_name attribute, since it might be an anonymous
6264 structure or union. This gets the type entered into our set of
6265 user defined types.
6266
6267 However, if the structure is incomplete (an opaque struct/union)
6268 then suppress creating a symbol table entry for it since gdb only
6269 wants to find the one with the complete definition. Note that if
6270 it is complete, we just call new_symbol, which does it's own
6271 checking about whether the struct/union is anonymous or not (and
6272 suppresses creating a symbol table entry itself). */
6273
f792889a 6274static struct type *
134d01f1 6275read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6276{
e7c27a73 6277 struct objfile *objfile = cu->objfile;
c906108c
SS
6278 struct type *type;
6279 struct attribute *attr;
39cbfefa 6280 char *name;
d3f41bb1 6281 struct cleanup *back_to;
c906108c 6282
348e048f
DE
6283 /* If the definition of this type lives in .debug_types, read that type.
6284 Don't follow DW_AT_specification though, that will take us back up
6285 the chain and we want to go down. */
6286 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6287 if (attr)
6288 {
6289 struct dwarf2_cu *type_cu = cu;
6290 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6291
348e048f
DE
6292 /* We could just recurse on read_structure_type, but we need to call
6293 get_die_type to ensure only one type for this DIE is created.
6294 This is important, for example, because for c++ classes we need
6295 TYPE_NAME set which is only done by new_symbol. Blech. */
6296 type = read_type_die (type_die, type_cu);
6297 return set_die_type (die, type, cu);
6298 }
6299
d3f41bb1
TT
6300 back_to = make_cleanup (null_cleanup, 0);
6301
c0dd20ea 6302 type = alloc_type (objfile);
c906108c 6303 INIT_CPLUS_SPECIFIC (type);
93311388 6304
39cbfefa
DJ
6305 name = dwarf2_name (die, cu);
6306 if (name != NULL)
c906108c 6307 {
987504bb
JJ
6308 if (cu->language == language_cplus
6309 || cu->language == language_java)
63d06c5c 6310 {
94af9270
KS
6311 TYPE_TAG_NAME (type) = (char *) dwarf2_full_name (name, die, cu);
6312 if (die->tag == DW_TAG_structure_type
6313 || die->tag == DW_TAG_class_type)
6314 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
6315 }
6316 else
6317 {
d8151005
DJ
6318 /* The name is already allocated along with this objfile, so
6319 we don't need to duplicate it for the type. */
94af9270
KS
6320 TYPE_TAG_NAME (type) = (char *) name;
6321 if (die->tag == DW_TAG_class_type)
6322 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 6323 }
c906108c
SS
6324 }
6325
6326 if (die->tag == DW_TAG_structure_type)
6327 {
6328 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6329 }
6330 else if (die->tag == DW_TAG_union_type)
6331 {
6332 TYPE_CODE (type) = TYPE_CODE_UNION;
6333 }
6334 else
6335 {
c906108c
SS
6336 TYPE_CODE (type) = TYPE_CODE_CLASS;
6337 }
6338
0cc2414c
TT
6339 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6340 TYPE_DECLARED_CLASS (type) = 1;
6341
e142c38c 6342 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6343 if (attr)
6344 {
6345 TYPE_LENGTH (type) = DW_UNSND (attr);
6346 }
6347 else
6348 {
6349 TYPE_LENGTH (type) = 0;
6350 }
6351
876cecd0 6352 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 6353 if (die_is_declaration (die, cu))
876cecd0 6354 TYPE_STUB (type) = 1;
a6c727b2
DJ
6355 else if (attr == NULL && die->child == NULL
6356 && producer_is_realview (cu->producer))
6357 /* RealView does not output the required DW_AT_declaration
6358 on incomplete types. */
6359 TYPE_STUB (type) = 1;
dc718098 6360
c906108c
SS
6361 /* We need to add the type field to the die immediately so we don't
6362 infinitely recurse when dealing with pointers to the structure
6363 type within the structure itself. */
1c379e20 6364 set_die_type (die, type, cu);
c906108c 6365
7e314c57
JK
6366 /* set_die_type should be already done. */
6367 set_descriptive_type (type, die, cu);
6368
e142c38c 6369 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
6370 {
6371 struct field_info fi;
6372 struct die_info *child_die;
c906108c
SS
6373
6374 memset (&fi, 0, sizeof (struct field_info));
6375
639d11d3 6376 child_die = die->child;
c906108c
SS
6377
6378 while (child_die && child_die->tag)
6379 {
a9a9bd0f
DC
6380 if (child_die->tag == DW_TAG_member
6381 || child_die->tag == DW_TAG_variable)
c906108c 6382 {
a9a9bd0f
DC
6383 /* NOTE: carlton/2002-11-05: A C++ static data member
6384 should be a DW_TAG_member that is a declaration, but
6385 all versions of G++ as of this writing (so through at
6386 least 3.2.1) incorrectly generate DW_TAG_variable
6387 tags for them instead. */
e7c27a73 6388 dwarf2_add_field (&fi, child_die, cu);
c906108c 6389 }
8713b1b1 6390 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
6391 {
6392 /* C++ member function. */
e7c27a73 6393 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
6394 }
6395 else if (child_die->tag == DW_TAG_inheritance)
6396 {
6397 /* C++ base class field. */
e7c27a73 6398 dwarf2_add_field (&fi, child_die, cu);
c906108c 6399 }
98751a41
JK
6400 else if (child_die->tag == DW_TAG_typedef)
6401 dwarf2_add_typedef (&fi, child_die, cu);
c906108c
SS
6402 child_die = sibling_die (child_die);
6403 }
6404
6405 /* Attach fields and member functions to the type. */
6406 if (fi.nfields)
e7c27a73 6407 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
6408 if (fi.nfnfields)
6409 {
e7c27a73 6410 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 6411
c5aa993b 6412 /* Get the type which refers to the base class (possibly this
c906108c 6413 class itself) which contains the vtable pointer for the current
0d564a31
DJ
6414 class from the DW_AT_containing_type attribute. This use of
6415 DW_AT_containing_type is a GNU extension. */
c906108c 6416
e142c38c 6417 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 6418 {
e7c27a73 6419 struct type *t = die_containing_type (die, cu);
c906108c
SS
6420
6421 TYPE_VPTR_BASETYPE (type) = t;
6422 if (type == t)
6423 {
c906108c
SS
6424 int i;
6425
6426 /* Our own class provides vtbl ptr. */
6427 for (i = TYPE_NFIELDS (t) - 1;
6428 i >= TYPE_N_BASECLASSES (t);
6429 --i)
6430 {
6431 char *fieldname = TYPE_FIELD_NAME (t, i);
6432
1168df01 6433 if (is_vtable_name (fieldname, cu))
c906108c
SS
6434 {
6435 TYPE_VPTR_FIELDNO (type) = i;
6436 break;
6437 }
6438 }
6439
6440 /* Complain if virtual function table field not found. */
6441 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 6442 complaint (&symfile_complaints,
e2e0b3e5 6443 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
6444 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6445 "");
c906108c
SS
6446 }
6447 else
6448 {
6449 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6450 }
6451 }
f6235d4c
EZ
6452 else if (cu->producer
6453 && strncmp (cu->producer,
6454 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6455 {
6456 /* The IBM XLC compiler does not provide direct indication
6457 of the containing type, but the vtable pointer is
6458 always named __vfp. */
6459
6460 int i;
6461
6462 for (i = TYPE_NFIELDS (type) - 1;
6463 i >= TYPE_N_BASECLASSES (type);
6464 --i)
6465 {
6466 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6467 {
6468 TYPE_VPTR_FIELDNO (type) = i;
6469 TYPE_VPTR_BASETYPE (type) = type;
6470 break;
6471 }
6472 }
6473 }
c906108c 6474 }
98751a41
JK
6475
6476 /* Copy fi.typedef_field_list linked list elements content into the
6477 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6478 if (fi.typedef_field_list)
6479 {
6480 int i = fi.typedef_field_list_count;
6481
a0d7a4ff 6482 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
6483 TYPE_TYPEDEF_FIELD_ARRAY (type)
6484 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6485 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6486
6487 /* Reverse the list order to keep the debug info elements order. */
6488 while (--i >= 0)
6489 {
6490 struct typedef_field *dest, *src;
6e70227d 6491
98751a41
JK
6492 dest = &TYPE_TYPEDEF_FIELD (type, i);
6493 src = &fi.typedef_field_list->field;
6494 fi.typedef_field_list = fi.typedef_field_list->next;
6495 *dest = *src;
6496 }
6497 }
c906108c 6498 }
63d06c5c 6499
0b92b5bb
TT
6500 quirk_gcc_member_function_pointer (type, cu->objfile);
6501
0114d602 6502 do_cleanups (back_to);
f792889a 6503 return type;
c906108c
SS
6504}
6505
134d01f1
DJ
6506static void
6507process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6508{
90aeadfc 6509 struct die_info *child_die = die->child;
f792889a 6510 struct type *this_type;
c906108c 6511
f792889a
DJ
6512 this_type = get_die_type (die, cu);
6513 if (this_type == NULL)
6514 this_type = read_structure_type (die, cu);
c906108c 6515
90aeadfc
DC
6516 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6517 snapshots) has been known to create a die giving a declaration
6518 for a class that has, as a child, a die giving a definition for a
6519 nested class. So we have to process our children even if the
6520 current die is a declaration. Normally, of course, a declaration
6521 won't have any children at all. */
134d01f1 6522
90aeadfc
DC
6523 while (child_die != NULL && child_die->tag)
6524 {
6525 if (child_die->tag == DW_TAG_member
6526 || child_die->tag == DW_TAG_variable
6527 || child_die->tag == DW_TAG_inheritance)
134d01f1 6528 {
90aeadfc 6529 /* Do nothing. */
134d01f1 6530 }
90aeadfc
DC
6531 else
6532 process_die (child_die, cu);
134d01f1 6533
90aeadfc 6534 child_die = sibling_die (child_die);
134d01f1
DJ
6535 }
6536
fa4028e9
JB
6537 /* Do not consider external references. According to the DWARF standard,
6538 these DIEs are identified by the fact that they have no byte_size
6539 attribute, and a declaration attribute. */
6540 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6541 || !die_is_declaration (die, cu))
f792889a 6542 new_symbol (die, this_type, cu);
134d01f1
DJ
6543}
6544
6545/* Given a DW_AT_enumeration_type die, set its type. We do not
6546 complete the type's fields yet, or create any symbols. */
c906108c 6547
f792889a 6548static struct type *
134d01f1 6549read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6550{
e7c27a73 6551 struct objfile *objfile = cu->objfile;
c906108c 6552 struct type *type;
c906108c 6553 struct attribute *attr;
0114d602 6554 const char *name;
134d01f1 6555
348e048f
DE
6556 /* If the definition of this type lives in .debug_types, read that type.
6557 Don't follow DW_AT_specification though, that will take us back up
6558 the chain and we want to go down. */
6559 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6560 if (attr)
6561 {
6562 struct dwarf2_cu *type_cu = cu;
6563 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 6564
348e048f
DE
6565 type = read_type_die (type_die, type_cu);
6566 return set_die_type (die, type, cu);
6567 }
6568
c906108c
SS
6569 type = alloc_type (objfile);
6570
6571 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 6572 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 6573 if (name != NULL)
0114d602 6574 TYPE_TAG_NAME (type) = (char *) name;
c906108c 6575
e142c38c 6576 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
6577 if (attr)
6578 {
6579 TYPE_LENGTH (type) = DW_UNSND (attr);
6580 }
6581 else
6582 {
6583 TYPE_LENGTH (type) = 0;
6584 }
6585
137033e9
JB
6586 /* The enumeration DIE can be incomplete. In Ada, any type can be
6587 declared as private in the package spec, and then defined only
6588 inside the package body. Such types are known as Taft Amendment
6589 Types. When another package uses such a type, an incomplete DIE
6590 may be generated by the compiler. */
02eb380e 6591 if (die_is_declaration (die, cu))
876cecd0 6592 TYPE_STUB (type) = 1;
02eb380e 6593
f792889a 6594 return set_die_type (die, type, cu);
134d01f1
DJ
6595}
6596
6597/* Given a pointer to a die which begins an enumeration, process all
6598 the dies that define the members of the enumeration, and create the
6599 symbol for the enumeration type.
6600
6601 NOTE: We reverse the order of the element list. */
6602
6603static void
6604process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
6605{
134d01f1
DJ
6606 struct die_info *child_die;
6607 struct field *fields;
134d01f1
DJ
6608 struct symbol *sym;
6609 int num_fields;
6610 int unsigned_enum = 1;
39cbfefa 6611 char *name;
f792889a 6612 struct type *this_type;
134d01f1 6613
c906108c
SS
6614 num_fields = 0;
6615 fields = NULL;
f792889a
DJ
6616 this_type = get_die_type (die, cu);
6617 if (this_type == NULL)
6618 this_type = read_enumeration_type (die, cu);
639d11d3 6619 if (die->child != NULL)
c906108c 6620 {
639d11d3 6621 child_die = die->child;
c906108c
SS
6622 while (child_die && child_die->tag)
6623 {
6624 if (child_die->tag != DW_TAG_enumerator)
6625 {
e7c27a73 6626 process_die (child_die, cu);
c906108c
SS
6627 }
6628 else
6629 {
39cbfefa
DJ
6630 name = dwarf2_name (child_die, cu);
6631 if (name)
c906108c 6632 {
f792889a 6633 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
6634 if (SYMBOL_VALUE (sym) < 0)
6635 unsigned_enum = 0;
6636
6637 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
6638 {
6639 fields = (struct field *)
6640 xrealloc (fields,
6641 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 6642 * sizeof (struct field));
c906108c
SS
6643 }
6644
3567439c 6645 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 6646 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 6647 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
6648 FIELD_BITSIZE (fields[num_fields]) = 0;
6649
6650 num_fields++;
6651 }
6652 }
6653
6654 child_die = sibling_die (child_die);
6655 }
6656
6657 if (num_fields)
6658 {
f792889a
DJ
6659 TYPE_NFIELDS (this_type) = num_fields;
6660 TYPE_FIELDS (this_type) = (struct field *)
6661 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
6662 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 6663 sizeof (struct field) * num_fields);
b8c9b27d 6664 xfree (fields);
c906108c
SS
6665 }
6666 if (unsigned_enum)
876cecd0 6667 TYPE_UNSIGNED (this_type) = 1;
c906108c 6668 }
134d01f1 6669
f792889a 6670 new_symbol (die, this_type, cu);
c906108c
SS
6671}
6672
6673/* Extract all information from a DW_TAG_array_type DIE and put it in
6674 the DIE's type field. For now, this only handles one dimensional
6675 arrays. */
6676
f792889a 6677static struct type *
e7c27a73 6678read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6679{
e7c27a73 6680 struct objfile *objfile = cu->objfile;
c906108c 6681 struct die_info *child_die;
7e314c57 6682 struct type *type;
c906108c
SS
6683 struct type *element_type, *range_type, *index_type;
6684 struct type **range_types = NULL;
6685 struct attribute *attr;
6686 int ndim = 0;
6687 struct cleanup *back_to;
39cbfefa 6688 char *name;
c906108c 6689
e7c27a73 6690 element_type = die_type (die, cu);
c906108c 6691
7e314c57
JK
6692 /* The die_type call above may have already set the type for this DIE. */
6693 type = get_die_type (die, cu);
6694 if (type)
6695 return type;
6696
c906108c
SS
6697 /* Irix 6.2 native cc creates array types without children for
6698 arrays with unspecified length. */
639d11d3 6699 if (die->child == NULL)
c906108c 6700 {
46bf5051 6701 index_type = objfile_type (objfile)->builtin_int;
c906108c 6702 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
6703 type = create_array_type (NULL, element_type, range_type);
6704 return set_die_type (die, type, cu);
c906108c
SS
6705 }
6706
6707 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 6708 child_die = die->child;
c906108c
SS
6709 while (child_die && child_die->tag)
6710 {
6711 if (child_die->tag == DW_TAG_subrange_type)
6712 {
f792889a 6713 struct type *child_type = read_type_die (child_die, cu);
9a619af0 6714
f792889a 6715 if (child_type != NULL)
a02abb62
JB
6716 {
6717 /* The range type was succesfully read. Save it for
6718 the array type creation. */
6719 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
6720 {
6721 range_types = (struct type **)
6722 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
6723 * sizeof (struct type *));
6724 if (ndim == 0)
6725 make_cleanup (free_current_contents, &range_types);
6726 }
f792889a 6727 range_types[ndim++] = child_type;
a02abb62 6728 }
c906108c
SS
6729 }
6730 child_die = sibling_die (child_die);
6731 }
6732
6733 /* Dwarf2 dimensions are output from left to right, create the
6734 necessary array types in backwards order. */
7ca2d3a3 6735
c906108c 6736 type = element_type;
7ca2d3a3
DL
6737
6738 if (read_array_order (die, cu) == DW_ORD_col_major)
6739 {
6740 int i = 0;
9a619af0 6741
7ca2d3a3
DL
6742 while (i < ndim)
6743 type = create_array_type (NULL, type, range_types[i++]);
6744 }
6745 else
6746 {
6747 while (ndim-- > 0)
6748 type = create_array_type (NULL, type, range_types[ndim]);
6749 }
c906108c 6750
f5f8a009
EZ
6751 /* Understand Dwarf2 support for vector types (like they occur on
6752 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
6753 array type. This is not part of the Dwarf2/3 standard yet, but a
6754 custom vendor extension. The main difference between a regular
6755 array and the vector variant is that vectors are passed by value
6756 to functions. */
e142c38c 6757 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 6758 if (attr)
ea37ba09 6759 make_vector_type (type);
f5f8a009 6760
39cbfefa
DJ
6761 name = dwarf2_name (die, cu);
6762 if (name)
6763 TYPE_NAME (type) = name;
6e70227d 6764
7e314c57
JK
6765 /* Install the type in the die. */
6766 set_die_type (die, type, cu);
6767
6768 /* set_die_type should be already done. */
b4ba55a1
JB
6769 set_descriptive_type (type, die, cu);
6770
c906108c
SS
6771 do_cleanups (back_to);
6772
7e314c57 6773 return type;
c906108c
SS
6774}
6775
7ca2d3a3 6776static enum dwarf_array_dim_ordering
6e70227d 6777read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
6778{
6779 struct attribute *attr;
6780
6781 attr = dwarf2_attr (die, DW_AT_ordering, cu);
6782
6783 if (attr) return DW_SND (attr);
6784
6785 /*
6786 GNU F77 is a special case, as at 08/2004 array type info is the
6e70227d 6787 opposite order to the dwarf2 specification, but data is still
7ca2d3a3
DL
6788 laid out as per normal fortran.
6789
6e70227d 6790 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7ca2d3a3
DL
6791 version checking.
6792 */
6793
905e0470
PM
6794 if (cu->language == language_fortran
6795 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
6796 {
6797 return DW_ORD_row_major;
6798 }
6799
6e70227d 6800 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
6801 {
6802 case array_column_major:
6803 return DW_ORD_col_major;
6804 case array_row_major:
6805 default:
6806 return DW_ORD_row_major;
6807 };
6808}
6809
72019c9c
GM
6810/* Extract all information from a DW_TAG_set_type DIE and put it in
6811 the DIE's type field. */
6812
f792889a 6813static struct type *
72019c9c
GM
6814read_set_type (struct die_info *die, struct dwarf2_cu *cu)
6815{
7e314c57
JK
6816 struct type *domain_type, *set_type;
6817 struct attribute *attr;
f792889a 6818
7e314c57
JK
6819 domain_type = die_type (die, cu);
6820
6821 /* The die_type call above may have already set the type for this DIE. */
6822 set_type = get_die_type (die, cu);
6823 if (set_type)
6824 return set_type;
6825
6826 set_type = create_set_type (NULL, domain_type);
6827
6828 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
6829 if (attr)
6830 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 6831
f792889a 6832 return set_die_type (die, set_type, cu);
72019c9c 6833}
7ca2d3a3 6834
c906108c
SS
6835/* First cut: install each common block member as a global variable. */
6836
6837static void
e7c27a73 6838read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
6839{
6840 struct die_info *child_die;
6841 struct attribute *attr;
6842 struct symbol *sym;
6843 CORE_ADDR base = (CORE_ADDR) 0;
6844
e142c38c 6845 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
6846 if (attr)
6847 {
8e19ed76
PS
6848 /* Support the .debug_loc offsets */
6849 if (attr_form_is_block (attr))
6850 {
e7c27a73 6851 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 6852 }
3690dd37 6853 else if (attr_form_is_section_offset (attr))
8e19ed76 6854 {
4d3c2250 6855 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6856 }
6857 else
6858 {
4d3c2250
KB
6859 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6860 "common block member");
8e19ed76 6861 }
c906108c 6862 }
639d11d3 6863 if (die->child != NULL)
c906108c 6864 {
639d11d3 6865 child_die = die->child;
c906108c
SS
6866 while (child_die && child_die->tag)
6867 {
e7c27a73 6868 sym = new_symbol (child_die, NULL, cu);
e142c38c 6869 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
6870 if (attr)
6871 {
d4b96c9a
JK
6872 CORE_ADDR byte_offset = 0;
6873
6874 if (attr_form_is_section_offset (attr))
6875 dwarf2_complex_location_expr_complaint ();
6876 else if (attr_form_is_constant (attr))
6877 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6878 else if (attr_form_is_block (attr))
6879 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6880 else
6881 dwarf2_complex_location_expr_complaint ();
6882
6883 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
c906108c
SS
6884 add_symbol_to_list (sym, &global_symbols);
6885 }
6886 child_die = sibling_die (child_die);
6887 }
6888 }
6889}
6890
0114d602 6891/* Create a type for a C++ namespace. */
d9fa45fe 6892
0114d602
DJ
6893static struct type *
6894read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 6895{
e7c27a73 6896 struct objfile *objfile = cu->objfile;
0114d602 6897 const char *previous_prefix, *name;
9219021c 6898 int is_anonymous;
0114d602
DJ
6899 struct type *type;
6900
6901 /* For extensions, reuse the type of the original namespace. */
6902 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
6903 {
6904 struct die_info *ext_die;
6905 struct dwarf2_cu *ext_cu = cu;
9a619af0 6906
0114d602
DJ
6907 ext_die = dwarf2_extension (die, &ext_cu);
6908 type = read_type_die (ext_die, ext_cu);
6909 return set_die_type (die, type, cu);
6910 }
9219021c 6911
e142c38c 6912 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
6913
6914 /* Now build the name of the current namespace. */
6915
0114d602
DJ
6916 previous_prefix = determine_prefix (die, cu);
6917 if (previous_prefix[0] != '\0')
6918 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 6919 previous_prefix, name, 0, cu);
0114d602
DJ
6920
6921 /* Create the type. */
6922 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
6923 objfile);
6924 TYPE_NAME (type) = (char *) name;
6925 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6926
60531b24 6927 return set_die_type (die, type, cu);
0114d602
DJ
6928}
6929
6930/* Read a C++ namespace. */
6931
6932static void
6933read_namespace (struct die_info *die, struct dwarf2_cu *cu)
6934{
6935 struct objfile *objfile = cu->objfile;
6936 const char *name;
6937 int is_anonymous;
9219021c 6938
5c4e30ca
DC
6939 /* Add a symbol associated to this if we haven't seen the namespace
6940 before. Also, add a using directive if it's an anonymous
6941 namespace. */
9219021c 6942
f2f0e013 6943 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
6944 {
6945 struct type *type;
6946
0114d602 6947 type = read_type_die (die, cu);
e7c27a73 6948 new_symbol (die, type, cu);
5c4e30ca 6949
0114d602 6950 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 6951 if (is_anonymous)
0114d602
DJ
6952 {
6953 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 6954
c0cc3a76 6955 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13387711 6956 NULL, &objfile->objfile_obstack);
0114d602 6957 }
5c4e30ca 6958 }
9219021c 6959
639d11d3 6960 if (die->child != NULL)
d9fa45fe 6961 {
639d11d3 6962 struct die_info *child_die = die->child;
6e70227d 6963
d9fa45fe
DC
6964 while (child_die && child_die->tag)
6965 {
e7c27a73 6966 process_die (child_die, cu);
d9fa45fe
DC
6967 child_die = sibling_die (child_die);
6968 }
6969 }
38d518c9
EZ
6970}
6971
f55ee35c
JK
6972/* Read a Fortran module as type. This DIE can be only a declaration used for
6973 imported module. Still we need that type as local Fortran "use ... only"
6974 declaration imports depend on the created type in determine_prefix. */
6975
6976static struct type *
6977read_module_type (struct die_info *die, struct dwarf2_cu *cu)
6978{
6979 struct objfile *objfile = cu->objfile;
6980 char *module_name;
6981 struct type *type;
6982
6983 module_name = dwarf2_name (die, cu);
6984 if (!module_name)
6985 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
6986 die->offset);
6987 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
6988
6989 /* determine_prefix uses TYPE_TAG_NAME. */
6990 TYPE_TAG_NAME (type) = TYPE_NAME (type);
6991
6992 return set_die_type (die, type, cu);
6993}
6994
5d7cb8df
JK
6995/* Read a Fortran module. */
6996
6997static void
6998read_module (struct die_info *die, struct dwarf2_cu *cu)
6999{
7000 struct die_info *child_die = die->child;
7001
5d7cb8df
JK
7002 while (child_die && child_die->tag)
7003 {
7004 process_die (child_die, cu);
7005 child_die = sibling_die (child_die);
7006 }
7007}
7008
38d518c9
EZ
7009/* Return the name of the namespace represented by DIE. Set
7010 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7011 namespace. */
7012
7013static const char *
e142c38c 7014namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
7015{
7016 struct die_info *current_die;
7017 const char *name = NULL;
7018
7019 /* Loop through the extensions until we find a name. */
7020
7021 for (current_die = die;
7022 current_die != NULL;
f2f0e013 7023 current_die = dwarf2_extension (die, &cu))
38d518c9 7024 {
e142c38c 7025 name = dwarf2_name (current_die, cu);
38d518c9
EZ
7026 if (name != NULL)
7027 break;
7028 }
7029
7030 /* Is it an anonymous namespace? */
7031
7032 *is_anonymous = (name == NULL);
7033 if (*is_anonymous)
7034 name = "(anonymous namespace)";
7035
7036 return name;
d9fa45fe
DC
7037}
7038
c906108c
SS
7039/* Extract all information from a DW_TAG_pointer_type DIE and add to
7040 the user defined type vector. */
7041
f792889a 7042static struct type *
e7c27a73 7043read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7044{
5e2b427d 7045 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 7046 struct comp_unit_head *cu_header = &cu->header;
c906108c 7047 struct type *type;
8b2dbe47
KB
7048 struct attribute *attr_byte_size;
7049 struct attribute *attr_address_class;
7050 int byte_size, addr_class;
7e314c57
JK
7051 struct type *target_type;
7052
7053 target_type = die_type (die, cu);
c906108c 7054
7e314c57
JK
7055 /* The die_type call above may have already set the type for this DIE. */
7056 type = get_die_type (die, cu);
7057 if (type)
7058 return type;
7059
7060 type = lookup_pointer_type (target_type);
8b2dbe47 7061
e142c38c 7062 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
7063 if (attr_byte_size)
7064 byte_size = DW_UNSND (attr_byte_size);
c906108c 7065 else
8b2dbe47
KB
7066 byte_size = cu_header->addr_size;
7067
e142c38c 7068 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
7069 if (attr_address_class)
7070 addr_class = DW_UNSND (attr_address_class);
7071 else
7072 addr_class = DW_ADDR_none;
7073
7074 /* If the pointer size or address class is different than the
7075 default, create a type variant marked as such and set the
7076 length accordingly. */
7077 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 7078 {
5e2b427d 7079 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
7080 {
7081 int type_flags;
7082
849957d9 7083 type_flags = gdbarch_address_class_type_flags
5e2b427d 7084 (gdbarch, byte_size, addr_class);
876cecd0
TT
7085 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7086 == 0);
8b2dbe47
KB
7087 type = make_type_with_address_space (type, type_flags);
7088 }
7089 else if (TYPE_LENGTH (type) != byte_size)
7090 {
e2e0b3e5 7091 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47 7092 }
6e70227d 7093 else
9a619af0
MS
7094 {
7095 /* Should we also complain about unhandled address classes? */
7096 }
c906108c 7097 }
8b2dbe47
KB
7098
7099 TYPE_LENGTH (type) = byte_size;
f792889a 7100 return set_die_type (die, type, cu);
c906108c
SS
7101}
7102
7103/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7104 the user defined type vector. */
7105
f792889a 7106static struct type *
e7c27a73 7107read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7108{
7109 struct type *type;
7110 struct type *to_type;
7111 struct type *domain;
7112
e7c27a73
DJ
7113 to_type = die_type (die, cu);
7114 domain = die_containing_type (die, cu);
0d5de010 7115
7e314c57
JK
7116 /* The calls above may have already set the type for this DIE. */
7117 type = get_die_type (die, cu);
7118 if (type)
7119 return type;
7120
0d5de010
DJ
7121 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7122 type = lookup_methodptr_type (to_type);
7123 else
7124 type = lookup_memberptr_type (to_type, domain);
c906108c 7125
f792889a 7126 return set_die_type (die, type, cu);
c906108c
SS
7127}
7128
7129/* Extract all information from a DW_TAG_reference_type DIE and add to
7130 the user defined type vector. */
7131
f792889a 7132static struct type *
e7c27a73 7133read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7134{
e7c27a73 7135 struct comp_unit_head *cu_header = &cu->header;
7e314c57 7136 struct type *type, *target_type;
c906108c
SS
7137 struct attribute *attr;
7138
7e314c57
JK
7139 target_type = die_type (die, cu);
7140
7141 /* The die_type call above may have already set the type for this DIE. */
7142 type = get_die_type (die, cu);
7143 if (type)
7144 return type;
7145
7146 type = lookup_reference_type (target_type);
e142c38c 7147 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7148 if (attr)
7149 {
7150 TYPE_LENGTH (type) = DW_UNSND (attr);
7151 }
7152 else
7153 {
107d2387 7154 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 7155 }
f792889a 7156 return set_die_type (die, type, cu);
c906108c
SS
7157}
7158
f792889a 7159static struct type *
e7c27a73 7160read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7161{
f792889a 7162 struct type *base_type, *cv_type;
c906108c 7163
e7c27a73 7164 base_type = die_type (die, cu);
7e314c57
JK
7165
7166 /* The die_type call above may have already set the type for this DIE. */
7167 cv_type = get_die_type (die, cu);
7168 if (cv_type)
7169 return cv_type;
7170
f792889a
DJ
7171 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7172 return set_die_type (die, cv_type, cu);
c906108c
SS
7173}
7174
f792889a 7175static struct type *
e7c27a73 7176read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7177{
f792889a 7178 struct type *base_type, *cv_type;
c906108c 7179
e7c27a73 7180 base_type = die_type (die, cu);
7e314c57
JK
7181
7182 /* The die_type call above may have already set the type for this DIE. */
7183 cv_type = get_die_type (die, cu);
7184 if (cv_type)
7185 return cv_type;
7186
f792889a
DJ
7187 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7188 return set_die_type (die, cv_type, cu);
c906108c
SS
7189}
7190
7191/* Extract all information from a DW_TAG_string_type DIE and add to
7192 the user defined type vector. It isn't really a user defined type,
7193 but it behaves like one, with other DIE's using an AT_user_def_type
7194 attribute to reference it. */
7195
f792889a 7196static struct type *
e7c27a73 7197read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7198{
e7c27a73 7199 struct objfile *objfile = cu->objfile;
3b7538c0 7200 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7201 struct type *type, *range_type, *index_type, *char_type;
7202 struct attribute *attr;
7203 unsigned int length;
7204
e142c38c 7205 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
7206 if (attr)
7207 {
7208 length = DW_UNSND (attr);
7209 }
7210 else
7211 {
b21b22e0 7212 /* check for the DW_AT_byte_size attribute */
e142c38c 7213 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
7214 if (attr)
7215 {
7216 length = DW_UNSND (attr);
7217 }
7218 else
7219 {
7220 length = 1;
7221 }
c906108c 7222 }
6ccb9162 7223
46bf5051 7224 index_type = objfile_type (objfile)->builtin_int;
c906108c 7225 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
7226 char_type = language_string_char_type (cu->language_defn, gdbarch);
7227 type = create_string_type (NULL, char_type, range_type);
6ccb9162 7228
f792889a 7229 return set_die_type (die, type, cu);
c906108c
SS
7230}
7231
7232/* Handle DIES due to C code like:
7233
7234 struct foo
c5aa993b
JM
7235 {
7236 int (*funcp)(int a, long l);
7237 int b;
7238 };
c906108c
SS
7239
7240 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 7241 */
c906108c 7242
f792889a 7243static struct type *
e7c27a73 7244read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7245{
7246 struct type *type; /* Type that this function returns */
7247 struct type *ftype; /* Function that returns above type */
7248 struct attribute *attr;
7249
e7c27a73 7250 type = die_type (die, cu);
7e314c57
JK
7251
7252 /* The die_type call above may have already set the type for this DIE. */
7253 ftype = get_die_type (die, cu);
7254 if (ftype)
7255 return ftype;
7256
0c8b41f1 7257 ftype = lookup_function_type (type);
c906108c 7258
5b8101ae 7259 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 7260 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 7261 if ((attr && (DW_UNSND (attr) != 0))
987504bb 7262 || cu->language == language_cplus
5b8101ae
PM
7263 || cu->language == language_java
7264 || cu->language == language_pascal)
876cecd0 7265 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
7266 else if (producer_is_realview (cu->producer))
7267 /* RealView does not emit DW_AT_prototyped. We can not
7268 distinguish prototyped and unprototyped functions; default to
7269 prototyped, since that is more common in modern code (and
7270 RealView warns about unprototyped functions). */
7271 TYPE_PROTOTYPED (ftype) = 1;
c906108c 7272
c055b101
CV
7273 /* Store the calling convention in the type if it's available in
7274 the subroutine die. Otherwise set the calling convention to
7275 the default value DW_CC_normal. */
7276 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7277 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
76c10ea2
GM
7278
7279 /* We need to add the subroutine type to the die immediately so
7280 we don't infinitely recurse when dealing with parameters
7281 declared as the same subroutine type. */
7282 set_die_type (die, ftype, cu);
6e70227d 7283
639d11d3 7284 if (die->child != NULL)
c906108c 7285 {
8072405b 7286 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
c906108c 7287 struct die_info *child_die;
8072405b 7288 int nparams, iparams;
c906108c
SS
7289
7290 /* Count the number of parameters.
7291 FIXME: GDB currently ignores vararg functions, but knows about
7292 vararg member functions. */
8072405b 7293 nparams = 0;
639d11d3 7294 child_die = die->child;
c906108c
SS
7295 while (child_die && child_die->tag)
7296 {
7297 if (child_die->tag == DW_TAG_formal_parameter)
7298 nparams++;
7299 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 7300 TYPE_VARARGS (ftype) = 1;
c906108c
SS
7301 child_die = sibling_die (child_die);
7302 }
7303
7304 /* Allocate storage for parameters and fill them in. */
7305 TYPE_NFIELDS (ftype) = nparams;
7306 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 7307 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 7308
8072405b
JK
7309 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7310 even if we error out during the parameters reading below. */
7311 for (iparams = 0; iparams < nparams; iparams++)
7312 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7313
7314 iparams = 0;
639d11d3 7315 child_die = die->child;
c906108c
SS
7316 while (child_die && child_die->tag)
7317 {
7318 if (child_die->tag == DW_TAG_formal_parameter)
7319 {
7320 /* Dwarf2 has no clean way to discern C++ static and non-static
c5aa993b
JM
7321 member functions. G++ helps GDB by marking the first
7322 parameter for non-static member functions (which is the
7323 this pointer) as artificial. We pass this information
7324 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
e142c38c 7325 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
7326 if (attr)
7327 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7328 else
418835cc
KS
7329 {
7330 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7331
7332 /* GCC/43521: In java, the formal parameter
7333 "this" is sometimes not marked with DW_AT_artificial. */
7334 if (cu->language == language_java)
7335 {
7336 const char *name = dwarf2_name (child_die, cu);
9a619af0 7337
418835cc
KS
7338 if (name && !strcmp (name, "this"))
7339 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7340 }
7341 }
e7c27a73 7342 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
c906108c
SS
7343 iparams++;
7344 }
7345 child_die = sibling_die (child_die);
7346 }
7347 }
7348
76c10ea2 7349 return ftype;
c906108c
SS
7350}
7351
f792889a 7352static struct type *
e7c27a73 7353read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7354{
e7c27a73 7355 struct objfile *objfile = cu->objfile;
0114d602 7356 const char *name = NULL;
f792889a 7357 struct type *this_type;
c906108c 7358
94af9270 7359 name = dwarf2_full_name (NULL, die, cu);
f792889a 7360 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
7361 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7362 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
7363 set_die_type (die, this_type, cu);
7364 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7365 return this_type;
c906108c
SS
7366}
7367
7368/* Find a representation of a given base type and install
7369 it in the TYPE field of the die. */
7370
f792889a 7371static struct type *
e7c27a73 7372read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7373{
e7c27a73 7374 struct objfile *objfile = cu->objfile;
c906108c
SS
7375 struct type *type;
7376 struct attribute *attr;
7377 int encoding = 0, size = 0;
39cbfefa 7378 char *name;
6ccb9162
UW
7379 enum type_code code = TYPE_CODE_INT;
7380 int type_flags = 0;
7381 struct type *target_type = NULL;
c906108c 7382
e142c38c 7383 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
7384 if (attr)
7385 {
7386 encoding = DW_UNSND (attr);
7387 }
e142c38c 7388 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
7389 if (attr)
7390 {
7391 size = DW_UNSND (attr);
7392 }
39cbfefa 7393 name = dwarf2_name (die, cu);
6ccb9162 7394 if (!name)
c906108c 7395 {
6ccb9162
UW
7396 complaint (&symfile_complaints,
7397 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 7398 }
6ccb9162
UW
7399
7400 switch (encoding)
c906108c 7401 {
6ccb9162
UW
7402 case DW_ATE_address:
7403 /* Turn DW_ATE_address into a void * pointer. */
7404 code = TYPE_CODE_PTR;
7405 type_flags |= TYPE_FLAG_UNSIGNED;
7406 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7407 break;
7408 case DW_ATE_boolean:
7409 code = TYPE_CODE_BOOL;
7410 type_flags |= TYPE_FLAG_UNSIGNED;
7411 break;
7412 case DW_ATE_complex_float:
7413 code = TYPE_CODE_COMPLEX;
7414 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7415 break;
7416 case DW_ATE_decimal_float:
7417 code = TYPE_CODE_DECFLOAT;
7418 break;
7419 case DW_ATE_float:
7420 code = TYPE_CODE_FLT;
7421 break;
7422 case DW_ATE_signed:
7423 break;
7424 case DW_ATE_unsigned:
7425 type_flags |= TYPE_FLAG_UNSIGNED;
7426 break;
7427 case DW_ATE_signed_char:
6e70227d 7428 if (cu->language == language_ada || cu->language == language_m2
868a0084 7429 || cu->language == language_pascal)
6ccb9162
UW
7430 code = TYPE_CODE_CHAR;
7431 break;
7432 case DW_ATE_unsigned_char:
868a0084
PM
7433 if (cu->language == language_ada || cu->language == language_m2
7434 || cu->language == language_pascal)
6ccb9162
UW
7435 code = TYPE_CODE_CHAR;
7436 type_flags |= TYPE_FLAG_UNSIGNED;
7437 break;
75079b2b
TT
7438 case DW_ATE_UTF:
7439 /* We just treat this as an integer and then recognize the
7440 type by name elsewhere. */
7441 break;
7442
6ccb9162
UW
7443 default:
7444 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7445 dwarf_type_encoding_name (encoding));
7446 break;
c906108c 7447 }
6ccb9162 7448
0114d602
DJ
7449 type = init_type (code, size, type_flags, NULL, objfile);
7450 TYPE_NAME (type) = name;
6ccb9162
UW
7451 TYPE_TARGET_TYPE (type) = target_type;
7452
0114d602 7453 if (name && strcmp (name, "char") == 0)
876cecd0 7454 TYPE_NOSIGN (type) = 1;
0114d602 7455
f792889a 7456 return set_die_type (die, type, cu);
c906108c
SS
7457}
7458
a02abb62
JB
7459/* Read the given DW_AT_subrange DIE. */
7460
f792889a 7461static struct type *
a02abb62
JB
7462read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7463{
5e2b427d 7464 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
7465 struct type *base_type;
7466 struct type *range_type;
7467 struct attribute *attr;
43bbcdc2
PH
7468 LONGEST low = 0;
7469 LONGEST high = -1;
39cbfefa 7470 char *name;
43bbcdc2 7471 LONGEST negative_mask;
e77813c8 7472
a02abb62 7473 base_type = die_type (die, cu);
953ac07e
JK
7474 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7475 check_typedef (base_type);
a02abb62 7476
7e314c57
JK
7477 /* The die_type call above may have already set the type for this DIE. */
7478 range_type = get_die_type (die, cu);
7479 if (range_type)
7480 return range_type;
7481
e142c38c 7482 if (cu->language == language_fortran)
6e70227d 7483 {
a02abb62
JB
7484 /* FORTRAN implies a lower bound of 1, if not given. */
7485 low = 1;
7486 }
7487
dd5e6932
DJ
7488 /* FIXME: For variable sized arrays either of these could be
7489 a variable rather than a constant value. We'll allow it,
7490 but we don't know how to handle it. */
e142c38c 7491 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
7492 if (attr)
7493 low = dwarf2_get_attr_constant_value (attr, 0);
7494
e142c38c 7495 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 7496 if (attr)
6e70227d 7497 {
e77813c8 7498 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
a02abb62
JB
7499 {
7500 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 7501 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
7502 FIXME: GDB does not yet know how to handle dynamic
7503 arrays properly, treat them as arrays with unspecified
7504 length for now.
7505
7506 FIXME: jimb/2003-09-22: GDB does not really know
7507 how to handle arrays of unspecified length
7508 either; we just represent them as zero-length
7509 arrays. Choose an appropriate upper bound given
7510 the lower bound we've computed above. */
7511 high = low - 1;
7512 }
7513 else
7514 high = dwarf2_get_attr_constant_value (attr, 1);
7515 }
e77813c8
PM
7516 else
7517 {
7518 attr = dwarf2_attr (die, DW_AT_count, cu);
7519 if (attr)
7520 {
7521 int count = dwarf2_get_attr_constant_value (attr, 1);
7522 high = low + count - 1;
7523 }
7524 }
7525
7526 /* Dwarf-2 specifications explicitly allows to create subrange types
7527 without specifying a base type.
7528 In that case, the base type must be set to the type of
7529 the lower bound, upper bound or count, in that order, if any of these
7530 three attributes references an object that has a type.
7531 If no base type is found, the Dwarf-2 specifications say that
7532 a signed integer type of size equal to the size of an address should
7533 be used.
7534 For the following C code: `extern char gdb_int [];'
7535 GCC produces an empty range DIE.
7536 FIXME: muller/2010-05-28: Possible references to object for low bound,
7537 high bound or count are not yet handled by this code.
7538 */
7539 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
7540 {
7541 struct objfile *objfile = cu->objfile;
7542 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7543 int addr_size = gdbarch_addr_bit (gdbarch) /8;
7544 struct type *int_type = objfile_type (objfile)->builtin_int;
7545
7546 /* Test "int", "long int", and "long long int" objfile types,
7547 and select the first one having a size above or equal to the
7548 architecture address size. */
7549 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7550 base_type = int_type;
7551 else
7552 {
7553 int_type = objfile_type (objfile)->builtin_long;
7554 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7555 base_type = int_type;
7556 else
7557 {
7558 int_type = objfile_type (objfile)->builtin_long_long;
7559 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7560 base_type = int_type;
7561 }
7562 }
7563 }
a02abb62 7564
6e70227d 7565 negative_mask =
43bbcdc2
PH
7566 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
7567 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
7568 low |= negative_mask;
7569 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
7570 high |= negative_mask;
7571
a02abb62
JB
7572 range_type = create_range_type (NULL, base_type, low, high);
7573
bbb0eef6
JK
7574 /* Mark arrays with dynamic length at least as an array of unspecified
7575 length. GDB could check the boundary but before it gets implemented at
7576 least allow accessing the array elements. */
7577 if (attr && attr->form == DW_FORM_block1)
7578 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
7579
39cbfefa
DJ
7580 name = dwarf2_name (die, cu);
7581 if (name)
7582 TYPE_NAME (range_type) = name;
6e70227d 7583
e142c38c 7584 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
7585 if (attr)
7586 TYPE_LENGTH (range_type) = DW_UNSND (attr);
7587
7e314c57
JK
7588 set_die_type (die, range_type, cu);
7589
7590 /* set_die_type should be already done. */
b4ba55a1
JB
7591 set_descriptive_type (range_type, die, cu);
7592
7e314c57 7593 return range_type;
a02abb62 7594}
6e70227d 7595
f792889a 7596static struct type *
81a17f79
JB
7597read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
7598{
7599 struct type *type;
81a17f79 7600
81a17f79
JB
7601 /* For now, we only support the C meaning of an unspecified type: void. */
7602
0114d602
DJ
7603 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
7604 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 7605
f792889a 7606 return set_die_type (die, type, cu);
81a17f79 7607}
a02abb62 7608
51545339
DJ
7609/* Trivial hash function for die_info: the hash value of a DIE
7610 is its offset in .debug_info for this objfile. */
7611
7612static hashval_t
7613die_hash (const void *item)
7614{
7615 const struct die_info *die = item;
9a619af0 7616
51545339
DJ
7617 return die->offset;
7618}
7619
7620/* Trivial comparison function for die_info structures: two DIEs
7621 are equal if they have the same offset. */
7622
7623static int
7624die_eq (const void *item_lhs, const void *item_rhs)
7625{
7626 const struct die_info *die_lhs = item_lhs;
7627 const struct die_info *die_rhs = item_rhs;
9a619af0 7628
51545339
DJ
7629 return die_lhs->offset == die_rhs->offset;
7630}
7631
c906108c
SS
7632/* Read a whole compilation unit into a linked list of dies. */
7633
f9aca02d 7634static struct die_info *
93311388 7635read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 7636{
93311388
DE
7637 struct die_reader_specs reader_specs;
7638
348e048f 7639 gdb_assert (cu->die_hash == NULL);
51545339
DJ
7640 cu->die_hash
7641 = htab_create_alloc_ex (cu->header.length / 12,
7642 die_hash,
7643 die_eq,
7644 NULL,
7645 &cu->comp_unit_obstack,
7646 hashtab_obstack_allocate,
7647 dummy_obstack_deallocate);
7648
93311388
DE
7649 init_cu_die_reader (&reader_specs, cu);
7650
7651 return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
639d11d3
DC
7652}
7653
d97bc12b
DE
7654/* Main entry point for reading a DIE and all children.
7655 Read the DIE and dump it if requested. */
7656
7657static struct die_info *
93311388
DE
7658read_die_and_children (const struct die_reader_specs *reader,
7659 gdb_byte *info_ptr,
d97bc12b
DE
7660 gdb_byte **new_info_ptr,
7661 struct die_info *parent)
7662{
93311388 7663 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
d97bc12b
DE
7664 new_info_ptr, parent);
7665
7666 if (dwarf2_die_debug)
7667 {
348e048f
DE
7668 fprintf_unfiltered (gdb_stdlog,
7669 "\nRead die from %s of %s:\n",
7670 reader->buffer == dwarf2_per_objfile->info.buffer
7671 ? ".debug_info"
7672 : reader->buffer == dwarf2_per_objfile->types.buffer
7673 ? ".debug_types"
7674 : "unknown section",
7675 reader->abfd->filename);
d97bc12b
DE
7676 dump_die (result, dwarf2_die_debug);
7677 }
7678
7679 return result;
7680}
7681
639d11d3
DC
7682/* Read a single die and all its descendents. Set the die's sibling
7683 field to NULL; set other fields in the die correctly, and set all
7684 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
7685 location of the info_ptr after reading all of those dies. PARENT
7686 is the parent of the die in question. */
7687
7688static struct die_info *
93311388
DE
7689read_die_and_children_1 (const struct die_reader_specs *reader,
7690 gdb_byte *info_ptr,
d97bc12b
DE
7691 gdb_byte **new_info_ptr,
7692 struct die_info *parent)
639d11d3
DC
7693{
7694 struct die_info *die;
fe1b8b76 7695 gdb_byte *cur_ptr;
639d11d3
DC
7696 int has_children;
7697
93311388 7698 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
7699 if (die == NULL)
7700 {
7701 *new_info_ptr = cur_ptr;
7702 return NULL;
7703 }
93311388 7704 store_in_ref_table (die, reader->cu);
639d11d3
DC
7705
7706 if (has_children)
348e048f 7707 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
7708 else
7709 {
7710 die->child = NULL;
7711 *new_info_ptr = cur_ptr;
7712 }
7713
7714 die->sibling = NULL;
7715 die->parent = parent;
7716 return die;
7717}
7718
7719/* Read a die, all of its descendents, and all of its siblings; set
7720 all of the fields of all of the dies correctly. Arguments are as
7721 in read_die_and_children. */
7722
7723static struct die_info *
93311388
DE
7724read_die_and_siblings (const struct die_reader_specs *reader,
7725 gdb_byte *info_ptr,
fe1b8b76 7726 gdb_byte **new_info_ptr,
639d11d3
DC
7727 struct die_info *parent)
7728{
7729 struct die_info *first_die, *last_sibling;
fe1b8b76 7730 gdb_byte *cur_ptr;
639d11d3 7731
c906108c 7732 cur_ptr = info_ptr;
639d11d3
DC
7733 first_die = last_sibling = NULL;
7734
7735 while (1)
c906108c 7736 {
639d11d3 7737 struct die_info *die
93311388 7738 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
639d11d3 7739
1d325ec1 7740 if (die == NULL)
c906108c 7741 {
639d11d3
DC
7742 *new_info_ptr = cur_ptr;
7743 return first_die;
c906108c 7744 }
1d325ec1
DJ
7745
7746 if (!first_die)
7747 first_die = die;
c906108c 7748 else
1d325ec1
DJ
7749 last_sibling->sibling = die;
7750
7751 last_sibling = die;
c906108c 7752 }
c906108c
SS
7753}
7754
93311388
DE
7755/* Read the die from the .debug_info section buffer. Set DIEP to
7756 point to a newly allocated die with its information, except for its
7757 child, sibling, and parent fields. Set HAS_CHILDREN to tell
7758 whether the die has children or not. */
7759
7760static gdb_byte *
7761read_full_die (const struct die_reader_specs *reader,
7762 struct die_info **diep, gdb_byte *info_ptr,
7763 int *has_children)
7764{
7765 unsigned int abbrev_number, bytes_read, i, offset;
7766 struct abbrev_info *abbrev;
7767 struct die_info *die;
7768 struct dwarf2_cu *cu = reader->cu;
7769 bfd *abfd = reader->abfd;
7770
7771 offset = info_ptr - reader->buffer;
7772 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7773 info_ptr += bytes_read;
7774 if (!abbrev_number)
7775 {
7776 *diep = NULL;
7777 *has_children = 0;
7778 return info_ptr;
7779 }
7780
7781 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
7782 if (!abbrev)
348e048f
DE
7783 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
7784 abbrev_number,
7785 bfd_get_filename (abfd));
7786
93311388
DE
7787 die = dwarf_alloc_die (cu, abbrev->num_attrs);
7788 die->offset = offset;
7789 die->tag = abbrev->tag;
7790 die->abbrev = abbrev_number;
7791
7792 die->num_attrs = abbrev->num_attrs;
7793
7794 for (i = 0; i < abbrev->num_attrs; ++i)
7795 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
7796 abfd, info_ptr, cu);
7797
7798 *diep = die;
7799 *has_children = abbrev->has_children;
7800 return info_ptr;
7801}
7802
c906108c
SS
7803/* In DWARF version 2, the description of the debugging information is
7804 stored in a separate .debug_abbrev section. Before we read any
7805 dies from a section we read in all abbreviations and install them
72bf9492
DJ
7806 in a hash table. This function also sets flags in CU describing
7807 the data found in the abbrev table. */
c906108c
SS
7808
7809static void
e7c27a73 7810dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 7811{
e7c27a73 7812 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 7813 gdb_byte *abbrev_ptr;
c906108c
SS
7814 struct abbrev_info *cur_abbrev;
7815 unsigned int abbrev_number, bytes_read, abbrev_name;
7816 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
7817 struct attr_abbrev *cur_attrs;
7818 unsigned int allocated_attrs;
c906108c 7819
57349743 7820 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
7821 obstack_init (&cu->abbrev_obstack);
7822 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
7823 (ABBREV_HASH_SIZE
7824 * sizeof (struct abbrev_info *)));
7825 memset (cu->dwarf2_abbrevs, 0,
7826 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 7827
be391dca
TT
7828 dwarf2_read_section (dwarf2_per_objfile->objfile,
7829 &dwarf2_per_objfile->abbrev);
dce234bc 7830 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
7831 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7832 abbrev_ptr += bytes_read;
7833
f3dd6933
DJ
7834 allocated_attrs = ATTR_ALLOC_CHUNK;
7835 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 7836
c906108c
SS
7837 /* loop until we reach an abbrev number of 0 */
7838 while (abbrev_number)
7839 {
f3dd6933 7840 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
7841
7842 /* read in abbrev header */
7843 cur_abbrev->number = abbrev_number;
7844 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7845 abbrev_ptr += bytes_read;
7846 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
7847 abbrev_ptr += 1;
7848
72bf9492
DJ
7849 if (cur_abbrev->tag == DW_TAG_namespace)
7850 cu->has_namespace_info = 1;
7851
c906108c
SS
7852 /* now read in declarations */
7853 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7854 abbrev_ptr += bytes_read;
7855 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7856 abbrev_ptr += bytes_read;
7857 while (abbrev_name)
7858 {
f3dd6933 7859 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 7860 {
f3dd6933
DJ
7861 allocated_attrs += ATTR_ALLOC_CHUNK;
7862 cur_attrs
7863 = xrealloc (cur_attrs, (allocated_attrs
7864 * sizeof (struct attr_abbrev)));
c906108c 7865 }
ae038cb0
DJ
7866
7867 /* Record whether this compilation unit might have
7868 inter-compilation-unit references. If we don't know what form
7869 this attribute will have, then it might potentially be a
7870 DW_FORM_ref_addr, so we conservatively expect inter-CU
7871 references. */
7872
7873 if (abbrev_form == DW_FORM_ref_addr
7874 || abbrev_form == DW_FORM_indirect)
7875 cu->has_form_ref_addr = 1;
7876
f3dd6933
DJ
7877 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
7878 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
7879 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7880 abbrev_ptr += bytes_read;
7881 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7882 abbrev_ptr += bytes_read;
7883 }
7884
f3dd6933
DJ
7885 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
7886 (cur_abbrev->num_attrs
7887 * sizeof (struct attr_abbrev)));
7888 memcpy (cur_abbrev->attrs, cur_attrs,
7889 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
7890
c906108c 7891 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
7892 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
7893 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
7894
7895 /* Get next abbreviation.
7896 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
7897 always properly terminated with an abbrev number of 0.
7898 Exit loop if we encounter an abbreviation which we have
7899 already read (which means we are about to read the abbreviations
7900 for the next compile unit) or if the end of the abbreviation
7901 table is reached. */
dce234bc
PP
7902 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
7903 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
7904 break;
7905 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7906 abbrev_ptr += bytes_read;
e7c27a73 7907 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
7908 break;
7909 }
f3dd6933
DJ
7910
7911 xfree (cur_attrs);
c906108c
SS
7912}
7913
f3dd6933 7914/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 7915
c906108c 7916static void
f3dd6933 7917dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 7918{
f3dd6933 7919 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 7920
f3dd6933
DJ
7921 obstack_free (&cu->abbrev_obstack, NULL);
7922 cu->dwarf2_abbrevs = NULL;
c906108c
SS
7923}
7924
7925/* Lookup an abbrev_info structure in the abbrev hash table. */
7926
7927static struct abbrev_info *
e7c27a73 7928dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
7929{
7930 unsigned int hash_number;
7931 struct abbrev_info *abbrev;
7932
7933 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 7934 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
7935
7936 while (abbrev)
7937 {
7938 if (abbrev->number == number)
7939 return abbrev;
7940 else
7941 abbrev = abbrev->next;
7942 }
7943 return NULL;
7944}
7945
72bf9492
DJ
7946/* Returns nonzero if TAG represents a type that we might generate a partial
7947 symbol for. */
7948
7949static int
7950is_type_tag_for_partial (int tag)
7951{
7952 switch (tag)
7953 {
7954#if 0
7955 /* Some types that would be reasonable to generate partial symbols for,
7956 that we don't at present. */
7957 case DW_TAG_array_type:
7958 case DW_TAG_file_type:
7959 case DW_TAG_ptr_to_member_type:
7960 case DW_TAG_set_type:
7961 case DW_TAG_string_type:
7962 case DW_TAG_subroutine_type:
7963#endif
7964 case DW_TAG_base_type:
7965 case DW_TAG_class_type:
680b30c7 7966 case DW_TAG_interface_type:
72bf9492
DJ
7967 case DW_TAG_enumeration_type:
7968 case DW_TAG_structure_type:
7969 case DW_TAG_subrange_type:
7970 case DW_TAG_typedef:
7971 case DW_TAG_union_type:
7972 return 1;
7973 default:
7974 return 0;
7975 }
7976}
7977
7978/* Load all DIEs that are interesting for partial symbols into memory. */
7979
7980static struct partial_die_info *
93311388
DE
7981load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
7982 int building_psymtab, struct dwarf2_cu *cu)
72bf9492
DJ
7983{
7984 struct partial_die_info *part_die;
7985 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
7986 struct abbrev_info *abbrev;
7987 unsigned int bytes_read;
5afb4e99 7988 unsigned int load_all = 0;
72bf9492
DJ
7989
7990 int nesting_level = 1;
7991
7992 parent_die = NULL;
7993 last_die = NULL;
7994
5afb4e99
DJ
7995 if (cu->per_cu && cu->per_cu->load_all_dies)
7996 load_all = 1;
7997
72bf9492
DJ
7998 cu->partial_dies
7999 = htab_create_alloc_ex (cu->header.length / 12,
8000 partial_die_hash,
8001 partial_die_eq,
8002 NULL,
8003 &cu->comp_unit_obstack,
8004 hashtab_obstack_allocate,
8005 dummy_obstack_deallocate);
8006
8007 part_die = obstack_alloc (&cu->comp_unit_obstack,
8008 sizeof (struct partial_die_info));
8009
8010 while (1)
8011 {
8012 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8013
8014 /* A NULL abbrev means the end of a series of children. */
8015 if (abbrev == NULL)
8016 {
8017 if (--nesting_level == 0)
8018 {
8019 /* PART_DIE was probably the last thing allocated on the
8020 comp_unit_obstack, so we could call obstack_free
8021 here. We don't do that because the waste is small,
8022 and will be cleaned up when we're done with this
8023 compilation unit. This way, we're also more robust
8024 against other users of the comp_unit_obstack. */
8025 return first_die;
8026 }
8027 info_ptr += bytes_read;
8028 last_die = parent_die;
8029 parent_die = parent_die->die_parent;
8030 continue;
8031 }
8032
5afb4e99
DJ
8033 /* Check whether this DIE is interesting enough to save. Normally
8034 we would not be interested in members here, but there may be
8035 later variables referencing them via DW_AT_specification (for
8036 static members). */
8037 if (!load_all
8038 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
8039 && abbrev->tag != DW_TAG_enumerator
8040 && abbrev->tag != DW_TAG_subprogram
bc30ff58 8041 && abbrev->tag != DW_TAG_lexical_block
72bf9492 8042 && abbrev->tag != DW_TAG_variable
5afb4e99 8043 && abbrev->tag != DW_TAG_namespace
f55ee35c 8044 && abbrev->tag != DW_TAG_module
5afb4e99 8045 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
8046 {
8047 /* Otherwise we skip to the next sibling, if any. */
93311388 8048 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
72bf9492
DJ
8049 continue;
8050 }
8051
93311388
DE
8052 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8053 buffer, info_ptr, cu);
72bf9492
DJ
8054
8055 /* This two-pass algorithm for processing partial symbols has a
8056 high cost in cache pressure. Thus, handle some simple cases
8057 here which cover the majority of C partial symbols. DIEs
8058 which neither have specification tags in them, nor could have
8059 specification tags elsewhere pointing at them, can simply be
8060 processed and discarded.
8061
8062 This segment is also optional; scan_partial_symbols and
8063 add_partial_symbol will handle these DIEs if we chain
8064 them in normally. When compilers which do not emit large
8065 quantities of duplicate debug information are more common,
8066 this code can probably be removed. */
8067
8068 /* Any complete simple types at the top level (pretty much all
8069 of them, for a language without namespaces), can be processed
8070 directly. */
8071 if (parent_die == NULL
8072 && part_die->has_specification == 0
8073 && part_die->is_declaration == 0
8074 && (part_die->tag == DW_TAG_typedef
8075 || part_die->tag == DW_TAG_base_type
8076 || part_die->tag == DW_TAG_subrange_type))
8077 {
8078 if (building_psymtab && part_die->name != NULL)
04a679b8 8079 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492
DJ
8080 VAR_DOMAIN, LOC_TYPEDEF,
8081 &cu->objfile->static_psymbols,
8082 0, (CORE_ADDR) 0, cu->language, cu->objfile);
93311388 8083 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8084 continue;
8085 }
8086
8087 /* If we're at the second level, and we're an enumerator, and
8088 our parent has no specification (meaning possibly lives in a
8089 namespace elsewhere), then we can add the partial symbol now
8090 instead of queueing it. */
8091 if (part_die->tag == DW_TAG_enumerator
8092 && parent_die != NULL
8093 && parent_die->die_parent == NULL
8094 && parent_die->tag == DW_TAG_enumeration_type
8095 && parent_die->has_specification == 0)
8096 {
8097 if (part_die->name == NULL)
e2e0b3e5 8098 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492 8099 else if (building_psymtab)
04a679b8 8100 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 8101 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
8102 (cu->language == language_cplus
8103 || cu->language == language_java)
72bf9492
DJ
8104 ? &cu->objfile->global_psymbols
8105 : &cu->objfile->static_psymbols,
8106 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8107
93311388 8108 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8109 continue;
8110 }
8111
8112 /* We'll save this DIE so link it in. */
8113 part_die->die_parent = parent_die;
8114 part_die->die_sibling = NULL;
8115 part_die->die_child = NULL;
8116
8117 if (last_die && last_die == parent_die)
8118 last_die->die_child = part_die;
8119 else if (last_die)
8120 last_die->die_sibling = part_die;
8121
8122 last_die = part_die;
8123
8124 if (first_die == NULL)
8125 first_die = part_die;
8126
8127 /* Maybe add the DIE to the hash table. Not all DIEs that we
8128 find interesting need to be in the hash table, because we
8129 also have the parent/sibling/child chains; only those that we
8130 might refer to by offset later during partial symbol reading.
8131
8132 For now this means things that might have be the target of a
8133 DW_AT_specification, DW_AT_abstract_origin, or
8134 DW_AT_extension. DW_AT_extension will refer only to
8135 namespaces; DW_AT_abstract_origin refers to functions (and
8136 many things under the function DIE, but we do not recurse
8137 into function DIEs during partial symbol reading) and
8138 possibly variables as well; DW_AT_specification refers to
8139 declarations. Declarations ought to have the DW_AT_declaration
8140 flag. It happens that GCC forgets to put it in sometimes, but
8141 only for functions, not for types.
8142
8143 Adding more things than necessary to the hash table is harmless
8144 except for the performance cost. Adding too few will result in
5afb4e99
DJ
8145 wasted time in find_partial_die, when we reread the compilation
8146 unit with load_all_dies set. */
72bf9492 8147
5afb4e99
DJ
8148 if (load_all
8149 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
8150 || abbrev->tag == DW_TAG_variable
8151 || abbrev->tag == DW_TAG_namespace
8152 || part_die->is_declaration)
8153 {
8154 void **slot;
8155
8156 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8157 part_die->offset, INSERT);
8158 *slot = part_die;
8159 }
8160
8161 part_die = obstack_alloc (&cu->comp_unit_obstack,
8162 sizeof (struct partial_die_info));
8163
8164 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 8165 we have no reason to follow the children of structures; for other
72bf9492 8166 languages we have to, both so that we can get at method physnames
bc30ff58
JB
8167 to infer fully qualified class names, and for DW_AT_specification.
8168
8169 For Ada, we need to scan the children of subprograms and lexical
8170 blocks as well because Ada allows the definition of nested
8171 entities that could be interesting for the debugger, such as
8172 nested subprograms for instance. */
72bf9492 8173 if (last_die->has_children
5afb4e99
DJ
8174 && (load_all
8175 || last_die->tag == DW_TAG_namespace
f55ee35c 8176 || last_die->tag == DW_TAG_module
72bf9492
DJ
8177 || last_die->tag == DW_TAG_enumeration_type
8178 || (cu->language != language_c
8179 && (last_die->tag == DW_TAG_class_type
680b30c7 8180 || last_die->tag == DW_TAG_interface_type
72bf9492 8181 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
8182 || last_die->tag == DW_TAG_union_type))
8183 || (cu->language == language_ada
8184 && (last_die->tag == DW_TAG_subprogram
8185 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
8186 {
8187 nesting_level++;
8188 parent_die = last_die;
8189 continue;
8190 }
8191
8192 /* Otherwise we skip to the next sibling, if any. */
93311388 8193 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
72bf9492
DJ
8194
8195 /* Back to the top, do it again. */
8196 }
8197}
8198
c906108c
SS
8199/* Read a minimal amount of information into the minimal die structure. */
8200
fe1b8b76 8201static gdb_byte *
72bf9492
DJ
8202read_partial_die (struct partial_die_info *part_die,
8203 struct abbrev_info *abbrev,
8204 unsigned int abbrev_len, bfd *abfd,
93311388
DE
8205 gdb_byte *buffer, gdb_byte *info_ptr,
8206 struct dwarf2_cu *cu)
c906108c 8207{
fa238c03 8208 unsigned int i;
c906108c 8209 struct attribute attr;
c5aa993b 8210 int has_low_pc_attr = 0;
c906108c
SS
8211 int has_high_pc_attr = 0;
8212
72bf9492 8213 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 8214
93311388 8215 part_die->offset = info_ptr - buffer;
72bf9492
DJ
8216
8217 info_ptr += abbrev_len;
8218
8219 if (abbrev == NULL)
8220 return info_ptr;
8221
c906108c
SS
8222 part_die->tag = abbrev->tag;
8223 part_die->has_children = abbrev->has_children;
c906108c
SS
8224
8225 for (i = 0; i < abbrev->num_attrs; ++i)
8226 {
e7c27a73 8227 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
8228
8229 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 8230 partial symbol table. */
c906108c
SS
8231 switch (attr.name)
8232 {
8233 case DW_AT_name:
71c25dea
TT
8234 switch (part_die->tag)
8235 {
8236 case DW_TAG_compile_unit:
348e048f 8237 case DW_TAG_type_unit:
71c25dea
TT
8238 /* Compilation units have a DW_AT_name that is a filename, not
8239 a source language identifier. */
8240 case DW_TAG_enumeration_type:
8241 case DW_TAG_enumerator:
8242 /* These tags always have simple identifiers already; no need
8243 to canonicalize them. */
8244 part_die->name = DW_STRING (&attr);
8245 break;
8246 default:
8247 part_die->name
8248 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
95519e0e 8249 &cu->objfile->objfile_obstack);
71c25dea
TT
8250 break;
8251 }
c906108c 8252 break;
31ef98ae 8253 case DW_AT_linkage_name:
c906108c 8254 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
8255 /* Note that both forms of linkage name might appear. We
8256 assume they will be the same, and we only store the last
8257 one we see. */
94af9270
KS
8258 if (cu->language == language_ada)
8259 part_die->name = DW_STRING (&attr);
c906108c
SS
8260 break;
8261 case DW_AT_low_pc:
8262 has_low_pc_attr = 1;
8263 part_die->lowpc = DW_ADDR (&attr);
8264 break;
8265 case DW_AT_high_pc:
8266 has_high_pc_attr = 1;
8267 part_die->highpc = DW_ADDR (&attr);
8268 break;
8269 case DW_AT_location:
8e19ed76
PS
8270 /* Support the .debug_loc offsets */
8271 if (attr_form_is_block (&attr))
8272 {
8273 part_die->locdesc = DW_BLOCK (&attr);
8274 }
3690dd37 8275 else if (attr_form_is_section_offset (&attr))
8e19ed76 8276 {
4d3c2250 8277 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8278 }
8279 else
8280 {
4d3c2250
KB
8281 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8282 "partial symbol information");
8e19ed76 8283 }
c906108c 8284 break;
c906108c
SS
8285 case DW_AT_external:
8286 part_die->is_external = DW_UNSND (&attr);
8287 break;
8288 case DW_AT_declaration:
8289 part_die->is_declaration = DW_UNSND (&attr);
8290 break;
8291 case DW_AT_type:
8292 part_die->has_type = 1;
8293 break;
8294 case DW_AT_abstract_origin:
8295 case DW_AT_specification:
72bf9492
DJ
8296 case DW_AT_extension:
8297 part_die->has_specification = 1;
c764a876 8298 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
8299 break;
8300 case DW_AT_sibling:
8301 /* Ignore absolute siblings, they might point outside of
8302 the current compile unit. */
8303 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 8304 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 8305 else
93311388 8306 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
c906108c 8307 break;
fa4028e9
JB
8308 case DW_AT_byte_size:
8309 part_die->has_byte_size = 1;
8310 break;
68511cec
CES
8311 case DW_AT_calling_convention:
8312 /* DWARF doesn't provide a way to identify a program's source-level
8313 entry point. DW_AT_calling_convention attributes are only meant
8314 to describe functions' calling conventions.
8315
8316 However, because it's a necessary piece of information in
8317 Fortran, and because DW_CC_program is the only piece of debugging
8318 information whose definition refers to a 'main program' at all,
8319 several compilers have begun marking Fortran main programs with
8320 DW_CC_program --- even when those functions use the standard
8321 calling conventions.
8322
8323 So until DWARF specifies a way to provide this information and
8324 compilers pick up the new representation, we'll support this
8325 practice. */
8326 if (DW_UNSND (&attr) == DW_CC_program
8327 && cu->language == language_fortran)
8328 set_main_name (part_die->name);
8329 break;
c906108c
SS
8330 default:
8331 break;
8332 }
8333 }
8334
c906108c
SS
8335 /* When using the GNU linker, .gnu.linkonce. sections are used to
8336 eliminate duplicate copies of functions and vtables and such.
8337 The linker will arbitrarily choose one and discard the others.
8338 The AT_*_pc values for such functions refer to local labels in
8339 these sections. If the section from that file was discarded, the
8340 labels are not in the output, so the relocs get a value of 0.
8341 If this is a discarded function, mark the pc bounds as invalid,
8342 so that GDB will ignore it. */
8343 if (has_low_pc_attr && has_high_pc_attr
8344 && part_die->lowpc < part_die->highpc
8345 && (part_die->lowpc != 0
72dca2f5 8346 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 8347 part_die->has_pc_info = 1;
85cbf3d3 8348
c906108c
SS
8349 return info_ptr;
8350}
8351
72bf9492
DJ
8352/* Find a cached partial DIE at OFFSET in CU. */
8353
8354static struct partial_die_info *
c764a876 8355find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
8356{
8357 struct partial_die_info *lookup_die = NULL;
8358 struct partial_die_info part_die;
8359
8360 part_die.offset = offset;
8361 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8362
72bf9492
DJ
8363 return lookup_die;
8364}
8365
348e048f
DE
8366/* Find a partial DIE at OFFSET, which may or may not be in CU,
8367 except in the case of .debug_types DIEs which do not reference
8368 outside their CU (they do however referencing other types via
8369 DW_FORM_sig8). */
72bf9492
DJ
8370
8371static struct partial_die_info *
c764a876 8372find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 8373{
5afb4e99
DJ
8374 struct dwarf2_per_cu_data *per_cu = NULL;
8375 struct partial_die_info *pd = NULL;
72bf9492 8376
348e048f
DE
8377 if (cu->per_cu->from_debug_types)
8378 {
8379 pd = find_partial_die_in_comp_unit (offset, cu);
8380 if (pd != NULL)
8381 return pd;
8382 goto not_found;
8383 }
8384
45452591 8385 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
8386 {
8387 pd = find_partial_die_in_comp_unit (offset, cu);
8388 if (pd != NULL)
8389 return pd;
8390 }
72bf9492 8391
ae038cb0
DJ
8392 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8393
ae038cb0
DJ
8394 if (per_cu->cu == NULL)
8395 {
93311388 8396 load_partial_comp_unit (per_cu, cu->objfile);
ae038cb0
DJ
8397 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8398 dwarf2_per_objfile->read_in_chain = per_cu;
8399 }
8400
8401 per_cu->cu->last_used = 0;
5afb4e99
DJ
8402 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8403
8404 if (pd == NULL && per_cu->load_all_dies == 0)
8405 {
8406 struct cleanup *back_to;
8407 struct partial_die_info comp_unit_die;
8408 struct abbrev_info *abbrev;
8409 unsigned int bytes_read;
8410 char *info_ptr;
8411
8412 per_cu->load_all_dies = 1;
8413
8414 /* Re-read the DIEs. */
8415 back_to = make_cleanup (null_cleanup, 0);
8416 if (per_cu->cu->dwarf2_abbrevs == NULL)
8417 {
8418 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
53d72f98 8419 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
5afb4e99 8420 }
dce234bc 8421 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
8422 + per_cu->cu->header.offset
8423 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
8424 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8425 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
93311388
DE
8426 per_cu->cu->objfile->obfd,
8427 dwarf2_per_objfile->info.buffer, info_ptr,
5afb4e99
DJ
8428 per_cu->cu);
8429 if (comp_unit_die.has_children)
93311388
DE
8430 load_partial_dies (per_cu->cu->objfile->obfd,
8431 dwarf2_per_objfile->info.buffer, info_ptr,
8432 0, per_cu->cu);
5afb4e99
DJ
8433 do_cleanups (back_to);
8434
8435 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8436 }
8437
348e048f
DE
8438 not_found:
8439
5afb4e99
DJ
8440 if (pd == NULL)
8441 internal_error (__FILE__, __LINE__,
c764a876 8442 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
8443 offset, bfd_get_filename (cu->objfile->obfd));
8444 return pd;
72bf9492
DJ
8445}
8446
8447/* Adjust PART_DIE before generating a symbol for it. This function
8448 may set the is_external flag or change the DIE's name. */
8449
8450static void
8451fixup_partial_die (struct partial_die_info *part_die,
8452 struct dwarf2_cu *cu)
8453{
8454 /* If we found a reference attribute and the DIE has no name, try
8455 to find a name in the referred to DIE. */
8456
8457 if (part_die->name == NULL && part_die->has_specification)
8458 {
8459 struct partial_die_info *spec_die;
72bf9492 8460
10b3939b 8461 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 8462
10b3939b 8463 fixup_partial_die (spec_die, cu);
72bf9492
DJ
8464
8465 if (spec_die->name)
8466 {
8467 part_die->name = spec_die->name;
8468
8469 /* Copy DW_AT_external attribute if it is set. */
8470 if (spec_die->is_external)
8471 part_die->is_external = spec_die->is_external;
8472 }
8473 }
8474
8475 /* Set default names for some unnamed DIEs. */
8476 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8477 || part_die->tag == DW_TAG_class_type))
8478 part_die->name = "(anonymous class)";
8479
8480 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8481 part_die->name = "(anonymous namespace)";
8482
8483 if (part_die->tag == DW_TAG_structure_type
8484 || part_die->tag == DW_TAG_class_type
8485 || part_die->tag == DW_TAG_union_type)
8486 guess_structure_name (part_die, cu);
8487}
8488
a8329558 8489/* Read an attribute value described by an attribute form. */
c906108c 8490
fe1b8b76 8491static gdb_byte *
a8329558 8492read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 8493 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 8494 struct dwarf2_cu *cu)
c906108c 8495{
e7c27a73 8496 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
8497 unsigned int bytes_read;
8498 struct dwarf_block *blk;
8499
a8329558
KW
8500 attr->form = form;
8501 switch (form)
c906108c 8502 {
c906108c 8503 case DW_FORM_ref_addr:
ae411497
TT
8504 if (cu->header.version == 2)
8505 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8506 else
8507 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8508 info_ptr += bytes_read;
8509 break;
8510 case DW_FORM_addr:
e7c27a73 8511 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 8512 info_ptr += bytes_read;
c906108c
SS
8513 break;
8514 case DW_FORM_block2:
7b5a2f43 8515 blk = dwarf_alloc_block (cu);
c906108c
SS
8516 blk->size = read_2_bytes (abfd, info_ptr);
8517 info_ptr += 2;
8518 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8519 info_ptr += blk->size;
8520 DW_BLOCK (attr) = blk;
8521 break;
8522 case DW_FORM_block4:
7b5a2f43 8523 blk = dwarf_alloc_block (cu);
c906108c
SS
8524 blk->size = read_4_bytes (abfd, info_ptr);
8525 info_ptr += 4;
8526 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8527 info_ptr += blk->size;
8528 DW_BLOCK (attr) = blk;
8529 break;
8530 case DW_FORM_data2:
8531 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
8532 info_ptr += 2;
8533 break;
8534 case DW_FORM_data4:
8535 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
8536 info_ptr += 4;
8537 break;
8538 case DW_FORM_data8:
8539 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
8540 info_ptr += 8;
8541 break;
2dc7f7b3
TT
8542 case DW_FORM_sec_offset:
8543 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8544 info_ptr += bytes_read;
8545 break;
c906108c
SS
8546 case DW_FORM_string:
8547 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
8285870a 8548 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
8549 info_ptr += bytes_read;
8550 break;
4bdf3d34
JJ
8551 case DW_FORM_strp:
8552 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
8553 &bytes_read);
8285870a 8554 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
8555 info_ptr += bytes_read;
8556 break;
2dc7f7b3 8557 case DW_FORM_exprloc:
c906108c 8558 case DW_FORM_block:
7b5a2f43 8559 blk = dwarf_alloc_block (cu);
c906108c
SS
8560 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8561 info_ptr += bytes_read;
8562 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8563 info_ptr += blk->size;
8564 DW_BLOCK (attr) = blk;
8565 break;
8566 case DW_FORM_block1:
7b5a2f43 8567 blk = dwarf_alloc_block (cu);
c906108c
SS
8568 blk->size = read_1_byte (abfd, info_ptr);
8569 info_ptr += 1;
8570 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8571 info_ptr += blk->size;
8572 DW_BLOCK (attr) = blk;
8573 break;
8574 case DW_FORM_data1:
8575 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8576 info_ptr += 1;
8577 break;
8578 case DW_FORM_flag:
8579 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8580 info_ptr += 1;
8581 break;
2dc7f7b3
TT
8582 case DW_FORM_flag_present:
8583 DW_UNSND (attr) = 1;
8584 break;
c906108c
SS
8585 case DW_FORM_sdata:
8586 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
8587 info_ptr += bytes_read;
8588 break;
8589 case DW_FORM_udata:
8590 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8591 info_ptr += bytes_read;
8592 break;
8593 case DW_FORM_ref1:
10b3939b 8594 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
8595 info_ptr += 1;
8596 break;
8597 case DW_FORM_ref2:
10b3939b 8598 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
8599 info_ptr += 2;
8600 break;
8601 case DW_FORM_ref4:
10b3939b 8602 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
8603 info_ptr += 4;
8604 break;
613e1657 8605 case DW_FORM_ref8:
10b3939b 8606 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
8607 info_ptr += 8;
8608 break;
348e048f
DE
8609 case DW_FORM_sig8:
8610 /* Convert the signature to something we can record in DW_UNSND
8611 for later lookup.
8612 NOTE: This is NULL if the type wasn't found. */
8613 DW_SIGNATURED_TYPE (attr) =
8614 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
8615 info_ptr += 8;
8616 break;
c906108c 8617 case DW_FORM_ref_udata:
10b3939b
DJ
8618 DW_ADDR (attr) = (cu->header.offset
8619 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
8620 info_ptr += bytes_read;
8621 break;
c906108c 8622 case DW_FORM_indirect:
a8329558
KW
8623 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8624 info_ptr += bytes_read;
e7c27a73 8625 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 8626 break;
c906108c 8627 default:
8a3fe4f8 8628 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
8629 dwarf_form_name (form),
8630 bfd_get_filename (abfd));
c906108c 8631 }
28e94949
JB
8632
8633 /* We have seen instances where the compiler tried to emit a byte
8634 size attribute of -1 which ended up being encoded as an unsigned
8635 0xffffffff. Although 0xffffffff is technically a valid size value,
8636 an object of this size seems pretty unlikely so we can relatively
8637 safely treat these cases as if the size attribute was invalid and
8638 treat them as zero by default. */
8639 if (attr->name == DW_AT_byte_size
8640 && form == DW_FORM_data4
8641 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
8642 {
8643 complaint
8644 (&symfile_complaints,
43bbcdc2
PH
8645 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
8646 hex_string (DW_UNSND (attr)));
01c66ae6
JB
8647 DW_UNSND (attr) = 0;
8648 }
28e94949 8649
c906108c
SS
8650 return info_ptr;
8651}
8652
a8329558
KW
8653/* Read an attribute described by an abbreviated attribute. */
8654
fe1b8b76 8655static gdb_byte *
a8329558 8656read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 8657 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
8658{
8659 attr->name = abbrev->name;
e7c27a73 8660 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
8661}
8662
c906108c
SS
8663/* read dwarf information from a buffer */
8664
8665static unsigned int
fe1b8b76 8666read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 8667{
fe1b8b76 8668 return bfd_get_8 (abfd, buf);
c906108c
SS
8669}
8670
8671static int
fe1b8b76 8672read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 8673{
fe1b8b76 8674 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
8675}
8676
8677static unsigned int
fe1b8b76 8678read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8679{
fe1b8b76 8680 return bfd_get_16 (abfd, buf);
c906108c
SS
8681}
8682
8683static int
fe1b8b76 8684read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8685{
fe1b8b76 8686 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
8687}
8688
8689static unsigned int
fe1b8b76 8690read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8691{
fe1b8b76 8692 return bfd_get_32 (abfd, buf);
c906108c
SS
8693}
8694
8695static int
fe1b8b76 8696read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8697{
fe1b8b76 8698 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
8699}
8700
93311388 8701static ULONGEST
fe1b8b76 8702read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 8703{
fe1b8b76 8704 return bfd_get_64 (abfd, buf);
c906108c
SS
8705}
8706
8707static CORE_ADDR
fe1b8b76 8708read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 8709 unsigned int *bytes_read)
c906108c 8710{
e7c27a73 8711 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
8712 CORE_ADDR retval = 0;
8713
107d2387 8714 if (cu_header->signed_addr_p)
c906108c 8715 {
107d2387
AC
8716 switch (cu_header->addr_size)
8717 {
8718 case 2:
fe1b8b76 8719 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
8720 break;
8721 case 4:
fe1b8b76 8722 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
8723 break;
8724 case 8:
fe1b8b76 8725 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
8726 break;
8727 default:
8e65ff28 8728 internal_error (__FILE__, __LINE__,
e2e0b3e5 8729 _("read_address: bad switch, signed [in module %s]"),
659b0389 8730 bfd_get_filename (abfd));
107d2387
AC
8731 }
8732 }
8733 else
8734 {
8735 switch (cu_header->addr_size)
8736 {
8737 case 2:
fe1b8b76 8738 retval = bfd_get_16 (abfd, buf);
107d2387
AC
8739 break;
8740 case 4:
fe1b8b76 8741 retval = bfd_get_32 (abfd, buf);
107d2387
AC
8742 break;
8743 case 8:
fe1b8b76 8744 retval = bfd_get_64 (abfd, buf);
107d2387
AC
8745 break;
8746 default:
8e65ff28 8747 internal_error (__FILE__, __LINE__,
e2e0b3e5 8748 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 8749 bfd_get_filename (abfd));
107d2387 8750 }
c906108c 8751 }
64367e0a 8752
107d2387
AC
8753 *bytes_read = cu_header->addr_size;
8754 return retval;
c906108c
SS
8755}
8756
f7ef9339 8757/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
8758 specification allows the initial length to take up either 4 bytes
8759 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
8760 bytes describe the length and all offsets will be 8 bytes in length
8761 instead of 4.
8762
f7ef9339
KB
8763 An older, non-standard 64-bit format is also handled by this
8764 function. The older format in question stores the initial length
8765 as an 8-byte quantity without an escape value. Lengths greater
8766 than 2^32 aren't very common which means that the initial 4 bytes
8767 is almost always zero. Since a length value of zero doesn't make
8768 sense for the 32-bit format, this initial zero can be considered to
8769 be an escape value which indicates the presence of the older 64-bit
8770 format. As written, the code can't detect (old format) lengths
917c78fc
MK
8771 greater than 4GB. If it becomes necessary to handle lengths
8772 somewhat larger than 4GB, we could allow other small values (such
8773 as the non-sensical values of 1, 2, and 3) to also be used as
8774 escape values indicating the presence of the old format.
f7ef9339 8775
917c78fc
MK
8776 The value returned via bytes_read should be used to increment the
8777 relevant pointer after calling read_initial_length().
c764a876 8778
613e1657
KB
8779 [ Note: read_initial_length() and read_offset() are based on the
8780 document entitled "DWARF Debugging Information Format", revision
f7ef9339 8781 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
8782 from:
8783
f7ef9339 8784 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 8785
613e1657
KB
8786 This document is only a draft and is subject to change. (So beware.)
8787
f7ef9339 8788 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
8789 determined empirically by examining 64-bit ELF files produced by
8790 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
8791
8792 - Kevin, July 16, 2002
613e1657
KB
8793 ] */
8794
8795static LONGEST
c764a876 8796read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 8797{
fe1b8b76 8798 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 8799
dd373385 8800 if (length == 0xffffffff)
613e1657 8801 {
fe1b8b76 8802 length = bfd_get_64 (abfd, buf + 4);
613e1657 8803 *bytes_read = 12;
613e1657 8804 }
dd373385 8805 else if (length == 0)
f7ef9339 8806 {
dd373385 8807 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 8808 length = bfd_get_64 (abfd, buf);
f7ef9339 8809 *bytes_read = 8;
f7ef9339 8810 }
613e1657
KB
8811 else
8812 {
8813 *bytes_read = 4;
613e1657
KB
8814 }
8815
c764a876
DE
8816 return length;
8817}
dd373385 8818
c764a876
DE
8819/* Cover function for read_initial_length.
8820 Returns the length of the object at BUF, and stores the size of the
8821 initial length in *BYTES_READ and stores the size that offsets will be in
8822 *OFFSET_SIZE.
8823 If the initial length size is not equivalent to that specified in
8824 CU_HEADER then issue a complaint.
8825 This is useful when reading non-comp-unit headers. */
dd373385 8826
c764a876
DE
8827static LONGEST
8828read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
8829 const struct comp_unit_head *cu_header,
8830 unsigned int *bytes_read,
8831 unsigned int *offset_size)
8832{
8833 LONGEST length = read_initial_length (abfd, buf, bytes_read);
8834
8835 gdb_assert (cu_header->initial_length_size == 4
8836 || cu_header->initial_length_size == 8
8837 || cu_header->initial_length_size == 12);
8838
8839 if (cu_header->initial_length_size != *bytes_read)
8840 complaint (&symfile_complaints,
8841 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 8842
c764a876 8843 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 8844 return length;
613e1657
KB
8845}
8846
8847/* Read an offset from the data stream. The size of the offset is
917c78fc 8848 given by cu_header->offset_size. */
613e1657
KB
8849
8850static LONGEST
fe1b8b76 8851read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 8852 unsigned int *bytes_read)
c764a876
DE
8853{
8854 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 8855
c764a876
DE
8856 *bytes_read = cu_header->offset_size;
8857 return offset;
8858}
8859
8860/* Read an offset from the data stream. */
8861
8862static LONGEST
8863read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
8864{
8865 LONGEST retval = 0;
8866
c764a876 8867 switch (offset_size)
613e1657
KB
8868 {
8869 case 4:
fe1b8b76 8870 retval = bfd_get_32 (abfd, buf);
613e1657
KB
8871 break;
8872 case 8:
fe1b8b76 8873 retval = bfd_get_64 (abfd, buf);
613e1657
KB
8874 break;
8875 default:
8e65ff28 8876 internal_error (__FILE__, __LINE__,
c764a876 8877 _("read_offset_1: bad switch [in module %s]"),
659b0389 8878 bfd_get_filename (abfd));
613e1657
KB
8879 }
8880
917c78fc 8881 return retval;
613e1657
KB
8882}
8883
fe1b8b76
JB
8884static gdb_byte *
8885read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
8886{
8887 /* If the size of a host char is 8 bits, we can return a pointer
8888 to the buffer, otherwise we have to copy the data to a buffer
8889 allocated on the temporary obstack. */
4bdf3d34 8890 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 8891 return buf;
c906108c
SS
8892}
8893
8894static char *
fe1b8b76 8895read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
8896{
8897 /* If the size of a host char is 8 bits, we can return a pointer
8898 to the string, otherwise we have to copy the string to a buffer
8899 allocated on the temporary obstack. */
4bdf3d34 8900 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
8901 if (*buf == '\0')
8902 {
8903 *bytes_read_ptr = 1;
8904 return NULL;
8905 }
fe1b8b76
JB
8906 *bytes_read_ptr = strlen ((char *) buf) + 1;
8907 return (char *) buf;
4bdf3d34
JJ
8908}
8909
8910static char *
fe1b8b76 8911read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
8912 const struct comp_unit_head *cu_header,
8913 unsigned int *bytes_read_ptr)
8914{
c764a876 8915 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 8916
be391dca 8917 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 8918 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 8919 {
8a3fe4f8 8920 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 8921 bfd_get_filename (abfd));
4bdf3d34 8922 return NULL;
c906108c 8923 }
dce234bc 8924 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 8925 {
8a3fe4f8 8926 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 8927 bfd_get_filename (abfd));
c906108c
SS
8928 return NULL;
8929 }
4bdf3d34 8930 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 8931 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 8932 return NULL;
dce234bc 8933 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
8934}
8935
ce5d95e1 8936static unsigned long
fe1b8b76 8937read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 8938{
ce5d95e1
JB
8939 unsigned long result;
8940 unsigned int num_read;
c906108c
SS
8941 int i, shift;
8942 unsigned char byte;
8943
8944 result = 0;
8945 shift = 0;
8946 num_read = 0;
8947 i = 0;
8948 while (1)
8949 {
fe1b8b76 8950 byte = bfd_get_8 (abfd, buf);
c906108c
SS
8951 buf++;
8952 num_read++;
ce5d95e1 8953 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
8954 if ((byte & 128) == 0)
8955 {
8956 break;
8957 }
8958 shift += 7;
8959 }
8960 *bytes_read_ptr = num_read;
8961 return result;
8962}
8963
ce5d95e1 8964static long
fe1b8b76 8965read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 8966{
ce5d95e1 8967 long result;
77e0b926 8968 int i, shift, num_read;
c906108c
SS
8969 unsigned char byte;
8970
8971 result = 0;
8972 shift = 0;
c906108c
SS
8973 num_read = 0;
8974 i = 0;
8975 while (1)
8976 {
fe1b8b76 8977 byte = bfd_get_8 (abfd, buf);
c906108c
SS
8978 buf++;
8979 num_read++;
ce5d95e1 8980 result |= ((long)(byte & 127) << shift);
c906108c
SS
8981 shift += 7;
8982 if ((byte & 128) == 0)
8983 {
8984 break;
8985 }
8986 }
77e0b926
DJ
8987 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
8988 result |= -(((long)1) << shift);
c906108c
SS
8989 *bytes_read_ptr = num_read;
8990 return result;
8991}
8992
4bb7a0a7
DJ
8993/* Return a pointer to just past the end of an LEB128 number in BUF. */
8994
fe1b8b76
JB
8995static gdb_byte *
8996skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
8997{
8998 int byte;
8999
9000 while (1)
9001 {
fe1b8b76 9002 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
9003 buf++;
9004 if ((byte & 128) == 0)
9005 return buf;
9006 }
9007}
9008
c906108c 9009static void
e142c38c 9010set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
9011{
9012 switch (lang)
9013 {
9014 case DW_LANG_C89:
76bee0cc 9015 case DW_LANG_C99:
c906108c 9016 case DW_LANG_C:
e142c38c 9017 cu->language = language_c;
c906108c
SS
9018 break;
9019 case DW_LANG_C_plus_plus:
e142c38c 9020 cu->language = language_cplus;
c906108c 9021 break;
6aecb9c2
JB
9022 case DW_LANG_D:
9023 cu->language = language_d;
9024 break;
c906108c
SS
9025 case DW_LANG_Fortran77:
9026 case DW_LANG_Fortran90:
b21b22e0 9027 case DW_LANG_Fortran95:
e142c38c 9028 cu->language = language_fortran;
c906108c
SS
9029 break;
9030 case DW_LANG_Mips_Assembler:
e142c38c 9031 cu->language = language_asm;
c906108c 9032 break;
bebd888e 9033 case DW_LANG_Java:
e142c38c 9034 cu->language = language_java;
bebd888e 9035 break;
c906108c 9036 case DW_LANG_Ada83:
8aaf0b47 9037 case DW_LANG_Ada95:
bc5f45f8
JB
9038 cu->language = language_ada;
9039 break;
72019c9c
GM
9040 case DW_LANG_Modula2:
9041 cu->language = language_m2;
9042 break;
fe8e67fd
PM
9043 case DW_LANG_Pascal83:
9044 cu->language = language_pascal;
9045 break;
22566fbd
DJ
9046 case DW_LANG_ObjC:
9047 cu->language = language_objc;
9048 break;
c906108c
SS
9049 case DW_LANG_Cobol74:
9050 case DW_LANG_Cobol85:
c906108c 9051 default:
e142c38c 9052 cu->language = language_minimal;
c906108c
SS
9053 break;
9054 }
e142c38c 9055 cu->language_defn = language_def (cu->language);
c906108c
SS
9056}
9057
9058/* Return the named attribute or NULL if not there. */
9059
9060static struct attribute *
e142c38c 9061dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
9062{
9063 unsigned int i;
9064 struct attribute *spec = NULL;
9065
9066 for (i = 0; i < die->num_attrs; ++i)
9067 {
9068 if (die->attrs[i].name == name)
10b3939b 9069 return &die->attrs[i];
c906108c
SS
9070 if (die->attrs[i].name == DW_AT_specification
9071 || die->attrs[i].name == DW_AT_abstract_origin)
9072 spec = &die->attrs[i];
9073 }
c906108c 9074
10b3939b 9075 if (spec)
f2f0e013
DJ
9076 {
9077 die = follow_die_ref (die, spec, &cu);
9078 return dwarf2_attr (die, name, cu);
9079 }
c5aa993b 9080
c906108c
SS
9081 return NULL;
9082}
9083
348e048f
DE
9084/* Return the named attribute or NULL if not there,
9085 but do not follow DW_AT_specification, etc.
9086 This is for use in contexts where we're reading .debug_types dies.
9087 Following DW_AT_specification, DW_AT_abstract_origin will take us
9088 back up the chain, and we want to go down. */
9089
9090static struct attribute *
9091dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9092 struct dwarf2_cu *cu)
9093{
9094 unsigned int i;
9095
9096 for (i = 0; i < die->num_attrs; ++i)
9097 if (die->attrs[i].name == name)
9098 return &die->attrs[i];
9099
9100 return NULL;
9101}
9102
05cf31d1
JB
9103/* Return non-zero iff the attribute NAME is defined for the given DIE,
9104 and holds a non-zero value. This function should only be used for
2dc7f7b3 9105 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
9106
9107static int
9108dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9109{
9110 struct attribute *attr = dwarf2_attr (die, name, cu);
9111
9112 return (attr && DW_UNSND (attr));
9113}
9114
3ca72b44 9115static int
e142c38c 9116die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 9117{
05cf31d1
JB
9118 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9119 which value is non-zero. However, we have to be careful with
9120 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9121 (via dwarf2_flag_true_p) follows this attribute. So we may
9122 end up accidently finding a declaration attribute that belongs
9123 to a different DIE referenced by the specification attribute,
9124 even though the given DIE does not have a declaration attribute. */
9125 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9126 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
9127}
9128
63d06c5c 9129/* Return the die giving the specification for DIE, if there is
f2f0e013 9130 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
9131 containing the return value on output. If there is no
9132 specification, but there is an abstract origin, that is
9133 returned. */
63d06c5c
DC
9134
9135static struct die_info *
f2f0e013 9136die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 9137{
f2f0e013
DJ
9138 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9139 *spec_cu);
63d06c5c 9140
edb3359d
DJ
9141 if (spec_attr == NULL)
9142 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9143
63d06c5c
DC
9144 if (spec_attr == NULL)
9145 return NULL;
9146 else
f2f0e013 9147 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 9148}
c906108c 9149
debd256d
JB
9150/* Free the line_header structure *LH, and any arrays and strings it
9151 refers to. */
9152static void
9153free_line_header (struct line_header *lh)
9154{
9155 if (lh->standard_opcode_lengths)
a8bc7b56 9156 xfree (lh->standard_opcode_lengths);
debd256d
JB
9157
9158 /* Remember that all the lh->file_names[i].name pointers are
9159 pointers into debug_line_buffer, and don't need to be freed. */
9160 if (lh->file_names)
a8bc7b56 9161 xfree (lh->file_names);
debd256d
JB
9162
9163 /* Similarly for the include directory names. */
9164 if (lh->include_dirs)
a8bc7b56 9165 xfree (lh->include_dirs);
debd256d 9166
a8bc7b56 9167 xfree (lh);
debd256d
JB
9168}
9169
9170
9171/* Add an entry to LH's include directory table. */
9172static void
9173add_include_dir (struct line_header *lh, char *include_dir)
c906108c 9174{
debd256d
JB
9175 /* Grow the array if necessary. */
9176 if (lh->include_dirs_size == 0)
c5aa993b 9177 {
debd256d
JB
9178 lh->include_dirs_size = 1; /* for testing */
9179 lh->include_dirs = xmalloc (lh->include_dirs_size
9180 * sizeof (*lh->include_dirs));
9181 }
9182 else if (lh->num_include_dirs >= lh->include_dirs_size)
9183 {
9184 lh->include_dirs_size *= 2;
9185 lh->include_dirs = xrealloc (lh->include_dirs,
9186 (lh->include_dirs_size
9187 * sizeof (*lh->include_dirs)));
c5aa993b 9188 }
c906108c 9189
debd256d
JB
9190 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9191}
6e70227d 9192
debd256d
JB
9193
9194/* Add an entry to LH's file name table. */
9195static void
9196add_file_name (struct line_header *lh,
9197 char *name,
9198 unsigned int dir_index,
9199 unsigned int mod_time,
9200 unsigned int length)
9201{
9202 struct file_entry *fe;
9203
9204 /* Grow the array if necessary. */
9205 if (lh->file_names_size == 0)
9206 {
9207 lh->file_names_size = 1; /* for testing */
9208 lh->file_names = xmalloc (lh->file_names_size
9209 * sizeof (*lh->file_names));
9210 }
9211 else if (lh->num_file_names >= lh->file_names_size)
9212 {
9213 lh->file_names_size *= 2;
9214 lh->file_names = xrealloc (lh->file_names,
9215 (lh->file_names_size
9216 * sizeof (*lh->file_names)));
9217 }
9218
9219 fe = &lh->file_names[lh->num_file_names++];
9220 fe->name = name;
9221 fe->dir_index = dir_index;
9222 fe->mod_time = mod_time;
9223 fe->length = length;
aaa75496 9224 fe->included_p = 0;
cb1df416 9225 fe->symtab = NULL;
debd256d 9226}
6e70227d 9227
debd256d
JB
9228
9229/* Read the statement program header starting at OFFSET in
6502dd73
DJ
9230 .debug_line, according to the endianness of ABFD. Return a pointer
9231 to a struct line_header, allocated using xmalloc.
debd256d
JB
9232
9233 NOTE: the strings in the include directory and file name tables of
9234 the returned object point into debug_line_buffer, and must not be
9235 freed. */
9236static struct line_header *
9237dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 9238 struct dwarf2_cu *cu)
debd256d
JB
9239{
9240 struct cleanup *back_to;
9241 struct line_header *lh;
fe1b8b76 9242 gdb_byte *line_ptr;
c764a876 9243 unsigned int bytes_read, offset_size;
debd256d
JB
9244 int i;
9245 char *cur_dir, *cur_file;
9246
be391dca 9247 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
dce234bc 9248 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 9249 {
e2e0b3e5 9250 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
9251 return 0;
9252 }
9253
a738430d
MK
9254 /* Make sure that at least there's room for the total_length field.
9255 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 9256 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 9257 {
4d3c2250 9258 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9259 return 0;
9260 }
9261
9262 lh = xmalloc (sizeof (*lh));
9263 memset (lh, 0, sizeof (*lh));
9264 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9265 (void *) lh);
9266
dce234bc 9267 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 9268
a738430d 9269 /* Read in the header. */
6e70227d 9270 lh->total_length =
c764a876
DE
9271 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9272 &bytes_read, &offset_size);
debd256d 9273 line_ptr += bytes_read;
dce234bc
PP
9274 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9275 + dwarf2_per_objfile->line.size))
debd256d 9276 {
4d3c2250 9277 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
9278 return 0;
9279 }
9280 lh->statement_program_end = line_ptr + lh->total_length;
9281 lh->version = read_2_bytes (abfd, line_ptr);
9282 line_ptr += 2;
c764a876
DE
9283 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9284 line_ptr += offset_size;
debd256d
JB
9285 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9286 line_ptr += 1;
2dc7f7b3
TT
9287 if (lh->version >= 4)
9288 {
9289 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9290 line_ptr += 1;
9291 }
9292 else
9293 lh->maximum_ops_per_instruction = 1;
9294
9295 if (lh->maximum_ops_per_instruction == 0)
9296 {
9297 lh->maximum_ops_per_instruction = 1;
9298 complaint (&symfile_complaints,
9299 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9300 }
9301
debd256d
JB
9302 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9303 line_ptr += 1;
9304 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9305 line_ptr += 1;
9306 lh->line_range = read_1_byte (abfd, line_ptr);
9307 line_ptr += 1;
9308 lh->opcode_base = read_1_byte (abfd, line_ptr);
9309 line_ptr += 1;
9310 lh->standard_opcode_lengths
fe1b8b76 9311 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
9312
9313 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9314 for (i = 1; i < lh->opcode_base; ++i)
9315 {
9316 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9317 line_ptr += 1;
9318 }
9319
a738430d 9320 /* Read directory table. */
debd256d
JB
9321 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
9322 {
9323 line_ptr += bytes_read;
9324 add_include_dir (lh, cur_dir);
9325 }
9326 line_ptr += bytes_read;
9327
a738430d 9328 /* Read file name table. */
debd256d
JB
9329 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
9330 {
9331 unsigned int dir_index, mod_time, length;
9332
9333 line_ptr += bytes_read;
9334 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9335 line_ptr += bytes_read;
9336 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9337 line_ptr += bytes_read;
9338 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9339 line_ptr += bytes_read;
9340
9341 add_file_name (lh, cur_file, dir_index, mod_time, length);
9342 }
9343 line_ptr += bytes_read;
6e70227d 9344 lh->statement_program_start = line_ptr;
debd256d 9345
dce234bc
PP
9346 if (line_ptr > (dwarf2_per_objfile->line.buffer
9347 + dwarf2_per_objfile->line.size))
4d3c2250 9348 complaint (&symfile_complaints,
e2e0b3e5 9349 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
9350
9351 discard_cleanups (back_to);
9352 return lh;
9353}
c906108c 9354
5fb290d7
DJ
9355/* This function exists to work around a bug in certain compilers
9356 (particularly GCC 2.95), in which the first line number marker of a
9357 function does not show up until after the prologue, right before
9358 the second line number marker. This function shifts ADDRESS down
9359 to the beginning of the function if necessary, and is called on
9360 addresses passed to record_line. */
9361
9362static CORE_ADDR
e142c38c 9363check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
9364{
9365 struct function_range *fn;
9366
9367 /* Find the function_range containing address. */
e142c38c 9368 if (!cu->first_fn)
5fb290d7
DJ
9369 return address;
9370
e142c38c
DJ
9371 if (!cu->cached_fn)
9372 cu->cached_fn = cu->first_fn;
5fb290d7 9373
e142c38c 9374 fn = cu->cached_fn;
5fb290d7
DJ
9375 while (fn)
9376 if (fn->lowpc <= address && fn->highpc > address)
9377 goto found;
9378 else
9379 fn = fn->next;
9380
e142c38c
DJ
9381 fn = cu->first_fn;
9382 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
9383 if (fn->lowpc <= address && fn->highpc > address)
9384 goto found;
9385 else
9386 fn = fn->next;
9387
9388 return address;
9389
9390 found:
9391 if (fn->seen_line)
9392 return address;
9393 if (address != fn->lowpc)
4d3c2250 9394 complaint (&symfile_complaints,
e2e0b3e5 9395 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 9396 (unsigned long) address, fn->name);
5fb290d7
DJ
9397 fn->seen_line = 1;
9398 return fn->lowpc;
9399}
9400
aaa75496
JB
9401/* Decode the Line Number Program (LNP) for the given line_header
9402 structure and CU. The actual information extracted and the type
9403 of structures created from the LNP depends on the value of PST.
9404
9405 1. If PST is NULL, then this procedure uses the data from the program
9406 to create all necessary symbol tables, and their linetables.
9407 The compilation directory of the file is passed in COMP_DIR,
9408 and must not be NULL.
6e70227d 9409
aaa75496
JB
9410 2. If PST is not NULL, this procedure reads the program to determine
9411 the list of files included by the unit represented by PST, and
9412 builds all the associated partial symbol tables. In this case,
9413 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9414 is not used to compute the full name of the symtab, and therefore
9415 omitting it when building the partial symtab does not introduce
9416 the potential for inconsistency - a partial symtab and its associated
9417 symbtab having a different fullname -). */
debd256d 9418
c906108c 9419static void
debd256d 9420dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 9421 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 9422{
a8c50c1f 9423 gdb_byte *line_ptr, *extended_end;
fe1b8b76 9424 gdb_byte *line_end;
a8c50c1f 9425 unsigned int bytes_read, extended_len;
c906108c 9426 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
9427 CORE_ADDR baseaddr;
9428 struct objfile *objfile = cu->objfile;
fbf65064 9429 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 9430 const int decode_for_pst_p = (pst != NULL);
cb1df416 9431 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
9432
9433 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9434
debd256d
JB
9435 line_ptr = lh->statement_program_start;
9436 line_end = lh->statement_program_end;
c906108c
SS
9437
9438 /* Read the statement sequences until there's nothing left. */
9439 while (line_ptr < line_end)
9440 {
9441 /* state machine registers */
9442 CORE_ADDR address = 0;
9443 unsigned int file = 1;
9444 unsigned int line = 1;
9445 unsigned int column = 0;
debd256d 9446 int is_stmt = lh->default_is_stmt;
c906108c
SS
9447 int basic_block = 0;
9448 int end_sequence = 0;
fbf65064 9449 CORE_ADDR addr;
2dc7f7b3 9450 unsigned char op_index = 0;
c906108c 9451
aaa75496 9452 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 9453 {
aaa75496 9454 /* Start a subfile for the current file of the state machine. */
debd256d
JB
9455 /* lh->include_dirs and lh->file_names are 0-based, but the
9456 directory and file name numbers in the statement program
9457 are 1-based. */
9458 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 9459 char *dir = NULL;
a738430d 9460
debd256d
JB
9461 if (fe->dir_index)
9462 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
9463
9464 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
9465 }
9466
a738430d 9467 /* Decode the table. */
c5aa993b 9468 while (!end_sequence)
c906108c
SS
9469 {
9470 op_code = read_1_byte (abfd, line_ptr);
9471 line_ptr += 1;
59205f5a
JB
9472 if (line_ptr > line_end)
9473 {
9474 dwarf2_debug_line_missing_end_sequence_complaint ();
9475 break;
9476 }
9aa1fe7e 9477
debd256d 9478 if (op_code >= lh->opcode_base)
6e70227d 9479 {
a738430d 9480 /* Special operand. */
debd256d 9481 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
9482 address += (((op_index + (adj_opcode / lh->line_range))
9483 / lh->maximum_ops_per_instruction)
9484 * lh->minimum_instruction_length);
9485 op_index = ((op_index + (adj_opcode / lh->line_range))
9486 % lh->maximum_ops_per_instruction);
debd256d 9487 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 9488 if (lh->num_file_names < file || file == 0)
25e43795 9489 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
9490 /* For now we ignore lines not starting on an
9491 instruction boundary. */
9492 else if (op_index == 0)
25e43795
DJ
9493 {
9494 lh->file_names[file - 1].included_p = 1;
ca5f395d 9495 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
9496 {
9497 if (last_subfile != current_subfile)
9498 {
9499 addr = gdbarch_addr_bits_remove (gdbarch, address);
9500 if (last_subfile)
9501 record_line (last_subfile, 0, addr);
9502 last_subfile = current_subfile;
9503 }
25e43795 9504 /* Append row to matrix using current values. */
fbf65064
UW
9505 addr = check_cu_functions (address, cu);
9506 addr = gdbarch_addr_bits_remove (gdbarch, addr);
9507 record_line (current_subfile, line, addr);
366da635 9508 }
25e43795 9509 }
ca5f395d 9510 basic_block = 0;
9aa1fe7e
GK
9511 }
9512 else switch (op_code)
c906108c
SS
9513 {
9514 case DW_LNS_extended_op:
a8c50c1f 9515 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 9516 line_ptr += bytes_read;
a8c50c1f 9517 extended_end = line_ptr + extended_len;
c906108c
SS
9518 extended_op = read_1_byte (abfd, line_ptr);
9519 line_ptr += 1;
9520 switch (extended_op)
9521 {
9522 case DW_LNE_end_sequence:
9523 end_sequence = 1;
c906108c
SS
9524 break;
9525 case DW_LNE_set_address:
e7c27a73 9526 address = read_address (abfd, line_ptr, cu, &bytes_read);
2dc7f7b3 9527 op_index = 0;
107d2387
AC
9528 line_ptr += bytes_read;
9529 address += baseaddr;
c906108c
SS
9530 break;
9531 case DW_LNE_define_file:
debd256d
JB
9532 {
9533 char *cur_file;
9534 unsigned int dir_index, mod_time, length;
6e70227d 9535
debd256d
JB
9536 cur_file = read_string (abfd, line_ptr, &bytes_read);
9537 line_ptr += bytes_read;
9538 dir_index =
9539 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9540 line_ptr += bytes_read;
9541 mod_time =
9542 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9543 line_ptr += bytes_read;
9544 length =
9545 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9546 line_ptr += bytes_read;
9547 add_file_name (lh, cur_file, dir_index, mod_time, length);
9548 }
c906108c 9549 break;
d0c6ba3d
CC
9550 case DW_LNE_set_discriminator:
9551 /* The discriminator is not interesting to the debugger;
9552 just ignore it. */
9553 line_ptr = extended_end;
9554 break;
c906108c 9555 default:
4d3c2250 9556 complaint (&symfile_complaints,
e2e0b3e5 9557 _("mangled .debug_line section"));
debd256d 9558 return;
c906108c 9559 }
a8c50c1f
DJ
9560 /* Make sure that we parsed the extended op correctly. If e.g.
9561 we expected a different address size than the producer used,
9562 we may have read the wrong number of bytes. */
9563 if (line_ptr != extended_end)
9564 {
9565 complaint (&symfile_complaints,
9566 _("mangled .debug_line section"));
9567 return;
9568 }
c906108c
SS
9569 break;
9570 case DW_LNS_copy:
59205f5a 9571 if (lh->num_file_names < file || file == 0)
25e43795
DJ
9572 dwarf2_debug_line_missing_file_complaint ();
9573 else
366da635 9574 {
25e43795 9575 lh->file_names[file - 1].included_p = 1;
ca5f395d 9576 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
9577 {
9578 if (last_subfile != current_subfile)
9579 {
9580 addr = gdbarch_addr_bits_remove (gdbarch, address);
9581 if (last_subfile)
9582 record_line (last_subfile, 0, addr);
9583 last_subfile = current_subfile;
9584 }
9585 addr = check_cu_functions (address, cu);
9586 addr = gdbarch_addr_bits_remove (gdbarch, addr);
9587 record_line (current_subfile, line, addr);
9588 }
366da635 9589 }
c906108c
SS
9590 basic_block = 0;
9591 break;
9592 case DW_LNS_advance_pc:
2dc7f7b3
TT
9593 {
9594 CORE_ADDR adjust
9595 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9596
9597 address += (((op_index + adjust)
9598 / lh->maximum_ops_per_instruction)
9599 * lh->minimum_instruction_length);
9600 op_index = ((op_index + adjust)
9601 % lh->maximum_ops_per_instruction);
9602 line_ptr += bytes_read;
9603 }
c906108c
SS
9604 break;
9605 case DW_LNS_advance_line:
9606 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
9607 line_ptr += bytes_read;
9608 break;
9609 case DW_LNS_set_file:
debd256d 9610 {
a738430d
MK
9611 /* The arrays lh->include_dirs and lh->file_names are
9612 0-based, but the directory and file name numbers in
9613 the statement program are 1-based. */
debd256d 9614 struct file_entry *fe;
4f1520fb 9615 char *dir = NULL;
a738430d 9616
debd256d
JB
9617 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9618 line_ptr += bytes_read;
59205f5a 9619 if (lh->num_file_names < file || file == 0)
25e43795
DJ
9620 dwarf2_debug_line_missing_file_complaint ();
9621 else
9622 {
9623 fe = &lh->file_names[file - 1];
9624 if (fe->dir_index)
9625 dir = lh->include_dirs[fe->dir_index - 1];
9626 if (!decode_for_pst_p)
9627 {
9628 last_subfile = current_subfile;
9629 dwarf2_start_subfile (fe->name, dir, comp_dir);
9630 }
9631 }
debd256d 9632 }
c906108c
SS
9633 break;
9634 case DW_LNS_set_column:
9635 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9636 line_ptr += bytes_read;
9637 break;
9638 case DW_LNS_negate_stmt:
9639 is_stmt = (!is_stmt);
9640 break;
9641 case DW_LNS_set_basic_block:
9642 basic_block = 1;
9643 break;
c2c6d25f
JM
9644 /* Add to the address register of the state machine the
9645 address increment value corresponding to special opcode
a738430d
MK
9646 255. I.e., this value is scaled by the minimum
9647 instruction length since special opcode 255 would have
9648 scaled the the increment. */
c906108c 9649 case DW_LNS_const_add_pc:
2dc7f7b3
TT
9650 {
9651 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
9652
9653 address += (((op_index + adjust)
9654 / lh->maximum_ops_per_instruction)
9655 * lh->minimum_instruction_length);
9656 op_index = ((op_index + adjust)
9657 % lh->maximum_ops_per_instruction);
9658 }
c906108c
SS
9659 break;
9660 case DW_LNS_fixed_advance_pc:
9661 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 9662 op_index = 0;
c906108c
SS
9663 line_ptr += 2;
9664 break;
9aa1fe7e 9665 default:
a738430d
MK
9666 {
9667 /* Unknown standard opcode, ignore it. */
9aa1fe7e 9668 int i;
a738430d 9669
debd256d 9670 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
9671 {
9672 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9673 line_ptr += bytes_read;
9674 }
9675 }
c906108c
SS
9676 }
9677 }
59205f5a
JB
9678 if (lh->num_file_names < file || file == 0)
9679 dwarf2_debug_line_missing_file_complaint ();
9680 else
9681 {
9682 lh->file_names[file - 1].included_p = 1;
9683 if (!decode_for_pst_p)
fbf65064
UW
9684 {
9685 addr = gdbarch_addr_bits_remove (gdbarch, address);
9686 record_line (current_subfile, 0, addr);
9687 }
59205f5a 9688 }
c906108c 9689 }
aaa75496
JB
9690
9691 if (decode_for_pst_p)
9692 {
9693 int file_index;
9694
9695 /* Now that we're done scanning the Line Header Program, we can
9696 create the psymtab of each included file. */
9697 for (file_index = 0; file_index < lh->num_file_names; file_index++)
9698 if (lh->file_names[file_index].included_p == 1)
9699 {
5b5464ad
JB
9700 const struct file_entry fe = lh->file_names [file_index];
9701 char *include_name = fe.name;
9702 char *dir_name = NULL;
9703 char *pst_filename = pst->filename;
9704
9705 if (fe.dir_index)
9706 dir_name = lh->include_dirs[fe.dir_index - 1];
9707
9708 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
9709 {
1754f103
MK
9710 include_name = concat (dir_name, SLASH_STRING,
9711 include_name, (char *)NULL);
5b5464ad
JB
9712 make_cleanup (xfree, include_name);
9713 }
9714
9715 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
9716 {
1754f103
MK
9717 pst_filename = concat (pst->dirname, SLASH_STRING,
9718 pst_filename, (char *)NULL);
5b5464ad
JB
9719 make_cleanup (xfree, pst_filename);
9720 }
9721
9722 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
9723 dwarf2_create_include_psymtab (include_name, pst, objfile);
9724 }
9725 }
cb1df416
DJ
9726 else
9727 {
9728 /* Make sure a symtab is created for every file, even files
9729 which contain only variables (i.e. no code with associated
9730 line numbers). */
9731
9732 int i;
9733 struct file_entry *fe;
9734
9735 for (i = 0; i < lh->num_file_names; i++)
9736 {
9737 char *dir = NULL;
9a619af0 9738
cb1df416
DJ
9739 fe = &lh->file_names[i];
9740 if (fe->dir_index)
9741 dir = lh->include_dirs[fe->dir_index - 1];
9742 dwarf2_start_subfile (fe->name, dir, comp_dir);
9743
9744 /* Skip the main file; we don't need it, and it must be
9745 allocated last, so that it will show up before the
9746 non-primary symtabs in the objfile's symtab list. */
9747 if (current_subfile == first_subfile)
9748 continue;
9749
9750 if (current_subfile->symtab == NULL)
9751 current_subfile->symtab = allocate_symtab (current_subfile->name,
9752 cu->objfile);
9753 fe->symtab = current_subfile->symtab;
9754 }
9755 }
c906108c
SS
9756}
9757
9758/* Start a subfile for DWARF. FILENAME is the name of the file and
9759 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
9760 or NULL if not known. COMP_DIR is the compilation directory for the
9761 linetable's compilation unit or NULL if not known.
c906108c
SS
9762 This routine tries to keep line numbers from identical absolute and
9763 relative file names in a common subfile.
9764
9765 Using the `list' example from the GDB testsuite, which resides in
9766 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
9767 of /srcdir/list0.c yields the following debugging information for list0.c:
9768
c5aa993b
JM
9769 DW_AT_name: /srcdir/list0.c
9770 DW_AT_comp_dir: /compdir
357e46e7 9771 files.files[0].name: list0.h
c5aa993b 9772 files.files[0].dir: /srcdir
357e46e7 9773 files.files[1].name: list0.c
c5aa993b 9774 files.files[1].dir: /srcdir
c906108c
SS
9775
9776 The line number information for list0.c has to end up in a single
4f1520fb
FR
9777 subfile, so that `break /srcdir/list0.c:1' works as expected.
9778 start_subfile will ensure that this happens provided that we pass the
9779 concatenation of files.files[1].dir and files.files[1].name as the
9780 subfile's name. */
c906108c
SS
9781
9782static void
4f1520fb 9783dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 9784{
4f1520fb
FR
9785 char *fullname;
9786
9787 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
9788 `start_symtab' will always pass the contents of DW_AT_comp_dir as
9789 second argument to start_subfile. To be consistent, we do the
9790 same here. In order not to lose the line information directory,
9791 we concatenate it to the filename when it makes sense.
9792 Note that the Dwarf3 standard says (speaking of filenames in line
9793 information): ``The directory index is ignored for file names
9794 that represent full path names''. Thus ignoring dirname in the
9795 `else' branch below isn't an issue. */
c906108c 9796
d5166ae1 9797 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
9798 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
9799 else
9800 fullname = filename;
c906108c 9801
4f1520fb
FR
9802 start_subfile (fullname, comp_dir);
9803
9804 if (fullname != filename)
9805 xfree (fullname);
c906108c
SS
9806}
9807
4c2df51b
DJ
9808static void
9809var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 9810 struct dwarf2_cu *cu)
4c2df51b 9811{
e7c27a73
DJ
9812 struct objfile *objfile = cu->objfile;
9813 struct comp_unit_head *cu_header = &cu->header;
9814
4c2df51b
DJ
9815 /* NOTE drow/2003-01-30: There used to be a comment and some special
9816 code here to turn a symbol with DW_AT_external and a
9817 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
9818 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
9819 with some versions of binutils) where shared libraries could have
9820 relocations against symbols in their debug information - the
9821 minimal symbol would have the right address, but the debug info
9822 would not. It's no longer necessary, because we will explicitly
9823 apply relocations when we read in the debug information now. */
9824
9825 /* A DW_AT_location attribute with no contents indicates that a
9826 variable has been optimized away. */
9827 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
9828 {
9829 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
9830 return;
9831 }
9832
9833 /* Handle one degenerate form of location expression specially, to
9834 preserve GDB's previous behavior when section offsets are
9835 specified. If this is just a DW_OP_addr then mark this symbol
9836 as LOC_STATIC. */
9837
9838 if (attr_form_is_block (attr)
9839 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
9840 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
9841 {
891d2f0b 9842 unsigned int dummy;
4c2df51b
DJ
9843
9844 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 9845 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 9846 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
9847 fixup_symbol_section (sym, objfile);
9848 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
9849 SYMBOL_SECTION (sym));
4c2df51b
DJ
9850 return;
9851 }
9852
9853 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
9854 expression evaluator, and use LOC_COMPUTED only when necessary
9855 (i.e. when the value of a register or memory location is
9856 referenced, or a thread-local block, etc.). Then again, it might
9857 not be worthwhile. I'm assuming that it isn't unless performance
9858 or memory numbers show me otherwise. */
9859
e7c27a73 9860 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
9861 SYMBOL_CLASS (sym) = LOC_COMPUTED;
9862}
9863
c906108c
SS
9864/* Given a pointer to a DWARF information entry, figure out if we need
9865 to make a symbol table entry for it, and if so, create a new entry
9866 and return a pointer to it.
9867 If TYPE is NULL, determine symbol type from the die, otherwise
2df3850c 9868 used the passed type. */
c906108c
SS
9869
9870static struct symbol *
e7c27a73 9871new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
c906108c 9872{
e7c27a73 9873 struct objfile *objfile = cu->objfile;
c906108c
SS
9874 struct symbol *sym = NULL;
9875 char *name;
9876 struct attribute *attr = NULL;
9877 struct attribute *attr2 = NULL;
e142c38c 9878 CORE_ADDR baseaddr;
edb3359d 9879 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
9880
9881 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 9882
94af9270 9883 name = dwarf2_name (die, cu);
c906108c
SS
9884 if (name)
9885 {
94af9270
KS
9886 const char *linkagename;
9887
4a146b47 9888 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
c906108c
SS
9889 sizeof (struct symbol));
9890 OBJSTAT (objfile, n_syms++);
9891 memset (sym, 0, sizeof (struct symbol));
2de7ced7
DJ
9892
9893 /* Cache this symbol's name and the name's demangled form (if any). */
e142c38c 9894 SYMBOL_LANGUAGE (sym) = cu->language;
94af9270
KS
9895 linkagename = dwarf2_physname (name, die, cu);
9896 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 9897
f55ee35c
JK
9898 /* Fortran does not have mangling standard and the mangling does differ
9899 between gfortran, iFort etc. */
9900 if (cu->language == language_fortran
b250c185 9901 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
9902 symbol_set_demangled_name (&(sym->ginfo),
9903 (char *) dwarf2_full_name (name, die, cu),
9904 NULL);
f55ee35c 9905
c906108c 9906 /* Default assumptions.
c5aa993b 9907 Use the passed type or decode it from the die. */
176620f1 9908 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 9909 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
9910 if (type != NULL)
9911 SYMBOL_TYPE (sym) = type;
9912 else
e7c27a73 9913 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
9914 attr = dwarf2_attr (die,
9915 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
9916 cu);
c906108c
SS
9917 if (attr)
9918 {
9919 SYMBOL_LINE (sym) = DW_UNSND (attr);
9920 }
cb1df416 9921
edb3359d
DJ
9922 attr = dwarf2_attr (die,
9923 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
9924 cu);
cb1df416
DJ
9925 if (attr)
9926 {
9927 int file_index = DW_UNSND (attr);
9a619af0 9928
cb1df416
DJ
9929 if (cu->line_header == NULL
9930 || file_index > cu->line_header->num_file_names)
9931 complaint (&symfile_complaints,
9932 _("file index out of range"));
1c3d648d 9933 else if (file_index > 0)
cb1df416
DJ
9934 {
9935 struct file_entry *fe;
9a619af0 9936
cb1df416
DJ
9937 fe = &cu->line_header->file_names[file_index - 1];
9938 SYMBOL_SYMTAB (sym) = fe->symtab;
9939 }
9940 }
9941
c906108c
SS
9942 switch (die->tag)
9943 {
9944 case DW_TAG_label:
e142c38c 9945 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
9946 if (attr)
9947 {
9948 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
9949 }
0f5238ed
TT
9950 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
9951 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 9952 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 9953 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
9954 break;
9955 case DW_TAG_subprogram:
9956 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
9957 finish_block. */
9958 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 9959 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
9960 if ((attr2 && (DW_UNSND (attr2) != 0))
9961 || cu->language == language_ada)
c906108c 9962 {
2cfa0c8d
JB
9963 /* Subprograms marked external are stored as a global symbol.
9964 Ada subprograms, whether marked external or not, are always
9965 stored as a global symbol, because we want to be able to
9966 access them globally. For instance, we want to be able
9967 to break on a nested subprogram without having to
9968 specify the context. */
c906108c
SS
9969 add_symbol_to_list (sym, &global_symbols);
9970 }
9971 else
9972 {
e142c38c 9973 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
9974 }
9975 break;
edb3359d
DJ
9976 case DW_TAG_inlined_subroutine:
9977 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
9978 finish_block. */
9979 SYMBOL_CLASS (sym) = LOC_BLOCK;
9980 SYMBOL_INLINED (sym) = 1;
9981 /* Do not add the symbol to any lists. It will be found via
9982 BLOCK_FUNCTION from the blockvector. */
9983 break;
c906108c 9984 case DW_TAG_variable:
254e6b9e 9985 case DW_TAG_member:
c906108c
SS
9986 /* Compilation with minimal debug info may result in variables
9987 with missing type entries. Change the misleading `void' type
9988 to something sensible. */
9989 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 9990 SYMBOL_TYPE (sym)
46bf5051 9991 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 9992
e142c38c 9993 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
9994 /* In the case of DW_TAG_member, we should only be called for
9995 static const members. */
9996 if (die->tag == DW_TAG_member)
9997 {
3863f96c
DE
9998 /* dwarf2_add_field uses die_is_declaration,
9999 so we do the same. */
254e6b9e
DE
10000 gdb_assert (die_is_declaration (die, cu));
10001 gdb_assert (attr);
10002 }
c906108c
SS
10003 if (attr)
10004 {
e7c27a73 10005 dwarf2_const_value (attr, sym, cu);
e142c38c 10006 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c
SS
10007 if (attr2 && (DW_UNSND (attr2) != 0))
10008 add_symbol_to_list (sym, &global_symbols);
10009 else
e142c38c 10010 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10011 break;
10012 }
e142c38c 10013 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10014 if (attr)
10015 {
e7c27a73 10016 var_decode_location (attr, sym, cu);
e142c38c 10017 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
10018 if (SYMBOL_CLASS (sym) == LOC_STATIC
10019 && SYMBOL_VALUE_ADDRESS (sym) == 0
10020 && !dwarf2_per_objfile->has_section_at_zero)
10021 {
10022 /* When a static variable is eliminated by the linker,
10023 the corresponding debug information is not stripped
10024 out, but the variable address is set to null;
10025 do not add such variables into symbol table. */
10026 }
10027 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68
TT
10028 {
10029 struct pending **list_to_add;
10030
f55ee35c
JK
10031 /* Workaround gfortran PR debug/40040 - it uses
10032 DW_AT_location for variables in -fPIC libraries which may
10033 get overriden by other libraries/executable and get
10034 a different address. Resolve it by the minimal symbol
10035 which may come from inferior's executable using copy
10036 relocation. Make this workaround only for gfortran as for
10037 other compilers GDB cannot guess the minimal symbol
10038 Fortran mangling kind. */
10039 if (cu->language == language_fortran && die->parent
10040 && die->parent->tag == DW_TAG_module
10041 && cu->producer
10042 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10043 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10044
1c809c68
TT
10045 /* A variable with DW_AT_external is never static,
10046 but it may be block-scoped. */
10047 list_to_add = (cu->list_in_scope == &file_symbols
10048 ? &global_symbols : cu->list_in_scope);
10049 add_symbol_to_list (sym, list_to_add);
10050 }
c906108c 10051 else
e142c38c 10052 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10053 }
10054 else
10055 {
10056 /* We do not know the address of this symbol.
c5aa993b
JM
10057 If it is an external symbol and we have type information
10058 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10059 The address of the variable will then be determined from
10060 the minimal symbol table whenever the variable is
10061 referenced. */
e142c38c 10062 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 10063 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 10064 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 10065 {
0fe7935b
DJ
10066 struct pending **list_to_add;
10067
10068 /* A variable with DW_AT_external is never static, but it
10069 may be block-scoped. */
10070 list_to_add = (cu->list_in_scope == &file_symbols
10071 ? &global_symbols : cu->list_in_scope);
10072
c906108c 10073 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
0fe7935b 10074 add_symbol_to_list (sym, list_to_add);
c906108c 10075 }
442ddf59
JK
10076 else if (!die_is_declaration (die, cu))
10077 {
10078 /* Use the default LOC_OPTIMIZED_OUT class. */
10079 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10080 add_symbol_to_list (sym, cu->list_in_scope);
10081 }
c906108c
SS
10082 }
10083 break;
10084 case DW_TAG_formal_parameter:
edb3359d
DJ
10085 /* If we are inside a function, mark this as an argument. If
10086 not, we might be looking at an argument to an inlined function
10087 when we do not have enough information to show inlined frames;
10088 pretend it's a local variable in that case so that the user can
10089 still see it. */
10090 if (context_stack_depth > 0
10091 && context_stack[context_stack_depth - 1].name != NULL)
10092 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 10093 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10094 if (attr)
10095 {
e7c27a73 10096 var_decode_location (attr, sym, cu);
c906108c 10097 }
e142c38c 10098 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10099 if (attr)
10100 {
e7c27a73 10101 dwarf2_const_value (attr, sym, cu);
c906108c 10102 }
f346a30d
PM
10103 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10104 if (attr && DW_UNSND (attr))
10105 {
10106 struct type *ref_type;
10107
10108 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10109 SYMBOL_TYPE (sym) = ref_type;
10110 }
10111
e142c38c 10112 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10113 break;
10114 case DW_TAG_unspecified_parameters:
10115 /* From varargs functions; gdb doesn't seem to have any
10116 interest in this information, so just ignore it for now.
10117 (FIXME?) */
10118 break;
10119 case DW_TAG_class_type:
680b30c7 10120 case DW_TAG_interface_type:
c906108c
SS
10121 case DW_TAG_structure_type:
10122 case DW_TAG_union_type:
72019c9c 10123 case DW_TAG_set_type:
c906108c
SS
10124 case DW_TAG_enumeration_type:
10125 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10126 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 10127
63d06c5c 10128 {
987504bb 10129 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
10130 really ever be static objects: otherwise, if you try
10131 to, say, break of a class's method and you're in a file
10132 which doesn't mention that class, it won't work unless
10133 the check for all static symbols in lookup_symbol_aux
10134 saves you. See the OtherFileClass tests in
10135 gdb.c++/namespace.exp. */
10136
10137 struct pending **list_to_add;
10138
e142c38c 10139 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10140 && (cu->language == language_cplus
10141 || cu->language == language_java)
e142c38c 10142 ? &global_symbols : cu->list_in_scope);
6e70227d 10143
63d06c5c
DC
10144 add_symbol_to_list (sym, list_to_add);
10145
10146 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 10147 defines a typedef for "foo". A Java class declaration also
5eeb2539 10148 defines a typedef for the class. */
987504bb 10149 if (cu->language == language_cplus
8c6860bb
JB
10150 || cu->language == language_java
10151 || cu->language == language_ada)
63d06c5c 10152 {
d8151005
DJ
10153 /* The symbol's name is already allocated along with
10154 this objfile, so we don't need to duplicate it for
10155 the type. */
63d06c5c 10156 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 10157 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
10158 }
10159 }
c906108c
SS
10160 break;
10161 case DW_TAG_typedef:
63d06c5c
DC
10162 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10163 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 10164 add_symbol_to_list (sym, cu->list_in_scope);
63d06c5c 10165 break;
c906108c 10166 case DW_TAG_base_type:
a02abb62 10167 case DW_TAG_subrange_type:
c906108c 10168 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 10169 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 10170 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
10171 break;
10172 case DW_TAG_enumerator:
e142c38c 10173 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
10174 if (attr)
10175 {
e7c27a73 10176 dwarf2_const_value (attr, sym, cu);
c906108c 10177 }
63d06c5c
DC
10178 {
10179 /* NOTE: carlton/2003-11-10: See comment above in the
10180 DW_TAG_class_type, etc. block. */
10181
10182 struct pending **list_to_add;
10183
e142c38c 10184 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
10185 && (cu->language == language_cplus
10186 || cu->language == language_java)
e142c38c 10187 ? &global_symbols : cu->list_in_scope);
6e70227d 10188
63d06c5c
DC
10189 add_symbol_to_list (sym, list_to_add);
10190 }
c906108c 10191 break;
5c4e30ca
DC
10192 case DW_TAG_namespace:
10193 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10194 add_symbol_to_list (sym, &global_symbols);
10195 break;
c906108c
SS
10196 default:
10197 /* Not a tag we recognize. Hopefully we aren't processing
10198 trash data, but since we must specifically ignore things
10199 we don't recognize, there is nothing else we should do at
10200 this point. */
e2e0b3e5 10201 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 10202 dwarf_tag_name (die->tag));
c906108c
SS
10203 break;
10204 }
df8a16a1
DJ
10205
10206 /* For the benefit of old versions of GCC, check for anonymous
10207 namespaces based on the demangled name. */
10208 if (!processing_has_namespace_info
94af9270 10209 && cu->language == language_cplus)
df8a16a1 10210 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
10211 }
10212 return (sym);
10213}
10214
10215/* Copy constant value from an attribute to a symbol. */
10216
10217static void
107d2387 10218dwarf2_const_value (struct attribute *attr, struct symbol *sym,
e7c27a73 10219 struct dwarf2_cu *cu)
c906108c 10220{
e7c27a73
DJ
10221 struct objfile *objfile = cu->objfile;
10222 struct comp_unit_head *cu_header = &cu->header;
e17a4113
UW
10223 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10224 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
c906108c
SS
10225 struct dwarf_block *blk;
10226
10227 switch (attr->form)
10228 {
10229 case DW_FORM_addr:
ac56253d
TT
10230 {
10231 struct dwarf2_locexpr_baton *baton;
10232 gdb_byte *data;
10233
10234 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
10235 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
10236 cu_header->addr_size,
10237 TYPE_LENGTH (SYMBOL_TYPE
10238 (sym)));
10239 /* Symbols of this form are reasonably rare, so we just
10240 piggyback on the existing location code rather than writing
10241 a new implementation of symbol_computed_ops. */
10242 baton = obstack_alloc (&objfile->objfile_obstack,
10243 sizeof (struct dwarf2_locexpr_baton));
10244 baton->per_cu = cu->per_cu;
10245 gdb_assert (baton->per_cu);
10246
10247 baton->size = 2 + cu_header->addr_size;
10248 data = obstack_alloc (&objfile->objfile_obstack, baton->size);
10249 baton->data = data;
10250
10251 data[0] = DW_OP_addr;
10252 store_unsigned_integer (&data[1], cu_header->addr_size,
10253 byte_order, DW_ADDR (attr));
10254 data[cu_header->addr_size + 1] = DW_OP_stack_value;
10255
10256 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10257 SYMBOL_LOCATION_BATON (sym) = baton;
10258 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10259 }
c906108c 10260 break;
4ac36638 10261 case DW_FORM_string:
93b5768b
PA
10262 case DW_FORM_strp:
10263 /* DW_STRING is already allocated on the obstack, point directly
10264 to it. */
10265 SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
10266 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10267 break;
c906108c
SS
10268 case DW_FORM_block1:
10269 case DW_FORM_block2:
10270 case DW_FORM_block4:
10271 case DW_FORM_block:
2dc7f7b3 10272 case DW_FORM_exprloc:
c906108c
SS
10273 blk = DW_BLOCK (attr);
10274 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
3567439c 10275 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
10276 blk->size,
10277 TYPE_LENGTH (SYMBOL_TYPE
10278 (sym)));
4e38b386 10279 SYMBOL_VALUE_BYTES (sym) =
4a146b47 10280 obstack_alloc (&objfile->objfile_obstack, blk->size);
c906108c
SS
10281 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
10282 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10283 break;
2df3850c
JM
10284
10285 /* The DW_AT_const_value attributes are supposed to carry the
10286 symbol's value "represented as it would be on the target
10287 architecture." By the time we get here, it's already been
10288 converted to host endianness, so we just need to sign- or
10289 zero-extend it as appropriate. */
10290 case DW_FORM_data1:
10291 dwarf2_const_value_data (attr, sym, 8);
10292 break;
c906108c 10293 case DW_FORM_data2:
2df3850c
JM
10294 dwarf2_const_value_data (attr, sym, 16);
10295 break;
c906108c 10296 case DW_FORM_data4:
2df3850c
JM
10297 dwarf2_const_value_data (attr, sym, 32);
10298 break;
c906108c 10299 case DW_FORM_data8:
2df3850c
JM
10300 dwarf2_const_value_data (attr, sym, 64);
10301 break;
10302
c906108c 10303 case DW_FORM_sdata:
2df3850c
JM
10304 SYMBOL_VALUE (sym) = DW_SND (attr);
10305 SYMBOL_CLASS (sym) = LOC_CONST;
10306 break;
10307
c906108c
SS
10308 case DW_FORM_udata:
10309 SYMBOL_VALUE (sym) = DW_UNSND (attr);
10310 SYMBOL_CLASS (sym) = LOC_CONST;
10311 break;
2df3850c 10312
c906108c 10313 default:
4d3c2250 10314 complaint (&symfile_complaints,
e2e0b3e5 10315 _("unsupported const value attribute form: '%s'"),
4d3c2250 10316 dwarf_form_name (attr->form));
c906108c
SS
10317 SYMBOL_VALUE (sym) = 0;
10318 SYMBOL_CLASS (sym) = LOC_CONST;
10319 break;
10320 }
10321}
10322
2df3850c
JM
10323
10324/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
10325 or zero-extend it as appropriate for the symbol's type. */
10326static void
10327dwarf2_const_value_data (struct attribute *attr,
10328 struct symbol *sym,
10329 int bits)
10330{
10331 LONGEST l = DW_UNSND (attr);
10332
10333 if (bits < sizeof (l) * 8)
10334 {
10335 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
10336 l &= ((LONGEST) 1 << bits) - 1;
10337 else
bf9198f1 10338 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
2df3850c
JM
10339 }
10340
10341 SYMBOL_VALUE (sym) = l;
10342 SYMBOL_CLASS (sym) = LOC_CONST;
10343}
10344
10345
c906108c
SS
10346/* Return the type of the die in question using its DW_AT_type attribute. */
10347
10348static struct type *
e7c27a73 10349die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10350{
c906108c
SS
10351 struct attribute *type_attr;
10352 struct die_info *type_die;
c906108c 10353
e142c38c 10354 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
10355 if (!type_attr)
10356 {
10357 /* A missing DW_AT_type represents a void type. */
46bf5051 10358 return objfile_type (cu->objfile)->builtin_void;
c906108c 10359 }
348e048f
DE
10360
10361 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10b3939b 10362
33ac96f0 10363 return tag_type_to_type (type_die, cu);
c906108c
SS
10364}
10365
b4ba55a1
JB
10366/* True iff CU's producer generates GNAT Ada auxiliary information
10367 that allows to find parallel types through that information instead
10368 of having to do expensive parallel lookups by type name. */
10369
10370static int
10371need_gnat_info (struct dwarf2_cu *cu)
10372{
10373 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10374 of GNAT produces this auxiliary information, without any indication
10375 that it is produced. Part of enhancing the FSF version of GNAT
10376 to produce that information will be to put in place an indicator
10377 that we can use in order to determine whether the descriptive type
10378 info is available or not. One suggestion that has been made is
10379 to use a new attribute, attached to the CU die. For now, assume
10380 that the descriptive type info is not available. */
10381 return 0;
10382}
10383
10384
10385/* Return the auxiliary type of the die in question using its
10386 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
10387 attribute is not present. */
10388
10389static struct type *
10390die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10391{
b4ba55a1
JB
10392 struct attribute *type_attr;
10393 struct die_info *type_die;
10394
10395 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10396 if (!type_attr)
10397 return NULL;
10398
10399 type_die = follow_die_ref (die, type_attr, &cu);
33ac96f0 10400 return tag_type_to_type (type_die, cu);
b4ba55a1
JB
10401}
10402
10403/* If DIE has a descriptive_type attribute, then set the TYPE's
10404 descriptive type accordingly. */
10405
10406static void
10407set_descriptive_type (struct type *type, struct die_info *die,
10408 struct dwarf2_cu *cu)
10409{
10410 struct type *descriptive_type = die_descriptive_type (die, cu);
10411
10412 if (descriptive_type)
10413 {
10414 ALLOCATE_GNAT_AUX_TYPE (type);
10415 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10416 }
10417}
10418
c906108c
SS
10419/* Return the containing type of the die in question using its
10420 DW_AT_containing_type attribute. */
10421
10422static struct type *
e7c27a73 10423die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10424{
c906108c 10425 struct attribute *type_attr;
33ac96f0 10426 struct die_info *type_die;
c906108c 10427
e142c38c 10428 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
10429 if (!type_attr)
10430 error (_("Dwarf Error: Problem turning containing type into gdb type "
10431 "[in module %s]"), cu->objfile->name);
10432
10433 type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10434 return tag_type_to_type (type_die, cu);
c906108c
SS
10435}
10436
c906108c 10437static struct type *
e7c27a73 10438tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10439{
f792889a
DJ
10440 struct type *this_type;
10441
10442 this_type = read_type_die (die, cu);
10443 if (!this_type)
c906108c 10444 {
b00fdb78
TT
10445 char *message, *saved;
10446
10447 /* read_type_die already issued a complaint. */
10448 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
10449 cu->objfile->name,
10450 cu->header.offset,
10451 die->offset);
10452 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
10453 message, strlen (message));
10454 xfree (message);
10455
10456 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
c906108c 10457 }
f792889a 10458 return this_type;
c906108c
SS
10459}
10460
f792889a 10461static struct type *
e7c27a73 10462read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10463{
f792889a
DJ
10464 struct type *this_type;
10465
10466 this_type = get_die_type (die, cu);
10467 if (this_type)
10468 return this_type;
10469
c906108c
SS
10470 switch (die->tag)
10471 {
10472 case DW_TAG_class_type:
680b30c7 10473 case DW_TAG_interface_type:
c906108c
SS
10474 case DW_TAG_structure_type:
10475 case DW_TAG_union_type:
f792889a 10476 this_type = read_structure_type (die, cu);
c906108c
SS
10477 break;
10478 case DW_TAG_enumeration_type:
f792889a 10479 this_type = read_enumeration_type (die, cu);
c906108c
SS
10480 break;
10481 case DW_TAG_subprogram:
10482 case DW_TAG_subroutine_type:
edb3359d 10483 case DW_TAG_inlined_subroutine:
f792889a 10484 this_type = read_subroutine_type (die, cu);
c906108c
SS
10485 break;
10486 case DW_TAG_array_type:
f792889a 10487 this_type = read_array_type (die, cu);
c906108c 10488 break;
72019c9c 10489 case DW_TAG_set_type:
f792889a 10490 this_type = read_set_type (die, cu);
72019c9c 10491 break;
c906108c 10492 case DW_TAG_pointer_type:
f792889a 10493 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
10494 break;
10495 case DW_TAG_ptr_to_member_type:
f792889a 10496 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
10497 break;
10498 case DW_TAG_reference_type:
f792889a 10499 this_type = read_tag_reference_type (die, cu);
c906108c
SS
10500 break;
10501 case DW_TAG_const_type:
f792889a 10502 this_type = read_tag_const_type (die, cu);
c906108c
SS
10503 break;
10504 case DW_TAG_volatile_type:
f792889a 10505 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
10506 break;
10507 case DW_TAG_string_type:
f792889a 10508 this_type = read_tag_string_type (die, cu);
c906108c
SS
10509 break;
10510 case DW_TAG_typedef:
f792889a 10511 this_type = read_typedef (die, cu);
c906108c 10512 break;
a02abb62 10513 case DW_TAG_subrange_type:
f792889a 10514 this_type = read_subrange_type (die, cu);
a02abb62 10515 break;
c906108c 10516 case DW_TAG_base_type:
f792889a 10517 this_type = read_base_type (die, cu);
c906108c 10518 break;
81a17f79 10519 case DW_TAG_unspecified_type:
f792889a 10520 this_type = read_unspecified_type (die, cu);
81a17f79 10521 break;
0114d602
DJ
10522 case DW_TAG_namespace:
10523 this_type = read_namespace_type (die, cu);
10524 break;
f55ee35c
JK
10525 case DW_TAG_module:
10526 this_type = read_module_type (die, cu);
10527 break;
c906108c 10528 default:
a1f5b845 10529 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 10530 dwarf_tag_name (die->tag));
c906108c
SS
10531 break;
10532 }
63d06c5c 10533
f792889a 10534 return this_type;
63d06c5c
DC
10535}
10536
fdde2d81 10537/* Return the name of the namespace/class that DIE is defined within,
0114d602 10538 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 10539
0114d602
DJ
10540 For example, if we're within the method foo() in the following
10541 code:
10542
10543 namespace N {
10544 class C {
10545 void foo () {
10546 }
10547 };
10548 }
10549
10550 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
10551
10552static char *
e142c38c 10553determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 10554{
0114d602
DJ
10555 struct die_info *parent, *spec_die;
10556 struct dwarf2_cu *spec_cu;
10557 struct type *parent_type;
63d06c5c 10558
f55ee35c
JK
10559 if (cu->language != language_cplus && cu->language != language_java
10560 && cu->language != language_fortran)
0114d602
DJ
10561 return "";
10562
10563 /* We have to be careful in the presence of DW_AT_specification.
10564 For example, with GCC 3.4, given the code
10565
10566 namespace N {
10567 void foo() {
10568 // Definition of N::foo.
10569 }
10570 }
10571
10572 then we'll have a tree of DIEs like this:
10573
10574 1: DW_TAG_compile_unit
10575 2: DW_TAG_namespace // N
10576 3: DW_TAG_subprogram // declaration of N::foo
10577 4: DW_TAG_subprogram // definition of N::foo
10578 DW_AT_specification // refers to die #3
10579
10580 Thus, when processing die #4, we have to pretend that we're in
10581 the context of its DW_AT_specification, namely the contex of die
10582 #3. */
10583 spec_cu = cu;
10584 spec_die = die_specification (die, &spec_cu);
10585 if (spec_die == NULL)
10586 parent = die->parent;
10587 else
63d06c5c 10588 {
0114d602
DJ
10589 parent = spec_die->parent;
10590 cu = spec_cu;
63d06c5c 10591 }
0114d602
DJ
10592
10593 if (parent == NULL)
10594 return "";
63d06c5c 10595 else
0114d602
DJ
10596 switch (parent->tag)
10597 {
63d06c5c 10598 case DW_TAG_namespace:
0114d602 10599 parent_type = read_type_die (parent, cu);
acebe513
UW
10600 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
10601 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
10602 Work around this problem here. */
10603 if (cu->language == language_cplus
10604 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
10605 return "";
0114d602
DJ
10606 /* We give a name to even anonymous namespaces. */
10607 return TYPE_TAG_NAME (parent_type);
63d06c5c 10608 case DW_TAG_class_type:
680b30c7 10609 case DW_TAG_interface_type:
63d06c5c 10610 case DW_TAG_structure_type:
0114d602 10611 case DW_TAG_union_type:
f55ee35c 10612 case DW_TAG_module:
0114d602
DJ
10613 parent_type = read_type_die (parent, cu);
10614 if (TYPE_TAG_NAME (parent_type) != NULL)
10615 return TYPE_TAG_NAME (parent_type);
10616 else
10617 /* An anonymous structure is only allowed non-static data
10618 members; no typedefs, no member functions, et cetera.
10619 So it does not need a prefix. */
10620 return "";
63d06c5c 10621 default:
8176b9b8 10622 return determine_prefix (parent, cu);
63d06c5c 10623 }
63d06c5c
DC
10624}
10625
987504bb
JJ
10626/* Return a newly-allocated string formed by concatenating PREFIX and
10627 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
10628 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
10629 perform an obconcat, otherwise allocate storage for the result. The CU argument
10630 is used to determine the language and hence, the appropriate separator. */
10631
f55ee35c 10632#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
10633
10634static char *
f55ee35c
JK
10635typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
10636 int physname, struct dwarf2_cu *cu)
63d06c5c 10637{
f55ee35c 10638 const char *lead = "";
5c315b68 10639 const char *sep;
63d06c5c 10640
987504bb
JJ
10641 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
10642 sep = "";
10643 else if (cu->language == language_java)
10644 sep = ".";
f55ee35c
JK
10645 else if (cu->language == language_fortran && physname)
10646 {
10647 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
10648 DW_AT_MIPS_linkage_name is preferred and used instead. */
10649
10650 lead = "__";
10651 sep = "_MOD_";
10652 }
987504bb
JJ
10653 else
10654 sep = "::";
63d06c5c 10655
6dd47d34
DE
10656 if (prefix == NULL)
10657 prefix = "";
10658 if (suffix == NULL)
10659 suffix = "";
10660
987504bb
JJ
10661 if (obs == NULL)
10662 {
10663 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 10664
f55ee35c
JK
10665 strcpy (retval, lead);
10666 strcat (retval, prefix);
6dd47d34
DE
10667 strcat (retval, sep);
10668 strcat (retval, suffix);
63d06c5c
DC
10669 return retval;
10670 }
987504bb
JJ
10671 else
10672 {
10673 /* We have an obstack. */
f55ee35c 10674 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 10675 }
63d06c5c
DC
10676}
10677
c906108c
SS
10678/* Return sibling of die, NULL if no sibling. */
10679
f9aca02d 10680static struct die_info *
fba45db2 10681sibling_die (struct die_info *die)
c906108c 10682{
639d11d3 10683 return die->sibling;
c906108c
SS
10684}
10685
71c25dea
TT
10686/* Get name of a die, return NULL if not found. */
10687
10688static char *
10689dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
10690 struct obstack *obstack)
10691{
10692 if (name && cu->language == language_cplus)
10693 {
10694 char *canon_name = cp_canonicalize_string (name);
10695
10696 if (canon_name != NULL)
10697 {
10698 if (strcmp (canon_name, name) != 0)
10699 name = obsavestring (canon_name, strlen (canon_name),
10700 obstack);
10701 xfree (canon_name);
10702 }
10703 }
10704
10705 return name;
c906108c
SS
10706}
10707
9219021c
DC
10708/* Get name of a die, return NULL if not found. */
10709
10710static char *
e142c38c 10711dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
10712{
10713 struct attribute *attr;
10714
e142c38c 10715 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
10716 if (!attr || !DW_STRING (attr))
10717 return NULL;
10718
10719 switch (die->tag)
10720 {
10721 case DW_TAG_compile_unit:
10722 /* Compilation units have a DW_AT_name that is a filename, not
10723 a source language identifier. */
10724 case DW_TAG_enumeration_type:
10725 case DW_TAG_enumerator:
10726 /* These tags always have simple identifiers already; no need
10727 to canonicalize them. */
10728 return DW_STRING (attr);
907af001 10729
418835cc
KS
10730 case DW_TAG_subprogram:
10731 /* Java constructors will all be named "<init>", so return
10732 the class name when we see this special case. */
10733 if (cu->language == language_java
10734 && DW_STRING (attr) != NULL
10735 && strcmp (DW_STRING (attr), "<init>") == 0)
10736 {
10737 struct dwarf2_cu *spec_cu = cu;
10738 struct die_info *spec_die;
10739
10740 /* GCJ will output '<init>' for Java constructor names.
10741 For this special case, return the name of the parent class. */
10742
10743 /* GCJ may output suprogram DIEs with AT_specification set.
10744 If so, use the name of the specified DIE. */
10745 spec_die = die_specification (die, &spec_cu);
10746 if (spec_die != NULL)
10747 return dwarf2_name (spec_die, spec_cu);
10748
10749 do
10750 {
10751 die = die->parent;
10752 if (die->tag == DW_TAG_class_type)
10753 return dwarf2_name (die, cu);
10754 }
10755 while (die->tag != DW_TAG_compile_unit);
10756 }
907af001
UW
10757 break;
10758
10759 case DW_TAG_class_type:
10760 case DW_TAG_interface_type:
10761 case DW_TAG_structure_type:
10762 case DW_TAG_union_type:
10763 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
10764 structures or unions. These were of the form "._%d" in GCC 4.1,
10765 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
10766 and GCC 4.4. We work around this problem by ignoring these. */
10767 if (strncmp (DW_STRING (attr), "._", 2) == 0
10768 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
10769 return NULL;
10770 break;
10771
71c25dea 10772 default:
907af001
UW
10773 break;
10774 }
10775
10776 if (!DW_STRING_IS_CANONICAL (attr))
10777 {
10778 DW_STRING (attr)
10779 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
10780 &cu->objfile->objfile_obstack);
10781 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 10782 }
907af001 10783 return DW_STRING (attr);
9219021c
DC
10784}
10785
10786/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
10787 is none. *EXT_CU is the CU containing DIE on input, and the CU
10788 containing the return value on output. */
9219021c
DC
10789
10790static struct die_info *
f2f0e013 10791dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
10792{
10793 struct attribute *attr;
9219021c 10794
f2f0e013 10795 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
10796 if (attr == NULL)
10797 return NULL;
10798
f2f0e013 10799 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
10800}
10801
c906108c
SS
10802/* Convert a DIE tag into its string name. */
10803
10804static char *
aa1ee363 10805dwarf_tag_name (unsigned tag)
c906108c
SS
10806{
10807 switch (tag)
10808 {
10809 case DW_TAG_padding:
10810 return "DW_TAG_padding";
10811 case DW_TAG_array_type:
10812 return "DW_TAG_array_type";
10813 case DW_TAG_class_type:
10814 return "DW_TAG_class_type";
10815 case DW_TAG_entry_point:
10816 return "DW_TAG_entry_point";
10817 case DW_TAG_enumeration_type:
10818 return "DW_TAG_enumeration_type";
10819 case DW_TAG_formal_parameter:
10820 return "DW_TAG_formal_parameter";
10821 case DW_TAG_imported_declaration:
10822 return "DW_TAG_imported_declaration";
10823 case DW_TAG_label:
10824 return "DW_TAG_label";
10825 case DW_TAG_lexical_block:
10826 return "DW_TAG_lexical_block";
10827 case DW_TAG_member:
10828 return "DW_TAG_member";
10829 case DW_TAG_pointer_type:
10830 return "DW_TAG_pointer_type";
10831 case DW_TAG_reference_type:
10832 return "DW_TAG_reference_type";
10833 case DW_TAG_compile_unit:
10834 return "DW_TAG_compile_unit";
10835 case DW_TAG_string_type:
10836 return "DW_TAG_string_type";
10837 case DW_TAG_structure_type:
10838 return "DW_TAG_structure_type";
10839 case DW_TAG_subroutine_type:
10840 return "DW_TAG_subroutine_type";
10841 case DW_TAG_typedef:
10842 return "DW_TAG_typedef";
10843 case DW_TAG_union_type:
10844 return "DW_TAG_union_type";
10845 case DW_TAG_unspecified_parameters:
10846 return "DW_TAG_unspecified_parameters";
10847 case DW_TAG_variant:
10848 return "DW_TAG_variant";
10849 case DW_TAG_common_block:
10850 return "DW_TAG_common_block";
10851 case DW_TAG_common_inclusion:
10852 return "DW_TAG_common_inclusion";
10853 case DW_TAG_inheritance:
10854 return "DW_TAG_inheritance";
10855 case DW_TAG_inlined_subroutine:
10856 return "DW_TAG_inlined_subroutine";
10857 case DW_TAG_module:
10858 return "DW_TAG_module";
10859 case DW_TAG_ptr_to_member_type:
10860 return "DW_TAG_ptr_to_member_type";
10861 case DW_TAG_set_type:
10862 return "DW_TAG_set_type";
10863 case DW_TAG_subrange_type:
10864 return "DW_TAG_subrange_type";
10865 case DW_TAG_with_stmt:
10866 return "DW_TAG_with_stmt";
10867 case DW_TAG_access_declaration:
10868 return "DW_TAG_access_declaration";
10869 case DW_TAG_base_type:
10870 return "DW_TAG_base_type";
10871 case DW_TAG_catch_block:
10872 return "DW_TAG_catch_block";
10873 case DW_TAG_const_type:
10874 return "DW_TAG_const_type";
10875 case DW_TAG_constant:
10876 return "DW_TAG_constant";
10877 case DW_TAG_enumerator:
10878 return "DW_TAG_enumerator";
10879 case DW_TAG_file_type:
10880 return "DW_TAG_file_type";
10881 case DW_TAG_friend:
10882 return "DW_TAG_friend";
10883 case DW_TAG_namelist:
10884 return "DW_TAG_namelist";
10885 case DW_TAG_namelist_item:
10886 return "DW_TAG_namelist_item";
10887 case DW_TAG_packed_type:
10888 return "DW_TAG_packed_type";
10889 case DW_TAG_subprogram:
10890 return "DW_TAG_subprogram";
10891 case DW_TAG_template_type_param:
10892 return "DW_TAG_template_type_param";
10893 case DW_TAG_template_value_param:
10894 return "DW_TAG_template_value_param";
10895 case DW_TAG_thrown_type:
10896 return "DW_TAG_thrown_type";
10897 case DW_TAG_try_block:
10898 return "DW_TAG_try_block";
10899 case DW_TAG_variant_part:
10900 return "DW_TAG_variant_part";
10901 case DW_TAG_variable:
10902 return "DW_TAG_variable";
10903 case DW_TAG_volatile_type:
10904 return "DW_TAG_volatile_type";
d9fa45fe
DC
10905 case DW_TAG_dwarf_procedure:
10906 return "DW_TAG_dwarf_procedure";
10907 case DW_TAG_restrict_type:
10908 return "DW_TAG_restrict_type";
10909 case DW_TAG_interface_type:
10910 return "DW_TAG_interface_type";
10911 case DW_TAG_namespace:
10912 return "DW_TAG_namespace";
10913 case DW_TAG_imported_module:
10914 return "DW_TAG_imported_module";
10915 case DW_TAG_unspecified_type:
10916 return "DW_TAG_unspecified_type";
10917 case DW_TAG_partial_unit:
10918 return "DW_TAG_partial_unit";
10919 case DW_TAG_imported_unit:
10920 return "DW_TAG_imported_unit";
b7619582
GF
10921 case DW_TAG_condition:
10922 return "DW_TAG_condition";
10923 case DW_TAG_shared_type:
10924 return "DW_TAG_shared_type";
348e048f
DE
10925 case DW_TAG_type_unit:
10926 return "DW_TAG_type_unit";
c906108c
SS
10927 case DW_TAG_MIPS_loop:
10928 return "DW_TAG_MIPS_loop";
b7619582
GF
10929 case DW_TAG_HP_array_descriptor:
10930 return "DW_TAG_HP_array_descriptor";
c906108c
SS
10931 case DW_TAG_format_label:
10932 return "DW_TAG_format_label";
10933 case DW_TAG_function_template:
10934 return "DW_TAG_function_template";
10935 case DW_TAG_class_template:
10936 return "DW_TAG_class_template";
b7619582
GF
10937 case DW_TAG_GNU_BINCL:
10938 return "DW_TAG_GNU_BINCL";
10939 case DW_TAG_GNU_EINCL:
10940 return "DW_TAG_GNU_EINCL";
10941 case DW_TAG_upc_shared_type:
10942 return "DW_TAG_upc_shared_type";
10943 case DW_TAG_upc_strict_type:
10944 return "DW_TAG_upc_strict_type";
10945 case DW_TAG_upc_relaxed_type:
10946 return "DW_TAG_upc_relaxed_type";
10947 case DW_TAG_PGI_kanji_type:
10948 return "DW_TAG_PGI_kanji_type";
10949 case DW_TAG_PGI_interface_block:
10950 return "DW_TAG_PGI_interface_block";
c906108c
SS
10951 default:
10952 return "DW_TAG_<unknown>";
10953 }
10954}
10955
10956/* Convert a DWARF attribute code into its string name. */
10957
10958static char *
aa1ee363 10959dwarf_attr_name (unsigned attr)
c906108c
SS
10960{
10961 switch (attr)
10962 {
10963 case DW_AT_sibling:
10964 return "DW_AT_sibling";
10965 case DW_AT_location:
10966 return "DW_AT_location";
10967 case DW_AT_name:
10968 return "DW_AT_name";
10969 case DW_AT_ordering:
10970 return "DW_AT_ordering";
10971 case DW_AT_subscr_data:
10972 return "DW_AT_subscr_data";
10973 case DW_AT_byte_size:
10974 return "DW_AT_byte_size";
10975 case DW_AT_bit_offset:
10976 return "DW_AT_bit_offset";
10977 case DW_AT_bit_size:
10978 return "DW_AT_bit_size";
10979 case DW_AT_element_list:
10980 return "DW_AT_element_list";
10981 case DW_AT_stmt_list:
10982 return "DW_AT_stmt_list";
10983 case DW_AT_low_pc:
10984 return "DW_AT_low_pc";
10985 case DW_AT_high_pc:
10986 return "DW_AT_high_pc";
10987 case DW_AT_language:
10988 return "DW_AT_language";
10989 case DW_AT_member:
10990 return "DW_AT_member";
10991 case DW_AT_discr:
10992 return "DW_AT_discr";
10993 case DW_AT_discr_value:
10994 return "DW_AT_discr_value";
10995 case DW_AT_visibility:
10996 return "DW_AT_visibility";
10997 case DW_AT_import:
10998 return "DW_AT_import";
10999 case DW_AT_string_length:
11000 return "DW_AT_string_length";
11001 case DW_AT_common_reference:
11002 return "DW_AT_common_reference";
11003 case DW_AT_comp_dir:
11004 return "DW_AT_comp_dir";
11005 case DW_AT_const_value:
11006 return "DW_AT_const_value";
11007 case DW_AT_containing_type:
11008 return "DW_AT_containing_type";
11009 case DW_AT_default_value:
11010 return "DW_AT_default_value";
11011 case DW_AT_inline:
11012 return "DW_AT_inline";
11013 case DW_AT_is_optional:
11014 return "DW_AT_is_optional";
11015 case DW_AT_lower_bound:
11016 return "DW_AT_lower_bound";
11017 case DW_AT_producer:
11018 return "DW_AT_producer";
11019 case DW_AT_prototyped:
11020 return "DW_AT_prototyped";
11021 case DW_AT_return_addr:
11022 return "DW_AT_return_addr";
11023 case DW_AT_start_scope:
11024 return "DW_AT_start_scope";
09fa0d7c
JK
11025 case DW_AT_bit_stride:
11026 return "DW_AT_bit_stride";
c906108c
SS
11027 case DW_AT_upper_bound:
11028 return "DW_AT_upper_bound";
11029 case DW_AT_abstract_origin:
11030 return "DW_AT_abstract_origin";
11031 case DW_AT_accessibility:
11032 return "DW_AT_accessibility";
11033 case DW_AT_address_class:
11034 return "DW_AT_address_class";
11035 case DW_AT_artificial:
11036 return "DW_AT_artificial";
11037 case DW_AT_base_types:
11038 return "DW_AT_base_types";
11039 case DW_AT_calling_convention:
11040 return "DW_AT_calling_convention";
11041 case DW_AT_count:
11042 return "DW_AT_count";
11043 case DW_AT_data_member_location:
11044 return "DW_AT_data_member_location";
11045 case DW_AT_decl_column:
11046 return "DW_AT_decl_column";
11047 case DW_AT_decl_file:
11048 return "DW_AT_decl_file";
11049 case DW_AT_decl_line:
11050 return "DW_AT_decl_line";
11051 case DW_AT_declaration:
11052 return "DW_AT_declaration";
11053 case DW_AT_discr_list:
11054 return "DW_AT_discr_list";
11055 case DW_AT_encoding:
11056 return "DW_AT_encoding";
11057 case DW_AT_external:
11058 return "DW_AT_external";
11059 case DW_AT_frame_base:
11060 return "DW_AT_frame_base";
11061 case DW_AT_friend:
11062 return "DW_AT_friend";
11063 case DW_AT_identifier_case:
11064 return "DW_AT_identifier_case";
11065 case DW_AT_macro_info:
11066 return "DW_AT_macro_info";
11067 case DW_AT_namelist_items:
11068 return "DW_AT_namelist_items";
11069 case DW_AT_priority:
11070 return "DW_AT_priority";
11071 case DW_AT_segment:
11072 return "DW_AT_segment";
11073 case DW_AT_specification:
11074 return "DW_AT_specification";
11075 case DW_AT_static_link:
11076 return "DW_AT_static_link";
11077 case DW_AT_type:
11078 return "DW_AT_type";
11079 case DW_AT_use_location:
11080 return "DW_AT_use_location";
11081 case DW_AT_variable_parameter:
11082 return "DW_AT_variable_parameter";
11083 case DW_AT_virtuality:
11084 return "DW_AT_virtuality";
11085 case DW_AT_vtable_elem_location:
11086 return "DW_AT_vtable_elem_location";
b7619582 11087 /* DWARF 3 values. */
d9fa45fe
DC
11088 case DW_AT_allocated:
11089 return "DW_AT_allocated";
11090 case DW_AT_associated:
11091 return "DW_AT_associated";
11092 case DW_AT_data_location:
11093 return "DW_AT_data_location";
09fa0d7c
JK
11094 case DW_AT_byte_stride:
11095 return "DW_AT_byte_stride";
d9fa45fe
DC
11096 case DW_AT_entry_pc:
11097 return "DW_AT_entry_pc";
11098 case DW_AT_use_UTF8:
11099 return "DW_AT_use_UTF8";
11100 case DW_AT_extension:
11101 return "DW_AT_extension";
11102 case DW_AT_ranges:
11103 return "DW_AT_ranges";
11104 case DW_AT_trampoline:
11105 return "DW_AT_trampoline";
11106 case DW_AT_call_column:
11107 return "DW_AT_call_column";
11108 case DW_AT_call_file:
11109 return "DW_AT_call_file";
11110 case DW_AT_call_line:
11111 return "DW_AT_call_line";
b7619582
GF
11112 case DW_AT_description:
11113 return "DW_AT_description";
11114 case DW_AT_binary_scale:
11115 return "DW_AT_binary_scale";
11116 case DW_AT_decimal_scale:
11117 return "DW_AT_decimal_scale";
11118 case DW_AT_small:
11119 return "DW_AT_small";
11120 case DW_AT_decimal_sign:
11121 return "DW_AT_decimal_sign";
11122 case DW_AT_digit_count:
11123 return "DW_AT_digit_count";
11124 case DW_AT_picture_string:
11125 return "DW_AT_picture_string";
11126 case DW_AT_mutable:
11127 return "DW_AT_mutable";
11128 case DW_AT_threads_scaled:
11129 return "DW_AT_threads_scaled";
11130 case DW_AT_explicit:
11131 return "DW_AT_explicit";
11132 case DW_AT_object_pointer:
11133 return "DW_AT_object_pointer";
11134 case DW_AT_endianity:
11135 return "DW_AT_endianity";
11136 case DW_AT_elemental:
11137 return "DW_AT_elemental";
11138 case DW_AT_pure:
11139 return "DW_AT_pure";
11140 case DW_AT_recursive:
11141 return "DW_AT_recursive";
348e048f
DE
11142 /* DWARF 4 values. */
11143 case DW_AT_signature:
11144 return "DW_AT_signature";
31ef98ae
TT
11145 case DW_AT_linkage_name:
11146 return "DW_AT_linkage_name";
b7619582 11147 /* SGI/MIPS extensions. */
c764a876 11148#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
11149 case DW_AT_MIPS_fde:
11150 return "DW_AT_MIPS_fde";
c764a876 11151#endif
c906108c
SS
11152 case DW_AT_MIPS_loop_begin:
11153 return "DW_AT_MIPS_loop_begin";
11154 case DW_AT_MIPS_tail_loop_begin:
11155 return "DW_AT_MIPS_tail_loop_begin";
11156 case DW_AT_MIPS_epilog_begin:
11157 return "DW_AT_MIPS_epilog_begin";
11158 case DW_AT_MIPS_loop_unroll_factor:
11159 return "DW_AT_MIPS_loop_unroll_factor";
11160 case DW_AT_MIPS_software_pipeline_depth:
11161 return "DW_AT_MIPS_software_pipeline_depth";
11162 case DW_AT_MIPS_linkage_name:
11163 return "DW_AT_MIPS_linkage_name";
b7619582
GF
11164 case DW_AT_MIPS_stride:
11165 return "DW_AT_MIPS_stride";
11166 case DW_AT_MIPS_abstract_name:
11167 return "DW_AT_MIPS_abstract_name";
11168 case DW_AT_MIPS_clone_origin:
11169 return "DW_AT_MIPS_clone_origin";
11170 case DW_AT_MIPS_has_inlines:
11171 return "DW_AT_MIPS_has_inlines";
b7619582 11172 /* HP extensions. */
c764a876 11173#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
11174 case DW_AT_HP_block_index:
11175 return "DW_AT_HP_block_index";
c764a876 11176#endif
b7619582
GF
11177 case DW_AT_HP_unmodifiable:
11178 return "DW_AT_HP_unmodifiable";
11179 case DW_AT_HP_actuals_stmt_list:
11180 return "DW_AT_HP_actuals_stmt_list";
11181 case DW_AT_HP_proc_per_section:
11182 return "DW_AT_HP_proc_per_section";
11183 case DW_AT_HP_raw_data_ptr:
11184 return "DW_AT_HP_raw_data_ptr";
11185 case DW_AT_HP_pass_by_reference:
11186 return "DW_AT_HP_pass_by_reference";
11187 case DW_AT_HP_opt_level:
11188 return "DW_AT_HP_opt_level";
11189 case DW_AT_HP_prof_version_id:
11190 return "DW_AT_HP_prof_version_id";
11191 case DW_AT_HP_opt_flags:
11192 return "DW_AT_HP_opt_flags";
11193 case DW_AT_HP_cold_region_low_pc:
11194 return "DW_AT_HP_cold_region_low_pc";
11195 case DW_AT_HP_cold_region_high_pc:
11196 return "DW_AT_HP_cold_region_high_pc";
11197 case DW_AT_HP_all_variables_modifiable:
11198 return "DW_AT_HP_all_variables_modifiable";
11199 case DW_AT_HP_linkage_name:
11200 return "DW_AT_HP_linkage_name";
11201 case DW_AT_HP_prof_flags:
11202 return "DW_AT_HP_prof_flags";
11203 /* GNU extensions. */
c906108c
SS
11204 case DW_AT_sf_names:
11205 return "DW_AT_sf_names";
11206 case DW_AT_src_info:
11207 return "DW_AT_src_info";
11208 case DW_AT_mac_info:
11209 return "DW_AT_mac_info";
11210 case DW_AT_src_coords:
11211 return "DW_AT_src_coords";
11212 case DW_AT_body_begin:
11213 return "DW_AT_body_begin";
11214 case DW_AT_body_end:
11215 return "DW_AT_body_end";
f5f8a009
EZ
11216 case DW_AT_GNU_vector:
11217 return "DW_AT_GNU_vector";
b7619582
GF
11218 /* VMS extensions. */
11219 case DW_AT_VMS_rtnbeg_pd_address:
11220 return "DW_AT_VMS_rtnbeg_pd_address";
11221 /* UPC extension. */
11222 case DW_AT_upc_threads_scaled:
11223 return "DW_AT_upc_threads_scaled";
11224 /* PGI (STMicroelectronics) extensions. */
11225 case DW_AT_PGI_lbase:
11226 return "DW_AT_PGI_lbase";
11227 case DW_AT_PGI_soffset:
11228 return "DW_AT_PGI_soffset";
11229 case DW_AT_PGI_lstride:
11230 return "DW_AT_PGI_lstride";
c906108c
SS
11231 default:
11232 return "DW_AT_<unknown>";
11233 }
11234}
11235
11236/* Convert a DWARF value form code into its string name. */
11237
11238static char *
aa1ee363 11239dwarf_form_name (unsigned form)
c906108c
SS
11240{
11241 switch (form)
11242 {
11243 case DW_FORM_addr:
11244 return "DW_FORM_addr";
11245 case DW_FORM_block2:
11246 return "DW_FORM_block2";
11247 case DW_FORM_block4:
11248 return "DW_FORM_block4";
11249 case DW_FORM_data2:
11250 return "DW_FORM_data2";
11251 case DW_FORM_data4:
11252 return "DW_FORM_data4";
11253 case DW_FORM_data8:
11254 return "DW_FORM_data8";
11255 case DW_FORM_string:
11256 return "DW_FORM_string";
11257 case DW_FORM_block:
11258 return "DW_FORM_block";
11259 case DW_FORM_block1:
11260 return "DW_FORM_block1";
11261 case DW_FORM_data1:
11262 return "DW_FORM_data1";
11263 case DW_FORM_flag:
11264 return "DW_FORM_flag";
11265 case DW_FORM_sdata:
11266 return "DW_FORM_sdata";
11267 case DW_FORM_strp:
11268 return "DW_FORM_strp";
11269 case DW_FORM_udata:
11270 return "DW_FORM_udata";
11271 case DW_FORM_ref_addr:
11272 return "DW_FORM_ref_addr";
11273 case DW_FORM_ref1:
11274 return "DW_FORM_ref1";
11275 case DW_FORM_ref2:
11276 return "DW_FORM_ref2";
11277 case DW_FORM_ref4:
11278 return "DW_FORM_ref4";
11279 case DW_FORM_ref8:
11280 return "DW_FORM_ref8";
11281 case DW_FORM_ref_udata:
11282 return "DW_FORM_ref_udata";
11283 case DW_FORM_indirect:
11284 return "DW_FORM_indirect";
348e048f
DE
11285 case DW_FORM_sec_offset:
11286 return "DW_FORM_sec_offset";
11287 case DW_FORM_exprloc:
11288 return "DW_FORM_exprloc";
11289 case DW_FORM_flag_present:
11290 return "DW_FORM_flag_present";
11291 case DW_FORM_sig8:
11292 return "DW_FORM_sig8";
c906108c
SS
11293 default:
11294 return "DW_FORM_<unknown>";
11295 }
11296}
11297
11298/* Convert a DWARF stack opcode into its string name. */
11299
9eae7c52
TT
11300const char *
11301dwarf_stack_op_name (unsigned op, int def)
c906108c
SS
11302{
11303 switch (op)
11304 {
11305 case DW_OP_addr:
11306 return "DW_OP_addr";
11307 case DW_OP_deref:
11308 return "DW_OP_deref";
11309 case DW_OP_const1u:
11310 return "DW_OP_const1u";
11311 case DW_OP_const1s:
11312 return "DW_OP_const1s";
11313 case DW_OP_const2u:
11314 return "DW_OP_const2u";
11315 case DW_OP_const2s:
11316 return "DW_OP_const2s";
11317 case DW_OP_const4u:
11318 return "DW_OP_const4u";
11319 case DW_OP_const4s:
11320 return "DW_OP_const4s";
11321 case DW_OP_const8u:
11322 return "DW_OP_const8u";
11323 case DW_OP_const8s:
11324 return "DW_OP_const8s";
11325 case DW_OP_constu:
11326 return "DW_OP_constu";
11327 case DW_OP_consts:
11328 return "DW_OP_consts";
11329 case DW_OP_dup:
11330 return "DW_OP_dup";
11331 case DW_OP_drop:
11332 return "DW_OP_drop";
11333 case DW_OP_over:
11334 return "DW_OP_over";
11335 case DW_OP_pick:
11336 return "DW_OP_pick";
11337 case DW_OP_swap:
11338 return "DW_OP_swap";
11339 case DW_OP_rot:
11340 return "DW_OP_rot";
11341 case DW_OP_xderef:
11342 return "DW_OP_xderef";
11343 case DW_OP_abs:
11344 return "DW_OP_abs";
11345 case DW_OP_and:
11346 return "DW_OP_and";
11347 case DW_OP_div:
11348 return "DW_OP_div";
11349 case DW_OP_minus:
11350 return "DW_OP_minus";
11351 case DW_OP_mod:
11352 return "DW_OP_mod";
11353 case DW_OP_mul:
11354 return "DW_OP_mul";
11355 case DW_OP_neg:
11356 return "DW_OP_neg";
11357 case DW_OP_not:
11358 return "DW_OP_not";
11359 case DW_OP_or:
11360 return "DW_OP_or";
11361 case DW_OP_plus:
11362 return "DW_OP_plus";
11363 case DW_OP_plus_uconst:
11364 return "DW_OP_plus_uconst";
11365 case DW_OP_shl:
11366 return "DW_OP_shl";
11367 case DW_OP_shr:
11368 return "DW_OP_shr";
11369 case DW_OP_shra:
11370 return "DW_OP_shra";
11371 case DW_OP_xor:
11372 return "DW_OP_xor";
11373 case DW_OP_bra:
11374 return "DW_OP_bra";
11375 case DW_OP_eq:
11376 return "DW_OP_eq";
11377 case DW_OP_ge:
11378 return "DW_OP_ge";
11379 case DW_OP_gt:
11380 return "DW_OP_gt";
11381 case DW_OP_le:
11382 return "DW_OP_le";
11383 case DW_OP_lt:
11384 return "DW_OP_lt";
11385 case DW_OP_ne:
11386 return "DW_OP_ne";
11387 case DW_OP_skip:
11388 return "DW_OP_skip";
11389 case DW_OP_lit0:
11390 return "DW_OP_lit0";
11391 case DW_OP_lit1:
11392 return "DW_OP_lit1";
11393 case DW_OP_lit2:
11394 return "DW_OP_lit2";
11395 case DW_OP_lit3:
11396 return "DW_OP_lit3";
11397 case DW_OP_lit4:
11398 return "DW_OP_lit4";
11399 case DW_OP_lit5:
11400 return "DW_OP_lit5";
11401 case DW_OP_lit6:
11402 return "DW_OP_lit6";
11403 case DW_OP_lit7:
11404 return "DW_OP_lit7";
11405 case DW_OP_lit8:
11406 return "DW_OP_lit8";
11407 case DW_OP_lit9:
11408 return "DW_OP_lit9";
11409 case DW_OP_lit10:
11410 return "DW_OP_lit10";
11411 case DW_OP_lit11:
11412 return "DW_OP_lit11";
11413 case DW_OP_lit12:
11414 return "DW_OP_lit12";
11415 case DW_OP_lit13:
11416 return "DW_OP_lit13";
11417 case DW_OP_lit14:
11418 return "DW_OP_lit14";
11419 case DW_OP_lit15:
11420 return "DW_OP_lit15";
11421 case DW_OP_lit16:
11422 return "DW_OP_lit16";
11423 case DW_OP_lit17:
11424 return "DW_OP_lit17";
11425 case DW_OP_lit18:
11426 return "DW_OP_lit18";
11427 case DW_OP_lit19:
11428 return "DW_OP_lit19";
11429 case DW_OP_lit20:
11430 return "DW_OP_lit20";
11431 case DW_OP_lit21:
11432 return "DW_OP_lit21";
11433 case DW_OP_lit22:
11434 return "DW_OP_lit22";
11435 case DW_OP_lit23:
11436 return "DW_OP_lit23";
11437 case DW_OP_lit24:
11438 return "DW_OP_lit24";
11439 case DW_OP_lit25:
11440 return "DW_OP_lit25";
11441 case DW_OP_lit26:
11442 return "DW_OP_lit26";
11443 case DW_OP_lit27:
11444 return "DW_OP_lit27";
11445 case DW_OP_lit28:
11446 return "DW_OP_lit28";
11447 case DW_OP_lit29:
11448 return "DW_OP_lit29";
11449 case DW_OP_lit30:
11450 return "DW_OP_lit30";
11451 case DW_OP_lit31:
11452 return "DW_OP_lit31";
11453 case DW_OP_reg0:
11454 return "DW_OP_reg0";
11455 case DW_OP_reg1:
11456 return "DW_OP_reg1";
11457 case DW_OP_reg2:
11458 return "DW_OP_reg2";
11459 case DW_OP_reg3:
11460 return "DW_OP_reg3";
11461 case DW_OP_reg4:
11462 return "DW_OP_reg4";
11463 case DW_OP_reg5:
11464 return "DW_OP_reg5";
11465 case DW_OP_reg6:
11466 return "DW_OP_reg6";
11467 case DW_OP_reg7:
11468 return "DW_OP_reg7";
11469 case DW_OP_reg8:
11470 return "DW_OP_reg8";
11471 case DW_OP_reg9:
11472 return "DW_OP_reg9";
11473 case DW_OP_reg10:
11474 return "DW_OP_reg10";
11475 case DW_OP_reg11:
11476 return "DW_OP_reg11";
11477 case DW_OP_reg12:
11478 return "DW_OP_reg12";
11479 case DW_OP_reg13:
11480 return "DW_OP_reg13";
11481 case DW_OP_reg14:
11482 return "DW_OP_reg14";
11483 case DW_OP_reg15:
11484 return "DW_OP_reg15";
11485 case DW_OP_reg16:
11486 return "DW_OP_reg16";
11487 case DW_OP_reg17:
11488 return "DW_OP_reg17";
11489 case DW_OP_reg18:
11490 return "DW_OP_reg18";
11491 case DW_OP_reg19:
11492 return "DW_OP_reg19";
11493 case DW_OP_reg20:
11494 return "DW_OP_reg20";
11495 case DW_OP_reg21:
11496 return "DW_OP_reg21";
11497 case DW_OP_reg22:
11498 return "DW_OP_reg22";
11499 case DW_OP_reg23:
11500 return "DW_OP_reg23";
11501 case DW_OP_reg24:
11502 return "DW_OP_reg24";
11503 case DW_OP_reg25:
11504 return "DW_OP_reg25";
11505 case DW_OP_reg26:
11506 return "DW_OP_reg26";
11507 case DW_OP_reg27:
11508 return "DW_OP_reg27";
11509 case DW_OP_reg28:
11510 return "DW_OP_reg28";
11511 case DW_OP_reg29:
11512 return "DW_OP_reg29";
11513 case DW_OP_reg30:
11514 return "DW_OP_reg30";
11515 case DW_OP_reg31:
11516 return "DW_OP_reg31";
11517 case DW_OP_breg0:
11518 return "DW_OP_breg0";
11519 case DW_OP_breg1:
11520 return "DW_OP_breg1";
11521 case DW_OP_breg2:
11522 return "DW_OP_breg2";
11523 case DW_OP_breg3:
11524 return "DW_OP_breg3";
11525 case DW_OP_breg4:
11526 return "DW_OP_breg4";
11527 case DW_OP_breg5:
11528 return "DW_OP_breg5";
11529 case DW_OP_breg6:
11530 return "DW_OP_breg6";
11531 case DW_OP_breg7:
11532 return "DW_OP_breg7";
11533 case DW_OP_breg8:
11534 return "DW_OP_breg8";
11535 case DW_OP_breg9:
11536 return "DW_OP_breg9";
11537 case DW_OP_breg10:
11538 return "DW_OP_breg10";
11539 case DW_OP_breg11:
11540 return "DW_OP_breg11";
11541 case DW_OP_breg12:
11542 return "DW_OP_breg12";
11543 case DW_OP_breg13:
11544 return "DW_OP_breg13";
11545 case DW_OP_breg14:
11546 return "DW_OP_breg14";
11547 case DW_OP_breg15:
11548 return "DW_OP_breg15";
11549 case DW_OP_breg16:
11550 return "DW_OP_breg16";
11551 case DW_OP_breg17:
11552 return "DW_OP_breg17";
11553 case DW_OP_breg18:
11554 return "DW_OP_breg18";
11555 case DW_OP_breg19:
11556 return "DW_OP_breg19";
11557 case DW_OP_breg20:
11558 return "DW_OP_breg20";
11559 case DW_OP_breg21:
11560 return "DW_OP_breg21";
11561 case DW_OP_breg22:
11562 return "DW_OP_breg22";
11563 case DW_OP_breg23:
11564 return "DW_OP_breg23";
11565 case DW_OP_breg24:
11566 return "DW_OP_breg24";
11567 case DW_OP_breg25:
11568 return "DW_OP_breg25";
11569 case DW_OP_breg26:
11570 return "DW_OP_breg26";
11571 case DW_OP_breg27:
11572 return "DW_OP_breg27";
11573 case DW_OP_breg28:
11574 return "DW_OP_breg28";
11575 case DW_OP_breg29:
11576 return "DW_OP_breg29";
11577 case DW_OP_breg30:
11578 return "DW_OP_breg30";
11579 case DW_OP_breg31:
11580 return "DW_OP_breg31";
11581 case DW_OP_regx:
11582 return "DW_OP_regx";
11583 case DW_OP_fbreg:
11584 return "DW_OP_fbreg";
11585 case DW_OP_bregx:
11586 return "DW_OP_bregx";
11587 case DW_OP_piece:
11588 return "DW_OP_piece";
11589 case DW_OP_deref_size:
11590 return "DW_OP_deref_size";
11591 case DW_OP_xderef_size:
11592 return "DW_OP_xderef_size";
11593 case DW_OP_nop:
11594 return "DW_OP_nop";
b7619582 11595 /* DWARF 3 extensions. */
ed348acc
EZ
11596 case DW_OP_push_object_address:
11597 return "DW_OP_push_object_address";
11598 case DW_OP_call2:
11599 return "DW_OP_call2";
11600 case DW_OP_call4:
11601 return "DW_OP_call4";
11602 case DW_OP_call_ref:
11603 return "DW_OP_call_ref";
b7619582
GF
11604 case DW_OP_form_tls_address:
11605 return "DW_OP_form_tls_address";
11606 case DW_OP_call_frame_cfa:
11607 return "DW_OP_call_frame_cfa";
11608 case DW_OP_bit_piece:
11609 return "DW_OP_bit_piece";
9eae7c52
TT
11610 /* DWARF 4 extensions. */
11611 case DW_OP_implicit_value:
11612 return "DW_OP_implicit_value";
11613 case DW_OP_stack_value:
11614 return "DW_OP_stack_value";
11615 /* GNU extensions. */
ed348acc
EZ
11616 case DW_OP_GNU_push_tls_address:
11617 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
11618 case DW_OP_GNU_uninit:
11619 return "DW_OP_GNU_uninit";
c906108c 11620 default:
9eae7c52 11621 return def ? "OP_<unknown>" : NULL;
c906108c
SS
11622 }
11623}
11624
11625static char *
fba45db2 11626dwarf_bool_name (unsigned mybool)
c906108c
SS
11627{
11628 if (mybool)
11629 return "TRUE";
11630 else
11631 return "FALSE";
11632}
11633
11634/* Convert a DWARF type code into its string name. */
11635
11636static char *
aa1ee363 11637dwarf_type_encoding_name (unsigned enc)
c906108c
SS
11638{
11639 switch (enc)
11640 {
b7619582
GF
11641 case DW_ATE_void:
11642 return "DW_ATE_void";
c906108c
SS
11643 case DW_ATE_address:
11644 return "DW_ATE_address";
11645 case DW_ATE_boolean:
11646 return "DW_ATE_boolean";
11647 case DW_ATE_complex_float:
11648 return "DW_ATE_complex_float";
11649 case DW_ATE_float:
11650 return "DW_ATE_float";
11651 case DW_ATE_signed:
11652 return "DW_ATE_signed";
11653 case DW_ATE_signed_char:
11654 return "DW_ATE_signed_char";
11655 case DW_ATE_unsigned:
11656 return "DW_ATE_unsigned";
11657 case DW_ATE_unsigned_char:
11658 return "DW_ATE_unsigned_char";
b7619582 11659 /* DWARF 3. */
d9fa45fe
DC
11660 case DW_ATE_imaginary_float:
11661 return "DW_ATE_imaginary_float";
b7619582
GF
11662 case DW_ATE_packed_decimal:
11663 return "DW_ATE_packed_decimal";
11664 case DW_ATE_numeric_string:
11665 return "DW_ATE_numeric_string";
11666 case DW_ATE_edited:
11667 return "DW_ATE_edited";
11668 case DW_ATE_signed_fixed:
11669 return "DW_ATE_signed_fixed";
11670 case DW_ATE_unsigned_fixed:
11671 return "DW_ATE_unsigned_fixed";
11672 case DW_ATE_decimal_float:
11673 return "DW_ATE_decimal_float";
75079b2b
TT
11674 /* DWARF 4. */
11675 case DW_ATE_UTF:
11676 return "DW_ATE_UTF";
b7619582
GF
11677 /* HP extensions. */
11678 case DW_ATE_HP_float80:
11679 return "DW_ATE_HP_float80";
11680 case DW_ATE_HP_complex_float80:
11681 return "DW_ATE_HP_complex_float80";
11682 case DW_ATE_HP_float128:
11683 return "DW_ATE_HP_float128";
11684 case DW_ATE_HP_complex_float128:
11685 return "DW_ATE_HP_complex_float128";
11686 case DW_ATE_HP_floathpintel:
11687 return "DW_ATE_HP_floathpintel";
11688 case DW_ATE_HP_imaginary_float80:
11689 return "DW_ATE_HP_imaginary_float80";
11690 case DW_ATE_HP_imaginary_float128:
11691 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
11692 default:
11693 return "DW_ATE_<unknown>";
11694 }
11695}
11696
11697/* Convert a DWARF call frame info operation to its string name. */
11698
11699#if 0
11700static char *
aa1ee363 11701dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
11702{
11703 switch (cfi_opc)
11704 {
11705 case DW_CFA_advance_loc:
11706 return "DW_CFA_advance_loc";
11707 case DW_CFA_offset:
11708 return "DW_CFA_offset";
11709 case DW_CFA_restore:
11710 return "DW_CFA_restore";
11711 case DW_CFA_nop:
11712 return "DW_CFA_nop";
11713 case DW_CFA_set_loc:
11714 return "DW_CFA_set_loc";
11715 case DW_CFA_advance_loc1:
11716 return "DW_CFA_advance_loc1";
11717 case DW_CFA_advance_loc2:
11718 return "DW_CFA_advance_loc2";
11719 case DW_CFA_advance_loc4:
11720 return "DW_CFA_advance_loc4";
11721 case DW_CFA_offset_extended:
11722 return "DW_CFA_offset_extended";
11723 case DW_CFA_restore_extended:
11724 return "DW_CFA_restore_extended";
11725 case DW_CFA_undefined:
11726 return "DW_CFA_undefined";
11727 case DW_CFA_same_value:
11728 return "DW_CFA_same_value";
11729 case DW_CFA_register:
11730 return "DW_CFA_register";
11731 case DW_CFA_remember_state:
11732 return "DW_CFA_remember_state";
11733 case DW_CFA_restore_state:
11734 return "DW_CFA_restore_state";
11735 case DW_CFA_def_cfa:
11736 return "DW_CFA_def_cfa";
11737 case DW_CFA_def_cfa_register:
11738 return "DW_CFA_def_cfa_register";
11739 case DW_CFA_def_cfa_offset:
11740 return "DW_CFA_def_cfa_offset";
b7619582 11741 /* DWARF 3. */
985cb1a3
JM
11742 case DW_CFA_def_cfa_expression:
11743 return "DW_CFA_def_cfa_expression";
11744 case DW_CFA_expression:
11745 return "DW_CFA_expression";
11746 case DW_CFA_offset_extended_sf:
11747 return "DW_CFA_offset_extended_sf";
11748 case DW_CFA_def_cfa_sf:
11749 return "DW_CFA_def_cfa_sf";
11750 case DW_CFA_def_cfa_offset_sf:
11751 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
11752 case DW_CFA_val_offset:
11753 return "DW_CFA_val_offset";
11754 case DW_CFA_val_offset_sf:
11755 return "DW_CFA_val_offset_sf";
11756 case DW_CFA_val_expression:
11757 return "DW_CFA_val_expression";
11758 /* SGI/MIPS specific. */
c906108c
SS
11759 case DW_CFA_MIPS_advance_loc8:
11760 return "DW_CFA_MIPS_advance_loc8";
b7619582 11761 /* GNU extensions. */
985cb1a3
JM
11762 case DW_CFA_GNU_window_save:
11763 return "DW_CFA_GNU_window_save";
11764 case DW_CFA_GNU_args_size:
11765 return "DW_CFA_GNU_args_size";
11766 case DW_CFA_GNU_negative_offset_extended:
11767 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
11768 default:
11769 return "DW_CFA_<unknown>";
11770 }
11771}
11772#endif
11773
f9aca02d 11774static void
d97bc12b 11775dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
11776{
11777 unsigned int i;
11778
d97bc12b
DE
11779 print_spaces (indent, f);
11780 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 11781 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
11782
11783 if (die->parent != NULL)
11784 {
11785 print_spaces (indent, f);
11786 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
11787 die->parent->offset);
11788 }
11789
11790 print_spaces (indent, f);
11791 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 11792 dwarf_bool_name (die->child != NULL));
c906108c 11793
d97bc12b
DE
11794 print_spaces (indent, f);
11795 fprintf_unfiltered (f, " attributes:\n");
11796
c906108c
SS
11797 for (i = 0; i < die->num_attrs; ++i)
11798 {
d97bc12b
DE
11799 print_spaces (indent, f);
11800 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
11801 dwarf_attr_name (die->attrs[i].name),
11802 dwarf_form_name (die->attrs[i].form));
d97bc12b 11803
c906108c
SS
11804 switch (die->attrs[i].form)
11805 {
11806 case DW_FORM_ref_addr:
11807 case DW_FORM_addr:
d97bc12b 11808 fprintf_unfiltered (f, "address: ");
5af949e3 11809 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
11810 break;
11811 case DW_FORM_block2:
11812 case DW_FORM_block4:
11813 case DW_FORM_block:
11814 case DW_FORM_block1:
d97bc12b 11815 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 11816 break;
2dc7f7b3
TT
11817 case DW_FORM_exprloc:
11818 fprintf_unfiltered (f, "expression: size %u",
11819 DW_BLOCK (&die->attrs[i])->size);
11820 break;
10b3939b
DJ
11821 case DW_FORM_ref1:
11822 case DW_FORM_ref2:
11823 case DW_FORM_ref4:
d97bc12b 11824 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
11825 (long) (DW_ADDR (&die->attrs[i])));
11826 break;
c906108c
SS
11827 case DW_FORM_data1:
11828 case DW_FORM_data2:
11829 case DW_FORM_data4:
ce5d95e1 11830 case DW_FORM_data8:
c906108c
SS
11831 case DW_FORM_udata:
11832 case DW_FORM_sdata:
43bbcdc2
PH
11833 fprintf_unfiltered (f, "constant: %s",
11834 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 11835 break;
2dc7f7b3
TT
11836 case DW_FORM_sec_offset:
11837 fprintf_unfiltered (f, "section offset: %s",
11838 pulongest (DW_UNSND (&die->attrs[i])));
11839 break;
348e048f
DE
11840 case DW_FORM_sig8:
11841 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
11842 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
11843 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
11844 else
11845 fprintf_unfiltered (f, "signatured type, offset: unknown");
11846 break;
c906108c 11847 case DW_FORM_string:
4bdf3d34 11848 case DW_FORM_strp:
8285870a 11849 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 11850 DW_STRING (&die->attrs[i])
8285870a
JK
11851 ? DW_STRING (&die->attrs[i]) : "",
11852 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
11853 break;
11854 case DW_FORM_flag:
11855 if (DW_UNSND (&die->attrs[i]))
d97bc12b 11856 fprintf_unfiltered (f, "flag: TRUE");
c906108c 11857 else
d97bc12b 11858 fprintf_unfiltered (f, "flag: FALSE");
c906108c 11859 break;
2dc7f7b3
TT
11860 case DW_FORM_flag_present:
11861 fprintf_unfiltered (f, "flag: TRUE");
11862 break;
a8329558
KW
11863 case DW_FORM_indirect:
11864 /* the reader will have reduced the indirect form to
11865 the "base form" so this form should not occur */
d97bc12b 11866 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 11867 break;
c906108c 11868 default:
d97bc12b 11869 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 11870 die->attrs[i].form);
d97bc12b 11871 break;
c906108c 11872 }
d97bc12b 11873 fprintf_unfiltered (f, "\n");
c906108c
SS
11874 }
11875}
11876
f9aca02d 11877static void
d97bc12b 11878dump_die_for_error (struct die_info *die)
c906108c 11879{
d97bc12b
DE
11880 dump_die_shallow (gdb_stderr, 0, die);
11881}
11882
11883static void
11884dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
11885{
11886 int indent = level * 4;
11887
11888 gdb_assert (die != NULL);
11889
11890 if (level >= max_level)
11891 return;
11892
11893 dump_die_shallow (f, indent, die);
11894
11895 if (die->child != NULL)
c906108c 11896 {
d97bc12b
DE
11897 print_spaces (indent, f);
11898 fprintf_unfiltered (f, " Children:");
11899 if (level + 1 < max_level)
11900 {
11901 fprintf_unfiltered (f, "\n");
11902 dump_die_1 (f, level + 1, max_level, die->child);
11903 }
11904 else
11905 {
11906 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
11907 }
11908 }
11909
11910 if (die->sibling != NULL && level > 0)
11911 {
11912 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
11913 }
11914}
11915
d97bc12b
DE
11916/* This is called from the pdie macro in gdbinit.in.
11917 It's not static so gcc will keep a copy callable from gdb. */
11918
11919void
11920dump_die (struct die_info *die, int max_level)
11921{
11922 dump_die_1 (gdb_stdlog, 0, max_level, die);
11923}
11924
f9aca02d 11925static void
51545339 11926store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11927{
51545339 11928 void **slot;
c906108c 11929
51545339
DJ
11930 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
11931
11932 *slot = die;
c906108c
SS
11933}
11934
93311388
DE
11935static int
11936is_ref_attr (struct attribute *attr)
c906108c 11937{
c906108c
SS
11938 switch (attr->form)
11939 {
11940 case DW_FORM_ref_addr:
c906108c
SS
11941 case DW_FORM_ref1:
11942 case DW_FORM_ref2:
11943 case DW_FORM_ref4:
613e1657 11944 case DW_FORM_ref8:
c906108c 11945 case DW_FORM_ref_udata:
93311388 11946 return 1;
c906108c 11947 default:
93311388 11948 return 0;
c906108c 11949 }
93311388
DE
11950}
11951
11952static unsigned int
11953dwarf2_get_ref_die_offset (struct attribute *attr)
11954{
11955 if (is_ref_attr (attr))
11956 return DW_ADDR (attr);
11957
11958 complaint (&symfile_complaints,
11959 _("unsupported die ref attribute form: '%s'"),
11960 dwarf_form_name (attr->form));
11961 return 0;
c906108c
SS
11962}
11963
43bbcdc2
PH
11964/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
11965 * the value held by the attribute is not constant. */
a02abb62 11966
43bbcdc2 11967static LONGEST
a02abb62
JB
11968dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
11969{
11970 if (attr->form == DW_FORM_sdata)
11971 return DW_SND (attr);
11972 else if (attr->form == DW_FORM_udata
11973 || attr->form == DW_FORM_data1
11974 || attr->form == DW_FORM_data2
11975 || attr->form == DW_FORM_data4
11976 || attr->form == DW_FORM_data8)
11977 return DW_UNSND (attr);
11978 else
11979 {
e2e0b3e5 11980 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
11981 dwarf_form_name (attr->form));
11982 return default_value;
11983 }
11984}
11985
03dd20cc 11986/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
11987 unit and add it to our queue.
11988 The result is non-zero if PER_CU was queued, otherwise the result is zero
11989 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 11990
348e048f 11991static int
03dd20cc
DJ
11992maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
11993 struct dwarf2_per_cu_data *per_cu)
11994{
11995 /* Mark the dependence relation so that we don't flush PER_CU
11996 too early. */
11997 dwarf2_add_dependence (this_cu, per_cu);
11998
11999 /* If it's already on the queue, we have nothing to do. */
12000 if (per_cu->queued)
348e048f 12001 return 0;
03dd20cc
DJ
12002
12003 /* If the compilation unit is already loaded, just mark it as
12004 used. */
12005 if (per_cu->cu != NULL)
12006 {
12007 per_cu->cu->last_used = 0;
348e048f 12008 return 0;
03dd20cc
DJ
12009 }
12010
12011 /* Add it to the queue. */
12012 queue_comp_unit (per_cu, this_cu->objfile);
348e048f
DE
12013
12014 return 1;
12015}
12016
12017/* Follow reference or signature attribute ATTR of SRC_DIE.
12018 On entry *REF_CU is the CU of SRC_DIE.
12019 On exit *REF_CU is the CU of the result. */
12020
12021static struct die_info *
12022follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12023 struct dwarf2_cu **ref_cu)
12024{
12025 struct die_info *die;
12026
12027 if (is_ref_attr (attr))
12028 die = follow_die_ref (src_die, attr, ref_cu);
12029 else if (attr->form == DW_FORM_sig8)
12030 die = follow_die_sig (src_die, attr, ref_cu);
12031 else
12032 {
12033 dump_die_for_error (src_die);
12034 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12035 (*ref_cu)->objfile->name);
12036 }
12037
12038 return die;
03dd20cc
DJ
12039}
12040
5c631832
JK
12041/* Follow reference OFFSET.
12042 On entry *REF_CU is the CU of source DIE referencing OFFSET.
f504f079
DE
12043 On exit *REF_CU is the CU of the result. */
12044
f9aca02d 12045static struct die_info *
5c631832 12046follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
c906108c 12047{
10b3939b 12048 struct die_info temp_die;
f2f0e013 12049 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 12050
348e048f
DE
12051 gdb_assert (cu->per_cu != NULL);
12052
348e048f
DE
12053 if (cu->per_cu->from_debug_types)
12054 {
12055 /* .debug_types CUs cannot reference anything outside their CU.
12056 If they need to, they have to reference a signatured type via
12057 DW_FORM_sig8. */
12058 if (! offset_in_cu_p (&cu->header, offset))
5c631832 12059 return NULL;
348e048f
DE
12060 target_cu = cu;
12061 }
12062 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
12063 {
12064 struct dwarf2_per_cu_data *per_cu;
9a619af0 12065
45452591 12066 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
12067
12068 /* If necessary, add it to the queue and load its DIEs. */
348e048f
DE
12069 if (maybe_queue_comp_unit (cu, per_cu))
12070 load_full_comp_unit (per_cu, cu->objfile);
03dd20cc 12071
10b3939b
DJ
12072 target_cu = per_cu->cu;
12073 }
12074 else
12075 target_cu = cu;
c906108c 12076
f2f0e013 12077 *ref_cu = target_cu;
51545339 12078 temp_die.offset = offset;
5c631832
JK
12079 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12080}
10b3939b 12081
5c631832
JK
12082/* Follow reference attribute ATTR of SRC_DIE.
12083 On entry *REF_CU is the CU of SRC_DIE.
12084 On exit *REF_CU is the CU of the result. */
12085
12086static struct die_info *
12087follow_die_ref (struct die_info *src_die, struct attribute *attr,
12088 struct dwarf2_cu **ref_cu)
12089{
12090 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12091 struct dwarf2_cu *cu = *ref_cu;
12092 struct die_info *die;
12093
12094 die = follow_die_offset (offset, ref_cu);
12095 if (!die)
12096 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12097 "at 0x%x [in module %s]"),
12098 offset, src_die->offset, cu->objfile->name);
348e048f 12099
5c631832
JK
12100 return die;
12101}
12102
12103/* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
12104 value is intended for DW_OP_call*. */
12105
12106struct dwarf2_locexpr_baton
12107dwarf2_fetch_die_location_block (unsigned int offset,
12108 struct dwarf2_per_cu_data *per_cu)
12109{
12110 struct dwarf2_cu *cu = per_cu->cu;
12111 struct die_info *die;
12112 struct attribute *attr;
12113 struct dwarf2_locexpr_baton retval;
12114
12115 die = follow_die_offset (offset, &cu);
12116 if (!die)
12117 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12118 offset, per_cu->cu->objfile->name);
12119
12120 attr = dwarf2_attr (die, DW_AT_location, cu);
12121 if (!attr)
12122 {
12123 /* DWARF: "If there is no such attribute, then there is no effect.". */
12124
12125 retval.data = NULL;
12126 retval.size = 0;
12127 }
12128 else
12129 {
12130 if (!attr_form_is_block (attr))
12131 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12132 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12133 offset, per_cu->cu->objfile->name);
12134
12135 retval.data = DW_BLOCK (attr)->data;
12136 retval.size = DW_BLOCK (attr)->size;
12137 }
12138 retval.per_cu = cu->per_cu;
12139 return retval;
348e048f
DE
12140}
12141
12142/* Follow the signature attribute ATTR in SRC_DIE.
12143 On entry *REF_CU is the CU of SRC_DIE.
12144 On exit *REF_CU is the CU of the result. */
12145
12146static struct die_info *
12147follow_die_sig (struct die_info *src_die, struct attribute *attr,
12148 struct dwarf2_cu **ref_cu)
12149{
12150 struct objfile *objfile = (*ref_cu)->objfile;
12151 struct die_info temp_die;
12152 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12153 struct dwarf2_cu *sig_cu;
12154 struct die_info *die;
12155
12156 /* sig_type will be NULL if the signatured type is missing from
12157 the debug info. */
12158 if (sig_type == NULL)
12159 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12160 "at 0x%x [in module %s]"),
12161 src_die->offset, objfile->name);
12162
12163 /* If necessary, add it to the queue and load its DIEs. */
12164
12165 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12166 read_signatured_type (objfile, sig_type);
12167
12168 gdb_assert (sig_type->per_cu.cu != NULL);
12169
12170 sig_cu = sig_type->per_cu.cu;
12171 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12172 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12173 if (die)
12174 {
12175 *ref_cu = sig_cu;
12176 return die;
12177 }
12178
12179 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12180 "at 0x%x [in module %s]"),
12181 sig_type->type_offset, src_die->offset, objfile->name);
12182}
12183
12184/* Given an offset of a signatured type, return its signatured_type. */
12185
12186static struct signatured_type *
12187lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12188{
12189 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12190 unsigned int length, initial_length_size;
12191 unsigned int sig_offset;
12192 struct signatured_type find_entry, *type_sig;
12193
12194 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12195 sig_offset = (initial_length_size
12196 + 2 /*version*/
12197 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12198 + 1 /*address_size*/);
12199 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12200 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12201
12202 /* This is only used to lookup previously recorded types.
12203 If we didn't find it, it's our bug. */
12204 gdb_assert (type_sig != NULL);
12205 gdb_assert (offset == type_sig->offset);
12206
12207 return type_sig;
12208}
12209
12210/* Read in signatured type at OFFSET and build its CU and die(s). */
12211
12212static void
12213read_signatured_type_at_offset (struct objfile *objfile,
12214 unsigned int offset)
12215{
12216 struct signatured_type *type_sig;
12217
be391dca
TT
12218 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12219
348e048f
DE
12220 /* We have the section offset, but we need the signature to do the
12221 hash table lookup. */
12222 type_sig = lookup_signatured_type_at_offset (objfile, offset);
12223
12224 gdb_assert (type_sig->per_cu.cu == NULL);
12225
12226 read_signatured_type (objfile, type_sig);
12227
12228 gdb_assert (type_sig->per_cu.cu != NULL);
12229}
12230
12231/* Read in a signatured type and build its CU and DIEs. */
12232
12233static void
12234read_signatured_type (struct objfile *objfile,
12235 struct signatured_type *type_sig)
12236{
1fd400ff 12237 gdb_byte *types_ptr;
348e048f
DE
12238 struct die_reader_specs reader_specs;
12239 struct dwarf2_cu *cu;
12240 ULONGEST signature;
12241 struct cleanup *back_to, *free_cu_cleanup;
12242 struct attribute *attr;
12243
1fd400ff
TT
12244 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12245 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12246
348e048f
DE
12247 gdb_assert (type_sig->per_cu.cu == NULL);
12248
12249 cu = xmalloc (sizeof (struct dwarf2_cu));
12250 memset (cu, 0, sizeof (struct dwarf2_cu));
12251 obstack_init (&cu->comp_unit_obstack);
12252 cu->objfile = objfile;
12253 type_sig->per_cu.cu = cu;
12254 cu->per_cu = &type_sig->per_cu;
12255
12256 /* If an error occurs while loading, release our storage. */
12257 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12258
12259 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12260 types_ptr, objfile->obfd);
12261 gdb_assert (signature == type_sig->signature);
12262
12263 cu->die_hash
12264 = htab_create_alloc_ex (cu->header.length / 12,
12265 die_hash,
12266 die_eq,
12267 NULL,
12268 &cu->comp_unit_obstack,
12269 hashtab_obstack_allocate,
12270 dummy_obstack_deallocate);
12271
12272 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12273 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12274
12275 init_cu_die_reader (&reader_specs, cu);
12276
12277 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12278 NULL /*parent*/);
12279
12280 /* We try not to read any attributes in this function, because not
12281 all objfiles needed for references have been loaded yet, and symbol
12282 table processing isn't initialized. But we have to set the CU language,
12283 or we won't be able to build types correctly. */
12284 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12285 if (attr)
12286 set_cu_language (DW_UNSND (attr), cu);
12287 else
12288 set_cu_language (language_minimal, cu);
12289
12290 do_cleanups (back_to);
12291
12292 /* We've successfully allocated this compilation unit. Let our caller
12293 clean it up when finished with it. */
12294 discard_cleanups (free_cu_cleanup);
12295
12296 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
12297 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
c906108c
SS
12298}
12299
c906108c
SS
12300/* Decode simple location descriptions.
12301 Given a pointer to a dwarf block that defines a location, compute
12302 the location and return the value.
12303
4cecd739
DJ
12304 NOTE drow/2003-11-18: This function is called in two situations
12305 now: for the address of static or global variables (partial symbols
12306 only) and for offsets into structures which are expected to be
12307 (more or less) constant. The partial symbol case should go away,
12308 and only the constant case should remain. That will let this
12309 function complain more accurately. A few special modes are allowed
12310 without complaint for global variables (for instance, global
12311 register values and thread-local values).
c906108c
SS
12312
12313 A location description containing no operations indicates that the
4cecd739 12314 object is optimized out. The return value is 0 for that case.
6b992462
DJ
12315 FIXME drow/2003-11-16: No callers check for this case any more; soon all
12316 callers will only want a very basic result and this can become a
12317 complaint.
c906108c 12318
c906108c
SS
12319 Note that stack[0] is unused except as a default error return.
12320 Note that stack overflow is not yet handled. */
12321
12322static CORE_ADDR
e7c27a73 12323decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 12324{
e7c27a73 12325 struct objfile *objfile = cu->objfile;
c906108c
SS
12326 int i;
12327 int size = blk->size;
fe1b8b76 12328 gdb_byte *data = blk->data;
c906108c
SS
12329 CORE_ADDR stack[64];
12330 int stacki;
12331 unsigned int bytes_read, unsnd;
fe1b8b76 12332 gdb_byte op;
c906108c
SS
12333
12334 i = 0;
12335 stacki = 0;
12336 stack[stacki] = 0;
c906108c
SS
12337
12338 while (i < size)
12339 {
c906108c
SS
12340 op = data[i++];
12341 switch (op)
12342 {
f1bea926
JM
12343 case DW_OP_lit0:
12344 case DW_OP_lit1:
12345 case DW_OP_lit2:
12346 case DW_OP_lit3:
12347 case DW_OP_lit4:
12348 case DW_OP_lit5:
12349 case DW_OP_lit6:
12350 case DW_OP_lit7:
12351 case DW_OP_lit8:
12352 case DW_OP_lit9:
12353 case DW_OP_lit10:
12354 case DW_OP_lit11:
12355 case DW_OP_lit12:
12356 case DW_OP_lit13:
12357 case DW_OP_lit14:
12358 case DW_OP_lit15:
12359 case DW_OP_lit16:
12360 case DW_OP_lit17:
12361 case DW_OP_lit18:
12362 case DW_OP_lit19:
12363 case DW_OP_lit20:
12364 case DW_OP_lit21:
12365 case DW_OP_lit22:
12366 case DW_OP_lit23:
12367 case DW_OP_lit24:
12368 case DW_OP_lit25:
12369 case DW_OP_lit26:
12370 case DW_OP_lit27:
12371 case DW_OP_lit28:
12372 case DW_OP_lit29:
12373 case DW_OP_lit30:
12374 case DW_OP_lit31:
12375 stack[++stacki] = op - DW_OP_lit0;
12376 break;
12377
c906108c
SS
12378 case DW_OP_reg0:
12379 case DW_OP_reg1:
12380 case DW_OP_reg2:
12381 case DW_OP_reg3:
12382 case DW_OP_reg4:
12383 case DW_OP_reg5:
12384 case DW_OP_reg6:
12385 case DW_OP_reg7:
12386 case DW_OP_reg8:
12387 case DW_OP_reg9:
12388 case DW_OP_reg10:
12389 case DW_OP_reg11:
12390 case DW_OP_reg12:
12391 case DW_OP_reg13:
12392 case DW_OP_reg14:
12393 case DW_OP_reg15:
12394 case DW_OP_reg16:
12395 case DW_OP_reg17:
12396 case DW_OP_reg18:
12397 case DW_OP_reg19:
12398 case DW_OP_reg20:
12399 case DW_OP_reg21:
12400 case DW_OP_reg22:
12401 case DW_OP_reg23:
12402 case DW_OP_reg24:
12403 case DW_OP_reg25:
12404 case DW_OP_reg26:
12405 case DW_OP_reg27:
12406 case DW_OP_reg28:
12407 case DW_OP_reg29:
12408 case DW_OP_reg30:
12409 case DW_OP_reg31:
c906108c 12410 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
12411 if (i < size)
12412 dwarf2_complex_location_expr_complaint ();
c906108c
SS
12413 break;
12414
12415 case DW_OP_regx:
c906108c
SS
12416 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12417 i += bytes_read;
c906108c 12418 stack[++stacki] = unsnd;
4cecd739
DJ
12419 if (i < size)
12420 dwarf2_complex_location_expr_complaint ();
c906108c
SS
12421 break;
12422
12423 case DW_OP_addr:
107d2387 12424 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 12425 cu, &bytes_read);
107d2387 12426 i += bytes_read;
c906108c
SS
12427 break;
12428
12429 case DW_OP_const1u:
12430 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
12431 i += 1;
12432 break;
12433
12434 case DW_OP_const1s:
12435 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
12436 i += 1;
12437 break;
12438
12439 case DW_OP_const2u:
12440 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
12441 i += 2;
12442 break;
12443
12444 case DW_OP_const2s:
12445 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
12446 i += 2;
12447 break;
12448
12449 case DW_OP_const4u:
12450 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
12451 i += 4;
12452 break;
12453
12454 case DW_OP_const4s:
12455 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
12456 i += 4;
12457 break;
12458
12459 case DW_OP_constu:
12460 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 12461 &bytes_read);
c906108c
SS
12462 i += bytes_read;
12463 break;
12464
12465 case DW_OP_consts:
12466 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
12467 i += bytes_read;
12468 break;
12469
f1bea926
JM
12470 case DW_OP_dup:
12471 stack[stacki + 1] = stack[stacki];
12472 stacki++;
12473 break;
12474
c906108c
SS
12475 case DW_OP_plus:
12476 stack[stacki - 1] += stack[stacki];
12477 stacki--;
12478 break;
12479
12480 case DW_OP_plus_uconst:
12481 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12482 i += bytes_read;
12483 break;
12484
12485 case DW_OP_minus:
f1bea926 12486 stack[stacki - 1] -= stack[stacki];
c906108c
SS
12487 stacki--;
12488 break;
12489
7a292a7a 12490 case DW_OP_deref:
7a292a7a 12491 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
12492 this using GDB's address_class enum. This is valid for partial
12493 global symbols, although the variable's address will be bogus
12494 in the psymtab. */
7a292a7a 12495 if (i < size)
4d3c2250 12496 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
12497 break;
12498
9d774e44 12499 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
12500 /* The top of the stack has the offset from the beginning
12501 of the thread control block at which the variable is located. */
12502 /* Nothing should follow this operator, so the top of stack would
12503 be returned. */
4cecd739
DJ
12504 /* This is valid for partial global symbols, but the variable's
12505 address will be bogus in the psymtab. */
9d774e44 12506 if (i < size)
4d3c2250 12507 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
12508 break;
12509
42be36b3
CT
12510 case DW_OP_GNU_uninit:
12511 break;
12512
c906108c 12513 default:
e2e0b3e5 12514 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
9eae7c52 12515 dwarf_stack_op_name (op, 1));
c906108c
SS
12516 return (stack[stacki]);
12517 }
12518 }
12519 return (stack[stacki]);
12520}
12521
12522/* memory allocation interface */
12523
c906108c 12524static struct dwarf_block *
7b5a2f43 12525dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
12526{
12527 struct dwarf_block *blk;
12528
12529 blk = (struct dwarf_block *)
7b5a2f43 12530 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
12531 return (blk);
12532}
12533
12534static struct abbrev_info *
f3dd6933 12535dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
12536{
12537 struct abbrev_info *abbrev;
12538
f3dd6933
DJ
12539 abbrev = (struct abbrev_info *)
12540 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
12541 memset (abbrev, 0, sizeof (struct abbrev_info));
12542 return (abbrev);
12543}
12544
12545static struct die_info *
b60c80d6 12546dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
12547{
12548 struct die_info *die;
b60c80d6
DJ
12549 size_t size = sizeof (struct die_info);
12550
12551 if (num_attrs > 1)
12552 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 12553
b60c80d6 12554 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
12555 memset (die, 0, sizeof (struct die_info));
12556 return (die);
12557}
2e276125
JB
12558
12559\f
12560/* Macro support. */
12561
12562
12563/* Return the full name of file number I in *LH's file name table.
12564 Use COMP_DIR as the name of the current directory of the
12565 compilation. The result is allocated using xmalloc; the caller is
12566 responsible for freeing it. */
12567static char *
12568file_full_name (int file, struct line_header *lh, const char *comp_dir)
12569{
6a83a1e6
EZ
12570 /* Is the file number a valid index into the line header's file name
12571 table? Remember that file numbers start with one, not zero. */
12572 if (1 <= file && file <= lh->num_file_names)
12573 {
12574 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 12575
6a83a1e6
EZ
12576 if (IS_ABSOLUTE_PATH (fe->name))
12577 return xstrdup (fe->name);
12578 else
12579 {
12580 const char *dir;
12581 int dir_len;
12582 char *full_name;
12583
12584 if (fe->dir_index)
12585 dir = lh->include_dirs[fe->dir_index - 1];
12586 else
12587 dir = comp_dir;
12588
12589 if (dir)
12590 {
12591 dir_len = strlen (dir);
12592 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
12593 strcpy (full_name, dir);
12594 full_name[dir_len] = '/';
12595 strcpy (full_name + dir_len + 1, fe->name);
12596 return full_name;
12597 }
12598 else
12599 return xstrdup (fe->name);
12600 }
12601 }
2e276125
JB
12602 else
12603 {
6a83a1e6
EZ
12604 /* The compiler produced a bogus file number. We can at least
12605 record the macro definitions made in the file, even if we
12606 won't be able to find the file by name. */
12607 char fake_name[80];
9a619af0 12608
6a83a1e6 12609 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 12610
6e70227d 12611 complaint (&symfile_complaints,
6a83a1e6
EZ
12612 _("bad file number in macro information (%d)"),
12613 file);
2e276125 12614
6a83a1e6 12615 return xstrdup (fake_name);
2e276125
JB
12616 }
12617}
12618
12619
12620static struct macro_source_file *
12621macro_start_file (int file, int line,
12622 struct macro_source_file *current_file,
12623 const char *comp_dir,
12624 struct line_header *lh, struct objfile *objfile)
12625{
12626 /* The full name of this source file. */
12627 char *full_name = file_full_name (file, lh, comp_dir);
12628
12629 /* We don't create a macro table for this compilation unit
12630 at all until we actually get a filename. */
12631 if (! pending_macros)
4a146b47 12632 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 12633 objfile->macro_cache);
2e276125
JB
12634
12635 if (! current_file)
12636 /* If we have no current file, then this must be the start_file
12637 directive for the compilation unit's main source file. */
12638 current_file = macro_set_main (pending_macros, full_name);
12639 else
12640 current_file = macro_include (current_file, line, full_name);
12641
12642 xfree (full_name);
6e70227d 12643
2e276125
JB
12644 return current_file;
12645}
12646
12647
12648/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
12649 followed by a null byte. */
12650static char *
12651copy_string (const char *buf, int len)
12652{
12653 char *s = xmalloc (len + 1);
9a619af0 12654
2e276125
JB
12655 memcpy (s, buf, len);
12656 s[len] = '\0';
2e276125
JB
12657 return s;
12658}
12659
12660
12661static const char *
12662consume_improper_spaces (const char *p, const char *body)
12663{
12664 if (*p == ' ')
12665 {
4d3c2250 12666 complaint (&symfile_complaints,
e2e0b3e5 12667 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 12668 body);
2e276125
JB
12669
12670 while (*p == ' ')
12671 p++;
12672 }
12673
12674 return p;
12675}
12676
12677
12678static void
12679parse_macro_definition (struct macro_source_file *file, int line,
12680 const char *body)
12681{
12682 const char *p;
12683
12684 /* The body string takes one of two forms. For object-like macro
12685 definitions, it should be:
12686
12687 <macro name> " " <definition>
12688
12689 For function-like macro definitions, it should be:
12690
12691 <macro name> "() " <definition>
12692 or
12693 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
12694
12695 Spaces may appear only where explicitly indicated, and in the
12696 <definition>.
12697
12698 The Dwarf 2 spec says that an object-like macro's name is always
12699 followed by a space, but versions of GCC around March 2002 omit
6e70227d 12700 the space when the macro's definition is the empty string.
2e276125
JB
12701
12702 The Dwarf 2 spec says that there should be no spaces between the
12703 formal arguments in a function-like macro's formal argument list,
12704 but versions of GCC around March 2002 include spaces after the
12705 commas. */
12706
12707
12708 /* Find the extent of the macro name. The macro name is terminated
12709 by either a space or null character (for an object-like macro) or
12710 an opening paren (for a function-like macro). */
12711 for (p = body; *p; p++)
12712 if (*p == ' ' || *p == '(')
12713 break;
12714
12715 if (*p == ' ' || *p == '\0')
12716 {
12717 /* It's an object-like macro. */
12718 int name_len = p - body;
12719 char *name = copy_string (body, name_len);
12720 const char *replacement;
12721
12722 if (*p == ' ')
12723 replacement = body + name_len + 1;
12724 else
12725 {
4d3c2250 12726 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12727 replacement = body + name_len;
12728 }
6e70227d 12729
2e276125
JB
12730 macro_define_object (file, line, name, replacement);
12731
12732 xfree (name);
12733 }
12734 else if (*p == '(')
12735 {
12736 /* It's a function-like macro. */
12737 char *name = copy_string (body, p - body);
12738 int argc = 0;
12739 int argv_size = 1;
12740 char **argv = xmalloc (argv_size * sizeof (*argv));
12741
12742 p++;
12743
12744 p = consume_improper_spaces (p, body);
12745
12746 /* Parse the formal argument list. */
12747 while (*p && *p != ')')
12748 {
12749 /* Find the extent of the current argument name. */
12750 const char *arg_start = p;
12751
12752 while (*p && *p != ',' && *p != ')' && *p != ' ')
12753 p++;
12754
12755 if (! *p || p == arg_start)
4d3c2250 12756 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12757 else
12758 {
12759 /* Make sure argv has room for the new argument. */
12760 if (argc >= argv_size)
12761 {
12762 argv_size *= 2;
12763 argv = xrealloc (argv, argv_size * sizeof (*argv));
12764 }
12765
12766 argv[argc++] = copy_string (arg_start, p - arg_start);
12767 }
12768
12769 p = consume_improper_spaces (p, body);
12770
12771 /* Consume the comma, if present. */
12772 if (*p == ',')
12773 {
12774 p++;
12775
12776 p = consume_improper_spaces (p, body);
12777 }
12778 }
12779
12780 if (*p == ')')
12781 {
12782 p++;
12783
12784 if (*p == ' ')
12785 /* Perfectly formed definition, no complaints. */
12786 macro_define_function (file, line, name,
6e70227d 12787 argc, (const char **) argv,
2e276125
JB
12788 p + 1);
12789 else if (*p == '\0')
12790 {
12791 /* Complain, but do define it. */
4d3c2250 12792 dwarf2_macro_malformed_definition_complaint (body);
2e276125 12793 macro_define_function (file, line, name,
6e70227d 12794 argc, (const char **) argv,
2e276125
JB
12795 p);
12796 }
12797 else
12798 /* Just complain. */
4d3c2250 12799 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12800 }
12801 else
12802 /* Just complain. */
4d3c2250 12803 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12804
12805 xfree (name);
12806 {
12807 int i;
12808
12809 for (i = 0; i < argc; i++)
12810 xfree (argv[i]);
12811 }
12812 xfree (argv);
12813 }
12814 else
4d3c2250 12815 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
12816}
12817
12818
12819static void
12820dwarf_decode_macros (struct line_header *lh, unsigned int offset,
12821 char *comp_dir, bfd *abfd,
e7c27a73 12822 struct dwarf2_cu *cu)
2e276125 12823{
fe1b8b76 12824 gdb_byte *mac_ptr, *mac_end;
2e276125 12825 struct macro_source_file *current_file = 0;
757a13d0
JK
12826 enum dwarf_macinfo_record_type macinfo_type;
12827 int at_commandline;
2e276125 12828
be391dca
TT
12829 dwarf2_read_section (dwarf2_per_objfile->objfile,
12830 &dwarf2_per_objfile->macinfo);
dce234bc 12831 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 12832 {
e2e0b3e5 12833 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
12834 return;
12835 }
12836
757a13d0
JK
12837 /* First pass: Find the name of the base filename.
12838 This filename is needed in order to process all macros whose definition
12839 (or undefinition) comes from the command line. These macros are defined
12840 before the first DW_MACINFO_start_file entry, and yet still need to be
12841 associated to the base file.
12842
12843 To determine the base file name, we scan the macro definitions until we
12844 reach the first DW_MACINFO_start_file entry. We then initialize
12845 CURRENT_FILE accordingly so that any macro definition found before the
12846 first DW_MACINFO_start_file can still be associated to the base file. */
12847
dce234bc
PP
12848 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
12849 mac_end = dwarf2_per_objfile->macinfo.buffer
12850 + dwarf2_per_objfile->macinfo.size;
2e276125 12851
757a13d0 12852 do
2e276125 12853 {
2e276125
JB
12854 /* Do we at least have room for a macinfo type byte? */
12855 if (mac_ptr >= mac_end)
12856 {
757a13d0
JK
12857 /* Complaint is printed during the second pass as GDB will probably
12858 stop the first pass earlier upon finding DW_MACINFO_start_file. */
12859 break;
2e276125
JB
12860 }
12861
12862 macinfo_type = read_1_byte (abfd, mac_ptr);
12863 mac_ptr++;
12864
12865 switch (macinfo_type)
12866 {
12867 /* A zero macinfo type indicates the end of the macro
12868 information. */
12869 case 0:
757a13d0
JK
12870 break;
12871
12872 case DW_MACINFO_define:
12873 case DW_MACINFO_undef:
12874 /* Only skip the data by MAC_PTR. */
12875 {
12876 unsigned int bytes_read;
12877
12878 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12879 mac_ptr += bytes_read;
12880 read_string (abfd, mac_ptr, &bytes_read);
12881 mac_ptr += bytes_read;
12882 }
12883 break;
12884
12885 case DW_MACINFO_start_file:
12886 {
12887 unsigned int bytes_read;
12888 int line, file;
12889
12890 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12891 mac_ptr += bytes_read;
12892 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12893 mac_ptr += bytes_read;
12894
12895 current_file = macro_start_file (file, line, current_file, comp_dir,
12896 lh, cu->objfile);
12897 }
12898 break;
12899
12900 case DW_MACINFO_end_file:
12901 /* No data to skip by MAC_PTR. */
12902 break;
12903
12904 case DW_MACINFO_vendor_ext:
12905 /* Only skip the data by MAC_PTR. */
12906 {
12907 unsigned int bytes_read;
12908
12909 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12910 mac_ptr += bytes_read;
12911 read_string (abfd, mac_ptr, &bytes_read);
12912 mac_ptr += bytes_read;
12913 }
12914 break;
12915
12916 default:
12917 break;
12918 }
12919 } while (macinfo_type != 0 && current_file == NULL);
12920
12921 /* Second pass: Process all entries.
12922
12923 Use the AT_COMMAND_LINE flag to determine whether we are still processing
12924 command-line macro definitions/undefinitions. This flag is unset when we
12925 reach the first DW_MACINFO_start_file entry. */
12926
dce234bc 12927 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
12928
12929 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
12930 GDB is still reading the definitions from command line. First
12931 DW_MACINFO_start_file will need to be ignored as it was already executed
12932 to create CURRENT_FILE for the main source holding also the command line
12933 definitions. On first met DW_MACINFO_start_file this flag is reset to
12934 normally execute all the remaining DW_MACINFO_start_file macinfos. */
12935
12936 at_commandline = 1;
12937
12938 do
12939 {
12940 /* Do we at least have room for a macinfo type byte? */
12941 if (mac_ptr >= mac_end)
12942 {
12943 dwarf2_macros_too_long_complaint ();
12944 break;
12945 }
12946
12947 macinfo_type = read_1_byte (abfd, mac_ptr);
12948 mac_ptr++;
12949
12950 switch (macinfo_type)
12951 {
12952 /* A zero macinfo type indicates the end of the macro
12953 information. */
12954 case 0:
12955 break;
2e276125
JB
12956
12957 case DW_MACINFO_define:
12958 case DW_MACINFO_undef:
12959 {
891d2f0b 12960 unsigned int bytes_read;
2e276125
JB
12961 int line;
12962 char *body;
12963
12964 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12965 mac_ptr += bytes_read;
12966 body = read_string (abfd, mac_ptr, &bytes_read);
12967 mac_ptr += bytes_read;
12968
12969 if (! current_file)
757a13d0
JK
12970 {
12971 /* DWARF violation as no main source is present. */
12972 complaint (&symfile_complaints,
12973 _("debug info with no main source gives macro %s "
12974 "on line %d: %s"),
6e70227d
DE
12975 macinfo_type == DW_MACINFO_define ?
12976 _("definition") :
905e0470
PM
12977 macinfo_type == DW_MACINFO_undef ?
12978 _("undefinition") :
12979 _("something-or-other"), line, body);
757a13d0
JK
12980 break;
12981 }
12982 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 12983 complaint (&symfile_complaints,
757a13d0
JK
12984 _("debug info gives %s macro %s with %s line %d: %s"),
12985 at_commandline ? _("command-line") : _("in-file"),
905e0470 12986 macinfo_type == DW_MACINFO_define ?
6e70227d 12987 _("definition") :
905e0470
PM
12988 macinfo_type == DW_MACINFO_undef ?
12989 _("undefinition") :
12990 _("something-or-other"),
757a13d0
JK
12991 line == 0 ? _("zero") : _("non-zero"), line, body);
12992
12993 if (macinfo_type == DW_MACINFO_define)
12994 parse_macro_definition (current_file, line, body);
12995 else if (macinfo_type == DW_MACINFO_undef)
12996 macro_undef (current_file, line, body);
2e276125
JB
12997 }
12998 break;
12999
13000 case DW_MACINFO_start_file:
13001 {
891d2f0b 13002 unsigned int bytes_read;
2e276125
JB
13003 int line, file;
13004
13005 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13006 mac_ptr += bytes_read;
13007 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13008 mac_ptr += bytes_read;
13009
757a13d0
JK
13010 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13011 complaint (&symfile_complaints,
13012 _("debug info gives source %d included "
13013 "from %s at %s line %d"),
13014 file, at_commandline ? _("command-line") : _("file"),
13015 line == 0 ? _("zero") : _("non-zero"), line);
13016
13017 if (at_commandline)
13018 {
13019 /* This DW_MACINFO_start_file was executed in the pass one. */
13020 at_commandline = 0;
13021 }
13022 else
13023 current_file = macro_start_file (file, line,
13024 current_file, comp_dir,
13025 lh, cu->objfile);
2e276125
JB
13026 }
13027 break;
13028
13029 case DW_MACINFO_end_file:
13030 if (! current_file)
4d3c2250 13031 complaint (&symfile_complaints,
e2e0b3e5 13032 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
13033 else
13034 {
13035 current_file = current_file->included_by;
13036 if (! current_file)
13037 {
13038 enum dwarf_macinfo_record_type next_type;
13039
13040 /* GCC circa March 2002 doesn't produce the zero
13041 type byte marking the end of the compilation
13042 unit. Complain if it's not there, but exit no
13043 matter what. */
13044
13045 /* Do we at least have room for a macinfo type byte? */
13046 if (mac_ptr >= mac_end)
13047 {
4d3c2250 13048 dwarf2_macros_too_long_complaint ();
2e276125
JB
13049 return;
13050 }
13051
13052 /* We don't increment mac_ptr here, so this is just
13053 a look-ahead. */
13054 next_type = read_1_byte (abfd, mac_ptr);
13055 if (next_type != 0)
4d3c2250 13056 complaint (&symfile_complaints,
e2e0b3e5 13057 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
13058
13059 return;
13060 }
13061 }
13062 break;
13063
13064 case DW_MACINFO_vendor_ext:
13065 {
891d2f0b 13066 unsigned int bytes_read;
2e276125
JB
13067 int constant;
13068 char *string;
13069
13070 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13071 mac_ptr += bytes_read;
13072 string = read_string (abfd, mac_ptr, &bytes_read);
13073 mac_ptr += bytes_read;
13074
13075 /* We don't recognize any vendor extensions. */
13076 }
13077 break;
13078 }
757a13d0 13079 } while (macinfo_type != 0);
2e276125 13080}
8e19ed76
PS
13081
13082/* Check if the attribute's form is a DW_FORM_block*
13083 if so return true else false. */
13084static int
13085attr_form_is_block (struct attribute *attr)
13086{
13087 return (attr == NULL ? 0 :
13088 attr->form == DW_FORM_block1
13089 || attr->form == DW_FORM_block2
13090 || attr->form == DW_FORM_block4
2dc7f7b3
TT
13091 || attr->form == DW_FORM_block
13092 || attr->form == DW_FORM_exprloc);
8e19ed76 13093}
4c2df51b 13094
c6a0999f
JB
13095/* Return non-zero if ATTR's value is a section offset --- classes
13096 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13097 You may use DW_UNSND (attr) to retrieve such offsets.
13098
13099 Section 7.5.4, "Attribute Encodings", explains that no attribute
13100 may have a value that belongs to more than one of these classes; it
13101 would be ambiguous if we did, because we use the same forms for all
13102 of them. */
3690dd37
JB
13103static int
13104attr_form_is_section_offset (struct attribute *attr)
13105{
13106 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
13107 || attr->form == DW_FORM_data8
13108 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
13109}
13110
13111
13112/* Return non-zero if ATTR's value falls in the 'constant' class, or
13113 zero otherwise. When this function returns true, you can apply
13114 dwarf2_get_attr_constant_value to it.
13115
13116 However, note that for some attributes you must check
13117 attr_form_is_section_offset before using this test. DW_FORM_data4
13118 and DW_FORM_data8 are members of both the constant class, and of
13119 the classes that contain offsets into other debug sections
13120 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
13121 that, if an attribute's can be either a constant or one of the
13122 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13123 taken as section offsets, not constants. */
13124static int
13125attr_form_is_constant (struct attribute *attr)
13126{
13127 switch (attr->form)
13128 {
13129 case DW_FORM_sdata:
13130 case DW_FORM_udata:
13131 case DW_FORM_data1:
13132 case DW_FORM_data2:
13133 case DW_FORM_data4:
13134 case DW_FORM_data8:
13135 return 1;
13136 default:
13137 return 0;
13138 }
13139}
13140
4c2df51b
DJ
13141static void
13142dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 13143 struct dwarf2_cu *cu)
4c2df51b 13144{
3690dd37 13145 if (attr_form_is_section_offset (attr)
99bcc461
DJ
13146 /* ".debug_loc" may not exist at all, or the offset may be outside
13147 the section. If so, fall through to the complaint in the
13148 other branch. */
dce234bc 13149 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 13150 {
0d53c4c4 13151 struct dwarf2_loclist_baton *baton;
4c2df51b 13152
4a146b47 13153 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13154 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
13155 baton->per_cu = cu->per_cu;
13156 gdb_assert (baton->per_cu);
4c2df51b 13157
be391dca
TT
13158 dwarf2_read_section (dwarf2_per_objfile->objfile,
13159 &dwarf2_per_objfile->loc);
13160
0d53c4c4
DJ
13161 /* We don't know how long the location list is, but make sure we
13162 don't run off the edge of the section. */
dce234bc
PP
13163 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13164 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
13165 baton->base_address = cu->base_address;
13166 if (cu->base_known == 0)
0d53c4c4 13167 complaint (&symfile_complaints,
e2e0b3e5 13168 _("Location list used without specifying the CU base address."));
4c2df51b 13169
768a979c 13170 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
13171 SYMBOL_LOCATION_BATON (sym) = baton;
13172 }
13173 else
13174 {
13175 struct dwarf2_locexpr_baton *baton;
13176
4a146b47 13177 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 13178 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
13179 baton->per_cu = cu->per_cu;
13180 gdb_assert (baton->per_cu);
0d53c4c4
DJ
13181
13182 if (attr_form_is_block (attr))
13183 {
13184 /* Note that we're just copying the block's data pointer
13185 here, not the actual data. We're still pointing into the
6502dd73
DJ
13186 info_buffer for SYM's objfile; right now we never release
13187 that buffer, but when we do clean up properly this may
13188 need to change. */
0d53c4c4
DJ
13189 baton->size = DW_BLOCK (attr)->size;
13190 baton->data = DW_BLOCK (attr)->data;
13191 }
13192 else
13193 {
13194 dwarf2_invalid_attrib_class_complaint ("location description",
13195 SYMBOL_NATURAL_NAME (sym));
13196 baton->size = 0;
13197 baton->data = NULL;
13198 }
6e70227d 13199
768a979c 13200 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
13201 SYMBOL_LOCATION_BATON (sym) = baton;
13202 }
4c2df51b 13203}
6502dd73 13204
9aa1f1e3
TT
13205/* Return the OBJFILE associated with the compilation unit CU. If CU
13206 came from a separate debuginfo file, then the master objfile is
13207 returned. */
ae0d2f24
UW
13208
13209struct objfile *
13210dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13211{
9291a0cd 13212 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13213
13214 /* Return the master objfile, so that we can report and look up the
13215 correct file containing this variable. */
13216 if (objfile->separate_debug_objfile_backlink)
13217 objfile = objfile->separate_debug_objfile_backlink;
13218
13219 return objfile;
13220}
13221
13222/* Return the address size given in the compilation unit header for CU. */
13223
13224CORE_ADDR
13225dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13226{
13227 if (per_cu->cu)
13228 return per_cu->cu->header.addr_size;
13229 else
13230 {
13231 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13232 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
13233 struct dwarf2_per_objfile *per_objfile
13234 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 13235 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24 13236 struct comp_unit_head cu_header;
9a619af0 13237
ae0d2f24
UW
13238 memset (&cu_header, 0, sizeof cu_header);
13239 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13240 return cu_header.addr_size;
13241 }
13242}
13243
9eae7c52
TT
13244/* Return the offset size given in the compilation unit header for CU. */
13245
13246int
13247dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13248{
13249 if (per_cu->cu)
13250 return per_cu->cu->header.offset_size;
13251 else
13252 {
13253 /* If the CU is not currently read in, we re-read its header. */
9291a0cd 13254 struct objfile *objfile = per_cu->objfile;
9eae7c52
TT
13255 struct dwarf2_per_objfile *per_objfile
13256 = objfile_data (objfile, dwarf2_objfile_data_key);
13257 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13258 struct comp_unit_head cu_header;
13259
13260 memset (&cu_header, 0, sizeof cu_header);
13261 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13262 return cu_header.offset_size;
13263 }
13264}
13265
9aa1f1e3
TT
13266/* Return the text offset of the CU. The returned offset comes from
13267 this CU's objfile. If this objfile came from a separate debuginfo
13268 file, then the offset may be different from the corresponding
13269 offset in the parent objfile. */
13270
13271CORE_ADDR
13272dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13273{
bb3fa9d0 13274 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
13275
13276 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13277}
13278
348e048f
DE
13279/* Locate the .debug_info compilation unit from CU's objfile which contains
13280 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
13281
13282static struct dwarf2_per_cu_data *
c764a876 13283dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
13284 struct objfile *objfile)
13285{
13286 struct dwarf2_per_cu_data *this_cu;
13287 int low, high;
13288
ae038cb0
DJ
13289 low = 0;
13290 high = dwarf2_per_objfile->n_comp_units - 1;
13291 while (high > low)
13292 {
13293 int mid = low + (high - low) / 2;
9a619af0 13294
ae038cb0
DJ
13295 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
13296 high = mid;
13297 else
13298 low = mid + 1;
13299 }
13300 gdb_assert (low == high);
13301 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
13302 {
10b3939b 13303 if (low == 0)
8a3fe4f8
AC
13304 error (_("Dwarf Error: could not find partial DIE containing "
13305 "offset 0x%lx [in module %s]"),
10b3939b
DJ
13306 (long) offset, bfd_get_filename (objfile->obfd));
13307
ae038cb0
DJ
13308 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
13309 return dwarf2_per_objfile->all_comp_units[low-1];
13310 }
13311 else
13312 {
13313 this_cu = dwarf2_per_objfile->all_comp_units[low];
13314 if (low == dwarf2_per_objfile->n_comp_units - 1
13315 && offset >= this_cu->offset + this_cu->length)
c764a876 13316 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
13317 gdb_assert (offset < this_cu->offset + this_cu->length);
13318 return this_cu;
13319 }
13320}
13321
10b3939b
DJ
13322/* Locate the compilation unit from OBJFILE which is located at exactly
13323 OFFSET. Raises an error on failure. */
13324
ae038cb0 13325static struct dwarf2_per_cu_data *
c764a876 13326dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
13327{
13328 struct dwarf2_per_cu_data *this_cu;
9a619af0 13329
ae038cb0
DJ
13330 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
13331 if (this_cu->offset != offset)
c764a876 13332 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
13333 return this_cu;
13334}
13335
93311388
DE
13336/* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
13337
13338static struct dwarf2_cu *
13339alloc_one_comp_unit (struct objfile *objfile)
13340{
13341 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
13342 cu->objfile = objfile;
13343 obstack_init (&cu->comp_unit_obstack);
13344 return cu;
13345}
13346
ae038cb0
DJ
13347/* Release one cached compilation unit, CU. We unlink it from the tree
13348 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
13349 the caller is responsible for that.
13350 NOTE: DATA is a void * because this function is also used as a
13351 cleanup routine. */
ae038cb0
DJ
13352
13353static void
13354free_one_comp_unit (void *data)
13355{
13356 struct dwarf2_cu *cu = data;
13357
13358 if (cu->per_cu != NULL)
13359 cu->per_cu->cu = NULL;
13360 cu->per_cu = NULL;
13361
13362 obstack_free (&cu->comp_unit_obstack, NULL);
13363
13364 xfree (cu);
13365}
13366
72bf9492 13367/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
13368 when we're finished with it. We can't free the pointer itself, but be
13369 sure to unlink it from the cache. Also release any associated storage
13370 and perform cache maintenance.
72bf9492
DJ
13371
13372 Only used during partial symbol parsing. */
13373
13374static void
13375free_stack_comp_unit (void *data)
13376{
13377 struct dwarf2_cu *cu = data;
13378
13379 obstack_free (&cu->comp_unit_obstack, NULL);
13380 cu->partial_dies = NULL;
ae038cb0
DJ
13381
13382 if (cu->per_cu != NULL)
13383 {
13384 /* This compilation unit is on the stack in our caller, so we
13385 should not xfree it. Just unlink it. */
13386 cu->per_cu->cu = NULL;
13387 cu->per_cu = NULL;
13388
13389 /* If we had a per-cu pointer, then we may have other compilation
13390 units loaded, so age them now. */
13391 age_cached_comp_units ();
13392 }
13393}
13394
13395/* Free all cached compilation units. */
13396
13397static void
13398free_cached_comp_units (void *data)
13399{
13400 struct dwarf2_per_cu_data *per_cu, **last_chain;
13401
13402 per_cu = dwarf2_per_objfile->read_in_chain;
13403 last_chain = &dwarf2_per_objfile->read_in_chain;
13404 while (per_cu != NULL)
13405 {
13406 struct dwarf2_per_cu_data *next_cu;
13407
13408 next_cu = per_cu->cu->read_in_chain;
13409
13410 free_one_comp_unit (per_cu->cu);
13411 *last_chain = next_cu;
13412
13413 per_cu = next_cu;
13414 }
13415}
13416
13417/* Increase the age counter on each cached compilation unit, and free
13418 any that are too old. */
13419
13420static void
13421age_cached_comp_units (void)
13422{
13423 struct dwarf2_per_cu_data *per_cu, **last_chain;
13424
13425 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
13426 per_cu = dwarf2_per_objfile->read_in_chain;
13427 while (per_cu != NULL)
13428 {
13429 per_cu->cu->last_used ++;
13430 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
13431 dwarf2_mark (per_cu->cu);
13432 per_cu = per_cu->cu->read_in_chain;
13433 }
13434
13435 per_cu = dwarf2_per_objfile->read_in_chain;
13436 last_chain = &dwarf2_per_objfile->read_in_chain;
13437 while (per_cu != NULL)
13438 {
13439 struct dwarf2_per_cu_data *next_cu;
13440
13441 next_cu = per_cu->cu->read_in_chain;
13442
13443 if (!per_cu->cu->mark)
13444 {
13445 free_one_comp_unit (per_cu->cu);
13446 *last_chain = next_cu;
13447 }
13448 else
13449 last_chain = &per_cu->cu->read_in_chain;
13450
13451 per_cu = next_cu;
13452 }
13453}
13454
13455/* Remove a single compilation unit from the cache. */
13456
13457static void
13458free_one_cached_comp_unit (void *target_cu)
13459{
13460 struct dwarf2_per_cu_data *per_cu, **last_chain;
13461
13462 per_cu = dwarf2_per_objfile->read_in_chain;
13463 last_chain = &dwarf2_per_objfile->read_in_chain;
13464 while (per_cu != NULL)
13465 {
13466 struct dwarf2_per_cu_data *next_cu;
13467
13468 next_cu = per_cu->cu->read_in_chain;
13469
13470 if (per_cu->cu == target_cu)
13471 {
13472 free_one_comp_unit (per_cu->cu);
13473 *last_chain = next_cu;
13474 break;
13475 }
13476 else
13477 last_chain = &per_cu->cu->read_in_chain;
13478
13479 per_cu = next_cu;
13480 }
13481}
13482
fe3e1990
DJ
13483/* Release all extra memory associated with OBJFILE. */
13484
13485void
13486dwarf2_free_objfile (struct objfile *objfile)
13487{
13488 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
13489
13490 if (dwarf2_per_objfile == NULL)
13491 return;
13492
13493 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
13494 free_cached_comp_units (NULL);
13495
9291a0cd
TT
13496 if (dwarf2_per_objfile->using_index)
13497 {
13498 int i;
13499
13500 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
13501 {
13502 int j;
13503 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
13504
13505 if (!cu->v.quick->lines)
13506 continue;
13507
13508 for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
13509 {
13510 if (cu->v.quick->file_names)
13511 xfree ((void *) cu->v.quick->file_names[j]);
13512 if (cu->v.quick->full_names)
13513 xfree ((void *) cu->v.quick->full_names[j]);
13514 }
13515
13516 free_line_header (cu->v.quick->lines);
13517 }
13518 }
13519
fe3e1990
DJ
13520 /* Everything else should be on the objfile obstack. */
13521}
13522
1c379e20
DJ
13523/* A pair of DIE offset and GDB type pointer. We store these
13524 in a hash table separate from the DIEs, and preserve them
13525 when the DIEs are flushed out of cache. */
13526
13527struct dwarf2_offset_and_type
13528{
13529 unsigned int offset;
13530 struct type *type;
13531};
13532
13533/* Hash function for a dwarf2_offset_and_type. */
13534
13535static hashval_t
13536offset_and_type_hash (const void *item)
13537{
13538 const struct dwarf2_offset_and_type *ofs = item;
9a619af0 13539
1c379e20
DJ
13540 return ofs->offset;
13541}
13542
13543/* Equality function for a dwarf2_offset_and_type. */
13544
13545static int
13546offset_and_type_eq (const void *item_lhs, const void *item_rhs)
13547{
13548 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
13549 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9a619af0 13550
1c379e20
DJ
13551 return ofs_lhs->offset == ofs_rhs->offset;
13552}
13553
13554/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
13555 table if necessary. For convenience, return TYPE.
13556
13557 The DIEs reading must have careful ordering to:
13558 * Not cause infite loops trying to read in DIEs as a prerequisite for
13559 reading current DIE.
13560 * Not trying to dereference contents of still incompletely read in types
13561 while reading in other DIEs.
13562 * Enable referencing still incompletely read in types just by a pointer to
13563 the type without accessing its fields.
13564
13565 Therefore caller should follow these rules:
13566 * Try to fetch any prerequisite types we may need to build this DIE type
13567 before building the type and calling set_die_type.
13568 * After building typer call set_die_type for current DIE as soon as
13569 possible before fetching more types to complete the current type.
13570 * Make the type as complete as possible before fetching more types. */
1c379e20 13571
f792889a 13572static struct type *
1c379e20
DJ
13573set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13574{
13575 struct dwarf2_offset_and_type **slot, ofs;
13576
b4ba55a1
JB
13577 /* For Ada types, make sure that the gnat-specific data is always
13578 initialized (if not already set). There are a few types where
13579 we should not be doing so, because the type-specific area is
13580 already used to hold some other piece of info (eg: TYPE_CODE_FLT
13581 where the type-specific area is used to store the floatformat).
13582 But this is not a problem, because the gnat-specific information
13583 is actually not needed for these types. */
13584 if (need_gnat_info (cu)
13585 && TYPE_CODE (type) != TYPE_CODE_FUNC
13586 && TYPE_CODE (type) != TYPE_CODE_FLT
13587 && !HAVE_GNAT_AUX_INFO (type))
13588 INIT_GNAT_SPECIFIC (type);
13589
f792889a
DJ
13590 if (cu->type_hash == NULL)
13591 {
13592 gdb_assert (cu->per_cu != NULL);
13593 cu->per_cu->type_hash
13594 = htab_create_alloc_ex (cu->header.length / 24,
13595 offset_and_type_hash,
13596 offset_and_type_eq,
13597 NULL,
13598 &cu->objfile->objfile_obstack,
13599 hashtab_obstack_allocate,
13600 dummy_obstack_deallocate);
13601 cu->type_hash = cu->per_cu->type_hash;
13602 }
1c379e20
DJ
13603
13604 ofs.offset = die->offset;
13605 ofs.type = type;
13606 slot = (struct dwarf2_offset_and_type **)
f792889a 13607 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
7e314c57
JK
13608 if (*slot)
13609 complaint (&symfile_complaints,
13610 _("A problem internal to GDB: DIE 0x%x has type already set"),
13611 die->offset);
1c379e20
DJ
13612 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
13613 **slot = ofs;
f792889a 13614 return type;
1c379e20
DJ
13615}
13616
f792889a
DJ
13617/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
13618 not have a saved type. */
1c379e20
DJ
13619
13620static struct type *
f792889a 13621get_die_type (struct die_info *die, struct dwarf2_cu *cu)
1c379e20
DJ
13622{
13623 struct dwarf2_offset_and_type *slot, ofs;
f792889a
DJ
13624 htab_t type_hash = cu->type_hash;
13625
13626 if (type_hash == NULL)
13627 return NULL;
1c379e20
DJ
13628
13629 ofs.offset = die->offset;
13630 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
13631 if (slot)
13632 return slot->type;
13633 else
13634 return NULL;
13635}
13636
10b3939b
DJ
13637/* Add a dependence relationship from CU to REF_PER_CU. */
13638
13639static void
13640dwarf2_add_dependence (struct dwarf2_cu *cu,
13641 struct dwarf2_per_cu_data *ref_per_cu)
13642{
13643 void **slot;
13644
13645 if (cu->dependencies == NULL)
13646 cu->dependencies
13647 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
13648 NULL, &cu->comp_unit_obstack,
13649 hashtab_obstack_allocate,
13650 dummy_obstack_deallocate);
13651
13652 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
13653 if (*slot == NULL)
13654 *slot = ref_per_cu;
13655}
1c379e20 13656
f504f079
DE
13657/* Subroutine of dwarf2_mark to pass to htab_traverse.
13658 Set the mark field in every compilation unit in the
ae038cb0
DJ
13659 cache that we must keep because we are keeping CU. */
13660
10b3939b
DJ
13661static int
13662dwarf2_mark_helper (void **slot, void *data)
13663{
13664 struct dwarf2_per_cu_data *per_cu;
13665
13666 per_cu = (struct dwarf2_per_cu_data *) *slot;
13667 if (per_cu->cu->mark)
13668 return 1;
13669 per_cu->cu->mark = 1;
13670
13671 if (per_cu->cu->dependencies != NULL)
13672 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
13673
13674 return 1;
13675}
13676
f504f079
DE
13677/* Set the mark field in CU and in every other compilation unit in the
13678 cache that we must keep because we are keeping CU. */
13679
ae038cb0
DJ
13680static void
13681dwarf2_mark (struct dwarf2_cu *cu)
13682{
13683 if (cu->mark)
13684 return;
13685 cu->mark = 1;
10b3939b
DJ
13686 if (cu->dependencies != NULL)
13687 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
13688}
13689
13690static void
13691dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
13692{
13693 while (per_cu)
13694 {
13695 per_cu->cu->mark = 0;
13696 per_cu = per_cu->cu->read_in_chain;
13697 }
72bf9492
DJ
13698}
13699
72bf9492
DJ
13700/* Trivial hash function for partial_die_info: the hash value of a DIE
13701 is its offset in .debug_info for this objfile. */
13702
13703static hashval_t
13704partial_die_hash (const void *item)
13705{
13706 const struct partial_die_info *part_die = item;
9a619af0 13707
72bf9492
DJ
13708 return part_die->offset;
13709}
13710
13711/* Trivial comparison function for partial_die_info structures: two DIEs
13712 are equal if they have the same offset. */
13713
13714static int
13715partial_die_eq (const void *item_lhs, const void *item_rhs)
13716{
13717 const struct partial_die_info *part_die_lhs = item_lhs;
13718 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 13719
72bf9492
DJ
13720 return part_die_lhs->offset == part_die_rhs->offset;
13721}
13722
ae038cb0
DJ
13723static struct cmd_list_element *set_dwarf2_cmdlist;
13724static struct cmd_list_element *show_dwarf2_cmdlist;
13725
13726static void
13727set_dwarf2_cmd (char *args, int from_tty)
13728{
13729 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
13730}
13731
13732static void
13733show_dwarf2_cmd (char *args, int from_tty)
6e70227d 13734{
ae038cb0
DJ
13735 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
13736}
13737
dce234bc
PP
13738/* If section described by INFO was mmapped, munmap it now. */
13739
13740static void
13741munmap_section_buffer (struct dwarf2_section_info *info)
13742{
13743 if (info->was_mmapped)
13744 {
13745#ifdef HAVE_MMAP
13746 intptr_t begin = (intptr_t) info->buffer;
13747 intptr_t map_begin = begin & ~(pagesize - 1);
13748 size_t map_length = info->size + begin - map_begin;
9a619af0 13749
dce234bc
PP
13750 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
13751#else
13752 /* Without HAVE_MMAP, we should never be here to begin with. */
13753 gdb_assert (0);
13754#endif
13755 }
13756}
13757
13758/* munmap debug sections for OBJFILE, if necessary. */
13759
13760static void
c1bd65d0 13761dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
13762{
13763 struct dwarf2_per_objfile *data = d;
9a619af0 13764
dce234bc
PP
13765 munmap_section_buffer (&data->info);
13766 munmap_section_buffer (&data->abbrev);
13767 munmap_section_buffer (&data->line);
13768 munmap_section_buffer (&data->str);
13769 munmap_section_buffer (&data->macinfo);
13770 munmap_section_buffer (&data->ranges);
13771 munmap_section_buffer (&data->loc);
13772 munmap_section_buffer (&data->frame);
13773 munmap_section_buffer (&data->eh_frame);
9291a0cd
TT
13774 munmap_section_buffer (&data->gdb_index);
13775}
13776
13777\f
13778
13779/* The contents of the hash table we create when building the string
13780 table. */
13781struct strtab_entry
13782{
13783 offset_type offset;
13784 const char *str;
13785};
13786
13787/* Hash function for a strtab_entry. */
13788static hashval_t
13789hash_strtab_entry (const void *e)
13790{
13791 const struct strtab_entry *entry = e;
13792 return mapped_index_string_hash (entry->str);
13793}
13794
13795/* Equality function for a strtab_entry. */
13796static int
13797eq_strtab_entry (const void *a, const void *b)
13798{
13799 const struct strtab_entry *ea = a;
13800 const struct strtab_entry *eb = b;
13801 return !strcmp (ea->str, eb->str);
13802}
13803
13804/* Create a strtab_entry hash table. */
13805static htab_t
13806create_strtab (void)
13807{
13808 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
13809 xfree, xcalloc, xfree);
13810}
13811
13812/* Add a string to the constant pool. Return the string's offset in
13813 host order. */
13814static offset_type
13815add_string (htab_t table, struct obstack *cpool, const char *str)
13816{
13817 void **slot;
13818 struct strtab_entry entry;
13819 struct strtab_entry *result;
13820
13821 entry.str = str;
13822 slot = htab_find_slot (table, &entry, INSERT);
13823 if (*slot)
13824 result = *slot;
13825 else
13826 {
13827 result = XNEW (struct strtab_entry);
13828 result->offset = obstack_object_size (cpool);
13829 result->str = str;
13830 obstack_grow_str0 (cpool, str);
13831 *slot = result;
13832 }
13833 return result->offset;
13834}
13835
13836/* An entry in the symbol table. */
13837struct symtab_index_entry
13838{
13839 /* The name of the symbol. */
13840 const char *name;
13841 /* The offset of the name in the constant pool. */
13842 offset_type index_offset;
13843 /* A sorted vector of the indices of all the CUs that hold an object
13844 of this name. */
13845 VEC (offset_type) *cu_indices;
13846};
13847
13848/* The symbol table. This is a power-of-2-sized hash table. */
13849struct mapped_symtab
13850{
13851 offset_type n_elements;
13852 offset_type size;
13853 struct symtab_index_entry **data;
13854};
13855
13856/* Hash function for a symtab_index_entry. */
13857static hashval_t
13858hash_symtab_entry (const void *e)
13859{
13860 const struct symtab_index_entry *entry = e;
13861 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
13862 sizeof (offset_type) * VEC_length (offset_type,
13863 entry->cu_indices),
13864 0);
13865}
13866
13867/* Equality function for a symtab_index_entry. */
13868static int
13869eq_symtab_entry (const void *a, const void *b)
13870{
13871 const struct symtab_index_entry *ea = a;
13872 const struct symtab_index_entry *eb = b;
13873 int len = VEC_length (offset_type, ea->cu_indices);
13874 if (len != VEC_length (offset_type, eb->cu_indices))
13875 return 0;
13876 return !memcmp (VEC_address (offset_type, ea->cu_indices),
13877 VEC_address (offset_type, eb->cu_indices),
13878 sizeof (offset_type) * len);
13879}
13880
13881/* Destroy a symtab_index_entry. */
13882static void
13883delete_symtab_entry (void *p)
13884{
13885 struct symtab_index_entry *entry = p;
13886 VEC_free (offset_type, entry->cu_indices);
13887 xfree (entry);
13888}
13889
13890/* Create a hash table holding symtab_index_entry objects. */
13891static htab_t
13892create_index_table (void)
13893{
13894 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
13895 delete_symtab_entry, xcalloc, xfree);
13896}
13897
13898/* Create a new mapped symtab object. */
13899static struct mapped_symtab *
13900create_mapped_symtab (void)
13901{
13902 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
13903 symtab->n_elements = 0;
13904 symtab->size = 1024;
13905 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
13906 return symtab;
13907}
13908
13909/* Destroy a mapped_symtab. */
13910static void
13911cleanup_mapped_symtab (void *p)
13912{
13913 struct mapped_symtab *symtab = p;
13914 /* The contents of the array are freed when the other hash table is
13915 destroyed. */
13916 xfree (symtab->data);
13917 xfree (symtab);
13918}
13919
13920/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
13921 the slot. */
13922static struct symtab_index_entry **
13923find_slot (struct mapped_symtab *symtab, const char *name)
13924{
13925 offset_type index, step, hash = mapped_index_string_hash (name);
13926
13927 index = hash & (symtab->size - 1);
13928 step = ((hash * 17) & (symtab->size - 1)) | 1;
13929
13930 for (;;)
13931 {
13932 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
13933 return &symtab->data[index];
13934 index = (index + step) & (symtab->size - 1);
13935 }
13936}
13937
13938/* Expand SYMTAB's hash table. */
13939static void
13940hash_expand (struct mapped_symtab *symtab)
13941{
13942 offset_type old_size = symtab->size;
13943 offset_type i;
13944 struct symtab_index_entry **old_entries = symtab->data;
13945
13946 symtab->size *= 2;
13947 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
13948
13949 for (i = 0; i < old_size; ++i)
13950 {
13951 if (old_entries[i])
13952 {
13953 struct symtab_index_entry **slot = find_slot (symtab,
13954 old_entries[i]->name);
13955 *slot = old_entries[i];
13956 }
13957 }
13958
13959 xfree (old_entries);
13960}
13961
13962/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
13963 is the index of the CU in which the symbol appears. */
13964static void
13965add_index_entry (struct mapped_symtab *symtab, const char *name,
13966 offset_type cu_index)
13967{
13968 struct symtab_index_entry **slot;
13969
13970 ++symtab->n_elements;
13971 if (4 * symtab->n_elements / 3 >= symtab->size)
13972 hash_expand (symtab);
13973
13974 slot = find_slot (symtab, name);
13975 if (!*slot)
13976 {
13977 *slot = XNEW (struct symtab_index_entry);
13978 (*slot)->name = name;
13979 (*slot)->cu_indices = NULL;
13980 }
13981 /* Don't push an index twice. Due to how we add entries we only
13982 have to check the last one. */
13983 if (VEC_empty (offset_type, (*slot)->cu_indices)
13984 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
13985 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
13986}
13987
13988/* Add a vector of indices to the constant pool. */
13989static offset_type
13990add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
13991 struct symtab_index_entry *entry)
13992{
13993 void **slot;
13994
13995 slot = htab_find_slot (index_table, entry, INSERT);
13996 if (!*slot)
13997 {
13998 offset_type len = VEC_length (offset_type, entry->cu_indices);
13999 offset_type val = MAYBE_SWAP (len);
14000 offset_type iter;
14001 int i;
14002
14003 *slot = entry;
14004 entry->index_offset = obstack_object_size (cpool);
14005
14006 obstack_grow (cpool, &val, sizeof (val));
14007 for (i = 0;
14008 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14009 ++i)
14010 {
14011 val = MAYBE_SWAP (iter);
14012 obstack_grow (cpool, &val, sizeof (val));
14013 }
14014 }
14015 else
14016 {
14017 struct symtab_index_entry *old_entry = *slot;
14018 entry->index_offset = old_entry->index_offset;
14019 entry = old_entry;
14020 }
14021 return entry->index_offset;
14022}
14023
14024/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14025 constant pool entries going into the obstack CPOOL. */
14026static void
14027write_hash_table (struct mapped_symtab *symtab,
14028 struct obstack *output, struct obstack *cpool)
14029{
14030 offset_type i;
14031 htab_t index_table;
14032 htab_t str_table;
14033
14034 index_table = create_index_table ();
14035 str_table = create_strtab ();
14036 /* We add all the index vectors to the constant pool first, to
14037 ensure alignment is ok. */
14038 for (i = 0; i < symtab->size; ++i)
14039 {
14040 if (symtab->data[i])
14041 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14042 }
14043
14044 /* Now write out the hash table. */
14045 for (i = 0; i < symtab->size; ++i)
14046 {
14047 offset_type str_off, vec_off;
14048
14049 if (symtab->data[i])
14050 {
14051 str_off = add_string (str_table, cpool, symtab->data[i]->name);
14052 vec_off = symtab->data[i]->index_offset;
14053 }
14054 else
14055 {
14056 /* While 0 is a valid constant pool index, it is not valid
14057 to have 0 for both offsets. */
14058 str_off = 0;
14059 vec_off = 0;
14060 }
14061
14062 str_off = MAYBE_SWAP (str_off);
14063 vec_off = MAYBE_SWAP (vec_off);
14064
14065 obstack_grow (output, &str_off, sizeof (str_off));
14066 obstack_grow (output, &vec_off, sizeof (vec_off));
14067 }
14068
14069 htab_delete (str_table);
14070 htab_delete (index_table);
14071}
14072
14073/* Write an address entry to ADDR_OBSTACK. The addresses are taken
14074 from PST; CU_INDEX is the index of the CU in the vector of all
14075 CUs. */
14076static void
14077add_address_entry (struct objfile *objfile,
14078 struct obstack *addr_obstack, struct partial_symtab *pst,
14079 unsigned int cu_index)
14080{
14081 offset_type offset;
14082 char addr[8];
14083 CORE_ADDR baseaddr;
14084
1fd400ff
TT
14085 /* Don't bother recording empty ranges. */
14086 if (pst->textlow == pst->texthigh)
14087 return;
14088
9291a0cd
TT
14089 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14090
14091 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14092 obstack_grow (addr_obstack, addr, 8);
14093 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14094 obstack_grow (addr_obstack, addr, 8);
14095 offset = MAYBE_SWAP (cu_index);
14096 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14097}
14098
14099/* Add a list of partial symbols to SYMTAB. */
14100static void
14101write_psymbols (struct mapped_symtab *symtab,
14102 struct partial_symbol **psymp,
14103 int count,
14104 offset_type cu_index)
14105{
14106 for (; count-- > 0; ++psymp)
14107 {
14108 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14109 error (_("Ada is not currently supported by the index"));
14110 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14111 }
14112}
14113
14114/* Write the contents of an ("unfinished") obstack to FILE. Throw an
14115 exception if there is an error. */
14116static void
14117write_obstack (FILE *file, struct obstack *obstack)
14118{
14119 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14120 file)
14121 != obstack_object_size (obstack))
14122 error (_("couldn't data write to file"));
14123}
14124
14125/* Unlink a file if the argument is not NULL. */
14126static void
14127unlink_if_set (void *p)
14128{
14129 char **filename = p;
14130 if (*filename)
14131 unlink (*filename);
14132}
14133
1fd400ff
TT
14134/* A helper struct used when iterating over debug_types. */
14135struct signatured_type_index_data
14136{
14137 struct objfile *objfile;
14138 struct mapped_symtab *symtab;
14139 struct obstack *types_list;
14140 int cu_index;
14141};
14142
14143/* A helper function that writes a single signatured_type to an
14144 obstack. */
14145static int
14146write_one_signatured_type (void **slot, void *d)
14147{
14148 struct signatured_type_index_data *info = d;
14149 struct signatured_type *entry = (struct signatured_type *) *slot;
14150 struct dwarf2_per_cu_data *cu = &entry->per_cu;
14151 struct partial_symtab *psymtab = cu->v.psymtab;
14152 gdb_byte val[8];
14153
14154 write_psymbols (info->symtab,
14155 info->objfile->global_psymbols.list + psymtab->globals_offset,
14156 psymtab->n_global_syms, info->cu_index);
14157 write_psymbols (info->symtab,
14158 info->objfile->static_psymbols.list + psymtab->statics_offset,
14159 psymtab->n_static_syms, info->cu_index);
14160
14161 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14162 obstack_grow (info->types_list, val, 8);
14163 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14164 obstack_grow (info->types_list, val, 8);
14165 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14166 obstack_grow (info->types_list, val, 8);
14167
14168 ++info->cu_index;
14169
14170 return 1;
14171}
14172
9291a0cd
TT
14173/* Create an index file for OBJFILE in the directory DIR. */
14174static void
14175write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14176{
14177 struct cleanup *cleanup;
14178 char *filename, *cleanup_filename;
1fd400ff
TT
14179 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14180 struct obstack cu_list, types_cu_list;
9291a0cd
TT
14181 int i;
14182 FILE *out_file;
14183 struct mapped_symtab *symtab;
14184 offset_type val, size_of_contents, total_len;
14185 struct stat st;
14186 char buf[8];
14187
14188 if (!objfile->psymtabs)
14189 return;
14190 if (dwarf2_per_objfile->using_index)
14191 error (_("Cannot use an index to create the index"));
14192
14193 if (stat (objfile->name, &st) < 0)
14194 perror_with_name (_("Could not stat"));
14195
14196 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
14197 INDEX_SUFFIX, (char *) NULL);
14198 cleanup = make_cleanup (xfree, filename);
14199
14200 out_file = fopen (filename, "wb");
14201 if (!out_file)
14202 error (_("Can't open `%s' for writing"), filename);
14203
14204 cleanup_filename = filename;
14205 make_cleanup (unlink_if_set, &cleanup_filename);
14206
14207 symtab = create_mapped_symtab ();
14208 make_cleanup (cleanup_mapped_symtab, symtab);
14209
14210 obstack_init (&addr_obstack);
14211 make_cleanup_obstack_free (&addr_obstack);
14212
14213 obstack_init (&cu_list);
14214 make_cleanup_obstack_free (&cu_list);
14215
1fd400ff
TT
14216 obstack_init (&types_cu_list);
14217 make_cleanup_obstack_free (&types_cu_list);
14218
14219 /* The list is already sorted, so we don't need to do additional
14220 work here. Also, the debug_types entries do not appear in
14221 all_comp_units, but only in their own hash table. */
9291a0cd
TT
14222 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14223 {
14224 struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14225 struct partial_symtab *psymtab = cu->v.psymtab;
14226 gdb_byte val[8];
14227
14228 write_psymbols (symtab,
14229 objfile->global_psymbols.list + psymtab->globals_offset,
14230 psymtab->n_global_syms, i);
14231 write_psymbols (symtab,
14232 objfile->static_psymbols.list + psymtab->statics_offset,
14233 psymtab->n_static_syms, i);
14234
14235 add_address_entry (objfile, &addr_obstack, psymtab, i);
14236
14237 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14238 obstack_grow (&cu_list, val, 8);
14239 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14240 obstack_grow (&cu_list, val, 8);
14241 }
14242
1fd400ff
TT
14243 /* Write out the .debug_type entries, if any. */
14244 if (dwarf2_per_objfile->signatured_types)
14245 {
14246 struct signatured_type_index_data sig_data;
14247
14248 sig_data.objfile = objfile;
14249 sig_data.symtab = symtab;
14250 sig_data.types_list = &types_cu_list;
14251 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
14252 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
14253 write_one_signatured_type, &sig_data);
14254 }
14255
9291a0cd
TT
14256 obstack_init (&constant_pool);
14257 make_cleanup_obstack_free (&constant_pool);
14258 obstack_init (&symtab_obstack);
14259 make_cleanup_obstack_free (&symtab_obstack);
14260 write_hash_table (symtab, &symtab_obstack, &constant_pool);
14261
14262 obstack_init (&contents);
14263 make_cleanup_obstack_free (&contents);
1fd400ff 14264 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
14265 total_len = size_of_contents;
14266
14267 /* The version number. */
1fd400ff 14268 val = MAYBE_SWAP (2);
9291a0cd
TT
14269 obstack_grow (&contents, &val, sizeof (val));
14270
14271 /* The offset of the CU list from the start of the file. */
14272 val = MAYBE_SWAP (total_len);
14273 obstack_grow (&contents, &val, sizeof (val));
14274 total_len += obstack_object_size (&cu_list);
14275
1fd400ff
TT
14276 /* The offset of the types CU list from the start of the file. */
14277 val = MAYBE_SWAP (total_len);
14278 obstack_grow (&contents, &val, sizeof (val));
14279 total_len += obstack_object_size (&types_cu_list);
14280
9291a0cd
TT
14281 /* The offset of the address table from the start of the file. */
14282 val = MAYBE_SWAP (total_len);
14283 obstack_grow (&contents, &val, sizeof (val));
14284 total_len += obstack_object_size (&addr_obstack);
14285
14286 /* The offset of the symbol table from the start of the file. */
14287 val = MAYBE_SWAP (total_len);
14288 obstack_grow (&contents, &val, sizeof (val));
14289 total_len += obstack_object_size (&symtab_obstack);
14290
14291 /* The offset of the constant pool from the start of the file. */
14292 val = MAYBE_SWAP (total_len);
14293 obstack_grow (&contents, &val, sizeof (val));
14294 total_len += obstack_object_size (&constant_pool);
14295
14296 gdb_assert (obstack_object_size (&contents) == size_of_contents);
14297
14298 write_obstack (out_file, &contents);
14299 write_obstack (out_file, &cu_list);
1fd400ff 14300 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
14301 write_obstack (out_file, &addr_obstack);
14302 write_obstack (out_file, &symtab_obstack);
14303 write_obstack (out_file, &constant_pool);
14304
14305 fclose (out_file);
14306
14307 /* We want to keep the file, so we set cleanup_filename to NULL
14308 here. See unlink_if_set. */
14309 cleanup_filename = NULL;
14310
14311 do_cleanups (cleanup);
14312}
14313
14314/* The mapped index file format is designed to be directly mmap()able
14315 on any architecture. In most cases, a datum is represented using a
14316 little-endian 32-bit integer value, called an offset_type. Big
14317 endian machines must byte-swap the values before using them.
14318 Exceptions to this rule are noted. The data is laid out such that
14319 alignment is always respected.
14320
14321 A mapped index consists of several sections.
14322
14323 1. The file header. This is a sequence of values, of offset_type
14324 unless otherwise noted:
1fd400ff
TT
14325 [0] The version number. Currently 1 or 2. The differences are
14326 noted below. Version 1 did not account for .debug_types sections;
14327 the presence of a .debug_types section invalidates any version 1
14328 index that may exist.
9291a0cd 14329 [1] The offset, from the start of the file, of the CU list.
1fd400ff
TT
14330 [1.5] In version 2, the offset, from the start of the file, of the
14331 types CU list. This offset does not appear in version 1. Note
14332 that this can be empty, in which case this offset will be equal to
14333 the next offset.
9291a0cd
TT
14334 [2] The offset, from the start of the file, of the address section.
14335 [3] The offset, from the start of the file, of the symbol table.
14336 [4] The offset, from the start of the file, of the constant pool.
14337
14338 2. The CU list. This is a sequence of pairs of 64-bit
1fd400ff
TT
14339 little-endian values, sorted by the CU offset. The first element
14340 in each pair is the offset of a CU in the .debug_info section. The
14341 second element in each pair is the length of that CU. References
14342 to a CU elsewhere in the map are done using a CU index, which is
14343 just the 0-based index into this table. Note that if there are
14344 type CUs, then conceptually CUs and type CUs form a single list for
14345 the purposes of CU indices.
14346
14347 2.5 The types CU list. This does not appear in a version 1 index.
14348 This is a sequence of triplets of 64-bit little-endian values. In
14349 a triplet, the first value is the CU offset, the second value is
14350 the type offset in the CU, and the third value is the type
14351 signature. The types CU list is not sorted.
9291a0cd
TT
14352
14353 3. The address section. The address section consists of a sequence
14354 of address entries. Each address entry has three elements.
14355 [0] The low address. This is a 64-bit little-endian value.
14356 [1] The high address. This is a 64-bit little-endian value.
14357 [2] The CU index. This is an offset_type value.
14358
14359 4. The symbol table. This is a hash table. The size of the hash
14360 table is always a power of 2. The initial hash and the step are
14361 currently defined by the `find_slot' function.
14362
14363 Each slot in the hash table consists of a pair of offset_type
14364 values. The first value is the offset of the symbol's name in the
14365 constant pool. The second value is the offset of the CU vector in
14366 the constant pool.
14367
14368 If both values are 0, then this slot in the hash table is empty.
14369 This is ok because while 0 is a valid constant pool index, it
14370 cannot be a valid index for both a string and a CU vector.
14371
14372 A string in the constant pool is stored as a \0-terminated string,
14373 as you'd expect.
14374
14375 A CU vector in the constant pool is a sequence of offset_type
14376 values. The first value is the number of CU indices in the vector.
14377 Each subsequent value is the index of a CU in the CU list. This
14378 element in the hash table is used to indicate which CUs define the
14379 symbol.
14380
14381 5. The constant pool. This is simply a bunch of bytes. It is
14382 organized so that alignment is correct: CU vectors are stored
14383 first, followed by strings. */
14384static void
14385save_gdb_index_command (char *arg, int from_tty)
14386{
14387 struct objfile *objfile;
14388
14389 if (!arg || !*arg)
96d19272 14390 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
14391
14392 ALL_OBJFILES (objfile)
14393 {
14394 struct stat st;
14395
14396 /* If the objfile does not correspond to an actual file, skip it. */
14397 if (stat (objfile->name, &st) < 0)
14398 continue;
14399
14400 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14401 if (dwarf2_per_objfile)
14402 {
14403 volatile struct gdb_exception except;
14404
14405 TRY_CATCH (except, RETURN_MASK_ERROR)
14406 {
14407 write_psymtabs_to_index (objfile, arg);
14408 }
14409 if (except.reason < 0)
14410 exception_fprintf (gdb_stderr, except,
14411 _("Error while writing index for `%s': "),
14412 objfile->name);
14413 }
14414 }
dce234bc
PP
14415}
14416
9291a0cd
TT
14417\f
14418
9eae7c52
TT
14419int dwarf2_always_disassemble;
14420
14421static void
14422show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
14423 struct cmd_list_element *c, const char *value)
14424{
14425 fprintf_filtered (file, _("\
14426Whether to always disassemble DWARF expressions is %s.\n"),
14427 value);
14428}
14429
6502dd73
DJ
14430void _initialize_dwarf2_read (void);
14431
14432void
14433_initialize_dwarf2_read (void)
14434{
96d19272
JK
14435 struct cmd_list_element *c;
14436
dce234bc 14437 dwarf2_objfile_data_key
c1bd65d0 14438 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 14439
1bedd215
AC
14440 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
14441Set DWARF 2 specific variables.\n\
14442Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
14443 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
14444 0/*allow-unknown*/, &maintenance_set_cmdlist);
14445
1bedd215
AC
14446 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
14447Show DWARF 2 specific variables\n\
14448Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
14449 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
14450 0/*allow-unknown*/, &maintenance_show_cmdlist);
14451
14452 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
14453 &dwarf2_max_cache_age, _("\
14454Set the upper bound on the age of cached dwarf2 compilation units."), _("\
14455Show the upper bound on the age of cached dwarf2 compilation units."), _("\
14456A higher limit means that cached compilation units will be stored\n\
14457in memory longer, and more total memory will be used. Zero disables\n\
14458caching, which can slow down startup."),
2c5b56ce 14459 NULL,
920d2a44 14460 show_dwarf2_max_cache_age,
2c5b56ce 14461 &set_dwarf2_cmdlist,
ae038cb0 14462 &show_dwarf2_cmdlist);
d97bc12b 14463
9eae7c52
TT
14464 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
14465 &dwarf2_always_disassemble, _("\
14466Set whether `info address' always disassembles DWARF expressions."), _("\
14467Show whether `info address' always disassembles DWARF expressions."), _("\
14468When enabled, DWARF expressions are always printed in an assembly-like\n\
14469syntax. When disabled, expressions will be printed in a more\n\
14470conversational style, when possible."),
14471 NULL,
14472 show_dwarf2_always_disassemble,
14473 &set_dwarf2_cmdlist,
14474 &show_dwarf2_cmdlist);
14475
d97bc12b
DE
14476 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
14477Set debugging of the dwarf2 DIE reader."), _("\
14478Show debugging of the dwarf2 DIE reader."), _("\
14479When enabled (non-zero), DIEs are dumped after they are read in.\n\
14480The value is the maximum depth to print."),
14481 NULL,
14482 NULL,
14483 &setdebuglist, &showdebuglist);
9291a0cd 14484
96d19272
JK
14485 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
14486 _("Save a .gdb-index file"),
14487 &save_cmdlist);
14488 set_cmd_completer (c, filename_completer);
6502dd73 14489}
This page took 2.053038 seconds and 4 git commands to generate.