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