* ada-valprint.c (ada_print_scalar): Accept NULL type argument
[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,
0fb0cc75
JB
4 2004, 2005, 2006, 2007, 2008, 2009
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
SS
33#include "objfiles.h"
34#include "elf/dwarf2.h"
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"
4c2df51b 51
c906108c
SS
52#include <fcntl.h>
53#include "gdb_string.h"
4bdf3d34 54#include "gdb_assert.h"
c906108c 55#include <sys/types.h>
233a11ab
CS
56#ifdef HAVE_ZLIB_H
57#include <zlib.h>
58#endif
dce234bc
PP
59#ifdef HAVE_MMAP
60#include <sys/mman.h>
61#endif
d8151005 62
107d2387 63#if 0
357e46e7 64/* .debug_info header for a compilation unit
c906108c
SS
65 Because of alignment constraints, this structure has padding and cannot
66 be mapped directly onto the beginning of the .debug_info section. */
67typedef struct comp_unit_header
68 {
69 unsigned int length; /* length of the .debug_info
70 contribution */
71 unsigned short version; /* version number -- 2 for DWARF
72 version 2 */
73 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
74 unsigned char addr_size; /* byte size of an address -- 4 */
75 }
76_COMP_UNIT_HEADER;
77#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 78#endif
c906108c
SS
79
80/* .debug_pubnames header
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83typedef struct pubnames_header
84 {
85 unsigned int length; /* length of the .debug_pubnames
86 contribution */
87 unsigned char version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int info_offset; /* offset into .debug_info section */
90 unsigned int info_size; /* byte size of .debug_info section
91 portion */
92 }
93_PUBNAMES_HEADER;
94#define _ACTUAL_PUBNAMES_HEADER_SIZE 13
95
96/* .debug_pubnames header
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99typedef struct aranges_header
100 {
101 unsigned int length; /* byte len of the .debug_aranges
102 contribution */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int info_offset; /* offset into .debug_info section */
106 unsigned char addr_size; /* byte size of an address */
107 unsigned char seg_size; /* byte size of segment descriptor */
108 }
109_ARANGES_HEADER;
110#define _ACTUAL_ARANGES_HEADER_SIZE 12
111
112/* .debug_line statement program prologue
113 Because of alignment constraints, this structure has padding and cannot
114 be mapped directly onto the beginning of the .debug_info section. */
115typedef struct statement_prologue
116 {
117 unsigned int total_length; /* byte length of the statement
118 information */
119 unsigned short version; /* version number -- 2 for DWARF
120 version 2 */
121 unsigned int prologue_length; /* # bytes between prologue &
122 stmt program */
123 unsigned char minimum_instruction_length; /* byte size of
124 smallest instr */
125 unsigned char default_is_stmt; /* initial value of is_stmt
126 register */
127 char line_base;
128 unsigned char line_range;
129 unsigned char opcode_base; /* number assigned to first special
130 opcode */
131 unsigned char *standard_opcode_lengths;
132 }
133_STATEMENT_PROLOGUE;
134
d97bc12b
DE
135/* When non-zero, dump DIEs after they are read in. */
136static int dwarf2_die_debug = 0;
137
dce234bc
PP
138static int pagesize;
139
df8a16a1
DJ
140/* When set, the file that we're processing is known to have debugging
141 info for C++ namespaces. GCC 3.3.x did not produce this information,
142 but later versions do. */
143
144static int processing_has_namespace_info;
145
6502dd73
DJ
146static const struct objfile_data *dwarf2_objfile_data_key;
147
dce234bc
PP
148struct dwarf2_section_info
149{
150 asection *asection;
151 gdb_byte *buffer;
152 bfd_size_type size;
153 int was_mmapped;
154};
155
6502dd73
DJ
156struct dwarf2_per_objfile
157{
dce234bc
PP
158 struct dwarf2_section_info info;
159 struct dwarf2_section_info abbrev;
160 struct dwarf2_section_info line;
161 struct dwarf2_section_info pubnames;
162 struct dwarf2_section_info aranges;
163 struct dwarf2_section_info loc;
164 struct dwarf2_section_info macinfo;
165 struct dwarf2_section_info str;
166 struct dwarf2_section_info ranges;
167 struct dwarf2_section_info frame;
168 struct dwarf2_section_info eh_frame;
ae038cb0 169
10b3939b
DJ
170 /* A list of all the compilation units. This is used to locate
171 the target compilation unit of a particular reference. */
ae038cb0
DJ
172 struct dwarf2_per_cu_data **all_comp_units;
173
174 /* The number of compilation units in ALL_COMP_UNITS. */
175 int n_comp_units;
176
177 /* A chain of compilation units that are currently read in, so that
178 they can be freed later. */
179 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5
FR
180
181 /* A flag indicating wether this objfile has a section loaded at a
182 VMA of 0. */
183 int has_section_at_zero;
6502dd73
DJ
184};
185
186static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c
SS
187
188/* names of the debugging sections */
189
233a11ab
CS
190/* Note that if the debugging section has been compressed, it might
191 have a name like .zdebug_info. */
192
193#define INFO_SECTION "debug_info"
194#define ABBREV_SECTION "debug_abbrev"
195#define LINE_SECTION "debug_line"
196#define PUBNAMES_SECTION "debug_pubnames"
197#define ARANGES_SECTION "debug_aranges"
198#define LOC_SECTION "debug_loc"
199#define MACINFO_SECTION "debug_macinfo"
200#define STR_SECTION "debug_str"
201#define RANGES_SECTION "debug_ranges"
202#define FRAME_SECTION "debug_frame"
203#define EH_FRAME_SECTION "eh_frame"
c906108c
SS
204
205/* local data types */
206
57349743
JB
207/* We hold several abbreviation tables in memory at the same time. */
208#ifndef ABBREV_HASH_SIZE
209#define ABBREV_HASH_SIZE 121
210#endif
211
107d2387
AC
212/* The data in a compilation unit header, after target2host
213 translation, looks like this. */
c906108c 214struct comp_unit_head
a738430d 215{
c764a876 216 unsigned int length;
a738430d 217 short version;
a738430d
MK
218 unsigned char addr_size;
219 unsigned char signed_addr_p;
9cbfa09e 220 unsigned int abbrev_offset;
57349743 221
a738430d
MK
222 /* Size of file offsets; either 4 or 8. */
223 unsigned int offset_size;
57349743 224
a738430d
MK
225 /* Size of the length field; either 4 or 12. */
226 unsigned int initial_length_size;
57349743 227
a738430d
MK
228 /* Offset to the first byte of this compilation unit header in the
229 .debug_info section, for resolving relative reference dies. */
230 unsigned int offset;
57349743 231
d00adf39
DE
232 /* Offset to first die in this cu from the start of the cu.
233 This will be the first byte following the compilation unit header. */
234 unsigned int first_die_offset;
a738430d 235};
c906108c 236
e7c27a73
DJ
237/* Internal state when decoding a particular compilation unit. */
238struct dwarf2_cu
239{
240 /* The objfile containing this compilation unit. */
241 struct objfile *objfile;
242
d00adf39 243 /* The header of the compilation unit. */
e7c27a73 244 struct comp_unit_head header;
e142c38c 245
d00adf39
DE
246 /* Base address of this compilation unit. */
247 CORE_ADDR base_address;
248
249 /* Non-zero if base_address has been set. */
250 int base_known;
251
e142c38c
DJ
252 struct function_range *first_fn, *last_fn, *cached_fn;
253
254 /* The language we are debugging. */
255 enum language language;
256 const struct language_defn *language_defn;
257
b0f35d58
DL
258 const char *producer;
259
e142c38c
DJ
260 /* The generic symbol table building routines have separate lists for
261 file scope symbols and all all other scopes (local scopes). So
262 we need to select the right one to pass to add_symbol_to_list().
263 We do it by keeping a pointer to the correct list in list_in_scope.
264
265 FIXME: The original dwarf code just treated the file scope as the
266 first local scope, and all other local scopes as nested local
267 scopes, and worked fine. Check to see if we really need to
268 distinguish these in buildsym.c. */
269 struct pending **list_in_scope;
270
f3dd6933
DJ
271 /* DWARF abbreviation table associated with this compilation unit. */
272 struct abbrev_info **dwarf2_abbrevs;
273
274 /* Storage for the abbrev table. */
275 struct obstack abbrev_obstack;
72bf9492
DJ
276
277 /* Hash table holding all the loaded partial DIEs. */
278 htab_t partial_dies;
279
ff013f42 280 /* `.debug_ranges' offset for this `DW_TAG_compile_unit' DIE. */
c764a876 281 unsigned int ranges_offset;
ff013f42 282
72bf9492
DJ
283 /* Storage for things with the same lifetime as this read-in compilation
284 unit, including partial DIEs. */
285 struct obstack comp_unit_obstack;
286
ae038cb0
DJ
287 /* When multiple dwarf2_cu structures are living in memory, this field
288 chains them all together, so that they can be released efficiently.
289 We will probably also want a generation counter so that most-recently-used
290 compilation units are cached... */
291 struct dwarf2_per_cu_data *read_in_chain;
292
293 /* Backchain to our per_cu entry if the tree has been built. */
294 struct dwarf2_per_cu_data *per_cu;
295
f792889a
DJ
296 /* Pointer to the die -> type map. Although it is stored
297 permanently in per_cu, we copy it here to avoid double
298 indirection. */
299 htab_t type_hash;
300
ae038cb0
DJ
301 /* How many compilation units ago was this CU last referenced? */
302 int last_used;
303
10b3939b 304 /* A hash table of die offsets for following references. */
51545339 305 htab_t die_hash;
10b3939b
DJ
306
307 /* Full DIEs if read in. */
308 struct die_info *dies;
309
310 /* A set of pointers to dwarf2_per_cu_data objects for compilation
311 units referenced by this one. Only set during full symbol processing;
312 partial symbol tables do not have dependencies. */
313 htab_t dependencies;
314
cb1df416
DJ
315 /* Header data from the line table, during full symbol processing. */
316 struct line_header *line_header;
317
ae038cb0
DJ
318 /* Mark used when releasing cached dies. */
319 unsigned int mark : 1;
320
321 /* This flag will be set if this compilation unit might include
322 inter-compilation-unit references. */
323 unsigned int has_form_ref_addr : 1;
324
72bf9492
DJ
325 /* This flag will be set if this compilation unit includes any
326 DW_TAG_namespace DIEs. If we know that there are explicit
327 DIEs for namespaces, we don't need to try to infer them
328 from mangled names. */
329 unsigned int has_namespace_info : 1;
ff013f42
JK
330
331 /* Field `ranges_offset' is filled in; flag as the value may be zero. */
332 unsigned int has_ranges_offset : 1;
e7c27a73
DJ
333};
334
10b3939b
DJ
335/* Persistent data held for a compilation unit, even when not
336 processing it. We put a pointer to this structure in the
337 read_symtab_private field of the psymtab. If we encounter
338 inter-compilation-unit references, we also maintain a sorted
339 list of all compilation units. */
340
ae038cb0
DJ
341struct dwarf2_per_cu_data
342{
5afb4e99 343 /* The start offset and length of this compilation unit. 2**30-1
ae038cb0 344 bytes should suffice to store the length of any compilation unit
45452591
DE
345 - if it doesn't, GDB will fall over anyway.
346 NOTE: Unlike comp_unit_head.length, this length includes
347 initial_length_size. */
c764a876
DE
348 unsigned int offset;
349 unsigned int length : 30;
ae038cb0
DJ
350
351 /* Flag indicating this compilation unit will be read in before
352 any of the current compilation units are processed. */
c764a876 353 unsigned int queued : 1;
ae038cb0 354
5afb4e99
DJ
355 /* This flag will be set if we need to load absolutely all DIEs
356 for this compilation unit, instead of just the ones we think
357 are interesting. It gets set if we look for a DIE in the
358 hash table and don't find it. */
359 unsigned int load_all_dies : 1;
360
ae038cb0
DJ
361 /* Set iff currently read in. */
362 struct dwarf2_cu *cu;
1c379e20
DJ
363
364 /* If full symbols for this CU have been read in, then this field
365 holds a map of DIE offsets to types. It isn't always possible
366 to reconstruct this information later, so we have to preserve
367 it. */
1c379e20 368 htab_t type_hash;
10b3939b 369
31ffec48
DJ
370 /* The partial symbol table associated with this compilation unit,
371 or NULL for partial units (which do not have an associated
372 symtab). */
10b3939b 373 struct partial_symtab *psymtab;
ae038cb0
DJ
374};
375
debd256d
JB
376/* The line number information for a compilation unit (found in the
377 .debug_line section) begins with a "statement program header",
378 which contains the following information. */
379struct line_header
380{
381 unsigned int total_length;
382 unsigned short version;
383 unsigned int header_length;
384 unsigned char minimum_instruction_length;
385 unsigned char default_is_stmt;
386 int line_base;
387 unsigned char line_range;
388 unsigned char opcode_base;
389
390 /* standard_opcode_lengths[i] is the number of operands for the
391 standard opcode whose value is i. This means that
392 standard_opcode_lengths[0] is unused, and the last meaningful
393 element is standard_opcode_lengths[opcode_base - 1]. */
394 unsigned char *standard_opcode_lengths;
395
396 /* The include_directories table. NOTE! These strings are not
397 allocated with xmalloc; instead, they are pointers into
398 debug_line_buffer. If you try to free them, `free' will get
399 indigestion. */
400 unsigned int num_include_dirs, include_dirs_size;
401 char **include_dirs;
402
403 /* The file_names table. NOTE! These strings are not allocated
404 with xmalloc; instead, they are pointers into debug_line_buffer.
405 Don't try to free them directly. */
406 unsigned int num_file_names, file_names_size;
407 struct file_entry
c906108c 408 {
debd256d
JB
409 char *name;
410 unsigned int dir_index;
411 unsigned int mod_time;
412 unsigned int length;
aaa75496 413 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 414 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
415 } *file_names;
416
417 /* The start and end of the statement program following this
6502dd73 418 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 419 gdb_byte *statement_program_start, *statement_program_end;
debd256d 420};
c906108c
SS
421
422/* When we construct a partial symbol table entry we only
423 need this much information. */
424struct partial_die_info
425 {
72bf9492 426 /* Offset of this DIE. */
c906108c 427 unsigned int offset;
72bf9492
DJ
428
429 /* DWARF-2 tag for this DIE. */
430 ENUM_BITFIELD(dwarf_tag) tag : 16;
431
432 /* Language code associated with this DIE. This is only used
433 for the compilation unit DIE. */
434 unsigned int language : 8;
435
436 /* Assorted flags describing the data found in this DIE. */
437 unsigned int has_children : 1;
438 unsigned int is_external : 1;
439 unsigned int is_declaration : 1;
440 unsigned int has_type : 1;
441 unsigned int has_specification : 1;
aaa75496 442 unsigned int has_stmt_list : 1;
72bf9492
DJ
443 unsigned int has_pc_info : 1;
444
445 /* Flag set if the SCOPE field of this structure has been
446 computed. */
447 unsigned int scope_set : 1;
448
fa4028e9
JB
449 /* Flag set if the DIE has a byte_size attribute. */
450 unsigned int has_byte_size : 1;
451
72bf9492
DJ
452 /* The name of this DIE. Normally the value of DW_AT_name, but
453 sometimes DW_TAG_MIPS_linkage_name or a string computed in some
454 other fashion. */
c906108c 455 char *name;
57c22c6c 456 char *dirname;
72bf9492
DJ
457
458 /* The scope to prepend to our children. This is generally
459 allocated on the comp_unit_obstack, so will disappear
460 when this compilation unit leaves the cache. */
461 char *scope;
462
463 /* The location description associated with this DIE, if any. */
464 struct dwarf_block *locdesc;
465
466 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
467 CORE_ADDR lowpc;
468 CORE_ADDR highpc;
72bf9492
DJ
469
470 /* Pointer into the info_buffer pointing at the target of
471 DW_AT_sibling, if any. */
fe1b8b76 472 gdb_byte *sibling;
72bf9492
DJ
473
474 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
475 DW_AT_specification (or DW_AT_abstract_origin or
476 DW_AT_extension). */
477 unsigned int spec_offset;
478
aaa75496
JB
479 /* If HAS_STMT_LIST, the offset of the Line Number Information data. */
480 unsigned int line_offset;
481
72bf9492
DJ
482 /* Pointers to this DIE's parent, first child, and next sibling,
483 if any. */
484 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
485 };
486
487/* This data structure holds the information of an abbrev. */
488struct abbrev_info
489 {
490 unsigned int number; /* number identifying abbrev */
491 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
492 unsigned short has_children; /* boolean */
493 unsigned short num_attrs; /* number of attributes */
c906108c
SS
494 struct attr_abbrev *attrs; /* an array of attribute descriptions */
495 struct abbrev_info *next; /* next in chain */
496 };
497
498struct attr_abbrev
499 {
9d25dd43
DE
500 ENUM_BITFIELD(dwarf_attribute) name : 16;
501 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
502 };
503
71c25dea
TT
504/* Additional GDB-specific attribute forms. */
505enum
506 {
507 /* A string which has been updated to GDB's internal
508 representation (e.g. converted to canonical form) and does not
509 need to be updated again. */
510 GDB_FORM_cached_string = 0xff
511 };
512
b60c80d6
DJ
513/* Attributes have a name and a value */
514struct attribute
515 {
9d25dd43
DE
516 ENUM_BITFIELD(dwarf_attribute) name : 16;
517 ENUM_BITFIELD(dwarf_form) form : 16;
b60c80d6
DJ
518 union
519 {
520 char *str;
521 struct dwarf_block *blk;
522 unsigned long unsnd;
523 long int snd;
524 CORE_ADDR addr;
525 }
526 u;
527 };
528
c906108c
SS
529/* This data structure holds a complete die structure. */
530struct die_info
531 {
76815b17
DE
532 /* DWARF-2 tag for this DIE. */
533 ENUM_BITFIELD(dwarf_tag) tag : 16;
534
535 /* Number of attributes */
536 unsigned short num_attrs;
537
538 /* Abbrev number */
539 unsigned int abbrev;
540
541 /* Offset in .debug_info section */
542 unsigned int offset;
78ba4af6
JB
543
544 /* The dies in a compilation unit form an n-ary tree. PARENT
545 points to this die's parent; CHILD points to the first child of
546 this node; and all the children of a given node are chained
547 together via their SIBLING fields, terminated by a die whose
548 tag is zero. */
639d11d3
DC
549 struct die_info *child; /* Its first child, if any. */
550 struct die_info *sibling; /* Its next sibling, if any. */
551 struct die_info *parent; /* Its parent, if any. */
c906108c 552
b60c80d6
DJ
553 /* An array of attributes, with NUM_ATTRS elements. There may be
554 zero, but it's not common and zero-sized arrays are not
555 sufficiently portable C. */
556 struct attribute attrs[1];
c906108c
SS
557 };
558
5fb290d7
DJ
559struct function_range
560{
561 const char *name;
562 CORE_ADDR lowpc, highpc;
563 int seen_line;
564 struct function_range *next;
565};
566
c906108c
SS
567/* Get at parts of an attribute structure */
568
569#define DW_STRING(attr) ((attr)->u.str)
570#define DW_UNSND(attr) ((attr)->u.unsnd)
571#define DW_BLOCK(attr) ((attr)->u.blk)
572#define DW_SND(attr) ((attr)->u.snd)
573#define DW_ADDR(attr) ((attr)->u.addr)
574
575/* Blocks are a bunch of untyped bytes. */
576struct dwarf_block
577 {
578 unsigned int size;
fe1b8b76 579 gdb_byte *data;
c906108c
SS
580 };
581
c906108c
SS
582#ifndef ATTR_ALLOC_CHUNK
583#define ATTR_ALLOC_CHUNK 4
584#endif
585
c906108c
SS
586/* Allocate fields for structs, unions and enums in this size. */
587#ifndef DW_FIELD_ALLOC_CHUNK
588#define DW_FIELD_ALLOC_CHUNK 4
589#endif
590
c906108c
SS
591/* A zeroed version of a partial die for initialization purposes. */
592static struct partial_die_info zeroed_partial_die;
593
c906108c
SS
594/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
595 but this would require a corresponding change in unpack_field_as_long
596 and friends. */
597static int bits_per_byte = 8;
598
599/* The routines that read and process dies for a C struct or C++ class
600 pass lists of data member fields and lists of member function fields
601 in an instance of a field_info structure, as defined below. */
602struct field_info
c5aa993b
JM
603 {
604 /* List of data member and baseclasses fields. */
605 struct nextfield
606 {
607 struct nextfield *next;
608 int accessibility;
609 int virtuality;
610 struct field field;
611 }
612 *fields;
c906108c 613
c5aa993b
JM
614 /* Number of fields. */
615 int nfields;
c906108c 616
c5aa993b
JM
617 /* Number of baseclasses. */
618 int nbaseclasses;
c906108c 619
c5aa993b
JM
620 /* Set if the accesibility of one of the fields is not public. */
621 int non_public_fields;
c906108c 622
c5aa993b
JM
623 /* Member function fields array, entries are allocated in the order they
624 are encountered in the object file. */
625 struct nextfnfield
626 {
627 struct nextfnfield *next;
628 struct fn_field fnfield;
629 }
630 *fnfields;
c906108c 631
c5aa993b
JM
632 /* Member function fieldlist array, contains name of possibly overloaded
633 member function, number of overloaded member functions and a pointer
634 to the head of the member function field chain. */
635 struct fnfieldlist
636 {
637 char *name;
638 int length;
639 struct nextfnfield *head;
640 }
641 *fnfieldlists;
c906108c 642
c5aa993b
JM
643 /* Number of entries in the fnfieldlists array. */
644 int nfnfields;
645 };
c906108c 646
10b3939b
DJ
647/* One item on the queue of compilation units to read in full symbols
648 for. */
649struct dwarf2_queue_item
650{
651 struct dwarf2_per_cu_data *per_cu;
652 struct dwarf2_queue_item *next;
653};
654
655/* The current queue. */
656static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
657
ae038cb0
DJ
658/* Loaded secondary compilation units are kept in memory until they
659 have not been referenced for the processing of this many
660 compilation units. Set this to zero to disable caching. Cache
661 sizes of up to at least twenty will improve startup time for
662 typical inter-CU-reference binaries, at an obvious memory cost. */
663static int dwarf2_max_cache_age = 5;
920d2a44
AC
664static void
665show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
666 struct cmd_list_element *c, const char *value)
667{
668 fprintf_filtered (file, _("\
669The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
670 value);
671}
672
ae038cb0 673
c906108c
SS
674/* Various complaints about symbol reading that don't abort the process */
675
4d3c2250
KB
676static void
677dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 678{
4d3c2250 679 complaint (&symfile_complaints,
e2e0b3e5 680 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
681}
682
25e43795
DJ
683static void
684dwarf2_debug_line_missing_file_complaint (void)
685{
686 complaint (&symfile_complaints,
687 _(".debug_line section has line data without a file"));
688}
689
59205f5a
JB
690static void
691dwarf2_debug_line_missing_end_sequence_complaint (void)
692{
693 complaint (&symfile_complaints,
694 _(".debug_line section has line program sequence without an end"));
695}
696
4d3c2250
KB
697static void
698dwarf2_complex_location_expr_complaint (void)
2e276125 699{
e2e0b3e5 700 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
701}
702
4d3c2250
KB
703static void
704dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
705 int arg3)
2e276125 706{
4d3c2250 707 complaint (&symfile_complaints,
e2e0b3e5 708 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
4d3c2250
KB
709 arg2, arg3);
710}
711
712static void
713dwarf2_macros_too_long_complaint (void)
2e276125 714{
4d3c2250 715 complaint (&symfile_complaints,
e2e0b3e5 716 _("macro info runs off end of `.debug_macinfo' section"));
4d3c2250
KB
717}
718
719static void
720dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 721{
4d3c2250 722 complaint (&symfile_complaints,
e2e0b3e5 723 _("macro debug info contains a malformed macro definition:\n`%s'"),
4d3c2250
KB
724 arg1);
725}
726
727static void
728dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 729{
4d3c2250 730 complaint (&symfile_complaints,
e2e0b3e5 731 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
4d3c2250 732}
c906108c 733
c906108c
SS
734/* local function prototypes */
735
4efb68b1 736static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c
SS
737
738#if 0
a14ed312 739static void dwarf2_build_psymtabs_easy (struct objfile *, int);
c906108c
SS
740#endif
741
aaa75496
JB
742static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
743 struct objfile *);
744
745static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
746 struct partial_die_info *,
747 struct partial_symtab *);
748
a14ed312 749static void dwarf2_build_psymtabs_hard (struct objfile *, int);
c906108c 750
72bf9492
DJ
751static void scan_partial_symbols (struct partial_die_info *,
752 CORE_ADDR *, CORE_ADDR *,
5734ee8b 753 int, struct dwarf2_cu *);
c906108c 754
72bf9492
DJ
755static void add_partial_symbol (struct partial_die_info *,
756 struct dwarf2_cu *);
63d06c5c 757
72bf9492 758static int pdi_needs_namespace (enum dwarf_tag tag);
91c24f0a 759
72bf9492
DJ
760static void add_partial_namespace (struct partial_die_info *pdi,
761 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 762 int need_pc, struct dwarf2_cu *cu);
63d06c5c 763
5d7cb8df
JK
764static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
765 CORE_ADDR *highpc, int need_pc,
766 struct dwarf2_cu *cu);
767
72bf9492
DJ
768static void add_partial_enumeration (struct partial_die_info *enum_pdi,
769 struct dwarf2_cu *cu);
91c24f0a 770
bc30ff58
JB
771static void add_partial_subprogram (struct partial_die_info *pdi,
772 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 773 int need_pc, struct dwarf2_cu *cu);
bc30ff58 774
fe1b8b76
JB
775static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
776 gdb_byte *info_ptr,
777 bfd *abfd,
778 struct dwarf2_cu *cu);
91c24f0a 779
a14ed312 780static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 781
a14ed312 782static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 783
e7c27a73 784static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 785
f3dd6933 786static void dwarf2_free_abbrev_table (void *);
c906108c 787
fe1b8b76 788static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 789 struct dwarf2_cu *);
72bf9492 790
57349743 791static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 792 struct dwarf2_cu *);
c906108c 793
fe1b8b76 794static struct partial_die_info *load_partial_dies (bfd *, gdb_byte *, int,
72bf9492
DJ
795 struct dwarf2_cu *);
796
fe1b8b76
JB
797static gdb_byte *read_partial_die (struct partial_die_info *,
798 struct abbrev_info *abbrev, unsigned int,
799 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 800
c764a876 801static struct partial_die_info *find_partial_die (unsigned int,
10b3939b 802 struct dwarf2_cu *);
72bf9492
DJ
803
804static void fixup_partial_die (struct partial_die_info *,
805 struct dwarf2_cu *);
806
fe1b8b76
JB
807static gdb_byte *read_full_die (struct die_info **, bfd *, gdb_byte *,
808 struct dwarf2_cu *, int *);
c906108c 809
fe1b8b76
JB
810static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
811 bfd *, gdb_byte *, struct dwarf2_cu *);
c906108c 812
fe1b8b76
JB
813static gdb_byte *read_attribute_value (struct attribute *, unsigned,
814 bfd *, gdb_byte *, struct dwarf2_cu *);
a8329558 815
fe1b8b76 816static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 817
fe1b8b76 818static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 819
fe1b8b76 820static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 821
fe1b8b76 822static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 823
fe1b8b76 824static unsigned long read_8_bytes (bfd *, gdb_byte *);
c906108c 825
fe1b8b76 826static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 827 unsigned int *);
c906108c 828
c764a876
DE
829static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
830
831static LONGEST read_checked_initial_length_and_offset
832 (bfd *, gdb_byte *, const struct comp_unit_head *,
833 unsigned int *, unsigned int *);
613e1657 834
fe1b8b76 835static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
836 unsigned int *);
837
838static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 839
fe1b8b76 840static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 841
fe1b8b76 842static char *read_string (bfd *, gdb_byte *, unsigned int *);
c906108c 843
fe1b8b76
JB
844static char *read_indirect_string (bfd *, gdb_byte *,
845 const struct comp_unit_head *,
846 unsigned int *);
4bdf3d34 847
fe1b8b76 848static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 849
fe1b8b76 850static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 851
fe1b8b76 852static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 853
e142c38c 854static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 855
e142c38c
DJ
856static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
857 struct dwarf2_cu *);
c906108c 858
05cf31d1
JB
859static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
860 struct dwarf2_cu *cu);
861
e142c38c 862static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 863
e142c38c 864static struct die_info *die_specification (struct die_info *die,
f2f0e013 865 struct dwarf2_cu **);
63d06c5c 866
debd256d
JB
867static void free_line_header (struct line_header *lh);
868
aaa75496
JB
869static void add_file_name (struct line_header *, char *, unsigned int,
870 unsigned int, unsigned int);
871
debd256d
JB
872static struct line_header *(dwarf_decode_line_header
873 (unsigned int offset,
e7c27a73 874 bfd *abfd, struct dwarf2_cu *cu));
debd256d
JB
875
876static void dwarf_decode_lines (struct line_header *, char *, bfd *,
aaa75496 877 struct dwarf2_cu *, struct partial_symtab *);
c906108c 878
4f1520fb 879static void dwarf2_start_subfile (char *, char *, char *);
c906108c 880
a14ed312 881static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 882 struct dwarf2_cu *);
c906108c 883
a14ed312 884static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 885 struct dwarf2_cu *);
c906108c 886
2df3850c
JM
887static void dwarf2_const_value_data (struct attribute *attr,
888 struct symbol *sym,
889 int bits);
890
e7c27a73 891static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 892
e7c27a73
DJ
893static struct type *die_containing_type (struct die_info *,
894 struct dwarf2_cu *);
c906108c 895
e7c27a73 896static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
c906108c 897
f792889a 898static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 899
086ed43d 900static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 901
fe1b8b76
JB
902static char *typename_concat (struct obstack *,
903 const char *prefix,
904 const char *suffix,
987504bb 905 struct dwarf2_cu *);
63d06c5c 906
e7c27a73 907static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 908
e7c27a73 909static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 910
e7c27a73 911static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 912
ff013f42
JK
913static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
914 struct dwarf2_cu *, struct partial_symtab *);
915
a14ed312 916static int dwarf2_get_pc_bounds (struct die_info *,
e7c27a73 917 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *);
c906108c 918
fae299cd
DC
919static void get_scope_pc_bounds (struct die_info *,
920 CORE_ADDR *, CORE_ADDR *,
921 struct dwarf2_cu *);
922
801e3a5b
JB
923static void dwarf2_record_block_ranges (struct die_info *, struct block *,
924 CORE_ADDR, struct dwarf2_cu *);
925
a14ed312 926static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 927 struct dwarf2_cu *);
c906108c 928
a14ed312 929static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 930 struct type *, struct dwarf2_cu *);
c906108c 931
a14ed312 932static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 933 struct die_info *, struct type *,
e7c27a73 934 struct dwarf2_cu *);
c906108c 935
a14ed312 936static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 937 struct type *, struct dwarf2_cu *);
c906108c 938
134d01f1 939static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 940
0114d602
DJ
941static const char *determine_class_name (struct die_info *die,
942 struct dwarf2_cu *cu);
8176b9b8 943
e7c27a73 944static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 945
e7c27a73 946static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 947
5d7cb8df
JK
948static void read_module (struct die_info *die, struct dwarf2_cu *cu);
949
27aa8d6a
SW
950static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
951
38d518c9 952static const char *namespace_name (struct die_info *die,
e142c38c 953 int *is_anonymous, struct dwarf2_cu *);
38d518c9 954
134d01f1 955static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 956
e7c27a73 957static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 958
7ca2d3a3
DL
959static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
960 struct dwarf2_cu *);
961
fe1b8b76 962static struct die_info *read_comp_unit (gdb_byte *, bfd *, struct dwarf2_cu *);
c906108c 963
d97bc12b
DE
964static struct die_info *read_die_and_children_1 (gdb_byte *info_ptr, bfd *abfd,
965 struct dwarf2_cu *,
966 gdb_byte **new_info_ptr,
967 struct die_info *parent);
968
fe1b8b76 969static struct die_info *read_die_and_children (gdb_byte *info_ptr, bfd *abfd,
e7c27a73 970 struct dwarf2_cu *,
fe1b8b76 971 gdb_byte **new_info_ptr,
639d11d3
DC
972 struct die_info *parent);
973
fe1b8b76 974static struct die_info *read_die_and_siblings (gdb_byte *info_ptr, bfd *abfd,
e7c27a73 975 struct dwarf2_cu *,
fe1b8b76 976 gdb_byte **new_info_ptr,
639d11d3
DC
977 struct die_info *parent);
978
e7c27a73 979static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 980
e142c38c 981static char *dwarf2_linkage_name (struct die_info *, struct dwarf2_cu *);
c906108c 982
71c25dea
TT
983static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
984 struct obstack *);
985
e142c38c 986static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 987
e142c38c 988static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 989 struct dwarf2_cu **);
9219021c 990
a14ed312 991static char *dwarf_tag_name (unsigned int);
c906108c 992
a14ed312 993static char *dwarf_attr_name (unsigned int);
c906108c 994
a14ed312 995static char *dwarf_form_name (unsigned int);
c906108c 996
a14ed312 997static char *dwarf_stack_op_name (unsigned int);
c906108c 998
a14ed312 999static char *dwarf_bool_name (unsigned int);
c906108c 1000
a14ed312 1001static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
1002
1003#if 0
a14ed312 1004static char *dwarf_cfi_name (unsigned int);
c906108c
SS
1005#endif
1006
f9aca02d 1007static struct die_info *sibling_die (struct die_info *);
c906108c 1008
d97bc12b
DE
1009static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1010
1011static void dump_die_for_error (struct die_info *);
1012
1013static void dump_die_1 (struct ui_file *, int level, int max_level,
1014 struct die_info *);
c906108c 1015
d97bc12b 1016/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1017
51545339 1018static void store_in_ref_table (struct die_info *,
10b3939b 1019 struct dwarf2_cu *);
c906108c 1020
c764a876 1021static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1022
a02abb62
JB
1023static int dwarf2_get_attr_constant_value (struct attribute *, int);
1024
10b3939b
DJ
1025static struct die_info *follow_die_ref (struct die_info *,
1026 struct attribute *,
f2f0e013 1027 struct dwarf2_cu **);
c906108c 1028
c906108c
SS
1029/* memory allocation interface */
1030
7b5a2f43 1031static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1032
f3dd6933 1033static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1034
b60c80d6 1035static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1036
e142c38c 1037static void initialize_cu_func_list (struct dwarf2_cu *);
5fb290d7 1038
e142c38c
DJ
1039static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1040 struct dwarf2_cu *);
5fb290d7 1041
2e276125 1042static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 1043 char *, bfd *, struct dwarf2_cu *);
2e276125 1044
8e19ed76
PS
1045static int attr_form_is_block (struct attribute *);
1046
3690dd37
JB
1047static int attr_form_is_section_offset (struct attribute *);
1048
1049static int attr_form_is_constant (struct attribute *);
1050
93e7bd98
DJ
1051static void dwarf2_symbol_mark_computed (struct attribute *attr,
1052 struct symbol *sym,
1053 struct dwarf2_cu *cu);
4c2df51b 1054
fe1b8b76
JB
1055static gdb_byte *skip_one_die (gdb_byte *info_ptr, struct abbrev_info *abbrev,
1056 struct dwarf2_cu *cu);
4bb7a0a7 1057
72bf9492
DJ
1058static void free_stack_comp_unit (void *);
1059
72bf9492
DJ
1060static hashval_t partial_die_hash (const void *item);
1061
1062static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1063
ae038cb0 1064static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
c764a876 1065 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1066
1067static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
c764a876 1068 (unsigned int offset, struct objfile *objfile);
ae038cb0
DJ
1069
1070static void free_one_comp_unit (void *);
1071
1072static void free_cached_comp_units (void *);
1073
1074static void age_cached_comp_units (void);
1075
1076static void free_one_cached_comp_unit (void *);
1077
f792889a
DJ
1078static struct type *set_die_type (struct die_info *, struct type *,
1079 struct dwarf2_cu *);
1c379e20 1080
ae038cb0
DJ
1081static void create_all_comp_units (struct objfile *);
1082
31ffec48
DJ
1083static struct dwarf2_cu *load_full_comp_unit (struct dwarf2_per_cu_data *,
1084 struct objfile *);
10b3939b
DJ
1085
1086static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1087
1088static void dwarf2_add_dependence (struct dwarf2_cu *,
1089 struct dwarf2_per_cu_data *);
1090
ae038cb0
DJ
1091static void dwarf2_mark (struct dwarf2_cu *);
1092
1093static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1094
f792889a 1095static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1096
c906108c
SS
1097/* Try to locate the sections we need for DWARF 2 debugging
1098 information and return true if we have enough to do something. */
1099
1100int
6502dd73 1101dwarf2_has_info (struct objfile *objfile)
c906108c 1102{
6502dd73
DJ
1103 struct dwarf2_per_objfile *data;
1104
1105 /* Initialize per-objfile state. */
1106 data = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1107 memset (data, 0, sizeof (*data));
1108 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1109 dwarf2_per_objfile = data;
1110
6502dd73 1111 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
dce234bc 1112 return (data->info.asection != NULL && data->abbrev.asection != NULL);
c906108c
SS
1113}
1114
233a11ab
CS
1115/* When loading sections, we can either look for ".<name>", or for
1116 * ".z<name>", which indicates a compressed section. */
1117
1118static int
dce234bc 1119section_is_p (const char *section_name, const char *name)
233a11ab 1120{
dce234bc
PP
1121 return (section_name[0] == '.'
1122 && (strcmp (section_name + 1, name) == 0
1123 || (section_name[1] == 'z'
1124 && strcmp (section_name + 2, name) == 0)));
233a11ab
CS
1125}
1126
c906108c
SS
1127/* This function is mapped across the sections and remembers the
1128 offset and size of each of the debugging sections we are interested
1129 in. */
1130
1131static void
72dca2f5 1132dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
c906108c 1133{
dce234bc 1134 if (section_is_p (sectp->name, INFO_SECTION))
c906108c 1135 {
dce234bc
PP
1136 dwarf2_per_objfile->info.asection = sectp;
1137 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1138 }
dce234bc 1139 else if (section_is_p (sectp->name, ABBREV_SECTION))
c906108c 1140 {
dce234bc
PP
1141 dwarf2_per_objfile->abbrev.asection = sectp;
1142 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1143 }
dce234bc 1144 else if (section_is_p (sectp->name, LINE_SECTION))
c906108c 1145 {
dce234bc
PP
1146 dwarf2_per_objfile->line.asection = sectp;
1147 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1148 }
dce234bc 1149 else if (section_is_p (sectp->name, PUBNAMES_SECTION))
c906108c 1150 {
dce234bc
PP
1151 dwarf2_per_objfile->pubnames.asection = sectp;
1152 dwarf2_per_objfile->pubnames.size = bfd_get_section_size (sectp);
c906108c 1153 }
dce234bc 1154 else if (section_is_p (sectp->name, ARANGES_SECTION))
c906108c 1155 {
dce234bc
PP
1156 dwarf2_per_objfile->aranges.asection = sectp;
1157 dwarf2_per_objfile->aranges.size = bfd_get_section_size (sectp);
c906108c 1158 }
dce234bc 1159 else if (section_is_p (sectp->name, LOC_SECTION))
c906108c 1160 {
dce234bc
PP
1161 dwarf2_per_objfile->loc.asection = sectp;
1162 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1163 }
dce234bc 1164 else if (section_is_p (sectp->name, MACINFO_SECTION))
c906108c 1165 {
dce234bc
PP
1166 dwarf2_per_objfile->macinfo.asection = sectp;
1167 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1168 }
dce234bc 1169 else if (section_is_p (sectp->name, STR_SECTION))
c906108c 1170 {
dce234bc
PP
1171 dwarf2_per_objfile->str.asection = sectp;
1172 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1173 }
dce234bc 1174 else if (section_is_p (sectp->name, FRAME_SECTION))
b6af0555 1175 {
dce234bc
PP
1176 dwarf2_per_objfile->frame.asection = sectp;
1177 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1178 }
dce234bc 1179 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
b6af0555 1180 {
3799ccc6
EZ
1181 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1182 if (aflag & SEC_HAS_CONTENTS)
1183 {
dce234bc
PP
1184 dwarf2_per_objfile->eh_frame.asection = sectp;
1185 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1186 }
b6af0555 1187 }
dce234bc 1188 else if (section_is_p (sectp->name, RANGES_SECTION))
af34e669 1189 {
dce234bc
PP
1190 dwarf2_per_objfile->ranges.asection = sectp;
1191 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1192 }
dce234bc 1193
72dca2f5
FR
1194 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1195 && bfd_section_vma (abfd, sectp) == 0)
1196 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1197}
1198
dce234bc
PP
1199/* Decompress a section that was compressed using zlib. Store the
1200 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1201
1202static void
dce234bc
PP
1203zlib_decompress_section (struct objfile *objfile, asection *sectp,
1204 gdb_byte **outbuf, bfd_size_type *outsize)
1205{
1206 bfd *abfd = objfile->obfd;
1207#ifndef HAVE_ZLIB_H
1208 error (_("Support for zlib-compressed DWARF data (from '%s') "
1209 "is disabled in this copy of GDB"),
1210 bfd_get_filename (abfd));
1211#else
1212 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1213 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1214 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1215 bfd_size_type uncompressed_size;
1216 gdb_byte *uncompressed_buffer;
1217 z_stream strm;
1218 int rc;
1219 int header_size = 12;
1220
1221 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1222 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1223 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1224 bfd_get_filename (abfd));
1225
1226 /* Read the zlib header. In this case, it should be "ZLIB" followed
1227 by the uncompressed section size, 8 bytes in big-endian order. */
1228 if (compressed_size < header_size
1229 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1230 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1231 bfd_get_filename (abfd));
1232 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1233 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1234 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1235 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1236 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1237 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1238 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1239 uncompressed_size += compressed_buffer[11];
1240
1241 /* It is possible the section consists of several compressed
1242 buffers concatenated together, so we uncompress in a loop. */
1243 strm.zalloc = NULL;
1244 strm.zfree = NULL;
1245 strm.opaque = NULL;
1246 strm.avail_in = compressed_size - header_size;
1247 strm.next_in = (Bytef*) compressed_buffer + header_size;
1248 strm.avail_out = uncompressed_size;
1249 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1250 uncompressed_size);
1251 rc = inflateInit (&strm);
1252 while (strm.avail_in > 0)
1253 {
1254 if (rc != Z_OK)
1255 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1256 bfd_get_filename (abfd), rc);
1257 strm.next_out = ((Bytef*) uncompressed_buffer
1258 + (uncompressed_size - strm.avail_out));
1259 rc = inflate (&strm, Z_FINISH);
1260 if (rc != Z_STREAM_END)
1261 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1262 bfd_get_filename (abfd), rc);
1263 rc = inflateReset (&strm);
1264 }
1265 rc = inflateEnd (&strm);
1266 if (rc != Z_OK
1267 || strm.avail_out != 0)
1268 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1269 bfd_get_filename (abfd), rc);
1270
affddf13 1271 do_cleanups (cleanup);
dce234bc
PP
1272 *outbuf = uncompressed_buffer;
1273 *outsize = uncompressed_size;
1274#endif
233a11ab
CS
1275}
1276
dce234bc
PP
1277/* Read the contents of the section SECTP from object file specified by
1278 OBJFILE, store info about the section into INFO.
1279 If the section is compressed, uncompress it before returning. */
c906108c 1280
dce234bc
PP
1281static void
1282dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1283{
dce234bc
PP
1284 bfd *abfd = objfile->obfd;
1285 asection *sectp = info->asection;
1286 gdb_byte *buf, *retbuf;
1287 unsigned char header[4];
c906108c 1288
dce234bc
PP
1289 info->buffer = NULL;
1290 info->was_mmapped = 0;
188dd5d6 1291
dce234bc
PP
1292 if (info->asection == NULL || info->size == 0)
1293 return;
c906108c 1294
dce234bc
PP
1295 /* Check if the file has a 4-byte header indicating compression. */
1296 if (info->size > sizeof (header)
1297 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1298 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1299 {
1300 /* Upon decompression, update the buffer and its size. */
1301 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1302 {
1303 zlib_decompress_section (objfile, sectp, &info->buffer,
1304 &info->size);
1305 return;
1306 }
1307 }
4bdf3d34 1308
dce234bc
PP
1309#ifdef HAVE_MMAP
1310 if (pagesize == 0)
1311 pagesize = getpagesize ();
2e276125 1312
dce234bc
PP
1313 /* Only try to mmap sections which are large enough: we don't want to
1314 waste space due to fragmentation. Also, only try mmap for sections
1315 without relocations. */
1316
1317 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1318 {
1319 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1320 size_t map_length = info->size + sectp->filepos - pg_offset;
1321 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1322 MAP_PRIVATE, pg_offset);
1323
1324 if (retbuf != MAP_FAILED)
1325 {
1326 info->was_mmapped = 1;
1327 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1328 return;
1329 }
1330 }
1331#endif
1332
1333 /* If we get here, we are a normal, not-compressed section. */
1334 info->buffer = buf
1335 = obstack_alloc (&objfile->objfile_obstack, info->size);
1336
1337 /* When debugging .o files, we may need to apply relocations; see
1338 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1339 We never compress sections in .o files, so we only need to
1340 try this when the section is not compressed. */
1341 retbuf = symfile_relocate_debug_section (abfd, sectp, buf);
1342 if (retbuf != NULL)
1343 {
1344 info->buffer = retbuf;
1345 return;
1346 }
1347
1348 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1349 || bfd_bread (buf, info->size, abfd) != info->size)
1350 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1351 bfd_get_filename (abfd));
1352}
1353
1354/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1355 SECTION_NAME. */
af34e669 1356
dce234bc
PP
1357void
1358dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1359 asection **sectp, gdb_byte **bufp,
1360 bfd_size_type *sizep)
1361{
1362 struct dwarf2_per_objfile *data
1363 = objfile_data (objfile, dwarf2_objfile_data_key);
1364 struct dwarf2_section_info *info;
1365 if (section_is_p (section_name, EH_FRAME_SECTION))
1366 info = &data->eh_frame;
1367 else if (section_is_p (section_name, FRAME_SECTION))
1368 info = &data->frame;
0d53c4c4 1369 else
dce234bc
PP
1370 gdb_assert (0);
1371
1372 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1373 /* We haven't read this section in yet. Do it now. */
1374 dwarf2_read_section (objfile, info);
1375
1376 *sectp = info->asection;
1377 *bufp = info->buffer;
1378 *sizep = info->size;
1379}
1380
1381/* Build a partial symbol table. */
1382
1383void
1384dwarf2_build_psymtabs (struct objfile *objfile, int mainline)
1385{
1386 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
1387 dwarf2_read_section (objfile, &dwarf2_per_objfile->abbrev);
1388 dwarf2_read_section (objfile, &dwarf2_per_objfile->line);
1389 dwarf2_read_section (objfile, &dwarf2_per_objfile->str);
1390 dwarf2_read_section (objfile, &dwarf2_per_objfile->macinfo);
1391 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
1392 dwarf2_read_section (objfile, &dwarf2_per_objfile->loc);
1393 dwarf2_read_section (objfile, &dwarf2_per_objfile->eh_frame);
1394 dwarf2_read_section (objfile, &dwarf2_per_objfile->frame);
0d53c4c4 1395
ef96bde8
EZ
1396 if (mainline
1397 || (objfile->global_psymbols.size == 0
1398 && objfile->static_psymbols.size == 0))
c906108c
SS
1399 {
1400 init_psymbol_list (objfile, 1024);
1401 }
1402
1403#if 0
1404 if (dwarf_aranges_offset && dwarf_pubnames_offset)
1405 {
d4f3574e 1406 /* Things are significantly easier if we have .debug_aranges and
c906108c
SS
1407 .debug_pubnames sections */
1408
d4f3574e 1409 dwarf2_build_psymtabs_easy (objfile, mainline);
c906108c
SS
1410 }
1411 else
1412#endif
1413 /* only test this case for now */
c5aa993b 1414 {
c906108c 1415 /* In this case we have to work a bit harder */
d4f3574e 1416 dwarf2_build_psymtabs_hard (objfile, mainline);
c906108c
SS
1417 }
1418}
1419
1420#if 0
1421/* Build the partial symbol table from the information in the
1422 .debug_pubnames and .debug_aranges sections. */
1423
1424static void
fba45db2 1425dwarf2_build_psymtabs_easy (struct objfile *objfile, int mainline)
c906108c
SS
1426{
1427 bfd *abfd = objfile->obfd;
1428 char *aranges_buffer, *pubnames_buffer;
1429 char *aranges_ptr, *pubnames_ptr;
1430 unsigned int entry_length, version, info_offset, info_size;
1431
1432 pubnames_buffer = dwarf2_read_section (objfile,
086df311 1433 dwarf_pubnames_section);
c906108c 1434 pubnames_ptr = pubnames_buffer;
dce234bc 1435 while ((pubnames_ptr - pubnames_buffer) < dwarf2_per_objfile->pubnames.size)
c906108c 1436 {
891d2f0b 1437 unsigned int bytes_read;
613e1657 1438
c764a876 1439 entry_length = read_initial_length (abfd, pubnames_ptr, &bytes_read);
613e1657 1440 pubnames_ptr += bytes_read;
c906108c
SS
1441 version = read_1_byte (abfd, pubnames_ptr);
1442 pubnames_ptr += 1;
1443 info_offset = read_4_bytes (abfd, pubnames_ptr);
1444 pubnames_ptr += 4;
1445 info_size = read_4_bytes (abfd, pubnames_ptr);
1446 pubnames_ptr += 4;
1447 }
1448
1449 aranges_buffer = dwarf2_read_section (objfile,
086df311 1450 dwarf_aranges_section);
c906108c
SS
1451
1452}
1453#endif
1454
45452591
DE
1455/* Return TRUE if OFFSET is within CU_HEADER. */
1456
1457static inline int
1458offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
1459{
1460 unsigned int bottom = cu_header->offset;
1461 unsigned int top = (cu_header->offset
1462 + cu_header->length
1463 + cu_header->initial_length_size);
1464 return (offset >= bottom && offset < top);
1465}
1466
107d2387 1467/* Read in the comp unit header information from the debug_info at
917c78fc 1468 info_ptr. */
107d2387 1469
fe1b8b76 1470static gdb_byte *
107d2387 1471read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 1472 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
1473{
1474 int signed_addr;
891d2f0b 1475 unsigned int bytes_read;
c764a876
DE
1476
1477 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
1478 cu_header->initial_length_size = bytes_read;
1479 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 1480 info_ptr += bytes_read;
107d2387
AC
1481 cu_header->version = read_2_bytes (abfd, info_ptr);
1482 info_ptr += 2;
613e1657 1483 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
c764a876 1484 &bytes_read);
613e1657 1485 info_ptr += bytes_read;
107d2387
AC
1486 cu_header->addr_size = read_1_byte (abfd, info_ptr);
1487 info_ptr += 1;
1488 signed_addr = bfd_get_sign_extend_vma (abfd);
1489 if (signed_addr < 0)
8e65ff28 1490 internal_error (__FILE__, __LINE__,
e2e0b3e5 1491 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 1492 cu_header->signed_addr_p = signed_addr;
c764a876 1493
107d2387
AC
1494 return info_ptr;
1495}
1496
fe1b8b76
JB
1497static gdb_byte *
1498partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
72bf9492
DJ
1499 bfd *abfd)
1500{
fe1b8b76 1501 gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492
DJ
1502
1503 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
1504
2b949cb6 1505 if (header->version != 2 && header->version != 3)
8a3fe4f8
AC
1506 error (_("Dwarf Error: wrong version in compilation unit header "
1507 "(is %d, should be %d) [in module %s]"), header->version,
72bf9492
DJ
1508 2, bfd_get_filename (abfd));
1509
dce234bc 1510 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
8a3fe4f8
AC
1511 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
1512 "(offset 0x%lx + 6) [in module %s]"),
72bf9492 1513 (long) header->abbrev_offset,
dce234bc 1514 (long) (beg_of_comp_unit - dwarf2_per_objfile->info.buffer),
72bf9492
DJ
1515 bfd_get_filename (abfd));
1516
1517 if (beg_of_comp_unit + header->length + header->initial_length_size
dce234bc 1518 > dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
8a3fe4f8
AC
1519 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
1520 "(offset 0x%lx + 0) [in module %s]"),
72bf9492 1521 (long) header->length,
dce234bc 1522 (long) (beg_of_comp_unit - dwarf2_per_objfile->info.buffer),
72bf9492
DJ
1523 bfd_get_filename (abfd));
1524
1525 return info_ptr;
1526}
1527
aaa75496
JB
1528/* Allocate a new partial symtab for file named NAME and mark this new
1529 partial symtab as being an include of PST. */
1530
1531static void
1532dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
1533 struct objfile *objfile)
1534{
1535 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
1536
1537 subpst->section_offsets = pst->section_offsets;
1538 subpst->textlow = 0;
1539 subpst->texthigh = 0;
1540
1541 subpst->dependencies = (struct partial_symtab **)
1542 obstack_alloc (&objfile->objfile_obstack,
1543 sizeof (struct partial_symtab *));
1544 subpst->dependencies[0] = pst;
1545 subpst->number_of_dependencies = 1;
1546
1547 subpst->globals_offset = 0;
1548 subpst->n_global_syms = 0;
1549 subpst->statics_offset = 0;
1550 subpst->n_static_syms = 0;
1551 subpst->symtab = NULL;
1552 subpst->read_symtab = pst->read_symtab;
1553 subpst->readin = 0;
1554
1555 /* No private part is necessary for include psymtabs. This property
1556 can be used to differentiate between such include psymtabs and
10b3939b 1557 the regular ones. */
58a9656e 1558 subpst->read_symtab_private = NULL;
aaa75496
JB
1559}
1560
1561/* Read the Line Number Program data and extract the list of files
1562 included by the source file represented by PST. Build an include
1563 partial symtab for each of these included files.
1564
1565 This procedure assumes that there *is* a Line Number Program in
1566 the given CU. Callers should check that PDI->HAS_STMT_LIST is set
1567 before calling this procedure. */
1568
1569static void
1570dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
1571 struct partial_die_info *pdi,
1572 struct partial_symtab *pst)
1573{
1574 struct objfile *objfile = cu->objfile;
1575 bfd *abfd = objfile->obfd;
1576 struct line_header *lh;
1577
1578 lh = dwarf_decode_line_header (pdi->line_offset, abfd, cu);
1579 if (lh == NULL)
1580 return; /* No linetable, so no includes. */
1581
1582 dwarf_decode_lines (lh, NULL, abfd, cu, pst);
1583
1584 free_line_header (lh);
1585}
1586
1587
c906108c
SS
1588/* Build the partial symbol table by doing a quick pass through the
1589 .debug_info and .debug_abbrev sections. */
1590
1591static void
fba45db2 1592dwarf2_build_psymtabs_hard (struct objfile *objfile, int mainline)
c906108c
SS
1593{
1594 /* Instead of reading this into a big buffer, we should probably use
1595 mmap() on architectures that support it. (FIXME) */
1596 bfd *abfd = objfile->obfd;
fe1b8b76
JB
1597 gdb_byte *info_ptr;
1598 gdb_byte *beg_of_comp_unit;
c906108c
SS
1599 struct partial_die_info comp_unit_die;
1600 struct partial_symtab *pst;
ae038cb0 1601 struct cleanup *back_to;
5734ee8b 1602 CORE_ADDR baseaddr;
c906108c 1603
dce234bc 1604 info_ptr = dwarf2_per_objfile->info.buffer;
c906108c 1605
ae038cb0
DJ
1606 /* Any cached compilation units will be linked by the per-objfile
1607 read_in_chain. Make sure to free them when we're done. */
1608 back_to = make_cleanup (free_cached_comp_units, NULL);
1609
10b3939b
DJ
1610 create_all_comp_units (objfile);
1611
ff013f42
JK
1612 objfile->psymtabs_addrmap = addrmap_create_mutable
1613 (&objfile->objfile_obstack);
1614
6502dd73 1615 /* Since the objects we're extracting from .debug_info vary in
af703f96 1616 length, only the individual functions to extract them (like
72bf9492 1617 read_comp_unit_head and load_partial_die) can really know whether
af703f96
JB
1618 the buffer is large enough to hold another complete object.
1619
6502dd73
DJ
1620 At the moment, they don't actually check that. If .debug_info
1621 holds just one extra byte after the last compilation unit's dies,
1622 then read_comp_unit_head will happily read off the end of the
1623 buffer. read_partial_die is similarly casual. Those functions
1624 should be fixed.
af703f96
JB
1625
1626 For this loop condition, simply checking whether there's any data
1627 left at all should be sufficient. */
dce234bc
PP
1628 while (info_ptr < (dwarf2_per_objfile->info.buffer
1629 + dwarf2_per_objfile->info.size))
c906108c 1630 {
f3dd6933 1631 struct cleanup *back_to_inner;
e7c27a73 1632 struct dwarf2_cu cu;
72bf9492
DJ
1633 struct abbrev_info *abbrev;
1634 unsigned int bytes_read;
1635 struct dwarf2_per_cu_data *this_cu;
1636
c906108c 1637 beg_of_comp_unit = info_ptr;
c906108c 1638
72bf9492
DJ
1639 memset (&cu, 0, sizeof (cu));
1640
1641 obstack_init (&cu.comp_unit_obstack);
1642
1643 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
1644
e7c27a73 1645 cu.objfile = objfile;
72bf9492 1646 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr, abfd);
e7c27a73 1647
57349743 1648 /* Complete the cu_header */
dce234bc 1649 cu.header.offset = beg_of_comp_unit - dwarf2_per_objfile->info.buffer;
d00adf39 1650 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
57349743 1651
e142c38c
DJ
1652 cu.list_in_scope = &file_symbols;
1653
c906108c 1654 /* Read the abbrevs for this compilation unit into a table */
e7c27a73 1655 dwarf2_read_abbrevs (abfd, &cu);
72bf9492 1656 make_cleanup (dwarf2_free_abbrev_table, &cu);
c906108c 1657
10b3939b 1658 this_cu = dwarf2_find_comp_unit (cu.header.offset, objfile);
ae038cb0 1659
c906108c 1660 /* Read the compilation unit die */
72bf9492
DJ
1661 abbrev = peek_die_abbrev (info_ptr, &bytes_read, &cu);
1662 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
1663 abfd, info_ptr, &cu);
c906108c 1664
31ffec48
DJ
1665 if (comp_unit_die.tag == DW_TAG_partial_unit)
1666 {
1667 info_ptr = (beg_of_comp_unit + cu.header.length
1668 + cu.header.initial_length_size);
1669 do_cleanups (back_to_inner);
1670 continue;
1671 }
1672
c906108c 1673 /* Set the language we're debugging */
e142c38c 1674 set_cu_language (comp_unit_die.language, &cu);
c906108c
SS
1675
1676 /* Allocate a new partial symbol table structure */
d4f3574e 1677 pst = start_psymtab_common (objfile, objfile->section_offsets,
96baa820 1678 comp_unit_die.name ? comp_unit_die.name : "",
ff013f42
JK
1679 /* TEXTLOW and TEXTHIGH are set below. */
1680 0,
c906108c
SS
1681 objfile->global_psymbols.next,
1682 objfile->static_psymbols.next);
1683
ae038cb0 1684 if (comp_unit_die.dirname)
a683b3c0
TT
1685 pst->dirname = obsavestring (comp_unit_die.dirname,
1686 strlen (comp_unit_die.dirname),
1687 &objfile->objfile_obstack);
57c22c6c 1688
10b3939b
DJ
1689 pst->read_symtab_private = (char *) this_cu;
1690
613e1657 1691 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
1692
1693 /* Store the function that reads in the rest of the symbol table */
1694 pst->read_symtab = dwarf2_psymtab_to_symtab;
1695
10b3939b
DJ
1696 /* If this compilation unit was already read in, free the
1697 cached copy in order to read it in again. This is
1698 necessary because we skipped some symbols when we first
1699 read in the compilation unit (see load_partial_dies).
1700 This problem could be avoided, but the benefit is
1701 unclear. */
1702 if (this_cu->cu != NULL)
1703 free_one_cached_comp_unit (this_cu->cu);
ae038cb0 1704
10b3939b 1705 cu.per_cu = this_cu;
ae038cb0 1706
10b3939b
DJ
1707 /* Note that this is a pointer to our stack frame, being
1708 added to a global data structure. It will be cleaned up
1709 in free_stack_comp_unit when we finish with this
1710 compilation unit. */
1711 this_cu->cu = &cu;
ae038cb0 1712
10b3939b 1713 this_cu->psymtab = pst;
ae038cb0 1714
ff013f42
JK
1715 /* Possibly set the default values of LOWPC and HIGHPC from
1716 `DW_AT_ranges'. */
1717 if (cu.has_ranges_offset)
1718 {
1719 if (dwarf2_ranges_read (cu.ranges_offset, &comp_unit_die.lowpc,
1720 &comp_unit_die.highpc, &cu, pst))
1721 comp_unit_die.has_pc_info = 1;
1722 }
5734ee8b
DJ
1723 else if (comp_unit_die.has_pc_info
1724 && comp_unit_die.lowpc < comp_unit_die.highpc)
1725 /* Store the contiguous range if it is not empty; it can be empty for
1726 CUs with no code. */
1727 addrmap_set_empty (objfile->psymtabs_addrmap,
1728 comp_unit_die.lowpc + baseaddr,
1729 comp_unit_die.highpc + baseaddr - 1, pst);
ff013f42 1730
c906108c
SS
1731 /* Check if comp unit has_children.
1732 If so, read the rest of the partial symbols from this comp unit.
1733 If not, there's no more debug_info for this comp unit. */
1734 if (comp_unit_die.has_children)
1735 {
72bf9492 1736 struct partial_die_info *first_die;
5734ee8b 1737 CORE_ADDR lowpc, highpc;
72bf9492 1738
91c24f0a
DC
1739 lowpc = ((CORE_ADDR) -1);
1740 highpc = ((CORE_ADDR) 0);
1741
72bf9492
DJ
1742 first_die = load_partial_dies (abfd, info_ptr, 1, &cu);
1743
5734ee8b
DJ
1744 scan_partial_symbols (first_die, &lowpc, &highpc,
1745 ! comp_unit_die.has_pc_info, &cu);
c906108c 1746
91c24f0a
DC
1747 /* If we didn't find a lowpc, set it to highpc to avoid
1748 complaints from `maint check'. */
1749 if (lowpc == ((CORE_ADDR) -1))
1750 lowpc = highpc;
72bf9492 1751
c906108c
SS
1752 /* If the compilation unit didn't have an explicit address range,
1753 then use the information extracted from its child dies. */
0b010bcc 1754 if (! comp_unit_die.has_pc_info)
c906108c 1755 {
c5aa993b 1756 comp_unit_die.lowpc = lowpc;
c906108c
SS
1757 comp_unit_die.highpc = highpc;
1758 }
1759 }
c5aa993b 1760 pst->textlow = comp_unit_die.lowpc + baseaddr;
c906108c
SS
1761 pst->texthigh = comp_unit_die.highpc + baseaddr;
1762
1763 pst->n_global_syms = objfile->global_psymbols.next -
1764 (objfile->global_psymbols.list + pst->globals_offset);
1765 pst->n_static_syms = objfile->static_psymbols.next -
1766 (objfile->static_psymbols.list + pst->statics_offset);
1767 sort_pst_symbols (pst);
1768
1769 /* If there is already a psymtab or symtab for a file of this
1770 name, remove it. (If there is a symtab, more drastic things
1771 also happen.) This happens in VxWorks. */
1772 free_named_symtabs (pst->filename);
1773
dd373385
EZ
1774 info_ptr = beg_of_comp_unit + cu.header.length
1775 + cu.header.initial_length_size;
1776
aaa75496
JB
1777 if (comp_unit_die.has_stmt_list)
1778 {
1779 /* Get the list of files included in the current compilation unit,
1780 and build a psymtab for each of them. */
1781 dwarf2_build_include_psymtabs (&cu, &comp_unit_die, pst);
1782 }
1783
f3dd6933 1784 do_cleanups (back_to_inner);
c906108c 1785 }
ff013f42
JK
1786
1787 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
1788 &objfile->objfile_obstack);
1789
ae038cb0
DJ
1790 do_cleanups (back_to);
1791}
1792
1793/* Load the DIEs for a secondary CU into memory. */
1794
1795static void
1796load_comp_unit (struct dwarf2_per_cu_data *this_cu, struct objfile *objfile)
1797{
1798 bfd *abfd = objfile->obfd;
fe1b8b76 1799 gdb_byte *info_ptr, *beg_of_comp_unit;
ae038cb0
DJ
1800 struct partial_die_info comp_unit_die;
1801 struct dwarf2_cu *cu;
1802 struct abbrev_info *abbrev;
1803 unsigned int bytes_read;
1804 struct cleanup *back_to;
1805
dce234bc 1806 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
ae038cb0
DJ
1807 beg_of_comp_unit = info_ptr;
1808
1809 cu = xmalloc (sizeof (struct dwarf2_cu));
1810 memset (cu, 0, sizeof (struct dwarf2_cu));
1811
1812 obstack_init (&cu->comp_unit_obstack);
1813
1814 cu->objfile = objfile;
1815 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr, abfd);
1816
1817 /* Complete the cu_header. */
dce234bc 1818 cu->header.offset = beg_of_comp_unit - dwarf2_per_objfile->info.buffer;
d00adf39 1819 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
ae038cb0
DJ
1820
1821 /* Read the abbrevs for this compilation unit into a table. */
1822 dwarf2_read_abbrevs (abfd, cu);
1823 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
1824
1825 /* Read the compilation unit die. */
1826 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
1827 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
1828 abfd, info_ptr, cu);
1829
1830 /* Set the language we're debugging. */
1831 set_cu_language (comp_unit_die.language, cu);
1832
1833 /* Link this compilation unit into the compilation unit tree. */
1834 this_cu->cu = cu;
1835 cu->per_cu = this_cu;
f792889a 1836 cu->type_hash = cu->per_cu->type_hash;
ae038cb0
DJ
1837
1838 /* Check if comp unit has_children.
1839 If so, read the rest of the partial symbols from this comp unit.
1840 If not, there's no more debug_info for this comp unit. */
1841 if (comp_unit_die.has_children)
1842 load_partial_dies (abfd, info_ptr, 0, cu);
1843
1844 do_cleanups (back_to);
1845}
1846
1847/* Create a list of all compilation units in OBJFILE. We do this only
1848 if an inter-comp-unit reference is found; presumably if there is one,
1849 there will be many, and one will occur early in the .debug_info section.
1850 So there's no point in building this list incrementally. */
1851
1852static void
1853create_all_comp_units (struct objfile *objfile)
1854{
1855 int n_allocated;
1856 int n_comp_units;
1857 struct dwarf2_per_cu_data **all_comp_units;
dce234bc 1858 gdb_byte *info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
1859
1860 n_comp_units = 0;
1861 n_allocated = 10;
1862 all_comp_units = xmalloc (n_allocated
1863 * sizeof (struct dwarf2_per_cu_data *));
1864
dce234bc 1865 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
ae038cb0 1866 {
c764a876 1867 unsigned int length, initial_length_size;
fe1b8b76 1868 gdb_byte *beg_of_comp_unit;
ae038cb0 1869 struct dwarf2_per_cu_data *this_cu;
c764a876 1870 unsigned int offset;
ae038cb0 1871
dce234bc 1872 offset = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
1873
1874 /* Read just enough information to find out where the next
1875 compilation unit is. */
c764a876
DE
1876 length = read_initial_length (objfile->obfd, info_ptr,
1877 &initial_length_size);
ae038cb0
DJ
1878
1879 /* Save the compilation unit for later lookup. */
1880 this_cu = obstack_alloc (&objfile->objfile_obstack,
1881 sizeof (struct dwarf2_per_cu_data));
1882 memset (this_cu, 0, sizeof (*this_cu));
1883 this_cu->offset = offset;
c764a876 1884 this_cu->length = length + initial_length_size;
ae038cb0
DJ
1885
1886 if (n_comp_units == n_allocated)
1887 {
1888 n_allocated *= 2;
1889 all_comp_units = xrealloc (all_comp_units,
1890 n_allocated
1891 * sizeof (struct dwarf2_per_cu_data *));
1892 }
1893 all_comp_units[n_comp_units++] = this_cu;
1894
1895 info_ptr = info_ptr + this_cu->length;
1896 }
1897
1898 dwarf2_per_objfile->all_comp_units
1899 = obstack_alloc (&objfile->objfile_obstack,
1900 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
1901 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
1902 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
1903 xfree (all_comp_units);
1904 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
1905}
1906
5734ee8b
DJ
1907/* Process all loaded DIEs for compilation unit CU, starting at
1908 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
1909 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
1910 DW_AT_ranges). If NEED_PC is set, then this function will set
1911 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
1912 and record the covered ranges in the addrmap. */
c906108c 1913
72bf9492
DJ
1914static void
1915scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 1916 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 1917{
e7c27a73 1918 struct objfile *objfile = cu->objfile;
c906108c 1919 bfd *abfd = objfile->obfd;
72bf9492 1920 struct partial_die_info *pdi;
c906108c 1921
91c24f0a
DC
1922 /* Now, march along the PDI's, descending into ones which have
1923 interesting children but skipping the children of the other ones,
1924 until we reach the end of the compilation unit. */
c906108c 1925
72bf9492 1926 pdi = first_die;
91c24f0a 1927
72bf9492
DJ
1928 while (pdi != NULL)
1929 {
1930 fixup_partial_die (pdi, cu);
c906108c 1931
91c24f0a
DC
1932 /* Anonymous namespaces have no name but have interesting
1933 children, so we need to look at them. Ditto for anonymous
1934 enums. */
933c6fe4 1935
72bf9492
DJ
1936 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
1937 || pdi->tag == DW_TAG_enumeration_type)
c906108c 1938 {
72bf9492 1939 switch (pdi->tag)
c906108c
SS
1940 {
1941 case DW_TAG_subprogram:
5734ee8b 1942 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c
SS
1943 break;
1944 case DW_TAG_variable:
1945 case DW_TAG_typedef:
91c24f0a 1946 case DW_TAG_union_type:
72bf9492 1947 if (!pdi->is_declaration)
63d06c5c 1948 {
72bf9492 1949 add_partial_symbol (pdi, cu);
63d06c5c
DC
1950 }
1951 break;
c906108c 1952 case DW_TAG_class_type:
680b30c7 1953 case DW_TAG_interface_type:
c906108c 1954 case DW_TAG_structure_type:
72bf9492 1955 if (!pdi->is_declaration)
c906108c 1956 {
72bf9492 1957 add_partial_symbol (pdi, cu);
c906108c
SS
1958 }
1959 break;
91c24f0a 1960 case DW_TAG_enumeration_type:
72bf9492
DJ
1961 if (!pdi->is_declaration)
1962 add_partial_enumeration (pdi, cu);
c906108c
SS
1963 break;
1964 case DW_TAG_base_type:
a02abb62 1965 case DW_TAG_subrange_type:
c906108c 1966 /* File scope base type definitions are added to the partial
c5aa993b 1967 symbol table. */
72bf9492 1968 add_partial_symbol (pdi, cu);
c906108c 1969 break;
d9fa45fe 1970 case DW_TAG_namespace:
5734ee8b 1971 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 1972 break;
5d7cb8df
JK
1973 case DW_TAG_module:
1974 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
1975 break;
c906108c
SS
1976 default:
1977 break;
1978 }
1979 }
1980
72bf9492
DJ
1981 /* If the die has a sibling, skip to the sibling. */
1982
1983 pdi = pdi->die_sibling;
1984 }
1985}
1986
1987/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 1988
72bf9492 1989 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
1990 name is concatenated with "::" and the partial DIE's name. For
1991 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
1992 Enumerators are an exception; they use the scope of their parent
1993 enumeration type, i.e. the name of the enumeration type is not
1994 prepended to the enumerator.
91c24f0a 1995
72bf9492
DJ
1996 There are two complexities. One is DW_AT_specification; in this
1997 case "parent" means the parent of the target of the specification,
1998 instead of the direct parent of the DIE. The other is compilers
1999 which do not emit DW_TAG_namespace; in this case we try to guess
2000 the fully qualified name of structure types from their members'
2001 linkage names. This must be done using the DIE's children rather
2002 than the children of any DW_AT_specification target. We only need
2003 to do this for structures at the top level, i.e. if the target of
2004 any DW_AT_specification (if any; otherwise the DIE itself) does not
2005 have a parent. */
2006
2007/* Compute the scope prefix associated with PDI's parent, in
2008 compilation unit CU. The result will be allocated on CU's
2009 comp_unit_obstack, or a copy of the already allocated PDI->NAME
2010 field. NULL is returned if no prefix is necessary. */
2011static char *
2012partial_die_parent_scope (struct partial_die_info *pdi,
2013 struct dwarf2_cu *cu)
2014{
2015 char *grandparent_scope;
2016 struct partial_die_info *parent, *real_pdi;
91c24f0a 2017
72bf9492
DJ
2018 /* We need to look at our parent DIE; if we have a DW_AT_specification,
2019 then this means the parent of the specification DIE. */
2020
2021 real_pdi = pdi;
72bf9492 2022 while (real_pdi->has_specification)
10b3939b 2023 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
2024
2025 parent = real_pdi->die_parent;
2026 if (parent == NULL)
2027 return NULL;
2028
2029 if (parent->scope_set)
2030 return parent->scope;
2031
2032 fixup_partial_die (parent, cu);
2033
10b3939b 2034 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492
DJ
2035
2036 if (parent->tag == DW_TAG_namespace
2037 || parent->tag == DW_TAG_structure_type
2038 || parent->tag == DW_TAG_class_type
680b30c7 2039 || parent->tag == DW_TAG_interface_type
72bf9492
DJ
2040 || parent->tag == DW_TAG_union_type)
2041 {
2042 if (grandparent_scope == NULL)
2043 parent->scope = parent->name;
2044 else
987504bb
JJ
2045 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
2046 parent->name, cu);
72bf9492
DJ
2047 }
2048 else if (parent->tag == DW_TAG_enumeration_type)
2049 /* Enumerators should not get the name of the enumeration as a prefix. */
2050 parent->scope = grandparent_scope;
2051 else
2052 {
2053 /* FIXME drow/2004-04-01: What should we be doing with
2054 function-local names? For partial symbols, we should probably be
2055 ignoring them. */
2056 complaint (&symfile_complaints,
e2e0b3e5 2057 _("unhandled containing DIE tag %d for DIE at %d"),
72bf9492
DJ
2058 parent->tag, pdi->offset);
2059 parent->scope = grandparent_scope;
c906108c
SS
2060 }
2061
72bf9492
DJ
2062 parent->scope_set = 1;
2063 return parent->scope;
2064}
2065
2066/* Return the fully scoped name associated with PDI, from compilation unit
2067 CU. The result will be allocated with malloc. */
2068static char *
2069partial_die_full_name (struct partial_die_info *pdi,
2070 struct dwarf2_cu *cu)
2071{
2072 char *parent_scope;
2073
2074 parent_scope = partial_die_parent_scope (pdi, cu);
2075 if (parent_scope == NULL)
2076 return NULL;
2077 else
987504bb 2078 return typename_concat (NULL, parent_scope, pdi->name, cu);
c906108c
SS
2079}
2080
2081static void
72bf9492 2082add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 2083{
e7c27a73 2084 struct objfile *objfile = cu->objfile;
c906108c 2085 CORE_ADDR addr = 0;
decbce07 2086 char *actual_name = NULL;
72bf9492 2087 const char *my_prefix;
5c4e30ca 2088 const struct partial_symbol *psym = NULL;
e142c38c 2089 CORE_ADDR baseaddr;
72bf9492 2090 int built_actual_name = 0;
e142c38c
DJ
2091
2092 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 2093
72bf9492 2094 if (pdi_needs_namespace (pdi->tag))
63d06c5c 2095 {
72bf9492
DJ
2096 actual_name = partial_die_full_name (pdi, cu);
2097 if (actual_name)
2098 built_actual_name = 1;
63d06c5c
DC
2099 }
2100
72bf9492
DJ
2101 if (actual_name == NULL)
2102 actual_name = pdi->name;
2103
c906108c
SS
2104 switch (pdi->tag)
2105 {
2106 case DW_TAG_subprogram:
2cfa0c8d 2107 if (pdi->is_external || cu->language == language_ada)
c906108c 2108 {
2cfa0c8d
JB
2109 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
2110 of the global scope. But in Ada, we want to be able to access
2111 nested procedures globally. So all Ada subprograms are stored
2112 in the global scope. */
38d518c9 2113 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 2114 mst_text, objfile); */
38d518c9 2115 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
2116 VAR_DOMAIN, LOC_BLOCK,
2117 &objfile->global_psymbols,
2118 0, pdi->lowpc + baseaddr,
e142c38c 2119 cu->language, objfile);
c906108c
SS
2120 }
2121 else
2122 {
38d518c9 2123 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 2124 mst_file_text, objfile); */
38d518c9 2125 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
2126 VAR_DOMAIN, LOC_BLOCK,
2127 &objfile->static_psymbols,
2128 0, pdi->lowpc + baseaddr,
e142c38c 2129 cu->language, objfile);
c906108c
SS
2130 }
2131 break;
2132 case DW_TAG_variable:
2133 if (pdi->is_external)
2134 {
2135 /* Global Variable.
2136 Don't enter into the minimal symbol tables as there is
2137 a minimal symbol table entry from the ELF symbols already.
2138 Enter into partial symbol table if it has a location
2139 descriptor or a type.
2140 If the location descriptor is missing, new_symbol will create
2141 a LOC_UNRESOLVED symbol, the address of the variable will then
2142 be determined from the minimal symbol table whenever the variable
2143 is referenced.
2144 The address for the partial symbol table entry is not
2145 used by GDB, but it comes in handy for debugging partial symbol
2146 table building. */
2147
2148 if (pdi->locdesc)
e7c27a73 2149 addr = decode_locdesc (pdi->locdesc, cu);
c906108c 2150 if (pdi->locdesc || pdi->has_type)
38d518c9 2151 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
2152 VAR_DOMAIN, LOC_STATIC,
2153 &objfile->global_psymbols,
2154 0, addr + baseaddr,
e142c38c 2155 cu->language, objfile);
c906108c
SS
2156 }
2157 else
2158 {
2159 /* Static Variable. Skip symbols without location descriptors. */
2160 if (pdi->locdesc == NULL)
decbce07
MS
2161 {
2162 if (built_actual_name)
2163 xfree (actual_name);
2164 return;
2165 }
e7c27a73 2166 addr = decode_locdesc (pdi->locdesc, cu);
38d518c9 2167 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 2168 mst_file_data, objfile); */
38d518c9 2169 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
2170 VAR_DOMAIN, LOC_STATIC,
2171 &objfile->static_psymbols,
2172 0, addr + baseaddr,
e142c38c 2173 cu->language, objfile);
c906108c
SS
2174 }
2175 break;
2176 case DW_TAG_typedef:
2177 case DW_TAG_base_type:
a02abb62 2178 case DW_TAG_subrange_type:
38d518c9 2179 add_psymbol_to_list (actual_name, strlen (actual_name),
176620f1 2180 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 2181 &objfile->static_psymbols,
e142c38c 2182 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 2183 break;
72bf9492
DJ
2184 case DW_TAG_namespace:
2185 add_psymbol_to_list (actual_name, strlen (actual_name),
2186 VAR_DOMAIN, LOC_TYPEDEF,
2187 &objfile->global_psymbols,
2188 0, (CORE_ADDR) 0, cu->language, objfile);
2189 break;
c906108c 2190 case DW_TAG_class_type:
680b30c7 2191 case DW_TAG_interface_type:
c906108c
SS
2192 case DW_TAG_structure_type:
2193 case DW_TAG_union_type:
2194 case DW_TAG_enumeration_type:
fa4028e9
JB
2195 /* Skip external references. The DWARF standard says in the section
2196 about "Structure, Union, and Class Type Entries": "An incomplete
2197 structure, union or class type is represented by a structure,
2198 union or class entry that does not have a byte size attribute
2199 and that has a DW_AT_declaration attribute." */
2200 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
2201 {
2202 if (built_actual_name)
2203 xfree (actual_name);
2204 return;
2205 }
fa4028e9 2206
63d06c5c
DC
2207 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
2208 static vs. global. */
38d518c9 2209 add_psymbol_to_list (actual_name, strlen (actual_name),
176620f1 2210 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
2211 (cu->language == language_cplus
2212 || cu->language == language_java)
63d06c5c
DC
2213 ? &objfile->global_psymbols
2214 : &objfile->static_psymbols,
e142c38c 2215 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 2216
c906108c
SS
2217 break;
2218 case DW_TAG_enumerator:
38d518c9 2219 add_psymbol_to_list (actual_name, strlen (actual_name),
176620f1 2220 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
2221 (cu->language == language_cplus
2222 || cu->language == language_java)
f6fe98ef
DJ
2223 ? &objfile->global_psymbols
2224 : &objfile->static_psymbols,
e142c38c 2225 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
2226 break;
2227 default:
2228 break;
2229 }
5c4e30ca
DC
2230
2231 /* Check to see if we should scan the name for possible namespace
2232 info. Only do this if this is C++, if we don't have namespace
2233 debugging info in the file, if the psym is of an appropriate type
2234 (otherwise we'll have psym == NULL), and if we actually had a
2235 mangled name to begin with. */
2236
72bf9492
DJ
2237 /* FIXME drow/2004-02-22: Why don't we do this for classes, i.e. the
2238 cases which do not set PSYM above? */
2239
e142c38c 2240 if (cu->language == language_cplus
72bf9492 2241 && cu->has_namespace_info == 0
5c4e30ca
DC
2242 && psym != NULL
2243 && SYMBOL_CPLUS_DEMANGLED_NAME (psym) != NULL)
2244 cp_check_possible_namespace_symbols (SYMBOL_CPLUS_DEMANGLED_NAME (psym),
2245 objfile);
72bf9492
DJ
2246
2247 if (built_actual_name)
2248 xfree (actual_name);
c906108c
SS
2249}
2250
72bf9492
DJ
2251/* Determine whether a die of type TAG living in a C++ class or
2252 namespace needs to have the name of the scope prepended to the
63d06c5c
DC
2253 name listed in the die. */
2254
2255static int
72bf9492 2256pdi_needs_namespace (enum dwarf_tag tag)
63d06c5c 2257{
63d06c5c
DC
2258 switch (tag)
2259 {
72bf9492 2260 case DW_TAG_namespace:
63d06c5c
DC
2261 case DW_TAG_typedef:
2262 case DW_TAG_class_type:
680b30c7 2263 case DW_TAG_interface_type:
63d06c5c
DC
2264 case DW_TAG_structure_type:
2265 case DW_TAG_union_type:
2266 case DW_TAG_enumeration_type:
2267 case DW_TAG_enumerator:
2268 return 1;
2269 default:
2270 return 0;
2271 }
2272}
2273
5c4e30ca
DC
2274/* Read a partial die corresponding to a namespace; also, add a symbol
2275 corresponding to that namespace to the symbol table. NAMESPACE is
2276 the name of the enclosing namespace. */
91c24f0a 2277
72bf9492
DJ
2278static void
2279add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 2280 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 2281 int need_pc, struct dwarf2_cu *cu)
91c24f0a 2282{
e7c27a73 2283 struct objfile *objfile = cu->objfile;
5c4e30ca 2284
72bf9492 2285 /* Add a symbol for the namespace. */
e7c27a73 2286
72bf9492 2287 add_partial_symbol (pdi, cu);
5c4e30ca
DC
2288
2289 /* Now scan partial symbols in that namespace. */
2290
91c24f0a 2291 if (pdi->has_children)
5734ee8b 2292 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
2293}
2294
5d7cb8df
JK
2295/* Read a partial die corresponding to a Fortran module. */
2296
2297static void
2298add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
2299 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
2300{
2301 /* Now scan partial symbols in that module.
2302
2303 FIXME: Support the separate Fortran module namespaces. */
2304
2305 if (pdi->has_children)
2306 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
2307}
2308
bc30ff58
JB
2309/* Read a partial die corresponding to a subprogram and create a partial
2310 symbol for that subprogram. When the CU language allows it, this
2311 routine also defines a partial symbol for each nested subprogram
2312 that this subprogram contains.
2313
2314 DIE my also be a lexical block, in which case we simply search
2315 recursively for suprograms defined inside that lexical block.
2316 Again, this is only performed when the CU language allows this
2317 type of definitions. */
2318
2319static void
2320add_partial_subprogram (struct partial_die_info *pdi,
2321 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 2322 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
2323{
2324 if (pdi->tag == DW_TAG_subprogram)
2325 {
2326 if (pdi->has_pc_info)
2327 {
2328 if (pdi->lowpc < *lowpc)
2329 *lowpc = pdi->lowpc;
2330 if (pdi->highpc > *highpc)
2331 *highpc = pdi->highpc;
5734ee8b
DJ
2332 if (need_pc)
2333 {
2334 CORE_ADDR baseaddr;
2335 struct objfile *objfile = cu->objfile;
2336
2337 baseaddr = ANOFFSET (objfile->section_offsets,
2338 SECT_OFF_TEXT (objfile));
2339 addrmap_set_empty (objfile->psymtabs_addrmap,
2340 pdi->lowpc, pdi->highpc - 1,
2341 cu->per_cu->psymtab);
2342 }
bc30ff58
JB
2343 if (!pdi->is_declaration)
2344 add_partial_symbol (pdi, cu);
2345 }
2346 }
2347
2348 if (! pdi->has_children)
2349 return;
2350
2351 if (cu->language == language_ada)
2352 {
2353 pdi = pdi->die_child;
2354 while (pdi != NULL)
2355 {
2356 fixup_partial_die (pdi, cu);
2357 if (pdi->tag == DW_TAG_subprogram
2358 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 2359 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
2360 pdi = pdi->die_sibling;
2361 }
2362 }
2363}
2364
72bf9492
DJ
2365/* See if we can figure out if the class lives in a namespace. We do
2366 this by looking for a member function; its demangled name will
2367 contain namespace info, if there is any. */
63d06c5c 2368
72bf9492
DJ
2369static void
2370guess_structure_name (struct partial_die_info *struct_pdi,
2371 struct dwarf2_cu *cu)
63d06c5c 2372{
987504bb
JJ
2373 if ((cu->language == language_cplus
2374 || cu->language == language_java)
72bf9492 2375 && cu->has_namespace_info == 0
63d06c5c
DC
2376 && struct_pdi->has_children)
2377 {
63d06c5c
DC
2378 /* NOTE: carlton/2003-10-07: Getting the info this way changes
2379 what template types look like, because the demangler
2380 frequently doesn't give the same name as the debug info. We
2381 could fix this by only using the demangled name to get the
134d01f1 2382 prefix (but see comment in read_structure_type). */
63d06c5c 2383
72bf9492
DJ
2384 struct partial_die_info *child_pdi = struct_pdi->die_child;
2385 struct partial_die_info *real_pdi;
5d51ca54 2386
72bf9492
DJ
2387 /* If this DIE (this DIE's specification, if any) has a parent, then
2388 we should not do this. We'll prepend the parent's fully qualified
2389 name when we create the partial symbol. */
5d51ca54 2390
72bf9492 2391 real_pdi = struct_pdi;
72bf9492 2392 while (real_pdi->has_specification)
10b3939b 2393 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
63d06c5c 2394
72bf9492
DJ
2395 if (real_pdi->die_parent != NULL)
2396 return;
63d06c5c 2397
72bf9492
DJ
2398 while (child_pdi != NULL)
2399 {
2400 if (child_pdi->tag == DW_TAG_subprogram)
63d06c5c 2401 {
72bf9492 2402 char *actual_class_name
31c27f77
JJ
2403 = language_class_name_from_physname (cu->language_defn,
2404 child_pdi->name);
63d06c5c 2405 if (actual_class_name != NULL)
72bf9492
DJ
2406 {
2407 struct_pdi->name
2408 = obsavestring (actual_class_name,
2409 strlen (actual_class_name),
2410 &cu->comp_unit_obstack);
2411 xfree (actual_class_name);
2412 }
63d06c5c
DC
2413 break;
2414 }
72bf9492
DJ
2415
2416 child_pdi = child_pdi->die_sibling;
63d06c5c
DC
2417 }
2418 }
63d06c5c
DC
2419}
2420
91c24f0a
DC
2421/* Read a partial die corresponding to an enumeration type. */
2422
72bf9492
DJ
2423static void
2424add_partial_enumeration (struct partial_die_info *enum_pdi,
2425 struct dwarf2_cu *cu)
91c24f0a 2426{
e7c27a73 2427 struct objfile *objfile = cu->objfile;
91c24f0a 2428 bfd *abfd = objfile->obfd;
72bf9492 2429 struct partial_die_info *pdi;
91c24f0a
DC
2430
2431 if (enum_pdi->name != NULL)
72bf9492
DJ
2432 add_partial_symbol (enum_pdi, cu);
2433
2434 pdi = enum_pdi->die_child;
2435 while (pdi)
91c24f0a 2436 {
72bf9492 2437 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 2438 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 2439 else
72bf9492
DJ
2440 add_partial_symbol (pdi, cu);
2441 pdi = pdi->die_sibling;
91c24f0a 2442 }
91c24f0a
DC
2443}
2444
4bb7a0a7
DJ
2445/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
2446 Return the corresponding abbrev, or NULL if the number is zero (indicating
2447 an empty DIE). In either case *BYTES_READ will be set to the length of
2448 the initial number. */
2449
2450static struct abbrev_info *
fe1b8b76 2451peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 2452 struct dwarf2_cu *cu)
4bb7a0a7
DJ
2453{
2454 bfd *abfd = cu->objfile->obfd;
2455 unsigned int abbrev_number;
2456 struct abbrev_info *abbrev;
2457
2458 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
2459
2460 if (abbrev_number == 0)
2461 return NULL;
2462
2463 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
2464 if (!abbrev)
2465 {
8a3fe4f8 2466 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
4bb7a0a7
DJ
2467 bfd_get_filename (abfd));
2468 }
2469
2470 return abbrev;
2471}
2472
2473/* Scan the debug information for CU starting at INFO_PTR. Returns a
2474 pointer to the end of a series of DIEs, terminated by an empty
2475 DIE. Any children of the skipped DIEs will also be skipped. */
2476
fe1b8b76
JB
2477static gdb_byte *
2478skip_children (gdb_byte *info_ptr, struct dwarf2_cu *cu)
4bb7a0a7
DJ
2479{
2480 struct abbrev_info *abbrev;
2481 unsigned int bytes_read;
2482
2483 while (1)
2484 {
2485 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
2486 if (abbrev == NULL)
2487 return info_ptr + bytes_read;
2488 else
2489 info_ptr = skip_one_die (info_ptr + bytes_read, abbrev, cu);
2490 }
2491}
2492
2493/* Scan the debug information for CU starting at INFO_PTR. INFO_PTR
2494 should point just after the initial uleb128 of a DIE, and the
2495 abbrev corresponding to that skipped uleb128 should be passed in
2496 ABBREV. Returns a pointer to this DIE's sibling, skipping any
2497 children. */
2498
fe1b8b76
JB
2499static gdb_byte *
2500skip_one_die (gdb_byte *info_ptr, struct abbrev_info *abbrev,
4bb7a0a7
DJ
2501 struct dwarf2_cu *cu)
2502{
2503 unsigned int bytes_read;
2504 struct attribute attr;
2505 bfd *abfd = cu->objfile->obfd;
2506 unsigned int form, i;
2507
2508 for (i = 0; i < abbrev->num_attrs; i++)
2509 {
2510 /* The only abbrev we care about is DW_AT_sibling. */
2511 if (abbrev->attrs[i].name == DW_AT_sibling)
2512 {
2513 read_attribute (&attr, &abbrev->attrs[i],
2514 abfd, info_ptr, cu);
2515 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 2516 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 2517 else
dce234bc 2518 return dwarf2_per_objfile->info.buffer
c764a876 2519 + dwarf2_get_ref_die_offset (&attr);
4bb7a0a7
DJ
2520 }
2521
2522 /* If it isn't DW_AT_sibling, skip this attribute. */
2523 form = abbrev->attrs[i].form;
2524 skip_attribute:
2525 switch (form)
2526 {
2527 case DW_FORM_addr:
2528 case DW_FORM_ref_addr:
2529 info_ptr += cu->header.addr_size;
2530 break;
2531 case DW_FORM_data1:
2532 case DW_FORM_ref1:
2533 case DW_FORM_flag:
2534 info_ptr += 1;
2535 break;
2536 case DW_FORM_data2:
2537 case DW_FORM_ref2:
2538 info_ptr += 2;
2539 break;
2540 case DW_FORM_data4:
2541 case DW_FORM_ref4:
2542 info_ptr += 4;
2543 break;
2544 case DW_FORM_data8:
2545 case DW_FORM_ref8:
2546 info_ptr += 8;
2547 break;
2548 case DW_FORM_string:
2549 read_string (abfd, info_ptr, &bytes_read);
2550 info_ptr += bytes_read;
2551 break;
2552 case DW_FORM_strp:
2553 info_ptr += cu->header.offset_size;
2554 break;
2555 case DW_FORM_block:
2556 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2557 info_ptr += bytes_read;
2558 break;
2559 case DW_FORM_block1:
2560 info_ptr += 1 + read_1_byte (abfd, info_ptr);
2561 break;
2562 case DW_FORM_block2:
2563 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
2564 break;
2565 case DW_FORM_block4:
2566 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
2567 break;
2568 case DW_FORM_sdata:
2569 case DW_FORM_udata:
2570 case DW_FORM_ref_udata:
2571 info_ptr = skip_leb128 (abfd, info_ptr);
2572 break;
2573 case DW_FORM_indirect:
2574 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2575 info_ptr += bytes_read;
2576 /* We need to continue parsing from here, so just go back to
2577 the top. */
2578 goto skip_attribute;
2579
2580 default:
8a3fe4f8 2581 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4bb7a0a7
DJ
2582 dwarf_form_name (form),
2583 bfd_get_filename (abfd));
2584 }
2585 }
2586
2587 if (abbrev->has_children)
2588 return skip_children (info_ptr, cu);
2589 else
2590 return info_ptr;
2591}
2592
2593/* Locate ORIG_PDI's sibling; INFO_PTR should point to the start of
2594 the next DIE after ORIG_PDI. */
91c24f0a 2595
fe1b8b76
JB
2596static gdb_byte *
2597locate_pdi_sibling (struct partial_die_info *orig_pdi, gdb_byte *info_ptr,
e7c27a73 2598 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
2599{
2600 /* Do we know the sibling already? */
72bf9492 2601
91c24f0a
DC
2602 if (orig_pdi->sibling)
2603 return orig_pdi->sibling;
2604
2605 /* Are there any children to deal with? */
2606
2607 if (!orig_pdi->has_children)
2608 return info_ptr;
2609
4bb7a0a7 2610 /* Skip the children the long way. */
91c24f0a 2611
4bb7a0a7 2612 return skip_children (info_ptr, cu);
91c24f0a
DC
2613}
2614
c906108c
SS
2615/* Expand this partial symbol table into a full symbol table. */
2616
2617static void
fba45db2 2618dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c
SS
2619{
2620 /* FIXME: This is barely more than a stub. */
2621 if (pst != NULL)
2622 {
2623 if (pst->readin)
2624 {
8a3fe4f8 2625 warning (_("bug: psymtab for %s is already read in."), pst->filename);
c906108c
SS
2626 }
2627 else
2628 {
2629 if (info_verbose)
2630 {
a3f17187 2631 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
c906108c
SS
2632 gdb_flush (gdb_stdout);
2633 }
2634
10b3939b
DJ
2635 /* Restore our global data. */
2636 dwarf2_per_objfile = objfile_data (pst->objfile,
2637 dwarf2_objfile_data_key);
2638
b2ab525c
KB
2639 /* If this psymtab is constructed from a debug-only objfile, the
2640 has_section_at_zero flag will not necessarily be correct. We
2641 can get the correct value for this flag by looking at the data
2642 associated with the (presumably stripped) associated objfile. */
2643 if (pst->objfile->separate_debug_objfile_backlink)
2644 {
2645 struct dwarf2_per_objfile *dpo_backlink
2646 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
2647 dwarf2_objfile_data_key);
2648 dwarf2_per_objfile->has_section_at_zero
2649 = dpo_backlink->has_section_at_zero;
2650 }
2651
c906108c
SS
2652 psymtab_to_symtab_1 (pst);
2653
2654 /* Finish up the debug error message. */
2655 if (info_verbose)
a3f17187 2656 printf_filtered (_("done.\n"));
c906108c
SS
2657 }
2658 }
2659}
2660
10b3939b
DJ
2661/* Add PER_CU to the queue. */
2662
2663static void
03dd20cc 2664queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b
DJ
2665{
2666 struct dwarf2_queue_item *item;
2667
2668 per_cu->queued = 1;
2669 item = xmalloc (sizeof (*item));
2670 item->per_cu = per_cu;
2671 item->next = NULL;
2672
2673 if (dwarf2_queue == NULL)
2674 dwarf2_queue = item;
2675 else
2676 dwarf2_queue_tail->next = item;
2677
2678 dwarf2_queue_tail = item;
03dd20cc
DJ
2679
2680 /* Either PER_CU is the CU we want to process, or we're following a reference
2681 pointing into PER_CU. Either way, we need its DIEs now. */
2682 load_full_comp_unit (item->per_cu, objfile);
2683 item->per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
2684 dwarf2_per_objfile->read_in_chain = item->per_cu;
10b3939b
DJ
2685}
2686
2687/* Process the queue. */
2688
2689static void
2690process_queue (struct objfile *objfile)
2691{
2692 struct dwarf2_queue_item *item, *next_item;
2693
03dd20cc
DJ
2694 /* The queue starts out with one item, but following a DIE reference
2695 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
2696 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
2697 {
31ffec48 2698 if (item->per_cu->psymtab && !item->per_cu->psymtab->readin)
10b3939b
DJ
2699 process_full_comp_unit (item->per_cu);
2700
2701 item->per_cu->queued = 0;
2702 next_item = item->next;
2703 xfree (item);
2704 }
2705
2706 dwarf2_queue_tail = NULL;
2707}
2708
2709/* Free all allocated queue entries. This function only releases anything if
2710 an error was thrown; if the queue was processed then it would have been
2711 freed as we went along. */
2712
2713static void
2714dwarf2_release_queue (void *dummy)
2715{
2716 struct dwarf2_queue_item *item, *last;
2717
2718 item = dwarf2_queue;
2719 while (item)
2720 {
2721 /* Anything still marked queued is likely to be in an
2722 inconsistent state, so discard it. */
2723 if (item->per_cu->queued)
2724 {
2725 if (item->per_cu->cu != NULL)
2726 free_one_cached_comp_unit (item->per_cu->cu);
2727 item->per_cu->queued = 0;
2728 }
2729
2730 last = item;
2731 item = item->next;
2732 xfree (last);
2733 }
2734
2735 dwarf2_queue = dwarf2_queue_tail = NULL;
2736}
2737
2738/* Read in full symbols for PST, and anything it depends on. */
2739
c906108c 2740static void
fba45db2 2741psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 2742{
10b3939b 2743 struct dwarf2_per_cu_data *per_cu;
c906108c 2744 struct cleanup *back_to;
aaa75496
JB
2745 int i;
2746
2747 for (i = 0; i < pst->number_of_dependencies; i++)
2748 if (!pst->dependencies[i]->readin)
2749 {
2750 /* Inform about additional files that need to be read in. */
2751 if (info_verbose)
2752 {
a3f17187 2753 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
2754 fputs_filtered (" ", gdb_stdout);
2755 wrap_here ("");
2756 fputs_filtered ("and ", gdb_stdout);
2757 wrap_here ("");
2758 printf_filtered ("%s...", pst->dependencies[i]->filename);
2759 wrap_here (""); /* Flush output */
2760 gdb_flush (gdb_stdout);
2761 }
2762 psymtab_to_symtab_1 (pst->dependencies[i]);
2763 }
2764
10b3939b
DJ
2765 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
2766
2767 if (per_cu == NULL)
aaa75496
JB
2768 {
2769 /* It's an include file, no symbols to read for it.
2770 Everything is in the parent symtab. */
2771 pst->readin = 1;
2772 return;
2773 }
c906108c 2774
10b3939b
DJ
2775 back_to = make_cleanup (dwarf2_release_queue, NULL);
2776
03dd20cc 2777 queue_comp_unit (per_cu, pst->objfile);
10b3939b
DJ
2778
2779 process_queue (pst->objfile);
2780
2781 /* Age the cache, releasing compilation units that have not
2782 been used recently. */
2783 age_cached_comp_units ();
2784
2785 do_cleanups (back_to);
2786}
2787
2788/* Load the DIEs associated with PST and PER_CU into memory. */
2789
2790static struct dwarf2_cu *
31ffec48 2791load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
10b3939b 2792{
31ffec48 2793 bfd *abfd = objfile->obfd;
10b3939b 2794 struct dwarf2_cu *cu;
c764a876 2795 unsigned int offset;
fe1b8b76 2796 gdb_byte *info_ptr;
10b3939b
DJ
2797 struct cleanup *back_to, *free_cu_cleanup;
2798 struct attribute *attr;
2799 CORE_ADDR baseaddr;
6502dd73 2800
c906108c 2801 /* Set local variables from the partial symbol table info. */
10b3939b 2802 offset = per_cu->offset;
6502dd73 2803
dce234bc 2804 info_ptr = dwarf2_per_objfile->info.buffer + offset;
63d06c5c 2805
10b3939b
DJ
2806 cu = xmalloc (sizeof (struct dwarf2_cu));
2807 memset (cu, 0, sizeof (struct dwarf2_cu));
c906108c 2808
10b3939b
DJ
2809 /* If an error occurs while loading, release our storage. */
2810 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
c906108c 2811
31ffec48 2812 cu->objfile = objfile;
e7c27a73 2813
c906108c 2814 /* read in the comp_unit header */
10b3939b 2815 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
c906108c
SS
2816
2817 /* Read the abbrevs for this compilation unit */
10b3939b
DJ
2818 dwarf2_read_abbrevs (abfd, cu);
2819 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
2820
2821 cu->header.offset = offset;
c906108c 2822
10b3939b
DJ
2823 cu->per_cu = per_cu;
2824 per_cu->cu = cu;
f792889a 2825 cu->type_hash = per_cu->type_hash;
e142c38c 2826
10b3939b
DJ
2827 /* We use this obstack for block values in dwarf_alloc_block. */
2828 obstack_init (&cu->comp_unit_obstack);
2829
2830 cu->dies = read_comp_unit (info_ptr, abfd, cu);
2831
2832 /* We try not to read any attributes in this function, because not
2833 all objfiles needed for references have been loaded yet, and symbol
2834 table processing isn't initialized. But we have to set the CU language,
2835 or we won't be able to build types correctly. */
2836 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
2837 if (attr)
2838 set_cu_language (DW_UNSND (attr), cu);
2839 else
2840 set_cu_language (language_minimal, cu);
2841
2842 do_cleanups (back_to);
e142c38c 2843
10b3939b
DJ
2844 /* We've successfully allocated this compilation unit. Let our caller
2845 clean it up when finished with it. */
2846 discard_cleanups (free_cu_cleanup);
c906108c 2847
10b3939b
DJ
2848 return cu;
2849}
2850
2851/* Generate full symbol information for PST and CU, whose DIEs have
2852 already been loaded into memory. */
2853
2854static void
2855process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
2856{
2857 struct partial_symtab *pst = per_cu->psymtab;
2858 struct dwarf2_cu *cu = per_cu->cu;
2859 struct objfile *objfile = pst->objfile;
2860 bfd *abfd = objfile->obfd;
2861 CORE_ADDR lowpc, highpc;
2862 struct symtab *symtab;
2863 struct cleanup *back_to;
2864 struct attribute *attr;
2865 CORE_ADDR baseaddr;
2866
2867 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2868
10b3939b
DJ
2869 buildsym_init ();
2870 back_to = make_cleanup (really_free_pendings, NULL);
2871
2872 cu->list_in_scope = &file_symbols;
c906108c 2873
0d53c4c4
DJ
2874 /* Find the base address of the compilation unit for range lists and
2875 location lists. It will normally be specified by DW_AT_low_pc.
2876 In DWARF-3 draft 4, the base address could be overridden by
2877 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2878 compilation units with discontinuous ranges. */
2879
d00adf39
DE
2880 cu->base_known = 0;
2881 cu->base_address = 0;
0d53c4c4 2882
10b3939b 2883 attr = dwarf2_attr (cu->dies, DW_AT_entry_pc, cu);
0d53c4c4
DJ
2884 if (attr)
2885 {
d00adf39
DE
2886 cu->base_address = DW_ADDR (attr);
2887 cu->base_known = 1;
0d53c4c4
DJ
2888 }
2889 else
2890 {
10b3939b 2891 attr = dwarf2_attr (cu->dies, DW_AT_low_pc, cu);
0d53c4c4
DJ
2892 if (attr)
2893 {
d00adf39
DE
2894 cu->base_address = DW_ADDR (attr);
2895 cu->base_known = 1;
0d53c4c4
DJ
2896 }
2897 }
2898
c906108c 2899 /* Do line number decoding in read_file_scope () */
10b3939b 2900 process_die (cu->dies, cu);
c906108c 2901
fae299cd
DC
2902 /* Some compilers don't define a DW_AT_high_pc attribute for the
2903 compilation unit. If the DW_AT_high_pc is missing, synthesize
2904 it, by scanning the DIE's below the compilation unit. */
10b3939b 2905 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 2906
613e1657 2907 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
2908
2909 /* Set symtab language to language from DW_AT_language.
2910 If the compilation is from a C file generated by language preprocessors,
2911 do not set the language if it was already deduced by start_subfile. */
2912 if (symtab != NULL
10b3939b 2913 && !(cu->language == language_c && symtab->language != language_c))
c906108c 2914 {
10b3939b 2915 symtab->language = cu->language;
c906108c
SS
2916 }
2917 pst->symtab = symtab;
2918 pst->readin = 1;
c906108c
SS
2919
2920 do_cleanups (back_to);
2921}
2922
2923/* Process a die and its children. */
2924
2925static void
e7c27a73 2926process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
2927{
2928 switch (die->tag)
2929 {
2930 case DW_TAG_padding:
2931 break;
2932 case DW_TAG_compile_unit:
e7c27a73 2933 read_file_scope (die, cu);
c906108c
SS
2934 break;
2935 case DW_TAG_subprogram:
c906108c 2936 case DW_TAG_inlined_subroutine:
edb3359d 2937 read_func_scope (die, cu);
c906108c
SS
2938 break;
2939 case DW_TAG_lexical_block:
14898363
L
2940 case DW_TAG_try_block:
2941 case DW_TAG_catch_block:
e7c27a73 2942 read_lexical_block_scope (die, cu);
c906108c
SS
2943 break;
2944 case DW_TAG_class_type:
680b30c7 2945 case DW_TAG_interface_type:
c906108c
SS
2946 case DW_TAG_structure_type:
2947 case DW_TAG_union_type:
134d01f1 2948 process_structure_scope (die, cu);
c906108c
SS
2949 break;
2950 case DW_TAG_enumeration_type:
134d01f1 2951 process_enumeration_scope (die, cu);
c906108c 2952 break;
134d01f1 2953
f792889a
DJ
2954 /* These dies have a type, but processing them does not create
2955 a symbol or recurse to process the children. Therefore we can
2956 read them on-demand through read_type_die. */
c906108c 2957 case DW_TAG_subroutine_type:
72019c9c 2958 case DW_TAG_set_type:
c906108c 2959 case DW_TAG_array_type:
c906108c 2960 case DW_TAG_pointer_type:
c906108c 2961 case DW_TAG_ptr_to_member_type:
c906108c 2962 case DW_TAG_reference_type:
c906108c 2963 case DW_TAG_string_type:
c906108c 2964 break;
134d01f1 2965
c906108c 2966 case DW_TAG_base_type:
a02abb62 2967 case DW_TAG_subrange_type:
cb249c71 2968 case DW_TAG_typedef:
134d01f1
DJ
2969 /* Add a typedef symbol for the type definition, if it has a
2970 DW_AT_name. */
f792889a 2971 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 2972 break;
c906108c 2973 case DW_TAG_common_block:
e7c27a73 2974 read_common_block (die, cu);
c906108c
SS
2975 break;
2976 case DW_TAG_common_inclusion:
2977 break;
d9fa45fe 2978 case DW_TAG_namespace:
63d06c5c 2979 processing_has_namespace_info = 1;
e7c27a73 2980 read_namespace (die, cu);
d9fa45fe 2981 break;
5d7cb8df
JK
2982 case DW_TAG_module:
2983 read_module (die, cu);
2984 break;
d9fa45fe
DC
2985 case DW_TAG_imported_declaration:
2986 case DW_TAG_imported_module:
63d06c5c 2987 processing_has_namespace_info = 1;
27aa8d6a
SW
2988 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
2989 || cu->language != language_fortran))
2990 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
2991 dwarf_tag_name (die->tag));
2992 read_import_statement (die, cu);
d9fa45fe 2993 break;
c906108c 2994 default:
e7c27a73 2995 new_symbol (die, NULL, cu);
c906108c
SS
2996 break;
2997 }
2998}
2999
0114d602
DJ
3000/* Return the fully qualified name of DIE, based on its DW_AT_name.
3001 If scope qualifiers are appropriate they will be added. The result
3002 will be allocated on the objfile_obstack, or NULL if the DIE does
3003 not have a name. */
3004
3005static const char *
3006dwarf2_full_name (struct die_info *die, struct dwarf2_cu *cu)
3007{
3008 struct attribute *attr;
3009 char *prefix, *name;
3010 struct ui_file *buf = NULL;
3011
3012 name = dwarf2_name (die, cu);
3013 if (!name)
3014 return NULL;
3015
3016 /* These are the only languages we know how to qualify names in. */
3017 if (cu->language != language_cplus
3018 && cu->language != language_java)
3019 return name;
3020
3021 /* If no prefix is necessary for this type of DIE, return the
3022 unqualified name. The other three tags listed could be handled
3023 in pdi_needs_namespace, but that requires broader changes. */
3024 if (!pdi_needs_namespace (die->tag)
3025 && die->tag != DW_TAG_subprogram
3026 && die->tag != DW_TAG_variable
3027 && die->tag != DW_TAG_member)
3028 return name;
3029
3030 prefix = determine_prefix (die, cu);
3031 if (*prefix != '\0')
3032 name = typename_concat (&cu->objfile->objfile_obstack, prefix,
3033 name, cu);
3034
3035 return name;
3036}
3037
27aa8d6a
SW
3038/* Read the import statement specified by the given die and record it. */
3039
3040static void
3041read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
3042{
3043 struct attribute *import_attr;
3044 struct die_info *imported_die;
3045 const char *imported_name;
794684b6
SW
3046 const char *imported_name_prefix;
3047 const char *import_prefix;
3048 char *canonical_name;
27aa8d6a
SW
3049
3050 import_attr = dwarf2_attr (die, DW_AT_import, cu);
3051 if (import_attr == NULL)
3052 {
3053 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
3054 dwarf_tag_name (die->tag));
3055 return;
3056 }
3057
3058 imported_die = follow_die_ref (die, import_attr, &cu);
3059 imported_name = dwarf2_name (imported_die, cu);
3060 if (imported_name == NULL)
3061 {
3062 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
3063
3064 The import in the following code:
3065 namespace A
3066 {
3067 typedef int B;
3068 }
3069
3070 int main ()
3071 {
3072 using A::B;
3073 B b;
3074 return b;
3075 }
3076
3077 ...
3078 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
3079 <52> DW_AT_decl_file : 1
3080 <53> DW_AT_decl_line : 6
3081 <54> DW_AT_import : <0x75>
3082 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
3083 <59> DW_AT_name : B
3084 <5b> DW_AT_decl_file : 1
3085 <5c> DW_AT_decl_line : 2
3086 <5d> DW_AT_type : <0x6e>
3087 ...
3088 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
3089 <76> DW_AT_byte_size : 4
3090 <77> DW_AT_encoding : 5 (signed)
3091
3092 imports the wrong die ( 0x75 instead of 0x58 ).
3093 This case will be ignored until the gcc bug is fixed. */
3094 return;
3095 }
3096
3097 /* FIXME: dwarf2_name (die); for the local name after import. */
3098
794684b6
SW
3099 /* Figure out where the statement is being imported to. */
3100 import_prefix = determine_prefix (die, cu);
3101
3102 /* Figure out what the scope of the imported die is and prepend it
3103 to the name of the imported die. */
3104 imported_name_prefix = determine_prefix (imported_die, cu);
3105
3106 if (strlen (imported_name_prefix) > 0)
3107 {
3108 canonical_name = alloca (strlen (imported_name_prefix) + 2 + strlen (imported_name) + 1);
3109 strcpy (canonical_name, imported_name_prefix);
3110 strcat (canonical_name, "::");
3111 strcat (canonical_name, imported_name);
3112 }
3113 else
3114 {
3115 canonical_name = alloca (strlen (imported_name) + 1);
3116 strcpy (canonical_name, imported_name);
3117 }
3118
3119 using_directives = cp_add_using (import_prefix,canonical_name, using_directives);
27aa8d6a
SW
3120}
3121
5fb290d7 3122static void
e142c38c 3123initialize_cu_func_list (struct dwarf2_cu *cu)
5fb290d7 3124{
e142c38c 3125 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5fb290d7
DJ
3126}
3127
cb1df416
DJ
3128static void
3129free_cu_line_header (void *arg)
3130{
3131 struct dwarf2_cu *cu = arg;
3132
3133 free_line_header (cu->line_header);
3134 cu->line_header = NULL;
3135}
3136
c906108c 3137static void
e7c27a73 3138read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3139{
e7c27a73
DJ
3140 struct objfile *objfile = cu->objfile;
3141 struct comp_unit_head *cu_header = &cu->header;
debd256d 3142 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 3143 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
3144 CORE_ADDR highpc = ((CORE_ADDR) 0);
3145 struct attribute *attr;
e1024ff1 3146 char *name = NULL;
c906108c
SS
3147 char *comp_dir = NULL;
3148 struct die_info *child_die;
3149 bfd *abfd = objfile->obfd;
debd256d 3150 struct line_header *line_header = 0;
e142c38c
DJ
3151 CORE_ADDR baseaddr;
3152
3153 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 3154
fae299cd 3155 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
3156
3157 /* If we didn't find a lowpc, set it to highpc to avoid complaints
3158 from finish_block. */
2acceee2 3159 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
3160 lowpc = highpc;
3161 lowpc += baseaddr;
3162 highpc += baseaddr;
3163
39cbfefa
DJ
3164 /* Find the filename. Do not use dwarf2_name here, since the filename
3165 is not a source language identifier. */
e142c38c 3166 attr = dwarf2_attr (die, DW_AT_name, cu);
c906108c
SS
3167 if (attr)
3168 {
3169 name = DW_STRING (attr);
3170 }
e1024ff1 3171
e142c38c 3172 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
c906108c 3173 if (attr)
e1024ff1
DJ
3174 comp_dir = DW_STRING (attr);
3175 else if (name != NULL && IS_ABSOLUTE_PATH (name))
c906108c 3176 {
e1024ff1
DJ
3177 comp_dir = ldirname (name);
3178 if (comp_dir != NULL)
3179 make_cleanup (xfree, comp_dir);
3180 }
3181 if (comp_dir != NULL)
3182 {
3183 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3184 directory, get rid of it. */
3185 char *cp = strchr (comp_dir, ':');
c906108c 3186
e1024ff1
DJ
3187 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
3188 comp_dir = cp + 1;
c906108c
SS
3189 }
3190
e1024ff1
DJ
3191 if (name == NULL)
3192 name = "<unknown>";
3193
e142c38c 3194 attr = dwarf2_attr (die, DW_AT_language, cu);
c906108c
SS
3195 if (attr)
3196 {
e142c38c 3197 set_cu_language (DW_UNSND (attr), cu);
c906108c
SS
3198 }
3199
b0f35d58
DL
3200 attr = dwarf2_attr (die, DW_AT_producer, cu);
3201 if (attr)
3202 cu->producer = DW_STRING (attr);
303b6f5d 3203
c906108c
SS
3204 /* We assume that we're processing GCC output. */
3205 processing_gcc_compilation = 2;
c906108c 3206
df8a16a1
DJ
3207 processing_has_namespace_info = 0;
3208
c906108c
SS
3209 start_symtab (name, comp_dir, lowpc);
3210 record_debugformat ("DWARF 2");
303b6f5d 3211 record_producer (cu->producer);
c906108c 3212
e142c38c 3213 initialize_cu_func_list (cu);
c906108c 3214
cb1df416
DJ
3215 /* Decode line number information if present. We do this before
3216 processing child DIEs, so that the line header table is available
3217 for DW_AT_decl_file. */
e142c38c 3218 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5fb290d7
DJ
3219 if (attr)
3220 {
debd256d 3221 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 3222 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
3223 if (line_header)
3224 {
cb1df416
DJ
3225 cu->line_header = line_header;
3226 make_cleanup (free_cu_line_header, cu);
aaa75496 3227 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
debd256d 3228 }
5fb290d7 3229 }
debd256d 3230
cb1df416
DJ
3231 /* Process all dies in compilation unit. */
3232 if (die->child != NULL)
3233 {
3234 child_die = die->child;
3235 while (child_die && child_die->tag)
3236 {
3237 process_die (child_die, cu);
3238 child_die = sibling_die (child_die);
3239 }
3240 }
3241
2e276125
JB
3242 /* Decode macro information, if present. Dwarf 2 macro information
3243 refers to information in the line number info statement program
3244 header, so we can only read it if we've read the header
3245 successfully. */
e142c38c 3246 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
41ff2da1 3247 if (attr && line_header)
2e276125
JB
3248 {
3249 unsigned int macro_offset = DW_UNSND (attr);
3250 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 3251 comp_dir, abfd, cu);
2e276125 3252 }
debd256d 3253 do_cleanups (back_to);
5fb290d7
DJ
3254}
3255
3256static void
e142c38c
DJ
3257add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
3258 struct dwarf2_cu *cu)
5fb290d7
DJ
3259{
3260 struct function_range *thisfn;
3261
3262 thisfn = (struct function_range *)
7b5a2f43 3263 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5fb290d7
DJ
3264 thisfn->name = name;
3265 thisfn->lowpc = lowpc;
3266 thisfn->highpc = highpc;
3267 thisfn->seen_line = 0;
3268 thisfn->next = NULL;
3269
e142c38c
DJ
3270 if (cu->last_fn == NULL)
3271 cu->first_fn = thisfn;
5fb290d7 3272 else
e142c38c 3273 cu->last_fn->next = thisfn;
5fb290d7 3274
e142c38c 3275 cu->last_fn = thisfn;
c906108c
SS
3276}
3277
d389af10
JK
3278/* qsort helper for inherit_abstract_dies. */
3279
3280static int
3281unsigned_int_compar (const void *ap, const void *bp)
3282{
3283 unsigned int a = *(unsigned int *) ap;
3284 unsigned int b = *(unsigned int *) bp;
3285
3286 return (a > b) - (b > a);
3287}
3288
3289/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3290 Inherit only the children of the DW_AT_abstract_origin DIE not being already
3291 referenced by DW_AT_abstract_origin from the children of the current DIE. */
3292
3293static void
3294inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
3295{
3296 struct die_info *child_die;
3297 unsigned die_children_count;
3298 /* CU offsets which were referenced by children of the current DIE. */
3299 unsigned *offsets;
3300 unsigned *offsets_end, *offsetp;
3301 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
3302 struct die_info *origin_die;
3303 /* Iterator of the ORIGIN_DIE children. */
3304 struct die_info *origin_child_die;
3305 struct cleanup *cleanups;
3306 struct attribute *attr;
3307
3308 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
3309 if (!attr)
3310 return;
3311
3312 origin_die = follow_die_ref (die, attr, &cu);
edb3359d
DJ
3313 if (die->tag != origin_die->tag
3314 && !(die->tag == DW_TAG_inlined_subroutine
3315 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
3316 complaint (&symfile_complaints,
3317 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
3318 die->offset, origin_die->offset);
3319
3320 child_die = die->child;
3321 die_children_count = 0;
3322 while (child_die && child_die->tag)
3323 {
3324 child_die = sibling_die (child_die);
3325 die_children_count++;
3326 }
3327 offsets = xmalloc (sizeof (*offsets) * die_children_count);
3328 cleanups = make_cleanup (xfree, offsets);
3329
3330 offsets_end = offsets;
3331 child_die = die->child;
3332 while (child_die && child_die->tag)
3333 {
3334 attr = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
3335 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
3336 counterpart may exist. */
3337 if (attr)
3338 {
3339 struct die_info *child_origin_die;
3340
3341 child_origin_die = follow_die_ref (child_die, attr, &cu);
edb3359d
DJ
3342 if (child_die->tag != child_origin_die->tag
3343 && !(child_die->tag == DW_TAG_inlined_subroutine
3344 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
3345 complaint (&symfile_complaints,
3346 _("Child DIE 0x%x and its abstract origin 0x%x have "
3347 "different tags"), child_die->offset,
3348 child_origin_die->offset);
3349 *offsets_end++ = child_origin_die->offset;
3350 }
3351 child_die = sibling_die (child_die);
3352 }
3353 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
3354 unsigned_int_compar);
3355 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
3356 if (offsetp[-1] == *offsetp)
3357 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
3358 "to DIE 0x%x as their abstract origin"),
3359 die->offset, *offsetp);
3360
3361 offsetp = offsets;
3362 origin_child_die = origin_die->child;
3363 while (origin_child_die && origin_child_die->tag)
3364 {
3365 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
3366 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
3367 offsetp++;
3368 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
3369 {
3370 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
3371 process_die (origin_child_die, cu);
3372 }
3373 origin_child_die = sibling_die (origin_child_die);
3374 }
3375
3376 do_cleanups (cleanups);
3377}
3378
c906108c 3379static void
e7c27a73 3380read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3381{
e7c27a73 3382 struct objfile *objfile = cu->objfile;
52f0bd74 3383 struct context_stack *new;
c906108c
SS
3384 CORE_ADDR lowpc;
3385 CORE_ADDR highpc;
3386 struct die_info *child_die;
edb3359d 3387 struct attribute *attr, *call_line, *call_file;
c906108c 3388 char *name;
e142c38c 3389 CORE_ADDR baseaddr;
801e3a5b 3390 struct block *block;
edb3359d
DJ
3391 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
3392
3393 if (inlined_func)
3394 {
3395 /* If we do not have call site information, we can't show the
3396 caller of this inlined function. That's too confusing, so
3397 only use the scope for local variables. */
3398 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
3399 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
3400 if (call_line == NULL || call_file == NULL)
3401 {
3402 read_lexical_block_scope (die, cu);
3403 return;
3404 }
3405 }
c906108c 3406
e142c38c
DJ
3407 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3408
3409 name = dwarf2_linkage_name (die, cu);
c906108c
SS
3410
3411 /* Ignore functions with missing or empty names and functions with
3412 missing or invalid low and high pc attributes. */
e7c27a73 3413 if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
c906108c
SS
3414 return;
3415
3416 lowpc += baseaddr;
3417 highpc += baseaddr;
3418
5fb290d7 3419 /* Record the function range for dwarf_decode_lines. */
e142c38c 3420 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 3421
c906108c 3422 new = push_context (0, lowpc);
f792889a 3423 new->name = new_symbol (die, read_type_die (die, cu), cu);
4c2df51b 3424
4cecd739
DJ
3425 /* If there is a location expression for DW_AT_frame_base, record
3426 it. */
e142c38c 3427 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 3428 if (attr)
c034e007
AC
3429 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
3430 expression is being recorded directly in the function's symbol
3431 and not in a separate frame-base object. I guess this hack is
3432 to avoid adding some sort of frame-base adjunct/annex to the
3433 function's symbol :-(. The problem with doing this is that it
3434 results in a function symbol with a location expression that
3435 has nothing to do with the location of the function, ouch! The
3436 relationship should be: a function's symbol has-a frame base; a
3437 frame-base has-a location expression. */
e7c27a73 3438 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 3439
e142c38c 3440 cu->list_in_scope = &local_symbols;
c906108c 3441
639d11d3 3442 if (die->child != NULL)
c906108c 3443 {
639d11d3 3444 child_die = die->child;
c906108c
SS
3445 while (child_die && child_die->tag)
3446 {
e7c27a73 3447 process_die (child_die, cu);
c906108c
SS
3448 child_die = sibling_die (child_die);
3449 }
3450 }
3451
d389af10
JK
3452 inherit_abstract_dies (die, cu);
3453
c906108c
SS
3454 new = pop_context ();
3455 /* Make a block for the local symbols within. */
801e3a5b
JB
3456 block = finish_block (new->name, &local_symbols, new->old_blocks,
3457 lowpc, highpc, objfile);
3458
df8a16a1
DJ
3459 /* For C++, set the block's scope. */
3460 if (cu->language == language_cplus)
3461 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 3462 determine_prefix (die, cu),
df8a16a1
DJ
3463 processing_has_namespace_info);
3464
801e3a5b
JB
3465 /* If we have address ranges, record them. */
3466 dwarf2_record_block_ranges (die, block, baseaddr, cu);
208d8187
JB
3467
3468 /* In C++, we can have functions nested inside functions (e.g., when
3469 a function declares a class that has methods). This means that
3470 when we finish processing a function scope, we may need to go
3471 back to building a containing block's symbol lists. */
3472 local_symbols = new->locals;
3473 param_symbols = new->params;
27aa8d6a 3474 using_directives = new->using_directives;
208d8187 3475
921e78cf
JB
3476 /* If we've finished processing a top-level function, subsequent
3477 symbols go in the file symbol list. */
3478 if (outermost_context_p ())
e142c38c 3479 cu->list_in_scope = &file_symbols;
c906108c
SS
3480}
3481
3482/* Process all the DIES contained within a lexical block scope. Start
3483 a new scope, process the dies, and then close the scope. */
3484
3485static void
e7c27a73 3486read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3487{
e7c27a73 3488 struct objfile *objfile = cu->objfile;
52f0bd74 3489 struct context_stack *new;
c906108c
SS
3490 CORE_ADDR lowpc, highpc;
3491 struct die_info *child_die;
e142c38c
DJ
3492 CORE_ADDR baseaddr;
3493
3494 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
3495
3496 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
3497 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
3498 as multiple lexical blocks? Handling children in a sane way would
3499 be nasty. Might be easier to properly extend generic blocks to
3500 describe ranges. */
e7c27a73 3501 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
c906108c
SS
3502 return;
3503 lowpc += baseaddr;
3504 highpc += baseaddr;
3505
3506 push_context (0, lowpc);
639d11d3 3507 if (die->child != NULL)
c906108c 3508 {
639d11d3 3509 child_die = die->child;
c906108c
SS
3510 while (child_die && child_die->tag)
3511 {
e7c27a73 3512 process_die (child_die, cu);
c906108c
SS
3513 child_die = sibling_die (child_die);
3514 }
3515 }
3516 new = pop_context ();
3517
3518 if (local_symbols != NULL)
3519 {
801e3a5b
JB
3520 struct block *block
3521 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
3522 highpc, objfile);
3523
3524 /* Note that recording ranges after traversing children, as we
3525 do here, means that recording a parent's ranges entails
3526 walking across all its children's ranges as they appear in
3527 the address map, which is quadratic behavior.
3528
3529 It would be nicer to record the parent's ranges before
3530 traversing its children, simply overriding whatever you find
3531 there. But since we don't even decide whether to create a
3532 block until after we've traversed its children, that's hard
3533 to do. */
3534 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
3535 }
3536 local_symbols = new->locals;
27aa8d6a 3537 using_directives = new->using_directives;
c906108c
SS
3538}
3539
43039443 3540/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
3541 Return 1 if the attributes are present and valid, otherwise, return 0.
3542 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
3543
3544static int
3545dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
3546 CORE_ADDR *high_return, struct dwarf2_cu *cu,
3547 struct partial_symtab *ranges_pst)
43039443
JK
3548{
3549 struct objfile *objfile = cu->objfile;
3550 struct comp_unit_head *cu_header = &cu->header;
3551 bfd *obfd = objfile->obfd;
3552 unsigned int addr_size = cu_header->addr_size;
3553 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
3554 /* Base address selection entry. */
3555 CORE_ADDR base;
3556 int found_base;
3557 unsigned int dummy;
3558 gdb_byte *buffer;
3559 CORE_ADDR marker;
3560 int low_set;
3561 CORE_ADDR low = 0;
3562 CORE_ADDR high = 0;
ff013f42 3563 CORE_ADDR baseaddr;
43039443 3564
d00adf39
DE
3565 found_base = cu->base_known;
3566 base = cu->base_address;
43039443 3567
dce234bc 3568 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
3569 {
3570 complaint (&symfile_complaints,
3571 _("Offset %d out of bounds for DW_AT_ranges attribute"),
3572 offset);
3573 return 0;
3574 }
dce234bc 3575 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
3576
3577 /* Read in the largest possible address. */
3578 marker = read_address (obfd, buffer, cu, &dummy);
3579 if ((marker & mask) == mask)
3580 {
3581 /* If we found the largest possible address, then
3582 read the base address. */
3583 base = read_address (obfd, buffer + addr_size, cu, &dummy);
3584 buffer += 2 * addr_size;
3585 offset += 2 * addr_size;
3586 found_base = 1;
3587 }
3588
3589 low_set = 0;
3590
e7030f15 3591 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 3592
43039443
JK
3593 while (1)
3594 {
3595 CORE_ADDR range_beginning, range_end;
3596
3597 range_beginning = read_address (obfd, buffer, cu, &dummy);
3598 buffer += addr_size;
3599 range_end = read_address (obfd, buffer, cu, &dummy);
3600 buffer += addr_size;
3601 offset += 2 * addr_size;
3602
3603 /* An end of list marker is a pair of zero addresses. */
3604 if (range_beginning == 0 && range_end == 0)
3605 /* Found the end of list entry. */
3606 break;
3607
3608 /* Each base address selection entry is a pair of 2 values.
3609 The first is the largest possible address, the second is
3610 the base address. Check for a base address here. */
3611 if ((range_beginning & mask) == mask)
3612 {
3613 /* If we found the largest possible address, then
3614 read the base address. */
3615 base = read_address (obfd, buffer + addr_size, cu, &dummy);
3616 found_base = 1;
3617 continue;
3618 }
3619
3620 if (!found_base)
3621 {
3622 /* We have no valid base address for the ranges
3623 data. */
3624 complaint (&symfile_complaints,
3625 _("Invalid .debug_ranges data (no base address)"));
3626 return 0;
3627 }
3628
3629 range_beginning += base;
3630 range_end += base;
3631
ff013f42
JK
3632 if (ranges_pst != NULL && range_beginning < range_end)
3633 addrmap_set_empty (objfile->psymtabs_addrmap,
3634 range_beginning + baseaddr, range_end - 1 + baseaddr,
3635 ranges_pst);
3636
43039443
JK
3637 /* FIXME: This is recording everything as a low-high
3638 segment of consecutive addresses. We should have a
3639 data structure for discontiguous block ranges
3640 instead. */
3641 if (! low_set)
3642 {
3643 low = range_beginning;
3644 high = range_end;
3645 low_set = 1;
3646 }
3647 else
3648 {
3649 if (range_beginning < low)
3650 low = range_beginning;
3651 if (range_end > high)
3652 high = range_end;
3653 }
3654 }
3655
3656 if (! low_set)
3657 /* If the first entry is an end-of-list marker, the range
3658 describes an empty scope, i.e. no instructions. */
3659 return 0;
3660
3661 if (low_return)
3662 *low_return = low;
3663 if (high_return)
3664 *high_return = high;
3665 return 1;
3666}
3667
af34e669
DJ
3668/* Get low and high pc attributes from a die. Return 1 if the attributes
3669 are present and valid, otherwise, return 0. Return -1 if the range is
3670 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 3671static int
af34e669 3672dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
e7c27a73 3673 CORE_ADDR *highpc, struct dwarf2_cu *cu)
c906108c
SS
3674{
3675 struct attribute *attr;
af34e669
DJ
3676 CORE_ADDR low = 0;
3677 CORE_ADDR high = 0;
3678 int ret = 0;
c906108c 3679
e142c38c 3680 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 3681 if (attr)
af34e669
DJ
3682 {
3683 high = DW_ADDR (attr);
e142c38c 3684 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
3685 if (attr)
3686 low = DW_ADDR (attr);
3687 else
3688 /* Found high w/o low attribute. */
3689 return 0;
3690
3691 /* Found consecutive range of addresses. */
3692 ret = 1;
3693 }
c906108c 3694 else
af34e669 3695 {
e142c38c 3696 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
3697 if (attr != NULL)
3698 {
af34e669 3699 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 3700 .debug_ranges section. */
ff013f42 3701 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, NULL))
af34e669 3702 return 0;
43039443 3703 /* Found discontinuous range of addresses. */
af34e669
DJ
3704 ret = -1;
3705 }
3706 }
c906108c
SS
3707
3708 if (high < low)
3709 return 0;
3710
3711 /* When using the GNU linker, .gnu.linkonce. sections are used to
3712 eliminate duplicate copies of functions and vtables and such.
3713 The linker will arbitrarily choose one and discard the others.
3714 The AT_*_pc values for such functions refer to local labels in
3715 these sections. If the section from that file was discarded, the
3716 labels are not in the output, so the relocs get a value of 0.
3717 If this is a discarded function, mark the pc bounds as invalid,
3718 so that GDB will ignore it. */
72dca2f5 3719 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
3720 return 0;
3721
3722 *lowpc = low;
3723 *highpc = high;
af34e669 3724 return ret;
c906108c
SS
3725}
3726
b084d499
JB
3727/* Assuming that DIE represents a subprogram DIE or a lexical block, get
3728 its low and high PC addresses. Do nothing if these addresses could not
3729 be determined. Otherwise, set LOWPC to the low address if it is smaller,
3730 and HIGHPC to the high address if greater than HIGHPC. */
3731
3732static void
3733dwarf2_get_subprogram_pc_bounds (struct die_info *die,
3734 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3735 struct dwarf2_cu *cu)
3736{
3737 CORE_ADDR low, high;
3738 struct die_info *child = die->child;
3739
3740 if (dwarf2_get_pc_bounds (die, &low, &high, cu))
3741 {
3742 *lowpc = min (*lowpc, low);
3743 *highpc = max (*highpc, high);
3744 }
3745
3746 /* If the language does not allow nested subprograms (either inside
3747 subprograms or lexical blocks), we're done. */
3748 if (cu->language != language_ada)
3749 return;
3750
3751 /* Check all the children of the given DIE. If it contains nested
3752 subprograms, then check their pc bounds. Likewise, we need to
3753 check lexical blocks as well, as they may also contain subprogram
3754 definitions. */
3755 while (child && child->tag)
3756 {
3757 if (child->tag == DW_TAG_subprogram
3758 || child->tag == DW_TAG_lexical_block)
3759 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
3760 child = sibling_die (child);
3761 }
3762}
3763
fae299cd
DC
3764/* Get the low and high pc's represented by the scope DIE, and store
3765 them in *LOWPC and *HIGHPC. If the correct values can't be
3766 determined, set *LOWPC to -1 and *HIGHPC to 0. */
3767
3768static void
3769get_scope_pc_bounds (struct die_info *die,
3770 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3771 struct dwarf2_cu *cu)
3772{
3773 CORE_ADDR best_low = (CORE_ADDR) -1;
3774 CORE_ADDR best_high = (CORE_ADDR) 0;
3775 CORE_ADDR current_low, current_high;
3776
3777 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu))
3778 {
3779 best_low = current_low;
3780 best_high = current_high;
3781 }
3782 else
3783 {
3784 struct die_info *child = die->child;
3785
3786 while (child && child->tag)
3787 {
3788 switch (child->tag) {
3789 case DW_TAG_subprogram:
b084d499 3790 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
3791 break;
3792 case DW_TAG_namespace:
3793 /* FIXME: carlton/2004-01-16: Should we do this for
3794 DW_TAG_class_type/DW_TAG_structure_type, too? I think
3795 that current GCC's always emit the DIEs corresponding
3796 to definitions of methods of classes as children of a
3797 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
3798 the DIEs giving the declarations, which could be
3799 anywhere). But I don't see any reason why the
3800 standards says that they have to be there. */
3801 get_scope_pc_bounds (child, &current_low, &current_high, cu);
3802
3803 if (current_low != ((CORE_ADDR) -1))
3804 {
3805 best_low = min (best_low, current_low);
3806 best_high = max (best_high, current_high);
3807 }
3808 break;
3809 default:
3810 /* Ignore. */
3811 break;
3812 }
3813
3814 child = sibling_die (child);
3815 }
3816 }
3817
3818 *lowpc = best_low;
3819 *highpc = best_high;
3820}
3821
801e3a5b
JB
3822/* Record the address ranges for BLOCK, offset by BASEADDR, as given
3823 in DIE. */
3824static void
3825dwarf2_record_block_ranges (struct die_info *die, struct block *block,
3826 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
3827{
3828 struct attribute *attr;
3829
3830 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
3831 if (attr)
3832 {
3833 CORE_ADDR high = DW_ADDR (attr);
3834 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3835 if (attr)
3836 {
3837 CORE_ADDR low = DW_ADDR (attr);
3838 record_block_range (block, baseaddr + low, baseaddr + high - 1);
3839 }
3840 }
3841
3842 attr = dwarf2_attr (die, DW_AT_ranges, cu);
3843 if (attr)
3844 {
3845 bfd *obfd = cu->objfile->obfd;
3846
3847 /* The value of the DW_AT_ranges attribute is the offset of the
3848 address range list in the .debug_ranges section. */
3849 unsigned long offset = DW_UNSND (attr);
dce234bc 3850 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
3851
3852 /* For some target architectures, but not others, the
3853 read_address function sign-extends the addresses it returns.
3854 To recognize base address selection entries, we need a
3855 mask. */
3856 unsigned int addr_size = cu->header.addr_size;
3857 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
3858
3859 /* The base address, to which the next pair is relative. Note
3860 that this 'base' is a DWARF concept: most entries in a range
3861 list are relative, to reduce the number of relocs against the
3862 debugging information. This is separate from this function's
3863 'baseaddr' argument, which GDB uses to relocate debugging
3864 information from a shared library based on the address at
3865 which the library was loaded. */
d00adf39
DE
3866 CORE_ADDR base = cu->base_address;
3867 int base_known = cu->base_known;
801e3a5b 3868
dce234bc 3869 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
3870 {
3871 complaint (&symfile_complaints,
3872 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
3873 offset);
3874 return;
3875 }
3876
3877 for (;;)
3878 {
3879 unsigned int bytes_read;
3880 CORE_ADDR start, end;
3881
3882 start = read_address (obfd, buffer, cu, &bytes_read);
3883 buffer += bytes_read;
3884 end = read_address (obfd, buffer, cu, &bytes_read);
3885 buffer += bytes_read;
3886
3887 /* Did we find the end of the range list? */
3888 if (start == 0 && end == 0)
3889 break;
3890
3891 /* Did we find a base address selection entry? */
3892 else if ((start & base_select_mask) == base_select_mask)
3893 {
3894 base = end;
3895 base_known = 1;
3896 }
3897
3898 /* We found an ordinary address range. */
3899 else
3900 {
3901 if (!base_known)
3902 {
3903 complaint (&symfile_complaints,
3904 _("Invalid .debug_ranges data (no base address)"));
3905 return;
3906 }
3907
3908 record_block_range (block,
3909 baseaddr + base + start,
3910 baseaddr + base + end - 1);
3911 }
3912 }
3913 }
3914}
3915
c906108c
SS
3916/* Add an aggregate field to the field list. */
3917
3918static void
107d2387 3919dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73
DJ
3920 struct dwarf2_cu *cu)
3921{
3922 struct objfile *objfile = cu->objfile;
5e2b427d 3923 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
3924 struct nextfield *new_field;
3925 struct attribute *attr;
3926 struct field *fp;
3927 char *fieldname = "";
3928
3929 /* Allocate a new field list entry and link it in. */
3930 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 3931 make_cleanup (xfree, new_field);
c906108c
SS
3932 memset (new_field, 0, sizeof (struct nextfield));
3933 new_field->next = fip->fields;
3934 fip->fields = new_field;
3935 fip->nfields++;
3936
3937 /* Handle accessibility and virtuality of field.
3938 The default accessibility for members is public, the default
3939 accessibility for inheritance is private. */
3940 if (die->tag != DW_TAG_inheritance)
3941 new_field->accessibility = DW_ACCESS_public;
3942 else
3943 new_field->accessibility = DW_ACCESS_private;
3944 new_field->virtuality = DW_VIRTUALITY_none;
3945
e142c38c 3946 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
3947 if (attr)
3948 new_field->accessibility = DW_UNSND (attr);
3949 if (new_field->accessibility != DW_ACCESS_public)
3950 fip->non_public_fields = 1;
e142c38c 3951 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
3952 if (attr)
3953 new_field->virtuality = DW_UNSND (attr);
3954
3955 fp = &new_field->field;
a9a9bd0f 3956
e142c38c 3957 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 3958 {
a9a9bd0f
DC
3959 /* Data member other than a C++ static data member. */
3960
c906108c 3961 /* Get type of field. */
e7c27a73 3962 fp->type = die_type (die, cu);
c906108c 3963
d6a843b5 3964 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 3965
c906108c 3966 /* Get bit size of field (zero if none). */
e142c38c 3967 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
3968 if (attr)
3969 {
3970 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
3971 }
3972 else
3973 {
3974 FIELD_BITSIZE (*fp) = 0;
3975 }
3976
3977 /* Get bit offset of field. */
e142c38c 3978 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
3979 if (attr)
3980 {
c6a0999f
JB
3981 int byte_offset;
3982
3690dd37
JB
3983 if (attr_form_is_section_offset (attr))
3984 {
3985 dwarf2_complex_location_expr_complaint ();
c6a0999f 3986 byte_offset = 0;
3690dd37
JB
3987 }
3988 else if (attr_form_is_constant (attr))
c6a0999f 3989 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
3690dd37 3990 else
c6a0999f
JB
3991 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
3992
d6a843b5 3993 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 3994 }
e142c38c 3995 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
3996 if (attr)
3997 {
5e2b427d 3998 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
3999 {
4000 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
4001 additional bit offset from the MSB of the containing
4002 anonymous object to the MSB of the field. We don't
4003 have to do anything special since we don't need to
4004 know the size of the anonymous object. */
c906108c
SS
4005 FIELD_BITPOS (*fp) += DW_UNSND (attr);
4006 }
4007 else
4008 {
4009 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
4010 MSB of the anonymous object, subtract off the number of
4011 bits from the MSB of the field to the MSB of the
4012 object, and then subtract off the number of bits of
4013 the field itself. The result is the bit offset of
4014 the LSB of the field. */
c906108c
SS
4015 int anonymous_size;
4016 int bit_offset = DW_UNSND (attr);
4017
e142c38c 4018 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4019 if (attr)
4020 {
4021 /* The size of the anonymous object containing
4022 the bit field is explicit, so use the
4023 indicated size (in bytes). */
4024 anonymous_size = DW_UNSND (attr);
4025 }
4026 else
4027 {
4028 /* The size of the anonymous object containing
4029 the bit field must be inferred from the type
4030 attribute of the data member containing the
4031 bit field. */
4032 anonymous_size = TYPE_LENGTH (fp->type);
4033 }
4034 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
4035 - bit_offset - FIELD_BITSIZE (*fp);
4036 }
4037 }
4038
4039 /* Get name of field. */
39cbfefa
DJ
4040 fieldname = dwarf2_name (die, cu);
4041 if (fieldname == NULL)
4042 fieldname = "";
d8151005
DJ
4043
4044 /* The name is already allocated along with this objfile, so we don't
4045 need to duplicate it for the type. */
4046 fp->name = fieldname;
c906108c
SS
4047
4048 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 4049 pointer or virtual base class pointer) to private. */
e142c38c 4050 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c
SS
4051 {
4052 new_field->accessibility = DW_ACCESS_private;
4053 fip->non_public_fields = 1;
4054 }
4055 }
a9a9bd0f 4056 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 4057 {
a9a9bd0f
DC
4058 /* C++ static member. */
4059
4060 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
4061 is a declaration, but all versions of G++ as of this writing
4062 (so through at least 3.2.1) incorrectly generate
4063 DW_TAG_variable tags. */
4064
c906108c 4065 char *physname;
c906108c 4066
a9a9bd0f 4067 /* Get name of field. */
39cbfefa
DJ
4068 fieldname = dwarf2_name (die, cu);
4069 if (fieldname == NULL)
c906108c
SS
4070 return;
4071
2df3850c 4072 /* Get physical name. */
e142c38c 4073 physname = dwarf2_linkage_name (die, cu);
c906108c 4074
d8151005
DJ
4075 /* The name is already allocated along with this objfile, so we don't
4076 need to duplicate it for the type. */
4077 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 4078 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 4079 FIELD_NAME (*fp) = fieldname;
c906108c
SS
4080 }
4081 else if (die->tag == DW_TAG_inheritance)
4082 {
4083 /* C++ base class field. */
e142c38c 4084 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 4085 if (attr)
d6a843b5
JK
4086 SET_FIELD_BITPOS (*fp, decode_locdesc (DW_BLOCK (attr), cu)
4087 * bits_per_byte);
c906108c 4088 FIELD_BITSIZE (*fp) = 0;
e7c27a73 4089 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
4090 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
4091 fip->nbaseclasses++;
4092 }
4093}
4094
4095/* Create the vector of fields, and attach it to the type. */
4096
4097static void
fba45db2 4098dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 4099 struct dwarf2_cu *cu)
c906108c
SS
4100{
4101 int nfields = fip->nfields;
4102
4103 /* Record the field count, allocate space for the array of fields,
4104 and create blank accessibility bitfields if necessary. */
4105 TYPE_NFIELDS (type) = nfields;
4106 TYPE_FIELDS (type) = (struct field *)
4107 TYPE_ALLOC (type, sizeof (struct field) * nfields);
4108 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
4109
4110 if (fip->non_public_fields)
4111 {
4112 ALLOCATE_CPLUS_STRUCT_TYPE (type);
4113
4114 TYPE_FIELD_PRIVATE_BITS (type) =
4115 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4116 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
4117
4118 TYPE_FIELD_PROTECTED_BITS (type) =
4119 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4120 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
4121
4122 TYPE_FIELD_IGNORE_BITS (type) =
4123 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4124 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
4125 }
4126
4127 /* If the type has baseclasses, allocate and clear a bit vector for
4128 TYPE_FIELD_VIRTUAL_BITS. */
4129 if (fip->nbaseclasses)
4130 {
4131 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 4132 unsigned char *pointer;
c906108c
SS
4133
4134 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
4135 pointer = TYPE_ALLOC (type, num_bytes);
4136 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
4137 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
4138 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
4139 }
4140
4141 /* Copy the saved-up fields into the field vector. Start from the head
4142 of the list, adding to the tail of the field array, so that they end
4143 up in the same order in the array in which they were added to the list. */
4144 while (nfields-- > 0)
4145 {
4146 TYPE_FIELD (type, nfields) = fip->fields->field;
4147 switch (fip->fields->accessibility)
4148 {
c5aa993b
JM
4149 case DW_ACCESS_private:
4150 SET_TYPE_FIELD_PRIVATE (type, nfields);
4151 break;
c906108c 4152
c5aa993b
JM
4153 case DW_ACCESS_protected:
4154 SET_TYPE_FIELD_PROTECTED (type, nfields);
4155 break;
c906108c 4156
c5aa993b
JM
4157 case DW_ACCESS_public:
4158 break;
c906108c 4159
c5aa993b
JM
4160 default:
4161 /* Unknown accessibility. Complain and treat it as public. */
4162 {
e2e0b3e5 4163 complaint (&symfile_complaints, _("unsupported accessibility %d"),
4d3c2250 4164 fip->fields->accessibility);
c5aa993b
JM
4165 }
4166 break;
c906108c
SS
4167 }
4168 if (nfields < fip->nbaseclasses)
4169 {
4170 switch (fip->fields->virtuality)
4171 {
c5aa993b
JM
4172 case DW_VIRTUALITY_virtual:
4173 case DW_VIRTUALITY_pure_virtual:
4174 SET_TYPE_FIELD_VIRTUAL (type, nfields);
4175 break;
c906108c
SS
4176 }
4177 }
4178 fip->fields = fip->fields->next;
4179 }
4180}
4181
c906108c
SS
4182/* Add a member function to the proper fieldlist. */
4183
4184static void
107d2387 4185dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 4186 struct type *type, struct dwarf2_cu *cu)
c906108c 4187{
e7c27a73 4188 struct objfile *objfile = cu->objfile;
c906108c
SS
4189 struct attribute *attr;
4190 struct fnfieldlist *flp;
4191 int i;
4192 struct fn_field *fnp;
4193 char *fieldname;
4194 char *physname;
4195 struct nextfnfield *new_fnfield;
f792889a 4196 struct type *this_type;
c906108c 4197
2df3850c 4198 /* Get name of member function. */
39cbfefa
DJ
4199 fieldname = dwarf2_name (die, cu);
4200 if (fieldname == NULL)
2df3850c 4201 return;
c906108c 4202
2df3850c 4203 /* Get the mangled name. */
e142c38c 4204 physname = dwarf2_linkage_name (die, cu);
c906108c
SS
4205
4206 /* Look up member function name in fieldlist. */
4207 for (i = 0; i < fip->nfnfields; i++)
4208 {
27bfe10e 4209 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
4210 break;
4211 }
4212
4213 /* Create new list element if necessary. */
4214 if (i < fip->nfnfields)
4215 flp = &fip->fnfieldlists[i];
4216 else
4217 {
4218 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
4219 {
4220 fip->fnfieldlists = (struct fnfieldlist *)
4221 xrealloc (fip->fnfieldlists,
4222 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 4223 * sizeof (struct fnfieldlist));
c906108c 4224 if (fip->nfnfields == 0)
c13c43fd 4225 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
4226 }
4227 flp = &fip->fnfieldlists[fip->nfnfields];
4228 flp->name = fieldname;
4229 flp->length = 0;
4230 flp->head = NULL;
4231 fip->nfnfields++;
4232 }
4233
4234 /* Create a new member function field and chain it to the field list
4235 entry. */
4236 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 4237 make_cleanup (xfree, new_fnfield);
c906108c
SS
4238 memset (new_fnfield, 0, sizeof (struct nextfnfield));
4239 new_fnfield->next = flp->head;
4240 flp->head = new_fnfield;
4241 flp->length++;
4242
4243 /* Fill in the member function field info. */
4244 fnp = &new_fnfield->fnfield;
d8151005
DJ
4245 /* The name is already allocated along with this objfile, so we don't
4246 need to duplicate it for the type. */
4247 fnp->physname = physname ? physname : "";
c906108c 4248 fnp->type = alloc_type (objfile);
f792889a
DJ
4249 this_type = read_type_die (die, cu);
4250 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 4251 {
f792889a 4252 int nparams = TYPE_NFIELDS (this_type);
c906108c 4253
f792889a 4254 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
4255 of the method itself (TYPE_CODE_METHOD). */
4256 smash_to_method_type (fnp->type, type,
f792889a
DJ
4257 TYPE_TARGET_TYPE (this_type),
4258 TYPE_FIELDS (this_type),
4259 TYPE_NFIELDS (this_type),
4260 TYPE_VARARGS (this_type));
c906108c
SS
4261
4262 /* Handle static member functions.
c5aa993b
JM
4263 Dwarf2 has no clean way to discern C++ static and non-static
4264 member functions. G++ helps GDB by marking the first
4265 parameter for non-static member functions (which is the
4266 this pointer) as artificial. We obtain this information
4267 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 4268 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
4269 fnp->voffset = VOFFSET_STATIC;
4270 }
4271 else
e2e0b3e5 4272 complaint (&symfile_complaints, _("member function type missing for '%s'"),
4d3c2250 4273 physname);
c906108c
SS
4274
4275 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 4276 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 4277 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
4278
4279 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
4280 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
4281
4282 /* Get accessibility. */
e142c38c 4283 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
4284 if (attr)
4285 {
4286 switch (DW_UNSND (attr))
4287 {
c5aa993b
JM
4288 case DW_ACCESS_private:
4289 fnp->is_private = 1;
4290 break;
4291 case DW_ACCESS_protected:
4292 fnp->is_protected = 1;
4293 break;
c906108c
SS
4294 }
4295 }
4296
b02dede2 4297 /* Check for artificial methods. */
e142c38c 4298 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
4299 if (attr && DW_UNSND (attr) != 0)
4300 fnp->is_artificial = 1;
4301
c906108c 4302 /* Get index in virtual function table if it is a virtual member function. */
e142c38c 4303 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
c906108c 4304 if (attr)
8e19ed76
PS
4305 {
4306 /* Support the .debug_loc offsets */
4307 if (attr_form_is_block (attr))
4308 {
e7c27a73 4309 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8e19ed76 4310 }
3690dd37 4311 else if (attr_form_is_section_offset (attr))
8e19ed76 4312 {
4d3c2250 4313 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
4314 }
4315 else
4316 {
4d3c2250
KB
4317 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
4318 fieldname);
8e19ed76
PS
4319 }
4320 }
c906108c
SS
4321}
4322
4323/* Create the vector of member function fields, and attach it to the type. */
4324
4325static void
fba45db2 4326dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 4327 struct dwarf2_cu *cu)
c906108c
SS
4328{
4329 struct fnfieldlist *flp;
4330 int total_length = 0;
4331 int i;
4332
4333 ALLOCATE_CPLUS_STRUCT_TYPE (type);
4334 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
4335 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
4336
4337 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
4338 {
4339 struct nextfnfield *nfp = flp->head;
4340 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
4341 int k;
4342
4343 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
4344 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
4345 fn_flp->fn_fields = (struct fn_field *)
4346 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
4347 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 4348 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
4349
4350 total_length += flp->length;
4351 }
4352
4353 TYPE_NFN_FIELDS (type) = fip->nfnfields;
4354 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
4355}
4356
1168df01
JB
4357/* Returns non-zero if NAME is the name of a vtable member in CU's
4358 language, zero otherwise. */
4359static int
4360is_vtable_name (const char *name, struct dwarf2_cu *cu)
4361{
4362 static const char vptr[] = "_vptr";
987504bb 4363 static const char vtable[] = "vtable";
1168df01 4364
987504bb
JJ
4365 /* Look for the C++ and Java forms of the vtable. */
4366 if ((cu->language == language_java
4367 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
4368 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
4369 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
4370 return 1;
4371
4372 return 0;
4373}
4374
c0dd20ea
DJ
4375/* GCC outputs unnamed structures that are really pointers to member
4376 functions, with the ABI-specified layout. If DIE (from CU) describes
4377 such a structure, set its type, and return nonzero. Otherwise return
61049d3b
DJ
4378 zero.
4379
4380 GCC shouldn't do this; it should just output pointer to member DIEs.
4381 This is GCC PR debug/28767. */
c0dd20ea 4382
f792889a 4383static struct type *
c0dd20ea
DJ
4384quirk_gcc_member_function_pointer (struct die_info *die, struct dwarf2_cu *cu)
4385{
4386 struct objfile *objfile = cu->objfile;
4387 struct type *type;
4388 struct die_info *pfn_die, *delta_die;
4389 struct attribute *pfn_name, *delta_name;
4390 struct type *pfn_type, *domain_type;
4391
4392 /* Check for a structure with no name and two children. */
4393 if (die->tag != DW_TAG_structure_type
4394 || dwarf2_attr (die, DW_AT_name, cu) != NULL
4395 || die->child == NULL
4396 || die->child->sibling == NULL
4397 || (die->child->sibling->sibling != NULL
4398 && die->child->sibling->sibling->tag != DW_TAG_padding))
f792889a 4399 return NULL;
c0dd20ea
DJ
4400
4401 /* Check for __pfn and __delta members. */
4402 pfn_die = die->child;
4403 pfn_name = dwarf2_attr (pfn_die, DW_AT_name, cu);
4404 if (pfn_die->tag != DW_TAG_member
4405 || pfn_name == NULL
4406 || DW_STRING (pfn_name) == NULL
4407 || strcmp ("__pfn", DW_STRING (pfn_name)) != 0)
f792889a 4408 return NULL;
c0dd20ea
DJ
4409
4410 delta_die = pfn_die->sibling;
4411 delta_name = dwarf2_attr (delta_die, DW_AT_name, cu);
4412 if (delta_die->tag != DW_TAG_member
4413 || delta_name == NULL
4414 || DW_STRING (delta_name) == NULL
4415 || strcmp ("__delta", DW_STRING (delta_name)) != 0)
f792889a 4416 return NULL;
c0dd20ea
DJ
4417
4418 /* Find the type of the method. */
4419 pfn_type = die_type (pfn_die, cu);
4420 if (pfn_type == NULL
4421 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
4422 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
f792889a 4423 return NULL;
c0dd20ea
DJ
4424
4425 /* Look for the "this" argument. */
4426 pfn_type = TYPE_TARGET_TYPE (pfn_type);
4427 if (TYPE_NFIELDS (pfn_type) == 0
4428 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
f792889a 4429 return NULL;
c0dd20ea
DJ
4430
4431 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
4432 type = alloc_type (objfile);
4433 smash_to_method_type (type, domain_type, TYPE_TARGET_TYPE (pfn_type),
4434 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
4435 TYPE_VARARGS (pfn_type));
0d5de010 4436 type = lookup_methodptr_type (type);
f792889a 4437 return set_die_type (die, type, cu);
c0dd20ea 4438}
1168df01 4439
c906108c
SS
4440/* Called when we find the DIE that starts a structure or union scope
4441 (definition) to process all dies that define the members of the
4442 structure or union.
4443
4444 NOTE: we need to call struct_type regardless of whether or not the
4445 DIE has an at_name attribute, since it might be an anonymous
4446 structure or union. This gets the type entered into our set of
4447 user defined types.
4448
4449 However, if the structure is incomplete (an opaque struct/union)
4450 then suppress creating a symbol table entry for it since gdb only
4451 wants to find the one with the complete definition. Note that if
4452 it is complete, we just call new_symbol, which does it's own
4453 checking about whether the struct/union is anonymous or not (and
4454 suppresses creating a symbol table entry itself). */
4455
f792889a 4456static struct type *
134d01f1 4457read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4458{
e7c27a73 4459 struct objfile *objfile = cu->objfile;
c906108c
SS
4460 struct type *type;
4461 struct attribute *attr;
39cbfefa 4462 char *name;
0114d602 4463 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c 4464
f792889a
DJ
4465 type = quirk_gcc_member_function_pointer (die, cu);
4466 if (type)
4467 return type;
c906108c 4468
c0dd20ea 4469 type = alloc_type (objfile);
c906108c 4470 INIT_CPLUS_SPECIFIC (type);
39cbfefa
DJ
4471 name = dwarf2_name (die, cu);
4472 if (name != NULL)
c906108c 4473 {
987504bb
JJ
4474 if (cu->language == language_cplus
4475 || cu->language == language_java)
63d06c5c 4476 {
0114d602
DJ
4477 const char *new_prefix = determine_class_name (die, cu);
4478 TYPE_TAG_NAME (type) = (char *) new_prefix;
63d06c5c
DC
4479 }
4480 else
4481 {
d8151005
DJ
4482 /* The name is already allocated along with this objfile, so
4483 we don't need to duplicate it for the type. */
39cbfefa 4484 TYPE_TAG_NAME (type) = name;
63d06c5c 4485 }
c906108c
SS
4486 }
4487
4488 if (die->tag == DW_TAG_structure_type)
4489 {
4490 TYPE_CODE (type) = TYPE_CODE_STRUCT;
4491 }
4492 else if (die->tag == DW_TAG_union_type)
4493 {
4494 TYPE_CODE (type) = TYPE_CODE_UNION;
4495 }
4496 else
4497 {
4498 /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
c5aa993b 4499 in gdbtypes.h. */
c906108c
SS
4500 TYPE_CODE (type) = TYPE_CODE_CLASS;
4501 }
4502
e142c38c 4503 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4504 if (attr)
4505 {
4506 TYPE_LENGTH (type) = DW_UNSND (attr);
4507 }
4508 else
4509 {
4510 TYPE_LENGTH (type) = 0;
4511 }
4512
876cecd0 4513 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 4514 if (die_is_declaration (die, cu))
876cecd0 4515 TYPE_STUB (type) = 1;
dc718098 4516
c906108c
SS
4517 /* We need to add the type field to the die immediately so we don't
4518 infinitely recurse when dealing with pointers to the structure
4519 type within the structure itself. */
1c379e20 4520 set_die_type (die, type, cu);
c906108c 4521
e142c38c 4522 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
4523 {
4524 struct field_info fi;
4525 struct die_info *child_die;
c906108c
SS
4526
4527 memset (&fi, 0, sizeof (struct field_info));
4528
639d11d3 4529 child_die = die->child;
c906108c
SS
4530
4531 while (child_die && child_die->tag)
4532 {
a9a9bd0f
DC
4533 if (child_die->tag == DW_TAG_member
4534 || child_die->tag == DW_TAG_variable)
c906108c 4535 {
a9a9bd0f
DC
4536 /* NOTE: carlton/2002-11-05: A C++ static data member
4537 should be a DW_TAG_member that is a declaration, but
4538 all versions of G++ as of this writing (so through at
4539 least 3.2.1) incorrectly generate DW_TAG_variable
4540 tags for them instead. */
e7c27a73 4541 dwarf2_add_field (&fi, child_die, cu);
c906108c 4542 }
8713b1b1 4543 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
4544 {
4545 /* C++ member function. */
e7c27a73 4546 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
4547 }
4548 else if (child_die->tag == DW_TAG_inheritance)
4549 {
4550 /* C++ base class field. */
e7c27a73 4551 dwarf2_add_field (&fi, child_die, cu);
c906108c 4552 }
c906108c
SS
4553 child_die = sibling_die (child_die);
4554 }
4555
4556 /* Attach fields and member functions to the type. */
4557 if (fi.nfields)
e7c27a73 4558 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
4559 if (fi.nfnfields)
4560 {
e7c27a73 4561 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 4562
c5aa993b 4563 /* Get the type which refers to the base class (possibly this
c906108c
SS
4564 class itself) which contains the vtable pointer for the current
4565 class from the DW_AT_containing_type attribute. */
4566
e142c38c 4567 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 4568 {
e7c27a73 4569 struct type *t = die_containing_type (die, cu);
c906108c
SS
4570
4571 TYPE_VPTR_BASETYPE (type) = t;
4572 if (type == t)
4573 {
c906108c
SS
4574 int i;
4575
4576 /* Our own class provides vtbl ptr. */
4577 for (i = TYPE_NFIELDS (t) - 1;
4578 i >= TYPE_N_BASECLASSES (t);
4579 --i)
4580 {
4581 char *fieldname = TYPE_FIELD_NAME (t, i);
4582
1168df01 4583 if (is_vtable_name (fieldname, cu))
c906108c
SS
4584 {
4585 TYPE_VPTR_FIELDNO (type) = i;
4586 break;
4587 }
4588 }
4589
4590 /* Complain if virtual function table field not found. */
4591 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 4592 complaint (&symfile_complaints,
e2e0b3e5 4593 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
4594 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
4595 "");
c906108c
SS
4596 }
4597 else
4598 {
4599 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
4600 }
4601 }
f6235d4c
EZ
4602 else if (cu->producer
4603 && strncmp (cu->producer,
4604 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
4605 {
4606 /* The IBM XLC compiler does not provide direct indication
4607 of the containing type, but the vtable pointer is
4608 always named __vfp. */
4609
4610 int i;
4611
4612 for (i = TYPE_NFIELDS (type) - 1;
4613 i >= TYPE_N_BASECLASSES (type);
4614 --i)
4615 {
4616 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
4617 {
4618 TYPE_VPTR_FIELDNO (type) = i;
4619 TYPE_VPTR_BASETYPE (type) = type;
4620 break;
4621 }
4622 }
4623 }
c906108c 4624 }
c906108c 4625 }
63d06c5c 4626
0114d602 4627 do_cleanups (back_to);
f792889a 4628 return type;
c906108c
SS
4629}
4630
134d01f1
DJ
4631static void
4632process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
4633{
4634 struct objfile *objfile = cu->objfile;
90aeadfc 4635 struct die_info *child_die = die->child;
f792889a 4636 struct type *this_type;
c906108c 4637
f792889a
DJ
4638 this_type = get_die_type (die, cu);
4639 if (this_type == NULL)
4640 this_type = read_structure_type (die, cu);
c906108c 4641
90aeadfc
DC
4642 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
4643 snapshots) has been known to create a die giving a declaration
4644 for a class that has, as a child, a die giving a definition for a
4645 nested class. So we have to process our children even if the
4646 current die is a declaration. Normally, of course, a declaration
4647 won't have any children at all. */
134d01f1 4648
90aeadfc
DC
4649 while (child_die != NULL && child_die->tag)
4650 {
4651 if (child_die->tag == DW_TAG_member
4652 || child_die->tag == DW_TAG_variable
4653 || child_die->tag == DW_TAG_inheritance)
134d01f1 4654 {
90aeadfc 4655 /* Do nothing. */
134d01f1 4656 }
90aeadfc
DC
4657 else
4658 process_die (child_die, cu);
134d01f1 4659
90aeadfc 4660 child_die = sibling_die (child_die);
134d01f1
DJ
4661 }
4662
fa4028e9
JB
4663 /* Do not consider external references. According to the DWARF standard,
4664 these DIEs are identified by the fact that they have no byte_size
4665 attribute, and a declaration attribute. */
4666 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
4667 || !die_is_declaration (die, cu))
f792889a 4668 new_symbol (die, this_type, cu);
134d01f1
DJ
4669}
4670
4671/* Given a DW_AT_enumeration_type die, set its type. We do not
4672 complete the type's fields yet, or create any symbols. */
c906108c 4673
f792889a 4674static struct type *
134d01f1 4675read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4676{
e7c27a73 4677 struct objfile *objfile = cu->objfile;
c906108c 4678 struct type *type;
c906108c 4679 struct attribute *attr;
0114d602 4680 const char *name;
134d01f1 4681
c906108c
SS
4682 type = alloc_type (objfile);
4683
4684 TYPE_CODE (type) = TYPE_CODE_ENUM;
0114d602 4685 name = dwarf2_full_name (die, cu);
39cbfefa 4686 if (name != NULL)
0114d602 4687 TYPE_TAG_NAME (type) = (char *) name;
c906108c 4688
e142c38c 4689 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4690 if (attr)
4691 {
4692 TYPE_LENGTH (type) = DW_UNSND (attr);
4693 }
4694 else
4695 {
4696 TYPE_LENGTH (type) = 0;
4697 }
4698
137033e9
JB
4699 /* The enumeration DIE can be incomplete. In Ada, any type can be
4700 declared as private in the package spec, and then defined only
4701 inside the package body. Such types are known as Taft Amendment
4702 Types. When another package uses such a type, an incomplete DIE
4703 may be generated by the compiler. */
02eb380e 4704 if (die_is_declaration (die, cu))
876cecd0 4705 TYPE_STUB (type) = 1;
02eb380e 4706
f792889a 4707 return set_die_type (die, type, cu);
134d01f1
DJ
4708}
4709
8176b9b8 4710/* Determine the name of the type represented by DIE, which should be
0114d602
DJ
4711 a named C++ or Java compound type. Return the name in question,
4712 allocated on the objfile obstack. */
8176b9b8 4713
0114d602 4714static const char *
8176b9b8
DC
4715determine_class_name (struct die_info *die, struct dwarf2_cu *cu)
4716{
0114d602 4717 const char *new_prefix = NULL;
8176b9b8
DC
4718
4719 /* If we don't have namespace debug info, guess the name by trying
4720 to demangle the names of members, just like we did in
72bf9492 4721 guess_structure_name. */
8176b9b8
DC
4722 if (!processing_has_namespace_info)
4723 {
4724 struct die_info *child;
4725
4726 for (child = die->child;
4727 child != NULL && child->tag != 0;
4728 child = sibling_die (child))
4729 {
4730 if (child->tag == DW_TAG_subprogram)
4731 {
0114d602 4732 char *phys_prefix
31c27f77
JJ
4733 = language_class_name_from_physname (cu->language_defn,
4734 dwarf2_linkage_name
8176b9b8
DC
4735 (child, cu));
4736
0114d602
DJ
4737 if (phys_prefix != NULL)
4738 {
4739 new_prefix
4740 = obsavestring (phys_prefix, strlen (phys_prefix),
4741 &cu->objfile->objfile_obstack);
4742 xfree (phys_prefix);
4743 break;
4744 }
8176b9b8
DC
4745 }
4746 }
4747 }
4748
4749 if (new_prefix == NULL)
0114d602 4750 new_prefix = dwarf2_full_name (die, cu);
8176b9b8
DC
4751
4752 return new_prefix;
4753}
4754
134d01f1
DJ
4755/* Given a pointer to a die which begins an enumeration, process all
4756 the dies that define the members of the enumeration, and create the
4757 symbol for the enumeration type.
4758
4759 NOTE: We reverse the order of the element list. */
4760
4761static void
4762process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
4763{
4764 struct objfile *objfile = cu->objfile;
4765 struct die_info *child_die;
4766 struct field *fields;
134d01f1
DJ
4767 struct symbol *sym;
4768 int num_fields;
4769 int unsigned_enum = 1;
39cbfefa 4770 char *name;
f792889a 4771 struct type *this_type;
134d01f1 4772
c906108c
SS
4773 num_fields = 0;
4774 fields = NULL;
f792889a
DJ
4775 this_type = get_die_type (die, cu);
4776 if (this_type == NULL)
4777 this_type = read_enumeration_type (die, cu);
639d11d3 4778 if (die->child != NULL)
c906108c 4779 {
639d11d3 4780 child_die = die->child;
c906108c
SS
4781 while (child_die && child_die->tag)
4782 {
4783 if (child_die->tag != DW_TAG_enumerator)
4784 {
e7c27a73 4785 process_die (child_die, cu);
c906108c
SS
4786 }
4787 else
4788 {
39cbfefa
DJ
4789 name = dwarf2_name (child_die, cu);
4790 if (name)
c906108c 4791 {
f792889a 4792 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
4793 if (SYMBOL_VALUE (sym) < 0)
4794 unsigned_enum = 0;
4795
4796 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
4797 {
4798 fields = (struct field *)
4799 xrealloc (fields,
4800 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 4801 * sizeof (struct field));
c906108c
SS
4802 }
4803
3567439c 4804 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 4805 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 4806 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
4807 FIELD_BITSIZE (fields[num_fields]) = 0;
4808
4809 num_fields++;
4810 }
4811 }
4812
4813 child_die = sibling_die (child_die);
4814 }
4815
4816 if (num_fields)
4817 {
f792889a
DJ
4818 TYPE_NFIELDS (this_type) = num_fields;
4819 TYPE_FIELDS (this_type) = (struct field *)
4820 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
4821 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 4822 sizeof (struct field) * num_fields);
b8c9b27d 4823 xfree (fields);
c906108c
SS
4824 }
4825 if (unsigned_enum)
876cecd0 4826 TYPE_UNSIGNED (this_type) = 1;
c906108c 4827 }
134d01f1 4828
f792889a 4829 new_symbol (die, this_type, cu);
c906108c
SS
4830}
4831
4832/* Extract all information from a DW_TAG_array_type DIE and put it in
4833 the DIE's type field. For now, this only handles one dimensional
4834 arrays. */
4835
f792889a 4836static struct type *
e7c27a73 4837read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4838{
e7c27a73 4839 struct objfile *objfile = cu->objfile;
c906108c
SS
4840 struct die_info *child_die;
4841 struct type *type = NULL;
4842 struct type *element_type, *range_type, *index_type;
4843 struct type **range_types = NULL;
4844 struct attribute *attr;
4845 int ndim = 0;
4846 struct cleanup *back_to;
39cbfefa 4847 char *name;
c906108c 4848
e7c27a73 4849 element_type = die_type (die, cu);
c906108c
SS
4850
4851 /* Irix 6.2 native cc creates array types without children for
4852 arrays with unspecified length. */
639d11d3 4853 if (die->child == NULL)
c906108c 4854 {
46bf5051 4855 index_type = objfile_type (objfile)->builtin_int;
c906108c 4856 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
4857 type = create_array_type (NULL, element_type, range_type);
4858 return set_die_type (die, type, cu);
c906108c
SS
4859 }
4860
4861 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 4862 child_die = die->child;
c906108c
SS
4863 while (child_die && child_die->tag)
4864 {
4865 if (child_die->tag == DW_TAG_subrange_type)
4866 {
f792889a
DJ
4867 struct type *child_type = read_type_die (child_die, cu);
4868 if (child_type != NULL)
a02abb62
JB
4869 {
4870 /* The range type was succesfully read. Save it for
4871 the array type creation. */
4872 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
4873 {
4874 range_types = (struct type **)
4875 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
4876 * sizeof (struct type *));
4877 if (ndim == 0)
4878 make_cleanup (free_current_contents, &range_types);
4879 }
f792889a 4880 range_types[ndim++] = child_type;
a02abb62 4881 }
c906108c
SS
4882 }
4883 child_die = sibling_die (child_die);
4884 }
4885
4886 /* Dwarf2 dimensions are output from left to right, create the
4887 necessary array types in backwards order. */
7ca2d3a3 4888
c906108c 4889 type = element_type;
7ca2d3a3
DL
4890
4891 if (read_array_order (die, cu) == DW_ORD_col_major)
4892 {
4893 int i = 0;
4894 while (i < ndim)
4895 type = create_array_type (NULL, type, range_types[i++]);
4896 }
4897 else
4898 {
4899 while (ndim-- > 0)
4900 type = create_array_type (NULL, type, range_types[ndim]);
4901 }
c906108c 4902
f5f8a009
EZ
4903 /* Understand Dwarf2 support for vector types (like they occur on
4904 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
4905 array type. This is not part of the Dwarf2/3 standard yet, but a
4906 custom vendor extension. The main difference between a regular
4907 array and the vector variant is that vectors are passed by value
4908 to functions. */
e142c38c 4909 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 4910 if (attr)
ea37ba09 4911 make_vector_type (type);
f5f8a009 4912
39cbfefa
DJ
4913 name = dwarf2_name (die, cu);
4914 if (name)
4915 TYPE_NAME (type) = name;
714e295e 4916
c906108c
SS
4917 do_cleanups (back_to);
4918
4919 /* Install the type in the die. */
f792889a 4920 return set_die_type (die, type, cu);
c906108c
SS
4921}
4922
7ca2d3a3
DL
4923static enum dwarf_array_dim_ordering
4924read_array_order (struct die_info *die, struct dwarf2_cu *cu)
4925{
4926 struct attribute *attr;
4927
4928 attr = dwarf2_attr (die, DW_AT_ordering, cu);
4929
4930 if (attr) return DW_SND (attr);
4931
4932 /*
4933 GNU F77 is a special case, as at 08/2004 array type info is the
4934 opposite order to the dwarf2 specification, but data is still
4935 laid out as per normal fortran.
4936
4937 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
4938 version checking.
4939 */
4940
4941 if (cu->language == language_fortran &&
4942 cu->producer && strstr (cu->producer, "GNU F77"))
4943 {
4944 return DW_ORD_row_major;
4945 }
4946
4947 switch (cu->language_defn->la_array_ordering)
4948 {
4949 case array_column_major:
4950 return DW_ORD_col_major;
4951 case array_row_major:
4952 default:
4953 return DW_ORD_row_major;
4954 };
4955}
4956
72019c9c
GM
4957/* Extract all information from a DW_TAG_set_type DIE and put it in
4958 the DIE's type field. */
4959
f792889a 4960static struct type *
72019c9c
GM
4961read_set_type (struct die_info *die, struct dwarf2_cu *cu)
4962{
f792889a
DJ
4963 struct type *set_type = create_set_type (NULL, die_type (die, cu));
4964
4965 return set_die_type (die, set_type, cu);
72019c9c 4966}
7ca2d3a3 4967
c906108c
SS
4968/* First cut: install each common block member as a global variable. */
4969
4970static void
e7c27a73 4971read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4972{
4973 struct die_info *child_die;
4974 struct attribute *attr;
4975 struct symbol *sym;
4976 CORE_ADDR base = (CORE_ADDR) 0;
4977
e142c38c 4978 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
4979 if (attr)
4980 {
8e19ed76
PS
4981 /* Support the .debug_loc offsets */
4982 if (attr_form_is_block (attr))
4983 {
e7c27a73 4984 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 4985 }
3690dd37 4986 else if (attr_form_is_section_offset (attr))
8e19ed76 4987 {
4d3c2250 4988 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
4989 }
4990 else
4991 {
4d3c2250
KB
4992 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
4993 "common block member");
8e19ed76 4994 }
c906108c 4995 }
639d11d3 4996 if (die->child != NULL)
c906108c 4997 {
639d11d3 4998 child_die = die->child;
c906108c
SS
4999 while (child_die && child_die->tag)
5000 {
e7c27a73 5001 sym = new_symbol (child_die, NULL, cu);
e142c38c 5002 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
5003 if (attr)
5004 {
5005 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 5006 base + decode_locdesc (DW_BLOCK (attr), cu);
c906108c
SS
5007 add_symbol_to_list (sym, &global_symbols);
5008 }
5009 child_die = sibling_die (child_die);
5010 }
5011 }
5012}
5013
0114d602 5014/* Create a type for a C++ namespace. */
d9fa45fe 5015
0114d602
DJ
5016static struct type *
5017read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 5018{
e7c27a73 5019 struct objfile *objfile = cu->objfile;
0114d602 5020 const char *previous_prefix, *name;
9219021c 5021 int is_anonymous;
0114d602
DJ
5022 struct type *type;
5023
5024 /* For extensions, reuse the type of the original namespace. */
5025 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
5026 {
5027 struct die_info *ext_die;
5028 struct dwarf2_cu *ext_cu = cu;
5029 ext_die = dwarf2_extension (die, &ext_cu);
5030 type = read_type_die (ext_die, ext_cu);
5031 return set_die_type (die, type, cu);
5032 }
9219021c 5033
e142c38c 5034 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
5035
5036 /* Now build the name of the current namespace. */
5037
0114d602
DJ
5038 previous_prefix = determine_prefix (die, cu);
5039 if (previous_prefix[0] != '\0')
5040 name = typename_concat (&objfile->objfile_obstack,
5041 previous_prefix, name, cu);
5042
5043 /* Create the type. */
5044 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
5045 objfile);
5046 TYPE_NAME (type) = (char *) name;
5047 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5048
5049 set_die_type (die, type, cu);
5050
5051 return type;
5052}
5053
5054/* Read a C++ namespace. */
5055
5056static void
5057read_namespace (struct die_info *die, struct dwarf2_cu *cu)
5058{
5059 struct objfile *objfile = cu->objfile;
5060 const char *name;
5061 int is_anonymous;
9219021c 5062
5c4e30ca
DC
5063 /* Add a symbol associated to this if we haven't seen the namespace
5064 before. Also, add a using directive if it's an anonymous
5065 namespace. */
9219021c 5066
f2f0e013 5067 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
5068 {
5069 struct type *type;
5070
0114d602 5071 type = read_type_die (die, cu);
e7c27a73 5072 new_symbol (die, type, cu);
5c4e30ca 5073
0114d602 5074 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 5075 if (is_anonymous)
0114d602
DJ
5076 {
5077 const char *previous_prefix = determine_prefix (die, cu);
794684b6 5078 cp_add_using_directive (previous_prefix, TYPE_NAME (type));
0114d602 5079 }
5c4e30ca 5080 }
9219021c 5081
639d11d3 5082 if (die->child != NULL)
d9fa45fe 5083 {
639d11d3 5084 struct die_info *child_die = die->child;
d9fa45fe
DC
5085
5086 while (child_die && child_die->tag)
5087 {
e7c27a73 5088 process_die (child_die, cu);
d9fa45fe
DC
5089 child_die = sibling_die (child_die);
5090 }
5091 }
38d518c9
EZ
5092}
5093
5d7cb8df
JK
5094/* Read a Fortran module. */
5095
5096static void
5097read_module (struct die_info *die, struct dwarf2_cu *cu)
5098{
5099 struct die_info *child_die = die->child;
5100
5101 /* FIXME: Support the separate Fortran module namespaces. */
5102
5103 while (child_die && child_die->tag)
5104 {
5105 process_die (child_die, cu);
5106 child_die = sibling_die (child_die);
5107 }
5108}
5109
38d518c9
EZ
5110/* Return the name of the namespace represented by DIE. Set
5111 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
5112 namespace. */
5113
5114static const char *
e142c38c 5115namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
5116{
5117 struct die_info *current_die;
5118 const char *name = NULL;
5119
5120 /* Loop through the extensions until we find a name. */
5121
5122 for (current_die = die;
5123 current_die != NULL;
f2f0e013 5124 current_die = dwarf2_extension (die, &cu))
38d518c9 5125 {
e142c38c 5126 name = dwarf2_name (current_die, cu);
38d518c9
EZ
5127 if (name != NULL)
5128 break;
5129 }
5130
5131 /* Is it an anonymous namespace? */
5132
5133 *is_anonymous = (name == NULL);
5134 if (*is_anonymous)
5135 name = "(anonymous namespace)";
5136
5137 return name;
d9fa45fe
DC
5138}
5139
c906108c
SS
5140/* Extract all information from a DW_TAG_pointer_type DIE and add to
5141 the user defined type vector. */
5142
f792889a 5143static struct type *
e7c27a73 5144read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5145{
5e2b427d 5146 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 5147 struct comp_unit_head *cu_header = &cu->header;
c906108c 5148 struct type *type;
8b2dbe47
KB
5149 struct attribute *attr_byte_size;
5150 struct attribute *attr_address_class;
5151 int byte_size, addr_class;
c906108c 5152
e7c27a73 5153 type = lookup_pointer_type (die_type (die, cu));
8b2dbe47 5154
e142c38c 5155 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
5156 if (attr_byte_size)
5157 byte_size = DW_UNSND (attr_byte_size);
c906108c 5158 else
8b2dbe47
KB
5159 byte_size = cu_header->addr_size;
5160
e142c38c 5161 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
5162 if (attr_address_class)
5163 addr_class = DW_UNSND (attr_address_class);
5164 else
5165 addr_class = DW_ADDR_none;
5166
5167 /* If the pointer size or address class is different than the
5168 default, create a type variant marked as such and set the
5169 length accordingly. */
5170 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 5171 {
5e2b427d 5172 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
5173 {
5174 int type_flags;
5175
849957d9 5176 type_flags = gdbarch_address_class_type_flags
5e2b427d 5177 (gdbarch, byte_size, addr_class);
876cecd0
TT
5178 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
5179 == 0);
8b2dbe47
KB
5180 type = make_type_with_address_space (type, type_flags);
5181 }
5182 else if (TYPE_LENGTH (type) != byte_size)
5183 {
e2e0b3e5 5184 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47
KB
5185 }
5186 else {
5187 /* Should we also complain about unhandled address classes? */
5188 }
c906108c 5189 }
8b2dbe47
KB
5190
5191 TYPE_LENGTH (type) = byte_size;
f792889a 5192 return set_die_type (die, type, cu);
c906108c
SS
5193}
5194
5195/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
5196 the user defined type vector. */
5197
f792889a 5198static struct type *
e7c27a73 5199read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5200{
e7c27a73 5201 struct objfile *objfile = cu->objfile;
c906108c
SS
5202 struct type *type;
5203 struct type *to_type;
5204 struct type *domain;
5205
e7c27a73
DJ
5206 to_type = die_type (die, cu);
5207 domain = die_containing_type (die, cu);
0d5de010
DJ
5208
5209 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
5210 type = lookup_methodptr_type (to_type);
5211 else
5212 type = lookup_memberptr_type (to_type, domain);
c906108c 5213
f792889a 5214 return set_die_type (die, type, cu);
c906108c
SS
5215}
5216
5217/* Extract all information from a DW_TAG_reference_type DIE and add to
5218 the user defined type vector. */
5219
f792889a 5220static struct type *
e7c27a73 5221read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5222{
e7c27a73 5223 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
5224 struct type *type;
5225 struct attribute *attr;
5226
e7c27a73 5227 type = lookup_reference_type (die_type (die, cu));
e142c38c 5228 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
5229 if (attr)
5230 {
5231 TYPE_LENGTH (type) = DW_UNSND (attr);
5232 }
5233 else
5234 {
107d2387 5235 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 5236 }
f792889a 5237 return set_die_type (die, type, cu);
c906108c
SS
5238}
5239
f792889a 5240static struct type *
e7c27a73 5241read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5242{
f792889a 5243 struct type *base_type, *cv_type;
c906108c 5244
e7c27a73 5245 base_type = die_type (die, cu);
f792889a
DJ
5246 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
5247 return set_die_type (die, cv_type, cu);
c906108c
SS
5248}
5249
f792889a 5250static struct type *
e7c27a73 5251read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5252{
f792889a 5253 struct type *base_type, *cv_type;
c906108c 5254
e7c27a73 5255 base_type = die_type (die, cu);
f792889a
DJ
5256 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
5257 return set_die_type (die, cv_type, cu);
c906108c
SS
5258}
5259
5260/* Extract all information from a DW_TAG_string_type DIE and add to
5261 the user defined type vector. It isn't really a user defined type,
5262 but it behaves like one, with other DIE's using an AT_user_def_type
5263 attribute to reference it. */
5264
f792889a 5265static struct type *
e7c27a73 5266read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5267{
e7c27a73 5268 struct objfile *objfile = cu->objfile;
3b7538c0 5269 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
5270 struct type *type, *range_type, *index_type, *char_type;
5271 struct attribute *attr;
5272 unsigned int length;
5273
e142c38c 5274 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
5275 if (attr)
5276 {
5277 length = DW_UNSND (attr);
5278 }
5279 else
5280 {
b21b22e0 5281 /* check for the DW_AT_byte_size attribute */
e142c38c 5282 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
5283 if (attr)
5284 {
5285 length = DW_UNSND (attr);
5286 }
5287 else
5288 {
5289 length = 1;
5290 }
c906108c 5291 }
6ccb9162 5292
46bf5051 5293 index_type = objfile_type (objfile)->builtin_int;
c906108c 5294 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
5295 char_type = language_string_char_type (cu->language_defn, gdbarch);
5296 type = create_string_type (NULL, char_type, range_type);
6ccb9162 5297
f792889a 5298 return set_die_type (die, type, cu);
c906108c
SS
5299}
5300
5301/* Handle DIES due to C code like:
5302
5303 struct foo
c5aa993b
JM
5304 {
5305 int (*funcp)(int a, long l);
5306 int b;
5307 };
c906108c
SS
5308
5309 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 5310 */
c906108c 5311
f792889a 5312static struct type *
e7c27a73 5313read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5314{
5315 struct type *type; /* Type that this function returns */
5316 struct type *ftype; /* Function that returns above type */
5317 struct attribute *attr;
5318
e7c27a73 5319 type = die_type (die, cu);
0c8b41f1 5320 ftype = lookup_function_type (type);
c906108c 5321
5b8101ae 5322 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 5323 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 5324 if ((attr && (DW_UNSND (attr) != 0))
987504bb 5325 || cu->language == language_cplus
5b8101ae
PM
5326 || cu->language == language_java
5327 || cu->language == language_pascal)
876cecd0 5328 TYPE_PROTOTYPED (ftype) = 1;
c906108c 5329
c055b101
CV
5330 /* Store the calling convention in the type if it's available in
5331 the subroutine die. Otherwise set the calling convention to
5332 the default value DW_CC_normal. */
5333 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
5334 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
5335
639d11d3 5336 if (die->child != NULL)
c906108c
SS
5337 {
5338 struct die_info *child_die;
5339 int nparams = 0;
5340 int iparams = 0;
5341
5342 /* Count the number of parameters.
5343 FIXME: GDB currently ignores vararg functions, but knows about
5344 vararg member functions. */
639d11d3 5345 child_die = die->child;
c906108c
SS
5346 while (child_die && child_die->tag)
5347 {
5348 if (child_die->tag == DW_TAG_formal_parameter)
5349 nparams++;
5350 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 5351 TYPE_VARARGS (ftype) = 1;
c906108c
SS
5352 child_die = sibling_die (child_die);
5353 }
5354
5355 /* Allocate storage for parameters and fill them in. */
5356 TYPE_NFIELDS (ftype) = nparams;
5357 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 5358 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 5359
639d11d3 5360 child_die = die->child;
c906108c
SS
5361 while (child_die && child_die->tag)
5362 {
5363 if (child_die->tag == DW_TAG_formal_parameter)
5364 {
5365 /* Dwarf2 has no clean way to discern C++ static and non-static
c5aa993b
JM
5366 member functions. G++ helps GDB by marking the first
5367 parameter for non-static member functions (which is the
5368 this pointer) as artificial. We pass this information
5369 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
e142c38c 5370 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
5371 if (attr)
5372 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
5373 else
5374 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
e7c27a73 5375 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
c906108c
SS
5376 iparams++;
5377 }
5378 child_die = sibling_die (child_die);
5379 }
5380 }
5381
f792889a 5382 return set_die_type (die, ftype, cu);
c906108c
SS
5383}
5384
f792889a 5385static struct type *
e7c27a73 5386read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5387{
e7c27a73 5388 struct objfile *objfile = cu->objfile;
2f038fcb 5389 struct attribute *attr;
0114d602 5390 const char *name = NULL;
f792889a 5391 struct type *this_type;
c906108c 5392
0114d602 5393 name = dwarf2_full_name (die, cu);
f792889a 5394 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
5395 TYPE_FLAG_TARGET_STUB, NULL, objfile);
5396 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
5397 set_die_type (die, this_type, cu);
5398 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
5399 return this_type;
c906108c
SS
5400}
5401
5402/* Find a representation of a given base type and install
5403 it in the TYPE field of the die. */
5404
f792889a 5405static struct type *
e7c27a73 5406read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5407{
e7c27a73 5408 struct objfile *objfile = cu->objfile;
c906108c
SS
5409 struct type *type;
5410 struct attribute *attr;
5411 int encoding = 0, size = 0;
39cbfefa 5412 char *name;
6ccb9162
UW
5413 enum type_code code = TYPE_CODE_INT;
5414 int type_flags = 0;
5415 struct type *target_type = NULL;
c906108c 5416
e142c38c 5417 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
5418 if (attr)
5419 {
5420 encoding = DW_UNSND (attr);
5421 }
e142c38c 5422 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
5423 if (attr)
5424 {
5425 size = DW_UNSND (attr);
5426 }
39cbfefa 5427 name = dwarf2_name (die, cu);
6ccb9162 5428 if (!name)
c906108c 5429 {
6ccb9162
UW
5430 complaint (&symfile_complaints,
5431 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 5432 }
6ccb9162
UW
5433
5434 switch (encoding)
c906108c 5435 {
6ccb9162
UW
5436 case DW_ATE_address:
5437 /* Turn DW_ATE_address into a void * pointer. */
5438 code = TYPE_CODE_PTR;
5439 type_flags |= TYPE_FLAG_UNSIGNED;
5440 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
5441 break;
5442 case DW_ATE_boolean:
5443 code = TYPE_CODE_BOOL;
5444 type_flags |= TYPE_FLAG_UNSIGNED;
5445 break;
5446 case DW_ATE_complex_float:
5447 code = TYPE_CODE_COMPLEX;
5448 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
5449 break;
5450 case DW_ATE_decimal_float:
5451 code = TYPE_CODE_DECFLOAT;
5452 break;
5453 case DW_ATE_float:
5454 code = TYPE_CODE_FLT;
5455 break;
5456 case DW_ATE_signed:
5457 break;
5458 case DW_ATE_unsigned:
5459 type_flags |= TYPE_FLAG_UNSIGNED;
5460 break;
5461 case DW_ATE_signed_char:
868a0084
PM
5462 if (cu->language == language_ada || cu->language == language_m2
5463 || cu->language == language_pascal)
6ccb9162
UW
5464 code = TYPE_CODE_CHAR;
5465 break;
5466 case DW_ATE_unsigned_char:
868a0084
PM
5467 if (cu->language == language_ada || cu->language == language_m2
5468 || cu->language == language_pascal)
6ccb9162
UW
5469 code = TYPE_CODE_CHAR;
5470 type_flags |= TYPE_FLAG_UNSIGNED;
5471 break;
5472 default:
5473 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
5474 dwarf_type_encoding_name (encoding));
5475 break;
c906108c 5476 }
6ccb9162 5477
0114d602
DJ
5478 type = init_type (code, size, type_flags, NULL, objfile);
5479 TYPE_NAME (type) = name;
6ccb9162
UW
5480 TYPE_TARGET_TYPE (type) = target_type;
5481
0114d602 5482 if (name && strcmp (name, "char") == 0)
876cecd0 5483 TYPE_NOSIGN (type) = 1;
0114d602 5484
f792889a 5485 return set_die_type (die, type, cu);
c906108c
SS
5486}
5487
a02abb62
JB
5488/* Read the given DW_AT_subrange DIE. */
5489
f792889a 5490static struct type *
a02abb62
JB
5491read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
5492{
5e2b427d 5493 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
5494 struct type *base_type;
5495 struct type *range_type;
5496 struct attribute *attr;
5497 int low = 0;
5498 int high = -1;
39cbfefa 5499 char *name;
a02abb62 5500
a02abb62 5501 base_type = die_type (die, cu);
3d1f72c2 5502 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
a02abb62
JB
5503 {
5504 complaint (&symfile_complaints,
e2e0b3e5 5505 _("DW_AT_type missing from DW_TAG_subrange_type"));
17a912b6 5506 base_type
5e2b427d 5507 = init_type (TYPE_CODE_INT, gdbarch_addr_bit (gdbarch) / 8,
6ccb9162 5508 0, NULL, cu->objfile);
a02abb62
JB
5509 }
5510
e142c38c 5511 if (cu->language == language_fortran)
a02abb62
JB
5512 {
5513 /* FORTRAN implies a lower bound of 1, if not given. */
5514 low = 1;
5515 }
5516
dd5e6932
DJ
5517 /* FIXME: For variable sized arrays either of these could be
5518 a variable rather than a constant value. We'll allow it,
5519 but we don't know how to handle it. */
e142c38c 5520 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
5521 if (attr)
5522 low = dwarf2_get_attr_constant_value (attr, 0);
5523
e142c38c 5524 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62
JB
5525 if (attr)
5526 {
5527 if (attr->form == DW_FORM_block1)
5528 {
5529 /* GCC encodes arrays with unspecified or dynamic length
5530 with a DW_FORM_block1 attribute.
5531 FIXME: GDB does not yet know how to handle dynamic
5532 arrays properly, treat them as arrays with unspecified
5533 length for now.
5534
5535 FIXME: jimb/2003-09-22: GDB does not really know
5536 how to handle arrays of unspecified length
5537 either; we just represent them as zero-length
5538 arrays. Choose an appropriate upper bound given
5539 the lower bound we've computed above. */
5540 high = low - 1;
5541 }
5542 else
5543 high = dwarf2_get_attr_constant_value (attr, 1);
5544 }
5545
5546 range_type = create_range_type (NULL, base_type, low, high);
5547
39cbfefa
DJ
5548 name = dwarf2_name (die, cu);
5549 if (name)
5550 TYPE_NAME (range_type) = name;
a02abb62 5551
e142c38c 5552 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
5553 if (attr)
5554 TYPE_LENGTH (range_type) = DW_UNSND (attr);
5555
f792889a 5556 return set_die_type (die, range_type, cu);
a02abb62
JB
5557}
5558
f792889a 5559static struct type *
81a17f79
JB
5560read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
5561{
5562 struct type *type;
81a17f79 5563
81a17f79
JB
5564 /* For now, we only support the C meaning of an unspecified type: void. */
5565
0114d602
DJ
5566 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
5567 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 5568
f792889a 5569 return set_die_type (die, type, cu);
81a17f79 5570}
a02abb62 5571
51545339
DJ
5572/* Trivial hash function for die_info: the hash value of a DIE
5573 is its offset in .debug_info for this objfile. */
5574
5575static hashval_t
5576die_hash (const void *item)
5577{
5578 const struct die_info *die = item;
5579 return die->offset;
5580}
5581
5582/* Trivial comparison function for die_info structures: two DIEs
5583 are equal if they have the same offset. */
5584
5585static int
5586die_eq (const void *item_lhs, const void *item_rhs)
5587{
5588 const struct die_info *die_lhs = item_lhs;
5589 const struct die_info *die_rhs = item_rhs;
5590 return die_lhs->offset == die_rhs->offset;
5591}
5592
c906108c
SS
5593/* Read a whole compilation unit into a linked list of dies. */
5594
f9aca02d 5595static struct die_info *
fe1b8b76 5596read_comp_unit (gdb_byte *info_ptr, bfd *abfd, struct dwarf2_cu *cu)
c906108c 5597{
51545339
DJ
5598 cu->die_hash
5599 = htab_create_alloc_ex (cu->header.length / 12,
5600 die_hash,
5601 die_eq,
5602 NULL,
5603 &cu->comp_unit_obstack,
5604 hashtab_obstack_allocate,
5605 dummy_obstack_deallocate);
5606
e7c27a73 5607 return read_die_and_children (info_ptr, abfd, cu, &info_ptr, NULL);
639d11d3
DC
5608}
5609
d97bc12b
DE
5610/* Main entry point for reading a DIE and all children.
5611 Read the DIE and dump it if requested. */
5612
5613static struct die_info *
5614read_die_and_children (gdb_byte *info_ptr, bfd *abfd,
5615 struct dwarf2_cu *cu,
5616 gdb_byte **new_info_ptr,
5617 struct die_info *parent)
5618{
5619 struct die_info *result = read_die_and_children_1 (info_ptr, abfd, cu,
5620 new_info_ptr, parent);
5621
5622 if (dwarf2_die_debug)
5623 {
5624 fprintf_unfiltered (gdb_stdlog, "Read die from .debug_info:\n");
5625 dump_die (result, dwarf2_die_debug);
5626 }
5627
5628 return result;
5629}
5630
639d11d3
DC
5631/* Read a single die and all its descendents. Set the die's sibling
5632 field to NULL; set other fields in the die correctly, and set all
5633 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
5634 location of the info_ptr after reading all of those dies. PARENT
5635 is the parent of the die in question. */
5636
5637static struct die_info *
d97bc12b
DE
5638read_die_and_children_1 (gdb_byte *info_ptr, bfd *abfd,
5639 struct dwarf2_cu *cu,
5640 gdb_byte **new_info_ptr,
5641 struct die_info *parent)
639d11d3
DC
5642{
5643 struct die_info *die;
fe1b8b76 5644 gdb_byte *cur_ptr;
639d11d3
DC
5645 int has_children;
5646
e7c27a73 5647 cur_ptr = read_full_die (&die, abfd, info_ptr, cu, &has_children);
1d325ec1
DJ
5648 if (die == NULL)
5649 {
5650 *new_info_ptr = cur_ptr;
5651 return NULL;
5652 }
51545339 5653 store_in_ref_table (die, cu);
639d11d3
DC
5654
5655 if (has_children)
5656 {
e7c27a73 5657 die->child = read_die_and_siblings (cur_ptr, abfd, cu,
639d11d3
DC
5658 new_info_ptr, die);
5659 }
5660 else
5661 {
5662 die->child = NULL;
5663 *new_info_ptr = cur_ptr;
5664 }
5665
5666 die->sibling = NULL;
5667 die->parent = parent;
5668 return die;
5669}
5670
5671/* Read a die, all of its descendents, and all of its siblings; set
5672 all of the fields of all of the dies correctly. Arguments are as
5673 in read_die_and_children. */
5674
5675static struct die_info *
fe1b8b76 5676read_die_and_siblings (gdb_byte *info_ptr, bfd *abfd,
e7c27a73 5677 struct dwarf2_cu *cu,
fe1b8b76 5678 gdb_byte **new_info_ptr,
639d11d3
DC
5679 struct die_info *parent)
5680{
5681 struct die_info *first_die, *last_sibling;
fe1b8b76 5682 gdb_byte *cur_ptr;
639d11d3 5683
c906108c 5684 cur_ptr = info_ptr;
639d11d3
DC
5685 first_die = last_sibling = NULL;
5686
5687 while (1)
c906108c 5688 {
639d11d3 5689 struct die_info *die
d97bc12b 5690 = read_die_and_children_1 (cur_ptr, abfd, cu, &cur_ptr, parent);
639d11d3 5691
1d325ec1 5692 if (die == NULL)
c906108c 5693 {
639d11d3
DC
5694 *new_info_ptr = cur_ptr;
5695 return first_die;
c906108c 5696 }
1d325ec1
DJ
5697
5698 if (!first_die)
5699 first_die = die;
c906108c 5700 else
1d325ec1
DJ
5701 last_sibling->sibling = die;
5702
5703 last_sibling = die;
c906108c 5704 }
c906108c
SS
5705}
5706
c906108c
SS
5707/* In DWARF version 2, the description of the debugging information is
5708 stored in a separate .debug_abbrev section. Before we read any
5709 dies from a section we read in all abbreviations and install them
72bf9492
DJ
5710 in a hash table. This function also sets flags in CU describing
5711 the data found in the abbrev table. */
c906108c
SS
5712
5713static void
e7c27a73 5714dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 5715{
e7c27a73 5716 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 5717 gdb_byte *abbrev_ptr;
c906108c
SS
5718 struct abbrev_info *cur_abbrev;
5719 unsigned int abbrev_number, bytes_read, abbrev_name;
5720 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
5721 struct attr_abbrev *cur_attrs;
5722 unsigned int allocated_attrs;
c906108c 5723
57349743 5724 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
5725 obstack_init (&cu->abbrev_obstack);
5726 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
5727 (ABBREV_HASH_SIZE
5728 * sizeof (struct abbrev_info *)));
5729 memset (cu->dwarf2_abbrevs, 0,
5730 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 5731
dce234bc 5732 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
c906108c
SS
5733 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5734 abbrev_ptr += bytes_read;
5735
f3dd6933
DJ
5736 allocated_attrs = ATTR_ALLOC_CHUNK;
5737 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
5738
c906108c
SS
5739 /* loop until we reach an abbrev number of 0 */
5740 while (abbrev_number)
5741 {
f3dd6933 5742 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
5743
5744 /* read in abbrev header */
5745 cur_abbrev->number = abbrev_number;
5746 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5747 abbrev_ptr += bytes_read;
5748 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
5749 abbrev_ptr += 1;
5750
72bf9492
DJ
5751 if (cur_abbrev->tag == DW_TAG_namespace)
5752 cu->has_namespace_info = 1;
5753
c906108c
SS
5754 /* now read in declarations */
5755 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5756 abbrev_ptr += bytes_read;
5757 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5758 abbrev_ptr += bytes_read;
5759 while (abbrev_name)
5760 {
f3dd6933 5761 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 5762 {
f3dd6933
DJ
5763 allocated_attrs += ATTR_ALLOC_CHUNK;
5764 cur_attrs
5765 = xrealloc (cur_attrs, (allocated_attrs
5766 * sizeof (struct attr_abbrev)));
c906108c 5767 }
ae038cb0
DJ
5768
5769 /* Record whether this compilation unit might have
5770 inter-compilation-unit references. If we don't know what form
5771 this attribute will have, then it might potentially be a
5772 DW_FORM_ref_addr, so we conservatively expect inter-CU
5773 references. */
5774
5775 if (abbrev_form == DW_FORM_ref_addr
5776 || abbrev_form == DW_FORM_indirect)
5777 cu->has_form_ref_addr = 1;
5778
f3dd6933
DJ
5779 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
5780 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
5781 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5782 abbrev_ptr += bytes_read;
5783 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5784 abbrev_ptr += bytes_read;
5785 }
5786
f3dd6933
DJ
5787 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
5788 (cur_abbrev->num_attrs
5789 * sizeof (struct attr_abbrev)));
5790 memcpy (cur_abbrev->attrs, cur_attrs,
5791 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
5792
c906108c 5793 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
5794 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
5795 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
5796
5797 /* Get next abbreviation.
5798 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
5799 always properly terminated with an abbrev number of 0.
5800 Exit loop if we encounter an abbreviation which we have
5801 already read (which means we are about to read the abbreviations
5802 for the next compile unit) or if the end of the abbreviation
5803 table is reached. */
dce234bc
PP
5804 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
5805 >= dwarf2_per_objfile->abbrev.size)
c906108c
SS
5806 break;
5807 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5808 abbrev_ptr += bytes_read;
e7c27a73 5809 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
5810 break;
5811 }
f3dd6933
DJ
5812
5813 xfree (cur_attrs);
c906108c
SS
5814}
5815
f3dd6933 5816/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 5817
c906108c 5818static void
f3dd6933 5819dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 5820{
f3dd6933 5821 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 5822
f3dd6933
DJ
5823 obstack_free (&cu->abbrev_obstack, NULL);
5824 cu->dwarf2_abbrevs = NULL;
c906108c
SS
5825}
5826
5827/* Lookup an abbrev_info structure in the abbrev hash table. */
5828
5829static struct abbrev_info *
e7c27a73 5830dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
5831{
5832 unsigned int hash_number;
5833 struct abbrev_info *abbrev;
5834
5835 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 5836 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
5837
5838 while (abbrev)
5839 {
5840 if (abbrev->number == number)
5841 return abbrev;
5842 else
5843 abbrev = abbrev->next;
5844 }
5845 return NULL;
5846}
5847
72bf9492
DJ
5848/* Returns nonzero if TAG represents a type that we might generate a partial
5849 symbol for. */
5850
5851static int
5852is_type_tag_for_partial (int tag)
5853{
5854 switch (tag)
5855 {
5856#if 0
5857 /* Some types that would be reasonable to generate partial symbols for,
5858 that we don't at present. */
5859 case DW_TAG_array_type:
5860 case DW_TAG_file_type:
5861 case DW_TAG_ptr_to_member_type:
5862 case DW_TAG_set_type:
5863 case DW_TAG_string_type:
5864 case DW_TAG_subroutine_type:
5865#endif
5866 case DW_TAG_base_type:
5867 case DW_TAG_class_type:
680b30c7 5868 case DW_TAG_interface_type:
72bf9492
DJ
5869 case DW_TAG_enumeration_type:
5870 case DW_TAG_structure_type:
5871 case DW_TAG_subrange_type:
5872 case DW_TAG_typedef:
5873 case DW_TAG_union_type:
5874 return 1;
5875 default:
5876 return 0;
5877 }
5878}
5879
5880/* Load all DIEs that are interesting for partial symbols into memory. */
5881
5882static struct partial_die_info *
fe1b8b76 5883load_partial_dies (bfd *abfd, gdb_byte *info_ptr, int building_psymtab,
72bf9492
DJ
5884 struct dwarf2_cu *cu)
5885{
5886 struct partial_die_info *part_die;
5887 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
5888 struct abbrev_info *abbrev;
5889 unsigned int bytes_read;
5afb4e99 5890 unsigned int load_all = 0;
72bf9492
DJ
5891
5892 int nesting_level = 1;
5893
5894 parent_die = NULL;
5895 last_die = NULL;
5896
5afb4e99
DJ
5897 if (cu->per_cu && cu->per_cu->load_all_dies)
5898 load_all = 1;
5899
72bf9492
DJ
5900 cu->partial_dies
5901 = htab_create_alloc_ex (cu->header.length / 12,
5902 partial_die_hash,
5903 partial_die_eq,
5904 NULL,
5905 &cu->comp_unit_obstack,
5906 hashtab_obstack_allocate,
5907 dummy_obstack_deallocate);
5908
5909 part_die = obstack_alloc (&cu->comp_unit_obstack,
5910 sizeof (struct partial_die_info));
5911
5912 while (1)
5913 {
5914 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5915
5916 /* A NULL abbrev means the end of a series of children. */
5917 if (abbrev == NULL)
5918 {
5919 if (--nesting_level == 0)
5920 {
5921 /* PART_DIE was probably the last thing allocated on the
5922 comp_unit_obstack, so we could call obstack_free
5923 here. We don't do that because the waste is small,
5924 and will be cleaned up when we're done with this
5925 compilation unit. This way, we're also more robust
5926 against other users of the comp_unit_obstack. */
5927 return first_die;
5928 }
5929 info_ptr += bytes_read;
5930 last_die = parent_die;
5931 parent_die = parent_die->die_parent;
5932 continue;
5933 }
5934
5afb4e99
DJ
5935 /* Check whether this DIE is interesting enough to save. Normally
5936 we would not be interested in members here, but there may be
5937 later variables referencing them via DW_AT_specification (for
5938 static members). */
5939 if (!load_all
5940 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
5941 && abbrev->tag != DW_TAG_enumerator
5942 && abbrev->tag != DW_TAG_subprogram
bc30ff58 5943 && abbrev->tag != DW_TAG_lexical_block
72bf9492 5944 && abbrev->tag != DW_TAG_variable
5afb4e99
DJ
5945 && abbrev->tag != DW_TAG_namespace
5946 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
5947 {
5948 /* Otherwise we skip to the next sibling, if any. */
5949 info_ptr = skip_one_die (info_ptr + bytes_read, abbrev, cu);
5950 continue;
5951 }
5952
5953 info_ptr = read_partial_die (part_die, abbrev, bytes_read,
5954 abfd, info_ptr, cu);
5955
5956 /* This two-pass algorithm for processing partial symbols has a
5957 high cost in cache pressure. Thus, handle some simple cases
5958 here which cover the majority of C partial symbols. DIEs
5959 which neither have specification tags in them, nor could have
5960 specification tags elsewhere pointing at them, can simply be
5961 processed and discarded.
5962
5963 This segment is also optional; scan_partial_symbols and
5964 add_partial_symbol will handle these DIEs if we chain
5965 them in normally. When compilers which do not emit large
5966 quantities of duplicate debug information are more common,
5967 this code can probably be removed. */
5968
5969 /* Any complete simple types at the top level (pretty much all
5970 of them, for a language without namespaces), can be processed
5971 directly. */
5972 if (parent_die == NULL
5973 && part_die->has_specification == 0
5974 && part_die->is_declaration == 0
5975 && (part_die->tag == DW_TAG_typedef
5976 || part_die->tag == DW_TAG_base_type
5977 || part_die->tag == DW_TAG_subrange_type))
5978 {
5979 if (building_psymtab && part_die->name != NULL)
5980 add_psymbol_to_list (part_die->name, strlen (part_die->name),
5981 VAR_DOMAIN, LOC_TYPEDEF,
5982 &cu->objfile->static_psymbols,
5983 0, (CORE_ADDR) 0, cu->language, cu->objfile);
5984 info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5985 continue;
5986 }
5987
5988 /* If we're at the second level, and we're an enumerator, and
5989 our parent has no specification (meaning possibly lives in a
5990 namespace elsewhere), then we can add the partial symbol now
5991 instead of queueing it. */
5992 if (part_die->tag == DW_TAG_enumerator
5993 && parent_die != NULL
5994 && parent_die->die_parent == NULL
5995 && parent_die->tag == DW_TAG_enumeration_type
5996 && parent_die->has_specification == 0)
5997 {
5998 if (part_die->name == NULL)
e2e0b3e5 5999 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492
DJ
6000 else if (building_psymtab)
6001 add_psymbol_to_list (part_die->name, strlen (part_die->name),
6002 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
6003 (cu->language == language_cplus
6004 || cu->language == language_java)
72bf9492
DJ
6005 ? &cu->objfile->global_psymbols
6006 : &cu->objfile->static_psymbols,
6007 0, (CORE_ADDR) 0, cu->language, cu->objfile);
6008
6009 info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
6010 continue;
6011 }
6012
6013 /* We'll save this DIE so link it in. */
6014 part_die->die_parent = parent_die;
6015 part_die->die_sibling = NULL;
6016 part_die->die_child = NULL;
6017
6018 if (last_die && last_die == parent_die)
6019 last_die->die_child = part_die;
6020 else if (last_die)
6021 last_die->die_sibling = part_die;
6022
6023 last_die = part_die;
6024
6025 if (first_die == NULL)
6026 first_die = part_die;
6027
6028 /* Maybe add the DIE to the hash table. Not all DIEs that we
6029 find interesting need to be in the hash table, because we
6030 also have the parent/sibling/child chains; only those that we
6031 might refer to by offset later during partial symbol reading.
6032
6033 For now this means things that might have be the target of a
6034 DW_AT_specification, DW_AT_abstract_origin, or
6035 DW_AT_extension. DW_AT_extension will refer only to
6036 namespaces; DW_AT_abstract_origin refers to functions (and
6037 many things under the function DIE, but we do not recurse
6038 into function DIEs during partial symbol reading) and
6039 possibly variables as well; DW_AT_specification refers to
6040 declarations. Declarations ought to have the DW_AT_declaration
6041 flag. It happens that GCC forgets to put it in sometimes, but
6042 only for functions, not for types.
6043
6044 Adding more things than necessary to the hash table is harmless
6045 except for the performance cost. Adding too few will result in
5afb4e99
DJ
6046 wasted time in find_partial_die, when we reread the compilation
6047 unit with load_all_dies set. */
72bf9492 6048
5afb4e99
DJ
6049 if (load_all
6050 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
6051 || abbrev->tag == DW_TAG_variable
6052 || abbrev->tag == DW_TAG_namespace
6053 || part_die->is_declaration)
6054 {
6055 void **slot;
6056
6057 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
6058 part_die->offset, INSERT);
6059 *slot = part_die;
6060 }
6061
6062 part_die = obstack_alloc (&cu->comp_unit_obstack,
6063 sizeof (struct partial_die_info));
6064
6065 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 6066 we have no reason to follow the children of structures; for other
72bf9492 6067 languages we have to, both so that we can get at method physnames
bc30ff58
JB
6068 to infer fully qualified class names, and for DW_AT_specification.
6069
6070 For Ada, we need to scan the children of subprograms and lexical
6071 blocks as well because Ada allows the definition of nested
6072 entities that could be interesting for the debugger, such as
6073 nested subprograms for instance. */
72bf9492 6074 if (last_die->has_children
5afb4e99
DJ
6075 && (load_all
6076 || last_die->tag == DW_TAG_namespace
72bf9492
DJ
6077 || last_die->tag == DW_TAG_enumeration_type
6078 || (cu->language != language_c
6079 && (last_die->tag == DW_TAG_class_type
680b30c7 6080 || last_die->tag == DW_TAG_interface_type
72bf9492 6081 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
6082 || last_die->tag == DW_TAG_union_type))
6083 || (cu->language == language_ada
6084 && (last_die->tag == DW_TAG_subprogram
6085 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
6086 {
6087 nesting_level++;
6088 parent_die = last_die;
6089 continue;
6090 }
6091
6092 /* Otherwise we skip to the next sibling, if any. */
6093 info_ptr = locate_pdi_sibling (last_die, info_ptr, abfd, cu);
6094
6095 /* Back to the top, do it again. */
6096 }
6097}
6098
c906108c
SS
6099/* Read a minimal amount of information into the minimal die structure. */
6100
fe1b8b76 6101static gdb_byte *
72bf9492
DJ
6102read_partial_die (struct partial_die_info *part_die,
6103 struct abbrev_info *abbrev,
6104 unsigned int abbrev_len, bfd *abfd,
fe1b8b76 6105 gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 6106{
72bf9492 6107 unsigned int bytes_read, i;
c906108c 6108 struct attribute attr;
c5aa993b 6109 int has_low_pc_attr = 0;
c906108c 6110 int has_high_pc_attr = 0;
e7030f15 6111 CORE_ADDR base_address = 0;
85cbf3d3
JK
6112 enum
6113 {
6114 base_address_none,
6115 base_address_low_pc,
6116 /* Overrides BASE_ADDRESS_LOW_PC. */
6117 base_address_entry_pc
6118 }
6119 base_address_type = base_address_none;
c906108c 6120
72bf9492 6121 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 6122
dce234bc 6123 part_die->offset = info_ptr - dwarf2_per_objfile->info.buffer;
72bf9492
DJ
6124
6125 info_ptr += abbrev_len;
6126
6127 if (abbrev == NULL)
6128 return info_ptr;
6129
c906108c
SS
6130 part_die->tag = abbrev->tag;
6131 part_die->has_children = abbrev->has_children;
c906108c
SS
6132
6133 for (i = 0; i < abbrev->num_attrs; ++i)
6134 {
e7c27a73 6135 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
6136
6137 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 6138 partial symbol table. */
c906108c
SS
6139 switch (attr.name)
6140 {
6141 case DW_AT_name:
71c25dea
TT
6142 switch (part_die->tag)
6143 {
6144 case DW_TAG_compile_unit:
6145 /* Compilation units have a DW_AT_name that is a filename, not
6146 a source language identifier. */
6147 case DW_TAG_enumeration_type:
6148 case DW_TAG_enumerator:
6149 /* These tags always have simple identifiers already; no need
6150 to canonicalize them. */
6151 part_die->name = DW_STRING (&attr);
6152 break;
6153 default:
6154 part_die->name
6155 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
6156 &cu->comp_unit_obstack);
6157 break;
6158 }
c906108c 6159 break;
57c22c6c
BR
6160 case DW_AT_comp_dir:
6161 if (part_die->dirname == NULL)
6162 part_die->dirname = DW_STRING (&attr);
6163 break;
c906108c
SS
6164 case DW_AT_MIPS_linkage_name:
6165 part_die->name = DW_STRING (&attr);
6166 break;
6167 case DW_AT_low_pc:
6168 has_low_pc_attr = 1;
6169 part_die->lowpc = DW_ADDR (&attr);
85cbf3d3
JK
6170 if (part_die->tag == DW_TAG_compile_unit
6171 && base_address_type < base_address_low_pc)
6172 {
6173 base_address = DW_ADDR (&attr);
6174 base_address_type = base_address_low_pc;
6175 }
c906108c
SS
6176 break;
6177 case DW_AT_high_pc:
6178 has_high_pc_attr = 1;
6179 part_die->highpc = DW_ADDR (&attr);
6180 break;
85cbf3d3
JK
6181 case DW_AT_entry_pc:
6182 if (part_die->tag == DW_TAG_compile_unit
6183 && base_address_type < base_address_entry_pc)
6184 {
6185 base_address = DW_ADDR (&attr);
6186 base_address_type = base_address_entry_pc;
6187 }
6188 break;
43039443 6189 case DW_AT_ranges:
ff013f42
JK
6190 if (part_die->tag == DW_TAG_compile_unit)
6191 {
6192 cu->ranges_offset = DW_UNSND (&attr);
6193 cu->has_ranges_offset = 1;
6194 }
43039443 6195 break;
c906108c 6196 case DW_AT_location:
8e19ed76
PS
6197 /* Support the .debug_loc offsets */
6198 if (attr_form_is_block (&attr))
6199 {
6200 part_die->locdesc = DW_BLOCK (&attr);
6201 }
3690dd37 6202 else if (attr_form_is_section_offset (&attr))
8e19ed76 6203 {
4d3c2250 6204 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6205 }
6206 else
6207 {
4d3c2250
KB
6208 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6209 "partial symbol information");
8e19ed76 6210 }
c906108c
SS
6211 break;
6212 case DW_AT_language:
6213 part_die->language = DW_UNSND (&attr);
6214 break;
6215 case DW_AT_external:
6216 part_die->is_external = DW_UNSND (&attr);
6217 break;
6218 case DW_AT_declaration:
6219 part_die->is_declaration = DW_UNSND (&attr);
6220 break;
6221 case DW_AT_type:
6222 part_die->has_type = 1;
6223 break;
6224 case DW_AT_abstract_origin:
6225 case DW_AT_specification:
72bf9492
DJ
6226 case DW_AT_extension:
6227 part_die->has_specification = 1;
c764a876 6228 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
6229 break;
6230 case DW_AT_sibling:
6231 /* Ignore absolute siblings, they might point outside of
6232 the current compile unit. */
6233 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 6234 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 6235 else
dce234bc 6236 part_die->sibling = dwarf2_per_objfile->info.buffer
c764a876 6237 + dwarf2_get_ref_die_offset (&attr);
c906108c 6238 break;
aaa75496
JB
6239 case DW_AT_stmt_list:
6240 part_die->has_stmt_list = 1;
6241 part_die->line_offset = DW_UNSND (&attr);
6242 break;
fa4028e9
JB
6243 case DW_AT_byte_size:
6244 part_die->has_byte_size = 1;
6245 break;
68511cec
CES
6246 case DW_AT_calling_convention:
6247 /* DWARF doesn't provide a way to identify a program's source-level
6248 entry point. DW_AT_calling_convention attributes are only meant
6249 to describe functions' calling conventions.
6250
6251 However, because it's a necessary piece of information in
6252 Fortran, and because DW_CC_program is the only piece of debugging
6253 information whose definition refers to a 'main program' at all,
6254 several compilers have begun marking Fortran main programs with
6255 DW_CC_program --- even when those functions use the standard
6256 calling conventions.
6257
6258 So until DWARF specifies a way to provide this information and
6259 compilers pick up the new representation, we'll support this
6260 practice. */
6261 if (DW_UNSND (&attr) == DW_CC_program
6262 && cu->language == language_fortran)
6263 set_main_name (part_die->name);
6264 break;
c906108c
SS
6265 default:
6266 break;
6267 }
6268 }
6269
c906108c
SS
6270 /* When using the GNU linker, .gnu.linkonce. sections are used to
6271 eliminate duplicate copies of functions and vtables and such.
6272 The linker will arbitrarily choose one and discard the others.
6273 The AT_*_pc values for such functions refer to local labels in
6274 these sections. If the section from that file was discarded, the
6275 labels are not in the output, so the relocs get a value of 0.
6276 If this is a discarded function, mark the pc bounds as invalid,
6277 so that GDB will ignore it. */
6278 if (has_low_pc_attr && has_high_pc_attr
6279 && part_die->lowpc < part_die->highpc
6280 && (part_die->lowpc != 0
72dca2f5 6281 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 6282 part_die->has_pc_info = 1;
85cbf3d3 6283
d00adf39 6284 if (base_address_type != base_address_none && !cu->base_known)
85cbf3d3
JK
6285 {
6286 gdb_assert (part_die->tag == DW_TAG_compile_unit);
d00adf39
DE
6287 cu->base_known = 1;
6288 cu->base_address = base_address;
85cbf3d3
JK
6289 }
6290
c906108c
SS
6291 return info_ptr;
6292}
6293
72bf9492
DJ
6294/* Find a cached partial DIE at OFFSET in CU. */
6295
6296static struct partial_die_info *
c764a876 6297find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
6298{
6299 struct partial_die_info *lookup_die = NULL;
6300 struct partial_die_info part_die;
6301
6302 part_die.offset = offset;
6303 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
6304
72bf9492
DJ
6305 return lookup_die;
6306}
6307
6308/* Find a partial DIE at OFFSET, which may or may not be in CU. */
6309
6310static struct partial_die_info *
c764a876 6311find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 6312{
5afb4e99
DJ
6313 struct dwarf2_per_cu_data *per_cu = NULL;
6314 struct partial_die_info *pd = NULL;
72bf9492 6315
45452591 6316 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
6317 {
6318 pd = find_partial_die_in_comp_unit (offset, cu);
6319 if (pd != NULL)
6320 return pd;
6321 }
72bf9492 6322
ae038cb0
DJ
6323 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
6324
ae038cb0
DJ
6325 if (per_cu->cu == NULL)
6326 {
6327 load_comp_unit (per_cu, cu->objfile);
6328 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6329 dwarf2_per_objfile->read_in_chain = per_cu;
6330 }
6331
6332 per_cu->cu->last_used = 0;
5afb4e99
DJ
6333 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6334
6335 if (pd == NULL && per_cu->load_all_dies == 0)
6336 {
6337 struct cleanup *back_to;
6338 struct partial_die_info comp_unit_die;
6339 struct abbrev_info *abbrev;
6340 unsigned int bytes_read;
6341 char *info_ptr;
6342
6343 per_cu->load_all_dies = 1;
6344
6345 /* Re-read the DIEs. */
6346 back_to = make_cleanup (null_cleanup, 0);
6347 if (per_cu->cu->dwarf2_abbrevs == NULL)
6348 {
6349 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
6350 back_to = make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
6351 }
dce234bc 6352 info_ptr = (dwarf2_per_objfile->info.buffer
d00adf39
DE
6353 + per_cu->cu->header.offset
6354 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
6355 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
6356 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
6357 per_cu->cu->objfile->obfd, info_ptr,
6358 per_cu->cu);
6359 if (comp_unit_die.has_children)
6360 load_partial_dies (per_cu->cu->objfile->obfd, info_ptr, 0, per_cu->cu);
6361 do_cleanups (back_to);
6362
6363 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6364 }
6365
6366 if (pd == NULL)
6367 internal_error (__FILE__, __LINE__,
c764a876 6368 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
6369 offset, bfd_get_filename (cu->objfile->obfd));
6370 return pd;
72bf9492
DJ
6371}
6372
6373/* Adjust PART_DIE before generating a symbol for it. This function
6374 may set the is_external flag or change the DIE's name. */
6375
6376static void
6377fixup_partial_die (struct partial_die_info *part_die,
6378 struct dwarf2_cu *cu)
6379{
6380 /* If we found a reference attribute and the DIE has no name, try
6381 to find a name in the referred to DIE. */
6382
6383 if (part_die->name == NULL && part_die->has_specification)
6384 {
6385 struct partial_die_info *spec_die;
72bf9492 6386
10b3939b 6387 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 6388
10b3939b 6389 fixup_partial_die (spec_die, cu);
72bf9492
DJ
6390
6391 if (spec_die->name)
6392 {
6393 part_die->name = spec_die->name;
6394
6395 /* Copy DW_AT_external attribute if it is set. */
6396 if (spec_die->is_external)
6397 part_die->is_external = spec_die->is_external;
6398 }
6399 }
6400
6401 /* Set default names for some unnamed DIEs. */
6402 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
6403 || part_die->tag == DW_TAG_class_type))
6404 part_die->name = "(anonymous class)";
6405
6406 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
6407 part_die->name = "(anonymous namespace)";
6408
6409 if (part_die->tag == DW_TAG_structure_type
6410 || part_die->tag == DW_TAG_class_type
6411 || part_die->tag == DW_TAG_union_type)
6412 guess_structure_name (part_die, cu);
6413}
6414
639d11d3
DC
6415/* Read the die from the .debug_info section buffer. Set DIEP to
6416 point to a newly allocated die with its information, except for its
6417 child, sibling, and parent fields. Set HAS_CHILDREN to tell
6418 whether the die has children or not. */
c906108c 6419
fe1b8b76
JB
6420static gdb_byte *
6421read_full_die (struct die_info **diep, bfd *abfd, gdb_byte *info_ptr,
e7c27a73 6422 struct dwarf2_cu *cu, int *has_children)
c906108c
SS
6423{
6424 unsigned int abbrev_number, bytes_read, i, offset;
6425 struct abbrev_info *abbrev;
6426 struct die_info *die;
6427
dce234bc 6428 offset = info_ptr - dwarf2_per_objfile->info.buffer;
c906108c
SS
6429 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6430 info_ptr += bytes_read;
6431 if (!abbrev_number)
6432 {
1d325ec1 6433 *diep = NULL;
639d11d3 6434 *has_children = 0;
c906108c
SS
6435 return info_ptr;
6436 }
6437
e7c27a73 6438 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
c906108c
SS
6439 if (!abbrev)
6440 {
8a3fe4f8 6441 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
72bf9492 6442 abbrev_number,
639d11d3 6443 bfd_get_filename (abfd));
c906108c 6444 }
b60c80d6 6445 die = dwarf_alloc_die (cu, abbrev->num_attrs);
c906108c
SS
6446 die->offset = offset;
6447 die->tag = abbrev->tag;
c906108c 6448 die->abbrev = abbrev_number;
c906108c
SS
6449
6450 die->num_attrs = abbrev->num_attrs;
c906108c
SS
6451
6452 for (i = 0; i < abbrev->num_attrs; ++i)
03dd20cc
DJ
6453 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
6454 abfd, info_ptr, cu);
c906108c
SS
6455
6456 *diep = die;
639d11d3 6457 *has_children = abbrev->has_children;
c906108c
SS
6458 return info_ptr;
6459}
6460
a8329558 6461/* Read an attribute value described by an attribute form. */
c906108c 6462
fe1b8b76 6463static gdb_byte *
a8329558 6464read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 6465 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 6466 struct dwarf2_cu *cu)
c906108c 6467{
e7c27a73 6468 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
6469 unsigned int bytes_read;
6470 struct dwarf_block *blk;
6471
a8329558
KW
6472 attr->form = form;
6473 switch (form)
c906108c
SS
6474 {
6475 case DW_FORM_addr:
6476 case DW_FORM_ref_addr:
e7c27a73 6477 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 6478 info_ptr += bytes_read;
c906108c
SS
6479 break;
6480 case DW_FORM_block2:
7b5a2f43 6481 blk = dwarf_alloc_block (cu);
c906108c
SS
6482 blk->size = read_2_bytes (abfd, info_ptr);
6483 info_ptr += 2;
6484 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6485 info_ptr += blk->size;
6486 DW_BLOCK (attr) = blk;
6487 break;
6488 case DW_FORM_block4:
7b5a2f43 6489 blk = dwarf_alloc_block (cu);
c906108c
SS
6490 blk->size = read_4_bytes (abfd, info_ptr);
6491 info_ptr += 4;
6492 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6493 info_ptr += blk->size;
6494 DW_BLOCK (attr) = blk;
6495 break;
6496 case DW_FORM_data2:
6497 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
6498 info_ptr += 2;
6499 break;
6500 case DW_FORM_data4:
6501 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
6502 info_ptr += 4;
6503 break;
6504 case DW_FORM_data8:
6505 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
6506 info_ptr += 8;
6507 break;
6508 case DW_FORM_string:
6509 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
6510 info_ptr += bytes_read;
6511 break;
4bdf3d34
JJ
6512 case DW_FORM_strp:
6513 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
6514 &bytes_read);
6515 info_ptr += bytes_read;
6516 break;
c906108c 6517 case DW_FORM_block:
7b5a2f43 6518 blk = dwarf_alloc_block (cu);
c906108c
SS
6519 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6520 info_ptr += bytes_read;
6521 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6522 info_ptr += blk->size;
6523 DW_BLOCK (attr) = blk;
6524 break;
6525 case DW_FORM_block1:
7b5a2f43 6526 blk = dwarf_alloc_block (cu);
c906108c
SS
6527 blk->size = read_1_byte (abfd, info_ptr);
6528 info_ptr += 1;
6529 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6530 info_ptr += blk->size;
6531 DW_BLOCK (attr) = blk;
6532 break;
6533 case DW_FORM_data1:
6534 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
6535 info_ptr += 1;
6536 break;
6537 case DW_FORM_flag:
6538 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
6539 info_ptr += 1;
6540 break;
6541 case DW_FORM_sdata:
6542 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
6543 info_ptr += bytes_read;
6544 break;
6545 case DW_FORM_udata:
6546 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6547 info_ptr += bytes_read;
6548 break;
6549 case DW_FORM_ref1:
10b3939b 6550 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
6551 info_ptr += 1;
6552 break;
6553 case DW_FORM_ref2:
10b3939b 6554 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
6555 info_ptr += 2;
6556 break;
6557 case DW_FORM_ref4:
10b3939b 6558 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
6559 info_ptr += 4;
6560 break;
613e1657 6561 case DW_FORM_ref8:
10b3939b 6562 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
6563 info_ptr += 8;
6564 break;
c906108c 6565 case DW_FORM_ref_udata:
10b3939b
DJ
6566 DW_ADDR (attr) = (cu->header.offset
6567 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
6568 info_ptr += bytes_read;
6569 break;
c906108c 6570 case DW_FORM_indirect:
a8329558
KW
6571 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6572 info_ptr += bytes_read;
e7c27a73 6573 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 6574 break;
c906108c 6575 default:
8a3fe4f8 6576 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
6577 dwarf_form_name (form),
6578 bfd_get_filename (abfd));
c906108c 6579 }
28e94949
JB
6580
6581 /* We have seen instances where the compiler tried to emit a byte
6582 size attribute of -1 which ended up being encoded as an unsigned
6583 0xffffffff. Although 0xffffffff is technically a valid size value,
6584 an object of this size seems pretty unlikely so we can relatively
6585 safely treat these cases as if the size attribute was invalid and
6586 treat them as zero by default. */
6587 if (attr->name == DW_AT_byte_size
6588 && form == DW_FORM_data4
6589 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
6590 {
6591 complaint
6592 (&symfile_complaints,
6593 _("Suspicious DW_AT_byte_size value treated as zero instead of 0x%lx"),
6594 DW_UNSND (attr));
6595 DW_UNSND (attr) = 0;
6596 }
28e94949 6597
c906108c
SS
6598 return info_ptr;
6599}
6600
a8329558
KW
6601/* Read an attribute described by an abbreviated attribute. */
6602
fe1b8b76 6603static gdb_byte *
a8329558 6604read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 6605 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
6606{
6607 attr->name = abbrev->name;
e7c27a73 6608 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
6609}
6610
c906108c
SS
6611/* read dwarf information from a buffer */
6612
6613static unsigned int
fe1b8b76 6614read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 6615{
fe1b8b76 6616 return bfd_get_8 (abfd, buf);
c906108c
SS
6617}
6618
6619static int
fe1b8b76 6620read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 6621{
fe1b8b76 6622 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
6623}
6624
6625static unsigned int
fe1b8b76 6626read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6627{
fe1b8b76 6628 return bfd_get_16 (abfd, buf);
c906108c
SS
6629}
6630
6631static int
fe1b8b76 6632read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6633{
fe1b8b76 6634 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
6635}
6636
6637static unsigned int
fe1b8b76 6638read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6639{
fe1b8b76 6640 return bfd_get_32 (abfd, buf);
c906108c
SS
6641}
6642
6643static int
fe1b8b76 6644read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6645{
fe1b8b76 6646 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
6647}
6648
ce5d95e1 6649static unsigned long
fe1b8b76 6650read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6651{
fe1b8b76 6652 return bfd_get_64 (abfd, buf);
c906108c
SS
6653}
6654
6655static CORE_ADDR
fe1b8b76 6656read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 6657 unsigned int *bytes_read)
c906108c 6658{
e7c27a73 6659 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
6660 CORE_ADDR retval = 0;
6661
107d2387 6662 if (cu_header->signed_addr_p)
c906108c 6663 {
107d2387
AC
6664 switch (cu_header->addr_size)
6665 {
6666 case 2:
fe1b8b76 6667 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
6668 break;
6669 case 4:
fe1b8b76 6670 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
6671 break;
6672 case 8:
fe1b8b76 6673 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
6674 break;
6675 default:
8e65ff28 6676 internal_error (__FILE__, __LINE__,
e2e0b3e5 6677 _("read_address: bad switch, signed [in module %s]"),
659b0389 6678 bfd_get_filename (abfd));
107d2387
AC
6679 }
6680 }
6681 else
6682 {
6683 switch (cu_header->addr_size)
6684 {
6685 case 2:
fe1b8b76 6686 retval = bfd_get_16 (abfd, buf);
107d2387
AC
6687 break;
6688 case 4:
fe1b8b76 6689 retval = bfd_get_32 (abfd, buf);
107d2387
AC
6690 break;
6691 case 8:
fe1b8b76 6692 retval = bfd_get_64 (abfd, buf);
107d2387
AC
6693 break;
6694 default:
8e65ff28 6695 internal_error (__FILE__, __LINE__,
e2e0b3e5 6696 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 6697 bfd_get_filename (abfd));
107d2387 6698 }
c906108c 6699 }
64367e0a 6700
107d2387
AC
6701 *bytes_read = cu_header->addr_size;
6702 return retval;
c906108c
SS
6703}
6704
f7ef9339 6705/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
6706 specification allows the initial length to take up either 4 bytes
6707 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
6708 bytes describe the length and all offsets will be 8 bytes in length
6709 instead of 4.
6710
f7ef9339
KB
6711 An older, non-standard 64-bit format is also handled by this
6712 function. The older format in question stores the initial length
6713 as an 8-byte quantity without an escape value. Lengths greater
6714 than 2^32 aren't very common which means that the initial 4 bytes
6715 is almost always zero. Since a length value of zero doesn't make
6716 sense for the 32-bit format, this initial zero can be considered to
6717 be an escape value which indicates the presence of the older 64-bit
6718 format. As written, the code can't detect (old format) lengths
917c78fc
MK
6719 greater than 4GB. If it becomes necessary to handle lengths
6720 somewhat larger than 4GB, we could allow other small values (such
6721 as the non-sensical values of 1, 2, and 3) to also be used as
6722 escape values indicating the presence of the old format.
f7ef9339 6723
917c78fc
MK
6724 The value returned via bytes_read should be used to increment the
6725 relevant pointer after calling read_initial_length().
c764a876 6726
613e1657
KB
6727 [ Note: read_initial_length() and read_offset() are based on the
6728 document entitled "DWARF Debugging Information Format", revision
f7ef9339 6729 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
6730 from:
6731
f7ef9339 6732 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
613e1657
KB
6733
6734 This document is only a draft and is subject to change. (So beware.)
6735
f7ef9339 6736 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
6737 determined empirically by examining 64-bit ELF files produced by
6738 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
6739
6740 - Kevin, July 16, 2002
613e1657
KB
6741 ] */
6742
6743static LONGEST
c764a876 6744read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 6745{
fe1b8b76 6746 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 6747
dd373385 6748 if (length == 0xffffffff)
613e1657 6749 {
fe1b8b76 6750 length = bfd_get_64 (abfd, buf + 4);
613e1657 6751 *bytes_read = 12;
613e1657 6752 }
dd373385 6753 else if (length == 0)
f7ef9339 6754 {
dd373385 6755 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 6756 length = bfd_get_64 (abfd, buf);
f7ef9339 6757 *bytes_read = 8;
f7ef9339 6758 }
613e1657
KB
6759 else
6760 {
6761 *bytes_read = 4;
613e1657
KB
6762 }
6763
c764a876
DE
6764 return length;
6765}
dd373385 6766
c764a876
DE
6767/* Cover function for read_initial_length.
6768 Returns the length of the object at BUF, and stores the size of the
6769 initial length in *BYTES_READ and stores the size that offsets will be in
6770 *OFFSET_SIZE.
6771 If the initial length size is not equivalent to that specified in
6772 CU_HEADER then issue a complaint.
6773 This is useful when reading non-comp-unit headers. */
dd373385 6774
c764a876
DE
6775static LONGEST
6776read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
6777 const struct comp_unit_head *cu_header,
6778 unsigned int *bytes_read,
6779 unsigned int *offset_size)
6780{
6781 LONGEST length = read_initial_length (abfd, buf, bytes_read);
6782
6783 gdb_assert (cu_header->initial_length_size == 4
6784 || cu_header->initial_length_size == 8
6785 || cu_header->initial_length_size == 12);
6786
6787 if (cu_header->initial_length_size != *bytes_read)
6788 complaint (&symfile_complaints,
6789 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 6790
c764a876 6791 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 6792 return length;
613e1657
KB
6793}
6794
6795/* Read an offset from the data stream. The size of the offset is
917c78fc 6796 given by cu_header->offset_size. */
613e1657
KB
6797
6798static LONGEST
fe1b8b76 6799read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 6800 unsigned int *bytes_read)
c764a876
DE
6801{
6802 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
6803 *bytes_read = cu_header->offset_size;
6804 return offset;
6805}
6806
6807/* Read an offset from the data stream. */
6808
6809static LONGEST
6810read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
6811{
6812 LONGEST retval = 0;
6813
c764a876 6814 switch (offset_size)
613e1657
KB
6815 {
6816 case 4:
fe1b8b76 6817 retval = bfd_get_32 (abfd, buf);
613e1657
KB
6818 break;
6819 case 8:
fe1b8b76 6820 retval = bfd_get_64 (abfd, buf);
613e1657
KB
6821 break;
6822 default:
8e65ff28 6823 internal_error (__FILE__, __LINE__,
c764a876 6824 _("read_offset_1: bad switch [in module %s]"),
659b0389 6825 bfd_get_filename (abfd));
613e1657
KB
6826 }
6827
917c78fc 6828 return retval;
613e1657
KB
6829}
6830
fe1b8b76
JB
6831static gdb_byte *
6832read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
6833{
6834 /* If the size of a host char is 8 bits, we can return a pointer
6835 to the buffer, otherwise we have to copy the data to a buffer
6836 allocated on the temporary obstack. */
4bdf3d34 6837 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 6838 return buf;
c906108c
SS
6839}
6840
6841static char *
fe1b8b76 6842read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
6843{
6844 /* If the size of a host char is 8 bits, we can return a pointer
6845 to the string, otherwise we have to copy the string to a buffer
6846 allocated on the temporary obstack. */
4bdf3d34 6847 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
6848 if (*buf == '\0')
6849 {
6850 *bytes_read_ptr = 1;
6851 return NULL;
6852 }
fe1b8b76
JB
6853 *bytes_read_ptr = strlen ((char *) buf) + 1;
6854 return (char *) buf;
4bdf3d34
JJ
6855}
6856
6857static char *
fe1b8b76 6858read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
6859 const struct comp_unit_head *cu_header,
6860 unsigned int *bytes_read_ptr)
6861{
c764a876 6862 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 6863
dce234bc 6864 if (dwarf2_per_objfile->str.buffer == NULL)
c906108c 6865 {
8a3fe4f8 6866 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 6867 bfd_get_filename (abfd));
4bdf3d34 6868 return NULL;
c906108c 6869 }
dce234bc 6870 if (str_offset >= dwarf2_per_objfile->str.size)
c906108c 6871 {
8a3fe4f8 6872 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 6873 bfd_get_filename (abfd));
c906108c
SS
6874 return NULL;
6875 }
4bdf3d34 6876 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 6877 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 6878 return NULL;
dce234bc 6879 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
6880}
6881
ce5d95e1 6882static unsigned long
fe1b8b76 6883read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 6884{
ce5d95e1
JB
6885 unsigned long result;
6886 unsigned int num_read;
c906108c
SS
6887 int i, shift;
6888 unsigned char byte;
6889
6890 result = 0;
6891 shift = 0;
6892 num_read = 0;
6893 i = 0;
6894 while (1)
6895 {
fe1b8b76 6896 byte = bfd_get_8 (abfd, buf);
c906108c
SS
6897 buf++;
6898 num_read++;
ce5d95e1 6899 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
6900 if ((byte & 128) == 0)
6901 {
6902 break;
6903 }
6904 shift += 7;
6905 }
6906 *bytes_read_ptr = num_read;
6907 return result;
6908}
6909
ce5d95e1 6910static long
fe1b8b76 6911read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 6912{
ce5d95e1 6913 long result;
77e0b926 6914 int i, shift, num_read;
c906108c
SS
6915 unsigned char byte;
6916
6917 result = 0;
6918 shift = 0;
c906108c
SS
6919 num_read = 0;
6920 i = 0;
6921 while (1)
6922 {
fe1b8b76 6923 byte = bfd_get_8 (abfd, buf);
c906108c
SS
6924 buf++;
6925 num_read++;
ce5d95e1 6926 result |= ((long)(byte & 127) << shift);
c906108c
SS
6927 shift += 7;
6928 if ((byte & 128) == 0)
6929 {
6930 break;
6931 }
6932 }
77e0b926
DJ
6933 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
6934 result |= -(((long)1) << shift);
c906108c
SS
6935 *bytes_read_ptr = num_read;
6936 return result;
6937}
6938
4bb7a0a7
DJ
6939/* Return a pointer to just past the end of an LEB128 number in BUF. */
6940
fe1b8b76
JB
6941static gdb_byte *
6942skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
6943{
6944 int byte;
6945
6946 while (1)
6947 {
fe1b8b76 6948 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
6949 buf++;
6950 if ((byte & 128) == 0)
6951 return buf;
6952 }
6953}
6954
c906108c 6955static void
e142c38c 6956set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
6957{
6958 switch (lang)
6959 {
6960 case DW_LANG_C89:
76bee0cc 6961 case DW_LANG_C99:
c906108c 6962 case DW_LANG_C:
e142c38c 6963 cu->language = language_c;
c906108c
SS
6964 break;
6965 case DW_LANG_C_plus_plus:
e142c38c 6966 cu->language = language_cplus;
c906108c
SS
6967 break;
6968 case DW_LANG_Fortran77:
6969 case DW_LANG_Fortran90:
b21b22e0 6970 case DW_LANG_Fortran95:
e142c38c 6971 cu->language = language_fortran;
c906108c
SS
6972 break;
6973 case DW_LANG_Mips_Assembler:
e142c38c 6974 cu->language = language_asm;
c906108c 6975 break;
bebd888e 6976 case DW_LANG_Java:
e142c38c 6977 cu->language = language_java;
bebd888e 6978 break;
c906108c 6979 case DW_LANG_Ada83:
8aaf0b47 6980 case DW_LANG_Ada95:
bc5f45f8
JB
6981 cu->language = language_ada;
6982 break;
72019c9c
GM
6983 case DW_LANG_Modula2:
6984 cu->language = language_m2;
6985 break;
fe8e67fd
PM
6986 case DW_LANG_Pascal83:
6987 cu->language = language_pascal;
6988 break;
22566fbd
DJ
6989 case DW_LANG_ObjC:
6990 cu->language = language_objc;
6991 break;
c906108c
SS
6992 case DW_LANG_Cobol74:
6993 case DW_LANG_Cobol85:
c906108c 6994 default:
e142c38c 6995 cu->language = language_minimal;
c906108c
SS
6996 break;
6997 }
e142c38c 6998 cu->language_defn = language_def (cu->language);
c906108c
SS
6999}
7000
7001/* Return the named attribute or NULL if not there. */
7002
7003static struct attribute *
e142c38c 7004dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
7005{
7006 unsigned int i;
7007 struct attribute *spec = NULL;
7008
7009 for (i = 0; i < die->num_attrs; ++i)
7010 {
7011 if (die->attrs[i].name == name)
10b3939b 7012 return &die->attrs[i];
c906108c
SS
7013 if (die->attrs[i].name == DW_AT_specification
7014 || die->attrs[i].name == DW_AT_abstract_origin)
7015 spec = &die->attrs[i];
7016 }
c906108c 7017
10b3939b 7018 if (spec)
f2f0e013
DJ
7019 {
7020 die = follow_die_ref (die, spec, &cu);
7021 return dwarf2_attr (die, name, cu);
7022 }
c5aa993b 7023
c906108c
SS
7024 return NULL;
7025}
7026
05cf31d1
JB
7027/* Return non-zero iff the attribute NAME is defined for the given DIE,
7028 and holds a non-zero value. This function should only be used for
7029 DW_FORM_flag attributes. */
7030
7031static int
7032dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
7033{
7034 struct attribute *attr = dwarf2_attr (die, name, cu);
7035
7036 return (attr && DW_UNSND (attr));
7037}
7038
3ca72b44 7039static int
e142c38c 7040die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 7041{
05cf31d1
JB
7042 /* A DIE is a declaration if it has a DW_AT_declaration attribute
7043 which value is non-zero. However, we have to be careful with
7044 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
7045 (via dwarf2_flag_true_p) follows this attribute. So we may
7046 end up accidently finding a declaration attribute that belongs
7047 to a different DIE referenced by the specification attribute,
7048 even though the given DIE does not have a declaration attribute. */
7049 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
7050 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
7051}
7052
63d06c5c 7053/* Return the die giving the specification for DIE, if there is
f2f0e013 7054 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
7055 containing the return value on output. If there is no
7056 specification, but there is an abstract origin, that is
7057 returned. */
63d06c5c
DC
7058
7059static struct die_info *
f2f0e013 7060die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 7061{
f2f0e013
DJ
7062 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
7063 *spec_cu);
63d06c5c 7064
edb3359d
DJ
7065 if (spec_attr == NULL)
7066 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
7067
63d06c5c
DC
7068 if (spec_attr == NULL)
7069 return NULL;
7070 else
f2f0e013 7071 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 7072}
c906108c 7073
debd256d
JB
7074/* Free the line_header structure *LH, and any arrays and strings it
7075 refers to. */
7076static void
7077free_line_header (struct line_header *lh)
7078{
7079 if (lh->standard_opcode_lengths)
a8bc7b56 7080 xfree (lh->standard_opcode_lengths);
debd256d
JB
7081
7082 /* Remember that all the lh->file_names[i].name pointers are
7083 pointers into debug_line_buffer, and don't need to be freed. */
7084 if (lh->file_names)
a8bc7b56 7085 xfree (lh->file_names);
debd256d
JB
7086
7087 /* Similarly for the include directory names. */
7088 if (lh->include_dirs)
a8bc7b56 7089 xfree (lh->include_dirs);
debd256d 7090
a8bc7b56 7091 xfree (lh);
debd256d
JB
7092}
7093
7094
7095/* Add an entry to LH's include directory table. */
7096static void
7097add_include_dir (struct line_header *lh, char *include_dir)
c906108c 7098{
debd256d
JB
7099 /* Grow the array if necessary. */
7100 if (lh->include_dirs_size == 0)
c5aa993b 7101 {
debd256d
JB
7102 lh->include_dirs_size = 1; /* for testing */
7103 lh->include_dirs = xmalloc (lh->include_dirs_size
7104 * sizeof (*lh->include_dirs));
7105 }
7106 else if (lh->num_include_dirs >= lh->include_dirs_size)
7107 {
7108 lh->include_dirs_size *= 2;
7109 lh->include_dirs = xrealloc (lh->include_dirs,
7110 (lh->include_dirs_size
7111 * sizeof (*lh->include_dirs)));
c5aa993b 7112 }
c906108c 7113
debd256d
JB
7114 lh->include_dirs[lh->num_include_dirs++] = include_dir;
7115}
7116
7117
7118/* Add an entry to LH's file name table. */
7119static void
7120add_file_name (struct line_header *lh,
7121 char *name,
7122 unsigned int dir_index,
7123 unsigned int mod_time,
7124 unsigned int length)
7125{
7126 struct file_entry *fe;
7127
7128 /* Grow the array if necessary. */
7129 if (lh->file_names_size == 0)
7130 {
7131 lh->file_names_size = 1; /* for testing */
7132 lh->file_names = xmalloc (lh->file_names_size
7133 * sizeof (*lh->file_names));
7134 }
7135 else if (lh->num_file_names >= lh->file_names_size)
7136 {
7137 lh->file_names_size *= 2;
7138 lh->file_names = xrealloc (lh->file_names,
7139 (lh->file_names_size
7140 * sizeof (*lh->file_names)));
7141 }
7142
7143 fe = &lh->file_names[lh->num_file_names++];
7144 fe->name = name;
7145 fe->dir_index = dir_index;
7146 fe->mod_time = mod_time;
7147 fe->length = length;
aaa75496 7148 fe->included_p = 0;
cb1df416 7149 fe->symtab = NULL;
debd256d
JB
7150}
7151
7152
7153/* Read the statement program header starting at OFFSET in
6502dd73
DJ
7154 .debug_line, according to the endianness of ABFD. Return a pointer
7155 to a struct line_header, allocated using xmalloc.
debd256d
JB
7156
7157 NOTE: the strings in the include directory and file name tables of
7158 the returned object point into debug_line_buffer, and must not be
7159 freed. */
7160static struct line_header *
7161dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 7162 struct dwarf2_cu *cu)
debd256d
JB
7163{
7164 struct cleanup *back_to;
7165 struct line_header *lh;
fe1b8b76 7166 gdb_byte *line_ptr;
c764a876 7167 unsigned int bytes_read, offset_size;
debd256d
JB
7168 int i;
7169 char *cur_dir, *cur_file;
7170
dce234bc 7171 if (dwarf2_per_objfile->line.buffer == NULL)
debd256d 7172 {
e2e0b3e5 7173 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
7174 return 0;
7175 }
7176
a738430d
MK
7177 /* Make sure that at least there's room for the total_length field.
7178 That could be 12 bytes long, but we're just going to fudge that. */
dce234bc 7179 if (offset + 4 >= dwarf2_per_objfile->line.size)
debd256d 7180 {
4d3c2250 7181 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
7182 return 0;
7183 }
7184
7185 lh = xmalloc (sizeof (*lh));
7186 memset (lh, 0, sizeof (*lh));
7187 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
7188 (void *) lh);
7189
dce234bc 7190 line_ptr = dwarf2_per_objfile->line.buffer + offset;
debd256d 7191
a738430d 7192 /* Read in the header. */
dd373385 7193 lh->total_length =
c764a876
DE
7194 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
7195 &bytes_read, &offset_size);
debd256d 7196 line_ptr += bytes_read;
dce234bc
PP
7197 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
7198 + dwarf2_per_objfile->line.size))
debd256d 7199 {
4d3c2250 7200 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
7201 return 0;
7202 }
7203 lh->statement_program_end = line_ptr + lh->total_length;
7204 lh->version = read_2_bytes (abfd, line_ptr);
7205 line_ptr += 2;
c764a876
DE
7206 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
7207 line_ptr += offset_size;
debd256d
JB
7208 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
7209 line_ptr += 1;
7210 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
7211 line_ptr += 1;
7212 lh->line_base = read_1_signed_byte (abfd, line_ptr);
7213 line_ptr += 1;
7214 lh->line_range = read_1_byte (abfd, line_ptr);
7215 line_ptr += 1;
7216 lh->opcode_base = read_1_byte (abfd, line_ptr);
7217 line_ptr += 1;
7218 lh->standard_opcode_lengths
fe1b8b76 7219 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
7220
7221 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
7222 for (i = 1; i < lh->opcode_base; ++i)
7223 {
7224 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
7225 line_ptr += 1;
7226 }
7227
a738430d 7228 /* Read directory table. */
debd256d
JB
7229 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7230 {
7231 line_ptr += bytes_read;
7232 add_include_dir (lh, cur_dir);
7233 }
7234 line_ptr += bytes_read;
7235
a738430d 7236 /* Read file name table. */
debd256d
JB
7237 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7238 {
7239 unsigned int dir_index, mod_time, length;
7240
7241 line_ptr += bytes_read;
7242 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7243 line_ptr += bytes_read;
7244 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7245 line_ptr += bytes_read;
7246 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7247 line_ptr += bytes_read;
7248
7249 add_file_name (lh, cur_file, dir_index, mod_time, length);
7250 }
7251 line_ptr += bytes_read;
7252 lh->statement_program_start = line_ptr;
7253
dce234bc
PP
7254 if (line_ptr > (dwarf2_per_objfile->line.buffer
7255 + dwarf2_per_objfile->line.size))
4d3c2250 7256 complaint (&symfile_complaints,
e2e0b3e5 7257 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
7258
7259 discard_cleanups (back_to);
7260 return lh;
7261}
c906108c 7262
5fb290d7
DJ
7263/* This function exists to work around a bug in certain compilers
7264 (particularly GCC 2.95), in which the first line number marker of a
7265 function does not show up until after the prologue, right before
7266 the second line number marker. This function shifts ADDRESS down
7267 to the beginning of the function if necessary, and is called on
7268 addresses passed to record_line. */
7269
7270static CORE_ADDR
e142c38c 7271check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
7272{
7273 struct function_range *fn;
7274
7275 /* Find the function_range containing address. */
e142c38c 7276 if (!cu->first_fn)
5fb290d7
DJ
7277 return address;
7278
e142c38c
DJ
7279 if (!cu->cached_fn)
7280 cu->cached_fn = cu->first_fn;
5fb290d7 7281
e142c38c 7282 fn = cu->cached_fn;
5fb290d7
DJ
7283 while (fn)
7284 if (fn->lowpc <= address && fn->highpc > address)
7285 goto found;
7286 else
7287 fn = fn->next;
7288
e142c38c
DJ
7289 fn = cu->first_fn;
7290 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
7291 if (fn->lowpc <= address && fn->highpc > address)
7292 goto found;
7293 else
7294 fn = fn->next;
7295
7296 return address;
7297
7298 found:
7299 if (fn->seen_line)
7300 return address;
7301 if (address != fn->lowpc)
4d3c2250 7302 complaint (&symfile_complaints,
e2e0b3e5 7303 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 7304 (unsigned long) address, fn->name);
5fb290d7
DJ
7305 fn->seen_line = 1;
7306 return fn->lowpc;
7307}
7308
aaa75496
JB
7309/* Decode the Line Number Program (LNP) for the given line_header
7310 structure and CU. The actual information extracted and the type
7311 of structures created from the LNP depends on the value of PST.
7312
7313 1. If PST is NULL, then this procedure uses the data from the program
7314 to create all necessary symbol tables, and their linetables.
7315 The compilation directory of the file is passed in COMP_DIR,
7316 and must not be NULL.
7317
7318 2. If PST is not NULL, this procedure reads the program to determine
7319 the list of files included by the unit represented by PST, and
7320 builds all the associated partial symbol tables. In this case,
7321 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
7322 is not used to compute the full name of the symtab, and therefore
7323 omitting it when building the partial symtab does not introduce
7324 the potential for inconsistency - a partial symtab and its associated
7325 symbtab having a different fullname -). */
debd256d 7326
c906108c 7327static void
debd256d 7328dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 7329 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 7330{
a8c50c1f 7331 gdb_byte *line_ptr, *extended_end;
fe1b8b76 7332 gdb_byte *line_end;
a8c50c1f 7333 unsigned int bytes_read, extended_len;
c906108c 7334 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
7335 CORE_ADDR baseaddr;
7336 struct objfile *objfile = cu->objfile;
fbf65064 7337 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 7338 const int decode_for_pst_p = (pst != NULL);
cb1df416 7339 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
7340
7341 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7342
debd256d
JB
7343 line_ptr = lh->statement_program_start;
7344 line_end = lh->statement_program_end;
c906108c
SS
7345
7346 /* Read the statement sequences until there's nothing left. */
7347 while (line_ptr < line_end)
7348 {
7349 /* state machine registers */
7350 CORE_ADDR address = 0;
7351 unsigned int file = 1;
7352 unsigned int line = 1;
7353 unsigned int column = 0;
debd256d 7354 int is_stmt = lh->default_is_stmt;
c906108c
SS
7355 int basic_block = 0;
7356 int end_sequence = 0;
fbf65064 7357 CORE_ADDR addr;
c906108c 7358
aaa75496 7359 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 7360 {
aaa75496 7361 /* Start a subfile for the current file of the state machine. */
debd256d
JB
7362 /* lh->include_dirs and lh->file_names are 0-based, but the
7363 directory and file name numbers in the statement program
7364 are 1-based. */
7365 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 7366 char *dir = NULL;
a738430d 7367
debd256d
JB
7368 if (fe->dir_index)
7369 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
7370
7371 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
7372 }
7373
a738430d 7374 /* Decode the table. */
c5aa993b 7375 while (!end_sequence)
c906108c
SS
7376 {
7377 op_code = read_1_byte (abfd, line_ptr);
7378 line_ptr += 1;
59205f5a
JB
7379 if (line_ptr > line_end)
7380 {
7381 dwarf2_debug_line_missing_end_sequence_complaint ();
7382 break;
7383 }
9aa1fe7e 7384
debd256d 7385 if (op_code >= lh->opcode_base)
a738430d
MK
7386 {
7387 /* Special operand. */
debd256d
JB
7388 adj_opcode = op_code - lh->opcode_base;
7389 address += (adj_opcode / lh->line_range)
7390 * lh->minimum_instruction_length;
7391 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 7392 if (lh->num_file_names < file || file == 0)
25e43795
DJ
7393 dwarf2_debug_line_missing_file_complaint ();
7394 else
7395 {
7396 lh->file_names[file - 1].included_p = 1;
ca5f395d 7397 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
7398 {
7399 if (last_subfile != current_subfile)
7400 {
7401 addr = gdbarch_addr_bits_remove (gdbarch, address);
7402 if (last_subfile)
7403 record_line (last_subfile, 0, addr);
7404 last_subfile = current_subfile;
7405 }
25e43795 7406 /* Append row to matrix using current values. */
fbf65064
UW
7407 addr = check_cu_functions (address, cu);
7408 addr = gdbarch_addr_bits_remove (gdbarch, addr);
7409 record_line (current_subfile, line, addr);
366da635 7410 }
25e43795 7411 }
ca5f395d 7412 basic_block = 0;
9aa1fe7e
GK
7413 }
7414 else switch (op_code)
c906108c
SS
7415 {
7416 case DW_LNS_extended_op:
a8c50c1f 7417 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 7418 line_ptr += bytes_read;
a8c50c1f 7419 extended_end = line_ptr + extended_len;
c906108c
SS
7420 extended_op = read_1_byte (abfd, line_ptr);
7421 line_ptr += 1;
7422 switch (extended_op)
7423 {
7424 case DW_LNE_end_sequence:
7425 end_sequence = 1;
c906108c
SS
7426 break;
7427 case DW_LNE_set_address:
e7c27a73 7428 address = read_address (abfd, line_ptr, cu, &bytes_read);
107d2387
AC
7429 line_ptr += bytes_read;
7430 address += baseaddr;
c906108c
SS
7431 break;
7432 case DW_LNE_define_file:
debd256d
JB
7433 {
7434 char *cur_file;
7435 unsigned int dir_index, mod_time, length;
7436
7437 cur_file = read_string (abfd, line_ptr, &bytes_read);
7438 line_ptr += bytes_read;
7439 dir_index =
7440 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7441 line_ptr += bytes_read;
7442 mod_time =
7443 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7444 line_ptr += bytes_read;
7445 length =
7446 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7447 line_ptr += bytes_read;
7448 add_file_name (lh, cur_file, dir_index, mod_time, length);
7449 }
c906108c 7450 break;
d0c6ba3d
CC
7451 case DW_LNE_set_discriminator:
7452 /* The discriminator is not interesting to the debugger;
7453 just ignore it. */
7454 line_ptr = extended_end;
7455 break;
c906108c 7456 default:
4d3c2250 7457 complaint (&symfile_complaints,
e2e0b3e5 7458 _("mangled .debug_line section"));
debd256d 7459 return;
c906108c 7460 }
a8c50c1f
DJ
7461 /* Make sure that we parsed the extended op correctly. If e.g.
7462 we expected a different address size than the producer used,
7463 we may have read the wrong number of bytes. */
7464 if (line_ptr != extended_end)
7465 {
7466 complaint (&symfile_complaints,
7467 _("mangled .debug_line section"));
7468 return;
7469 }
c906108c
SS
7470 break;
7471 case DW_LNS_copy:
59205f5a 7472 if (lh->num_file_names < file || file == 0)
25e43795
DJ
7473 dwarf2_debug_line_missing_file_complaint ();
7474 else
366da635 7475 {
25e43795 7476 lh->file_names[file - 1].included_p = 1;
ca5f395d 7477 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
7478 {
7479 if (last_subfile != current_subfile)
7480 {
7481 addr = gdbarch_addr_bits_remove (gdbarch, address);
7482 if (last_subfile)
7483 record_line (last_subfile, 0, addr);
7484 last_subfile = current_subfile;
7485 }
7486 addr = check_cu_functions (address, cu);
7487 addr = gdbarch_addr_bits_remove (gdbarch, addr);
7488 record_line (current_subfile, line, addr);
7489 }
366da635 7490 }
c906108c
SS
7491 basic_block = 0;
7492 break;
7493 case DW_LNS_advance_pc:
debd256d 7494 address += lh->minimum_instruction_length
c906108c
SS
7495 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7496 line_ptr += bytes_read;
7497 break;
7498 case DW_LNS_advance_line:
7499 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
7500 line_ptr += bytes_read;
7501 break;
7502 case DW_LNS_set_file:
debd256d 7503 {
a738430d
MK
7504 /* The arrays lh->include_dirs and lh->file_names are
7505 0-based, but the directory and file name numbers in
7506 the statement program are 1-based. */
debd256d 7507 struct file_entry *fe;
4f1520fb 7508 char *dir = NULL;
a738430d 7509
debd256d
JB
7510 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7511 line_ptr += bytes_read;
59205f5a 7512 if (lh->num_file_names < file || file == 0)
25e43795
DJ
7513 dwarf2_debug_line_missing_file_complaint ();
7514 else
7515 {
7516 fe = &lh->file_names[file - 1];
7517 if (fe->dir_index)
7518 dir = lh->include_dirs[fe->dir_index - 1];
7519 if (!decode_for_pst_p)
7520 {
7521 last_subfile = current_subfile;
7522 dwarf2_start_subfile (fe->name, dir, comp_dir);
7523 }
7524 }
debd256d 7525 }
c906108c
SS
7526 break;
7527 case DW_LNS_set_column:
7528 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7529 line_ptr += bytes_read;
7530 break;
7531 case DW_LNS_negate_stmt:
7532 is_stmt = (!is_stmt);
7533 break;
7534 case DW_LNS_set_basic_block:
7535 basic_block = 1;
7536 break;
c2c6d25f
JM
7537 /* Add to the address register of the state machine the
7538 address increment value corresponding to special opcode
a738430d
MK
7539 255. I.e., this value is scaled by the minimum
7540 instruction length since special opcode 255 would have
7541 scaled the the increment. */
c906108c 7542 case DW_LNS_const_add_pc:
debd256d
JB
7543 address += (lh->minimum_instruction_length
7544 * ((255 - lh->opcode_base) / lh->line_range));
c906108c
SS
7545 break;
7546 case DW_LNS_fixed_advance_pc:
7547 address += read_2_bytes (abfd, line_ptr);
7548 line_ptr += 2;
7549 break;
9aa1fe7e 7550 default:
a738430d
MK
7551 {
7552 /* Unknown standard opcode, ignore it. */
9aa1fe7e 7553 int i;
a738430d 7554
debd256d 7555 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
7556 {
7557 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7558 line_ptr += bytes_read;
7559 }
7560 }
c906108c
SS
7561 }
7562 }
59205f5a
JB
7563 if (lh->num_file_names < file || file == 0)
7564 dwarf2_debug_line_missing_file_complaint ();
7565 else
7566 {
7567 lh->file_names[file - 1].included_p = 1;
7568 if (!decode_for_pst_p)
fbf65064
UW
7569 {
7570 addr = gdbarch_addr_bits_remove (gdbarch, address);
7571 record_line (current_subfile, 0, addr);
7572 }
59205f5a 7573 }
c906108c 7574 }
aaa75496
JB
7575
7576 if (decode_for_pst_p)
7577 {
7578 int file_index;
7579
7580 /* Now that we're done scanning the Line Header Program, we can
7581 create the psymtab of each included file. */
7582 for (file_index = 0; file_index < lh->num_file_names; file_index++)
7583 if (lh->file_names[file_index].included_p == 1)
7584 {
5b5464ad
JB
7585 const struct file_entry fe = lh->file_names [file_index];
7586 char *include_name = fe.name;
7587 char *dir_name = NULL;
7588 char *pst_filename = pst->filename;
7589
7590 if (fe.dir_index)
7591 dir_name = lh->include_dirs[fe.dir_index - 1];
7592
7593 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
7594 {
1754f103
MK
7595 include_name = concat (dir_name, SLASH_STRING,
7596 include_name, (char *)NULL);
5b5464ad
JB
7597 make_cleanup (xfree, include_name);
7598 }
7599
7600 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
7601 {
1754f103
MK
7602 pst_filename = concat (pst->dirname, SLASH_STRING,
7603 pst_filename, (char *)NULL);
5b5464ad
JB
7604 make_cleanup (xfree, pst_filename);
7605 }
7606
7607 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
7608 dwarf2_create_include_psymtab (include_name, pst, objfile);
7609 }
7610 }
cb1df416
DJ
7611 else
7612 {
7613 /* Make sure a symtab is created for every file, even files
7614 which contain only variables (i.e. no code with associated
7615 line numbers). */
7616
7617 int i;
7618 struct file_entry *fe;
7619
7620 for (i = 0; i < lh->num_file_names; i++)
7621 {
7622 char *dir = NULL;
7623 fe = &lh->file_names[i];
7624 if (fe->dir_index)
7625 dir = lh->include_dirs[fe->dir_index - 1];
7626 dwarf2_start_subfile (fe->name, dir, comp_dir);
7627
7628 /* Skip the main file; we don't need it, and it must be
7629 allocated last, so that it will show up before the
7630 non-primary symtabs in the objfile's symtab list. */
7631 if (current_subfile == first_subfile)
7632 continue;
7633
7634 if (current_subfile->symtab == NULL)
7635 current_subfile->symtab = allocate_symtab (current_subfile->name,
7636 cu->objfile);
7637 fe->symtab = current_subfile->symtab;
7638 }
7639 }
c906108c
SS
7640}
7641
7642/* Start a subfile for DWARF. FILENAME is the name of the file and
7643 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
7644 or NULL if not known. COMP_DIR is the compilation directory for the
7645 linetable's compilation unit or NULL if not known.
c906108c
SS
7646 This routine tries to keep line numbers from identical absolute and
7647 relative file names in a common subfile.
7648
7649 Using the `list' example from the GDB testsuite, which resides in
7650 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
7651 of /srcdir/list0.c yields the following debugging information for list0.c:
7652
c5aa993b
JM
7653 DW_AT_name: /srcdir/list0.c
7654 DW_AT_comp_dir: /compdir
357e46e7 7655 files.files[0].name: list0.h
c5aa993b 7656 files.files[0].dir: /srcdir
357e46e7 7657 files.files[1].name: list0.c
c5aa993b 7658 files.files[1].dir: /srcdir
c906108c
SS
7659
7660 The line number information for list0.c has to end up in a single
4f1520fb
FR
7661 subfile, so that `break /srcdir/list0.c:1' works as expected.
7662 start_subfile will ensure that this happens provided that we pass the
7663 concatenation of files.files[1].dir and files.files[1].name as the
7664 subfile's name. */
c906108c
SS
7665
7666static void
4f1520fb 7667dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 7668{
4f1520fb
FR
7669 char *fullname;
7670
7671 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
7672 `start_symtab' will always pass the contents of DW_AT_comp_dir as
7673 second argument to start_subfile. To be consistent, we do the
7674 same here. In order not to lose the line information directory,
7675 we concatenate it to the filename when it makes sense.
7676 Note that the Dwarf3 standard says (speaking of filenames in line
7677 information): ``The directory index is ignored for file names
7678 that represent full path names''. Thus ignoring dirname in the
7679 `else' branch below isn't an issue. */
c906108c 7680
d5166ae1 7681 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
7682 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
7683 else
7684 fullname = filename;
c906108c 7685
4f1520fb
FR
7686 start_subfile (fullname, comp_dir);
7687
7688 if (fullname != filename)
7689 xfree (fullname);
c906108c
SS
7690}
7691
4c2df51b
DJ
7692static void
7693var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 7694 struct dwarf2_cu *cu)
4c2df51b 7695{
e7c27a73
DJ
7696 struct objfile *objfile = cu->objfile;
7697 struct comp_unit_head *cu_header = &cu->header;
7698
4c2df51b
DJ
7699 /* NOTE drow/2003-01-30: There used to be a comment and some special
7700 code here to turn a symbol with DW_AT_external and a
7701 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
7702 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
7703 with some versions of binutils) where shared libraries could have
7704 relocations against symbols in their debug information - the
7705 minimal symbol would have the right address, but the debug info
7706 would not. It's no longer necessary, because we will explicitly
7707 apply relocations when we read in the debug information now. */
7708
7709 /* A DW_AT_location attribute with no contents indicates that a
7710 variable has been optimized away. */
7711 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
7712 {
7713 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
7714 return;
7715 }
7716
7717 /* Handle one degenerate form of location expression specially, to
7718 preserve GDB's previous behavior when section offsets are
7719 specified. If this is just a DW_OP_addr then mark this symbol
7720 as LOC_STATIC. */
7721
7722 if (attr_form_is_block (attr)
7723 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
7724 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
7725 {
891d2f0b 7726 unsigned int dummy;
4c2df51b
DJ
7727
7728 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 7729 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 7730 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
7731 fixup_symbol_section (sym, objfile);
7732 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
7733 SYMBOL_SECTION (sym));
4c2df51b
DJ
7734 return;
7735 }
7736
7737 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
7738 expression evaluator, and use LOC_COMPUTED only when necessary
7739 (i.e. when the value of a register or memory location is
7740 referenced, or a thread-local block, etc.). Then again, it might
7741 not be worthwhile. I'm assuming that it isn't unless performance
7742 or memory numbers show me otherwise. */
7743
e7c27a73 7744 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
7745 SYMBOL_CLASS (sym) = LOC_COMPUTED;
7746}
7747
c906108c
SS
7748/* Given a pointer to a DWARF information entry, figure out if we need
7749 to make a symbol table entry for it, and if so, create a new entry
7750 and return a pointer to it.
7751 If TYPE is NULL, determine symbol type from the die, otherwise
2df3850c 7752 used the passed type. */
c906108c
SS
7753
7754static struct symbol *
e7c27a73 7755new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
c906108c 7756{
e7c27a73 7757 struct objfile *objfile = cu->objfile;
c906108c
SS
7758 struct symbol *sym = NULL;
7759 char *name;
7760 struct attribute *attr = NULL;
7761 struct attribute *attr2 = NULL;
e142c38c 7762 CORE_ADDR baseaddr;
edb3359d 7763 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
7764
7765 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7766
5c4e30ca 7767 if (die->tag != DW_TAG_namespace)
e142c38c 7768 name = dwarf2_linkage_name (die, cu);
5c4e30ca
DC
7769 else
7770 name = TYPE_NAME (type);
7771
c906108c
SS
7772 if (name)
7773 {
4a146b47 7774 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
c906108c
SS
7775 sizeof (struct symbol));
7776 OBJSTAT (objfile, n_syms++);
7777 memset (sym, 0, sizeof (struct symbol));
2de7ced7
DJ
7778
7779 /* Cache this symbol's name and the name's demangled form (if any). */
e142c38c 7780 SYMBOL_LANGUAGE (sym) = cu->language;
2de7ced7 7781 SYMBOL_SET_NAMES (sym, name, strlen (name), objfile);
c906108c
SS
7782
7783 /* Default assumptions.
c5aa993b 7784 Use the passed type or decode it from the die. */
176620f1 7785 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 7786 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
7787 if (type != NULL)
7788 SYMBOL_TYPE (sym) = type;
7789 else
e7c27a73 7790 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
7791 attr = dwarf2_attr (die,
7792 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
7793 cu);
c906108c
SS
7794 if (attr)
7795 {
7796 SYMBOL_LINE (sym) = DW_UNSND (attr);
7797 }
cb1df416 7798
edb3359d
DJ
7799 attr = dwarf2_attr (die,
7800 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
7801 cu);
cb1df416
DJ
7802 if (attr)
7803 {
7804 int file_index = DW_UNSND (attr);
7805 if (cu->line_header == NULL
7806 || file_index > cu->line_header->num_file_names)
7807 complaint (&symfile_complaints,
7808 _("file index out of range"));
1c3d648d 7809 else if (file_index > 0)
cb1df416
DJ
7810 {
7811 struct file_entry *fe;
7812 fe = &cu->line_header->file_names[file_index - 1];
7813 SYMBOL_SYMTAB (sym) = fe->symtab;
7814 }
7815 }
7816
c906108c
SS
7817 switch (die->tag)
7818 {
7819 case DW_TAG_label:
e142c38c 7820 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
7821 if (attr)
7822 {
7823 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
7824 }
7825 SYMBOL_CLASS (sym) = LOC_LABEL;
7826 break;
7827 case DW_TAG_subprogram:
7828 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
7829 finish_block. */
7830 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 7831 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
7832 if ((attr2 && (DW_UNSND (attr2) != 0))
7833 || cu->language == language_ada)
c906108c 7834 {
2cfa0c8d
JB
7835 /* Subprograms marked external are stored as a global symbol.
7836 Ada subprograms, whether marked external or not, are always
7837 stored as a global symbol, because we want to be able to
7838 access them globally. For instance, we want to be able
7839 to break on a nested subprogram without having to
7840 specify the context. */
c906108c
SS
7841 add_symbol_to_list (sym, &global_symbols);
7842 }
7843 else
7844 {
e142c38c 7845 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7846 }
7847 break;
edb3359d
DJ
7848 case DW_TAG_inlined_subroutine:
7849 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
7850 finish_block. */
7851 SYMBOL_CLASS (sym) = LOC_BLOCK;
7852 SYMBOL_INLINED (sym) = 1;
7853 /* Do not add the symbol to any lists. It will be found via
7854 BLOCK_FUNCTION from the blockvector. */
7855 break;
c906108c
SS
7856 case DW_TAG_variable:
7857 /* Compilation with minimal debug info may result in variables
7858 with missing type entries. Change the misleading `void' type
7859 to something sensible. */
7860 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 7861 SYMBOL_TYPE (sym)
46bf5051 7862 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 7863
e142c38c 7864 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
7865 if (attr)
7866 {
e7c27a73 7867 dwarf2_const_value (attr, sym, cu);
e142c38c 7868 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c
SS
7869 if (attr2 && (DW_UNSND (attr2) != 0))
7870 add_symbol_to_list (sym, &global_symbols);
7871 else
e142c38c 7872 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7873 break;
7874 }
e142c38c 7875 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7876 if (attr)
7877 {
e7c27a73 7878 var_decode_location (attr, sym, cu);
e142c38c 7879 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 7880 if (attr2 && (DW_UNSND (attr2) != 0))
4c2df51b 7881 add_symbol_to_list (sym, &global_symbols);
c906108c 7882 else
e142c38c 7883 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7884 }
7885 else
7886 {
7887 /* We do not know the address of this symbol.
c5aa993b
JM
7888 If it is an external symbol and we have type information
7889 for it, enter the symbol as a LOC_UNRESOLVED symbol.
7890 The address of the variable will then be determined from
7891 the minimal symbol table whenever the variable is
7892 referenced. */
e142c38c 7893 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 7894 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 7895 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c
SS
7896 {
7897 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
de40b933 7898 add_symbol_to_list (sym, cu->list_in_scope);
c906108c 7899 }
442ddf59
JK
7900 else if (!die_is_declaration (die, cu))
7901 {
7902 /* Use the default LOC_OPTIMIZED_OUT class. */
7903 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
7904 add_symbol_to_list (sym, cu->list_in_scope);
7905 }
c906108c
SS
7906 }
7907 break;
7908 case DW_TAG_formal_parameter:
edb3359d
DJ
7909 /* If we are inside a function, mark this as an argument. If
7910 not, we might be looking at an argument to an inlined function
7911 when we do not have enough information to show inlined frames;
7912 pretend it's a local variable in that case so that the user can
7913 still see it. */
7914 if (context_stack_depth > 0
7915 && context_stack[context_stack_depth - 1].name != NULL)
7916 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 7917 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7918 if (attr)
7919 {
e7c27a73 7920 var_decode_location (attr, sym, cu);
c906108c 7921 }
e142c38c 7922 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
7923 if (attr)
7924 {
e7c27a73 7925 dwarf2_const_value (attr, sym, cu);
c906108c 7926 }
e142c38c 7927 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7928 break;
7929 case DW_TAG_unspecified_parameters:
7930 /* From varargs functions; gdb doesn't seem to have any
7931 interest in this information, so just ignore it for now.
7932 (FIXME?) */
7933 break;
7934 case DW_TAG_class_type:
680b30c7 7935 case DW_TAG_interface_type:
c906108c
SS
7936 case DW_TAG_structure_type:
7937 case DW_TAG_union_type:
72019c9c 7938 case DW_TAG_set_type:
c906108c
SS
7939 case DW_TAG_enumeration_type:
7940 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 7941 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 7942
63d06c5c
DC
7943 /* Make sure that the symbol includes appropriate enclosing
7944 classes/namespaces in its name. These are calculated in
134d01f1 7945 read_structure_type, and the correct name is saved in
63d06c5c
DC
7946 the type. */
7947
987504bb
JJ
7948 if (cu->language == language_cplus
7949 || cu->language == language_java)
c906108c 7950 {
63d06c5c
DC
7951 struct type *type = SYMBOL_TYPE (sym);
7952
7953 if (TYPE_TAG_NAME (type) != NULL)
7954 {
7955 /* FIXME: carlton/2003-11-10: Should this use
7956 SYMBOL_SET_NAMES instead? (The same problem also
d8151005
DJ
7957 arises further down in this function.) */
7958 /* The type's name is already allocated along with
7959 this objfile, so we don't need to duplicate it
7960 for the symbol. */
7961 SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
63d06c5c 7962 }
c906108c 7963 }
63d06c5c
DC
7964
7965 {
987504bb 7966 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
7967 really ever be static objects: otherwise, if you try
7968 to, say, break of a class's method and you're in a file
7969 which doesn't mention that class, it won't work unless
7970 the check for all static symbols in lookup_symbol_aux
7971 saves you. See the OtherFileClass tests in
7972 gdb.c++/namespace.exp. */
7973
7974 struct pending **list_to_add;
7975
e142c38c 7976 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
7977 && (cu->language == language_cplus
7978 || cu->language == language_java)
e142c38c 7979 ? &global_symbols : cu->list_in_scope);
63d06c5c
DC
7980
7981 add_symbol_to_list (sym, list_to_add);
7982
7983 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 7984 defines a typedef for "foo". A Java class declaration also
5eeb2539 7985 defines a typedef for the class. */
987504bb 7986 if (cu->language == language_cplus
8c6860bb
JB
7987 || cu->language == language_java
7988 || cu->language == language_ada)
63d06c5c 7989 {
d8151005
DJ
7990 /* The symbol's name is already allocated along with
7991 this objfile, so we don't need to duplicate it for
7992 the type. */
63d06c5c 7993 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 7994 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
7995 }
7996 }
c906108c
SS
7997 break;
7998 case DW_TAG_typedef:
0114d602 7999 SYMBOL_LINKAGE_NAME (sym) = (char *) dwarf2_full_name (die, cu);
63d06c5c
DC
8000 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8001 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 8002 add_symbol_to_list (sym, cu->list_in_scope);
63d06c5c 8003 break;
c906108c 8004 case DW_TAG_base_type:
a02abb62 8005 case DW_TAG_subrange_type:
c906108c 8006 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 8007 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 8008 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
8009 break;
8010 case DW_TAG_enumerator:
0114d602 8011 SYMBOL_LINKAGE_NAME (sym) = (char *) dwarf2_full_name (die, cu);
e142c38c 8012 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
8013 if (attr)
8014 {
e7c27a73 8015 dwarf2_const_value (attr, sym, cu);
c906108c 8016 }
63d06c5c
DC
8017 {
8018 /* NOTE: carlton/2003-11-10: See comment above in the
8019 DW_TAG_class_type, etc. block. */
8020
8021 struct pending **list_to_add;
8022
e142c38c 8023 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
8024 && (cu->language == language_cplus
8025 || cu->language == language_java)
e142c38c 8026 ? &global_symbols : cu->list_in_scope);
63d06c5c
DC
8027
8028 add_symbol_to_list (sym, list_to_add);
8029 }
c906108c 8030 break;
5c4e30ca
DC
8031 case DW_TAG_namespace:
8032 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8033 add_symbol_to_list (sym, &global_symbols);
8034 break;
c906108c
SS
8035 default:
8036 /* Not a tag we recognize. Hopefully we aren't processing
8037 trash data, but since we must specifically ignore things
8038 we don't recognize, there is nothing else we should do at
8039 this point. */
e2e0b3e5 8040 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 8041 dwarf_tag_name (die->tag));
c906108c
SS
8042 break;
8043 }
df8a16a1
DJ
8044
8045 /* For the benefit of old versions of GCC, check for anonymous
8046 namespaces based on the demangled name. */
8047 if (!processing_has_namespace_info
8048 && cu->language == language_cplus
8049 && dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu) != NULL)
8050 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
8051 }
8052 return (sym);
8053}
8054
8055/* Copy constant value from an attribute to a symbol. */
8056
8057static void
107d2387 8058dwarf2_const_value (struct attribute *attr, struct symbol *sym,
e7c27a73 8059 struct dwarf2_cu *cu)
c906108c 8060{
e7c27a73
DJ
8061 struct objfile *objfile = cu->objfile;
8062 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
8063 struct dwarf_block *blk;
8064
8065 switch (attr->form)
8066 {
8067 case DW_FORM_addr:
107d2387 8068 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
3567439c 8069 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
8070 cu_header->addr_size,
8071 TYPE_LENGTH (SYMBOL_TYPE
8072 (sym)));
4e38b386 8073 SYMBOL_VALUE_BYTES (sym) =
4a146b47 8074 obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
fbd9dcd3
AC
8075 /* NOTE: cagney/2003-05-09: In-lined store_address call with
8076 it's body - store_unsigned_integer. */
8077 store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
8078 DW_ADDR (attr));
c906108c
SS
8079 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8080 break;
4ac36638 8081 case DW_FORM_string:
93b5768b
PA
8082 case DW_FORM_strp:
8083 /* DW_STRING is already allocated on the obstack, point directly
8084 to it. */
8085 SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
8086 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8087 break;
c906108c
SS
8088 case DW_FORM_block1:
8089 case DW_FORM_block2:
8090 case DW_FORM_block4:
8091 case DW_FORM_block:
8092 blk = DW_BLOCK (attr);
8093 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
3567439c 8094 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
8095 blk->size,
8096 TYPE_LENGTH (SYMBOL_TYPE
8097 (sym)));
4e38b386 8098 SYMBOL_VALUE_BYTES (sym) =
4a146b47 8099 obstack_alloc (&objfile->objfile_obstack, blk->size);
c906108c
SS
8100 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
8101 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8102 break;
2df3850c
JM
8103
8104 /* The DW_AT_const_value attributes are supposed to carry the
8105 symbol's value "represented as it would be on the target
8106 architecture." By the time we get here, it's already been
8107 converted to host endianness, so we just need to sign- or
8108 zero-extend it as appropriate. */
8109 case DW_FORM_data1:
8110 dwarf2_const_value_data (attr, sym, 8);
8111 break;
c906108c 8112 case DW_FORM_data2:
2df3850c
JM
8113 dwarf2_const_value_data (attr, sym, 16);
8114 break;
c906108c 8115 case DW_FORM_data4:
2df3850c
JM
8116 dwarf2_const_value_data (attr, sym, 32);
8117 break;
c906108c 8118 case DW_FORM_data8:
2df3850c
JM
8119 dwarf2_const_value_data (attr, sym, 64);
8120 break;
8121
c906108c 8122 case DW_FORM_sdata:
2df3850c
JM
8123 SYMBOL_VALUE (sym) = DW_SND (attr);
8124 SYMBOL_CLASS (sym) = LOC_CONST;
8125 break;
8126
c906108c
SS
8127 case DW_FORM_udata:
8128 SYMBOL_VALUE (sym) = DW_UNSND (attr);
8129 SYMBOL_CLASS (sym) = LOC_CONST;
8130 break;
2df3850c 8131
c906108c 8132 default:
4d3c2250 8133 complaint (&symfile_complaints,
e2e0b3e5 8134 _("unsupported const value attribute form: '%s'"),
4d3c2250 8135 dwarf_form_name (attr->form));
c906108c
SS
8136 SYMBOL_VALUE (sym) = 0;
8137 SYMBOL_CLASS (sym) = LOC_CONST;
8138 break;
8139 }
8140}
8141
2df3850c
JM
8142
8143/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
8144 or zero-extend it as appropriate for the symbol's type. */
8145static void
8146dwarf2_const_value_data (struct attribute *attr,
8147 struct symbol *sym,
8148 int bits)
8149{
8150 LONGEST l = DW_UNSND (attr);
8151
8152 if (bits < sizeof (l) * 8)
8153 {
8154 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
8155 l &= ((LONGEST) 1 << bits) - 1;
8156 else
bf9198f1 8157 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
2df3850c
JM
8158 }
8159
8160 SYMBOL_VALUE (sym) = l;
8161 SYMBOL_CLASS (sym) = LOC_CONST;
8162}
8163
8164
c906108c
SS
8165/* Return the type of the die in question using its DW_AT_type attribute. */
8166
8167static struct type *
e7c27a73 8168die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8169{
8170 struct type *type;
8171 struct attribute *type_attr;
8172 struct die_info *type_die;
c906108c 8173
e142c38c 8174 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
8175 if (!type_attr)
8176 {
8177 /* A missing DW_AT_type represents a void type. */
46bf5051 8178 return objfile_type (cu->objfile)->builtin_void;
c906108c
SS
8179 }
8180 else
f2f0e013 8181 type_die = follow_die_ref (die, type_attr, &cu);
10b3939b 8182
e7c27a73 8183 type = tag_type_to_type (type_die, cu);
c906108c
SS
8184 if (!type)
8185 {
d97bc12b 8186 dump_die_for_error (type_die);
8a3fe4f8 8187 error (_("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]"),
e7c27a73 8188 cu->objfile->name);
c906108c
SS
8189 }
8190 return type;
8191}
8192
8193/* Return the containing type of the die in question using its
8194 DW_AT_containing_type attribute. */
8195
8196static struct type *
e7c27a73 8197die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8198{
8199 struct type *type = NULL;
8200 struct attribute *type_attr;
8201 struct die_info *type_die = NULL;
c906108c 8202
e142c38c 8203 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
c906108c
SS
8204 if (type_attr)
8205 {
f2f0e013 8206 type_die = follow_die_ref (die, type_attr, &cu);
e7c27a73 8207 type = tag_type_to_type (type_die, cu);
c906108c
SS
8208 }
8209 if (!type)
8210 {
8211 if (type_die)
d97bc12b 8212 dump_die_for_error (type_die);
8a3fe4f8 8213 error (_("Dwarf Error: Problem turning containing type into gdb type [in module %s]"),
e7c27a73 8214 cu->objfile->name);
c906108c
SS
8215 }
8216 return type;
8217}
8218
c906108c 8219static struct type *
e7c27a73 8220tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8221{
f792889a
DJ
8222 struct type *this_type;
8223
8224 this_type = read_type_die (die, cu);
8225 if (!this_type)
c906108c 8226 {
d97bc12b 8227 dump_die_for_error (die);
f792889a
DJ
8228 error (_("Dwarf Error: Cannot find type of die [in module %s]"),
8229 cu->objfile->name);
c906108c 8230 }
f792889a 8231 return this_type;
c906108c
SS
8232}
8233
f792889a 8234static struct type *
e7c27a73 8235read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8236{
f792889a
DJ
8237 struct type *this_type;
8238
8239 this_type = get_die_type (die, cu);
8240 if (this_type)
8241 return this_type;
8242
c906108c
SS
8243 switch (die->tag)
8244 {
8245 case DW_TAG_class_type:
680b30c7 8246 case DW_TAG_interface_type:
c906108c
SS
8247 case DW_TAG_structure_type:
8248 case DW_TAG_union_type:
f792889a 8249 this_type = read_structure_type (die, cu);
c906108c
SS
8250 break;
8251 case DW_TAG_enumeration_type:
f792889a 8252 this_type = read_enumeration_type (die, cu);
c906108c
SS
8253 break;
8254 case DW_TAG_subprogram:
8255 case DW_TAG_subroutine_type:
edb3359d 8256 case DW_TAG_inlined_subroutine:
f792889a 8257 this_type = read_subroutine_type (die, cu);
c906108c
SS
8258 break;
8259 case DW_TAG_array_type:
f792889a 8260 this_type = read_array_type (die, cu);
c906108c 8261 break;
72019c9c 8262 case DW_TAG_set_type:
f792889a 8263 this_type = read_set_type (die, cu);
72019c9c 8264 break;
c906108c 8265 case DW_TAG_pointer_type:
f792889a 8266 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
8267 break;
8268 case DW_TAG_ptr_to_member_type:
f792889a 8269 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
8270 break;
8271 case DW_TAG_reference_type:
f792889a 8272 this_type = read_tag_reference_type (die, cu);
c906108c
SS
8273 break;
8274 case DW_TAG_const_type:
f792889a 8275 this_type = read_tag_const_type (die, cu);
c906108c
SS
8276 break;
8277 case DW_TAG_volatile_type:
f792889a 8278 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
8279 break;
8280 case DW_TAG_string_type:
f792889a 8281 this_type = read_tag_string_type (die, cu);
c906108c
SS
8282 break;
8283 case DW_TAG_typedef:
f792889a 8284 this_type = read_typedef (die, cu);
c906108c 8285 break;
a02abb62 8286 case DW_TAG_subrange_type:
f792889a 8287 this_type = read_subrange_type (die, cu);
a02abb62 8288 break;
c906108c 8289 case DW_TAG_base_type:
f792889a 8290 this_type = read_base_type (die, cu);
c906108c 8291 break;
81a17f79 8292 case DW_TAG_unspecified_type:
f792889a 8293 this_type = read_unspecified_type (die, cu);
81a17f79 8294 break;
0114d602
DJ
8295 case DW_TAG_namespace:
8296 this_type = read_namespace_type (die, cu);
8297 break;
c906108c 8298 default:
a1f5b845 8299 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 8300 dwarf_tag_name (die->tag));
c906108c
SS
8301 break;
8302 }
63d06c5c 8303
f792889a 8304 return this_type;
63d06c5c
DC
8305}
8306
fdde2d81 8307/* Return the name of the namespace/class that DIE is defined within,
0114d602 8308 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 8309
0114d602
DJ
8310 For example, if we're within the method foo() in the following
8311 code:
8312
8313 namespace N {
8314 class C {
8315 void foo () {
8316 }
8317 };
8318 }
8319
8320 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
8321
8322static char *
e142c38c 8323determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 8324{
0114d602
DJ
8325 struct die_info *parent, *spec_die;
8326 struct dwarf2_cu *spec_cu;
8327 struct type *parent_type;
63d06c5c 8328
987504bb
JJ
8329 if (cu->language != language_cplus
8330 && cu->language != language_java)
0114d602
DJ
8331 return "";
8332
8333 /* We have to be careful in the presence of DW_AT_specification.
8334 For example, with GCC 3.4, given the code
8335
8336 namespace N {
8337 void foo() {
8338 // Definition of N::foo.
8339 }
8340 }
8341
8342 then we'll have a tree of DIEs like this:
8343
8344 1: DW_TAG_compile_unit
8345 2: DW_TAG_namespace // N
8346 3: DW_TAG_subprogram // declaration of N::foo
8347 4: DW_TAG_subprogram // definition of N::foo
8348 DW_AT_specification // refers to die #3
8349
8350 Thus, when processing die #4, we have to pretend that we're in
8351 the context of its DW_AT_specification, namely the contex of die
8352 #3. */
8353 spec_cu = cu;
8354 spec_die = die_specification (die, &spec_cu);
8355 if (spec_die == NULL)
8356 parent = die->parent;
8357 else
63d06c5c 8358 {
0114d602
DJ
8359 parent = spec_die->parent;
8360 cu = spec_cu;
63d06c5c 8361 }
0114d602
DJ
8362
8363 if (parent == NULL)
8364 return "";
63d06c5c 8365 else
0114d602
DJ
8366 switch (parent->tag)
8367 {
63d06c5c 8368 case DW_TAG_namespace:
0114d602
DJ
8369 parent_type = read_type_die (parent, cu);
8370 /* We give a name to even anonymous namespaces. */
8371 return TYPE_TAG_NAME (parent_type);
63d06c5c 8372 case DW_TAG_class_type:
680b30c7 8373 case DW_TAG_interface_type:
63d06c5c 8374 case DW_TAG_structure_type:
0114d602
DJ
8375 case DW_TAG_union_type:
8376 parent_type = read_type_die (parent, cu);
8377 if (TYPE_TAG_NAME (parent_type) != NULL)
8378 return TYPE_TAG_NAME (parent_type);
8379 else
8380 /* An anonymous structure is only allowed non-static data
8381 members; no typedefs, no member functions, et cetera.
8382 So it does not need a prefix. */
8383 return "";
63d06c5c 8384 default:
8176b9b8 8385 return determine_prefix (parent, cu);
63d06c5c 8386 }
63d06c5c
DC
8387}
8388
987504bb
JJ
8389/* Return a newly-allocated string formed by concatenating PREFIX and
8390 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
8391 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
8392 perform an obconcat, otherwise allocate storage for the result. The CU argument
8393 is used to determine the language and hence, the appropriate separator. */
8394
8395#define MAX_SEP_LEN 2 /* sizeof ("::") */
63d06c5c
DC
8396
8397static char *
987504bb
JJ
8398typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
8399 struct dwarf2_cu *cu)
63d06c5c 8400{
987504bb 8401 char *sep;
63d06c5c 8402
987504bb
JJ
8403 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
8404 sep = "";
8405 else if (cu->language == language_java)
8406 sep = ".";
8407 else
8408 sep = "::";
63d06c5c 8409
6dd47d34
DE
8410 if (prefix == NULL)
8411 prefix = "";
8412 if (suffix == NULL)
8413 suffix = "";
8414
987504bb
JJ
8415 if (obs == NULL)
8416 {
8417 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
6dd47d34
DE
8418 strcpy (retval, prefix);
8419 strcat (retval, sep);
8420 strcat (retval, suffix);
63d06c5c
DC
8421 return retval;
8422 }
987504bb
JJ
8423 else
8424 {
8425 /* We have an obstack. */
8426 return obconcat (obs, prefix, sep, suffix);
8427 }
63d06c5c
DC
8428}
8429
c906108c
SS
8430/* Return sibling of die, NULL if no sibling. */
8431
f9aca02d 8432static struct die_info *
fba45db2 8433sibling_die (struct die_info *die)
c906108c 8434{
639d11d3 8435 return die->sibling;
c906108c
SS
8436}
8437
8438/* Get linkage name of a die, return NULL if not found. */
8439
8440static char *
e142c38c 8441dwarf2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8442{
8443 struct attribute *attr;
8444
e142c38c 8445 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
c906108c
SS
8446 if (attr && DW_STRING (attr))
8447 return DW_STRING (attr);
71c25dea
TT
8448 return dwarf2_name (die, cu);
8449}
8450
8451/* Get name of a die, return NULL if not found. */
8452
8453static char *
8454dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
8455 struct obstack *obstack)
8456{
8457 if (name && cu->language == language_cplus)
8458 {
8459 char *canon_name = cp_canonicalize_string (name);
8460
8461 if (canon_name != NULL)
8462 {
8463 if (strcmp (canon_name, name) != 0)
8464 name = obsavestring (canon_name, strlen (canon_name),
8465 obstack);
8466 xfree (canon_name);
8467 }
8468 }
8469
8470 return name;
c906108c
SS
8471}
8472
9219021c
DC
8473/* Get name of a die, return NULL if not found. */
8474
8475static char *
e142c38c 8476dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
8477{
8478 struct attribute *attr;
8479
e142c38c 8480 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
8481 if (!attr || !DW_STRING (attr))
8482 return NULL;
8483
8484 switch (die->tag)
8485 {
8486 case DW_TAG_compile_unit:
8487 /* Compilation units have a DW_AT_name that is a filename, not
8488 a source language identifier. */
8489 case DW_TAG_enumeration_type:
8490 case DW_TAG_enumerator:
8491 /* These tags always have simple identifiers already; no need
8492 to canonicalize them. */
8493 return DW_STRING (attr);
8494 default:
8495 if (attr->form != GDB_FORM_cached_string)
8496 {
8497 DW_STRING (attr)
8498 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
8499 &cu->objfile->objfile_obstack);
8500 attr->form = GDB_FORM_cached_string;
8501 }
8502 return DW_STRING (attr);
8503 }
9219021c
DC
8504}
8505
8506/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
8507 is none. *EXT_CU is the CU containing DIE on input, and the CU
8508 containing the return value on output. */
9219021c
DC
8509
8510static struct die_info *
f2f0e013 8511dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
8512{
8513 struct attribute *attr;
9219021c 8514
f2f0e013 8515 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
8516 if (attr == NULL)
8517 return NULL;
8518
f2f0e013 8519 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
8520}
8521
c906108c
SS
8522/* Convert a DIE tag into its string name. */
8523
8524static char *
aa1ee363 8525dwarf_tag_name (unsigned tag)
c906108c
SS
8526{
8527 switch (tag)
8528 {
8529 case DW_TAG_padding:
8530 return "DW_TAG_padding";
8531 case DW_TAG_array_type:
8532 return "DW_TAG_array_type";
8533 case DW_TAG_class_type:
8534 return "DW_TAG_class_type";
8535 case DW_TAG_entry_point:
8536 return "DW_TAG_entry_point";
8537 case DW_TAG_enumeration_type:
8538 return "DW_TAG_enumeration_type";
8539 case DW_TAG_formal_parameter:
8540 return "DW_TAG_formal_parameter";
8541 case DW_TAG_imported_declaration:
8542 return "DW_TAG_imported_declaration";
8543 case DW_TAG_label:
8544 return "DW_TAG_label";
8545 case DW_TAG_lexical_block:
8546 return "DW_TAG_lexical_block";
8547 case DW_TAG_member:
8548 return "DW_TAG_member";
8549 case DW_TAG_pointer_type:
8550 return "DW_TAG_pointer_type";
8551 case DW_TAG_reference_type:
8552 return "DW_TAG_reference_type";
8553 case DW_TAG_compile_unit:
8554 return "DW_TAG_compile_unit";
8555 case DW_TAG_string_type:
8556 return "DW_TAG_string_type";
8557 case DW_TAG_structure_type:
8558 return "DW_TAG_structure_type";
8559 case DW_TAG_subroutine_type:
8560 return "DW_TAG_subroutine_type";
8561 case DW_TAG_typedef:
8562 return "DW_TAG_typedef";
8563 case DW_TAG_union_type:
8564 return "DW_TAG_union_type";
8565 case DW_TAG_unspecified_parameters:
8566 return "DW_TAG_unspecified_parameters";
8567 case DW_TAG_variant:
8568 return "DW_TAG_variant";
8569 case DW_TAG_common_block:
8570 return "DW_TAG_common_block";
8571 case DW_TAG_common_inclusion:
8572 return "DW_TAG_common_inclusion";
8573 case DW_TAG_inheritance:
8574 return "DW_TAG_inheritance";
8575 case DW_TAG_inlined_subroutine:
8576 return "DW_TAG_inlined_subroutine";
8577 case DW_TAG_module:
8578 return "DW_TAG_module";
8579 case DW_TAG_ptr_to_member_type:
8580 return "DW_TAG_ptr_to_member_type";
8581 case DW_TAG_set_type:
8582 return "DW_TAG_set_type";
8583 case DW_TAG_subrange_type:
8584 return "DW_TAG_subrange_type";
8585 case DW_TAG_with_stmt:
8586 return "DW_TAG_with_stmt";
8587 case DW_TAG_access_declaration:
8588 return "DW_TAG_access_declaration";
8589 case DW_TAG_base_type:
8590 return "DW_TAG_base_type";
8591 case DW_TAG_catch_block:
8592 return "DW_TAG_catch_block";
8593 case DW_TAG_const_type:
8594 return "DW_TAG_const_type";
8595 case DW_TAG_constant:
8596 return "DW_TAG_constant";
8597 case DW_TAG_enumerator:
8598 return "DW_TAG_enumerator";
8599 case DW_TAG_file_type:
8600 return "DW_TAG_file_type";
8601 case DW_TAG_friend:
8602 return "DW_TAG_friend";
8603 case DW_TAG_namelist:
8604 return "DW_TAG_namelist";
8605 case DW_TAG_namelist_item:
8606 return "DW_TAG_namelist_item";
8607 case DW_TAG_packed_type:
8608 return "DW_TAG_packed_type";
8609 case DW_TAG_subprogram:
8610 return "DW_TAG_subprogram";
8611 case DW_TAG_template_type_param:
8612 return "DW_TAG_template_type_param";
8613 case DW_TAG_template_value_param:
8614 return "DW_TAG_template_value_param";
8615 case DW_TAG_thrown_type:
8616 return "DW_TAG_thrown_type";
8617 case DW_TAG_try_block:
8618 return "DW_TAG_try_block";
8619 case DW_TAG_variant_part:
8620 return "DW_TAG_variant_part";
8621 case DW_TAG_variable:
8622 return "DW_TAG_variable";
8623 case DW_TAG_volatile_type:
8624 return "DW_TAG_volatile_type";
d9fa45fe
DC
8625 case DW_TAG_dwarf_procedure:
8626 return "DW_TAG_dwarf_procedure";
8627 case DW_TAG_restrict_type:
8628 return "DW_TAG_restrict_type";
8629 case DW_TAG_interface_type:
8630 return "DW_TAG_interface_type";
8631 case DW_TAG_namespace:
8632 return "DW_TAG_namespace";
8633 case DW_TAG_imported_module:
8634 return "DW_TAG_imported_module";
8635 case DW_TAG_unspecified_type:
8636 return "DW_TAG_unspecified_type";
8637 case DW_TAG_partial_unit:
8638 return "DW_TAG_partial_unit";
8639 case DW_TAG_imported_unit:
8640 return "DW_TAG_imported_unit";
b7619582
GF
8641 case DW_TAG_condition:
8642 return "DW_TAG_condition";
8643 case DW_TAG_shared_type:
8644 return "DW_TAG_shared_type";
c906108c
SS
8645 case DW_TAG_MIPS_loop:
8646 return "DW_TAG_MIPS_loop";
b7619582
GF
8647 case DW_TAG_HP_array_descriptor:
8648 return "DW_TAG_HP_array_descriptor";
c906108c
SS
8649 case DW_TAG_format_label:
8650 return "DW_TAG_format_label";
8651 case DW_TAG_function_template:
8652 return "DW_TAG_function_template";
8653 case DW_TAG_class_template:
8654 return "DW_TAG_class_template";
b7619582
GF
8655 case DW_TAG_GNU_BINCL:
8656 return "DW_TAG_GNU_BINCL";
8657 case DW_TAG_GNU_EINCL:
8658 return "DW_TAG_GNU_EINCL";
8659 case DW_TAG_upc_shared_type:
8660 return "DW_TAG_upc_shared_type";
8661 case DW_TAG_upc_strict_type:
8662 return "DW_TAG_upc_strict_type";
8663 case DW_TAG_upc_relaxed_type:
8664 return "DW_TAG_upc_relaxed_type";
8665 case DW_TAG_PGI_kanji_type:
8666 return "DW_TAG_PGI_kanji_type";
8667 case DW_TAG_PGI_interface_block:
8668 return "DW_TAG_PGI_interface_block";
c906108c
SS
8669 default:
8670 return "DW_TAG_<unknown>";
8671 }
8672}
8673
8674/* Convert a DWARF attribute code into its string name. */
8675
8676static char *
aa1ee363 8677dwarf_attr_name (unsigned attr)
c906108c
SS
8678{
8679 switch (attr)
8680 {
8681 case DW_AT_sibling:
8682 return "DW_AT_sibling";
8683 case DW_AT_location:
8684 return "DW_AT_location";
8685 case DW_AT_name:
8686 return "DW_AT_name";
8687 case DW_AT_ordering:
8688 return "DW_AT_ordering";
8689 case DW_AT_subscr_data:
8690 return "DW_AT_subscr_data";
8691 case DW_AT_byte_size:
8692 return "DW_AT_byte_size";
8693 case DW_AT_bit_offset:
8694 return "DW_AT_bit_offset";
8695 case DW_AT_bit_size:
8696 return "DW_AT_bit_size";
8697 case DW_AT_element_list:
8698 return "DW_AT_element_list";
8699 case DW_AT_stmt_list:
8700 return "DW_AT_stmt_list";
8701 case DW_AT_low_pc:
8702 return "DW_AT_low_pc";
8703 case DW_AT_high_pc:
8704 return "DW_AT_high_pc";
8705 case DW_AT_language:
8706 return "DW_AT_language";
8707 case DW_AT_member:
8708 return "DW_AT_member";
8709 case DW_AT_discr:
8710 return "DW_AT_discr";
8711 case DW_AT_discr_value:
8712 return "DW_AT_discr_value";
8713 case DW_AT_visibility:
8714 return "DW_AT_visibility";
8715 case DW_AT_import:
8716 return "DW_AT_import";
8717 case DW_AT_string_length:
8718 return "DW_AT_string_length";
8719 case DW_AT_common_reference:
8720 return "DW_AT_common_reference";
8721 case DW_AT_comp_dir:
8722 return "DW_AT_comp_dir";
8723 case DW_AT_const_value:
8724 return "DW_AT_const_value";
8725 case DW_AT_containing_type:
8726 return "DW_AT_containing_type";
8727 case DW_AT_default_value:
8728 return "DW_AT_default_value";
8729 case DW_AT_inline:
8730 return "DW_AT_inline";
8731 case DW_AT_is_optional:
8732 return "DW_AT_is_optional";
8733 case DW_AT_lower_bound:
8734 return "DW_AT_lower_bound";
8735 case DW_AT_producer:
8736 return "DW_AT_producer";
8737 case DW_AT_prototyped:
8738 return "DW_AT_prototyped";
8739 case DW_AT_return_addr:
8740 return "DW_AT_return_addr";
8741 case DW_AT_start_scope:
8742 return "DW_AT_start_scope";
09fa0d7c
JK
8743 case DW_AT_bit_stride:
8744 return "DW_AT_bit_stride";
c906108c
SS
8745 case DW_AT_upper_bound:
8746 return "DW_AT_upper_bound";
8747 case DW_AT_abstract_origin:
8748 return "DW_AT_abstract_origin";
8749 case DW_AT_accessibility:
8750 return "DW_AT_accessibility";
8751 case DW_AT_address_class:
8752 return "DW_AT_address_class";
8753 case DW_AT_artificial:
8754 return "DW_AT_artificial";
8755 case DW_AT_base_types:
8756 return "DW_AT_base_types";
8757 case DW_AT_calling_convention:
8758 return "DW_AT_calling_convention";
8759 case DW_AT_count:
8760 return "DW_AT_count";
8761 case DW_AT_data_member_location:
8762 return "DW_AT_data_member_location";
8763 case DW_AT_decl_column:
8764 return "DW_AT_decl_column";
8765 case DW_AT_decl_file:
8766 return "DW_AT_decl_file";
8767 case DW_AT_decl_line:
8768 return "DW_AT_decl_line";
8769 case DW_AT_declaration:
8770 return "DW_AT_declaration";
8771 case DW_AT_discr_list:
8772 return "DW_AT_discr_list";
8773 case DW_AT_encoding:
8774 return "DW_AT_encoding";
8775 case DW_AT_external:
8776 return "DW_AT_external";
8777 case DW_AT_frame_base:
8778 return "DW_AT_frame_base";
8779 case DW_AT_friend:
8780 return "DW_AT_friend";
8781 case DW_AT_identifier_case:
8782 return "DW_AT_identifier_case";
8783 case DW_AT_macro_info:
8784 return "DW_AT_macro_info";
8785 case DW_AT_namelist_items:
8786 return "DW_AT_namelist_items";
8787 case DW_AT_priority:
8788 return "DW_AT_priority";
8789 case DW_AT_segment:
8790 return "DW_AT_segment";
8791 case DW_AT_specification:
8792 return "DW_AT_specification";
8793 case DW_AT_static_link:
8794 return "DW_AT_static_link";
8795 case DW_AT_type:
8796 return "DW_AT_type";
8797 case DW_AT_use_location:
8798 return "DW_AT_use_location";
8799 case DW_AT_variable_parameter:
8800 return "DW_AT_variable_parameter";
8801 case DW_AT_virtuality:
8802 return "DW_AT_virtuality";
8803 case DW_AT_vtable_elem_location:
8804 return "DW_AT_vtable_elem_location";
b7619582 8805 /* DWARF 3 values. */
d9fa45fe
DC
8806 case DW_AT_allocated:
8807 return "DW_AT_allocated";
8808 case DW_AT_associated:
8809 return "DW_AT_associated";
8810 case DW_AT_data_location:
8811 return "DW_AT_data_location";
09fa0d7c
JK
8812 case DW_AT_byte_stride:
8813 return "DW_AT_byte_stride";
d9fa45fe
DC
8814 case DW_AT_entry_pc:
8815 return "DW_AT_entry_pc";
8816 case DW_AT_use_UTF8:
8817 return "DW_AT_use_UTF8";
8818 case DW_AT_extension:
8819 return "DW_AT_extension";
8820 case DW_AT_ranges:
8821 return "DW_AT_ranges";
8822 case DW_AT_trampoline:
8823 return "DW_AT_trampoline";
8824 case DW_AT_call_column:
8825 return "DW_AT_call_column";
8826 case DW_AT_call_file:
8827 return "DW_AT_call_file";
8828 case DW_AT_call_line:
8829 return "DW_AT_call_line";
b7619582
GF
8830 case DW_AT_description:
8831 return "DW_AT_description";
8832 case DW_AT_binary_scale:
8833 return "DW_AT_binary_scale";
8834 case DW_AT_decimal_scale:
8835 return "DW_AT_decimal_scale";
8836 case DW_AT_small:
8837 return "DW_AT_small";
8838 case DW_AT_decimal_sign:
8839 return "DW_AT_decimal_sign";
8840 case DW_AT_digit_count:
8841 return "DW_AT_digit_count";
8842 case DW_AT_picture_string:
8843 return "DW_AT_picture_string";
8844 case DW_AT_mutable:
8845 return "DW_AT_mutable";
8846 case DW_AT_threads_scaled:
8847 return "DW_AT_threads_scaled";
8848 case DW_AT_explicit:
8849 return "DW_AT_explicit";
8850 case DW_AT_object_pointer:
8851 return "DW_AT_object_pointer";
8852 case DW_AT_endianity:
8853 return "DW_AT_endianity";
8854 case DW_AT_elemental:
8855 return "DW_AT_elemental";
8856 case DW_AT_pure:
8857 return "DW_AT_pure";
8858 case DW_AT_recursive:
8859 return "DW_AT_recursive";
b7619582 8860 /* SGI/MIPS extensions. */
c764a876 8861#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
8862 case DW_AT_MIPS_fde:
8863 return "DW_AT_MIPS_fde";
c764a876 8864#endif
c906108c
SS
8865 case DW_AT_MIPS_loop_begin:
8866 return "DW_AT_MIPS_loop_begin";
8867 case DW_AT_MIPS_tail_loop_begin:
8868 return "DW_AT_MIPS_tail_loop_begin";
8869 case DW_AT_MIPS_epilog_begin:
8870 return "DW_AT_MIPS_epilog_begin";
8871 case DW_AT_MIPS_loop_unroll_factor:
8872 return "DW_AT_MIPS_loop_unroll_factor";
8873 case DW_AT_MIPS_software_pipeline_depth:
8874 return "DW_AT_MIPS_software_pipeline_depth";
8875 case DW_AT_MIPS_linkage_name:
8876 return "DW_AT_MIPS_linkage_name";
b7619582
GF
8877 case DW_AT_MIPS_stride:
8878 return "DW_AT_MIPS_stride";
8879 case DW_AT_MIPS_abstract_name:
8880 return "DW_AT_MIPS_abstract_name";
8881 case DW_AT_MIPS_clone_origin:
8882 return "DW_AT_MIPS_clone_origin";
8883 case DW_AT_MIPS_has_inlines:
8884 return "DW_AT_MIPS_has_inlines";
b7619582 8885 /* HP extensions. */
c764a876 8886#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
8887 case DW_AT_HP_block_index:
8888 return "DW_AT_HP_block_index";
c764a876 8889#endif
b7619582
GF
8890 case DW_AT_HP_unmodifiable:
8891 return "DW_AT_HP_unmodifiable";
8892 case DW_AT_HP_actuals_stmt_list:
8893 return "DW_AT_HP_actuals_stmt_list";
8894 case DW_AT_HP_proc_per_section:
8895 return "DW_AT_HP_proc_per_section";
8896 case DW_AT_HP_raw_data_ptr:
8897 return "DW_AT_HP_raw_data_ptr";
8898 case DW_AT_HP_pass_by_reference:
8899 return "DW_AT_HP_pass_by_reference";
8900 case DW_AT_HP_opt_level:
8901 return "DW_AT_HP_opt_level";
8902 case DW_AT_HP_prof_version_id:
8903 return "DW_AT_HP_prof_version_id";
8904 case DW_AT_HP_opt_flags:
8905 return "DW_AT_HP_opt_flags";
8906 case DW_AT_HP_cold_region_low_pc:
8907 return "DW_AT_HP_cold_region_low_pc";
8908 case DW_AT_HP_cold_region_high_pc:
8909 return "DW_AT_HP_cold_region_high_pc";
8910 case DW_AT_HP_all_variables_modifiable:
8911 return "DW_AT_HP_all_variables_modifiable";
8912 case DW_AT_HP_linkage_name:
8913 return "DW_AT_HP_linkage_name";
8914 case DW_AT_HP_prof_flags:
8915 return "DW_AT_HP_prof_flags";
8916 /* GNU extensions. */
c906108c
SS
8917 case DW_AT_sf_names:
8918 return "DW_AT_sf_names";
8919 case DW_AT_src_info:
8920 return "DW_AT_src_info";
8921 case DW_AT_mac_info:
8922 return "DW_AT_mac_info";
8923 case DW_AT_src_coords:
8924 return "DW_AT_src_coords";
8925 case DW_AT_body_begin:
8926 return "DW_AT_body_begin";
8927 case DW_AT_body_end:
8928 return "DW_AT_body_end";
f5f8a009
EZ
8929 case DW_AT_GNU_vector:
8930 return "DW_AT_GNU_vector";
b7619582
GF
8931 /* VMS extensions. */
8932 case DW_AT_VMS_rtnbeg_pd_address:
8933 return "DW_AT_VMS_rtnbeg_pd_address";
8934 /* UPC extension. */
8935 case DW_AT_upc_threads_scaled:
8936 return "DW_AT_upc_threads_scaled";
8937 /* PGI (STMicroelectronics) extensions. */
8938 case DW_AT_PGI_lbase:
8939 return "DW_AT_PGI_lbase";
8940 case DW_AT_PGI_soffset:
8941 return "DW_AT_PGI_soffset";
8942 case DW_AT_PGI_lstride:
8943 return "DW_AT_PGI_lstride";
c906108c
SS
8944 default:
8945 return "DW_AT_<unknown>";
8946 }
8947}
8948
8949/* Convert a DWARF value form code into its string name. */
8950
8951static char *
aa1ee363 8952dwarf_form_name (unsigned form)
c906108c
SS
8953{
8954 switch (form)
8955 {
8956 case DW_FORM_addr:
8957 return "DW_FORM_addr";
8958 case DW_FORM_block2:
8959 return "DW_FORM_block2";
8960 case DW_FORM_block4:
8961 return "DW_FORM_block4";
8962 case DW_FORM_data2:
8963 return "DW_FORM_data2";
8964 case DW_FORM_data4:
8965 return "DW_FORM_data4";
8966 case DW_FORM_data8:
8967 return "DW_FORM_data8";
8968 case DW_FORM_string:
8969 return "DW_FORM_string";
8970 case DW_FORM_block:
8971 return "DW_FORM_block";
8972 case DW_FORM_block1:
8973 return "DW_FORM_block1";
8974 case DW_FORM_data1:
8975 return "DW_FORM_data1";
8976 case DW_FORM_flag:
8977 return "DW_FORM_flag";
8978 case DW_FORM_sdata:
8979 return "DW_FORM_sdata";
8980 case DW_FORM_strp:
8981 return "DW_FORM_strp";
8982 case DW_FORM_udata:
8983 return "DW_FORM_udata";
8984 case DW_FORM_ref_addr:
8985 return "DW_FORM_ref_addr";
8986 case DW_FORM_ref1:
8987 return "DW_FORM_ref1";
8988 case DW_FORM_ref2:
8989 return "DW_FORM_ref2";
8990 case DW_FORM_ref4:
8991 return "DW_FORM_ref4";
8992 case DW_FORM_ref8:
8993 return "DW_FORM_ref8";
8994 case DW_FORM_ref_udata:
8995 return "DW_FORM_ref_udata";
8996 case DW_FORM_indirect:
8997 return "DW_FORM_indirect";
71c25dea
TT
8998 case GDB_FORM_cached_string:
8999 return "GDB_FORM_cached_string";
c906108c
SS
9000 default:
9001 return "DW_FORM_<unknown>";
9002 }
9003}
9004
9005/* Convert a DWARF stack opcode into its string name. */
9006
9007static char *
aa1ee363 9008dwarf_stack_op_name (unsigned op)
c906108c
SS
9009{
9010 switch (op)
9011 {
9012 case DW_OP_addr:
9013 return "DW_OP_addr";
9014 case DW_OP_deref:
9015 return "DW_OP_deref";
9016 case DW_OP_const1u:
9017 return "DW_OP_const1u";
9018 case DW_OP_const1s:
9019 return "DW_OP_const1s";
9020 case DW_OP_const2u:
9021 return "DW_OP_const2u";
9022 case DW_OP_const2s:
9023 return "DW_OP_const2s";
9024 case DW_OP_const4u:
9025 return "DW_OP_const4u";
9026 case DW_OP_const4s:
9027 return "DW_OP_const4s";
9028 case DW_OP_const8u:
9029 return "DW_OP_const8u";
9030 case DW_OP_const8s:
9031 return "DW_OP_const8s";
9032 case DW_OP_constu:
9033 return "DW_OP_constu";
9034 case DW_OP_consts:
9035 return "DW_OP_consts";
9036 case DW_OP_dup:
9037 return "DW_OP_dup";
9038 case DW_OP_drop:
9039 return "DW_OP_drop";
9040 case DW_OP_over:
9041 return "DW_OP_over";
9042 case DW_OP_pick:
9043 return "DW_OP_pick";
9044 case DW_OP_swap:
9045 return "DW_OP_swap";
9046 case DW_OP_rot:
9047 return "DW_OP_rot";
9048 case DW_OP_xderef:
9049 return "DW_OP_xderef";
9050 case DW_OP_abs:
9051 return "DW_OP_abs";
9052 case DW_OP_and:
9053 return "DW_OP_and";
9054 case DW_OP_div:
9055 return "DW_OP_div";
9056 case DW_OP_minus:
9057 return "DW_OP_minus";
9058 case DW_OP_mod:
9059 return "DW_OP_mod";
9060 case DW_OP_mul:
9061 return "DW_OP_mul";
9062 case DW_OP_neg:
9063 return "DW_OP_neg";
9064 case DW_OP_not:
9065 return "DW_OP_not";
9066 case DW_OP_or:
9067 return "DW_OP_or";
9068 case DW_OP_plus:
9069 return "DW_OP_plus";
9070 case DW_OP_plus_uconst:
9071 return "DW_OP_plus_uconst";
9072 case DW_OP_shl:
9073 return "DW_OP_shl";
9074 case DW_OP_shr:
9075 return "DW_OP_shr";
9076 case DW_OP_shra:
9077 return "DW_OP_shra";
9078 case DW_OP_xor:
9079 return "DW_OP_xor";
9080 case DW_OP_bra:
9081 return "DW_OP_bra";
9082 case DW_OP_eq:
9083 return "DW_OP_eq";
9084 case DW_OP_ge:
9085 return "DW_OP_ge";
9086 case DW_OP_gt:
9087 return "DW_OP_gt";
9088 case DW_OP_le:
9089 return "DW_OP_le";
9090 case DW_OP_lt:
9091 return "DW_OP_lt";
9092 case DW_OP_ne:
9093 return "DW_OP_ne";
9094 case DW_OP_skip:
9095 return "DW_OP_skip";
9096 case DW_OP_lit0:
9097 return "DW_OP_lit0";
9098 case DW_OP_lit1:
9099 return "DW_OP_lit1";
9100 case DW_OP_lit2:
9101 return "DW_OP_lit2";
9102 case DW_OP_lit3:
9103 return "DW_OP_lit3";
9104 case DW_OP_lit4:
9105 return "DW_OP_lit4";
9106 case DW_OP_lit5:
9107 return "DW_OP_lit5";
9108 case DW_OP_lit6:
9109 return "DW_OP_lit6";
9110 case DW_OP_lit7:
9111 return "DW_OP_lit7";
9112 case DW_OP_lit8:
9113 return "DW_OP_lit8";
9114 case DW_OP_lit9:
9115 return "DW_OP_lit9";
9116 case DW_OP_lit10:
9117 return "DW_OP_lit10";
9118 case DW_OP_lit11:
9119 return "DW_OP_lit11";
9120 case DW_OP_lit12:
9121 return "DW_OP_lit12";
9122 case DW_OP_lit13:
9123 return "DW_OP_lit13";
9124 case DW_OP_lit14:
9125 return "DW_OP_lit14";
9126 case DW_OP_lit15:
9127 return "DW_OP_lit15";
9128 case DW_OP_lit16:
9129 return "DW_OP_lit16";
9130 case DW_OP_lit17:
9131 return "DW_OP_lit17";
9132 case DW_OP_lit18:
9133 return "DW_OP_lit18";
9134 case DW_OP_lit19:
9135 return "DW_OP_lit19";
9136 case DW_OP_lit20:
9137 return "DW_OP_lit20";
9138 case DW_OP_lit21:
9139 return "DW_OP_lit21";
9140 case DW_OP_lit22:
9141 return "DW_OP_lit22";
9142 case DW_OP_lit23:
9143 return "DW_OP_lit23";
9144 case DW_OP_lit24:
9145 return "DW_OP_lit24";
9146 case DW_OP_lit25:
9147 return "DW_OP_lit25";
9148 case DW_OP_lit26:
9149 return "DW_OP_lit26";
9150 case DW_OP_lit27:
9151 return "DW_OP_lit27";
9152 case DW_OP_lit28:
9153 return "DW_OP_lit28";
9154 case DW_OP_lit29:
9155 return "DW_OP_lit29";
9156 case DW_OP_lit30:
9157 return "DW_OP_lit30";
9158 case DW_OP_lit31:
9159 return "DW_OP_lit31";
9160 case DW_OP_reg0:
9161 return "DW_OP_reg0";
9162 case DW_OP_reg1:
9163 return "DW_OP_reg1";
9164 case DW_OP_reg2:
9165 return "DW_OP_reg2";
9166 case DW_OP_reg3:
9167 return "DW_OP_reg3";
9168 case DW_OP_reg4:
9169 return "DW_OP_reg4";
9170 case DW_OP_reg5:
9171 return "DW_OP_reg5";
9172 case DW_OP_reg6:
9173 return "DW_OP_reg6";
9174 case DW_OP_reg7:
9175 return "DW_OP_reg7";
9176 case DW_OP_reg8:
9177 return "DW_OP_reg8";
9178 case DW_OP_reg9:
9179 return "DW_OP_reg9";
9180 case DW_OP_reg10:
9181 return "DW_OP_reg10";
9182 case DW_OP_reg11:
9183 return "DW_OP_reg11";
9184 case DW_OP_reg12:
9185 return "DW_OP_reg12";
9186 case DW_OP_reg13:
9187 return "DW_OP_reg13";
9188 case DW_OP_reg14:
9189 return "DW_OP_reg14";
9190 case DW_OP_reg15:
9191 return "DW_OP_reg15";
9192 case DW_OP_reg16:
9193 return "DW_OP_reg16";
9194 case DW_OP_reg17:
9195 return "DW_OP_reg17";
9196 case DW_OP_reg18:
9197 return "DW_OP_reg18";
9198 case DW_OP_reg19:
9199 return "DW_OP_reg19";
9200 case DW_OP_reg20:
9201 return "DW_OP_reg20";
9202 case DW_OP_reg21:
9203 return "DW_OP_reg21";
9204 case DW_OP_reg22:
9205 return "DW_OP_reg22";
9206 case DW_OP_reg23:
9207 return "DW_OP_reg23";
9208 case DW_OP_reg24:
9209 return "DW_OP_reg24";
9210 case DW_OP_reg25:
9211 return "DW_OP_reg25";
9212 case DW_OP_reg26:
9213 return "DW_OP_reg26";
9214 case DW_OP_reg27:
9215 return "DW_OP_reg27";
9216 case DW_OP_reg28:
9217 return "DW_OP_reg28";
9218 case DW_OP_reg29:
9219 return "DW_OP_reg29";
9220 case DW_OP_reg30:
9221 return "DW_OP_reg30";
9222 case DW_OP_reg31:
9223 return "DW_OP_reg31";
9224 case DW_OP_breg0:
9225 return "DW_OP_breg0";
9226 case DW_OP_breg1:
9227 return "DW_OP_breg1";
9228 case DW_OP_breg2:
9229 return "DW_OP_breg2";
9230 case DW_OP_breg3:
9231 return "DW_OP_breg3";
9232 case DW_OP_breg4:
9233 return "DW_OP_breg4";
9234 case DW_OP_breg5:
9235 return "DW_OP_breg5";
9236 case DW_OP_breg6:
9237 return "DW_OP_breg6";
9238 case DW_OP_breg7:
9239 return "DW_OP_breg7";
9240 case DW_OP_breg8:
9241 return "DW_OP_breg8";
9242 case DW_OP_breg9:
9243 return "DW_OP_breg9";
9244 case DW_OP_breg10:
9245 return "DW_OP_breg10";
9246 case DW_OP_breg11:
9247 return "DW_OP_breg11";
9248 case DW_OP_breg12:
9249 return "DW_OP_breg12";
9250 case DW_OP_breg13:
9251 return "DW_OP_breg13";
9252 case DW_OP_breg14:
9253 return "DW_OP_breg14";
9254 case DW_OP_breg15:
9255 return "DW_OP_breg15";
9256 case DW_OP_breg16:
9257 return "DW_OP_breg16";
9258 case DW_OP_breg17:
9259 return "DW_OP_breg17";
9260 case DW_OP_breg18:
9261 return "DW_OP_breg18";
9262 case DW_OP_breg19:
9263 return "DW_OP_breg19";
9264 case DW_OP_breg20:
9265 return "DW_OP_breg20";
9266 case DW_OP_breg21:
9267 return "DW_OP_breg21";
9268 case DW_OP_breg22:
9269 return "DW_OP_breg22";
9270 case DW_OP_breg23:
9271 return "DW_OP_breg23";
9272 case DW_OP_breg24:
9273 return "DW_OP_breg24";
9274 case DW_OP_breg25:
9275 return "DW_OP_breg25";
9276 case DW_OP_breg26:
9277 return "DW_OP_breg26";
9278 case DW_OP_breg27:
9279 return "DW_OP_breg27";
9280 case DW_OP_breg28:
9281 return "DW_OP_breg28";
9282 case DW_OP_breg29:
9283 return "DW_OP_breg29";
9284 case DW_OP_breg30:
9285 return "DW_OP_breg30";
9286 case DW_OP_breg31:
9287 return "DW_OP_breg31";
9288 case DW_OP_regx:
9289 return "DW_OP_regx";
9290 case DW_OP_fbreg:
9291 return "DW_OP_fbreg";
9292 case DW_OP_bregx:
9293 return "DW_OP_bregx";
9294 case DW_OP_piece:
9295 return "DW_OP_piece";
9296 case DW_OP_deref_size:
9297 return "DW_OP_deref_size";
9298 case DW_OP_xderef_size:
9299 return "DW_OP_xderef_size";
9300 case DW_OP_nop:
9301 return "DW_OP_nop";
b7619582 9302 /* DWARF 3 extensions. */
ed348acc
EZ
9303 case DW_OP_push_object_address:
9304 return "DW_OP_push_object_address";
9305 case DW_OP_call2:
9306 return "DW_OP_call2";
9307 case DW_OP_call4:
9308 return "DW_OP_call4";
9309 case DW_OP_call_ref:
9310 return "DW_OP_call_ref";
b7619582
GF
9311 /* GNU extensions. */
9312 case DW_OP_form_tls_address:
9313 return "DW_OP_form_tls_address";
9314 case DW_OP_call_frame_cfa:
9315 return "DW_OP_call_frame_cfa";
9316 case DW_OP_bit_piece:
9317 return "DW_OP_bit_piece";
ed348acc
EZ
9318 case DW_OP_GNU_push_tls_address:
9319 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
9320 case DW_OP_GNU_uninit:
9321 return "DW_OP_GNU_uninit";
b7619582
GF
9322 /* HP extensions. */
9323 case DW_OP_HP_is_value:
9324 return "DW_OP_HP_is_value";
9325 case DW_OP_HP_fltconst4:
9326 return "DW_OP_HP_fltconst4";
9327 case DW_OP_HP_fltconst8:
9328 return "DW_OP_HP_fltconst8";
9329 case DW_OP_HP_mod_range:
9330 return "DW_OP_HP_mod_range";
9331 case DW_OP_HP_unmod_range:
9332 return "DW_OP_HP_unmod_range";
9333 case DW_OP_HP_tls:
9334 return "DW_OP_HP_tls";
c906108c
SS
9335 default:
9336 return "OP_<unknown>";
9337 }
9338}
9339
9340static char *
fba45db2 9341dwarf_bool_name (unsigned mybool)
c906108c
SS
9342{
9343 if (mybool)
9344 return "TRUE";
9345 else
9346 return "FALSE";
9347}
9348
9349/* Convert a DWARF type code into its string name. */
9350
9351static char *
aa1ee363 9352dwarf_type_encoding_name (unsigned enc)
c906108c
SS
9353{
9354 switch (enc)
9355 {
b7619582
GF
9356 case DW_ATE_void:
9357 return "DW_ATE_void";
c906108c
SS
9358 case DW_ATE_address:
9359 return "DW_ATE_address";
9360 case DW_ATE_boolean:
9361 return "DW_ATE_boolean";
9362 case DW_ATE_complex_float:
9363 return "DW_ATE_complex_float";
9364 case DW_ATE_float:
9365 return "DW_ATE_float";
9366 case DW_ATE_signed:
9367 return "DW_ATE_signed";
9368 case DW_ATE_signed_char:
9369 return "DW_ATE_signed_char";
9370 case DW_ATE_unsigned:
9371 return "DW_ATE_unsigned";
9372 case DW_ATE_unsigned_char:
9373 return "DW_ATE_unsigned_char";
b7619582 9374 /* DWARF 3. */
d9fa45fe
DC
9375 case DW_ATE_imaginary_float:
9376 return "DW_ATE_imaginary_float";
b7619582
GF
9377 case DW_ATE_packed_decimal:
9378 return "DW_ATE_packed_decimal";
9379 case DW_ATE_numeric_string:
9380 return "DW_ATE_numeric_string";
9381 case DW_ATE_edited:
9382 return "DW_ATE_edited";
9383 case DW_ATE_signed_fixed:
9384 return "DW_ATE_signed_fixed";
9385 case DW_ATE_unsigned_fixed:
9386 return "DW_ATE_unsigned_fixed";
9387 case DW_ATE_decimal_float:
9388 return "DW_ATE_decimal_float";
9389 /* HP extensions. */
9390 case DW_ATE_HP_float80:
9391 return "DW_ATE_HP_float80";
9392 case DW_ATE_HP_complex_float80:
9393 return "DW_ATE_HP_complex_float80";
9394 case DW_ATE_HP_float128:
9395 return "DW_ATE_HP_float128";
9396 case DW_ATE_HP_complex_float128:
9397 return "DW_ATE_HP_complex_float128";
9398 case DW_ATE_HP_floathpintel:
9399 return "DW_ATE_HP_floathpintel";
9400 case DW_ATE_HP_imaginary_float80:
9401 return "DW_ATE_HP_imaginary_float80";
9402 case DW_ATE_HP_imaginary_float128:
9403 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
9404 default:
9405 return "DW_ATE_<unknown>";
9406 }
9407}
9408
9409/* Convert a DWARF call frame info operation to its string name. */
9410
9411#if 0
9412static char *
aa1ee363 9413dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
9414{
9415 switch (cfi_opc)
9416 {
9417 case DW_CFA_advance_loc:
9418 return "DW_CFA_advance_loc";
9419 case DW_CFA_offset:
9420 return "DW_CFA_offset";
9421 case DW_CFA_restore:
9422 return "DW_CFA_restore";
9423 case DW_CFA_nop:
9424 return "DW_CFA_nop";
9425 case DW_CFA_set_loc:
9426 return "DW_CFA_set_loc";
9427 case DW_CFA_advance_loc1:
9428 return "DW_CFA_advance_loc1";
9429 case DW_CFA_advance_loc2:
9430 return "DW_CFA_advance_loc2";
9431 case DW_CFA_advance_loc4:
9432 return "DW_CFA_advance_loc4";
9433 case DW_CFA_offset_extended:
9434 return "DW_CFA_offset_extended";
9435 case DW_CFA_restore_extended:
9436 return "DW_CFA_restore_extended";
9437 case DW_CFA_undefined:
9438 return "DW_CFA_undefined";
9439 case DW_CFA_same_value:
9440 return "DW_CFA_same_value";
9441 case DW_CFA_register:
9442 return "DW_CFA_register";
9443 case DW_CFA_remember_state:
9444 return "DW_CFA_remember_state";
9445 case DW_CFA_restore_state:
9446 return "DW_CFA_restore_state";
9447 case DW_CFA_def_cfa:
9448 return "DW_CFA_def_cfa";
9449 case DW_CFA_def_cfa_register:
9450 return "DW_CFA_def_cfa_register";
9451 case DW_CFA_def_cfa_offset:
9452 return "DW_CFA_def_cfa_offset";
b7619582 9453 /* DWARF 3. */
985cb1a3
JM
9454 case DW_CFA_def_cfa_expression:
9455 return "DW_CFA_def_cfa_expression";
9456 case DW_CFA_expression:
9457 return "DW_CFA_expression";
9458 case DW_CFA_offset_extended_sf:
9459 return "DW_CFA_offset_extended_sf";
9460 case DW_CFA_def_cfa_sf:
9461 return "DW_CFA_def_cfa_sf";
9462 case DW_CFA_def_cfa_offset_sf:
9463 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
9464 case DW_CFA_val_offset:
9465 return "DW_CFA_val_offset";
9466 case DW_CFA_val_offset_sf:
9467 return "DW_CFA_val_offset_sf";
9468 case DW_CFA_val_expression:
9469 return "DW_CFA_val_expression";
9470 /* SGI/MIPS specific. */
c906108c
SS
9471 case DW_CFA_MIPS_advance_loc8:
9472 return "DW_CFA_MIPS_advance_loc8";
b7619582 9473 /* GNU extensions. */
985cb1a3
JM
9474 case DW_CFA_GNU_window_save:
9475 return "DW_CFA_GNU_window_save";
9476 case DW_CFA_GNU_args_size:
9477 return "DW_CFA_GNU_args_size";
9478 case DW_CFA_GNU_negative_offset_extended:
9479 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
9480 default:
9481 return "DW_CFA_<unknown>";
9482 }
9483}
9484#endif
9485
f9aca02d 9486static void
d97bc12b 9487dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
9488{
9489 unsigned int i;
9490
d97bc12b
DE
9491 print_spaces (indent, f);
9492 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 9493 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
9494
9495 if (die->parent != NULL)
9496 {
9497 print_spaces (indent, f);
9498 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9499 die->parent->offset);
9500 }
9501
9502 print_spaces (indent, f);
9503 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 9504 dwarf_bool_name (die->child != NULL));
c906108c 9505
d97bc12b
DE
9506 print_spaces (indent, f);
9507 fprintf_unfiltered (f, " attributes:\n");
9508
c906108c
SS
9509 for (i = 0; i < die->num_attrs; ++i)
9510 {
d97bc12b
DE
9511 print_spaces (indent, f);
9512 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
9513 dwarf_attr_name (die->attrs[i].name),
9514 dwarf_form_name (die->attrs[i].form));
d97bc12b 9515
c906108c
SS
9516 switch (die->attrs[i].form)
9517 {
9518 case DW_FORM_ref_addr:
9519 case DW_FORM_addr:
d97bc12b
DE
9520 fprintf_unfiltered (f, "address: ");
9521 fputs_filtered (paddress (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
9522 break;
9523 case DW_FORM_block2:
9524 case DW_FORM_block4:
9525 case DW_FORM_block:
9526 case DW_FORM_block1:
d97bc12b 9527 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 9528 break;
10b3939b
DJ
9529 case DW_FORM_ref1:
9530 case DW_FORM_ref2:
9531 case DW_FORM_ref4:
d97bc12b 9532 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
9533 (long) (DW_ADDR (&die->attrs[i])));
9534 break;
c906108c
SS
9535 case DW_FORM_data1:
9536 case DW_FORM_data2:
9537 case DW_FORM_data4:
ce5d95e1 9538 case DW_FORM_data8:
c906108c
SS
9539 case DW_FORM_udata:
9540 case DW_FORM_sdata:
d97bc12b 9541 fprintf_unfiltered (f, "constant: %ld", DW_UNSND (&die->attrs[i]));
c906108c
SS
9542 break;
9543 case DW_FORM_string:
4bdf3d34 9544 case DW_FORM_strp:
71c25dea 9545 case GDB_FORM_cached_string:
d97bc12b 9546 fprintf_unfiltered (f, "string: \"%s\"",
c906108c 9547 DW_STRING (&die->attrs[i])
c5aa993b 9548 ? DW_STRING (&die->attrs[i]) : "");
c906108c
SS
9549 break;
9550 case DW_FORM_flag:
9551 if (DW_UNSND (&die->attrs[i]))
d97bc12b 9552 fprintf_unfiltered (f, "flag: TRUE");
c906108c 9553 else
d97bc12b 9554 fprintf_unfiltered (f, "flag: FALSE");
c906108c 9555 break;
a8329558
KW
9556 case DW_FORM_indirect:
9557 /* the reader will have reduced the indirect form to
9558 the "base form" so this form should not occur */
d97bc12b 9559 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 9560 break;
c906108c 9561 default:
d97bc12b 9562 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 9563 die->attrs[i].form);
d97bc12b 9564 break;
c906108c 9565 }
d97bc12b 9566 fprintf_unfiltered (f, "\n");
c906108c
SS
9567 }
9568}
9569
f9aca02d 9570static void
d97bc12b 9571dump_die_for_error (struct die_info *die)
c906108c 9572{
d97bc12b
DE
9573 dump_die_shallow (gdb_stderr, 0, die);
9574}
9575
9576static void
9577dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
9578{
9579 int indent = level * 4;
9580
9581 gdb_assert (die != NULL);
9582
9583 if (level >= max_level)
9584 return;
9585
9586 dump_die_shallow (f, indent, die);
9587
9588 if (die->child != NULL)
c906108c 9589 {
d97bc12b
DE
9590 print_spaces (indent, f);
9591 fprintf_unfiltered (f, " Children:");
9592 if (level + 1 < max_level)
9593 {
9594 fprintf_unfiltered (f, "\n");
9595 dump_die_1 (f, level + 1, max_level, die->child);
9596 }
9597 else
9598 {
9599 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
9600 }
9601 }
9602
9603 if (die->sibling != NULL && level > 0)
9604 {
9605 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
9606 }
9607}
9608
d97bc12b
DE
9609/* This is called from the pdie macro in gdbinit.in.
9610 It's not static so gcc will keep a copy callable from gdb. */
9611
9612void
9613dump_die (struct die_info *die, int max_level)
9614{
9615 dump_die_1 (gdb_stdlog, 0, max_level, die);
9616}
9617
f9aca02d 9618static void
51545339 9619store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9620{
51545339 9621 void **slot;
c906108c 9622
51545339
DJ
9623 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
9624
9625 *slot = die;
c906108c
SS
9626}
9627
9628static unsigned int
c764a876 9629dwarf2_get_ref_die_offset (struct attribute *attr)
c906108c
SS
9630{
9631 unsigned int result = 0;
9632
9633 switch (attr->form)
9634 {
9635 case DW_FORM_ref_addr:
c906108c
SS
9636 case DW_FORM_ref1:
9637 case DW_FORM_ref2:
9638 case DW_FORM_ref4:
613e1657 9639 case DW_FORM_ref8:
c906108c 9640 case DW_FORM_ref_udata:
10b3939b 9641 result = DW_ADDR (attr);
c906108c
SS
9642 break;
9643 default:
4d3c2250 9644 complaint (&symfile_complaints,
e2e0b3e5 9645 _("unsupported die ref attribute form: '%s'"),
4d3c2250 9646 dwarf_form_name (attr->form));
c906108c
SS
9647 }
9648 return result;
9649}
9650
a02abb62
JB
9651/* Return the constant value held by the given attribute. Return -1
9652 if the value held by the attribute is not constant. */
9653
9654static int
9655dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
9656{
9657 if (attr->form == DW_FORM_sdata)
9658 return DW_SND (attr);
9659 else if (attr->form == DW_FORM_udata
9660 || attr->form == DW_FORM_data1
9661 || attr->form == DW_FORM_data2
9662 || attr->form == DW_FORM_data4
9663 || attr->form == DW_FORM_data8)
9664 return DW_UNSND (attr);
9665 else
9666 {
e2e0b3e5 9667 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
9668 dwarf_form_name (attr->form));
9669 return default_value;
9670 }
9671}
9672
03dd20cc
DJ
9673/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
9674 unit and add it to our queue. */
9675
9676static void
9677maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
9678 struct dwarf2_per_cu_data *per_cu)
9679{
9680 /* Mark the dependence relation so that we don't flush PER_CU
9681 too early. */
9682 dwarf2_add_dependence (this_cu, per_cu);
9683
9684 /* If it's already on the queue, we have nothing to do. */
9685 if (per_cu->queued)
9686 return;
9687
9688 /* If the compilation unit is already loaded, just mark it as
9689 used. */
9690 if (per_cu->cu != NULL)
9691 {
9692 per_cu->cu->last_used = 0;
9693 return;
9694 }
9695
9696 /* Add it to the queue. */
9697 queue_comp_unit (per_cu, this_cu->objfile);
9698}
9699
f504f079
DE
9700/* Follow reference attribute ATTR of SRC_DIE.
9701 On entry *REF_CU is the CU of SRC_DIE.
9702 On exit *REF_CU is the CU of the result. */
9703
f9aca02d 9704static struct die_info *
10b3939b 9705follow_die_ref (struct die_info *src_die, struct attribute *attr,
f2f0e013 9706 struct dwarf2_cu **ref_cu)
c906108c
SS
9707{
9708 struct die_info *die;
10b3939b 9709 unsigned int offset;
10b3939b 9710 struct die_info temp_die;
f2f0e013 9711 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 9712
c764a876 9713 offset = dwarf2_get_ref_die_offset (attr);
10b3939b 9714
45452591 9715 if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
9716 {
9717 struct dwarf2_per_cu_data *per_cu;
45452591 9718 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
9719
9720 /* If necessary, add it to the queue and load its DIEs. */
9721 maybe_queue_comp_unit (cu, per_cu);
9722
10b3939b
DJ
9723 target_cu = per_cu->cu;
9724 }
9725 else
9726 target_cu = cu;
c906108c 9727
f2f0e013 9728 *ref_cu = target_cu;
51545339
DJ
9729 temp_die.offset = offset;
9730 die = htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
9731 if (die)
9732 return die;
10b3939b 9733
8a3fe4f8
AC
9734 error (_("Dwarf Error: Cannot find DIE at 0x%lx referenced from DIE "
9735 "at 0x%lx [in module %s]"),
1d325ec1 9736 (long) offset, (long) src_die->offset, cu->objfile->name);
c906108c
SS
9737}
9738
c906108c
SS
9739/* Decode simple location descriptions.
9740 Given a pointer to a dwarf block that defines a location, compute
9741 the location and return the value.
9742
4cecd739
DJ
9743 NOTE drow/2003-11-18: This function is called in two situations
9744 now: for the address of static or global variables (partial symbols
9745 only) and for offsets into structures which are expected to be
9746 (more or less) constant. The partial symbol case should go away,
9747 and only the constant case should remain. That will let this
9748 function complain more accurately. A few special modes are allowed
9749 without complaint for global variables (for instance, global
9750 register values and thread-local values).
c906108c
SS
9751
9752 A location description containing no operations indicates that the
4cecd739 9753 object is optimized out. The return value is 0 for that case.
6b992462
DJ
9754 FIXME drow/2003-11-16: No callers check for this case any more; soon all
9755 callers will only want a very basic result and this can become a
9756 complaint.
c906108c 9757
c906108c
SS
9758 Note that stack[0] is unused except as a default error return.
9759 Note that stack overflow is not yet handled. */
9760
9761static CORE_ADDR
e7c27a73 9762decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 9763{
e7c27a73
DJ
9764 struct objfile *objfile = cu->objfile;
9765 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9766 int i;
9767 int size = blk->size;
fe1b8b76 9768 gdb_byte *data = blk->data;
c906108c
SS
9769 CORE_ADDR stack[64];
9770 int stacki;
9771 unsigned int bytes_read, unsnd;
fe1b8b76 9772 gdb_byte op;
c906108c
SS
9773
9774 i = 0;
9775 stacki = 0;
9776 stack[stacki] = 0;
c906108c
SS
9777
9778 while (i < size)
9779 {
c906108c
SS
9780 op = data[i++];
9781 switch (op)
9782 {
f1bea926
JM
9783 case DW_OP_lit0:
9784 case DW_OP_lit1:
9785 case DW_OP_lit2:
9786 case DW_OP_lit3:
9787 case DW_OP_lit4:
9788 case DW_OP_lit5:
9789 case DW_OP_lit6:
9790 case DW_OP_lit7:
9791 case DW_OP_lit8:
9792 case DW_OP_lit9:
9793 case DW_OP_lit10:
9794 case DW_OP_lit11:
9795 case DW_OP_lit12:
9796 case DW_OP_lit13:
9797 case DW_OP_lit14:
9798 case DW_OP_lit15:
9799 case DW_OP_lit16:
9800 case DW_OP_lit17:
9801 case DW_OP_lit18:
9802 case DW_OP_lit19:
9803 case DW_OP_lit20:
9804 case DW_OP_lit21:
9805 case DW_OP_lit22:
9806 case DW_OP_lit23:
9807 case DW_OP_lit24:
9808 case DW_OP_lit25:
9809 case DW_OP_lit26:
9810 case DW_OP_lit27:
9811 case DW_OP_lit28:
9812 case DW_OP_lit29:
9813 case DW_OP_lit30:
9814 case DW_OP_lit31:
9815 stack[++stacki] = op - DW_OP_lit0;
9816 break;
9817
c906108c
SS
9818 case DW_OP_reg0:
9819 case DW_OP_reg1:
9820 case DW_OP_reg2:
9821 case DW_OP_reg3:
9822 case DW_OP_reg4:
9823 case DW_OP_reg5:
9824 case DW_OP_reg6:
9825 case DW_OP_reg7:
9826 case DW_OP_reg8:
9827 case DW_OP_reg9:
9828 case DW_OP_reg10:
9829 case DW_OP_reg11:
9830 case DW_OP_reg12:
9831 case DW_OP_reg13:
9832 case DW_OP_reg14:
9833 case DW_OP_reg15:
9834 case DW_OP_reg16:
9835 case DW_OP_reg17:
9836 case DW_OP_reg18:
9837 case DW_OP_reg19:
9838 case DW_OP_reg20:
9839 case DW_OP_reg21:
9840 case DW_OP_reg22:
9841 case DW_OP_reg23:
9842 case DW_OP_reg24:
9843 case DW_OP_reg25:
9844 case DW_OP_reg26:
9845 case DW_OP_reg27:
9846 case DW_OP_reg28:
9847 case DW_OP_reg29:
9848 case DW_OP_reg30:
9849 case DW_OP_reg31:
c906108c 9850 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
9851 if (i < size)
9852 dwarf2_complex_location_expr_complaint ();
c906108c
SS
9853 break;
9854
9855 case DW_OP_regx:
c906108c
SS
9856 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
9857 i += bytes_read;
c906108c 9858 stack[++stacki] = unsnd;
4cecd739
DJ
9859 if (i < size)
9860 dwarf2_complex_location_expr_complaint ();
c906108c
SS
9861 break;
9862
9863 case DW_OP_addr:
107d2387 9864 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 9865 cu, &bytes_read);
107d2387 9866 i += bytes_read;
c906108c
SS
9867 break;
9868
9869 case DW_OP_const1u:
9870 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
9871 i += 1;
9872 break;
9873
9874 case DW_OP_const1s:
9875 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
9876 i += 1;
9877 break;
9878
9879 case DW_OP_const2u:
9880 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
9881 i += 2;
9882 break;
9883
9884 case DW_OP_const2s:
9885 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
9886 i += 2;
9887 break;
9888
9889 case DW_OP_const4u:
9890 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
9891 i += 4;
9892 break;
9893
9894 case DW_OP_const4s:
9895 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
9896 i += 4;
9897 break;
9898
9899 case DW_OP_constu:
9900 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 9901 &bytes_read);
c906108c
SS
9902 i += bytes_read;
9903 break;
9904
9905 case DW_OP_consts:
9906 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
9907 i += bytes_read;
9908 break;
9909
f1bea926
JM
9910 case DW_OP_dup:
9911 stack[stacki + 1] = stack[stacki];
9912 stacki++;
9913 break;
9914
c906108c
SS
9915 case DW_OP_plus:
9916 stack[stacki - 1] += stack[stacki];
9917 stacki--;
9918 break;
9919
9920 case DW_OP_plus_uconst:
9921 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
9922 i += bytes_read;
9923 break;
9924
9925 case DW_OP_minus:
f1bea926 9926 stack[stacki - 1] -= stack[stacki];
c906108c
SS
9927 stacki--;
9928 break;
9929
7a292a7a 9930 case DW_OP_deref:
7a292a7a 9931 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
9932 this using GDB's address_class enum. This is valid for partial
9933 global symbols, although the variable's address will be bogus
9934 in the psymtab. */
7a292a7a 9935 if (i < size)
4d3c2250 9936 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
9937 break;
9938
9d774e44 9939 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
9940 /* The top of the stack has the offset from the beginning
9941 of the thread control block at which the variable is located. */
9942 /* Nothing should follow this operator, so the top of stack would
9943 be returned. */
4cecd739
DJ
9944 /* This is valid for partial global symbols, but the variable's
9945 address will be bogus in the psymtab. */
9d774e44 9946 if (i < size)
4d3c2250 9947 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
9948 break;
9949
42be36b3
CT
9950 case DW_OP_GNU_uninit:
9951 break;
9952
c906108c 9953 default:
e2e0b3e5 9954 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
4d3c2250 9955 dwarf_stack_op_name (op));
c906108c
SS
9956 return (stack[stacki]);
9957 }
9958 }
9959 return (stack[stacki]);
9960}
9961
9962/* memory allocation interface */
9963
c906108c 9964static struct dwarf_block *
7b5a2f43 9965dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
9966{
9967 struct dwarf_block *blk;
9968
9969 blk = (struct dwarf_block *)
7b5a2f43 9970 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
9971 return (blk);
9972}
9973
9974static struct abbrev_info *
f3dd6933 9975dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
9976{
9977 struct abbrev_info *abbrev;
9978
f3dd6933
DJ
9979 abbrev = (struct abbrev_info *)
9980 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
9981 memset (abbrev, 0, sizeof (struct abbrev_info));
9982 return (abbrev);
9983}
9984
9985static struct die_info *
b60c80d6 9986dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
9987{
9988 struct die_info *die;
b60c80d6
DJ
9989 size_t size = sizeof (struct die_info);
9990
9991 if (num_attrs > 1)
9992 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 9993
b60c80d6 9994 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
9995 memset (die, 0, sizeof (struct die_info));
9996 return (die);
9997}
2e276125
JB
9998
9999\f
10000/* Macro support. */
10001
10002
10003/* Return the full name of file number I in *LH's file name table.
10004 Use COMP_DIR as the name of the current directory of the
10005 compilation. The result is allocated using xmalloc; the caller is
10006 responsible for freeing it. */
10007static char *
10008file_full_name (int file, struct line_header *lh, const char *comp_dir)
10009{
6a83a1e6
EZ
10010 /* Is the file number a valid index into the line header's file name
10011 table? Remember that file numbers start with one, not zero. */
10012 if (1 <= file && file <= lh->num_file_names)
10013 {
10014 struct file_entry *fe = &lh->file_names[file - 1];
2e276125 10015
6a83a1e6
EZ
10016 if (IS_ABSOLUTE_PATH (fe->name))
10017 return xstrdup (fe->name);
10018 else
10019 {
10020 const char *dir;
10021 int dir_len;
10022 char *full_name;
10023
10024 if (fe->dir_index)
10025 dir = lh->include_dirs[fe->dir_index - 1];
10026 else
10027 dir = comp_dir;
10028
10029 if (dir)
10030 {
10031 dir_len = strlen (dir);
10032 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
10033 strcpy (full_name, dir);
10034 full_name[dir_len] = '/';
10035 strcpy (full_name + dir_len + 1, fe->name);
10036 return full_name;
10037 }
10038 else
10039 return xstrdup (fe->name);
10040 }
10041 }
2e276125
JB
10042 else
10043 {
6a83a1e6
EZ
10044 /* The compiler produced a bogus file number. We can at least
10045 record the macro definitions made in the file, even if we
10046 won't be able to find the file by name. */
10047 char fake_name[80];
10048 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 10049
6a83a1e6
EZ
10050 complaint (&symfile_complaints,
10051 _("bad file number in macro information (%d)"),
10052 file);
2e276125 10053
6a83a1e6 10054 return xstrdup (fake_name);
2e276125
JB
10055 }
10056}
10057
10058
10059static struct macro_source_file *
10060macro_start_file (int file, int line,
10061 struct macro_source_file *current_file,
10062 const char *comp_dir,
10063 struct line_header *lh, struct objfile *objfile)
10064{
10065 /* The full name of this source file. */
10066 char *full_name = file_full_name (file, lh, comp_dir);
10067
10068 /* We don't create a macro table for this compilation unit
10069 at all until we actually get a filename. */
10070 if (! pending_macros)
4a146b47 10071 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 10072 objfile->macro_cache);
2e276125
JB
10073
10074 if (! current_file)
10075 /* If we have no current file, then this must be the start_file
10076 directive for the compilation unit's main source file. */
10077 current_file = macro_set_main (pending_macros, full_name);
10078 else
10079 current_file = macro_include (current_file, line, full_name);
10080
10081 xfree (full_name);
10082
10083 return current_file;
10084}
10085
10086
10087/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
10088 followed by a null byte. */
10089static char *
10090copy_string (const char *buf, int len)
10091{
10092 char *s = xmalloc (len + 1);
10093 memcpy (s, buf, len);
10094 s[len] = '\0';
10095
10096 return s;
10097}
10098
10099
10100static const char *
10101consume_improper_spaces (const char *p, const char *body)
10102{
10103 if (*p == ' ')
10104 {
4d3c2250 10105 complaint (&symfile_complaints,
e2e0b3e5 10106 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 10107 body);
2e276125
JB
10108
10109 while (*p == ' ')
10110 p++;
10111 }
10112
10113 return p;
10114}
10115
10116
10117static void
10118parse_macro_definition (struct macro_source_file *file, int line,
10119 const char *body)
10120{
10121 const char *p;
10122
10123 /* The body string takes one of two forms. For object-like macro
10124 definitions, it should be:
10125
10126 <macro name> " " <definition>
10127
10128 For function-like macro definitions, it should be:
10129
10130 <macro name> "() " <definition>
10131 or
10132 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
10133
10134 Spaces may appear only where explicitly indicated, and in the
10135 <definition>.
10136
10137 The Dwarf 2 spec says that an object-like macro's name is always
10138 followed by a space, but versions of GCC around March 2002 omit
10139 the space when the macro's definition is the empty string.
10140
10141 The Dwarf 2 spec says that there should be no spaces between the
10142 formal arguments in a function-like macro's formal argument list,
10143 but versions of GCC around March 2002 include spaces after the
10144 commas. */
10145
10146
10147 /* Find the extent of the macro name. The macro name is terminated
10148 by either a space or null character (for an object-like macro) or
10149 an opening paren (for a function-like macro). */
10150 for (p = body; *p; p++)
10151 if (*p == ' ' || *p == '(')
10152 break;
10153
10154 if (*p == ' ' || *p == '\0')
10155 {
10156 /* It's an object-like macro. */
10157 int name_len = p - body;
10158 char *name = copy_string (body, name_len);
10159 const char *replacement;
10160
10161 if (*p == ' ')
10162 replacement = body + name_len + 1;
10163 else
10164 {
4d3c2250 10165 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10166 replacement = body + name_len;
10167 }
10168
10169 macro_define_object (file, line, name, replacement);
10170
10171 xfree (name);
10172 }
10173 else if (*p == '(')
10174 {
10175 /* It's a function-like macro. */
10176 char *name = copy_string (body, p - body);
10177 int argc = 0;
10178 int argv_size = 1;
10179 char **argv = xmalloc (argv_size * sizeof (*argv));
10180
10181 p++;
10182
10183 p = consume_improper_spaces (p, body);
10184
10185 /* Parse the formal argument list. */
10186 while (*p && *p != ')')
10187 {
10188 /* Find the extent of the current argument name. */
10189 const char *arg_start = p;
10190
10191 while (*p && *p != ',' && *p != ')' && *p != ' ')
10192 p++;
10193
10194 if (! *p || p == arg_start)
4d3c2250 10195 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10196 else
10197 {
10198 /* Make sure argv has room for the new argument. */
10199 if (argc >= argv_size)
10200 {
10201 argv_size *= 2;
10202 argv = xrealloc (argv, argv_size * sizeof (*argv));
10203 }
10204
10205 argv[argc++] = copy_string (arg_start, p - arg_start);
10206 }
10207
10208 p = consume_improper_spaces (p, body);
10209
10210 /* Consume the comma, if present. */
10211 if (*p == ',')
10212 {
10213 p++;
10214
10215 p = consume_improper_spaces (p, body);
10216 }
10217 }
10218
10219 if (*p == ')')
10220 {
10221 p++;
10222
10223 if (*p == ' ')
10224 /* Perfectly formed definition, no complaints. */
10225 macro_define_function (file, line, name,
10226 argc, (const char **) argv,
10227 p + 1);
10228 else if (*p == '\0')
10229 {
10230 /* Complain, but do define it. */
4d3c2250 10231 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10232 macro_define_function (file, line, name,
10233 argc, (const char **) argv,
10234 p);
10235 }
10236 else
10237 /* Just complain. */
4d3c2250 10238 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10239 }
10240 else
10241 /* Just complain. */
4d3c2250 10242 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10243
10244 xfree (name);
10245 {
10246 int i;
10247
10248 for (i = 0; i < argc; i++)
10249 xfree (argv[i]);
10250 }
10251 xfree (argv);
10252 }
10253 else
4d3c2250 10254 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10255}
10256
10257
10258static void
10259dwarf_decode_macros (struct line_header *lh, unsigned int offset,
10260 char *comp_dir, bfd *abfd,
e7c27a73 10261 struct dwarf2_cu *cu)
2e276125 10262{
fe1b8b76 10263 gdb_byte *mac_ptr, *mac_end;
2e276125 10264 struct macro_source_file *current_file = 0;
757a13d0
JK
10265 enum dwarf_macinfo_record_type macinfo_type;
10266 int at_commandline;
2e276125 10267
dce234bc 10268 if (dwarf2_per_objfile->macinfo.buffer == NULL)
2e276125 10269 {
e2e0b3e5 10270 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
10271 return;
10272 }
10273
757a13d0
JK
10274 /* First pass: Find the name of the base filename.
10275 This filename is needed in order to process all macros whose definition
10276 (or undefinition) comes from the command line. These macros are defined
10277 before the first DW_MACINFO_start_file entry, and yet still need to be
10278 associated to the base file.
10279
10280 To determine the base file name, we scan the macro definitions until we
10281 reach the first DW_MACINFO_start_file entry. We then initialize
10282 CURRENT_FILE accordingly so that any macro definition found before the
10283 first DW_MACINFO_start_file can still be associated to the base file. */
10284
dce234bc
PP
10285 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
10286 mac_end = dwarf2_per_objfile->macinfo.buffer
10287 + dwarf2_per_objfile->macinfo.size;
2e276125 10288
757a13d0 10289 do
2e276125 10290 {
2e276125
JB
10291 /* Do we at least have room for a macinfo type byte? */
10292 if (mac_ptr >= mac_end)
10293 {
757a13d0
JK
10294 /* Complaint is printed during the second pass as GDB will probably
10295 stop the first pass earlier upon finding DW_MACINFO_start_file. */
10296 break;
2e276125
JB
10297 }
10298
10299 macinfo_type = read_1_byte (abfd, mac_ptr);
10300 mac_ptr++;
10301
10302 switch (macinfo_type)
10303 {
10304 /* A zero macinfo type indicates the end of the macro
10305 information. */
10306 case 0:
757a13d0
JK
10307 break;
10308
10309 case DW_MACINFO_define:
10310 case DW_MACINFO_undef:
10311 /* Only skip the data by MAC_PTR. */
10312 {
10313 unsigned int bytes_read;
10314
10315 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10316 mac_ptr += bytes_read;
10317 read_string (abfd, mac_ptr, &bytes_read);
10318 mac_ptr += bytes_read;
10319 }
10320 break;
10321
10322 case DW_MACINFO_start_file:
10323 {
10324 unsigned int bytes_read;
10325 int line, file;
10326
10327 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10328 mac_ptr += bytes_read;
10329 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10330 mac_ptr += bytes_read;
10331
10332 current_file = macro_start_file (file, line, current_file, comp_dir,
10333 lh, cu->objfile);
10334 }
10335 break;
10336
10337 case DW_MACINFO_end_file:
10338 /* No data to skip by MAC_PTR. */
10339 break;
10340
10341 case DW_MACINFO_vendor_ext:
10342 /* Only skip the data by MAC_PTR. */
10343 {
10344 unsigned int bytes_read;
10345
10346 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10347 mac_ptr += bytes_read;
10348 read_string (abfd, mac_ptr, &bytes_read);
10349 mac_ptr += bytes_read;
10350 }
10351 break;
10352
10353 default:
10354 break;
10355 }
10356 } while (macinfo_type != 0 && current_file == NULL);
10357
10358 /* Second pass: Process all entries.
10359
10360 Use the AT_COMMAND_LINE flag to determine whether we are still processing
10361 command-line macro definitions/undefinitions. This flag is unset when we
10362 reach the first DW_MACINFO_start_file entry. */
10363
dce234bc 10364 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
757a13d0
JK
10365
10366 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
10367 GDB is still reading the definitions from command line. First
10368 DW_MACINFO_start_file will need to be ignored as it was already executed
10369 to create CURRENT_FILE for the main source holding also the command line
10370 definitions. On first met DW_MACINFO_start_file this flag is reset to
10371 normally execute all the remaining DW_MACINFO_start_file macinfos. */
10372
10373 at_commandline = 1;
10374
10375 do
10376 {
10377 /* Do we at least have room for a macinfo type byte? */
10378 if (mac_ptr >= mac_end)
10379 {
10380 dwarf2_macros_too_long_complaint ();
10381 break;
10382 }
10383
10384 macinfo_type = read_1_byte (abfd, mac_ptr);
10385 mac_ptr++;
10386
10387 switch (macinfo_type)
10388 {
10389 /* A zero macinfo type indicates the end of the macro
10390 information. */
10391 case 0:
10392 break;
2e276125
JB
10393
10394 case DW_MACINFO_define:
10395 case DW_MACINFO_undef:
10396 {
891d2f0b 10397 unsigned int bytes_read;
2e276125
JB
10398 int line;
10399 char *body;
10400
10401 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10402 mac_ptr += bytes_read;
10403 body = read_string (abfd, mac_ptr, &bytes_read);
10404 mac_ptr += bytes_read;
10405
10406 if (! current_file)
757a13d0
JK
10407 {
10408 /* DWARF violation as no main source is present. */
10409 complaint (&symfile_complaints,
10410 _("debug info with no main source gives macro %s "
10411 "on line %d: %s"),
10412 macinfo_type ==
10413 DW_MACINFO_define ? _("definition") : macinfo_type ==
10414 DW_MACINFO_undef ? _("undefinition") :
10415 "something-or-other", line, body);
10416 break;
10417 }
10418 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 10419 complaint (&symfile_complaints,
757a13d0
JK
10420 _("debug info gives %s macro %s with %s line %d: %s"),
10421 at_commandline ? _("command-line") : _("in-file"),
4d3c2250 10422 macinfo_type ==
757a13d0
JK
10423 DW_MACINFO_define ? _("definition") : macinfo_type ==
10424 DW_MACINFO_undef ? _("undefinition") :
10425 "something-or-other",
10426 line == 0 ? _("zero") : _("non-zero"), line, body);
10427
10428 if (macinfo_type == DW_MACINFO_define)
10429 parse_macro_definition (current_file, line, body);
10430 else if (macinfo_type == DW_MACINFO_undef)
10431 macro_undef (current_file, line, body);
2e276125
JB
10432 }
10433 break;
10434
10435 case DW_MACINFO_start_file:
10436 {
891d2f0b 10437 unsigned int bytes_read;
2e276125
JB
10438 int line, file;
10439
10440 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10441 mac_ptr += bytes_read;
10442 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10443 mac_ptr += bytes_read;
10444
757a13d0
JK
10445 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
10446 complaint (&symfile_complaints,
10447 _("debug info gives source %d included "
10448 "from %s at %s line %d"),
10449 file, at_commandline ? _("command-line") : _("file"),
10450 line == 0 ? _("zero") : _("non-zero"), line);
10451
10452 if (at_commandline)
10453 {
10454 /* This DW_MACINFO_start_file was executed in the pass one. */
10455 at_commandline = 0;
10456 }
10457 else
10458 current_file = macro_start_file (file, line,
10459 current_file, comp_dir,
10460 lh, cu->objfile);
2e276125
JB
10461 }
10462 break;
10463
10464 case DW_MACINFO_end_file:
10465 if (! current_file)
4d3c2250 10466 complaint (&symfile_complaints,
e2e0b3e5 10467 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
10468 else
10469 {
10470 current_file = current_file->included_by;
10471 if (! current_file)
10472 {
10473 enum dwarf_macinfo_record_type next_type;
10474
10475 /* GCC circa March 2002 doesn't produce the zero
10476 type byte marking the end of the compilation
10477 unit. Complain if it's not there, but exit no
10478 matter what. */
10479
10480 /* Do we at least have room for a macinfo type byte? */
10481 if (mac_ptr >= mac_end)
10482 {
4d3c2250 10483 dwarf2_macros_too_long_complaint ();
2e276125
JB
10484 return;
10485 }
10486
10487 /* We don't increment mac_ptr here, so this is just
10488 a look-ahead. */
10489 next_type = read_1_byte (abfd, mac_ptr);
10490 if (next_type != 0)
4d3c2250 10491 complaint (&symfile_complaints,
e2e0b3e5 10492 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
10493
10494 return;
10495 }
10496 }
10497 break;
10498
10499 case DW_MACINFO_vendor_ext:
10500 {
891d2f0b 10501 unsigned int bytes_read;
2e276125
JB
10502 int constant;
10503 char *string;
10504
10505 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10506 mac_ptr += bytes_read;
10507 string = read_string (abfd, mac_ptr, &bytes_read);
10508 mac_ptr += bytes_read;
10509
10510 /* We don't recognize any vendor extensions. */
10511 }
10512 break;
10513 }
757a13d0 10514 } while (macinfo_type != 0);
2e276125 10515}
8e19ed76
PS
10516
10517/* Check if the attribute's form is a DW_FORM_block*
10518 if so return true else false. */
10519static int
10520attr_form_is_block (struct attribute *attr)
10521{
10522 return (attr == NULL ? 0 :
10523 attr->form == DW_FORM_block1
10524 || attr->form == DW_FORM_block2
10525 || attr->form == DW_FORM_block4
10526 || attr->form == DW_FORM_block);
10527}
4c2df51b 10528
c6a0999f
JB
10529/* Return non-zero if ATTR's value is a section offset --- classes
10530 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
10531 You may use DW_UNSND (attr) to retrieve such offsets.
10532
10533 Section 7.5.4, "Attribute Encodings", explains that no attribute
10534 may have a value that belongs to more than one of these classes; it
10535 would be ambiguous if we did, because we use the same forms for all
10536 of them. */
3690dd37
JB
10537static int
10538attr_form_is_section_offset (struct attribute *attr)
10539{
10540 return (attr->form == DW_FORM_data4
10541 || attr->form == DW_FORM_data8);
10542}
10543
10544
10545/* Return non-zero if ATTR's value falls in the 'constant' class, or
10546 zero otherwise. When this function returns true, you can apply
10547 dwarf2_get_attr_constant_value to it.
10548
10549 However, note that for some attributes you must check
10550 attr_form_is_section_offset before using this test. DW_FORM_data4
10551 and DW_FORM_data8 are members of both the constant class, and of
10552 the classes that contain offsets into other debug sections
10553 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
10554 that, if an attribute's can be either a constant or one of the
10555 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
10556 taken as section offsets, not constants. */
10557static int
10558attr_form_is_constant (struct attribute *attr)
10559{
10560 switch (attr->form)
10561 {
10562 case DW_FORM_sdata:
10563 case DW_FORM_udata:
10564 case DW_FORM_data1:
10565 case DW_FORM_data2:
10566 case DW_FORM_data4:
10567 case DW_FORM_data8:
10568 return 1;
10569 default:
10570 return 0;
10571 }
10572}
10573
4c2df51b
DJ
10574static void
10575dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 10576 struct dwarf2_cu *cu)
4c2df51b 10577{
3690dd37 10578 if (attr_form_is_section_offset (attr)
99bcc461
DJ
10579 /* ".debug_loc" may not exist at all, or the offset may be outside
10580 the section. If so, fall through to the complaint in the
10581 other branch. */
dce234bc 10582 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
4c2df51b 10583 {
0d53c4c4 10584 struct dwarf2_loclist_baton *baton;
4c2df51b 10585
4a146b47 10586 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 10587 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
10588 baton->per_cu = cu->per_cu;
10589 gdb_assert (baton->per_cu);
4c2df51b 10590
0d53c4c4
DJ
10591 /* We don't know how long the location list is, but make sure we
10592 don't run off the edge of the section. */
dce234bc
PP
10593 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
10594 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
d00adf39
DE
10595 baton->base_address = cu->base_address;
10596 if (cu->base_known == 0)
0d53c4c4 10597 complaint (&symfile_complaints,
e2e0b3e5 10598 _("Location list used without specifying the CU base address."));
4c2df51b 10599
768a979c 10600 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
10601 SYMBOL_LOCATION_BATON (sym) = baton;
10602 }
10603 else
10604 {
10605 struct dwarf2_locexpr_baton *baton;
10606
4a146b47 10607 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 10608 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
10609 baton->per_cu = cu->per_cu;
10610 gdb_assert (baton->per_cu);
0d53c4c4
DJ
10611
10612 if (attr_form_is_block (attr))
10613 {
10614 /* Note that we're just copying the block's data pointer
10615 here, not the actual data. We're still pointing into the
6502dd73
DJ
10616 info_buffer for SYM's objfile; right now we never release
10617 that buffer, but when we do clean up properly this may
10618 need to change. */
0d53c4c4
DJ
10619 baton->size = DW_BLOCK (attr)->size;
10620 baton->data = DW_BLOCK (attr)->data;
10621 }
10622 else
10623 {
10624 dwarf2_invalid_attrib_class_complaint ("location description",
10625 SYMBOL_NATURAL_NAME (sym));
10626 baton->size = 0;
10627 baton->data = NULL;
10628 }
10629
768a979c 10630 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
10631 SYMBOL_LOCATION_BATON (sym) = baton;
10632 }
4c2df51b 10633}
6502dd73 10634
ae0d2f24
UW
10635/* Return the OBJFILE associated with the compilation unit CU. */
10636
10637struct objfile *
10638dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
10639{
10640 struct objfile *objfile = per_cu->psymtab->objfile;
10641
10642 /* Return the master objfile, so that we can report and look up the
10643 correct file containing this variable. */
10644 if (objfile->separate_debug_objfile_backlink)
10645 objfile = objfile->separate_debug_objfile_backlink;
10646
10647 return objfile;
10648}
10649
10650/* Return the address size given in the compilation unit header for CU. */
10651
10652CORE_ADDR
10653dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
10654{
10655 if (per_cu->cu)
10656 return per_cu->cu->header.addr_size;
10657 else
10658 {
10659 /* If the CU is not currently read in, we re-read its header. */
10660 struct objfile *objfile = per_cu->psymtab->objfile;
10661 struct dwarf2_per_objfile *per_objfile
10662 = objfile_data (objfile, dwarf2_objfile_data_key);
dce234bc 10663 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
ae0d2f24
UW
10664
10665 struct comp_unit_head cu_header;
10666 memset (&cu_header, 0, sizeof cu_header);
10667 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
10668 return cu_header.addr_size;
10669 }
10670}
10671
ae038cb0 10672/* Locate the compilation unit from CU's objfile which contains the
10b3939b 10673 DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
10674
10675static struct dwarf2_per_cu_data *
c764a876 10676dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
10677 struct objfile *objfile)
10678{
10679 struct dwarf2_per_cu_data *this_cu;
10680 int low, high;
10681
ae038cb0
DJ
10682 low = 0;
10683 high = dwarf2_per_objfile->n_comp_units - 1;
10684 while (high > low)
10685 {
10686 int mid = low + (high - low) / 2;
10687 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
10688 high = mid;
10689 else
10690 low = mid + 1;
10691 }
10692 gdb_assert (low == high);
10693 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
10694 {
10b3939b 10695 if (low == 0)
8a3fe4f8
AC
10696 error (_("Dwarf Error: could not find partial DIE containing "
10697 "offset 0x%lx [in module %s]"),
10b3939b
DJ
10698 (long) offset, bfd_get_filename (objfile->obfd));
10699
ae038cb0
DJ
10700 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
10701 return dwarf2_per_objfile->all_comp_units[low-1];
10702 }
10703 else
10704 {
10705 this_cu = dwarf2_per_objfile->all_comp_units[low];
10706 if (low == dwarf2_per_objfile->n_comp_units - 1
10707 && offset >= this_cu->offset + this_cu->length)
c764a876 10708 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
10709 gdb_assert (offset < this_cu->offset + this_cu->length);
10710 return this_cu;
10711 }
10712}
10713
10b3939b
DJ
10714/* Locate the compilation unit from OBJFILE which is located at exactly
10715 OFFSET. Raises an error on failure. */
10716
ae038cb0 10717static struct dwarf2_per_cu_data *
c764a876 10718dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
10719{
10720 struct dwarf2_per_cu_data *this_cu;
10721 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
10722 if (this_cu->offset != offset)
c764a876 10723 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
10724 return this_cu;
10725}
10726
10727/* Release one cached compilation unit, CU. We unlink it from the tree
10728 of compilation units, but we don't remove it from the read_in_chain;
10729 the caller is responsible for that. */
10730
10731static void
10732free_one_comp_unit (void *data)
10733{
10734 struct dwarf2_cu *cu = data;
10735
10736 if (cu->per_cu != NULL)
10737 cu->per_cu->cu = NULL;
10738 cu->per_cu = NULL;
10739
10740 obstack_free (&cu->comp_unit_obstack, NULL);
10741
10742 xfree (cu);
10743}
10744
72bf9492 10745/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
10746 when we're finished with it. We can't free the pointer itself, but be
10747 sure to unlink it from the cache. Also release any associated storage
10748 and perform cache maintenance.
72bf9492
DJ
10749
10750 Only used during partial symbol parsing. */
10751
10752static void
10753free_stack_comp_unit (void *data)
10754{
10755 struct dwarf2_cu *cu = data;
10756
10757 obstack_free (&cu->comp_unit_obstack, NULL);
10758 cu->partial_dies = NULL;
ae038cb0
DJ
10759
10760 if (cu->per_cu != NULL)
10761 {
10762 /* This compilation unit is on the stack in our caller, so we
10763 should not xfree it. Just unlink it. */
10764 cu->per_cu->cu = NULL;
10765 cu->per_cu = NULL;
10766
10767 /* If we had a per-cu pointer, then we may have other compilation
10768 units loaded, so age them now. */
10769 age_cached_comp_units ();
10770 }
10771}
10772
10773/* Free all cached compilation units. */
10774
10775static void
10776free_cached_comp_units (void *data)
10777{
10778 struct dwarf2_per_cu_data *per_cu, **last_chain;
10779
10780 per_cu = dwarf2_per_objfile->read_in_chain;
10781 last_chain = &dwarf2_per_objfile->read_in_chain;
10782 while (per_cu != NULL)
10783 {
10784 struct dwarf2_per_cu_data *next_cu;
10785
10786 next_cu = per_cu->cu->read_in_chain;
10787
10788 free_one_comp_unit (per_cu->cu);
10789 *last_chain = next_cu;
10790
10791 per_cu = next_cu;
10792 }
10793}
10794
10795/* Increase the age counter on each cached compilation unit, and free
10796 any that are too old. */
10797
10798static void
10799age_cached_comp_units (void)
10800{
10801 struct dwarf2_per_cu_data *per_cu, **last_chain;
10802
10803 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
10804 per_cu = dwarf2_per_objfile->read_in_chain;
10805 while (per_cu != NULL)
10806 {
10807 per_cu->cu->last_used ++;
10808 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
10809 dwarf2_mark (per_cu->cu);
10810 per_cu = per_cu->cu->read_in_chain;
10811 }
10812
10813 per_cu = dwarf2_per_objfile->read_in_chain;
10814 last_chain = &dwarf2_per_objfile->read_in_chain;
10815 while (per_cu != NULL)
10816 {
10817 struct dwarf2_per_cu_data *next_cu;
10818
10819 next_cu = per_cu->cu->read_in_chain;
10820
10821 if (!per_cu->cu->mark)
10822 {
10823 free_one_comp_unit (per_cu->cu);
10824 *last_chain = next_cu;
10825 }
10826 else
10827 last_chain = &per_cu->cu->read_in_chain;
10828
10829 per_cu = next_cu;
10830 }
10831}
10832
10833/* Remove a single compilation unit from the cache. */
10834
10835static void
10836free_one_cached_comp_unit (void *target_cu)
10837{
10838 struct dwarf2_per_cu_data *per_cu, **last_chain;
10839
10840 per_cu = dwarf2_per_objfile->read_in_chain;
10841 last_chain = &dwarf2_per_objfile->read_in_chain;
10842 while (per_cu != NULL)
10843 {
10844 struct dwarf2_per_cu_data *next_cu;
10845
10846 next_cu = per_cu->cu->read_in_chain;
10847
10848 if (per_cu->cu == target_cu)
10849 {
10850 free_one_comp_unit (per_cu->cu);
10851 *last_chain = next_cu;
10852 break;
10853 }
10854 else
10855 last_chain = &per_cu->cu->read_in_chain;
10856
10857 per_cu = next_cu;
10858 }
10859}
10860
fe3e1990
DJ
10861/* Release all extra memory associated with OBJFILE. */
10862
10863void
10864dwarf2_free_objfile (struct objfile *objfile)
10865{
10866 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10867
10868 if (dwarf2_per_objfile == NULL)
10869 return;
10870
10871 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
10872 free_cached_comp_units (NULL);
10873
10874 /* Everything else should be on the objfile obstack. */
10875}
10876
1c379e20
DJ
10877/* A pair of DIE offset and GDB type pointer. We store these
10878 in a hash table separate from the DIEs, and preserve them
10879 when the DIEs are flushed out of cache. */
10880
10881struct dwarf2_offset_and_type
10882{
10883 unsigned int offset;
10884 struct type *type;
10885};
10886
10887/* Hash function for a dwarf2_offset_and_type. */
10888
10889static hashval_t
10890offset_and_type_hash (const void *item)
10891{
10892 const struct dwarf2_offset_and_type *ofs = item;
10893 return ofs->offset;
10894}
10895
10896/* Equality function for a dwarf2_offset_and_type. */
10897
10898static int
10899offset_and_type_eq (const void *item_lhs, const void *item_rhs)
10900{
10901 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
10902 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
10903 return ofs_lhs->offset == ofs_rhs->offset;
10904}
10905
10906/* Set the type associated with DIE to TYPE. Save it in CU's hash
f792889a 10907 table if necessary. For convenience, return TYPE. */
1c379e20 10908
f792889a 10909static struct type *
1c379e20
DJ
10910set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10911{
10912 struct dwarf2_offset_and_type **slot, ofs;
10913
f792889a
DJ
10914 if (cu->type_hash == NULL)
10915 {
10916 gdb_assert (cu->per_cu != NULL);
10917 cu->per_cu->type_hash
10918 = htab_create_alloc_ex (cu->header.length / 24,
10919 offset_and_type_hash,
10920 offset_and_type_eq,
10921 NULL,
10922 &cu->objfile->objfile_obstack,
10923 hashtab_obstack_allocate,
10924 dummy_obstack_deallocate);
10925 cu->type_hash = cu->per_cu->type_hash;
10926 }
1c379e20
DJ
10927
10928 ofs.offset = die->offset;
10929 ofs.type = type;
10930 slot = (struct dwarf2_offset_and_type **)
f792889a 10931 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
1c379e20
DJ
10932 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
10933 **slot = ofs;
f792889a 10934 return type;
1c379e20
DJ
10935}
10936
f792889a
DJ
10937/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
10938 not have a saved type. */
1c379e20
DJ
10939
10940static struct type *
f792889a 10941get_die_type (struct die_info *die, struct dwarf2_cu *cu)
1c379e20
DJ
10942{
10943 struct dwarf2_offset_and_type *slot, ofs;
f792889a
DJ
10944 htab_t type_hash = cu->type_hash;
10945
10946 if (type_hash == NULL)
10947 return NULL;
1c379e20
DJ
10948
10949 ofs.offset = die->offset;
10950 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
10951 if (slot)
10952 return slot->type;
10953 else
10954 return NULL;
10955}
10956
10b3939b
DJ
10957/* Add a dependence relationship from CU to REF_PER_CU. */
10958
10959static void
10960dwarf2_add_dependence (struct dwarf2_cu *cu,
10961 struct dwarf2_per_cu_data *ref_per_cu)
10962{
10963 void **slot;
10964
10965 if (cu->dependencies == NULL)
10966 cu->dependencies
10967 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
10968 NULL, &cu->comp_unit_obstack,
10969 hashtab_obstack_allocate,
10970 dummy_obstack_deallocate);
10971
10972 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
10973 if (*slot == NULL)
10974 *slot = ref_per_cu;
10975}
1c379e20 10976
f504f079
DE
10977/* Subroutine of dwarf2_mark to pass to htab_traverse.
10978 Set the mark field in every compilation unit in the
ae038cb0
DJ
10979 cache that we must keep because we are keeping CU. */
10980
10b3939b
DJ
10981static int
10982dwarf2_mark_helper (void **slot, void *data)
10983{
10984 struct dwarf2_per_cu_data *per_cu;
10985
10986 per_cu = (struct dwarf2_per_cu_data *) *slot;
10987 if (per_cu->cu->mark)
10988 return 1;
10989 per_cu->cu->mark = 1;
10990
10991 if (per_cu->cu->dependencies != NULL)
10992 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
10993
10994 return 1;
10995}
10996
f504f079
DE
10997/* Set the mark field in CU and in every other compilation unit in the
10998 cache that we must keep because we are keeping CU. */
10999
ae038cb0
DJ
11000static void
11001dwarf2_mark (struct dwarf2_cu *cu)
11002{
11003 if (cu->mark)
11004 return;
11005 cu->mark = 1;
10b3939b
DJ
11006 if (cu->dependencies != NULL)
11007 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
11008}
11009
11010static void
11011dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
11012{
11013 while (per_cu)
11014 {
11015 per_cu->cu->mark = 0;
11016 per_cu = per_cu->cu->read_in_chain;
11017 }
72bf9492
DJ
11018}
11019
72bf9492
DJ
11020/* Trivial hash function for partial_die_info: the hash value of a DIE
11021 is its offset in .debug_info for this objfile. */
11022
11023static hashval_t
11024partial_die_hash (const void *item)
11025{
11026 const struct partial_die_info *part_die = item;
11027 return part_die->offset;
11028}
11029
11030/* Trivial comparison function for partial_die_info structures: two DIEs
11031 are equal if they have the same offset. */
11032
11033static int
11034partial_die_eq (const void *item_lhs, const void *item_rhs)
11035{
11036 const struct partial_die_info *part_die_lhs = item_lhs;
11037 const struct partial_die_info *part_die_rhs = item_rhs;
11038 return part_die_lhs->offset == part_die_rhs->offset;
11039}
11040
ae038cb0
DJ
11041static struct cmd_list_element *set_dwarf2_cmdlist;
11042static struct cmd_list_element *show_dwarf2_cmdlist;
11043
11044static void
11045set_dwarf2_cmd (char *args, int from_tty)
11046{
11047 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
11048}
11049
11050static void
11051show_dwarf2_cmd (char *args, int from_tty)
11052{
11053 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
11054}
11055
dce234bc
PP
11056/* If section described by INFO was mmapped, munmap it now. */
11057
11058static void
11059munmap_section_buffer (struct dwarf2_section_info *info)
11060{
11061 if (info->was_mmapped)
11062 {
11063#ifdef HAVE_MMAP
11064 intptr_t begin = (intptr_t) info->buffer;
11065 intptr_t map_begin = begin & ~(pagesize - 1);
11066 size_t map_length = info->size + begin - map_begin;
11067 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
11068#else
11069 /* Without HAVE_MMAP, we should never be here to begin with. */
11070 gdb_assert (0);
11071#endif
11072 }
11073}
11074
11075/* munmap debug sections for OBJFILE, if necessary. */
11076
11077static void
11078dwarf2_per_objfile_cleanup (struct objfile *objfile, void *d)
11079{
11080 struct dwarf2_per_objfile *data = d;
11081 munmap_section_buffer (&data->info);
11082 munmap_section_buffer (&data->abbrev);
11083 munmap_section_buffer (&data->line);
11084 munmap_section_buffer (&data->str);
11085 munmap_section_buffer (&data->macinfo);
11086 munmap_section_buffer (&data->ranges);
11087 munmap_section_buffer (&data->loc);
11088 munmap_section_buffer (&data->frame);
11089 munmap_section_buffer (&data->eh_frame);
11090}
11091
6502dd73
DJ
11092void _initialize_dwarf2_read (void);
11093
11094void
11095_initialize_dwarf2_read (void)
11096{
dce234bc
PP
11097 dwarf2_objfile_data_key
11098 = register_objfile_data_with_cleanup (dwarf2_per_objfile_cleanup);
ae038cb0 11099
1bedd215
AC
11100 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
11101Set DWARF 2 specific variables.\n\
11102Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
11103 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
11104 0/*allow-unknown*/, &maintenance_set_cmdlist);
11105
1bedd215
AC
11106 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
11107Show DWARF 2 specific variables\n\
11108Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
11109 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
11110 0/*allow-unknown*/, &maintenance_show_cmdlist);
11111
11112 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
11113 &dwarf2_max_cache_age, _("\
11114Set the upper bound on the age of cached dwarf2 compilation units."), _("\
11115Show the upper bound on the age of cached dwarf2 compilation units."), _("\
11116A higher limit means that cached compilation units will be stored\n\
11117in memory longer, and more total memory will be used. Zero disables\n\
11118caching, which can slow down startup."),
2c5b56ce 11119 NULL,
920d2a44 11120 show_dwarf2_max_cache_age,
2c5b56ce 11121 &set_dwarf2_cmdlist,
ae038cb0 11122 &show_dwarf2_cmdlist);
d97bc12b
DE
11123
11124 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
11125Set debugging of the dwarf2 DIE reader."), _("\
11126Show debugging of the dwarf2 DIE reader."), _("\
11127When enabled (non-zero), DIEs are dumped after they are read in.\n\
11128The value is the maximum depth to print."),
11129 NULL,
11130 NULL,
11131 &setdebuglist, &showdebuglist);
6502dd73 11132}
This page took 1.447821 seconds and 4 git commands to generate.