Fix typo in ChangeLog entry.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004
4 Free Software Foundation, Inc.
5
6 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
7 Inc. with support from Florida State University (under contract
8 with the Ada Joint Program Office), and Silicon Graphics, Inc.
9 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
10 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
11 support in dwarfread.c
12
13 This file is part of GDB.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or (at
18 your option) any later version.
19
20 This program is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
29
30 #include "defs.h"
31 #include "bfd.h"
32 #include "symtab.h"
33 #include "gdbtypes.h"
34 #include "objfiles.h"
35 #include "elf/dwarf2.h"
36 #include "buildsym.h"
37 #include "demangle.h"
38 #include "expression.h"
39 #include "filenames.h" /* for DOSish file names */
40 #include "macrotab.h"
41 #include "language.h"
42 #include "complaints.h"
43 #include "bcache.h"
44 #include "dwarf2expr.h"
45 #include "dwarf2loc.h"
46 #include "cp-support.h"
47
48 #include <fcntl.h>
49 #include "gdb_string.h"
50 #include "gdb_assert.h"
51 #include <sys/types.h>
52
53 /* A note on memory usage for this file.
54
55 At the present time, this code reads the debug info sections into
56 the objfile's objfile_obstack. A definite improvement for startup
57 time, on platforms which do not emit relocations for debug
58 sections, would be to use mmap instead. The object's complete
59 debug information is loaded into memory, partly to simplify
60 absolute DIE references.
61
62 Whether using obstacks or mmap, the sections should remain loaded
63 until the objfile is released, and pointers into the section data
64 can be used for any other data associated to the objfile (symbol
65 names, type names, location expressions to name a few). */
66
67 #ifndef DWARF2_REG_TO_REGNUM
68 #define DWARF2_REG_TO_REGNUM(REG) (REG)
69 #endif
70
71 #if 0
72 /* .debug_info header for a compilation unit
73 Because of alignment constraints, this structure has padding and cannot
74 be mapped directly onto the beginning of the .debug_info section. */
75 typedef struct comp_unit_header
76 {
77 unsigned int length; /* length of the .debug_info
78 contribution */
79 unsigned short version; /* version number -- 2 for DWARF
80 version 2 */
81 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
82 unsigned char addr_size; /* byte size of an address -- 4 */
83 }
84 _COMP_UNIT_HEADER;
85 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
86 #endif
87
88 /* .debug_pubnames header
89 Because of alignment constraints, this structure has padding and cannot
90 be mapped directly onto the beginning of the .debug_info section. */
91 typedef struct pubnames_header
92 {
93 unsigned int length; /* length of the .debug_pubnames
94 contribution */
95 unsigned char version; /* version number -- 2 for DWARF
96 version 2 */
97 unsigned int info_offset; /* offset into .debug_info section */
98 unsigned int info_size; /* byte size of .debug_info section
99 portion */
100 }
101 _PUBNAMES_HEADER;
102 #define _ACTUAL_PUBNAMES_HEADER_SIZE 13
103
104 /* .debug_pubnames header
105 Because of alignment constraints, this structure has padding and cannot
106 be mapped directly onto the beginning of the .debug_info section. */
107 typedef struct aranges_header
108 {
109 unsigned int length; /* byte len of the .debug_aranges
110 contribution */
111 unsigned short version; /* version number -- 2 for DWARF
112 version 2 */
113 unsigned int info_offset; /* offset into .debug_info section */
114 unsigned char addr_size; /* byte size of an address */
115 unsigned char seg_size; /* byte size of segment descriptor */
116 }
117 _ARANGES_HEADER;
118 #define _ACTUAL_ARANGES_HEADER_SIZE 12
119
120 /* .debug_line statement program prologue
121 Because of alignment constraints, this structure has padding and cannot
122 be mapped directly onto the beginning of the .debug_info section. */
123 typedef struct statement_prologue
124 {
125 unsigned int total_length; /* byte length of the statement
126 information */
127 unsigned short version; /* version number -- 2 for DWARF
128 version 2 */
129 unsigned int prologue_length; /* # bytes between prologue &
130 stmt program */
131 unsigned char minimum_instruction_length; /* byte size of
132 smallest instr */
133 unsigned char default_is_stmt; /* initial value of is_stmt
134 register */
135 char line_base;
136 unsigned char line_range;
137 unsigned char opcode_base; /* number assigned to first special
138 opcode */
139 unsigned char *standard_opcode_lengths;
140 }
141 _STATEMENT_PROLOGUE;
142
143 /* offsets and sizes of debugging sections */
144
145 static unsigned int dwarf_info_size;
146 static unsigned int dwarf_abbrev_size;
147 static unsigned int dwarf_line_size;
148 static unsigned int dwarf_pubnames_size;
149 static unsigned int dwarf_aranges_size;
150 static unsigned int dwarf_loc_size;
151 static unsigned int dwarf_macinfo_size;
152 static unsigned int dwarf_str_size;
153 static unsigned int dwarf_ranges_size;
154 unsigned int dwarf_frame_size;
155 unsigned int dwarf_eh_frame_size;
156
157 static asection *dwarf_info_section;
158 static asection *dwarf_abbrev_section;
159 static asection *dwarf_line_section;
160 static asection *dwarf_pubnames_section;
161 static asection *dwarf_aranges_section;
162 static asection *dwarf_loc_section;
163 static asection *dwarf_macinfo_section;
164 static asection *dwarf_str_section;
165 static asection *dwarf_ranges_section;
166 asection *dwarf_frame_section;
167 asection *dwarf_eh_frame_section;
168
169 /* names of the debugging sections */
170
171 #define INFO_SECTION ".debug_info"
172 #define ABBREV_SECTION ".debug_abbrev"
173 #define LINE_SECTION ".debug_line"
174 #define PUBNAMES_SECTION ".debug_pubnames"
175 #define ARANGES_SECTION ".debug_aranges"
176 #define LOC_SECTION ".debug_loc"
177 #define MACINFO_SECTION ".debug_macinfo"
178 #define STR_SECTION ".debug_str"
179 #define RANGES_SECTION ".debug_ranges"
180 #define FRAME_SECTION ".debug_frame"
181 #define EH_FRAME_SECTION ".eh_frame"
182
183 /* local data types */
184
185 /* We hold several abbreviation tables in memory at the same time. */
186 #ifndef ABBREV_HASH_SIZE
187 #define ABBREV_HASH_SIZE 121
188 #endif
189
190 /* The data in a compilation unit header, after target2host
191 translation, looks like this. */
192 struct comp_unit_head
193 {
194 unsigned long length;
195 short version;
196 unsigned int abbrev_offset;
197 unsigned char addr_size;
198 unsigned char signed_addr_p;
199 unsigned int offset_size; /* size of file offsets; either 4 or 8 */
200 unsigned int initial_length_size; /* size of the length field; either
201 4 or 12 */
202
203 /* Offset to the first byte of this compilation unit header in the
204 * .debug_info section, for resolving relative reference dies. */
205
206 unsigned int offset;
207
208 /* Pointer to this compilation unit header in the .debug_info
209 * section */
210
211 char *cu_head_ptr;
212
213 /* Pointer to the first die of this compilatio unit. This will
214 * be the first byte following the compilation unit header. */
215
216 char *first_die_ptr;
217
218 /* Pointer to the next compilation unit header in the program. */
219
220 struct comp_unit_head *next;
221
222 /* Base address of this compilation unit. */
223
224 CORE_ADDR base_address;
225
226 /* Non-zero if base_address has been set. */
227
228 int base_known;
229 };
230
231 /* Internal state when decoding a particular compilation unit. */
232 struct dwarf2_cu
233 {
234 /* The objfile containing this compilation unit. */
235 struct objfile *objfile;
236
237 /* The header of the compilation unit.
238
239 FIXME drow/2003-11-10: Some of the things from the comp_unit_head
240 should logically be moved to the dwarf2_cu structure. */
241 struct comp_unit_head header;
242
243 struct function_range *first_fn, *last_fn, *cached_fn;
244
245 /* The language we are debugging. */
246 enum language language;
247 const struct language_defn *language_defn;
248
249 /* The generic symbol table building routines have separate lists for
250 file scope symbols and all all other scopes (local scopes). So
251 we need to select the right one to pass to add_symbol_to_list().
252 We do it by keeping a pointer to the correct list in list_in_scope.
253
254 FIXME: The original dwarf code just treated the file scope as the
255 first local scope, and all other local scopes as nested local
256 scopes, and worked fine. Check to see if we really need to
257 distinguish these in buildsym.c. */
258 struct pending **list_in_scope;
259
260 /* Maintain an array of referenced fundamental types for the current
261 compilation unit being read. For DWARF version 1, we have to construct
262 the fundamental types on the fly, since no information about the
263 fundamental types is supplied. Each such fundamental type is created by
264 calling a language dependent routine to create the type, and then a
265 pointer to that type is then placed in the array at the index specified
266 by it's FT_<TYPENAME> value. The array has a fixed size set by the
267 FT_NUM_MEMBERS compile time constant, which is the number of predefined
268 fundamental types gdb knows how to construct. */
269 struct type *ftypes[FT_NUM_MEMBERS]; /* Fundamental types */
270
271 /* DWARF abbreviation table associated with this compilation unit. */
272 struct abbrev_info **dwarf2_abbrevs;
273
274 /* Storage for the abbrev table. */
275 struct obstack abbrev_obstack;
276 };
277
278 /* The line number information for a compilation unit (found in the
279 .debug_line section) begins with a "statement program header",
280 which contains the following information. */
281 struct line_header
282 {
283 unsigned int total_length;
284 unsigned short version;
285 unsigned int header_length;
286 unsigned char minimum_instruction_length;
287 unsigned char default_is_stmt;
288 int line_base;
289 unsigned char line_range;
290 unsigned char opcode_base;
291
292 /* standard_opcode_lengths[i] is the number of operands for the
293 standard opcode whose value is i. This means that
294 standard_opcode_lengths[0] is unused, and the last meaningful
295 element is standard_opcode_lengths[opcode_base - 1]. */
296 unsigned char *standard_opcode_lengths;
297
298 /* The include_directories table. NOTE! These strings are not
299 allocated with xmalloc; instead, they are pointers into
300 debug_line_buffer. If you try to free them, `free' will get
301 indigestion. */
302 unsigned int num_include_dirs, include_dirs_size;
303 char **include_dirs;
304
305 /* The file_names table. NOTE! These strings are not allocated
306 with xmalloc; instead, they are pointers into debug_line_buffer.
307 Don't try to free them directly. */
308 unsigned int num_file_names, file_names_size;
309 struct file_entry
310 {
311 char *name;
312 unsigned int dir_index;
313 unsigned int mod_time;
314 unsigned int length;
315 } *file_names;
316
317 /* The start and end of the statement program following this
318 header. These point into dwarf_line_buffer. */
319 char *statement_program_start, *statement_program_end;
320 };
321
322 /* When we construct a partial symbol table entry we only
323 need this much information. */
324 struct partial_die_info
325 {
326 enum dwarf_tag tag;
327 unsigned char has_children;
328 unsigned char is_external;
329 unsigned char is_declaration;
330 unsigned char has_type;
331 unsigned int offset;
332 unsigned int abbrev;
333 char *name;
334 int has_pc_info;
335 CORE_ADDR lowpc;
336 CORE_ADDR highpc;
337 struct dwarf_block *locdesc;
338 unsigned int language;
339 char *sibling;
340 };
341
342 /* This data structure holds the information of an abbrev. */
343 struct abbrev_info
344 {
345 unsigned int number; /* number identifying abbrev */
346 enum dwarf_tag tag; /* dwarf tag */
347 unsigned short has_children; /* boolean */
348 unsigned short num_attrs; /* number of attributes */
349 struct attr_abbrev *attrs; /* an array of attribute descriptions */
350 struct abbrev_info *next; /* next in chain */
351 };
352
353 struct attr_abbrev
354 {
355 enum dwarf_attribute name;
356 enum dwarf_form form;
357 };
358
359 /* This data structure holds a complete die structure. */
360 struct die_info
361 {
362 enum dwarf_tag tag; /* Tag indicating type of die */
363 unsigned int abbrev; /* Abbrev number */
364 unsigned int offset; /* Offset in .debug_info section */
365 unsigned int num_attrs; /* Number of attributes */
366 struct attribute *attrs; /* An array of attributes */
367 struct die_info *next_ref; /* Next die in ref hash table */
368
369 /* The dies in a compilation unit form an n-ary tree. PARENT
370 points to this die's parent; CHILD points to the first child of
371 this node; and all the children of a given node are chained
372 together via their SIBLING fields, terminated by a die whose
373 tag is zero. */
374 struct die_info *child; /* Its first child, if any. */
375 struct die_info *sibling; /* Its next sibling, if any. */
376 struct die_info *parent; /* Its parent, if any. */
377
378 struct type *type; /* Cached type information */
379 };
380
381 /* Attributes have a name and a value */
382 struct attribute
383 {
384 enum dwarf_attribute name;
385 enum dwarf_form form;
386 union
387 {
388 char *str;
389 struct dwarf_block *blk;
390 unsigned long unsnd;
391 long int snd;
392 CORE_ADDR addr;
393 }
394 u;
395 };
396
397 struct function_range
398 {
399 const char *name;
400 CORE_ADDR lowpc, highpc;
401 int seen_line;
402 struct function_range *next;
403 };
404
405 /* Get at parts of an attribute structure */
406
407 #define DW_STRING(attr) ((attr)->u.str)
408 #define DW_UNSND(attr) ((attr)->u.unsnd)
409 #define DW_BLOCK(attr) ((attr)->u.blk)
410 #define DW_SND(attr) ((attr)->u.snd)
411 #define DW_ADDR(attr) ((attr)->u.addr)
412
413 /* Blocks are a bunch of untyped bytes. */
414 struct dwarf_block
415 {
416 unsigned int size;
417 char *data;
418 };
419
420 #ifndef ATTR_ALLOC_CHUNK
421 #define ATTR_ALLOC_CHUNK 4
422 #endif
423
424 /* A hash table of die offsets for following references. */
425 #ifndef REF_HASH_SIZE
426 #define REF_HASH_SIZE 1021
427 #endif
428
429 static struct die_info *die_ref_table[REF_HASH_SIZE];
430
431 /* Obstack for allocating temporary storage used during symbol reading. */
432 static struct obstack dwarf2_tmp_obstack;
433
434 /* Allocate fields for structs, unions and enums in this size. */
435 #ifndef DW_FIELD_ALLOC_CHUNK
436 #define DW_FIELD_ALLOC_CHUNK 4
437 #endif
438
439 /* Actually data from the sections. */
440 static char *dwarf_info_buffer;
441 static char *dwarf_abbrev_buffer;
442 static char *dwarf_line_buffer;
443 static char *dwarf_str_buffer;
444 static char *dwarf_macinfo_buffer;
445 static char *dwarf_ranges_buffer;
446 static char *dwarf_loc_buffer;
447
448 /* A zeroed version of a partial die for initialization purposes. */
449 static struct partial_die_info zeroed_partial_die;
450
451 /* FIXME: decode_locdesc sets these variables to describe the location
452 to the caller. These ought to be a structure or something. If
453 none of the flags are set, the object lives at the address returned
454 by decode_locdesc. */
455
456 static int isreg; /* Object lives in register.
457 decode_locdesc's return value is
458 the register number. */
459
460 /* We put a pointer to this structure in the read_symtab_private field
461 of the psymtab.
462
463 Most of the information in this structure is related to an entire
464 object file and could be passed via the sym_private field of the
465 objfile. It is possible to have both dwarf2 and some other form
466 of debug symbols in one object file. */
467
468 struct dwarf2_pinfo
469 {
470 /* Pointer to start of dwarf info buffer for the objfile. */
471
472 char *dwarf_info_buffer;
473
474 /* Offset in dwarf_info_buffer for this compilation unit. */
475
476 unsigned long dwarf_info_offset;
477
478 /* Pointer to start of dwarf abbreviation buffer for the objfile. */
479
480 char *dwarf_abbrev_buffer;
481
482 /* Size of dwarf abbreviation section for the objfile. */
483
484 unsigned int dwarf_abbrev_size;
485
486 /* Pointer to start of dwarf line buffer for the objfile. */
487
488 char *dwarf_line_buffer;
489
490 /* Size of dwarf_line_buffer, in bytes. */
491
492 unsigned int dwarf_line_size;
493
494 /* Pointer to start of dwarf string buffer for the objfile. */
495
496 char *dwarf_str_buffer;
497
498 /* Size of dwarf string section for the objfile. */
499
500 unsigned int dwarf_str_size;
501
502 /* Pointer to start of dwarf macro buffer for the objfile. */
503
504 char *dwarf_macinfo_buffer;
505
506 /* Size of dwarf macinfo section for the objfile. */
507
508 unsigned int dwarf_macinfo_size;
509
510 /* Pointer to start of dwarf ranges buffer for the objfile. */
511
512 char *dwarf_ranges_buffer;
513
514 /* Size of dwarf ranges buffer for the objfile. */
515
516 unsigned int dwarf_ranges_size;
517
518 /* Pointer to start of dwarf locations buffer for the objfile. */
519
520 char *dwarf_loc_buffer;
521
522 /* Size of dwarf locations buffer for the objfile. */
523
524 unsigned int dwarf_loc_size;
525 };
526
527 #define PST_PRIVATE(p) ((struct dwarf2_pinfo *)(p)->read_symtab_private)
528 #define DWARF_INFO_BUFFER(p) (PST_PRIVATE(p)->dwarf_info_buffer)
529 #define DWARF_INFO_OFFSET(p) (PST_PRIVATE(p)->dwarf_info_offset)
530 #define DWARF_ABBREV_BUFFER(p) (PST_PRIVATE(p)->dwarf_abbrev_buffer)
531 #define DWARF_ABBREV_SIZE(p) (PST_PRIVATE(p)->dwarf_abbrev_size)
532 #define DWARF_LINE_BUFFER(p) (PST_PRIVATE(p)->dwarf_line_buffer)
533 #define DWARF_LINE_SIZE(p) (PST_PRIVATE(p)->dwarf_line_size)
534 #define DWARF_STR_BUFFER(p) (PST_PRIVATE(p)->dwarf_str_buffer)
535 #define DWARF_STR_SIZE(p) (PST_PRIVATE(p)->dwarf_str_size)
536 #define DWARF_MACINFO_BUFFER(p) (PST_PRIVATE(p)->dwarf_macinfo_buffer)
537 #define DWARF_MACINFO_SIZE(p) (PST_PRIVATE(p)->dwarf_macinfo_size)
538 #define DWARF_RANGES_BUFFER(p) (PST_PRIVATE(p)->dwarf_ranges_buffer)
539 #define DWARF_RANGES_SIZE(p) (PST_PRIVATE(p)->dwarf_ranges_size)
540 #define DWARF_LOC_BUFFER(p) (PST_PRIVATE(p)->dwarf_loc_buffer)
541 #define DWARF_LOC_SIZE(p) (PST_PRIVATE(p)->dwarf_loc_size)
542
543 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
544 but this would require a corresponding change in unpack_field_as_long
545 and friends. */
546 static int bits_per_byte = 8;
547
548 /* The routines that read and process dies for a C struct or C++ class
549 pass lists of data member fields and lists of member function fields
550 in an instance of a field_info structure, as defined below. */
551 struct field_info
552 {
553 /* List of data member and baseclasses fields. */
554 struct nextfield
555 {
556 struct nextfield *next;
557 int accessibility;
558 int virtuality;
559 struct field field;
560 }
561 *fields;
562
563 /* Number of fields. */
564 int nfields;
565
566 /* Number of baseclasses. */
567 int nbaseclasses;
568
569 /* Set if the accesibility of one of the fields is not public. */
570 int non_public_fields;
571
572 /* Member function fields array, entries are allocated in the order they
573 are encountered in the object file. */
574 struct nextfnfield
575 {
576 struct nextfnfield *next;
577 struct fn_field fnfield;
578 }
579 *fnfields;
580
581 /* Member function fieldlist array, contains name of possibly overloaded
582 member function, number of overloaded member functions and a pointer
583 to the head of the member function field chain. */
584 struct fnfieldlist
585 {
586 char *name;
587 int length;
588 struct nextfnfield *head;
589 }
590 *fnfieldlists;
591
592 /* Number of entries in the fnfieldlists array. */
593 int nfnfields;
594 };
595
596 /* Various complaints about symbol reading that don't abort the process */
597
598 static void
599 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
600 {
601 complaint (&symfile_complaints,
602 "statement list doesn't fit in .debug_line section");
603 }
604
605 static void
606 dwarf2_complex_location_expr_complaint (void)
607 {
608 complaint (&symfile_complaints, "location expression too complex");
609 }
610
611 static void
612 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
613 int arg3)
614 {
615 complaint (&symfile_complaints,
616 "const value length mismatch for '%s', got %d, expected %d", arg1,
617 arg2, arg3);
618 }
619
620 static void
621 dwarf2_macros_too_long_complaint (void)
622 {
623 complaint (&symfile_complaints,
624 "macro info runs off end of `.debug_macinfo' section");
625 }
626
627 static void
628 dwarf2_macro_malformed_definition_complaint (const char *arg1)
629 {
630 complaint (&symfile_complaints,
631 "macro debug info contains a malformed macro definition:\n`%s'",
632 arg1);
633 }
634
635 static void
636 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
637 {
638 complaint (&symfile_complaints,
639 "invalid attribute class or form for '%s' in '%s'", arg1, arg2);
640 }
641
642 /* local function prototypes */
643
644 static void dwarf2_locate_sections (bfd *, asection *, void *);
645
646 #if 0
647 static void dwarf2_build_psymtabs_easy (struct objfile *, int);
648 #endif
649
650 static void dwarf2_build_psymtabs_hard (struct objfile *, int);
651
652 static char *scan_partial_symbols (char *, CORE_ADDR *, CORE_ADDR *,
653 struct dwarf2_cu *,
654 const char *namespace);
655
656 static void add_partial_symbol (struct partial_die_info *, struct dwarf2_cu *,
657 const char *namespace);
658
659 static int pdi_needs_namespace (enum dwarf_tag tag, const char *namespace);
660
661 static char *add_partial_namespace (struct partial_die_info *pdi,
662 char *info_ptr,
663 CORE_ADDR *lowpc, CORE_ADDR *highpc,
664 struct dwarf2_cu *cu,
665 const char *namespace);
666
667 static char *add_partial_structure (struct partial_die_info *struct_pdi,
668 char *info_ptr,
669 struct dwarf2_cu *cu,
670 const char *namespace);
671
672 static char *add_partial_enumeration (struct partial_die_info *enum_pdi,
673 char *info_ptr,
674 struct dwarf2_cu *cu,
675 const char *namespace);
676
677 static char *locate_pdi_sibling (struct partial_die_info *orig_pdi,
678 char *info_ptr,
679 bfd *abfd,
680 struct dwarf2_cu *cu);
681
682 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
683
684 static void psymtab_to_symtab_1 (struct partial_symtab *);
685
686 char *dwarf2_read_section (struct objfile *, asection *);
687
688 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
689
690 static void dwarf2_free_abbrev_table (void *);
691
692 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
693 struct dwarf2_cu *);
694
695 static char *read_partial_die (struct partial_die_info *,
696 bfd *, char *, struct dwarf2_cu *);
697
698 static char *read_full_die (struct die_info **, bfd *, char *,
699 struct dwarf2_cu *, int *);
700
701 static char *read_attribute (struct attribute *, struct attr_abbrev *,
702 bfd *, char *, struct dwarf2_cu *);
703
704 static char *read_attribute_value (struct attribute *, unsigned,
705 bfd *, char *, struct dwarf2_cu *);
706
707 static unsigned int read_1_byte (bfd *, char *);
708
709 static int read_1_signed_byte (bfd *, char *);
710
711 static unsigned int read_2_bytes (bfd *, char *);
712
713 static unsigned int read_4_bytes (bfd *, char *);
714
715 static unsigned long read_8_bytes (bfd *, char *);
716
717 static CORE_ADDR read_address (bfd *, char *ptr, struct dwarf2_cu *,
718 int *bytes_read);
719
720 static LONGEST read_initial_length (bfd *, char *,
721 struct comp_unit_head *, int *bytes_read);
722
723 static LONGEST read_offset (bfd *, char *, const struct comp_unit_head *,
724 int *bytes_read);
725
726 static char *read_n_bytes (bfd *, char *, unsigned int);
727
728 static char *read_string (bfd *, char *, unsigned int *);
729
730 static char *read_indirect_string (bfd *, char *, const struct comp_unit_head *,
731 unsigned int *);
732
733 static unsigned long read_unsigned_leb128 (bfd *, char *, unsigned int *);
734
735 static long read_signed_leb128 (bfd *, char *, unsigned int *);
736
737 static 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 struct die_info *child_die = die->child;
3335
3336 if (TYPE_TAG_NAME (die->type) != NULL)
3337 processing_current_prefix = TYPE_TAG_NAME (die->type);
3338
3339 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
3340 snapshots) has been known to create a die giving a declaration
3341 for a class that has, as a child, a die giving a definition for a
3342 nested class. So we have to process our children even if the
3343 current die is a declaration. Normally, of course, a declaration
3344 won't have any children at all. */
3345
3346 while (child_die != NULL && child_die->tag)
3347 {
3348 if (child_die->tag == DW_TAG_member
3349 || child_die->tag == DW_TAG_variable
3350 || child_die->tag == DW_TAG_inheritance)
3351 {
3352 /* Do nothing. */
3353 }
3354 else
3355 process_die (child_die, cu);
3356
3357 child_die = sibling_die (child_die);
3358 }
3359
3360 if (die->child != NULL && ! die_is_declaration (die, cu))
3361 new_symbol (die, die->type, cu);
3362
3363 processing_current_prefix = previous_prefix;
3364 }
3365
3366 /* Given a DW_AT_enumeration_type die, set its type. We do not
3367 complete the type's fields yet, or create any symbols. */
3368
3369 static void
3370 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
3371 {
3372 struct objfile *objfile = cu->objfile;
3373 struct type *type;
3374 struct attribute *attr;
3375
3376 if (die->type)
3377 return;
3378
3379 type = alloc_type (objfile);
3380
3381 TYPE_CODE (type) = TYPE_CODE_ENUM;
3382 attr = dwarf2_attr (die, DW_AT_name, cu);
3383 if (attr && DW_STRING (attr))
3384 {
3385 char *name = DW_STRING (attr);
3386
3387 if (processing_has_namespace_info)
3388 {
3389 TYPE_TAG_NAME (type) = obconcat (&objfile->objfile_obstack,
3390 processing_current_prefix,
3391 processing_current_prefix[0] == '\0'
3392 ? "" : "::",
3393 name);
3394 }
3395 else
3396 {
3397 /* The name is already allocated along with this objfile, so
3398 we don't need to duplicate it for the type. */
3399 TYPE_TAG_NAME (type) = name;
3400 }
3401 }
3402
3403 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
3404 if (attr)
3405 {
3406 TYPE_LENGTH (type) = DW_UNSND (attr);
3407 }
3408 else
3409 {
3410 TYPE_LENGTH (type) = 0;
3411 }
3412
3413 die->type = type;
3414 }
3415
3416 /* Determine the name of the type represented by DIE, which should be
3417 a named C++ compound type. Return the name in question; the caller
3418 is responsible for xfree()'ing it. */
3419
3420 static char *
3421 determine_class_name (struct die_info *die, struct dwarf2_cu *cu)
3422 {
3423 struct cleanup *back_to = NULL;
3424 struct die_info *spec_die = die_specification (die, cu);
3425 char *new_prefix = NULL;
3426
3427 /* If this is the definition of a class that is declared by another
3428 die, then processing_current_prefix may not be accurate; see
3429 read_func_scope for a similar example. */
3430 if (spec_die != NULL)
3431 {
3432 char *specification_prefix = determine_prefix (spec_die, cu);
3433 processing_current_prefix = specification_prefix;
3434 back_to = make_cleanup (xfree, specification_prefix);
3435 }
3436
3437 /* If we don't have namespace debug info, guess the name by trying
3438 to demangle the names of members, just like we did in
3439 add_partial_structure. */
3440 if (!processing_has_namespace_info)
3441 {
3442 struct die_info *child;
3443
3444 for (child = die->child;
3445 child != NULL && child->tag != 0;
3446 child = sibling_die (child))
3447 {
3448 if (child->tag == DW_TAG_subprogram)
3449 {
3450 new_prefix = class_name_from_physname (dwarf2_linkage_name
3451 (child, cu));
3452
3453 if (new_prefix != NULL)
3454 break;
3455 }
3456 }
3457 }
3458
3459 if (new_prefix == NULL)
3460 {
3461 const char *name = dwarf2_name (die, cu);
3462 new_prefix = typename_concat (processing_current_prefix,
3463 name ? name : "<<anonymous>>");
3464 }
3465
3466 if (back_to != NULL)
3467 do_cleanups (back_to);
3468
3469 return new_prefix;
3470 }
3471
3472 /* Given a pointer to a die which begins an enumeration, process all
3473 the dies that define the members of the enumeration, and create the
3474 symbol for the enumeration type.
3475
3476 NOTE: We reverse the order of the element list. */
3477
3478 static void
3479 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
3480 {
3481 struct objfile *objfile = cu->objfile;
3482 struct die_info *child_die;
3483 struct field *fields;
3484 struct attribute *attr;
3485 struct symbol *sym;
3486 int num_fields;
3487 int unsigned_enum = 1;
3488
3489 num_fields = 0;
3490 fields = NULL;
3491 if (die->child != NULL)
3492 {
3493 child_die = die->child;
3494 while (child_die && child_die->tag)
3495 {
3496 if (child_die->tag != DW_TAG_enumerator)
3497 {
3498 process_die (child_die, cu);
3499 }
3500 else
3501 {
3502 attr = dwarf2_attr (child_die, DW_AT_name, cu);
3503 if (attr)
3504 {
3505 sym = new_symbol (child_die, die->type, cu);
3506 if (SYMBOL_VALUE (sym) < 0)
3507 unsigned_enum = 0;
3508
3509 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
3510 {
3511 fields = (struct field *)
3512 xrealloc (fields,
3513 (num_fields + DW_FIELD_ALLOC_CHUNK)
3514 * sizeof (struct field));
3515 }
3516
3517 FIELD_NAME (fields[num_fields]) = DEPRECATED_SYMBOL_NAME (sym);
3518 FIELD_TYPE (fields[num_fields]) = NULL;
3519 FIELD_BITPOS (fields[num_fields]) = SYMBOL_VALUE (sym);
3520 FIELD_BITSIZE (fields[num_fields]) = 0;
3521 FIELD_STATIC_KIND (fields[num_fields]) = 0;
3522
3523 num_fields++;
3524 }
3525 }
3526
3527 child_die = sibling_die (child_die);
3528 }
3529
3530 if (num_fields)
3531 {
3532 TYPE_NFIELDS (die->type) = num_fields;
3533 TYPE_FIELDS (die->type) = (struct field *)
3534 TYPE_ALLOC (die->type, sizeof (struct field) * num_fields);
3535 memcpy (TYPE_FIELDS (die->type), fields,
3536 sizeof (struct field) * num_fields);
3537 xfree (fields);
3538 }
3539 if (unsigned_enum)
3540 TYPE_FLAGS (die->type) |= TYPE_FLAG_UNSIGNED;
3541 }
3542
3543 new_symbol (die, die->type, cu);
3544 }
3545
3546 /* Extract all information from a DW_TAG_array_type DIE and put it in
3547 the DIE's type field. For now, this only handles one dimensional
3548 arrays. */
3549
3550 static void
3551 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
3552 {
3553 struct objfile *objfile = cu->objfile;
3554 struct die_info *child_die;
3555 struct type *type = NULL;
3556 struct type *element_type, *range_type, *index_type;
3557 struct type **range_types = NULL;
3558 struct attribute *attr;
3559 int ndim = 0;
3560 struct cleanup *back_to;
3561
3562 /* Return if we've already decoded this type. */
3563 if (die->type)
3564 {
3565 return;
3566 }
3567
3568 element_type = die_type (die, cu);
3569
3570 /* Irix 6.2 native cc creates array types without children for
3571 arrays with unspecified length. */
3572 if (die->child == NULL)
3573 {
3574 index_type = dwarf2_fundamental_type (objfile, FT_INTEGER, cu);
3575 range_type = create_range_type (NULL, index_type, 0, -1);
3576 die->type = create_array_type (NULL, element_type, range_type);
3577 return;
3578 }
3579
3580 back_to = make_cleanup (null_cleanup, NULL);
3581 child_die = die->child;
3582 while (child_die && child_die->tag)
3583 {
3584 if (child_die->tag == DW_TAG_subrange_type)
3585 {
3586 read_subrange_type (child_die, cu);
3587
3588 if (child_die->type != NULL)
3589 {
3590 /* The range type was succesfully read. Save it for
3591 the array type creation. */
3592 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
3593 {
3594 range_types = (struct type **)
3595 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
3596 * sizeof (struct type *));
3597 if (ndim == 0)
3598 make_cleanup (free_current_contents, &range_types);
3599 }
3600 range_types[ndim++] = child_die->type;
3601 }
3602 }
3603 child_die = sibling_die (child_die);
3604 }
3605
3606 /* Dwarf2 dimensions are output from left to right, create the
3607 necessary array types in backwards order. */
3608 type = element_type;
3609 while (ndim-- > 0)
3610 type = create_array_type (NULL, type, range_types[ndim]);
3611
3612 /* Understand Dwarf2 support for vector types (like they occur on
3613 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
3614 array type. This is not part of the Dwarf2/3 standard yet, but a
3615 custom vendor extension. The main difference between a regular
3616 array and the vector variant is that vectors are passed by value
3617 to functions. */
3618 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
3619 if (attr)
3620 TYPE_FLAGS (type) |= TYPE_FLAG_VECTOR;
3621
3622 do_cleanups (back_to);
3623
3624 /* Install the type in the die. */
3625 die->type = type;
3626 }
3627
3628 /* First cut: install each common block member as a global variable. */
3629
3630 static void
3631 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
3632 {
3633 struct die_info *child_die;
3634 struct attribute *attr;
3635 struct symbol *sym;
3636 CORE_ADDR base = (CORE_ADDR) 0;
3637
3638 attr = dwarf2_attr (die, DW_AT_location, cu);
3639 if (attr)
3640 {
3641 /* Support the .debug_loc offsets */
3642 if (attr_form_is_block (attr))
3643 {
3644 base = decode_locdesc (DW_BLOCK (attr), cu);
3645 }
3646 else if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
3647 {
3648 dwarf2_complex_location_expr_complaint ();
3649 }
3650 else
3651 {
3652 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
3653 "common block member");
3654 }
3655 }
3656 if (die->child != NULL)
3657 {
3658 child_die = die->child;
3659 while (child_die && child_die->tag)
3660 {
3661 sym = new_symbol (child_die, NULL, cu);
3662 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
3663 if (attr)
3664 {
3665 SYMBOL_VALUE_ADDRESS (sym) =
3666 base + decode_locdesc (DW_BLOCK (attr), cu);
3667 add_symbol_to_list (sym, &global_symbols);
3668 }
3669 child_die = sibling_die (child_die);
3670 }
3671 }
3672 }
3673
3674 /* Read a C++ namespace. */
3675
3676 static void
3677 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
3678 {
3679 struct objfile *objfile = cu->objfile;
3680 const char *previous_prefix = processing_current_prefix;
3681 const char *name;
3682 int is_anonymous;
3683 struct die_info *current_die;
3684
3685 name = namespace_name (die, &is_anonymous, cu);
3686
3687 /* Now build the name of the current namespace. */
3688
3689 if (previous_prefix[0] == '\0')
3690 {
3691 processing_current_prefix = name;
3692 }
3693 else
3694 {
3695 /* We need temp_name around because processing_current_prefix
3696 is a const char *. */
3697 char *temp_name = alloca (strlen (previous_prefix)
3698 + 2 + strlen(name) + 1);
3699 strcpy (temp_name, previous_prefix);
3700 strcat (temp_name, "::");
3701 strcat (temp_name, name);
3702
3703 processing_current_prefix = temp_name;
3704 }
3705
3706 /* Add a symbol associated to this if we haven't seen the namespace
3707 before. Also, add a using directive if it's an anonymous
3708 namespace. */
3709
3710 if (dwarf2_extension (die, cu) == NULL)
3711 {
3712 struct type *type;
3713
3714 /* FIXME: carlton/2003-06-27: Once GDB is more const-correct,
3715 this cast will hopefully become unnecessary. */
3716 type = init_type (TYPE_CODE_NAMESPACE, 0, 0,
3717 (char *) processing_current_prefix,
3718 objfile);
3719 TYPE_TAG_NAME (type) = TYPE_NAME (type);
3720
3721 new_symbol (die, type, cu);
3722 die->type = type;
3723
3724 if (is_anonymous)
3725 cp_add_using_directive (processing_current_prefix,
3726 strlen (previous_prefix),
3727 strlen (processing_current_prefix));
3728 }
3729
3730 if (die->child != NULL)
3731 {
3732 struct die_info *child_die = die->child;
3733
3734 while (child_die && child_die->tag)
3735 {
3736 process_die (child_die, cu);
3737 child_die = sibling_die (child_die);
3738 }
3739 }
3740
3741 processing_current_prefix = previous_prefix;
3742 }
3743
3744 /* Return the name of the namespace represented by DIE. Set
3745 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
3746 namespace. */
3747
3748 static const char *
3749 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
3750 {
3751 struct die_info *current_die;
3752 const char *name = NULL;
3753
3754 /* Loop through the extensions until we find a name. */
3755
3756 for (current_die = die;
3757 current_die != NULL;
3758 current_die = dwarf2_extension (die, cu))
3759 {
3760 name = dwarf2_name (current_die, cu);
3761 if (name != NULL)
3762 break;
3763 }
3764
3765 /* Is it an anonymous namespace? */
3766
3767 *is_anonymous = (name == NULL);
3768 if (*is_anonymous)
3769 name = "(anonymous namespace)";
3770
3771 return name;
3772 }
3773
3774 /* Extract all information from a DW_TAG_pointer_type DIE and add to
3775 the user defined type vector. */
3776
3777 static void
3778 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
3779 {
3780 struct comp_unit_head *cu_header = &cu->header;
3781 struct type *type;
3782 struct attribute *attr_byte_size;
3783 struct attribute *attr_address_class;
3784 int byte_size, addr_class;
3785
3786 if (die->type)
3787 {
3788 return;
3789 }
3790
3791 type = lookup_pointer_type (die_type (die, cu));
3792
3793 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
3794 if (attr_byte_size)
3795 byte_size = DW_UNSND (attr_byte_size);
3796 else
3797 byte_size = cu_header->addr_size;
3798
3799 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
3800 if (attr_address_class)
3801 addr_class = DW_UNSND (attr_address_class);
3802 else
3803 addr_class = DW_ADDR_none;
3804
3805 /* If the pointer size or address class is different than the
3806 default, create a type variant marked as such and set the
3807 length accordingly. */
3808 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
3809 {
3810 if (ADDRESS_CLASS_TYPE_FLAGS_P ())
3811 {
3812 int type_flags;
3813
3814 type_flags = ADDRESS_CLASS_TYPE_FLAGS (byte_size, addr_class);
3815 gdb_assert ((type_flags & ~TYPE_FLAG_ADDRESS_CLASS_ALL) == 0);
3816 type = make_type_with_address_space (type, type_flags);
3817 }
3818 else if (TYPE_LENGTH (type) != byte_size)
3819 {
3820 complaint (&symfile_complaints, "invalid pointer size %d", byte_size);
3821 }
3822 else {
3823 /* Should we also complain about unhandled address classes? */
3824 }
3825 }
3826
3827 TYPE_LENGTH (type) = byte_size;
3828 die->type = type;
3829 }
3830
3831 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
3832 the user defined type vector. */
3833
3834 static void
3835 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
3836 {
3837 struct objfile *objfile = cu->objfile;
3838 struct type *type;
3839 struct type *to_type;
3840 struct type *domain;
3841
3842 if (die->type)
3843 {
3844 return;
3845 }
3846
3847 type = alloc_type (objfile);
3848 to_type = die_type (die, cu);
3849 domain = die_containing_type (die, cu);
3850 smash_to_member_type (type, domain, to_type);
3851
3852 die->type = type;
3853 }
3854
3855 /* Extract all information from a DW_TAG_reference_type DIE and add to
3856 the user defined type vector. */
3857
3858 static void
3859 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
3860 {
3861 struct comp_unit_head *cu_header = &cu->header;
3862 struct type *type;
3863 struct attribute *attr;
3864
3865 if (die->type)
3866 {
3867 return;
3868 }
3869
3870 type = lookup_reference_type (die_type (die, cu));
3871 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
3872 if (attr)
3873 {
3874 TYPE_LENGTH (type) = DW_UNSND (attr);
3875 }
3876 else
3877 {
3878 TYPE_LENGTH (type) = cu_header->addr_size;
3879 }
3880 die->type = type;
3881 }
3882
3883 static void
3884 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
3885 {
3886 struct type *base_type;
3887
3888 if (die->type)
3889 {
3890 return;
3891 }
3892
3893 base_type = die_type (die, cu);
3894 die->type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
3895 }
3896
3897 static void
3898 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
3899 {
3900 struct type *base_type;
3901
3902 if (die->type)
3903 {
3904 return;
3905 }
3906
3907 base_type = die_type (die, cu);
3908 die->type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
3909 }
3910
3911 /* Extract all information from a DW_TAG_string_type DIE and add to
3912 the user defined type vector. It isn't really a user defined type,
3913 but it behaves like one, with other DIE's using an AT_user_def_type
3914 attribute to reference it. */
3915
3916 static void
3917 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
3918 {
3919 struct objfile *objfile = cu->objfile;
3920 struct type *type, *range_type, *index_type, *char_type;
3921 struct attribute *attr;
3922 unsigned int length;
3923
3924 if (die->type)
3925 {
3926 return;
3927 }
3928
3929 attr = dwarf2_attr (die, DW_AT_string_length, cu);
3930 if (attr)
3931 {
3932 length = DW_UNSND (attr);
3933 }
3934 else
3935 {
3936 /* check for the DW_AT_byte_size attribute */
3937 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
3938 if (attr)
3939 {
3940 length = DW_UNSND (attr);
3941 }
3942 else
3943 {
3944 length = 1;
3945 }
3946 }
3947 index_type = dwarf2_fundamental_type (objfile, FT_INTEGER, cu);
3948 range_type = create_range_type (NULL, index_type, 1, length);
3949 if (cu->language == language_fortran)
3950 {
3951 /* Need to create a unique string type for bounds
3952 information */
3953 type = create_string_type (0, range_type);
3954 }
3955 else
3956 {
3957 char_type = dwarf2_fundamental_type (objfile, FT_CHAR, cu);
3958 type = create_string_type (char_type, range_type);
3959 }
3960 die->type = type;
3961 }
3962
3963 /* Handle DIES due to C code like:
3964
3965 struct foo
3966 {
3967 int (*funcp)(int a, long l);
3968 int b;
3969 };
3970
3971 ('funcp' generates a DW_TAG_subroutine_type DIE)
3972 */
3973
3974 static void
3975 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
3976 {
3977 struct type *type; /* Type that this function returns */
3978 struct type *ftype; /* Function that returns above type */
3979 struct attribute *attr;
3980
3981 /* Decode the type that this subroutine returns */
3982 if (die->type)
3983 {
3984 return;
3985 }
3986 type = die_type (die, cu);
3987 ftype = lookup_function_type (type);
3988
3989 /* All functions in C++ have prototypes. */
3990 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
3991 if ((attr && (DW_UNSND (attr) != 0))
3992 || cu->language == language_cplus)
3993 TYPE_FLAGS (ftype) |= TYPE_FLAG_PROTOTYPED;
3994
3995 if (die->child != NULL)
3996 {
3997 struct die_info *child_die;
3998 int nparams = 0;
3999 int iparams = 0;
4000
4001 /* Count the number of parameters.
4002 FIXME: GDB currently ignores vararg functions, but knows about
4003 vararg member functions. */
4004 child_die = die->child;
4005 while (child_die && child_die->tag)
4006 {
4007 if (child_die->tag == DW_TAG_formal_parameter)
4008 nparams++;
4009 else if (child_die->tag == DW_TAG_unspecified_parameters)
4010 TYPE_FLAGS (ftype) |= TYPE_FLAG_VARARGS;
4011 child_die = sibling_die (child_die);
4012 }
4013
4014 /* Allocate storage for parameters and fill them in. */
4015 TYPE_NFIELDS (ftype) = nparams;
4016 TYPE_FIELDS (ftype) = (struct field *)
4017 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
4018
4019 child_die = die->child;
4020 while (child_die && child_die->tag)
4021 {
4022 if (child_die->tag == DW_TAG_formal_parameter)
4023 {
4024 /* Dwarf2 has no clean way to discern C++ static and non-static
4025 member functions. G++ helps GDB by marking the first
4026 parameter for non-static member functions (which is the
4027 this pointer) as artificial. We pass this information
4028 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
4029 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
4030 if (attr)
4031 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
4032 else
4033 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
4034 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
4035 iparams++;
4036 }
4037 child_die = sibling_die (child_die);
4038 }
4039 }
4040
4041 die->type = ftype;
4042 }
4043
4044 static void
4045 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
4046 {
4047 struct objfile *objfile = cu->objfile;
4048 struct attribute *attr;
4049 char *name = NULL;
4050
4051 if (!die->type)
4052 {
4053 attr = dwarf2_attr (die, DW_AT_name, cu);
4054 if (attr && DW_STRING (attr))
4055 {
4056 name = DW_STRING (attr);
4057 }
4058 die->type = init_type (TYPE_CODE_TYPEDEF, 0, TYPE_FLAG_TARGET_STUB, name, objfile);
4059 TYPE_TARGET_TYPE (die->type) = die_type (die, cu);
4060 }
4061 }
4062
4063 /* Find a representation of a given base type and install
4064 it in the TYPE field of the die. */
4065
4066 static void
4067 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
4068 {
4069 struct objfile *objfile = cu->objfile;
4070 struct type *type;
4071 struct attribute *attr;
4072 int encoding = 0, size = 0;
4073
4074 /* If we've already decoded this die, this is a no-op. */
4075 if (die->type)
4076 {
4077 return;
4078 }
4079
4080 attr = dwarf2_attr (die, DW_AT_encoding, cu);
4081 if (attr)
4082 {
4083 encoding = DW_UNSND (attr);
4084 }
4085 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4086 if (attr)
4087 {
4088 size = DW_UNSND (attr);
4089 }
4090 attr = dwarf2_attr (die, DW_AT_name, cu);
4091 if (attr && DW_STRING (attr))
4092 {
4093 enum type_code code = TYPE_CODE_INT;
4094 int type_flags = 0;
4095
4096 switch (encoding)
4097 {
4098 case DW_ATE_address:
4099 /* Turn DW_ATE_address into a void * pointer. */
4100 code = TYPE_CODE_PTR;
4101 type_flags |= TYPE_FLAG_UNSIGNED;
4102 break;
4103 case DW_ATE_boolean:
4104 code = TYPE_CODE_BOOL;
4105 type_flags |= TYPE_FLAG_UNSIGNED;
4106 break;
4107 case DW_ATE_complex_float:
4108 code = TYPE_CODE_COMPLEX;
4109 break;
4110 case DW_ATE_float:
4111 code = TYPE_CODE_FLT;
4112 break;
4113 case DW_ATE_signed:
4114 case DW_ATE_signed_char:
4115 break;
4116 case DW_ATE_unsigned:
4117 case DW_ATE_unsigned_char:
4118 type_flags |= TYPE_FLAG_UNSIGNED;
4119 break;
4120 default:
4121 complaint (&symfile_complaints, "unsupported DW_AT_encoding: '%s'",
4122 dwarf_type_encoding_name (encoding));
4123 break;
4124 }
4125 type = init_type (code, size, type_flags, DW_STRING (attr), objfile);
4126 if (encoding == DW_ATE_address)
4127 TYPE_TARGET_TYPE (type) = dwarf2_fundamental_type (objfile, FT_VOID,
4128 cu);
4129 else if (encoding == DW_ATE_complex_float)
4130 {
4131 if (size == 32)
4132 TYPE_TARGET_TYPE (type)
4133 = dwarf2_fundamental_type (objfile, FT_EXT_PREC_FLOAT, cu);
4134 else if (size == 16)
4135 TYPE_TARGET_TYPE (type)
4136 = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT, cu);
4137 else if (size == 8)
4138 TYPE_TARGET_TYPE (type)
4139 = dwarf2_fundamental_type (objfile, FT_FLOAT, cu);
4140 }
4141 }
4142 else
4143 {
4144 type = dwarf_base_type (encoding, size, cu);
4145 }
4146 die->type = type;
4147 }
4148
4149 /* Read the given DW_AT_subrange DIE. */
4150
4151 static void
4152 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
4153 {
4154 struct type *base_type;
4155 struct type *range_type;
4156 struct attribute *attr;
4157 int low = 0;
4158 int high = -1;
4159
4160 /* If we have already decoded this die, then nothing more to do. */
4161 if (die->type)
4162 return;
4163
4164 base_type = die_type (die, cu);
4165 if (base_type == NULL)
4166 {
4167 complaint (&symfile_complaints,
4168 "DW_AT_type missing from DW_TAG_subrange_type");
4169 return;
4170 }
4171
4172 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
4173 base_type = alloc_type (NULL);
4174
4175 if (cu->language == language_fortran)
4176 {
4177 /* FORTRAN implies a lower bound of 1, if not given. */
4178 low = 1;
4179 }
4180
4181 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
4182 if (attr)
4183 low = dwarf2_get_attr_constant_value (attr, 0);
4184
4185 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
4186 if (attr)
4187 {
4188 if (attr->form == DW_FORM_block1)
4189 {
4190 /* GCC encodes arrays with unspecified or dynamic length
4191 with a DW_FORM_block1 attribute.
4192 FIXME: GDB does not yet know how to handle dynamic
4193 arrays properly, treat them as arrays with unspecified
4194 length for now.
4195
4196 FIXME: jimb/2003-09-22: GDB does not really know
4197 how to handle arrays of unspecified length
4198 either; we just represent them as zero-length
4199 arrays. Choose an appropriate upper bound given
4200 the lower bound we've computed above. */
4201 high = low - 1;
4202 }
4203 else
4204 high = dwarf2_get_attr_constant_value (attr, 1);
4205 }
4206
4207 range_type = create_range_type (NULL, base_type, low, high);
4208
4209 attr = dwarf2_attr (die, DW_AT_name, cu);
4210 if (attr && DW_STRING (attr))
4211 TYPE_NAME (range_type) = DW_STRING (attr);
4212
4213 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4214 if (attr)
4215 TYPE_LENGTH (range_type) = DW_UNSND (attr);
4216
4217 die->type = range_type;
4218 }
4219
4220
4221 /* Read a whole compilation unit into a linked list of dies. */
4222
4223 static struct die_info *
4224 read_comp_unit (char *info_ptr, bfd *abfd, struct dwarf2_cu *cu)
4225 {
4226 /* Reset die reference table; we are
4227 building new ones now. */
4228 dwarf2_empty_hash_tables ();
4229
4230 return read_die_and_children (info_ptr, abfd, cu, &info_ptr, NULL);
4231 }
4232
4233 /* Read a single die and all its descendents. Set the die's sibling
4234 field to NULL; set other fields in the die correctly, and set all
4235 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
4236 location of the info_ptr after reading all of those dies. PARENT
4237 is the parent of the die in question. */
4238
4239 static struct die_info *
4240 read_die_and_children (char *info_ptr, bfd *abfd,
4241 struct dwarf2_cu *cu,
4242 char **new_info_ptr,
4243 struct die_info *parent)
4244 {
4245 struct die_info *die;
4246 char *cur_ptr;
4247 int has_children;
4248
4249 cur_ptr = read_full_die (&die, abfd, info_ptr, cu, &has_children);
4250 store_in_ref_table (die->offset, die);
4251
4252 if (has_children)
4253 {
4254 die->child = read_die_and_siblings (cur_ptr, abfd, cu,
4255 new_info_ptr, die);
4256 }
4257 else
4258 {
4259 die->child = NULL;
4260 *new_info_ptr = cur_ptr;
4261 }
4262
4263 die->sibling = NULL;
4264 die->parent = parent;
4265 return die;
4266 }
4267
4268 /* Read a die, all of its descendents, and all of its siblings; set
4269 all of the fields of all of the dies correctly. Arguments are as
4270 in read_die_and_children. */
4271
4272 static struct die_info *
4273 read_die_and_siblings (char *info_ptr, bfd *abfd,
4274 struct dwarf2_cu *cu,
4275 char **new_info_ptr,
4276 struct die_info *parent)
4277 {
4278 struct die_info *first_die, *last_sibling;
4279 char *cur_ptr;
4280
4281 cur_ptr = info_ptr;
4282 first_die = last_sibling = NULL;
4283
4284 while (1)
4285 {
4286 struct die_info *die
4287 = read_die_and_children (cur_ptr, abfd, cu, &cur_ptr, parent);
4288
4289 if (!first_die)
4290 {
4291 first_die = die;
4292 }
4293 else
4294 {
4295 last_sibling->sibling = die;
4296 }
4297
4298 if (die->tag == 0)
4299 {
4300 *new_info_ptr = cur_ptr;
4301 return first_die;
4302 }
4303 else
4304 {
4305 last_sibling = die;
4306 }
4307 }
4308 }
4309
4310 /* Free a linked list of dies. */
4311
4312 static void
4313 free_die_list (struct die_info *dies)
4314 {
4315 struct die_info *die, *next;
4316
4317 die = dies;
4318 while (die)
4319 {
4320 if (die->child != NULL)
4321 free_die_list (die->child);
4322 next = die->sibling;
4323 xfree (die->attrs);
4324 xfree (die);
4325 die = next;
4326 }
4327 }
4328
4329 static void
4330 do_free_die_list_cleanup (void *dies)
4331 {
4332 free_die_list (dies);
4333 }
4334
4335 static struct cleanup *
4336 make_cleanup_free_die_list (struct die_info *dies)
4337 {
4338 return make_cleanup (do_free_die_list_cleanup, dies);
4339 }
4340
4341
4342 /* Read the contents of the section at OFFSET and of size SIZE from the
4343 object file specified by OBJFILE into the objfile_obstack and return it. */
4344
4345 char *
4346 dwarf2_read_section (struct objfile *objfile, asection *sectp)
4347 {
4348 bfd *abfd = objfile->obfd;
4349 char *buf, *retbuf;
4350 bfd_size_type size = bfd_get_section_size_before_reloc (sectp);
4351
4352 if (size == 0)
4353 return NULL;
4354
4355 buf = (char *) obstack_alloc (&objfile->objfile_obstack, size);
4356 retbuf
4357 = (char *) symfile_relocate_debug_section (abfd, sectp, (bfd_byte *) buf);
4358 if (retbuf != NULL)
4359 return retbuf;
4360
4361 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
4362 || bfd_bread (buf, size, abfd) != size)
4363 error ("Dwarf Error: Can't read DWARF data from '%s'",
4364 bfd_get_filename (abfd));
4365
4366 return buf;
4367 }
4368
4369 /* In DWARF version 2, the description of the debugging information is
4370 stored in a separate .debug_abbrev section. Before we read any
4371 dies from a section we read in all abbreviations and install them
4372 in a hash table. */
4373
4374 static void
4375 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
4376 {
4377 struct comp_unit_head *cu_header = &cu->header;
4378 char *abbrev_ptr;
4379 struct abbrev_info *cur_abbrev;
4380 unsigned int abbrev_number, bytes_read, abbrev_name;
4381 unsigned int abbrev_form, hash_number;
4382 struct attr_abbrev *cur_attrs;
4383 unsigned int allocated_attrs;
4384
4385 /* Initialize dwarf2 abbrevs */
4386 obstack_init (&cu->abbrev_obstack);
4387 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
4388 (ABBREV_HASH_SIZE
4389 * sizeof (struct abbrev_info *)));
4390 memset (cu->dwarf2_abbrevs, 0,
4391 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
4392
4393 abbrev_ptr = dwarf_abbrev_buffer + cu_header->abbrev_offset;
4394 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4395 abbrev_ptr += bytes_read;
4396
4397 allocated_attrs = ATTR_ALLOC_CHUNK;
4398 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
4399
4400 /* loop until we reach an abbrev number of 0 */
4401 while (abbrev_number)
4402 {
4403 cur_abbrev = dwarf_alloc_abbrev (cu);
4404
4405 /* read in abbrev header */
4406 cur_abbrev->number = abbrev_number;
4407 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4408 abbrev_ptr += bytes_read;
4409 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
4410 abbrev_ptr += 1;
4411
4412 /* now read in declarations */
4413 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4414 abbrev_ptr += bytes_read;
4415 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4416 abbrev_ptr += bytes_read;
4417 while (abbrev_name)
4418 {
4419 if (cur_abbrev->num_attrs == allocated_attrs)
4420 {
4421 allocated_attrs += ATTR_ALLOC_CHUNK;
4422 cur_attrs
4423 = xrealloc (cur_attrs, (allocated_attrs
4424 * sizeof (struct attr_abbrev)));
4425 }
4426 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
4427 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
4428 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4429 abbrev_ptr += bytes_read;
4430 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4431 abbrev_ptr += bytes_read;
4432 }
4433
4434 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
4435 (cur_abbrev->num_attrs
4436 * sizeof (struct attr_abbrev)));
4437 memcpy (cur_abbrev->attrs, cur_attrs,
4438 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
4439
4440 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4441 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
4442 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
4443
4444 /* Get next abbreviation.
4445 Under Irix6 the abbreviations for a compilation unit are not
4446 always properly terminated with an abbrev number of 0.
4447 Exit loop if we encounter an abbreviation which we have
4448 already read (which means we are about to read the abbreviations
4449 for the next compile unit) or if the end of the abbreviation
4450 table is reached. */
4451 if ((unsigned int) (abbrev_ptr - dwarf_abbrev_buffer)
4452 >= dwarf_abbrev_size)
4453 break;
4454 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4455 abbrev_ptr += bytes_read;
4456 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
4457 break;
4458 }
4459
4460 xfree (cur_attrs);
4461 }
4462
4463 /* Release the memory used by the abbrev table for a compilation unit. */
4464
4465 static void
4466 dwarf2_free_abbrev_table (void *ptr_to_cu)
4467 {
4468 struct dwarf2_cu *cu = ptr_to_cu;
4469
4470 obstack_free (&cu->abbrev_obstack, NULL);
4471 cu->dwarf2_abbrevs = NULL;
4472 }
4473
4474 /* Lookup an abbrev_info structure in the abbrev hash table. */
4475
4476 static struct abbrev_info *
4477 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
4478 {
4479 unsigned int hash_number;
4480 struct abbrev_info *abbrev;
4481
4482 hash_number = number % ABBREV_HASH_SIZE;
4483 abbrev = cu->dwarf2_abbrevs[hash_number];
4484
4485 while (abbrev)
4486 {
4487 if (abbrev->number == number)
4488 return abbrev;
4489 else
4490 abbrev = abbrev->next;
4491 }
4492 return NULL;
4493 }
4494
4495 /* Read a minimal amount of information into the minimal die structure. */
4496
4497 static char *
4498 read_partial_die (struct partial_die_info *part_die, bfd *abfd,
4499 char *info_ptr, struct dwarf2_cu *cu)
4500 {
4501 unsigned int abbrev_number, bytes_read, i;
4502 struct abbrev_info *abbrev;
4503 struct attribute attr;
4504 struct attribute spec_attr;
4505 int found_spec_attr = 0;
4506 int has_low_pc_attr = 0;
4507 int has_high_pc_attr = 0;
4508
4509 *part_die = zeroed_partial_die;
4510 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4511 info_ptr += bytes_read;
4512 if (!abbrev_number)
4513 return info_ptr;
4514
4515 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4516 if (!abbrev)
4517 {
4518 error ("Dwarf Error: Could not find abbrev number %d [in module %s]", abbrev_number,
4519 bfd_get_filename (abfd));
4520 }
4521 part_die->offset = info_ptr - dwarf_info_buffer;
4522 part_die->tag = abbrev->tag;
4523 part_die->has_children = abbrev->has_children;
4524 part_die->abbrev = abbrev_number;
4525
4526 for (i = 0; i < abbrev->num_attrs; ++i)
4527 {
4528 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
4529
4530 /* Store the data if it is of an attribute we want to keep in a
4531 partial symbol table. */
4532 switch (attr.name)
4533 {
4534 case DW_AT_name:
4535
4536 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
4537 if (part_die->name == NULL)
4538 part_die->name = DW_STRING (&attr);
4539 break;
4540 case DW_AT_MIPS_linkage_name:
4541 part_die->name = DW_STRING (&attr);
4542 break;
4543 case DW_AT_low_pc:
4544 has_low_pc_attr = 1;
4545 part_die->lowpc = DW_ADDR (&attr);
4546 break;
4547 case DW_AT_high_pc:
4548 has_high_pc_attr = 1;
4549 part_die->highpc = DW_ADDR (&attr);
4550 break;
4551 case DW_AT_location:
4552 /* Support the .debug_loc offsets */
4553 if (attr_form_is_block (&attr))
4554 {
4555 part_die->locdesc = DW_BLOCK (&attr);
4556 }
4557 else if (attr.form == DW_FORM_data4 || attr.form == DW_FORM_data8)
4558 {
4559 dwarf2_complex_location_expr_complaint ();
4560 }
4561 else
4562 {
4563 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
4564 "partial symbol information");
4565 }
4566 break;
4567 case DW_AT_language:
4568 part_die->language = DW_UNSND (&attr);
4569 break;
4570 case DW_AT_external:
4571 part_die->is_external = DW_UNSND (&attr);
4572 break;
4573 case DW_AT_declaration:
4574 part_die->is_declaration = DW_UNSND (&attr);
4575 break;
4576 case DW_AT_type:
4577 part_die->has_type = 1;
4578 break;
4579 case DW_AT_abstract_origin:
4580 case DW_AT_specification:
4581 found_spec_attr = 1;
4582 spec_attr = attr;
4583 break;
4584 case DW_AT_sibling:
4585 /* Ignore absolute siblings, they might point outside of
4586 the current compile unit. */
4587 if (attr.form == DW_FORM_ref_addr)
4588 complaint (&symfile_complaints, "ignoring absolute DW_AT_sibling");
4589 else
4590 part_die->sibling =
4591 dwarf_info_buffer + dwarf2_get_ref_die_offset (&attr, cu);
4592 break;
4593 default:
4594 break;
4595 }
4596 }
4597
4598 /* If we found a reference attribute and the die has no name, try
4599 to find a name in the referred to die. */
4600
4601 if (found_spec_attr && part_die->name == NULL)
4602 {
4603 struct partial_die_info spec_die;
4604 char *spec_ptr;
4605
4606 spec_ptr = dwarf_info_buffer
4607 + dwarf2_get_ref_die_offset (&spec_attr, cu);
4608 read_partial_die (&spec_die, abfd, spec_ptr, cu);
4609 if (spec_die.name)
4610 {
4611 part_die->name = spec_die.name;
4612
4613 /* Copy DW_AT_external attribute if it is set. */
4614 if (spec_die.is_external)
4615 part_die->is_external = spec_die.is_external;
4616 }
4617 }
4618
4619 /* When using the GNU linker, .gnu.linkonce. sections are used to
4620 eliminate duplicate copies of functions and vtables and such.
4621 The linker will arbitrarily choose one and discard the others.
4622 The AT_*_pc values for such functions refer to local labels in
4623 these sections. If the section from that file was discarded, the
4624 labels are not in the output, so the relocs get a value of 0.
4625 If this is a discarded function, mark the pc bounds as invalid,
4626 so that GDB will ignore it. */
4627 if (has_low_pc_attr && has_high_pc_attr
4628 && part_die->lowpc < part_die->highpc
4629 && (part_die->lowpc != 0
4630 || (bfd_get_file_flags (abfd) & HAS_RELOC)))
4631 part_die->has_pc_info = 1;
4632 return info_ptr;
4633 }
4634
4635 /* Read the die from the .debug_info section buffer. Set DIEP to
4636 point to a newly allocated die with its information, except for its
4637 child, sibling, and parent fields. Set HAS_CHILDREN to tell
4638 whether the die has children or not. */
4639
4640 static char *
4641 read_full_die (struct die_info **diep, bfd *abfd, char *info_ptr,
4642 struct dwarf2_cu *cu, int *has_children)
4643 {
4644 unsigned int abbrev_number, bytes_read, i, offset;
4645 struct abbrev_info *abbrev;
4646 struct die_info *die;
4647
4648 offset = info_ptr - dwarf_info_buffer;
4649 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4650 info_ptr += bytes_read;
4651 if (!abbrev_number)
4652 {
4653 die = dwarf_alloc_die ();
4654 die->tag = 0;
4655 die->abbrev = abbrev_number;
4656 die->type = NULL;
4657 *diep = die;
4658 *has_children = 0;
4659 return info_ptr;
4660 }
4661
4662 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4663 if (!abbrev)
4664 {
4665 error ("Dwarf Error: could not find abbrev number %d [in module %s]",
4666 abbrev_number,
4667 bfd_get_filename (abfd));
4668 }
4669 die = dwarf_alloc_die ();
4670 die->offset = offset;
4671 die->tag = abbrev->tag;
4672 die->abbrev = abbrev_number;
4673 die->type = NULL;
4674
4675 die->num_attrs = abbrev->num_attrs;
4676 die->attrs = (struct attribute *)
4677 xmalloc (die->num_attrs * sizeof (struct attribute));
4678
4679 for (i = 0; i < abbrev->num_attrs; ++i)
4680 {
4681 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
4682 abfd, info_ptr, cu);
4683 }
4684
4685 *diep = die;
4686 *has_children = abbrev->has_children;
4687 return info_ptr;
4688 }
4689
4690 /* Read an attribute value described by an attribute form. */
4691
4692 static char *
4693 read_attribute_value (struct attribute *attr, unsigned form,
4694 bfd *abfd, char *info_ptr,
4695 struct dwarf2_cu *cu)
4696 {
4697 struct comp_unit_head *cu_header = &cu->header;
4698 unsigned int bytes_read;
4699 struct dwarf_block *blk;
4700
4701 attr->form = form;
4702 switch (form)
4703 {
4704 case DW_FORM_addr:
4705 case DW_FORM_ref_addr:
4706 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
4707 info_ptr += bytes_read;
4708 break;
4709 case DW_FORM_block2:
4710 blk = dwarf_alloc_block ();
4711 blk->size = read_2_bytes (abfd, info_ptr);
4712 info_ptr += 2;
4713 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4714 info_ptr += blk->size;
4715 DW_BLOCK (attr) = blk;
4716 break;
4717 case DW_FORM_block4:
4718 blk = dwarf_alloc_block ();
4719 blk->size = read_4_bytes (abfd, info_ptr);
4720 info_ptr += 4;
4721 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4722 info_ptr += blk->size;
4723 DW_BLOCK (attr) = blk;
4724 break;
4725 case DW_FORM_data2:
4726 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
4727 info_ptr += 2;
4728 break;
4729 case DW_FORM_data4:
4730 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
4731 info_ptr += 4;
4732 break;
4733 case DW_FORM_data8:
4734 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
4735 info_ptr += 8;
4736 break;
4737 case DW_FORM_string:
4738 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
4739 info_ptr += bytes_read;
4740 break;
4741 case DW_FORM_strp:
4742 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
4743 &bytes_read);
4744 info_ptr += bytes_read;
4745 break;
4746 case DW_FORM_block:
4747 blk = dwarf_alloc_block ();
4748 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4749 info_ptr += bytes_read;
4750 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4751 info_ptr += blk->size;
4752 DW_BLOCK (attr) = blk;
4753 break;
4754 case DW_FORM_block1:
4755 blk = dwarf_alloc_block ();
4756 blk->size = read_1_byte (abfd, info_ptr);
4757 info_ptr += 1;
4758 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4759 info_ptr += blk->size;
4760 DW_BLOCK (attr) = blk;
4761 break;
4762 case DW_FORM_data1:
4763 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
4764 info_ptr += 1;
4765 break;
4766 case DW_FORM_flag:
4767 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
4768 info_ptr += 1;
4769 break;
4770 case DW_FORM_sdata:
4771 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
4772 info_ptr += bytes_read;
4773 break;
4774 case DW_FORM_udata:
4775 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4776 info_ptr += bytes_read;
4777 break;
4778 case DW_FORM_ref1:
4779 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
4780 info_ptr += 1;
4781 break;
4782 case DW_FORM_ref2:
4783 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
4784 info_ptr += 2;
4785 break;
4786 case DW_FORM_ref4:
4787 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
4788 info_ptr += 4;
4789 break;
4790 case DW_FORM_ref8:
4791 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
4792 info_ptr += 8;
4793 break;
4794 case DW_FORM_ref_udata:
4795 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4796 info_ptr += bytes_read;
4797 break;
4798 case DW_FORM_indirect:
4799 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4800 info_ptr += bytes_read;
4801 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
4802 break;
4803 default:
4804 error ("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]",
4805 dwarf_form_name (form),
4806 bfd_get_filename (abfd));
4807 }
4808 return info_ptr;
4809 }
4810
4811 /* Read an attribute described by an abbreviated attribute. */
4812
4813 static char *
4814 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
4815 bfd *abfd, char *info_ptr, struct dwarf2_cu *cu)
4816 {
4817 attr->name = abbrev->name;
4818 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
4819 }
4820
4821 /* read dwarf information from a buffer */
4822
4823 static unsigned int
4824 read_1_byte (bfd *abfd, char *buf)
4825 {
4826 return bfd_get_8 (abfd, (bfd_byte *) buf);
4827 }
4828
4829 static int
4830 read_1_signed_byte (bfd *abfd, char *buf)
4831 {
4832 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
4833 }
4834
4835 static unsigned int
4836 read_2_bytes (bfd *abfd, char *buf)
4837 {
4838 return bfd_get_16 (abfd, (bfd_byte *) buf);
4839 }
4840
4841 static int
4842 read_2_signed_bytes (bfd *abfd, char *buf)
4843 {
4844 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
4845 }
4846
4847 static unsigned int
4848 read_4_bytes (bfd *abfd, char *buf)
4849 {
4850 return bfd_get_32 (abfd, (bfd_byte *) buf);
4851 }
4852
4853 static int
4854 read_4_signed_bytes (bfd *abfd, char *buf)
4855 {
4856 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
4857 }
4858
4859 static unsigned long
4860 read_8_bytes (bfd *abfd, char *buf)
4861 {
4862 return bfd_get_64 (abfd, (bfd_byte *) buf);
4863 }
4864
4865 static CORE_ADDR
4866 read_address (bfd *abfd, char *buf, struct dwarf2_cu *cu, int *bytes_read)
4867 {
4868 struct comp_unit_head *cu_header = &cu->header;
4869 CORE_ADDR retval = 0;
4870
4871 if (cu_header->signed_addr_p)
4872 {
4873 switch (cu_header->addr_size)
4874 {
4875 case 2:
4876 retval = bfd_get_signed_16 (abfd, (bfd_byte *) buf);
4877 break;
4878 case 4:
4879 retval = bfd_get_signed_32 (abfd, (bfd_byte *) buf);
4880 break;
4881 case 8:
4882 retval = bfd_get_signed_64 (abfd, (bfd_byte *) buf);
4883 break;
4884 default:
4885 internal_error (__FILE__, __LINE__,
4886 "read_address: bad switch, signed [in module %s]",
4887 bfd_get_filename (abfd));
4888 }
4889 }
4890 else
4891 {
4892 switch (cu_header->addr_size)
4893 {
4894 case 2:
4895 retval = bfd_get_16 (abfd, (bfd_byte *) buf);
4896 break;
4897 case 4:
4898 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
4899 break;
4900 case 8:
4901 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
4902 break;
4903 default:
4904 internal_error (__FILE__, __LINE__,
4905 "read_address: bad switch, unsigned [in module %s]",
4906 bfd_get_filename (abfd));
4907 }
4908 }
4909
4910 *bytes_read = cu_header->addr_size;
4911 return retval;
4912 }
4913
4914 /* Read the initial length from a section. The (draft) DWARF 3
4915 specification allows the initial length to take up either 4 bytes
4916 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
4917 bytes describe the length and all offsets will be 8 bytes in length
4918 instead of 4.
4919
4920 An older, non-standard 64-bit format is also handled by this
4921 function. The older format in question stores the initial length
4922 as an 8-byte quantity without an escape value. Lengths greater
4923 than 2^32 aren't very common which means that the initial 4 bytes
4924 is almost always zero. Since a length value of zero doesn't make
4925 sense for the 32-bit format, this initial zero can be considered to
4926 be an escape value which indicates the presence of the older 64-bit
4927 format. As written, the code can't detect (old format) lengths
4928 greater than 4GB. If it becomes necessary to handle lengths somewhat
4929 larger than 4GB, we could allow other small values (such as the
4930 non-sensical values of 1, 2, and 3) to also be used as escape values
4931 indicating the presence of the old format.
4932
4933 The value returned via bytes_read should be used to increment
4934 the relevant pointer after calling read_initial_length().
4935
4936 As a side effect, this function sets the fields initial_length_size
4937 and offset_size in cu_header to the values appropriate for the
4938 length field. (The format of the initial length field determines
4939 the width of file offsets to be fetched later with fetch_offset().)
4940
4941 [ Note: read_initial_length() and read_offset() are based on the
4942 document entitled "DWARF Debugging Information Format", revision
4943 3, draft 8, dated November 19, 2001. This document was obtained
4944 from:
4945
4946 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
4947
4948 This document is only a draft and is subject to change. (So beware.)
4949
4950 Details regarding the older, non-standard 64-bit format were
4951 determined empirically by examining 64-bit ELF files produced
4952 by the SGI toolchain on an IRIX 6.5 machine.
4953
4954 - Kevin, July 16, 2002
4955 ] */
4956
4957 static LONGEST
4958 read_initial_length (bfd *abfd, char *buf, struct comp_unit_head *cu_header,
4959 int *bytes_read)
4960 {
4961 LONGEST retval = 0;
4962
4963 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
4964
4965 if (retval == 0xffffffff)
4966 {
4967 retval = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
4968 *bytes_read = 12;
4969 if (cu_header != NULL)
4970 {
4971 cu_header->initial_length_size = 12;
4972 cu_header->offset_size = 8;
4973 }
4974 }
4975 else if (retval == 0)
4976 {
4977 /* Handle (non-standard) 64-bit DWARF2 formats such as that used
4978 by IRIX. */
4979 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
4980 *bytes_read = 8;
4981 if (cu_header != NULL)
4982 {
4983 cu_header->initial_length_size = 8;
4984 cu_header->offset_size = 8;
4985 }
4986 }
4987 else
4988 {
4989 *bytes_read = 4;
4990 if (cu_header != NULL)
4991 {
4992 cu_header->initial_length_size = 4;
4993 cu_header->offset_size = 4;
4994 }
4995 }
4996
4997 return retval;
4998 }
4999
5000 /* Read an offset from the data stream. The size of the offset is
5001 given by cu_header->offset_size. */
5002
5003 static LONGEST
5004 read_offset (bfd *abfd, char *buf, const struct comp_unit_head *cu_header,
5005 int *bytes_read)
5006 {
5007 LONGEST retval = 0;
5008
5009 switch (cu_header->offset_size)
5010 {
5011 case 4:
5012 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
5013 *bytes_read = 4;
5014 break;
5015 case 8:
5016 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
5017 *bytes_read = 8;
5018 break;
5019 default:
5020 internal_error (__FILE__, __LINE__,
5021 "read_offset: bad switch [in module %s]",
5022 bfd_get_filename (abfd));
5023 }
5024
5025 return retval;
5026 }
5027
5028 static char *
5029 read_n_bytes (bfd *abfd, char *buf, unsigned int size)
5030 {
5031 /* If the size of a host char is 8 bits, we can return a pointer
5032 to the buffer, otherwise we have to copy the data to a buffer
5033 allocated on the temporary obstack. */
5034 gdb_assert (HOST_CHAR_BIT == 8);
5035 return buf;
5036 }
5037
5038 static char *
5039 read_string (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
5040 {
5041 /* If the size of a host char is 8 bits, we can return a pointer
5042 to the string, otherwise we have to copy the string to a buffer
5043 allocated on the temporary obstack. */
5044 gdb_assert (HOST_CHAR_BIT == 8);
5045 if (*buf == '\0')
5046 {
5047 *bytes_read_ptr = 1;
5048 return NULL;
5049 }
5050 *bytes_read_ptr = strlen (buf) + 1;
5051 return buf;
5052 }
5053
5054 static char *
5055 read_indirect_string (bfd *abfd, char *buf,
5056 const struct comp_unit_head *cu_header,
5057 unsigned int *bytes_read_ptr)
5058 {
5059 LONGEST str_offset = read_offset (abfd, buf, cu_header,
5060 (int *) bytes_read_ptr);
5061
5062 if (dwarf_str_buffer == NULL)
5063 {
5064 error ("DW_FORM_strp used without .debug_str section [in module %s]",
5065 bfd_get_filename (abfd));
5066 return NULL;
5067 }
5068 if (str_offset >= dwarf_str_size)
5069 {
5070 error ("DW_FORM_strp pointing outside of .debug_str section [in module %s]",
5071 bfd_get_filename (abfd));
5072 return NULL;
5073 }
5074 gdb_assert (HOST_CHAR_BIT == 8);
5075 if (dwarf_str_buffer[str_offset] == '\0')
5076 return NULL;
5077 return dwarf_str_buffer + str_offset;
5078 }
5079
5080 static unsigned long
5081 read_unsigned_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
5082 {
5083 unsigned long result;
5084 unsigned int num_read;
5085 int i, shift;
5086 unsigned char byte;
5087
5088 result = 0;
5089 shift = 0;
5090 num_read = 0;
5091 i = 0;
5092 while (1)
5093 {
5094 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
5095 buf++;
5096 num_read++;
5097 result |= ((unsigned long)(byte & 127) << shift);
5098 if ((byte & 128) == 0)
5099 {
5100 break;
5101 }
5102 shift += 7;
5103 }
5104 *bytes_read_ptr = num_read;
5105 return result;
5106 }
5107
5108 static long
5109 read_signed_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
5110 {
5111 long result;
5112 int i, shift, size, num_read;
5113 unsigned char byte;
5114
5115 result = 0;
5116 shift = 0;
5117 size = 32;
5118 num_read = 0;
5119 i = 0;
5120 while (1)
5121 {
5122 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
5123 buf++;
5124 num_read++;
5125 result |= ((long)(byte & 127) << shift);
5126 shift += 7;
5127 if ((byte & 128) == 0)
5128 {
5129 break;
5130 }
5131 }
5132 if ((shift < size) && (byte & 0x40))
5133 {
5134 result |= -(1 << shift);
5135 }
5136 *bytes_read_ptr = num_read;
5137 return result;
5138 }
5139
5140 /* Return a pointer to just past the end of an LEB128 number in BUF. */
5141
5142 static char *
5143 skip_leb128 (bfd *abfd, char *buf)
5144 {
5145 int byte;
5146
5147 while (1)
5148 {
5149 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
5150 buf++;
5151 if ((byte & 128) == 0)
5152 return buf;
5153 }
5154 }
5155
5156 static void
5157 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
5158 {
5159 switch (lang)
5160 {
5161 case DW_LANG_C89:
5162 case DW_LANG_C:
5163 cu->language = language_c;
5164 break;
5165 case DW_LANG_C_plus_plus:
5166 cu->language = language_cplus;
5167 break;
5168 case DW_LANG_Fortran77:
5169 case DW_LANG_Fortran90:
5170 case DW_LANG_Fortran95:
5171 cu->language = language_fortran;
5172 break;
5173 case DW_LANG_Mips_Assembler:
5174 cu->language = language_asm;
5175 break;
5176 case DW_LANG_Java:
5177 cu->language = language_java;
5178 break;
5179 case DW_LANG_Ada83:
5180 case DW_LANG_Ada95:
5181 case DW_LANG_Cobol74:
5182 case DW_LANG_Cobol85:
5183 case DW_LANG_Pascal83:
5184 case DW_LANG_Modula2:
5185 default:
5186 cu->language = language_minimal;
5187 break;
5188 }
5189 cu->language_defn = language_def (cu->language);
5190 }
5191
5192 /* Return the named attribute or NULL if not there. */
5193
5194 static struct attribute *
5195 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
5196 {
5197 unsigned int i;
5198 struct attribute *spec = NULL;
5199
5200 for (i = 0; i < die->num_attrs; ++i)
5201 {
5202 if (die->attrs[i].name == name)
5203 {
5204 return &die->attrs[i];
5205 }
5206 if (die->attrs[i].name == DW_AT_specification
5207 || die->attrs[i].name == DW_AT_abstract_origin)
5208 spec = &die->attrs[i];
5209 }
5210 if (spec)
5211 {
5212 struct die_info *ref_die =
5213 follow_die_ref (dwarf2_get_ref_die_offset (spec, cu));
5214
5215 if (ref_die)
5216 return dwarf2_attr (ref_die, name, cu);
5217 }
5218
5219 return NULL;
5220 }
5221
5222 static int
5223 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
5224 {
5225 return (dwarf2_attr (die, DW_AT_declaration, cu)
5226 && ! dwarf2_attr (die, DW_AT_specification, cu));
5227 }
5228
5229 /* Return the die giving the specification for DIE, if there is
5230 one. */
5231
5232 static struct die_info *
5233 die_specification (struct die_info *die, struct dwarf2_cu *cu)
5234 {
5235 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification, cu);
5236
5237 if (spec_attr == NULL)
5238 return NULL;
5239 else
5240 return follow_die_ref (dwarf2_get_ref_die_offset (spec_attr, cu));
5241 }
5242
5243 /* Free the line_header structure *LH, and any arrays and strings it
5244 refers to. */
5245 static void
5246 free_line_header (struct line_header *lh)
5247 {
5248 if (lh->standard_opcode_lengths)
5249 xfree (lh->standard_opcode_lengths);
5250
5251 /* Remember that all the lh->file_names[i].name pointers are
5252 pointers into debug_line_buffer, and don't need to be freed. */
5253 if (lh->file_names)
5254 xfree (lh->file_names);
5255
5256 /* Similarly for the include directory names. */
5257 if (lh->include_dirs)
5258 xfree (lh->include_dirs);
5259
5260 xfree (lh);
5261 }
5262
5263
5264 /* Add an entry to LH's include directory table. */
5265 static void
5266 add_include_dir (struct line_header *lh, char *include_dir)
5267 {
5268 /* Grow the array if necessary. */
5269 if (lh->include_dirs_size == 0)
5270 {
5271 lh->include_dirs_size = 1; /* for testing */
5272 lh->include_dirs = xmalloc (lh->include_dirs_size
5273 * sizeof (*lh->include_dirs));
5274 }
5275 else if (lh->num_include_dirs >= lh->include_dirs_size)
5276 {
5277 lh->include_dirs_size *= 2;
5278 lh->include_dirs = xrealloc (lh->include_dirs,
5279 (lh->include_dirs_size
5280 * sizeof (*lh->include_dirs)));
5281 }
5282
5283 lh->include_dirs[lh->num_include_dirs++] = include_dir;
5284 }
5285
5286
5287 /* Add an entry to LH's file name table. */
5288 static void
5289 add_file_name (struct line_header *lh,
5290 char *name,
5291 unsigned int dir_index,
5292 unsigned int mod_time,
5293 unsigned int length)
5294 {
5295 struct file_entry *fe;
5296
5297 /* Grow the array if necessary. */
5298 if (lh->file_names_size == 0)
5299 {
5300 lh->file_names_size = 1; /* for testing */
5301 lh->file_names = xmalloc (lh->file_names_size
5302 * sizeof (*lh->file_names));
5303 }
5304 else if (lh->num_file_names >= lh->file_names_size)
5305 {
5306 lh->file_names_size *= 2;
5307 lh->file_names = xrealloc (lh->file_names,
5308 (lh->file_names_size
5309 * sizeof (*lh->file_names)));
5310 }
5311
5312 fe = &lh->file_names[lh->num_file_names++];
5313 fe->name = name;
5314 fe->dir_index = dir_index;
5315 fe->mod_time = mod_time;
5316 fe->length = length;
5317 }
5318
5319
5320 /* Read the statement program header starting at OFFSET in
5321 dwarf_line_buffer, according to the endianness of ABFD. Return a
5322 pointer to a struct line_header, allocated using xmalloc.
5323
5324 NOTE: the strings in the include directory and file name tables of
5325 the returned object point into debug_line_buffer, and must not be
5326 freed. */
5327 static struct line_header *
5328 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
5329 struct dwarf2_cu *cu)
5330 {
5331 struct cleanup *back_to;
5332 struct line_header *lh;
5333 char *line_ptr;
5334 int bytes_read;
5335 int i;
5336 char *cur_dir, *cur_file;
5337
5338 if (dwarf_line_buffer == NULL)
5339 {
5340 complaint (&symfile_complaints, "missing .debug_line section");
5341 return 0;
5342 }
5343
5344 /* Make sure that at least there's room for the total_length field. That
5345 could be 12 bytes long, but we're just going to fudge that. */
5346 if (offset + 4 >= dwarf_line_size)
5347 {
5348 dwarf2_statement_list_fits_in_line_number_section_complaint ();
5349 return 0;
5350 }
5351
5352 lh = xmalloc (sizeof (*lh));
5353 memset (lh, 0, sizeof (*lh));
5354 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
5355 (void *) lh);
5356
5357 line_ptr = dwarf_line_buffer + offset;
5358
5359 /* read in the header */
5360 lh->total_length = read_initial_length (abfd, line_ptr, NULL, &bytes_read);
5361 line_ptr += bytes_read;
5362 if (line_ptr + lh->total_length > dwarf_line_buffer + dwarf_line_size)
5363 {
5364 dwarf2_statement_list_fits_in_line_number_section_complaint ();
5365 return 0;
5366 }
5367 lh->statement_program_end = line_ptr + lh->total_length;
5368 lh->version = read_2_bytes (abfd, line_ptr);
5369 line_ptr += 2;
5370 lh->header_length = read_offset (abfd, line_ptr, &cu->header, &bytes_read);
5371 line_ptr += bytes_read;
5372 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
5373 line_ptr += 1;
5374 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
5375 line_ptr += 1;
5376 lh->line_base = read_1_signed_byte (abfd, line_ptr);
5377 line_ptr += 1;
5378 lh->line_range = read_1_byte (abfd, line_ptr);
5379 line_ptr += 1;
5380 lh->opcode_base = read_1_byte (abfd, line_ptr);
5381 line_ptr += 1;
5382 lh->standard_opcode_lengths
5383 = (unsigned char *) xmalloc (lh->opcode_base * sizeof (unsigned char));
5384
5385 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
5386 for (i = 1; i < lh->opcode_base; ++i)
5387 {
5388 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
5389 line_ptr += 1;
5390 }
5391
5392 /* Read directory table */
5393 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
5394 {
5395 line_ptr += bytes_read;
5396 add_include_dir (lh, cur_dir);
5397 }
5398 line_ptr += bytes_read;
5399
5400 /* Read file name table */
5401 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
5402 {
5403 unsigned int dir_index, mod_time, length;
5404
5405 line_ptr += bytes_read;
5406 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5407 line_ptr += bytes_read;
5408 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5409 line_ptr += bytes_read;
5410 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5411 line_ptr += bytes_read;
5412
5413 add_file_name (lh, cur_file, dir_index, mod_time, length);
5414 }
5415 line_ptr += bytes_read;
5416 lh->statement_program_start = line_ptr;
5417
5418 if (line_ptr > dwarf_line_buffer + dwarf_line_size)
5419 complaint (&symfile_complaints,
5420 "line number info header doesn't fit in `.debug_line' section");
5421
5422 discard_cleanups (back_to);
5423 return lh;
5424 }
5425
5426 /* This function exists to work around a bug in certain compilers
5427 (particularly GCC 2.95), in which the first line number marker of a
5428 function does not show up until after the prologue, right before
5429 the second line number marker. This function shifts ADDRESS down
5430 to the beginning of the function if necessary, and is called on
5431 addresses passed to record_line. */
5432
5433 static CORE_ADDR
5434 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
5435 {
5436 struct function_range *fn;
5437
5438 /* Find the function_range containing address. */
5439 if (!cu->first_fn)
5440 return address;
5441
5442 if (!cu->cached_fn)
5443 cu->cached_fn = cu->first_fn;
5444
5445 fn = cu->cached_fn;
5446 while (fn)
5447 if (fn->lowpc <= address && fn->highpc > address)
5448 goto found;
5449 else
5450 fn = fn->next;
5451
5452 fn = cu->first_fn;
5453 while (fn && fn != cu->cached_fn)
5454 if (fn->lowpc <= address && fn->highpc > address)
5455 goto found;
5456 else
5457 fn = fn->next;
5458
5459 return address;
5460
5461 found:
5462 if (fn->seen_line)
5463 return address;
5464 if (address != fn->lowpc)
5465 complaint (&symfile_complaints,
5466 "misplaced first line number at 0x%lx for '%s'",
5467 (unsigned long) address, fn->name);
5468 fn->seen_line = 1;
5469 return fn->lowpc;
5470 }
5471
5472 /* Decode the line number information for the compilation unit whose
5473 line number info is at OFFSET in the .debug_line section.
5474 The compilation directory of the file is passed in COMP_DIR. */
5475
5476 static void
5477 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
5478 struct dwarf2_cu *cu)
5479 {
5480 char *line_ptr;
5481 char *line_end;
5482 unsigned int bytes_read;
5483 unsigned char op_code, extended_op, adj_opcode;
5484 CORE_ADDR baseaddr;
5485 struct objfile *objfile = cu->objfile;
5486
5487 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5488
5489 line_ptr = lh->statement_program_start;
5490 line_end = lh->statement_program_end;
5491
5492 /* Read the statement sequences until there's nothing left. */
5493 while (line_ptr < line_end)
5494 {
5495 /* state machine registers */
5496 CORE_ADDR address = 0;
5497 unsigned int file = 1;
5498 unsigned int line = 1;
5499 unsigned int column = 0;
5500 int is_stmt = lh->default_is_stmt;
5501 int basic_block = 0;
5502 int end_sequence = 0;
5503
5504 /* Start a subfile for the current file of the state machine. */
5505 if (lh->num_file_names >= file)
5506 {
5507 /* lh->include_dirs and lh->file_names are 0-based, but the
5508 directory and file name numbers in the statement program
5509 are 1-based. */
5510 struct file_entry *fe = &lh->file_names[file - 1];
5511 char *dir;
5512 if (fe->dir_index)
5513 dir = lh->include_dirs[fe->dir_index - 1];
5514 else
5515 dir = comp_dir;
5516 dwarf2_start_subfile (fe->name, dir);
5517 }
5518
5519 /* Decode the table. */
5520 while (!end_sequence)
5521 {
5522 op_code = read_1_byte (abfd, line_ptr);
5523 line_ptr += 1;
5524
5525 if (op_code >= lh->opcode_base)
5526 { /* Special operand. */
5527 adj_opcode = op_code - lh->opcode_base;
5528 address += (adj_opcode / lh->line_range)
5529 * lh->minimum_instruction_length;
5530 line += lh->line_base + (adj_opcode % lh->line_range);
5531 /* append row to matrix using current values */
5532 record_line (current_subfile, line,
5533 check_cu_functions (address, cu));
5534 basic_block = 1;
5535 }
5536 else switch (op_code)
5537 {
5538 case DW_LNS_extended_op:
5539 line_ptr += 1; /* ignore length */
5540 extended_op = read_1_byte (abfd, line_ptr);
5541 line_ptr += 1;
5542 switch (extended_op)
5543 {
5544 case DW_LNE_end_sequence:
5545 end_sequence = 1;
5546 record_line (current_subfile, 0, address);
5547 break;
5548 case DW_LNE_set_address:
5549 address = read_address (abfd, line_ptr, cu, &bytes_read);
5550 line_ptr += bytes_read;
5551 address += baseaddr;
5552 break;
5553 case DW_LNE_define_file:
5554 {
5555 char *cur_file;
5556 unsigned int dir_index, mod_time, length;
5557
5558 cur_file = read_string (abfd, line_ptr, &bytes_read);
5559 line_ptr += bytes_read;
5560 dir_index =
5561 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5562 line_ptr += bytes_read;
5563 mod_time =
5564 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5565 line_ptr += bytes_read;
5566 length =
5567 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5568 line_ptr += bytes_read;
5569 add_file_name (lh, cur_file, dir_index, mod_time, length);
5570 }
5571 break;
5572 default:
5573 complaint (&symfile_complaints,
5574 "mangled .debug_line section");
5575 return;
5576 }
5577 break;
5578 case DW_LNS_copy:
5579 record_line (current_subfile, line,
5580 check_cu_functions (address, cu));
5581 basic_block = 0;
5582 break;
5583 case DW_LNS_advance_pc:
5584 address += lh->minimum_instruction_length
5585 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5586 line_ptr += bytes_read;
5587 break;
5588 case DW_LNS_advance_line:
5589 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
5590 line_ptr += bytes_read;
5591 break;
5592 case DW_LNS_set_file:
5593 {
5594 /* lh->include_dirs and lh->file_names are 0-based,
5595 but the directory and file name numbers in the
5596 statement program are 1-based. */
5597 struct file_entry *fe;
5598 char *dir;
5599 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5600 line_ptr += bytes_read;
5601 fe = &lh->file_names[file - 1];
5602 if (fe->dir_index)
5603 dir = lh->include_dirs[fe->dir_index - 1];
5604 else
5605 dir = comp_dir;
5606 dwarf2_start_subfile (fe->name, dir);
5607 }
5608 break;
5609 case DW_LNS_set_column:
5610 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5611 line_ptr += bytes_read;
5612 break;
5613 case DW_LNS_negate_stmt:
5614 is_stmt = (!is_stmt);
5615 break;
5616 case DW_LNS_set_basic_block:
5617 basic_block = 1;
5618 break;
5619 /* Add to the address register of the state machine the
5620 address increment value corresponding to special opcode
5621 255. Ie, this value is scaled by the minimum instruction
5622 length since special opcode 255 would have scaled the
5623 the increment. */
5624 case DW_LNS_const_add_pc:
5625 address += (lh->minimum_instruction_length
5626 * ((255 - lh->opcode_base) / lh->line_range));
5627 break;
5628 case DW_LNS_fixed_advance_pc:
5629 address += read_2_bytes (abfd, line_ptr);
5630 line_ptr += 2;
5631 break;
5632 default:
5633 { /* Unknown standard opcode, ignore it. */
5634 int i;
5635 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
5636 {
5637 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5638 line_ptr += bytes_read;
5639 }
5640 }
5641 }
5642 }
5643 }
5644 }
5645
5646 /* Start a subfile for DWARF. FILENAME is the name of the file and
5647 DIRNAME the name of the source directory which contains FILENAME
5648 or NULL if not known.
5649 This routine tries to keep line numbers from identical absolute and
5650 relative file names in a common subfile.
5651
5652 Using the `list' example from the GDB testsuite, which resides in
5653 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
5654 of /srcdir/list0.c yields the following debugging information for list0.c:
5655
5656 DW_AT_name: /srcdir/list0.c
5657 DW_AT_comp_dir: /compdir
5658 files.files[0].name: list0.h
5659 files.files[0].dir: /srcdir
5660 files.files[1].name: list0.c
5661 files.files[1].dir: /srcdir
5662
5663 The line number information for list0.c has to end up in a single
5664 subfile, so that `break /srcdir/list0.c:1' works as expected. */
5665
5666 static void
5667 dwarf2_start_subfile (char *filename, char *dirname)
5668 {
5669 /* If the filename isn't absolute, try to match an existing subfile
5670 with the full pathname. */
5671
5672 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
5673 {
5674 struct subfile *subfile;
5675 char *fullname = concat (dirname, "/", filename, NULL);
5676
5677 for (subfile = subfiles; subfile; subfile = subfile->next)
5678 {
5679 if (FILENAME_CMP (subfile->name, fullname) == 0)
5680 {
5681 current_subfile = subfile;
5682 xfree (fullname);
5683 return;
5684 }
5685 }
5686 xfree (fullname);
5687 }
5688 start_subfile (filename, dirname);
5689 }
5690
5691 static void
5692 var_decode_location (struct attribute *attr, struct symbol *sym,
5693 struct dwarf2_cu *cu)
5694 {
5695 struct objfile *objfile = cu->objfile;
5696 struct comp_unit_head *cu_header = &cu->header;
5697
5698 /* NOTE drow/2003-01-30: There used to be a comment and some special
5699 code here to turn a symbol with DW_AT_external and a
5700 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
5701 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
5702 with some versions of binutils) where shared libraries could have
5703 relocations against symbols in their debug information - the
5704 minimal symbol would have the right address, but the debug info
5705 would not. It's no longer necessary, because we will explicitly
5706 apply relocations when we read in the debug information now. */
5707
5708 /* A DW_AT_location attribute with no contents indicates that a
5709 variable has been optimized away. */
5710 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
5711 {
5712 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
5713 return;
5714 }
5715
5716 /* Handle one degenerate form of location expression specially, to
5717 preserve GDB's previous behavior when section offsets are
5718 specified. If this is just a DW_OP_addr then mark this symbol
5719 as LOC_STATIC. */
5720
5721 if (attr_form_is_block (attr)
5722 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
5723 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
5724 {
5725 int dummy;
5726
5727 SYMBOL_VALUE_ADDRESS (sym) =
5728 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
5729 fixup_symbol_section (sym, objfile);
5730 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
5731 SYMBOL_SECTION (sym));
5732 SYMBOL_CLASS (sym) = LOC_STATIC;
5733 return;
5734 }
5735
5736 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
5737 expression evaluator, and use LOC_COMPUTED only when necessary
5738 (i.e. when the value of a register or memory location is
5739 referenced, or a thread-local block, etc.). Then again, it might
5740 not be worthwhile. I'm assuming that it isn't unless performance
5741 or memory numbers show me otherwise. */
5742
5743 dwarf2_symbol_mark_computed (attr, sym, cu);
5744 SYMBOL_CLASS (sym) = LOC_COMPUTED;
5745 }
5746
5747 /* Given a pointer to a DWARF information entry, figure out if we need
5748 to make a symbol table entry for it, and if so, create a new entry
5749 and return a pointer to it.
5750 If TYPE is NULL, determine symbol type from the die, otherwise
5751 used the passed type. */
5752
5753 static struct symbol *
5754 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
5755 {
5756 struct objfile *objfile = cu->objfile;
5757 struct symbol *sym = NULL;
5758 char *name;
5759 struct attribute *attr = NULL;
5760 struct attribute *attr2 = NULL;
5761 CORE_ADDR baseaddr;
5762
5763 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5764
5765 if (die->tag != DW_TAG_namespace)
5766 name = dwarf2_linkage_name (die, cu);
5767 else
5768 name = TYPE_NAME (type);
5769
5770 if (name)
5771 {
5772 sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
5773 sizeof (struct symbol));
5774 OBJSTAT (objfile, n_syms++);
5775 memset (sym, 0, sizeof (struct symbol));
5776
5777 /* Cache this symbol's name and the name's demangled form (if any). */
5778 SYMBOL_LANGUAGE (sym) = cu->language;
5779 SYMBOL_SET_NAMES (sym, name, strlen (name), objfile);
5780
5781 /* Default assumptions.
5782 Use the passed type or decode it from the die. */
5783 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
5784 SYMBOL_CLASS (sym) = LOC_STATIC;
5785 if (type != NULL)
5786 SYMBOL_TYPE (sym) = type;
5787 else
5788 SYMBOL_TYPE (sym) = die_type (die, cu);
5789 attr = dwarf2_attr (die, DW_AT_decl_line, cu);
5790 if (attr)
5791 {
5792 SYMBOL_LINE (sym) = DW_UNSND (attr);
5793 }
5794 switch (die->tag)
5795 {
5796 case DW_TAG_label:
5797 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5798 if (attr)
5799 {
5800 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
5801 }
5802 SYMBOL_CLASS (sym) = LOC_LABEL;
5803 break;
5804 case DW_TAG_subprogram:
5805 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
5806 finish_block. */
5807 SYMBOL_CLASS (sym) = LOC_BLOCK;
5808 attr2 = dwarf2_attr (die, DW_AT_external, cu);
5809 if (attr2 && (DW_UNSND (attr2) != 0))
5810 {
5811 add_symbol_to_list (sym, &global_symbols);
5812 }
5813 else
5814 {
5815 add_symbol_to_list (sym, cu->list_in_scope);
5816 }
5817 break;
5818 case DW_TAG_variable:
5819 /* Compilation with minimal debug info may result in variables
5820 with missing type entries. Change the misleading `void' type
5821 to something sensible. */
5822 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
5823 SYMBOL_TYPE (sym) = init_type (TYPE_CODE_INT,
5824 TARGET_INT_BIT / HOST_CHAR_BIT, 0,
5825 "<variable, no debug info>",
5826 objfile);
5827 attr = dwarf2_attr (die, DW_AT_const_value, cu);
5828 if (attr)
5829 {
5830 dwarf2_const_value (attr, sym, cu);
5831 attr2 = dwarf2_attr (die, DW_AT_external, cu);
5832 if (attr2 && (DW_UNSND (attr2) != 0))
5833 add_symbol_to_list (sym, &global_symbols);
5834 else
5835 add_symbol_to_list (sym, cu->list_in_scope);
5836 break;
5837 }
5838 attr = dwarf2_attr (die, DW_AT_location, cu);
5839 if (attr)
5840 {
5841 var_decode_location (attr, sym, cu);
5842 attr2 = dwarf2_attr (die, DW_AT_external, cu);
5843 if (attr2 && (DW_UNSND (attr2) != 0))
5844 add_symbol_to_list (sym, &global_symbols);
5845 else
5846 add_symbol_to_list (sym, cu->list_in_scope);
5847 }
5848 else
5849 {
5850 /* We do not know the address of this symbol.
5851 If it is an external symbol and we have type information
5852 for it, enter the symbol as a LOC_UNRESOLVED symbol.
5853 The address of the variable will then be determined from
5854 the minimal symbol table whenever the variable is
5855 referenced. */
5856 attr2 = dwarf2_attr (die, DW_AT_external, cu);
5857 if (attr2 && (DW_UNSND (attr2) != 0)
5858 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
5859 {
5860 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
5861 add_symbol_to_list (sym, &global_symbols);
5862 }
5863 }
5864 break;
5865 case DW_TAG_formal_parameter:
5866 attr = dwarf2_attr (die, DW_AT_location, cu);
5867 if (attr)
5868 {
5869 var_decode_location (attr, sym, cu);
5870 /* FIXME drow/2003-07-31: Is LOC_COMPUTED_ARG necessary? */
5871 if (SYMBOL_CLASS (sym) == LOC_COMPUTED)
5872 SYMBOL_CLASS (sym) = LOC_COMPUTED_ARG;
5873 }
5874 attr = dwarf2_attr (die, DW_AT_const_value, cu);
5875 if (attr)
5876 {
5877 dwarf2_const_value (attr, sym, cu);
5878 }
5879 add_symbol_to_list (sym, cu->list_in_scope);
5880 break;
5881 case DW_TAG_unspecified_parameters:
5882 /* From varargs functions; gdb doesn't seem to have any
5883 interest in this information, so just ignore it for now.
5884 (FIXME?) */
5885 break;
5886 case DW_TAG_class_type:
5887 case DW_TAG_structure_type:
5888 case DW_TAG_union_type:
5889 case DW_TAG_enumeration_type:
5890 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5891 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5892
5893 /* Make sure that the symbol includes appropriate enclosing
5894 classes/namespaces in its name. These are calculated in
5895 read_structure_type, and the correct name is saved in
5896 the type. */
5897
5898 if (cu->language == language_cplus)
5899 {
5900 struct type *type = SYMBOL_TYPE (sym);
5901
5902 if (TYPE_TAG_NAME (type) != NULL)
5903 {
5904 /* FIXME: carlton/2003-11-10: Should this use
5905 SYMBOL_SET_NAMES instead? (The same problem also
5906 arises further down in this function.) */
5907 /* The type's name is already allocated along with
5908 this objfile, so we don't need to duplicate it
5909 for the symbol. */
5910 SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
5911 }
5912 }
5913
5914 {
5915 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
5916 really ever be static objects: otherwise, if you try
5917 to, say, break of a class's method and you're in a file
5918 which doesn't mention that class, it won't work unless
5919 the check for all static symbols in lookup_symbol_aux
5920 saves you. See the OtherFileClass tests in
5921 gdb.c++/namespace.exp. */
5922
5923 struct pending **list_to_add;
5924
5925 list_to_add = (cu->list_in_scope == &file_symbols
5926 && cu->language == language_cplus
5927 ? &global_symbols : cu->list_in_scope);
5928
5929 add_symbol_to_list (sym, list_to_add);
5930
5931 /* The semantics of C++ state that "struct foo { ... }" also
5932 defines a typedef for "foo". Synthesize a typedef symbol so
5933 that "ptype foo" works as expected. */
5934 if (cu->language == language_cplus)
5935 {
5936 struct symbol *typedef_sym = (struct symbol *)
5937 obstack_alloc (&objfile->objfile_obstack,
5938 sizeof (struct symbol));
5939 *typedef_sym = *sym;
5940 SYMBOL_DOMAIN (typedef_sym) = VAR_DOMAIN;
5941 /* The symbol's name is already allocated along with
5942 this objfile, so we don't need to duplicate it for
5943 the type. */
5944 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
5945 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_NATURAL_NAME (sym);
5946 add_symbol_to_list (typedef_sym, list_to_add);
5947 }
5948 }
5949 break;
5950 case DW_TAG_typedef:
5951 if (processing_has_namespace_info
5952 && processing_current_prefix[0] != '\0')
5953 {
5954 SYMBOL_LINKAGE_NAME (sym) = obconcat (&objfile->objfile_obstack,
5955 processing_current_prefix,
5956 "::",
5957 name);
5958 }
5959 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5960 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
5961 add_symbol_to_list (sym, cu->list_in_scope);
5962 break;
5963 case DW_TAG_base_type:
5964 case DW_TAG_subrange_type:
5965 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5966 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
5967 add_symbol_to_list (sym, cu->list_in_scope);
5968 break;
5969 case DW_TAG_enumerator:
5970 if (processing_has_namespace_info
5971 && processing_current_prefix[0] != '\0')
5972 {
5973 SYMBOL_LINKAGE_NAME (sym) = obconcat (&objfile->objfile_obstack,
5974 processing_current_prefix,
5975 "::",
5976 name);
5977 }
5978 attr = dwarf2_attr (die, DW_AT_const_value, cu);
5979 if (attr)
5980 {
5981 dwarf2_const_value (attr, sym, cu);
5982 }
5983 {
5984 /* NOTE: carlton/2003-11-10: See comment above in the
5985 DW_TAG_class_type, etc. block. */
5986
5987 struct pending **list_to_add;
5988
5989 list_to_add = (cu->list_in_scope == &file_symbols
5990 && cu->language == language_cplus
5991 ? &global_symbols : cu->list_in_scope);
5992
5993 add_symbol_to_list (sym, list_to_add);
5994 }
5995 break;
5996 case DW_TAG_namespace:
5997 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5998 add_symbol_to_list (sym, &global_symbols);
5999 break;
6000 default:
6001 /* Not a tag we recognize. Hopefully we aren't processing
6002 trash data, but since we must specifically ignore things
6003 we don't recognize, there is nothing else we should do at
6004 this point. */
6005 complaint (&symfile_complaints, "unsupported tag: '%s'",
6006 dwarf_tag_name (die->tag));
6007 break;
6008 }
6009 }
6010 return (sym);
6011 }
6012
6013 /* Copy constant value from an attribute to a symbol. */
6014
6015 static void
6016 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
6017 struct dwarf2_cu *cu)
6018 {
6019 struct objfile *objfile = cu->objfile;
6020 struct comp_unit_head *cu_header = &cu->header;
6021 struct dwarf_block *blk;
6022
6023 switch (attr->form)
6024 {
6025 case DW_FORM_addr:
6026 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
6027 dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
6028 cu_header->addr_size,
6029 TYPE_LENGTH (SYMBOL_TYPE
6030 (sym)));
6031 SYMBOL_VALUE_BYTES (sym) = (char *)
6032 obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
6033 /* NOTE: cagney/2003-05-09: In-lined store_address call with
6034 it's body - store_unsigned_integer. */
6035 store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
6036 DW_ADDR (attr));
6037 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
6038 break;
6039 case DW_FORM_block1:
6040 case DW_FORM_block2:
6041 case DW_FORM_block4:
6042 case DW_FORM_block:
6043 blk = DW_BLOCK (attr);
6044 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
6045 dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
6046 blk->size,
6047 TYPE_LENGTH (SYMBOL_TYPE
6048 (sym)));
6049 SYMBOL_VALUE_BYTES (sym) = (char *)
6050 obstack_alloc (&objfile->objfile_obstack, blk->size);
6051 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
6052 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
6053 break;
6054
6055 /* The DW_AT_const_value attributes are supposed to carry the
6056 symbol's value "represented as it would be on the target
6057 architecture." By the time we get here, it's already been
6058 converted to host endianness, so we just need to sign- or
6059 zero-extend it as appropriate. */
6060 case DW_FORM_data1:
6061 dwarf2_const_value_data (attr, sym, 8);
6062 break;
6063 case DW_FORM_data2:
6064 dwarf2_const_value_data (attr, sym, 16);
6065 break;
6066 case DW_FORM_data4:
6067 dwarf2_const_value_data (attr, sym, 32);
6068 break;
6069 case DW_FORM_data8:
6070 dwarf2_const_value_data (attr, sym, 64);
6071 break;
6072
6073 case DW_FORM_sdata:
6074 SYMBOL_VALUE (sym) = DW_SND (attr);
6075 SYMBOL_CLASS (sym) = LOC_CONST;
6076 break;
6077
6078 case DW_FORM_udata:
6079 SYMBOL_VALUE (sym) = DW_UNSND (attr);
6080 SYMBOL_CLASS (sym) = LOC_CONST;
6081 break;
6082
6083 default:
6084 complaint (&symfile_complaints,
6085 "unsupported const value attribute form: '%s'",
6086 dwarf_form_name (attr->form));
6087 SYMBOL_VALUE (sym) = 0;
6088 SYMBOL_CLASS (sym) = LOC_CONST;
6089 break;
6090 }
6091 }
6092
6093
6094 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
6095 or zero-extend it as appropriate for the symbol's type. */
6096 static void
6097 dwarf2_const_value_data (struct attribute *attr,
6098 struct symbol *sym,
6099 int bits)
6100 {
6101 LONGEST l = DW_UNSND (attr);
6102
6103 if (bits < sizeof (l) * 8)
6104 {
6105 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
6106 l &= ((LONGEST) 1 << bits) - 1;
6107 else
6108 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
6109 }
6110
6111 SYMBOL_VALUE (sym) = l;
6112 SYMBOL_CLASS (sym) = LOC_CONST;
6113 }
6114
6115
6116 /* Return the type of the die in question using its DW_AT_type attribute. */
6117
6118 static struct type *
6119 die_type (struct die_info *die, struct dwarf2_cu *cu)
6120 {
6121 struct type *type;
6122 struct attribute *type_attr;
6123 struct die_info *type_die;
6124 unsigned int ref;
6125
6126 type_attr = dwarf2_attr (die, DW_AT_type, cu);
6127 if (!type_attr)
6128 {
6129 /* A missing DW_AT_type represents a void type. */
6130 return dwarf2_fundamental_type (cu->objfile, FT_VOID, cu);
6131 }
6132 else
6133 {
6134 ref = dwarf2_get_ref_die_offset (type_attr, cu);
6135 type_die = follow_die_ref (ref);
6136 if (!type_die)
6137 {
6138 error ("Dwarf Error: Cannot find referent at offset %d [in module %s]",
6139 ref, cu->objfile->name);
6140 return NULL;
6141 }
6142 }
6143 type = tag_type_to_type (type_die, cu);
6144 if (!type)
6145 {
6146 dump_die (type_die);
6147 error ("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]",
6148 cu->objfile->name);
6149 }
6150 return type;
6151 }
6152
6153 /* Return the containing type of the die in question using its
6154 DW_AT_containing_type attribute. */
6155
6156 static struct type *
6157 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
6158 {
6159 struct type *type = NULL;
6160 struct attribute *type_attr;
6161 struct die_info *type_die = NULL;
6162 unsigned int ref;
6163
6164 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
6165 if (type_attr)
6166 {
6167 ref = dwarf2_get_ref_die_offset (type_attr, cu);
6168 type_die = follow_die_ref (ref);
6169 if (!type_die)
6170 {
6171 error ("Dwarf Error: Cannot find referent at offset %d [in module %s]", ref,
6172 cu->objfile->name);
6173 return NULL;
6174 }
6175 type = tag_type_to_type (type_die, cu);
6176 }
6177 if (!type)
6178 {
6179 if (type_die)
6180 dump_die (type_die);
6181 error ("Dwarf Error: Problem turning containing type into gdb type [in module %s]",
6182 cu->objfile->name);
6183 }
6184 return type;
6185 }
6186
6187 #if 0
6188 static struct type *
6189 type_at_offset (unsigned int offset, struct dwarf2_cu *cu)
6190 {
6191 struct die_info *die;
6192 struct type *type;
6193
6194 die = follow_die_ref (offset);
6195 if (!die)
6196 {
6197 error ("Dwarf Error: Cannot find type referent at offset %d.", offset);
6198 return NULL;
6199 }
6200 type = tag_type_to_type (die, cu);
6201 return type;
6202 }
6203 #endif
6204
6205 static struct type *
6206 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
6207 {
6208 if (die->type)
6209 {
6210 return die->type;
6211 }
6212 else
6213 {
6214 read_type_die (die, cu);
6215 if (!die->type)
6216 {
6217 dump_die (die);
6218 error ("Dwarf Error: Cannot find type of die [in module %s]",
6219 cu->objfile->name);
6220 }
6221 return die->type;
6222 }
6223 }
6224
6225 static void
6226 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
6227 {
6228 char *prefix = determine_prefix (die, cu);
6229 const char *old_prefix = processing_current_prefix;
6230 struct cleanup *back_to = make_cleanup (xfree, prefix);
6231 processing_current_prefix = prefix;
6232
6233 switch (die->tag)
6234 {
6235 case DW_TAG_class_type:
6236 case DW_TAG_structure_type:
6237 case DW_TAG_union_type:
6238 read_structure_type (die, cu);
6239 break;
6240 case DW_TAG_enumeration_type:
6241 read_enumeration_type (die, cu);
6242 break;
6243 case DW_TAG_subprogram:
6244 case DW_TAG_subroutine_type:
6245 read_subroutine_type (die, cu);
6246 break;
6247 case DW_TAG_array_type:
6248 read_array_type (die, cu);
6249 break;
6250 case DW_TAG_pointer_type:
6251 read_tag_pointer_type (die, cu);
6252 break;
6253 case DW_TAG_ptr_to_member_type:
6254 read_tag_ptr_to_member_type (die, cu);
6255 break;
6256 case DW_TAG_reference_type:
6257 read_tag_reference_type (die, cu);
6258 break;
6259 case DW_TAG_const_type:
6260 read_tag_const_type (die, cu);
6261 break;
6262 case DW_TAG_volatile_type:
6263 read_tag_volatile_type (die, cu);
6264 break;
6265 case DW_TAG_string_type:
6266 read_tag_string_type (die, cu);
6267 break;
6268 case DW_TAG_typedef:
6269 read_typedef (die, cu);
6270 break;
6271 case DW_TAG_subrange_type:
6272 read_subrange_type (die, cu);
6273 break;
6274 case DW_TAG_base_type:
6275 read_base_type (die, cu);
6276 break;
6277 default:
6278 complaint (&symfile_complaints, "unexepected tag in read_type_die: '%s'",
6279 dwarf_tag_name (die->tag));
6280 break;
6281 }
6282
6283 processing_current_prefix = old_prefix;
6284 do_cleanups (back_to);
6285 }
6286
6287 /* Return the name of the namespace/class that DIE is defined within,
6288 or "" if we can't tell. The caller should xfree the result. */
6289
6290 /* NOTE: carlton/2004-01-23: See read_func_scope (and the comment
6291 therein) for an example of how to use this function to deal with
6292 DW_AT_specification. */
6293
6294 static char *
6295 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
6296 {
6297 struct die_info *parent;
6298
6299 if (cu->language != language_cplus)
6300 return NULL;
6301
6302 parent = die->parent;
6303
6304 if (parent == NULL)
6305 {
6306 return xstrdup ("");
6307 }
6308 else
6309 {
6310 switch (parent->tag) {
6311 case DW_TAG_namespace:
6312 {
6313 /* FIXME: carlton/2004-03-05: Should I follow extension dies
6314 before doing this check? */
6315 if (parent->type != NULL && TYPE_TAG_NAME (parent->type) != NULL)
6316 {
6317 return xstrdup (TYPE_TAG_NAME (parent->type));
6318 }
6319 else
6320 {
6321 int dummy;
6322 char *parent_prefix = determine_prefix (parent, cu);
6323 char *retval = typename_concat (parent_prefix,
6324 namespace_name (parent, &dummy,
6325 cu));
6326 xfree (parent_prefix);
6327 return retval;
6328 }
6329 }
6330 break;
6331 case DW_TAG_class_type:
6332 case DW_TAG_structure_type:
6333 {
6334 if (parent->type != NULL && TYPE_TAG_NAME (parent->type) != NULL)
6335 {
6336 return xstrdup (TYPE_TAG_NAME (parent->type));
6337 }
6338 else
6339 {
6340 const char *old_prefix = processing_current_prefix;
6341 char *new_prefix = determine_prefix (parent, cu);
6342 char *retval;
6343
6344 processing_current_prefix = new_prefix;
6345 retval = determine_class_name (parent, cu);
6346 processing_current_prefix = old_prefix;
6347
6348 xfree (new_prefix);
6349 return retval;
6350 }
6351 }
6352 default:
6353 return determine_prefix (parent, cu);
6354 }
6355 }
6356 }
6357
6358 /* Return a newly-allocated string formed by concatenating PREFIX,
6359 "::", and SUFFIX, except that if PREFIX is NULL or the empty
6360 string, just return a copy of SUFFIX. */
6361
6362 static char *
6363 typename_concat (const char *prefix, const char *suffix)
6364 {
6365 if (prefix == NULL || prefix[0] == '\0')
6366 return xstrdup (suffix);
6367 else
6368 {
6369 char *retval = xmalloc (strlen (prefix) + 2 + strlen (suffix) + 1);
6370
6371 strcpy (retval, prefix);
6372 strcat (retval, "::");
6373 strcat (retval, suffix);
6374
6375 return retval;
6376 }
6377 }
6378
6379 static struct type *
6380 dwarf_base_type (int encoding, int size, struct dwarf2_cu *cu)
6381 {
6382 struct objfile *objfile = cu->objfile;
6383
6384 /* FIXME - this should not produce a new (struct type *)
6385 every time. It should cache base types. */
6386 struct type *type;
6387 switch (encoding)
6388 {
6389 case DW_ATE_address:
6390 type = dwarf2_fundamental_type (objfile, FT_VOID, cu);
6391 return type;
6392 case DW_ATE_boolean:
6393 type = dwarf2_fundamental_type (objfile, FT_BOOLEAN, cu);
6394 return type;
6395 case DW_ATE_complex_float:
6396 if (size == 16)
6397 {
6398 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_COMPLEX, cu);
6399 }
6400 else
6401 {
6402 type = dwarf2_fundamental_type (objfile, FT_COMPLEX, cu);
6403 }
6404 return type;
6405 case DW_ATE_float:
6406 if (size == 8)
6407 {
6408 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT, cu);
6409 }
6410 else
6411 {
6412 type = dwarf2_fundamental_type (objfile, FT_FLOAT, cu);
6413 }
6414 return type;
6415 case DW_ATE_signed:
6416 switch (size)
6417 {
6418 case 1:
6419 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR, cu);
6420 break;
6421 case 2:
6422 type = dwarf2_fundamental_type (objfile, FT_SIGNED_SHORT, cu);
6423 break;
6424 default:
6425 case 4:
6426 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER, cu);
6427 break;
6428 }
6429 return type;
6430 case DW_ATE_signed_char:
6431 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR, cu);
6432 return type;
6433 case DW_ATE_unsigned:
6434 switch (size)
6435 {
6436 case 1:
6437 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR, cu);
6438 break;
6439 case 2:
6440 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_SHORT, cu);
6441 break;
6442 default:
6443 case 4:
6444 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_INTEGER, cu);
6445 break;
6446 }
6447 return type;
6448 case DW_ATE_unsigned_char:
6449 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR, cu);
6450 return type;
6451 default:
6452 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER, cu);
6453 return type;
6454 }
6455 }
6456
6457 #if 0
6458 struct die_info *
6459 copy_die (struct die_info *old_die)
6460 {
6461 struct die_info *new_die;
6462 int i, num_attrs;
6463
6464 new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
6465 memset (new_die, 0, sizeof (struct die_info));
6466
6467 new_die->tag = old_die->tag;
6468 new_die->has_children = old_die->has_children;
6469 new_die->abbrev = old_die->abbrev;
6470 new_die->offset = old_die->offset;
6471 new_die->type = NULL;
6472
6473 num_attrs = old_die->num_attrs;
6474 new_die->num_attrs = num_attrs;
6475 new_die->attrs = (struct attribute *)
6476 xmalloc (num_attrs * sizeof (struct attribute));
6477
6478 for (i = 0; i < old_die->num_attrs; ++i)
6479 {
6480 new_die->attrs[i].name = old_die->attrs[i].name;
6481 new_die->attrs[i].form = old_die->attrs[i].form;
6482 new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
6483 }
6484
6485 new_die->next = NULL;
6486 return new_die;
6487 }
6488 #endif
6489
6490 /* Return sibling of die, NULL if no sibling. */
6491
6492 static struct die_info *
6493 sibling_die (struct die_info *die)
6494 {
6495 return die->sibling;
6496 }
6497
6498 /* Get linkage name of a die, return NULL if not found. */
6499
6500 static char *
6501 dwarf2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
6502 {
6503 struct attribute *attr;
6504
6505 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6506 if (attr && DW_STRING (attr))
6507 return DW_STRING (attr);
6508 attr = dwarf2_attr (die, DW_AT_name, cu);
6509 if (attr && DW_STRING (attr))
6510 return DW_STRING (attr);
6511 return NULL;
6512 }
6513
6514 /* Get name of a die, return NULL if not found. */
6515
6516 static char *
6517 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
6518 {
6519 struct attribute *attr;
6520
6521 attr = dwarf2_attr (die, DW_AT_name, cu);
6522 if (attr && DW_STRING (attr))
6523 return DW_STRING (attr);
6524 return NULL;
6525 }
6526
6527 /* Return the die that this die in an extension of, or NULL if there
6528 is none. */
6529
6530 static struct die_info *
6531 dwarf2_extension (struct die_info *die, struct dwarf2_cu *cu)
6532 {
6533 struct attribute *attr;
6534 struct die_info *extension_die;
6535 unsigned int ref;
6536
6537 attr = dwarf2_attr (die, DW_AT_extension, cu);
6538 if (attr == NULL)
6539 return NULL;
6540
6541 ref = dwarf2_get_ref_die_offset (attr, cu);
6542 extension_die = follow_die_ref (ref);
6543 if (!extension_die)
6544 {
6545 error ("Dwarf Error: Cannot find referent at offset %d.", ref);
6546 }
6547
6548 return extension_die;
6549 }
6550
6551 /* Convert a DIE tag into its string name. */
6552
6553 static char *
6554 dwarf_tag_name (unsigned tag)
6555 {
6556 switch (tag)
6557 {
6558 case DW_TAG_padding:
6559 return "DW_TAG_padding";
6560 case DW_TAG_array_type:
6561 return "DW_TAG_array_type";
6562 case DW_TAG_class_type:
6563 return "DW_TAG_class_type";
6564 case DW_TAG_entry_point:
6565 return "DW_TAG_entry_point";
6566 case DW_TAG_enumeration_type:
6567 return "DW_TAG_enumeration_type";
6568 case DW_TAG_formal_parameter:
6569 return "DW_TAG_formal_parameter";
6570 case DW_TAG_imported_declaration:
6571 return "DW_TAG_imported_declaration";
6572 case DW_TAG_label:
6573 return "DW_TAG_label";
6574 case DW_TAG_lexical_block:
6575 return "DW_TAG_lexical_block";
6576 case DW_TAG_member:
6577 return "DW_TAG_member";
6578 case DW_TAG_pointer_type:
6579 return "DW_TAG_pointer_type";
6580 case DW_TAG_reference_type:
6581 return "DW_TAG_reference_type";
6582 case DW_TAG_compile_unit:
6583 return "DW_TAG_compile_unit";
6584 case DW_TAG_string_type:
6585 return "DW_TAG_string_type";
6586 case DW_TAG_structure_type:
6587 return "DW_TAG_structure_type";
6588 case DW_TAG_subroutine_type:
6589 return "DW_TAG_subroutine_type";
6590 case DW_TAG_typedef:
6591 return "DW_TAG_typedef";
6592 case DW_TAG_union_type:
6593 return "DW_TAG_union_type";
6594 case DW_TAG_unspecified_parameters:
6595 return "DW_TAG_unspecified_parameters";
6596 case DW_TAG_variant:
6597 return "DW_TAG_variant";
6598 case DW_TAG_common_block:
6599 return "DW_TAG_common_block";
6600 case DW_TAG_common_inclusion:
6601 return "DW_TAG_common_inclusion";
6602 case DW_TAG_inheritance:
6603 return "DW_TAG_inheritance";
6604 case DW_TAG_inlined_subroutine:
6605 return "DW_TAG_inlined_subroutine";
6606 case DW_TAG_module:
6607 return "DW_TAG_module";
6608 case DW_TAG_ptr_to_member_type:
6609 return "DW_TAG_ptr_to_member_type";
6610 case DW_TAG_set_type:
6611 return "DW_TAG_set_type";
6612 case DW_TAG_subrange_type:
6613 return "DW_TAG_subrange_type";
6614 case DW_TAG_with_stmt:
6615 return "DW_TAG_with_stmt";
6616 case DW_TAG_access_declaration:
6617 return "DW_TAG_access_declaration";
6618 case DW_TAG_base_type:
6619 return "DW_TAG_base_type";
6620 case DW_TAG_catch_block:
6621 return "DW_TAG_catch_block";
6622 case DW_TAG_const_type:
6623 return "DW_TAG_const_type";
6624 case DW_TAG_constant:
6625 return "DW_TAG_constant";
6626 case DW_TAG_enumerator:
6627 return "DW_TAG_enumerator";
6628 case DW_TAG_file_type:
6629 return "DW_TAG_file_type";
6630 case DW_TAG_friend:
6631 return "DW_TAG_friend";
6632 case DW_TAG_namelist:
6633 return "DW_TAG_namelist";
6634 case DW_TAG_namelist_item:
6635 return "DW_TAG_namelist_item";
6636 case DW_TAG_packed_type:
6637 return "DW_TAG_packed_type";
6638 case DW_TAG_subprogram:
6639 return "DW_TAG_subprogram";
6640 case DW_TAG_template_type_param:
6641 return "DW_TAG_template_type_param";
6642 case DW_TAG_template_value_param:
6643 return "DW_TAG_template_value_param";
6644 case DW_TAG_thrown_type:
6645 return "DW_TAG_thrown_type";
6646 case DW_TAG_try_block:
6647 return "DW_TAG_try_block";
6648 case DW_TAG_variant_part:
6649 return "DW_TAG_variant_part";
6650 case DW_TAG_variable:
6651 return "DW_TAG_variable";
6652 case DW_TAG_volatile_type:
6653 return "DW_TAG_volatile_type";
6654 case DW_TAG_dwarf_procedure:
6655 return "DW_TAG_dwarf_procedure";
6656 case DW_TAG_restrict_type:
6657 return "DW_TAG_restrict_type";
6658 case DW_TAG_interface_type:
6659 return "DW_TAG_interface_type";
6660 case DW_TAG_namespace:
6661 return "DW_TAG_namespace";
6662 case DW_TAG_imported_module:
6663 return "DW_TAG_imported_module";
6664 case DW_TAG_unspecified_type:
6665 return "DW_TAG_unspecified_type";
6666 case DW_TAG_partial_unit:
6667 return "DW_TAG_partial_unit";
6668 case DW_TAG_imported_unit:
6669 return "DW_TAG_imported_unit";
6670 case DW_TAG_MIPS_loop:
6671 return "DW_TAG_MIPS_loop";
6672 case DW_TAG_format_label:
6673 return "DW_TAG_format_label";
6674 case DW_TAG_function_template:
6675 return "DW_TAG_function_template";
6676 case DW_TAG_class_template:
6677 return "DW_TAG_class_template";
6678 default:
6679 return "DW_TAG_<unknown>";
6680 }
6681 }
6682
6683 /* Convert a DWARF attribute code into its string name. */
6684
6685 static char *
6686 dwarf_attr_name (unsigned attr)
6687 {
6688 switch (attr)
6689 {
6690 case DW_AT_sibling:
6691 return "DW_AT_sibling";
6692 case DW_AT_location:
6693 return "DW_AT_location";
6694 case DW_AT_name:
6695 return "DW_AT_name";
6696 case DW_AT_ordering:
6697 return "DW_AT_ordering";
6698 case DW_AT_subscr_data:
6699 return "DW_AT_subscr_data";
6700 case DW_AT_byte_size:
6701 return "DW_AT_byte_size";
6702 case DW_AT_bit_offset:
6703 return "DW_AT_bit_offset";
6704 case DW_AT_bit_size:
6705 return "DW_AT_bit_size";
6706 case DW_AT_element_list:
6707 return "DW_AT_element_list";
6708 case DW_AT_stmt_list:
6709 return "DW_AT_stmt_list";
6710 case DW_AT_low_pc:
6711 return "DW_AT_low_pc";
6712 case DW_AT_high_pc:
6713 return "DW_AT_high_pc";
6714 case DW_AT_language:
6715 return "DW_AT_language";
6716 case DW_AT_member:
6717 return "DW_AT_member";
6718 case DW_AT_discr:
6719 return "DW_AT_discr";
6720 case DW_AT_discr_value:
6721 return "DW_AT_discr_value";
6722 case DW_AT_visibility:
6723 return "DW_AT_visibility";
6724 case DW_AT_import:
6725 return "DW_AT_import";
6726 case DW_AT_string_length:
6727 return "DW_AT_string_length";
6728 case DW_AT_common_reference:
6729 return "DW_AT_common_reference";
6730 case DW_AT_comp_dir:
6731 return "DW_AT_comp_dir";
6732 case DW_AT_const_value:
6733 return "DW_AT_const_value";
6734 case DW_AT_containing_type:
6735 return "DW_AT_containing_type";
6736 case DW_AT_default_value:
6737 return "DW_AT_default_value";
6738 case DW_AT_inline:
6739 return "DW_AT_inline";
6740 case DW_AT_is_optional:
6741 return "DW_AT_is_optional";
6742 case DW_AT_lower_bound:
6743 return "DW_AT_lower_bound";
6744 case DW_AT_producer:
6745 return "DW_AT_producer";
6746 case DW_AT_prototyped:
6747 return "DW_AT_prototyped";
6748 case DW_AT_return_addr:
6749 return "DW_AT_return_addr";
6750 case DW_AT_start_scope:
6751 return "DW_AT_start_scope";
6752 case DW_AT_stride_size:
6753 return "DW_AT_stride_size";
6754 case DW_AT_upper_bound:
6755 return "DW_AT_upper_bound";
6756 case DW_AT_abstract_origin:
6757 return "DW_AT_abstract_origin";
6758 case DW_AT_accessibility:
6759 return "DW_AT_accessibility";
6760 case DW_AT_address_class:
6761 return "DW_AT_address_class";
6762 case DW_AT_artificial:
6763 return "DW_AT_artificial";
6764 case DW_AT_base_types:
6765 return "DW_AT_base_types";
6766 case DW_AT_calling_convention:
6767 return "DW_AT_calling_convention";
6768 case DW_AT_count:
6769 return "DW_AT_count";
6770 case DW_AT_data_member_location:
6771 return "DW_AT_data_member_location";
6772 case DW_AT_decl_column:
6773 return "DW_AT_decl_column";
6774 case DW_AT_decl_file:
6775 return "DW_AT_decl_file";
6776 case DW_AT_decl_line:
6777 return "DW_AT_decl_line";
6778 case DW_AT_declaration:
6779 return "DW_AT_declaration";
6780 case DW_AT_discr_list:
6781 return "DW_AT_discr_list";
6782 case DW_AT_encoding:
6783 return "DW_AT_encoding";
6784 case DW_AT_external:
6785 return "DW_AT_external";
6786 case DW_AT_frame_base:
6787 return "DW_AT_frame_base";
6788 case DW_AT_friend:
6789 return "DW_AT_friend";
6790 case DW_AT_identifier_case:
6791 return "DW_AT_identifier_case";
6792 case DW_AT_macro_info:
6793 return "DW_AT_macro_info";
6794 case DW_AT_namelist_items:
6795 return "DW_AT_namelist_items";
6796 case DW_AT_priority:
6797 return "DW_AT_priority";
6798 case DW_AT_segment:
6799 return "DW_AT_segment";
6800 case DW_AT_specification:
6801 return "DW_AT_specification";
6802 case DW_AT_static_link:
6803 return "DW_AT_static_link";
6804 case DW_AT_type:
6805 return "DW_AT_type";
6806 case DW_AT_use_location:
6807 return "DW_AT_use_location";
6808 case DW_AT_variable_parameter:
6809 return "DW_AT_variable_parameter";
6810 case DW_AT_virtuality:
6811 return "DW_AT_virtuality";
6812 case DW_AT_vtable_elem_location:
6813 return "DW_AT_vtable_elem_location";
6814 case DW_AT_allocated:
6815 return "DW_AT_allocated";
6816 case DW_AT_associated:
6817 return "DW_AT_associated";
6818 case DW_AT_data_location:
6819 return "DW_AT_data_location";
6820 case DW_AT_stride:
6821 return "DW_AT_stride";
6822 case DW_AT_entry_pc:
6823 return "DW_AT_entry_pc";
6824 case DW_AT_use_UTF8:
6825 return "DW_AT_use_UTF8";
6826 case DW_AT_extension:
6827 return "DW_AT_extension";
6828 case DW_AT_ranges:
6829 return "DW_AT_ranges";
6830 case DW_AT_trampoline:
6831 return "DW_AT_trampoline";
6832 case DW_AT_call_column:
6833 return "DW_AT_call_column";
6834 case DW_AT_call_file:
6835 return "DW_AT_call_file";
6836 case DW_AT_call_line:
6837 return "DW_AT_call_line";
6838 #ifdef MIPS
6839 case DW_AT_MIPS_fde:
6840 return "DW_AT_MIPS_fde";
6841 case DW_AT_MIPS_loop_begin:
6842 return "DW_AT_MIPS_loop_begin";
6843 case DW_AT_MIPS_tail_loop_begin:
6844 return "DW_AT_MIPS_tail_loop_begin";
6845 case DW_AT_MIPS_epilog_begin:
6846 return "DW_AT_MIPS_epilog_begin";
6847 case DW_AT_MIPS_loop_unroll_factor:
6848 return "DW_AT_MIPS_loop_unroll_factor";
6849 case DW_AT_MIPS_software_pipeline_depth:
6850 return "DW_AT_MIPS_software_pipeline_depth";
6851 #endif
6852 case DW_AT_MIPS_linkage_name:
6853 return "DW_AT_MIPS_linkage_name";
6854
6855 case DW_AT_sf_names:
6856 return "DW_AT_sf_names";
6857 case DW_AT_src_info:
6858 return "DW_AT_src_info";
6859 case DW_AT_mac_info:
6860 return "DW_AT_mac_info";
6861 case DW_AT_src_coords:
6862 return "DW_AT_src_coords";
6863 case DW_AT_body_begin:
6864 return "DW_AT_body_begin";
6865 case DW_AT_body_end:
6866 return "DW_AT_body_end";
6867 case DW_AT_GNU_vector:
6868 return "DW_AT_GNU_vector";
6869 default:
6870 return "DW_AT_<unknown>";
6871 }
6872 }
6873
6874 /* Convert a DWARF value form code into its string name. */
6875
6876 static char *
6877 dwarf_form_name (unsigned form)
6878 {
6879 switch (form)
6880 {
6881 case DW_FORM_addr:
6882 return "DW_FORM_addr";
6883 case DW_FORM_block2:
6884 return "DW_FORM_block2";
6885 case DW_FORM_block4:
6886 return "DW_FORM_block4";
6887 case DW_FORM_data2:
6888 return "DW_FORM_data2";
6889 case DW_FORM_data4:
6890 return "DW_FORM_data4";
6891 case DW_FORM_data8:
6892 return "DW_FORM_data8";
6893 case DW_FORM_string:
6894 return "DW_FORM_string";
6895 case DW_FORM_block:
6896 return "DW_FORM_block";
6897 case DW_FORM_block1:
6898 return "DW_FORM_block1";
6899 case DW_FORM_data1:
6900 return "DW_FORM_data1";
6901 case DW_FORM_flag:
6902 return "DW_FORM_flag";
6903 case DW_FORM_sdata:
6904 return "DW_FORM_sdata";
6905 case DW_FORM_strp:
6906 return "DW_FORM_strp";
6907 case DW_FORM_udata:
6908 return "DW_FORM_udata";
6909 case DW_FORM_ref_addr:
6910 return "DW_FORM_ref_addr";
6911 case DW_FORM_ref1:
6912 return "DW_FORM_ref1";
6913 case DW_FORM_ref2:
6914 return "DW_FORM_ref2";
6915 case DW_FORM_ref4:
6916 return "DW_FORM_ref4";
6917 case DW_FORM_ref8:
6918 return "DW_FORM_ref8";
6919 case DW_FORM_ref_udata:
6920 return "DW_FORM_ref_udata";
6921 case DW_FORM_indirect:
6922 return "DW_FORM_indirect";
6923 default:
6924 return "DW_FORM_<unknown>";
6925 }
6926 }
6927
6928 /* Convert a DWARF stack opcode into its string name. */
6929
6930 static char *
6931 dwarf_stack_op_name (unsigned op)
6932 {
6933 switch (op)
6934 {
6935 case DW_OP_addr:
6936 return "DW_OP_addr";
6937 case DW_OP_deref:
6938 return "DW_OP_deref";
6939 case DW_OP_const1u:
6940 return "DW_OP_const1u";
6941 case DW_OP_const1s:
6942 return "DW_OP_const1s";
6943 case DW_OP_const2u:
6944 return "DW_OP_const2u";
6945 case DW_OP_const2s:
6946 return "DW_OP_const2s";
6947 case DW_OP_const4u:
6948 return "DW_OP_const4u";
6949 case DW_OP_const4s:
6950 return "DW_OP_const4s";
6951 case DW_OP_const8u:
6952 return "DW_OP_const8u";
6953 case DW_OP_const8s:
6954 return "DW_OP_const8s";
6955 case DW_OP_constu:
6956 return "DW_OP_constu";
6957 case DW_OP_consts:
6958 return "DW_OP_consts";
6959 case DW_OP_dup:
6960 return "DW_OP_dup";
6961 case DW_OP_drop:
6962 return "DW_OP_drop";
6963 case DW_OP_over:
6964 return "DW_OP_over";
6965 case DW_OP_pick:
6966 return "DW_OP_pick";
6967 case DW_OP_swap:
6968 return "DW_OP_swap";
6969 case DW_OP_rot:
6970 return "DW_OP_rot";
6971 case DW_OP_xderef:
6972 return "DW_OP_xderef";
6973 case DW_OP_abs:
6974 return "DW_OP_abs";
6975 case DW_OP_and:
6976 return "DW_OP_and";
6977 case DW_OP_div:
6978 return "DW_OP_div";
6979 case DW_OP_minus:
6980 return "DW_OP_minus";
6981 case DW_OP_mod:
6982 return "DW_OP_mod";
6983 case DW_OP_mul:
6984 return "DW_OP_mul";
6985 case DW_OP_neg:
6986 return "DW_OP_neg";
6987 case DW_OP_not:
6988 return "DW_OP_not";
6989 case DW_OP_or:
6990 return "DW_OP_or";
6991 case DW_OP_plus:
6992 return "DW_OP_plus";
6993 case DW_OP_plus_uconst:
6994 return "DW_OP_plus_uconst";
6995 case DW_OP_shl:
6996 return "DW_OP_shl";
6997 case DW_OP_shr:
6998 return "DW_OP_shr";
6999 case DW_OP_shra:
7000 return "DW_OP_shra";
7001 case DW_OP_xor:
7002 return "DW_OP_xor";
7003 case DW_OP_bra:
7004 return "DW_OP_bra";
7005 case DW_OP_eq:
7006 return "DW_OP_eq";
7007 case DW_OP_ge:
7008 return "DW_OP_ge";
7009 case DW_OP_gt:
7010 return "DW_OP_gt";
7011 case DW_OP_le:
7012 return "DW_OP_le";
7013 case DW_OP_lt:
7014 return "DW_OP_lt";
7015 case DW_OP_ne:
7016 return "DW_OP_ne";
7017 case DW_OP_skip:
7018 return "DW_OP_skip";
7019 case DW_OP_lit0:
7020 return "DW_OP_lit0";
7021 case DW_OP_lit1:
7022 return "DW_OP_lit1";
7023 case DW_OP_lit2:
7024 return "DW_OP_lit2";
7025 case DW_OP_lit3:
7026 return "DW_OP_lit3";
7027 case DW_OP_lit4:
7028 return "DW_OP_lit4";
7029 case DW_OP_lit5:
7030 return "DW_OP_lit5";
7031 case DW_OP_lit6:
7032 return "DW_OP_lit6";
7033 case DW_OP_lit7:
7034 return "DW_OP_lit7";
7035 case DW_OP_lit8:
7036 return "DW_OP_lit8";
7037 case DW_OP_lit9:
7038 return "DW_OP_lit9";
7039 case DW_OP_lit10:
7040 return "DW_OP_lit10";
7041 case DW_OP_lit11:
7042 return "DW_OP_lit11";
7043 case DW_OP_lit12:
7044 return "DW_OP_lit12";
7045 case DW_OP_lit13:
7046 return "DW_OP_lit13";
7047 case DW_OP_lit14:
7048 return "DW_OP_lit14";
7049 case DW_OP_lit15:
7050 return "DW_OP_lit15";
7051 case DW_OP_lit16:
7052 return "DW_OP_lit16";
7053 case DW_OP_lit17:
7054 return "DW_OP_lit17";
7055 case DW_OP_lit18:
7056 return "DW_OP_lit18";
7057 case DW_OP_lit19:
7058 return "DW_OP_lit19";
7059 case DW_OP_lit20:
7060 return "DW_OP_lit20";
7061 case DW_OP_lit21:
7062 return "DW_OP_lit21";
7063 case DW_OP_lit22:
7064 return "DW_OP_lit22";
7065 case DW_OP_lit23:
7066 return "DW_OP_lit23";
7067 case DW_OP_lit24:
7068 return "DW_OP_lit24";
7069 case DW_OP_lit25:
7070 return "DW_OP_lit25";
7071 case DW_OP_lit26:
7072 return "DW_OP_lit26";
7073 case DW_OP_lit27:
7074 return "DW_OP_lit27";
7075 case DW_OP_lit28:
7076 return "DW_OP_lit28";
7077 case DW_OP_lit29:
7078 return "DW_OP_lit29";
7079 case DW_OP_lit30:
7080 return "DW_OP_lit30";
7081 case DW_OP_lit31:
7082 return "DW_OP_lit31";
7083 case DW_OP_reg0:
7084 return "DW_OP_reg0";
7085 case DW_OP_reg1:
7086 return "DW_OP_reg1";
7087 case DW_OP_reg2:
7088 return "DW_OP_reg2";
7089 case DW_OP_reg3:
7090 return "DW_OP_reg3";
7091 case DW_OP_reg4:
7092 return "DW_OP_reg4";
7093 case DW_OP_reg5:
7094 return "DW_OP_reg5";
7095 case DW_OP_reg6:
7096 return "DW_OP_reg6";
7097 case DW_OP_reg7:
7098 return "DW_OP_reg7";
7099 case DW_OP_reg8:
7100 return "DW_OP_reg8";
7101 case DW_OP_reg9:
7102 return "DW_OP_reg9";
7103 case DW_OP_reg10:
7104 return "DW_OP_reg10";
7105 case DW_OP_reg11:
7106 return "DW_OP_reg11";
7107 case DW_OP_reg12:
7108 return "DW_OP_reg12";
7109 case DW_OP_reg13:
7110 return "DW_OP_reg13";
7111 case DW_OP_reg14:
7112 return "DW_OP_reg14";
7113 case DW_OP_reg15:
7114 return "DW_OP_reg15";
7115 case DW_OP_reg16:
7116 return "DW_OP_reg16";
7117 case DW_OP_reg17:
7118 return "DW_OP_reg17";
7119 case DW_OP_reg18:
7120 return "DW_OP_reg18";
7121 case DW_OP_reg19:
7122 return "DW_OP_reg19";
7123 case DW_OP_reg20:
7124 return "DW_OP_reg20";
7125 case DW_OP_reg21:
7126 return "DW_OP_reg21";
7127 case DW_OP_reg22:
7128 return "DW_OP_reg22";
7129 case DW_OP_reg23:
7130 return "DW_OP_reg23";
7131 case DW_OP_reg24:
7132 return "DW_OP_reg24";
7133 case DW_OP_reg25:
7134 return "DW_OP_reg25";
7135 case DW_OP_reg26:
7136 return "DW_OP_reg26";
7137 case DW_OP_reg27:
7138 return "DW_OP_reg27";
7139 case DW_OP_reg28:
7140 return "DW_OP_reg28";
7141 case DW_OP_reg29:
7142 return "DW_OP_reg29";
7143 case DW_OP_reg30:
7144 return "DW_OP_reg30";
7145 case DW_OP_reg31:
7146 return "DW_OP_reg31";
7147 case DW_OP_breg0:
7148 return "DW_OP_breg0";
7149 case DW_OP_breg1:
7150 return "DW_OP_breg1";
7151 case DW_OP_breg2:
7152 return "DW_OP_breg2";
7153 case DW_OP_breg3:
7154 return "DW_OP_breg3";
7155 case DW_OP_breg4:
7156 return "DW_OP_breg4";
7157 case DW_OP_breg5:
7158 return "DW_OP_breg5";
7159 case DW_OP_breg6:
7160 return "DW_OP_breg6";
7161 case DW_OP_breg7:
7162 return "DW_OP_breg7";
7163 case DW_OP_breg8:
7164 return "DW_OP_breg8";
7165 case DW_OP_breg9:
7166 return "DW_OP_breg9";
7167 case DW_OP_breg10:
7168 return "DW_OP_breg10";
7169 case DW_OP_breg11:
7170 return "DW_OP_breg11";
7171 case DW_OP_breg12:
7172 return "DW_OP_breg12";
7173 case DW_OP_breg13:
7174 return "DW_OP_breg13";
7175 case DW_OP_breg14:
7176 return "DW_OP_breg14";
7177 case DW_OP_breg15:
7178 return "DW_OP_breg15";
7179 case DW_OP_breg16:
7180 return "DW_OP_breg16";
7181 case DW_OP_breg17:
7182 return "DW_OP_breg17";
7183 case DW_OP_breg18:
7184 return "DW_OP_breg18";
7185 case DW_OP_breg19:
7186 return "DW_OP_breg19";
7187 case DW_OP_breg20:
7188 return "DW_OP_breg20";
7189 case DW_OP_breg21:
7190 return "DW_OP_breg21";
7191 case DW_OP_breg22:
7192 return "DW_OP_breg22";
7193 case DW_OP_breg23:
7194 return "DW_OP_breg23";
7195 case DW_OP_breg24:
7196 return "DW_OP_breg24";
7197 case DW_OP_breg25:
7198 return "DW_OP_breg25";
7199 case DW_OP_breg26:
7200 return "DW_OP_breg26";
7201 case DW_OP_breg27:
7202 return "DW_OP_breg27";
7203 case DW_OP_breg28:
7204 return "DW_OP_breg28";
7205 case DW_OP_breg29:
7206 return "DW_OP_breg29";
7207 case DW_OP_breg30:
7208 return "DW_OP_breg30";
7209 case DW_OP_breg31:
7210 return "DW_OP_breg31";
7211 case DW_OP_regx:
7212 return "DW_OP_regx";
7213 case DW_OP_fbreg:
7214 return "DW_OP_fbreg";
7215 case DW_OP_bregx:
7216 return "DW_OP_bregx";
7217 case DW_OP_piece:
7218 return "DW_OP_piece";
7219 case DW_OP_deref_size:
7220 return "DW_OP_deref_size";
7221 case DW_OP_xderef_size:
7222 return "DW_OP_xderef_size";
7223 case DW_OP_nop:
7224 return "DW_OP_nop";
7225 /* DWARF 3 extensions. */
7226 case DW_OP_push_object_address:
7227 return "DW_OP_push_object_address";
7228 case DW_OP_call2:
7229 return "DW_OP_call2";
7230 case DW_OP_call4:
7231 return "DW_OP_call4";
7232 case DW_OP_call_ref:
7233 return "DW_OP_call_ref";
7234 /* GNU extensions. */
7235 case DW_OP_GNU_push_tls_address:
7236 return "DW_OP_GNU_push_tls_address";
7237 default:
7238 return "OP_<unknown>";
7239 }
7240 }
7241
7242 static char *
7243 dwarf_bool_name (unsigned mybool)
7244 {
7245 if (mybool)
7246 return "TRUE";
7247 else
7248 return "FALSE";
7249 }
7250
7251 /* Convert a DWARF type code into its string name. */
7252
7253 static char *
7254 dwarf_type_encoding_name (unsigned enc)
7255 {
7256 switch (enc)
7257 {
7258 case DW_ATE_address:
7259 return "DW_ATE_address";
7260 case DW_ATE_boolean:
7261 return "DW_ATE_boolean";
7262 case DW_ATE_complex_float:
7263 return "DW_ATE_complex_float";
7264 case DW_ATE_float:
7265 return "DW_ATE_float";
7266 case DW_ATE_signed:
7267 return "DW_ATE_signed";
7268 case DW_ATE_signed_char:
7269 return "DW_ATE_signed_char";
7270 case DW_ATE_unsigned:
7271 return "DW_ATE_unsigned";
7272 case DW_ATE_unsigned_char:
7273 return "DW_ATE_unsigned_char";
7274 case DW_ATE_imaginary_float:
7275 return "DW_ATE_imaginary_float";
7276 default:
7277 return "DW_ATE_<unknown>";
7278 }
7279 }
7280
7281 /* Convert a DWARF call frame info operation to its string name. */
7282
7283 #if 0
7284 static char *
7285 dwarf_cfi_name (unsigned cfi_opc)
7286 {
7287 switch (cfi_opc)
7288 {
7289 case DW_CFA_advance_loc:
7290 return "DW_CFA_advance_loc";
7291 case DW_CFA_offset:
7292 return "DW_CFA_offset";
7293 case DW_CFA_restore:
7294 return "DW_CFA_restore";
7295 case DW_CFA_nop:
7296 return "DW_CFA_nop";
7297 case DW_CFA_set_loc:
7298 return "DW_CFA_set_loc";
7299 case DW_CFA_advance_loc1:
7300 return "DW_CFA_advance_loc1";
7301 case DW_CFA_advance_loc2:
7302 return "DW_CFA_advance_loc2";
7303 case DW_CFA_advance_loc4:
7304 return "DW_CFA_advance_loc4";
7305 case DW_CFA_offset_extended:
7306 return "DW_CFA_offset_extended";
7307 case DW_CFA_restore_extended:
7308 return "DW_CFA_restore_extended";
7309 case DW_CFA_undefined:
7310 return "DW_CFA_undefined";
7311 case DW_CFA_same_value:
7312 return "DW_CFA_same_value";
7313 case DW_CFA_register:
7314 return "DW_CFA_register";
7315 case DW_CFA_remember_state:
7316 return "DW_CFA_remember_state";
7317 case DW_CFA_restore_state:
7318 return "DW_CFA_restore_state";
7319 case DW_CFA_def_cfa:
7320 return "DW_CFA_def_cfa";
7321 case DW_CFA_def_cfa_register:
7322 return "DW_CFA_def_cfa_register";
7323 case DW_CFA_def_cfa_offset:
7324 return "DW_CFA_def_cfa_offset";
7325
7326 /* DWARF 3 */
7327 case DW_CFA_def_cfa_expression:
7328 return "DW_CFA_def_cfa_expression";
7329 case DW_CFA_expression:
7330 return "DW_CFA_expression";
7331 case DW_CFA_offset_extended_sf:
7332 return "DW_CFA_offset_extended_sf";
7333 case DW_CFA_def_cfa_sf:
7334 return "DW_CFA_def_cfa_sf";
7335 case DW_CFA_def_cfa_offset_sf:
7336 return "DW_CFA_def_cfa_offset_sf";
7337
7338 /* SGI/MIPS specific */
7339 case DW_CFA_MIPS_advance_loc8:
7340 return "DW_CFA_MIPS_advance_loc8";
7341
7342 /* GNU extensions */
7343 case DW_CFA_GNU_window_save:
7344 return "DW_CFA_GNU_window_save";
7345 case DW_CFA_GNU_args_size:
7346 return "DW_CFA_GNU_args_size";
7347 case DW_CFA_GNU_negative_offset_extended:
7348 return "DW_CFA_GNU_negative_offset_extended";
7349
7350 default:
7351 return "DW_CFA_<unknown>";
7352 }
7353 }
7354 #endif
7355
7356 static void
7357 dump_die (struct die_info *die)
7358 {
7359 unsigned int i;
7360
7361 fprintf_unfiltered (gdb_stderr, "Die: %s (abbrev = %d, offset = %d)\n",
7362 dwarf_tag_name (die->tag), die->abbrev, die->offset);
7363 fprintf_unfiltered (gdb_stderr, "\thas children: %s\n",
7364 dwarf_bool_name (die->child != NULL));
7365
7366 fprintf_unfiltered (gdb_stderr, "\tattributes:\n");
7367 for (i = 0; i < die->num_attrs; ++i)
7368 {
7369 fprintf_unfiltered (gdb_stderr, "\t\t%s (%s) ",
7370 dwarf_attr_name (die->attrs[i].name),
7371 dwarf_form_name (die->attrs[i].form));
7372 switch (die->attrs[i].form)
7373 {
7374 case DW_FORM_ref_addr:
7375 case DW_FORM_addr:
7376 fprintf_unfiltered (gdb_stderr, "address: ");
7377 print_address_numeric (DW_ADDR (&die->attrs[i]), 1, gdb_stderr);
7378 break;
7379 case DW_FORM_block2:
7380 case DW_FORM_block4:
7381 case DW_FORM_block:
7382 case DW_FORM_block1:
7383 fprintf_unfiltered (gdb_stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
7384 break;
7385 case DW_FORM_data1:
7386 case DW_FORM_data2:
7387 case DW_FORM_data4:
7388 case DW_FORM_data8:
7389 case DW_FORM_ref1:
7390 case DW_FORM_ref2:
7391 case DW_FORM_ref4:
7392 case DW_FORM_udata:
7393 case DW_FORM_sdata:
7394 fprintf_unfiltered (gdb_stderr, "constant: %ld", DW_UNSND (&die->attrs[i]));
7395 break;
7396 case DW_FORM_string:
7397 case DW_FORM_strp:
7398 fprintf_unfiltered (gdb_stderr, "string: \"%s\"",
7399 DW_STRING (&die->attrs[i])
7400 ? DW_STRING (&die->attrs[i]) : "");
7401 break;
7402 case DW_FORM_flag:
7403 if (DW_UNSND (&die->attrs[i]))
7404 fprintf_unfiltered (gdb_stderr, "flag: TRUE");
7405 else
7406 fprintf_unfiltered (gdb_stderr, "flag: FALSE");
7407 break;
7408 case DW_FORM_indirect:
7409 /* the reader will have reduced the indirect form to
7410 the "base form" so this form should not occur */
7411 fprintf_unfiltered (gdb_stderr, "unexpected attribute form: DW_FORM_indirect");
7412 break;
7413 default:
7414 fprintf_unfiltered (gdb_stderr, "unsupported attribute form: %d.",
7415 die->attrs[i].form);
7416 }
7417 fprintf_unfiltered (gdb_stderr, "\n");
7418 }
7419 }
7420
7421 static void
7422 dump_die_list (struct die_info *die)
7423 {
7424 while (die)
7425 {
7426 dump_die (die);
7427 if (die->child != NULL)
7428 dump_die_list (die->child);
7429 if (die->sibling != NULL)
7430 dump_die_list (die->sibling);
7431 }
7432 }
7433
7434 static void
7435 store_in_ref_table (unsigned int offset, struct die_info *die)
7436 {
7437 int h;
7438 struct die_info *old;
7439
7440 h = (offset % REF_HASH_SIZE);
7441 old = die_ref_table[h];
7442 die->next_ref = old;
7443 die_ref_table[h] = die;
7444 }
7445
7446
7447 static void
7448 dwarf2_empty_hash_tables (void)
7449 {
7450 memset (die_ref_table, 0, sizeof (die_ref_table));
7451 }
7452
7453 static unsigned int
7454 dwarf2_get_ref_die_offset (struct attribute *attr, struct dwarf2_cu *cu)
7455 {
7456 unsigned int result = 0;
7457
7458 switch (attr->form)
7459 {
7460 case DW_FORM_ref_addr:
7461 result = DW_ADDR (attr);
7462 break;
7463 case DW_FORM_ref1:
7464 case DW_FORM_ref2:
7465 case DW_FORM_ref4:
7466 case DW_FORM_ref8:
7467 case DW_FORM_ref_udata:
7468 result = cu->header.offset + DW_UNSND (attr);
7469 break;
7470 default:
7471 complaint (&symfile_complaints,
7472 "unsupported die ref attribute form: '%s'",
7473 dwarf_form_name (attr->form));
7474 }
7475 return result;
7476 }
7477
7478 /* Return the constant value held by the given attribute. Return -1
7479 if the value held by the attribute is not constant. */
7480
7481 static int
7482 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
7483 {
7484 if (attr->form == DW_FORM_sdata)
7485 return DW_SND (attr);
7486 else if (attr->form == DW_FORM_udata
7487 || attr->form == DW_FORM_data1
7488 || attr->form == DW_FORM_data2
7489 || attr->form == DW_FORM_data4
7490 || attr->form == DW_FORM_data8)
7491 return DW_UNSND (attr);
7492 else
7493 {
7494 complaint (&symfile_complaints, "Attribute value is not a constant (%s)",
7495 dwarf_form_name (attr->form));
7496 return default_value;
7497 }
7498 }
7499
7500 static struct die_info *
7501 follow_die_ref (unsigned int offset)
7502 {
7503 struct die_info *die;
7504 int h;
7505
7506 h = (offset % REF_HASH_SIZE);
7507 die = die_ref_table[h];
7508 while (die)
7509 {
7510 if (die->offset == offset)
7511 {
7512 return die;
7513 }
7514 die = die->next_ref;
7515 }
7516 return NULL;
7517 }
7518
7519 static struct type *
7520 dwarf2_fundamental_type (struct objfile *objfile, int typeid,
7521 struct dwarf2_cu *cu)
7522 {
7523 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
7524 {
7525 error ("Dwarf Error: internal error - invalid fundamental type id %d [in module %s]",
7526 typeid, objfile->name);
7527 }
7528
7529 /* Look for this particular type in the fundamental type vector. If
7530 one is not found, create and install one appropriate for the
7531 current language and the current target machine. */
7532
7533 if (cu->ftypes[typeid] == NULL)
7534 {
7535 cu->ftypes[typeid] = cu->language_defn->la_fund_type (objfile, typeid);
7536 }
7537
7538 return (cu->ftypes[typeid]);
7539 }
7540
7541 /* Decode simple location descriptions.
7542 Given a pointer to a dwarf block that defines a location, compute
7543 the location and return the value.
7544
7545 NOTE drow/2003-11-18: This function is called in two situations
7546 now: for the address of static or global variables (partial symbols
7547 only) and for offsets into structures which are expected to be
7548 (more or less) constant. The partial symbol case should go away,
7549 and only the constant case should remain. That will let this
7550 function complain more accurately. A few special modes are allowed
7551 without complaint for global variables (for instance, global
7552 register values and thread-local values).
7553
7554 A location description containing no operations indicates that the
7555 object is optimized out. The return value is 0 for that case.
7556 FIXME drow/2003-11-16: No callers check for this case any more; soon all
7557 callers will only want a very basic result and this can become a
7558 complaint.
7559
7560 When the result is a register number, the global isreg flag is set,
7561 otherwise it is cleared.
7562
7563 Note that stack[0] is unused except as a default error return.
7564 Note that stack overflow is not yet handled. */
7565
7566 static CORE_ADDR
7567 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
7568 {
7569 struct objfile *objfile = cu->objfile;
7570 struct comp_unit_head *cu_header = &cu->header;
7571 int i;
7572 int size = blk->size;
7573 char *data = blk->data;
7574 CORE_ADDR stack[64];
7575 int stacki;
7576 unsigned int bytes_read, unsnd;
7577 unsigned char op;
7578
7579 i = 0;
7580 stacki = 0;
7581 stack[stacki] = 0;
7582 isreg = 0;
7583
7584 while (i < size)
7585 {
7586 op = data[i++];
7587 switch (op)
7588 {
7589 case DW_OP_lit0:
7590 case DW_OP_lit1:
7591 case DW_OP_lit2:
7592 case DW_OP_lit3:
7593 case DW_OP_lit4:
7594 case DW_OP_lit5:
7595 case DW_OP_lit6:
7596 case DW_OP_lit7:
7597 case DW_OP_lit8:
7598 case DW_OP_lit9:
7599 case DW_OP_lit10:
7600 case DW_OP_lit11:
7601 case DW_OP_lit12:
7602 case DW_OP_lit13:
7603 case DW_OP_lit14:
7604 case DW_OP_lit15:
7605 case DW_OP_lit16:
7606 case DW_OP_lit17:
7607 case DW_OP_lit18:
7608 case DW_OP_lit19:
7609 case DW_OP_lit20:
7610 case DW_OP_lit21:
7611 case DW_OP_lit22:
7612 case DW_OP_lit23:
7613 case DW_OP_lit24:
7614 case DW_OP_lit25:
7615 case DW_OP_lit26:
7616 case DW_OP_lit27:
7617 case DW_OP_lit28:
7618 case DW_OP_lit29:
7619 case DW_OP_lit30:
7620 case DW_OP_lit31:
7621 stack[++stacki] = op - DW_OP_lit0;
7622 break;
7623
7624 case DW_OP_reg0:
7625 case DW_OP_reg1:
7626 case DW_OP_reg2:
7627 case DW_OP_reg3:
7628 case DW_OP_reg4:
7629 case DW_OP_reg5:
7630 case DW_OP_reg6:
7631 case DW_OP_reg7:
7632 case DW_OP_reg8:
7633 case DW_OP_reg9:
7634 case DW_OP_reg10:
7635 case DW_OP_reg11:
7636 case DW_OP_reg12:
7637 case DW_OP_reg13:
7638 case DW_OP_reg14:
7639 case DW_OP_reg15:
7640 case DW_OP_reg16:
7641 case DW_OP_reg17:
7642 case DW_OP_reg18:
7643 case DW_OP_reg19:
7644 case DW_OP_reg20:
7645 case DW_OP_reg21:
7646 case DW_OP_reg22:
7647 case DW_OP_reg23:
7648 case DW_OP_reg24:
7649 case DW_OP_reg25:
7650 case DW_OP_reg26:
7651 case DW_OP_reg27:
7652 case DW_OP_reg28:
7653 case DW_OP_reg29:
7654 case DW_OP_reg30:
7655 case DW_OP_reg31:
7656 isreg = 1;
7657 stack[++stacki] = op - DW_OP_reg0;
7658 if (i < size)
7659 dwarf2_complex_location_expr_complaint ();
7660 break;
7661
7662 case DW_OP_regx:
7663 isreg = 1;
7664 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
7665 i += bytes_read;
7666 stack[++stacki] = unsnd;
7667 if (i < size)
7668 dwarf2_complex_location_expr_complaint ();
7669 break;
7670
7671 case DW_OP_addr:
7672 stack[++stacki] = read_address (objfile->obfd, &data[i],
7673 cu, &bytes_read);
7674 i += bytes_read;
7675 break;
7676
7677 case DW_OP_const1u:
7678 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
7679 i += 1;
7680 break;
7681
7682 case DW_OP_const1s:
7683 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
7684 i += 1;
7685 break;
7686
7687 case DW_OP_const2u:
7688 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
7689 i += 2;
7690 break;
7691
7692 case DW_OP_const2s:
7693 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
7694 i += 2;
7695 break;
7696
7697 case DW_OP_const4u:
7698 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
7699 i += 4;
7700 break;
7701
7702 case DW_OP_const4s:
7703 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
7704 i += 4;
7705 break;
7706
7707 case DW_OP_constu:
7708 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
7709 &bytes_read);
7710 i += bytes_read;
7711 break;
7712
7713 case DW_OP_consts:
7714 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
7715 i += bytes_read;
7716 break;
7717
7718 case DW_OP_dup:
7719 stack[stacki + 1] = stack[stacki];
7720 stacki++;
7721 break;
7722
7723 case DW_OP_plus:
7724 stack[stacki - 1] += stack[stacki];
7725 stacki--;
7726 break;
7727
7728 case DW_OP_plus_uconst:
7729 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
7730 i += bytes_read;
7731 break;
7732
7733 case DW_OP_minus:
7734 stack[stacki - 1] -= stack[stacki];
7735 stacki--;
7736 break;
7737
7738 case DW_OP_deref:
7739 /* If we're not the last op, then we definitely can't encode
7740 this using GDB's address_class enum. This is valid for partial
7741 global symbols, although the variable's address will be bogus
7742 in the psymtab. */
7743 if (i < size)
7744 dwarf2_complex_location_expr_complaint ();
7745 break;
7746
7747 case DW_OP_GNU_push_tls_address:
7748 /* The top of the stack has the offset from the beginning
7749 of the thread control block at which the variable is located. */
7750 /* Nothing should follow this operator, so the top of stack would
7751 be returned. */
7752 /* This is valid for partial global symbols, but the variable's
7753 address will be bogus in the psymtab. */
7754 if (i < size)
7755 dwarf2_complex_location_expr_complaint ();
7756 break;
7757
7758 default:
7759 complaint (&symfile_complaints, "unsupported stack op: '%s'",
7760 dwarf_stack_op_name (op));
7761 return (stack[stacki]);
7762 }
7763 }
7764 return (stack[stacki]);
7765 }
7766
7767 /* memory allocation interface */
7768
7769 static void
7770 dwarf2_free_tmp_obstack (void *ignore)
7771 {
7772 obstack_free (&dwarf2_tmp_obstack, NULL);
7773 }
7774
7775 static struct dwarf_block *
7776 dwarf_alloc_block (void)
7777 {
7778 struct dwarf_block *blk;
7779
7780 blk = (struct dwarf_block *)
7781 obstack_alloc (&dwarf2_tmp_obstack, sizeof (struct dwarf_block));
7782 return (blk);
7783 }
7784
7785 static struct abbrev_info *
7786 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
7787 {
7788 struct abbrev_info *abbrev;
7789
7790 abbrev = (struct abbrev_info *)
7791 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
7792 memset (abbrev, 0, sizeof (struct abbrev_info));
7793 return (abbrev);
7794 }
7795
7796 static struct die_info *
7797 dwarf_alloc_die (void)
7798 {
7799 struct die_info *die;
7800
7801 die = (struct die_info *) xmalloc (sizeof (struct die_info));
7802 memset (die, 0, sizeof (struct die_info));
7803 return (die);
7804 }
7805
7806 \f
7807 /* Macro support. */
7808
7809
7810 /* Return the full name of file number I in *LH's file name table.
7811 Use COMP_DIR as the name of the current directory of the
7812 compilation. The result is allocated using xmalloc; the caller is
7813 responsible for freeing it. */
7814 static char *
7815 file_full_name (int file, struct line_header *lh, const char *comp_dir)
7816 {
7817 struct file_entry *fe = &lh->file_names[file - 1];
7818
7819 if (IS_ABSOLUTE_PATH (fe->name))
7820 return xstrdup (fe->name);
7821 else
7822 {
7823 const char *dir;
7824 int dir_len;
7825 char *full_name;
7826
7827 if (fe->dir_index)
7828 dir = lh->include_dirs[fe->dir_index - 1];
7829 else
7830 dir = comp_dir;
7831
7832 if (dir)
7833 {
7834 dir_len = strlen (dir);
7835 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
7836 strcpy (full_name, dir);
7837 full_name[dir_len] = '/';
7838 strcpy (full_name + dir_len + 1, fe->name);
7839 return full_name;
7840 }
7841 else
7842 return xstrdup (fe->name);
7843 }
7844 }
7845
7846
7847 static struct macro_source_file *
7848 macro_start_file (int file, int line,
7849 struct macro_source_file *current_file,
7850 const char *comp_dir,
7851 struct line_header *lh, struct objfile *objfile)
7852 {
7853 /* The full name of this source file. */
7854 char *full_name = file_full_name (file, lh, comp_dir);
7855
7856 /* We don't create a macro table for this compilation unit
7857 at all until we actually get a filename. */
7858 if (! pending_macros)
7859 pending_macros = new_macro_table (&objfile->objfile_obstack,
7860 objfile->macro_cache);
7861
7862 if (! current_file)
7863 /* If we have no current file, then this must be the start_file
7864 directive for the compilation unit's main source file. */
7865 current_file = macro_set_main (pending_macros, full_name);
7866 else
7867 current_file = macro_include (current_file, line, full_name);
7868
7869 xfree (full_name);
7870
7871 return current_file;
7872 }
7873
7874
7875 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
7876 followed by a null byte. */
7877 static char *
7878 copy_string (const char *buf, int len)
7879 {
7880 char *s = xmalloc (len + 1);
7881 memcpy (s, buf, len);
7882 s[len] = '\0';
7883
7884 return s;
7885 }
7886
7887
7888 static const char *
7889 consume_improper_spaces (const char *p, const char *body)
7890 {
7891 if (*p == ' ')
7892 {
7893 complaint (&symfile_complaints,
7894 "macro definition contains spaces in formal argument list:\n`%s'",
7895 body);
7896
7897 while (*p == ' ')
7898 p++;
7899 }
7900
7901 return p;
7902 }
7903
7904
7905 static void
7906 parse_macro_definition (struct macro_source_file *file, int line,
7907 const char *body)
7908 {
7909 const char *p;
7910
7911 /* The body string takes one of two forms. For object-like macro
7912 definitions, it should be:
7913
7914 <macro name> " " <definition>
7915
7916 For function-like macro definitions, it should be:
7917
7918 <macro name> "() " <definition>
7919 or
7920 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
7921
7922 Spaces may appear only where explicitly indicated, and in the
7923 <definition>.
7924
7925 The Dwarf 2 spec says that an object-like macro's name is always
7926 followed by a space, but versions of GCC around March 2002 omit
7927 the space when the macro's definition is the empty string.
7928
7929 The Dwarf 2 spec says that there should be no spaces between the
7930 formal arguments in a function-like macro's formal argument list,
7931 but versions of GCC around March 2002 include spaces after the
7932 commas. */
7933
7934
7935 /* Find the extent of the macro name. The macro name is terminated
7936 by either a space or null character (for an object-like macro) or
7937 an opening paren (for a function-like macro). */
7938 for (p = body; *p; p++)
7939 if (*p == ' ' || *p == '(')
7940 break;
7941
7942 if (*p == ' ' || *p == '\0')
7943 {
7944 /* It's an object-like macro. */
7945 int name_len = p - body;
7946 char *name = copy_string (body, name_len);
7947 const char *replacement;
7948
7949 if (*p == ' ')
7950 replacement = body + name_len + 1;
7951 else
7952 {
7953 dwarf2_macro_malformed_definition_complaint (body);
7954 replacement = body + name_len;
7955 }
7956
7957 macro_define_object (file, line, name, replacement);
7958
7959 xfree (name);
7960 }
7961 else if (*p == '(')
7962 {
7963 /* It's a function-like macro. */
7964 char *name = copy_string (body, p - body);
7965 int argc = 0;
7966 int argv_size = 1;
7967 char **argv = xmalloc (argv_size * sizeof (*argv));
7968
7969 p++;
7970
7971 p = consume_improper_spaces (p, body);
7972
7973 /* Parse the formal argument list. */
7974 while (*p && *p != ')')
7975 {
7976 /* Find the extent of the current argument name. */
7977 const char *arg_start = p;
7978
7979 while (*p && *p != ',' && *p != ')' && *p != ' ')
7980 p++;
7981
7982 if (! *p || p == arg_start)
7983 dwarf2_macro_malformed_definition_complaint (body);
7984 else
7985 {
7986 /* Make sure argv has room for the new argument. */
7987 if (argc >= argv_size)
7988 {
7989 argv_size *= 2;
7990 argv = xrealloc (argv, argv_size * sizeof (*argv));
7991 }
7992
7993 argv[argc++] = copy_string (arg_start, p - arg_start);
7994 }
7995
7996 p = consume_improper_spaces (p, body);
7997
7998 /* Consume the comma, if present. */
7999 if (*p == ',')
8000 {
8001 p++;
8002
8003 p = consume_improper_spaces (p, body);
8004 }
8005 }
8006
8007 if (*p == ')')
8008 {
8009 p++;
8010
8011 if (*p == ' ')
8012 /* Perfectly formed definition, no complaints. */
8013 macro_define_function (file, line, name,
8014 argc, (const char **) argv,
8015 p + 1);
8016 else if (*p == '\0')
8017 {
8018 /* Complain, but do define it. */
8019 dwarf2_macro_malformed_definition_complaint (body);
8020 macro_define_function (file, line, name,
8021 argc, (const char **) argv,
8022 p);
8023 }
8024 else
8025 /* Just complain. */
8026 dwarf2_macro_malformed_definition_complaint (body);
8027 }
8028 else
8029 /* Just complain. */
8030 dwarf2_macro_malformed_definition_complaint (body);
8031
8032 xfree (name);
8033 {
8034 int i;
8035
8036 for (i = 0; i < argc; i++)
8037 xfree (argv[i]);
8038 }
8039 xfree (argv);
8040 }
8041 else
8042 dwarf2_macro_malformed_definition_complaint (body);
8043 }
8044
8045
8046 static void
8047 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
8048 char *comp_dir, bfd *abfd,
8049 struct dwarf2_cu *cu)
8050 {
8051 char *mac_ptr, *mac_end;
8052 struct macro_source_file *current_file = 0;
8053
8054 if (dwarf_macinfo_buffer == NULL)
8055 {
8056 complaint (&symfile_complaints, "missing .debug_macinfo section");
8057 return;
8058 }
8059
8060 mac_ptr = dwarf_macinfo_buffer + offset;
8061 mac_end = dwarf_macinfo_buffer + dwarf_macinfo_size;
8062
8063 for (;;)
8064 {
8065 enum dwarf_macinfo_record_type macinfo_type;
8066
8067 /* Do we at least have room for a macinfo type byte? */
8068 if (mac_ptr >= mac_end)
8069 {
8070 dwarf2_macros_too_long_complaint ();
8071 return;
8072 }
8073
8074 macinfo_type = read_1_byte (abfd, mac_ptr);
8075 mac_ptr++;
8076
8077 switch (macinfo_type)
8078 {
8079 /* A zero macinfo type indicates the end of the macro
8080 information. */
8081 case 0:
8082 return;
8083
8084 case DW_MACINFO_define:
8085 case DW_MACINFO_undef:
8086 {
8087 int bytes_read;
8088 int line;
8089 char *body;
8090
8091 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8092 mac_ptr += bytes_read;
8093 body = read_string (abfd, mac_ptr, &bytes_read);
8094 mac_ptr += bytes_read;
8095
8096 if (! current_file)
8097 complaint (&symfile_complaints,
8098 "debug info gives macro %s outside of any file: %s",
8099 macinfo_type ==
8100 DW_MACINFO_define ? "definition" : macinfo_type ==
8101 DW_MACINFO_undef ? "undefinition" :
8102 "something-or-other", body);
8103 else
8104 {
8105 if (macinfo_type == DW_MACINFO_define)
8106 parse_macro_definition (current_file, line, body);
8107 else if (macinfo_type == DW_MACINFO_undef)
8108 macro_undef (current_file, line, body);
8109 }
8110 }
8111 break;
8112
8113 case DW_MACINFO_start_file:
8114 {
8115 int bytes_read;
8116 int line, file;
8117
8118 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8119 mac_ptr += bytes_read;
8120 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8121 mac_ptr += bytes_read;
8122
8123 current_file = macro_start_file (file, line,
8124 current_file, comp_dir,
8125 lh, cu->objfile);
8126 }
8127 break;
8128
8129 case DW_MACINFO_end_file:
8130 if (! current_file)
8131 complaint (&symfile_complaints,
8132 "macro debug info has an unmatched `close_file' directive");
8133 else
8134 {
8135 current_file = current_file->included_by;
8136 if (! current_file)
8137 {
8138 enum dwarf_macinfo_record_type next_type;
8139
8140 /* GCC circa March 2002 doesn't produce the zero
8141 type byte marking the end of the compilation
8142 unit. Complain if it's not there, but exit no
8143 matter what. */
8144
8145 /* Do we at least have room for a macinfo type byte? */
8146 if (mac_ptr >= mac_end)
8147 {
8148 dwarf2_macros_too_long_complaint ();
8149 return;
8150 }
8151
8152 /* We don't increment mac_ptr here, so this is just
8153 a look-ahead. */
8154 next_type = read_1_byte (abfd, mac_ptr);
8155 if (next_type != 0)
8156 complaint (&symfile_complaints,
8157 "no terminating 0-type entry for macros in `.debug_macinfo' section");
8158
8159 return;
8160 }
8161 }
8162 break;
8163
8164 case DW_MACINFO_vendor_ext:
8165 {
8166 int bytes_read;
8167 int constant;
8168 char *string;
8169
8170 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8171 mac_ptr += bytes_read;
8172 string = read_string (abfd, mac_ptr, &bytes_read);
8173 mac_ptr += bytes_read;
8174
8175 /* We don't recognize any vendor extensions. */
8176 }
8177 break;
8178 }
8179 }
8180 }
8181
8182 /* Check if the attribute's form is a DW_FORM_block*
8183 if so return true else false. */
8184 static int
8185 attr_form_is_block (struct attribute *attr)
8186 {
8187 return (attr == NULL ? 0 :
8188 attr->form == DW_FORM_block1
8189 || attr->form == DW_FORM_block2
8190 || attr->form == DW_FORM_block4
8191 || attr->form == DW_FORM_block);
8192 }
8193
8194 static void
8195 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
8196 struct dwarf2_cu *cu)
8197 {
8198 if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
8199 {
8200 struct dwarf2_loclist_baton *baton;
8201
8202 baton = obstack_alloc (&cu->objfile->objfile_obstack,
8203 sizeof (struct dwarf2_loclist_baton));
8204 baton->objfile = cu->objfile;
8205
8206 /* We don't know how long the location list is, but make sure we
8207 don't run off the edge of the section. */
8208 baton->size = dwarf_loc_size - DW_UNSND (attr);
8209 baton->data = dwarf_loc_buffer + DW_UNSND (attr);
8210 baton->base_address = cu->header.base_address;
8211 if (cu->header.base_known == 0)
8212 complaint (&symfile_complaints,
8213 "Location list used without specifying the CU base address.");
8214
8215 SYMBOL_OPS (sym) = &dwarf2_loclist_funcs;
8216 SYMBOL_LOCATION_BATON (sym) = baton;
8217 }
8218 else
8219 {
8220 struct dwarf2_locexpr_baton *baton;
8221
8222 baton = obstack_alloc (&cu->objfile->objfile_obstack,
8223 sizeof (struct dwarf2_locexpr_baton));
8224 baton->objfile = cu->objfile;
8225
8226 if (attr_form_is_block (attr))
8227 {
8228 /* Note that we're just copying the block's data pointer
8229 here, not the actual data. We're still pointing into the
8230 dwarf_info_buffer for SYM's objfile; right now we never
8231 release that buffer, but when we do clean up properly
8232 this may need to change. */
8233 baton->size = DW_BLOCK (attr)->size;
8234 baton->data = DW_BLOCK (attr)->data;
8235 }
8236 else
8237 {
8238 dwarf2_invalid_attrib_class_complaint ("location description",
8239 SYMBOL_NATURAL_NAME (sym));
8240 baton->size = 0;
8241 baton->data = NULL;
8242 }
8243
8244 SYMBOL_OPS (sym) = &dwarf2_locexpr_funcs;
8245 SYMBOL_LOCATION_BATON (sym) = baton;
8246 }
8247 }
This page took 0.218205 seconds and 4 git commands to generate.