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