gdb
[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
3092static void
e7c27a73 3093read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3094{
e7c27a73 3095 struct objfile *objfile = cu->objfile;
52f0bd74 3096 struct context_stack *new;
c906108c
SS
3097 CORE_ADDR lowpc;
3098 CORE_ADDR highpc;
3099 struct die_info *child_die;
3100 struct attribute *attr;
3101 char *name;
e142c38c 3102 CORE_ADDR baseaddr;
801e3a5b 3103 struct block *block;
c906108c 3104
e142c38c
DJ
3105 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3106
3107 name = dwarf2_linkage_name (die, cu);
c906108c
SS
3108
3109 /* Ignore functions with missing or empty names and functions with
3110 missing or invalid low and high pc attributes. */
e7c27a73 3111 if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
c906108c
SS
3112 return;
3113
3114 lowpc += baseaddr;
3115 highpc += baseaddr;
3116
5fb290d7 3117 /* Record the function range for dwarf_decode_lines. */
e142c38c 3118 add_to_cu_func_list (name, lowpc, highpc, cu);
5fb290d7 3119
c906108c 3120 new = push_context (0, lowpc);
f792889a 3121 new->name = new_symbol (die, read_type_die (die, cu), cu);
4c2df51b 3122
4cecd739
DJ
3123 /* If there is a location expression for DW_AT_frame_base, record
3124 it. */
e142c38c 3125 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 3126 if (attr)
c034e007
AC
3127 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
3128 expression is being recorded directly in the function's symbol
3129 and not in a separate frame-base object. I guess this hack is
3130 to avoid adding some sort of frame-base adjunct/annex to the
3131 function's symbol :-(. The problem with doing this is that it
3132 results in a function symbol with a location expression that
3133 has nothing to do with the location of the function, ouch! The
3134 relationship should be: a function's symbol has-a frame base; a
3135 frame-base has-a location expression. */
e7c27a73 3136 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 3137
e142c38c 3138 cu->list_in_scope = &local_symbols;
c906108c 3139
639d11d3 3140 if (die->child != NULL)
c906108c 3141 {
639d11d3 3142 child_die = die->child;
c906108c
SS
3143 while (child_die && child_die->tag)
3144 {
e7c27a73 3145 process_die (child_die, cu);
c906108c
SS
3146 child_die = sibling_die (child_die);
3147 }
3148 }
3149
3150 new = pop_context ();
3151 /* Make a block for the local symbols within. */
801e3a5b
JB
3152 block = finish_block (new->name, &local_symbols, new->old_blocks,
3153 lowpc, highpc, objfile);
3154
df8a16a1
DJ
3155 /* For C++, set the block's scope. */
3156 if (cu->language == language_cplus)
3157 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 3158 determine_prefix (die, cu),
df8a16a1
DJ
3159 processing_has_namespace_info);
3160
801e3a5b
JB
3161 /* If we have address ranges, record them. */
3162 dwarf2_record_block_ranges (die, block, baseaddr, cu);
208d8187
JB
3163
3164 /* In C++, we can have functions nested inside functions (e.g., when
3165 a function declares a class that has methods). This means that
3166 when we finish processing a function scope, we may need to go
3167 back to building a containing block's symbol lists. */
3168 local_symbols = new->locals;
3169 param_symbols = new->params;
3170
921e78cf
JB
3171 /* If we've finished processing a top-level function, subsequent
3172 symbols go in the file symbol list. */
3173 if (outermost_context_p ())
e142c38c 3174 cu->list_in_scope = &file_symbols;
c906108c
SS
3175}
3176
3177/* Process all the DIES contained within a lexical block scope. Start
3178 a new scope, process the dies, and then close the scope. */
3179
3180static void
e7c27a73 3181read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3182{
e7c27a73 3183 struct objfile *objfile = cu->objfile;
52f0bd74 3184 struct context_stack *new;
c906108c
SS
3185 CORE_ADDR lowpc, highpc;
3186 struct die_info *child_die;
e142c38c
DJ
3187 CORE_ADDR baseaddr;
3188
3189 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
3190
3191 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
3192 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
3193 as multiple lexical blocks? Handling children in a sane way would
3194 be nasty. Might be easier to properly extend generic blocks to
3195 describe ranges. */
e7c27a73 3196 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
c906108c
SS
3197 return;
3198 lowpc += baseaddr;
3199 highpc += baseaddr;
3200
3201 push_context (0, lowpc);
639d11d3 3202 if (die->child != NULL)
c906108c 3203 {
639d11d3 3204 child_die = die->child;
c906108c
SS
3205 while (child_die && child_die->tag)
3206 {
e7c27a73 3207 process_die (child_die, cu);
c906108c
SS
3208 child_die = sibling_die (child_die);
3209 }
3210 }
3211 new = pop_context ();
3212
3213 if (local_symbols != NULL)
3214 {
801e3a5b
JB
3215 struct block *block
3216 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
3217 highpc, objfile);
3218
3219 /* Note that recording ranges after traversing children, as we
3220 do here, means that recording a parent's ranges entails
3221 walking across all its children's ranges as they appear in
3222 the address map, which is quadratic behavior.
3223
3224 It would be nicer to record the parent's ranges before
3225 traversing its children, simply overriding whatever you find
3226 there. But since we don't even decide whether to create a
3227 block until after we've traversed its children, that's hard
3228 to do. */
3229 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
3230 }
3231 local_symbols = new->locals;
3232}
3233
43039443 3234/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
3235 Return 1 if the attributes are present and valid, otherwise, return 0.
3236 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
3237
3238static int
3239dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
3240 CORE_ADDR *high_return, struct dwarf2_cu *cu,
3241 struct partial_symtab *ranges_pst)
43039443
JK
3242{
3243 struct objfile *objfile = cu->objfile;
3244 struct comp_unit_head *cu_header = &cu->header;
3245 bfd *obfd = objfile->obfd;
3246 unsigned int addr_size = cu_header->addr_size;
3247 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
3248 /* Base address selection entry. */
3249 CORE_ADDR base;
3250 int found_base;
3251 unsigned int dummy;
3252 gdb_byte *buffer;
3253 CORE_ADDR marker;
3254 int low_set;
3255 CORE_ADDR low = 0;
3256 CORE_ADDR high = 0;
ff013f42 3257 CORE_ADDR baseaddr;
43039443 3258
d00adf39
DE
3259 found_base = cu->base_known;
3260 base = cu->base_address;
43039443
JK
3261
3262 if (offset >= dwarf2_per_objfile->ranges_size)
3263 {
3264 complaint (&symfile_complaints,
3265 _("Offset %d out of bounds for DW_AT_ranges attribute"),
3266 offset);
3267 return 0;
3268 }
3269 buffer = dwarf2_per_objfile->ranges_buffer + offset;
3270
3271 /* Read in the largest possible address. */
3272 marker = read_address (obfd, buffer, cu, &dummy);
3273 if ((marker & mask) == mask)
3274 {
3275 /* If we found the largest possible address, then
3276 read the base address. */
3277 base = read_address (obfd, buffer + addr_size, cu, &dummy);
3278 buffer += 2 * addr_size;
3279 offset += 2 * addr_size;
3280 found_base = 1;
3281 }
3282
3283 low_set = 0;
3284
e7030f15 3285 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 3286
43039443
JK
3287 while (1)
3288 {
3289 CORE_ADDR range_beginning, range_end;
3290
3291 range_beginning = read_address (obfd, buffer, cu, &dummy);
3292 buffer += addr_size;
3293 range_end = read_address (obfd, buffer, cu, &dummy);
3294 buffer += addr_size;
3295 offset += 2 * addr_size;
3296
3297 /* An end of list marker is a pair of zero addresses. */
3298 if (range_beginning == 0 && range_end == 0)
3299 /* Found the end of list entry. */
3300 break;
3301
3302 /* Each base address selection entry is a pair of 2 values.
3303 The first is the largest possible address, the second is
3304 the base address. Check for a base address here. */
3305 if ((range_beginning & mask) == mask)
3306 {
3307 /* If we found the largest possible address, then
3308 read the base address. */
3309 base = read_address (obfd, buffer + addr_size, cu, &dummy);
3310 found_base = 1;
3311 continue;
3312 }
3313
3314 if (!found_base)
3315 {
3316 /* We have no valid base address for the ranges
3317 data. */
3318 complaint (&symfile_complaints,
3319 _("Invalid .debug_ranges data (no base address)"));
3320 return 0;
3321 }
3322
3323 range_beginning += base;
3324 range_end += base;
3325
ff013f42
JK
3326 if (ranges_pst != NULL && range_beginning < range_end)
3327 addrmap_set_empty (objfile->psymtabs_addrmap,
3328 range_beginning + baseaddr, range_end - 1 + baseaddr,
3329 ranges_pst);
3330
43039443
JK
3331 /* FIXME: This is recording everything as a low-high
3332 segment of consecutive addresses. We should have a
3333 data structure for discontiguous block ranges
3334 instead. */
3335 if (! low_set)
3336 {
3337 low = range_beginning;
3338 high = range_end;
3339 low_set = 1;
3340 }
3341 else
3342 {
3343 if (range_beginning < low)
3344 low = range_beginning;
3345 if (range_end > high)
3346 high = range_end;
3347 }
3348 }
3349
3350 if (! low_set)
3351 /* If the first entry is an end-of-list marker, the range
3352 describes an empty scope, i.e. no instructions. */
3353 return 0;
3354
3355 if (low_return)
3356 *low_return = low;
3357 if (high_return)
3358 *high_return = high;
3359 return 1;
3360}
3361
af34e669
DJ
3362/* Get low and high pc attributes from a die. Return 1 if the attributes
3363 are present and valid, otherwise, return 0. Return -1 if the range is
3364 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 3365static int
af34e669 3366dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
e7c27a73 3367 CORE_ADDR *highpc, struct dwarf2_cu *cu)
c906108c
SS
3368{
3369 struct attribute *attr;
af34e669
DJ
3370 CORE_ADDR low = 0;
3371 CORE_ADDR high = 0;
3372 int ret = 0;
c906108c 3373
e142c38c 3374 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
c906108c 3375 if (attr)
af34e669
DJ
3376 {
3377 high = DW_ADDR (attr);
e142c38c 3378 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669
DJ
3379 if (attr)
3380 low = DW_ADDR (attr);
3381 else
3382 /* Found high w/o low attribute. */
3383 return 0;
3384
3385 /* Found consecutive range of addresses. */
3386 ret = 1;
3387 }
c906108c 3388 else
af34e669 3389 {
e142c38c 3390 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
3391 if (attr != NULL)
3392 {
af34e669 3393 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 3394 .debug_ranges section. */
ff013f42 3395 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, NULL))
af34e669 3396 return 0;
43039443 3397 /* Found discontinuous range of addresses. */
af34e669
DJ
3398 ret = -1;
3399 }
3400 }
c906108c
SS
3401
3402 if (high < low)
3403 return 0;
3404
3405 /* When using the GNU linker, .gnu.linkonce. sections are used to
3406 eliminate duplicate copies of functions and vtables and such.
3407 The linker will arbitrarily choose one and discard the others.
3408 The AT_*_pc values for such functions refer to local labels in
3409 these sections. If the section from that file was discarded, the
3410 labels are not in the output, so the relocs get a value of 0.
3411 If this is a discarded function, mark the pc bounds as invalid,
3412 so that GDB will ignore it. */
72dca2f5 3413 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
3414 return 0;
3415
3416 *lowpc = low;
3417 *highpc = high;
af34e669 3418 return ret;
c906108c
SS
3419}
3420
b084d499
JB
3421/* Assuming that DIE represents a subprogram DIE or a lexical block, get
3422 its low and high PC addresses. Do nothing if these addresses could not
3423 be determined. Otherwise, set LOWPC to the low address if it is smaller,
3424 and HIGHPC to the high address if greater than HIGHPC. */
3425
3426static void
3427dwarf2_get_subprogram_pc_bounds (struct die_info *die,
3428 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3429 struct dwarf2_cu *cu)
3430{
3431 CORE_ADDR low, high;
3432 struct die_info *child = die->child;
3433
3434 if (dwarf2_get_pc_bounds (die, &low, &high, cu))
3435 {
3436 *lowpc = min (*lowpc, low);
3437 *highpc = max (*highpc, high);
3438 }
3439
3440 /* If the language does not allow nested subprograms (either inside
3441 subprograms or lexical blocks), we're done. */
3442 if (cu->language != language_ada)
3443 return;
3444
3445 /* Check all the children of the given DIE. If it contains nested
3446 subprograms, then check their pc bounds. Likewise, we need to
3447 check lexical blocks as well, as they may also contain subprogram
3448 definitions. */
3449 while (child && child->tag)
3450 {
3451 if (child->tag == DW_TAG_subprogram
3452 || child->tag == DW_TAG_lexical_block)
3453 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
3454 child = sibling_die (child);
3455 }
3456}
3457
fae299cd
DC
3458/* Get the low and high pc's represented by the scope DIE, and store
3459 them in *LOWPC and *HIGHPC. If the correct values can't be
3460 determined, set *LOWPC to -1 and *HIGHPC to 0. */
3461
3462static void
3463get_scope_pc_bounds (struct die_info *die,
3464 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3465 struct dwarf2_cu *cu)
3466{
3467 CORE_ADDR best_low = (CORE_ADDR) -1;
3468 CORE_ADDR best_high = (CORE_ADDR) 0;
3469 CORE_ADDR current_low, current_high;
3470
3471 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu))
3472 {
3473 best_low = current_low;
3474 best_high = current_high;
3475 }
3476 else
3477 {
3478 struct die_info *child = die->child;
3479
3480 while (child && child->tag)
3481 {
3482 switch (child->tag) {
3483 case DW_TAG_subprogram:
b084d499 3484 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
3485 break;
3486 case DW_TAG_namespace:
3487 /* FIXME: carlton/2004-01-16: Should we do this for
3488 DW_TAG_class_type/DW_TAG_structure_type, too? I think
3489 that current GCC's always emit the DIEs corresponding
3490 to definitions of methods of classes as children of a
3491 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
3492 the DIEs giving the declarations, which could be
3493 anywhere). But I don't see any reason why the
3494 standards says that they have to be there. */
3495 get_scope_pc_bounds (child, &current_low, &current_high, cu);
3496
3497 if (current_low != ((CORE_ADDR) -1))
3498 {
3499 best_low = min (best_low, current_low);
3500 best_high = max (best_high, current_high);
3501 }
3502 break;
3503 default:
3504 /* Ignore. */
3505 break;
3506 }
3507
3508 child = sibling_die (child);
3509 }
3510 }
3511
3512 *lowpc = best_low;
3513 *highpc = best_high;
3514}
3515
801e3a5b
JB
3516/* Record the address ranges for BLOCK, offset by BASEADDR, as given
3517 in DIE. */
3518static void
3519dwarf2_record_block_ranges (struct die_info *die, struct block *block,
3520 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
3521{
3522 struct attribute *attr;
3523
3524 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
3525 if (attr)
3526 {
3527 CORE_ADDR high = DW_ADDR (attr);
3528 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3529 if (attr)
3530 {
3531 CORE_ADDR low = DW_ADDR (attr);
3532 record_block_range (block, baseaddr + low, baseaddr + high - 1);
3533 }
3534 }
3535
3536 attr = dwarf2_attr (die, DW_AT_ranges, cu);
3537 if (attr)
3538 {
3539 bfd *obfd = cu->objfile->obfd;
3540
3541 /* The value of the DW_AT_ranges attribute is the offset of the
3542 address range list in the .debug_ranges section. */
3543 unsigned long offset = DW_UNSND (attr);
3544 gdb_byte *buffer = dwarf2_per_objfile->ranges_buffer + offset;
3545
3546 /* For some target architectures, but not others, the
3547 read_address function sign-extends the addresses it returns.
3548 To recognize base address selection entries, we need a
3549 mask. */
3550 unsigned int addr_size = cu->header.addr_size;
3551 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
3552
3553 /* The base address, to which the next pair is relative. Note
3554 that this 'base' is a DWARF concept: most entries in a range
3555 list are relative, to reduce the number of relocs against the
3556 debugging information. This is separate from this function's
3557 'baseaddr' argument, which GDB uses to relocate debugging
3558 information from a shared library based on the address at
3559 which the library was loaded. */
d00adf39
DE
3560 CORE_ADDR base = cu->base_address;
3561 int base_known = cu->base_known;
801e3a5b
JB
3562
3563 if (offset >= dwarf2_per_objfile->ranges_size)
3564 {
3565 complaint (&symfile_complaints,
3566 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
3567 offset);
3568 return;
3569 }
3570
3571 for (;;)
3572 {
3573 unsigned int bytes_read;
3574 CORE_ADDR start, end;
3575
3576 start = read_address (obfd, buffer, cu, &bytes_read);
3577 buffer += bytes_read;
3578 end = read_address (obfd, buffer, cu, &bytes_read);
3579 buffer += bytes_read;
3580
3581 /* Did we find the end of the range list? */
3582 if (start == 0 && end == 0)
3583 break;
3584
3585 /* Did we find a base address selection entry? */
3586 else if ((start & base_select_mask) == base_select_mask)
3587 {
3588 base = end;
3589 base_known = 1;
3590 }
3591
3592 /* We found an ordinary address range. */
3593 else
3594 {
3595 if (!base_known)
3596 {
3597 complaint (&symfile_complaints,
3598 _("Invalid .debug_ranges data (no base address)"));
3599 return;
3600 }
3601
3602 record_block_range (block,
3603 baseaddr + base + start,
3604 baseaddr + base + end - 1);
3605 }
3606 }
3607 }
3608}
3609
c906108c
SS
3610/* Add an aggregate field to the field list. */
3611
3612static void
107d2387 3613dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73
DJ
3614 struct dwarf2_cu *cu)
3615{
3616 struct objfile *objfile = cu->objfile;
5e2b427d 3617 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
3618 struct nextfield *new_field;
3619 struct attribute *attr;
3620 struct field *fp;
3621 char *fieldname = "";
3622
3623 /* Allocate a new field list entry and link it in. */
3624 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 3625 make_cleanup (xfree, new_field);
c906108c
SS
3626 memset (new_field, 0, sizeof (struct nextfield));
3627 new_field->next = fip->fields;
3628 fip->fields = new_field;
3629 fip->nfields++;
3630
3631 /* Handle accessibility and virtuality of field.
3632 The default accessibility for members is public, the default
3633 accessibility for inheritance is private. */
3634 if (die->tag != DW_TAG_inheritance)
3635 new_field->accessibility = DW_ACCESS_public;
3636 else
3637 new_field->accessibility = DW_ACCESS_private;
3638 new_field->virtuality = DW_VIRTUALITY_none;
3639
e142c38c 3640 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
3641 if (attr)
3642 new_field->accessibility = DW_UNSND (attr);
3643 if (new_field->accessibility != DW_ACCESS_public)
3644 fip->non_public_fields = 1;
e142c38c 3645 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
3646 if (attr)
3647 new_field->virtuality = DW_UNSND (attr);
3648
3649 fp = &new_field->field;
a9a9bd0f 3650
e142c38c 3651 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 3652 {
a9a9bd0f
DC
3653 /* Data member other than a C++ static data member. */
3654
c906108c 3655 /* Get type of field. */
e7c27a73 3656 fp->type = die_type (die, cu);
c906108c 3657
d6a843b5 3658 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 3659
c906108c 3660 /* Get bit size of field (zero if none). */
e142c38c 3661 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
3662 if (attr)
3663 {
3664 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
3665 }
3666 else
3667 {
3668 FIELD_BITSIZE (*fp) = 0;
3669 }
3670
3671 /* Get bit offset of field. */
e142c38c 3672 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c
SS
3673 if (attr)
3674 {
c6a0999f
JB
3675 int byte_offset;
3676
3690dd37
JB
3677 if (attr_form_is_section_offset (attr))
3678 {
3679 dwarf2_complex_location_expr_complaint ();
c6a0999f 3680 byte_offset = 0;
3690dd37
JB
3681 }
3682 else if (attr_form_is_constant (attr))
c6a0999f 3683 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
3690dd37 3684 else
c6a0999f
JB
3685 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
3686
d6a843b5 3687 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
c906108c 3688 }
e142c38c 3689 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
3690 if (attr)
3691 {
5e2b427d 3692 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
3693 {
3694 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
3695 additional bit offset from the MSB of the containing
3696 anonymous object to the MSB of the field. We don't
3697 have to do anything special since we don't need to
3698 know the size of the anonymous object. */
c906108c
SS
3699 FIELD_BITPOS (*fp) += DW_UNSND (attr);
3700 }
3701 else
3702 {
3703 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
3704 MSB of the anonymous object, subtract off the number of
3705 bits from the MSB of the field to the MSB of the
3706 object, and then subtract off the number of bits of
3707 the field itself. The result is the bit offset of
3708 the LSB of the field. */
c906108c
SS
3709 int anonymous_size;
3710 int bit_offset = DW_UNSND (attr);
3711
e142c38c 3712 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
3713 if (attr)
3714 {
3715 /* The size of the anonymous object containing
3716 the bit field is explicit, so use the
3717 indicated size (in bytes). */
3718 anonymous_size = DW_UNSND (attr);
3719 }
3720 else
3721 {
3722 /* The size of the anonymous object containing
3723 the bit field must be inferred from the type
3724 attribute of the data member containing the
3725 bit field. */
3726 anonymous_size = TYPE_LENGTH (fp->type);
3727 }
3728 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
3729 - bit_offset - FIELD_BITSIZE (*fp);
3730 }
3731 }
3732
3733 /* Get name of field. */
39cbfefa
DJ
3734 fieldname = dwarf2_name (die, cu);
3735 if (fieldname == NULL)
3736 fieldname = "";
d8151005
DJ
3737
3738 /* The name is already allocated along with this objfile, so we don't
3739 need to duplicate it for the type. */
3740 fp->name = fieldname;
c906108c
SS
3741
3742 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 3743 pointer or virtual base class pointer) to private. */
e142c38c 3744 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c
SS
3745 {
3746 new_field->accessibility = DW_ACCESS_private;
3747 fip->non_public_fields = 1;
3748 }
3749 }
a9a9bd0f 3750 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 3751 {
a9a9bd0f
DC
3752 /* C++ static member. */
3753
3754 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
3755 is a declaration, but all versions of G++ as of this writing
3756 (so through at least 3.2.1) incorrectly generate
3757 DW_TAG_variable tags. */
3758
c906108c 3759 char *physname;
c906108c 3760
a9a9bd0f 3761 /* Get name of field. */
39cbfefa
DJ
3762 fieldname = dwarf2_name (die, cu);
3763 if (fieldname == NULL)
c906108c
SS
3764 return;
3765
2df3850c 3766 /* Get physical name. */
e142c38c 3767 physname = dwarf2_linkage_name (die, cu);
c906108c 3768
d8151005
DJ
3769 /* The name is already allocated along with this objfile, so we don't
3770 need to duplicate it for the type. */
3771 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 3772 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 3773 FIELD_NAME (*fp) = fieldname;
c906108c
SS
3774 }
3775 else if (die->tag == DW_TAG_inheritance)
3776 {
3777 /* C++ base class field. */
e142c38c 3778 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
c906108c 3779 if (attr)
d6a843b5
JK
3780 SET_FIELD_BITPOS (*fp, decode_locdesc (DW_BLOCK (attr), cu)
3781 * bits_per_byte);
c906108c 3782 FIELD_BITSIZE (*fp) = 0;
e7c27a73 3783 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
3784 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
3785 fip->nbaseclasses++;
3786 }
3787}
3788
3789/* Create the vector of fields, and attach it to the type. */
3790
3791static void
fba45db2 3792dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 3793 struct dwarf2_cu *cu)
c906108c
SS
3794{
3795 int nfields = fip->nfields;
3796
3797 /* Record the field count, allocate space for the array of fields,
3798 and create blank accessibility bitfields if necessary. */
3799 TYPE_NFIELDS (type) = nfields;
3800 TYPE_FIELDS (type) = (struct field *)
3801 TYPE_ALLOC (type, sizeof (struct field) * nfields);
3802 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
3803
3804 if (fip->non_public_fields)
3805 {
3806 ALLOCATE_CPLUS_STRUCT_TYPE (type);
3807
3808 TYPE_FIELD_PRIVATE_BITS (type) =
3809 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3810 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
3811
3812 TYPE_FIELD_PROTECTED_BITS (type) =
3813 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3814 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
3815
3816 TYPE_FIELD_IGNORE_BITS (type) =
3817 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3818 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
3819 }
3820
3821 /* If the type has baseclasses, allocate and clear a bit vector for
3822 TYPE_FIELD_VIRTUAL_BITS. */
3823 if (fip->nbaseclasses)
3824 {
3825 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 3826 unsigned char *pointer;
c906108c
SS
3827
3828 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
3829 pointer = TYPE_ALLOC (type, num_bytes);
3830 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
3831 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
3832 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
3833 }
3834
3835 /* Copy the saved-up fields into the field vector. Start from the head
3836 of the list, adding to the tail of the field array, so that they end
3837 up in the same order in the array in which they were added to the list. */
3838 while (nfields-- > 0)
3839 {
3840 TYPE_FIELD (type, nfields) = fip->fields->field;
3841 switch (fip->fields->accessibility)
3842 {
c5aa993b
JM
3843 case DW_ACCESS_private:
3844 SET_TYPE_FIELD_PRIVATE (type, nfields);
3845 break;
c906108c 3846
c5aa993b
JM
3847 case DW_ACCESS_protected:
3848 SET_TYPE_FIELD_PROTECTED (type, nfields);
3849 break;
c906108c 3850
c5aa993b
JM
3851 case DW_ACCESS_public:
3852 break;
c906108c 3853
c5aa993b
JM
3854 default:
3855 /* Unknown accessibility. Complain and treat it as public. */
3856 {
e2e0b3e5 3857 complaint (&symfile_complaints, _("unsupported accessibility %d"),
4d3c2250 3858 fip->fields->accessibility);
c5aa993b
JM
3859 }
3860 break;
c906108c
SS
3861 }
3862 if (nfields < fip->nbaseclasses)
3863 {
3864 switch (fip->fields->virtuality)
3865 {
c5aa993b
JM
3866 case DW_VIRTUALITY_virtual:
3867 case DW_VIRTUALITY_pure_virtual:
3868 SET_TYPE_FIELD_VIRTUAL (type, nfields);
3869 break;
c906108c
SS
3870 }
3871 }
3872 fip->fields = fip->fields->next;
3873 }
3874}
3875
c906108c
SS
3876/* Add a member function to the proper fieldlist. */
3877
3878static void
107d2387 3879dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 3880 struct type *type, struct dwarf2_cu *cu)
c906108c 3881{
e7c27a73 3882 struct objfile *objfile = cu->objfile;
c906108c
SS
3883 struct attribute *attr;
3884 struct fnfieldlist *flp;
3885 int i;
3886 struct fn_field *fnp;
3887 char *fieldname;
3888 char *physname;
3889 struct nextfnfield *new_fnfield;
f792889a 3890 struct type *this_type;
c906108c 3891
2df3850c 3892 /* Get name of member function. */
39cbfefa
DJ
3893 fieldname = dwarf2_name (die, cu);
3894 if (fieldname == NULL)
2df3850c 3895 return;
c906108c 3896
2df3850c 3897 /* Get the mangled name. */
e142c38c 3898 physname = dwarf2_linkage_name (die, cu);
c906108c
SS
3899
3900 /* Look up member function name in fieldlist. */
3901 for (i = 0; i < fip->nfnfields; i++)
3902 {
27bfe10e 3903 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
3904 break;
3905 }
3906
3907 /* Create new list element if necessary. */
3908 if (i < fip->nfnfields)
3909 flp = &fip->fnfieldlists[i];
3910 else
3911 {
3912 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
3913 {
3914 fip->fnfieldlists = (struct fnfieldlist *)
3915 xrealloc (fip->fnfieldlists,
3916 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 3917 * sizeof (struct fnfieldlist));
c906108c 3918 if (fip->nfnfields == 0)
c13c43fd 3919 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
3920 }
3921 flp = &fip->fnfieldlists[fip->nfnfields];
3922 flp->name = fieldname;
3923 flp->length = 0;
3924 flp->head = NULL;
3925 fip->nfnfields++;
3926 }
3927
3928 /* Create a new member function field and chain it to the field list
3929 entry. */
3930 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 3931 make_cleanup (xfree, new_fnfield);
c906108c
SS
3932 memset (new_fnfield, 0, sizeof (struct nextfnfield));
3933 new_fnfield->next = flp->head;
3934 flp->head = new_fnfield;
3935 flp->length++;
3936
3937 /* Fill in the member function field info. */
3938 fnp = &new_fnfield->fnfield;
d8151005
DJ
3939 /* The name is already allocated along with this objfile, so we don't
3940 need to duplicate it for the type. */
3941 fnp->physname = physname ? physname : "";
c906108c 3942 fnp->type = alloc_type (objfile);
f792889a
DJ
3943 this_type = read_type_die (die, cu);
3944 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 3945 {
f792889a 3946 int nparams = TYPE_NFIELDS (this_type);
c906108c 3947
f792889a 3948 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
3949 of the method itself (TYPE_CODE_METHOD). */
3950 smash_to_method_type (fnp->type, type,
f792889a
DJ
3951 TYPE_TARGET_TYPE (this_type),
3952 TYPE_FIELDS (this_type),
3953 TYPE_NFIELDS (this_type),
3954 TYPE_VARARGS (this_type));
c906108c
SS
3955
3956 /* Handle static member functions.
c5aa993b
JM
3957 Dwarf2 has no clean way to discern C++ static and non-static
3958 member functions. G++ helps GDB by marking the first
3959 parameter for non-static member functions (which is the
3960 this pointer) as artificial. We obtain this information
3961 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 3962 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
3963 fnp->voffset = VOFFSET_STATIC;
3964 }
3965 else
e2e0b3e5 3966 complaint (&symfile_complaints, _("member function type missing for '%s'"),
4d3c2250 3967 physname);
c906108c
SS
3968
3969 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 3970 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 3971 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
3972
3973 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
3974 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
3975
3976 /* Get accessibility. */
e142c38c 3977 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
3978 if (attr)
3979 {
3980 switch (DW_UNSND (attr))
3981 {
c5aa993b
JM
3982 case DW_ACCESS_private:
3983 fnp->is_private = 1;
3984 break;
3985 case DW_ACCESS_protected:
3986 fnp->is_protected = 1;
3987 break;
c906108c
SS
3988 }
3989 }
3990
b02dede2 3991 /* Check for artificial methods. */
e142c38c 3992 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
3993 if (attr && DW_UNSND (attr) != 0)
3994 fnp->is_artificial = 1;
3995
c906108c 3996 /* Get index in virtual function table if it is a virtual member function. */
e142c38c 3997 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
c906108c 3998 if (attr)
8e19ed76
PS
3999 {
4000 /* Support the .debug_loc offsets */
4001 if (attr_form_is_block (attr))
4002 {
e7c27a73 4003 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8e19ed76 4004 }
3690dd37 4005 else if (attr_form_is_section_offset (attr))
8e19ed76 4006 {
4d3c2250 4007 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
4008 }
4009 else
4010 {
4d3c2250
KB
4011 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
4012 fieldname);
8e19ed76
PS
4013 }
4014 }
c906108c
SS
4015}
4016
4017/* Create the vector of member function fields, and attach it to the type. */
4018
4019static void
fba45db2 4020dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 4021 struct dwarf2_cu *cu)
c906108c
SS
4022{
4023 struct fnfieldlist *flp;
4024 int total_length = 0;
4025 int i;
4026
4027 ALLOCATE_CPLUS_STRUCT_TYPE (type);
4028 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
4029 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
4030
4031 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
4032 {
4033 struct nextfnfield *nfp = flp->head;
4034 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
4035 int k;
4036
4037 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
4038 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
4039 fn_flp->fn_fields = (struct fn_field *)
4040 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
4041 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 4042 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
4043
4044 total_length += flp->length;
4045 }
4046
4047 TYPE_NFN_FIELDS (type) = fip->nfnfields;
4048 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
4049}
4050
1168df01
JB
4051/* Returns non-zero if NAME is the name of a vtable member in CU's
4052 language, zero otherwise. */
4053static int
4054is_vtable_name (const char *name, struct dwarf2_cu *cu)
4055{
4056 static const char vptr[] = "_vptr";
987504bb 4057 static const char vtable[] = "vtable";
1168df01 4058
987504bb
JJ
4059 /* Look for the C++ and Java forms of the vtable. */
4060 if ((cu->language == language_java
4061 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
4062 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
4063 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
4064 return 1;
4065
4066 return 0;
4067}
4068
c0dd20ea
DJ
4069/* GCC outputs unnamed structures that are really pointers to member
4070 functions, with the ABI-specified layout. If DIE (from CU) describes
4071 such a structure, set its type, and return nonzero. Otherwise return
61049d3b
DJ
4072 zero.
4073
4074 GCC shouldn't do this; it should just output pointer to member DIEs.
4075 This is GCC PR debug/28767. */
c0dd20ea 4076
f792889a 4077static struct type *
c0dd20ea
DJ
4078quirk_gcc_member_function_pointer (struct die_info *die, struct dwarf2_cu *cu)
4079{
4080 struct objfile *objfile = cu->objfile;
4081 struct type *type;
4082 struct die_info *pfn_die, *delta_die;
4083 struct attribute *pfn_name, *delta_name;
4084 struct type *pfn_type, *domain_type;
4085
4086 /* Check for a structure with no name and two children. */
4087 if (die->tag != DW_TAG_structure_type
4088 || dwarf2_attr (die, DW_AT_name, cu) != NULL
4089 || die->child == NULL
4090 || die->child->sibling == NULL
4091 || (die->child->sibling->sibling != NULL
4092 && die->child->sibling->sibling->tag != DW_TAG_padding))
f792889a 4093 return NULL;
c0dd20ea
DJ
4094
4095 /* Check for __pfn and __delta members. */
4096 pfn_die = die->child;
4097 pfn_name = dwarf2_attr (pfn_die, DW_AT_name, cu);
4098 if (pfn_die->tag != DW_TAG_member
4099 || pfn_name == NULL
4100 || DW_STRING (pfn_name) == NULL
4101 || strcmp ("__pfn", DW_STRING (pfn_name)) != 0)
f792889a 4102 return NULL;
c0dd20ea
DJ
4103
4104 delta_die = pfn_die->sibling;
4105 delta_name = dwarf2_attr (delta_die, DW_AT_name, cu);
4106 if (delta_die->tag != DW_TAG_member
4107 || delta_name == NULL
4108 || DW_STRING (delta_name) == NULL
4109 || strcmp ("__delta", DW_STRING (delta_name)) != 0)
f792889a 4110 return NULL;
c0dd20ea
DJ
4111
4112 /* Find the type of the method. */
4113 pfn_type = die_type (pfn_die, cu);
4114 if (pfn_type == NULL
4115 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
4116 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
f792889a 4117 return NULL;
c0dd20ea
DJ
4118
4119 /* Look for the "this" argument. */
4120 pfn_type = TYPE_TARGET_TYPE (pfn_type);
4121 if (TYPE_NFIELDS (pfn_type) == 0
4122 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
f792889a 4123 return NULL;
c0dd20ea
DJ
4124
4125 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
4126 type = alloc_type (objfile);
4127 smash_to_method_type (type, domain_type, TYPE_TARGET_TYPE (pfn_type),
4128 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
4129 TYPE_VARARGS (pfn_type));
0d5de010 4130 type = lookup_methodptr_type (type);
f792889a 4131 return set_die_type (die, type, cu);
c0dd20ea 4132}
1168df01 4133
c906108c
SS
4134/* Called when we find the DIE that starts a structure or union scope
4135 (definition) to process all dies that define the members of the
4136 structure or union.
4137
4138 NOTE: we need to call struct_type regardless of whether or not the
4139 DIE has an at_name attribute, since it might be an anonymous
4140 structure or union. This gets the type entered into our set of
4141 user defined types.
4142
4143 However, if the structure is incomplete (an opaque struct/union)
4144 then suppress creating a symbol table entry for it since gdb only
4145 wants to find the one with the complete definition. Note that if
4146 it is complete, we just call new_symbol, which does it's own
4147 checking about whether the struct/union is anonymous or not (and
4148 suppresses creating a symbol table entry itself). */
4149
f792889a 4150static struct type *
134d01f1 4151read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4152{
e7c27a73 4153 struct objfile *objfile = cu->objfile;
c906108c
SS
4154 struct type *type;
4155 struct attribute *attr;
39cbfefa 4156 char *name;
0114d602 4157 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c 4158
f792889a
DJ
4159 type = quirk_gcc_member_function_pointer (die, cu);
4160 if (type)
4161 return type;
c906108c 4162
c0dd20ea 4163 type = alloc_type (objfile);
c906108c 4164 INIT_CPLUS_SPECIFIC (type);
39cbfefa
DJ
4165 name = dwarf2_name (die, cu);
4166 if (name != NULL)
c906108c 4167 {
987504bb
JJ
4168 if (cu->language == language_cplus
4169 || cu->language == language_java)
63d06c5c 4170 {
0114d602
DJ
4171 const char *new_prefix = determine_class_name (die, cu);
4172 TYPE_TAG_NAME (type) = (char *) new_prefix;
63d06c5c
DC
4173 }
4174 else
4175 {
d8151005
DJ
4176 /* The name is already allocated along with this objfile, so
4177 we don't need to duplicate it for the type. */
39cbfefa 4178 TYPE_TAG_NAME (type) = name;
63d06c5c 4179 }
c906108c
SS
4180 }
4181
4182 if (die->tag == DW_TAG_structure_type)
4183 {
4184 TYPE_CODE (type) = TYPE_CODE_STRUCT;
4185 }
4186 else if (die->tag == DW_TAG_union_type)
4187 {
4188 TYPE_CODE (type) = TYPE_CODE_UNION;
4189 }
4190 else
4191 {
4192 /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
c5aa993b 4193 in gdbtypes.h. */
c906108c
SS
4194 TYPE_CODE (type) = TYPE_CODE_CLASS;
4195 }
4196
e142c38c 4197 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4198 if (attr)
4199 {
4200 TYPE_LENGTH (type) = DW_UNSND (attr);
4201 }
4202 else
4203 {
4204 TYPE_LENGTH (type) = 0;
4205 }
4206
876cecd0 4207 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 4208 if (die_is_declaration (die, cu))
876cecd0 4209 TYPE_STUB (type) = 1;
dc718098 4210
c906108c
SS
4211 /* We need to add the type field to the die immediately so we don't
4212 infinitely recurse when dealing with pointers to the structure
4213 type within the structure itself. */
1c379e20 4214 set_die_type (die, type, cu);
c906108c 4215
e142c38c 4216 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
4217 {
4218 struct field_info fi;
4219 struct die_info *child_die;
c906108c
SS
4220
4221 memset (&fi, 0, sizeof (struct field_info));
4222
639d11d3 4223 child_die = die->child;
c906108c
SS
4224
4225 while (child_die && child_die->tag)
4226 {
a9a9bd0f
DC
4227 if (child_die->tag == DW_TAG_member
4228 || child_die->tag == DW_TAG_variable)
c906108c 4229 {
a9a9bd0f
DC
4230 /* NOTE: carlton/2002-11-05: A C++ static data member
4231 should be a DW_TAG_member that is a declaration, but
4232 all versions of G++ as of this writing (so through at
4233 least 3.2.1) incorrectly generate DW_TAG_variable
4234 tags for them instead. */
e7c27a73 4235 dwarf2_add_field (&fi, child_die, cu);
c906108c 4236 }
8713b1b1 4237 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
4238 {
4239 /* C++ member function. */
e7c27a73 4240 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
4241 }
4242 else if (child_die->tag == DW_TAG_inheritance)
4243 {
4244 /* C++ base class field. */
e7c27a73 4245 dwarf2_add_field (&fi, child_die, cu);
c906108c 4246 }
c906108c
SS
4247 child_die = sibling_die (child_die);
4248 }
4249
4250 /* Attach fields and member functions to the type. */
4251 if (fi.nfields)
e7c27a73 4252 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
4253 if (fi.nfnfields)
4254 {
e7c27a73 4255 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 4256
c5aa993b 4257 /* Get the type which refers to the base class (possibly this
c906108c
SS
4258 class itself) which contains the vtable pointer for the current
4259 class from the DW_AT_containing_type attribute. */
4260
e142c38c 4261 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 4262 {
e7c27a73 4263 struct type *t = die_containing_type (die, cu);
c906108c
SS
4264
4265 TYPE_VPTR_BASETYPE (type) = t;
4266 if (type == t)
4267 {
c906108c
SS
4268 int i;
4269
4270 /* Our own class provides vtbl ptr. */
4271 for (i = TYPE_NFIELDS (t) - 1;
4272 i >= TYPE_N_BASECLASSES (t);
4273 --i)
4274 {
4275 char *fieldname = TYPE_FIELD_NAME (t, i);
4276
1168df01 4277 if (is_vtable_name (fieldname, cu))
c906108c
SS
4278 {
4279 TYPE_VPTR_FIELDNO (type) = i;
4280 break;
4281 }
4282 }
4283
4284 /* Complain if virtual function table field not found. */
4285 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 4286 complaint (&symfile_complaints,
e2e0b3e5 4287 _("virtual function table pointer not found when defining class '%s'"),
4d3c2250
KB
4288 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
4289 "");
c906108c
SS
4290 }
4291 else
4292 {
4293 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
4294 }
4295 }
f6235d4c
EZ
4296 else if (cu->producer
4297 && strncmp (cu->producer,
4298 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
4299 {
4300 /* The IBM XLC compiler does not provide direct indication
4301 of the containing type, but the vtable pointer is
4302 always named __vfp. */
4303
4304 int i;
4305
4306 for (i = TYPE_NFIELDS (type) - 1;
4307 i >= TYPE_N_BASECLASSES (type);
4308 --i)
4309 {
4310 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
4311 {
4312 TYPE_VPTR_FIELDNO (type) = i;
4313 TYPE_VPTR_BASETYPE (type) = type;
4314 break;
4315 }
4316 }
4317 }
c906108c 4318 }
c906108c 4319 }
63d06c5c 4320
0114d602 4321 do_cleanups (back_to);
f792889a 4322 return type;
c906108c
SS
4323}
4324
134d01f1
DJ
4325static void
4326process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
4327{
4328 struct objfile *objfile = cu->objfile;
90aeadfc 4329 struct die_info *child_die = die->child;
f792889a 4330 struct type *this_type;
c906108c 4331
f792889a
DJ
4332 this_type = get_die_type (die, cu);
4333 if (this_type == NULL)
4334 this_type = read_structure_type (die, cu);
c906108c 4335
90aeadfc
DC
4336 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
4337 snapshots) has been known to create a die giving a declaration
4338 for a class that has, as a child, a die giving a definition for a
4339 nested class. So we have to process our children even if the
4340 current die is a declaration. Normally, of course, a declaration
4341 won't have any children at all. */
134d01f1 4342
90aeadfc
DC
4343 while (child_die != NULL && child_die->tag)
4344 {
4345 if (child_die->tag == DW_TAG_member
4346 || child_die->tag == DW_TAG_variable
4347 || child_die->tag == DW_TAG_inheritance)
134d01f1 4348 {
90aeadfc 4349 /* Do nothing. */
134d01f1 4350 }
90aeadfc
DC
4351 else
4352 process_die (child_die, cu);
134d01f1 4353
90aeadfc 4354 child_die = sibling_die (child_die);
134d01f1
DJ
4355 }
4356
fa4028e9
JB
4357 /* Do not consider external references. According to the DWARF standard,
4358 these DIEs are identified by the fact that they have no byte_size
4359 attribute, and a declaration attribute. */
4360 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
4361 || !die_is_declaration (die, cu))
f792889a 4362 new_symbol (die, this_type, cu);
134d01f1
DJ
4363}
4364
4365/* Given a DW_AT_enumeration_type die, set its type. We do not
4366 complete the type's fields yet, or create any symbols. */
c906108c 4367
f792889a 4368static struct type *
134d01f1 4369read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4370{
e7c27a73 4371 struct objfile *objfile = cu->objfile;
c906108c 4372 struct type *type;
c906108c 4373 struct attribute *attr;
0114d602 4374 const char *name;
134d01f1 4375
c906108c
SS
4376 type = alloc_type (objfile);
4377
4378 TYPE_CODE (type) = TYPE_CODE_ENUM;
0114d602 4379 name = dwarf2_full_name (die, cu);
39cbfefa 4380 if (name != NULL)
0114d602 4381 TYPE_TAG_NAME (type) = (char *) name;
c906108c 4382
e142c38c 4383 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4384 if (attr)
4385 {
4386 TYPE_LENGTH (type) = DW_UNSND (attr);
4387 }
4388 else
4389 {
4390 TYPE_LENGTH (type) = 0;
4391 }
4392
137033e9
JB
4393 /* The enumeration DIE can be incomplete. In Ada, any type can be
4394 declared as private in the package spec, and then defined only
4395 inside the package body. Such types are known as Taft Amendment
4396 Types. When another package uses such a type, an incomplete DIE
4397 may be generated by the compiler. */
02eb380e 4398 if (die_is_declaration (die, cu))
876cecd0 4399 TYPE_STUB (type) = 1;
02eb380e 4400
f792889a 4401 return set_die_type (die, type, cu);
134d01f1
DJ
4402}
4403
8176b9b8 4404/* Determine the name of the type represented by DIE, which should be
0114d602
DJ
4405 a named C++ or Java compound type. Return the name in question,
4406 allocated on the objfile obstack. */
8176b9b8 4407
0114d602 4408static const char *
8176b9b8
DC
4409determine_class_name (struct die_info *die, struct dwarf2_cu *cu)
4410{
0114d602 4411 const char *new_prefix = NULL;
8176b9b8
DC
4412
4413 /* If we don't have namespace debug info, guess the name by trying
4414 to demangle the names of members, just like we did in
72bf9492 4415 guess_structure_name. */
8176b9b8
DC
4416 if (!processing_has_namespace_info)
4417 {
4418 struct die_info *child;
4419
4420 for (child = die->child;
4421 child != NULL && child->tag != 0;
4422 child = sibling_die (child))
4423 {
4424 if (child->tag == DW_TAG_subprogram)
4425 {
0114d602 4426 char *phys_prefix
31c27f77
JJ
4427 = language_class_name_from_physname (cu->language_defn,
4428 dwarf2_linkage_name
8176b9b8
DC
4429 (child, cu));
4430
0114d602
DJ
4431 if (phys_prefix != NULL)
4432 {
4433 new_prefix
4434 = obsavestring (phys_prefix, strlen (phys_prefix),
4435 &cu->objfile->objfile_obstack);
4436 xfree (phys_prefix);
4437 break;
4438 }
8176b9b8
DC
4439 }
4440 }
4441 }
4442
4443 if (new_prefix == NULL)
0114d602 4444 new_prefix = dwarf2_full_name (die, cu);
8176b9b8
DC
4445
4446 return new_prefix;
4447}
4448
134d01f1
DJ
4449/* Given a pointer to a die which begins an enumeration, process all
4450 the dies that define the members of the enumeration, and create the
4451 symbol for the enumeration type.
4452
4453 NOTE: We reverse the order of the element list. */
4454
4455static void
4456process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
4457{
4458 struct objfile *objfile = cu->objfile;
4459 struct die_info *child_die;
4460 struct field *fields;
134d01f1
DJ
4461 struct symbol *sym;
4462 int num_fields;
4463 int unsigned_enum = 1;
39cbfefa 4464 char *name;
f792889a 4465 struct type *this_type;
134d01f1 4466
c906108c
SS
4467 num_fields = 0;
4468 fields = NULL;
f792889a
DJ
4469 this_type = get_die_type (die, cu);
4470 if (this_type == NULL)
4471 this_type = read_enumeration_type (die, cu);
639d11d3 4472 if (die->child != NULL)
c906108c 4473 {
639d11d3 4474 child_die = die->child;
c906108c
SS
4475 while (child_die && child_die->tag)
4476 {
4477 if (child_die->tag != DW_TAG_enumerator)
4478 {
e7c27a73 4479 process_die (child_die, cu);
c906108c
SS
4480 }
4481 else
4482 {
39cbfefa
DJ
4483 name = dwarf2_name (child_die, cu);
4484 if (name)
c906108c 4485 {
f792889a 4486 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
4487 if (SYMBOL_VALUE (sym) < 0)
4488 unsigned_enum = 0;
4489
4490 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
4491 {
4492 fields = (struct field *)
4493 xrealloc (fields,
4494 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 4495 * sizeof (struct field));
c906108c
SS
4496 }
4497
3567439c 4498 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 4499 FIELD_TYPE (fields[num_fields]) = NULL;
d6a843b5 4500 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
4501 FIELD_BITSIZE (fields[num_fields]) = 0;
4502
4503 num_fields++;
4504 }
4505 }
4506
4507 child_die = sibling_die (child_die);
4508 }
4509
4510 if (num_fields)
4511 {
f792889a
DJ
4512 TYPE_NFIELDS (this_type) = num_fields;
4513 TYPE_FIELDS (this_type) = (struct field *)
4514 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
4515 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 4516 sizeof (struct field) * num_fields);
b8c9b27d 4517 xfree (fields);
c906108c
SS
4518 }
4519 if (unsigned_enum)
876cecd0 4520 TYPE_UNSIGNED (this_type) = 1;
c906108c 4521 }
134d01f1 4522
f792889a 4523 new_symbol (die, this_type, cu);
c906108c
SS
4524}
4525
4526/* Extract all information from a DW_TAG_array_type DIE and put it in
4527 the DIE's type field. For now, this only handles one dimensional
4528 arrays. */
4529
f792889a 4530static struct type *
e7c27a73 4531read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4532{
e7c27a73 4533 struct objfile *objfile = cu->objfile;
c906108c
SS
4534 struct die_info *child_die;
4535 struct type *type = NULL;
4536 struct type *element_type, *range_type, *index_type;
4537 struct type **range_types = NULL;
4538 struct attribute *attr;
4539 int ndim = 0;
4540 struct cleanup *back_to;
39cbfefa 4541 char *name;
c906108c 4542
e7c27a73 4543 element_type = die_type (die, cu);
c906108c
SS
4544
4545 /* Irix 6.2 native cc creates array types without children for
4546 arrays with unspecified length. */
639d11d3 4547 if (die->child == NULL)
c906108c 4548 {
6ccb9162 4549 index_type = builtin_type_int32;
c906108c 4550 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
4551 type = create_array_type (NULL, element_type, range_type);
4552 return set_die_type (die, type, cu);
c906108c
SS
4553 }
4554
4555 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 4556 child_die = die->child;
c906108c
SS
4557 while (child_die && child_die->tag)
4558 {
4559 if (child_die->tag == DW_TAG_subrange_type)
4560 {
f792889a
DJ
4561 struct type *child_type = read_type_die (child_die, cu);
4562 if (child_type != NULL)
a02abb62
JB
4563 {
4564 /* The range type was succesfully read. Save it for
4565 the array type creation. */
4566 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
4567 {
4568 range_types = (struct type **)
4569 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
4570 * sizeof (struct type *));
4571 if (ndim == 0)
4572 make_cleanup (free_current_contents, &range_types);
4573 }
f792889a 4574 range_types[ndim++] = child_type;
a02abb62 4575 }
c906108c
SS
4576 }
4577 child_die = sibling_die (child_die);
4578 }
4579
4580 /* Dwarf2 dimensions are output from left to right, create the
4581 necessary array types in backwards order. */
7ca2d3a3 4582
c906108c 4583 type = element_type;
7ca2d3a3
DL
4584
4585 if (read_array_order (die, cu) == DW_ORD_col_major)
4586 {
4587 int i = 0;
4588 while (i < ndim)
4589 type = create_array_type (NULL, type, range_types[i++]);
4590 }
4591 else
4592 {
4593 while (ndim-- > 0)
4594 type = create_array_type (NULL, type, range_types[ndim]);
4595 }
c906108c 4596
f5f8a009
EZ
4597 /* Understand Dwarf2 support for vector types (like they occur on
4598 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
4599 array type. This is not part of the Dwarf2/3 standard yet, but a
4600 custom vendor extension. The main difference between a regular
4601 array and the vector variant is that vectors are passed by value
4602 to functions. */
e142c38c 4603 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 4604 if (attr)
ea37ba09 4605 make_vector_type (type);
f5f8a009 4606
39cbfefa
DJ
4607 name = dwarf2_name (die, cu);
4608 if (name)
4609 TYPE_NAME (type) = name;
714e295e 4610
c906108c
SS
4611 do_cleanups (back_to);
4612
4613 /* Install the type in the die. */
f792889a 4614 return set_die_type (die, type, cu);
c906108c
SS
4615}
4616
7ca2d3a3
DL
4617static enum dwarf_array_dim_ordering
4618read_array_order (struct die_info *die, struct dwarf2_cu *cu)
4619{
4620 struct attribute *attr;
4621
4622 attr = dwarf2_attr (die, DW_AT_ordering, cu);
4623
4624 if (attr) return DW_SND (attr);
4625
4626 /*
4627 GNU F77 is a special case, as at 08/2004 array type info is the
4628 opposite order to the dwarf2 specification, but data is still
4629 laid out as per normal fortran.
4630
4631 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
4632 version checking.
4633 */
4634
4635 if (cu->language == language_fortran &&
4636 cu->producer && strstr (cu->producer, "GNU F77"))
4637 {
4638 return DW_ORD_row_major;
4639 }
4640
4641 switch (cu->language_defn->la_array_ordering)
4642 {
4643 case array_column_major:
4644 return DW_ORD_col_major;
4645 case array_row_major:
4646 default:
4647 return DW_ORD_row_major;
4648 };
4649}
4650
72019c9c
GM
4651/* Extract all information from a DW_TAG_set_type DIE and put it in
4652 the DIE's type field. */
4653
f792889a 4654static struct type *
72019c9c
GM
4655read_set_type (struct die_info *die, struct dwarf2_cu *cu)
4656{
f792889a
DJ
4657 struct type *set_type = create_set_type (NULL, die_type (die, cu));
4658
4659 return set_die_type (die, set_type, cu);
72019c9c 4660}
7ca2d3a3 4661
c906108c
SS
4662/* First cut: install each common block member as a global variable. */
4663
4664static void
e7c27a73 4665read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
4666{
4667 struct die_info *child_die;
4668 struct attribute *attr;
4669 struct symbol *sym;
4670 CORE_ADDR base = (CORE_ADDR) 0;
4671
e142c38c 4672 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
4673 if (attr)
4674 {
8e19ed76
PS
4675 /* Support the .debug_loc offsets */
4676 if (attr_form_is_block (attr))
4677 {
e7c27a73 4678 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 4679 }
3690dd37 4680 else if (attr_form_is_section_offset (attr))
8e19ed76 4681 {
4d3c2250 4682 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
4683 }
4684 else
4685 {
4d3c2250
KB
4686 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
4687 "common block member");
8e19ed76 4688 }
c906108c 4689 }
639d11d3 4690 if (die->child != NULL)
c906108c 4691 {
639d11d3 4692 child_die = die->child;
c906108c
SS
4693 while (child_die && child_die->tag)
4694 {
e7c27a73 4695 sym = new_symbol (child_die, NULL, cu);
e142c38c 4696 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
c906108c
SS
4697 if (attr)
4698 {
4699 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 4700 base + decode_locdesc (DW_BLOCK (attr), cu);
c906108c
SS
4701 add_symbol_to_list (sym, &global_symbols);
4702 }
4703 child_die = sibling_die (child_die);
4704 }
4705 }
4706}
4707
0114d602 4708/* Create a type for a C++ namespace. */
d9fa45fe 4709
0114d602
DJ
4710static struct type *
4711read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 4712{
e7c27a73 4713 struct objfile *objfile = cu->objfile;
0114d602 4714 const char *previous_prefix, *name;
9219021c 4715 int is_anonymous;
0114d602
DJ
4716 struct type *type;
4717
4718 /* For extensions, reuse the type of the original namespace. */
4719 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
4720 {
4721 struct die_info *ext_die;
4722 struct dwarf2_cu *ext_cu = cu;
4723 ext_die = dwarf2_extension (die, &ext_cu);
4724 type = read_type_die (ext_die, ext_cu);
4725 return set_die_type (die, type, cu);
4726 }
9219021c 4727
e142c38c 4728 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
4729
4730 /* Now build the name of the current namespace. */
4731
0114d602
DJ
4732 previous_prefix = determine_prefix (die, cu);
4733 if (previous_prefix[0] != '\0')
4734 name = typename_concat (&objfile->objfile_obstack,
4735 previous_prefix, name, cu);
4736
4737 /* Create the type. */
4738 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
4739 objfile);
4740 TYPE_NAME (type) = (char *) name;
4741 TYPE_TAG_NAME (type) = TYPE_NAME (type);
4742
4743 set_die_type (die, type, cu);
4744
4745 return type;
4746}
4747
4748/* Read a C++ namespace. */
4749
4750static void
4751read_namespace (struct die_info *die, struct dwarf2_cu *cu)
4752{
4753 struct objfile *objfile = cu->objfile;
4754 const char *name;
4755 int is_anonymous;
9219021c 4756
5c4e30ca
DC
4757 /* Add a symbol associated to this if we haven't seen the namespace
4758 before. Also, add a using directive if it's an anonymous
4759 namespace. */
9219021c 4760
f2f0e013 4761 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
4762 {
4763 struct type *type;
4764
0114d602 4765 type = read_type_die (die, cu);
e7c27a73 4766 new_symbol (die, type, cu);
5c4e30ca 4767
0114d602 4768 name = namespace_name (die, &is_anonymous, cu);
5c4e30ca 4769 if (is_anonymous)
0114d602
DJ
4770 {
4771 const char *previous_prefix = determine_prefix (die, cu);
4772 cp_add_using_directive (TYPE_NAME (type),
4773 strlen (previous_prefix),
4774 strlen (TYPE_NAME (type)));
4775 }
5c4e30ca 4776 }
9219021c 4777
639d11d3 4778 if (die->child != NULL)
d9fa45fe 4779 {
639d11d3 4780 struct die_info *child_die = die->child;
d9fa45fe
DC
4781
4782 while (child_die && child_die->tag)
4783 {
e7c27a73 4784 process_die (child_die, cu);
d9fa45fe
DC
4785 child_die = sibling_die (child_die);
4786 }
4787 }
38d518c9
EZ
4788}
4789
5d7cb8df
JK
4790/* Read a Fortran module. */
4791
4792static void
4793read_module (struct die_info *die, struct dwarf2_cu *cu)
4794{
4795 struct die_info *child_die = die->child;
4796
4797 /* FIXME: Support the separate Fortran module namespaces. */
4798
4799 while (child_die && child_die->tag)
4800 {
4801 process_die (child_die, cu);
4802 child_die = sibling_die (child_die);
4803 }
4804}
4805
38d518c9
EZ
4806/* Return the name of the namespace represented by DIE. Set
4807 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
4808 namespace. */
4809
4810static const char *
e142c38c 4811namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
4812{
4813 struct die_info *current_die;
4814 const char *name = NULL;
4815
4816 /* Loop through the extensions until we find a name. */
4817
4818 for (current_die = die;
4819 current_die != NULL;
f2f0e013 4820 current_die = dwarf2_extension (die, &cu))
38d518c9 4821 {
e142c38c 4822 name = dwarf2_name (current_die, cu);
38d518c9
EZ
4823 if (name != NULL)
4824 break;
4825 }
4826
4827 /* Is it an anonymous namespace? */
4828
4829 *is_anonymous = (name == NULL);
4830 if (*is_anonymous)
4831 name = "(anonymous namespace)";
4832
4833 return name;
d9fa45fe
DC
4834}
4835
c906108c
SS
4836/* Extract all information from a DW_TAG_pointer_type DIE and add to
4837 the user defined type vector. */
4838
f792889a 4839static struct type *
e7c27a73 4840read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4841{
5e2b427d 4842 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 4843 struct comp_unit_head *cu_header = &cu->header;
c906108c 4844 struct type *type;
8b2dbe47
KB
4845 struct attribute *attr_byte_size;
4846 struct attribute *attr_address_class;
4847 int byte_size, addr_class;
c906108c 4848
e7c27a73 4849 type = lookup_pointer_type (die_type (die, cu));
8b2dbe47 4850
e142c38c 4851 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
4852 if (attr_byte_size)
4853 byte_size = DW_UNSND (attr_byte_size);
c906108c 4854 else
8b2dbe47
KB
4855 byte_size = cu_header->addr_size;
4856
e142c38c 4857 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
4858 if (attr_address_class)
4859 addr_class = DW_UNSND (attr_address_class);
4860 else
4861 addr_class = DW_ADDR_none;
4862
4863 /* If the pointer size or address class is different than the
4864 default, create a type variant marked as such and set the
4865 length accordingly. */
4866 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 4867 {
5e2b427d 4868 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
4869 {
4870 int type_flags;
4871
849957d9 4872 type_flags = gdbarch_address_class_type_flags
5e2b427d 4873 (gdbarch, byte_size, addr_class);
876cecd0
TT
4874 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
4875 == 0);
8b2dbe47
KB
4876 type = make_type_with_address_space (type, type_flags);
4877 }
4878 else if (TYPE_LENGTH (type) != byte_size)
4879 {
e2e0b3e5 4880 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
8b2dbe47
KB
4881 }
4882 else {
4883 /* Should we also complain about unhandled address classes? */
4884 }
c906108c 4885 }
8b2dbe47
KB
4886
4887 TYPE_LENGTH (type) = byte_size;
f792889a 4888 return set_die_type (die, type, cu);
c906108c
SS
4889}
4890
4891/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
4892 the user defined type vector. */
4893
f792889a 4894static struct type *
e7c27a73 4895read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4896{
e7c27a73 4897 struct objfile *objfile = cu->objfile;
c906108c
SS
4898 struct type *type;
4899 struct type *to_type;
4900 struct type *domain;
4901
e7c27a73
DJ
4902 to_type = die_type (die, cu);
4903 domain = die_containing_type (die, cu);
0d5de010
DJ
4904
4905 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
4906 type = lookup_methodptr_type (to_type);
4907 else
4908 type = lookup_memberptr_type (to_type, domain);
c906108c 4909
f792889a 4910 return set_die_type (die, type, cu);
c906108c
SS
4911}
4912
4913/* Extract all information from a DW_TAG_reference_type DIE and add to
4914 the user defined type vector. */
4915
f792889a 4916static struct type *
e7c27a73 4917read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4918{
e7c27a73 4919 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
4920 struct type *type;
4921 struct attribute *attr;
4922
e7c27a73 4923 type = lookup_reference_type (die_type (die, cu));
e142c38c 4924 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
4925 if (attr)
4926 {
4927 TYPE_LENGTH (type) = DW_UNSND (attr);
4928 }
4929 else
4930 {
107d2387 4931 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 4932 }
f792889a 4933 return set_die_type (die, type, cu);
c906108c
SS
4934}
4935
f792889a 4936static struct type *
e7c27a73 4937read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4938{
f792889a 4939 struct type *base_type, *cv_type;
c906108c 4940
e7c27a73 4941 base_type = die_type (die, cu);
f792889a
DJ
4942 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
4943 return set_die_type (die, cv_type, cu);
c906108c
SS
4944}
4945
f792889a 4946static struct type *
e7c27a73 4947read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4948{
f792889a 4949 struct type *base_type, *cv_type;
c906108c 4950
e7c27a73 4951 base_type = die_type (die, cu);
f792889a
DJ
4952 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
4953 return set_die_type (die, cv_type, cu);
c906108c
SS
4954}
4955
4956/* Extract all information from a DW_TAG_string_type DIE and add to
4957 the user defined type vector. It isn't really a user defined type,
4958 but it behaves like one, with other DIE's using an AT_user_def_type
4959 attribute to reference it. */
4960
f792889a 4961static struct type *
e7c27a73 4962read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 4963{
e7c27a73 4964 struct objfile *objfile = cu->objfile;
c906108c
SS
4965 struct type *type, *range_type, *index_type, *char_type;
4966 struct attribute *attr;
4967 unsigned int length;
4968
e142c38c 4969 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
4970 if (attr)
4971 {
4972 length = DW_UNSND (attr);
4973 }
4974 else
4975 {
b21b22e0 4976 /* check for the DW_AT_byte_size attribute */
e142c38c 4977 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
4978 if (attr)
4979 {
4980 length = DW_UNSND (attr);
4981 }
4982 else
4983 {
4984 length = 1;
4985 }
c906108c 4986 }
6ccb9162
UW
4987
4988 index_type = builtin_type_int32;
c906108c 4989 range_type = create_range_type (NULL, index_type, 1, length);
6ccb9162
UW
4990 type = create_string_type (NULL, range_type);
4991
f792889a 4992 return set_die_type (die, type, cu);
c906108c
SS
4993}
4994
4995/* Handle DIES due to C code like:
4996
4997 struct foo
c5aa993b
JM
4998 {
4999 int (*funcp)(int a, long l);
5000 int b;
5001 };
c906108c
SS
5002
5003 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 5004 */
c906108c 5005
f792889a 5006static struct type *
e7c27a73 5007read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5008{
5009 struct type *type; /* Type that this function returns */
5010 struct type *ftype; /* Function that returns above type */
5011 struct attribute *attr;
5012
e7c27a73 5013 type = die_type (die, cu);
1326e61b 5014 ftype = make_function_type (type, (struct type **) 0);
c906108c 5015
5b8101ae 5016 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 5017 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 5018 if ((attr && (DW_UNSND (attr) != 0))
987504bb 5019 || cu->language == language_cplus
5b8101ae
PM
5020 || cu->language == language_java
5021 || cu->language == language_pascal)
876cecd0 5022 TYPE_PROTOTYPED (ftype) = 1;
c906108c 5023
c055b101
CV
5024 /* Store the calling convention in the type if it's available in
5025 the subroutine die. Otherwise set the calling convention to
5026 the default value DW_CC_normal. */
5027 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
5028 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
5029
639d11d3 5030 if (die->child != NULL)
c906108c
SS
5031 {
5032 struct die_info *child_die;
5033 int nparams = 0;
5034 int iparams = 0;
5035
5036 /* Count the number of parameters.
5037 FIXME: GDB currently ignores vararg functions, but knows about
5038 vararg member functions. */
639d11d3 5039 child_die = die->child;
c906108c
SS
5040 while (child_die && child_die->tag)
5041 {
5042 if (child_die->tag == DW_TAG_formal_parameter)
5043 nparams++;
5044 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 5045 TYPE_VARARGS (ftype) = 1;
c906108c
SS
5046 child_die = sibling_die (child_die);
5047 }
5048
5049 /* Allocate storage for parameters and fill them in. */
5050 TYPE_NFIELDS (ftype) = nparams;
5051 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 5052 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 5053
639d11d3 5054 child_die = die->child;
c906108c
SS
5055 while (child_die && child_die->tag)
5056 {
5057 if (child_die->tag == DW_TAG_formal_parameter)
5058 {
5059 /* Dwarf2 has no clean way to discern C++ static and non-static
c5aa993b
JM
5060 member functions. G++ helps GDB by marking the first
5061 parameter for non-static member functions (which is the
5062 this pointer) as artificial. We pass this information
5063 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
e142c38c 5064 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
5065 if (attr)
5066 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
5067 else
5068 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
e7c27a73 5069 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
c906108c
SS
5070 iparams++;
5071 }
5072 child_die = sibling_die (child_die);
5073 }
5074 }
5075
f792889a 5076 return set_die_type (die, ftype, cu);
c906108c
SS
5077}
5078
f792889a 5079static struct type *
e7c27a73 5080read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5081{
e7c27a73 5082 struct objfile *objfile = cu->objfile;
2f038fcb 5083 struct attribute *attr;
0114d602 5084 const char *name = NULL;
f792889a 5085 struct type *this_type;
c906108c 5086
0114d602 5087 name = dwarf2_full_name (die, cu);
f792889a 5088 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
5089 TYPE_FLAG_TARGET_STUB, NULL, objfile);
5090 TYPE_NAME (this_type) = (char *) name;
f792889a
DJ
5091 set_die_type (die, this_type, cu);
5092 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
5093 return this_type;
c906108c
SS
5094}
5095
5096/* Find a representation of a given base type and install
5097 it in the TYPE field of the die. */
5098
f792889a 5099static struct type *
e7c27a73 5100read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5101{
e7c27a73 5102 struct objfile *objfile = cu->objfile;
c906108c
SS
5103 struct type *type;
5104 struct attribute *attr;
5105 int encoding = 0, size = 0;
39cbfefa 5106 char *name;
6ccb9162
UW
5107 enum type_code code = TYPE_CODE_INT;
5108 int type_flags = 0;
5109 struct type *target_type = NULL;
c906108c 5110
e142c38c 5111 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
5112 if (attr)
5113 {
5114 encoding = DW_UNSND (attr);
5115 }
e142c38c 5116 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
5117 if (attr)
5118 {
5119 size = DW_UNSND (attr);
5120 }
39cbfefa 5121 name = dwarf2_name (die, cu);
6ccb9162 5122 if (!name)
c906108c 5123 {
6ccb9162
UW
5124 complaint (&symfile_complaints,
5125 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 5126 }
6ccb9162
UW
5127
5128 switch (encoding)
c906108c 5129 {
6ccb9162
UW
5130 case DW_ATE_address:
5131 /* Turn DW_ATE_address into a void * pointer. */
5132 code = TYPE_CODE_PTR;
5133 type_flags |= TYPE_FLAG_UNSIGNED;
5134 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
5135 break;
5136 case DW_ATE_boolean:
5137 code = TYPE_CODE_BOOL;
5138 type_flags |= TYPE_FLAG_UNSIGNED;
5139 break;
5140 case DW_ATE_complex_float:
5141 code = TYPE_CODE_COMPLEX;
5142 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
5143 break;
5144 case DW_ATE_decimal_float:
5145 code = TYPE_CODE_DECFLOAT;
5146 break;
5147 case DW_ATE_float:
5148 code = TYPE_CODE_FLT;
5149 break;
5150 case DW_ATE_signed:
5151 break;
5152 case DW_ATE_unsigned:
5153 type_flags |= TYPE_FLAG_UNSIGNED;
5154 break;
5155 case DW_ATE_signed_char:
868a0084
PM
5156 if (cu->language == language_ada || cu->language == language_m2
5157 || cu->language == language_pascal)
6ccb9162
UW
5158 code = TYPE_CODE_CHAR;
5159 break;
5160 case DW_ATE_unsigned_char:
868a0084
PM
5161 if (cu->language == language_ada || cu->language == language_m2
5162 || cu->language == language_pascal)
6ccb9162
UW
5163 code = TYPE_CODE_CHAR;
5164 type_flags |= TYPE_FLAG_UNSIGNED;
5165 break;
5166 default:
5167 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
5168 dwarf_type_encoding_name (encoding));
5169 break;
c906108c 5170 }
6ccb9162 5171
0114d602
DJ
5172 type = init_type (code, size, type_flags, NULL, objfile);
5173 TYPE_NAME (type) = name;
6ccb9162
UW
5174 TYPE_TARGET_TYPE (type) = target_type;
5175
0114d602 5176 if (name && strcmp (name, "char") == 0)
876cecd0 5177 TYPE_NOSIGN (type) = 1;
0114d602 5178
f792889a 5179 return set_die_type (die, type, cu);
c906108c
SS
5180}
5181
a02abb62
JB
5182/* Read the given DW_AT_subrange DIE. */
5183
f792889a 5184static struct type *
a02abb62
JB
5185read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
5186{
5e2b427d 5187 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
a02abb62
JB
5188 struct type *base_type;
5189 struct type *range_type;
5190 struct attribute *attr;
5191 int low = 0;
5192 int high = -1;
39cbfefa 5193 char *name;
a02abb62 5194
a02abb62 5195 base_type = die_type (die, cu);
3d1f72c2 5196 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
a02abb62
JB
5197 {
5198 complaint (&symfile_complaints,
e2e0b3e5 5199 _("DW_AT_type missing from DW_TAG_subrange_type"));
17a912b6 5200 base_type
5e2b427d 5201 = init_type (TYPE_CODE_INT, gdbarch_addr_bit (gdbarch) / 8,
6ccb9162 5202 0, NULL, cu->objfile);
a02abb62
JB
5203 }
5204
e142c38c 5205 if (cu->language == language_fortran)
a02abb62
JB
5206 {
5207 /* FORTRAN implies a lower bound of 1, if not given. */
5208 low = 1;
5209 }
5210
dd5e6932
DJ
5211 /* FIXME: For variable sized arrays either of these could be
5212 a variable rather than a constant value. We'll allow it,
5213 but we don't know how to handle it. */
e142c38c 5214 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62
JB
5215 if (attr)
5216 low = dwarf2_get_attr_constant_value (attr, 0);
5217
e142c38c 5218 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62
JB
5219 if (attr)
5220 {
5221 if (attr->form == DW_FORM_block1)
5222 {
5223 /* GCC encodes arrays with unspecified or dynamic length
5224 with a DW_FORM_block1 attribute.
5225 FIXME: GDB does not yet know how to handle dynamic
5226 arrays properly, treat them as arrays with unspecified
5227 length for now.
5228
5229 FIXME: jimb/2003-09-22: GDB does not really know
5230 how to handle arrays of unspecified length
5231 either; we just represent them as zero-length
5232 arrays. Choose an appropriate upper bound given
5233 the lower bound we've computed above. */
5234 high = low - 1;
5235 }
5236 else
5237 high = dwarf2_get_attr_constant_value (attr, 1);
5238 }
5239
5240 range_type = create_range_type (NULL, base_type, low, high);
5241
39cbfefa
DJ
5242 name = dwarf2_name (die, cu);
5243 if (name)
5244 TYPE_NAME (range_type) = name;
a02abb62 5245
e142c38c 5246 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
5247 if (attr)
5248 TYPE_LENGTH (range_type) = DW_UNSND (attr);
5249
f792889a 5250 return set_die_type (die, range_type, cu);
a02abb62
JB
5251}
5252
f792889a 5253static struct type *
81a17f79
JB
5254read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
5255{
5256 struct type *type;
81a17f79 5257
81a17f79
JB
5258 /* For now, we only support the C meaning of an unspecified type: void. */
5259
0114d602
DJ
5260 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
5261 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 5262
f792889a 5263 return set_die_type (die, type, cu);
81a17f79 5264}
a02abb62 5265
51545339
DJ
5266/* Trivial hash function for die_info: the hash value of a DIE
5267 is its offset in .debug_info for this objfile. */
5268
5269static hashval_t
5270die_hash (const void *item)
5271{
5272 const struct die_info *die = item;
5273 return die->offset;
5274}
5275
5276/* Trivial comparison function for die_info structures: two DIEs
5277 are equal if they have the same offset. */
5278
5279static int
5280die_eq (const void *item_lhs, const void *item_rhs)
5281{
5282 const struct die_info *die_lhs = item_lhs;
5283 const struct die_info *die_rhs = item_rhs;
5284 return die_lhs->offset == die_rhs->offset;
5285}
5286
c906108c
SS
5287/* Read a whole compilation unit into a linked list of dies. */
5288
f9aca02d 5289static struct die_info *
fe1b8b76 5290read_comp_unit (gdb_byte *info_ptr, bfd *abfd, struct dwarf2_cu *cu)
c906108c 5291{
51545339
DJ
5292 cu->die_hash
5293 = htab_create_alloc_ex (cu->header.length / 12,
5294 die_hash,
5295 die_eq,
5296 NULL,
5297 &cu->comp_unit_obstack,
5298 hashtab_obstack_allocate,
5299 dummy_obstack_deallocate);
5300
e7c27a73 5301 return read_die_and_children (info_ptr, abfd, cu, &info_ptr, NULL);
639d11d3
DC
5302}
5303
d97bc12b
DE
5304/* Main entry point for reading a DIE and all children.
5305 Read the DIE and dump it if requested. */
5306
5307static struct die_info *
5308read_die_and_children (gdb_byte *info_ptr, bfd *abfd,
5309 struct dwarf2_cu *cu,
5310 gdb_byte **new_info_ptr,
5311 struct die_info *parent)
5312{
5313 struct die_info *result = read_die_and_children_1 (info_ptr, abfd, cu,
5314 new_info_ptr, parent);
5315
5316 if (dwarf2_die_debug)
5317 {
5318 fprintf_unfiltered (gdb_stdlog, "Read die from .debug_info:\n");
5319 dump_die (result, dwarf2_die_debug);
5320 }
5321
5322 return result;
5323}
5324
639d11d3
DC
5325/* Read a single die and all its descendents. Set the die's sibling
5326 field to NULL; set other fields in the die correctly, and set all
5327 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
5328 location of the info_ptr after reading all of those dies. PARENT
5329 is the parent of the die in question. */
5330
5331static struct die_info *
d97bc12b
DE
5332read_die_and_children_1 (gdb_byte *info_ptr, bfd *abfd,
5333 struct dwarf2_cu *cu,
5334 gdb_byte **new_info_ptr,
5335 struct die_info *parent)
639d11d3
DC
5336{
5337 struct die_info *die;
fe1b8b76 5338 gdb_byte *cur_ptr;
639d11d3
DC
5339 int has_children;
5340
e7c27a73 5341 cur_ptr = read_full_die (&die, abfd, info_ptr, cu, &has_children);
1d325ec1
DJ
5342 if (die == NULL)
5343 {
5344 *new_info_ptr = cur_ptr;
5345 return NULL;
5346 }
51545339 5347 store_in_ref_table (die, cu);
639d11d3
DC
5348
5349 if (has_children)
5350 {
e7c27a73 5351 die->child = read_die_and_siblings (cur_ptr, abfd, cu,
639d11d3
DC
5352 new_info_ptr, die);
5353 }
5354 else
5355 {
5356 die->child = NULL;
5357 *new_info_ptr = cur_ptr;
5358 }
5359
5360 die->sibling = NULL;
5361 die->parent = parent;
5362 return die;
5363}
5364
5365/* Read a die, all of its descendents, and all of its siblings; set
5366 all of the fields of all of the dies correctly. Arguments are as
5367 in read_die_and_children. */
5368
5369static struct die_info *
fe1b8b76 5370read_die_and_siblings (gdb_byte *info_ptr, bfd *abfd,
e7c27a73 5371 struct dwarf2_cu *cu,
fe1b8b76 5372 gdb_byte **new_info_ptr,
639d11d3
DC
5373 struct die_info *parent)
5374{
5375 struct die_info *first_die, *last_sibling;
fe1b8b76 5376 gdb_byte *cur_ptr;
639d11d3 5377
c906108c 5378 cur_ptr = info_ptr;
639d11d3
DC
5379 first_die = last_sibling = NULL;
5380
5381 while (1)
c906108c 5382 {
639d11d3 5383 struct die_info *die
d97bc12b 5384 = read_die_and_children_1 (cur_ptr, abfd, cu, &cur_ptr, parent);
639d11d3 5385
1d325ec1 5386 if (die == NULL)
c906108c 5387 {
639d11d3
DC
5388 *new_info_ptr = cur_ptr;
5389 return first_die;
c906108c 5390 }
1d325ec1
DJ
5391
5392 if (!first_die)
5393 first_die = die;
c906108c 5394 else
1d325ec1
DJ
5395 last_sibling->sibling = die;
5396
5397 last_sibling = die;
c906108c 5398 }
c906108c
SS
5399}
5400
233a11ab
CS
5401/* Decompress a section that was compressed using zlib. Store the
5402 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
5403
5404static void
5405zlib_decompress_section (struct objfile *objfile, asection *sectp,
5406 gdb_byte **outbuf, bfd_size_type *outsize)
5407{
ef72380d 5408 bfd *abfd = objfile->obfd;
233a11ab
CS
5409#ifndef HAVE_ZLIB_H
5410 error (_("Support for zlib-compressed DWARF data (from '%s') "
5411 "is disabled in this copy of GDB"),
5412 bfd_get_filename (abfd));
5413#else
233a11ab
CS
5414 bfd_size_type compressed_size = bfd_get_section_size (sectp);
5415 gdb_byte *compressed_buffer = xmalloc (compressed_size);
5416 bfd_size_type uncompressed_size;
5417 gdb_byte *uncompressed_buffer;
5418 z_stream strm;
5419 int rc;
5420 int header_size = 12;
5421
5422 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
5423 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
5424 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
5425 bfd_get_filename (abfd));
5426
5427 /* Read the zlib header. In this case, it should be "ZLIB" followed
5428 by the uncompressed section size, 8 bytes in big-endian order. */
5429 if (compressed_size < header_size
5430 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
5431 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
5432 bfd_get_filename (abfd));
5433 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
5434 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
5435 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
5436 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
5437 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
5438 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
5439 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
5440 uncompressed_size += compressed_buffer[11];
5441
5442 /* It is possible the section consists of several compressed
5443 buffers concatenated together, so we uncompress in a loop. */
5444 strm.zalloc = NULL;
5445 strm.zfree = NULL;
5446 strm.opaque = NULL;
5447 strm.avail_in = compressed_size - header_size;
5448 strm.next_in = (Bytef*) compressed_buffer + header_size;
5449 strm.avail_out = uncompressed_size;
5450 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
5451 uncompressed_size);
5452 rc = inflateInit (&strm);
5453 while (strm.avail_in > 0)
5454 {
5455 if (rc != Z_OK)
5456 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
5457 bfd_get_filename (abfd), rc);
5458 strm.next_out = ((Bytef*) uncompressed_buffer
5459 + (uncompressed_size - strm.avail_out));
5460 rc = inflate (&strm, Z_FINISH);
5461 if (rc != Z_STREAM_END)
5462 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
5463 bfd_get_filename (abfd), rc);
5464 rc = inflateReset (&strm);
5465 }
5466 rc = inflateEnd (&strm);
5467 if (rc != Z_OK
5468 || strm.avail_out != 0)
5469 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
5470 bfd_get_filename (abfd), rc);
5471
5472 xfree (compressed_buffer);
5473 *outbuf = uncompressed_buffer;
5474 *outsize = uncompressed_size;
5475#endif
5476}
5477
5478
c906108c 5479/* Read the contents of the section at OFFSET and of size SIZE from the
233a11ab
CS
5480 object file specified by OBJFILE into the objfile_obstack and return it.
5481 If the section is compressed, uncompress it before returning. */
c906108c 5482
fe1b8b76 5483gdb_byte *
188dd5d6 5484dwarf2_read_section (struct objfile *objfile, asection *sectp)
c906108c
SS
5485{
5486 bfd *abfd = objfile->obfd;
fe1b8b76 5487 gdb_byte *buf, *retbuf;
2c500098 5488 bfd_size_type size = bfd_get_section_size (sectp);
233a11ab 5489 unsigned char header[4];
c906108c
SS
5490
5491 if (size == 0)
5492 return NULL;
5493
233a11ab
CS
5494 /* Check if the file has a 4-byte header indicating compression. */
5495 if (size > sizeof (header)
5496 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
5497 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
5498 {
5499 /* Upon decompression, update the buffer and its size. */
5500 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
5501 {
5502 zlib_decompress_section (objfile, sectp, &buf, &size);
5503 dwarf2_resize_section (sectp, size);
5504 return buf;
5505 }
5506 }
5507
5508 /* If we get here, we are a normal, not-compressed section. */
fe1b8b76 5509 buf = obstack_alloc (&objfile->objfile_obstack, size);
233a11ab
CS
5510 /* When debugging .o files, we may need to apply relocations; see
5511 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
5512 We never compress sections in .o files, so we only need to
5513 try this when the section is not compressed. */
fe1b8b76 5514 retbuf = symfile_relocate_debug_section (abfd, sectp, buf);
086df311
DJ
5515 if (retbuf != NULL)
5516 return retbuf;
5517
188dd5d6
DJ
5518 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
5519 || bfd_bread (buf, size, abfd) != size)
8a3fe4f8 5520 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
188dd5d6
DJ
5521 bfd_get_filename (abfd));
5522
c906108c
SS
5523 return buf;
5524}
5525
5526/* In DWARF version 2, the description of the debugging information is
5527 stored in a separate .debug_abbrev section. Before we read any
5528 dies from a section we read in all abbreviations and install them
72bf9492
DJ
5529 in a hash table. This function also sets flags in CU describing
5530 the data found in the abbrev table. */
c906108c
SS
5531
5532static void
e7c27a73 5533dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 5534{
e7c27a73 5535 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 5536 gdb_byte *abbrev_ptr;
c906108c
SS
5537 struct abbrev_info *cur_abbrev;
5538 unsigned int abbrev_number, bytes_read, abbrev_name;
5539 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
5540 struct attr_abbrev *cur_attrs;
5541 unsigned int allocated_attrs;
c906108c 5542
57349743 5543 /* Initialize dwarf2 abbrevs */
f3dd6933
DJ
5544 obstack_init (&cu->abbrev_obstack);
5545 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
5546 (ABBREV_HASH_SIZE
5547 * sizeof (struct abbrev_info *)));
5548 memset (cu->dwarf2_abbrevs, 0,
5549 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 5550
6502dd73 5551 abbrev_ptr = dwarf2_per_objfile->abbrev_buffer + cu_header->abbrev_offset;
c906108c
SS
5552 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5553 abbrev_ptr += bytes_read;
5554
f3dd6933
DJ
5555 allocated_attrs = ATTR_ALLOC_CHUNK;
5556 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
5557
c906108c
SS
5558 /* loop until we reach an abbrev number of 0 */
5559 while (abbrev_number)
5560 {
f3dd6933 5561 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
5562
5563 /* read in abbrev header */
5564 cur_abbrev->number = abbrev_number;
5565 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5566 abbrev_ptr += bytes_read;
5567 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
5568 abbrev_ptr += 1;
5569
72bf9492
DJ
5570 if (cur_abbrev->tag == DW_TAG_namespace)
5571 cu->has_namespace_info = 1;
5572
c906108c
SS
5573 /* now read in declarations */
5574 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5575 abbrev_ptr += bytes_read;
5576 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5577 abbrev_ptr += bytes_read;
5578 while (abbrev_name)
5579 {
f3dd6933 5580 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 5581 {
f3dd6933
DJ
5582 allocated_attrs += ATTR_ALLOC_CHUNK;
5583 cur_attrs
5584 = xrealloc (cur_attrs, (allocated_attrs
5585 * sizeof (struct attr_abbrev)));
c906108c 5586 }
ae038cb0
DJ
5587
5588 /* Record whether this compilation unit might have
5589 inter-compilation-unit references. If we don't know what form
5590 this attribute will have, then it might potentially be a
5591 DW_FORM_ref_addr, so we conservatively expect inter-CU
5592 references. */
5593
5594 if (abbrev_form == DW_FORM_ref_addr
5595 || abbrev_form == DW_FORM_indirect)
5596 cu->has_form_ref_addr = 1;
5597
f3dd6933
DJ
5598 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
5599 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
5600 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5601 abbrev_ptr += bytes_read;
5602 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5603 abbrev_ptr += bytes_read;
5604 }
5605
f3dd6933
DJ
5606 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
5607 (cur_abbrev->num_attrs
5608 * sizeof (struct attr_abbrev)));
5609 memcpy (cur_abbrev->attrs, cur_attrs,
5610 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
5611
c906108c 5612 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
5613 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
5614 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
5615
5616 /* Get next abbreviation.
5617 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
5618 always properly terminated with an abbrev number of 0.
5619 Exit loop if we encounter an abbreviation which we have
5620 already read (which means we are about to read the abbreviations
5621 for the next compile unit) or if the end of the abbreviation
5622 table is reached. */
6502dd73
DJ
5623 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev_buffer)
5624 >= dwarf2_per_objfile->abbrev_size)
c906108c
SS
5625 break;
5626 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
5627 abbrev_ptr += bytes_read;
e7c27a73 5628 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
5629 break;
5630 }
f3dd6933
DJ
5631
5632 xfree (cur_attrs);
c906108c
SS
5633}
5634
f3dd6933 5635/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 5636
c906108c 5637static void
f3dd6933 5638dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 5639{
f3dd6933 5640 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 5641
f3dd6933
DJ
5642 obstack_free (&cu->abbrev_obstack, NULL);
5643 cu->dwarf2_abbrevs = NULL;
c906108c
SS
5644}
5645
5646/* Lookup an abbrev_info structure in the abbrev hash table. */
5647
5648static struct abbrev_info *
e7c27a73 5649dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
5650{
5651 unsigned int hash_number;
5652 struct abbrev_info *abbrev;
5653
5654 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 5655 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
5656
5657 while (abbrev)
5658 {
5659 if (abbrev->number == number)
5660 return abbrev;
5661 else
5662 abbrev = abbrev->next;
5663 }
5664 return NULL;
5665}
5666
72bf9492
DJ
5667/* Returns nonzero if TAG represents a type that we might generate a partial
5668 symbol for. */
5669
5670static int
5671is_type_tag_for_partial (int tag)
5672{
5673 switch (tag)
5674 {
5675#if 0
5676 /* Some types that would be reasonable to generate partial symbols for,
5677 that we don't at present. */
5678 case DW_TAG_array_type:
5679 case DW_TAG_file_type:
5680 case DW_TAG_ptr_to_member_type:
5681 case DW_TAG_set_type:
5682 case DW_TAG_string_type:
5683 case DW_TAG_subroutine_type:
5684#endif
5685 case DW_TAG_base_type:
5686 case DW_TAG_class_type:
680b30c7 5687 case DW_TAG_interface_type:
72bf9492
DJ
5688 case DW_TAG_enumeration_type:
5689 case DW_TAG_structure_type:
5690 case DW_TAG_subrange_type:
5691 case DW_TAG_typedef:
5692 case DW_TAG_union_type:
5693 return 1;
5694 default:
5695 return 0;
5696 }
5697}
5698
5699/* Load all DIEs that are interesting for partial symbols into memory. */
5700
5701static struct partial_die_info *
fe1b8b76 5702load_partial_dies (bfd *abfd, gdb_byte *info_ptr, int building_psymtab,
72bf9492
DJ
5703 struct dwarf2_cu *cu)
5704{
5705 struct partial_die_info *part_die;
5706 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
5707 struct abbrev_info *abbrev;
5708 unsigned int bytes_read;
5afb4e99 5709 unsigned int load_all = 0;
72bf9492
DJ
5710
5711 int nesting_level = 1;
5712
5713 parent_die = NULL;
5714 last_die = NULL;
5715
5afb4e99
DJ
5716 if (cu->per_cu && cu->per_cu->load_all_dies)
5717 load_all = 1;
5718
72bf9492
DJ
5719 cu->partial_dies
5720 = htab_create_alloc_ex (cu->header.length / 12,
5721 partial_die_hash,
5722 partial_die_eq,
5723 NULL,
5724 &cu->comp_unit_obstack,
5725 hashtab_obstack_allocate,
5726 dummy_obstack_deallocate);
5727
5728 part_die = obstack_alloc (&cu->comp_unit_obstack,
5729 sizeof (struct partial_die_info));
5730
5731 while (1)
5732 {
5733 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5734
5735 /* A NULL abbrev means the end of a series of children. */
5736 if (abbrev == NULL)
5737 {
5738 if (--nesting_level == 0)
5739 {
5740 /* PART_DIE was probably the last thing allocated on the
5741 comp_unit_obstack, so we could call obstack_free
5742 here. We don't do that because the waste is small,
5743 and will be cleaned up when we're done with this
5744 compilation unit. This way, we're also more robust
5745 against other users of the comp_unit_obstack. */
5746 return first_die;
5747 }
5748 info_ptr += bytes_read;
5749 last_die = parent_die;
5750 parent_die = parent_die->die_parent;
5751 continue;
5752 }
5753
5afb4e99
DJ
5754 /* Check whether this DIE is interesting enough to save. Normally
5755 we would not be interested in members here, but there may be
5756 later variables referencing them via DW_AT_specification (for
5757 static members). */
5758 if (!load_all
5759 && !is_type_tag_for_partial (abbrev->tag)
72bf9492
DJ
5760 && abbrev->tag != DW_TAG_enumerator
5761 && abbrev->tag != DW_TAG_subprogram
bc30ff58 5762 && abbrev->tag != DW_TAG_lexical_block
72bf9492 5763 && abbrev->tag != DW_TAG_variable
5afb4e99
DJ
5764 && abbrev->tag != DW_TAG_namespace
5765 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
5766 {
5767 /* Otherwise we skip to the next sibling, if any. */
5768 info_ptr = skip_one_die (info_ptr + bytes_read, abbrev, cu);
5769 continue;
5770 }
5771
5772 info_ptr = read_partial_die (part_die, abbrev, bytes_read,
5773 abfd, info_ptr, cu);
5774
5775 /* This two-pass algorithm for processing partial symbols has a
5776 high cost in cache pressure. Thus, handle some simple cases
5777 here which cover the majority of C partial symbols. DIEs
5778 which neither have specification tags in them, nor could have
5779 specification tags elsewhere pointing at them, can simply be
5780 processed and discarded.
5781
5782 This segment is also optional; scan_partial_symbols and
5783 add_partial_symbol will handle these DIEs if we chain
5784 them in normally. When compilers which do not emit large
5785 quantities of duplicate debug information are more common,
5786 this code can probably be removed. */
5787
5788 /* Any complete simple types at the top level (pretty much all
5789 of them, for a language without namespaces), can be processed
5790 directly. */
5791 if (parent_die == NULL
5792 && part_die->has_specification == 0
5793 && part_die->is_declaration == 0
5794 && (part_die->tag == DW_TAG_typedef
5795 || part_die->tag == DW_TAG_base_type
5796 || part_die->tag == DW_TAG_subrange_type))
5797 {
5798 if (building_psymtab && part_die->name != NULL)
5799 add_psymbol_to_list (part_die->name, strlen (part_die->name),
5800 VAR_DOMAIN, LOC_TYPEDEF,
5801 &cu->objfile->static_psymbols,
5802 0, (CORE_ADDR) 0, cu->language, cu->objfile);
5803 info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5804 continue;
5805 }
5806
5807 /* If we're at the second level, and we're an enumerator, and
5808 our parent has no specification (meaning possibly lives in a
5809 namespace elsewhere), then we can add the partial symbol now
5810 instead of queueing it. */
5811 if (part_die->tag == DW_TAG_enumerator
5812 && parent_die != NULL
5813 && parent_die->die_parent == NULL
5814 && parent_die->tag == DW_TAG_enumeration_type
5815 && parent_die->has_specification == 0)
5816 {
5817 if (part_die->name == NULL)
e2e0b3e5 5818 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
72bf9492
DJ
5819 else if (building_psymtab)
5820 add_psymbol_to_list (part_die->name, strlen (part_die->name),
5821 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
5822 (cu->language == language_cplus
5823 || cu->language == language_java)
72bf9492
DJ
5824 ? &cu->objfile->global_psymbols
5825 : &cu->objfile->static_psymbols,
5826 0, (CORE_ADDR) 0, cu->language, cu->objfile);
5827
5828 info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5829 continue;
5830 }
5831
5832 /* We'll save this DIE so link it in. */
5833 part_die->die_parent = parent_die;
5834 part_die->die_sibling = NULL;
5835 part_die->die_child = NULL;
5836
5837 if (last_die && last_die == parent_die)
5838 last_die->die_child = part_die;
5839 else if (last_die)
5840 last_die->die_sibling = part_die;
5841
5842 last_die = part_die;
5843
5844 if (first_die == NULL)
5845 first_die = part_die;
5846
5847 /* Maybe add the DIE to the hash table. Not all DIEs that we
5848 find interesting need to be in the hash table, because we
5849 also have the parent/sibling/child chains; only those that we
5850 might refer to by offset later during partial symbol reading.
5851
5852 For now this means things that might have be the target of a
5853 DW_AT_specification, DW_AT_abstract_origin, or
5854 DW_AT_extension. DW_AT_extension will refer only to
5855 namespaces; DW_AT_abstract_origin refers to functions (and
5856 many things under the function DIE, but we do not recurse
5857 into function DIEs during partial symbol reading) and
5858 possibly variables as well; DW_AT_specification refers to
5859 declarations. Declarations ought to have the DW_AT_declaration
5860 flag. It happens that GCC forgets to put it in sometimes, but
5861 only for functions, not for types.
5862
5863 Adding more things than necessary to the hash table is harmless
5864 except for the performance cost. Adding too few will result in
5afb4e99
DJ
5865 wasted time in find_partial_die, when we reread the compilation
5866 unit with load_all_dies set. */
72bf9492 5867
5afb4e99
DJ
5868 if (load_all
5869 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
5870 || abbrev->tag == DW_TAG_variable
5871 || abbrev->tag == DW_TAG_namespace
5872 || part_die->is_declaration)
5873 {
5874 void **slot;
5875
5876 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
5877 part_die->offset, INSERT);
5878 *slot = part_die;
5879 }
5880
5881 part_die = obstack_alloc (&cu->comp_unit_obstack,
5882 sizeof (struct partial_die_info));
5883
5884 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 5885 we have no reason to follow the children of structures; for other
72bf9492 5886 languages we have to, both so that we can get at method physnames
bc30ff58
JB
5887 to infer fully qualified class names, and for DW_AT_specification.
5888
5889 For Ada, we need to scan the children of subprograms and lexical
5890 blocks as well because Ada allows the definition of nested
5891 entities that could be interesting for the debugger, such as
5892 nested subprograms for instance. */
72bf9492 5893 if (last_die->has_children
5afb4e99
DJ
5894 && (load_all
5895 || last_die->tag == DW_TAG_namespace
72bf9492
DJ
5896 || last_die->tag == DW_TAG_enumeration_type
5897 || (cu->language != language_c
5898 && (last_die->tag == DW_TAG_class_type
680b30c7 5899 || last_die->tag == DW_TAG_interface_type
72bf9492 5900 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
5901 || last_die->tag == DW_TAG_union_type))
5902 || (cu->language == language_ada
5903 && (last_die->tag == DW_TAG_subprogram
5904 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
5905 {
5906 nesting_level++;
5907 parent_die = last_die;
5908 continue;
5909 }
5910
5911 /* Otherwise we skip to the next sibling, if any. */
5912 info_ptr = locate_pdi_sibling (last_die, info_ptr, abfd, cu);
5913
5914 /* Back to the top, do it again. */
5915 }
5916}
5917
c906108c
SS
5918/* Read a minimal amount of information into the minimal die structure. */
5919
fe1b8b76 5920static gdb_byte *
72bf9492
DJ
5921read_partial_die (struct partial_die_info *part_die,
5922 struct abbrev_info *abbrev,
5923 unsigned int abbrev_len, bfd *abfd,
fe1b8b76 5924 gdb_byte *info_ptr, struct dwarf2_cu *cu)
c906108c 5925{
72bf9492 5926 unsigned int bytes_read, i;
c906108c 5927 struct attribute attr;
c5aa993b 5928 int has_low_pc_attr = 0;
c906108c 5929 int has_high_pc_attr = 0;
e7030f15 5930 CORE_ADDR base_address = 0;
85cbf3d3
JK
5931 enum
5932 {
5933 base_address_none,
5934 base_address_low_pc,
5935 /* Overrides BASE_ADDRESS_LOW_PC. */
5936 base_address_entry_pc
5937 }
5938 base_address_type = base_address_none;
c906108c 5939
72bf9492 5940 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 5941
6502dd73 5942 part_die->offset = info_ptr - dwarf2_per_objfile->info_buffer;
72bf9492
DJ
5943
5944 info_ptr += abbrev_len;
5945
5946 if (abbrev == NULL)
5947 return info_ptr;
5948
c906108c
SS
5949 part_die->tag = abbrev->tag;
5950 part_die->has_children = abbrev->has_children;
c906108c
SS
5951
5952 for (i = 0; i < abbrev->num_attrs; ++i)
5953 {
e7c27a73 5954 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
5955
5956 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 5957 partial symbol table. */
c906108c
SS
5958 switch (attr.name)
5959 {
5960 case DW_AT_name:
71c25dea
TT
5961 switch (part_die->tag)
5962 {
5963 case DW_TAG_compile_unit:
5964 /* Compilation units have a DW_AT_name that is a filename, not
5965 a source language identifier. */
5966 case DW_TAG_enumeration_type:
5967 case DW_TAG_enumerator:
5968 /* These tags always have simple identifiers already; no need
5969 to canonicalize them. */
5970 part_die->name = DW_STRING (&attr);
5971 break;
5972 default:
5973 part_die->name
5974 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
5975 &cu->comp_unit_obstack);
5976 break;
5977 }
c906108c 5978 break;
57c22c6c
BR
5979 case DW_AT_comp_dir:
5980 if (part_die->dirname == NULL)
5981 part_die->dirname = DW_STRING (&attr);
5982 break;
c906108c
SS
5983 case DW_AT_MIPS_linkage_name:
5984 part_die->name = DW_STRING (&attr);
5985 break;
5986 case DW_AT_low_pc:
5987 has_low_pc_attr = 1;
5988 part_die->lowpc = DW_ADDR (&attr);
85cbf3d3
JK
5989 if (part_die->tag == DW_TAG_compile_unit
5990 && base_address_type < base_address_low_pc)
5991 {
5992 base_address = DW_ADDR (&attr);
5993 base_address_type = base_address_low_pc;
5994 }
c906108c
SS
5995 break;
5996 case DW_AT_high_pc:
5997 has_high_pc_attr = 1;
5998 part_die->highpc = DW_ADDR (&attr);
5999 break;
85cbf3d3
JK
6000 case DW_AT_entry_pc:
6001 if (part_die->tag == DW_TAG_compile_unit
6002 && base_address_type < base_address_entry_pc)
6003 {
6004 base_address = DW_ADDR (&attr);
6005 base_address_type = base_address_entry_pc;
6006 }
6007 break;
43039443 6008 case DW_AT_ranges:
ff013f42
JK
6009 if (part_die->tag == DW_TAG_compile_unit)
6010 {
6011 cu->ranges_offset = DW_UNSND (&attr);
6012 cu->has_ranges_offset = 1;
6013 }
43039443 6014 break;
c906108c 6015 case DW_AT_location:
8e19ed76
PS
6016 /* Support the .debug_loc offsets */
6017 if (attr_form_is_block (&attr))
6018 {
6019 part_die->locdesc = DW_BLOCK (&attr);
6020 }
3690dd37 6021 else if (attr_form_is_section_offset (&attr))
8e19ed76 6022 {
4d3c2250 6023 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
6024 }
6025 else
6026 {
4d3c2250
KB
6027 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6028 "partial symbol information");
8e19ed76 6029 }
c906108c
SS
6030 break;
6031 case DW_AT_language:
6032 part_die->language = DW_UNSND (&attr);
6033 break;
6034 case DW_AT_external:
6035 part_die->is_external = DW_UNSND (&attr);
6036 break;
6037 case DW_AT_declaration:
6038 part_die->is_declaration = DW_UNSND (&attr);
6039 break;
6040 case DW_AT_type:
6041 part_die->has_type = 1;
6042 break;
6043 case DW_AT_abstract_origin:
6044 case DW_AT_specification:
72bf9492
DJ
6045 case DW_AT_extension:
6046 part_die->has_specification = 1;
c764a876 6047 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
6048 break;
6049 case DW_AT_sibling:
6050 /* Ignore absolute siblings, they might point outside of
6051 the current compile unit. */
6052 if (attr.form == DW_FORM_ref_addr)
e2e0b3e5 6053 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
c906108c 6054 else
6502dd73 6055 part_die->sibling = dwarf2_per_objfile->info_buffer
c764a876 6056 + dwarf2_get_ref_die_offset (&attr);
c906108c 6057 break;
aaa75496
JB
6058 case DW_AT_stmt_list:
6059 part_die->has_stmt_list = 1;
6060 part_die->line_offset = DW_UNSND (&attr);
6061 break;
fa4028e9
JB
6062 case DW_AT_byte_size:
6063 part_die->has_byte_size = 1;
6064 break;
68511cec
CES
6065 case DW_AT_calling_convention:
6066 /* DWARF doesn't provide a way to identify a program's source-level
6067 entry point. DW_AT_calling_convention attributes are only meant
6068 to describe functions' calling conventions.
6069
6070 However, because it's a necessary piece of information in
6071 Fortran, and because DW_CC_program is the only piece of debugging
6072 information whose definition refers to a 'main program' at all,
6073 several compilers have begun marking Fortran main programs with
6074 DW_CC_program --- even when those functions use the standard
6075 calling conventions.
6076
6077 So until DWARF specifies a way to provide this information and
6078 compilers pick up the new representation, we'll support this
6079 practice. */
6080 if (DW_UNSND (&attr) == DW_CC_program
6081 && cu->language == language_fortran)
6082 set_main_name (part_die->name);
6083 break;
c906108c
SS
6084 default:
6085 break;
6086 }
6087 }
6088
c906108c
SS
6089 /* When using the GNU linker, .gnu.linkonce. sections are used to
6090 eliminate duplicate copies of functions and vtables and such.
6091 The linker will arbitrarily choose one and discard the others.
6092 The AT_*_pc values for such functions refer to local labels in
6093 these sections. If the section from that file was discarded, the
6094 labels are not in the output, so the relocs get a value of 0.
6095 If this is a discarded function, mark the pc bounds as invalid,
6096 so that GDB will ignore it. */
6097 if (has_low_pc_attr && has_high_pc_attr
6098 && part_die->lowpc < part_die->highpc
6099 && (part_die->lowpc != 0
72dca2f5 6100 || dwarf2_per_objfile->has_section_at_zero))
0b010bcc 6101 part_die->has_pc_info = 1;
85cbf3d3 6102
d00adf39 6103 if (base_address_type != base_address_none && !cu->base_known)
85cbf3d3
JK
6104 {
6105 gdb_assert (part_die->tag == DW_TAG_compile_unit);
d00adf39
DE
6106 cu->base_known = 1;
6107 cu->base_address = base_address;
85cbf3d3
JK
6108 }
6109
c906108c
SS
6110 return info_ptr;
6111}
6112
72bf9492
DJ
6113/* Find a cached partial DIE at OFFSET in CU. */
6114
6115static struct partial_die_info *
c764a876 6116find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
72bf9492
DJ
6117{
6118 struct partial_die_info *lookup_die = NULL;
6119 struct partial_die_info part_die;
6120
6121 part_die.offset = offset;
6122 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
6123
72bf9492
DJ
6124 return lookup_die;
6125}
6126
6127/* Find a partial DIE at OFFSET, which may or may not be in CU. */
6128
6129static struct partial_die_info *
c764a876 6130find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
72bf9492 6131{
5afb4e99
DJ
6132 struct dwarf2_per_cu_data *per_cu = NULL;
6133 struct partial_die_info *pd = NULL;
72bf9492 6134
45452591 6135 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
6136 {
6137 pd = find_partial_die_in_comp_unit (offset, cu);
6138 if (pd != NULL)
6139 return pd;
6140 }
72bf9492 6141
ae038cb0
DJ
6142 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
6143
ae038cb0
DJ
6144 if (per_cu->cu == NULL)
6145 {
6146 load_comp_unit (per_cu, cu->objfile);
6147 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6148 dwarf2_per_objfile->read_in_chain = per_cu;
6149 }
6150
6151 per_cu->cu->last_used = 0;
5afb4e99
DJ
6152 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6153
6154 if (pd == NULL && per_cu->load_all_dies == 0)
6155 {
6156 struct cleanup *back_to;
6157 struct partial_die_info comp_unit_die;
6158 struct abbrev_info *abbrev;
6159 unsigned int bytes_read;
6160 char *info_ptr;
6161
6162 per_cu->load_all_dies = 1;
6163
6164 /* Re-read the DIEs. */
6165 back_to = make_cleanup (null_cleanup, 0);
6166 if (per_cu->cu->dwarf2_abbrevs == NULL)
6167 {
6168 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
6169 back_to = make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
6170 }
d00adf39
DE
6171 info_ptr = (dwarf2_per_objfile->info_buffer
6172 + per_cu->cu->header.offset
6173 + per_cu->cu->header.first_die_offset);
5afb4e99
DJ
6174 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
6175 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
6176 per_cu->cu->objfile->obfd, info_ptr,
6177 per_cu->cu);
6178 if (comp_unit_die.has_children)
6179 load_partial_dies (per_cu->cu->objfile->obfd, info_ptr, 0, per_cu->cu);
6180 do_cleanups (back_to);
6181
6182 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6183 }
6184
6185 if (pd == NULL)
6186 internal_error (__FILE__, __LINE__,
c764a876 6187 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
5afb4e99
DJ
6188 offset, bfd_get_filename (cu->objfile->obfd));
6189 return pd;
72bf9492
DJ
6190}
6191
6192/* Adjust PART_DIE before generating a symbol for it. This function
6193 may set the is_external flag or change the DIE's name. */
6194
6195static void
6196fixup_partial_die (struct partial_die_info *part_die,
6197 struct dwarf2_cu *cu)
6198{
6199 /* If we found a reference attribute and the DIE has no name, try
6200 to find a name in the referred to DIE. */
6201
6202 if (part_die->name == NULL && part_die->has_specification)
6203 {
6204 struct partial_die_info *spec_die;
72bf9492 6205
10b3939b 6206 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 6207
10b3939b 6208 fixup_partial_die (spec_die, cu);
72bf9492
DJ
6209
6210 if (spec_die->name)
6211 {
6212 part_die->name = spec_die->name;
6213
6214 /* Copy DW_AT_external attribute if it is set. */
6215 if (spec_die->is_external)
6216 part_die->is_external = spec_die->is_external;
6217 }
6218 }
6219
6220 /* Set default names for some unnamed DIEs. */
6221 if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
6222 || part_die->tag == DW_TAG_class_type))
6223 part_die->name = "(anonymous class)";
6224
6225 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
6226 part_die->name = "(anonymous namespace)";
6227
6228 if (part_die->tag == DW_TAG_structure_type
6229 || part_die->tag == DW_TAG_class_type
6230 || part_die->tag == DW_TAG_union_type)
6231 guess_structure_name (part_die, cu);
6232}
6233
639d11d3
DC
6234/* Read the die from the .debug_info section buffer. Set DIEP to
6235 point to a newly allocated die with its information, except for its
6236 child, sibling, and parent fields. Set HAS_CHILDREN to tell
6237 whether the die has children or not. */
c906108c 6238
fe1b8b76
JB
6239static gdb_byte *
6240read_full_die (struct die_info **diep, bfd *abfd, gdb_byte *info_ptr,
e7c27a73 6241 struct dwarf2_cu *cu, int *has_children)
c906108c
SS
6242{
6243 unsigned int abbrev_number, bytes_read, i, offset;
6244 struct abbrev_info *abbrev;
6245 struct die_info *die;
6246
6502dd73 6247 offset = info_ptr - dwarf2_per_objfile->info_buffer;
c906108c
SS
6248 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6249 info_ptr += bytes_read;
6250 if (!abbrev_number)
6251 {
1d325ec1 6252 *diep = NULL;
639d11d3 6253 *has_children = 0;
c906108c
SS
6254 return info_ptr;
6255 }
6256
e7c27a73 6257 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
c906108c
SS
6258 if (!abbrev)
6259 {
8a3fe4f8 6260 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
72bf9492 6261 abbrev_number,
639d11d3 6262 bfd_get_filename (abfd));
c906108c 6263 }
b60c80d6 6264 die = dwarf_alloc_die (cu, abbrev->num_attrs);
c906108c
SS
6265 die->offset = offset;
6266 die->tag = abbrev->tag;
c906108c 6267 die->abbrev = abbrev_number;
c906108c
SS
6268
6269 die->num_attrs = abbrev->num_attrs;
c906108c
SS
6270
6271 for (i = 0; i < abbrev->num_attrs; ++i)
03dd20cc
DJ
6272 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
6273 abfd, info_ptr, cu);
c906108c
SS
6274
6275 *diep = die;
639d11d3 6276 *has_children = abbrev->has_children;
c906108c
SS
6277 return info_ptr;
6278}
6279
a8329558 6280/* Read an attribute value described by an attribute form. */
c906108c 6281
fe1b8b76 6282static gdb_byte *
a8329558 6283read_attribute_value (struct attribute *attr, unsigned form,
fe1b8b76 6284 bfd *abfd, gdb_byte *info_ptr,
e7c27a73 6285 struct dwarf2_cu *cu)
c906108c 6286{
e7c27a73 6287 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
6288 unsigned int bytes_read;
6289 struct dwarf_block *blk;
6290
a8329558
KW
6291 attr->form = form;
6292 switch (form)
c906108c
SS
6293 {
6294 case DW_FORM_addr:
6295 case DW_FORM_ref_addr:
e7c27a73 6296 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 6297 info_ptr += bytes_read;
c906108c
SS
6298 break;
6299 case DW_FORM_block2:
7b5a2f43 6300 blk = dwarf_alloc_block (cu);
c906108c
SS
6301 blk->size = read_2_bytes (abfd, info_ptr);
6302 info_ptr += 2;
6303 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6304 info_ptr += blk->size;
6305 DW_BLOCK (attr) = blk;
6306 break;
6307 case DW_FORM_block4:
7b5a2f43 6308 blk = dwarf_alloc_block (cu);
c906108c
SS
6309 blk->size = read_4_bytes (abfd, info_ptr);
6310 info_ptr += 4;
6311 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6312 info_ptr += blk->size;
6313 DW_BLOCK (attr) = blk;
6314 break;
6315 case DW_FORM_data2:
6316 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
6317 info_ptr += 2;
6318 break;
6319 case DW_FORM_data4:
6320 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
6321 info_ptr += 4;
6322 break;
6323 case DW_FORM_data8:
6324 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
6325 info_ptr += 8;
6326 break;
6327 case DW_FORM_string:
6328 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
6329 info_ptr += bytes_read;
6330 break;
4bdf3d34
JJ
6331 case DW_FORM_strp:
6332 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
6333 &bytes_read);
6334 info_ptr += bytes_read;
6335 break;
c906108c 6336 case DW_FORM_block:
7b5a2f43 6337 blk = dwarf_alloc_block (cu);
c906108c
SS
6338 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6339 info_ptr += bytes_read;
6340 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6341 info_ptr += blk->size;
6342 DW_BLOCK (attr) = blk;
6343 break;
6344 case DW_FORM_block1:
7b5a2f43 6345 blk = dwarf_alloc_block (cu);
c906108c
SS
6346 blk->size = read_1_byte (abfd, info_ptr);
6347 info_ptr += 1;
6348 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
6349 info_ptr += blk->size;
6350 DW_BLOCK (attr) = blk;
6351 break;
6352 case DW_FORM_data1:
6353 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
6354 info_ptr += 1;
6355 break;
6356 case DW_FORM_flag:
6357 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
6358 info_ptr += 1;
6359 break;
6360 case DW_FORM_sdata:
6361 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
6362 info_ptr += bytes_read;
6363 break;
6364 case DW_FORM_udata:
6365 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6366 info_ptr += bytes_read;
6367 break;
6368 case DW_FORM_ref1:
10b3939b 6369 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
c906108c
SS
6370 info_ptr += 1;
6371 break;
6372 case DW_FORM_ref2:
10b3939b 6373 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
c906108c
SS
6374 info_ptr += 2;
6375 break;
6376 case DW_FORM_ref4:
10b3939b 6377 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
c906108c
SS
6378 info_ptr += 4;
6379 break;
613e1657 6380 case DW_FORM_ref8:
10b3939b 6381 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
613e1657
KB
6382 info_ptr += 8;
6383 break;
c906108c 6384 case DW_FORM_ref_udata:
10b3939b
DJ
6385 DW_ADDR (attr) = (cu->header.offset
6386 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
6387 info_ptr += bytes_read;
6388 break;
c906108c 6389 case DW_FORM_indirect:
a8329558
KW
6390 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6391 info_ptr += bytes_read;
e7c27a73 6392 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 6393 break;
c906108c 6394 default:
8a3fe4f8 6395 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
6396 dwarf_form_name (form),
6397 bfd_get_filename (abfd));
c906108c 6398 }
28e94949
JB
6399
6400 /* We have seen instances where the compiler tried to emit a byte
6401 size attribute of -1 which ended up being encoded as an unsigned
6402 0xffffffff. Although 0xffffffff is technically a valid size value,
6403 an object of this size seems pretty unlikely so we can relatively
6404 safely treat these cases as if the size attribute was invalid and
6405 treat them as zero by default. */
6406 if (attr->name == DW_AT_byte_size
6407 && form == DW_FORM_data4
6408 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
6409 {
6410 complaint
6411 (&symfile_complaints,
6412 _("Suspicious DW_AT_byte_size value treated as zero instead of 0x%lx"),
6413 DW_UNSND (attr));
6414 DW_UNSND (attr) = 0;
6415 }
28e94949 6416
c906108c
SS
6417 return info_ptr;
6418}
6419
a8329558
KW
6420/* Read an attribute described by an abbreviated attribute. */
6421
fe1b8b76 6422static gdb_byte *
a8329558 6423read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
fe1b8b76 6424 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
6425{
6426 attr->name = abbrev->name;
e7c27a73 6427 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
6428}
6429
c906108c
SS
6430/* read dwarf information from a buffer */
6431
6432static unsigned int
fe1b8b76 6433read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 6434{
fe1b8b76 6435 return bfd_get_8 (abfd, buf);
c906108c
SS
6436}
6437
6438static int
fe1b8b76 6439read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 6440{
fe1b8b76 6441 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
6442}
6443
6444static unsigned int
fe1b8b76 6445read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6446{
fe1b8b76 6447 return bfd_get_16 (abfd, buf);
c906108c
SS
6448}
6449
6450static int
fe1b8b76 6451read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6452{
fe1b8b76 6453 return bfd_get_signed_16 (abfd, buf);
c906108c
SS
6454}
6455
6456static unsigned int
fe1b8b76 6457read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6458{
fe1b8b76 6459 return bfd_get_32 (abfd, buf);
c906108c
SS
6460}
6461
6462static int
fe1b8b76 6463read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6464{
fe1b8b76 6465 return bfd_get_signed_32 (abfd, buf);
c906108c
SS
6466}
6467
ce5d95e1 6468static unsigned long
fe1b8b76 6469read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 6470{
fe1b8b76 6471 return bfd_get_64 (abfd, buf);
c906108c
SS
6472}
6473
6474static CORE_ADDR
fe1b8b76 6475read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 6476 unsigned int *bytes_read)
c906108c 6477{
e7c27a73 6478 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
6479 CORE_ADDR retval = 0;
6480
107d2387 6481 if (cu_header->signed_addr_p)
c906108c 6482 {
107d2387
AC
6483 switch (cu_header->addr_size)
6484 {
6485 case 2:
fe1b8b76 6486 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
6487 break;
6488 case 4:
fe1b8b76 6489 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
6490 break;
6491 case 8:
fe1b8b76 6492 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
6493 break;
6494 default:
8e65ff28 6495 internal_error (__FILE__, __LINE__,
e2e0b3e5 6496 _("read_address: bad switch, signed [in module %s]"),
659b0389 6497 bfd_get_filename (abfd));
107d2387
AC
6498 }
6499 }
6500 else
6501 {
6502 switch (cu_header->addr_size)
6503 {
6504 case 2:
fe1b8b76 6505 retval = bfd_get_16 (abfd, buf);
107d2387
AC
6506 break;
6507 case 4:
fe1b8b76 6508 retval = bfd_get_32 (abfd, buf);
107d2387
AC
6509 break;
6510 case 8:
fe1b8b76 6511 retval = bfd_get_64 (abfd, buf);
107d2387
AC
6512 break;
6513 default:
8e65ff28 6514 internal_error (__FILE__, __LINE__,
e2e0b3e5 6515 _("read_address: bad switch, unsigned [in module %s]"),
659b0389 6516 bfd_get_filename (abfd));
107d2387 6517 }
c906108c 6518 }
64367e0a 6519
107d2387
AC
6520 *bytes_read = cu_header->addr_size;
6521 return retval;
c906108c
SS
6522}
6523
f7ef9339 6524/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
6525 specification allows the initial length to take up either 4 bytes
6526 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
6527 bytes describe the length and all offsets will be 8 bytes in length
6528 instead of 4.
6529
f7ef9339
KB
6530 An older, non-standard 64-bit format is also handled by this
6531 function. The older format in question stores the initial length
6532 as an 8-byte quantity without an escape value. Lengths greater
6533 than 2^32 aren't very common which means that the initial 4 bytes
6534 is almost always zero. Since a length value of zero doesn't make
6535 sense for the 32-bit format, this initial zero can be considered to
6536 be an escape value which indicates the presence of the older 64-bit
6537 format. As written, the code can't detect (old format) lengths
917c78fc
MK
6538 greater than 4GB. If it becomes necessary to handle lengths
6539 somewhat larger than 4GB, we could allow other small values (such
6540 as the non-sensical values of 1, 2, and 3) to also be used as
6541 escape values indicating the presence of the old format.
f7ef9339 6542
917c78fc
MK
6543 The value returned via bytes_read should be used to increment the
6544 relevant pointer after calling read_initial_length().
c764a876 6545
613e1657
KB
6546 [ Note: read_initial_length() and read_offset() are based on the
6547 document entitled "DWARF Debugging Information Format", revision
f7ef9339 6548 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
6549 from:
6550
f7ef9339 6551 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
613e1657
KB
6552
6553 This document is only a draft and is subject to change. (So beware.)
6554
f7ef9339 6555 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
6556 determined empirically by examining 64-bit ELF files produced by
6557 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
6558
6559 - Kevin, July 16, 2002
613e1657
KB
6560 ] */
6561
6562static LONGEST
c764a876 6563read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 6564{
fe1b8b76 6565 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 6566
dd373385 6567 if (length == 0xffffffff)
613e1657 6568 {
fe1b8b76 6569 length = bfd_get_64 (abfd, buf + 4);
613e1657 6570 *bytes_read = 12;
613e1657 6571 }
dd373385 6572 else if (length == 0)
f7ef9339 6573 {
dd373385 6574 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 6575 length = bfd_get_64 (abfd, buf);
f7ef9339 6576 *bytes_read = 8;
f7ef9339 6577 }
613e1657
KB
6578 else
6579 {
6580 *bytes_read = 4;
613e1657
KB
6581 }
6582
c764a876
DE
6583 return length;
6584}
dd373385 6585
c764a876
DE
6586/* Cover function for read_initial_length.
6587 Returns the length of the object at BUF, and stores the size of the
6588 initial length in *BYTES_READ and stores the size that offsets will be in
6589 *OFFSET_SIZE.
6590 If the initial length size is not equivalent to that specified in
6591 CU_HEADER then issue a complaint.
6592 This is useful when reading non-comp-unit headers. */
dd373385 6593
c764a876
DE
6594static LONGEST
6595read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
6596 const struct comp_unit_head *cu_header,
6597 unsigned int *bytes_read,
6598 unsigned int *offset_size)
6599{
6600 LONGEST length = read_initial_length (abfd, buf, bytes_read);
6601
6602 gdb_assert (cu_header->initial_length_size == 4
6603 || cu_header->initial_length_size == 8
6604 || cu_header->initial_length_size == 12);
6605
6606 if (cu_header->initial_length_size != *bytes_read)
6607 complaint (&symfile_complaints,
6608 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 6609
c764a876 6610 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 6611 return length;
613e1657
KB
6612}
6613
6614/* Read an offset from the data stream. The size of the offset is
917c78fc 6615 given by cu_header->offset_size. */
613e1657
KB
6616
6617static LONGEST
fe1b8b76 6618read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 6619 unsigned int *bytes_read)
c764a876
DE
6620{
6621 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
6622 *bytes_read = cu_header->offset_size;
6623 return offset;
6624}
6625
6626/* Read an offset from the data stream. */
6627
6628static LONGEST
6629read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
6630{
6631 LONGEST retval = 0;
6632
c764a876 6633 switch (offset_size)
613e1657
KB
6634 {
6635 case 4:
fe1b8b76 6636 retval = bfd_get_32 (abfd, buf);
613e1657
KB
6637 break;
6638 case 8:
fe1b8b76 6639 retval = bfd_get_64 (abfd, buf);
613e1657
KB
6640 break;
6641 default:
8e65ff28 6642 internal_error (__FILE__, __LINE__,
c764a876 6643 _("read_offset_1: bad switch [in module %s]"),
659b0389 6644 bfd_get_filename (abfd));
613e1657
KB
6645 }
6646
917c78fc 6647 return retval;
613e1657
KB
6648}
6649
fe1b8b76
JB
6650static gdb_byte *
6651read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
6652{
6653 /* If the size of a host char is 8 bits, we can return a pointer
6654 to the buffer, otherwise we have to copy the data to a buffer
6655 allocated on the temporary obstack. */
4bdf3d34 6656 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 6657 return buf;
c906108c
SS
6658}
6659
6660static char *
fe1b8b76 6661read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
6662{
6663 /* If the size of a host char is 8 bits, we can return a pointer
6664 to the string, otherwise we have to copy the string to a buffer
6665 allocated on the temporary obstack. */
4bdf3d34 6666 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
6667 if (*buf == '\0')
6668 {
6669 *bytes_read_ptr = 1;
6670 return NULL;
6671 }
fe1b8b76
JB
6672 *bytes_read_ptr = strlen ((char *) buf) + 1;
6673 return (char *) buf;
4bdf3d34
JJ
6674}
6675
6676static char *
fe1b8b76 6677read_indirect_string (bfd *abfd, gdb_byte *buf,
4bdf3d34
JJ
6678 const struct comp_unit_head *cu_header,
6679 unsigned int *bytes_read_ptr)
6680{
c764a876 6681 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
c906108c 6682
6502dd73 6683 if (dwarf2_per_objfile->str_buffer == NULL)
c906108c 6684 {
8a3fe4f8 6685 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
659b0389 6686 bfd_get_filename (abfd));
4bdf3d34 6687 return NULL;
c906108c 6688 }
6502dd73 6689 if (str_offset >= dwarf2_per_objfile->str_size)
c906108c 6690 {
8a3fe4f8 6691 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
659b0389 6692 bfd_get_filename (abfd));
c906108c
SS
6693 return NULL;
6694 }
4bdf3d34 6695 gdb_assert (HOST_CHAR_BIT == 8);
6502dd73 6696 if (dwarf2_per_objfile->str_buffer[str_offset] == '\0')
4bdf3d34 6697 return NULL;
fe1b8b76 6698 return (char *) (dwarf2_per_objfile->str_buffer + str_offset);
c906108c
SS
6699}
6700
ce5d95e1 6701static unsigned long
fe1b8b76 6702read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 6703{
ce5d95e1
JB
6704 unsigned long result;
6705 unsigned int num_read;
c906108c
SS
6706 int i, shift;
6707 unsigned char byte;
6708
6709 result = 0;
6710 shift = 0;
6711 num_read = 0;
6712 i = 0;
6713 while (1)
6714 {
fe1b8b76 6715 byte = bfd_get_8 (abfd, buf);
c906108c
SS
6716 buf++;
6717 num_read++;
ce5d95e1 6718 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
6719 if ((byte & 128) == 0)
6720 {
6721 break;
6722 }
6723 shift += 7;
6724 }
6725 *bytes_read_ptr = num_read;
6726 return result;
6727}
6728
ce5d95e1 6729static long
fe1b8b76 6730read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 6731{
ce5d95e1 6732 long result;
77e0b926 6733 int i, shift, num_read;
c906108c
SS
6734 unsigned char byte;
6735
6736 result = 0;
6737 shift = 0;
c906108c
SS
6738 num_read = 0;
6739 i = 0;
6740 while (1)
6741 {
fe1b8b76 6742 byte = bfd_get_8 (abfd, buf);
c906108c
SS
6743 buf++;
6744 num_read++;
ce5d95e1 6745 result |= ((long)(byte & 127) << shift);
c906108c
SS
6746 shift += 7;
6747 if ((byte & 128) == 0)
6748 {
6749 break;
6750 }
6751 }
77e0b926
DJ
6752 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
6753 result |= -(((long)1) << shift);
c906108c
SS
6754 *bytes_read_ptr = num_read;
6755 return result;
6756}
6757
4bb7a0a7
DJ
6758/* Return a pointer to just past the end of an LEB128 number in BUF. */
6759
fe1b8b76
JB
6760static gdb_byte *
6761skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
6762{
6763 int byte;
6764
6765 while (1)
6766 {
fe1b8b76 6767 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
6768 buf++;
6769 if ((byte & 128) == 0)
6770 return buf;
6771 }
6772}
6773
c906108c 6774static void
e142c38c 6775set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
6776{
6777 switch (lang)
6778 {
6779 case DW_LANG_C89:
6780 case DW_LANG_C:
e142c38c 6781 cu->language = language_c;
c906108c
SS
6782 break;
6783 case DW_LANG_C_plus_plus:
e142c38c 6784 cu->language = language_cplus;
c906108c
SS
6785 break;
6786 case DW_LANG_Fortran77:
6787 case DW_LANG_Fortran90:
b21b22e0 6788 case DW_LANG_Fortran95:
e142c38c 6789 cu->language = language_fortran;
c906108c
SS
6790 break;
6791 case DW_LANG_Mips_Assembler:
e142c38c 6792 cu->language = language_asm;
c906108c 6793 break;
bebd888e 6794 case DW_LANG_Java:
e142c38c 6795 cu->language = language_java;
bebd888e 6796 break;
c906108c 6797 case DW_LANG_Ada83:
8aaf0b47 6798 case DW_LANG_Ada95:
bc5f45f8
JB
6799 cu->language = language_ada;
6800 break;
72019c9c
GM
6801 case DW_LANG_Modula2:
6802 cu->language = language_m2;
6803 break;
fe8e67fd
PM
6804 case DW_LANG_Pascal83:
6805 cu->language = language_pascal;
6806 break;
22566fbd
DJ
6807 case DW_LANG_ObjC:
6808 cu->language = language_objc;
6809 break;
c906108c
SS
6810 case DW_LANG_Cobol74:
6811 case DW_LANG_Cobol85:
c906108c 6812 default:
e142c38c 6813 cu->language = language_minimal;
c906108c
SS
6814 break;
6815 }
e142c38c 6816 cu->language_defn = language_def (cu->language);
c906108c
SS
6817}
6818
6819/* Return the named attribute or NULL if not there. */
6820
6821static struct attribute *
e142c38c 6822dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c
SS
6823{
6824 unsigned int i;
6825 struct attribute *spec = NULL;
6826
6827 for (i = 0; i < die->num_attrs; ++i)
6828 {
6829 if (die->attrs[i].name == name)
10b3939b 6830 return &die->attrs[i];
c906108c
SS
6831 if (die->attrs[i].name == DW_AT_specification
6832 || die->attrs[i].name == DW_AT_abstract_origin)
6833 spec = &die->attrs[i];
6834 }
c906108c 6835
10b3939b 6836 if (spec)
f2f0e013
DJ
6837 {
6838 die = follow_die_ref (die, spec, &cu);
6839 return dwarf2_attr (die, name, cu);
6840 }
c5aa993b 6841
c906108c
SS
6842 return NULL;
6843}
6844
05cf31d1
JB
6845/* Return non-zero iff the attribute NAME is defined for the given DIE,
6846 and holds a non-zero value. This function should only be used for
6847 DW_FORM_flag attributes. */
6848
6849static int
6850dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
6851{
6852 struct attribute *attr = dwarf2_attr (die, name, cu);
6853
6854 return (attr && DW_UNSND (attr));
6855}
6856
3ca72b44 6857static int
e142c38c 6858die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 6859{
05cf31d1
JB
6860 /* A DIE is a declaration if it has a DW_AT_declaration attribute
6861 which value is non-zero. However, we have to be careful with
6862 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
6863 (via dwarf2_flag_true_p) follows this attribute. So we may
6864 end up accidently finding a declaration attribute that belongs
6865 to a different DIE referenced by the specification attribute,
6866 even though the given DIE does not have a declaration attribute. */
6867 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
6868 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
6869}
6870
63d06c5c 6871/* Return the die giving the specification for DIE, if there is
f2f0e013
DJ
6872 one. *SPEC_CU is the CU containing DIE on input, and the CU
6873 containing the return value on output. */
63d06c5c
DC
6874
6875static struct die_info *
f2f0e013 6876die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 6877{
f2f0e013
DJ
6878 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
6879 *spec_cu);
63d06c5c
DC
6880
6881 if (spec_attr == NULL)
6882 return NULL;
6883 else
f2f0e013 6884 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 6885}
c906108c 6886
debd256d
JB
6887/* Free the line_header structure *LH, and any arrays and strings it
6888 refers to. */
6889static void
6890free_line_header (struct line_header *lh)
6891{
6892 if (lh->standard_opcode_lengths)
a8bc7b56 6893 xfree (lh->standard_opcode_lengths);
debd256d
JB
6894
6895 /* Remember that all the lh->file_names[i].name pointers are
6896 pointers into debug_line_buffer, and don't need to be freed. */
6897 if (lh->file_names)
a8bc7b56 6898 xfree (lh->file_names);
debd256d
JB
6899
6900 /* Similarly for the include directory names. */
6901 if (lh->include_dirs)
a8bc7b56 6902 xfree (lh->include_dirs);
debd256d 6903
a8bc7b56 6904 xfree (lh);
debd256d
JB
6905}
6906
6907
6908/* Add an entry to LH's include directory table. */
6909static void
6910add_include_dir (struct line_header *lh, char *include_dir)
c906108c 6911{
debd256d
JB
6912 /* Grow the array if necessary. */
6913 if (lh->include_dirs_size == 0)
c5aa993b 6914 {
debd256d
JB
6915 lh->include_dirs_size = 1; /* for testing */
6916 lh->include_dirs = xmalloc (lh->include_dirs_size
6917 * sizeof (*lh->include_dirs));
6918 }
6919 else if (lh->num_include_dirs >= lh->include_dirs_size)
6920 {
6921 lh->include_dirs_size *= 2;
6922 lh->include_dirs = xrealloc (lh->include_dirs,
6923 (lh->include_dirs_size
6924 * sizeof (*lh->include_dirs)));
c5aa993b 6925 }
c906108c 6926
debd256d
JB
6927 lh->include_dirs[lh->num_include_dirs++] = include_dir;
6928}
6929
6930
6931/* Add an entry to LH's file name table. */
6932static void
6933add_file_name (struct line_header *lh,
6934 char *name,
6935 unsigned int dir_index,
6936 unsigned int mod_time,
6937 unsigned int length)
6938{
6939 struct file_entry *fe;
6940
6941 /* Grow the array if necessary. */
6942 if (lh->file_names_size == 0)
6943 {
6944 lh->file_names_size = 1; /* for testing */
6945 lh->file_names = xmalloc (lh->file_names_size
6946 * sizeof (*lh->file_names));
6947 }
6948 else if (lh->num_file_names >= lh->file_names_size)
6949 {
6950 lh->file_names_size *= 2;
6951 lh->file_names = xrealloc (lh->file_names,
6952 (lh->file_names_size
6953 * sizeof (*lh->file_names)));
6954 }
6955
6956 fe = &lh->file_names[lh->num_file_names++];
6957 fe->name = name;
6958 fe->dir_index = dir_index;
6959 fe->mod_time = mod_time;
6960 fe->length = length;
aaa75496 6961 fe->included_p = 0;
cb1df416 6962 fe->symtab = NULL;
debd256d
JB
6963}
6964
6965
6966/* Read the statement program header starting at OFFSET in
6502dd73
DJ
6967 .debug_line, according to the endianness of ABFD. Return a pointer
6968 to a struct line_header, allocated using xmalloc.
debd256d
JB
6969
6970 NOTE: the strings in the include directory and file name tables of
6971 the returned object point into debug_line_buffer, and must not be
6972 freed. */
6973static struct line_header *
6974dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 6975 struct dwarf2_cu *cu)
debd256d
JB
6976{
6977 struct cleanup *back_to;
6978 struct line_header *lh;
fe1b8b76 6979 gdb_byte *line_ptr;
c764a876 6980 unsigned int bytes_read, offset_size;
debd256d
JB
6981 int i;
6982 char *cur_dir, *cur_file;
6983
6502dd73 6984 if (dwarf2_per_objfile->line_buffer == NULL)
debd256d 6985 {
e2e0b3e5 6986 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
6987 return 0;
6988 }
6989
a738430d
MK
6990 /* Make sure that at least there's room for the total_length field.
6991 That could be 12 bytes long, but we're just going to fudge that. */
6502dd73 6992 if (offset + 4 >= dwarf2_per_objfile->line_size)
debd256d 6993 {
4d3c2250 6994 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
6995 return 0;
6996 }
6997
6998 lh = xmalloc (sizeof (*lh));
6999 memset (lh, 0, sizeof (*lh));
7000 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
7001 (void *) lh);
7002
6502dd73 7003 line_ptr = dwarf2_per_objfile->line_buffer + offset;
debd256d 7004
a738430d 7005 /* Read in the header. */
dd373385 7006 lh->total_length =
c764a876
DE
7007 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
7008 &bytes_read, &offset_size);
debd256d 7009 line_ptr += bytes_read;
6502dd73
DJ
7010 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line_buffer
7011 + dwarf2_per_objfile->line_size))
debd256d 7012 {
4d3c2250 7013 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
7014 return 0;
7015 }
7016 lh->statement_program_end = line_ptr + lh->total_length;
7017 lh->version = read_2_bytes (abfd, line_ptr);
7018 line_ptr += 2;
c764a876
DE
7019 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
7020 line_ptr += offset_size;
debd256d
JB
7021 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
7022 line_ptr += 1;
7023 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
7024 line_ptr += 1;
7025 lh->line_base = read_1_signed_byte (abfd, line_ptr);
7026 line_ptr += 1;
7027 lh->line_range = read_1_byte (abfd, line_ptr);
7028 line_ptr += 1;
7029 lh->opcode_base = read_1_byte (abfd, line_ptr);
7030 line_ptr += 1;
7031 lh->standard_opcode_lengths
fe1b8b76 7032 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
7033
7034 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
7035 for (i = 1; i < lh->opcode_base; ++i)
7036 {
7037 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
7038 line_ptr += 1;
7039 }
7040
a738430d 7041 /* Read directory table. */
debd256d
JB
7042 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7043 {
7044 line_ptr += bytes_read;
7045 add_include_dir (lh, cur_dir);
7046 }
7047 line_ptr += bytes_read;
7048
a738430d 7049 /* Read file name table. */
debd256d
JB
7050 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7051 {
7052 unsigned int dir_index, mod_time, length;
7053
7054 line_ptr += bytes_read;
7055 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7056 line_ptr += bytes_read;
7057 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7058 line_ptr += bytes_read;
7059 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7060 line_ptr += bytes_read;
7061
7062 add_file_name (lh, cur_file, dir_index, mod_time, length);
7063 }
7064 line_ptr += bytes_read;
7065 lh->statement_program_start = line_ptr;
7066
6502dd73
DJ
7067 if (line_ptr > (dwarf2_per_objfile->line_buffer
7068 + dwarf2_per_objfile->line_size))
4d3c2250 7069 complaint (&symfile_complaints,
e2e0b3e5 7070 _("line number info header doesn't fit in `.debug_line' section"));
debd256d
JB
7071
7072 discard_cleanups (back_to);
7073 return lh;
7074}
c906108c 7075
5fb290d7
DJ
7076/* This function exists to work around a bug in certain compilers
7077 (particularly GCC 2.95), in which the first line number marker of a
7078 function does not show up until after the prologue, right before
7079 the second line number marker. This function shifts ADDRESS down
7080 to the beginning of the function if necessary, and is called on
7081 addresses passed to record_line. */
7082
7083static CORE_ADDR
e142c38c 7084check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5fb290d7
DJ
7085{
7086 struct function_range *fn;
7087
7088 /* Find the function_range containing address. */
e142c38c 7089 if (!cu->first_fn)
5fb290d7
DJ
7090 return address;
7091
e142c38c
DJ
7092 if (!cu->cached_fn)
7093 cu->cached_fn = cu->first_fn;
5fb290d7 7094
e142c38c 7095 fn = cu->cached_fn;
5fb290d7
DJ
7096 while (fn)
7097 if (fn->lowpc <= address && fn->highpc > address)
7098 goto found;
7099 else
7100 fn = fn->next;
7101
e142c38c
DJ
7102 fn = cu->first_fn;
7103 while (fn && fn != cu->cached_fn)
5fb290d7
DJ
7104 if (fn->lowpc <= address && fn->highpc > address)
7105 goto found;
7106 else
7107 fn = fn->next;
7108
7109 return address;
7110
7111 found:
7112 if (fn->seen_line)
7113 return address;
7114 if (address != fn->lowpc)
4d3c2250 7115 complaint (&symfile_complaints,
e2e0b3e5 7116 _("misplaced first line number at 0x%lx for '%s'"),
4d3c2250 7117 (unsigned long) address, fn->name);
5fb290d7
DJ
7118 fn->seen_line = 1;
7119 return fn->lowpc;
7120}
7121
aaa75496
JB
7122/* Decode the Line Number Program (LNP) for the given line_header
7123 structure and CU. The actual information extracted and the type
7124 of structures created from the LNP depends on the value of PST.
7125
7126 1. If PST is NULL, then this procedure uses the data from the program
7127 to create all necessary symbol tables, and their linetables.
7128 The compilation directory of the file is passed in COMP_DIR,
7129 and must not be NULL.
7130
7131 2. If PST is not NULL, this procedure reads the program to determine
7132 the list of files included by the unit represented by PST, and
7133 builds all the associated partial symbol tables. In this case,
7134 the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
7135 is not used to compute the full name of the symtab, and therefore
7136 omitting it when building the partial symtab does not introduce
7137 the potential for inconsistency - a partial symtab and its associated
7138 symbtab having a different fullname -). */
debd256d 7139
c906108c 7140static void
debd256d 7141dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
aaa75496 7142 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 7143{
a8c50c1f 7144 gdb_byte *line_ptr, *extended_end;
fe1b8b76 7145 gdb_byte *line_end;
a8c50c1f 7146 unsigned int bytes_read, extended_len;
c906108c 7147 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
7148 CORE_ADDR baseaddr;
7149 struct objfile *objfile = cu->objfile;
aaa75496 7150 const int decode_for_pst_p = (pst != NULL);
cb1df416 7151 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
e142c38c
DJ
7152
7153 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7154
debd256d
JB
7155 line_ptr = lh->statement_program_start;
7156 line_end = lh->statement_program_end;
c906108c
SS
7157
7158 /* Read the statement sequences until there's nothing left. */
7159 while (line_ptr < line_end)
7160 {
7161 /* state machine registers */
7162 CORE_ADDR address = 0;
7163 unsigned int file = 1;
7164 unsigned int line = 1;
7165 unsigned int column = 0;
debd256d 7166 int is_stmt = lh->default_is_stmt;
c906108c
SS
7167 int basic_block = 0;
7168 int end_sequence = 0;
7169
aaa75496 7170 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 7171 {
aaa75496 7172 /* Start a subfile for the current file of the state machine. */
debd256d
JB
7173 /* lh->include_dirs and lh->file_names are 0-based, but the
7174 directory and file name numbers in the statement program
7175 are 1-based. */
7176 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 7177 char *dir = NULL;
a738430d 7178
debd256d
JB
7179 if (fe->dir_index)
7180 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
7181
7182 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
7183 }
7184
a738430d 7185 /* Decode the table. */
c5aa993b 7186 while (!end_sequence)
c906108c
SS
7187 {
7188 op_code = read_1_byte (abfd, line_ptr);
7189 line_ptr += 1;
59205f5a
JB
7190 if (line_ptr > line_end)
7191 {
7192 dwarf2_debug_line_missing_end_sequence_complaint ();
7193 break;
7194 }
9aa1fe7e 7195
debd256d 7196 if (op_code >= lh->opcode_base)
a738430d
MK
7197 {
7198 /* Special operand. */
debd256d
JB
7199 adj_opcode = op_code - lh->opcode_base;
7200 address += (adj_opcode / lh->line_range)
7201 * lh->minimum_instruction_length;
7202 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 7203 if (lh->num_file_names < file || file == 0)
25e43795
DJ
7204 dwarf2_debug_line_missing_file_complaint ();
7205 else
7206 {
7207 lh->file_names[file - 1].included_p = 1;
7208 if (!decode_for_pst_p)
7209 {
7210 if (last_subfile != current_subfile)
7211 {
7212 if (last_subfile)
7213 record_line (last_subfile, 0, address);
7214 last_subfile = current_subfile;
7215 }
7216 /* Append row to matrix using current values. */
7217 record_line (current_subfile, line,
7218 check_cu_functions (address, cu));
366da635 7219 }
25e43795 7220 }
9aa1fe7e
GK
7221 basic_block = 1;
7222 }
7223 else switch (op_code)
c906108c
SS
7224 {
7225 case DW_LNS_extended_op:
a8c50c1f 7226 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
473b7be6 7227 line_ptr += bytes_read;
a8c50c1f 7228 extended_end = line_ptr + extended_len;
c906108c
SS
7229 extended_op = read_1_byte (abfd, line_ptr);
7230 line_ptr += 1;
7231 switch (extended_op)
7232 {
7233 case DW_LNE_end_sequence:
7234 end_sequence = 1;
c906108c
SS
7235 break;
7236 case DW_LNE_set_address:
e7c27a73 7237 address = read_address (abfd, line_ptr, cu, &bytes_read);
107d2387
AC
7238 line_ptr += bytes_read;
7239 address += baseaddr;
c906108c
SS
7240 break;
7241 case DW_LNE_define_file:
debd256d
JB
7242 {
7243 char *cur_file;
7244 unsigned int dir_index, mod_time, length;
7245
7246 cur_file = read_string (abfd, line_ptr, &bytes_read);
7247 line_ptr += bytes_read;
7248 dir_index =
7249 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7250 line_ptr += bytes_read;
7251 mod_time =
7252 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7253 line_ptr += bytes_read;
7254 length =
7255 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7256 line_ptr += bytes_read;
7257 add_file_name (lh, cur_file, dir_index, mod_time, length);
7258 }
c906108c
SS
7259 break;
7260 default:
4d3c2250 7261 complaint (&symfile_complaints,
e2e0b3e5 7262 _("mangled .debug_line section"));
debd256d 7263 return;
c906108c 7264 }
a8c50c1f
DJ
7265 /* Make sure that we parsed the extended op correctly. If e.g.
7266 we expected a different address size than the producer used,
7267 we may have read the wrong number of bytes. */
7268 if (line_ptr != extended_end)
7269 {
7270 complaint (&symfile_complaints,
7271 _("mangled .debug_line section"));
7272 return;
7273 }
c906108c
SS
7274 break;
7275 case DW_LNS_copy:
59205f5a 7276 if (lh->num_file_names < file || file == 0)
25e43795
DJ
7277 dwarf2_debug_line_missing_file_complaint ();
7278 else
366da635 7279 {
25e43795
DJ
7280 lh->file_names[file - 1].included_p = 1;
7281 if (!decode_for_pst_p)
7282 {
7283 if (last_subfile != current_subfile)
7284 {
7285 if (last_subfile)
7286 record_line (last_subfile, 0, address);
7287 last_subfile = current_subfile;
7288 }
7289 record_line (current_subfile, line,
7290 check_cu_functions (address, cu));
7291 }
366da635 7292 }
c906108c
SS
7293 basic_block = 0;
7294 break;
7295 case DW_LNS_advance_pc:
debd256d 7296 address += lh->minimum_instruction_length
c906108c
SS
7297 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7298 line_ptr += bytes_read;
7299 break;
7300 case DW_LNS_advance_line:
7301 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
7302 line_ptr += bytes_read;
7303 break;
7304 case DW_LNS_set_file:
debd256d 7305 {
a738430d
MK
7306 /* The arrays lh->include_dirs and lh->file_names are
7307 0-based, but the directory and file name numbers in
7308 the statement program are 1-based. */
debd256d 7309 struct file_entry *fe;
4f1520fb 7310 char *dir = NULL;
a738430d 7311
debd256d
JB
7312 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7313 line_ptr += bytes_read;
59205f5a 7314 if (lh->num_file_names < file || file == 0)
25e43795
DJ
7315 dwarf2_debug_line_missing_file_complaint ();
7316 else
7317 {
7318 fe = &lh->file_names[file - 1];
7319 if (fe->dir_index)
7320 dir = lh->include_dirs[fe->dir_index - 1];
7321 if (!decode_for_pst_p)
7322 {
7323 last_subfile = current_subfile;
7324 dwarf2_start_subfile (fe->name, dir, comp_dir);
7325 }
7326 }
debd256d 7327 }
c906108c
SS
7328 break;
7329 case DW_LNS_set_column:
7330 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7331 line_ptr += bytes_read;
7332 break;
7333 case DW_LNS_negate_stmt:
7334 is_stmt = (!is_stmt);
7335 break;
7336 case DW_LNS_set_basic_block:
7337 basic_block = 1;
7338 break;
c2c6d25f
JM
7339 /* Add to the address register of the state machine the
7340 address increment value corresponding to special opcode
a738430d
MK
7341 255. I.e., this value is scaled by the minimum
7342 instruction length since special opcode 255 would have
7343 scaled the the increment. */
c906108c 7344 case DW_LNS_const_add_pc:
debd256d
JB
7345 address += (lh->minimum_instruction_length
7346 * ((255 - lh->opcode_base) / lh->line_range));
c906108c
SS
7347 break;
7348 case DW_LNS_fixed_advance_pc:
7349 address += read_2_bytes (abfd, line_ptr);
7350 line_ptr += 2;
7351 break;
9aa1fe7e 7352 default:
a738430d
MK
7353 {
7354 /* Unknown standard opcode, ignore it. */
9aa1fe7e 7355 int i;
a738430d 7356
debd256d 7357 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
7358 {
7359 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7360 line_ptr += bytes_read;
7361 }
7362 }
c906108c
SS
7363 }
7364 }
59205f5a
JB
7365 if (lh->num_file_names < file || file == 0)
7366 dwarf2_debug_line_missing_file_complaint ();
7367 else
7368 {
7369 lh->file_names[file - 1].included_p = 1;
7370 if (!decode_for_pst_p)
7371 record_line (current_subfile, 0, address);
7372 }
c906108c 7373 }
aaa75496
JB
7374
7375 if (decode_for_pst_p)
7376 {
7377 int file_index;
7378
7379 /* Now that we're done scanning the Line Header Program, we can
7380 create the psymtab of each included file. */
7381 for (file_index = 0; file_index < lh->num_file_names; file_index++)
7382 if (lh->file_names[file_index].included_p == 1)
7383 {
5b5464ad
JB
7384 const struct file_entry fe = lh->file_names [file_index];
7385 char *include_name = fe.name;
7386 char *dir_name = NULL;
7387 char *pst_filename = pst->filename;
7388
7389 if (fe.dir_index)
7390 dir_name = lh->include_dirs[fe.dir_index - 1];
7391
7392 if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
7393 {
1754f103
MK
7394 include_name = concat (dir_name, SLASH_STRING,
7395 include_name, (char *)NULL);
5b5464ad
JB
7396 make_cleanup (xfree, include_name);
7397 }
7398
7399 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
7400 {
1754f103
MK
7401 pst_filename = concat (pst->dirname, SLASH_STRING,
7402 pst_filename, (char *)NULL);
5b5464ad
JB
7403 make_cleanup (xfree, pst_filename);
7404 }
7405
7406 if (strcmp (include_name, pst_filename) != 0)
aaa75496
JB
7407 dwarf2_create_include_psymtab (include_name, pst, objfile);
7408 }
7409 }
cb1df416
DJ
7410 else
7411 {
7412 /* Make sure a symtab is created for every file, even files
7413 which contain only variables (i.e. no code with associated
7414 line numbers). */
7415
7416 int i;
7417 struct file_entry *fe;
7418
7419 for (i = 0; i < lh->num_file_names; i++)
7420 {
7421 char *dir = NULL;
7422 fe = &lh->file_names[i];
7423 if (fe->dir_index)
7424 dir = lh->include_dirs[fe->dir_index - 1];
7425 dwarf2_start_subfile (fe->name, dir, comp_dir);
7426
7427 /* Skip the main file; we don't need it, and it must be
7428 allocated last, so that it will show up before the
7429 non-primary symtabs in the objfile's symtab list. */
7430 if (current_subfile == first_subfile)
7431 continue;
7432
7433 if (current_subfile->symtab == NULL)
7434 current_subfile->symtab = allocate_symtab (current_subfile->name,
7435 cu->objfile);
7436 fe->symtab = current_subfile->symtab;
7437 }
7438 }
c906108c
SS
7439}
7440
7441/* Start a subfile for DWARF. FILENAME is the name of the file and
7442 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
7443 or NULL if not known. COMP_DIR is the compilation directory for the
7444 linetable's compilation unit or NULL if not known.
c906108c
SS
7445 This routine tries to keep line numbers from identical absolute and
7446 relative file names in a common subfile.
7447
7448 Using the `list' example from the GDB testsuite, which resides in
7449 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
7450 of /srcdir/list0.c yields the following debugging information for list0.c:
7451
c5aa993b
JM
7452 DW_AT_name: /srcdir/list0.c
7453 DW_AT_comp_dir: /compdir
357e46e7 7454 files.files[0].name: list0.h
c5aa993b 7455 files.files[0].dir: /srcdir
357e46e7 7456 files.files[1].name: list0.c
c5aa993b 7457 files.files[1].dir: /srcdir
c906108c
SS
7458
7459 The line number information for list0.c has to end up in a single
4f1520fb
FR
7460 subfile, so that `break /srcdir/list0.c:1' works as expected.
7461 start_subfile will ensure that this happens provided that we pass the
7462 concatenation of files.files[1].dir and files.files[1].name as the
7463 subfile's name. */
c906108c
SS
7464
7465static void
4f1520fb 7466dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
c906108c 7467{
4f1520fb
FR
7468 char *fullname;
7469
7470 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
7471 `start_symtab' will always pass the contents of DW_AT_comp_dir as
7472 second argument to start_subfile. To be consistent, we do the
7473 same here. In order not to lose the line information directory,
7474 we concatenate it to the filename when it makes sense.
7475 Note that the Dwarf3 standard says (speaking of filenames in line
7476 information): ``The directory index is ignored for file names
7477 that represent full path names''. Thus ignoring dirname in the
7478 `else' branch below isn't an issue. */
c906108c 7479
d5166ae1 7480 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
7481 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
7482 else
7483 fullname = filename;
c906108c 7484
4f1520fb
FR
7485 start_subfile (fullname, comp_dir);
7486
7487 if (fullname != filename)
7488 xfree (fullname);
c906108c
SS
7489}
7490
4c2df51b
DJ
7491static void
7492var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 7493 struct dwarf2_cu *cu)
4c2df51b 7494{
e7c27a73
DJ
7495 struct objfile *objfile = cu->objfile;
7496 struct comp_unit_head *cu_header = &cu->header;
7497
4c2df51b
DJ
7498 /* NOTE drow/2003-01-30: There used to be a comment and some special
7499 code here to turn a symbol with DW_AT_external and a
7500 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
7501 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
7502 with some versions of binutils) where shared libraries could have
7503 relocations against symbols in their debug information - the
7504 minimal symbol would have the right address, but the debug info
7505 would not. It's no longer necessary, because we will explicitly
7506 apply relocations when we read in the debug information now. */
7507
7508 /* A DW_AT_location attribute with no contents indicates that a
7509 variable has been optimized away. */
7510 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
7511 {
7512 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
7513 return;
7514 }
7515
7516 /* Handle one degenerate form of location expression specially, to
7517 preserve GDB's previous behavior when section offsets are
7518 specified. If this is just a DW_OP_addr then mark this symbol
7519 as LOC_STATIC. */
7520
7521 if (attr_form_is_block (attr)
7522 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
7523 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
7524 {
891d2f0b 7525 unsigned int dummy;
4c2df51b
DJ
7526
7527 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 7528 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
907fc202 7529 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
7530 fixup_symbol_section (sym, objfile);
7531 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
7532 SYMBOL_SECTION (sym));
4c2df51b
DJ
7533 return;
7534 }
7535
7536 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
7537 expression evaluator, and use LOC_COMPUTED only when necessary
7538 (i.e. when the value of a register or memory location is
7539 referenced, or a thread-local block, etc.). Then again, it might
7540 not be worthwhile. I'm assuming that it isn't unless performance
7541 or memory numbers show me otherwise. */
7542
e7c27a73 7543 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
7544 SYMBOL_CLASS (sym) = LOC_COMPUTED;
7545}
7546
c906108c
SS
7547/* Given a pointer to a DWARF information entry, figure out if we need
7548 to make a symbol table entry for it, and if so, create a new entry
7549 and return a pointer to it.
7550 If TYPE is NULL, determine symbol type from the die, otherwise
2df3850c 7551 used the passed type. */
c906108c
SS
7552
7553static struct symbol *
e7c27a73 7554new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
c906108c 7555{
e7c27a73 7556 struct objfile *objfile = cu->objfile;
5e2b427d 7557 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
7558 struct symbol *sym = NULL;
7559 char *name;
7560 struct attribute *attr = NULL;
7561 struct attribute *attr2 = NULL;
e142c38c
DJ
7562 CORE_ADDR baseaddr;
7563
7564 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 7565
5c4e30ca 7566 if (die->tag != DW_TAG_namespace)
e142c38c 7567 name = dwarf2_linkage_name (die, cu);
5c4e30ca
DC
7568 else
7569 name = TYPE_NAME (type);
7570
c906108c
SS
7571 if (name)
7572 {
4a146b47 7573 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
c906108c
SS
7574 sizeof (struct symbol));
7575 OBJSTAT (objfile, n_syms++);
7576 memset (sym, 0, sizeof (struct symbol));
2de7ced7
DJ
7577
7578 /* Cache this symbol's name and the name's demangled form (if any). */
e142c38c 7579 SYMBOL_LANGUAGE (sym) = cu->language;
2de7ced7 7580 SYMBOL_SET_NAMES (sym, name, strlen (name), objfile);
c906108c
SS
7581
7582 /* Default assumptions.
c5aa993b 7583 Use the passed type or decode it from the die. */
176620f1 7584 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 7585 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
7586 if (type != NULL)
7587 SYMBOL_TYPE (sym) = type;
7588 else
e7c27a73 7589 SYMBOL_TYPE (sym) = die_type (die, cu);
e142c38c 7590 attr = dwarf2_attr (die, DW_AT_decl_line, cu);
c906108c
SS
7591 if (attr)
7592 {
7593 SYMBOL_LINE (sym) = DW_UNSND (attr);
7594 }
cb1df416
DJ
7595
7596 attr = dwarf2_attr (die, DW_AT_decl_file, cu);
7597 if (attr)
7598 {
7599 int file_index = DW_UNSND (attr);
7600 if (cu->line_header == NULL
7601 || file_index > cu->line_header->num_file_names)
7602 complaint (&symfile_complaints,
7603 _("file index out of range"));
1c3d648d 7604 else if (file_index > 0)
cb1df416
DJ
7605 {
7606 struct file_entry *fe;
7607 fe = &cu->line_header->file_names[file_index - 1];
7608 SYMBOL_SYMTAB (sym) = fe->symtab;
7609 }
7610 }
7611
c906108c
SS
7612 switch (die->tag)
7613 {
7614 case DW_TAG_label:
e142c38c 7615 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
7616 if (attr)
7617 {
7618 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
7619 }
7620 SYMBOL_CLASS (sym) = LOC_LABEL;
7621 break;
7622 case DW_TAG_subprogram:
7623 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
7624 finish_block. */
7625 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 7626 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
7627 if ((attr2 && (DW_UNSND (attr2) != 0))
7628 || cu->language == language_ada)
c906108c 7629 {
2cfa0c8d
JB
7630 /* Subprograms marked external are stored as a global symbol.
7631 Ada subprograms, whether marked external or not, are always
7632 stored as a global symbol, because we want to be able to
7633 access them globally. For instance, we want to be able
7634 to break on a nested subprogram without having to
7635 specify the context. */
c906108c
SS
7636 add_symbol_to_list (sym, &global_symbols);
7637 }
7638 else
7639 {
e142c38c 7640 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7641 }
7642 break;
7643 case DW_TAG_variable:
7644 /* Compilation with minimal debug info may result in variables
7645 with missing type entries. Change the misleading `void' type
7646 to something sensible. */
7647 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 7648 SYMBOL_TYPE (sym)
5e2b427d 7649 = builtin_type (gdbarch)->nodebug_data_symbol;
64c50499 7650
e142c38c 7651 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
7652 if (attr)
7653 {
e7c27a73 7654 dwarf2_const_value (attr, sym, cu);
e142c38c 7655 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c
SS
7656 if (attr2 && (DW_UNSND (attr2) != 0))
7657 add_symbol_to_list (sym, &global_symbols);
7658 else
e142c38c 7659 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7660 break;
7661 }
e142c38c 7662 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7663 if (attr)
7664 {
e7c27a73 7665 var_decode_location (attr, sym, cu);
e142c38c 7666 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 7667 if (attr2 && (DW_UNSND (attr2) != 0))
4c2df51b 7668 add_symbol_to_list (sym, &global_symbols);
c906108c 7669 else
e142c38c 7670 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7671 }
7672 else
7673 {
7674 /* We do not know the address of this symbol.
c5aa993b
JM
7675 If it is an external symbol and we have type information
7676 for it, enter the symbol as a LOC_UNRESOLVED symbol.
7677 The address of the variable will then be determined from
7678 the minimal symbol table whenever the variable is
7679 referenced. */
e142c38c 7680 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 7681 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 7682 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c
SS
7683 {
7684 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
de40b933 7685 add_symbol_to_list (sym, cu->list_in_scope);
c906108c 7686 }
442ddf59
JK
7687 else if (!die_is_declaration (die, cu))
7688 {
7689 /* Use the default LOC_OPTIMIZED_OUT class. */
7690 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
7691 add_symbol_to_list (sym, cu->list_in_scope);
7692 }
c906108c
SS
7693 }
7694 break;
7695 case DW_TAG_formal_parameter:
2a2d4dc3 7696 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 7697 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
7698 if (attr)
7699 {
e7c27a73 7700 var_decode_location (attr, sym, cu);
c906108c 7701 }
e142c38c 7702 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
7703 if (attr)
7704 {
e7c27a73 7705 dwarf2_const_value (attr, sym, cu);
c906108c 7706 }
e142c38c 7707 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7708 break;
7709 case DW_TAG_unspecified_parameters:
7710 /* From varargs functions; gdb doesn't seem to have any
7711 interest in this information, so just ignore it for now.
7712 (FIXME?) */
7713 break;
7714 case DW_TAG_class_type:
680b30c7 7715 case DW_TAG_interface_type:
c906108c
SS
7716 case DW_TAG_structure_type:
7717 case DW_TAG_union_type:
72019c9c 7718 case DW_TAG_set_type:
c906108c
SS
7719 case DW_TAG_enumeration_type:
7720 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 7721 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 7722
63d06c5c
DC
7723 /* Make sure that the symbol includes appropriate enclosing
7724 classes/namespaces in its name. These are calculated in
134d01f1 7725 read_structure_type, and the correct name is saved in
63d06c5c
DC
7726 the type. */
7727
987504bb
JJ
7728 if (cu->language == language_cplus
7729 || cu->language == language_java)
c906108c 7730 {
63d06c5c
DC
7731 struct type *type = SYMBOL_TYPE (sym);
7732
7733 if (TYPE_TAG_NAME (type) != NULL)
7734 {
7735 /* FIXME: carlton/2003-11-10: Should this use
7736 SYMBOL_SET_NAMES instead? (The same problem also
d8151005
DJ
7737 arises further down in this function.) */
7738 /* The type's name is already allocated along with
7739 this objfile, so we don't need to duplicate it
7740 for the symbol. */
7741 SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
63d06c5c 7742 }
c906108c 7743 }
63d06c5c
DC
7744
7745 {
987504bb 7746 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
7747 really ever be static objects: otherwise, if you try
7748 to, say, break of a class's method and you're in a file
7749 which doesn't mention that class, it won't work unless
7750 the check for all static symbols in lookup_symbol_aux
7751 saves you. See the OtherFileClass tests in
7752 gdb.c++/namespace.exp. */
7753
7754 struct pending **list_to_add;
7755
e142c38c 7756 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
7757 && (cu->language == language_cplus
7758 || cu->language == language_java)
e142c38c 7759 ? &global_symbols : cu->list_in_scope);
63d06c5c
DC
7760
7761 add_symbol_to_list (sym, list_to_add);
7762
7763 /* The semantics of C++ state that "struct foo { ... }" also
987504bb 7764 defines a typedef for "foo". A Java class declaration also
5eeb2539 7765 defines a typedef for the class. */
987504bb 7766 if (cu->language == language_cplus
8c6860bb
JB
7767 || cu->language == language_java
7768 || cu->language == language_ada)
63d06c5c 7769 {
d8151005
DJ
7770 /* The symbol's name is already allocated along with
7771 this objfile, so we don't need to duplicate it for
7772 the type. */
63d06c5c 7773 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
77ef991d 7774 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
63d06c5c
DC
7775 }
7776 }
c906108c
SS
7777 break;
7778 case DW_TAG_typedef:
0114d602 7779 SYMBOL_LINKAGE_NAME (sym) = (char *) dwarf2_full_name (die, cu);
63d06c5c
DC
7780 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
7781 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 7782 add_symbol_to_list (sym, cu->list_in_scope);
63d06c5c 7783 break;
c906108c 7784 case DW_TAG_base_type:
a02abb62 7785 case DW_TAG_subrange_type:
c906108c 7786 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 7787 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e142c38c 7788 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
7789 break;
7790 case DW_TAG_enumerator:
0114d602 7791 SYMBOL_LINKAGE_NAME (sym) = (char *) dwarf2_full_name (die, cu);
e142c38c 7792 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
7793 if (attr)
7794 {
e7c27a73 7795 dwarf2_const_value (attr, sym, cu);
c906108c 7796 }
63d06c5c
DC
7797 {
7798 /* NOTE: carlton/2003-11-10: See comment above in the
7799 DW_TAG_class_type, etc. block. */
7800
7801 struct pending **list_to_add;
7802
e142c38c 7803 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
7804 && (cu->language == language_cplus
7805 || cu->language == language_java)
e142c38c 7806 ? &global_symbols : cu->list_in_scope);
63d06c5c
DC
7807
7808 add_symbol_to_list (sym, list_to_add);
7809 }
c906108c 7810 break;
5c4e30ca
DC
7811 case DW_TAG_namespace:
7812 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
7813 add_symbol_to_list (sym, &global_symbols);
7814 break;
c906108c
SS
7815 default:
7816 /* Not a tag we recognize. Hopefully we aren't processing
7817 trash data, but since we must specifically ignore things
7818 we don't recognize, there is nothing else we should do at
7819 this point. */
e2e0b3e5 7820 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 7821 dwarf_tag_name (die->tag));
c906108c
SS
7822 break;
7823 }
df8a16a1
DJ
7824
7825 /* For the benefit of old versions of GCC, check for anonymous
7826 namespaces based on the demangled name. */
7827 if (!processing_has_namespace_info
7828 && cu->language == language_cplus
7829 && dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu) != NULL)
7830 cp_scan_for_anonymous_namespaces (sym);
c906108c
SS
7831 }
7832 return (sym);
7833}
7834
7835/* Copy constant value from an attribute to a symbol. */
7836
7837static void
107d2387 7838dwarf2_const_value (struct attribute *attr, struct symbol *sym,
e7c27a73 7839 struct dwarf2_cu *cu)
c906108c 7840{
e7c27a73
DJ
7841 struct objfile *objfile = cu->objfile;
7842 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
7843 struct dwarf_block *blk;
7844
7845 switch (attr->form)
7846 {
7847 case DW_FORM_addr:
107d2387 7848 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
3567439c 7849 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
7850 cu_header->addr_size,
7851 TYPE_LENGTH (SYMBOL_TYPE
7852 (sym)));
4e38b386 7853 SYMBOL_VALUE_BYTES (sym) =
4a146b47 7854 obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
fbd9dcd3
AC
7855 /* NOTE: cagney/2003-05-09: In-lined store_address call with
7856 it's body - store_unsigned_integer. */
7857 store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
7858 DW_ADDR (attr));
c906108c
SS
7859 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
7860 break;
93b5768b
PA
7861 case DW_FORM_strp:
7862 /* DW_STRING is already allocated on the obstack, point directly
7863 to it. */
7864 SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
7865 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
7866 break;
c906108c
SS
7867 case DW_FORM_block1:
7868 case DW_FORM_block2:
7869 case DW_FORM_block4:
7870 case DW_FORM_block:
7871 blk = DW_BLOCK (attr);
7872 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
3567439c 7873 dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
4d3c2250
KB
7874 blk->size,
7875 TYPE_LENGTH (SYMBOL_TYPE
7876 (sym)));
4e38b386 7877 SYMBOL_VALUE_BYTES (sym) =
4a146b47 7878 obstack_alloc (&objfile->objfile_obstack, blk->size);
c906108c
SS
7879 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
7880 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
7881 break;
2df3850c
JM
7882
7883 /* The DW_AT_const_value attributes are supposed to carry the
7884 symbol's value "represented as it would be on the target
7885 architecture." By the time we get here, it's already been
7886 converted to host endianness, so we just need to sign- or
7887 zero-extend it as appropriate. */
7888 case DW_FORM_data1:
7889 dwarf2_const_value_data (attr, sym, 8);
7890 break;
c906108c 7891 case DW_FORM_data2:
2df3850c
JM
7892 dwarf2_const_value_data (attr, sym, 16);
7893 break;
c906108c 7894 case DW_FORM_data4:
2df3850c
JM
7895 dwarf2_const_value_data (attr, sym, 32);
7896 break;
c906108c 7897 case DW_FORM_data8:
2df3850c
JM
7898 dwarf2_const_value_data (attr, sym, 64);
7899 break;
7900
c906108c 7901 case DW_FORM_sdata:
2df3850c
JM
7902 SYMBOL_VALUE (sym) = DW_SND (attr);
7903 SYMBOL_CLASS (sym) = LOC_CONST;
7904 break;
7905
c906108c
SS
7906 case DW_FORM_udata:
7907 SYMBOL_VALUE (sym) = DW_UNSND (attr);
7908 SYMBOL_CLASS (sym) = LOC_CONST;
7909 break;
2df3850c 7910
c906108c 7911 default:
4d3c2250 7912 complaint (&symfile_complaints,
e2e0b3e5 7913 _("unsupported const value attribute form: '%s'"),
4d3c2250 7914 dwarf_form_name (attr->form));
c906108c
SS
7915 SYMBOL_VALUE (sym) = 0;
7916 SYMBOL_CLASS (sym) = LOC_CONST;
7917 break;
7918 }
7919}
7920
2df3850c
JM
7921
7922/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
7923 or zero-extend it as appropriate for the symbol's type. */
7924static void
7925dwarf2_const_value_data (struct attribute *attr,
7926 struct symbol *sym,
7927 int bits)
7928{
7929 LONGEST l = DW_UNSND (attr);
7930
7931 if (bits < sizeof (l) * 8)
7932 {
7933 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
7934 l &= ((LONGEST) 1 << bits) - 1;
7935 else
bf9198f1 7936 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
2df3850c
JM
7937 }
7938
7939 SYMBOL_VALUE (sym) = l;
7940 SYMBOL_CLASS (sym) = LOC_CONST;
7941}
7942
7943
c906108c
SS
7944/* Return the type of the die in question using its DW_AT_type attribute. */
7945
7946static struct type *
e7c27a73 7947die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7948{
5e2b427d 7949 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
c906108c
SS
7950 struct type *type;
7951 struct attribute *type_attr;
7952 struct die_info *type_die;
c906108c 7953
e142c38c 7954 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
7955 if (!type_attr)
7956 {
7957 /* A missing DW_AT_type represents a void type. */
5e2b427d 7958 return builtin_type (gdbarch)->builtin_void;
c906108c
SS
7959 }
7960 else
f2f0e013 7961 type_die = follow_die_ref (die, type_attr, &cu);
10b3939b 7962
e7c27a73 7963 type = tag_type_to_type (type_die, cu);
c906108c
SS
7964 if (!type)
7965 {
d97bc12b 7966 dump_die_for_error (type_die);
8a3fe4f8 7967 error (_("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]"),
e7c27a73 7968 cu->objfile->name);
c906108c
SS
7969 }
7970 return type;
7971}
7972
7973/* Return the containing type of the die in question using its
7974 DW_AT_containing_type attribute. */
7975
7976static struct type *
e7c27a73 7977die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
7978{
7979 struct type *type = NULL;
7980 struct attribute *type_attr;
7981 struct die_info *type_die = NULL;
c906108c 7982
e142c38c 7983 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
c906108c
SS
7984 if (type_attr)
7985 {
f2f0e013 7986 type_die = follow_die_ref (die, type_attr, &cu);
e7c27a73 7987 type = tag_type_to_type (type_die, cu);
c906108c
SS
7988 }
7989 if (!type)
7990 {
7991 if (type_die)
d97bc12b 7992 dump_die_for_error (type_die);
8a3fe4f8 7993 error (_("Dwarf Error: Problem turning containing type into gdb type [in module %s]"),
e7c27a73 7994 cu->objfile->name);
c906108c
SS
7995 }
7996 return type;
7997}
7998
c906108c 7999static struct type *
e7c27a73 8000tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8001{
f792889a
DJ
8002 struct type *this_type;
8003
8004 this_type = read_type_die (die, cu);
8005 if (!this_type)
c906108c 8006 {
d97bc12b 8007 dump_die_for_error (die);
f792889a
DJ
8008 error (_("Dwarf Error: Cannot find type of die [in module %s]"),
8009 cu->objfile->name);
c906108c 8010 }
f792889a 8011 return this_type;
c906108c
SS
8012}
8013
f792889a 8014static struct type *
e7c27a73 8015read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8016{
f792889a
DJ
8017 struct type *this_type;
8018
8019 this_type = get_die_type (die, cu);
8020 if (this_type)
8021 return this_type;
8022
c906108c
SS
8023 switch (die->tag)
8024 {
8025 case DW_TAG_class_type:
680b30c7 8026 case DW_TAG_interface_type:
c906108c
SS
8027 case DW_TAG_structure_type:
8028 case DW_TAG_union_type:
f792889a 8029 this_type = read_structure_type (die, cu);
c906108c
SS
8030 break;
8031 case DW_TAG_enumeration_type:
f792889a 8032 this_type = read_enumeration_type (die, cu);
c906108c
SS
8033 break;
8034 case DW_TAG_subprogram:
8035 case DW_TAG_subroutine_type:
f792889a 8036 this_type = read_subroutine_type (die, cu);
c906108c
SS
8037 break;
8038 case DW_TAG_array_type:
f792889a 8039 this_type = read_array_type (die, cu);
c906108c 8040 break;
72019c9c 8041 case DW_TAG_set_type:
f792889a 8042 this_type = read_set_type (die, cu);
72019c9c 8043 break;
c906108c 8044 case DW_TAG_pointer_type:
f792889a 8045 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
8046 break;
8047 case DW_TAG_ptr_to_member_type:
f792889a 8048 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
8049 break;
8050 case DW_TAG_reference_type:
f792889a 8051 this_type = read_tag_reference_type (die, cu);
c906108c
SS
8052 break;
8053 case DW_TAG_const_type:
f792889a 8054 this_type = read_tag_const_type (die, cu);
c906108c
SS
8055 break;
8056 case DW_TAG_volatile_type:
f792889a 8057 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
8058 break;
8059 case DW_TAG_string_type:
f792889a 8060 this_type = read_tag_string_type (die, cu);
c906108c
SS
8061 break;
8062 case DW_TAG_typedef:
f792889a 8063 this_type = read_typedef (die, cu);
c906108c 8064 break;
a02abb62 8065 case DW_TAG_subrange_type:
f792889a 8066 this_type = read_subrange_type (die, cu);
a02abb62 8067 break;
c906108c 8068 case DW_TAG_base_type:
f792889a 8069 this_type = read_base_type (die, cu);
c906108c 8070 break;
81a17f79 8071 case DW_TAG_unspecified_type:
f792889a 8072 this_type = read_unspecified_type (die, cu);
81a17f79 8073 break;
0114d602
DJ
8074 case DW_TAG_namespace:
8075 this_type = read_namespace_type (die, cu);
8076 break;
c906108c 8077 default:
a1f5b845 8078 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
4d3c2250 8079 dwarf_tag_name (die->tag));
c906108c
SS
8080 break;
8081 }
63d06c5c 8082
f792889a 8083 return this_type;
63d06c5c
DC
8084}
8085
fdde2d81 8086/* Return the name of the namespace/class that DIE is defined within,
0114d602 8087 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 8088
0114d602
DJ
8089 For example, if we're within the method foo() in the following
8090 code:
8091
8092 namespace N {
8093 class C {
8094 void foo () {
8095 }
8096 };
8097 }
8098
8099 then determine_prefix on foo's die will return "N::C". */
fdde2d81
DC
8100
8101static char *
e142c38c 8102determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 8103{
0114d602
DJ
8104 struct die_info *parent, *spec_die;
8105 struct dwarf2_cu *spec_cu;
8106 struct type *parent_type;
63d06c5c 8107
987504bb
JJ
8108 if (cu->language != language_cplus
8109 && cu->language != language_java)
0114d602
DJ
8110 return "";
8111
8112 /* We have to be careful in the presence of DW_AT_specification.
8113 For example, with GCC 3.4, given the code
8114
8115 namespace N {
8116 void foo() {
8117 // Definition of N::foo.
8118 }
8119 }
8120
8121 then we'll have a tree of DIEs like this:
8122
8123 1: DW_TAG_compile_unit
8124 2: DW_TAG_namespace // N
8125 3: DW_TAG_subprogram // declaration of N::foo
8126 4: DW_TAG_subprogram // definition of N::foo
8127 DW_AT_specification // refers to die #3
8128
8129 Thus, when processing die #4, we have to pretend that we're in
8130 the context of its DW_AT_specification, namely the contex of die
8131 #3. */
8132 spec_cu = cu;
8133 spec_die = die_specification (die, &spec_cu);
8134 if (spec_die == NULL)
8135 parent = die->parent;
8136 else
63d06c5c 8137 {
0114d602
DJ
8138 parent = spec_die->parent;
8139 cu = spec_cu;
63d06c5c 8140 }
0114d602
DJ
8141
8142 if (parent == NULL)
8143 return "";
63d06c5c 8144 else
0114d602
DJ
8145 switch (parent->tag)
8146 {
63d06c5c 8147 case DW_TAG_namespace:
0114d602
DJ
8148 parent_type = read_type_die (parent, cu);
8149 /* We give a name to even anonymous namespaces. */
8150 return TYPE_TAG_NAME (parent_type);
63d06c5c 8151 case DW_TAG_class_type:
680b30c7 8152 case DW_TAG_interface_type:
63d06c5c 8153 case DW_TAG_structure_type:
0114d602
DJ
8154 case DW_TAG_union_type:
8155 parent_type = read_type_die (parent, cu);
8156 if (TYPE_TAG_NAME (parent_type) != NULL)
8157 return TYPE_TAG_NAME (parent_type);
8158 else
8159 /* An anonymous structure is only allowed non-static data
8160 members; no typedefs, no member functions, et cetera.
8161 So it does not need a prefix. */
8162 return "";
63d06c5c 8163 default:
8176b9b8 8164 return determine_prefix (parent, cu);
63d06c5c 8165 }
63d06c5c
DC
8166}
8167
987504bb
JJ
8168/* Return a newly-allocated string formed by concatenating PREFIX and
8169 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
8170 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
8171 perform an obconcat, otherwise allocate storage for the result. The CU argument
8172 is used to determine the language and hence, the appropriate separator. */
8173
8174#define MAX_SEP_LEN 2 /* sizeof ("::") */
63d06c5c
DC
8175
8176static char *
987504bb
JJ
8177typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
8178 struct dwarf2_cu *cu)
63d06c5c 8179{
987504bb 8180 char *sep;
63d06c5c 8181
987504bb
JJ
8182 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
8183 sep = "";
8184 else if (cu->language == language_java)
8185 sep = ".";
8186 else
8187 sep = "::";
63d06c5c 8188
6dd47d34
DE
8189 if (prefix == NULL)
8190 prefix = "";
8191 if (suffix == NULL)
8192 suffix = "";
8193
987504bb
JJ
8194 if (obs == NULL)
8195 {
8196 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
6dd47d34
DE
8197 strcpy (retval, prefix);
8198 strcat (retval, sep);
8199 strcat (retval, suffix);
63d06c5c
DC
8200 return retval;
8201 }
987504bb
JJ
8202 else
8203 {
8204 /* We have an obstack. */
8205 return obconcat (obs, prefix, sep, suffix);
8206 }
63d06c5c
DC
8207}
8208
c906108c
SS
8209/* Return sibling of die, NULL if no sibling. */
8210
f9aca02d 8211static struct die_info *
fba45db2 8212sibling_die (struct die_info *die)
c906108c 8213{
639d11d3 8214 return die->sibling;
c906108c
SS
8215}
8216
8217/* Get linkage name of a die, return NULL if not found. */
8218
8219static char *
e142c38c 8220dwarf2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
8221{
8222 struct attribute *attr;
8223
e142c38c 8224 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
c906108c
SS
8225 if (attr && DW_STRING (attr))
8226 return DW_STRING (attr);
71c25dea
TT
8227 return dwarf2_name (die, cu);
8228}
8229
8230/* Get name of a die, return NULL if not found. */
8231
8232static char *
8233dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
8234 struct obstack *obstack)
8235{
8236 if (name && cu->language == language_cplus)
8237 {
8238 char *canon_name = cp_canonicalize_string (name);
8239
8240 if (canon_name != NULL)
8241 {
8242 if (strcmp (canon_name, name) != 0)
8243 name = obsavestring (canon_name, strlen (canon_name),
8244 obstack);
8245 xfree (canon_name);
8246 }
8247 }
8248
8249 return name;
c906108c
SS
8250}
8251
9219021c
DC
8252/* Get name of a die, return NULL if not found. */
8253
8254static char *
e142c38c 8255dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
8256{
8257 struct attribute *attr;
8258
e142c38c 8259 attr = dwarf2_attr (die, DW_AT_name, cu);
71c25dea
TT
8260 if (!attr || !DW_STRING (attr))
8261 return NULL;
8262
8263 switch (die->tag)
8264 {
8265 case DW_TAG_compile_unit:
8266 /* Compilation units have a DW_AT_name that is a filename, not
8267 a source language identifier. */
8268 case DW_TAG_enumeration_type:
8269 case DW_TAG_enumerator:
8270 /* These tags always have simple identifiers already; no need
8271 to canonicalize them. */
8272 return DW_STRING (attr);
8273 default:
8274 if (attr->form != GDB_FORM_cached_string)
8275 {
8276 DW_STRING (attr)
8277 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
8278 &cu->objfile->objfile_obstack);
8279 attr->form = GDB_FORM_cached_string;
8280 }
8281 return DW_STRING (attr);
8282 }
9219021c
DC
8283}
8284
8285/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
8286 is none. *EXT_CU is the CU containing DIE on input, and the CU
8287 containing the return value on output. */
9219021c
DC
8288
8289static struct die_info *
f2f0e013 8290dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
8291{
8292 struct attribute *attr;
9219021c 8293
f2f0e013 8294 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
8295 if (attr == NULL)
8296 return NULL;
8297
f2f0e013 8298 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
8299}
8300
c906108c
SS
8301/* Convert a DIE tag into its string name. */
8302
8303static char *
aa1ee363 8304dwarf_tag_name (unsigned tag)
c906108c
SS
8305{
8306 switch (tag)
8307 {
8308 case DW_TAG_padding:
8309 return "DW_TAG_padding";
8310 case DW_TAG_array_type:
8311 return "DW_TAG_array_type";
8312 case DW_TAG_class_type:
8313 return "DW_TAG_class_type";
8314 case DW_TAG_entry_point:
8315 return "DW_TAG_entry_point";
8316 case DW_TAG_enumeration_type:
8317 return "DW_TAG_enumeration_type";
8318 case DW_TAG_formal_parameter:
8319 return "DW_TAG_formal_parameter";
8320 case DW_TAG_imported_declaration:
8321 return "DW_TAG_imported_declaration";
8322 case DW_TAG_label:
8323 return "DW_TAG_label";
8324 case DW_TAG_lexical_block:
8325 return "DW_TAG_lexical_block";
8326 case DW_TAG_member:
8327 return "DW_TAG_member";
8328 case DW_TAG_pointer_type:
8329 return "DW_TAG_pointer_type";
8330 case DW_TAG_reference_type:
8331 return "DW_TAG_reference_type";
8332 case DW_TAG_compile_unit:
8333 return "DW_TAG_compile_unit";
8334 case DW_TAG_string_type:
8335 return "DW_TAG_string_type";
8336 case DW_TAG_structure_type:
8337 return "DW_TAG_structure_type";
8338 case DW_TAG_subroutine_type:
8339 return "DW_TAG_subroutine_type";
8340 case DW_TAG_typedef:
8341 return "DW_TAG_typedef";
8342 case DW_TAG_union_type:
8343 return "DW_TAG_union_type";
8344 case DW_TAG_unspecified_parameters:
8345 return "DW_TAG_unspecified_parameters";
8346 case DW_TAG_variant:
8347 return "DW_TAG_variant";
8348 case DW_TAG_common_block:
8349 return "DW_TAG_common_block";
8350 case DW_TAG_common_inclusion:
8351 return "DW_TAG_common_inclusion";
8352 case DW_TAG_inheritance:
8353 return "DW_TAG_inheritance";
8354 case DW_TAG_inlined_subroutine:
8355 return "DW_TAG_inlined_subroutine";
8356 case DW_TAG_module:
8357 return "DW_TAG_module";
8358 case DW_TAG_ptr_to_member_type:
8359 return "DW_TAG_ptr_to_member_type";
8360 case DW_TAG_set_type:
8361 return "DW_TAG_set_type";
8362 case DW_TAG_subrange_type:
8363 return "DW_TAG_subrange_type";
8364 case DW_TAG_with_stmt:
8365 return "DW_TAG_with_stmt";
8366 case DW_TAG_access_declaration:
8367 return "DW_TAG_access_declaration";
8368 case DW_TAG_base_type:
8369 return "DW_TAG_base_type";
8370 case DW_TAG_catch_block:
8371 return "DW_TAG_catch_block";
8372 case DW_TAG_const_type:
8373 return "DW_TAG_const_type";
8374 case DW_TAG_constant:
8375 return "DW_TAG_constant";
8376 case DW_TAG_enumerator:
8377 return "DW_TAG_enumerator";
8378 case DW_TAG_file_type:
8379 return "DW_TAG_file_type";
8380 case DW_TAG_friend:
8381 return "DW_TAG_friend";
8382 case DW_TAG_namelist:
8383 return "DW_TAG_namelist";
8384 case DW_TAG_namelist_item:
8385 return "DW_TAG_namelist_item";
8386 case DW_TAG_packed_type:
8387 return "DW_TAG_packed_type";
8388 case DW_TAG_subprogram:
8389 return "DW_TAG_subprogram";
8390 case DW_TAG_template_type_param:
8391 return "DW_TAG_template_type_param";
8392 case DW_TAG_template_value_param:
8393 return "DW_TAG_template_value_param";
8394 case DW_TAG_thrown_type:
8395 return "DW_TAG_thrown_type";
8396 case DW_TAG_try_block:
8397 return "DW_TAG_try_block";
8398 case DW_TAG_variant_part:
8399 return "DW_TAG_variant_part";
8400 case DW_TAG_variable:
8401 return "DW_TAG_variable";
8402 case DW_TAG_volatile_type:
8403 return "DW_TAG_volatile_type";
d9fa45fe
DC
8404 case DW_TAG_dwarf_procedure:
8405 return "DW_TAG_dwarf_procedure";
8406 case DW_TAG_restrict_type:
8407 return "DW_TAG_restrict_type";
8408 case DW_TAG_interface_type:
8409 return "DW_TAG_interface_type";
8410 case DW_TAG_namespace:
8411 return "DW_TAG_namespace";
8412 case DW_TAG_imported_module:
8413 return "DW_TAG_imported_module";
8414 case DW_TAG_unspecified_type:
8415 return "DW_TAG_unspecified_type";
8416 case DW_TAG_partial_unit:
8417 return "DW_TAG_partial_unit";
8418 case DW_TAG_imported_unit:
8419 return "DW_TAG_imported_unit";
b7619582
GF
8420 case DW_TAG_condition:
8421 return "DW_TAG_condition";
8422 case DW_TAG_shared_type:
8423 return "DW_TAG_shared_type";
c906108c
SS
8424 case DW_TAG_MIPS_loop:
8425 return "DW_TAG_MIPS_loop";
b7619582
GF
8426 case DW_TAG_HP_array_descriptor:
8427 return "DW_TAG_HP_array_descriptor";
c906108c
SS
8428 case DW_TAG_format_label:
8429 return "DW_TAG_format_label";
8430 case DW_TAG_function_template:
8431 return "DW_TAG_function_template";
8432 case DW_TAG_class_template:
8433 return "DW_TAG_class_template";
b7619582
GF
8434 case DW_TAG_GNU_BINCL:
8435 return "DW_TAG_GNU_BINCL";
8436 case DW_TAG_GNU_EINCL:
8437 return "DW_TAG_GNU_EINCL";
8438 case DW_TAG_upc_shared_type:
8439 return "DW_TAG_upc_shared_type";
8440 case DW_TAG_upc_strict_type:
8441 return "DW_TAG_upc_strict_type";
8442 case DW_TAG_upc_relaxed_type:
8443 return "DW_TAG_upc_relaxed_type";
8444 case DW_TAG_PGI_kanji_type:
8445 return "DW_TAG_PGI_kanji_type";
8446 case DW_TAG_PGI_interface_block:
8447 return "DW_TAG_PGI_interface_block";
c906108c
SS
8448 default:
8449 return "DW_TAG_<unknown>";
8450 }
8451}
8452
8453/* Convert a DWARF attribute code into its string name. */
8454
8455static char *
aa1ee363 8456dwarf_attr_name (unsigned attr)
c906108c
SS
8457{
8458 switch (attr)
8459 {
8460 case DW_AT_sibling:
8461 return "DW_AT_sibling";
8462 case DW_AT_location:
8463 return "DW_AT_location";
8464 case DW_AT_name:
8465 return "DW_AT_name";
8466 case DW_AT_ordering:
8467 return "DW_AT_ordering";
8468 case DW_AT_subscr_data:
8469 return "DW_AT_subscr_data";
8470 case DW_AT_byte_size:
8471 return "DW_AT_byte_size";
8472 case DW_AT_bit_offset:
8473 return "DW_AT_bit_offset";
8474 case DW_AT_bit_size:
8475 return "DW_AT_bit_size";
8476 case DW_AT_element_list:
8477 return "DW_AT_element_list";
8478 case DW_AT_stmt_list:
8479 return "DW_AT_stmt_list";
8480 case DW_AT_low_pc:
8481 return "DW_AT_low_pc";
8482 case DW_AT_high_pc:
8483 return "DW_AT_high_pc";
8484 case DW_AT_language:
8485 return "DW_AT_language";
8486 case DW_AT_member:
8487 return "DW_AT_member";
8488 case DW_AT_discr:
8489 return "DW_AT_discr";
8490 case DW_AT_discr_value:
8491 return "DW_AT_discr_value";
8492 case DW_AT_visibility:
8493 return "DW_AT_visibility";
8494 case DW_AT_import:
8495 return "DW_AT_import";
8496 case DW_AT_string_length:
8497 return "DW_AT_string_length";
8498 case DW_AT_common_reference:
8499 return "DW_AT_common_reference";
8500 case DW_AT_comp_dir:
8501 return "DW_AT_comp_dir";
8502 case DW_AT_const_value:
8503 return "DW_AT_const_value";
8504 case DW_AT_containing_type:
8505 return "DW_AT_containing_type";
8506 case DW_AT_default_value:
8507 return "DW_AT_default_value";
8508 case DW_AT_inline:
8509 return "DW_AT_inline";
8510 case DW_AT_is_optional:
8511 return "DW_AT_is_optional";
8512 case DW_AT_lower_bound:
8513 return "DW_AT_lower_bound";
8514 case DW_AT_producer:
8515 return "DW_AT_producer";
8516 case DW_AT_prototyped:
8517 return "DW_AT_prototyped";
8518 case DW_AT_return_addr:
8519 return "DW_AT_return_addr";
8520 case DW_AT_start_scope:
8521 return "DW_AT_start_scope";
09fa0d7c
JK
8522 case DW_AT_bit_stride:
8523 return "DW_AT_bit_stride";
c906108c
SS
8524 case DW_AT_upper_bound:
8525 return "DW_AT_upper_bound";
8526 case DW_AT_abstract_origin:
8527 return "DW_AT_abstract_origin";
8528 case DW_AT_accessibility:
8529 return "DW_AT_accessibility";
8530 case DW_AT_address_class:
8531 return "DW_AT_address_class";
8532 case DW_AT_artificial:
8533 return "DW_AT_artificial";
8534 case DW_AT_base_types:
8535 return "DW_AT_base_types";
8536 case DW_AT_calling_convention:
8537 return "DW_AT_calling_convention";
8538 case DW_AT_count:
8539 return "DW_AT_count";
8540 case DW_AT_data_member_location:
8541 return "DW_AT_data_member_location";
8542 case DW_AT_decl_column:
8543 return "DW_AT_decl_column";
8544 case DW_AT_decl_file:
8545 return "DW_AT_decl_file";
8546 case DW_AT_decl_line:
8547 return "DW_AT_decl_line";
8548 case DW_AT_declaration:
8549 return "DW_AT_declaration";
8550 case DW_AT_discr_list:
8551 return "DW_AT_discr_list";
8552 case DW_AT_encoding:
8553 return "DW_AT_encoding";
8554 case DW_AT_external:
8555 return "DW_AT_external";
8556 case DW_AT_frame_base:
8557 return "DW_AT_frame_base";
8558 case DW_AT_friend:
8559 return "DW_AT_friend";
8560 case DW_AT_identifier_case:
8561 return "DW_AT_identifier_case";
8562 case DW_AT_macro_info:
8563 return "DW_AT_macro_info";
8564 case DW_AT_namelist_items:
8565 return "DW_AT_namelist_items";
8566 case DW_AT_priority:
8567 return "DW_AT_priority";
8568 case DW_AT_segment:
8569 return "DW_AT_segment";
8570 case DW_AT_specification:
8571 return "DW_AT_specification";
8572 case DW_AT_static_link:
8573 return "DW_AT_static_link";
8574 case DW_AT_type:
8575 return "DW_AT_type";
8576 case DW_AT_use_location:
8577 return "DW_AT_use_location";
8578 case DW_AT_variable_parameter:
8579 return "DW_AT_variable_parameter";
8580 case DW_AT_virtuality:
8581 return "DW_AT_virtuality";
8582 case DW_AT_vtable_elem_location:
8583 return "DW_AT_vtable_elem_location";
b7619582 8584 /* DWARF 3 values. */
d9fa45fe
DC
8585 case DW_AT_allocated:
8586 return "DW_AT_allocated";
8587 case DW_AT_associated:
8588 return "DW_AT_associated";
8589 case DW_AT_data_location:
8590 return "DW_AT_data_location";
09fa0d7c
JK
8591 case DW_AT_byte_stride:
8592 return "DW_AT_byte_stride";
d9fa45fe
DC
8593 case DW_AT_entry_pc:
8594 return "DW_AT_entry_pc";
8595 case DW_AT_use_UTF8:
8596 return "DW_AT_use_UTF8";
8597 case DW_AT_extension:
8598 return "DW_AT_extension";
8599 case DW_AT_ranges:
8600 return "DW_AT_ranges";
8601 case DW_AT_trampoline:
8602 return "DW_AT_trampoline";
8603 case DW_AT_call_column:
8604 return "DW_AT_call_column";
8605 case DW_AT_call_file:
8606 return "DW_AT_call_file";
8607 case DW_AT_call_line:
8608 return "DW_AT_call_line";
b7619582
GF
8609 case DW_AT_description:
8610 return "DW_AT_description";
8611 case DW_AT_binary_scale:
8612 return "DW_AT_binary_scale";
8613 case DW_AT_decimal_scale:
8614 return "DW_AT_decimal_scale";
8615 case DW_AT_small:
8616 return "DW_AT_small";
8617 case DW_AT_decimal_sign:
8618 return "DW_AT_decimal_sign";
8619 case DW_AT_digit_count:
8620 return "DW_AT_digit_count";
8621 case DW_AT_picture_string:
8622 return "DW_AT_picture_string";
8623 case DW_AT_mutable:
8624 return "DW_AT_mutable";
8625 case DW_AT_threads_scaled:
8626 return "DW_AT_threads_scaled";
8627 case DW_AT_explicit:
8628 return "DW_AT_explicit";
8629 case DW_AT_object_pointer:
8630 return "DW_AT_object_pointer";
8631 case DW_AT_endianity:
8632 return "DW_AT_endianity";
8633 case DW_AT_elemental:
8634 return "DW_AT_elemental";
8635 case DW_AT_pure:
8636 return "DW_AT_pure";
8637 case DW_AT_recursive:
8638 return "DW_AT_recursive";
b7619582 8639 /* SGI/MIPS extensions. */
c764a876 8640#ifdef MIPS /* collides with DW_AT_HP_block_index */
c906108c
SS
8641 case DW_AT_MIPS_fde:
8642 return "DW_AT_MIPS_fde";
c764a876 8643#endif
c906108c
SS
8644 case DW_AT_MIPS_loop_begin:
8645 return "DW_AT_MIPS_loop_begin";
8646 case DW_AT_MIPS_tail_loop_begin:
8647 return "DW_AT_MIPS_tail_loop_begin";
8648 case DW_AT_MIPS_epilog_begin:
8649 return "DW_AT_MIPS_epilog_begin";
8650 case DW_AT_MIPS_loop_unroll_factor:
8651 return "DW_AT_MIPS_loop_unroll_factor";
8652 case DW_AT_MIPS_software_pipeline_depth:
8653 return "DW_AT_MIPS_software_pipeline_depth";
8654 case DW_AT_MIPS_linkage_name:
8655 return "DW_AT_MIPS_linkage_name";
b7619582
GF
8656 case DW_AT_MIPS_stride:
8657 return "DW_AT_MIPS_stride";
8658 case DW_AT_MIPS_abstract_name:
8659 return "DW_AT_MIPS_abstract_name";
8660 case DW_AT_MIPS_clone_origin:
8661 return "DW_AT_MIPS_clone_origin";
8662 case DW_AT_MIPS_has_inlines:
8663 return "DW_AT_MIPS_has_inlines";
b7619582 8664 /* HP extensions. */
c764a876 8665#ifndef MIPS /* collides with DW_AT_MIPS_fde */
b7619582
GF
8666 case DW_AT_HP_block_index:
8667 return "DW_AT_HP_block_index";
c764a876 8668#endif
b7619582
GF
8669 case DW_AT_HP_unmodifiable:
8670 return "DW_AT_HP_unmodifiable";
8671 case DW_AT_HP_actuals_stmt_list:
8672 return "DW_AT_HP_actuals_stmt_list";
8673 case DW_AT_HP_proc_per_section:
8674 return "DW_AT_HP_proc_per_section";
8675 case DW_AT_HP_raw_data_ptr:
8676 return "DW_AT_HP_raw_data_ptr";
8677 case DW_AT_HP_pass_by_reference:
8678 return "DW_AT_HP_pass_by_reference";
8679 case DW_AT_HP_opt_level:
8680 return "DW_AT_HP_opt_level";
8681 case DW_AT_HP_prof_version_id:
8682 return "DW_AT_HP_prof_version_id";
8683 case DW_AT_HP_opt_flags:
8684 return "DW_AT_HP_opt_flags";
8685 case DW_AT_HP_cold_region_low_pc:
8686 return "DW_AT_HP_cold_region_low_pc";
8687 case DW_AT_HP_cold_region_high_pc:
8688 return "DW_AT_HP_cold_region_high_pc";
8689 case DW_AT_HP_all_variables_modifiable:
8690 return "DW_AT_HP_all_variables_modifiable";
8691 case DW_AT_HP_linkage_name:
8692 return "DW_AT_HP_linkage_name";
8693 case DW_AT_HP_prof_flags:
8694 return "DW_AT_HP_prof_flags";
8695 /* GNU extensions. */
c906108c
SS
8696 case DW_AT_sf_names:
8697 return "DW_AT_sf_names";
8698 case DW_AT_src_info:
8699 return "DW_AT_src_info";
8700 case DW_AT_mac_info:
8701 return "DW_AT_mac_info";
8702 case DW_AT_src_coords:
8703 return "DW_AT_src_coords";
8704 case DW_AT_body_begin:
8705 return "DW_AT_body_begin";
8706 case DW_AT_body_end:
8707 return "DW_AT_body_end";
f5f8a009
EZ
8708 case DW_AT_GNU_vector:
8709 return "DW_AT_GNU_vector";
b7619582
GF
8710 /* VMS extensions. */
8711 case DW_AT_VMS_rtnbeg_pd_address:
8712 return "DW_AT_VMS_rtnbeg_pd_address";
8713 /* UPC extension. */
8714 case DW_AT_upc_threads_scaled:
8715 return "DW_AT_upc_threads_scaled";
8716 /* PGI (STMicroelectronics) extensions. */
8717 case DW_AT_PGI_lbase:
8718 return "DW_AT_PGI_lbase";
8719 case DW_AT_PGI_soffset:
8720 return "DW_AT_PGI_soffset";
8721 case DW_AT_PGI_lstride:
8722 return "DW_AT_PGI_lstride";
c906108c
SS
8723 default:
8724 return "DW_AT_<unknown>";
8725 }
8726}
8727
8728/* Convert a DWARF value form code into its string name. */
8729
8730static char *
aa1ee363 8731dwarf_form_name (unsigned form)
c906108c
SS
8732{
8733 switch (form)
8734 {
8735 case DW_FORM_addr:
8736 return "DW_FORM_addr";
8737 case DW_FORM_block2:
8738 return "DW_FORM_block2";
8739 case DW_FORM_block4:
8740 return "DW_FORM_block4";
8741 case DW_FORM_data2:
8742 return "DW_FORM_data2";
8743 case DW_FORM_data4:
8744 return "DW_FORM_data4";
8745 case DW_FORM_data8:
8746 return "DW_FORM_data8";
8747 case DW_FORM_string:
8748 return "DW_FORM_string";
8749 case DW_FORM_block:
8750 return "DW_FORM_block";
8751 case DW_FORM_block1:
8752 return "DW_FORM_block1";
8753 case DW_FORM_data1:
8754 return "DW_FORM_data1";
8755 case DW_FORM_flag:
8756 return "DW_FORM_flag";
8757 case DW_FORM_sdata:
8758 return "DW_FORM_sdata";
8759 case DW_FORM_strp:
8760 return "DW_FORM_strp";
8761 case DW_FORM_udata:
8762 return "DW_FORM_udata";
8763 case DW_FORM_ref_addr:
8764 return "DW_FORM_ref_addr";
8765 case DW_FORM_ref1:
8766 return "DW_FORM_ref1";
8767 case DW_FORM_ref2:
8768 return "DW_FORM_ref2";
8769 case DW_FORM_ref4:
8770 return "DW_FORM_ref4";
8771 case DW_FORM_ref8:
8772 return "DW_FORM_ref8";
8773 case DW_FORM_ref_udata:
8774 return "DW_FORM_ref_udata";
8775 case DW_FORM_indirect:
8776 return "DW_FORM_indirect";
71c25dea
TT
8777 case GDB_FORM_cached_string:
8778 return "GDB_FORM_cached_string";
c906108c
SS
8779 default:
8780 return "DW_FORM_<unknown>";
8781 }
8782}
8783
8784/* Convert a DWARF stack opcode into its string name. */
8785
8786static char *
aa1ee363 8787dwarf_stack_op_name (unsigned op)
c906108c
SS
8788{
8789 switch (op)
8790 {
8791 case DW_OP_addr:
8792 return "DW_OP_addr";
8793 case DW_OP_deref:
8794 return "DW_OP_deref";
8795 case DW_OP_const1u:
8796 return "DW_OP_const1u";
8797 case DW_OP_const1s:
8798 return "DW_OP_const1s";
8799 case DW_OP_const2u:
8800 return "DW_OP_const2u";
8801 case DW_OP_const2s:
8802 return "DW_OP_const2s";
8803 case DW_OP_const4u:
8804 return "DW_OP_const4u";
8805 case DW_OP_const4s:
8806 return "DW_OP_const4s";
8807 case DW_OP_const8u:
8808 return "DW_OP_const8u";
8809 case DW_OP_const8s:
8810 return "DW_OP_const8s";
8811 case DW_OP_constu:
8812 return "DW_OP_constu";
8813 case DW_OP_consts:
8814 return "DW_OP_consts";
8815 case DW_OP_dup:
8816 return "DW_OP_dup";
8817 case DW_OP_drop:
8818 return "DW_OP_drop";
8819 case DW_OP_over:
8820 return "DW_OP_over";
8821 case DW_OP_pick:
8822 return "DW_OP_pick";
8823 case DW_OP_swap:
8824 return "DW_OP_swap";
8825 case DW_OP_rot:
8826 return "DW_OP_rot";
8827 case DW_OP_xderef:
8828 return "DW_OP_xderef";
8829 case DW_OP_abs:
8830 return "DW_OP_abs";
8831 case DW_OP_and:
8832 return "DW_OP_and";
8833 case DW_OP_div:
8834 return "DW_OP_div";
8835 case DW_OP_minus:
8836 return "DW_OP_minus";
8837 case DW_OP_mod:
8838 return "DW_OP_mod";
8839 case DW_OP_mul:
8840 return "DW_OP_mul";
8841 case DW_OP_neg:
8842 return "DW_OP_neg";
8843 case DW_OP_not:
8844 return "DW_OP_not";
8845 case DW_OP_or:
8846 return "DW_OP_or";
8847 case DW_OP_plus:
8848 return "DW_OP_plus";
8849 case DW_OP_plus_uconst:
8850 return "DW_OP_plus_uconst";
8851 case DW_OP_shl:
8852 return "DW_OP_shl";
8853 case DW_OP_shr:
8854 return "DW_OP_shr";
8855 case DW_OP_shra:
8856 return "DW_OP_shra";
8857 case DW_OP_xor:
8858 return "DW_OP_xor";
8859 case DW_OP_bra:
8860 return "DW_OP_bra";
8861 case DW_OP_eq:
8862 return "DW_OP_eq";
8863 case DW_OP_ge:
8864 return "DW_OP_ge";
8865 case DW_OP_gt:
8866 return "DW_OP_gt";
8867 case DW_OP_le:
8868 return "DW_OP_le";
8869 case DW_OP_lt:
8870 return "DW_OP_lt";
8871 case DW_OP_ne:
8872 return "DW_OP_ne";
8873 case DW_OP_skip:
8874 return "DW_OP_skip";
8875 case DW_OP_lit0:
8876 return "DW_OP_lit0";
8877 case DW_OP_lit1:
8878 return "DW_OP_lit1";
8879 case DW_OP_lit2:
8880 return "DW_OP_lit2";
8881 case DW_OP_lit3:
8882 return "DW_OP_lit3";
8883 case DW_OP_lit4:
8884 return "DW_OP_lit4";
8885 case DW_OP_lit5:
8886 return "DW_OP_lit5";
8887 case DW_OP_lit6:
8888 return "DW_OP_lit6";
8889 case DW_OP_lit7:
8890 return "DW_OP_lit7";
8891 case DW_OP_lit8:
8892 return "DW_OP_lit8";
8893 case DW_OP_lit9:
8894 return "DW_OP_lit9";
8895 case DW_OP_lit10:
8896 return "DW_OP_lit10";
8897 case DW_OP_lit11:
8898 return "DW_OP_lit11";
8899 case DW_OP_lit12:
8900 return "DW_OP_lit12";
8901 case DW_OP_lit13:
8902 return "DW_OP_lit13";
8903 case DW_OP_lit14:
8904 return "DW_OP_lit14";
8905 case DW_OP_lit15:
8906 return "DW_OP_lit15";
8907 case DW_OP_lit16:
8908 return "DW_OP_lit16";
8909 case DW_OP_lit17:
8910 return "DW_OP_lit17";
8911 case DW_OP_lit18:
8912 return "DW_OP_lit18";
8913 case DW_OP_lit19:
8914 return "DW_OP_lit19";
8915 case DW_OP_lit20:
8916 return "DW_OP_lit20";
8917 case DW_OP_lit21:
8918 return "DW_OP_lit21";
8919 case DW_OP_lit22:
8920 return "DW_OP_lit22";
8921 case DW_OP_lit23:
8922 return "DW_OP_lit23";
8923 case DW_OP_lit24:
8924 return "DW_OP_lit24";
8925 case DW_OP_lit25:
8926 return "DW_OP_lit25";
8927 case DW_OP_lit26:
8928 return "DW_OP_lit26";
8929 case DW_OP_lit27:
8930 return "DW_OP_lit27";
8931 case DW_OP_lit28:
8932 return "DW_OP_lit28";
8933 case DW_OP_lit29:
8934 return "DW_OP_lit29";
8935 case DW_OP_lit30:
8936 return "DW_OP_lit30";
8937 case DW_OP_lit31:
8938 return "DW_OP_lit31";
8939 case DW_OP_reg0:
8940 return "DW_OP_reg0";
8941 case DW_OP_reg1:
8942 return "DW_OP_reg1";
8943 case DW_OP_reg2:
8944 return "DW_OP_reg2";
8945 case DW_OP_reg3:
8946 return "DW_OP_reg3";
8947 case DW_OP_reg4:
8948 return "DW_OP_reg4";
8949 case DW_OP_reg5:
8950 return "DW_OP_reg5";
8951 case DW_OP_reg6:
8952 return "DW_OP_reg6";
8953 case DW_OP_reg7:
8954 return "DW_OP_reg7";
8955 case DW_OP_reg8:
8956 return "DW_OP_reg8";
8957 case DW_OP_reg9:
8958 return "DW_OP_reg9";
8959 case DW_OP_reg10:
8960 return "DW_OP_reg10";
8961 case DW_OP_reg11:
8962 return "DW_OP_reg11";
8963 case DW_OP_reg12:
8964 return "DW_OP_reg12";
8965 case DW_OP_reg13:
8966 return "DW_OP_reg13";
8967 case DW_OP_reg14:
8968 return "DW_OP_reg14";
8969 case DW_OP_reg15:
8970 return "DW_OP_reg15";
8971 case DW_OP_reg16:
8972 return "DW_OP_reg16";
8973 case DW_OP_reg17:
8974 return "DW_OP_reg17";
8975 case DW_OP_reg18:
8976 return "DW_OP_reg18";
8977 case DW_OP_reg19:
8978 return "DW_OP_reg19";
8979 case DW_OP_reg20:
8980 return "DW_OP_reg20";
8981 case DW_OP_reg21:
8982 return "DW_OP_reg21";
8983 case DW_OP_reg22:
8984 return "DW_OP_reg22";
8985 case DW_OP_reg23:
8986 return "DW_OP_reg23";
8987 case DW_OP_reg24:
8988 return "DW_OP_reg24";
8989 case DW_OP_reg25:
8990 return "DW_OP_reg25";
8991 case DW_OP_reg26:
8992 return "DW_OP_reg26";
8993 case DW_OP_reg27:
8994 return "DW_OP_reg27";
8995 case DW_OP_reg28:
8996 return "DW_OP_reg28";
8997 case DW_OP_reg29:
8998 return "DW_OP_reg29";
8999 case DW_OP_reg30:
9000 return "DW_OP_reg30";
9001 case DW_OP_reg31:
9002 return "DW_OP_reg31";
9003 case DW_OP_breg0:
9004 return "DW_OP_breg0";
9005 case DW_OP_breg1:
9006 return "DW_OP_breg1";
9007 case DW_OP_breg2:
9008 return "DW_OP_breg2";
9009 case DW_OP_breg3:
9010 return "DW_OP_breg3";
9011 case DW_OP_breg4:
9012 return "DW_OP_breg4";
9013 case DW_OP_breg5:
9014 return "DW_OP_breg5";
9015 case DW_OP_breg6:
9016 return "DW_OP_breg6";
9017 case DW_OP_breg7:
9018 return "DW_OP_breg7";
9019 case DW_OP_breg8:
9020 return "DW_OP_breg8";
9021 case DW_OP_breg9:
9022 return "DW_OP_breg9";
9023 case DW_OP_breg10:
9024 return "DW_OP_breg10";
9025 case DW_OP_breg11:
9026 return "DW_OP_breg11";
9027 case DW_OP_breg12:
9028 return "DW_OP_breg12";
9029 case DW_OP_breg13:
9030 return "DW_OP_breg13";
9031 case DW_OP_breg14:
9032 return "DW_OP_breg14";
9033 case DW_OP_breg15:
9034 return "DW_OP_breg15";
9035 case DW_OP_breg16:
9036 return "DW_OP_breg16";
9037 case DW_OP_breg17:
9038 return "DW_OP_breg17";
9039 case DW_OP_breg18:
9040 return "DW_OP_breg18";
9041 case DW_OP_breg19:
9042 return "DW_OP_breg19";
9043 case DW_OP_breg20:
9044 return "DW_OP_breg20";
9045 case DW_OP_breg21:
9046 return "DW_OP_breg21";
9047 case DW_OP_breg22:
9048 return "DW_OP_breg22";
9049 case DW_OP_breg23:
9050 return "DW_OP_breg23";
9051 case DW_OP_breg24:
9052 return "DW_OP_breg24";
9053 case DW_OP_breg25:
9054 return "DW_OP_breg25";
9055 case DW_OP_breg26:
9056 return "DW_OP_breg26";
9057 case DW_OP_breg27:
9058 return "DW_OP_breg27";
9059 case DW_OP_breg28:
9060 return "DW_OP_breg28";
9061 case DW_OP_breg29:
9062 return "DW_OP_breg29";
9063 case DW_OP_breg30:
9064 return "DW_OP_breg30";
9065 case DW_OP_breg31:
9066 return "DW_OP_breg31";
9067 case DW_OP_regx:
9068 return "DW_OP_regx";
9069 case DW_OP_fbreg:
9070 return "DW_OP_fbreg";
9071 case DW_OP_bregx:
9072 return "DW_OP_bregx";
9073 case DW_OP_piece:
9074 return "DW_OP_piece";
9075 case DW_OP_deref_size:
9076 return "DW_OP_deref_size";
9077 case DW_OP_xderef_size:
9078 return "DW_OP_xderef_size";
9079 case DW_OP_nop:
9080 return "DW_OP_nop";
b7619582 9081 /* DWARF 3 extensions. */
ed348acc
EZ
9082 case DW_OP_push_object_address:
9083 return "DW_OP_push_object_address";
9084 case DW_OP_call2:
9085 return "DW_OP_call2";
9086 case DW_OP_call4:
9087 return "DW_OP_call4";
9088 case DW_OP_call_ref:
9089 return "DW_OP_call_ref";
b7619582
GF
9090 /* GNU extensions. */
9091 case DW_OP_form_tls_address:
9092 return "DW_OP_form_tls_address";
9093 case DW_OP_call_frame_cfa:
9094 return "DW_OP_call_frame_cfa";
9095 case DW_OP_bit_piece:
9096 return "DW_OP_bit_piece";
ed348acc
EZ
9097 case DW_OP_GNU_push_tls_address:
9098 return "DW_OP_GNU_push_tls_address";
42be36b3
CT
9099 case DW_OP_GNU_uninit:
9100 return "DW_OP_GNU_uninit";
b7619582
GF
9101 /* HP extensions. */
9102 case DW_OP_HP_is_value:
9103 return "DW_OP_HP_is_value";
9104 case DW_OP_HP_fltconst4:
9105 return "DW_OP_HP_fltconst4";
9106 case DW_OP_HP_fltconst8:
9107 return "DW_OP_HP_fltconst8";
9108 case DW_OP_HP_mod_range:
9109 return "DW_OP_HP_mod_range";
9110 case DW_OP_HP_unmod_range:
9111 return "DW_OP_HP_unmod_range";
9112 case DW_OP_HP_tls:
9113 return "DW_OP_HP_tls";
c906108c
SS
9114 default:
9115 return "OP_<unknown>";
9116 }
9117}
9118
9119static char *
fba45db2 9120dwarf_bool_name (unsigned mybool)
c906108c
SS
9121{
9122 if (mybool)
9123 return "TRUE";
9124 else
9125 return "FALSE";
9126}
9127
9128/* Convert a DWARF type code into its string name. */
9129
9130static char *
aa1ee363 9131dwarf_type_encoding_name (unsigned enc)
c906108c
SS
9132{
9133 switch (enc)
9134 {
b7619582
GF
9135 case DW_ATE_void:
9136 return "DW_ATE_void";
c906108c
SS
9137 case DW_ATE_address:
9138 return "DW_ATE_address";
9139 case DW_ATE_boolean:
9140 return "DW_ATE_boolean";
9141 case DW_ATE_complex_float:
9142 return "DW_ATE_complex_float";
9143 case DW_ATE_float:
9144 return "DW_ATE_float";
9145 case DW_ATE_signed:
9146 return "DW_ATE_signed";
9147 case DW_ATE_signed_char:
9148 return "DW_ATE_signed_char";
9149 case DW_ATE_unsigned:
9150 return "DW_ATE_unsigned";
9151 case DW_ATE_unsigned_char:
9152 return "DW_ATE_unsigned_char";
b7619582 9153 /* DWARF 3. */
d9fa45fe
DC
9154 case DW_ATE_imaginary_float:
9155 return "DW_ATE_imaginary_float";
b7619582
GF
9156 case DW_ATE_packed_decimal:
9157 return "DW_ATE_packed_decimal";
9158 case DW_ATE_numeric_string:
9159 return "DW_ATE_numeric_string";
9160 case DW_ATE_edited:
9161 return "DW_ATE_edited";
9162 case DW_ATE_signed_fixed:
9163 return "DW_ATE_signed_fixed";
9164 case DW_ATE_unsigned_fixed:
9165 return "DW_ATE_unsigned_fixed";
9166 case DW_ATE_decimal_float:
9167 return "DW_ATE_decimal_float";
9168 /* HP extensions. */
9169 case DW_ATE_HP_float80:
9170 return "DW_ATE_HP_float80";
9171 case DW_ATE_HP_complex_float80:
9172 return "DW_ATE_HP_complex_float80";
9173 case DW_ATE_HP_float128:
9174 return "DW_ATE_HP_float128";
9175 case DW_ATE_HP_complex_float128:
9176 return "DW_ATE_HP_complex_float128";
9177 case DW_ATE_HP_floathpintel:
9178 return "DW_ATE_HP_floathpintel";
9179 case DW_ATE_HP_imaginary_float80:
9180 return "DW_ATE_HP_imaginary_float80";
9181 case DW_ATE_HP_imaginary_float128:
9182 return "DW_ATE_HP_imaginary_float128";
c906108c
SS
9183 default:
9184 return "DW_ATE_<unknown>";
9185 }
9186}
9187
9188/* Convert a DWARF call frame info operation to its string name. */
9189
9190#if 0
9191static char *
aa1ee363 9192dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
9193{
9194 switch (cfi_opc)
9195 {
9196 case DW_CFA_advance_loc:
9197 return "DW_CFA_advance_loc";
9198 case DW_CFA_offset:
9199 return "DW_CFA_offset";
9200 case DW_CFA_restore:
9201 return "DW_CFA_restore";
9202 case DW_CFA_nop:
9203 return "DW_CFA_nop";
9204 case DW_CFA_set_loc:
9205 return "DW_CFA_set_loc";
9206 case DW_CFA_advance_loc1:
9207 return "DW_CFA_advance_loc1";
9208 case DW_CFA_advance_loc2:
9209 return "DW_CFA_advance_loc2";
9210 case DW_CFA_advance_loc4:
9211 return "DW_CFA_advance_loc4";
9212 case DW_CFA_offset_extended:
9213 return "DW_CFA_offset_extended";
9214 case DW_CFA_restore_extended:
9215 return "DW_CFA_restore_extended";
9216 case DW_CFA_undefined:
9217 return "DW_CFA_undefined";
9218 case DW_CFA_same_value:
9219 return "DW_CFA_same_value";
9220 case DW_CFA_register:
9221 return "DW_CFA_register";
9222 case DW_CFA_remember_state:
9223 return "DW_CFA_remember_state";
9224 case DW_CFA_restore_state:
9225 return "DW_CFA_restore_state";
9226 case DW_CFA_def_cfa:
9227 return "DW_CFA_def_cfa";
9228 case DW_CFA_def_cfa_register:
9229 return "DW_CFA_def_cfa_register";
9230 case DW_CFA_def_cfa_offset:
9231 return "DW_CFA_def_cfa_offset";
b7619582 9232 /* DWARF 3. */
985cb1a3
JM
9233 case DW_CFA_def_cfa_expression:
9234 return "DW_CFA_def_cfa_expression";
9235 case DW_CFA_expression:
9236 return "DW_CFA_expression";
9237 case DW_CFA_offset_extended_sf:
9238 return "DW_CFA_offset_extended_sf";
9239 case DW_CFA_def_cfa_sf:
9240 return "DW_CFA_def_cfa_sf";
9241 case DW_CFA_def_cfa_offset_sf:
9242 return "DW_CFA_def_cfa_offset_sf";
b7619582
GF
9243 case DW_CFA_val_offset:
9244 return "DW_CFA_val_offset";
9245 case DW_CFA_val_offset_sf:
9246 return "DW_CFA_val_offset_sf";
9247 case DW_CFA_val_expression:
9248 return "DW_CFA_val_expression";
9249 /* SGI/MIPS specific. */
c906108c
SS
9250 case DW_CFA_MIPS_advance_loc8:
9251 return "DW_CFA_MIPS_advance_loc8";
b7619582 9252 /* GNU extensions. */
985cb1a3
JM
9253 case DW_CFA_GNU_window_save:
9254 return "DW_CFA_GNU_window_save";
9255 case DW_CFA_GNU_args_size:
9256 return "DW_CFA_GNU_args_size";
9257 case DW_CFA_GNU_negative_offset_extended:
9258 return "DW_CFA_GNU_negative_offset_extended";
c906108c
SS
9259 default:
9260 return "DW_CFA_<unknown>";
9261 }
9262}
9263#endif
9264
f9aca02d 9265static void
d97bc12b 9266dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
9267{
9268 unsigned int i;
9269
d97bc12b
DE
9270 print_spaces (indent, f);
9271 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
c906108c 9272 dwarf_tag_name (die->tag), die->abbrev, die->offset);
d97bc12b
DE
9273
9274 if (die->parent != NULL)
9275 {
9276 print_spaces (indent, f);
9277 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
9278 die->parent->offset);
9279 }
9280
9281 print_spaces (indent, f);
9282 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 9283 dwarf_bool_name (die->child != NULL));
c906108c 9284
d97bc12b
DE
9285 print_spaces (indent, f);
9286 fprintf_unfiltered (f, " attributes:\n");
9287
c906108c
SS
9288 for (i = 0; i < die->num_attrs; ++i)
9289 {
d97bc12b
DE
9290 print_spaces (indent, f);
9291 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
9292 dwarf_attr_name (die->attrs[i].name),
9293 dwarf_form_name (die->attrs[i].form));
d97bc12b 9294
c906108c
SS
9295 switch (die->attrs[i].form)
9296 {
9297 case DW_FORM_ref_addr:
9298 case DW_FORM_addr:
d97bc12b
DE
9299 fprintf_unfiltered (f, "address: ");
9300 fputs_filtered (paddress (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
9301 break;
9302 case DW_FORM_block2:
9303 case DW_FORM_block4:
9304 case DW_FORM_block:
9305 case DW_FORM_block1:
d97bc12b 9306 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c 9307 break;
10b3939b
DJ
9308 case DW_FORM_ref1:
9309 case DW_FORM_ref2:
9310 case DW_FORM_ref4:
d97bc12b 9311 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10b3939b
DJ
9312 (long) (DW_ADDR (&die->attrs[i])));
9313 break;
c906108c
SS
9314 case DW_FORM_data1:
9315 case DW_FORM_data2:
9316 case DW_FORM_data4:
ce5d95e1 9317 case DW_FORM_data8:
c906108c
SS
9318 case DW_FORM_udata:
9319 case DW_FORM_sdata:
d97bc12b 9320 fprintf_unfiltered (f, "constant: %ld", DW_UNSND (&die->attrs[i]));
c906108c
SS
9321 break;
9322 case DW_FORM_string:
4bdf3d34 9323 case DW_FORM_strp:
71c25dea 9324 case GDB_FORM_cached_string:
d97bc12b 9325 fprintf_unfiltered (f, "string: \"%s\"",
c906108c 9326 DW_STRING (&die->attrs[i])
c5aa993b 9327 ? DW_STRING (&die->attrs[i]) : "");
c906108c
SS
9328 break;
9329 case DW_FORM_flag:
9330 if (DW_UNSND (&die->attrs[i]))
d97bc12b 9331 fprintf_unfiltered (f, "flag: TRUE");
c906108c 9332 else
d97bc12b 9333 fprintf_unfiltered (f, "flag: FALSE");
c906108c 9334 break;
a8329558
KW
9335 case DW_FORM_indirect:
9336 /* the reader will have reduced the indirect form to
9337 the "base form" so this form should not occur */
d97bc12b 9338 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
a8329558 9339 break;
c906108c 9340 default:
d97bc12b 9341 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 9342 die->attrs[i].form);
d97bc12b 9343 break;
c906108c 9344 }
d97bc12b 9345 fprintf_unfiltered (f, "\n");
c906108c
SS
9346 }
9347}
9348
f9aca02d 9349static void
d97bc12b 9350dump_die_for_error (struct die_info *die)
c906108c 9351{
d97bc12b
DE
9352 dump_die_shallow (gdb_stderr, 0, die);
9353}
9354
9355static void
9356dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
9357{
9358 int indent = level * 4;
9359
9360 gdb_assert (die != NULL);
9361
9362 if (level >= max_level)
9363 return;
9364
9365 dump_die_shallow (f, indent, die);
9366
9367 if (die->child != NULL)
c906108c 9368 {
d97bc12b
DE
9369 print_spaces (indent, f);
9370 fprintf_unfiltered (f, " Children:");
9371 if (level + 1 < max_level)
9372 {
9373 fprintf_unfiltered (f, "\n");
9374 dump_die_1 (f, level + 1, max_level, die->child);
9375 }
9376 else
9377 {
9378 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
9379 }
9380 }
9381
9382 if (die->sibling != NULL && level > 0)
9383 {
9384 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
9385 }
9386}
9387
d97bc12b
DE
9388/* This is called from the pdie macro in gdbinit.in.
9389 It's not static so gcc will keep a copy callable from gdb. */
9390
9391void
9392dump_die (struct die_info *die, int max_level)
9393{
9394 dump_die_1 (gdb_stdlog, 0, max_level, die);
9395}
9396
f9aca02d 9397static void
51545339 9398store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9399{
51545339 9400 void **slot;
c906108c 9401
51545339
DJ
9402 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
9403
9404 *slot = die;
c906108c
SS
9405}
9406
9407static unsigned int
c764a876 9408dwarf2_get_ref_die_offset (struct attribute *attr)
c906108c
SS
9409{
9410 unsigned int result = 0;
9411
9412 switch (attr->form)
9413 {
9414 case DW_FORM_ref_addr:
c906108c
SS
9415 case DW_FORM_ref1:
9416 case DW_FORM_ref2:
9417 case DW_FORM_ref4:
613e1657 9418 case DW_FORM_ref8:
c906108c 9419 case DW_FORM_ref_udata:
10b3939b 9420 result = DW_ADDR (attr);
c906108c
SS
9421 break;
9422 default:
4d3c2250 9423 complaint (&symfile_complaints,
e2e0b3e5 9424 _("unsupported die ref attribute form: '%s'"),
4d3c2250 9425 dwarf_form_name (attr->form));
c906108c
SS
9426 }
9427 return result;
9428}
9429
a02abb62
JB
9430/* Return the constant value held by the given attribute. Return -1
9431 if the value held by the attribute is not constant. */
9432
9433static int
9434dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
9435{
9436 if (attr->form == DW_FORM_sdata)
9437 return DW_SND (attr);
9438 else if (attr->form == DW_FORM_udata
9439 || attr->form == DW_FORM_data1
9440 || attr->form == DW_FORM_data2
9441 || attr->form == DW_FORM_data4
9442 || attr->form == DW_FORM_data8)
9443 return DW_UNSND (attr);
9444 else
9445 {
e2e0b3e5 9446 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
a02abb62
JB
9447 dwarf_form_name (attr->form));
9448 return default_value;
9449 }
9450}
9451
03dd20cc
DJ
9452/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
9453 unit and add it to our queue. */
9454
9455static void
9456maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
9457 struct dwarf2_per_cu_data *per_cu)
9458{
9459 /* Mark the dependence relation so that we don't flush PER_CU
9460 too early. */
9461 dwarf2_add_dependence (this_cu, per_cu);
9462
9463 /* If it's already on the queue, we have nothing to do. */
9464 if (per_cu->queued)
9465 return;
9466
9467 /* If the compilation unit is already loaded, just mark it as
9468 used. */
9469 if (per_cu->cu != NULL)
9470 {
9471 per_cu->cu->last_used = 0;
9472 return;
9473 }
9474
9475 /* Add it to the queue. */
9476 queue_comp_unit (per_cu, this_cu->objfile);
9477}
9478
f9aca02d 9479static struct die_info *
10b3939b 9480follow_die_ref (struct die_info *src_die, struct attribute *attr,
f2f0e013 9481 struct dwarf2_cu **ref_cu)
c906108c
SS
9482{
9483 struct die_info *die;
10b3939b 9484 unsigned int offset;
10b3939b 9485 struct die_info temp_die;
f2f0e013 9486 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 9487
c764a876 9488 offset = dwarf2_get_ref_die_offset (attr);
10b3939b 9489
45452591 9490 if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
9491 {
9492 struct dwarf2_per_cu_data *per_cu;
45452591 9493 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
9494
9495 /* If necessary, add it to the queue and load its DIEs. */
9496 maybe_queue_comp_unit (cu, per_cu);
9497
10b3939b
DJ
9498 target_cu = per_cu->cu;
9499 }
9500 else
9501 target_cu = cu;
c906108c 9502
f2f0e013 9503 *ref_cu = target_cu;
51545339
DJ
9504 temp_die.offset = offset;
9505 die = htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
9506 if (die)
9507 return die;
10b3939b 9508
8a3fe4f8
AC
9509 error (_("Dwarf Error: Cannot find DIE at 0x%lx referenced from DIE "
9510 "at 0x%lx [in module %s]"),
1d325ec1 9511 (long) offset, (long) src_die->offset, cu->objfile->name);
c906108c
SS
9512}
9513
c906108c
SS
9514/* Decode simple location descriptions.
9515 Given a pointer to a dwarf block that defines a location, compute
9516 the location and return the value.
9517
4cecd739
DJ
9518 NOTE drow/2003-11-18: This function is called in two situations
9519 now: for the address of static or global variables (partial symbols
9520 only) and for offsets into structures which are expected to be
9521 (more or less) constant. The partial symbol case should go away,
9522 and only the constant case should remain. That will let this
9523 function complain more accurately. A few special modes are allowed
9524 without complaint for global variables (for instance, global
9525 register values and thread-local values).
c906108c
SS
9526
9527 A location description containing no operations indicates that the
4cecd739 9528 object is optimized out. The return value is 0 for that case.
6b992462
DJ
9529 FIXME drow/2003-11-16: No callers check for this case any more; soon all
9530 callers will only want a very basic result and this can become a
9531 complaint.
c906108c 9532
c906108c
SS
9533 Note that stack[0] is unused except as a default error return.
9534 Note that stack overflow is not yet handled. */
9535
9536static CORE_ADDR
e7c27a73 9537decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 9538{
e7c27a73
DJ
9539 struct objfile *objfile = cu->objfile;
9540 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
9541 int i;
9542 int size = blk->size;
fe1b8b76 9543 gdb_byte *data = blk->data;
c906108c
SS
9544 CORE_ADDR stack[64];
9545 int stacki;
9546 unsigned int bytes_read, unsnd;
fe1b8b76 9547 gdb_byte op;
c906108c
SS
9548
9549 i = 0;
9550 stacki = 0;
9551 stack[stacki] = 0;
c906108c
SS
9552
9553 while (i < size)
9554 {
c906108c
SS
9555 op = data[i++];
9556 switch (op)
9557 {
f1bea926
JM
9558 case DW_OP_lit0:
9559 case DW_OP_lit1:
9560 case DW_OP_lit2:
9561 case DW_OP_lit3:
9562 case DW_OP_lit4:
9563 case DW_OP_lit5:
9564 case DW_OP_lit6:
9565 case DW_OP_lit7:
9566 case DW_OP_lit8:
9567 case DW_OP_lit9:
9568 case DW_OP_lit10:
9569 case DW_OP_lit11:
9570 case DW_OP_lit12:
9571 case DW_OP_lit13:
9572 case DW_OP_lit14:
9573 case DW_OP_lit15:
9574 case DW_OP_lit16:
9575 case DW_OP_lit17:
9576 case DW_OP_lit18:
9577 case DW_OP_lit19:
9578 case DW_OP_lit20:
9579 case DW_OP_lit21:
9580 case DW_OP_lit22:
9581 case DW_OP_lit23:
9582 case DW_OP_lit24:
9583 case DW_OP_lit25:
9584 case DW_OP_lit26:
9585 case DW_OP_lit27:
9586 case DW_OP_lit28:
9587 case DW_OP_lit29:
9588 case DW_OP_lit30:
9589 case DW_OP_lit31:
9590 stack[++stacki] = op - DW_OP_lit0;
9591 break;
9592
c906108c
SS
9593 case DW_OP_reg0:
9594 case DW_OP_reg1:
9595 case DW_OP_reg2:
9596 case DW_OP_reg3:
9597 case DW_OP_reg4:
9598 case DW_OP_reg5:
9599 case DW_OP_reg6:
9600 case DW_OP_reg7:
9601 case DW_OP_reg8:
9602 case DW_OP_reg9:
9603 case DW_OP_reg10:
9604 case DW_OP_reg11:
9605 case DW_OP_reg12:
9606 case DW_OP_reg13:
9607 case DW_OP_reg14:
9608 case DW_OP_reg15:
9609 case DW_OP_reg16:
9610 case DW_OP_reg17:
9611 case DW_OP_reg18:
9612 case DW_OP_reg19:
9613 case DW_OP_reg20:
9614 case DW_OP_reg21:
9615 case DW_OP_reg22:
9616 case DW_OP_reg23:
9617 case DW_OP_reg24:
9618 case DW_OP_reg25:
9619 case DW_OP_reg26:
9620 case DW_OP_reg27:
9621 case DW_OP_reg28:
9622 case DW_OP_reg29:
9623 case DW_OP_reg30:
9624 case DW_OP_reg31:
c906108c 9625 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
9626 if (i < size)
9627 dwarf2_complex_location_expr_complaint ();
c906108c
SS
9628 break;
9629
9630 case DW_OP_regx:
c906108c
SS
9631 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
9632 i += bytes_read;
c906108c 9633 stack[++stacki] = unsnd;
4cecd739
DJ
9634 if (i < size)
9635 dwarf2_complex_location_expr_complaint ();
c906108c
SS
9636 break;
9637
9638 case DW_OP_addr:
107d2387 9639 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 9640 cu, &bytes_read);
107d2387 9641 i += bytes_read;
c906108c
SS
9642 break;
9643
9644 case DW_OP_const1u:
9645 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
9646 i += 1;
9647 break;
9648
9649 case DW_OP_const1s:
9650 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
9651 i += 1;
9652 break;
9653
9654 case DW_OP_const2u:
9655 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
9656 i += 2;
9657 break;
9658
9659 case DW_OP_const2s:
9660 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
9661 i += 2;
9662 break;
9663
9664 case DW_OP_const4u:
9665 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
9666 i += 4;
9667 break;
9668
9669 case DW_OP_const4s:
9670 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
9671 i += 4;
9672 break;
9673
9674 case DW_OP_constu:
9675 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 9676 &bytes_read);
c906108c
SS
9677 i += bytes_read;
9678 break;
9679
9680 case DW_OP_consts:
9681 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
9682 i += bytes_read;
9683 break;
9684
f1bea926
JM
9685 case DW_OP_dup:
9686 stack[stacki + 1] = stack[stacki];
9687 stacki++;
9688 break;
9689
c906108c
SS
9690 case DW_OP_plus:
9691 stack[stacki - 1] += stack[stacki];
9692 stacki--;
9693 break;
9694
9695 case DW_OP_plus_uconst:
9696 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
9697 i += bytes_read;
9698 break;
9699
9700 case DW_OP_minus:
f1bea926 9701 stack[stacki - 1] -= stack[stacki];
c906108c
SS
9702 stacki--;
9703 break;
9704
7a292a7a 9705 case DW_OP_deref:
7a292a7a 9706 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
9707 this using GDB's address_class enum. This is valid for partial
9708 global symbols, although the variable's address will be bogus
9709 in the psymtab. */
7a292a7a 9710 if (i < size)
4d3c2250 9711 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
9712 break;
9713
9d774e44 9714 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
9715 /* The top of the stack has the offset from the beginning
9716 of the thread control block at which the variable is located. */
9717 /* Nothing should follow this operator, so the top of stack would
9718 be returned. */
4cecd739
DJ
9719 /* This is valid for partial global symbols, but the variable's
9720 address will be bogus in the psymtab. */
9d774e44 9721 if (i < size)
4d3c2250 9722 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
9723 break;
9724
42be36b3
CT
9725 case DW_OP_GNU_uninit:
9726 break;
9727
c906108c 9728 default:
e2e0b3e5 9729 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
4d3c2250 9730 dwarf_stack_op_name (op));
c906108c
SS
9731 return (stack[stacki]);
9732 }
9733 }
9734 return (stack[stacki]);
9735}
9736
9737/* memory allocation interface */
9738
c906108c 9739static struct dwarf_block *
7b5a2f43 9740dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
9741{
9742 struct dwarf_block *blk;
9743
9744 blk = (struct dwarf_block *)
7b5a2f43 9745 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
9746 return (blk);
9747}
9748
9749static struct abbrev_info *
f3dd6933 9750dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
9751{
9752 struct abbrev_info *abbrev;
9753
f3dd6933
DJ
9754 abbrev = (struct abbrev_info *)
9755 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
9756 memset (abbrev, 0, sizeof (struct abbrev_info));
9757 return (abbrev);
9758}
9759
9760static struct die_info *
b60c80d6 9761dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
9762{
9763 struct die_info *die;
b60c80d6
DJ
9764 size_t size = sizeof (struct die_info);
9765
9766 if (num_attrs > 1)
9767 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 9768
b60c80d6 9769 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
9770 memset (die, 0, sizeof (struct die_info));
9771 return (die);
9772}
2e276125
JB
9773
9774\f
9775/* Macro support. */
9776
9777
9778/* Return the full name of file number I in *LH's file name table.
9779 Use COMP_DIR as the name of the current directory of the
9780 compilation. The result is allocated using xmalloc; the caller is
9781 responsible for freeing it. */
9782static char *
9783file_full_name (int file, struct line_header *lh, const char *comp_dir)
9784{
6a83a1e6
EZ
9785 /* Is the file number a valid index into the line header's file name
9786 table? Remember that file numbers start with one, not zero. */
9787 if (1 <= file && file <= lh->num_file_names)
9788 {
9789 struct file_entry *fe = &lh->file_names[file - 1];
2e276125 9790
6a83a1e6
EZ
9791 if (IS_ABSOLUTE_PATH (fe->name))
9792 return xstrdup (fe->name);
9793 else
9794 {
9795 const char *dir;
9796 int dir_len;
9797 char *full_name;
9798
9799 if (fe->dir_index)
9800 dir = lh->include_dirs[fe->dir_index - 1];
9801 else
9802 dir = comp_dir;
9803
9804 if (dir)
9805 {
9806 dir_len = strlen (dir);
9807 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
9808 strcpy (full_name, dir);
9809 full_name[dir_len] = '/';
9810 strcpy (full_name + dir_len + 1, fe->name);
9811 return full_name;
9812 }
9813 else
9814 return xstrdup (fe->name);
9815 }
9816 }
2e276125
JB
9817 else
9818 {
6a83a1e6
EZ
9819 /* The compiler produced a bogus file number. We can at least
9820 record the macro definitions made in the file, even if we
9821 won't be able to find the file by name. */
9822 char fake_name[80];
9823 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 9824
6a83a1e6
EZ
9825 complaint (&symfile_complaints,
9826 _("bad file number in macro information (%d)"),
9827 file);
2e276125 9828
6a83a1e6 9829 return xstrdup (fake_name);
2e276125
JB
9830 }
9831}
9832
9833
9834static struct macro_source_file *
9835macro_start_file (int file, int line,
9836 struct macro_source_file *current_file,
9837 const char *comp_dir,
9838 struct line_header *lh, struct objfile *objfile)
9839{
9840 /* The full name of this source file. */
9841 char *full_name = file_full_name (file, lh, comp_dir);
9842
9843 /* We don't create a macro table for this compilation unit
9844 at all until we actually get a filename. */
9845 if (! pending_macros)
4a146b47 9846 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 9847 objfile->macro_cache);
2e276125
JB
9848
9849 if (! current_file)
9850 /* If we have no current file, then this must be the start_file
9851 directive for the compilation unit's main source file. */
9852 current_file = macro_set_main (pending_macros, full_name);
9853 else
9854 current_file = macro_include (current_file, line, full_name);
9855
9856 xfree (full_name);
9857
9858 return current_file;
9859}
9860
9861
9862/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
9863 followed by a null byte. */
9864static char *
9865copy_string (const char *buf, int len)
9866{
9867 char *s = xmalloc (len + 1);
9868 memcpy (s, buf, len);
9869 s[len] = '\0';
9870
9871 return s;
9872}
9873
9874
9875static const char *
9876consume_improper_spaces (const char *p, const char *body)
9877{
9878 if (*p == ' ')
9879 {
4d3c2250 9880 complaint (&symfile_complaints,
e2e0b3e5 9881 _("macro definition contains spaces in formal argument list:\n`%s'"),
4d3c2250 9882 body);
2e276125
JB
9883
9884 while (*p == ' ')
9885 p++;
9886 }
9887
9888 return p;
9889}
9890
9891
9892static void
9893parse_macro_definition (struct macro_source_file *file, int line,
9894 const char *body)
9895{
9896 const char *p;
9897
9898 /* The body string takes one of two forms. For object-like macro
9899 definitions, it should be:
9900
9901 <macro name> " " <definition>
9902
9903 For function-like macro definitions, it should be:
9904
9905 <macro name> "() " <definition>
9906 or
9907 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
9908
9909 Spaces may appear only where explicitly indicated, and in the
9910 <definition>.
9911
9912 The Dwarf 2 spec says that an object-like macro's name is always
9913 followed by a space, but versions of GCC around March 2002 omit
9914 the space when the macro's definition is the empty string.
9915
9916 The Dwarf 2 spec says that there should be no spaces between the
9917 formal arguments in a function-like macro's formal argument list,
9918 but versions of GCC around March 2002 include spaces after the
9919 commas. */
9920
9921
9922 /* Find the extent of the macro name. The macro name is terminated
9923 by either a space or null character (for an object-like macro) or
9924 an opening paren (for a function-like macro). */
9925 for (p = body; *p; p++)
9926 if (*p == ' ' || *p == '(')
9927 break;
9928
9929 if (*p == ' ' || *p == '\0')
9930 {
9931 /* It's an object-like macro. */
9932 int name_len = p - body;
9933 char *name = copy_string (body, name_len);
9934 const char *replacement;
9935
9936 if (*p == ' ')
9937 replacement = body + name_len + 1;
9938 else
9939 {
4d3c2250 9940 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
9941 replacement = body + name_len;
9942 }
9943
9944 macro_define_object (file, line, name, replacement);
9945
9946 xfree (name);
9947 }
9948 else if (*p == '(')
9949 {
9950 /* It's a function-like macro. */
9951 char *name = copy_string (body, p - body);
9952 int argc = 0;
9953 int argv_size = 1;
9954 char **argv = xmalloc (argv_size * sizeof (*argv));
9955
9956 p++;
9957
9958 p = consume_improper_spaces (p, body);
9959
9960 /* Parse the formal argument list. */
9961 while (*p && *p != ')')
9962 {
9963 /* Find the extent of the current argument name. */
9964 const char *arg_start = p;
9965
9966 while (*p && *p != ',' && *p != ')' && *p != ' ')
9967 p++;
9968
9969 if (! *p || p == arg_start)
4d3c2250 9970 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
9971 else
9972 {
9973 /* Make sure argv has room for the new argument. */
9974 if (argc >= argv_size)
9975 {
9976 argv_size *= 2;
9977 argv = xrealloc (argv, argv_size * sizeof (*argv));
9978 }
9979
9980 argv[argc++] = copy_string (arg_start, p - arg_start);
9981 }
9982
9983 p = consume_improper_spaces (p, body);
9984
9985 /* Consume the comma, if present. */
9986 if (*p == ',')
9987 {
9988 p++;
9989
9990 p = consume_improper_spaces (p, body);
9991 }
9992 }
9993
9994 if (*p == ')')
9995 {
9996 p++;
9997
9998 if (*p == ' ')
9999 /* Perfectly formed definition, no complaints. */
10000 macro_define_function (file, line, name,
10001 argc, (const char **) argv,
10002 p + 1);
10003 else if (*p == '\0')
10004 {
10005 /* Complain, but do define it. */
4d3c2250 10006 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10007 macro_define_function (file, line, name,
10008 argc, (const char **) argv,
10009 p);
10010 }
10011 else
10012 /* Just complain. */
4d3c2250 10013 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10014 }
10015 else
10016 /* Just complain. */
4d3c2250 10017 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10018
10019 xfree (name);
10020 {
10021 int i;
10022
10023 for (i = 0; i < argc; i++)
10024 xfree (argv[i]);
10025 }
10026 xfree (argv);
10027 }
10028 else
4d3c2250 10029 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
10030}
10031
10032
10033static void
10034dwarf_decode_macros (struct line_header *lh, unsigned int offset,
10035 char *comp_dir, bfd *abfd,
e7c27a73 10036 struct dwarf2_cu *cu)
2e276125 10037{
fe1b8b76 10038 gdb_byte *mac_ptr, *mac_end;
2e276125 10039 struct macro_source_file *current_file = 0;
757a13d0
JK
10040 enum dwarf_macinfo_record_type macinfo_type;
10041 int at_commandline;
2e276125 10042
6502dd73 10043 if (dwarf2_per_objfile->macinfo_buffer == NULL)
2e276125 10044 {
e2e0b3e5 10045 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
2e276125
JB
10046 return;
10047 }
10048
757a13d0
JK
10049 /* First pass: Find the name of the base filename.
10050 This filename is needed in order to process all macros whose definition
10051 (or undefinition) comes from the command line. These macros are defined
10052 before the first DW_MACINFO_start_file entry, and yet still need to be
10053 associated to the base file.
10054
10055 To determine the base file name, we scan the macro definitions until we
10056 reach the first DW_MACINFO_start_file entry. We then initialize
10057 CURRENT_FILE accordingly so that any macro definition found before the
10058 first DW_MACINFO_start_file can still be associated to the base file. */
10059
6502dd73
DJ
10060 mac_ptr = dwarf2_per_objfile->macinfo_buffer + offset;
10061 mac_end = dwarf2_per_objfile->macinfo_buffer
10062 + dwarf2_per_objfile->macinfo_size;
2e276125 10063
757a13d0 10064 do
2e276125 10065 {
2e276125
JB
10066 /* Do we at least have room for a macinfo type byte? */
10067 if (mac_ptr >= mac_end)
10068 {
757a13d0
JK
10069 /* Complaint is printed during the second pass as GDB will probably
10070 stop the first pass earlier upon finding DW_MACINFO_start_file. */
10071 break;
2e276125
JB
10072 }
10073
10074 macinfo_type = read_1_byte (abfd, mac_ptr);
10075 mac_ptr++;
10076
10077 switch (macinfo_type)
10078 {
10079 /* A zero macinfo type indicates the end of the macro
10080 information. */
10081 case 0:
757a13d0
JK
10082 break;
10083
10084 case DW_MACINFO_define:
10085 case DW_MACINFO_undef:
10086 /* Only skip the data by MAC_PTR. */
10087 {
10088 unsigned int bytes_read;
10089
10090 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10091 mac_ptr += bytes_read;
10092 read_string (abfd, mac_ptr, &bytes_read);
10093 mac_ptr += bytes_read;
10094 }
10095 break;
10096
10097 case DW_MACINFO_start_file:
10098 {
10099 unsigned int bytes_read;
10100 int line, file;
10101
10102 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10103 mac_ptr += bytes_read;
10104 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10105 mac_ptr += bytes_read;
10106
10107 current_file = macro_start_file (file, line, current_file, comp_dir,
10108 lh, cu->objfile);
10109 }
10110 break;
10111
10112 case DW_MACINFO_end_file:
10113 /* No data to skip by MAC_PTR. */
10114 break;
10115
10116 case DW_MACINFO_vendor_ext:
10117 /* Only skip the data by MAC_PTR. */
10118 {
10119 unsigned int bytes_read;
10120
10121 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10122 mac_ptr += bytes_read;
10123 read_string (abfd, mac_ptr, &bytes_read);
10124 mac_ptr += bytes_read;
10125 }
10126 break;
10127
10128 default:
10129 break;
10130 }
10131 } while (macinfo_type != 0 && current_file == NULL);
10132
10133 /* Second pass: Process all entries.
10134
10135 Use the AT_COMMAND_LINE flag to determine whether we are still processing
10136 command-line macro definitions/undefinitions. This flag is unset when we
10137 reach the first DW_MACINFO_start_file entry. */
10138
10139 mac_ptr = dwarf2_per_objfile->macinfo_buffer + offset;
10140
10141 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
10142 GDB is still reading the definitions from command line. First
10143 DW_MACINFO_start_file will need to be ignored as it was already executed
10144 to create CURRENT_FILE for the main source holding also the command line
10145 definitions. On first met DW_MACINFO_start_file this flag is reset to
10146 normally execute all the remaining DW_MACINFO_start_file macinfos. */
10147
10148 at_commandline = 1;
10149
10150 do
10151 {
10152 /* Do we at least have room for a macinfo type byte? */
10153 if (mac_ptr >= mac_end)
10154 {
10155 dwarf2_macros_too_long_complaint ();
10156 break;
10157 }
10158
10159 macinfo_type = read_1_byte (abfd, mac_ptr);
10160 mac_ptr++;
10161
10162 switch (macinfo_type)
10163 {
10164 /* A zero macinfo type indicates the end of the macro
10165 information. */
10166 case 0:
10167 break;
2e276125
JB
10168
10169 case DW_MACINFO_define:
10170 case DW_MACINFO_undef:
10171 {
891d2f0b 10172 unsigned int bytes_read;
2e276125
JB
10173 int line;
10174 char *body;
10175
10176 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10177 mac_ptr += bytes_read;
10178 body = read_string (abfd, mac_ptr, &bytes_read);
10179 mac_ptr += bytes_read;
10180
10181 if (! current_file)
757a13d0
JK
10182 {
10183 /* DWARF violation as no main source is present. */
10184 complaint (&symfile_complaints,
10185 _("debug info with no main source gives macro %s "
10186 "on line %d: %s"),
10187 macinfo_type ==
10188 DW_MACINFO_define ? _("definition") : macinfo_type ==
10189 DW_MACINFO_undef ? _("undefinition") :
10190 "something-or-other", line, body);
10191 break;
10192 }
10193 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
4d3c2250 10194 complaint (&symfile_complaints,
757a13d0
JK
10195 _("debug info gives %s macro %s with %s line %d: %s"),
10196 at_commandline ? _("command-line") : _("in-file"),
4d3c2250 10197 macinfo_type ==
757a13d0
JK
10198 DW_MACINFO_define ? _("definition") : macinfo_type ==
10199 DW_MACINFO_undef ? _("undefinition") :
10200 "something-or-other",
10201 line == 0 ? _("zero") : _("non-zero"), line, body);
10202
10203 if (macinfo_type == DW_MACINFO_define)
10204 parse_macro_definition (current_file, line, body);
10205 else if (macinfo_type == DW_MACINFO_undef)
10206 macro_undef (current_file, line, body);
2e276125
JB
10207 }
10208 break;
10209
10210 case DW_MACINFO_start_file:
10211 {
891d2f0b 10212 unsigned int bytes_read;
2e276125
JB
10213 int line, file;
10214
10215 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10216 mac_ptr += bytes_read;
10217 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10218 mac_ptr += bytes_read;
10219
757a13d0
JK
10220 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
10221 complaint (&symfile_complaints,
10222 _("debug info gives source %d included "
10223 "from %s at %s line %d"),
10224 file, at_commandline ? _("command-line") : _("file"),
10225 line == 0 ? _("zero") : _("non-zero"), line);
10226
10227 if (at_commandline)
10228 {
10229 /* This DW_MACINFO_start_file was executed in the pass one. */
10230 at_commandline = 0;
10231 }
10232 else
10233 current_file = macro_start_file (file, line,
10234 current_file, comp_dir,
10235 lh, cu->objfile);
2e276125
JB
10236 }
10237 break;
10238
10239 case DW_MACINFO_end_file:
10240 if (! current_file)
4d3c2250 10241 complaint (&symfile_complaints,
e2e0b3e5 10242 _("macro debug info has an unmatched `close_file' directive"));
2e276125
JB
10243 else
10244 {
10245 current_file = current_file->included_by;
10246 if (! current_file)
10247 {
10248 enum dwarf_macinfo_record_type next_type;
10249
10250 /* GCC circa March 2002 doesn't produce the zero
10251 type byte marking the end of the compilation
10252 unit. Complain if it's not there, but exit no
10253 matter what. */
10254
10255 /* Do we at least have room for a macinfo type byte? */
10256 if (mac_ptr >= mac_end)
10257 {
4d3c2250 10258 dwarf2_macros_too_long_complaint ();
2e276125
JB
10259 return;
10260 }
10261
10262 /* We don't increment mac_ptr here, so this is just
10263 a look-ahead. */
10264 next_type = read_1_byte (abfd, mac_ptr);
10265 if (next_type != 0)
4d3c2250 10266 complaint (&symfile_complaints,
e2e0b3e5 10267 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
2e276125
JB
10268
10269 return;
10270 }
10271 }
10272 break;
10273
10274 case DW_MACINFO_vendor_ext:
10275 {
891d2f0b 10276 unsigned int bytes_read;
2e276125
JB
10277 int constant;
10278 char *string;
10279
10280 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
10281 mac_ptr += bytes_read;
10282 string = read_string (abfd, mac_ptr, &bytes_read);
10283 mac_ptr += bytes_read;
10284
10285 /* We don't recognize any vendor extensions. */
10286 }
10287 break;
10288 }
757a13d0 10289 } while (macinfo_type != 0);
2e276125 10290}
8e19ed76
PS
10291
10292/* Check if the attribute's form is a DW_FORM_block*
10293 if so return true else false. */
10294static int
10295attr_form_is_block (struct attribute *attr)
10296{
10297 return (attr == NULL ? 0 :
10298 attr->form == DW_FORM_block1
10299 || attr->form == DW_FORM_block2
10300 || attr->form == DW_FORM_block4
10301 || attr->form == DW_FORM_block);
10302}
4c2df51b 10303
c6a0999f
JB
10304/* Return non-zero if ATTR's value is a section offset --- classes
10305 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
10306 You may use DW_UNSND (attr) to retrieve such offsets.
10307
10308 Section 7.5.4, "Attribute Encodings", explains that no attribute
10309 may have a value that belongs to more than one of these classes; it
10310 would be ambiguous if we did, because we use the same forms for all
10311 of them. */
3690dd37
JB
10312static int
10313attr_form_is_section_offset (struct attribute *attr)
10314{
10315 return (attr->form == DW_FORM_data4
10316 || attr->form == DW_FORM_data8);
10317}
10318
10319
10320/* Return non-zero if ATTR's value falls in the 'constant' class, or
10321 zero otherwise. When this function returns true, you can apply
10322 dwarf2_get_attr_constant_value to it.
10323
10324 However, note that for some attributes you must check
10325 attr_form_is_section_offset before using this test. DW_FORM_data4
10326 and DW_FORM_data8 are members of both the constant class, and of
10327 the classes that contain offsets into other debug sections
10328 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
10329 that, if an attribute's can be either a constant or one of the
10330 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
10331 taken as section offsets, not constants. */
10332static int
10333attr_form_is_constant (struct attribute *attr)
10334{
10335 switch (attr->form)
10336 {
10337 case DW_FORM_sdata:
10338 case DW_FORM_udata:
10339 case DW_FORM_data1:
10340 case DW_FORM_data2:
10341 case DW_FORM_data4:
10342 case DW_FORM_data8:
10343 return 1;
10344 default:
10345 return 0;
10346 }
10347}
10348
4c2df51b
DJ
10349static void
10350dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 10351 struct dwarf2_cu *cu)
4c2df51b 10352{
3690dd37 10353 if (attr_form_is_section_offset (attr)
99bcc461
DJ
10354 /* ".debug_loc" may not exist at all, or the offset may be outside
10355 the section. If so, fall through to the complaint in the
10356 other branch. */
10357 && DW_UNSND (attr) < dwarf2_per_objfile->loc_size)
4c2df51b 10358 {
0d53c4c4 10359 struct dwarf2_loclist_baton *baton;
4c2df51b 10360
4a146b47 10361 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 10362 sizeof (struct dwarf2_loclist_baton));
ae0d2f24
UW
10363 baton->per_cu = cu->per_cu;
10364 gdb_assert (baton->per_cu);
4c2df51b 10365
0d53c4c4
DJ
10366 /* We don't know how long the location list is, but make sure we
10367 don't run off the edge of the section. */
6502dd73
DJ
10368 baton->size = dwarf2_per_objfile->loc_size - DW_UNSND (attr);
10369 baton->data = dwarf2_per_objfile->loc_buffer + DW_UNSND (attr);
d00adf39
DE
10370 baton->base_address = cu->base_address;
10371 if (cu->base_known == 0)
0d53c4c4 10372 complaint (&symfile_complaints,
e2e0b3e5 10373 _("Location list used without specifying the CU base address."));
4c2df51b 10374
a67af2b9 10375 SYMBOL_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
10376 SYMBOL_LOCATION_BATON (sym) = baton;
10377 }
10378 else
10379 {
10380 struct dwarf2_locexpr_baton *baton;
10381
4a146b47 10382 baton = obstack_alloc (&cu->objfile->objfile_obstack,
0d53c4c4 10383 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
10384 baton->per_cu = cu->per_cu;
10385 gdb_assert (baton->per_cu);
0d53c4c4
DJ
10386
10387 if (attr_form_is_block (attr))
10388 {
10389 /* Note that we're just copying the block's data pointer
10390 here, not the actual data. We're still pointing into the
6502dd73
DJ
10391 info_buffer for SYM's objfile; right now we never release
10392 that buffer, but when we do clean up properly this may
10393 need to change. */
0d53c4c4
DJ
10394 baton->size = DW_BLOCK (attr)->size;
10395 baton->data = DW_BLOCK (attr)->data;
10396 }
10397 else
10398 {
10399 dwarf2_invalid_attrib_class_complaint ("location description",
10400 SYMBOL_NATURAL_NAME (sym));
10401 baton->size = 0;
10402 baton->data = NULL;
10403 }
10404
a67af2b9 10405 SYMBOL_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
10406 SYMBOL_LOCATION_BATON (sym) = baton;
10407 }
4c2df51b 10408}
6502dd73 10409
ae0d2f24
UW
10410/* Return the OBJFILE associated with the compilation unit CU. */
10411
10412struct objfile *
10413dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
10414{
10415 struct objfile *objfile = per_cu->psymtab->objfile;
10416
10417 /* Return the master objfile, so that we can report and look up the
10418 correct file containing this variable. */
10419 if (objfile->separate_debug_objfile_backlink)
10420 objfile = objfile->separate_debug_objfile_backlink;
10421
10422 return objfile;
10423}
10424
10425/* Return the address size given in the compilation unit header for CU. */
10426
10427CORE_ADDR
10428dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
10429{
10430 if (per_cu->cu)
10431 return per_cu->cu->header.addr_size;
10432 else
10433 {
10434 /* If the CU is not currently read in, we re-read its header. */
10435 struct objfile *objfile = per_cu->psymtab->objfile;
10436 struct dwarf2_per_objfile *per_objfile
10437 = objfile_data (objfile, dwarf2_objfile_data_key);
10438 gdb_byte *info_ptr = per_objfile->info_buffer + per_cu->offset;
10439
10440 struct comp_unit_head cu_header;
10441 memset (&cu_header, 0, sizeof cu_header);
10442 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
10443 return cu_header.addr_size;
10444 }
10445}
10446
ae038cb0 10447/* Locate the compilation unit from CU's objfile which contains the
10b3939b 10448 DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
10449
10450static struct dwarf2_per_cu_data *
c764a876 10451dwarf2_find_containing_comp_unit (unsigned int offset,
ae038cb0
DJ
10452 struct objfile *objfile)
10453{
10454 struct dwarf2_per_cu_data *this_cu;
10455 int low, high;
10456
ae038cb0
DJ
10457 low = 0;
10458 high = dwarf2_per_objfile->n_comp_units - 1;
10459 while (high > low)
10460 {
10461 int mid = low + (high - low) / 2;
10462 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
10463 high = mid;
10464 else
10465 low = mid + 1;
10466 }
10467 gdb_assert (low == high);
10468 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
10469 {
10b3939b 10470 if (low == 0)
8a3fe4f8
AC
10471 error (_("Dwarf Error: could not find partial DIE containing "
10472 "offset 0x%lx [in module %s]"),
10b3939b
DJ
10473 (long) offset, bfd_get_filename (objfile->obfd));
10474
ae038cb0
DJ
10475 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
10476 return dwarf2_per_objfile->all_comp_units[low-1];
10477 }
10478 else
10479 {
10480 this_cu = dwarf2_per_objfile->all_comp_units[low];
10481 if (low == dwarf2_per_objfile->n_comp_units - 1
10482 && offset >= this_cu->offset + this_cu->length)
c764a876 10483 error (_("invalid dwarf2 offset %u"), offset);
ae038cb0
DJ
10484 gdb_assert (offset < this_cu->offset + this_cu->length);
10485 return this_cu;
10486 }
10487}
10488
10b3939b
DJ
10489/* Locate the compilation unit from OBJFILE which is located at exactly
10490 OFFSET. Raises an error on failure. */
10491
ae038cb0 10492static struct dwarf2_per_cu_data *
c764a876 10493dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
ae038cb0
DJ
10494{
10495 struct dwarf2_per_cu_data *this_cu;
10496 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
10497 if (this_cu->offset != offset)
c764a876 10498 error (_("no compilation unit with offset %u."), offset);
ae038cb0
DJ
10499 return this_cu;
10500}
10501
10502/* Release one cached compilation unit, CU. We unlink it from the tree
10503 of compilation units, but we don't remove it from the read_in_chain;
10504 the caller is responsible for that. */
10505
10506static void
10507free_one_comp_unit (void *data)
10508{
10509 struct dwarf2_cu *cu = data;
10510
10511 if (cu->per_cu != NULL)
10512 cu->per_cu->cu = NULL;
10513 cu->per_cu = NULL;
10514
10515 obstack_free (&cu->comp_unit_obstack, NULL);
10516
10517 xfree (cu);
10518}
10519
72bf9492 10520/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0
DJ
10521 when we're finished with it. We can't free the pointer itself, but be
10522 sure to unlink it from the cache. Also release any associated storage
10523 and perform cache maintenance.
72bf9492
DJ
10524
10525 Only used during partial symbol parsing. */
10526
10527static void
10528free_stack_comp_unit (void *data)
10529{
10530 struct dwarf2_cu *cu = data;
10531
10532 obstack_free (&cu->comp_unit_obstack, NULL);
10533 cu->partial_dies = NULL;
ae038cb0
DJ
10534
10535 if (cu->per_cu != NULL)
10536 {
10537 /* This compilation unit is on the stack in our caller, so we
10538 should not xfree it. Just unlink it. */
10539 cu->per_cu->cu = NULL;
10540 cu->per_cu = NULL;
10541
10542 /* If we had a per-cu pointer, then we may have other compilation
10543 units loaded, so age them now. */
10544 age_cached_comp_units ();
10545 }
10546}
10547
10548/* Free all cached compilation units. */
10549
10550static void
10551free_cached_comp_units (void *data)
10552{
10553 struct dwarf2_per_cu_data *per_cu, **last_chain;
10554
10555 per_cu = dwarf2_per_objfile->read_in_chain;
10556 last_chain = &dwarf2_per_objfile->read_in_chain;
10557 while (per_cu != NULL)
10558 {
10559 struct dwarf2_per_cu_data *next_cu;
10560
10561 next_cu = per_cu->cu->read_in_chain;
10562
10563 free_one_comp_unit (per_cu->cu);
10564 *last_chain = next_cu;
10565
10566 per_cu = next_cu;
10567 }
10568}
10569
10570/* Increase the age counter on each cached compilation unit, and free
10571 any that are too old. */
10572
10573static void
10574age_cached_comp_units (void)
10575{
10576 struct dwarf2_per_cu_data *per_cu, **last_chain;
10577
10578 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
10579 per_cu = dwarf2_per_objfile->read_in_chain;
10580 while (per_cu != NULL)
10581 {
10582 per_cu->cu->last_used ++;
10583 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
10584 dwarf2_mark (per_cu->cu);
10585 per_cu = per_cu->cu->read_in_chain;
10586 }
10587
10588 per_cu = dwarf2_per_objfile->read_in_chain;
10589 last_chain = &dwarf2_per_objfile->read_in_chain;
10590 while (per_cu != NULL)
10591 {
10592 struct dwarf2_per_cu_data *next_cu;
10593
10594 next_cu = per_cu->cu->read_in_chain;
10595
10596 if (!per_cu->cu->mark)
10597 {
10598 free_one_comp_unit (per_cu->cu);
10599 *last_chain = next_cu;
10600 }
10601 else
10602 last_chain = &per_cu->cu->read_in_chain;
10603
10604 per_cu = next_cu;
10605 }
10606}
10607
10608/* Remove a single compilation unit from the cache. */
10609
10610static void
10611free_one_cached_comp_unit (void *target_cu)
10612{
10613 struct dwarf2_per_cu_data *per_cu, **last_chain;
10614
10615 per_cu = dwarf2_per_objfile->read_in_chain;
10616 last_chain = &dwarf2_per_objfile->read_in_chain;
10617 while (per_cu != NULL)
10618 {
10619 struct dwarf2_per_cu_data *next_cu;
10620
10621 next_cu = per_cu->cu->read_in_chain;
10622
10623 if (per_cu->cu == target_cu)
10624 {
10625 free_one_comp_unit (per_cu->cu);
10626 *last_chain = next_cu;
10627 break;
10628 }
10629 else
10630 last_chain = &per_cu->cu->read_in_chain;
10631
10632 per_cu = next_cu;
10633 }
10634}
10635
fe3e1990
DJ
10636/* Release all extra memory associated with OBJFILE. */
10637
10638void
10639dwarf2_free_objfile (struct objfile *objfile)
10640{
10641 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
10642
10643 if (dwarf2_per_objfile == NULL)
10644 return;
10645
10646 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
10647 free_cached_comp_units (NULL);
10648
10649 /* Everything else should be on the objfile obstack. */
10650}
10651
1c379e20
DJ
10652/* A pair of DIE offset and GDB type pointer. We store these
10653 in a hash table separate from the DIEs, and preserve them
10654 when the DIEs are flushed out of cache. */
10655
10656struct dwarf2_offset_and_type
10657{
10658 unsigned int offset;
10659 struct type *type;
10660};
10661
10662/* Hash function for a dwarf2_offset_and_type. */
10663
10664static hashval_t
10665offset_and_type_hash (const void *item)
10666{
10667 const struct dwarf2_offset_and_type *ofs = item;
10668 return ofs->offset;
10669}
10670
10671/* Equality function for a dwarf2_offset_and_type. */
10672
10673static int
10674offset_and_type_eq (const void *item_lhs, const void *item_rhs)
10675{
10676 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
10677 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
10678 return ofs_lhs->offset == ofs_rhs->offset;
10679}
10680
10681/* Set the type associated with DIE to TYPE. Save it in CU's hash
f792889a 10682 table if necessary. For convenience, return TYPE. */
1c379e20 10683
f792889a 10684static struct type *
1c379e20
DJ
10685set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10686{
10687 struct dwarf2_offset_and_type **slot, ofs;
10688
f792889a
DJ
10689 if (cu->type_hash == NULL)
10690 {
10691 gdb_assert (cu->per_cu != NULL);
10692 cu->per_cu->type_hash
10693 = htab_create_alloc_ex (cu->header.length / 24,
10694 offset_and_type_hash,
10695 offset_and_type_eq,
10696 NULL,
10697 &cu->objfile->objfile_obstack,
10698 hashtab_obstack_allocate,
10699 dummy_obstack_deallocate);
10700 cu->type_hash = cu->per_cu->type_hash;
10701 }
1c379e20
DJ
10702
10703 ofs.offset = die->offset;
10704 ofs.type = type;
10705 slot = (struct dwarf2_offset_and_type **)
f792889a 10706 htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
1c379e20
DJ
10707 *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
10708 **slot = ofs;
f792889a 10709 return type;
1c379e20
DJ
10710}
10711
f792889a
DJ
10712/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
10713 not have a saved type. */
1c379e20
DJ
10714
10715static struct type *
f792889a 10716get_die_type (struct die_info *die, struct dwarf2_cu *cu)
1c379e20
DJ
10717{
10718 struct dwarf2_offset_and_type *slot, ofs;
f792889a
DJ
10719 htab_t type_hash = cu->type_hash;
10720
10721 if (type_hash == NULL)
10722 return NULL;
1c379e20
DJ
10723
10724 ofs.offset = die->offset;
10725 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
10726 if (slot)
10727 return slot->type;
10728 else
10729 return NULL;
10730}
10731
10b3939b
DJ
10732/* Set the mark field in CU and in every other compilation unit in the
10733 cache that we must keep because we are keeping CU. */
10734
10735/* Add a dependence relationship from CU to REF_PER_CU. */
10736
10737static void
10738dwarf2_add_dependence (struct dwarf2_cu *cu,
10739 struct dwarf2_per_cu_data *ref_per_cu)
10740{
10741 void **slot;
10742
10743 if (cu->dependencies == NULL)
10744 cu->dependencies
10745 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
10746 NULL, &cu->comp_unit_obstack,
10747 hashtab_obstack_allocate,
10748 dummy_obstack_deallocate);
10749
10750 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
10751 if (*slot == NULL)
10752 *slot = ref_per_cu;
10753}
1c379e20 10754
ae038cb0
DJ
10755/* Set the mark field in CU and in every other compilation unit in the
10756 cache that we must keep because we are keeping CU. */
10757
10b3939b
DJ
10758static int
10759dwarf2_mark_helper (void **slot, void *data)
10760{
10761 struct dwarf2_per_cu_data *per_cu;
10762
10763 per_cu = (struct dwarf2_per_cu_data *) *slot;
10764 if (per_cu->cu->mark)
10765 return 1;
10766 per_cu->cu->mark = 1;
10767
10768 if (per_cu->cu->dependencies != NULL)
10769 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
10770
10771 return 1;
10772}
10773
ae038cb0
DJ
10774static void
10775dwarf2_mark (struct dwarf2_cu *cu)
10776{
10777 if (cu->mark)
10778 return;
10779 cu->mark = 1;
10b3939b
DJ
10780 if (cu->dependencies != NULL)
10781 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
10782}
10783
10784static void
10785dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
10786{
10787 while (per_cu)
10788 {
10789 per_cu->cu->mark = 0;
10790 per_cu = per_cu->cu->read_in_chain;
10791 }
72bf9492
DJ
10792}
10793
72bf9492
DJ
10794/* Trivial hash function for partial_die_info: the hash value of a DIE
10795 is its offset in .debug_info for this objfile. */
10796
10797static hashval_t
10798partial_die_hash (const void *item)
10799{
10800 const struct partial_die_info *part_die = item;
10801 return part_die->offset;
10802}
10803
10804/* Trivial comparison function for partial_die_info structures: two DIEs
10805 are equal if they have the same offset. */
10806
10807static int
10808partial_die_eq (const void *item_lhs, const void *item_rhs)
10809{
10810 const struct partial_die_info *part_die_lhs = item_lhs;
10811 const struct partial_die_info *part_die_rhs = item_rhs;
10812 return part_die_lhs->offset == part_die_rhs->offset;
10813}
10814
ae038cb0
DJ
10815static struct cmd_list_element *set_dwarf2_cmdlist;
10816static struct cmd_list_element *show_dwarf2_cmdlist;
10817
10818static void
10819set_dwarf2_cmd (char *args, int from_tty)
10820{
10821 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
10822}
10823
10824static void
10825show_dwarf2_cmd (char *args, int from_tty)
10826{
10827 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
10828}
10829
6502dd73
DJ
10830void _initialize_dwarf2_read (void);
10831
10832void
10833_initialize_dwarf2_read (void)
10834{
10835 dwarf2_objfile_data_key = register_objfile_data ();
ae038cb0 10836
1bedd215
AC
10837 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
10838Set DWARF 2 specific variables.\n\
10839Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
10840 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
10841 0/*allow-unknown*/, &maintenance_set_cmdlist);
10842
1bedd215
AC
10843 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
10844Show DWARF 2 specific variables\n\
10845Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
10846 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
10847 0/*allow-unknown*/, &maintenance_show_cmdlist);
10848
10849 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
10850 &dwarf2_max_cache_age, _("\
10851Set the upper bound on the age of cached dwarf2 compilation units."), _("\
10852Show the upper bound on the age of cached dwarf2 compilation units."), _("\
10853A higher limit means that cached compilation units will be stored\n\
10854in memory longer, and more total memory will be used. Zero disables\n\
10855caching, which can slow down startup."),
2c5b56ce 10856 NULL,
920d2a44 10857 show_dwarf2_max_cache_age,
2c5b56ce 10858 &set_dwarf2_cmdlist,
ae038cb0 10859 &show_dwarf2_cmdlist);
d97bc12b
DE
10860
10861 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
10862Set debugging of the dwarf2 DIE reader."), _("\
10863Show debugging of the dwarf2 DIE reader."), _("\
10864When enabled (non-zero), DIEs are dumped after they are read in.\n\
10865The value is the maximum depth to print."),
10866 NULL,
10867 NULL,
10868 &setdebuglist, &showdebuglist);
6502dd73 10869}
This page took 1.502015 seconds and 4 git commands to generate.