2004-01-14 David Carlton <carlton@bactrian.org>
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
b44e9041
EZ
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004
8e65ff28 4 Free Software Foundation, Inc.
c906108c
SS
5
6 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
7 Inc. with support from Florida State University (under contract
8 with the Ada Joint Program Office), and Silicon Graphics, Inc.
9 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
10 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
11 support in dwarfread.c
12
c5aa993b 13 This file is part of GDB.
c906108c 14
c5aa993b
JM
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or (at
18 your option) any later version.
c906108c 19
c5aa993b
JM
20 This program is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
c906108c 24
c5aa993b
JM
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
c906108c
SS
29
30#include "defs.h"
31#include "bfd.h"
c906108c
SS
32#include "symtab.h"
33#include "gdbtypes.h"
34#include "symfile.h"
35#include "objfiles.h"
36#include "elf/dwarf2.h"
37#include "buildsym.h"
38#include "demangle.h"
39#include "expression.h"
d5166ae1 40#include "filenames.h" /* for DOSish file names */
2e276125 41#include "macrotab.h"
c906108c
SS
42#include "language.h"
43#include "complaints.h"
357e46e7 44#include "bcache.h"
4c2df51b
DJ
45#include "dwarf2expr.h"
46#include "dwarf2loc.h"
9219021c 47#include "cp-support.h"
4c2df51b 48
c906108c
SS
49#include <fcntl.h>
50#include "gdb_string.h"
4bdf3d34 51#include "gdb_assert.h"
c906108c
SS
52#include <sys/types.h>
53
88496bb5
MS
54#ifndef DWARF2_REG_TO_REGNUM
55#define DWARF2_REG_TO_REGNUM(REG) (REG)
56#endif
57
107d2387 58#if 0
357e46e7 59/* .debug_info header for a compilation unit
c906108c
SS
60 Because of alignment constraints, this structure has padding and cannot
61 be mapped directly onto the beginning of the .debug_info section. */
62typedef struct comp_unit_header
63 {
64 unsigned int length; /* length of the .debug_info
65 contribution */
66 unsigned short version; /* version number -- 2 for DWARF
67 version 2 */
68 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
69 unsigned char addr_size; /* byte size of an address -- 4 */
70 }
71_COMP_UNIT_HEADER;
72#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
107d2387 73#endif
c906108c
SS
74
75/* .debug_pubnames header
76 Because of alignment constraints, this structure has padding and cannot
77 be mapped directly onto the beginning of the .debug_info section. */
78typedef struct pubnames_header
79 {
80 unsigned int length; /* length of the .debug_pubnames
81 contribution */
82 unsigned char version; /* version number -- 2 for DWARF
83 version 2 */
84 unsigned int info_offset; /* offset into .debug_info section */
85 unsigned int info_size; /* byte size of .debug_info section
86 portion */
87 }
88_PUBNAMES_HEADER;
89#define _ACTUAL_PUBNAMES_HEADER_SIZE 13
90
91/* .debug_pubnames header
92 Because of alignment constraints, this structure has padding and cannot
93 be mapped directly onto the beginning of the .debug_info section. */
94typedef struct aranges_header
95 {
96 unsigned int length; /* byte len of the .debug_aranges
97 contribution */
98 unsigned short version; /* version number -- 2 for DWARF
99 version 2 */
100 unsigned int info_offset; /* offset into .debug_info section */
101 unsigned char addr_size; /* byte size of an address */
102 unsigned char seg_size; /* byte size of segment descriptor */
103 }
104_ARANGES_HEADER;
105#define _ACTUAL_ARANGES_HEADER_SIZE 12
106
107/* .debug_line statement program prologue
108 Because of alignment constraints, this structure has padding and cannot
109 be mapped directly onto the beginning of the .debug_info section. */
110typedef struct statement_prologue
111 {
112 unsigned int total_length; /* byte length of the statement
113 information */
114 unsigned short version; /* version number -- 2 for DWARF
115 version 2 */
116 unsigned int prologue_length; /* # bytes between prologue &
117 stmt program */
118 unsigned char minimum_instruction_length; /* byte size of
119 smallest instr */
120 unsigned char default_is_stmt; /* initial value of is_stmt
121 register */
122 char line_base;
123 unsigned char line_range;
124 unsigned char opcode_base; /* number assigned to first special
125 opcode */
126 unsigned char *standard_opcode_lengths;
127 }
128_STATEMENT_PROLOGUE;
129
130/* offsets and sizes of debugging sections */
131
c906108c
SS
132static unsigned int dwarf_info_size;
133static unsigned int dwarf_abbrev_size;
134static unsigned int dwarf_line_size;
135static unsigned int dwarf_pubnames_size;
136static unsigned int dwarf_aranges_size;
137static unsigned int dwarf_loc_size;
138static unsigned int dwarf_macinfo_size;
139static unsigned int dwarf_str_size;
af34e669 140static unsigned int dwarf_ranges_size;
b6af0555
JS
141unsigned int dwarf_frame_size;
142unsigned int dwarf_eh_frame_size;
c906108c 143
086df311
DJ
144static asection *dwarf_info_section;
145static asection *dwarf_abbrev_section;
146static asection *dwarf_line_section;
147static asection *dwarf_pubnames_section;
148static asection *dwarf_aranges_section;
149static asection *dwarf_loc_section;
150static asection *dwarf_macinfo_section;
151static asection *dwarf_str_section;
152static asection *dwarf_ranges_section;
153asection *dwarf_frame_section;
154asection *dwarf_eh_frame_section;
155
c906108c
SS
156/* names of the debugging sections */
157
158#define INFO_SECTION ".debug_info"
159#define ABBREV_SECTION ".debug_abbrev"
160#define LINE_SECTION ".debug_line"
161#define PUBNAMES_SECTION ".debug_pubnames"
162#define ARANGES_SECTION ".debug_aranges"
163#define LOC_SECTION ".debug_loc"
164#define MACINFO_SECTION ".debug_macinfo"
165#define STR_SECTION ".debug_str"
af34e669 166#define RANGES_SECTION ".debug_ranges"
b6af0555
JS
167#define FRAME_SECTION ".debug_frame"
168#define EH_FRAME_SECTION ".eh_frame"
c906108c
SS
169
170/* local data types */
171
57349743
JB
172/* We hold several abbreviation tables in memory at the same time. */
173#ifndef ABBREV_HASH_SIZE
174#define ABBREV_HASH_SIZE 121
175#endif
176
107d2387
AC
177/* The data in a compilation unit header, after target2host
178 translation, looks like this. */
c906108c
SS
179struct comp_unit_head
180 {
613e1657 181 unsigned long length;
c906108c
SS
182 short version;
183 unsigned int abbrev_offset;
184 unsigned char addr_size;
107d2387 185 unsigned char signed_addr_p;
613e1657
KB
186 unsigned int offset_size; /* size of file offsets; either 4 or 8 */
187 unsigned int initial_length_size; /* size of the length field; either
188 4 or 12 */
57349743
JB
189
190 /* Offset to the first byte of this compilation unit header in the
191 * .debug_info section, for resolving relative reference dies. */
192
193 unsigned int offset;
194
195 /* Pointer to this compilation unit header in the .debug_info
196 * section */
197
198 char *cu_head_ptr;
199
200 /* Pointer to the first die of this compilatio unit. This will
201 * be the first byte following the compilation unit header. */
202
203 char *first_die_ptr;
204
205 /* Pointer to the next compilation unit header in the program. */
206
207 struct comp_unit_head *next;
208
209 /* DWARF abbreviation table associated with this compilation unit */
210
211 struct abbrev_info *dwarf2_abbrevs[ABBREV_HASH_SIZE];
af34e669 212
0d53c4c4 213 /* Base address of this compilation unit. */
af34e669 214
0d53c4c4
DJ
215 CORE_ADDR base_address;
216
217 /* Non-zero if base_address has been set. */
218
219 int base_known;
c906108c
SS
220 };
221
e7c27a73
DJ
222/* Internal state when decoding a particular compilation unit. */
223struct dwarf2_cu
224{
225 /* The objfile containing this compilation unit. */
226 struct objfile *objfile;
227
228 /* The header of the compilation unit.
229
230 FIXME drow/2003-11-10: Some of the things from the comp_unit_head
231 should be moved to the dwarf2_cu structure; for instance the abbrevs
232 hash table. */
233 struct comp_unit_head header;
234};
235
debd256d
JB
236/* The line number information for a compilation unit (found in the
237 .debug_line section) begins with a "statement program header",
238 which contains the following information. */
239struct line_header
240{
241 unsigned int total_length;
242 unsigned short version;
243 unsigned int header_length;
244 unsigned char minimum_instruction_length;
245 unsigned char default_is_stmt;
246 int line_base;
247 unsigned char line_range;
248 unsigned char opcode_base;
249
250 /* standard_opcode_lengths[i] is the number of operands for the
251 standard opcode whose value is i. This means that
252 standard_opcode_lengths[0] is unused, and the last meaningful
253 element is standard_opcode_lengths[opcode_base - 1]. */
254 unsigned char *standard_opcode_lengths;
255
256 /* The include_directories table. NOTE! These strings are not
257 allocated with xmalloc; instead, they are pointers into
258 debug_line_buffer. If you try to free them, `free' will get
259 indigestion. */
260 unsigned int num_include_dirs, include_dirs_size;
261 char **include_dirs;
262
263 /* The file_names table. NOTE! These strings are not allocated
264 with xmalloc; instead, they are pointers into debug_line_buffer.
265 Don't try to free them directly. */
266 unsigned int num_file_names, file_names_size;
267 struct file_entry
c906108c 268 {
debd256d
JB
269 char *name;
270 unsigned int dir_index;
271 unsigned int mod_time;
272 unsigned int length;
273 } *file_names;
274
275 /* The start and end of the statement program following this
276 header. These point into dwarf_line_buffer. */
277 char *statement_program_start, *statement_program_end;
278};
c906108c
SS
279
280/* When we construct a partial symbol table entry we only
281 need this much information. */
282struct partial_die_info
283 {
284 enum dwarf_tag tag;
285 unsigned char has_children;
286 unsigned char is_external;
287 unsigned char is_declaration;
288 unsigned char has_type;
289 unsigned int offset;
290 unsigned int abbrev;
291 char *name;
0b010bcc 292 int has_pc_info;
c906108c
SS
293 CORE_ADDR lowpc;
294 CORE_ADDR highpc;
295 struct dwarf_block *locdesc;
296 unsigned int language;
297 char *sibling;
298 };
299
300/* This data structure holds the information of an abbrev. */
301struct abbrev_info
302 {
303 unsigned int number; /* number identifying abbrev */
304 enum dwarf_tag tag; /* dwarf tag */
305 int has_children; /* boolean */
306 unsigned int num_attrs; /* number of attributes */
307 struct attr_abbrev *attrs; /* an array of attribute descriptions */
308 struct abbrev_info *next; /* next in chain */
309 };
310
311struct attr_abbrev
312 {
313 enum dwarf_attribute name;
314 enum dwarf_form form;
315 };
316
317/* This data structure holds a complete die structure. */
318struct die_info
319 {
c5aa993b 320 enum dwarf_tag tag; /* Tag indicating type of die */
c5aa993b
JM
321 unsigned int abbrev; /* Abbrev number */
322 unsigned int offset; /* Offset in .debug_info section */
323 unsigned int num_attrs; /* Number of attributes */
324 struct attribute *attrs; /* An array of attributes */
325 struct die_info *next_ref; /* Next die in ref hash table */
78ba4af6
JB
326
327 /* The dies in a compilation unit form an n-ary tree. PARENT
328 points to this die's parent; CHILD points to the first child of
329 this node; and all the children of a given node are chained
330 together via their SIBLING fields, terminated by a die whose
331 tag is zero. */
639d11d3
DC
332 struct die_info *child; /* Its first child, if any. */
333 struct die_info *sibling; /* Its next sibling, if any. */
334 struct die_info *parent; /* Its parent, if any. */
78ba4af6 335
c5aa993b 336 struct type *type; /* Cached type information */
c906108c
SS
337 };
338
339/* Attributes have a name and a value */
340struct attribute
341 {
342 enum dwarf_attribute name;
343 enum dwarf_form form;
344 union
345 {
346 char *str;
347 struct dwarf_block *blk;
ce5d95e1
JB
348 unsigned long unsnd;
349 long int snd;
c906108c
SS
350 CORE_ADDR addr;
351 }
352 u;
353 };
354
5fb290d7
DJ
355struct function_range
356{
357 const char *name;
358 CORE_ADDR lowpc, highpc;
359 int seen_line;
360 struct function_range *next;
361};
362
363static struct function_range *cu_first_fn, *cu_last_fn, *cu_cached_fn;
364
c906108c
SS
365/* Get at parts of an attribute structure */
366
367#define DW_STRING(attr) ((attr)->u.str)
368#define DW_UNSND(attr) ((attr)->u.unsnd)
369#define DW_BLOCK(attr) ((attr)->u.blk)
370#define DW_SND(attr) ((attr)->u.snd)
371#define DW_ADDR(attr) ((attr)->u.addr)
372
373/* Blocks are a bunch of untyped bytes. */
374struct dwarf_block
375 {
376 unsigned int size;
377 char *data;
378 };
379
c906108c
SS
380#ifndef ATTR_ALLOC_CHUNK
381#define ATTR_ALLOC_CHUNK 4
382#endif
383
c906108c
SS
384/* A hash table of die offsets for following references. */
385#ifndef REF_HASH_SIZE
386#define REF_HASH_SIZE 1021
387#endif
388
389static struct die_info *die_ref_table[REF_HASH_SIZE];
390
391/* Obstack for allocating temporary storage used during symbol reading. */
392static struct obstack dwarf2_tmp_obstack;
393
394/* Offset to the first byte of the current compilation unit header,
395 for resolving relative reference dies. */
396static unsigned int cu_header_offset;
397
398/* Allocate fields for structs, unions and enums in this size. */
399#ifndef DW_FIELD_ALLOC_CHUNK
400#define DW_FIELD_ALLOC_CHUNK 4
401#endif
402
403/* The language we are debugging. */
404static enum language cu_language;
405static const struct language_defn *cu_language_defn;
406
407/* Actually data from the sections. */
408static char *dwarf_info_buffer;
409static char *dwarf_abbrev_buffer;
410static char *dwarf_line_buffer;
4bdf3d34 411static char *dwarf_str_buffer;
2e276125 412static char *dwarf_macinfo_buffer;
af34e669 413static char *dwarf_ranges_buffer;
0d53c4c4 414static char *dwarf_loc_buffer;
c906108c
SS
415
416/* A zeroed version of a partial die for initialization purposes. */
417static struct partial_die_info zeroed_partial_die;
418
419/* The generic symbol table building routines have separate lists for
420 file scope symbols and all all other scopes (local scopes). So
421 we need to select the right one to pass to add_symbol_to_list().
422 We do it by keeping a pointer to the correct list in list_in_scope.
423
424 FIXME: The original dwarf code just treated the file scope as the first
425 local scope, and all other local scopes as nested local scopes, and worked
426 fine. Check to see if we really need to distinguish these
427 in buildsym.c. */
428static struct pending **list_in_scope = &file_symbols;
429
7a292a7a
SS
430/* FIXME: decode_locdesc sets these variables to describe the location
431 to the caller. These ought to be a structure or something. If
432 none of the flags are set, the object lives at the address returned
433 by decode_locdesc. */
434
7a292a7a
SS
435static int isreg; /* Object lives in register.
436 decode_locdesc's return value is
437 the register number. */
c906108c 438
357e46e7 439/* This value is added to each symbol value. FIXME: Generalize to
c906108c
SS
440 the section_offsets structure used by dbxread (once this is done,
441 pass the appropriate section number to end_symtab). */
442static CORE_ADDR baseaddr; /* Add to each symbol value */
443
444/* We put a pointer to this structure in the read_symtab_private field
445 of the psymtab.
446 The complete dwarf information for an objfile is kept in the
447 psymbol_obstack, so that absolute die references can be handled.
448 Most of the information in this structure is related to an entire
449 object file and could be passed via the sym_private field of the objfile.
450 It is however conceivable that dwarf2 might not be the only type
451 of symbols read from an object file. */
452
453struct dwarf2_pinfo
c5aa993b
JM
454 {
455 /* Pointer to start of dwarf info buffer for the objfile. */
c906108c 456
c5aa993b 457 char *dwarf_info_buffer;
c906108c 458
c5aa993b 459 /* Offset in dwarf_info_buffer for this compilation unit. */
c906108c 460
c5aa993b 461 unsigned long dwarf_info_offset;
c906108c 462
c5aa993b 463 /* Pointer to start of dwarf abbreviation buffer for the objfile. */
c906108c 464
c5aa993b 465 char *dwarf_abbrev_buffer;
c906108c 466
c5aa993b 467 /* Size of dwarf abbreviation section for the objfile. */
c906108c 468
c5aa993b 469 unsigned int dwarf_abbrev_size;
c906108c 470
c5aa993b 471 /* Pointer to start of dwarf line buffer for the objfile. */
c906108c 472
c5aa993b 473 char *dwarf_line_buffer;
4bdf3d34 474
9ab3e532
JB
475 /* Size of dwarf_line_buffer, in bytes. */
476
477 unsigned int dwarf_line_size;
478
4bdf3d34
JJ
479 /* Pointer to start of dwarf string buffer for the objfile. */
480
481 char *dwarf_str_buffer;
482
483 /* Size of dwarf string section for the objfile. */
484
485 unsigned int dwarf_str_size;
2e276125
JB
486
487 /* Pointer to start of dwarf macro buffer for the objfile. */
488
489 char *dwarf_macinfo_buffer;
490
491 /* Size of dwarf macinfo section for the objfile. */
492
493 unsigned int dwarf_macinfo_size;
494
af34e669
DJ
495 /* Pointer to start of dwarf ranges buffer for the objfile. */
496
497 char *dwarf_ranges_buffer;
498
499 /* Size of dwarf ranges buffer for the objfile. */
500
501 unsigned int dwarf_ranges_size;
502
0d53c4c4
DJ
503 /* Pointer to start of dwarf locations buffer for the objfile. */
504
505 char *dwarf_loc_buffer;
506
507 /* Size of dwarf locations buffer for the objfile. */
508
509 unsigned int dwarf_loc_size;
c5aa993b 510 };
c906108c
SS
511
512#define PST_PRIVATE(p) ((struct dwarf2_pinfo *)(p)->read_symtab_private)
513#define DWARF_INFO_BUFFER(p) (PST_PRIVATE(p)->dwarf_info_buffer)
514#define DWARF_INFO_OFFSET(p) (PST_PRIVATE(p)->dwarf_info_offset)
515#define DWARF_ABBREV_BUFFER(p) (PST_PRIVATE(p)->dwarf_abbrev_buffer)
516#define DWARF_ABBREV_SIZE(p) (PST_PRIVATE(p)->dwarf_abbrev_size)
517#define DWARF_LINE_BUFFER(p) (PST_PRIVATE(p)->dwarf_line_buffer)
9ab3e532 518#define DWARF_LINE_SIZE(p) (PST_PRIVATE(p)->dwarf_line_size)
4bdf3d34
JJ
519#define DWARF_STR_BUFFER(p) (PST_PRIVATE(p)->dwarf_str_buffer)
520#define DWARF_STR_SIZE(p) (PST_PRIVATE(p)->dwarf_str_size)
2e276125
JB
521#define DWARF_MACINFO_BUFFER(p) (PST_PRIVATE(p)->dwarf_macinfo_buffer)
522#define DWARF_MACINFO_SIZE(p) (PST_PRIVATE(p)->dwarf_macinfo_size)
af34e669
DJ
523#define DWARF_RANGES_BUFFER(p) (PST_PRIVATE(p)->dwarf_ranges_buffer)
524#define DWARF_RANGES_SIZE(p) (PST_PRIVATE(p)->dwarf_ranges_size)
0d53c4c4
DJ
525#define DWARF_LOC_BUFFER(p) (PST_PRIVATE(p)->dwarf_loc_buffer)
526#define DWARF_LOC_SIZE(p) (PST_PRIVATE(p)->dwarf_loc_size)
c906108c
SS
527
528/* Maintain an array of referenced fundamental types for the current
529 compilation unit being read. For DWARF version 1, we have to construct
530 the fundamental types on the fly, since no information about the
531 fundamental types is supplied. Each such fundamental type is created by
532 calling a language dependent routine to create the type, and then a
533 pointer to that type is then placed in the array at the index specified
534 by it's FT_<TYPENAME> value. The array has a fixed size set by the
535 FT_NUM_MEMBERS compile time constant, which is the number of predefined
536 fundamental types gdb knows how to construct. */
537static struct type *ftypes[FT_NUM_MEMBERS]; /* Fundamental types */
538
539/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
540 but this would require a corresponding change in unpack_field_as_long
541 and friends. */
542static int bits_per_byte = 8;
543
544/* The routines that read and process dies for a C struct or C++ class
545 pass lists of data member fields and lists of member function fields
546 in an instance of a field_info structure, as defined below. */
547struct field_info
c5aa993b
JM
548 {
549 /* List of data member and baseclasses fields. */
550 struct nextfield
551 {
552 struct nextfield *next;
553 int accessibility;
554 int virtuality;
555 struct field field;
556 }
557 *fields;
c906108c 558
c5aa993b
JM
559 /* Number of fields. */
560 int nfields;
c906108c 561
c5aa993b
JM
562 /* Number of baseclasses. */
563 int nbaseclasses;
c906108c 564
c5aa993b
JM
565 /* Set if the accesibility of one of the fields is not public. */
566 int non_public_fields;
c906108c 567
c5aa993b
JM
568 /* Member function fields array, entries are allocated in the order they
569 are encountered in the object file. */
570 struct nextfnfield
571 {
572 struct nextfnfield *next;
573 struct fn_field fnfield;
574 }
575 *fnfields;
c906108c 576
c5aa993b
JM
577 /* Member function fieldlist array, contains name of possibly overloaded
578 member function, number of overloaded member functions and a pointer
579 to the head of the member function field chain. */
580 struct fnfieldlist
581 {
582 char *name;
583 int length;
584 struct nextfnfield *head;
585 }
586 *fnfieldlists;
c906108c 587
c5aa993b
JM
588 /* Number of entries in the fnfieldlists array. */
589 int nfnfields;
590 };
c906108c 591
c906108c
SS
592/* Various complaints about symbol reading that don't abort the process */
593
4d3c2250
KB
594static void
595dwarf2_non_const_array_bound_ignored_complaint (const char *arg1)
2e276125 596{
4d3c2250
KB
597 complaint (&symfile_complaints, "non-constant array bounds form '%s' ignored",
598 arg1);
599}
600
601static void
602dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 603{
4d3c2250
KB
604 complaint (&symfile_complaints,
605 "statement list doesn't fit in .debug_line section");
606}
607
608static void
609dwarf2_complex_location_expr_complaint (void)
2e276125 610{
4d3c2250
KB
611 complaint (&symfile_complaints, "location expression too complex");
612}
613
614static void
615dwarf2_unsupported_at_frame_base_complaint (const char *arg1)
2e276125 616{
4d3c2250
KB
617 complaint (&symfile_complaints,
618 "unsupported DW_AT_frame_base for function '%s'", arg1);
619}
620
621static void
622dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
623 int arg3)
2e276125 624{
4d3c2250
KB
625 complaint (&symfile_complaints,
626 "const value length mismatch for '%s', got %d, expected %d", arg1,
627 arg2, arg3);
628}
629
630static void
631dwarf2_macros_too_long_complaint (void)
2e276125 632{
4d3c2250
KB
633 complaint (&symfile_complaints,
634 "macro info runs off end of `.debug_macinfo' section");
635}
636
637static void
638dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 639{
4d3c2250
KB
640 complaint (&symfile_complaints,
641 "macro debug info contains a malformed macro definition:\n`%s'",
642 arg1);
643}
644
645static void
646dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 647{
4d3c2250
KB
648 complaint (&symfile_complaints,
649 "invalid attribute class or form for '%s' in '%s'", arg1, arg2);
650}
c906108c 651
c906108c
SS
652/* local function prototypes */
653
4efb68b1 654static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c
SS
655
656#if 0
a14ed312 657static void dwarf2_build_psymtabs_easy (struct objfile *, int);
c906108c
SS
658#endif
659
a14ed312 660static void dwarf2_build_psymtabs_hard (struct objfile *, int);
c906108c 661
e7c27a73
DJ
662static char *scan_partial_symbols (char *, CORE_ADDR *, CORE_ADDR *,
663 struct dwarf2_cu *,
5c4e30ca 664 const char *namespace);
c906108c 665
e7c27a73 666static void add_partial_symbol (struct partial_die_info *, struct dwarf2_cu *,
5c4e30ca 667 const char *namespace);
c906108c 668
63d06c5c
DC
669static int pdi_needs_namespace (enum dwarf_tag tag, const char *namespace);
670
91c24f0a
DC
671static char *add_partial_namespace (struct partial_die_info *pdi,
672 char *info_ptr,
91c24f0a 673 CORE_ADDR *lowpc, CORE_ADDR *highpc,
e7c27a73 674 struct dwarf2_cu *cu,
5c4e30ca 675 const char *namespace);
91c24f0a 676
63d06c5c
DC
677static char *add_partial_structure (struct partial_die_info *struct_pdi,
678 char *info_ptr,
679 struct dwarf2_cu *cu,
680 const char *namespace);
681
91c24f0a
DC
682static char *add_partial_enumeration (struct partial_die_info *enum_pdi,
683 char *info_ptr,
e7c27a73 684 struct dwarf2_cu *cu,
5c4e30ca 685 const char *namespace);
91c24f0a
DC
686
687static char *locate_pdi_sibling (struct partial_die_info *orig_pdi,
688 char *info_ptr,
689 bfd *abfd,
e7c27a73 690 struct dwarf2_cu *cu);
91c24f0a 691
a14ed312 692static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 693
a14ed312 694static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 695
188dd5d6 696char *dwarf2_read_section (struct objfile *, asection *);
c906108c 697
e7c27a73 698static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
c906108c 699
4efb68b1 700static void dwarf2_empty_abbrev_table (void *);
c906108c 701
57349743 702static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 703 struct dwarf2_cu *);
c906108c 704
a14ed312 705static char *read_partial_die (struct partial_die_info *,
e7c27a73 706 bfd *, char *, struct dwarf2_cu *);
c906108c 707
107d2387 708static char *read_full_die (struct die_info **, bfd *, char *,
e7c27a73 709 struct dwarf2_cu *, int *);
c906108c 710
a14ed312 711static char *read_attribute (struct attribute *, struct attr_abbrev *,
e7c27a73 712 bfd *, char *, struct dwarf2_cu *);
c906108c 713
a8329558 714static char *read_attribute_value (struct attribute *, unsigned,
e7c27a73 715 bfd *, char *, struct dwarf2_cu *);
a8329558 716
a14ed312 717static unsigned int read_1_byte (bfd *, char *);
c906108c 718
a14ed312 719static int read_1_signed_byte (bfd *, char *);
c906108c 720
a14ed312 721static unsigned int read_2_bytes (bfd *, char *);
c906108c 722
a14ed312 723static unsigned int read_4_bytes (bfd *, char *);
c906108c 724
ce5d95e1 725static unsigned long read_8_bytes (bfd *, char *);
c906108c 726
e7c27a73 727static CORE_ADDR read_address (bfd *, char *ptr, struct dwarf2_cu *,
107d2387 728 int *bytes_read);
c906108c 729
613e1657
KB
730static LONGEST read_initial_length (bfd *, char *,
731 struct comp_unit_head *, int *bytes_read);
732
733static LONGEST read_offset (bfd *, char *, const struct comp_unit_head *,
734 int *bytes_read);
735
a14ed312 736static char *read_n_bytes (bfd *, char *, unsigned int);
c906108c 737
a14ed312 738static char *read_string (bfd *, char *, unsigned int *);
c906108c 739
4bdf3d34
JJ
740static char *read_indirect_string (bfd *, char *, const struct comp_unit_head *,
741 unsigned int *);
742
ce5d95e1 743static unsigned long read_unsigned_leb128 (bfd *, char *, unsigned int *);
c906108c 744
ce5d95e1 745static long read_signed_leb128 (bfd *, char *, unsigned int *);
c906108c 746
a14ed312 747static void set_cu_language (unsigned int);
c906108c 748
a14ed312 749static struct attribute *dwarf_attr (struct die_info *, unsigned int);
c906108c 750
3ca72b44
AC
751static int die_is_declaration (struct die_info *);
752
63d06c5c
DC
753static struct die_info *die_specification (struct die_info *die);
754
debd256d
JB
755static void free_line_header (struct line_header *lh);
756
757static struct line_header *(dwarf_decode_line_header
758 (unsigned int offset,
e7c27a73 759 bfd *abfd, struct dwarf2_cu *cu));
debd256d
JB
760
761static void dwarf_decode_lines (struct line_header *, char *, bfd *,
e7c27a73 762 struct dwarf2_cu *);
c906108c 763
a14ed312 764static void dwarf2_start_subfile (char *, char *);
c906108c 765
a14ed312 766static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 767 struct dwarf2_cu *);
c906108c 768
a14ed312 769static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 770 struct dwarf2_cu *);
c906108c 771
2df3850c
JM
772static void dwarf2_const_value_data (struct attribute *attr,
773 struct symbol *sym,
774 int bits);
775
e7c27a73 776static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 777
e7c27a73
DJ
778static struct type *die_containing_type (struct die_info *,
779 struct dwarf2_cu *);
c906108c
SS
780
781#if 0
a14ed312 782static struct type *type_at_offset (unsigned int, struct objfile *);
c906108c
SS
783#endif
784
e7c27a73 785static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
c906108c 786
e7c27a73 787static void read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 788
63d06c5c
DC
789static char *determine_prefix (struct die_info *die);
790
791static char *typename_concat (const char *prefix, const char *suffix);
792
793static char *class_name (struct die_info *die);
794
e7c27a73 795static void read_typedef (struct die_info *, struct dwarf2_cu *);
c906108c 796
e7c27a73 797static void read_base_type (struct die_info *, struct dwarf2_cu *);
c906108c 798
e7c27a73 799static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 800
e7c27a73 801static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 802
e7c27a73 803static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 804
a14ed312 805static int dwarf2_get_pc_bounds (struct die_info *,
e7c27a73 806 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *);
c906108c 807
a14ed312 808static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 809 struct dwarf2_cu *);
c906108c 810
a14ed312 811static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 812 struct type *, struct dwarf2_cu *);
c906108c 813
a14ed312 814static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 815 struct die_info *, struct type *,
e7c27a73 816 struct dwarf2_cu *);
c906108c 817
a14ed312 818static void dwarf2_attach_fn_fields_to_type (struct field_info *,
e7c27a73 819 struct type *, struct dwarf2_cu *);
c906108c 820
e7c27a73 821static void read_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 822
e7c27a73 823static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 824
e7c27a73 825static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 826
38d518c9
EZ
827static const char *namespace_name (struct die_info *die,
828 int *is_anonymous);
829
e7c27a73 830static void read_enumeration (struct die_info *, struct dwarf2_cu *);
c906108c 831
e7c27a73 832static struct type *dwarf_base_type (int, int, struct dwarf2_cu *);
c906108c 833
e7c27a73 834static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 835
e7c27a73 836static void read_array_type (struct die_info *, struct dwarf2_cu *);
c906108c 837
e7c27a73 838static void read_tag_pointer_type (struct die_info *, struct dwarf2_cu *);
c906108c 839
e7c27a73
DJ
840static void read_tag_ptr_to_member_type (struct die_info *,
841 struct dwarf2_cu *);
c906108c 842
e7c27a73 843static void read_tag_reference_type (struct die_info *, struct dwarf2_cu *);
c906108c 844
e7c27a73 845static void read_tag_const_type (struct die_info *, struct dwarf2_cu *);
c906108c 846
e7c27a73 847static void read_tag_volatile_type (struct die_info *, struct dwarf2_cu *);
c906108c 848
e7c27a73 849static void read_tag_string_type (struct die_info *, struct dwarf2_cu *);
c906108c 850
e7c27a73 851static void read_subroutine_type (struct die_info *, struct dwarf2_cu *);
c906108c 852
e7c27a73 853static struct die_info *read_comp_unit (char *, bfd *, struct dwarf2_cu *);
c906108c 854
639d11d3 855static struct die_info *read_die_and_children (char *info_ptr, bfd *abfd,
e7c27a73 856 struct dwarf2_cu *,
639d11d3
DC
857 char **new_info_ptr,
858 struct die_info *parent);
859
860static struct die_info *read_die_and_siblings (char *info_ptr, bfd *abfd,
e7c27a73 861 struct dwarf2_cu *,
639d11d3
DC
862 char **new_info_ptr,
863 struct die_info *parent);
864
a14ed312 865static void free_die_list (struct die_info *);
c906108c 866
74b7792f
AC
867static struct cleanup *make_cleanup_free_die_list (struct die_info *);
868
e7c27a73 869static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 870
a14ed312 871static char *dwarf2_linkage_name (struct die_info *);
c906108c 872
9219021c
DC
873static char *dwarf2_name (struct die_info *die);
874
875static struct die_info *dwarf2_extension (struct die_info *die);
876
a14ed312 877static char *dwarf_tag_name (unsigned int);
c906108c 878
a14ed312 879static char *dwarf_attr_name (unsigned int);
c906108c 880
a14ed312 881static char *dwarf_form_name (unsigned int);
c906108c 882
a14ed312 883static char *dwarf_stack_op_name (unsigned int);
c906108c 884
a14ed312 885static char *dwarf_bool_name (unsigned int);
c906108c 886
a14ed312 887static char *dwarf_type_encoding_name (unsigned int);
c906108c
SS
888
889#if 0
a14ed312 890static char *dwarf_cfi_name (unsigned int);
c906108c 891
a14ed312 892struct die_info *copy_die (struct die_info *);
c906108c
SS
893#endif
894
f9aca02d 895static struct die_info *sibling_die (struct die_info *);
c906108c 896
f9aca02d 897static void dump_die (struct die_info *);
c906108c 898
f9aca02d 899static void dump_die_list (struct die_info *);
c906108c 900
f9aca02d 901static void store_in_ref_table (unsigned int, struct die_info *);
c906108c 902
7f0e3f52 903static void dwarf2_empty_hash_tables (void);
c906108c 904
a14ed312 905static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
c906108c 906
f9aca02d 907static struct die_info *follow_die_ref (unsigned int);
c906108c 908
a14ed312 909static struct type *dwarf2_fundamental_type (struct objfile *, int);
c906108c
SS
910
911/* memory allocation interface */
912
4efb68b1 913static void dwarf2_free_tmp_obstack (void *);
c906108c 914
a14ed312 915static struct dwarf_block *dwarf_alloc_block (void);
c906108c 916
a14ed312 917static struct abbrev_info *dwarf_alloc_abbrev (void);
c906108c 918
a14ed312 919static struct die_info *dwarf_alloc_die (void);
c906108c 920
5fb290d7
DJ
921static void initialize_cu_func_list (void);
922
923static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR);
924
2e276125 925static void dwarf_decode_macros (struct line_header *, unsigned int,
e7c27a73 926 char *, bfd *, struct dwarf2_cu *);
2e276125 927
8e19ed76
PS
928static int attr_form_is_block (struct attribute *);
929
4c2df51b
DJ
930static void
931dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 932 struct dwarf2_cu *cu);
4c2df51b 933
c906108c
SS
934/* Try to locate the sections we need for DWARF 2 debugging
935 information and return true if we have enough to do something. */
936
937int
fba45db2 938dwarf2_has_info (bfd *abfd)
c906108c 939{
188dd5d6
DJ
940 dwarf_info_section = 0;
941 dwarf_abbrev_section = 0;
942 dwarf_line_section = 0;
943 dwarf_str_section = 0;
944 dwarf_macinfo_section = 0;
945 dwarf_frame_section = 0;
946 dwarf_eh_frame_section = 0;
947 dwarf_ranges_section = 0;
948 dwarf_loc_section = 0;
af34e669 949
c906108c 950 bfd_map_over_sections (abfd, dwarf2_locate_sections, NULL);
188dd5d6 951 return (dwarf_info_section != NULL && dwarf_abbrev_section != NULL);
c906108c
SS
952}
953
954/* This function is mapped across the sections and remembers the
955 offset and size of each of the debugging sections we are interested
956 in. */
957
958static void
4efb68b1 959dwarf2_locate_sections (bfd *ignore_abfd, asection *sectp, void *ignore_ptr)
c906108c 960{
6314a349 961 if (strcmp (sectp->name, INFO_SECTION) == 0)
c906108c 962 {
c906108c 963 dwarf_info_size = bfd_get_section_size_before_reloc (sectp);
086df311 964 dwarf_info_section = sectp;
c906108c 965 }
6314a349 966 else if (strcmp (sectp->name, ABBREV_SECTION) == 0)
c906108c 967 {
c906108c 968 dwarf_abbrev_size = bfd_get_section_size_before_reloc (sectp);
086df311 969 dwarf_abbrev_section = sectp;
c906108c 970 }
6314a349 971 else if (strcmp (sectp->name, LINE_SECTION) == 0)
c906108c 972 {
c906108c 973 dwarf_line_size = bfd_get_section_size_before_reloc (sectp);
086df311 974 dwarf_line_section = sectp;
c906108c 975 }
6314a349 976 else if (strcmp (sectp->name, PUBNAMES_SECTION) == 0)
c906108c 977 {
c906108c 978 dwarf_pubnames_size = bfd_get_section_size_before_reloc (sectp);
086df311 979 dwarf_pubnames_section = sectp;
c906108c 980 }
6314a349 981 else if (strcmp (sectp->name, ARANGES_SECTION) == 0)
c906108c 982 {
c906108c 983 dwarf_aranges_size = bfd_get_section_size_before_reloc (sectp);
086df311 984 dwarf_aranges_section = sectp;
c906108c 985 }
6314a349 986 else if (strcmp (sectp->name, LOC_SECTION) == 0)
c906108c 987 {
c906108c 988 dwarf_loc_size = bfd_get_section_size_before_reloc (sectp);
086df311 989 dwarf_loc_section = sectp;
c906108c 990 }
6314a349 991 else if (strcmp (sectp->name, MACINFO_SECTION) == 0)
c906108c 992 {
c906108c 993 dwarf_macinfo_size = bfd_get_section_size_before_reloc (sectp);
0cf824c9 994 dwarf_macinfo_section = sectp;
c906108c 995 }
6314a349 996 else if (strcmp (sectp->name, STR_SECTION) == 0)
c906108c 997 {
c906108c 998 dwarf_str_size = bfd_get_section_size_before_reloc (sectp);
086df311 999 dwarf_str_section = sectp;
c906108c 1000 }
6314a349 1001 else if (strcmp (sectp->name, FRAME_SECTION) == 0)
b6af0555 1002 {
b6af0555 1003 dwarf_frame_size = bfd_get_section_size_before_reloc (sectp);
086df311 1004 dwarf_frame_section = sectp;
b6af0555 1005 }
6314a349 1006 else if (strcmp (sectp->name, EH_FRAME_SECTION) == 0)
b6af0555 1007 {
3799ccc6
EZ
1008 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1009 if (aflag & SEC_HAS_CONTENTS)
1010 {
3799ccc6
EZ
1011 dwarf_eh_frame_size = bfd_get_section_size_before_reloc (sectp);
1012 dwarf_eh_frame_section = sectp;
1013 }
b6af0555 1014 }
6314a349 1015 else if (strcmp (sectp->name, RANGES_SECTION) == 0)
af34e669 1016 {
af34e669 1017 dwarf_ranges_size = bfd_get_section_size_before_reloc (sectp);
6f10aeb1 1018 dwarf_ranges_section = sectp;
af34e669 1019 }
c906108c
SS
1020}
1021
1022/* Build a partial symbol table. */
1023
1024void
fba45db2 1025dwarf2_build_psymtabs (struct objfile *objfile, int mainline)
c906108c
SS
1026{
1027
1028 /* We definitely need the .debug_info and .debug_abbrev sections */
1029
188dd5d6
DJ
1030 dwarf_info_buffer = dwarf2_read_section (objfile, dwarf_info_section);
1031 dwarf_abbrev_buffer = dwarf2_read_section (objfile, dwarf_abbrev_section);
1032
1033 if (dwarf_line_section)
1034 dwarf_line_buffer = dwarf2_read_section (objfile, dwarf_line_section);
41ff2da1
DC
1035 else
1036 dwarf_line_buffer = NULL;
c906108c 1037
188dd5d6
DJ
1038 if (dwarf_str_section)
1039 dwarf_str_buffer = dwarf2_read_section (objfile, dwarf_str_section);
4bdf3d34
JJ
1040 else
1041 dwarf_str_buffer = NULL;
1042
188dd5d6 1043 if (dwarf_macinfo_section)
2e276125 1044 dwarf_macinfo_buffer = dwarf2_read_section (objfile,
086df311 1045 dwarf_macinfo_section);
2e276125
JB
1046 else
1047 dwarf_macinfo_buffer = NULL;
1048
188dd5d6
DJ
1049 if (dwarf_ranges_section)
1050 dwarf_ranges_buffer = dwarf2_read_section (objfile, dwarf_ranges_section);
af34e669
DJ
1051 else
1052 dwarf_ranges_buffer = NULL;
1053
188dd5d6
DJ
1054 if (dwarf_loc_section)
1055 dwarf_loc_buffer = dwarf2_read_section (objfile, dwarf_loc_section);
0d53c4c4
DJ
1056 else
1057 dwarf_loc_buffer = NULL;
1058
ef96bde8
EZ
1059 if (mainline
1060 || (objfile->global_psymbols.size == 0
1061 && objfile->static_psymbols.size == 0))
c906108c
SS
1062 {
1063 init_psymbol_list (objfile, 1024);
1064 }
1065
1066#if 0
1067 if (dwarf_aranges_offset && dwarf_pubnames_offset)
1068 {
d4f3574e 1069 /* Things are significantly easier if we have .debug_aranges and
c906108c
SS
1070 .debug_pubnames sections */
1071
d4f3574e 1072 dwarf2_build_psymtabs_easy (objfile, mainline);
c906108c
SS
1073 }
1074 else
1075#endif
1076 /* only test this case for now */
c5aa993b 1077 {
c906108c 1078 /* In this case we have to work a bit harder */
d4f3574e 1079 dwarf2_build_psymtabs_hard (objfile, mainline);
c906108c
SS
1080 }
1081}
1082
1083#if 0
1084/* Build the partial symbol table from the information in the
1085 .debug_pubnames and .debug_aranges sections. */
1086
1087static void
fba45db2 1088dwarf2_build_psymtabs_easy (struct objfile *objfile, int mainline)
c906108c
SS
1089{
1090 bfd *abfd = objfile->obfd;
1091 char *aranges_buffer, *pubnames_buffer;
1092 char *aranges_ptr, *pubnames_ptr;
1093 unsigned int entry_length, version, info_offset, info_size;
1094
1095 pubnames_buffer = dwarf2_read_section (objfile,
086df311 1096 dwarf_pubnames_section);
c906108c
SS
1097 pubnames_ptr = pubnames_buffer;
1098 while ((pubnames_ptr - pubnames_buffer) < dwarf_pubnames_size)
1099 {
613e1657
KB
1100 struct comp_unit_head cu_header;
1101 int bytes_read;
1102
1103 entry_length = read_initial_length (abfd, pubnames_ptr, &cu_header,
1104 &bytes_read);
1105 pubnames_ptr += bytes_read;
c906108c
SS
1106 version = read_1_byte (abfd, pubnames_ptr);
1107 pubnames_ptr += 1;
1108 info_offset = read_4_bytes (abfd, pubnames_ptr);
1109 pubnames_ptr += 4;
1110 info_size = read_4_bytes (abfd, pubnames_ptr);
1111 pubnames_ptr += 4;
1112 }
1113
1114 aranges_buffer = dwarf2_read_section (objfile,
086df311 1115 dwarf_aranges_section);
c906108c
SS
1116
1117}
1118#endif
1119
107d2387
AC
1120/* Read in the comp unit header information from the debug_info at
1121 info_ptr. */
1122
1123static char *
1124read_comp_unit_head (struct comp_unit_head *cu_header,
1125 char *info_ptr, bfd *abfd)
1126{
1127 int signed_addr;
613e1657
KB
1128 int bytes_read;
1129 cu_header->length = read_initial_length (abfd, info_ptr, cu_header,
1130 &bytes_read);
1131 info_ptr += bytes_read;
107d2387
AC
1132 cu_header->version = read_2_bytes (abfd, info_ptr);
1133 info_ptr += 2;
613e1657
KB
1134 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
1135 &bytes_read);
1136 info_ptr += bytes_read;
107d2387
AC
1137 cu_header->addr_size = read_1_byte (abfd, info_ptr);
1138 info_ptr += 1;
1139 signed_addr = bfd_get_sign_extend_vma (abfd);
1140 if (signed_addr < 0)
8e65ff28
AC
1141 internal_error (__FILE__, __LINE__,
1142 "read_comp_unit_head: dwarf from non elf file");
107d2387
AC
1143 cu_header->signed_addr_p = signed_addr;
1144 return info_ptr;
1145}
1146
c906108c
SS
1147/* Build the partial symbol table by doing a quick pass through the
1148 .debug_info and .debug_abbrev sections. */
1149
1150static void
fba45db2 1151dwarf2_build_psymtabs_hard (struct objfile *objfile, int mainline)
c906108c
SS
1152{
1153 /* Instead of reading this into a big buffer, we should probably use
1154 mmap() on architectures that support it. (FIXME) */
1155 bfd *abfd = objfile->obfd;
1156 char *info_ptr, *abbrev_ptr;
1157 char *beg_of_comp_unit;
c906108c
SS
1158 struct partial_die_info comp_unit_die;
1159 struct partial_symtab *pst;
1160 struct cleanup *back_to;
c906108c
SS
1161 CORE_ADDR lowpc, highpc;
1162
c906108c
SS
1163 info_ptr = dwarf_info_buffer;
1164 abbrev_ptr = dwarf_abbrev_buffer;
1165
9e84cbde
JB
1166 /* We use dwarf2_tmp_obstack for objects that don't need to survive
1167 the partial symbol scan, like attribute values.
1168
1169 We could reduce our peak memory consumption during partial symbol
1170 table construction by freeing stuff from this obstack more often
1171 --- say, after processing each compilation unit, or each die ---
1172 but it turns out that this saves almost nothing. For an
1173 executable with 11Mb of Dwarf 2 data, I found about 64k allocated
1174 on dwarf2_tmp_obstack. Some investigation showed:
1175
1176 1) 69% of the attributes used forms DW_FORM_addr, DW_FORM_data*,
1177 DW_FORM_flag, DW_FORM_[su]data, and DW_FORM_ref*. These are
1178 all fixed-length values not requiring dynamic allocation.
1179
1180 2) 30% of the attributes used the form DW_FORM_string. For
1181 DW_FORM_string, read_attribute simply hands back a pointer to
1182 the null-terminated string in dwarf_info_buffer, so no dynamic
1183 allocation is needed there either.
1184
1185 3) The remaining 1% of the attributes all used DW_FORM_block1.
1186 75% of those were DW_AT_frame_base location lists for
1187 functions; the rest were DW_AT_location attributes, probably
1188 for the global variables.
1189
1190 Anyway, what this all means is that the memory the dwarf2
1191 reader uses as temporary space reading partial symbols is about
1192 0.5% as much as we use for dwarf_*_buffer. That's noise. */
1193
c906108c
SS
1194 obstack_init (&dwarf2_tmp_obstack);
1195 back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
1196
af703f96
JB
1197 /* Since the objects we're extracting from dwarf_info_buffer vary in
1198 length, only the individual functions to extract them (like
1199 read_comp_unit_head and read_partial_die) can really know whether
1200 the buffer is large enough to hold another complete object.
1201
1202 At the moment, they don't actually check that. If
1203 dwarf_info_buffer holds just one extra byte after the last
1204 compilation unit's dies, then read_comp_unit_head will happily
1205 read off the end of the buffer. read_partial_die is similarly
1206 casual. Those functions should be fixed.
1207
1208 For this loop condition, simply checking whether there's any data
1209 left at all should be sufficient. */
2541c7cf 1210 while (info_ptr < dwarf_info_buffer + dwarf_info_size)
c906108c 1211 {
e7c27a73 1212 struct dwarf2_cu cu;
c906108c 1213 beg_of_comp_unit = info_ptr;
c906108c 1214
e7c27a73
DJ
1215 cu.objfile = objfile;
1216 info_ptr = read_comp_unit_head (&cu.header, info_ptr, abfd);
1217
1218 if (cu.header.version != 2)
c906108c 1219 {
e7c27a73 1220 error ("Dwarf Error: wrong version in compilation unit header (is %d, should be %d) [in module %s]", cu.header.version, 2, bfd_get_filename (abfd));
c906108c
SS
1221 return;
1222 }
e7c27a73 1223 if (cu.header.abbrev_offset >= dwarf_abbrev_size)
c906108c 1224 {
659b0389 1225 error ("Dwarf Error: bad offset (0x%lx) in compilation unit header (offset 0x%lx + 6) [in module %s]",
e7c27a73 1226 (long) cu.header.abbrev_offset,
659b0389
ML
1227 (long) (beg_of_comp_unit - dwarf_info_buffer),
1228 bfd_get_filename (abfd));
c906108c
SS
1229 return;
1230 }
e7c27a73 1231 if (beg_of_comp_unit + cu.header.length + cu.header.initial_length_size
c906108c
SS
1232 > dwarf_info_buffer + dwarf_info_size)
1233 {
659b0389 1234 error ("Dwarf Error: bad length (0x%lx) in compilation unit header (offset 0x%lx + 0) [in module %s]",
e7c27a73 1235 (long) cu.header.length,
659b0389
ML
1236 (long) (beg_of_comp_unit - dwarf_info_buffer),
1237 bfd_get_filename (abfd));
c906108c
SS
1238 return;
1239 }
57349743 1240 /* Complete the cu_header */
e7c27a73
DJ
1241 cu.header.offset = beg_of_comp_unit - dwarf_info_buffer;
1242 cu.header.first_die_ptr = info_ptr;
1243 cu.header.cu_head_ptr = beg_of_comp_unit;
57349743 1244
c906108c 1245 /* Read the abbrevs for this compilation unit into a table */
e7c27a73
DJ
1246 dwarf2_read_abbrevs (abfd, &cu);
1247 make_cleanup (dwarf2_empty_abbrev_table, cu.header.dwarf2_abbrevs);
c906108c
SS
1248
1249 /* Read the compilation unit die */
107d2387 1250 info_ptr = read_partial_die (&comp_unit_die, abfd, info_ptr,
e7c27a73 1251 &cu);
c906108c
SS
1252
1253 /* Set the language we're debugging */
1254 set_cu_language (comp_unit_die.language);
1255
1256 /* Allocate a new partial symbol table structure */
d4f3574e 1257 pst = start_psymtab_common (objfile, objfile->section_offsets,
96baa820 1258 comp_unit_die.name ? comp_unit_die.name : "",
c906108c
SS
1259 comp_unit_die.lowpc,
1260 objfile->global_psymbols.next,
1261 objfile->static_psymbols.next);
1262
1263 pst->read_symtab_private = (char *)
1264 obstack_alloc (&objfile->psymbol_obstack, sizeof (struct dwarf2_pinfo));
1265 cu_header_offset = beg_of_comp_unit - dwarf_info_buffer;
c5aa993b
JM
1266 DWARF_INFO_BUFFER (pst) = dwarf_info_buffer;
1267 DWARF_INFO_OFFSET (pst) = beg_of_comp_unit - dwarf_info_buffer;
1268 DWARF_ABBREV_BUFFER (pst) = dwarf_abbrev_buffer;
1269 DWARF_ABBREV_SIZE (pst) = dwarf_abbrev_size;
1270 DWARF_LINE_BUFFER (pst) = dwarf_line_buffer;
9ab3e532 1271 DWARF_LINE_SIZE (pst) = dwarf_line_size;
4bdf3d34
JJ
1272 DWARF_STR_BUFFER (pst) = dwarf_str_buffer;
1273 DWARF_STR_SIZE (pst) = dwarf_str_size;
2e276125
JB
1274 DWARF_MACINFO_BUFFER (pst) = dwarf_macinfo_buffer;
1275 DWARF_MACINFO_SIZE (pst) = dwarf_macinfo_size;
af34e669
DJ
1276 DWARF_RANGES_BUFFER (pst) = dwarf_ranges_buffer;
1277 DWARF_RANGES_SIZE (pst) = dwarf_ranges_size;
0d53c4c4
DJ
1278 DWARF_LOC_BUFFER (pst) = dwarf_loc_buffer;
1279 DWARF_LOC_SIZE (pst) = dwarf_loc_size;
613e1657 1280 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
1281
1282 /* Store the function that reads in the rest of the symbol table */
1283 pst->read_symtab = dwarf2_psymtab_to_symtab;
1284
1285 /* Check if comp unit has_children.
1286 If so, read the rest of the partial symbols from this comp unit.
1287 If not, there's no more debug_info for this comp unit. */
1288 if (comp_unit_die.has_children)
1289 {
91c24f0a
DC
1290 lowpc = ((CORE_ADDR) -1);
1291 highpc = ((CORE_ADDR) 0);
1292
e7c27a73
DJ
1293 info_ptr = scan_partial_symbols (info_ptr, &lowpc, &highpc,
1294 &cu, NULL);
c906108c 1295
91c24f0a
DC
1296 /* If we didn't find a lowpc, set it to highpc to avoid
1297 complaints from `maint check'. */
1298 if (lowpc == ((CORE_ADDR) -1))
1299 lowpc = highpc;
1300
c906108c
SS
1301 /* If the compilation unit didn't have an explicit address range,
1302 then use the information extracted from its child dies. */
0b010bcc 1303 if (! comp_unit_die.has_pc_info)
c906108c 1304 {
c5aa993b 1305 comp_unit_die.lowpc = lowpc;
c906108c
SS
1306 comp_unit_die.highpc = highpc;
1307 }
1308 }
c5aa993b 1309 pst->textlow = comp_unit_die.lowpc + baseaddr;
c906108c
SS
1310 pst->texthigh = comp_unit_die.highpc + baseaddr;
1311
1312 pst->n_global_syms = objfile->global_psymbols.next -
1313 (objfile->global_psymbols.list + pst->globals_offset);
1314 pst->n_static_syms = objfile->static_psymbols.next -
1315 (objfile->static_psymbols.list + pst->statics_offset);
1316 sort_pst_symbols (pst);
1317
1318 /* If there is already a psymtab or symtab for a file of this
1319 name, remove it. (If there is a symtab, more drastic things
1320 also happen.) This happens in VxWorks. */
1321 free_named_symtabs (pst->filename);
1322
e7c27a73
DJ
1323 info_ptr = beg_of_comp_unit + cu.header.length
1324 + cu.header.initial_length_size;
c906108c
SS
1325 }
1326 do_cleanups (back_to);
1327}
1328
91c24f0a 1329/* Read in all interesting dies to the end of the compilation unit or
5c4e30ca
DC
1330 to the end of the current namespace. NAMESPACE is NULL if we
1331 haven't yet encountered any DW_TAG_namespace entries; otherwise,
1332 it's the name of the current namespace. In particular, it's the
1333 empty string if we're currently in the global namespace but have
1334 previously encountered a DW_TAG_namespace. */
c906108c
SS
1335
1336static char *
e7c27a73
DJ
1337scan_partial_symbols (char *info_ptr, CORE_ADDR *lowpc,
1338 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5c4e30ca 1339 const char *namespace)
c906108c 1340{
e7c27a73 1341 struct objfile *objfile = cu->objfile;
c906108c
SS
1342 bfd *abfd = objfile->obfd;
1343 struct partial_die_info pdi;
1344
91c24f0a
DC
1345 /* Now, march along the PDI's, descending into ones which have
1346 interesting children but skipping the children of the other ones,
1347 until we reach the end of the compilation unit. */
c906108c 1348
91c24f0a 1349 while (1)
c906108c 1350 {
91c24f0a
DC
1351 /* This flag tells whether or not info_ptr has gotten updated
1352 inside the loop. */
1353 int info_ptr_updated = 0;
1354
e7c27a73 1355 info_ptr = read_partial_die (&pdi, abfd, info_ptr, cu);
c906108c 1356
91c24f0a
DC
1357 /* Anonymous namespaces have no name but have interesting
1358 children, so we need to look at them. Ditto for anonymous
1359 enums. */
933c6fe4 1360
91c24f0a
DC
1361 if (pdi.name != NULL || pdi.tag == DW_TAG_namespace
1362 || pdi.tag == DW_TAG_enumeration_type)
c906108c
SS
1363 {
1364 switch (pdi.tag)
1365 {
1366 case DW_TAG_subprogram:
0b010bcc 1367 if (pdi.has_pc_info)
c906108c
SS
1368 {
1369 if (pdi.lowpc < *lowpc)
1370 {
1371 *lowpc = pdi.lowpc;
1372 }
1373 if (pdi.highpc > *highpc)
1374 {
1375 *highpc = pdi.highpc;
1376 }
91c24f0a 1377 if (!pdi.is_declaration)
c906108c 1378 {
e7c27a73 1379 add_partial_symbol (&pdi, cu, namespace);
c906108c
SS
1380 }
1381 }
1382 break;
1383 case DW_TAG_variable:
1384 case DW_TAG_typedef:
91c24f0a 1385 case DW_TAG_union_type:
63d06c5c
DC
1386 if (!pdi.is_declaration)
1387 {
1388 add_partial_symbol (&pdi, cu, namespace);
1389 }
1390 break;
c906108c
SS
1391 case DW_TAG_class_type:
1392 case DW_TAG_structure_type:
91c24f0a 1393 if (!pdi.is_declaration)
c906108c 1394 {
63d06c5c
DC
1395 info_ptr = add_partial_structure (&pdi, info_ptr, cu,
1396 namespace);
1397 info_ptr_updated = 1;
c906108c
SS
1398 }
1399 break;
91c24f0a
DC
1400 case DW_TAG_enumeration_type:
1401 if (!pdi.is_declaration)
1402 {
e7c27a73 1403 info_ptr = add_partial_enumeration (&pdi, info_ptr, cu,
5c4e30ca 1404 namespace);
91c24f0a
DC
1405 info_ptr_updated = 1;
1406 }
c906108c
SS
1407 break;
1408 case DW_TAG_base_type:
1409 /* File scope base type definitions are added to the partial
c5aa993b 1410 symbol table. */
e7c27a73 1411 add_partial_symbol (&pdi, cu, namespace);
c906108c 1412 break;
d9fa45fe 1413 case DW_TAG_namespace:
5c4e30ca
DC
1414 /* We've hit a DW_TAG_namespace entry, so we know this
1415 file has been compiled using a compiler that
1416 generates them; update NAMESPACE to reflect that. */
1417 if (namespace == NULL)
1418 namespace = "";
e7c27a73
DJ
1419 info_ptr = add_partial_namespace (&pdi, info_ptr, lowpc, highpc,
1420 cu, namespace);
91c24f0a
DC
1421 info_ptr_updated = 1;
1422 break;
c906108c
SS
1423 default:
1424 break;
1425 }
1426 }
1427
c906108c 1428 if (pdi.tag == 0)
91c24f0a
DC
1429 break;
1430
1431 /* If the die has a sibling, skip to the sibling, unless another
1432 function has already updated info_ptr for us. */
1433
1434 /* NOTE: carlton/2003-06-16: This is a bit hackish, but whether
1435 or not we want to update this depends on enough stuff (not
1436 only pdi.tag but also whether or not pdi.name is NULL) that
1437 this seems like the easiest way to handle the issue. */
1438
1439 if (!info_ptr_updated)
e7c27a73 1440 info_ptr = locate_pdi_sibling (&pdi, info_ptr, abfd, cu);
c906108c
SS
1441 }
1442
c906108c
SS
1443 return info_ptr;
1444}
1445
1446static void
e7c27a73
DJ
1447add_partial_symbol (struct partial_die_info *pdi,
1448 struct dwarf2_cu *cu, const char *namespace)
c906108c 1449{
e7c27a73 1450 struct objfile *objfile = cu->objfile;
c906108c 1451 CORE_ADDR addr = 0;
38d518c9 1452 char *actual_name = pdi->name;
5c4e30ca 1453 const struct partial_symbol *psym = NULL;
c906108c 1454
63d06c5c
DC
1455 /* If we're not in the global namespace and if the namespace name
1456 isn't encoded in a mangled actual_name, add it. */
1457
1458 if (pdi_needs_namespace (pdi->tag, namespace))
1459 {
1460 actual_name = alloca (strlen (pdi->name) + 2 + strlen (namespace) + 1);
1461 strcpy (actual_name, namespace);
1462 strcat (actual_name, "::");
1463 strcat (actual_name, pdi->name);
1464 }
1465
c906108c
SS
1466 switch (pdi->tag)
1467 {
1468 case DW_TAG_subprogram:
1469 if (pdi->is_external)
1470 {
38d518c9 1471 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 1472 mst_text, objfile); */
38d518c9 1473 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
1474 VAR_DOMAIN, LOC_BLOCK,
1475 &objfile->global_psymbols,
1476 0, pdi->lowpc + baseaddr,
1477 cu_language, objfile);
c906108c
SS
1478 }
1479 else
1480 {
38d518c9 1481 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 1482 mst_file_text, objfile); */
38d518c9 1483 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
1484 VAR_DOMAIN, LOC_BLOCK,
1485 &objfile->static_psymbols,
1486 0, pdi->lowpc + baseaddr,
1487 cu_language, objfile);
c906108c
SS
1488 }
1489 break;
1490 case DW_TAG_variable:
1491 if (pdi->is_external)
1492 {
1493 /* Global Variable.
1494 Don't enter into the minimal symbol tables as there is
1495 a minimal symbol table entry from the ELF symbols already.
1496 Enter into partial symbol table if it has a location
1497 descriptor or a type.
1498 If the location descriptor is missing, new_symbol will create
1499 a LOC_UNRESOLVED symbol, the address of the variable will then
1500 be determined from the minimal symbol table whenever the variable
1501 is referenced.
1502 The address for the partial symbol table entry is not
1503 used by GDB, but it comes in handy for debugging partial symbol
1504 table building. */
1505
1506 if (pdi->locdesc)
e7c27a73 1507 addr = decode_locdesc (pdi->locdesc, cu);
c906108c 1508 if (pdi->locdesc || pdi->has_type)
38d518c9 1509 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
1510 VAR_DOMAIN, LOC_STATIC,
1511 &objfile->global_psymbols,
1512 0, addr + baseaddr,
1513 cu_language, objfile);
c906108c
SS
1514 }
1515 else
1516 {
1517 /* Static Variable. Skip symbols without location descriptors. */
1518 if (pdi->locdesc == NULL)
1519 return;
e7c27a73 1520 addr = decode_locdesc (pdi->locdesc, cu);
38d518c9 1521 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 1522 mst_file_data, objfile); */
38d518c9 1523 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
5c4e30ca
DC
1524 VAR_DOMAIN, LOC_STATIC,
1525 &objfile->static_psymbols,
1526 0, addr + baseaddr,
1527 cu_language, objfile);
c906108c
SS
1528 }
1529 break;
1530 case DW_TAG_typedef:
1531 case DW_TAG_base_type:
38d518c9 1532 add_psymbol_to_list (actual_name, strlen (actual_name),
176620f1 1533 VAR_DOMAIN, LOC_TYPEDEF,
c906108c
SS
1534 &objfile->static_psymbols,
1535 0, (CORE_ADDR) 0, cu_language, objfile);
1536 break;
1537 case DW_TAG_class_type:
1538 case DW_TAG_structure_type:
1539 case DW_TAG_union_type:
1540 case DW_TAG_enumeration_type:
1541 /* Skip aggregate types without children, these are external
c5aa993b 1542 references. */
63d06c5c
DC
1543 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
1544 static vs. global. */
c906108c
SS
1545 if (pdi->has_children == 0)
1546 return;
38d518c9 1547 add_psymbol_to_list (actual_name, strlen (actual_name),
176620f1 1548 STRUCT_DOMAIN, LOC_TYPEDEF,
63d06c5c
DC
1549 cu_language == language_cplus
1550 ? &objfile->global_psymbols
1551 : &objfile->static_psymbols,
c906108c
SS
1552 0, (CORE_ADDR) 0, cu_language, objfile);
1553
1554 if (cu_language == language_cplus)
1555 {
1556 /* For C++, these implicitly act as typedefs as well. */
38d518c9 1557 add_psymbol_to_list (actual_name, strlen (actual_name),
176620f1 1558 VAR_DOMAIN, LOC_TYPEDEF,
63d06c5c 1559 &objfile->global_psymbols,
c906108c
SS
1560 0, (CORE_ADDR) 0, cu_language, objfile);
1561 }
1562 break;
1563 case DW_TAG_enumerator:
38d518c9 1564 add_psymbol_to_list (actual_name, strlen (actual_name),
176620f1 1565 VAR_DOMAIN, LOC_CONST,
63d06c5c
DC
1566 cu_language == language_cplus
1567 ? &objfile->static_psymbols
1568 : &objfile->global_psymbols,
c906108c
SS
1569 0, (CORE_ADDR) 0, cu_language, objfile);
1570 break;
1571 default:
1572 break;
1573 }
5c4e30ca
DC
1574
1575 /* Check to see if we should scan the name for possible namespace
1576 info. Only do this if this is C++, if we don't have namespace
1577 debugging info in the file, if the psym is of an appropriate type
1578 (otherwise we'll have psym == NULL), and if we actually had a
1579 mangled name to begin with. */
1580
1581 if (cu_language == language_cplus
1582 && namespace == NULL
1583 && psym != NULL
1584 && SYMBOL_CPLUS_DEMANGLED_NAME (psym) != NULL)
1585 cp_check_possible_namespace_symbols (SYMBOL_CPLUS_DEMANGLED_NAME (psym),
1586 objfile);
c906108c
SS
1587}
1588
63d06c5c
DC
1589/* Determine whether a die of type TAG living in the C++ namespace
1590 NAMESPACE needs to have the name of the namespace prepended to the
1591 name listed in the die. */
1592
1593static int
1594pdi_needs_namespace (enum dwarf_tag tag, const char *namespace)
1595{
1596 if (namespace == NULL || namespace[0] == '\0')
1597 return 0;
1598
1599 switch (tag)
1600 {
1601 case DW_TAG_typedef:
1602 case DW_TAG_class_type:
1603 case DW_TAG_structure_type:
1604 case DW_TAG_union_type:
1605 case DW_TAG_enumeration_type:
1606 case DW_TAG_enumerator:
1607 return 1;
1608 default:
1609 return 0;
1610 }
1611}
1612
5c4e30ca
DC
1613/* Read a partial die corresponding to a namespace; also, add a symbol
1614 corresponding to that namespace to the symbol table. NAMESPACE is
1615 the name of the enclosing namespace. */
91c24f0a
DC
1616
1617static char *
1618add_partial_namespace (struct partial_die_info *pdi, char *info_ptr,
91c24f0a 1619 CORE_ADDR *lowpc, CORE_ADDR *highpc,
e7c27a73 1620 struct dwarf2_cu *cu, const char *namespace)
91c24f0a 1621{
e7c27a73 1622 struct objfile *objfile = cu->objfile;
5c4e30ca
DC
1623 const char *new_name = pdi->name;
1624 char *full_name;
1625
e7c27a73
DJ
1626 /* Calculate the full name of the namespace that we just entered. */
1627
5c4e30ca
DC
1628 if (new_name == NULL)
1629 new_name = "(anonymous namespace)";
1630 full_name = alloca (strlen (namespace) + 2 + strlen (new_name) + 1);
1631 strcpy (full_name, namespace);
1632 if (*namespace != '\0')
1633 strcat (full_name, "::");
1634 strcat (full_name, new_name);
1635
63d06c5c
DC
1636 /* FIXME: carlton/2003-10-07: We can't just replace this by a call
1637 to add_partial_symbol, because we don't have a way to pass in the
1638 full name to that function; that might be a flaw in
1639 add_partial_symbol's interface. */
5c4e30ca
DC
1640
1641 add_psymbol_to_list (full_name, strlen (full_name),
1642 VAR_DOMAIN, LOC_TYPEDEF,
1643 &objfile->global_psymbols,
1644 0, 0, cu_language, objfile);
1645
1646 /* Now scan partial symbols in that namespace. */
1647
91c24f0a 1648 if (pdi->has_children)
e7c27a73 1649 info_ptr = scan_partial_symbols (info_ptr, lowpc, highpc, cu, full_name);
91c24f0a
DC
1650
1651 return info_ptr;
1652}
1653
63d06c5c
DC
1654/* Read a partial die corresponding to a class or structure. */
1655
1656static char *
1657add_partial_structure (struct partial_die_info *struct_pdi, char *info_ptr,
1658 struct dwarf2_cu *cu,
1659 const char *namespace)
1660{
1661 bfd *abfd = cu->objfile->obfd;
1662 char *actual_class_name = NULL;
1663
1664 if (cu_language == language_cplus
1665 && namespace == NULL
1666 && struct_pdi->name != NULL
1667 && struct_pdi->has_children)
1668 {
1669 /* We don't have namespace debugging information, so see if we
1670 can figure out if this structure lives in a namespace. Look
1671 for a member function; its demangled name will contain
1672 namespace info, if there is any. */
1673
1674 /* NOTE: carlton/2003-10-07: Getting the info this way changes
1675 what template types look like, because the demangler
1676 frequently doesn't give the same name as the debug info. We
1677 could fix this by only using the demangled name to get the
1678 prefix (but see comment in read_structure_scope). */
1679
1680 char *next_child = info_ptr;
1681
1682 while (1)
1683 {
1684 struct partial_die_info child_pdi;
1685
1686 next_child = read_partial_die (&child_pdi, abfd, next_child,
1687 cu);
1688 if (!child_pdi.tag)
1689 break;
1690 if (child_pdi.tag == DW_TAG_subprogram)
1691 {
1692 actual_class_name = class_name_from_physname (child_pdi.name);
1693 if (actual_class_name != NULL)
1694 struct_pdi->name = actual_class_name;
1695 break;
1696 }
1697 else
1698 {
1699 next_child = locate_pdi_sibling (&child_pdi, next_child,
1700 abfd, cu);
1701 }
1702 }
1703 }
1704
1705 add_partial_symbol (struct_pdi, cu, namespace);
1706 xfree(actual_class_name);
1707
1708 return locate_pdi_sibling (struct_pdi, info_ptr, abfd, cu);
1709}
1710
91c24f0a
DC
1711/* Read a partial die corresponding to an enumeration type. */
1712
1713static char *
1714add_partial_enumeration (struct partial_die_info *enum_pdi, char *info_ptr,
e7c27a73 1715 struct dwarf2_cu *cu, const char *namespace)
91c24f0a 1716{
e7c27a73 1717 struct objfile *objfile = cu->objfile;
91c24f0a
DC
1718 bfd *abfd = objfile->obfd;
1719 struct partial_die_info pdi;
1720
1721 if (enum_pdi->name != NULL)
e7c27a73 1722 add_partial_symbol (enum_pdi, cu, namespace);
91c24f0a
DC
1723
1724 while (1)
1725 {
e7c27a73 1726 info_ptr = read_partial_die (&pdi, abfd, info_ptr, cu);
91c24f0a
DC
1727 if (pdi.tag == 0)
1728 break;
1729 if (pdi.tag != DW_TAG_enumerator || pdi.name == NULL)
1730 complaint (&symfile_complaints, "malformed enumerator DIE ignored");
1731 else
e7c27a73 1732 add_partial_symbol (&pdi, cu, namespace);
91c24f0a
DC
1733 }
1734
1735 return info_ptr;
1736}
1737
1738/* Locate ORIG_PDI's sibling; INFO_PTR should point to the next DIE
1739 after ORIG_PDI. */
1740
1741static char *
1742locate_pdi_sibling (struct partial_die_info *orig_pdi, char *info_ptr,
e7c27a73 1743 bfd *abfd, struct dwarf2_cu *cu)
91c24f0a
DC
1744{
1745 /* Do we know the sibling already? */
1746
1747 if (orig_pdi->sibling)
1748 return orig_pdi->sibling;
1749
1750 /* Are there any children to deal with? */
1751
1752 if (!orig_pdi->has_children)
1753 return info_ptr;
1754
1755 /* Okay, we don't know the sibling, but we have children that we
1756 want to skip. So read children until we run into one without a
1757 tag; return whatever follows it. */
1758
1759 while (1)
1760 {
1761 struct partial_die_info pdi;
1762
e7c27a73 1763 info_ptr = read_partial_die (&pdi, abfd, info_ptr, cu);
91c24f0a
DC
1764
1765 if (pdi.tag == 0)
1766 return info_ptr;
1767 else
e7c27a73 1768 info_ptr = locate_pdi_sibling (&pdi, info_ptr, abfd, cu);
91c24f0a
DC
1769 }
1770}
1771
c906108c
SS
1772/* Expand this partial symbol table into a full symbol table. */
1773
1774static void
fba45db2 1775dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c
SS
1776{
1777 /* FIXME: This is barely more than a stub. */
1778 if (pst != NULL)
1779 {
1780 if (pst->readin)
1781 {
1782 warning ("bug: psymtab for %s is already read in.", pst->filename);
1783 }
1784 else
1785 {
1786 if (info_verbose)
1787 {
1788 printf_filtered ("Reading in symbols for %s...", pst->filename);
1789 gdb_flush (gdb_stdout);
1790 }
1791
1792 psymtab_to_symtab_1 (pst);
1793
1794 /* Finish up the debug error message. */
1795 if (info_verbose)
1796 printf_filtered ("done.\n");
1797 }
1798 }
1799}
1800
1801static void
fba45db2 1802psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c
SS
1803{
1804 struct objfile *objfile = pst->objfile;
1805 bfd *abfd = objfile->obfd;
e7c27a73 1806 struct dwarf2_cu cu;
c906108c
SS
1807 struct die_info *dies;
1808 unsigned long offset;
1809 CORE_ADDR lowpc, highpc;
1810 struct die_info *child_die;
1811 char *info_ptr;
1812 struct symtab *symtab;
1813 struct cleanup *back_to;
0d53c4c4 1814 struct attribute *attr;
c906108c
SS
1815
1816 /* Set local variables from the partial symbol table info. */
c5aa993b
JM
1817 offset = DWARF_INFO_OFFSET (pst);
1818 dwarf_info_buffer = DWARF_INFO_BUFFER (pst);
1819 dwarf_abbrev_buffer = DWARF_ABBREV_BUFFER (pst);
1820 dwarf_abbrev_size = DWARF_ABBREV_SIZE (pst);
1821 dwarf_line_buffer = DWARF_LINE_BUFFER (pst);
9ab3e532 1822 dwarf_line_size = DWARF_LINE_SIZE (pst);
4bdf3d34
JJ
1823 dwarf_str_buffer = DWARF_STR_BUFFER (pst);
1824 dwarf_str_size = DWARF_STR_SIZE (pst);
2e276125
JB
1825 dwarf_macinfo_buffer = DWARF_MACINFO_BUFFER (pst);
1826 dwarf_macinfo_size = DWARF_MACINFO_SIZE (pst);
af34e669
DJ
1827 dwarf_ranges_buffer = DWARF_RANGES_BUFFER (pst);
1828 dwarf_ranges_size = DWARF_RANGES_SIZE (pst);
0d53c4c4
DJ
1829 dwarf_loc_buffer = DWARF_LOC_BUFFER (pst);
1830 dwarf_loc_size = DWARF_LOC_SIZE (pst);
613e1657 1831 baseaddr = ANOFFSET (pst->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
1832 cu_header_offset = offset;
1833 info_ptr = dwarf_info_buffer + offset;
1834
63d06c5c
DC
1835 /* We're in the global namespace. */
1836 processing_current_prefix = "";
1837
c906108c
SS
1838 obstack_init (&dwarf2_tmp_obstack);
1839 back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
1840
1841 buildsym_init ();
a0b3c4fd 1842 make_cleanup (really_free_pendings, NULL);
c906108c 1843
e7c27a73
DJ
1844 cu.objfile = objfile;
1845
c906108c 1846 /* read in the comp_unit header */
e7c27a73 1847 info_ptr = read_comp_unit_head (&cu.header, info_ptr, abfd);
c906108c
SS
1848
1849 /* Read the abbrevs for this compilation unit */
e7c27a73
DJ
1850 dwarf2_read_abbrevs (abfd, &cu);
1851 make_cleanup (dwarf2_empty_abbrev_table, cu.header.dwarf2_abbrevs);
c906108c 1852
e7c27a73 1853 dies = read_comp_unit (info_ptr, abfd, &cu);
c906108c 1854
74b7792f 1855 make_cleanup_free_die_list (dies);
c906108c 1856
0d53c4c4
DJ
1857 /* Find the base address of the compilation unit for range lists and
1858 location lists. It will normally be specified by DW_AT_low_pc.
1859 In DWARF-3 draft 4, the base address could be overridden by
1860 DW_AT_entry_pc. It's been removed, but GCC still uses this for
1861 compilation units with discontinuous ranges. */
1862
e7c27a73
DJ
1863 cu.header.base_known = 0;
1864 cu.header.base_address = 0;
0d53c4c4
DJ
1865
1866 attr = dwarf_attr (dies, DW_AT_entry_pc);
1867 if (attr)
1868 {
e7c27a73
DJ
1869 cu.header.base_address = DW_ADDR (attr);
1870 cu.header.base_known = 1;
0d53c4c4
DJ
1871 }
1872 else
1873 {
1874 attr = dwarf_attr (dies, DW_AT_low_pc);
1875 if (attr)
1876 {
e7c27a73
DJ
1877 cu.header.base_address = DW_ADDR (attr);
1878 cu.header.base_known = 1;
0d53c4c4
DJ
1879 }
1880 }
1881
c906108c 1882 /* Do line number decoding in read_file_scope () */
e7c27a73 1883 process_die (dies, &cu);
c906108c 1884
e7c27a73 1885 if (!dwarf2_get_pc_bounds (dies, &lowpc, &highpc, &cu))
c906108c
SS
1886 {
1887 /* Some compilers don't define a DW_AT_high_pc attribute for
c5aa993b
JM
1888 the compilation unit. If the DW_AT_high_pc is missing,
1889 synthesize it, by scanning the DIE's below the compilation unit. */
c906108c 1890 highpc = 0;
639d11d3 1891 if (dies->child != NULL)
c906108c 1892 {
639d11d3 1893 child_die = dies->child;
c906108c
SS
1894 while (child_die && child_die->tag)
1895 {
1896 if (child_die->tag == DW_TAG_subprogram)
1897 {
1898 CORE_ADDR low, high;
1899
e7c27a73 1900 if (dwarf2_get_pc_bounds (child_die, &low, &high, &cu))
c906108c
SS
1901 {
1902 highpc = max (highpc, high);
1903 }
1904 }
1905 child_die = sibling_die (child_die);
1906 }
1907 }
1908 }
613e1657 1909 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c
SS
1910
1911 /* Set symtab language to language from DW_AT_language.
1912 If the compilation is from a C file generated by language preprocessors,
1913 do not set the language if it was already deduced by start_subfile. */
1914 if (symtab != NULL
1915 && !(cu_language == language_c && symtab->language != language_c))
1916 {
1917 symtab->language = cu_language;
1918 }
1919 pst->symtab = symtab;
1920 pst->readin = 1;
c906108c
SS
1921
1922 do_cleanups (back_to);
1923}
1924
1925/* Process a die and its children. */
1926
1927static void
e7c27a73 1928process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
1929{
1930 switch (die->tag)
1931 {
1932 case DW_TAG_padding:
1933 break;
1934 case DW_TAG_compile_unit:
e7c27a73 1935 read_file_scope (die, cu);
c906108c
SS
1936 break;
1937 case DW_TAG_subprogram:
e7c27a73
DJ
1938 read_subroutine_type (die, cu);
1939 read_func_scope (die, cu);
c906108c
SS
1940 break;
1941 case DW_TAG_inlined_subroutine:
1942 /* FIXME: These are ignored for now.
c5aa993b
JM
1943 They could be used to set breakpoints on all inlined instances
1944 of a function and make GDB `next' properly over inlined functions. */
c906108c
SS
1945 break;
1946 case DW_TAG_lexical_block:
14898363
L
1947 case DW_TAG_try_block:
1948 case DW_TAG_catch_block:
e7c27a73 1949 read_lexical_block_scope (die, cu);
c906108c
SS
1950 break;
1951 case DW_TAG_class_type:
1952 case DW_TAG_structure_type:
1953 case DW_TAG_union_type:
e7c27a73 1954 read_structure_scope (die, cu);
c906108c
SS
1955 break;
1956 case DW_TAG_enumeration_type:
e7c27a73 1957 read_enumeration (die, cu);
c906108c
SS
1958 break;
1959 case DW_TAG_subroutine_type:
e7c27a73 1960 read_subroutine_type (die, cu);
c906108c
SS
1961 break;
1962 case DW_TAG_array_type:
e7c27a73 1963 read_array_type (die, cu);
c906108c
SS
1964 break;
1965 case DW_TAG_pointer_type:
e7c27a73 1966 read_tag_pointer_type (die, cu);
c906108c
SS
1967 break;
1968 case DW_TAG_ptr_to_member_type:
e7c27a73 1969 read_tag_ptr_to_member_type (die, cu);
c906108c
SS
1970 break;
1971 case DW_TAG_reference_type:
e7c27a73 1972 read_tag_reference_type (die, cu);
c906108c
SS
1973 break;
1974 case DW_TAG_string_type:
e7c27a73 1975 read_tag_string_type (die, cu);
c906108c
SS
1976 break;
1977 case DW_TAG_base_type:
e7c27a73 1978 read_base_type (die, cu);
c906108c
SS
1979 if (dwarf_attr (die, DW_AT_name))
1980 {
1981 /* Add a typedef symbol for the base type definition. */
e7c27a73 1982 new_symbol (die, die->type, cu);
c906108c
SS
1983 }
1984 break;
1985 case DW_TAG_common_block:
e7c27a73 1986 read_common_block (die, cu);
c906108c
SS
1987 break;
1988 case DW_TAG_common_inclusion:
1989 break;
d9fa45fe 1990 case DW_TAG_namespace:
63d06c5c 1991 processing_has_namespace_info = 1;
e7c27a73 1992 read_namespace (die, cu);
d9fa45fe
DC
1993 break;
1994 case DW_TAG_imported_declaration:
1995 case DW_TAG_imported_module:
1996 /* FIXME: carlton/2002-10-16: Eventually, we should use the
1997 information contained in these. DW_TAG_imported_declaration
1998 dies shouldn't have children; DW_TAG_imported_module dies
1999 shouldn't in the C++ case, but conceivably could in the
2000 Fortran case, so we'll have to replace this gdb_assert if
2001 Fortran compilers start generating that info. */
63d06c5c 2002 processing_has_namespace_info = 1;
639d11d3 2003 gdb_assert (die->child == NULL);
d9fa45fe 2004 break;
c906108c 2005 default:
e7c27a73 2006 new_symbol (die, NULL, cu);
c906108c
SS
2007 break;
2008 }
2009}
2010
5fb290d7
DJ
2011static void
2012initialize_cu_func_list (void)
2013{
2014 cu_first_fn = cu_last_fn = cu_cached_fn = NULL;
2015}
2016
c906108c 2017static void
e7c27a73 2018read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 2019{
e7c27a73
DJ
2020 struct objfile *objfile = cu->objfile;
2021 struct comp_unit_head *cu_header = &cu->header;
debd256d 2022 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 2023 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
2024 CORE_ADDR highpc = ((CORE_ADDR) 0);
2025 struct attribute *attr;
2026 char *name = "<unknown>";
2027 char *comp_dir = NULL;
2028 struct die_info *child_die;
2029 bfd *abfd = objfile->obfd;
debd256d 2030 struct line_header *line_header = 0;
c906108c 2031
e7c27a73 2032 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
c906108c 2033 {
639d11d3 2034 if (die->child != NULL)
c906108c 2035 {
639d11d3 2036 child_die = die->child;
c906108c
SS
2037 while (child_die && child_die->tag)
2038 {
2039 if (child_die->tag == DW_TAG_subprogram)
2040 {
2041 CORE_ADDR low, high;
2042
e7c27a73 2043 if (dwarf2_get_pc_bounds (child_die, &low, &high, cu))
c906108c
SS
2044 {
2045 lowpc = min (lowpc, low);
2046 highpc = max (highpc, high);
2047 }
2048 }
2049 child_die = sibling_die (child_die);
2050 }
2051 }
2052 }
2053
2054 /* If we didn't find a lowpc, set it to highpc to avoid complaints
2055 from finish_block. */
2acceee2 2056 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
2057 lowpc = highpc;
2058 lowpc += baseaddr;
2059 highpc += baseaddr;
2060
2061 attr = dwarf_attr (die, DW_AT_name);
2062 if (attr)
2063 {
2064 name = DW_STRING (attr);
2065 }
2066 attr = dwarf_attr (die, DW_AT_comp_dir);
2067 if (attr)
2068 {
2069 comp_dir = DW_STRING (attr);
2070 if (comp_dir)
2071 {
2072 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2073 directory, get rid of it. */
2074 char *cp = strchr (comp_dir, ':');
2075
2076 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2077 comp_dir = cp + 1;
2078 }
2079 }
2080
2081 if (objfile->ei.entry_point >= lowpc &&
2082 objfile->ei.entry_point < highpc)
2083 {
627b3ba2
AC
2084 objfile->ei.deprecated_entry_file_lowpc = lowpc;
2085 objfile->ei.deprecated_entry_file_highpc = highpc;
c906108c
SS
2086 }
2087
2088 attr = dwarf_attr (die, DW_AT_language);
2089 if (attr)
2090 {
2091 set_cu_language (DW_UNSND (attr));
2092 }
2093
2094 /* We assume that we're processing GCC output. */
2095 processing_gcc_compilation = 2;
2096#if 0
c5aa993b
JM
2097 /* FIXME:Do something here. */
2098 if (dip->at_producer != NULL)
c906108c
SS
2099 {
2100 handle_producer (dip->at_producer);
2101 }
2102#endif
2103
2104 /* The compilation unit may be in a different language or objfile,
2105 zero out all remembered fundamental types. */
2106 memset (ftypes, 0, FT_NUM_MEMBERS * sizeof (struct type *));
2107
2108 start_symtab (name, comp_dir, lowpc);
2109 record_debugformat ("DWARF 2");
2110
5fb290d7 2111 initialize_cu_func_list ();
c906108c
SS
2112
2113 /* Process all dies in compilation unit. */
639d11d3 2114 if (die->child != NULL)
c906108c 2115 {
639d11d3 2116 child_die = die->child;
c906108c
SS
2117 while (child_die && child_die->tag)
2118 {
e7c27a73 2119 process_die (child_die, cu);
c906108c
SS
2120 child_die = sibling_die (child_die);
2121 }
2122 }
5fb290d7
DJ
2123
2124 /* Decode line number information if present. */
2125 attr = dwarf_attr (die, DW_AT_stmt_list);
2126 if (attr)
2127 {
debd256d 2128 unsigned int line_offset = DW_UNSND (attr);
e7c27a73 2129 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
debd256d
JB
2130 if (line_header)
2131 {
2132 make_cleanup ((make_cleanup_ftype *) free_line_header,
2133 (void *) line_header);
e7c27a73 2134 dwarf_decode_lines (line_header, comp_dir, abfd, cu);
debd256d 2135 }
5fb290d7 2136 }
debd256d 2137
2e276125
JB
2138 /* Decode macro information, if present. Dwarf 2 macro information
2139 refers to information in the line number info statement program
2140 header, so we can only read it if we've read the header
2141 successfully. */
2142 attr = dwarf_attr (die, DW_AT_macro_info);
41ff2da1 2143 if (attr && line_header)
2e276125
JB
2144 {
2145 unsigned int macro_offset = DW_UNSND (attr);
2146 dwarf_decode_macros (line_header, macro_offset,
e7c27a73 2147 comp_dir, abfd, cu);
2e276125 2148 }
debd256d 2149 do_cleanups (back_to);
5fb290d7
DJ
2150}
2151
2152static void
2153add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc)
2154{
2155 struct function_range *thisfn;
2156
2157 thisfn = (struct function_range *)
2158 obstack_alloc (&dwarf2_tmp_obstack, sizeof (struct function_range));
2159 thisfn->name = name;
2160 thisfn->lowpc = lowpc;
2161 thisfn->highpc = highpc;
2162 thisfn->seen_line = 0;
2163 thisfn->next = NULL;
2164
2165 if (cu_last_fn == NULL)
2166 cu_first_fn = thisfn;
2167 else
2168 cu_last_fn->next = thisfn;
2169
2170 cu_last_fn = thisfn;
c906108c
SS
2171}
2172
2173static void
e7c27a73 2174read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 2175{
e7c27a73 2176 struct objfile *objfile = cu->objfile;
52f0bd74 2177 struct context_stack *new;
c906108c
SS
2178 CORE_ADDR lowpc;
2179 CORE_ADDR highpc;
2180 struct die_info *child_die;
2181 struct attribute *attr;
2182 char *name;
2183
2184 name = dwarf2_linkage_name (die);
2185
2186 /* Ignore functions with missing or empty names and functions with
2187 missing or invalid low and high pc attributes. */
e7c27a73 2188 if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
c906108c
SS
2189 return;
2190
2191 lowpc += baseaddr;
2192 highpc += baseaddr;
2193
5fb290d7
DJ
2194 /* Record the function range for dwarf_decode_lines. */
2195 add_to_cu_func_list (name, lowpc, highpc);
2196
c906108c
SS
2197 if (objfile->ei.entry_point >= lowpc &&
2198 objfile->ei.entry_point < highpc)
2199 {
2200 objfile->ei.entry_func_lowpc = lowpc;
2201 objfile->ei.entry_func_highpc = highpc;
2202 }
2203
c906108c 2204 new = push_context (0, lowpc);
e7c27a73 2205 new->name = new_symbol (die, die->type, cu);
4c2df51b 2206
4cecd739
DJ
2207 /* If there is a location expression for DW_AT_frame_base, record
2208 it. */
2209 attr = dwarf_attr (die, DW_AT_frame_base);
4c2df51b 2210 if (attr)
e7c27a73 2211 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 2212
c906108c
SS
2213 list_in_scope = &local_symbols;
2214
639d11d3 2215 if (die->child != NULL)
c906108c 2216 {
639d11d3 2217 child_die = die->child;
c906108c
SS
2218 while (child_die && child_die->tag)
2219 {
e7c27a73 2220 process_die (child_die, cu);
c906108c
SS
2221 child_die = sibling_die (child_die);
2222 }
2223 }
2224
2225 new = pop_context ();
2226 /* Make a block for the local symbols within. */
2227 finish_block (new->name, &local_symbols, new->old_blocks,
2228 lowpc, highpc, objfile);
208d8187
JB
2229
2230 /* In C++, we can have functions nested inside functions (e.g., when
2231 a function declares a class that has methods). This means that
2232 when we finish processing a function scope, we may need to go
2233 back to building a containing block's symbol lists. */
2234 local_symbols = new->locals;
2235 param_symbols = new->params;
2236
921e78cf
JB
2237 /* If we've finished processing a top-level function, subsequent
2238 symbols go in the file symbol list. */
2239 if (outermost_context_p ())
2240 list_in_scope = &file_symbols;
c906108c
SS
2241}
2242
2243/* Process all the DIES contained within a lexical block scope. Start
2244 a new scope, process the dies, and then close the scope. */
2245
2246static void
e7c27a73 2247read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 2248{
e7c27a73 2249 struct objfile *objfile = cu->objfile;
52f0bd74 2250 struct context_stack *new;
c906108c
SS
2251 CORE_ADDR lowpc, highpc;
2252 struct die_info *child_die;
2253
2254 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
2255 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
2256 as multiple lexical blocks? Handling children in a sane way would
2257 be nasty. Might be easier to properly extend generic blocks to
2258 describe ranges. */
e7c27a73 2259 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
c906108c
SS
2260 return;
2261 lowpc += baseaddr;
2262 highpc += baseaddr;
2263
2264 push_context (0, lowpc);
639d11d3 2265 if (die->child != NULL)
c906108c 2266 {
639d11d3 2267 child_die = die->child;
c906108c
SS
2268 while (child_die && child_die->tag)
2269 {
e7c27a73 2270 process_die (child_die, cu);
c906108c
SS
2271 child_die = sibling_die (child_die);
2272 }
2273 }
2274 new = pop_context ();
2275
2276 if (local_symbols != NULL)
2277 {
2278 finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
2279 highpc, objfile);
2280 }
2281 local_symbols = new->locals;
2282}
2283
af34e669
DJ
2284/* Get low and high pc attributes from a die. Return 1 if the attributes
2285 are present and valid, otherwise, return 0. Return -1 if the range is
2286 discontinuous, i.e. derived from DW_AT_ranges information. */
c906108c 2287static int
af34e669 2288dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
e7c27a73 2289 CORE_ADDR *highpc, struct dwarf2_cu *cu)
c906108c 2290{
e7c27a73
DJ
2291 struct objfile *objfile = cu->objfile;
2292 struct comp_unit_head *cu_header = &cu->header;
c906108c 2293 struct attribute *attr;
af34e669
DJ
2294 bfd *obfd = objfile->obfd;
2295 CORE_ADDR low = 0;
2296 CORE_ADDR high = 0;
2297 int ret = 0;
c906108c 2298
c906108c
SS
2299 attr = dwarf_attr (die, DW_AT_high_pc);
2300 if (attr)
af34e669
DJ
2301 {
2302 high = DW_ADDR (attr);
2303 attr = dwarf_attr (die, DW_AT_low_pc);
2304 if (attr)
2305 low = DW_ADDR (attr);
2306 else
2307 /* Found high w/o low attribute. */
2308 return 0;
2309
2310 /* Found consecutive range of addresses. */
2311 ret = 1;
2312 }
c906108c 2313 else
af34e669
DJ
2314 {
2315 attr = dwarf_attr (die, DW_AT_ranges);
2316 if (attr != NULL)
2317 {
2318 unsigned int addr_size = cu_header->addr_size;
2319 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
2320 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 2321 .debug_ranges section. */
af34e669
DJ
2322 unsigned int offset = DW_UNSND (attr);
2323 /* Base address selection entry. */
0d53c4c4
DJ
2324 CORE_ADDR base;
2325 int found_base;
af34e669 2326 int dummy;
af34e669
DJ
2327 char *buffer;
2328 CORE_ADDR marker;
2329 int low_set;
2330
0d53c4c4
DJ
2331 found_base = cu_header->base_known;
2332 base = cu_header->base_address;
a604369a
KB
2333
2334 if (offset >= dwarf_ranges_size)
2335 {
2336 complaint (&symfile_complaints,
2337 "Offset %d out of bounds for DW_AT_ranges attribute",
2338 offset);
2339 return 0;
2340 }
af34e669
DJ
2341 buffer = dwarf_ranges_buffer + offset;
2342
af34e669 2343 /* Read in the largest possible address. */
e7c27a73 2344 marker = read_address (obfd, buffer, cu, &dummy);
af34e669
DJ
2345 if ((marker & mask) == mask)
2346 {
2347 /* If we found the largest possible address, then
2348 read the base address. */
e7c27a73 2349 base = read_address (obfd, buffer + addr_size, cu, &dummy);
af34e669
DJ
2350 buffer += 2 * addr_size;
2351 offset += 2 * addr_size;
2352 found_base = 1;
2353 }
2354
2355 low_set = 0;
2356
2357 while (1)
2358 {
2359 CORE_ADDR range_beginning, range_end;
2360
e7c27a73 2361 range_beginning = read_address (obfd, buffer, cu, &dummy);
af34e669 2362 buffer += addr_size;
e7c27a73 2363 range_end = read_address (obfd, buffer, cu, &dummy);
af34e669
DJ
2364 buffer += addr_size;
2365 offset += 2 * addr_size;
2366
2367 /* An end of list marker is a pair of zero addresses. */
2368 if (range_beginning == 0 && range_end == 0)
2369 /* Found the end of list entry. */
2370 break;
2371
2372 /* Each base address selection entry is a pair of 2 values.
2373 The first is the largest possible address, the second is
2374 the base address. Check for a base address here. */
2375 if ((range_beginning & mask) == mask)
2376 {
2377 /* If we found the largest possible address, then
2378 read the base address. */
e7c27a73 2379 base = read_address (obfd, buffer + addr_size, cu, &dummy);
af34e669
DJ
2380 found_base = 1;
2381 continue;
2382 }
2383
2384 if (!found_base)
2385 {
2386 /* We have no valid base address for the ranges
2387 data. */
2388 complaint (&symfile_complaints,
2389 "Invalid .debug_ranges data (no base address)");
2390 return 0;
2391 }
2392
8f05cde5
DJ
2393 range_beginning += base;
2394 range_end += base;
2395
af34e669
DJ
2396 /* FIXME: This is recording everything as a low-high
2397 segment of consecutive addresses. We should have a
2398 data structure for discontiguous block ranges
2399 instead. */
2400 if (! low_set)
2401 {
2402 low = range_beginning;
2403 high = range_end;
2404 low_set = 1;
2405 }
2406 else
2407 {
2408 if (range_beginning < low)
2409 low = range_beginning;
2410 if (range_end > high)
2411 high = range_end;
2412 }
2413 }
2414
2415 if (! low_set)
2416 /* If the first entry is an end-of-list marker, the range
2417 describes an empty scope, i.e. no instructions. */
2418 return 0;
2419
2420 ret = -1;
2421 }
2422 }
c906108c
SS
2423
2424 if (high < low)
2425 return 0;
2426
2427 /* When using the GNU linker, .gnu.linkonce. sections are used to
2428 eliminate duplicate copies of functions and vtables and such.
2429 The linker will arbitrarily choose one and discard the others.
2430 The AT_*_pc values for such functions refer to local labels in
2431 these sections. If the section from that file was discarded, the
2432 labels are not in the output, so the relocs get a value of 0.
2433 If this is a discarded function, mark the pc bounds as invalid,
2434 so that GDB will ignore it. */
af34e669 2435 if (low == 0 && (bfd_get_file_flags (obfd) & HAS_RELOC) == 0)
c906108c
SS
2436 return 0;
2437
2438 *lowpc = low;
2439 *highpc = high;
af34e669 2440 return ret;
c906108c
SS
2441}
2442
2443/* Add an aggregate field to the field list. */
2444
2445static void
107d2387 2446dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73
DJ
2447 struct dwarf2_cu *cu)
2448{
2449 struct objfile *objfile = cu->objfile;
c906108c
SS
2450 struct nextfield *new_field;
2451 struct attribute *attr;
2452 struct field *fp;
2453 char *fieldname = "";
2454
2455 /* Allocate a new field list entry and link it in. */
2456 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 2457 make_cleanup (xfree, new_field);
c906108c
SS
2458 memset (new_field, 0, sizeof (struct nextfield));
2459 new_field->next = fip->fields;
2460 fip->fields = new_field;
2461 fip->nfields++;
2462
2463 /* Handle accessibility and virtuality of field.
2464 The default accessibility for members is public, the default
2465 accessibility for inheritance is private. */
2466 if (die->tag != DW_TAG_inheritance)
2467 new_field->accessibility = DW_ACCESS_public;
2468 else
2469 new_field->accessibility = DW_ACCESS_private;
2470 new_field->virtuality = DW_VIRTUALITY_none;
2471
2472 attr = dwarf_attr (die, DW_AT_accessibility);
2473 if (attr)
2474 new_field->accessibility = DW_UNSND (attr);
2475 if (new_field->accessibility != DW_ACCESS_public)
2476 fip->non_public_fields = 1;
2477 attr = dwarf_attr (die, DW_AT_virtuality);
2478 if (attr)
2479 new_field->virtuality = DW_UNSND (attr);
2480
2481 fp = &new_field->field;
a9a9bd0f
DC
2482
2483 if (die->tag == DW_TAG_member && ! die_is_declaration (die))
c906108c 2484 {
a9a9bd0f
DC
2485 /* Data member other than a C++ static data member. */
2486
c906108c 2487 /* Get type of field. */
e7c27a73 2488 fp->type = die_type (die, cu);
c906108c 2489
01ad7f36
DJ
2490 FIELD_STATIC_KIND (*fp) = 0;
2491
c906108c
SS
2492 /* Get bit size of field (zero if none). */
2493 attr = dwarf_attr (die, DW_AT_bit_size);
2494 if (attr)
2495 {
2496 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
2497 }
2498 else
2499 {
2500 FIELD_BITSIZE (*fp) = 0;
2501 }
2502
2503 /* Get bit offset of field. */
2504 attr = dwarf_attr (die, DW_AT_data_member_location);
2505 if (attr)
2506 {
2507 FIELD_BITPOS (*fp) =
e7c27a73 2508 decode_locdesc (DW_BLOCK (attr), cu) * bits_per_byte;
c906108c
SS
2509 }
2510 else
2511 FIELD_BITPOS (*fp) = 0;
2512 attr = dwarf_attr (die, DW_AT_bit_offset);
2513 if (attr)
2514 {
2515 if (BITS_BIG_ENDIAN)
2516 {
2517 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
2518 additional bit offset from the MSB of the containing
2519 anonymous object to the MSB of the field. We don't
2520 have to do anything special since we don't need to
2521 know the size of the anonymous object. */
c906108c
SS
2522 FIELD_BITPOS (*fp) += DW_UNSND (attr);
2523 }
2524 else
2525 {
2526 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
2527 MSB of the anonymous object, subtract off the number of
2528 bits from the MSB of the field to the MSB of the
2529 object, and then subtract off the number of bits of
2530 the field itself. The result is the bit offset of
2531 the LSB of the field. */
c906108c
SS
2532 int anonymous_size;
2533 int bit_offset = DW_UNSND (attr);
2534
2535 attr = dwarf_attr (die, DW_AT_byte_size);
2536 if (attr)
2537 {
2538 /* The size of the anonymous object containing
2539 the bit field is explicit, so use the
2540 indicated size (in bytes). */
2541 anonymous_size = DW_UNSND (attr);
2542 }
2543 else
2544 {
2545 /* The size of the anonymous object containing
2546 the bit field must be inferred from the type
2547 attribute of the data member containing the
2548 bit field. */
2549 anonymous_size = TYPE_LENGTH (fp->type);
2550 }
2551 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
2552 - bit_offset - FIELD_BITSIZE (*fp);
2553 }
2554 }
2555
2556 /* Get name of field. */
2557 attr = dwarf_attr (die, DW_AT_name);
2558 if (attr && DW_STRING (attr))
2559 fieldname = DW_STRING (attr);
2560 fp->name = obsavestring (fieldname, strlen (fieldname),
2561 &objfile->type_obstack);
2562
2563 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 2564 pointer or virtual base class pointer) to private. */
c906108c
SS
2565 if (dwarf_attr (die, DW_AT_artificial))
2566 {
2567 new_field->accessibility = DW_ACCESS_private;
2568 fip->non_public_fields = 1;
2569 }
2570 }
a9a9bd0f 2571 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 2572 {
a9a9bd0f
DC
2573 /* C++ static member. */
2574
2575 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
2576 is a declaration, but all versions of G++ as of this writing
2577 (so through at least 3.2.1) incorrectly generate
2578 DW_TAG_variable tags. */
2579
c906108c 2580 char *physname;
c906108c 2581
a9a9bd0f 2582 /* Get name of field. */
2df3850c
JM
2583 attr = dwarf_attr (die, DW_AT_name);
2584 if (attr && DW_STRING (attr))
2585 fieldname = DW_STRING (attr);
2586 else
c906108c
SS
2587 return;
2588
2df3850c
JM
2589 /* Get physical name. */
2590 physname = dwarf2_linkage_name (die);
c906108c
SS
2591
2592 SET_FIELD_PHYSNAME (*fp, obsavestring (physname, strlen (physname),
c5aa993b 2593 &objfile->type_obstack));
e7c27a73 2594 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c 2595 FIELD_NAME (*fp) = obsavestring (fieldname, strlen (fieldname),
c5aa993b 2596 &objfile->type_obstack);
c906108c
SS
2597 }
2598 else if (die->tag == DW_TAG_inheritance)
2599 {
2600 /* C++ base class field. */
2601 attr = dwarf_attr (die, DW_AT_data_member_location);
2602 if (attr)
e7c27a73 2603 FIELD_BITPOS (*fp) = (decode_locdesc (DW_BLOCK (attr), cu)
107d2387 2604 * bits_per_byte);
c906108c 2605 FIELD_BITSIZE (*fp) = 0;
01ad7f36 2606 FIELD_STATIC_KIND (*fp) = 0;
e7c27a73 2607 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
2608 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
2609 fip->nbaseclasses++;
2610 }
2611}
2612
2613/* Create the vector of fields, and attach it to the type. */
2614
2615static void
fba45db2 2616dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 2617 struct dwarf2_cu *cu)
c906108c
SS
2618{
2619 int nfields = fip->nfields;
2620
2621 /* Record the field count, allocate space for the array of fields,
2622 and create blank accessibility bitfields if necessary. */
2623 TYPE_NFIELDS (type) = nfields;
2624 TYPE_FIELDS (type) = (struct field *)
2625 TYPE_ALLOC (type, sizeof (struct field) * nfields);
2626 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
2627
2628 if (fip->non_public_fields)
2629 {
2630 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2631
2632 TYPE_FIELD_PRIVATE_BITS (type) =
2633 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
2634 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
2635
2636 TYPE_FIELD_PROTECTED_BITS (type) =
2637 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
2638 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
2639
2640 TYPE_FIELD_IGNORE_BITS (type) =
2641 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
2642 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
2643 }
2644
2645 /* If the type has baseclasses, allocate and clear a bit vector for
2646 TYPE_FIELD_VIRTUAL_BITS. */
2647 if (fip->nbaseclasses)
2648 {
2649 int num_bytes = B_BYTES (fip->nbaseclasses);
2650 char *pointer;
2651
2652 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2653 pointer = (char *) TYPE_ALLOC (type, num_bytes);
2654 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
2655 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
2656 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
2657 }
2658
2659 /* Copy the saved-up fields into the field vector. Start from the head
2660 of the list, adding to the tail of the field array, so that they end
2661 up in the same order in the array in which they were added to the list. */
2662 while (nfields-- > 0)
2663 {
2664 TYPE_FIELD (type, nfields) = fip->fields->field;
2665 switch (fip->fields->accessibility)
2666 {
c5aa993b
JM
2667 case DW_ACCESS_private:
2668 SET_TYPE_FIELD_PRIVATE (type, nfields);
2669 break;
c906108c 2670
c5aa993b
JM
2671 case DW_ACCESS_protected:
2672 SET_TYPE_FIELD_PROTECTED (type, nfields);
2673 break;
c906108c 2674
c5aa993b
JM
2675 case DW_ACCESS_public:
2676 break;
c906108c 2677
c5aa993b
JM
2678 default:
2679 /* Unknown accessibility. Complain and treat it as public. */
2680 {
4d3c2250
KB
2681 complaint (&symfile_complaints, "unsupported accessibility %d",
2682 fip->fields->accessibility);
c5aa993b
JM
2683 }
2684 break;
c906108c
SS
2685 }
2686 if (nfields < fip->nbaseclasses)
2687 {
2688 switch (fip->fields->virtuality)
2689 {
c5aa993b
JM
2690 case DW_VIRTUALITY_virtual:
2691 case DW_VIRTUALITY_pure_virtual:
2692 SET_TYPE_FIELD_VIRTUAL (type, nfields);
2693 break;
c906108c
SS
2694 }
2695 }
2696 fip->fields = fip->fields->next;
2697 }
2698}
2699
c906108c
SS
2700/* Add a member function to the proper fieldlist. */
2701
2702static void
107d2387 2703dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 2704 struct type *type, struct dwarf2_cu *cu)
c906108c 2705{
e7c27a73 2706 struct objfile *objfile = cu->objfile;
c906108c
SS
2707 struct attribute *attr;
2708 struct fnfieldlist *flp;
2709 int i;
2710 struct fn_field *fnp;
2711 char *fieldname;
2712 char *physname;
2713 struct nextfnfield *new_fnfield;
2714
2df3850c
JM
2715 /* Get name of member function. */
2716 attr = dwarf_attr (die, DW_AT_name);
2717 if (attr && DW_STRING (attr))
2718 fieldname = DW_STRING (attr);
c906108c 2719 else
2df3850c 2720 return;
c906108c 2721
2df3850c
JM
2722 /* Get the mangled name. */
2723 physname = dwarf2_linkage_name (die);
c906108c
SS
2724
2725 /* Look up member function name in fieldlist. */
2726 for (i = 0; i < fip->nfnfields; i++)
2727 {
27bfe10e 2728 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
2729 break;
2730 }
2731
2732 /* Create new list element if necessary. */
2733 if (i < fip->nfnfields)
2734 flp = &fip->fnfieldlists[i];
2735 else
2736 {
2737 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
2738 {
2739 fip->fnfieldlists = (struct fnfieldlist *)
2740 xrealloc (fip->fnfieldlists,
2741 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 2742 * sizeof (struct fnfieldlist));
c906108c 2743 if (fip->nfnfields == 0)
c13c43fd 2744 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
2745 }
2746 flp = &fip->fnfieldlists[fip->nfnfields];
2747 flp->name = fieldname;
2748 flp->length = 0;
2749 flp->head = NULL;
2750 fip->nfnfields++;
2751 }
2752
2753 /* Create a new member function field and chain it to the field list
2754 entry. */
2755 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 2756 make_cleanup (xfree, new_fnfield);
c906108c
SS
2757 memset (new_fnfield, 0, sizeof (struct nextfnfield));
2758 new_fnfield->next = flp->head;
2759 flp->head = new_fnfield;
2760 flp->length++;
2761
2762 /* Fill in the member function field info. */
2763 fnp = &new_fnfield->fnfield;
2764 fnp->physname = obsavestring (physname, strlen (physname),
2765 &objfile->type_obstack);
2766 fnp->type = alloc_type (objfile);
2767 if (die->type && TYPE_CODE (die->type) == TYPE_CODE_FUNC)
2768 {
c906108c 2769 int nparams = TYPE_NFIELDS (die->type);
c906108c 2770
e26fb1d7
DC
2771 /* TYPE is the domain of this method, and DIE->TYPE is the type
2772 of the method itself (TYPE_CODE_METHOD). */
2773 smash_to_method_type (fnp->type, type,
ad2f7632
DJ
2774 TYPE_TARGET_TYPE (die->type),
2775 TYPE_FIELDS (die->type),
2776 TYPE_NFIELDS (die->type),
2777 TYPE_VARARGS (die->type));
c906108c
SS
2778
2779 /* Handle static member functions.
c5aa993b
JM
2780 Dwarf2 has no clean way to discern C++ static and non-static
2781 member functions. G++ helps GDB by marking the first
2782 parameter for non-static member functions (which is the
2783 this pointer) as artificial. We obtain this information
2784 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
c906108c
SS
2785 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (die->type, 0) == 0)
2786 fnp->voffset = VOFFSET_STATIC;
2787 }
2788 else
4d3c2250
KB
2789 complaint (&symfile_complaints, "member function type missing for '%s'",
2790 physname);
c906108c
SS
2791
2792 /* Get fcontext from DW_AT_containing_type if present. */
2793 if (dwarf_attr (die, DW_AT_containing_type) != NULL)
e7c27a73 2794 fnp->fcontext = die_containing_type (die, cu);
c906108c
SS
2795
2796 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
2797 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
2798
2799 /* Get accessibility. */
2800 attr = dwarf_attr (die, DW_AT_accessibility);
2801 if (attr)
2802 {
2803 switch (DW_UNSND (attr))
2804 {
c5aa993b
JM
2805 case DW_ACCESS_private:
2806 fnp->is_private = 1;
2807 break;
2808 case DW_ACCESS_protected:
2809 fnp->is_protected = 1;
2810 break;
c906108c
SS
2811 }
2812 }
2813
b02dede2
DJ
2814 /* Check for artificial methods. */
2815 attr = dwarf_attr (die, DW_AT_artificial);
2816 if (attr && DW_UNSND (attr) != 0)
2817 fnp->is_artificial = 1;
2818
c906108c
SS
2819 /* Get index in virtual function table if it is a virtual member function. */
2820 attr = dwarf_attr (die, DW_AT_vtable_elem_location);
2821 if (attr)
8e19ed76
PS
2822 {
2823 /* Support the .debug_loc offsets */
2824 if (attr_form_is_block (attr))
2825 {
e7c27a73 2826 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8e19ed76
PS
2827 }
2828 else if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
2829 {
4d3c2250 2830 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
2831 }
2832 else
2833 {
4d3c2250
KB
2834 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
2835 fieldname);
8e19ed76
PS
2836 }
2837 }
c906108c
SS
2838}
2839
2840/* Create the vector of member function fields, and attach it to the type. */
2841
2842static void
fba45db2 2843dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 2844 struct dwarf2_cu *cu)
c906108c
SS
2845{
2846 struct fnfieldlist *flp;
2847 int total_length = 0;
2848 int i;
2849
2850 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2851 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
2852 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
2853
2854 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
2855 {
2856 struct nextfnfield *nfp = flp->head;
2857 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
2858 int k;
2859
2860 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
2861 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
2862 fn_flp->fn_fields = (struct fn_field *)
2863 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
2864 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 2865 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
2866
2867 total_length += flp->length;
2868 }
2869
2870 TYPE_NFN_FIELDS (type) = fip->nfnfields;
2871 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
2872}
2873
2874/* Called when we find the DIE that starts a structure or union scope
2875 (definition) to process all dies that define the members of the
2876 structure or union.
2877
2878 NOTE: we need to call struct_type regardless of whether or not the
2879 DIE has an at_name attribute, since it might be an anonymous
2880 structure or union. This gets the type entered into our set of
2881 user defined types.
2882
2883 However, if the structure is incomplete (an opaque struct/union)
2884 then suppress creating a symbol table entry for it since gdb only
2885 wants to find the one with the complete definition. Note that if
2886 it is complete, we just call new_symbol, which does it's own
2887 checking about whether the struct/union is anonymous or not (and
2888 suppresses creating a symbol table entry itself). */
2889
2890static void
e7c27a73 2891read_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 2892{
e7c27a73 2893 struct objfile *objfile = cu->objfile;
c906108c
SS
2894 struct type *type;
2895 struct attribute *attr;
63d06c5c
DC
2896 const char *name = NULL;
2897 const char *previous_prefix = processing_current_prefix;
2898 struct cleanup *back_to = NULL;
2899 /* This says whether or not we want to try to update the structure's
2900 name to include enclosing namespace/class information, if
2901 any. */
2902 int need_to_update_name = 0;
c906108c
SS
2903
2904 type = alloc_type (objfile);
2905
2906 INIT_CPLUS_SPECIFIC (type);
2907 attr = dwarf_attr (die, DW_AT_name);
2908 if (attr && DW_STRING (attr))
2909 {
63d06c5c
DC
2910 name = DW_STRING (attr);
2911
2912 if (cu_language == language_cplus)
2913 {
2914 struct die_info *spec_die = die_specification (die);
2915
2916 if (spec_die != NULL)
2917 {
2918 char *specification_prefix = determine_prefix (spec_die);
2919 processing_current_prefix = specification_prefix;
2920 back_to = make_cleanup (xfree, specification_prefix);
2921 }
2922 }
2923
2924 if (processing_has_namespace_info)
2925 {
2926 /* FIXME: carlton/2003-11-10: This variable exists only for
2927 const-correctness reasons. When I tried to change
2928 TYPE_TAG_NAME to be a const char *, I ran into a cascade
2929 of changes which would have forced decode_line_1 to take
2930 a const char **. */
2931 char *new_prefix = obconcat (&objfile->type_obstack,
2932 processing_current_prefix,
2933 processing_current_prefix[0] == '\0'
2934 ? "" : "::",
2935 name);
2936 TYPE_TAG_NAME (type) = new_prefix;
2937 processing_current_prefix = new_prefix;
2938 }
2939 else
2940 {
2941 TYPE_TAG_NAME (type) = obsavestring (name, strlen (name),
2942 &objfile->type_obstack);
2943 need_to_update_name = (cu_language == language_cplus);
2944 }
c906108c
SS
2945 }
2946
2947 if (die->tag == DW_TAG_structure_type)
2948 {
2949 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2950 }
2951 else if (die->tag == DW_TAG_union_type)
2952 {
2953 TYPE_CODE (type) = TYPE_CODE_UNION;
2954 }
2955 else
2956 {
2957 /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
c5aa993b 2958 in gdbtypes.h. */
c906108c
SS
2959 TYPE_CODE (type) = TYPE_CODE_CLASS;
2960 }
2961
2962 attr = dwarf_attr (die, DW_AT_byte_size);
2963 if (attr)
2964 {
2965 TYPE_LENGTH (type) = DW_UNSND (attr);
2966 }
2967 else
2968 {
2969 TYPE_LENGTH (type) = 0;
2970 }
2971
2972 /* We need to add the type field to the die immediately so we don't
2973 infinitely recurse when dealing with pointers to the structure
2974 type within the structure itself. */
2975 die->type = type;
2976
639d11d3 2977 if (die->child != NULL && ! die_is_declaration (die))
c906108c
SS
2978 {
2979 struct field_info fi;
2980 struct die_info *child_die;
2981 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
2982
2983 memset (&fi, 0, sizeof (struct field_info));
2984
639d11d3 2985 child_die = die->child;
c906108c
SS
2986
2987 while (child_die && child_die->tag)
2988 {
a9a9bd0f
DC
2989 if (child_die->tag == DW_TAG_member
2990 || child_die->tag == DW_TAG_variable)
c906108c 2991 {
a9a9bd0f
DC
2992 /* NOTE: carlton/2002-11-05: A C++ static data member
2993 should be a DW_TAG_member that is a declaration, but
2994 all versions of G++ as of this writing (so through at
2995 least 3.2.1) incorrectly generate DW_TAG_variable
2996 tags for them instead. */
e7c27a73 2997 dwarf2_add_field (&fi, child_die, cu);
c906108c 2998 }
8713b1b1 2999 else if (child_die->tag == DW_TAG_subprogram)
c906108c
SS
3000 {
3001 /* C++ member function. */
e7c27a73
DJ
3002 process_die (child_die, cu);
3003 dwarf2_add_member_fn (&fi, child_die, type, cu);
63d06c5c
DC
3004 if (need_to_update_name)
3005 {
3006 /* The demangled names of member functions contain
3007 information about enclosing namespaces/classes,
3008 if any. */
3009
3010 /* FIXME: carlton/2003-11-10: The excessive
3011 demangling here is a bit wasteful, as is the
3012 memory usage for names. */
3013
3014 /* NOTE: carlton/2003-11-10: As commented in
3015 add_partial_structure, the demangler sometimes
3016 prints the type info in a different form from the
3017 debug info. We could solve this by using the
3018 demangled name to get the prefix; if doing so,
3019 however, we'd need to be careful when reading a
3020 class that's nested inside a template class.
3021 That would also cause problems when trying to
3022 determine RTTI information, since we use the
3023 demangler to determine the appropriate class
3024 name. */
3025 char *actual_class_name
3026 = class_name_from_physname (dwarf2_linkage_name
3027 (child_die));
3028 if (actual_class_name != NULL
3029 && strcmp (actual_class_name, name) != 0)
3030 {
3031 TYPE_TAG_NAME (type)
3032 = obsavestring (actual_class_name,
3033 strlen (actual_class_name),
3034 &objfile->type_obstack);
3035 }
3036 xfree (actual_class_name);
3037 need_to_update_name = 0;
3038 }
c906108c
SS
3039 }
3040 else if (child_die->tag == DW_TAG_inheritance)
3041 {
3042 /* C++ base class field. */
e7c27a73 3043 dwarf2_add_field (&fi, child_die, cu);
c906108c
SS
3044 }
3045 else
3046 {
e7c27a73 3047 process_die (child_die, cu);
c906108c
SS
3048 }
3049 child_die = sibling_die (child_die);
3050 }
3051
3052 /* Attach fields and member functions to the type. */
3053 if (fi.nfields)
e7c27a73 3054 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
3055 if (fi.nfnfields)
3056 {
e7c27a73 3057 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 3058
c5aa993b 3059 /* Get the type which refers to the base class (possibly this
c906108c
SS
3060 class itself) which contains the vtable pointer for the current
3061 class from the DW_AT_containing_type attribute. */
3062
3063 if (dwarf_attr (die, DW_AT_containing_type) != NULL)
3064 {
e7c27a73 3065 struct type *t = die_containing_type (die, cu);
c906108c
SS
3066
3067 TYPE_VPTR_BASETYPE (type) = t;
3068 if (type == t)
3069 {
c5aa993b
JM
3070 static const char vptr_name[] =
3071 {'_', 'v', 'p', 't', 'r', '\0'};
c906108c
SS
3072 int i;
3073
3074 /* Our own class provides vtbl ptr. */
3075 for (i = TYPE_NFIELDS (t) - 1;
3076 i >= TYPE_N_BASECLASSES (t);
3077 --i)
3078 {
3079 char *fieldname = TYPE_FIELD_NAME (t, i);
3080
27bfe10e
JB
3081 if ((strncmp (fieldname, vptr_name,
3082 strlen (vptr_name) - 1)
3083 == 0)
c906108c
SS
3084 && is_cplus_marker (fieldname[strlen (vptr_name)]))
3085 {
3086 TYPE_VPTR_FIELDNO (type) = i;
3087 break;
3088 }
3089 }
3090
3091 /* Complain if virtual function table field not found. */
3092 if (i < TYPE_N_BASECLASSES (t))
4d3c2250
KB
3093 complaint (&symfile_complaints,
3094 "virtual function table pointer not found when defining class '%s'",
3095 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
3096 "");
c906108c
SS
3097 }
3098 else
3099 {
3100 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
3101 }
3102 }
3103 }
3104
e7c27a73 3105 new_symbol (die, type, cu);
c906108c
SS
3106
3107 do_cleanups (back_to);
3108 }
3109 else
3110 {
3111 /* No children, must be stub. */
3112 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
3113 }
63d06c5c
DC
3114
3115 processing_current_prefix = previous_prefix;
3116 if (back_to != NULL)
3117 do_cleanups (back_to);
c906108c
SS
3118}
3119
3120/* Given a pointer to a die which begins an enumeration, process all
3121 the dies that define the members of the enumeration.
3122
3123 This will be much nicer in draft 6 of the DWARF spec when our
3124 members will be dies instead squished into the DW_AT_element_list
3125 attribute.
3126
3127 NOTE: We reverse the order of the element list. */
3128
3129static void
e7c27a73 3130read_enumeration (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3131{
e7c27a73 3132 struct objfile *objfile = cu->objfile;
c906108c
SS
3133 struct die_info *child_die;
3134 struct type *type;
3135 struct field *fields;
3136 struct attribute *attr;
3137 struct symbol *sym;
3138 int num_fields;
3139 int unsigned_enum = 1;
3140
3141 type = alloc_type (objfile);
3142
3143 TYPE_CODE (type) = TYPE_CODE_ENUM;
3144 attr = dwarf_attr (die, DW_AT_name);
3145 if (attr && DW_STRING (attr))
3146 {
63d06c5c
DC
3147 const char *name = DW_STRING (attr);
3148
3149 if (processing_has_namespace_info)
3150 {
3151 TYPE_TAG_NAME (type) = obconcat (&objfile->type_obstack,
3152 processing_current_prefix,
3153 processing_current_prefix[0] == '\0'
3154 ? "" : "::",
3155 name);
3156 }
3157 else
3158 {
3159 TYPE_TAG_NAME (type) = obsavestring (name, strlen (name),
3160 &objfile->type_obstack);
3161 }
c906108c
SS
3162 }
3163
3164 attr = dwarf_attr (die, DW_AT_byte_size);
3165 if (attr)
3166 {
3167 TYPE_LENGTH (type) = DW_UNSND (attr);
3168 }
3169 else
3170 {
3171 TYPE_LENGTH (type) = 0;
3172 }
3173
3174 num_fields = 0;
3175 fields = NULL;
639d11d3 3176 if (die->child != NULL)
c906108c 3177 {
639d11d3 3178 child_die = die->child;
c906108c
SS
3179 while (child_die && child_die->tag)
3180 {
3181 if (child_die->tag != DW_TAG_enumerator)
3182 {
e7c27a73 3183 process_die (child_die, cu);
c906108c
SS
3184 }
3185 else
3186 {
3187 attr = dwarf_attr (child_die, DW_AT_name);
3188 if (attr)
3189 {
e7c27a73 3190 sym = new_symbol (child_die, type, cu);
c906108c
SS
3191 if (SYMBOL_VALUE (sym) < 0)
3192 unsigned_enum = 0;
3193
3194 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
3195 {
3196 fields = (struct field *)
3197 xrealloc (fields,
3198 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 3199 * sizeof (struct field));
c906108c
SS
3200 }
3201
22abf04a 3202 FIELD_NAME (fields[num_fields]) = DEPRECATED_SYMBOL_NAME (sym);
c906108c
SS
3203 FIELD_TYPE (fields[num_fields]) = NULL;
3204 FIELD_BITPOS (fields[num_fields]) = SYMBOL_VALUE (sym);
3205 FIELD_BITSIZE (fields[num_fields]) = 0;
01ad7f36 3206 FIELD_STATIC_KIND (fields[num_fields]) = 0;
c906108c
SS
3207
3208 num_fields++;
3209 }
3210 }
3211
3212 child_die = sibling_die (child_die);
3213 }
3214
3215 if (num_fields)
3216 {
3217 TYPE_NFIELDS (type) = num_fields;
3218 TYPE_FIELDS (type) = (struct field *)
3219 TYPE_ALLOC (type, sizeof (struct field) * num_fields);
3220 memcpy (TYPE_FIELDS (type), fields,
3221 sizeof (struct field) * num_fields);
b8c9b27d 3222 xfree (fields);
c906108c
SS
3223 }
3224 if (unsigned_enum)
3225 TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
3226 }
3227 die->type = type;
e7c27a73 3228 new_symbol (die, type, cu);
c906108c
SS
3229}
3230
3231/* Extract all information from a DW_TAG_array_type DIE and put it in
3232 the DIE's type field. For now, this only handles one dimensional
3233 arrays. */
3234
3235static void
e7c27a73 3236read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3237{
e7c27a73 3238 struct objfile *objfile = cu->objfile;
c906108c
SS
3239 struct die_info *child_die;
3240 struct type *type = NULL;
3241 struct type *element_type, *range_type, *index_type;
3242 struct type **range_types = NULL;
3243 struct attribute *attr;
3244 int ndim = 0;
3245 struct cleanup *back_to;
3246
3247 /* Return if we've already decoded this type. */
3248 if (die->type)
3249 {
3250 return;
3251 }
3252
e7c27a73 3253 element_type = die_type (die, cu);
c906108c
SS
3254
3255 /* Irix 6.2 native cc creates array types without children for
3256 arrays with unspecified length. */
639d11d3 3257 if (die->child == NULL)
c906108c
SS
3258 {
3259 index_type = dwarf2_fundamental_type (objfile, FT_INTEGER);
3260 range_type = create_range_type (NULL, index_type, 0, -1);
3261 die->type = create_array_type (NULL, element_type, range_type);
3262 return;
3263 }
3264
3265 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 3266 child_die = die->child;
c906108c
SS
3267 while (child_die && child_die->tag)
3268 {
3269 if (child_die->tag == DW_TAG_subrange_type)
3270 {
3271 unsigned int low, high;
3272
3273 /* Default bounds to an array with unspecified length. */
3274 low = 0;
3275 high = -1;
3276 if (cu_language == language_fortran)
3277 {
3278 /* FORTRAN implies a lower bound of 1, if not given. */
3279 low = 1;
3280 }
3281
e7c27a73 3282 index_type = die_type (child_die, cu);
c906108c
SS
3283 attr = dwarf_attr (child_die, DW_AT_lower_bound);
3284 if (attr)
3285 {
3286 if (attr->form == DW_FORM_sdata)
3287 {
3288 low = DW_SND (attr);
3289 }
3290 else if (attr->form == DW_FORM_udata
c5aa993b
JM
3291 || attr->form == DW_FORM_data1
3292 || attr->form == DW_FORM_data2
96383835
RH
3293 || attr->form == DW_FORM_data4
3294 || attr->form == DW_FORM_data8)
c906108c
SS
3295 {
3296 low = DW_UNSND (attr);
3297 }
3298 else
3299 {
4d3c2250
KB
3300 dwarf2_non_const_array_bound_ignored_complaint
3301 (dwarf_form_name (attr->form));
c906108c 3302 low = 0;
c906108c
SS
3303 }
3304 }
3305 attr = dwarf_attr (child_die, DW_AT_upper_bound);
3306 if (attr)
3307 {
3308 if (attr->form == DW_FORM_sdata)
3309 {
3310 high = DW_SND (attr);
3311 }
3312 else if (attr->form == DW_FORM_udata
c5aa993b
JM
3313 || attr->form == DW_FORM_data1
3314 || attr->form == DW_FORM_data2
96383835
RH
3315 || attr->form == DW_FORM_data4
3316 || attr->form == DW_FORM_data8)
c906108c
SS
3317 {
3318 high = DW_UNSND (attr);
3319 }
3320 else if (attr->form == DW_FORM_block1)
3321 {
3322 /* GCC encodes arrays with unspecified or dynamic length
3323 with a DW_FORM_block1 attribute.
3324 FIXME: GDB does not yet know how to handle dynamic
3325 arrays properly, treat them as arrays with unspecified
8c2957c4
JB
3326 length for now.
3327
3328 FIXME: jimb/2003-09-22: GDB does not really know
3329 how to handle arrays of unspecified length
3330 either; we just represent them as zero-length
3331 arrays. Choose an appropriate upper bound given
3332 the lower bound we've computed above. */
3333 high = low - 1;
c906108c
SS
3334 }
3335 else
3336 {
4d3c2250
KB
3337 dwarf2_non_const_array_bound_ignored_complaint
3338 (dwarf_form_name (attr->form));
c906108c 3339 high = 1;
c906108c
SS
3340 }
3341 }
3342
3343 /* Create a range type and save it for array type creation. */
3344 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
3345 {
3346 range_types = (struct type **)
3347 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
c5aa993b 3348 * sizeof (struct type *));
c906108c 3349 if (ndim == 0)
c13c43fd 3350 make_cleanup (free_current_contents, &range_types);
c906108c
SS
3351 }
3352 range_types[ndim++] = create_range_type (NULL, index_type, low, high);
3353 }
3354 child_die = sibling_die (child_die);
3355 }
3356
3357 /* Dwarf2 dimensions are output from left to right, create the
3358 necessary array types in backwards order. */
3359 type = element_type;
3360 while (ndim-- > 0)
3361 type = create_array_type (NULL, type, range_types[ndim]);
3362
f5f8a009
EZ
3363 /* Understand Dwarf2 support for vector types (like they occur on
3364 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
3365 array type. This is not part of the Dwarf2/3 standard yet, but a
3366 custom vendor extension. The main difference between a regular
3367 array and the vector variant is that vectors are passed by value
3368 to functions. */
3369 attr = dwarf_attr (die, DW_AT_GNU_vector);
3370 if (attr)
3371 TYPE_FLAGS (type) |= TYPE_FLAG_VECTOR;
3372
c906108c
SS
3373 do_cleanups (back_to);
3374
3375 /* Install the type in the die. */
3376 die->type = type;
3377}
3378
3379/* First cut: install each common block member as a global variable. */
3380
3381static void
e7c27a73 3382read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
3383{
3384 struct die_info *child_die;
3385 struct attribute *attr;
3386 struct symbol *sym;
3387 CORE_ADDR base = (CORE_ADDR) 0;
3388
3389 attr = dwarf_attr (die, DW_AT_location);
3390 if (attr)
3391 {
8e19ed76
PS
3392 /* Support the .debug_loc offsets */
3393 if (attr_form_is_block (attr))
3394 {
e7c27a73 3395 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76
PS
3396 }
3397 else if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
3398 {
4d3c2250 3399 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
3400 }
3401 else
3402 {
4d3c2250
KB
3403 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
3404 "common block member");
8e19ed76 3405 }
c906108c 3406 }
639d11d3 3407 if (die->child != NULL)
c906108c 3408 {
639d11d3 3409 child_die = die->child;
c906108c
SS
3410 while (child_die && child_die->tag)
3411 {
e7c27a73 3412 sym = new_symbol (child_die, NULL, cu);
c906108c
SS
3413 attr = dwarf_attr (child_die, DW_AT_data_member_location);
3414 if (attr)
3415 {
3416 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 3417 base + decode_locdesc (DW_BLOCK (attr), cu);
c906108c
SS
3418 add_symbol_to_list (sym, &global_symbols);
3419 }
3420 child_die = sibling_die (child_die);
3421 }
3422 }
3423}
3424
d9fa45fe
DC
3425/* Read a C++ namespace. */
3426
d9fa45fe 3427static void
e7c27a73 3428read_namespace (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 3429{
e7c27a73 3430 struct objfile *objfile = cu->objfile;
38d518c9 3431 const char *previous_prefix = processing_current_prefix;
63d06c5c 3432 const char *name;
9219021c
DC
3433 int is_anonymous;
3434 struct die_info *current_die;
3435
38d518c9 3436 name = namespace_name (die, &is_anonymous);
9219021c
DC
3437
3438 /* Now build the name of the current namespace. */
3439
38d518c9 3440 if (previous_prefix[0] == '\0')
9219021c 3441 {
38d518c9 3442 processing_current_prefix = name;
9219021c
DC
3443 }
3444 else
3445 {
38d518c9 3446 /* We need temp_name around because processing_current_prefix
9219021c 3447 is a const char *. */
38d518c9 3448 char *temp_name = alloca (strlen (previous_prefix)
9219021c 3449 + 2 + strlen(name) + 1);
38d518c9 3450 strcpy (temp_name, previous_prefix);
9219021c
DC
3451 strcat (temp_name, "::");
3452 strcat (temp_name, name);
3453
38d518c9 3454 processing_current_prefix = temp_name;
9219021c
DC
3455 }
3456
5c4e30ca
DC
3457 /* Add a symbol associated to this if we haven't seen the namespace
3458 before. Also, add a using directive if it's an anonymous
3459 namespace. */
9219021c 3460
5c4e30ca
DC
3461 if (dwarf2_extension (die) == NULL)
3462 {
3463 struct type *type;
3464
3465 /* FIXME: carlton/2003-06-27: Once GDB is more const-correct,
3466 this cast will hopefully become unnecessary. */
3467 type = init_type (TYPE_CODE_NAMESPACE, 0, 0,
38d518c9 3468 (char *) processing_current_prefix,
5c4e30ca
DC
3469 objfile);
3470 TYPE_TAG_NAME (type) = TYPE_NAME (type);
3471
e7c27a73 3472 new_symbol (die, type, cu);
5c4e30ca
DC
3473
3474 if (is_anonymous)
38d518c9
EZ
3475 cp_add_using_directive (processing_current_prefix,
3476 strlen (previous_prefix),
3477 strlen (processing_current_prefix));
5c4e30ca 3478 }
9219021c 3479
639d11d3 3480 if (die->child != NULL)
d9fa45fe 3481 {
639d11d3 3482 struct die_info *child_die = die->child;
d9fa45fe
DC
3483
3484 while (child_die && child_die->tag)
3485 {
e7c27a73 3486 process_die (child_die, cu);
d9fa45fe
DC
3487 child_die = sibling_die (child_die);
3488 }
3489 }
9219021c 3490
38d518c9
EZ
3491 processing_current_prefix = previous_prefix;
3492}
3493
3494/* Return the name of the namespace represented by DIE. Set
3495 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
3496 namespace. */
3497
3498static const char *
3499namespace_name (struct die_info *die, int *is_anonymous)
3500{
3501 struct die_info *current_die;
3502 const char *name = NULL;
3503
3504 /* Loop through the extensions until we find a name. */
3505
3506 for (current_die = die;
3507 current_die != NULL;
3508 current_die = dwarf2_extension (die))
3509 {
3510 name = dwarf2_name (current_die);
3511 if (name != NULL)
3512 break;
3513 }
3514
3515 /* Is it an anonymous namespace? */
3516
3517 *is_anonymous = (name == NULL);
3518 if (*is_anonymous)
3519 name = "(anonymous namespace)";
3520
3521 return name;
d9fa45fe
DC
3522}
3523
c906108c
SS
3524/* Extract all information from a DW_TAG_pointer_type DIE and add to
3525 the user defined type vector. */
3526
3527static void
e7c27a73 3528read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3529{
e7c27a73 3530 struct comp_unit_head *cu_header = &cu->header;
c906108c 3531 struct type *type;
8b2dbe47
KB
3532 struct attribute *attr_byte_size;
3533 struct attribute *attr_address_class;
3534 int byte_size, addr_class;
c906108c
SS
3535
3536 if (die->type)
3537 {
3538 return;
3539 }
3540
e7c27a73 3541 type = lookup_pointer_type (die_type (die, cu));
8b2dbe47
KB
3542
3543 attr_byte_size = dwarf_attr (die, DW_AT_byte_size);
3544 if (attr_byte_size)
3545 byte_size = DW_UNSND (attr_byte_size);
c906108c 3546 else
8b2dbe47
KB
3547 byte_size = cu_header->addr_size;
3548
3549 attr_address_class = dwarf_attr (die, DW_AT_address_class);
3550 if (attr_address_class)
3551 addr_class = DW_UNSND (attr_address_class);
3552 else
3553 addr_class = DW_ADDR_none;
3554
3555 /* If the pointer size or address class is different than the
3556 default, create a type variant marked as such and set the
3557 length accordingly. */
3558 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 3559 {
8b2dbe47
KB
3560 if (ADDRESS_CLASS_TYPE_FLAGS_P ())
3561 {
3562 int type_flags;
3563
3564 type_flags = ADDRESS_CLASS_TYPE_FLAGS (byte_size, addr_class);
3565 gdb_assert ((type_flags & ~TYPE_FLAG_ADDRESS_CLASS_ALL) == 0);
3566 type = make_type_with_address_space (type, type_flags);
3567 }
3568 else if (TYPE_LENGTH (type) != byte_size)
3569 {
4d3c2250 3570 complaint (&symfile_complaints, "invalid pointer size %d", byte_size);
8b2dbe47
KB
3571 }
3572 else {
3573 /* Should we also complain about unhandled address classes? */
3574 }
c906108c 3575 }
8b2dbe47
KB
3576
3577 TYPE_LENGTH (type) = byte_size;
c906108c
SS
3578 die->type = type;
3579}
3580
3581/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
3582 the user defined type vector. */
3583
3584static void
e7c27a73 3585read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3586{
e7c27a73 3587 struct objfile *objfile = cu->objfile;
c906108c
SS
3588 struct type *type;
3589 struct type *to_type;
3590 struct type *domain;
3591
3592 if (die->type)
3593 {
3594 return;
3595 }
3596
3597 type = alloc_type (objfile);
e7c27a73
DJ
3598 to_type = die_type (die, cu);
3599 domain = die_containing_type (die, cu);
c906108c
SS
3600 smash_to_member_type (type, domain, to_type);
3601
3602 die->type = type;
3603}
3604
3605/* Extract all information from a DW_TAG_reference_type DIE and add to
3606 the user defined type vector. */
3607
3608static void
e7c27a73 3609read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3610{
e7c27a73 3611 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
3612 struct type *type;
3613 struct attribute *attr;
3614
3615 if (die->type)
3616 {
3617 return;
3618 }
3619
e7c27a73 3620 type = lookup_reference_type (die_type (die, cu));
c906108c
SS
3621 attr = dwarf_attr (die, DW_AT_byte_size);
3622 if (attr)
3623 {
3624 TYPE_LENGTH (type) = DW_UNSND (attr);
3625 }
3626 else
3627 {
107d2387 3628 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c
SS
3629 }
3630 die->type = type;
3631}
3632
3633static void
e7c27a73 3634read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3635{
090c42a4
JB
3636 struct type *base_type;
3637
c906108c
SS
3638 if (die->type)
3639 {
3640 return;
3641 }
3642
e7c27a73 3643 base_type = die_type (die, cu);
090c42a4 3644 die->type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
c906108c
SS
3645}
3646
3647static void
e7c27a73 3648read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3649{
090c42a4
JB
3650 struct type *base_type;
3651
c906108c
SS
3652 if (die->type)
3653 {
3654 return;
3655 }
3656
e7c27a73 3657 base_type = die_type (die, cu);
090c42a4 3658 die->type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
c906108c
SS
3659}
3660
3661/* Extract all information from a DW_TAG_string_type DIE and add to
3662 the user defined type vector. It isn't really a user defined type,
3663 but it behaves like one, with other DIE's using an AT_user_def_type
3664 attribute to reference it. */
3665
3666static void
e7c27a73 3667read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3668{
e7c27a73 3669 struct objfile *objfile = cu->objfile;
c906108c
SS
3670 struct type *type, *range_type, *index_type, *char_type;
3671 struct attribute *attr;
3672 unsigned int length;
3673
3674 if (die->type)
3675 {
3676 return;
3677 }
3678
3679 attr = dwarf_attr (die, DW_AT_string_length);
3680 if (attr)
3681 {
3682 length = DW_UNSND (attr);
3683 }
3684 else
3685 {
b21b22e0
PS
3686 /* check for the DW_AT_byte_size attribute */
3687 attr = dwarf_attr (die, DW_AT_byte_size);
3688 if (attr)
3689 {
3690 length = DW_UNSND (attr);
3691 }
3692 else
3693 {
3694 length = 1;
3695 }
c906108c
SS
3696 }
3697 index_type = dwarf2_fundamental_type (objfile, FT_INTEGER);
3698 range_type = create_range_type (NULL, index_type, 1, length);
b21b22e0
PS
3699 if (cu_language == language_fortran)
3700 {
3701 /* Need to create a unique string type for bounds
3702 information */
3703 type = create_string_type (0, range_type);
3704 }
3705 else
3706 {
3707 char_type = dwarf2_fundamental_type (objfile, FT_CHAR);
3708 type = create_string_type (char_type, range_type);
3709 }
c906108c
SS
3710 die->type = type;
3711}
3712
3713/* Handle DIES due to C code like:
3714
3715 struct foo
c5aa993b
JM
3716 {
3717 int (*funcp)(int a, long l);
3718 int b;
3719 };
c906108c
SS
3720
3721 ('funcp' generates a DW_TAG_subroutine_type DIE)
c5aa993b 3722 */
c906108c
SS
3723
3724static void
e7c27a73 3725read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
3726{
3727 struct type *type; /* Type that this function returns */
3728 struct type *ftype; /* Function that returns above type */
3729 struct attribute *attr;
3730
3731 /* Decode the type that this subroutine returns */
3732 if (die->type)
3733 {
3734 return;
3735 }
e7c27a73 3736 type = die_type (die, cu);
c906108c
SS
3737 ftype = lookup_function_type (type);
3738
3739 /* All functions in C++ have prototypes. */
3740 attr = dwarf_attr (die, DW_AT_prototyped);
3741 if ((attr && (DW_UNSND (attr) != 0))
3742 || cu_language == language_cplus)
3743 TYPE_FLAGS (ftype) |= TYPE_FLAG_PROTOTYPED;
3744
639d11d3 3745 if (die->child != NULL)
c906108c
SS
3746 {
3747 struct die_info *child_die;
3748 int nparams = 0;
3749 int iparams = 0;
3750
3751 /* Count the number of parameters.
3752 FIXME: GDB currently ignores vararg functions, but knows about
3753 vararg member functions. */
639d11d3 3754 child_die = die->child;
c906108c
SS
3755 while (child_die && child_die->tag)
3756 {
3757 if (child_die->tag == DW_TAG_formal_parameter)
3758 nparams++;
3759 else if (child_die->tag == DW_TAG_unspecified_parameters)
3760 TYPE_FLAGS (ftype) |= TYPE_FLAG_VARARGS;
3761 child_die = sibling_die (child_die);
3762 }
3763
3764 /* Allocate storage for parameters and fill them in. */
3765 TYPE_NFIELDS (ftype) = nparams;
3766 TYPE_FIELDS (ftype) = (struct field *)
3767 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
3768
639d11d3 3769 child_die = die->child;
c906108c
SS
3770 while (child_die && child_die->tag)
3771 {
3772 if (child_die->tag == DW_TAG_formal_parameter)
3773 {
3774 /* Dwarf2 has no clean way to discern C++ static and non-static
c5aa993b
JM
3775 member functions. G++ helps GDB by marking the first
3776 parameter for non-static member functions (which is the
3777 this pointer) as artificial. We pass this information
3778 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
c906108c
SS
3779 attr = dwarf_attr (child_die, DW_AT_artificial);
3780 if (attr)
3781 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
3782 else
3783 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
e7c27a73 3784 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
c906108c
SS
3785 iparams++;
3786 }
3787 child_die = sibling_die (child_die);
3788 }
3789 }
3790
3791 die->type = ftype;
3792}
3793
3794static void
e7c27a73 3795read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3796{
e7c27a73 3797 struct objfile *objfile = cu->objfile;
2f038fcb
FF
3798 struct attribute *attr;
3799 char *name = NULL;
c906108c
SS
3800
3801 if (!die->type)
3802 {
c906108c
SS
3803 attr = dwarf_attr (die, DW_AT_name);
3804 if (attr && DW_STRING (attr))
2f038fcb
FF
3805 {
3806 name = DW_STRING (attr);
3807 }
3808 die->type = init_type (TYPE_CODE_TYPEDEF, 0, TYPE_FLAG_TARGET_STUB, name, objfile);
e7c27a73 3809 TYPE_TARGET_TYPE (die->type) = die_type (die, cu);
c906108c
SS
3810 }
3811}
3812
3813/* Find a representation of a given base type and install
3814 it in the TYPE field of the die. */
3815
3816static void
e7c27a73 3817read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 3818{
e7c27a73 3819 struct objfile *objfile = cu->objfile;
c906108c
SS
3820 struct type *type;
3821 struct attribute *attr;
3822 int encoding = 0, size = 0;
3823
3824 /* If we've already decoded this die, this is a no-op. */
3825 if (die->type)
3826 {
3827 return;
3828 }
3829
3830 attr = dwarf_attr (die, DW_AT_encoding);
3831 if (attr)
3832 {
3833 encoding = DW_UNSND (attr);
3834 }
3835 attr = dwarf_attr (die, DW_AT_byte_size);
3836 if (attr)
3837 {
3838 size = DW_UNSND (attr);
3839 }
3840 attr = dwarf_attr (die, DW_AT_name);
3841 if (attr && DW_STRING (attr))
3842 {
3843 enum type_code code = TYPE_CODE_INT;
f5ef7c67 3844 int type_flags = 0;
c906108c
SS
3845
3846 switch (encoding)
3847 {
3848 case DW_ATE_address:
3849 /* Turn DW_ATE_address into a void * pointer. */
3850 code = TYPE_CODE_PTR;
f5ef7c67 3851 type_flags |= TYPE_FLAG_UNSIGNED;
c906108c
SS
3852 break;
3853 case DW_ATE_boolean:
3854 code = TYPE_CODE_BOOL;
f5ef7c67 3855 type_flags |= TYPE_FLAG_UNSIGNED;
c906108c
SS
3856 break;
3857 case DW_ATE_complex_float:
3858 code = TYPE_CODE_COMPLEX;
3859 break;
3860 case DW_ATE_float:
3861 code = TYPE_CODE_FLT;
3862 break;
3863 case DW_ATE_signed:
3864 case DW_ATE_signed_char:
3865 break;
3866 case DW_ATE_unsigned:
3867 case DW_ATE_unsigned_char:
f5ef7c67 3868 type_flags |= TYPE_FLAG_UNSIGNED;
c906108c
SS
3869 break;
3870 default:
4d3c2250
KB
3871 complaint (&symfile_complaints, "unsupported DW_AT_encoding: '%s'",
3872 dwarf_type_encoding_name (encoding));
c906108c
SS
3873 break;
3874 }
f5ef7c67 3875 type = init_type (code, size, type_flags, DW_STRING (attr), objfile);
c906108c
SS
3876 if (encoding == DW_ATE_address)
3877 TYPE_TARGET_TYPE (type) = dwarf2_fundamental_type (objfile, FT_VOID);
f65ca430
DJ
3878 else if (encoding == DW_ATE_complex_float)
3879 {
3880 if (size == 32)
3881 TYPE_TARGET_TYPE (type)
3882 = dwarf2_fundamental_type (objfile, FT_EXT_PREC_FLOAT);
3883 else if (size == 16)
3884 TYPE_TARGET_TYPE (type)
3885 = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT);
3886 else if (size == 8)
3887 TYPE_TARGET_TYPE (type)
3888 = dwarf2_fundamental_type (objfile, FT_FLOAT);
3889 }
c906108c
SS
3890 }
3891 else
3892 {
e7c27a73 3893 type = dwarf_base_type (encoding, size, cu);
c906108c
SS
3894 }
3895 die->type = type;
3896}
3897
3898/* Read a whole compilation unit into a linked list of dies. */
3899
f9aca02d 3900static struct die_info *
e7c27a73 3901read_comp_unit (char *info_ptr, bfd *abfd, struct dwarf2_cu *cu)
c906108c 3902{
b3810801 3903 /* Reset die reference table; we are
7f0e3f52
AC
3904 building new ones now. */
3905 dwarf2_empty_hash_tables ();
c906108c 3906
e7c27a73 3907 return read_die_and_children (info_ptr, abfd, cu, &info_ptr, NULL);
639d11d3
DC
3908}
3909
3910/* Read a single die and all its descendents. Set the die's sibling
3911 field to NULL; set other fields in the die correctly, and set all
3912 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
3913 location of the info_ptr after reading all of those dies. PARENT
3914 is the parent of the die in question. */
3915
3916static struct die_info *
3917read_die_and_children (char *info_ptr, bfd *abfd,
e7c27a73 3918 struct dwarf2_cu *cu,
639d11d3
DC
3919 char **new_info_ptr,
3920 struct die_info *parent)
3921{
3922 struct die_info *die;
3923 char *cur_ptr;
3924 int has_children;
3925
e7c27a73 3926 cur_ptr = read_full_die (&die, abfd, info_ptr, cu, &has_children);
639d11d3
DC
3927 store_in_ref_table (die->offset, die);
3928
3929 if (has_children)
3930 {
e7c27a73 3931 die->child = read_die_and_siblings (cur_ptr, abfd, cu,
639d11d3
DC
3932 new_info_ptr, die);
3933 }
3934 else
3935 {
3936 die->child = NULL;
3937 *new_info_ptr = cur_ptr;
3938 }
3939
3940 die->sibling = NULL;
3941 die->parent = parent;
3942 return die;
3943}
3944
3945/* Read a die, all of its descendents, and all of its siblings; set
3946 all of the fields of all of the dies correctly. Arguments are as
3947 in read_die_and_children. */
3948
3949static struct die_info *
3950read_die_and_siblings (char *info_ptr, bfd *abfd,
e7c27a73 3951 struct dwarf2_cu *cu,
639d11d3
DC
3952 char **new_info_ptr,
3953 struct die_info *parent)
3954{
3955 struct die_info *first_die, *last_sibling;
3956 char *cur_ptr;
3957
c906108c 3958 cur_ptr = info_ptr;
639d11d3
DC
3959 first_die = last_sibling = NULL;
3960
3961 while (1)
c906108c 3962 {
639d11d3 3963 struct die_info *die
e7c27a73 3964 = read_die_and_children (cur_ptr, abfd, cu, &cur_ptr, parent);
639d11d3
DC
3965
3966 if (!first_die)
c906108c 3967 {
639d11d3 3968 first_die = die;
c906108c 3969 }
639d11d3 3970 else
c906108c 3971 {
639d11d3 3972 last_sibling->sibling = die;
c906108c
SS
3973 }
3974
639d11d3 3975 if (die->tag == 0)
c906108c 3976 {
639d11d3
DC
3977 *new_info_ptr = cur_ptr;
3978 return first_die;
c906108c
SS
3979 }
3980 else
3981 {
639d11d3 3982 last_sibling = die;
c906108c
SS
3983 }
3984 }
c906108c
SS
3985}
3986
3987/* Free a linked list of dies. */
3988
3989static void
fba45db2 3990free_die_list (struct die_info *dies)
c906108c
SS
3991{
3992 struct die_info *die, *next;
3993
3994 die = dies;
3995 while (die)
3996 {
639d11d3
DC
3997 if (die->child != NULL)
3998 free_die_list (die->child);
3999 next = die->sibling;
b8c9b27d
KB
4000 xfree (die->attrs);
4001 xfree (die);
c906108c
SS
4002 die = next;
4003 }
4004}
4005
74b7792f
AC
4006static void
4007do_free_die_list_cleanup (void *dies)
4008{
4009 free_die_list (dies);
4010}
4011
4012static struct cleanup *
4013make_cleanup_free_die_list (struct die_info *dies)
4014{
4015 return make_cleanup (do_free_die_list_cleanup, dies);
4016}
4017
4018
c906108c
SS
4019/* Read the contents of the section at OFFSET and of size SIZE from the
4020 object file specified by OBJFILE into the psymbol_obstack and return it. */
4021
b6af0555 4022char *
188dd5d6 4023dwarf2_read_section (struct objfile *objfile, asection *sectp)
c906108c
SS
4024{
4025 bfd *abfd = objfile->obfd;
086df311 4026 char *buf, *retbuf;
188dd5d6 4027 bfd_size_type size = bfd_get_section_size_before_reloc (sectp);
c906108c
SS
4028
4029 if (size == 0)
4030 return NULL;
4031
4032 buf = (char *) obstack_alloc (&objfile->psymbol_obstack, size);
086df311
DJ
4033 retbuf
4034 = (char *) symfile_relocate_debug_section (abfd, sectp, (bfd_byte *) buf);
4035 if (retbuf != NULL)
4036 return retbuf;
4037
188dd5d6
DJ
4038 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
4039 || bfd_bread (buf, size, abfd) != size)
4040 error ("Dwarf Error: Can't read DWARF data from '%s'",
4041 bfd_get_filename (abfd));
4042
c906108c
SS
4043 return buf;
4044}
4045
4046/* In DWARF version 2, the description of the debugging information is
4047 stored in a separate .debug_abbrev section. Before we read any
4048 dies from a section we read in all abbreviations and install them
4049 in a hash table. */
4050
4051static void
e7c27a73 4052dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
c906108c 4053{
e7c27a73 4054 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
4055 char *abbrev_ptr;
4056 struct abbrev_info *cur_abbrev;
4057 unsigned int abbrev_number, bytes_read, abbrev_name;
4058 unsigned int abbrev_form, hash_number;
4059
57349743
JB
4060 /* Initialize dwarf2 abbrevs */
4061 memset (cu_header->dwarf2_abbrevs, 0,
4062 ABBREV_HASH_SIZE*sizeof (struct abbrev_info *));
c906108c 4063
57349743 4064 abbrev_ptr = dwarf_abbrev_buffer + cu_header->abbrev_offset;
c906108c
SS
4065 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4066 abbrev_ptr += bytes_read;
4067
4068 /* loop until we reach an abbrev number of 0 */
4069 while (abbrev_number)
4070 {
4071 cur_abbrev = dwarf_alloc_abbrev ();
4072
4073 /* read in abbrev header */
4074 cur_abbrev->number = abbrev_number;
4075 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4076 abbrev_ptr += bytes_read;
4077 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
4078 abbrev_ptr += 1;
4079
4080 /* now read in declarations */
4081 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4082 abbrev_ptr += bytes_read;
4083 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4084 abbrev_ptr += bytes_read;
4085 while (abbrev_name)
4086 {
4087 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
4088 {
4089 cur_abbrev->attrs = (struct attr_abbrev *)
4090 xrealloc (cur_abbrev->attrs,
4091 (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
c5aa993b 4092 * sizeof (struct attr_abbrev));
c906108c
SS
4093 }
4094 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
4095 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
4096 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4097 abbrev_ptr += bytes_read;
4098 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4099 abbrev_ptr += bytes_read;
4100 }
4101
4102 hash_number = abbrev_number % ABBREV_HASH_SIZE;
57349743
JB
4103 cur_abbrev->next = cu_header->dwarf2_abbrevs[hash_number];
4104 cu_header->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
4105
4106 /* Get next abbreviation.
4107 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
4108 always properly terminated with an abbrev number of 0.
4109 Exit loop if we encounter an abbreviation which we have
4110 already read (which means we are about to read the abbreviations
4111 for the next compile unit) or if the end of the abbreviation
4112 table is reached. */
c906108c 4113 if ((unsigned int) (abbrev_ptr - dwarf_abbrev_buffer)
c5aa993b 4114 >= dwarf_abbrev_size)
c906108c
SS
4115 break;
4116 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4117 abbrev_ptr += bytes_read;
e7c27a73 4118 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
4119 break;
4120 }
4121}
4122
4123/* Empty the abbrev table for a new compilation unit. */
4124
c906108c 4125static void
4efb68b1 4126dwarf2_empty_abbrev_table (void *ptr_to_abbrevs_table)
c906108c
SS
4127{
4128 int i;
4129 struct abbrev_info *abbrev, *next;
57349743
JB
4130 struct abbrev_info **abbrevs;
4131
4132 abbrevs = (struct abbrev_info **)ptr_to_abbrevs_table;
c906108c
SS
4133
4134 for (i = 0; i < ABBREV_HASH_SIZE; ++i)
4135 {
4136 next = NULL;
57349743 4137 abbrev = abbrevs[i];
c906108c
SS
4138 while (abbrev)
4139 {
4140 next = abbrev->next;
b8c9b27d
KB
4141 xfree (abbrev->attrs);
4142 xfree (abbrev);
c906108c
SS
4143 abbrev = next;
4144 }
57349743 4145 abbrevs[i] = NULL;
c906108c
SS
4146 }
4147}
4148
4149/* Lookup an abbrev_info structure in the abbrev hash table. */
4150
4151static struct abbrev_info *
e7c27a73 4152dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c 4153{
e7c27a73 4154 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
4155 unsigned int hash_number;
4156 struct abbrev_info *abbrev;
4157
4158 hash_number = number % ABBREV_HASH_SIZE;
57349743 4159 abbrev = cu_header->dwarf2_abbrevs[hash_number];
c906108c
SS
4160
4161 while (abbrev)
4162 {
4163 if (abbrev->number == number)
4164 return abbrev;
4165 else
4166 abbrev = abbrev->next;
4167 }
4168 return NULL;
4169}
4170
4171/* Read a minimal amount of information into the minimal die structure. */
4172
4173static char *
107d2387 4174read_partial_die (struct partial_die_info *part_die, bfd *abfd,
e7c27a73 4175 char *info_ptr, struct dwarf2_cu *cu)
c906108c
SS
4176{
4177 unsigned int abbrev_number, bytes_read, i;
4178 struct abbrev_info *abbrev;
4179 struct attribute attr;
4180 struct attribute spec_attr;
4181 int found_spec_attr = 0;
c5aa993b 4182 int has_low_pc_attr = 0;
c906108c
SS
4183 int has_high_pc_attr = 0;
4184
4185 *part_die = zeroed_partial_die;
c906108c
SS
4186 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4187 info_ptr += bytes_read;
4188 if (!abbrev_number)
4189 return info_ptr;
4190
e7c27a73 4191 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
c906108c
SS
4192 if (!abbrev)
4193 {
659b0389
ML
4194 error ("Dwarf Error: Could not find abbrev number %d [in module %s]", abbrev_number,
4195 bfd_get_filename (abfd));
c906108c
SS
4196 }
4197 part_die->offset = info_ptr - dwarf_info_buffer;
4198 part_die->tag = abbrev->tag;
4199 part_die->has_children = abbrev->has_children;
4200 part_die->abbrev = abbrev_number;
4201
4202 for (i = 0; i < abbrev->num_attrs; ++i)
4203 {
e7c27a73 4204 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
c906108c
SS
4205
4206 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 4207 partial symbol table. */
c906108c
SS
4208 switch (attr.name)
4209 {
4210 case DW_AT_name:
4211
4212 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
4213 if (part_die->name == NULL)
4214 part_die->name = DW_STRING (&attr);
4215 break;
4216 case DW_AT_MIPS_linkage_name:
4217 part_die->name = DW_STRING (&attr);
4218 break;
4219 case DW_AT_low_pc:
4220 has_low_pc_attr = 1;
4221 part_die->lowpc = DW_ADDR (&attr);
4222 break;
4223 case DW_AT_high_pc:
4224 has_high_pc_attr = 1;
4225 part_die->highpc = DW_ADDR (&attr);
4226 break;
4227 case DW_AT_location:
8e19ed76
PS
4228 /* Support the .debug_loc offsets */
4229 if (attr_form_is_block (&attr))
4230 {
4231 part_die->locdesc = DW_BLOCK (&attr);
4232 }
4233 else if (attr.form == DW_FORM_data4 || attr.form == DW_FORM_data8)
4234 {
4d3c2250 4235 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
4236 }
4237 else
4238 {
4d3c2250
KB
4239 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
4240 "partial symbol information");
8e19ed76 4241 }
c906108c
SS
4242 break;
4243 case DW_AT_language:
4244 part_die->language = DW_UNSND (&attr);
4245 break;
4246 case DW_AT_external:
4247 part_die->is_external = DW_UNSND (&attr);
4248 break;
4249 case DW_AT_declaration:
4250 part_die->is_declaration = DW_UNSND (&attr);
4251 break;
4252 case DW_AT_type:
4253 part_die->has_type = 1;
4254 break;
4255 case DW_AT_abstract_origin:
4256 case DW_AT_specification:
4257 found_spec_attr = 1;
4258 spec_attr = attr;
4259 break;
4260 case DW_AT_sibling:
4261 /* Ignore absolute siblings, they might point outside of
4262 the current compile unit. */
4263 if (attr.form == DW_FORM_ref_addr)
4d3c2250 4264 complaint (&symfile_complaints, "ignoring absolute DW_AT_sibling");
c906108c
SS
4265 else
4266 part_die->sibling =
4267 dwarf_info_buffer + dwarf2_get_ref_die_offset (&attr);
4268 break;
4269 default:
4270 break;
4271 }
4272 }
4273
4274 /* If we found a reference attribute and the die has no name, try
4275 to find a name in the referred to die. */
4276
4277 if (found_spec_attr && part_die->name == NULL)
4278 {
4279 struct partial_die_info spec_die;
4280 char *spec_ptr;
c906108c
SS
4281
4282 spec_ptr = dwarf_info_buffer + dwarf2_get_ref_die_offset (&spec_attr);
e7c27a73 4283 read_partial_die (&spec_die, abfd, spec_ptr, cu);
c906108c
SS
4284 if (spec_die.name)
4285 {
4286 part_die->name = spec_die.name;
4287
4288 /* Copy DW_AT_external attribute if it is set. */
4289 if (spec_die.is_external)
4290 part_die->is_external = spec_die.is_external;
4291 }
4292 }
4293
4294 /* When using the GNU linker, .gnu.linkonce. sections are used to
4295 eliminate duplicate copies of functions and vtables and such.
4296 The linker will arbitrarily choose one and discard the others.
4297 The AT_*_pc values for such functions refer to local labels in
4298 these sections. If the section from that file was discarded, the
4299 labels are not in the output, so the relocs get a value of 0.
4300 If this is a discarded function, mark the pc bounds as invalid,
4301 so that GDB will ignore it. */
4302 if (has_low_pc_attr && has_high_pc_attr
4303 && part_die->lowpc < part_die->highpc
4304 && (part_die->lowpc != 0
4305 || (bfd_get_file_flags (abfd) & HAS_RELOC)))
0b010bcc 4306 part_die->has_pc_info = 1;
c906108c
SS
4307 return info_ptr;
4308}
4309
639d11d3
DC
4310/* Read the die from the .debug_info section buffer. Set DIEP to
4311 point to a newly allocated die with its information, except for its
4312 child, sibling, and parent fields. Set HAS_CHILDREN to tell
4313 whether the die has children or not. */
c906108c
SS
4314
4315static char *
107d2387 4316read_full_die (struct die_info **diep, bfd *abfd, char *info_ptr,
e7c27a73 4317 struct dwarf2_cu *cu, int *has_children)
c906108c
SS
4318{
4319 unsigned int abbrev_number, bytes_read, i, offset;
4320 struct abbrev_info *abbrev;
4321 struct die_info *die;
4322
4323 offset = info_ptr - dwarf_info_buffer;
4324 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4325 info_ptr += bytes_read;
4326 if (!abbrev_number)
4327 {
4328 die = dwarf_alloc_die ();
4329 die->tag = 0;
4330 die->abbrev = abbrev_number;
4331 die->type = NULL;
4332 *diep = die;
639d11d3 4333 *has_children = 0;
c906108c
SS
4334 return info_ptr;
4335 }
4336
e7c27a73 4337 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
c906108c
SS
4338 if (!abbrev)
4339 {
639d11d3
DC
4340 error ("Dwarf Error: could not find abbrev number %d [in module %s]",
4341 abbrev_number,
4342 bfd_get_filename (abfd));
c906108c
SS
4343 }
4344 die = dwarf_alloc_die ();
4345 die->offset = offset;
4346 die->tag = abbrev->tag;
c906108c
SS
4347 die->abbrev = abbrev_number;
4348 die->type = NULL;
4349
4350 die->num_attrs = abbrev->num_attrs;
4351 die->attrs = (struct attribute *)
4352 xmalloc (die->num_attrs * sizeof (struct attribute));
4353
4354 for (i = 0; i < abbrev->num_attrs; ++i)
4355 {
4356 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
e7c27a73 4357 abfd, info_ptr, cu);
c906108c
SS
4358 }
4359
4360 *diep = die;
639d11d3 4361 *has_children = abbrev->has_children;
c906108c
SS
4362 return info_ptr;
4363}
4364
a8329558 4365/* Read an attribute value described by an attribute form. */
c906108c
SS
4366
4367static char *
a8329558 4368read_attribute_value (struct attribute *attr, unsigned form,
e7c27a73
DJ
4369 bfd *abfd, char *info_ptr,
4370 struct dwarf2_cu *cu)
c906108c 4371{
e7c27a73 4372 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
4373 unsigned int bytes_read;
4374 struct dwarf_block *blk;
4375
a8329558
KW
4376 attr->form = form;
4377 switch (form)
c906108c
SS
4378 {
4379 case DW_FORM_addr:
4380 case DW_FORM_ref_addr:
e7c27a73 4381 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 4382 info_ptr += bytes_read;
c906108c
SS
4383 break;
4384 case DW_FORM_block2:
4385 blk = dwarf_alloc_block ();
4386 blk->size = read_2_bytes (abfd, info_ptr);
4387 info_ptr += 2;
4388 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4389 info_ptr += blk->size;
4390 DW_BLOCK (attr) = blk;
4391 break;
4392 case DW_FORM_block4:
4393 blk = dwarf_alloc_block ();
4394 blk->size = read_4_bytes (abfd, info_ptr);
4395 info_ptr += 4;
4396 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4397 info_ptr += blk->size;
4398 DW_BLOCK (attr) = blk;
4399 break;
4400 case DW_FORM_data2:
4401 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
4402 info_ptr += 2;
4403 break;
4404 case DW_FORM_data4:
4405 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
4406 info_ptr += 4;
4407 break;
4408 case DW_FORM_data8:
4409 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
4410 info_ptr += 8;
4411 break;
4412 case DW_FORM_string:
4413 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
4414 info_ptr += bytes_read;
4415 break;
4bdf3d34
JJ
4416 case DW_FORM_strp:
4417 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
4418 &bytes_read);
4419 info_ptr += bytes_read;
4420 break;
c906108c
SS
4421 case DW_FORM_block:
4422 blk = dwarf_alloc_block ();
4423 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4424 info_ptr += bytes_read;
4425 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4426 info_ptr += blk->size;
4427 DW_BLOCK (attr) = blk;
4428 break;
4429 case DW_FORM_block1:
4430 blk = dwarf_alloc_block ();
4431 blk->size = read_1_byte (abfd, info_ptr);
4432 info_ptr += 1;
4433 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4434 info_ptr += blk->size;
4435 DW_BLOCK (attr) = blk;
4436 break;
4437 case DW_FORM_data1:
4438 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
4439 info_ptr += 1;
4440 break;
4441 case DW_FORM_flag:
4442 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
4443 info_ptr += 1;
4444 break;
4445 case DW_FORM_sdata:
4446 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
4447 info_ptr += bytes_read;
4448 break;
4449 case DW_FORM_udata:
4450 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4451 info_ptr += bytes_read;
4452 break;
4453 case DW_FORM_ref1:
4454 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
4455 info_ptr += 1;
4456 break;
4457 case DW_FORM_ref2:
4458 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
4459 info_ptr += 2;
4460 break;
4461 case DW_FORM_ref4:
4462 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
4463 info_ptr += 4;
4464 break;
613e1657
KB
4465 case DW_FORM_ref8:
4466 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
4467 info_ptr += 8;
4468 break;
c906108c
SS
4469 case DW_FORM_ref_udata:
4470 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4471 info_ptr += bytes_read;
4472 break;
c906108c 4473 case DW_FORM_indirect:
a8329558
KW
4474 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4475 info_ptr += bytes_read;
e7c27a73 4476 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
a8329558 4477 break;
c906108c 4478 default:
659b0389
ML
4479 error ("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]",
4480 dwarf_form_name (form),
4481 bfd_get_filename (abfd));
c906108c
SS
4482 }
4483 return info_ptr;
4484}
4485
a8329558
KW
4486/* Read an attribute described by an abbreviated attribute. */
4487
4488static char *
4489read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
e7c27a73 4490 bfd *abfd, char *info_ptr, struct dwarf2_cu *cu)
a8329558
KW
4491{
4492 attr->name = abbrev->name;
e7c27a73 4493 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
a8329558
KW
4494}
4495
c906108c
SS
4496/* read dwarf information from a buffer */
4497
4498static unsigned int
fba45db2 4499read_1_byte (bfd *abfd, char *buf)
c906108c
SS
4500{
4501 return bfd_get_8 (abfd, (bfd_byte *) buf);
4502}
4503
4504static int
fba45db2 4505read_1_signed_byte (bfd *abfd, char *buf)
c906108c
SS
4506{
4507 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
4508}
4509
4510static unsigned int
fba45db2 4511read_2_bytes (bfd *abfd, char *buf)
c906108c
SS
4512{
4513 return bfd_get_16 (abfd, (bfd_byte *) buf);
4514}
4515
4516static int
fba45db2 4517read_2_signed_bytes (bfd *abfd, char *buf)
c906108c
SS
4518{
4519 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
4520}
4521
4522static unsigned int
fba45db2 4523read_4_bytes (bfd *abfd, char *buf)
c906108c
SS
4524{
4525 return bfd_get_32 (abfd, (bfd_byte *) buf);
4526}
4527
4528static int
fba45db2 4529read_4_signed_bytes (bfd *abfd, char *buf)
c906108c
SS
4530{
4531 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
4532}
4533
ce5d95e1 4534static unsigned long
fba45db2 4535read_8_bytes (bfd *abfd, char *buf)
c906108c
SS
4536{
4537 return bfd_get_64 (abfd, (bfd_byte *) buf);
4538}
4539
4540static CORE_ADDR
e7c27a73 4541read_address (bfd *abfd, char *buf, struct dwarf2_cu *cu, int *bytes_read)
c906108c 4542{
e7c27a73 4543 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
4544 CORE_ADDR retval = 0;
4545
107d2387 4546 if (cu_header->signed_addr_p)
c906108c 4547 {
107d2387
AC
4548 switch (cu_header->addr_size)
4549 {
4550 case 2:
4551 retval = bfd_get_signed_16 (abfd, (bfd_byte *) buf);
4552 break;
4553 case 4:
4554 retval = bfd_get_signed_32 (abfd, (bfd_byte *) buf);
4555 break;
4556 case 8:
4557 retval = bfd_get_signed_64 (abfd, (bfd_byte *) buf);
4558 break;
4559 default:
8e65ff28 4560 internal_error (__FILE__, __LINE__,
659b0389
ML
4561 "read_address: bad switch, signed [in module %s]",
4562 bfd_get_filename (abfd));
107d2387
AC
4563 }
4564 }
4565 else
4566 {
4567 switch (cu_header->addr_size)
4568 {
4569 case 2:
4570 retval = bfd_get_16 (abfd, (bfd_byte *) buf);
4571 break;
4572 case 4:
4573 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
4574 break;
4575 case 8:
4576 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
4577 break;
4578 default:
8e65ff28 4579 internal_error (__FILE__, __LINE__,
659b0389
ML
4580 "read_address: bad switch, unsigned [in module %s]",
4581 bfd_get_filename (abfd));
107d2387 4582 }
c906108c 4583 }
64367e0a 4584
107d2387
AC
4585 *bytes_read = cu_header->addr_size;
4586 return retval;
c906108c
SS
4587}
4588
f7ef9339 4589/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
4590 specification allows the initial length to take up either 4 bytes
4591 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
4592 bytes describe the length and all offsets will be 8 bytes in length
4593 instead of 4.
4594
f7ef9339
KB
4595 An older, non-standard 64-bit format is also handled by this
4596 function. The older format in question stores the initial length
4597 as an 8-byte quantity without an escape value. Lengths greater
4598 than 2^32 aren't very common which means that the initial 4 bytes
4599 is almost always zero. Since a length value of zero doesn't make
4600 sense for the 32-bit format, this initial zero can be considered to
4601 be an escape value which indicates the presence of the older 64-bit
4602 format. As written, the code can't detect (old format) lengths
4603 greater than 4GB. If it becomes necessary to handle lengths somewhat
4604 larger than 4GB, we could allow other small values (such as the
4605 non-sensical values of 1, 2, and 3) to also be used as escape values
4606 indicating the presence of the old format.
4607
613e1657
KB
4608 The value returned via bytes_read should be used to increment
4609 the relevant pointer after calling read_initial_length().
4610
4611 As a side effect, this function sets the fields initial_length_size
4612 and offset_size in cu_header to the values appropriate for the
4613 length field. (The format of the initial length field determines
4614 the width of file offsets to be fetched later with fetch_offset().)
4615
4616 [ Note: read_initial_length() and read_offset() are based on the
4617 document entitled "DWARF Debugging Information Format", revision
f7ef9339 4618 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
4619 from:
4620
f7ef9339 4621 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
613e1657
KB
4622
4623 This document is only a draft and is subject to change. (So beware.)
4624
f7ef9339
KB
4625 Details regarding the older, non-standard 64-bit format were
4626 determined empirically by examining 64-bit ELF files produced
4627 by the SGI toolchain on an IRIX 6.5 machine.
4628
4629 - Kevin, July 16, 2002
613e1657
KB
4630 ] */
4631
4632static LONGEST
4633read_initial_length (bfd *abfd, char *buf, struct comp_unit_head *cu_header,
4634 int *bytes_read)
4635{
4636 LONGEST retval = 0;
4637
4638 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
4639
4640 if (retval == 0xffffffff)
4641 {
4642 retval = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
4643 *bytes_read = 12;
4644 if (cu_header != NULL)
4645 {
4646 cu_header->initial_length_size = 12;
4647 cu_header->offset_size = 8;
4648 }
4649 }
f7ef9339
KB
4650 else if (retval == 0)
4651 {
4652 /* Handle (non-standard) 64-bit DWARF2 formats such as that used
4653 by IRIX. */
4654 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
4655 *bytes_read = 8;
4656 if (cu_header != NULL)
4657 {
4658 cu_header->initial_length_size = 8;
4659 cu_header->offset_size = 8;
4660 }
4661 }
613e1657
KB
4662 else
4663 {
4664 *bytes_read = 4;
4665 if (cu_header != NULL)
4666 {
4667 cu_header->initial_length_size = 4;
4668 cu_header->offset_size = 4;
4669 }
4670 }
4671
4672 return retval;
4673}
4674
4675/* Read an offset from the data stream. The size of the offset is
4676 given by cu_header->offset_size. */
4677
4678static LONGEST
4679read_offset (bfd *abfd, char *buf, const struct comp_unit_head *cu_header,
4680 int *bytes_read)
4681{
4682 LONGEST retval = 0;
4683
4684 switch (cu_header->offset_size)
4685 {
4686 case 4:
4687 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
4688 *bytes_read = 4;
4689 break;
4690 case 8:
4691 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
4692 *bytes_read = 8;
4693 break;
4694 default:
8e65ff28 4695 internal_error (__FILE__, __LINE__,
659b0389
ML
4696 "read_offset: bad switch [in module %s]",
4697 bfd_get_filename (abfd));
613e1657
KB
4698 }
4699
4700 return retval;
4701}
4702
c906108c 4703static char *
fba45db2 4704read_n_bytes (bfd *abfd, char *buf, unsigned int size)
c906108c
SS
4705{
4706 /* If the size of a host char is 8 bits, we can return a pointer
4707 to the buffer, otherwise we have to copy the data to a buffer
4708 allocated on the temporary obstack. */
4bdf3d34 4709 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 4710 return buf;
c906108c
SS
4711}
4712
4713static char *
fba45db2 4714read_string (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
c906108c
SS
4715{
4716 /* If the size of a host char is 8 bits, we can return a pointer
4717 to the string, otherwise we have to copy the string to a buffer
4718 allocated on the temporary obstack. */
4bdf3d34 4719 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
4720 if (*buf == '\0')
4721 {
4722 *bytes_read_ptr = 1;
4723 return NULL;
4724 }
4725 *bytes_read_ptr = strlen (buf) + 1;
4726 return buf;
4bdf3d34
JJ
4727}
4728
4729static char *
4730read_indirect_string (bfd *abfd, char *buf,
4731 const struct comp_unit_head *cu_header,
4732 unsigned int *bytes_read_ptr)
4733{
4734 LONGEST str_offset = read_offset (abfd, buf, cu_header,
4735 (int *) bytes_read_ptr);
c906108c 4736
4bdf3d34 4737 if (dwarf_str_buffer == NULL)
c906108c 4738 {
659b0389
ML
4739 error ("DW_FORM_strp used without .debug_str section [in module %s]",
4740 bfd_get_filename (abfd));
4bdf3d34 4741 return NULL;
c906108c 4742 }
4bdf3d34 4743 if (str_offset >= dwarf_str_size)
c906108c 4744 {
659b0389
ML
4745 error ("DW_FORM_strp pointing outside of .debug_str section [in module %s]",
4746 bfd_get_filename (abfd));
c906108c
SS
4747 return NULL;
4748 }
4bdf3d34
JJ
4749 gdb_assert (HOST_CHAR_BIT == 8);
4750 if (dwarf_str_buffer[str_offset] == '\0')
4751 return NULL;
4752 return dwarf_str_buffer + str_offset;
c906108c
SS
4753}
4754
ce5d95e1 4755static unsigned long
fba45db2 4756read_unsigned_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
c906108c 4757{
ce5d95e1
JB
4758 unsigned long result;
4759 unsigned int num_read;
c906108c
SS
4760 int i, shift;
4761 unsigned char byte;
4762
4763 result = 0;
4764 shift = 0;
4765 num_read = 0;
4766 i = 0;
4767 while (1)
4768 {
4769 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
4770 buf++;
4771 num_read++;
ce5d95e1 4772 result |= ((unsigned long)(byte & 127) << shift);
c906108c
SS
4773 if ((byte & 128) == 0)
4774 {
4775 break;
4776 }
4777 shift += 7;
4778 }
4779 *bytes_read_ptr = num_read;
4780 return result;
4781}
4782
ce5d95e1 4783static long
fba45db2 4784read_signed_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
c906108c 4785{
ce5d95e1 4786 long result;
c906108c
SS
4787 int i, shift, size, num_read;
4788 unsigned char byte;
4789
4790 result = 0;
4791 shift = 0;
4792 size = 32;
4793 num_read = 0;
4794 i = 0;
4795 while (1)
4796 {
4797 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
4798 buf++;
4799 num_read++;
ce5d95e1 4800 result |= ((long)(byte & 127) << shift);
c906108c
SS
4801 shift += 7;
4802 if ((byte & 128) == 0)
4803 {
4804 break;
4805 }
4806 }
4807 if ((shift < size) && (byte & 0x40))
4808 {
4809 result |= -(1 << shift);
4810 }
4811 *bytes_read_ptr = num_read;
4812 return result;
4813}
4814
4815static void
fba45db2 4816set_cu_language (unsigned int lang)
c906108c
SS
4817{
4818 switch (lang)
4819 {
4820 case DW_LANG_C89:
4821 case DW_LANG_C:
4822 cu_language = language_c;
4823 break;
4824 case DW_LANG_C_plus_plus:
4825 cu_language = language_cplus;
4826 break;
4827 case DW_LANG_Fortran77:
4828 case DW_LANG_Fortran90:
b21b22e0 4829 case DW_LANG_Fortran95:
c906108c
SS
4830 cu_language = language_fortran;
4831 break;
4832 case DW_LANG_Mips_Assembler:
4833 cu_language = language_asm;
4834 break;
bebd888e
PB
4835 case DW_LANG_Java:
4836 cu_language = language_java;
4837 break;
c906108c 4838 case DW_LANG_Ada83:
8aaf0b47 4839 case DW_LANG_Ada95:
c906108c
SS
4840 case DW_LANG_Cobol74:
4841 case DW_LANG_Cobol85:
4842 case DW_LANG_Pascal83:
4843 case DW_LANG_Modula2:
4844 default:
5d62c8b1 4845 cu_language = language_minimal;
c906108c
SS
4846 break;
4847 }
4848 cu_language_defn = language_def (cu_language);
4849}
4850
4851/* Return the named attribute or NULL if not there. */
4852
4853static struct attribute *
fba45db2 4854dwarf_attr (struct die_info *die, unsigned int name)
c906108c
SS
4855{
4856 unsigned int i;
4857 struct attribute *spec = NULL;
4858
4859 for (i = 0; i < die->num_attrs; ++i)
4860 {
4861 if (die->attrs[i].name == name)
4862 {
4863 return &die->attrs[i];
4864 }
4865 if (die->attrs[i].name == DW_AT_specification
4866 || die->attrs[i].name == DW_AT_abstract_origin)
4867 spec = &die->attrs[i];
4868 }
4869 if (spec)
4870 {
4871 struct die_info *ref_die =
c5aa993b 4872 follow_die_ref (dwarf2_get_ref_die_offset (spec));
c906108c
SS
4873
4874 if (ref_die)
4875 return dwarf_attr (ref_die, name);
4876 }
c5aa993b 4877
c906108c
SS
4878 return NULL;
4879}
4880
3ca72b44
AC
4881static int
4882die_is_declaration (struct die_info *die)
4883{
4884 return (dwarf_attr (die, DW_AT_declaration)
4885 && ! dwarf_attr (die, DW_AT_specification));
4886}
4887
63d06c5c
DC
4888/* Return the die giving the specification for DIE, if there is
4889 one. */
4890
4891static struct die_info *
4892die_specification (struct die_info *die)
4893{
4894 struct attribute *spec_attr = dwarf_attr (die, DW_AT_specification);
4895
4896 if (spec_attr == NULL)
4897 return NULL;
4898 else
4899 return follow_die_ref (dwarf2_get_ref_die_offset (spec_attr));
4900}
c906108c 4901
debd256d
JB
4902/* Free the line_header structure *LH, and any arrays and strings it
4903 refers to. */
4904static void
4905free_line_header (struct line_header *lh)
4906{
4907 if (lh->standard_opcode_lengths)
a8bc7b56 4908 xfree (lh->standard_opcode_lengths);
debd256d
JB
4909
4910 /* Remember that all the lh->file_names[i].name pointers are
4911 pointers into debug_line_buffer, and don't need to be freed. */
4912 if (lh->file_names)
a8bc7b56 4913 xfree (lh->file_names);
debd256d
JB
4914
4915 /* Similarly for the include directory names. */
4916 if (lh->include_dirs)
a8bc7b56 4917 xfree (lh->include_dirs);
debd256d 4918
a8bc7b56 4919 xfree (lh);
debd256d
JB
4920}
4921
4922
4923/* Add an entry to LH's include directory table. */
4924static void
4925add_include_dir (struct line_header *lh, char *include_dir)
c906108c 4926{
debd256d
JB
4927 /* Grow the array if necessary. */
4928 if (lh->include_dirs_size == 0)
c5aa993b 4929 {
debd256d
JB
4930 lh->include_dirs_size = 1; /* for testing */
4931 lh->include_dirs = xmalloc (lh->include_dirs_size
4932 * sizeof (*lh->include_dirs));
4933 }
4934 else if (lh->num_include_dirs >= lh->include_dirs_size)
4935 {
4936 lh->include_dirs_size *= 2;
4937 lh->include_dirs = xrealloc (lh->include_dirs,
4938 (lh->include_dirs_size
4939 * sizeof (*lh->include_dirs)));
c5aa993b 4940 }
c906108c 4941
debd256d
JB
4942 lh->include_dirs[lh->num_include_dirs++] = include_dir;
4943}
4944
4945
4946/* Add an entry to LH's file name table. */
4947static void
4948add_file_name (struct line_header *lh,
4949 char *name,
4950 unsigned int dir_index,
4951 unsigned int mod_time,
4952 unsigned int length)
4953{
4954 struct file_entry *fe;
4955
4956 /* Grow the array if necessary. */
4957 if (lh->file_names_size == 0)
4958 {
4959 lh->file_names_size = 1; /* for testing */
4960 lh->file_names = xmalloc (lh->file_names_size
4961 * sizeof (*lh->file_names));
4962 }
4963 else if (lh->num_file_names >= lh->file_names_size)
4964 {
4965 lh->file_names_size *= 2;
4966 lh->file_names = xrealloc (lh->file_names,
4967 (lh->file_names_size
4968 * sizeof (*lh->file_names)));
4969 }
4970
4971 fe = &lh->file_names[lh->num_file_names++];
4972 fe->name = name;
4973 fe->dir_index = dir_index;
4974 fe->mod_time = mod_time;
4975 fe->length = length;
4976}
4977
4978
4979/* Read the statement program header starting at OFFSET in
4980 dwarf_line_buffer, according to the endianness of ABFD. Return a
4981 pointer to a struct line_header, allocated using xmalloc.
4982
4983 NOTE: the strings in the include directory and file name tables of
4984 the returned object point into debug_line_buffer, and must not be
4985 freed. */
4986static struct line_header *
4987dwarf_decode_line_header (unsigned int offset, bfd *abfd,
e7c27a73 4988 struct dwarf2_cu *cu)
debd256d
JB
4989{
4990 struct cleanup *back_to;
4991 struct line_header *lh;
4992 char *line_ptr;
4993 int bytes_read;
4994 int i;
4995 char *cur_dir, *cur_file;
4996
4997 if (dwarf_line_buffer == NULL)
4998 {
4d3c2250 4999 complaint (&symfile_complaints, "missing .debug_line section");
debd256d
JB
5000 return 0;
5001 }
5002
5003 /* Make sure that at least there's room for the total_length field. That
5004 could be 12 bytes long, but we're just going to fudge that. */
5005 if (offset + 4 >= dwarf_line_size)
5006 {
4d3c2250 5007 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
5008 return 0;
5009 }
5010
5011 lh = xmalloc (sizeof (*lh));
5012 memset (lh, 0, sizeof (*lh));
5013 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
5014 (void *) lh);
5015
5016 line_ptr = dwarf_line_buffer + offset;
5017
5018 /* read in the header */
5019 lh->total_length = read_initial_length (abfd, line_ptr, NULL, &bytes_read);
5020 line_ptr += bytes_read;
5021 if (line_ptr + lh->total_length > dwarf_line_buffer + dwarf_line_size)
5022 {
4d3c2250 5023 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
5024 return 0;
5025 }
5026 lh->statement_program_end = line_ptr + lh->total_length;
5027 lh->version = read_2_bytes (abfd, line_ptr);
5028 line_ptr += 2;
e7c27a73 5029 lh->header_length = read_offset (abfd, line_ptr, &cu->header, &bytes_read);
debd256d
JB
5030 line_ptr += bytes_read;
5031 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
5032 line_ptr += 1;
5033 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
5034 line_ptr += 1;
5035 lh->line_base = read_1_signed_byte (abfd, line_ptr);
5036 line_ptr += 1;
5037 lh->line_range = read_1_byte (abfd, line_ptr);
5038 line_ptr += 1;
5039 lh->opcode_base = read_1_byte (abfd, line_ptr);
5040 line_ptr += 1;
5041 lh->standard_opcode_lengths
5042 = (unsigned char *) xmalloc (lh->opcode_base * sizeof (unsigned char));
5043
5044 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
5045 for (i = 1; i < lh->opcode_base; ++i)
5046 {
5047 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
5048 line_ptr += 1;
5049 }
5050
5051 /* Read directory table */
5052 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
5053 {
5054 line_ptr += bytes_read;
5055 add_include_dir (lh, cur_dir);
5056 }
5057 line_ptr += bytes_read;
5058
5059 /* Read file name table */
5060 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
5061 {
5062 unsigned int dir_index, mod_time, length;
5063
5064 line_ptr += bytes_read;
5065 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5066 line_ptr += bytes_read;
5067 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5068 line_ptr += bytes_read;
5069 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5070 line_ptr += bytes_read;
5071
5072 add_file_name (lh, cur_file, dir_index, mod_time, length);
5073 }
5074 line_ptr += bytes_read;
5075 lh->statement_program_start = line_ptr;
5076
5077 if (line_ptr > dwarf_line_buffer + dwarf_line_size)
4d3c2250
KB
5078 complaint (&symfile_complaints,
5079 "line number info header doesn't fit in `.debug_line' section");
debd256d
JB
5080
5081 discard_cleanups (back_to);
5082 return lh;
5083}
c906108c 5084
5fb290d7
DJ
5085/* This function exists to work around a bug in certain compilers
5086 (particularly GCC 2.95), in which the first line number marker of a
5087 function does not show up until after the prologue, right before
5088 the second line number marker. This function shifts ADDRESS down
5089 to the beginning of the function if necessary, and is called on
5090 addresses passed to record_line. */
5091
5092static CORE_ADDR
5093check_cu_functions (CORE_ADDR address)
5094{
5095 struct function_range *fn;
5096
5097 /* Find the function_range containing address. */
5098 if (!cu_first_fn)
5099 return address;
5100
5101 if (!cu_cached_fn)
5102 cu_cached_fn = cu_first_fn;
5103
5104 fn = cu_cached_fn;
5105 while (fn)
5106 if (fn->lowpc <= address && fn->highpc > address)
5107 goto found;
5108 else
5109 fn = fn->next;
5110
5111 fn = cu_first_fn;
5112 while (fn && fn != cu_cached_fn)
5113 if (fn->lowpc <= address && fn->highpc > address)
5114 goto found;
5115 else
5116 fn = fn->next;
5117
5118 return address;
5119
5120 found:
5121 if (fn->seen_line)
5122 return address;
5123 if (address != fn->lowpc)
4d3c2250
KB
5124 complaint (&symfile_complaints,
5125 "misplaced first line number at 0x%lx for '%s'",
5126 (unsigned long) address, fn->name);
5fb290d7
DJ
5127 fn->seen_line = 1;
5128 return fn->lowpc;
5129}
5130
debd256d
JB
5131/* Decode the line number information for the compilation unit whose
5132 line number info is at OFFSET in the .debug_line section.
5133 The compilation directory of the file is passed in COMP_DIR. */
5134
c906108c 5135static void
debd256d 5136dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
e7c27a73 5137 struct dwarf2_cu *cu)
c906108c
SS
5138{
5139 char *line_ptr;
5140 char *line_end;
e7c27a73 5141 unsigned int bytes_read;
c906108c
SS
5142 unsigned char op_code, extended_op, adj_opcode;
5143
debd256d
JB
5144 line_ptr = lh->statement_program_start;
5145 line_end = lh->statement_program_end;
c906108c
SS
5146
5147 /* Read the statement sequences until there's nothing left. */
5148 while (line_ptr < line_end)
5149 {
5150 /* state machine registers */
5151 CORE_ADDR address = 0;
5152 unsigned int file = 1;
5153 unsigned int line = 1;
5154 unsigned int column = 0;
debd256d 5155 int is_stmt = lh->default_is_stmt;
c906108c
SS
5156 int basic_block = 0;
5157 int end_sequence = 0;
5158
5159 /* Start a subfile for the current file of the state machine. */
debd256d 5160 if (lh->num_file_names >= file)
c906108c 5161 {
debd256d
JB
5162 /* lh->include_dirs and lh->file_names are 0-based, but the
5163 directory and file name numbers in the statement program
5164 are 1-based. */
5165 struct file_entry *fe = &lh->file_names[file - 1];
5166 char *dir;
5167 if (fe->dir_index)
5168 dir = lh->include_dirs[fe->dir_index - 1];
5169 else
5170 dir = comp_dir;
5171 dwarf2_start_subfile (fe->name, dir);
c906108c
SS
5172 }
5173
5174 /* Decode the table. */
c5aa993b 5175 while (!end_sequence)
c906108c
SS
5176 {
5177 op_code = read_1_byte (abfd, line_ptr);
5178 line_ptr += 1;
9aa1fe7e 5179
debd256d 5180 if (op_code >= lh->opcode_base)
9aa1fe7e 5181 { /* Special operand. */
debd256d
JB
5182 adj_opcode = op_code - lh->opcode_base;
5183 address += (adj_opcode / lh->line_range)
5184 * lh->minimum_instruction_length;
5185 line += lh->line_base + (adj_opcode % lh->line_range);
9aa1fe7e 5186 /* append row to matrix using current values */
ddf9f258
JJ
5187 record_line (current_subfile, line,
5188 check_cu_functions (address));
9aa1fe7e
GK
5189 basic_block = 1;
5190 }
5191 else switch (op_code)
c906108c
SS
5192 {
5193 case DW_LNS_extended_op:
5194 line_ptr += 1; /* ignore length */
5195 extended_op = read_1_byte (abfd, line_ptr);
5196 line_ptr += 1;
5197 switch (extended_op)
5198 {
5199 case DW_LNE_end_sequence:
5200 end_sequence = 1;
5fb290d7 5201 record_line (current_subfile, 0, address);
c906108c
SS
5202 break;
5203 case DW_LNE_set_address:
e7c27a73 5204 address = read_address (abfd, line_ptr, cu, &bytes_read);
107d2387
AC
5205 line_ptr += bytes_read;
5206 address += baseaddr;
c906108c
SS
5207 break;
5208 case DW_LNE_define_file:
debd256d
JB
5209 {
5210 char *cur_file;
5211 unsigned int dir_index, mod_time, length;
5212
5213 cur_file = read_string (abfd, line_ptr, &bytes_read);
5214 line_ptr += bytes_read;
5215 dir_index =
5216 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5217 line_ptr += bytes_read;
5218 mod_time =
5219 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5220 line_ptr += bytes_read;
5221 length =
5222 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5223 line_ptr += bytes_read;
5224 add_file_name (lh, cur_file, dir_index, mod_time, length);
5225 }
c906108c
SS
5226 break;
5227 default:
4d3c2250
KB
5228 complaint (&symfile_complaints,
5229 "mangled .debug_line section");
debd256d 5230 return;
c906108c
SS
5231 }
5232 break;
5233 case DW_LNS_copy:
ddf9f258
JJ
5234 record_line (current_subfile, line,
5235 check_cu_functions (address));
c906108c
SS
5236 basic_block = 0;
5237 break;
5238 case DW_LNS_advance_pc:
debd256d 5239 address += lh->minimum_instruction_length
c906108c
SS
5240 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5241 line_ptr += bytes_read;
5242 break;
5243 case DW_LNS_advance_line:
5244 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
5245 line_ptr += bytes_read;
5246 break;
5247 case DW_LNS_set_file:
debd256d
JB
5248 {
5249 /* lh->include_dirs and lh->file_names are 0-based,
5250 but the directory and file name numbers in the
5251 statement program are 1-based. */
5252 struct file_entry *fe;
5253 char *dir;
5254 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5255 line_ptr += bytes_read;
5256 fe = &lh->file_names[file - 1];
5257 if (fe->dir_index)
5258 dir = lh->include_dirs[fe->dir_index - 1];
5259 else
5260 dir = comp_dir;
5261 dwarf2_start_subfile (fe->name, dir);
5262 }
c906108c
SS
5263 break;
5264 case DW_LNS_set_column:
5265 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5266 line_ptr += bytes_read;
5267 break;
5268 case DW_LNS_negate_stmt:
5269 is_stmt = (!is_stmt);
5270 break;
5271 case DW_LNS_set_basic_block:
5272 basic_block = 1;
5273 break;
c2c6d25f
JM
5274 /* Add to the address register of the state machine the
5275 address increment value corresponding to special opcode
5276 255. Ie, this value is scaled by the minimum instruction
5277 length since special opcode 255 would have scaled the
5278 the increment. */
c906108c 5279 case DW_LNS_const_add_pc:
debd256d
JB
5280 address += (lh->minimum_instruction_length
5281 * ((255 - lh->opcode_base) / lh->line_range));
c906108c
SS
5282 break;
5283 case DW_LNS_fixed_advance_pc:
5284 address += read_2_bytes (abfd, line_ptr);
5285 line_ptr += 2;
5286 break;
9aa1fe7e
GK
5287 default:
5288 { /* Unknown standard opcode, ignore it. */
5289 int i;
debd256d 5290 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
5291 {
5292 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5293 line_ptr += bytes_read;
5294 }
5295 }
c906108c
SS
5296 }
5297 }
5298 }
c906108c
SS
5299}
5300
5301/* Start a subfile for DWARF. FILENAME is the name of the file and
5302 DIRNAME the name of the source directory which contains FILENAME
5303 or NULL if not known.
5304 This routine tries to keep line numbers from identical absolute and
5305 relative file names in a common subfile.
5306
5307 Using the `list' example from the GDB testsuite, which resides in
5308 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
5309 of /srcdir/list0.c yields the following debugging information for list0.c:
5310
c5aa993b
JM
5311 DW_AT_name: /srcdir/list0.c
5312 DW_AT_comp_dir: /compdir
357e46e7 5313 files.files[0].name: list0.h
c5aa993b 5314 files.files[0].dir: /srcdir
357e46e7 5315 files.files[1].name: list0.c
c5aa993b 5316 files.files[1].dir: /srcdir
c906108c
SS
5317
5318 The line number information for list0.c has to end up in a single
5319 subfile, so that `break /srcdir/list0.c:1' works as expected. */
5320
5321static void
fba45db2 5322dwarf2_start_subfile (char *filename, char *dirname)
c906108c
SS
5323{
5324 /* If the filename isn't absolute, try to match an existing subfile
5325 with the full pathname. */
5326
d5166ae1 5327 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
c906108c
SS
5328 {
5329 struct subfile *subfile;
5330 char *fullname = concat (dirname, "/", filename, NULL);
5331
5332 for (subfile = subfiles; subfile; subfile = subfile->next)
5333 {
d5166ae1 5334 if (FILENAME_CMP (subfile->name, fullname) == 0)
c906108c
SS
5335 {
5336 current_subfile = subfile;
b8c9b27d 5337 xfree (fullname);
c906108c
SS
5338 return;
5339 }
5340 }
b8c9b27d 5341 xfree (fullname);
c906108c
SS
5342 }
5343 start_subfile (filename, dirname);
5344}
5345
4c2df51b
DJ
5346static void
5347var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 5348 struct dwarf2_cu *cu)
4c2df51b 5349{
e7c27a73
DJ
5350 struct objfile *objfile = cu->objfile;
5351 struct comp_unit_head *cu_header = &cu->header;
5352
4c2df51b
DJ
5353 /* NOTE drow/2003-01-30: There used to be a comment and some special
5354 code here to turn a symbol with DW_AT_external and a
5355 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
5356 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
5357 with some versions of binutils) where shared libraries could have
5358 relocations against symbols in their debug information - the
5359 minimal symbol would have the right address, but the debug info
5360 would not. It's no longer necessary, because we will explicitly
5361 apply relocations when we read in the debug information now. */
5362
5363 /* A DW_AT_location attribute with no contents indicates that a
5364 variable has been optimized away. */
5365 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
5366 {
5367 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
5368 return;
5369 }
5370
5371 /* Handle one degenerate form of location expression specially, to
5372 preserve GDB's previous behavior when section offsets are
5373 specified. If this is just a DW_OP_addr then mark this symbol
5374 as LOC_STATIC. */
5375
5376 if (attr_form_is_block (attr)
5377 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
5378 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
5379 {
5380 int dummy;
5381
5382 SYMBOL_VALUE_ADDRESS (sym) =
e7c27a73 5383 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
4c2df51b
DJ
5384 fixup_symbol_section (sym, objfile);
5385 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
5386 SYMBOL_SECTION (sym));
5387 SYMBOL_CLASS (sym) = LOC_STATIC;
5388 return;
5389 }
5390
5391 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
5392 expression evaluator, and use LOC_COMPUTED only when necessary
5393 (i.e. when the value of a register or memory location is
5394 referenced, or a thread-local block, etc.). Then again, it might
5395 not be worthwhile. I'm assuming that it isn't unless performance
5396 or memory numbers show me otherwise. */
5397
e7c27a73 5398 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b
DJ
5399 SYMBOL_CLASS (sym) = LOC_COMPUTED;
5400}
5401
c906108c
SS
5402/* Given a pointer to a DWARF information entry, figure out if we need
5403 to make a symbol table entry for it, and if so, create a new entry
5404 and return a pointer to it.
5405 If TYPE is NULL, determine symbol type from the die, otherwise
2df3850c 5406 used the passed type. */
c906108c
SS
5407
5408static struct symbol *
e7c27a73 5409new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
c906108c 5410{
e7c27a73 5411 struct objfile *objfile = cu->objfile;
c906108c
SS
5412 struct symbol *sym = NULL;
5413 char *name;
5414 struct attribute *attr = NULL;
5415 struct attribute *attr2 = NULL;
c906108c 5416
5c4e30ca
DC
5417 if (die->tag != DW_TAG_namespace)
5418 name = dwarf2_linkage_name (die);
5419 else
5420 name = TYPE_NAME (type);
5421
c906108c
SS
5422 if (name)
5423 {
5424 sym = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
5425 sizeof (struct symbol));
5426 OBJSTAT (objfile, n_syms++);
5427 memset (sym, 0, sizeof (struct symbol));
2de7ced7
DJ
5428
5429 /* Cache this symbol's name and the name's demangled form (if any). */
5430 SYMBOL_LANGUAGE (sym) = cu_language;
5431 SYMBOL_SET_NAMES (sym, name, strlen (name), objfile);
c906108c
SS
5432
5433 /* Default assumptions.
c5aa993b 5434 Use the passed type or decode it from the die. */
176620f1 5435 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
c906108c
SS
5436 SYMBOL_CLASS (sym) = LOC_STATIC;
5437 if (type != NULL)
5438 SYMBOL_TYPE (sym) = type;
5439 else
e7c27a73 5440 SYMBOL_TYPE (sym) = die_type (die, cu);
c906108c
SS
5441 attr = dwarf_attr (die, DW_AT_decl_line);
5442 if (attr)
5443 {
5444 SYMBOL_LINE (sym) = DW_UNSND (attr);
5445 }
c906108c
SS
5446 switch (die->tag)
5447 {
5448 case DW_TAG_label:
5449 attr = dwarf_attr (die, DW_AT_low_pc);
5450 if (attr)
5451 {
5452 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
5453 }
5454 SYMBOL_CLASS (sym) = LOC_LABEL;
5455 break;
5456 case DW_TAG_subprogram:
5457 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
5458 finish_block. */
5459 SYMBOL_CLASS (sym) = LOC_BLOCK;
5460 attr2 = dwarf_attr (die, DW_AT_external);
5461 if (attr2 && (DW_UNSND (attr2) != 0))
5462 {
5463 add_symbol_to_list (sym, &global_symbols);
5464 }
5465 else
5466 {
5467 add_symbol_to_list (sym, list_in_scope);
5468 }
5469 break;
5470 case DW_TAG_variable:
5471 /* Compilation with minimal debug info may result in variables
5472 with missing type entries. Change the misleading `void' type
5473 to something sensible. */
5474 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
5475 SYMBOL_TYPE (sym) = init_type (TYPE_CODE_INT,
5476 TARGET_INT_BIT / HOST_CHAR_BIT, 0,
5477 "<variable, no debug info>",
5478 objfile);
5479 attr = dwarf_attr (die, DW_AT_const_value);
5480 if (attr)
5481 {
e7c27a73 5482 dwarf2_const_value (attr, sym, cu);
c906108c
SS
5483 attr2 = dwarf_attr (die, DW_AT_external);
5484 if (attr2 && (DW_UNSND (attr2) != 0))
5485 add_symbol_to_list (sym, &global_symbols);
5486 else
5487 add_symbol_to_list (sym, list_in_scope);
5488 break;
5489 }
5490 attr = dwarf_attr (die, DW_AT_location);
5491 if (attr)
5492 {
e7c27a73 5493 var_decode_location (attr, sym, cu);
c906108c
SS
5494 attr2 = dwarf_attr (die, DW_AT_external);
5495 if (attr2 && (DW_UNSND (attr2) != 0))
4c2df51b 5496 add_symbol_to_list (sym, &global_symbols);
c906108c 5497 else
4c2df51b 5498 add_symbol_to_list (sym, list_in_scope);
c906108c
SS
5499 }
5500 else
5501 {
5502 /* We do not know the address of this symbol.
c5aa993b
JM
5503 If it is an external symbol and we have type information
5504 for it, enter the symbol as a LOC_UNRESOLVED symbol.
5505 The address of the variable will then be determined from
5506 the minimal symbol table whenever the variable is
5507 referenced. */
c906108c
SS
5508 attr2 = dwarf_attr (die, DW_AT_external);
5509 if (attr2 && (DW_UNSND (attr2) != 0)
5510 && dwarf_attr (die, DW_AT_type) != NULL)
5511 {
5512 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
5513 add_symbol_to_list (sym, &global_symbols);
5514 }
5515 }
5516 break;
5517 case DW_TAG_formal_parameter:
5518 attr = dwarf_attr (die, DW_AT_location);
5519 if (attr)
5520 {
e7c27a73 5521 var_decode_location (attr, sym, cu);
7cf6e574
DJ
5522 /* FIXME drow/2003-07-31: Is LOC_COMPUTED_ARG necessary? */
5523 if (SYMBOL_CLASS (sym) == LOC_COMPUTED)
5524 SYMBOL_CLASS (sym) = LOC_COMPUTED_ARG;
c906108c
SS
5525 }
5526 attr = dwarf_attr (die, DW_AT_const_value);
5527 if (attr)
5528 {
e7c27a73 5529 dwarf2_const_value (attr, sym, cu);
c906108c
SS
5530 }
5531 add_symbol_to_list (sym, list_in_scope);
5532 break;
5533 case DW_TAG_unspecified_parameters:
5534 /* From varargs functions; gdb doesn't seem to have any
5535 interest in this information, so just ignore it for now.
5536 (FIXME?) */
5537 break;
5538 case DW_TAG_class_type:
5539 case DW_TAG_structure_type:
5540 case DW_TAG_union_type:
5541 case DW_TAG_enumeration_type:
5542 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 5543 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 5544
63d06c5c
DC
5545 /* Make sure that the symbol includes appropriate enclosing
5546 classes/namespaces in its name. These are calculated in
5547 read_structure_scope, and the correct name is saved in
5548 the type. */
5549
c906108c
SS
5550 if (cu_language == language_cplus)
5551 {
63d06c5c
DC
5552 struct type *type = SYMBOL_TYPE (sym);
5553
5554 if (TYPE_TAG_NAME (type) != NULL)
5555 {
5556 /* FIXME: carlton/2003-11-10: Should this use
5557 SYMBOL_SET_NAMES instead? (The same problem also
5558 arises a further down in the function.) */
5559 SYMBOL_LINKAGE_NAME (sym)
5560 = obsavestring (TYPE_TAG_NAME (type),
5561 strlen (TYPE_TAG_NAME (type)),
5562 &objfile->symbol_obstack);
5563 }
c906108c 5564 }
63d06c5c
DC
5565
5566 {
5567 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
5568 really ever be static objects: otherwise, if you try
5569 to, say, break of a class's method and you're in a file
5570 which doesn't mention that class, it won't work unless
5571 the check for all static symbols in lookup_symbol_aux
5572 saves you. See the OtherFileClass tests in
5573 gdb.c++/namespace.exp. */
5574
5575 struct pending **list_to_add;
5576
5577 list_to_add = (list_in_scope == &file_symbols
5578 && cu_language == language_cplus
5579 ? &global_symbols : list_in_scope);
5580
5581 add_symbol_to_list (sym, list_to_add);
5582
5583 /* The semantics of C++ state that "struct foo { ... }" also
5584 defines a typedef for "foo". Synthesize a typedef symbol so
5585 that "ptype foo" works as expected. */
5586 if (cu_language == language_cplus)
5587 {
5588 struct symbol *typedef_sym = (struct symbol *)
5589 obstack_alloc (&objfile->symbol_obstack,
5590 sizeof (struct symbol));
5591 *typedef_sym = *sym;
5592 SYMBOL_DOMAIN (typedef_sym) = VAR_DOMAIN;
5593 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
5594 TYPE_NAME (SYMBOL_TYPE (sym)) =
5595 obsavestring (SYMBOL_NATURAL_NAME (sym),
5596 strlen (SYMBOL_NATURAL_NAME (sym)),
5597 &objfile->type_obstack);
5598 add_symbol_to_list (typedef_sym, list_to_add);
5599 }
5600 }
c906108c
SS
5601 break;
5602 case DW_TAG_typedef:
63d06c5c
DC
5603 if (processing_has_namespace_info
5604 && processing_current_prefix[0] != '\0')
5605 {
5606 SYMBOL_LINKAGE_NAME (sym) = obconcat (&objfile->symbol_obstack,
5607 processing_current_prefix,
5608 "::",
5609 name);
5610 }
5611 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5612 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
5613 add_symbol_to_list (sym, list_in_scope);
5614 break;
c906108c
SS
5615 case DW_TAG_base_type:
5616 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 5617 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
c906108c
SS
5618 add_symbol_to_list (sym, list_in_scope);
5619 break;
5620 case DW_TAG_enumerator:
63d06c5c
DC
5621 if (processing_has_namespace_info
5622 && processing_current_prefix[0] != '\0')
5623 {
5624 SYMBOL_LINKAGE_NAME (sym) = obconcat (&objfile->symbol_obstack,
5625 processing_current_prefix,
5626 "::",
5627 name);
5628 }
c906108c
SS
5629 attr = dwarf_attr (die, DW_AT_const_value);
5630 if (attr)
5631 {
e7c27a73 5632 dwarf2_const_value (attr, sym, cu);
c906108c 5633 }
63d06c5c
DC
5634 {
5635 /* NOTE: carlton/2003-11-10: See comment above in the
5636 DW_TAG_class_type, etc. block. */
5637
5638 struct pending **list_to_add;
5639
5640 list_to_add = (list_in_scope == &file_symbols
5641 && cu_language == language_cplus
5642 ? &global_symbols : list_in_scope);
5643
5644 add_symbol_to_list (sym, list_to_add);
5645 }
c906108c 5646 break;
5c4e30ca
DC
5647 case DW_TAG_namespace:
5648 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5649 add_symbol_to_list (sym, &global_symbols);
5650 break;
c906108c
SS
5651 default:
5652 /* Not a tag we recognize. Hopefully we aren't processing
5653 trash data, but since we must specifically ignore things
5654 we don't recognize, there is nothing else we should do at
5655 this point. */
4d3c2250
KB
5656 complaint (&symfile_complaints, "unsupported tag: '%s'",
5657 dwarf_tag_name (die->tag));
c906108c
SS
5658 break;
5659 }
5660 }
5661 return (sym);
5662}
5663
5664/* Copy constant value from an attribute to a symbol. */
5665
5666static void
107d2387 5667dwarf2_const_value (struct attribute *attr, struct symbol *sym,
e7c27a73 5668 struct dwarf2_cu *cu)
c906108c 5669{
e7c27a73
DJ
5670 struct objfile *objfile = cu->objfile;
5671 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
5672 struct dwarf_block *blk;
5673
5674 switch (attr->form)
5675 {
5676 case DW_FORM_addr:
107d2387 5677 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
22abf04a 5678 dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
4d3c2250
KB
5679 cu_header->addr_size,
5680 TYPE_LENGTH (SYMBOL_TYPE
5681 (sym)));
c906108c 5682 SYMBOL_VALUE_BYTES (sym) = (char *)
107d2387 5683 obstack_alloc (&objfile->symbol_obstack, cu_header->addr_size);
fbd9dcd3
AC
5684 /* NOTE: cagney/2003-05-09: In-lined store_address call with
5685 it's body - store_unsigned_integer. */
5686 store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
5687 DW_ADDR (attr));
c906108c
SS
5688 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
5689 break;
5690 case DW_FORM_block1:
5691 case DW_FORM_block2:
5692 case DW_FORM_block4:
5693 case DW_FORM_block:
5694 blk = DW_BLOCK (attr);
5695 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
22abf04a 5696 dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
4d3c2250
KB
5697 blk->size,
5698 TYPE_LENGTH (SYMBOL_TYPE
5699 (sym)));
c906108c
SS
5700 SYMBOL_VALUE_BYTES (sym) = (char *)
5701 obstack_alloc (&objfile->symbol_obstack, blk->size);
5702 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
5703 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
5704 break;
2df3850c
JM
5705
5706 /* The DW_AT_const_value attributes are supposed to carry the
5707 symbol's value "represented as it would be on the target
5708 architecture." By the time we get here, it's already been
5709 converted to host endianness, so we just need to sign- or
5710 zero-extend it as appropriate. */
5711 case DW_FORM_data1:
5712 dwarf2_const_value_data (attr, sym, 8);
5713 break;
c906108c 5714 case DW_FORM_data2:
2df3850c
JM
5715 dwarf2_const_value_data (attr, sym, 16);
5716 break;
c906108c 5717 case DW_FORM_data4:
2df3850c
JM
5718 dwarf2_const_value_data (attr, sym, 32);
5719 break;
c906108c 5720 case DW_FORM_data8:
2df3850c
JM
5721 dwarf2_const_value_data (attr, sym, 64);
5722 break;
5723
c906108c 5724 case DW_FORM_sdata:
2df3850c
JM
5725 SYMBOL_VALUE (sym) = DW_SND (attr);
5726 SYMBOL_CLASS (sym) = LOC_CONST;
5727 break;
5728
c906108c
SS
5729 case DW_FORM_udata:
5730 SYMBOL_VALUE (sym) = DW_UNSND (attr);
5731 SYMBOL_CLASS (sym) = LOC_CONST;
5732 break;
2df3850c 5733
c906108c 5734 default:
4d3c2250
KB
5735 complaint (&symfile_complaints,
5736 "unsupported const value attribute form: '%s'",
5737 dwarf_form_name (attr->form));
c906108c
SS
5738 SYMBOL_VALUE (sym) = 0;
5739 SYMBOL_CLASS (sym) = LOC_CONST;
5740 break;
5741 }
5742}
5743
2df3850c
JM
5744
5745/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
5746 or zero-extend it as appropriate for the symbol's type. */
5747static void
5748dwarf2_const_value_data (struct attribute *attr,
5749 struct symbol *sym,
5750 int bits)
5751{
5752 LONGEST l = DW_UNSND (attr);
5753
5754 if (bits < sizeof (l) * 8)
5755 {
5756 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
5757 l &= ((LONGEST) 1 << bits) - 1;
5758 else
bf9198f1 5759 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
2df3850c
JM
5760 }
5761
5762 SYMBOL_VALUE (sym) = l;
5763 SYMBOL_CLASS (sym) = LOC_CONST;
5764}
5765
5766
c906108c
SS
5767/* Return the type of the die in question using its DW_AT_type attribute. */
5768
5769static struct type *
e7c27a73 5770die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5771{
5772 struct type *type;
5773 struct attribute *type_attr;
5774 struct die_info *type_die;
5775 unsigned int ref;
5776
5777 type_attr = dwarf_attr (die, DW_AT_type);
5778 if (!type_attr)
5779 {
5780 /* A missing DW_AT_type represents a void type. */
e7c27a73 5781 return dwarf2_fundamental_type (cu->objfile, FT_VOID);
c906108c
SS
5782 }
5783 else
5784 {
5785 ref = dwarf2_get_ref_die_offset (type_attr);
5786 type_die = follow_die_ref (ref);
5787 if (!type_die)
5788 {
659b0389 5789 error ("Dwarf Error: Cannot find referent at offset %d [in module %s]",
e7c27a73 5790 ref, cu->objfile->name);
c906108c
SS
5791 return NULL;
5792 }
5793 }
e7c27a73 5794 type = tag_type_to_type (type_die, cu);
c906108c
SS
5795 if (!type)
5796 {
5797 dump_die (type_die);
659b0389 5798 error ("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]",
e7c27a73 5799 cu->objfile->name);
c906108c
SS
5800 }
5801 return type;
5802}
5803
5804/* Return the containing type of the die in question using its
5805 DW_AT_containing_type attribute. */
5806
5807static struct type *
e7c27a73 5808die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5809{
5810 struct type *type = NULL;
5811 struct attribute *type_attr;
5812 struct die_info *type_die = NULL;
5813 unsigned int ref;
5814
5815 type_attr = dwarf_attr (die, DW_AT_containing_type);
5816 if (type_attr)
5817 {
5818 ref = dwarf2_get_ref_die_offset (type_attr);
5819 type_die = follow_die_ref (ref);
5820 if (!type_die)
5821 {
659b0389 5822 error ("Dwarf Error: Cannot find referent at offset %d [in module %s]", ref,
e7c27a73 5823 cu->objfile->name);
c906108c
SS
5824 return NULL;
5825 }
e7c27a73 5826 type = tag_type_to_type (type_die, cu);
c906108c
SS
5827 }
5828 if (!type)
5829 {
5830 if (type_die)
5831 dump_die (type_die);
659b0389 5832 error ("Dwarf Error: Problem turning containing type into gdb type [in module %s]",
e7c27a73 5833 cu->objfile->name);
c906108c
SS
5834 }
5835 return type;
5836}
5837
5838#if 0
5839static struct type *
e7c27a73 5840type_at_offset (unsigned int offset, struct dwarf2_cu *cu)
c906108c
SS
5841{
5842 struct die_info *die;
5843 struct type *type;
5844
5845 die = follow_die_ref (offset);
5846 if (!die)
5847 {
5848 error ("Dwarf Error: Cannot find type referent at offset %d.", offset);
5849 return NULL;
5850 }
e7c27a73 5851 type = tag_type_to_type (die, cu);
c906108c
SS
5852 return type;
5853}
5854#endif
5855
5856static struct type *
e7c27a73 5857tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5858{
5859 if (die->type)
5860 {
5861 return die->type;
5862 }
5863 else
5864 {
e7c27a73 5865 read_type_die (die, cu);
c906108c
SS
5866 if (!die->type)
5867 {
5868 dump_die (die);
659b0389 5869 error ("Dwarf Error: Cannot find type of die [in module %s]",
e7c27a73 5870 cu->objfile->name);
c906108c
SS
5871 }
5872 return die->type;
5873 }
5874}
5875
5876static void
e7c27a73 5877read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 5878{
63d06c5c
DC
5879 char *prefix = determine_prefix (die);
5880 const char *old_prefix = processing_current_prefix;
5881 struct cleanup *back_to = make_cleanup (xfree, prefix);
5882 processing_current_prefix = prefix;
5883
c906108c
SS
5884 switch (die->tag)
5885 {
5886 case DW_TAG_class_type:
5887 case DW_TAG_structure_type:
5888 case DW_TAG_union_type:
e7c27a73 5889 read_structure_scope (die, cu);
c906108c
SS
5890 break;
5891 case DW_TAG_enumeration_type:
e7c27a73 5892 read_enumeration (die, cu);
c906108c
SS
5893 break;
5894 case DW_TAG_subprogram:
5895 case DW_TAG_subroutine_type:
e7c27a73 5896 read_subroutine_type (die, cu);
c906108c
SS
5897 break;
5898 case DW_TAG_array_type:
e7c27a73 5899 read_array_type (die, cu);
c906108c
SS
5900 break;
5901 case DW_TAG_pointer_type:
e7c27a73 5902 read_tag_pointer_type (die, cu);
c906108c
SS
5903 break;
5904 case DW_TAG_ptr_to_member_type:
e7c27a73 5905 read_tag_ptr_to_member_type (die, cu);
c906108c
SS
5906 break;
5907 case DW_TAG_reference_type:
e7c27a73 5908 read_tag_reference_type (die, cu);
c906108c
SS
5909 break;
5910 case DW_TAG_const_type:
e7c27a73 5911 read_tag_const_type (die, cu);
c906108c
SS
5912 break;
5913 case DW_TAG_volatile_type:
e7c27a73 5914 read_tag_volatile_type (die, cu);
c906108c
SS
5915 break;
5916 case DW_TAG_string_type:
e7c27a73 5917 read_tag_string_type (die, cu);
c906108c
SS
5918 break;
5919 case DW_TAG_typedef:
e7c27a73 5920 read_typedef (die, cu);
c906108c
SS
5921 break;
5922 case DW_TAG_base_type:
e7c27a73 5923 read_base_type (die, cu);
c906108c
SS
5924 break;
5925 default:
4d3c2250
KB
5926 complaint (&symfile_complaints, "unexepected tag in read_type_die: '%s'",
5927 dwarf_tag_name (die->tag));
c906108c
SS
5928 break;
5929 }
63d06c5c
DC
5930
5931 processing_current_prefix = old_prefix;
5932 do_cleanups (back_to);
5933}
5934
5935/* Return the name of the namespace/class that DIE is defined
5936 within, or NULL if we can't tell. The caller should xfree the
5937 result. */
5938
5939static char *
5940determine_prefix (struct die_info *die)
5941{
5942 struct die_info *parent;
5943
5944 if (cu_language != language_cplus)
5945 return NULL;
5946
5947 parent = die->parent;
5948
5949 if (parent == NULL)
5950 {
5951 return (processing_has_namespace_info ? xstrdup ("") : NULL);
5952 }
5953 else
5954 {
5955 char *parent_prefix = determine_prefix (parent);
5956 char *retval;
5957
5958 switch (parent->tag) {
5959 case DW_TAG_namespace:
5960 {
5961 int dummy;
5962
5963 retval = typename_concat (parent_prefix,
5964 namespace_name (parent, &dummy));
5965 }
5966 break;
5967 case DW_TAG_class_type:
5968 case DW_TAG_structure_type:
5969 {
5970 if (parent_prefix != NULL)
5971 {
5972 const char *parent_name = dwarf2_name (parent);
5973
5974 if (parent_name != NULL)
5975 retval = typename_concat (parent_prefix, dwarf2_name (parent));
5976 else
5977 /* FIXME: carlton/2003-11-10: I'm not sure what the
5978 best thing to do here is. */
5979 retval = typename_concat (parent_prefix,
5980 "<<anonymous class>>");
5981 }
5982 else
5983 retval = class_name (parent);
5984 }
5985 break;
5986 default:
5987 retval = parent_prefix;
5988 break;
5989 }
5990
5991 if (retval != parent_prefix)
5992 xfree (parent_prefix);
5993 return retval;
5994 }
5995}
5996
5997/* Return a newly-allocated string formed by concatenating PREFIX,
5998 "::", and SUFFIX, except that if PREFIX is NULL or the empty
5999 string, just return a copy of SUFFIX. */
6000
6001static char *
6002typename_concat (const char *prefix, const char *suffix)
6003{
6004 if (prefix == NULL || prefix[0] == '\0')
6005 return xstrdup (suffix);
6006 else
6007 {
6008 char *retval = xmalloc (strlen (prefix) + 2 + strlen (suffix) + 1);
6009
6010 strcpy (retval, prefix);
6011 strcat (retval, "::");
6012 strcat (retval, suffix);
6013
6014 return retval;
6015 }
6016}
6017
6018/* Return a newly-allocated string giving the name of the class given
6019 by DIE. */
6020
6021static char *
6022class_name (struct die_info *die)
6023{
6024 struct die_info *child;
6025 const char *name;
6026
6027 for (child = die->child; child != NULL; child = sibling_die (child))
6028 {
6029 if (child->tag == DW_TAG_subprogram)
6030 return class_name_from_physname (dwarf2_linkage_name (child));
6031 }
6032
6033 name = dwarf2_name (die);
6034 if (name != NULL)
6035 return xstrdup (name);
6036 else
6037 return xstrdup ("");
c906108c
SS
6038}
6039
6040static struct type *
e7c27a73 6041dwarf_base_type (int encoding, int size, struct dwarf2_cu *cu)
c906108c 6042{
e7c27a73
DJ
6043 struct objfile *objfile = cu->objfile;
6044
c906108c
SS
6045 /* FIXME - this should not produce a new (struct type *)
6046 every time. It should cache base types. */
6047 struct type *type;
6048 switch (encoding)
6049 {
6050 case DW_ATE_address:
6051 type = dwarf2_fundamental_type (objfile, FT_VOID);
6052 return type;
6053 case DW_ATE_boolean:
6054 type = dwarf2_fundamental_type (objfile, FT_BOOLEAN);
6055 return type;
6056 case DW_ATE_complex_float:
6057 if (size == 16)
6058 {
6059 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_COMPLEX);
6060 }
6061 else
6062 {
6063 type = dwarf2_fundamental_type (objfile, FT_COMPLEX);
6064 }
6065 return type;
6066 case DW_ATE_float:
6067 if (size == 8)
6068 {
6069 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT);
6070 }
6071 else
6072 {
6073 type = dwarf2_fundamental_type (objfile, FT_FLOAT);
6074 }
6075 return type;
6076 case DW_ATE_signed:
6077 switch (size)
6078 {
6079 case 1:
6080 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
6081 break;
6082 case 2:
6083 type = dwarf2_fundamental_type (objfile, FT_SIGNED_SHORT);
6084 break;
6085 default:
6086 case 4:
6087 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
6088 break;
6089 }
6090 return type;
6091 case DW_ATE_signed_char:
6092 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
6093 return type;
6094 case DW_ATE_unsigned:
6095 switch (size)
6096 {
6097 case 1:
6098 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
6099 break;
6100 case 2:
6101 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_SHORT);
6102 break;
6103 default:
6104 case 4:
6105 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_INTEGER);
6106 break;
6107 }
6108 return type;
6109 case DW_ATE_unsigned_char:
6110 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
6111 return type;
6112 default:
6113 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
6114 return type;
6115 }
6116}
6117
6118#if 0
6119struct die_info *
fba45db2 6120copy_die (struct die_info *old_die)
c906108c
SS
6121{
6122 struct die_info *new_die;
6123 int i, num_attrs;
6124
6125 new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
6126 memset (new_die, 0, sizeof (struct die_info));
6127
6128 new_die->tag = old_die->tag;
6129 new_die->has_children = old_die->has_children;
6130 new_die->abbrev = old_die->abbrev;
6131 new_die->offset = old_die->offset;
6132 new_die->type = NULL;
6133
6134 num_attrs = old_die->num_attrs;
6135 new_die->num_attrs = num_attrs;
6136 new_die->attrs = (struct attribute *)
6137 xmalloc (num_attrs * sizeof (struct attribute));
6138
6139 for (i = 0; i < old_die->num_attrs; ++i)
6140 {
6141 new_die->attrs[i].name = old_die->attrs[i].name;
6142 new_die->attrs[i].form = old_die->attrs[i].form;
6143 new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
6144 }
6145
6146 new_die->next = NULL;
6147 return new_die;
6148}
6149#endif
6150
6151/* Return sibling of die, NULL if no sibling. */
6152
f9aca02d 6153static struct die_info *
fba45db2 6154sibling_die (struct die_info *die)
c906108c 6155{
639d11d3 6156 return die->sibling;
c906108c
SS
6157}
6158
6159/* Get linkage name of a die, return NULL if not found. */
6160
6161static char *
fba45db2 6162dwarf2_linkage_name (struct die_info *die)
c906108c
SS
6163{
6164 struct attribute *attr;
6165
6166 attr = dwarf_attr (die, DW_AT_MIPS_linkage_name);
6167 if (attr && DW_STRING (attr))
6168 return DW_STRING (attr);
6169 attr = dwarf_attr (die, DW_AT_name);
6170 if (attr && DW_STRING (attr))
6171 return DW_STRING (attr);
6172 return NULL;
6173}
6174
9219021c
DC
6175/* Get name of a die, return NULL if not found. */
6176
6177static char *
6178dwarf2_name (struct die_info *die)
6179{
6180 struct attribute *attr;
6181
6182 attr = dwarf_attr (die, DW_AT_name);
6183 if (attr && DW_STRING (attr))
6184 return DW_STRING (attr);
6185 return NULL;
6186}
6187
6188/* Return the die that this die in an extension of, or NULL if there
6189 is none. */
6190
6191static struct die_info *
6192dwarf2_extension (struct die_info *die)
6193{
6194 struct attribute *attr;
6195 struct die_info *extension_die;
6196 unsigned int ref;
6197
6198 attr = dwarf_attr (die, DW_AT_extension);
6199 if (attr == NULL)
6200 return NULL;
6201
6202 ref = dwarf2_get_ref_die_offset (attr);
6203 extension_die = follow_die_ref (ref);
6204 if (!extension_die)
6205 {
6206 error ("Dwarf Error: Cannot find referent at offset %d.", ref);
6207 }
6208
6209 return extension_die;
6210}
6211
c906108c
SS
6212/* Convert a DIE tag into its string name. */
6213
6214static char *
aa1ee363 6215dwarf_tag_name (unsigned tag)
c906108c
SS
6216{
6217 switch (tag)
6218 {
6219 case DW_TAG_padding:
6220 return "DW_TAG_padding";
6221 case DW_TAG_array_type:
6222 return "DW_TAG_array_type";
6223 case DW_TAG_class_type:
6224 return "DW_TAG_class_type";
6225 case DW_TAG_entry_point:
6226 return "DW_TAG_entry_point";
6227 case DW_TAG_enumeration_type:
6228 return "DW_TAG_enumeration_type";
6229 case DW_TAG_formal_parameter:
6230 return "DW_TAG_formal_parameter";
6231 case DW_TAG_imported_declaration:
6232 return "DW_TAG_imported_declaration";
6233 case DW_TAG_label:
6234 return "DW_TAG_label";
6235 case DW_TAG_lexical_block:
6236 return "DW_TAG_lexical_block";
6237 case DW_TAG_member:
6238 return "DW_TAG_member";
6239 case DW_TAG_pointer_type:
6240 return "DW_TAG_pointer_type";
6241 case DW_TAG_reference_type:
6242 return "DW_TAG_reference_type";
6243 case DW_TAG_compile_unit:
6244 return "DW_TAG_compile_unit";
6245 case DW_TAG_string_type:
6246 return "DW_TAG_string_type";
6247 case DW_TAG_structure_type:
6248 return "DW_TAG_structure_type";
6249 case DW_TAG_subroutine_type:
6250 return "DW_TAG_subroutine_type";
6251 case DW_TAG_typedef:
6252 return "DW_TAG_typedef";
6253 case DW_TAG_union_type:
6254 return "DW_TAG_union_type";
6255 case DW_TAG_unspecified_parameters:
6256 return "DW_TAG_unspecified_parameters";
6257 case DW_TAG_variant:
6258 return "DW_TAG_variant";
6259 case DW_TAG_common_block:
6260 return "DW_TAG_common_block";
6261 case DW_TAG_common_inclusion:
6262 return "DW_TAG_common_inclusion";
6263 case DW_TAG_inheritance:
6264 return "DW_TAG_inheritance";
6265 case DW_TAG_inlined_subroutine:
6266 return "DW_TAG_inlined_subroutine";
6267 case DW_TAG_module:
6268 return "DW_TAG_module";
6269 case DW_TAG_ptr_to_member_type:
6270 return "DW_TAG_ptr_to_member_type";
6271 case DW_TAG_set_type:
6272 return "DW_TAG_set_type";
6273 case DW_TAG_subrange_type:
6274 return "DW_TAG_subrange_type";
6275 case DW_TAG_with_stmt:
6276 return "DW_TAG_with_stmt";
6277 case DW_TAG_access_declaration:
6278 return "DW_TAG_access_declaration";
6279 case DW_TAG_base_type:
6280 return "DW_TAG_base_type";
6281 case DW_TAG_catch_block:
6282 return "DW_TAG_catch_block";
6283 case DW_TAG_const_type:
6284 return "DW_TAG_const_type";
6285 case DW_TAG_constant:
6286 return "DW_TAG_constant";
6287 case DW_TAG_enumerator:
6288 return "DW_TAG_enumerator";
6289 case DW_TAG_file_type:
6290 return "DW_TAG_file_type";
6291 case DW_TAG_friend:
6292 return "DW_TAG_friend";
6293 case DW_TAG_namelist:
6294 return "DW_TAG_namelist";
6295 case DW_TAG_namelist_item:
6296 return "DW_TAG_namelist_item";
6297 case DW_TAG_packed_type:
6298 return "DW_TAG_packed_type";
6299 case DW_TAG_subprogram:
6300 return "DW_TAG_subprogram";
6301 case DW_TAG_template_type_param:
6302 return "DW_TAG_template_type_param";
6303 case DW_TAG_template_value_param:
6304 return "DW_TAG_template_value_param";
6305 case DW_TAG_thrown_type:
6306 return "DW_TAG_thrown_type";
6307 case DW_TAG_try_block:
6308 return "DW_TAG_try_block";
6309 case DW_TAG_variant_part:
6310 return "DW_TAG_variant_part";
6311 case DW_TAG_variable:
6312 return "DW_TAG_variable";
6313 case DW_TAG_volatile_type:
6314 return "DW_TAG_volatile_type";
d9fa45fe
DC
6315 case DW_TAG_dwarf_procedure:
6316 return "DW_TAG_dwarf_procedure";
6317 case DW_TAG_restrict_type:
6318 return "DW_TAG_restrict_type";
6319 case DW_TAG_interface_type:
6320 return "DW_TAG_interface_type";
6321 case DW_TAG_namespace:
6322 return "DW_TAG_namespace";
6323 case DW_TAG_imported_module:
6324 return "DW_TAG_imported_module";
6325 case DW_TAG_unspecified_type:
6326 return "DW_TAG_unspecified_type";
6327 case DW_TAG_partial_unit:
6328 return "DW_TAG_partial_unit";
6329 case DW_TAG_imported_unit:
6330 return "DW_TAG_imported_unit";
c906108c
SS
6331 case DW_TAG_MIPS_loop:
6332 return "DW_TAG_MIPS_loop";
6333 case DW_TAG_format_label:
6334 return "DW_TAG_format_label";
6335 case DW_TAG_function_template:
6336 return "DW_TAG_function_template";
6337 case DW_TAG_class_template:
6338 return "DW_TAG_class_template";
6339 default:
6340 return "DW_TAG_<unknown>";
6341 }
6342}
6343
6344/* Convert a DWARF attribute code into its string name. */
6345
6346static char *
aa1ee363 6347dwarf_attr_name (unsigned attr)
c906108c
SS
6348{
6349 switch (attr)
6350 {
6351 case DW_AT_sibling:
6352 return "DW_AT_sibling";
6353 case DW_AT_location:
6354 return "DW_AT_location";
6355 case DW_AT_name:
6356 return "DW_AT_name";
6357 case DW_AT_ordering:
6358 return "DW_AT_ordering";
6359 case DW_AT_subscr_data:
6360 return "DW_AT_subscr_data";
6361 case DW_AT_byte_size:
6362 return "DW_AT_byte_size";
6363 case DW_AT_bit_offset:
6364 return "DW_AT_bit_offset";
6365 case DW_AT_bit_size:
6366 return "DW_AT_bit_size";
6367 case DW_AT_element_list:
6368 return "DW_AT_element_list";
6369 case DW_AT_stmt_list:
6370 return "DW_AT_stmt_list";
6371 case DW_AT_low_pc:
6372 return "DW_AT_low_pc";
6373 case DW_AT_high_pc:
6374 return "DW_AT_high_pc";
6375 case DW_AT_language:
6376 return "DW_AT_language";
6377 case DW_AT_member:
6378 return "DW_AT_member";
6379 case DW_AT_discr:
6380 return "DW_AT_discr";
6381 case DW_AT_discr_value:
6382 return "DW_AT_discr_value";
6383 case DW_AT_visibility:
6384 return "DW_AT_visibility";
6385 case DW_AT_import:
6386 return "DW_AT_import";
6387 case DW_AT_string_length:
6388 return "DW_AT_string_length";
6389 case DW_AT_common_reference:
6390 return "DW_AT_common_reference";
6391 case DW_AT_comp_dir:
6392 return "DW_AT_comp_dir";
6393 case DW_AT_const_value:
6394 return "DW_AT_const_value";
6395 case DW_AT_containing_type:
6396 return "DW_AT_containing_type";
6397 case DW_AT_default_value:
6398 return "DW_AT_default_value";
6399 case DW_AT_inline:
6400 return "DW_AT_inline";
6401 case DW_AT_is_optional:
6402 return "DW_AT_is_optional";
6403 case DW_AT_lower_bound:
6404 return "DW_AT_lower_bound";
6405 case DW_AT_producer:
6406 return "DW_AT_producer";
6407 case DW_AT_prototyped:
6408 return "DW_AT_prototyped";
6409 case DW_AT_return_addr:
6410 return "DW_AT_return_addr";
6411 case DW_AT_start_scope:
6412 return "DW_AT_start_scope";
6413 case DW_AT_stride_size:
6414 return "DW_AT_stride_size";
6415 case DW_AT_upper_bound:
6416 return "DW_AT_upper_bound";
6417 case DW_AT_abstract_origin:
6418 return "DW_AT_abstract_origin";
6419 case DW_AT_accessibility:
6420 return "DW_AT_accessibility";
6421 case DW_AT_address_class:
6422 return "DW_AT_address_class";
6423 case DW_AT_artificial:
6424 return "DW_AT_artificial";
6425 case DW_AT_base_types:
6426 return "DW_AT_base_types";
6427 case DW_AT_calling_convention:
6428 return "DW_AT_calling_convention";
6429 case DW_AT_count:
6430 return "DW_AT_count";
6431 case DW_AT_data_member_location:
6432 return "DW_AT_data_member_location";
6433 case DW_AT_decl_column:
6434 return "DW_AT_decl_column";
6435 case DW_AT_decl_file:
6436 return "DW_AT_decl_file";
6437 case DW_AT_decl_line:
6438 return "DW_AT_decl_line";
6439 case DW_AT_declaration:
6440 return "DW_AT_declaration";
6441 case DW_AT_discr_list:
6442 return "DW_AT_discr_list";
6443 case DW_AT_encoding:
6444 return "DW_AT_encoding";
6445 case DW_AT_external:
6446 return "DW_AT_external";
6447 case DW_AT_frame_base:
6448 return "DW_AT_frame_base";
6449 case DW_AT_friend:
6450 return "DW_AT_friend";
6451 case DW_AT_identifier_case:
6452 return "DW_AT_identifier_case";
6453 case DW_AT_macro_info:
6454 return "DW_AT_macro_info";
6455 case DW_AT_namelist_items:
6456 return "DW_AT_namelist_items";
6457 case DW_AT_priority:
6458 return "DW_AT_priority";
6459 case DW_AT_segment:
6460 return "DW_AT_segment";
6461 case DW_AT_specification:
6462 return "DW_AT_specification";
6463 case DW_AT_static_link:
6464 return "DW_AT_static_link";
6465 case DW_AT_type:
6466 return "DW_AT_type";
6467 case DW_AT_use_location:
6468 return "DW_AT_use_location";
6469 case DW_AT_variable_parameter:
6470 return "DW_AT_variable_parameter";
6471 case DW_AT_virtuality:
6472 return "DW_AT_virtuality";
6473 case DW_AT_vtable_elem_location:
6474 return "DW_AT_vtable_elem_location";
d9fa45fe
DC
6475 case DW_AT_allocated:
6476 return "DW_AT_allocated";
6477 case DW_AT_associated:
6478 return "DW_AT_associated";
6479 case DW_AT_data_location:
6480 return "DW_AT_data_location";
6481 case DW_AT_stride:
6482 return "DW_AT_stride";
6483 case DW_AT_entry_pc:
6484 return "DW_AT_entry_pc";
6485 case DW_AT_use_UTF8:
6486 return "DW_AT_use_UTF8";
6487 case DW_AT_extension:
6488 return "DW_AT_extension";
6489 case DW_AT_ranges:
6490 return "DW_AT_ranges";
6491 case DW_AT_trampoline:
6492 return "DW_AT_trampoline";
6493 case DW_AT_call_column:
6494 return "DW_AT_call_column";
6495 case DW_AT_call_file:
6496 return "DW_AT_call_file";
6497 case DW_AT_call_line:
6498 return "DW_AT_call_line";
c906108c
SS
6499#ifdef MIPS
6500 case DW_AT_MIPS_fde:
6501 return "DW_AT_MIPS_fde";
6502 case DW_AT_MIPS_loop_begin:
6503 return "DW_AT_MIPS_loop_begin";
6504 case DW_AT_MIPS_tail_loop_begin:
6505 return "DW_AT_MIPS_tail_loop_begin";
6506 case DW_AT_MIPS_epilog_begin:
6507 return "DW_AT_MIPS_epilog_begin";
6508 case DW_AT_MIPS_loop_unroll_factor:
6509 return "DW_AT_MIPS_loop_unroll_factor";
6510 case DW_AT_MIPS_software_pipeline_depth:
6511 return "DW_AT_MIPS_software_pipeline_depth";
e0a4f5a1 6512#endif
c906108c
SS
6513 case DW_AT_MIPS_linkage_name:
6514 return "DW_AT_MIPS_linkage_name";
c906108c
SS
6515
6516 case DW_AT_sf_names:
6517 return "DW_AT_sf_names";
6518 case DW_AT_src_info:
6519 return "DW_AT_src_info";
6520 case DW_AT_mac_info:
6521 return "DW_AT_mac_info";
6522 case DW_AT_src_coords:
6523 return "DW_AT_src_coords";
6524 case DW_AT_body_begin:
6525 return "DW_AT_body_begin";
6526 case DW_AT_body_end:
6527 return "DW_AT_body_end";
f5f8a009
EZ
6528 case DW_AT_GNU_vector:
6529 return "DW_AT_GNU_vector";
c906108c
SS
6530 default:
6531 return "DW_AT_<unknown>";
6532 }
6533}
6534
6535/* Convert a DWARF value form code into its string name. */
6536
6537static char *
aa1ee363 6538dwarf_form_name (unsigned form)
c906108c
SS
6539{
6540 switch (form)
6541 {
6542 case DW_FORM_addr:
6543 return "DW_FORM_addr";
6544 case DW_FORM_block2:
6545 return "DW_FORM_block2";
6546 case DW_FORM_block4:
6547 return "DW_FORM_block4";
6548 case DW_FORM_data2:
6549 return "DW_FORM_data2";
6550 case DW_FORM_data4:
6551 return "DW_FORM_data4";
6552 case DW_FORM_data8:
6553 return "DW_FORM_data8";
6554 case DW_FORM_string:
6555 return "DW_FORM_string";
6556 case DW_FORM_block:
6557 return "DW_FORM_block";
6558 case DW_FORM_block1:
6559 return "DW_FORM_block1";
6560 case DW_FORM_data1:
6561 return "DW_FORM_data1";
6562 case DW_FORM_flag:
6563 return "DW_FORM_flag";
6564 case DW_FORM_sdata:
6565 return "DW_FORM_sdata";
6566 case DW_FORM_strp:
6567 return "DW_FORM_strp";
6568 case DW_FORM_udata:
6569 return "DW_FORM_udata";
6570 case DW_FORM_ref_addr:
6571 return "DW_FORM_ref_addr";
6572 case DW_FORM_ref1:
6573 return "DW_FORM_ref1";
6574 case DW_FORM_ref2:
6575 return "DW_FORM_ref2";
6576 case DW_FORM_ref4:
6577 return "DW_FORM_ref4";
6578 case DW_FORM_ref8:
6579 return "DW_FORM_ref8";
6580 case DW_FORM_ref_udata:
6581 return "DW_FORM_ref_udata";
6582 case DW_FORM_indirect:
6583 return "DW_FORM_indirect";
6584 default:
6585 return "DW_FORM_<unknown>";
6586 }
6587}
6588
6589/* Convert a DWARF stack opcode into its string name. */
6590
6591static char *
aa1ee363 6592dwarf_stack_op_name (unsigned op)
c906108c
SS
6593{
6594 switch (op)
6595 {
6596 case DW_OP_addr:
6597 return "DW_OP_addr";
6598 case DW_OP_deref:
6599 return "DW_OP_deref";
6600 case DW_OP_const1u:
6601 return "DW_OP_const1u";
6602 case DW_OP_const1s:
6603 return "DW_OP_const1s";
6604 case DW_OP_const2u:
6605 return "DW_OP_const2u";
6606 case DW_OP_const2s:
6607 return "DW_OP_const2s";
6608 case DW_OP_const4u:
6609 return "DW_OP_const4u";
6610 case DW_OP_const4s:
6611 return "DW_OP_const4s";
6612 case DW_OP_const8u:
6613 return "DW_OP_const8u";
6614 case DW_OP_const8s:
6615 return "DW_OP_const8s";
6616 case DW_OP_constu:
6617 return "DW_OP_constu";
6618 case DW_OP_consts:
6619 return "DW_OP_consts";
6620 case DW_OP_dup:
6621 return "DW_OP_dup";
6622 case DW_OP_drop:
6623 return "DW_OP_drop";
6624 case DW_OP_over:
6625 return "DW_OP_over";
6626 case DW_OP_pick:
6627 return "DW_OP_pick";
6628 case DW_OP_swap:
6629 return "DW_OP_swap";
6630 case DW_OP_rot:
6631 return "DW_OP_rot";
6632 case DW_OP_xderef:
6633 return "DW_OP_xderef";
6634 case DW_OP_abs:
6635 return "DW_OP_abs";
6636 case DW_OP_and:
6637 return "DW_OP_and";
6638 case DW_OP_div:
6639 return "DW_OP_div";
6640 case DW_OP_minus:
6641 return "DW_OP_minus";
6642 case DW_OP_mod:
6643 return "DW_OP_mod";
6644 case DW_OP_mul:
6645 return "DW_OP_mul";
6646 case DW_OP_neg:
6647 return "DW_OP_neg";
6648 case DW_OP_not:
6649 return "DW_OP_not";
6650 case DW_OP_or:
6651 return "DW_OP_or";
6652 case DW_OP_plus:
6653 return "DW_OP_plus";
6654 case DW_OP_plus_uconst:
6655 return "DW_OP_plus_uconst";
6656 case DW_OP_shl:
6657 return "DW_OP_shl";
6658 case DW_OP_shr:
6659 return "DW_OP_shr";
6660 case DW_OP_shra:
6661 return "DW_OP_shra";
6662 case DW_OP_xor:
6663 return "DW_OP_xor";
6664 case DW_OP_bra:
6665 return "DW_OP_bra";
6666 case DW_OP_eq:
6667 return "DW_OP_eq";
6668 case DW_OP_ge:
6669 return "DW_OP_ge";
6670 case DW_OP_gt:
6671 return "DW_OP_gt";
6672 case DW_OP_le:
6673 return "DW_OP_le";
6674 case DW_OP_lt:
6675 return "DW_OP_lt";
6676 case DW_OP_ne:
6677 return "DW_OP_ne";
6678 case DW_OP_skip:
6679 return "DW_OP_skip";
6680 case DW_OP_lit0:
6681 return "DW_OP_lit0";
6682 case DW_OP_lit1:
6683 return "DW_OP_lit1";
6684 case DW_OP_lit2:
6685 return "DW_OP_lit2";
6686 case DW_OP_lit3:
6687 return "DW_OP_lit3";
6688 case DW_OP_lit4:
6689 return "DW_OP_lit4";
6690 case DW_OP_lit5:
6691 return "DW_OP_lit5";
6692 case DW_OP_lit6:
6693 return "DW_OP_lit6";
6694 case DW_OP_lit7:
6695 return "DW_OP_lit7";
6696 case DW_OP_lit8:
6697 return "DW_OP_lit8";
6698 case DW_OP_lit9:
6699 return "DW_OP_lit9";
6700 case DW_OP_lit10:
6701 return "DW_OP_lit10";
6702 case DW_OP_lit11:
6703 return "DW_OP_lit11";
6704 case DW_OP_lit12:
6705 return "DW_OP_lit12";
6706 case DW_OP_lit13:
6707 return "DW_OP_lit13";
6708 case DW_OP_lit14:
6709 return "DW_OP_lit14";
6710 case DW_OP_lit15:
6711 return "DW_OP_lit15";
6712 case DW_OP_lit16:
6713 return "DW_OP_lit16";
6714 case DW_OP_lit17:
6715 return "DW_OP_lit17";
6716 case DW_OP_lit18:
6717 return "DW_OP_lit18";
6718 case DW_OP_lit19:
6719 return "DW_OP_lit19";
6720 case DW_OP_lit20:
6721 return "DW_OP_lit20";
6722 case DW_OP_lit21:
6723 return "DW_OP_lit21";
6724 case DW_OP_lit22:
6725 return "DW_OP_lit22";
6726 case DW_OP_lit23:
6727 return "DW_OP_lit23";
6728 case DW_OP_lit24:
6729 return "DW_OP_lit24";
6730 case DW_OP_lit25:
6731 return "DW_OP_lit25";
6732 case DW_OP_lit26:
6733 return "DW_OP_lit26";
6734 case DW_OP_lit27:
6735 return "DW_OP_lit27";
6736 case DW_OP_lit28:
6737 return "DW_OP_lit28";
6738 case DW_OP_lit29:
6739 return "DW_OP_lit29";
6740 case DW_OP_lit30:
6741 return "DW_OP_lit30";
6742 case DW_OP_lit31:
6743 return "DW_OP_lit31";
6744 case DW_OP_reg0:
6745 return "DW_OP_reg0";
6746 case DW_OP_reg1:
6747 return "DW_OP_reg1";
6748 case DW_OP_reg2:
6749 return "DW_OP_reg2";
6750 case DW_OP_reg3:
6751 return "DW_OP_reg3";
6752 case DW_OP_reg4:
6753 return "DW_OP_reg4";
6754 case DW_OP_reg5:
6755 return "DW_OP_reg5";
6756 case DW_OP_reg6:
6757 return "DW_OP_reg6";
6758 case DW_OP_reg7:
6759 return "DW_OP_reg7";
6760 case DW_OP_reg8:
6761 return "DW_OP_reg8";
6762 case DW_OP_reg9:
6763 return "DW_OP_reg9";
6764 case DW_OP_reg10:
6765 return "DW_OP_reg10";
6766 case DW_OP_reg11:
6767 return "DW_OP_reg11";
6768 case DW_OP_reg12:
6769 return "DW_OP_reg12";
6770 case DW_OP_reg13:
6771 return "DW_OP_reg13";
6772 case DW_OP_reg14:
6773 return "DW_OP_reg14";
6774 case DW_OP_reg15:
6775 return "DW_OP_reg15";
6776 case DW_OP_reg16:
6777 return "DW_OP_reg16";
6778 case DW_OP_reg17:
6779 return "DW_OP_reg17";
6780 case DW_OP_reg18:
6781 return "DW_OP_reg18";
6782 case DW_OP_reg19:
6783 return "DW_OP_reg19";
6784 case DW_OP_reg20:
6785 return "DW_OP_reg20";
6786 case DW_OP_reg21:
6787 return "DW_OP_reg21";
6788 case DW_OP_reg22:
6789 return "DW_OP_reg22";
6790 case DW_OP_reg23:
6791 return "DW_OP_reg23";
6792 case DW_OP_reg24:
6793 return "DW_OP_reg24";
6794 case DW_OP_reg25:
6795 return "DW_OP_reg25";
6796 case DW_OP_reg26:
6797 return "DW_OP_reg26";
6798 case DW_OP_reg27:
6799 return "DW_OP_reg27";
6800 case DW_OP_reg28:
6801 return "DW_OP_reg28";
6802 case DW_OP_reg29:
6803 return "DW_OP_reg29";
6804 case DW_OP_reg30:
6805 return "DW_OP_reg30";
6806 case DW_OP_reg31:
6807 return "DW_OP_reg31";
6808 case DW_OP_breg0:
6809 return "DW_OP_breg0";
6810 case DW_OP_breg1:
6811 return "DW_OP_breg1";
6812 case DW_OP_breg2:
6813 return "DW_OP_breg2";
6814 case DW_OP_breg3:
6815 return "DW_OP_breg3";
6816 case DW_OP_breg4:
6817 return "DW_OP_breg4";
6818 case DW_OP_breg5:
6819 return "DW_OP_breg5";
6820 case DW_OP_breg6:
6821 return "DW_OP_breg6";
6822 case DW_OP_breg7:
6823 return "DW_OP_breg7";
6824 case DW_OP_breg8:
6825 return "DW_OP_breg8";
6826 case DW_OP_breg9:
6827 return "DW_OP_breg9";
6828 case DW_OP_breg10:
6829 return "DW_OP_breg10";
6830 case DW_OP_breg11:
6831 return "DW_OP_breg11";
6832 case DW_OP_breg12:
6833 return "DW_OP_breg12";
6834 case DW_OP_breg13:
6835 return "DW_OP_breg13";
6836 case DW_OP_breg14:
6837 return "DW_OP_breg14";
6838 case DW_OP_breg15:
6839 return "DW_OP_breg15";
6840 case DW_OP_breg16:
6841 return "DW_OP_breg16";
6842 case DW_OP_breg17:
6843 return "DW_OP_breg17";
6844 case DW_OP_breg18:
6845 return "DW_OP_breg18";
6846 case DW_OP_breg19:
6847 return "DW_OP_breg19";
6848 case DW_OP_breg20:
6849 return "DW_OP_breg20";
6850 case DW_OP_breg21:
6851 return "DW_OP_breg21";
6852 case DW_OP_breg22:
6853 return "DW_OP_breg22";
6854 case DW_OP_breg23:
6855 return "DW_OP_breg23";
6856 case DW_OP_breg24:
6857 return "DW_OP_breg24";
6858 case DW_OP_breg25:
6859 return "DW_OP_breg25";
6860 case DW_OP_breg26:
6861 return "DW_OP_breg26";
6862 case DW_OP_breg27:
6863 return "DW_OP_breg27";
6864 case DW_OP_breg28:
6865 return "DW_OP_breg28";
6866 case DW_OP_breg29:
6867 return "DW_OP_breg29";
6868 case DW_OP_breg30:
6869 return "DW_OP_breg30";
6870 case DW_OP_breg31:
6871 return "DW_OP_breg31";
6872 case DW_OP_regx:
6873 return "DW_OP_regx";
6874 case DW_OP_fbreg:
6875 return "DW_OP_fbreg";
6876 case DW_OP_bregx:
6877 return "DW_OP_bregx";
6878 case DW_OP_piece:
6879 return "DW_OP_piece";
6880 case DW_OP_deref_size:
6881 return "DW_OP_deref_size";
6882 case DW_OP_xderef_size:
6883 return "DW_OP_xderef_size";
6884 case DW_OP_nop:
6885 return "DW_OP_nop";
ed348acc
EZ
6886 /* DWARF 3 extensions. */
6887 case DW_OP_push_object_address:
6888 return "DW_OP_push_object_address";
6889 case DW_OP_call2:
6890 return "DW_OP_call2";
6891 case DW_OP_call4:
6892 return "DW_OP_call4";
6893 case DW_OP_call_ref:
6894 return "DW_OP_call_ref";
6895 /* GNU extensions. */
6896 case DW_OP_GNU_push_tls_address:
6897 return "DW_OP_GNU_push_tls_address";
c906108c
SS
6898 default:
6899 return "OP_<unknown>";
6900 }
6901}
6902
6903static char *
fba45db2 6904dwarf_bool_name (unsigned mybool)
c906108c
SS
6905{
6906 if (mybool)
6907 return "TRUE";
6908 else
6909 return "FALSE";
6910}
6911
6912/* Convert a DWARF type code into its string name. */
6913
6914static char *
aa1ee363 6915dwarf_type_encoding_name (unsigned enc)
c906108c
SS
6916{
6917 switch (enc)
6918 {
6919 case DW_ATE_address:
6920 return "DW_ATE_address";
6921 case DW_ATE_boolean:
6922 return "DW_ATE_boolean";
6923 case DW_ATE_complex_float:
6924 return "DW_ATE_complex_float";
6925 case DW_ATE_float:
6926 return "DW_ATE_float";
6927 case DW_ATE_signed:
6928 return "DW_ATE_signed";
6929 case DW_ATE_signed_char:
6930 return "DW_ATE_signed_char";
6931 case DW_ATE_unsigned:
6932 return "DW_ATE_unsigned";
6933 case DW_ATE_unsigned_char:
6934 return "DW_ATE_unsigned_char";
d9fa45fe
DC
6935 case DW_ATE_imaginary_float:
6936 return "DW_ATE_imaginary_float";
c906108c
SS
6937 default:
6938 return "DW_ATE_<unknown>";
6939 }
6940}
6941
6942/* Convert a DWARF call frame info operation to its string name. */
6943
6944#if 0
6945static char *
aa1ee363 6946dwarf_cfi_name (unsigned cfi_opc)
c906108c
SS
6947{
6948 switch (cfi_opc)
6949 {
6950 case DW_CFA_advance_loc:
6951 return "DW_CFA_advance_loc";
6952 case DW_CFA_offset:
6953 return "DW_CFA_offset";
6954 case DW_CFA_restore:
6955 return "DW_CFA_restore";
6956 case DW_CFA_nop:
6957 return "DW_CFA_nop";
6958 case DW_CFA_set_loc:
6959 return "DW_CFA_set_loc";
6960 case DW_CFA_advance_loc1:
6961 return "DW_CFA_advance_loc1";
6962 case DW_CFA_advance_loc2:
6963 return "DW_CFA_advance_loc2";
6964 case DW_CFA_advance_loc4:
6965 return "DW_CFA_advance_loc4";
6966 case DW_CFA_offset_extended:
6967 return "DW_CFA_offset_extended";
6968 case DW_CFA_restore_extended:
6969 return "DW_CFA_restore_extended";
6970 case DW_CFA_undefined:
6971 return "DW_CFA_undefined";
6972 case DW_CFA_same_value:
6973 return "DW_CFA_same_value";
6974 case DW_CFA_register:
6975 return "DW_CFA_register";
6976 case DW_CFA_remember_state:
6977 return "DW_CFA_remember_state";
6978 case DW_CFA_restore_state:
6979 return "DW_CFA_restore_state";
6980 case DW_CFA_def_cfa:
6981 return "DW_CFA_def_cfa";
6982 case DW_CFA_def_cfa_register:
6983 return "DW_CFA_def_cfa_register";
6984 case DW_CFA_def_cfa_offset:
6985 return "DW_CFA_def_cfa_offset";
985cb1a3
JM
6986
6987 /* DWARF 3 */
6988 case DW_CFA_def_cfa_expression:
6989 return "DW_CFA_def_cfa_expression";
6990 case DW_CFA_expression:
6991 return "DW_CFA_expression";
6992 case DW_CFA_offset_extended_sf:
6993 return "DW_CFA_offset_extended_sf";
6994 case DW_CFA_def_cfa_sf:
6995 return "DW_CFA_def_cfa_sf";
6996 case DW_CFA_def_cfa_offset_sf:
6997 return "DW_CFA_def_cfa_offset_sf";
6998
c906108c
SS
6999 /* SGI/MIPS specific */
7000 case DW_CFA_MIPS_advance_loc8:
7001 return "DW_CFA_MIPS_advance_loc8";
985cb1a3
JM
7002
7003 /* GNU extensions */
7004 case DW_CFA_GNU_window_save:
7005 return "DW_CFA_GNU_window_save";
7006 case DW_CFA_GNU_args_size:
7007 return "DW_CFA_GNU_args_size";
7008 case DW_CFA_GNU_negative_offset_extended:
7009 return "DW_CFA_GNU_negative_offset_extended";
7010
c906108c
SS
7011 default:
7012 return "DW_CFA_<unknown>";
7013 }
7014}
7015#endif
7016
f9aca02d 7017static void
fba45db2 7018dump_die (struct die_info *die)
c906108c
SS
7019{
7020 unsigned int i;
7021
48cd0caa 7022 fprintf_unfiltered (gdb_stderr, "Die: %s (abbrev = %d, offset = %d)\n",
c906108c 7023 dwarf_tag_name (die->tag), die->abbrev, die->offset);
48cd0caa 7024 fprintf_unfiltered (gdb_stderr, "\thas children: %s\n",
639d11d3 7025 dwarf_bool_name (die->child != NULL));
c906108c 7026
48cd0caa 7027 fprintf_unfiltered (gdb_stderr, "\tattributes:\n");
c906108c
SS
7028 for (i = 0; i < die->num_attrs; ++i)
7029 {
48cd0caa 7030 fprintf_unfiltered (gdb_stderr, "\t\t%s (%s) ",
c906108c
SS
7031 dwarf_attr_name (die->attrs[i].name),
7032 dwarf_form_name (die->attrs[i].form));
7033 switch (die->attrs[i].form)
7034 {
7035 case DW_FORM_ref_addr:
7036 case DW_FORM_addr:
48cd0caa 7037 fprintf_unfiltered (gdb_stderr, "address: ");
c906108c
SS
7038 print_address_numeric (DW_ADDR (&die->attrs[i]), 1, gdb_stderr);
7039 break;
7040 case DW_FORM_block2:
7041 case DW_FORM_block4:
7042 case DW_FORM_block:
7043 case DW_FORM_block1:
48cd0caa 7044 fprintf_unfiltered (gdb_stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
c906108c
SS
7045 break;
7046 case DW_FORM_data1:
7047 case DW_FORM_data2:
7048 case DW_FORM_data4:
ce5d95e1 7049 case DW_FORM_data8:
c906108c
SS
7050 case DW_FORM_ref1:
7051 case DW_FORM_ref2:
7052 case DW_FORM_ref4:
7053 case DW_FORM_udata:
7054 case DW_FORM_sdata:
48cd0caa 7055 fprintf_unfiltered (gdb_stderr, "constant: %ld", DW_UNSND (&die->attrs[i]));
c906108c
SS
7056 break;
7057 case DW_FORM_string:
4bdf3d34 7058 case DW_FORM_strp:
48cd0caa 7059 fprintf_unfiltered (gdb_stderr, "string: \"%s\"",
c906108c 7060 DW_STRING (&die->attrs[i])
c5aa993b 7061 ? DW_STRING (&die->attrs[i]) : "");
c906108c
SS
7062 break;
7063 case DW_FORM_flag:
7064 if (DW_UNSND (&die->attrs[i]))
48cd0caa 7065 fprintf_unfiltered (gdb_stderr, "flag: TRUE");
c906108c 7066 else
48cd0caa 7067 fprintf_unfiltered (gdb_stderr, "flag: FALSE");
c906108c 7068 break;
a8329558
KW
7069 case DW_FORM_indirect:
7070 /* the reader will have reduced the indirect form to
7071 the "base form" so this form should not occur */
48cd0caa 7072 fprintf_unfiltered (gdb_stderr, "unexpected attribute form: DW_FORM_indirect");
a8329558 7073 break;
c906108c 7074 default:
48cd0caa 7075 fprintf_unfiltered (gdb_stderr, "unsupported attribute form: %d.",
c5aa993b 7076 die->attrs[i].form);
c906108c 7077 }
48cd0caa 7078 fprintf_unfiltered (gdb_stderr, "\n");
c906108c
SS
7079 }
7080}
7081
f9aca02d 7082static void
fba45db2 7083dump_die_list (struct die_info *die)
c906108c
SS
7084{
7085 while (die)
7086 {
7087 dump_die (die);
639d11d3
DC
7088 if (die->child != NULL)
7089 dump_die_list (die->child);
7090 if (die->sibling != NULL)
7091 dump_die_list (die->sibling);
c906108c
SS
7092 }
7093}
7094
f9aca02d 7095static void
fba45db2 7096store_in_ref_table (unsigned int offset, struct die_info *die)
c906108c
SS
7097{
7098 int h;
7099 struct die_info *old;
7100
7101 h = (offset % REF_HASH_SIZE);
7102 old = die_ref_table[h];
7103 die->next_ref = old;
7104 die_ref_table[h] = die;
7105}
7106
7107
7108static void
fba45db2 7109dwarf2_empty_hash_tables (void)
c906108c
SS
7110{
7111 memset (die_ref_table, 0, sizeof (die_ref_table));
7112}
7113
7114static unsigned int
fba45db2 7115dwarf2_get_ref_die_offset (struct attribute *attr)
c906108c
SS
7116{
7117 unsigned int result = 0;
7118
7119 switch (attr->form)
7120 {
7121 case DW_FORM_ref_addr:
7122 result = DW_ADDR (attr);
7123 break;
7124 case DW_FORM_ref1:
7125 case DW_FORM_ref2:
7126 case DW_FORM_ref4:
613e1657 7127 case DW_FORM_ref8:
c906108c
SS
7128 case DW_FORM_ref_udata:
7129 result = cu_header_offset + DW_UNSND (attr);
7130 break;
7131 default:
4d3c2250
KB
7132 complaint (&symfile_complaints,
7133 "unsupported die ref attribute form: '%s'",
7134 dwarf_form_name (attr->form));
c906108c
SS
7135 }
7136 return result;
7137}
7138
f9aca02d 7139static struct die_info *
fba45db2 7140follow_die_ref (unsigned int offset)
c906108c
SS
7141{
7142 struct die_info *die;
7143 int h;
7144
7145 h = (offset % REF_HASH_SIZE);
7146 die = die_ref_table[h];
7147 while (die)
7148 {
7149 if (die->offset == offset)
7150 {
7151 return die;
7152 }
7153 die = die->next_ref;
7154 }
7155 return NULL;
7156}
7157
7158static struct type *
fba45db2 7159dwarf2_fundamental_type (struct objfile *objfile, int typeid)
c906108c
SS
7160{
7161 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
7162 {
659b0389
ML
7163 error ("Dwarf Error: internal error - invalid fundamental type id %d [in module %s]",
7164 typeid, objfile->name);
c906108c
SS
7165 }
7166
7167 /* Look for this particular type in the fundamental type vector. If
7168 one is not found, create and install one appropriate for the
7169 current language and the current target machine. */
7170
7171 if (ftypes[typeid] == NULL)
7172 {
7173 ftypes[typeid] = cu_language_defn->la_fund_type (objfile, typeid);
7174 }
7175
7176 return (ftypes[typeid]);
7177}
7178
7179/* Decode simple location descriptions.
7180 Given a pointer to a dwarf block that defines a location, compute
7181 the location and return the value.
7182
4cecd739
DJ
7183 NOTE drow/2003-11-18: This function is called in two situations
7184 now: for the address of static or global variables (partial symbols
7185 only) and for offsets into structures which are expected to be
7186 (more or less) constant. The partial symbol case should go away,
7187 and only the constant case should remain. That will let this
7188 function complain more accurately. A few special modes are allowed
7189 without complaint for global variables (for instance, global
7190 register values and thread-local values).
c906108c
SS
7191
7192 A location description containing no operations indicates that the
4cecd739 7193 object is optimized out. The return value is 0 for that case.
6b992462
DJ
7194 FIXME drow/2003-11-16: No callers check for this case any more; soon all
7195 callers will only want a very basic result and this can become a
7196 complaint.
c906108c
SS
7197
7198 When the result is a register number, the global isreg flag is set,
7199 otherwise it is cleared.
7200
c906108c
SS
7201 Note that stack[0] is unused except as a default error return.
7202 Note that stack overflow is not yet handled. */
7203
7204static CORE_ADDR
e7c27a73 7205decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 7206{
e7c27a73
DJ
7207 struct objfile *objfile = cu->objfile;
7208 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
7209 int i;
7210 int size = blk->size;
7211 char *data = blk->data;
7212 CORE_ADDR stack[64];
7213 int stacki;
7214 unsigned int bytes_read, unsnd;
7215 unsigned char op;
7216
7217 i = 0;
7218 stacki = 0;
7219 stack[stacki] = 0;
7220 isreg = 0;
c906108c
SS
7221
7222 while (i < size)
7223 {
c906108c
SS
7224 op = data[i++];
7225 switch (op)
7226 {
f1bea926
JM
7227 case DW_OP_lit0:
7228 case DW_OP_lit1:
7229 case DW_OP_lit2:
7230 case DW_OP_lit3:
7231 case DW_OP_lit4:
7232 case DW_OP_lit5:
7233 case DW_OP_lit6:
7234 case DW_OP_lit7:
7235 case DW_OP_lit8:
7236 case DW_OP_lit9:
7237 case DW_OP_lit10:
7238 case DW_OP_lit11:
7239 case DW_OP_lit12:
7240 case DW_OP_lit13:
7241 case DW_OP_lit14:
7242 case DW_OP_lit15:
7243 case DW_OP_lit16:
7244 case DW_OP_lit17:
7245 case DW_OP_lit18:
7246 case DW_OP_lit19:
7247 case DW_OP_lit20:
7248 case DW_OP_lit21:
7249 case DW_OP_lit22:
7250 case DW_OP_lit23:
7251 case DW_OP_lit24:
7252 case DW_OP_lit25:
7253 case DW_OP_lit26:
7254 case DW_OP_lit27:
7255 case DW_OP_lit28:
7256 case DW_OP_lit29:
7257 case DW_OP_lit30:
7258 case DW_OP_lit31:
7259 stack[++stacki] = op - DW_OP_lit0;
7260 break;
7261
c906108c
SS
7262 case DW_OP_reg0:
7263 case DW_OP_reg1:
7264 case DW_OP_reg2:
7265 case DW_OP_reg3:
7266 case DW_OP_reg4:
7267 case DW_OP_reg5:
7268 case DW_OP_reg6:
7269 case DW_OP_reg7:
7270 case DW_OP_reg8:
7271 case DW_OP_reg9:
7272 case DW_OP_reg10:
7273 case DW_OP_reg11:
7274 case DW_OP_reg12:
7275 case DW_OP_reg13:
7276 case DW_OP_reg14:
7277 case DW_OP_reg15:
7278 case DW_OP_reg16:
7279 case DW_OP_reg17:
7280 case DW_OP_reg18:
7281 case DW_OP_reg19:
7282 case DW_OP_reg20:
7283 case DW_OP_reg21:
7284 case DW_OP_reg22:
7285 case DW_OP_reg23:
7286 case DW_OP_reg24:
7287 case DW_OP_reg25:
7288 case DW_OP_reg26:
7289 case DW_OP_reg27:
7290 case DW_OP_reg28:
7291 case DW_OP_reg29:
7292 case DW_OP_reg30:
7293 case DW_OP_reg31:
7294 isreg = 1;
7295 stack[++stacki] = op - DW_OP_reg0;
4cecd739
DJ
7296 if (i < size)
7297 dwarf2_complex_location_expr_complaint ();
c906108c
SS
7298 break;
7299
7300 case DW_OP_regx:
7301 isreg = 1;
7302 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
7303 i += bytes_read;
c906108c 7304 stack[++stacki] = unsnd;
4cecd739
DJ
7305 if (i < size)
7306 dwarf2_complex_location_expr_complaint ();
c906108c
SS
7307 break;
7308
7309 case DW_OP_addr:
107d2387 7310 stack[++stacki] = read_address (objfile->obfd, &data[i],
e7c27a73 7311 cu, &bytes_read);
107d2387 7312 i += bytes_read;
c906108c
SS
7313 break;
7314
7315 case DW_OP_const1u:
7316 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
7317 i += 1;
7318 break;
7319
7320 case DW_OP_const1s:
7321 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
7322 i += 1;
7323 break;
7324
7325 case DW_OP_const2u:
7326 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
7327 i += 2;
7328 break;
7329
7330 case DW_OP_const2s:
7331 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
7332 i += 2;
7333 break;
7334
7335 case DW_OP_const4u:
7336 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
7337 i += 4;
7338 break;
7339
7340 case DW_OP_const4s:
7341 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
7342 i += 4;
7343 break;
7344
7345 case DW_OP_constu:
7346 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
c5aa993b 7347 &bytes_read);
c906108c
SS
7348 i += bytes_read;
7349 break;
7350
7351 case DW_OP_consts:
7352 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
7353 i += bytes_read;
7354 break;
7355
f1bea926
JM
7356 case DW_OP_dup:
7357 stack[stacki + 1] = stack[stacki];
7358 stacki++;
7359 break;
7360
c906108c
SS
7361 case DW_OP_plus:
7362 stack[stacki - 1] += stack[stacki];
7363 stacki--;
7364 break;
7365
7366 case DW_OP_plus_uconst:
7367 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
7368 i += bytes_read;
7369 break;
7370
7371 case DW_OP_minus:
f1bea926 7372 stack[stacki - 1] -= stack[stacki];
c906108c
SS
7373 stacki--;
7374 break;
7375
7a292a7a 7376 case DW_OP_deref:
7a292a7a 7377 /* If we're not the last op, then we definitely can't encode
4cecd739
DJ
7378 this using GDB's address_class enum. This is valid for partial
7379 global symbols, although the variable's address will be bogus
7380 in the psymtab. */
7a292a7a 7381 if (i < size)
4d3c2250 7382 dwarf2_complex_location_expr_complaint ();
7a292a7a
SS
7383 break;
7384
9d774e44 7385 case DW_OP_GNU_push_tls_address:
9d774e44
EZ
7386 /* The top of the stack has the offset from the beginning
7387 of the thread control block at which the variable is located. */
7388 /* Nothing should follow this operator, so the top of stack would
7389 be returned. */
4cecd739
DJ
7390 /* This is valid for partial global symbols, but the variable's
7391 address will be bogus in the psymtab. */
9d774e44 7392 if (i < size)
4d3c2250 7393 dwarf2_complex_location_expr_complaint ();
9d774e44
EZ
7394 break;
7395
c906108c 7396 default:
4d3c2250
KB
7397 complaint (&symfile_complaints, "unsupported stack op: '%s'",
7398 dwarf_stack_op_name (op));
c906108c
SS
7399 return (stack[stacki]);
7400 }
7401 }
7402 return (stack[stacki]);
7403}
7404
7405/* memory allocation interface */
7406
c906108c 7407static void
4efb68b1 7408dwarf2_free_tmp_obstack (void *ignore)
c906108c
SS
7409{
7410 obstack_free (&dwarf2_tmp_obstack, NULL);
7411}
7412
7413static struct dwarf_block *
fba45db2 7414dwarf_alloc_block (void)
c906108c
SS
7415{
7416 struct dwarf_block *blk;
7417
7418 blk = (struct dwarf_block *)
7419 obstack_alloc (&dwarf2_tmp_obstack, sizeof (struct dwarf_block));
7420 return (blk);
7421}
7422
7423static struct abbrev_info *
fba45db2 7424dwarf_alloc_abbrev (void)
c906108c
SS
7425{
7426 struct abbrev_info *abbrev;
7427
7428 abbrev = (struct abbrev_info *) xmalloc (sizeof (struct abbrev_info));
7429 memset (abbrev, 0, sizeof (struct abbrev_info));
7430 return (abbrev);
7431}
7432
7433static struct die_info *
fba45db2 7434dwarf_alloc_die (void)
c906108c
SS
7435{
7436 struct die_info *die;
7437
7438 die = (struct die_info *) xmalloc (sizeof (struct die_info));
7439 memset (die, 0, sizeof (struct die_info));
7440 return (die);
7441}
2e276125
JB
7442
7443\f
7444/* Macro support. */
7445
7446
7447/* Return the full name of file number I in *LH's file name table.
7448 Use COMP_DIR as the name of the current directory of the
7449 compilation. The result is allocated using xmalloc; the caller is
7450 responsible for freeing it. */
7451static char *
7452file_full_name (int file, struct line_header *lh, const char *comp_dir)
7453{
7454 struct file_entry *fe = &lh->file_names[file - 1];
7455
7456 if (IS_ABSOLUTE_PATH (fe->name))
7457 return xstrdup (fe->name);
7458 else
7459 {
7460 const char *dir;
7461 int dir_len;
7462 char *full_name;
7463
7464 if (fe->dir_index)
7465 dir = lh->include_dirs[fe->dir_index - 1];
7466 else
7467 dir = comp_dir;
7468
7469 if (dir)
7470 {
7471 dir_len = strlen (dir);
7472 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
7473 strcpy (full_name, dir);
7474 full_name[dir_len] = '/';
7475 strcpy (full_name + dir_len + 1, fe->name);
7476 return full_name;
7477 }
7478 else
7479 return xstrdup (fe->name);
7480 }
7481}
7482
7483
7484static struct macro_source_file *
7485macro_start_file (int file, int line,
7486 struct macro_source_file *current_file,
7487 const char *comp_dir,
7488 struct line_header *lh, struct objfile *objfile)
7489{
7490 /* The full name of this source file. */
7491 char *full_name = file_full_name (file, lh, comp_dir);
7492
7493 /* We don't create a macro table for this compilation unit
7494 at all until we actually get a filename. */
7495 if (! pending_macros)
7496 pending_macros = new_macro_table (&objfile->symbol_obstack,
af5f3db6 7497 objfile->macro_cache);
2e276125
JB
7498
7499 if (! current_file)
7500 /* If we have no current file, then this must be the start_file
7501 directive for the compilation unit's main source file. */
7502 current_file = macro_set_main (pending_macros, full_name);
7503 else
7504 current_file = macro_include (current_file, line, full_name);
7505
7506 xfree (full_name);
7507
7508 return current_file;
7509}
7510
7511
7512/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
7513 followed by a null byte. */
7514static char *
7515copy_string (const char *buf, int len)
7516{
7517 char *s = xmalloc (len + 1);
7518 memcpy (s, buf, len);
7519 s[len] = '\0';
7520
7521 return s;
7522}
7523
7524
7525static const char *
7526consume_improper_spaces (const char *p, const char *body)
7527{
7528 if (*p == ' ')
7529 {
4d3c2250
KB
7530 complaint (&symfile_complaints,
7531 "macro definition contains spaces in formal argument list:\n`%s'",
7532 body);
2e276125
JB
7533
7534 while (*p == ' ')
7535 p++;
7536 }
7537
7538 return p;
7539}
7540
7541
7542static void
7543parse_macro_definition (struct macro_source_file *file, int line,
7544 const char *body)
7545{
7546 const char *p;
7547
7548 /* The body string takes one of two forms. For object-like macro
7549 definitions, it should be:
7550
7551 <macro name> " " <definition>
7552
7553 For function-like macro definitions, it should be:
7554
7555 <macro name> "() " <definition>
7556 or
7557 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
7558
7559 Spaces may appear only where explicitly indicated, and in the
7560 <definition>.
7561
7562 The Dwarf 2 spec says that an object-like macro's name is always
7563 followed by a space, but versions of GCC around March 2002 omit
7564 the space when the macro's definition is the empty string.
7565
7566 The Dwarf 2 spec says that there should be no spaces between the
7567 formal arguments in a function-like macro's formal argument list,
7568 but versions of GCC around March 2002 include spaces after the
7569 commas. */
7570
7571
7572 /* Find the extent of the macro name. The macro name is terminated
7573 by either a space or null character (for an object-like macro) or
7574 an opening paren (for a function-like macro). */
7575 for (p = body; *p; p++)
7576 if (*p == ' ' || *p == '(')
7577 break;
7578
7579 if (*p == ' ' || *p == '\0')
7580 {
7581 /* It's an object-like macro. */
7582 int name_len = p - body;
7583 char *name = copy_string (body, name_len);
7584 const char *replacement;
7585
7586 if (*p == ' ')
7587 replacement = body + name_len + 1;
7588 else
7589 {
4d3c2250 7590 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
7591 replacement = body + name_len;
7592 }
7593
7594 macro_define_object (file, line, name, replacement);
7595
7596 xfree (name);
7597 }
7598 else if (*p == '(')
7599 {
7600 /* It's a function-like macro. */
7601 char *name = copy_string (body, p - body);
7602 int argc = 0;
7603 int argv_size = 1;
7604 char **argv = xmalloc (argv_size * sizeof (*argv));
7605
7606 p++;
7607
7608 p = consume_improper_spaces (p, body);
7609
7610 /* Parse the formal argument list. */
7611 while (*p && *p != ')')
7612 {
7613 /* Find the extent of the current argument name. */
7614 const char *arg_start = p;
7615
7616 while (*p && *p != ',' && *p != ')' && *p != ' ')
7617 p++;
7618
7619 if (! *p || p == arg_start)
4d3c2250 7620 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
7621 else
7622 {
7623 /* Make sure argv has room for the new argument. */
7624 if (argc >= argv_size)
7625 {
7626 argv_size *= 2;
7627 argv = xrealloc (argv, argv_size * sizeof (*argv));
7628 }
7629
7630 argv[argc++] = copy_string (arg_start, p - arg_start);
7631 }
7632
7633 p = consume_improper_spaces (p, body);
7634
7635 /* Consume the comma, if present. */
7636 if (*p == ',')
7637 {
7638 p++;
7639
7640 p = consume_improper_spaces (p, body);
7641 }
7642 }
7643
7644 if (*p == ')')
7645 {
7646 p++;
7647
7648 if (*p == ' ')
7649 /* Perfectly formed definition, no complaints. */
7650 macro_define_function (file, line, name,
7651 argc, (const char **) argv,
7652 p + 1);
7653 else if (*p == '\0')
7654 {
7655 /* Complain, but do define it. */
4d3c2250 7656 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
7657 macro_define_function (file, line, name,
7658 argc, (const char **) argv,
7659 p);
7660 }
7661 else
7662 /* Just complain. */
4d3c2250 7663 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
7664 }
7665 else
7666 /* Just complain. */
4d3c2250 7667 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
7668
7669 xfree (name);
7670 {
7671 int i;
7672
7673 for (i = 0; i < argc; i++)
7674 xfree (argv[i]);
7675 }
7676 xfree (argv);
7677 }
7678 else
4d3c2250 7679 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
7680}
7681
7682
7683static void
7684dwarf_decode_macros (struct line_header *lh, unsigned int offset,
7685 char *comp_dir, bfd *abfd,
e7c27a73 7686 struct dwarf2_cu *cu)
2e276125
JB
7687{
7688 char *mac_ptr, *mac_end;
7689 struct macro_source_file *current_file = 0;
7690
7691 if (dwarf_macinfo_buffer == NULL)
7692 {
4d3c2250 7693 complaint (&symfile_complaints, "missing .debug_macinfo section");
2e276125
JB
7694 return;
7695 }
7696
7697 mac_ptr = dwarf_macinfo_buffer + offset;
7698 mac_end = dwarf_macinfo_buffer + dwarf_macinfo_size;
7699
7700 for (;;)
7701 {
7702 enum dwarf_macinfo_record_type macinfo_type;
7703
7704 /* Do we at least have room for a macinfo type byte? */
7705 if (mac_ptr >= mac_end)
7706 {
4d3c2250 7707 dwarf2_macros_too_long_complaint ();
2e276125
JB
7708 return;
7709 }
7710
7711 macinfo_type = read_1_byte (abfd, mac_ptr);
7712 mac_ptr++;
7713
7714 switch (macinfo_type)
7715 {
7716 /* A zero macinfo type indicates the end of the macro
7717 information. */
7718 case 0:
7719 return;
7720
7721 case DW_MACINFO_define:
7722 case DW_MACINFO_undef:
7723 {
7724 int bytes_read;
7725 int line;
7726 char *body;
7727
7728 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
7729 mac_ptr += bytes_read;
7730 body = read_string (abfd, mac_ptr, &bytes_read);
7731 mac_ptr += bytes_read;
7732
7733 if (! current_file)
4d3c2250
KB
7734 complaint (&symfile_complaints,
7735 "debug info gives macro %s outside of any file: %s",
7736 macinfo_type ==
7737 DW_MACINFO_define ? "definition" : macinfo_type ==
7738 DW_MACINFO_undef ? "undefinition" :
7739 "something-or-other", body);
2e276125
JB
7740 else
7741 {
7742 if (macinfo_type == DW_MACINFO_define)
7743 parse_macro_definition (current_file, line, body);
7744 else if (macinfo_type == DW_MACINFO_undef)
7745 macro_undef (current_file, line, body);
7746 }
7747 }
7748 break;
7749
7750 case DW_MACINFO_start_file:
7751 {
7752 int bytes_read;
7753 int line, file;
7754
7755 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
7756 mac_ptr += bytes_read;
7757 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
7758 mac_ptr += bytes_read;
7759
7760 current_file = macro_start_file (file, line,
7761 current_file, comp_dir,
e7c27a73 7762 lh, cu->objfile);
2e276125
JB
7763 }
7764 break;
7765
7766 case DW_MACINFO_end_file:
7767 if (! current_file)
4d3c2250
KB
7768 complaint (&symfile_complaints,
7769 "macro debug info has an unmatched `close_file' directive");
2e276125
JB
7770 else
7771 {
7772 current_file = current_file->included_by;
7773 if (! current_file)
7774 {
7775 enum dwarf_macinfo_record_type next_type;
7776
7777 /* GCC circa March 2002 doesn't produce the zero
7778 type byte marking the end of the compilation
7779 unit. Complain if it's not there, but exit no
7780 matter what. */
7781
7782 /* Do we at least have room for a macinfo type byte? */
7783 if (mac_ptr >= mac_end)
7784 {
4d3c2250 7785 dwarf2_macros_too_long_complaint ();
2e276125
JB
7786 return;
7787 }
7788
7789 /* We don't increment mac_ptr here, so this is just
7790 a look-ahead. */
7791 next_type = read_1_byte (abfd, mac_ptr);
7792 if (next_type != 0)
4d3c2250
KB
7793 complaint (&symfile_complaints,
7794 "no terminating 0-type entry for macros in `.debug_macinfo' section");
2e276125
JB
7795
7796 return;
7797 }
7798 }
7799 break;
7800
7801 case DW_MACINFO_vendor_ext:
7802 {
7803 int bytes_read;
7804 int constant;
7805 char *string;
7806
7807 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
7808 mac_ptr += bytes_read;
7809 string = read_string (abfd, mac_ptr, &bytes_read);
7810 mac_ptr += bytes_read;
7811
7812 /* We don't recognize any vendor extensions. */
7813 }
7814 break;
7815 }
7816 }
7817}
8e19ed76
PS
7818
7819/* Check if the attribute's form is a DW_FORM_block*
7820 if so return true else false. */
7821static int
7822attr_form_is_block (struct attribute *attr)
7823{
7824 return (attr == NULL ? 0 :
7825 attr->form == DW_FORM_block1
7826 || attr->form == DW_FORM_block2
7827 || attr->form == DW_FORM_block4
7828 || attr->form == DW_FORM_block);
7829}
4c2df51b
DJ
7830
7831static void
7832dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 7833 struct dwarf2_cu *cu)
4c2df51b 7834{
0d53c4c4 7835 if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
4c2df51b 7836 {
0d53c4c4 7837 struct dwarf2_loclist_baton *baton;
4c2df51b 7838
e7c27a73 7839 baton = obstack_alloc (&cu->objfile->symbol_obstack,
0d53c4c4 7840 sizeof (struct dwarf2_loclist_baton));
e7c27a73 7841 baton->objfile = cu->objfile;
4c2df51b 7842
0d53c4c4
DJ
7843 /* We don't know how long the location list is, but make sure we
7844 don't run off the edge of the section. */
7845 baton->size = dwarf_loc_size - DW_UNSND (attr);
7846 baton->data = dwarf_loc_buffer + DW_UNSND (attr);
e7c27a73
DJ
7847 baton->base_address = cu->header.base_address;
7848 if (cu->header.base_known == 0)
0d53c4c4
DJ
7849 complaint (&symfile_complaints,
7850 "Location list used without specifying the CU base address.");
4c2df51b 7851
0d53c4c4
DJ
7852 SYMBOL_LOCATION_FUNCS (sym) = &dwarf2_loclist_funcs;
7853 SYMBOL_LOCATION_BATON (sym) = baton;
7854 }
7855 else
7856 {
7857 struct dwarf2_locexpr_baton *baton;
7858
e7c27a73 7859 baton = obstack_alloc (&cu->objfile->symbol_obstack,
0d53c4c4 7860 sizeof (struct dwarf2_locexpr_baton));
e7c27a73 7861 baton->objfile = cu->objfile;
0d53c4c4
DJ
7862
7863 if (attr_form_is_block (attr))
7864 {
7865 /* Note that we're just copying the block's data pointer
7866 here, not the actual data. We're still pointing into the
7867 dwarf_info_buffer for SYM's objfile; right now we never
7868 release that buffer, but when we do clean up properly
7869 this may need to change. */
7870 baton->size = DW_BLOCK (attr)->size;
7871 baton->data = DW_BLOCK (attr)->data;
7872 }
7873 else
7874 {
7875 dwarf2_invalid_attrib_class_complaint ("location description",
7876 SYMBOL_NATURAL_NAME (sym));
7877 baton->size = 0;
7878 baton->data = NULL;
7879 }
7880
7881 SYMBOL_LOCATION_FUNCS (sym) = &dwarf2_locexpr_funcs;
7882 SYMBOL_LOCATION_BATON (sym) = baton;
7883 }
4c2df51b 7884}
This page took 0.731114 seconds and 4 git commands to generate.