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