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