* h8300-dis.c: Fix formatting.
[binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2 Copyright 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3
4 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
5 Inc. with support from Florida State University (under contract
6 with the Ada Joint Program Office), and Silicon Graphics, Inc.
7 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
8 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
9 support in dwarfread.c
10
11 This file is part of GDB.
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or (at
16 your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330,
26 Boston, MA 02111-1307, USA. */
27
28 #include "defs.h"
29 #include "bfd.h"
30 #include "symtab.h"
31 #include "gdbtypes.h"
32 #include "symfile.h"
33 #include "objfiles.h"
34 #include "elf/dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38
39 #include "language.h"
40 #include "complaints.h"
41 #include "bcache.h"
42 #include <fcntl.h>
43 #include "gdb_string.h"
44 #include <sys/types.h>
45
46 /* .debug_info header for a compilation unit
47 Because of alignment constraints, this structure has padding and cannot
48 be mapped directly onto the beginning of the .debug_info section. */
49 typedef struct comp_unit_header
50 {
51 unsigned int length; /* length of the .debug_info
52 contribution */
53 unsigned short version; /* version number -- 2 for DWARF
54 version 2 */
55 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
56 unsigned char addr_size; /* byte size of an address -- 4 */
57 }
58 _COMP_UNIT_HEADER;
59 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
60
61 /* .debug_pubnames header
62 Because of alignment constraints, this structure has padding and cannot
63 be mapped directly onto the beginning of the .debug_info section. */
64 typedef struct pubnames_header
65 {
66 unsigned int length; /* length of the .debug_pubnames
67 contribution */
68 unsigned char version; /* version number -- 2 for DWARF
69 version 2 */
70 unsigned int info_offset; /* offset into .debug_info section */
71 unsigned int info_size; /* byte size of .debug_info section
72 portion */
73 }
74 _PUBNAMES_HEADER;
75 #define _ACTUAL_PUBNAMES_HEADER_SIZE 13
76
77 /* .debug_pubnames header
78 Because of alignment constraints, this structure has padding and cannot
79 be mapped directly onto the beginning of the .debug_info section. */
80 typedef struct aranges_header
81 {
82 unsigned int length; /* byte len of the .debug_aranges
83 contribution */
84 unsigned short version; /* version number -- 2 for DWARF
85 version 2 */
86 unsigned int info_offset; /* offset into .debug_info section */
87 unsigned char addr_size; /* byte size of an address */
88 unsigned char seg_size; /* byte size of segment descriptor */
89 }
90 _ARANGES_HEADER;
91 #define _ACTUAL_ARANGES_HEADER_SIZE 12
92
93 /* .debug_line statement program prologue
94 Because of alignment constraints, this structure has padding and cannot
95 be mapped directly onto the beginning of the .debug_info section. */
96 typedef struct statement_prologue
97 {
98 unsigned int total_length; /* byte length of the statement
99 information */
100 unsigned short version; /* version number -- 2 for DWARF
101 version 2 */
102 unsigned int prologue_length; /* # bytes between prologue &
103 stmt program */
104 unsigned char minimum_instruction_length; /* byte size of
105 smallest instr */
106 unsigned char default_is_stmt; /* initial value of is_stmt
107 register */
108 char line_base;
109 unsigned char line_range;
110 unsigned char opcode_base; /* number assigned to first special
111 opcode */
112 unsigned char *standard_opcode_lengths;
113 }
114 _STATEMENT_PROLOGUE;
115
116 /* offsets and sizes of debugging sections */
117
118 static file_ptr dwarf_info_offset;
119 static file_ptr dwarf_abbrev_offset;
120 static file_ptr dwarf_line_offset;
121 static file_ptr dwarf_pubnames_offset;
122 static file_ptr dwarf_aranges_offset;
123 static file_ptr dwarf_loc_offset;
124 static file_ptr dwarf_macinfo_offset;
125 static file_ptr dwarf_str_offset;
126
127 static unsigned int dwarf_info_size;
128 static unsigned int dwarf_abbrev_size;
129 static unsigned int dwarf_line_size;
130 static unsigned int dwarf_pubnames_size;
131 static unsigned int dwarf_aranges_size;
132 static unsigned int dwarf_loc_size;
133 static unsigned int dwarf_macinfo_size;
134 static unsigned int dwarf_str_size;
135
136 /* names of the debugging sections */
137
138 #define INFO_SECTION ".debug_info"
139 #define ABBREV_SECTION ".debug_abbrev"
140 #define LINE_SECTION ".debug_line"
141 #define PUBNAMES_SECTION ".debug_pubnames"
142 #define ARANGES_SECTION ".debug_aranges"
143 #define LOC_SECTION ".debug_loc"
144 #define MACINFO_SECTION ".debug_macinfo"
145 #define STR_SECTION ".debug_str"
146
147 /* local data types */
148
149 /* The data in a compilation unit header looks like this. */
150 struct comp_unit_head
151 {
152 unsigned int length;
153 short version;
154 unsigned int abbrev_offset;
155 unsigned char addr_size;
156 };
157
158 /* The data in the .debug_line statement prologue looks like this. */
159 struct line_head
160 {
161 unsigned int total_length;
162 unsigned short version;
163 unsigned int prologue_length;
164 unsigned char minimum_instruction_length;
165 unsigned char default_is_stmt;
166 int line_base;
167 unsigned char line_range;
168 unsigned char opcode_base;
169 unsigned char *standard_opcode_lengths;
170 };
171
172 /* When we construct a partial symbol table entry we only
173 need this much information. */
174 struct partial_die_info
175 {
176 enum dwarf_tag tag;
177 unsigned char has_children;
178 unsigned char is_external;
179 unsigned char is_declaration;
180 unsigned char has_type;
181 unsigned int offset;
182 unsigned int abbrev;
183 char *name;
184 CORE_ADDR lowpc;
185 CORE_ADDR highpc;
186 struct dwarf_block *locdesc;
187 unsigned int language;
188 char *sibling;
189 };
190
191 /* This data structure holds the information of an abbrev. */
192 struct abbrev_info
193 {
194 unsigned int number; /* number identifying abbrev */
195 enum dwarf_tag tag; /* dwarf tag */
196 int has_children; /* boolean */
197 unsigned int num_attrs; /* number of attributes */
198 struct attr_abbrev *attrs; /* an array of attribute descriptions */
199 struct abbrev_info *next; /* next in chain */
200 };
201
202 struct attr_abbrev
203 {
204 enum dwarf_attribute name;
205 enum dwarf_form form;
206 };
207
208 /* This data structure holds a complete die structure. */
209 struct die_info
210 {
211 enum dwarf_tag tag; /* Tag indicating type of die */
212 unsigned short has_children; /* Does the die have children */
213 unsigned int abbrev; /* Abbrev number */
214 unsigned int offset; /* Offset in .debug_info section */
215 unsigned int num_attrs; /* Number of attributes */
216 struct attribute *attrs; /* An array of attributes */
217 struct die_info *next_ref; /* Next die in ref hash table */
218 struct die_info *next; /* Next die in linked list */
219 struct type *type; /* Cached type information */
220 };
221
222 /* Attributes have a name and a value */
223 struct attribute
224 {
225 enum dwarf_attribute name;
226 enum dwarf_form form;
227 union
228 {
229 char *str;
230 struct dwarf_block *blk;
231 unsigned int unsnd;
232 int snd;
233 CORE_ADDR addr;
234 }
235 u;
236 };
237
238 /* Get at parts of an attribute structure */
239
240 #define DW_STRING(attr) ((attr)->u.str)
241 #define DW_UNSND(attr) ((attr)->u.unsnd)
242 #define DW_BLOCK(attr) ((attr)->u.blk)
243 #define DW_SND(attr) ((attr)->u.snd)
244 #define DW_ADDR(attr) ((attr)->u.addr)
245
246 /* Blocks are a bunch of untyped bytes. */
247 struct dwarf_block
248 {
249 unsigned int size;
250 char *data;
251 };
252
253 /* We only hold one compilation unit's abbrevs in
254 memory at any one time. */
255 #ifndef ABBREV_HASH_SIZE
256 #define ABBREV_HASH_SIZE 121
257 #endif
258 #ifndef ATTR_ALLOC_CHUNK
259 #define ATTR_ALLOC_CHUNK 4
260 #endif
261
262 static struct abbrev_info *dwarf2_abbrevs[ABBREV_HASH_SIZE];
263
264 /* A hash table of die offsets for following references. */
265 #ifndef REF_HASH_SIZE
266 #define REF_HASH_SIZE 1021
267 #endif
268
269 static struct die_info *die_ref_table[REF_HASH_SIZE];
270
271 #ifndef TYPE_HASH_SIZE
272 #define TYPE_HASH_SIZE 4096
273 #endif
274 static struct type *dwarf2_cached_types[TYPE_HASH_SIZE];
275
276 /* Obstack for allocating temporary storage used during symbol reading. */
277 static struct obstack dwarf2_tmp_obstack;
278
279 /* Offset to the first byte of the current compilation unit header,
280 for resolving relative reference dies. */
281 static unsigned int cu_header_offset;
282
283 /* Allocate fields for structs, unions and enums in this size. */
284 #ifndef DW_FIELD_ALLOC_CHUNK
285 #define DW_FIELD_ALLOC_CHUNK 4
286 #endif
287
288 /* The language we are debugging. */
289 static enum language cu_language;
290 static const struct language_defn *cu_language_defn;
291
292 /* Actually data from the sections. */
293 static char *dwarf_info_buffer;
294 static char *dwarf_abbrev_buffer;
295 static char *dwarf_line_buffer;
296
297 /* A zeroed version of a partial die for initialization purposes. */
298 static struct partial_die_info zeroed_partial_die;
299
300 /* The generic symbol table building routines have separate lists for
301 file scope symbols and all all other scopes (local scopes). So
302 we need to select the right one to pass to add_symbol_to_list().
303 We do it by keeping a pointer to the correct list in list_in_scope.
304
305 FIXME: The original dwarf code just treated the file scope as the first
306 local scope, and all other local scopes as nested local scopes, and worked
307 fine. Check to see if we really need to distinguish these
308 in buildsym.c. */
309 static struct pending **list_in_scope = &file_symbols;
310
311 /* FIXME: decode_locdesc sets these variables to describe the location
312 to the caller. These ought to be a structure or something. If
313 none of the flags are set, the object lives at the address returned
314 by decode_locdesc. */
315
316 static int optimized_out; /* No ops in location in expression,
317 so object was optimized out. */
318 static int isreg; /* Object lives in register.
319 decode_locdesc's return value is
320 the register number. */
321 static int offreg; /* Object's address is the sum of the
322 register specified by basereg, plus
323 the offset returned. */
324 static int basereg; /* See `offreg'. */
325 static int isderef; /* Value described by flags above is
326 the address of a pointer to the object. */
327 static int islocal; /* Variable is at the returned offset
328 from the frame start, but there's
329 no identified frame pointer for
330 this function, so we can't say
331 which register it's relative to;
332 use LOC_LOCAL. */
333
334 /* DW_AT_frame_base values for the current function.
335 frame_base_reg is -1 if DW_AT_frame_base is missing, otherwise it
336 contains the register number for the frame register.
337 frame_base_offset is the offset from the frame register to the
338 virtual stack frame. */
339 static int frame_base_reg;
340 static CORE_ADDR frame_base_offset;
341
342 /* This value is added to each symbol value. FIXME: Generalize to
343 the section_offsets structure used by dbxread (once this is done,
344 pass the appropriate section number to end_symtab). */
345 static CORE_ADDR baseaddr; /* Add to each symbol value */
346
347 /* We put a pointer to this structure in the read_symtab_private field
348 of the psymtab.
349 The complete dwarf information for an objfile is kept in the
350 psymbol_obstack, so that absolute die references can be handled.
351 Most of the information in this structure is related to an entire
352 object file and could be passed via the sym_private field of the objfile.
353 It is however conceivable that dwarf2 might not be the only type
354 of symbols read from an object file. */
355
356 struct dwarf2_pinfo
357 {
358 /* Pointer to start of dwarf info buffer for the objfile. */
359
360 char *dwarf_info_buffer;
361
362 /* Offset in dwarf_info_buffer for this compilation unit. */
363
364 unsigned long dwarf_info_offset;
365
366 /* Pointer to start of dwarf abbreviation buffer for the objfile. */
367
368 char *dwarf_abbrev_buffer;
369
370 /* Size of dwarf abbreviation section for the objfile. */
371
372 unsigned int dwarf_abbrev_size;
373
374 /* Pointer to start of dwarf line buffer for the objfile. */
375
376 char *dwarf_line_buffer;
377 };
378
379 #define PST_PRIVATE(p) ((struct dwarf2_pinfo *)(p)->read_symtab_private)
380 #define DWARF_INFO_BUFFER(p) (PST_PRIVATE(p)->dwarf_info_buffer)
381 #define DWARF_INFO_OFFSET(p) (PST_PRIVATE(p)->dwarf_info_offset)
382 #define DWARF_ABBREV_BUFFER(p) (PST_PRIVATE(p)->dwarf_abbrev_buffer)
383 #define DWARF_ABBREV_SIZE(p) (PST_PRIVATE(p)->dwarf_abbrev_size)
384 #define DWARF_LINE_BUFFER(p) (PST_PRIVATE(p)->dwarf_line_buffer)
385
386 /* Maintain an array of referenced fundamental types for the current
387 compilation unit being read. For DWARF version 1, we have to construct
388 the fundamental types on the fly, since no information about the
389 fundamental types is supplied. Each such fundamental type is created by
390 calling a language dependent routine to create the type, and then a
391 pointer to that type is then placed in the array at the index specified
392 by it's FT_<TYPENAME> value. The array has a fixed size set by the
393 FT_NUM_MEMBERS compile time constant, which is the number of predefined
394 fundamental types gdb knows how to construct. */
395 static struct type *ftypes[FT_NUM_MEMBERS]; /* Fundamental types */
396
397 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
398 but this would require a corresponding change in unpack_field_as_long
399 and friends. */
400 static int bits_per_byte = 8;
401
402 /* The routines that read and process dies for a C struct or C++ class
403 pass lists of data member fields and lists of member function fields
404 in an instance of a field_info structure, as defined below. */
405 struct field_info
406 {
407 /* List of data member and baseclasses fields. */
408 struct nextfield
409 {
410 struct nextfield *next;
411 int accessibility;
412 int virtuality;
413 struct field field;
414 }
415 *fields;
416
417 /* Number of fields. */
418 int nfields;
419
420 /* Number of baseclasses. */
421 int nbaseclasses;
422
423 /* Set if the accesibility of one of the fields is not public. */
424 int non_public_fields;
425
426 /* Member function fields array, entries are allocated in the order they
427 are encountered in the object file. */
428 struct nextfnfield
429 {
430 struct nextfnfield *next;
431 struct fn_field fnfield;
432 }
433 *fnfields;
434
435 /* Member function fieldlist array, contains name of possibly overloaded
436 member function, number of overloaded member functions and a pointer
437 to the head of the member function field chain. */
438 struct fnfieldlist
439 {
440 char *name;
441 int length;
442 struct nextfnfield *head;
443 }
444 *fnfieldlists;
445
446 /* Number of entries in the fnfieldlists array. */
447 int nfnfields;
448 };
449
450 /* FIXME: Kludge to mark a varargs function type for C++ member function
451 argument processing. */
452 #define TYPE_FLAG_VARARGS (1 << 10)
453
454 /* Dwarf2 has no clean way to discern C++ static and non-static member
455 functions. G++ helps GDB by marking the first parameter for non-static
456 member functions (which is the this pointer) as artificial.
457 We pass this information between dwarf2_add_member_fn and
458 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
459 #define TYPE_FIELD_ARTIFICIAL TYPE_FIELD_BITPOS
460
461 /* Various complaints about symbol reading that don't abort the process */
462
463 static struct complaint dwarf2_const_ignored =
464 {
465 "type qualifier 'const' ignored", 0, 0
466 };
467 static struct complaint dwarf2_volatile_ignored =
468 {
469 "type qualifier 'volatile' ignored", 0, 0
470 };
471 static struct complaint dwarf2_non_const_array_bound_ignored =
472 {
473 "non-constant array bounds form '%s' ignored", 0, 0
474 };
475 static struct complaint dwarf2_missing_line_number_section =
476 {
477 "missing .debug_line section", 0, 0
478 };
479 static struct complaint dwarf2_mangled_line_number_section =
480 {
481 "mangled .debug_line section", 0, 0
482 };
483 static struct complaint dwarf2_unsupported_die_ref_attr =
484 {
485 "unsupported die ref attribute form: '%s'", 0, 0
486 };
487 static struct complaint dwarf2_unsupported_stack_op =
488 {
489 "unsupported stack op: '%s'", 0, 0
490 };
491 static struct complaint dwarf2_complex_location_expr =
492 {
493 "location expression too complex", 0, 0
494 };
495 static struct complaint dwarf2_unsupported_tag =
496 {
497 "unsupported tag: '%s'", 0, 0
498 };
499 static struct complaint dwarf2_unsupported_at_encoding =
500 {
501 "unsupported DW_AT_encoding: '%s'", 0, 0
502 };
503 static struct complaint dwarf2_unsupported_at_frame_base =
504 {
505 "unsupported DW_AT_frame_base for function '%s'", 0, 0
506 };
507 static struct complaint dwarf2_unexpected_tag =
508 {
509 "unexepected tag in read_type_die: '%s'", 0, 0
510 };
511 static struct complaint dwarf2_missing_at_frame_base =
512 {
513 "DW_AT_frame_base missing for DW_OP_fbreg", 0, 0
514 };
515 static struct complaint dwarf2_bad_static_member_name =
516 {
517 "unrecognized static data member name '%s'", 0, 0
518 };
519 static struct complaint dwarf2_unsupported_accessibility =
520 {
521 "unsupported accessibility %d", 0, 0
522 };
523 static struct complaint dwarf2_bad_member_name_complaint =
524 {
525 "cannot extract member name from '%s'", 0, 0
526 };
527 static struct complaint dwarf2_missing_member_fn_type_complaint =
528 {
529 "member function type missing for '%s'", 0, 0
530 };
531 static struct complaint dwarf2_vtbl_not_found_complaint =
532 {
533 "virtual function table pointer not found when defining class '%s'", 0, 0
534 };
535 static struct complaint dwarf2_absolute_sibling_complaint =
536 {
537 "ignoring absolute DW_AT_sibling", 0, 0
538 };
539 static struct complaint dwarf2_const_value_length_mismatch =
540 {
541 "const value length mismatch for '%s', got %d, expected %d", 0, 0
542 };
543 static struct complaint dwarf2_unsupported_const_value_attr =
544 {
545 "unsupported const value attribute form: '%s'", 0, 0
546 };
547
548 /* Remember the addr_size read from the dwarf.
549 If a target expects to link compilation units with differing address
550 sizes, gdb needs to be sure that the appropriate size is here for
551 whatever scope is currently getting read. */
552 static int address_size;
553
554 /* Externals references. */
555 extern int info_verbose; /* From main.c; nonzero => verbose */
556
557 /* local function prototypes */
558
559 static void dwarf2_locate_sections (bfd *, asection *, PTR);
560
561 #if 0
562 static void dwarf2_build_psymtabs_easy (struct objfile *, int);
563 #endif
564
565 static void dwarf2_build_psymtabs_hard (struct objfile *, int);
566
567 static char *scan_partial_symbols (char *, struct objfile *,
568 CORE_ADDR *, CORE_ADDR *);
569
570 static void add_partial_symbol (struct partial_die_info *, struct objfile *);
571
572 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
573
574 static void psymtab_to_symtab_1 (struct partial_symtab *);
575
576 static char *dwarf2_read_section (struct objfile *, file_ptr, unsigned int);
577
578 static void dwarf2_read_abbrevs (bfd *, unsigned int);
579
580 static void dwarf2_empty_abbrev_table (PTR);
581
582 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int);
583
584 static char *read_partial_die (struct partial_die_info *,
585 bfd *, char *, int *);
586
587 static char *read_full_die (struct die_info **, bfd *, char *);
588
589 static char *read_attribute (struct attribute *, struct attr_abbrev *,
590 bfd *, char *);
591
592 static unsigned int read_1_byte (bfd *, char *);
593
594 static int read_1_signed_byte (bfd *, char *);
595
596 static unsigned int read_2_bytes (bfd *, char *);
597
598 static unsigned int read_4_bytes (bfd *, char *);
599
600 static unsigned int read_8_bytes (bfd *, char *);
601
602 static CORE_ADDR read_address (bfd *, char *);
603
604 static char *read_n_bytes (bfd *, char *, unsigned int);
605
606 static char *read_string (bfd *, char *, unsigned int *);
607
608 static unsigned int read_unsigned_leb128 (bfd *, char *, unsigned int *);
609
610 static int read_signed_leb128 (bfd *, char *, unsigned int *);
611
612 static void set_cu_language (unsigned int);
613
614 static struct attribute *dwarf_attr (struct die_info *, unsigned int);
615
616 static int die_is_declaration (struct die_info *);
617
618 static void dwarf_decode_lines (unsigned int, char *, bfd *);
619
620 static void dwarf2_start_subfile (char *, char *);
621
622 static struct symbol *new_symbol (struct die_info *, struct type *,
623 struct objfile *);
624
625 static void dwarf2_const_value (struct attribute *, struct symbol *,
626 struct objfile *);
627
628 static void dwarf2_const_value_data (struct attribute *attr,
629 struct symbol *sym,
630 int bits);
631
632 static struct type *die_type (struct die_info *, struct objfile *);
633
634 static struct type *die_containing_type (struct die_info *, struct objfile *);
635
636 #if 0
637 static struct type *type_at_offset (unsigned int, struct objfile *);
638 #endif
639
640 static struct type *tag_type_to_type (struct die_info *, struct objfile *);
641
642 static void read_type_die (struct die_info *, struct objfile *);
643
644 static void read_typedef (struct die_info *, struct objfile *);
645
646 static void read_base_type (struct die_info *, struct objfile *);
647
648 static void read_file_scope (struct die_info *, struct objfile *);
649
650 static void read_func_scope (struct die_info *, struct objfile *);
651
652 static void read_lexical_block_scope (struct die_info *, struct objfile *);
653
654 static int dwarf2_get_pc_bounds (struct die_info *,
655 CORE_ADDR *, CORE_ADDR *, struct objfile *);
656
657 static void dwarf2_add_field (struct field_info *, struct die_info *,
658 struct objfile *);
659
660 static void dwarf2_attach_fields_to_type (struct field_info *,
661 struct type *, struct objfile *);
662
663 static void dwarf2_add_member_fn (struct field_info *,
664 struct die_info *, struct type *,
665 struct objfile *objfile);
666
667 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
668 struct type *, struct objfile *);
669
670 static void read_structure_scope (struct die_info *, struct objfile *);
671
672 static void read_common_block (struct die_info *, struct objfile *);
673
674 static void read_enumeration (struct die_info *, struct objfile *);
675
676 static struct type *dwarf_base_type (int, int, struct objfile *);
677
678 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct objfile *);
679
680 static void read_array_type (struct die_info *, struct objfile *);
681
682 static void read_tag_pointer_type (struct die_info *, struct objfile *);
683
684 static void read_tag_ptr_to_member_type (struct die_info *, struct objfile *);
685
686 static void read_tag_reference_type (struct die_info *, struct objfile *);
687
688 static void read_tag_const_type (struct die_info *, struct objfile *);
689
690 static void read_tag_volatile_type (struct die_info *, struct objfile *);
691
692 static void read_tag_string_type (struct die_info *, struct objfile *);
693
694 static void read_subroutine_type (struct die_info *, struct objfile *);
695
696 struct die_info *read_comp_unit (char *, bfd *);
697
698 static void free_die_list (struct die_info *);
699
700 static struct cleanup *make_cleanup_free_die_list (struct die_info *);
701
702 static void process_die (struct die_info *, struct objfile *);
703
704 static char *dwarf2_linkage_name (struct die_info *);
705
706 static char *dwarf_tag_name (unsigned int);
707
708 static char *dwarf_attr_name (unsigned int);
709
710 static char *dwarf_form_name (unsigned int);
711
712 static char *dwarf_stack_op_name (unsigned int);
713
714 static char *dwarf_bool_name (unsigned int);
715
716 static char *dwarf_type_encoding_name (unsigned int);
717
718 #if 0
719 static char *dwarf_cfi_name (unsigned int);
720
721 struct die_info *copy_die (struct die_info *);
722 #endif
723
724 struct die_info *sibling_die (struct die_info *);
725
726 void dump_die (struct die_info *);
727
728 void dump_die_list (struct die_info *);
729
730 void store_in_ref_table (unsigned int, struct die_info *);
731
732 static void dwarf2_empty_die_ref_table (void);
733
734 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
735
736 struct die_info *follow_die_ref (unsigned int);
737
738 static struct type *dwarf2_fundamental_type (struct objfile *, int);
739
740 /* memory allocation interface */
741
742 static void dwarf2_free_tmp_obstack (PTR);
743
744 static struct dwarf_block *dwarf_alloc_block (void);
745
746 static struct abbrev_info *dwarf_alloc_abbrev (void);
747
748 static struct die_info *dwarf_alloc_die (void);
749
750 /* Try to locate the sections we need for DWARF 2 debugging
751 information and return true if we have enough to do something. */
752
753 int
754 dwarf2_has_info (abfd)
755 bfd *abfd;
756 {
757 dwarf_info_offset = dwarf_abbrev_offset = dwarf_line_offset = 0;
758 bfd_map_over_sections (abfd, dwarf2_locate_sections, NULL);
759 if (dwarf_info_offset && dwarf_abbrev_offset)
760 {
761 return 1;
762 }
763 else
764 {
765 return 0;
766 }
767 }
768
769 /* This function is mapped across the sections and remembers the
770 offset and size of each of the debugging sections we are interested
771 in. */
772
773 static void
774 dwarf2_locate_sections (ignore_abfd, sectp, ignore_ptr)
775 bfd *ignore_abfd;
776 asection *sectp;
777 PTR ignore_ptr;
778 {
779 if (STREQ (sectp->name, INFO_SECTION))
780 {
781 dwarf_info_offset = sectp->filepos;
782 dwarf_info_size = bfd_get_section_size_before_reloc (sectp);
783 }
784 else if (STREQ (sectp->name, ABBREV_SECTION))
785 {
786 dwarf_abbrev_offset = sectp->filepos;
787 dwarf_abbrev_size = bfd_get_section_size_before_reloc (sectp);
788 }
789 else if (STREQ (sectp->name, LINE_SECTION))
790 {
791 dwarf_line_offset = sectp->filepos;
792 dwarf_line_size = bfd_get_section_size_before_reloc (sectp);
793 }
794 else if (STREQ (sectp->name, PUBNAMES_SECTION))
795 {
796 dwarf_pubnames_offset = sectp->filepos;
797 dwarf_pubnames_size = bfd_get_section_size_before_reloc (sectp);
798 }
799 else if (STREQ (sectp->name, ARANGES_SECTION))
800 {
801 dwarf_aranges_offset = sectp->filepos;
802 dwarf_aranges_size = bfd_get_section_size_before_reloc (sectp);
803 }
804 else if (STREQ (sectp->name, LOC_SECTION))
805 {
806 dwarf_loc_offset = sectp->filepos;
807 dwarf_loc_size = bfd_get_section_size_before_reloc (sectp);
808 }
809 else if (STREQ (sectp->name, MACINFO_SECTION))
810 {
811 dwarf_macinfo_offset = sectp->filepos;
812 dwarf_macinfo_size = bfd_get_section_size_before_reloc (sectp);
813 }
814 else if (STREQ (sectp->name, STR_SECTION))
815 {
816 dwarf_str_offset = sectp->filepos;
817 dwarf_str_size = bfd_get_section_size_before_reloc (sectp);
818 }
819 }
820
821 /* Build a partial symbol table. */
822
823 void
824 dwarf2_build_psymtabs (objfile, mainline)
825 struct objfile *objfile;
826 int mainline;
827 {
828
829 /* We definitely need the .debug_info and .debug_abbrev sections */
830
831 dwarf_info_buffer = dwarf2_read_section (objfile,
832 dwarf_info_offset,
833 dwarf_info_size);
834 dwarf_abbrev_buffer = dwarf2_read_section (objfile,
835 dwarf_abbrev_offset,
836 dwarf_abbrev_size);
837 dwarf_line_buffer = dwarf2_read_section (objfile,
838 dwarf_line_offset,
839 dwarf_line_size);
840
841 if (mainline || objfile->global_psymbols.size == 0 ||
842 objfile->static_psymbols.size == 0)
843 {
844 init_psymbol_list (objfile, 1024);
845 }
846
847 #if 0
848 if (dwarf_aranges_offset && dwarf_pubnames_offset)
849 {
850 /* Things are significantly easier if we have .debug_aranges and
851 .debug_pubnames sections */
852
853 dwarf2_build_psymtabs_easy (objfile, mainline);
854 }
855 else
856 #endif
857 /* only test this case for now */
858 {
859 /* In this case we have to work a bit harder */
860 dwarf2_build_psymtabs_hard (objfile, mainline);
861 }
862 }
863
864 #if 0
865 /* Build the partial symbol table from the information in the
866 .debug_pubnames and .debug_aranges sections. */
867
868 static void
869 dwarf2_build_psymtabs_easy (objfile, mainline)
870 struct objfile *objfile;
871 int mainline;
872 {
873 bfd *abfd = objfile->obfd;
874 char *aranges_buffer, *pubnames_buffer;
875 char *aranges_ptr, *pubnames_ptr;
876 unsigned int entry_length, version, info_offset, info_size;
877
878 pubnames_buffer = dwarf2_read_section (objfile,
879 dwarf_pubnames_offset,
880 dwarf_pubnames_size);
881 pubnames_ptr = pubnames_buffer;
882 while ((pubnames_ptr - pubnames_buffer) < dwarf_pubnames_size)
883 {
884 entry_length = read_4_bytes (abfd, pubnames_ptr);
885 pubnames_ptr += 4;
886 version = read_1_byte (abfd, pubnames_ptr);
887 pubnames_ptr += 1;
888 info_offset = read_4_bytes (abfd, pubnames_ptr);
889 pubnames_ptr += 4;
890 info_size = read_4_bytes (abfd, pubnames_ptr);
891 pubnames_ptr += 4;
892 }
893
894 aranges_buffer = dwarf2_read_section (objfile,
895 dwarf_aranges_offset,
896 dwarf_aranges_size);
897
898 }
899 #endif
900
901 /* Build the partial symbol table by doing a quick pass through the
902 .debug_info and .debug_abbrev sections. */
903
904 static void
905 dwarf2_build_psymtabs_hard (objfile, mainline)
906 struct objfile *objfile;
907 int mainline;
908 {
909 /* Instead of reading this into a big buffer, we should probably use
910 mmap() on architectures that support it. (FIXME) */
911 bfd *abfd = objfile->obfd;
912 char *info_ptr, *abbrev_ptr;
913 char *beg_of_comp_unit;
914 struct comp_unit_head cu_header;
915 struct partial_die_info comp_unit_die;
916 struct partial_symtab *pst;
917 struct cleanup *back_to;
918 int comp_unit_has_pc_info;
919 CORE_ADDR lowpc, highpc;
920
921 info_ptr = dwarf_info_buffer;
922 abbrev_ptr = dwarf_abbrev_buffer;
923
924 obstack_init (&dwarf2_tmp_obstack);
925 back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
926
927 while ((unsigned int) (info_ptr - dwarf_info_buffer)
928 + ((info_ptr - dwarf_info_buffer) % 4) < dwarf_info_size)
929 {
930 beg_of_comp_unit = info_ptr;
931 cu_header.length = read_4_bytes (abfd, info_ptr);
932 info_ptr += 4;
933 cu_header.version = read_2_bytes (abfd, info_ptr);
934 info_ptr += 2;
935 cu_header.abbrev_offset = read_4_bytes (abfd, info_ptr);
936 info_ptr += 4;
937 cu_header.addr_size = read_1_byte (abfd, info_ptr);
938 info_ptr += 1;
939 address_size = cu_header.addr_size;
940
941 if (cu_header.version != 2)
942 {
943 error ("Dwarf Error: wrong version in compilation unit header.");
944 return;
945 }
946 if (cu_header.abbrev_offset >= dwarf_abbrev_size)
947 {
948 error ("Dwarf Error: bad offset (0x%lx) in compilation unit header (offset 0x%lx + 6).",
949 (long) cu_header.abbrev_offset,
950 (long) (beg_of_comp_unit - dwarf_info_buffer));
951 return;
952 }
953 if (beg_of_comp_unit + cu_header.length + 4
954 > dwarf_info_buffer + dwarf_info_size)
955 {
956 error ("Dwarf Error: bad length (0x%lx) in compilation unit header (offset 0x%lx + 0).",
957 (long) cu_header.length,
958 (long) (beg_of_comp_unit - dwarf_info_buffer));
959 return;
960 }
961 /* Read the abbrevs for this compilation unit into a table */
962 dwarf2_read_abbrevs (abfd, cu_header.abbrev_offset);
963 make_cleanup (dwarf2_empty_abbrev_table, NULL);
964
965 /* Read the compilation unit die */
966 info_ptr = read_partial_die (&comp_unit_die, abfd,
967 info_ptr, &comp_unit_has_pc_info);
968
969 /* Set the language we're debugging */
970 set_cu_language (comp_unit_die.language);
971
972 /* Allocate a new partial symbol table structure */
973 pst = start_psymtab_common (objfile, objfile->section_offsets,
974 comp_unit_die.name ? comp_unit_die.name : "",
975 comp_unit_die.lowpc,
976 objfile->global_psymbols.next,
977 objfile->static_psymbols.next);
978
979 pst->read_symtab_private = (char *)
980 obstack_alloc (&objfile->psymbol_obstack, sizeof (struct dwarf2_pinfo));
981 cu_header_offset = beg_of_comp_unit - dwarf_info_buffer;
982 DWARF_INFO_BUFFER (pst) = dwarf_info_buffer;
983 DWARF_INFO_OFFSET (pst) = beg_of_comp_unit - dwarf_info_buffer;
984 DWARF_ABBREV_BUFFER (pst) = dwarf_abbrev_buffer;
985 DWARF_ABBREV_SIZE (pst) = dwarf_abbrev_size;
986 DWARF_LINE_BUFFER (pst) = dwarf_line_buffer;
987 baseaddr = ANOFFSET (objfile->section_offsets, 0);
988
989 /* Store the function that reads in the rest of the symbol table */
990 pst->read_symtab = dwarf2_psymtab_to_symtab;
991
992 /* Check if comp unit has_children.
993 If so, read the rest of the partial symbols from this comp unit.
994 If not, there's no more debug_info for this comp unit. */
995 if (comp_unit_die.has_children)
996 {
997 info_ptr = scan_partial_symbols (info_ptr, objfile, &lowpc, &highpc);
998
999 /* If the compilation unit didn't have an explicit address range,
1000 then use the information extracted from its child dies. */
1001 if (!comp_unit_has_pc_info)
1002 {
1003 comp_unit_die.lowpc = lowpc;
1004 comp_unit_die.highpc = highpc;
1005 }
1006 }
1007 pst->textlow = comp_unit_die.lowpc + baseaddr;
1008 pst->texthigh = comp_unit_die.highpc + baseaddr;
1009
1010 pst->n_global_syms = objfile->global_psymbols.next -
1011 (objfile->global_psymbols.list + pst->globals_offset);
1012 pst->n_static_syms = objfile->static_psymbols.next -
1013 (objfile->static_psymbols.list + pst->statics_offset);
1014 sort_pst_symbols (pst);
1015
1016 /* If there is already a psymtab or symtab for a file of this
1017 name, remove it. (If there is a symtab, more drastic things
1018 also happen.) This happens in VxWorks. */
1019 free_named_symtabs (pst->filename);
1020
1021 info_ptr = beg_of_comp_unit + cu_header.length + 4;
1022 }
1023 do_cleanups (back_to);
1024 }
1025
1026 /* Read in all interesting dies to the end of the compilation unit. */
1027
1028 static char *
1029 scan_partial_symbols (info_ptr, objfile, lowpc, highpc)
1030 char *info_ptr;
1031 struct objfile *objfile;
1032 CORE_ADDR *lowpc;
1033 CORE_ADDR *highpc;
1034 {
1035 bfd *abfd = objfile->obfd;
1036 struct partial_die_info pdi;
1037
1038 /* This function is called after we've read in the comp_unit_die in
1039 order to read its children. We start the nesting level at 1 since
1040 we have pushed 1 level down in order to read the comp unit's children.
1041 The comp unit itself is at level 0, so we stop reading when we pop
1042 back to that level. */
1043
1044 int nesting_level = 1;
1045 int has_pc_info;
1046
1047 *lowpc = ((CORE_ADDR) -1);
1048 *highpc = ((CORE_ADDR) 0);
1049
1050 while (nesting_level)
1051 {
1052 info_ptr = read_partial_die (&pdi, abfd, info_ptr, &has_pc_info);
1053
1054 if (pdi.name)
1055 {
1056 switch (pdi.tag)
1057 {
1058 case DW_TAG_subprogram:
1059 if (has_pc_info)
1060 {
1061 if (pdi.lowpc < *lowpc)
1062 {
1063 *lowpc = pdi.lowpc;
1064 }
1065 if (pdi.highpc > *highpc)
1066 {
1067 *highpc = pdi.highpc;
1068 }
1069 if ((pdi.is_external || nesting_level == 1)
1070 && !pdi.is_declaration)
1071 {
1072 add_partial_symbol (&pdi, objfile);
1073 }
1074 }
1075 break;
1076 case DW_TAG_variable:
1077 case DW_TAG_typedef:
1078 case DW_TAG_class_type:
1079 case DW_TAG_structure_type:
1080 case DW_TAG_union_type:
1081 case DW_TAG_enumeration_type:
1082 if ((pdi.is_external || nesting_level == 1)
1083 && !pdi.is_declaration)
1084 {
1085 add_partial_symbol (&pdi, objfile);
1086 }
1087 break;
1088 case DW_TAG_enumerator:
1089 /* File scope enumerators are added to the partial symbol
1090 table. */
1091 if (nesting_level == 2)
1092 add_partial_symbol (&pdi, objfile);
1093 break;
1094 case DW_TAG_base_type:
1095 /* File scope base type definitions are added to the partial
1096 symbol table. */
1097 if (nesting_level == 1)
1098 add_partial_symbol (&pdi, objfile);
1099 break;
1100 default:
1101 break;
1102 }
1103 }
1104
1105 /* If the die has a sibling, skip to the sibling.
1106 Do not skip enumeration types, we want to record their
1107 enumerators. */
1108 if (pdi.sibling && pdi.tag != DW_TAG_enumeration_type)
1109 {
1110 info_ptr = pdi.sibling;
1111 }
1112 else if (pdi.has_children)
1113 {
1114 /* Die has children, but the optional DW_AT_sibling attribute
1115 is missing. */
1116 nesting_level++;
1117 }
1118
1119 if (pdi.tag == 0)
1120 {
1121 nesting_level--;
1122 }
1123 }
1124
1125 /* If we didn't find a lowpc, set it to highpc to avoid complaints
1126 from `maint check'. */
1127 if (*lowpc == ((CORE_ADDR) -1))
1128 *lowpc = *highpc;
1129 return info_ptr;
1130 }
1131
1132 static void
1133 add_partial_symbol (pdi, objfile)
1134 struct partial_die_info *pdi;
1135 struct objfile *objfile;
1136 {
1137 CORE_ADDR addr = 0;
1138
1139 switch (pdi->tag)
1140 {
1141 case DW_TAG_subprogram:
1142 if (pdi->is_external)
1143 {
1144 /*prim_record_minimal_symbol (pdi->name, pdi->lowpc + baseaddr,
1145 mst_text, objfile); */
1146 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1147 VAR_NAMESPACE, LOC_BLOCK,
1148 &objfile->global_psymbols,
1149 0, pdi->lowpc + baseaddr, cu_language, objfile);
1150 }
1151 else
1152 {
1153 /*prim_record_minimal_symbol (pdi->name, pdi->lowpc + baseaddr,
1154 mst_file_text, objfile); */
1155 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1156 VAR_NAMESPACE, LOC_BLOCK,
1157 &objfile->static_psymbols,
1158 0, pdi->lowpc + baseaddr, cu_language, objfile);
1159 }
1160 break;
1161 case DW_TAG_variable:
1162 if (pdi->is_external)
1163 {
1164 /* Global Variable.
1165 Don't enter into the minimal symbol tables as there is
1166 a minimal symbol table entry from the ELF symbols already.
1167 Enter into partial symbol table if it has a location
1168 descriptor or a type.
1169 If the location descriptor is missing, new_symbol will create
1170 a LOC_UNRESOLVED symbol, the address of the variable will then
1171 be determined from the minimal symbol table whenever the variable
1172 is referenced.
1173 The address for the partial symbol table entry is not
1174 used by GDB, but it comes in handy for debugging partial symbol
1175 table building. */
1176
1177 if (pdi->locdesc)
1178 addr = decode_locdesc (pdi->locdesc, objfile);
1179 if (pdi->locdesc || pdi->has_type)
1180 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1181 VAR_NAMESPACE, LOC_STATIC,
1182 &objfile->global_psymbols,
1183 0, addr + baseaddr, cu_language, objfile);
1184 }
1185 else
1186 {
1187 /* Static Variable. Skip symbols without location descriptors. */
1188 if (pdi->locdesc == NULL)
1189 return;
1190 addr = decode_locdesc (pdi->locdesc, objfile);
1191 /*prim_record_minimal_symbol (pdi->name, addr + baseaddr,
1192 mst_file_data, objfile); */
1193 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1194 VAR_NAMESPACE, LOC_STATIC,
1195 &objfile->static_psymbols,
1196 0, addr + baseaddr, cu_language, objfile);
1197 }
1198 break;
1199 case DW_TAG_typedef:
1200 case DW_TAG_base_type:
1201 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1202 VAR_NAMESPACE, LOC_TYPEDEF,
1203 &objfile->static_psymbols,
1204 0, (CORE_ADDR) 0, cu_language, objfile);
1205 break;
1206 case DW_TAG_class_type:
1207 case DW_TAG_structure_type:
1208 case DW_TAG_union_type:
1209 case DW_TAG_enumeration_type:
1210 /* Skip aggregate types without children, these are external
1211 references. */
1212 if (pdi->has_children == 0)
1213 return;
1214 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1215 STRUCT_NAMESPACE, LOC_TYPEDEF,
1216 &objfile->static_psymbols,
1217 0, (CORE_ADDR) 0, cu_language, objfile);
1218
1219 if (cu_language == language_cplus)
1220 {
1221 /* For C++, these implicitly act as typedefs as well. */
1222 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1223 VAR_NAMESPACE, LOC_TYPEDEF,
1224 &objfile->static_psymbols,
1225 0, (CORE_ADDR) 0, cu_language, objfile);
1226 }
1227 break;
1228 case DW_TAG_enumerator:
1229 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1230 VAR_NAMESPACE, LOC_CONST,
1231 &objfile->static_psymbols,
1232 0, (CORE_ADDR) 0, cu_language, objfile);
1233 break;
1234 default:
1235 break;
1236 }
1237 }
1238
1239 /* Expand this partial symbol table into a full symbol table. */
1240
1241 static void
1242 dwarf2_psymtab_to_symtab (pst)
1243 struct partial_symtab *pst;
1244 {
1245 /* FIXME: This is barely more than a stub. */
1246 if (pst != NULL)
1247 {
1248 if (pst->readin)
1249 {
1250 warning ("bug: psymtab for %s is already read in.", pst->filename);
1251 }
1252 else
1253 {
1254 if (info_verbose)
1255 {
1256 printf_filtered ("Reading in symbols for %s...", pst->filename);
1257 gdb_flush (gdb_stdout);
1258 }
1259
1260 psymtab_to_symtab_1 (pst);
1261
1262 /* Finish up the debug error message. */
1263 if (info_verbose)
1264 printf_filtered ("done.\n");
1265 }
1266 }
1267 }
1268
1269 static void
1270 psymtab_to_symtab_1 (pst)
1271 struct partial_symtab *pst;
1272 {
1273 struct objfile *objfile = pst->objfile;
1274 bfd *abfd = objfile->obfd;
1275 struct comp_unit_head cu_header;
1276 struct die_info *dies;
1277 unsigned long offset;
1278 CORE_ADDR lowpc, highpc;
1279 struct die_info *child_die;
1280 char *info_ptr;
1281 struct symtab *symtab;
1282 struct cleanup *back_to;
1283
1284 /* Set local variables from the partial symbol table info. */
1285 offset = DWARF_INFO_OFFSET (pst);
1286 dwarf_info_buffer = DWARF_INFO_BUFFER (pst);
1287 dwarf_abbrev_buffer = DWARF_ABBREV_BUFFER (pst);
1288 dwarf_abbrev_size = DWARF_ABBREV_SIZE (pst);
1289 dwarf_line_buffer = DWARF_LINE_BUFFER (pst);
1290 baseaddr = ANOFFSET (pst->section_offsets, 0);
1291 cu_header_offset = offset;
1292 info_ptr = dwarf_info_buffer + offset;
1293
1294 obstack_init (&dwarf2_tmp_obstack);
1295 back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
1296
1297 buildsym_init ();
1298 make_cleanup (really_free_pendings, NULL);
1299
1300 /* read in the comp_unit header */
1301 cu_header.length = read_4_bytes (abfd, info_ptr);
1302 info_ptr += 4;
1303 cu_header.version = read_2_bytes (abfd, info_ptr);
1304 info_ptr += 2;
1305 cu_header.abbrev_offset = read_4_bytes (abfd, info_ptr);
1306 info_ptr += 4;
1307 cu_header.addr_size = read_1_byte (abfd, info_ptr);
1308 info_ptr += 1;
1309
1310 /* Read the abbrevs for this compilation unit */
1311 dwarf2_read_abbrevs (abfd, cu_header.abbrev_offset);
1312 make_cleanup (dwarf2_empty_abbrev_table, NULL);
1313
1314 dies = read_comp_unit (info_ptr, abfd);
1315
1316 make_cleanup_free_die_list (dies);
1317
1318 /* Do line number decoding in read_file_scope () */
1319 process_die (dies, objfile);
1320
1321 if (!dwarf2_get_pc_bounds (dies, &lowpc, &highpc, objfile))
1322 {
1323 /* Some compilers don't define a DW_AT_high_pc attribute for
1324 the compilation unit. If the DW_AT_high_pc is missing,
1325 synthesize it, by scanning the DIE's below the compilation unit. */
1326 highpc = 0;
1327 if (dies->has_children)
1328 {
1329 child_die = dies->next;
1330 while (child_die && child_die->tag)
1331 {
1332 if (child_die->tag == DW_TAG_subprogram)
1333 {
1334 CORE_ADDR low, high;
1335
1336 if (dwarf2_get_pc_bounds (child_die, &low, &high, objfile))
1337 {
1338 highpc = max (highpc, high);
1339 }
1340 }
1341 child_die = sibling_die (child_die);
1342 }
1343 }
1344 }
1345 symtab = end_symtab (highpc + baseaddr, objfile, 0);
1346
1347 /* Set symtab language to language from DW_AT_language.
1348 If the compilation is from a C file generated by language preprocessors,
1349 do not set the language if it was already deduced by start_subfile. */
1350 if (symtab != NULL
1351 && !(cu_language == language_c && symtab->language != language_c))
1352 {
1353 symtab->language = cu_language;
1354 }
1355 pst->symtab = symtab;
1356 pst->readin = 1;
1357 sort_symtab_syms (pst->symtab);
1358
1359 do_cleanups (back_to);
1360 }
1361
1362 /* Process a die and its children. */
1363
1364 static void
1365 process_die (die, objfile)
1366 struct die_info *die;
1367 struct objfile *objfile;
1368 {
1369 switch (die->tag)
1370 {
1371 case DW_TAG_padding:
1372 break;
1373 case DW_TAG_compile_unit:
1374 read_file_scope (die, objfile);
1375 break;
1376 case DW_TAG_subprogram:
1377 read_subroutine_type (die, objfile);
1378 read_func_scope (die, objfile);
1379 break;
1380 case DW_TAG_inlined_subroutine:
1381 /* FIXME: These are ignored for now.
1382 They could be used to set breakpoints on all inlined instances
1383 of a function and make GDB `next' properly over inlined functions. */
1384 break;
1385 case DW_TAG_lexical_block:
1386 read_lexical_block_scope (die, objfile);
1387 break;
1388 case DW_TAG_class_type:
1389 case DW_TAG_structure_type:
1390 case DW_TAG_union_type:
1391 read_structure_scope (die, objfile);
1392 break;
1393 case DW_TAG_enumeration_type:
1394 read_enumeration (die, objfile);
1395 break;
1396 case DW_TAG_subroutine_type:
1397 read_subroutine_type (die, objfile);
1398 break;
1399 case DW_TAG_array_type:
1400 read_array_type (die, objfile);
1401 break;
1402 case DW_TAG_pointer_type:
1403 read_tag_pointer_type (die, objfile);
1404 break;
1405 case DW_TAG_ptr_to_member_type:
1406 read_tag_ptr_to_member_type (die, objfile);
1407 break;
1408 case DW_TAG_reference_type:
1409 read_tag_reference_type (die, objfile);
1410 break;
1411 case DW_TAG_string_type:
1412 read_tag_string_type (die, objfile);
1413 break;
1414 case DW_TAG_base_type:
1415 read_base_type (die, objfile);
1416 if (dwarf_attr (die, DW_AT_name))
1417 {
1418 /* Add a typedef symbol for the base type definition. */
1419 new_symbol (die, die->type, objfile);
1420 }
1421 break;
1422 case DW_TAG_common_block:
1423 read_common_block (die, objfile);
1424 break;
1425 case DW_TAG_common_inclusion:
1426 break;
1427 default:
1428 new_symbol (die, NULL, objfile);
1429 break;
1430 }
1431 }
1432
1433 static void
1434 read_file_scope (die, objfile)
1435 struct die_info *die;
1436 struct objfile *objfile;
1437 {
1438 unsigned int line_offset = 0;
1439 CORE_ADDR lowpc = ((CORE_ADDR) -1);
1440 CORE_ADDR highpc = ((CORE_ADDR) 0);
1441 struct attribute *attr;
1442 char *name = "<unknown>";
1443 char *comp_dir = NULL;
1444 struct die_info *child_die;
1445 bfd *abfd = objfile->obfd;
1446
1447 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, objfile))
1448 {
1449 if (die->has_children)
1450 {
1451 child_die = die->next;
1452 while (child_die && child_die->tag)
1453 {
1454 if (child_die->tag == DW_TAG_subprogram)
1455 {
1456 CORE_ADDR low, high;
1457
1458 if (dwarf2_get_pc_bounds (child_die, &low, &high, objfile))
1459 {
1460 lowpc = min (lowpc, low);
1461 highpc = max (highpc, high);
1462 }
1463 }
1464 child_die = sibling_die (child_die);
1465 }
1466 }
1467 }
1468
1469 /* If we didn't find a lowpc, set it to highpc to avoid complaints
1470 from finish_block. */
1471 if (lowpc == ((CORE_ADDR) -1))
1472 lowpc = highpc;
1473 lowpc += baseaddr;
1474 highpc += baseaddr;
1475
1476 attr = dwarf_attr (die, DW_AT_name);
1477 if (attr)
1478 {
1479 name = DW_STRING (attr);
1480 }
1481 attr = dwarf_attr (die, DW_AT_comp_dir);
1482 if (attr)
1483 {
1484 comp_dir = DW_STRING (attr);
1485 if (comp_dir)
1486 {
1487 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1488 directory, get rid of it. */
1489 char *cp = strchr (comp_dir, ':');
1490
1491 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1492 comp_dir = cp + 1;
1493 }
1494 }
1495
1496 if (objfile->ei.entry_point >= lowpc &&
1497 objfile->ei.entry_point < highpc)
1498 {
1499 objfile->ei.entry_file_lowpc = lowpc;
1500 objfile->ei.entry_file_highpc = highpc;
1501 }
1502
1503 attr = dwarf_attr (die, DW_AT_language);
1504 if (attr)
1505 {
1506 set_cu_language (DW_UNSND (attr));
1507 }
1508
1509 /* We assume that we're processing GCC output. */
1510 processing_gcc_compilation = 2;
1511 #if 0
1512 /* FIXME:Do something here. */
1513 if (dip->at_producer != NULL)
1514 {
1515 handle_producer (dip->at_producer);
1516 }
1517 #endif
1518
1519 /* The compilation unit may be in a different language or objfile,
1520 zero out all remembered fundamental types. */
1521 memset (ftypes, 0, FT_NUM_MEMBERS * sizeof (struct type *));
1522
1523 start_symtab (name, comp_dir, lowpc);
1524 record_debugformat ("DWARF 2");
1525
1526 /* Decode line number information if present. */
1527 attr = dwarf_attr (die, DW_AT_stmt_list);
1528 if (attr)
1529 {
1530 line_offset = DW_UNSND (attr);
1531 dwarf_decode_lines (line_offset, comp_dir, abfd);
1532 }
1533
1534 /* Process all dies in compilation unit. */
1535 if (die->has_children)
1536 {
1537 child_die = die->next;
1538 while (child_die && child_die->tag)
1539 {
1540 process_die (child_die, objfile);
1541 child_die = sibling_die (child_die);
1542 }
1543 }
1544 }
1545
1546 static void
1547 read_func_scope (die, objfile)
1548 struct die_info *die;
1549 struct objfile *objfile;
1550 {
1551 register struct context_stack *new;
1552 CORE_ADDR lowpc;
1553 CORE_ADDR highpc;
1554 struct die_info *child_die;
1555 struct attribute *attr;
1556 char *name;
1557
1558 name = dwarf2_linkage_name (die);
1559
1560 /* Ignore functions with missing or empty names and functions with
1561 missing or invalid low and high pc attributes. */
1562 if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, objfile))
1563 return;
1564
1565 lowpc += baseaddr;
1566 highpc += baseaddr;
1567
1568 if (objfile->ei.entry_point >= lowpc &&
1569 objfile->ei.entry_point < highpc)
1570 {
1571 objfile->ei.entry_func_lowpc = lowpc;
1572 objfile->ei.entry_func_highpc = highpc;
1573 }
1574
1575 /* Decode DW_AT_frame_base location descriptor if present, keep result
1576 for DW_OP_fbreg operands in decode_locdesc. */
1577 frame_base_reg = -1;
1578 frame_base_offset = 0;
1579 attr = dwarf_attr (die, DW_AT_frame_base);
1580 if (attr)
1581 {
1582 CORE_ADDR addr = decode_locdesc (DW_BLOCK (attr), objfile);
1583 if (isderef)
1584 complain (&dwarf2_unsupported_at_frame_base, name);
1585 else if (isreg)
1586 frame_base_reg = addr;
1587 else if (offreg)
1588 {
1589 frame_base_reg = basereg;
1590 frame_base_offset = addr;
1591 }
1592 else
1593 complain (&dwarf2_unsupported_at_frame_base, name);
1594 }
1595
1596 new = push_context (0, lowpc);
1597 new->name = new_symbol (die, die->type, objfile);
1598 list_in_scope = &local_symbols;
1599
1600 if (die->has_children)
1601 {
1602 child_die = die->next;
1603 while (child_die && child_die->tag)
1604 {
1605 process_die (child_die, objfile);
1606 child_die = sibling_die (child_die);
1607 }
1608 }
1609
1610 new = pop_context ();
1611 /* Make a block for the local symbols within. */
1612 finish_block (new->name, &local_symbols, new->old_blocks,
1613 lowpc, highpc, objfile);
1614 list_in_scope = &file_symbols;
1615 }
1616
1617 /* Process all the DIES contained within a lexical block scope. Start
1618 a new scope, process the dies, and then close the scope. */
1619
1620 static void
1621 read_lexical_block_scope (die, objfile)
1622 struct die_info *die;
1623 struct objfile *objfile;
1624 {
1625 register struct context_stack *new;
1626 CORE_ADDR lowpc, highpc;
1627 struct die_info *child_die;
1628
1629 /* Ignore blocks with missing or invalid low and high pc attributes. */
1630 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, objfile))
1631 return;
1632 lowpc += baseaddr;
1633 highpc += baseaddr;
1634
1635 push_context (0, lowpc);
1636 if (die->has_children)
1637 {
1638 child_die = die->next;
1639 while (child_die && child_die->tag)
1640 {
1641 process_die (child_die, objfile);
1642 child_die = sibling_die (child_die);
1643 }
1644 }
1645 new = pop_context ();
1646
1647 if (local_symbols != NULL)
1648 {
1649 finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
1650 highpc, objfile);
1651 }
1652 local_symbols = new->locals;
1653 }
1654
1655 /* Get low and high pc attributes from a die.
1656 Return 1 if the attributes are present and valid, otherwise, return 0. */
1657
1658 static int
1659 dwarf2_get_pc_bounds (die, lowpc, highpc, objfile)
1660 struct die_info *die;
1661 CORE_ADDR *lowpc;
1662 CORE_ADDR *highpc;
1663 struct objfile *objfile;
1664 {
1665 struct attribute *attr;
1666 CORE_ADDR low;
1667 CORE_ADDR high;
1668
1669 attr = dwarf_attr (die, DW_AT_low_pc);
1670 if (attr)
1671 low = DW_ADDR (attr);
1672 else
1673 return 0;
1674 attr = dwarf_attr (die, DW_AT_high_pc);
1675 if (attr)
1676 high = DW_ADDR (attr);
1677 else
1678 return 0;
1679
1680 if (high < low)
1681 return 0;
1682
1683 /* When using the GNU linker, .gnu.linkonce. sections are used to
1684 eliminate duplicate copies of functions and vtables and such.
1685 The linker will arbitrarily choose one and discard the others.
1686 The AT_*_pc values for such functions refer to local labels in
1687 these sections. If the section from that file was discarded, the
1688 labels are not in the output, so the relocs get a value of 0.
1689 If this is a discarded function, mark the pc bounds as invalid,
1690 so that GDB will ignore it. */
1691 if (low == 0 && (bfd_get_file_flags (objfile->obfd) & HAS_RELOC) == 0)
1692 return 0;
1693
1694 *lowpc = low;
1695 *highpc = high;
1696 return 1;
1697 }
1698
1699 /* Add an aggregate field to the field list. */
1700
1701 static void
1702 dwarf2_add_field (fip, die, objfile)
1703 struct field_info *fip;
1704 struct die_info *die;
1705 struct objfile *objfile;
1706 {
1707 struct nextfield *new_field;
1708 struct attribute *attr;
1709 struct field *fp;
1710 char *fieldname = "";
1711
1712 /* Allocate a new field list entry and link it in. */
1713 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
1714 make_cleanup (free, new_field);
1715 memset (new_field, 0, sizeof (struct nextfield));
1716 new_field->next = fip->fields;
1717 fip->fields = new_field;
1718 fip->nfields++;
1719
1720 /* Handle accessibility and virtuality of field.
1721 The default accessibility for members is public, the default
1722 accessibility for inheritance is private. */
1723 if (die->tag != DW_TAG_inheritance)
1724 new_field->accessibility = DW_ACCESS_public;
1725 else
1726 new_field->accessibility = DW_ACCESS_private;
1727 new_field->virtuality = DW_VIRTUALITY_none;
1728
1729 attr = dwarf_attr (die, DW_AT_accessibility);
1730 if (attr)
1731 new_field->accessibility = DW_UNSND (attr);
1732 if (new_field->accessibility != DW_ACCESS_public)
1733 fip->non_public_fields = 1;
1734 attr = dwarf_attr (die, DW_AT_virtuality);
1735 if (attr)
1736 new_field->virtuality = DW_UNSND (attr);
1737
1738 fp = &new_field->field;
1739 if (die->tag == DW_TAG_member)
1740 {
1741 /* Get type of field. */
1742 fp->type = die_type (die, objfile);
1743
1744 /* Get bit size of field (zero if none). */
1745 attr = dwarf_attr (die, DW_AT_bit_size);
1746 if (attr)
1747 {
1748 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
1749 }
1750 else
1751 {
1752 FIELD_BITSIZE (*fp) = 0;
1753 }
1754
1755 /* Get bit offset of field. */
1756 attr = dwarf_attr (die, DW_AT_data_member_location);
1757 if (attr)
1758 {
1759 FIELD_BITPOS (*fp) =
1760 decode_locdesc (DW_BLOCK (attr), objfile) * bits_per_byte;
1761 }
1762 else
1763 FIELD_BITPOS (*fp) = 0;
1764 attr = dwarf_attr (die, DW_AT_bit_offset);
1765 if (attr)
1766 {
1767 if (BITS_BIG_ENDIAN)
1768 {
1769 /* For big endian bits, the DW_AT_bit_offset gives the
1770 additional bit offset from the MSB of the containing
1771 anonymous object to the MSB of the field. We don't
1772 have to do anything special since we don't need to
1773 know the size of the anonymous object. */
1774 FIELD_BITPOS (*fp) += DW_UNSND (attr);
1775 }
1776 else
1777 {
1778 /* For little endian bits, compute the bit offset to the
1779 MSB of the anonymous object, subtract off the number of
1780 bits from the MSB of the field to the MSB of the
1781 object, and then subtract off the number of bits of
1782 the field itself. The result is the bit offset of
1783 the LSB of the field. */
1784 int anonymous_size;
1785 int bit_offset = DW_UNSND (attr);
1786
1787 attr = dwarf_attr (die, DW_AT_byte_size);
1788 if (attr)
1789 {
1790 /* The size of the anonymous object containing
1791 the bit field is explicit, so use the
1792 indicated size (in bytes). */
1793 anonymous_size = DW_UNSND (attr);
1794 }
1795 else
1796 {
1797 /* The size of the anonymous object containing
1798 the bit field must be inferred from the type
1799 attribute of the data member containing the
1800 bit field. */
1801 anonymous_size = TYPE_LENGTH (fp->type);
1802 }
1803 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
1804 - bit_offset - FIELD_BITSIZE (*fp);
1805 }
1806 }
1807
1808 /* Get name of field. */
1809 attr = dwarf_attr (die, DW_AT_name);
1810 if (attr && DW_STRING (attr))
1811 fieldname = DW_STRING (attr);
1812 fp->name = obsavestring (fieldname, strlen (fieldname),
1813 &objfile->type_obstack);
1814
1815 /* Change accessibility for artificial fields (e.g. virtual table
1816 pointer or virtual base class pointer) to private. */
1817 if (dwarf_attr (die, DW_AT_artificial))
1818 {
1819 new_field->accessibility = DW_ACCESS_private;
1820 fip->non_public_fields = 1;
1821 }
1822 }
1823 else if (die->tag == DW_TAG_variable)
1824 {
1825 char *physname;
1826
1827 /* C++ static member.
1828 Get name of field. */
1829 attr = dwarf_attr (die, DW_AT_name);
1830 if (attr && DW_STRING (attr))
1831 fieldname = DW_STRING (attr);
1832 else
1833 return;
1834
1835 /* Get physical name. */
1836 physname = dwarf2_linkage_name (die);
1837
1838 SET_FIELD_PHYSNAME (*fp, obsavestring (physname, strlen (physname),
1839 &objfile->type_obstack));
1840 FIELD_TYPE (*fp) = die_type (die, objfile);
1841 FIELD_NAME (*fp) = obsavestring (fieldname, strlen (fieldname),
1842 &objfile->type_obstack);
1843 }
1844 else if (die->tag == DW_TAG_inheritance)
1845 {
1846 /* C++ base class field. */
1847 attr = dwarf_attr (die, DW_AT_data_member_location);
1848 if (attr)
1849 FIELD_BITPOS (*fp) = decode_locdesc (DW_BLOCK (attr), objfile) * bits_per_byte;
1850 FIELD_BITSIZE (*fp) = 0;
1851 FIELD_TYPE (*fp) = die_type (die, objfile);
1852 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
1853 fip->nbaseclasses++;
1854 }
1855 }
1856
1857 /* Create the vector of fields, and attach it to the type. */
1858
1859 static void
1860 dwarf2_attach_fields_to_type (fip, type, objfile)
1861 struct field_info *fip;
1862 struct type *type;
1863 struct objfile *objfile;
1864 {
1865 int nfields = fip->nfields;
1866
1867 /* Record the field count, allocate space for the array of fields,
1868 and create blank accessibility bitfields if necessary. */
1869 TYPE_NFIELDS (type) = nfields;
1870 TYPE_FIELDS (type) = (struct field *)
1871 TYPE_ALLOC (type, sizeof (struct field) * nfields);
1872 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
1873
1874 if (fip->non_public_fields)
1875 {
1876 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1877
1878 TYPE_FIELD_PRIVATE_BITS (type) =
1879 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
1880 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
1881
1882 TYPE_FIELD_PROTECTED_BITS (type) =
1883 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
1884 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
1885
1886 TYPE_FIELD_IGNORE_BITS (type) =
1887 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
1888 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
1889 }
1890
1891 /* If the type has baseclasses, allocate and clear a bit vector for
1892 TYPE_FIELD_VIRTUAL_BITS. */
1893 if (fip->nbaseclasses)
1894 {
1895 int num_bytes = B_BYTES (fip->nbaseclasses);
1896 char *pointer;
1897
1898 ALLOCATE_CPLUS_STRUCT_TYPE (type);
1899 pointer = (char *) TYPE_ALLOC (type, num_bytes);
1900 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
1901 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
1902 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
1903 }
1904
1905 /* Copy the saved-up fields into the field vector. Start from the head
1906 of the list, adding to the tail of the field array, so that they end
1907 up in the same order in the array in which they were added to the list. */
1908 while (nfields-- > 0)
1909 {
1910 TYPE_FIELD (type, nfields) = fip->fields->field;
1911 switch (fip->fields->accessibility)
1912 {
1913 case DW_ACCESS_private:
1914 SET_TYPE_FIELD_PRIVATE (type, nfields);
1915 break;
1916
1917 case DW_ACCESS_protected:
1918 SET_TYPE_FIELD_PROTECTED (type, nfields);
1919 break;
1920
1921 case DW_ACCESS_public:
1922 break;
1923
1924 default:
1925 /* Unknown accessibility. Complain and treat it as public. */
1926 {
1927 complain (&dwarf2_unsupported_accessibility,
1928 fip->fields->accessibility);
1929 }
1930 break;
1931 }
1932 if (nfields < fip->nbaseclasses)
1933 {
1934 switch (fip->fields->virtuality)
1935 {
1936 case DW_VIRTUALITY_virtual:
1937 case DW_VIRTUALITY_pure_virtual:
1938 SET_TYPE_FIELD_VIRTUAL (type, nfields);
1939 break;
1940 }
1941 }
1942 fip->fields = fip->fields->next;
1943 }
1944 }
1945
1946 /* Add a member function to the proper fieldlist. */
1947
1948 static void
1949 dwarf2_add_member_fn (fip, die, type, objfile)
1950 struct field_info *fip;
1951 struct die_info *die;
1952 struct type *type;
1953 struct objfile *objfile;
1954 {
1955 struct attribute *attr;
1956 struct fnfieldlist *flp;
1957 int i;
1958 struct fn_field *fnp;
1959 char *fieldname;
1960 char *physname;
1961 struct nextfnfield *new_fnfield;
1962
1963 /* Get name of member function. */
1964 attr = dwarf_attr (die, DW_AT_name);
1965 if (attr && DW_STRING (attr))
1966 fieldname = DW_STRING (attr);
1967 else
1968 return;
1969
1970 /* Get the mangled name. */
1971 physname = dwarf2_linkage_name (die);
1972
1973 /* Look up member function name in fieldlist. */
1974 for (i = 0; i < fip->nfnfields; i++)
1975 {
1976 if (STREQ (fip->fnfieldlists[i].name, fieldname))
1977 break;
1978 }
1979
1980 /* Create new list element if necessary. */
1981 if (i < fip->nfnfields)
1982 flp = &fip->fnfieldlists[i];
1983 else
1984 {
1985 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
1986 {
1987 fip->fnfieldlists = (struct fnfieldlist *)
1988 xrealloc (fip->fnfieldlists,
1989 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
1990 * sizeof (struct fnfieldlist));
1991 if (fip->nfnfields == 0)
1992 make_cleanup (free_current_contents, &fip->fnfieldlists);
1993 }
1994 flp = &fip->fnfieldlists[fip->nfnfields];
1995 flp->name = fieldname;
1996 flp->length = 0;
1997 flp->head = NULL;
1998 fip->nfnfields++;
1999 }
2000
2001 /* Create a new member function field and chain it to the field list
2002 entry. */
2003 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
2004 make_cleanup (free, new_fnfield);
2005 memset (new_fnfield, 0, sizeof (struct nextfnfield));
2006 new_fnfield->next = flp->head;
2007 flp->head = new_fnfield;
2008 flp->length++;
2009
2010 /* Fill in the member function field info. */
2011 fnp = &new_fnfield->fnfield;
2012 fnp->physname = obsavestring (physname, strlen (physname),
2013 &objfile->type_obstack);
2014 fnp->type = alloc_type (objfile);
2015 if (die->type && TYPE_CODE (die->type) == TYPE_CODE_FUNC)
2016 {
2017 struct type *return_type = TYPE_TARGET_TYPE (die->type);
2018 struct type **arg_types;
2019 int nparams = TYPE_NFIELDS (die->type);
2020 int iparams;
2021
2022 /* Copy argument types from the subroutine type. */
2023 arg_types = (struct type **)
2024 TYPE_ALLOC (fnp->type, (nparams + 1) * sizeof (struct type *));
2025 for (iparams = 0; iparams < nparams; iparams++)
2026 arg_types[iparams] = TYPE_FIELD_TYPE (die->type, iparams);
2027
2028 /* Set last entry in argument type vector. */
2029 if (TYPE_FLAGS (die->type) & TYPE_FLAG_VARARGS)
2030 arg_types[nparams] = NULL;
2031 else
2032 arg_types[nparams] = dwarf2_fundamental_type (objfile, FT_VOID);
2033
2034 smash_to_method_type (fnp->type, type, return_type, arg_types);
2035
2036 /* Handle static member functions.
2037 Dwarf2 has no clean way to discern C++ static and non-static
2038 member functions. G++ helps GDB by marking the first
2039 parameter for non-static member functions (which is the
2040 this pointer) as artificial. We obtain this information
2041 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
2042 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (die->type, 0) == 0)
2043 fnp->voffset = VOFFSET_STATIC;
2044 }
2045 else
2046 complain (&dwarf2_missing_member_fn_type_complaint, physname);
2047
2048 /* Get fcontext from DW_AT_containing_type if present. */
2049 if (dwarf_attr (die, DW_AT_containing_type) != NULL)
2050 fnp->fcontext = die_containing_type (die, objfile);
2051
2052 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
2053 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
2054
2055 /* Get accessibility. */
2056 attr = dwarf_attr (die, DW_AT_accessibility);
2057 if (attr)
2058 {
2059 switch (DW_UNSND (attr))
2060 {
2061 case DW_ACCESS_private:
2062 fnp->is_private = 1;
2063 break;
2064 case DW_ACCESS_protected:
2065 fnp->is_protected = 1;
2066 break;
2067 }
2068 }
2069
2070 /* Get index in virtual function table if it is a virtual member function. */
2071 attr = dwarf_attr (die, DW_AT_vtable_elem_location);
2072 if (attr)
2073 fnp->voffset = decode_locdesc (DW_BLOCK (attr), objfile) + 2;
2074 }
2075
2076 /* Create the vector of member function fields, and attach it to the type. */
2077
2078 static void
2079 dwarf2_attach_fn_fields_to_type (fip, type, objfile)
2080 struct field_info *fip;
2081 struct type *type;
2082 struct objfile *objfile;
2083 {
2084 struct fnfieldlist *flp;
2085 int total_length = 0;
2086 int i;
2087
2088 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2089 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
2090 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
2091
2092 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
2093 {
2094 struct nextfnfield *nfp = flp->head;
2095 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
2096 int k;
2097
2098 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
2099 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
2100 fn_flp->fn_fields = (struct fn_field *)
2101 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
2102 for (k = flp->length; (k--, nfp); nfp = nfp->next)
2103 fn_flp->fn_fields[k] = nfp->fnfield;
2104
2105 total_length += flp->length;
2106 }
2107
2108 TYPE_NFN_FIELDS (type) = fip->nfnfields;
2109 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
2110 }
2111
2112 /* Called when we find the DIE that starts a structure or union scope
2113 (definition) to process all dies that define the members of the
2114 structure or union.
2115
2116 NOTE: we need to call struct_type regardless of whether or not the
2117 DIE has an at_name attribute, since it might be an anonymous
2118 structure or union. This gets the type entered into our set of
2119 user defined types.
2120
2121 However, if the structure is incomplete (an opaque struct/union)
2122 then suppress creating a symbol table entry for it since gdb only
2123 wants to find the one with the complete definition. Note that if
2124 it is complete, we just call new_symbol, which does it's own
2125 checking about whether the struct/union is anonymous or not (and
2126 suppresses creating a symbol table entry itself). */
2127
2128 static void
2129 read_structure_scope (die, objfile)
2130 struct die_info *die;
2131 struct objfile *objfile;
2132 {
2133 struct type *type;
2134 struct attribute *attr;
2135
2136 type = alloc_type (objfile);
2137
2138 INIT_CPLUS_SPECIFIC (type);
2139 attr = dwarf_attr (die, DW_AT_name);
2140 if (attr && DW_STRING (attr))
2141 {
2142 TYPE_TAG_NAME (type) = obsavestring (DW_STRING (attr),
2143 strlen (DW_STRING (attr)),
2144 &objfile->type_obstack);
2145 }
2146
2147 if (die->tag == DW_TAG_structure_type)
2148 {
2149 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2150 }
2151 else if (die->tag == DW_TAG_union_type)
2152 {
2153 TYPE_CODE (type) = TYPE_CODE_UNION;
2154 }
2155 else
2156 {
2157 /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
2158 in gdbtypes.h. */
2159 TYPE_CODE (type) = TYPE_CODE_CLASS;
2160 }
2161
2162 attr = dwarf_attr (die, DW_AT_byte_size);
2163 if (attr)
2164 {
2165 TYPE_LENGTH (type) = DW_UNSND (attr);
2166 }
2167 else
2168 {
2169 TYPE_LENGTH (type) = 0;
2170 }
2171
2172 /* We need to add the type field to the die immediately so we don't
2173 infinitely recurse when dealing with pointers to the structure
2174 type within the structure itself. */
2175 die->type = type;
2176
2177 if (die->has_children && ! die_is_declaration (die))
2178 {
2179 struct field_info fi;
2180 struct die_info *child_die;
2181 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
2182
2183 memset (&fi, 0, sizeof (struct field_info));
2184
2185 child_die = die->next;
2186
2187 while (child_die && child_die->tag)
2188 {
2189 if (child_die->tag == DW_TAG_member)
2190 {
2191 dwarf2_add_field (&fi, child_die, objfile);
2192 }
2193 else if (child_die->tag == DW_TAG_variable)
2194 {
2195 /* C++ static member. */
2196 dwarf2_add_field (&fi, child_die, objfile);
2197 }
2198 else if (child_die->tag == DW_TAG_subprogram)
2199 {
2200 /* C++ member function. */
2201 process_die (child_die, objfile);
2202 dwarf2_add_member_fn (&fi, child_die, type, objfile);
2203 }
2204 else if (child_die->tag == DW_TAG_inheritance)
2205 {
2206 /* C++ base class field. */
2207 dwarf2_add_field (&fi, child_die, objfile);
2208 }
2209 else
2210 {
2211 process_die (child_die, objfile);
2212 }
2213 child_die = sibling_die (child_die);
2214 }
2215
2216 /* Attach fields and member functions to the type. */
2217 if (fi.nfields)
2218 dwarf2_attach_fields_to_type (&fi, type, objfile);
2219 if (fi.nfnfields)
2220 {
2221 dwarf2_attach_fn_fields_to_type (&fi, type, objfile);
2222
2223 /* Get the type which refers to the base class (possibly this
2224 class itself) which contains the vtable pointer for the current
2225 class from the DW_AT_containing_type attribute. */
2226
2227 if (dwarf_attr (die, DW_AT_containing_type) != NULL)
2228 {
2229 struct type *t = die_containing_type (die, objfile);
2230
2231 TYPE_VPTR_BASETYPE (type) = t;
2232 if (type == t)
2233 {
2234 static const char vptr_name[] =
2235 {'_', 'v', 'p', 't', 'r', '\0'};
2236 int i;
2237
2238 /* Our own class provides vtbl ptr. */
2239 for (i = TYPE_NFIELDS (t) - 1;
2240 i >= TYPE_N_BASECLASSES (t);
2241 --i)
2242 {
2243 char *fieldname = TYPE_FIELD_NAME (t, i);
2244
2245 if (STREQN (fieldname, vptr_name, strlen (vptr_name) - 1)
2246 && is_cplus_marker (fieldname[strlen (vptr_name)]))
2247 {
2248 TYPE_VPTR_FIELDNO (type) = i;
2249 break;
2250 }
2251 }
2252
2253 /* Complain if virtual function table field not found. */
2254 if (i < TYPE_N_BASECLASSES (t))
2255 complain (&dwarf2_vtbl_not_found_complaint,
2256 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "");
2257 }
2258 else
2259 {
2260 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
2261 }
2262 }
2263 }
2264
2265 new_symbol (die, type, objfile);
2266
2267 do_cleanups (back_to);
2268 }
2269 else
2270 {
2271 /* No children, must be stub. */
2272 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
2273 }
2274
2275 die->type = type;
2276 }
2277
2278 /* Given a pointer to a die which begins an enumeration, process all
2279 the dies that define the members of the enumeration.
2280
2281 This will be much nicer in draft 6 of the DWARF spec when our
2282 members will be dies instead squished into the DW_AT_element_list
2283 attribute.
2284
2285 NOTE: We reverse the order of the element list. */
2286
2287 static void
2288 read_enumeration (die, objfile)
2289 struct die_info *die;
2290 struct objfile *objfile;
2291 {
2292 struct die_info *child_die;
2293 struct type *type;
2294 struct field *fields;
2295 struct attribute *attr;
2296 struct symbol *sym;
2297 int num_fields;
2298 int unsigned_enum = 1;
2299
2300 type = alloc_type (objfile);
2301
2302 TYPE_CODE (type) = TYPE_CODE_ENUM;
2303 attr = dwarf_attr (die, DW_AT_name);
2304 if (attr && DW_STRING (attr))
2305 {
2306 TYPE_TAG_NAME (type) = obsavestring (DW_STRING (attr),
2307 strlen (DW_STRING (attr)),
2308 &objfile->type_obstack);
2309 }
2310
2311 attr = dwarf_attr (die, DW_AT_byte_size);
2312 if (attr)
2313 {
2314 TYPE_LENGTH (type) = DW_UNSND (attr);
2315 }
2316 else
2317 {
2318 TYPE_LENGTH (type) = 0;
2319 }
2320
2321 num_fields = 0;
2322 fields = NULL;
2323 if (die->has_children)
2324 {
2325 child_die = die->next;
2326 while (child_die && child_die->tag)
2327 {
2328 if (child_die->tag != DW_TAG_enumerator)
2329 {
2330 process_die (child_die, objfile);
2331 }
2332 else
2333 {
2334 attr = dwarf_attr (child_die, DW_AT_name);
2335 if (attr)
2336 {
2337 sym = new_symbol (child_die, type, objfile);
2338 if (SYMBOL_VALUE (sym) < 0)
2339 unsigned_enum = 0;
2340
2341 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
2342 {
2343 fields = (struct field *)
2344 xrealloc (fields,
2345 (num_fields + DW_FIELD_ALLOC_CHUNK)
2346 * sizeof (struct field));
2347 }
2348
2349 FIELD_NAME (fields[num_fields]) = SYMBOL_NAME (sym);
2350 FIELD_TYPE (fields[num_fields]) = NULL;
2351 FIELD_BITPOS (fields[num_fields]) = SYMBOL_VALUE (sym);
2352 FIELD_BITSIZE (fields[num_fields]) = 0;
2353
2354 num_fields++;
2355 }
2356 }
2357
2358 child_die = sibling_die (child_die);
2359 }
2360
2361 if (num_fields)
2362 {
2363 TYPE_NFIELDS (type) = num_fields;
2364 TYPE_FIELDS (type) = (struct field *)
2365 TYPE_ALLOC (type, sizeof (struct field) * num_fields);
2366 memcpy (TYPE_FIELDS (type), fields,
2367 sizeof (struct field) * num_fields);
2368 free (fields);
2369 }
2370 if (unsigned_enum)
2371 TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
2372 }
2373 die->type = type;
2374 new_symbol (die, type, objfile);
2375 }
2376
2377 /* Extract all information from a DW_TAG_array_type DIE and put it in
2378 the DIE's type field. For now, this only handles one dimensional
2379 arrays. */
2380
2381 static void
2382 read_array_type (die, objfile)
2383 struct die_info *die;
2384 struct objfile *objfile;
2385 {
2386 struct die_info *child_die;
2387 struct type *type = NULL;
2388 struct type *element_type, *range_type, *index_type;
2389 struct type **range_types = NULL;
2390 struct attribute *attr;
2391 int ndim = 0;
2392 struct cleanup *back_to;
2393
2394 /* Return if we've already decoded this type. */
2395 if (die->type)
2396 {
2397 return;
2398 }
2399
2400 element_type = die_type (die, objfile);
2401
2402 /* Irix 6.2 native cc creates array types without children for
2403 arrays with unspecified length. */
2404 if (die->has_children == 0)
2405 {
2406 index_type = dwarf2_fundamental_type (objfile, FT_INTEGER);
2407 range_type = create_range_type (NULL, index_type, 0, -1);
2408 die->type = create_array_type (NULL, element_type, range_type);
2409 return;
2410 }
2411
2412 back_to = make_cleanup (null_cleanup, NULL);
2413 child_die = die->next;
2414 while (child_die && child_die->tag)
2415 {
2416 if (child_die->tag == DW_TAG_subrange_type)
2417 {
2418 unsigned int low, high;
2419
2420 /* Default bounds to an array with unspecified length. */
2421 low = 0;
2422 high = -1;
2423 if (cu_language == language_fortran)
2424 {
2425 /* FORTRAN implies a lower bound of 1, if not given. */
2426 low = 1;
2427 }
2428
2429 index_type = die_type (child_die, objfile);
2430 attr = dwarf_attr (child_die, DW_AT_lower_bound);
2431 if (attr)
2432 {
2433 if (attr->form == DW_FORM_sdata)
2434 {
2435 low = DW_SND (attr);
2436 }
2437 else if (attr->form == DW_FORM_udata
2438 || attr->form == DW_FORM_data1
2439 || attr->form == DW_FORM_data2
2440 || attr->form == DW_FORM_data4)
2441 {
2442 low = DW_UNSND (attr);
2443 }
2444 else
2445 {
2446 complain (&dwarf2_non_const_array_bound_ignored,
2447 dwarf_form_name (attr->form));
2448 #ifdef FORTRAN_HACK
2449 die->type = lookup_pointer_type (element_type);
2450 return;
2451 #else
2452 low = 0;
2453 #endif
2454 }
2455 }
2456 attr = dwarf_attr (child_die, DW_AT_upper_bound);
2457 if (attr)
2458 {
2459 if (attr->form == DW_FORM_sdata)
2460 {
2461 high = DW_SND (attr);
2462 }
2463 else if (attr->form == DW_FORM_udata
2464 || attr->form == DW_FORM_data1
2465 || attr->form == DW_FORM_data2
2466 || attr->form == DW_FORM_data4)
2467 {
2468 high = DW_UNSND (attr);
2469 }
2470 else if (attr->form == DW_FORM_block1)
2471 {
2472 /* GCC encodes arrays with unspecified or dynamic length
2473 with a DW_FORM_block1 attribute.
2474 FIXME: GDB does not yet know how to handle dynamic
2475 arrays properly, treat them as arrays with unspecified
2476 length for now. */
2477 high = -1;
2478 }
2479 else
2480 {
2481 complain (&dwarf2_non_const_array_bound_ignored,
2482 dwarf_form_name (attr->form));
2483 #ifdef FORTRAN_HACK
2484 die->type = lookup_pointer_type (element_type);
2485 return;
2486 #else
2487 high = 1;
2488 #endif
2489 }
2490 }
2491
2492 /* Create a range type and save it for array type creation. */
2493 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
2494 {
2495 range_types = (struct type **)
2496 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
2497 * sizeof (struct type *));
2498 if (ndim == 0)
2499 make_cleanup (free_current_contents, &range_types);
2500 }
2501 range_types[ndim++] = create_range_type (NULL, index_type, low, high);
2502 }
2503 child_die = sibling_die (child_die);
2504 }
2505
2506 /* Dwarf2 dimensions are output from left to right, create the
2507 necessary array types in backwards order. */
2508 type = element_type;
2509 while (ndim-- > 0)
2510 type = create_array_type (NULL, type, range_types[ndim]);
2511
2512 do_cleanups (back_to);
2513
2514 /* Install the type in the die. */
2515 die->type = type;
2516 }
2517
2518 /* First cut: install each common block member as a global variable. */
2519
2520 static void
2521 read_common_block (die, objfile)
2522 struct die_info *die;
2523 struct objfile *objfile;
2524 {
2525 struct die_info *child_die;
2526 struct attribute *attr;
2527 struct symbol *sym;
2528 CORE_ADDR base = (CORE_ADDR) 0;
2529
2530 attr = dwarf_attr (die, DW_AT_location);
2531 if (attr)
2532 {
2533 base = decode_locdesc (DW_BLOCK (attr), objfile);
2534 }
2535 if (die->has_children)
2536 {
2537 child_die = die->next;
2538 while (child_die && child_die->tag)
2539 {
2540 sym = new_symbol (child_die, NULL, objfile);
2541 attr = dwarf_attr (child_die, DW_AT_data_member_location);
2542 if (attr)
2543 {
2544 SYMBOL_VALUE_ADDRESS (sym) =
2545 base + decode_locdesc (DW_BLOCK (attr), objfile);
2546 add_symbol_to_list (sym, &global_symbols);
2547 }
2548 child_die = sibling_die (child_die);
2549 }
2550 }
2551 }
2552
2553 /* Extract all information from a DW_TAG_pointer_type DIE and add to
2554 the user defined type vector. */
2555
2556 static void
2557 read_tag_pointer_type (die, objfile)
2558 struct die_info *die;
2559 struct objfile *objfile;
2560 {
2561 struct type *type;
2562 struct attribute *attr;
2563
2564 if (die->type)
2565 {
2566 return;
2567 }
2568
2569 type = lookup_pointer_type (die_type (die, objfile));
2570 attr = dwarf_attr (die, DW_AT_byte_size);
2571 if (attr)
2572 {
2573 TYPE_LENGTH (type) = DW_UNSND (attr);
2574 }
2575 else
2576 {
2577 TYPE_LENGTH (type) = address_size;
2578 }
2579 die->type = type;
2580 }
2581
2582 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
2583 the user defined type vector. */
2584
2585 static void
2586 read_tag_ptr_to_member_type (die, objfile)
2587 struct die_info *die;
2588 struct objfile *objfile;
2589 {
2590 struct type *type;
2591 struct type *to_type;
2592 struct type *domain;
2593
2594 if (die->type)
2595 {
2596 return;
2597 }
2598
2599 type = alloc_type (objfile);
2600 to_type = die_type (die, objfile);
2601 domain = die_containing_type (die, objfile);
2602 smash_to_member_type (type, domain, to_type);
2603
2604 die->type = type;
2605 }
2606
2607 /* Extract all information from a DW_TAG_reference_type DIE and add to
2608 the user defined type vector. */
2609
2610 static void
2611 read_tag_reference_type (die, objfile)
2612 struct die_info *die;
2613 struct objfile *objfile;
2614 {
2615 struct type *type;
2616 struct attribute *attr;
2617
2618 if (die->type)
2619 {
2620 return;
2621 }
2622
2623 type = lookup_reference_type (die_type (die, objfile));
2624 attr = dwarf_attr (die, DW_AT_byte_size);
2625 if (attr)
2626 {
2627 TYPE_LENGTH (type) = DW_UNSND (attr);
2628 }
2629 else
2630 {
2631 TYPE_LENGTH (type) = address_size;
2632 }
2633 die->type = type;
2634 }
2635
2636 static void
2637 read_tag_const_type (die, objfile)
2638 struct die_info *die;
2639 struct objfile *objfile;
2640 {
2641 if (die->type)
2642 {
2643 return;
2644 }
2645
2646 complain (&dwarf2_const_ignored);
2647 die->type = die_type (die, objfile);
2648 }
2649
2650 static void
2651 read_tag_volatile_type (die, objfile)
2652 struct die_info *die;
2653 struct objfile *objfile;
2654 {
2655 if (die->type)
2656 {
2657 return;
2658 }
2659
2660 complain (&dwarf2_volatile_ignored);
2661 die->type = die_type (die, objfile);
2662 }
2663
2664 /* Extract all information from a DW_TAG_string_type DIE and add to
2665 the user defined type vector. It isn't really a user defined type,
2666 but it behaves like one, with other DIE's using an AT_user_def_type
2667 attribute to reference it. */
2668
2669 static void
2670 read_tag_string_type (die, objfile)
2671 struct die_info *die;
2672 struct objfile *objfile;
2673 {
2674 struct type *type, *range_type, *index_type, *char_type;
2675 struct attribute *attr;
2676 unsigned int length;
2677
2678 if (die->type)
2679 {
2680 return;
2681 }
2682
2683 attr = dwarf_attr (die, DW_AT_string_length);
2684 if (attr)
2685 {
2686 length = DW_UNSND (attr);
2687 }
2688 else
2689 {
2690 length = 1;
2691 }
2692 index_type = dwarf2_fundamental_type (objfile, FT_INTEGER);
2693 range_type = create_range_type (NULL, index_type, 1, length);
2694 char_type = dwarf2_fundamental_type (objfile, FT_CHAR);
2695 type = create_string_type (char_type, range_type);
2696 die->type = type;
2697 }
2698
2699 /* Handle DIES due to C code like:
2700
2701 struct foo
2702 {
2703 int (*funcp)(int a, long l);
2704 int b;
2705 };
2706
2707 ('funcp' generates a DW_TAG_subroutine_type DIE)
2708 */
2709
2710 static void
2711 read_subroutine_type (die, objfile)
2712 struct die_info *die;
2713 struct objfile *objfile;
2714 {
2715 struct type *type; /* Type that this function returns */
2716 struct type *ftype; /* Function that returns above type */
2717 struct attribute *attr;
2718
2719 /* Decode the type that this subroutine returns */
2720 if (die->type)
2721 {
2722 return;
2723 }
2724 type = die_type (die, objfile);
2725 ftype = lookup_function_type (type);
2726
2727 /* All functions in C++ have prototypes. */
2728 attr = dwarf_attr (die, DW_AT_prototyped);
2729 if ((attr && (DW_UNSND (attr) != 0))
2730 || cu_language == language_cplus)
2731 TYPE_FLAGS (ftype) |= TYPE_FLAG_PROTOTYPED;
2732
2733 if (die->has_children)
2734 {
2735 struct die_info *child_die;
2736 int nparams = 0;
2737 int iparams = 0;
2738
2739 /* Count the number of parameters.
2740 FIXME: GDB currently ignores vararg functions, but knows about
2741 vararg member functions. */
2742 child_die = die->next;
2743 while (child_die && child_die->tag)
2744 {
2745 if (child_die->tag == DW_TAG_formal_parameter)
2746 nparams++;
2747 else if (child_die->tag == DW_TAG_unspecified_parameters)
2748 TYPE_FLAGS (ftype) |= TYPE_FLAG_VARARGS;
2749 child_die = sibling_die (child_die);
2750 }
2751
2752 /* Allocate storage for parameters and fill them in. */
2753 TYPE_NFIELDS (ftype) = nparams;
2754 TYPE_FIELDS (ftype) = (struct field *)
2755 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
2756
2757 child_die = die->next;
2758 while (child_die && child_die->tag)
2759 {
2760 if (child_die->tag == DW_TAG_formal_parameter)
2761 {
2762 /* Dwarf2 has no clean way to discern C++ static and non-static
2763 member functions. G++ helps GDB by marking the first
2764 parameter for non-static member functions (which is the
2765 this pointer) as artificial. We pass this information
2766 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
2767 attr = dwarf_attr (child_die, DW_AT_artificial);
2768 if (attr)
2769 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
2770 else
2771 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
2772 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, objfile);
2773 iparams++;
2774 }
2775 child_die = sibling_die (child_die);
2776 }
2777 }
2778
2779 die->type = ftype;
2780 }
2781
2782 static void
2783 read_typedef (die, objfile)
2784 struct die_info *die;
2785 struct objfile *objfile;
2786 {
2787 struct type *type;
2788
2789 if (!die->type)
2790 {
2791 struct attribute *attr;
2792 struct type *xtype;
2793
2794 xtype = die_type (die, objfile);
2795
2796 type = alloc_type (objfile);
2797 TYPE_CODE (type) = TYPE_CODE_TYPEDEF;
2798 TYPE_FLAGS (type) |= TYPE_FLAG_TARGET_STUB;
2799 TYPE_TARGET_TYPE (type) = xtype;
2800 attr = dwarf_attr (die, DW_AT_name);
2801 if (attr && DW_STRING (attr))
2802 TYPE_NAME (type) = obsavestring (DW_STRING (attr),
2803 strlen (DW_STRING (attr)),
2804 &objfile->type_obstack);
2805
2806 die->type = type;
2807 }
2808 }
2809
2810 /* Find a representation of a given base type and install
2811 it in the TYPE field of the die. */
2812
2813 static void
2814 read_base_type (die, objfile)
2815 struct die_info *die;
2816 struct objfile *objfile;
2817 {
2818 struct type *type;
2819 struct attribute *attr;
2820 int encoding = 0, size = 0;
2821
2822 /* If we've already decoded this die, this is a no-op. */
2823 if (die->type)
2824 {
2825 return;
2826 }
2827
2828 attr = dwarf_attr (die, DW_AT_encoding);
2829 if (attr)
2830 {
2831 encoding = DW_UNSND (attr);
2832 }
2833 attr = dwarf_attr (die, DW_AT_byte_size);
2834 if (attr)
2835 {
2836 size = DW_UNSND (attr);
2837 }
2838 attr = dwarf_attr (die, DW_AT_name);
2839 if (attr && DW_STRING (attr))
2840 {
2841 enum type_code code = TYPE_CODE_INT;
2842 int is_unsigned = 0;
2843
2844 switch (encoding)
2845 {
2846 case DW_ATE_address:
2847 /* Turn DW_ATE_address into a void * pointer. */
2848 code = TYPE_CODE_PTR;
2849 is_unsigned = 1;
2850 break;
2851 case DW_ATE_boolean:
2852 code = TYPE_CODE_BOOL;
2853 is_unsigned = 1;
2854 break;
2855 case DW_ATE_complex_float:
2856 code = TYPE_CODE_COMPLEX;
2857 break;
2858 case DW_ATE_float:
2859 code = TYPE_CODE_FLT;
2860 break;
2861 case DW_ATE_signed:
2862 case DW_ATE_signed_char:
2863 break;
2864 case DW_ATE_unsigned:
2865 case DW_ATE_unsigned_char:
2866 is_unsigned = 1;
2867 break;
2868 default:
2869 complain (&dwarf2_unsupported_at_encoding,
2870 dwarf_type_encoding_name (encoding));
2871 break;
2872 }
2873 type = init_type (code, size, is_unsigned, DW_STRING (attr), objfile);
2874 if (encoding == DW_ATE_address)
2875 TYPE_TARGET_TYPE (type) = dwarf2_fundamental_type (objfile, FT_VOID);
2876 }
2877 else
2878 {
2879 type = dwarf_base_type (encoding, size, objfile);
2880 }
2881 die->type = type;
2882 }
2883
2884 /* Read a whole compilation unit into a linked list of dies. */
2885
2886 struct die_info *
2887 read_comp_unit (info_ptr, abfd)
2888 char *info_ptr;
2889 bfd *abfd;
2890 {
2891 struct die_info *first_die, *last_die, *die;
2892 char *cur_ptr;
2893 int nesting_level;
2894
2895 /* Reset die reference table, we are building a new one now. */
2896 dwarf2_empty_die_ref_table ();
2897
2898 cur_ptr = info_ptr;
2899 nesting_level = 0;
2900 first_die = last_die = NULL;
2901 do
2902 {
2903 cur_ptr = read_full_die (&die, abfd, cur_ptr);
2904 if (die->has_children)
2905 {
2906 nesting_level++;
2907 }
2908 if (die->tag == 0)
2909 {
2910 nesting_level--;
2911 }
2912
2913 die->next = NULL;
2914
2915 /* Enter die in reference hash table */
2916 store_in_ref_table (die->offset, die);
2917
2918 if (!first_die)
2919 {
2920 first_die = last_die = die;
2921 }
2922 else
2923 {
2924 last_die->next = die;
2925 last_die = die;
2926 }
2927 }
2928 while (nesting_level > 0);
2929 return first_die;
2930 }
2931
2932 /* Free a linked list of dies. */
2933
2934 static void
2935 free_die_list (dies)
2936 struct die_info *dies;
2937 {
2938 struct die_info *die, *next;
2939
2940 die = dies;
2941 while (die)
2942 {
2943 next = die->next;
2944 free (die->attrs);
2945 free (die);
2946 die = next;
2947 }
2948 }
2949
2950 static void
2951 do_free_die_list_cleanup (void *dies)
2952 {
2953 free_die_list (dies);
2954 }
2955
2956 static struct cleanup *
2957 make_cleanup_free_die_list (struct die_info *dies)
2958 {
2959 return make_cleanup (do_free_die_list_cleanup, dies);
2960 }
2961
2962
2963 /* Read the contents of the section at OFFSET and of size SIZE from the
2964 object file specified by OBJFILE into the psymbol_obstack and return it. */
2965
2966 static char *
2967 dwarf2_read_section (objfile, offset, size)
2968 struct objfile *objfile;
2969 file_ptr offset;
2970 unsigned int size;
2971 {
2972 bfd *abfd = objfile->obfd;
2973 char *buf;
2974
2975 if (size == 0)
2976 return NULL;
2977
2978 buf = (char *) obstack_alloc (&objfile->psymbol_obstack, size);
2979 if ((bfd_seek (abfd, offset, SEEK_SET) != 0) ||
2980 (bfd_read (buf, size, 1, abfd) != size))
2981 {
2982 buf = NULL;
2983 error ("Dwarf Error: Can't read DWARF data from '%s'",
2984 bfd_get_filename (abfd));
2985 }
2986 return buf;
2987 }
2988
2989 /* In DWARF version 2, the description of the debugging information is
2990 stored in a separate .debug_abbrev section. Before we read any
2991 dies from a section we read in all abbreviations and install them
2992 in a hash table. */
2993
2994 static void
2995 dwarf2_read_abbrevs (abfd, offset)
2996 bfd *abfd;
2997 unsigned int offset;
2998 {
2999 char *abbrev_ptr;
3000 struct abbrev_info *cur_abbrev;
3001 unsigned int abbrev_number, bytes_read, abbrev_name;
3002 unsigned int abbrev_form, hash_number;
3003
3004 /* empty the table */
3005 dwarf2_empty_abbrev_table (NULL);
3006
3007 abbrev_ptr = dwarf_abbrev_buffer + offset;
3008 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3009 abbrev_ptr += bytes_read;
3010
3011 /* loop until we reach an abbrev number of 0 */
3012 while (abbrev_number)
3013 {
3014 cur_abbrev = dwarf_alloc_abbrev ();
3015
3016 /* read in abbrev header */
3017 cur_abbrev->number = abbrev_number;
3018 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3019 abbrev_ptr += bytes_read;
3020 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
3021 abbrev_ptr += 1;
3022
3023 /* now read in declarations */
3024 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3025 abbrev_ptr += bytes_read;
3026 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3027 abbrev_ptr += bytes_read;
3028 while (abbrev_name)
3029 {
3030 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
3031 {
3032 cur_abbrev->attrs = (struct attr_abbrev *)
3033 xrealloc (cur_abbrev->attrs,
3034 (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
3035 * sizeof (struct attr_abbrev));
3036 }
3037 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
3038 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
3039 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3040 abbrev_ptr += bytes_read;
3041 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3042 abbrev_ptr += bytes_read;
3043 }
3044
3045 hash_number = abbrev_number % ABBREV_HASH_SIZE;
3046 cur_abbrev->next = dwarf2_abbrevs[hash_number];
3047 dwarf2_abbrevs[hash_number] = cur_abbrev;
3048
3049 /* Get next abbreviation.
3050 Under Irix6 the abbreviations for a compilation unit are not
3051 always properly terminated with an abbrev number of 0.
3052 Exit loop if we encounter an abbreviation which we have
3053 already read (which means we are about to read the abbreviations
3054 for the next compile unit) or if the end of the abbreviation
3055 table is reached. */
3056 if ((unsigned int) (abbrev_ptr - dwarf_abbrev_buffer)
3057 >= dwarf_abbrev_size)
3058 break;
3059 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3060 abbrev_ptr += bytes_read;
3061 if (dwarf2_lookup_abbrev (abbrev_number) != NULL)
3062 break;
3063 }
3064 }
3065
3066 /* Empty the abbrev table for a new compilation unit. */
3067
3068 /* ARGSUSED */
3069 static void
3070 dwarf2_empty_abbrev_table (ignore)
3071 PTR ignore;
3072 {
3073 int i;
3074 struct abbrev_info *abbrev, *next;
3075
3076 for (i = 0; i < ABBREV_HASH_SIZE; ++i)
3077 {
3078 next = NULL;
3079 abbrev = dwarf2_abbrevs[i];
3080 while (abbrev)
3081 {
3082 next = abbrev->next;
3083 free (abbrev->attrs);
3084 free (abbrev);
3085 abbrev = next;
3086 }
3087 dwarf2_abbrevs[i] = NULL;
3088 }
3089 }
3090
3091 /* Lookup an abbrev_info structure in the abbrev hash table. */
3092
3093 static struct abbrev_info *
3094 dwarf2_lookup_abbrev (number)
3095 unsigned int number;
3096 {
3097 unsigned int hash_number;
3098 struct abbrev_info *abbrev;
3099
3100 hash_number = number % ABBREV_HASH_SIZE;
3101 abbrev = dwarf2_abbrevs[hash_number];
3102
3103 while (abbrev)
3104 {
3105 if (abbrev->number == number)
3106 return abbrev;
3107 else
3108 abbrev = abbrev->next;
3109 }
3110 return NULL;
3111 }
3112
3113 /* Read a minimal amount of information into the minimal die structure. */
3114
3115 static char *
3116 read_partial_die (part_die, abfd, info_ptr, has_pc_info)
3117 struct partial_die_info *part_die;
3118 bfd *abfd;
3119 char *info_ptr;
3120 int *has_pc_info;
3121 {
3122 unsigned int abbrev_number, bytes_read, i;
3123 struct abbrev_info *abbrev;
3124 struct attribute attr;
3125 struct attribute spec_attr;
3126 int found_spec_attr = 0;
3127 int has_low_pc_attr = 0;
3128 int has_high_pc_attr = 0;
3129
3130 *part_die = zeroed_partial_die;
3131 *has_pc_info = 0;
3132 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3133 info_ptr += bytes_read;
3134 if (!abbrev_number)
3135 return info_ptr;
3136
3137 abbrev = dwarf2_lookup_abbrev (abbrev_number);
3138 if (!abbrev)
3139 {
3140 error ("Dwarf Error: Could not find abbrev number %d.", abbrev_number);
3141 }
3142 part_die->offset = info_ptr - dwarf_info_buffer;
3143 part_die->tag = abbrev->tag;
3144 part_die->has_children = abbrev->has_children;
3145 part_die->abbrev = abbrev_number;
3146
3147 for (i = 0; i < abbrev->num_attrs; ++i)
3148 {
3149 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr);
3150
3151 /* Store the data if it is of an attribute we want to keep in a
3152 partial symbol table. */
3153 switch (attr.name)
3154 {
3155 case DW_AT_name:
3156
3157 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
3158 if (part_die->name == NULL)
3159 part_die->name = DW_STRING (&attr);
3160 break;
3161 case DW_AT_MIPS_linkage_name:
3162 part_die->name = DW_STRING (&attr);
3163 break;
3164 case DW_AT_low_pc:
3165 has_low_pc_attr = 1;
3166 part_die->lowpc = DW_ADDR (&attr);
3167 break;
3168 case DW_AT_high_pc:
3169 has_high_pc_attr = 1;
3170 part_die->highpc = DW_ADDR (&attr);
3171 break;
3172 case DW_AT_location:
3173 part_die->locdesc = DW_BLOCK (&attr);
3174 break;
3175 case DW_AT_language:
3176 part_die->language = DW_UNSND (&attr);
3177 break;
3178 case DW_AT_external:
3179 part_die->is_external = DW_UNSND (&attr);
3180 break;
3181 case DW_AT_declaration:
3182 part_die->is_declaration = DW_UNSND (&attr);
3183 break;
3184 case DW_AT_type:
3185 part_die->has_type = 1;
3186 break;
3187 case DW_AT_abstract_origin:
3188 case DW_AT_specification:
3189 found_spec_attr = 1;
3190 spec_attr = attr;
3191 break;
3192 case DW_AT_sibling:
3193 /* Ignore absolute siblings, they might point outside of
3194 the current compile unit. */
3195 if (attr.form == DW_FORM_ref_addr)
3196 complain (&dwarf2_absolute_sibling_complaint);
3197 else
3198 part_die->sibling =
3199 dwarf_info_buffer + dwarf2_get_ref_die_offset (&attr);
3200 break;
3201 default:
3202 break;
3203 }
3204 }
3205
3206 /* If we found a reference attribute and the die has no name, try
3207 to find a name in the referred to die. */
3208
3209 if (found_spec_attr && part_die->name == NULL)
3210 {
3211 struct partial_die_info spec_die;
3212 char *spec_ptr;
3213 int dummy;
3214
3215 spec_ptr = dwarf_info_buffer + dwarf2_get_ref_die_offset (&spec_attr);
3216 read_partial_die (&spec_die, abfd, spec_ptr, &dummy);
3217 if (spec_die.name)
3218 {
3219 part_die->name = spec_die.name;
3220
3221 /* Copy DW_AT_external attribute if it is set. */
3222 if (spec_die.is_external)
3223 part_die->is_external = spec_die.is_external;
3224 }
3225 }
3226
3227 /* When using the GNU linker, .gnu.linkonce. sections are used to
3228 eliminate duplicate copies of functions and vtables and such.
3229 The linker will arbitrarily choose one and discard the others.
3230 The AT_*_pc values for such functions refer to local labels in
3231 these sections. If the section from that file was discarded, the
3232 labels are not in the output, so the relocs get a value of 0.
3233 If this is a discarded function, mark the pc bounds as invalid,
3234 so that GDB will ignore it. */
3235 if (has_low_pc_attr && has_high_pc_attr
3236 && part_die->lowpc < part_die->highpc
3237 && (part_die->lowpc != 0
3238 || (bfd_get_file_flags (abfd) & HAS_RELOC)))
3239 *has_pc_info = 1;
3240 return info_ptr;
3241 }
3242
3243 /* Read the die from the .debug_info section buffer. And set diep to
3244 point to a newly allocated die with its information. */
3245
3246 static char *
3247 read_full_die (diep, abfd, info_ptr)
3248 struct die_info **diep;
3249 bfd *abfd;
3250 char *info_ptr;
3251 {
3252 unsigned int abbrev_number, bytes_read, i, offset;
3253 struct abbrev_info *abbrev;
3254 struct die_info *die;
3255
3256 offset = info_ptr - dwarf_info_buffer;
3257 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3258 info_ptr += bytes_read;
3259 if (!abbrev_number)
3260 {
3261 die = dwarf_alloc_die ();
3262 die->tag = 0;
3263 die->abbrev = abbrev_number;
3264 die->type = NULL;
3265 *diep = die;
3266 return info_ptr;
3267 }
3268
3269 abbrev = dwarf2_lookup_abbrev (abbrev_number);
3270 if (!abbrev)
3271 {
3272 error ("Dwarf Error: could not find abbrev number %d.", abbrev_number);
3273 }
3274 die = dwarf_alloc_die ();
3275 die->offset = offset;
3276 die->tag = abbrev->tag;
3277 die->has_children = abbrev->has_children;
3278 die->abbrev = abbrev_number;
3279 die->type = NULL;
3280
3281 die->num_attrs = abbrev->num_attrs;
3282 die->attrs = (struct attribute *)
3283 xmalloc (die->num_attrs * sizeof (struct attribute));
3284
3285 for (i = 0; i < abbrev->num_attrs; ++i)
3286 {
3287 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
3288 abfd, info_ptr);
3289 }
3290
3291 *diep = die;
3292 return info_ptr;
3293 }
3294
3295 /* Read an attribute described by an abbreviated attribute. */
3296
3297 static char *
3298 read_attribute (attr, abbrev, abfd, info_ptr)
3299 struct attribute *attr;
3300 struct attr_abbrev *abbrev;
3301 bfd *abfd;
3302 char *info_ptr;
3303 {
3304 unsigned int bytes_read;
3305 struct dwarf_block *blk;
3306
3307 attr->name = abbrev->name;
3308 attr->form = abbrev->form;
3309 switch (abbrev->form)
3310 {
3311 case DW_FORM_addr:
3312 case DW_FORM_ref_addr:
3313 DW_ADDR (attr) = read_address (abfd, info_ptr);
3314 info_ptr += address_size;
3315 break;
3316 case DW_FORM_block2:
3317 blk = dwarf_alloc_block ();
3318 blk->size = read_2_bytes (abfd, info_ptr);
3319 info_ptr += 2;
3320 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3321 info_ptr += blk->size;
3322 DW_BLOCK (attr) = blk;
3323 break;
3324 case DW_FORM_block4:
3325 blk = dwarf_alloc_block ();
3326 blk->size = read_4_bytes (abfd, info_ptr);
3327 info_ptr += 4;
3328 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3329 info_ptr += blk->size;
3330 DW_BLOCK (attr) = blk;
3331 break;
3332 case DW_FORM_data2:
3333 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
3334 info_ptr += 2;
3335 break;
3336 case DW_FORM_data4:
3337 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
3338 info_ptr += 4;
3339 break;
3340 case DW_FORM_data8:
3341 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
3342 info_ptr += 8;
3343 break;
3344 case DW_FORM_string:
3345 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
3346 info_ptr += bytes_read;
3347 break;
3348 case DW_FORM_block:
3349 blk = dwarf_alloc_block ();
3350 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3351 info_ptr += bytes_read;
3352 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3353 info_ptr += blk->size;
3354 DW_BLOCK (attr) = blk;
3355 break;
3356 case DW_FORM_block1:
3357 blk = dwarf_alloc_block ();
3358 blk->size = read_1_byte (abfd, info_ptr);
3359 info_ptr += 1;
3360 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3361 info_ptr += blk->size;
3362 DW_BLOCK (attr) = blk;
3363 break;
3364 case DW_FORM_data1:
3365 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3366 info_ptr += 1;
3367 break;
3368 case DW_FORM_flag:
3369 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3370 info_ptr += 1;
3371 break;
3372 case DW_FORM_sdata:
3373 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
3374 info_ptr += bytes_read;
3375 break;
3376 case DW_FORM_udata:
3377 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3378 info_ptr += bytes_read;
3379 break;
3380 case DW_FORM_ref1:
3381 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3382 info_ptr += 1;
3383 break;
3384 case DW_FORM_ref2:
3385 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
3386 info_ptr += 2;
3387 break;
3388 case DW_FORM_ref4:
3389 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
3390 info_ptr += 4;
3391 break;
3392 case DW_FORM_ref_udata:
3393 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3394 info_ptr += bytes_read;
3395 break;
3396 case DW_FORM_strp:
3397 case DW_FORM_indirect:
3398 default:
3399 error ("Dwarf Error: Cannot handle %s in DWARF reader.",
3400 dwarf_form_name (abbrev->form));
3401 }
3402 return info_ptr;
3403 }
3404
3405 /* read dwarf information from a buffer */
3406
3407 static unsigned int
3408 read_1_byte (abfd, buf)
3409 bfd *abfd;
3410 char *buf;
3411 {
3412 return bfd_get_8 (abfd, (bfd_byte *) buf);
3413 }
3414
3415 static int
3416 read_1_signed_byte (abfd, buf)
3417 bfd *abfd;
3418 char *buf;
3419 {
3420 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
3421 }
3422
3423 static unsigned int
3424 read_2_bytes (abfd, buf)
3425 bfd *abfd;
3426 char *buf;
3427 {
3428 return bfd_get_16 (abfd, (bfd_byte *) buf);
3429 }
3430
3431 static int
3432 read_2_signed_bytes (abfd, buf)
3433 bfd *abfd;
3434 char *buf;
3435 {
3436 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
3437 }
3438
3439 static unsigned int
3440 read_4_bytes (abfd, buf)
3441 bfd *abfd;
3442 char *buf;
3443 {
3444 return bfd_get_32 (abfd, (bfd_byte *) buf);
3445 }
3446
3447 static int
3448 read_4_signed_bytes (abfd, buf)
3449 bfd *abfd;
3450 char *buf;
3451 {
3452 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
3453 }
3454
3455 static unsigned int
3456 read_8_bytes (abfd, buf)
3457 bfd *abfd;
3458 char *buf;
3459 {
3460 return bfd_get_64 (abfd, (bfd_byte *) buf);
3461 }
3462
3463 static CORE_ADDR
3464 read_address (abfd, buf)
3465 bfd *abfd;
3466 char *buf;
3467 {
3468 CORE_ADDR retval = 0;
3469
3470 switch (address_size)
3471 {
3472 case 2:
3473 retval = bfd_get_16 (abfd, (bfd_byte *) buf);
3474 break;
3475 case 4:
3476 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
3477 break;
3478 case 8:
3479 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
3480 break;
3481 default:
3482 /* *THE* alternative is 8, right? */
3483 abort ();
3484 }
3485
3486 return retval;
3487 }
3488
3489 static char *
3490 read_n_bytes (abfd, buf, size)
3491 bfd *abfd;
3492 char *buf;
3493 unsigned int size;
3494 {
3495 /* If the size of a host char is 8 bits, we can return a pointer
3496 to the buffer, otherwise we have to copy the data to a buffer
3497 allocated on the temporary obstack. */
3498 #if HOST_CHAR_BIT == 8
3499 return buf;
3500 #else
3501 char *ret;
3502 unsigned int i;
3503
3504 ret = obstack_alloc (&dwarf2_tmp_obstack, size);
3505 for (i = 0; i < size; ++i)
3506 {
3507 ret[i] = bfd_get_8 (abfd, (bfd_byte *) buf);
3508 buf++;
3509 }
3510 return ret;
3511 #endif
3512 }
3513
3514 static char *
3515 read_string (abfd, buf, bytes_read_ptr)
3516 bfd *abfd;
3517 char *buf;
3518 unsigned int *bytes_read_ptr;
3519 {
3520 /* If the size of a host char is 8 bits, we can return a pointer
3521 to the string, otherwise we have to copy the string to a buffer
3522 allocated on the temporary obstack. */
3523 #if HOST_CHAR_BIT == 8
3524 if (*buf == '\0')
3525 {
3526 *bytes_read_ptr = 1;
3527 return NULL;
3528 }
3529 *bytes_read_ptr = strlen (buf) + 1;
3530 return buf;
3531 #else
3532 int byte;
3533 unsigned int i = 0;
3534
3535 while ((byte = bfd_get_8 (abfd, (bfd_byte *) buf)) != 0)
3536 {
3537 obstack_1grow (&dwarf2_tmp_obstack, byte);
3538 i++;
3539 buf++;
3540 }
3541 if (i == 0)
3542 {
3543 *bytes_read_ptr = 1;
3544 return NULL;
3545 }
3546 obstack_1grow (&dwarf2_tmp_obstack, '\0');
3547 *bytes_read_ptr = i + 1;
3548 return obstack_finish (&dwarf2_tmp_obstack);
3549 #endif
3550 }
3551
3552 static unsigned int
3553 read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
3554 bfd *abfd;
3555 char *buf;
3556 unsigned int *bytes_read_ptr;
3557 {
3558 unsigned int result, num_read;
3559 int i, shift;
3560 unsigned char byte;
3561
3562 result = 0;
3563 shift = 0;
3564 num_read = 0;
3565 i = 0;
3566 while (1)
3567 {
3568 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
3569 buf++;
3570 num_read++;
3571 result |= ((byte & 127) << shift);
3572 if ((byte & 128) == 0)
3573 {
3574 break;
3575 }
3576 shift += 7;
3577 }
3578 *bytes_read_ptr = num_read;
3579 return result;
3580 }
3581
3582 static int
3583 read_signed_leb128 (abfd, buf, bytes_read_ptr)
3584 bfd *abfd;
3585 char *buf;
3586 unsigned int *bytes_read_ptr;
3587 {
3588 int result;
3589 int i, shift, size, num_read;
3590 unsigned char byte;
3591
3592 result = 0;
3593 shift = 0;
3594 size = 32;
3595 num_read = 0;
3596 i = 0;
3597 while (1)
3598 {
3599 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
3600 buf++;
3601 num_read++;
3602 result |= ((byte & 127) << shift);
3603 shift += 7;
3604 if ((byte & 128) == 0)
3605 {
3606 break;
3607 }
3608 }
3609 if ((shift < size) && (byte & 0x40))
3610 {
3611 result |= -(1 << shift);
3612 }
3613 *bytes_read_ptr = num_read;
3614 return result;
3615 }
3616
3617 static void
3618 set_cu_language (lang)
3619 unsigned int lang;
3620 {
3621 switch (lang)
3622 {
3623 case DW_LANG_C89:
3624 case DW_LANG_C:
3625 cu_language = language_c;
3626 break;
3627 case DW_LANG_C_plus_plus:
3628 cu_language = language_cplus;
3629 break;
3630 case DW_LANG_Fortran77:
3631 case DW_LANG_Fortran90:
3632 cu_language = language_fortran;
3633 break;
3634 case DW_LANG_Mips_Assembler:
3635 cu_language = language_asm;
3636 break;
3637 case DW_LANG_Ada83:
3638 case DW_LANG_Cobol74:
3639 case DW_LANG_Cobol85:
3640 case DW_LANG_Pascal83:
3641 case DW_LANG_Modula2:
3642 default:
3643 cu_language = language_unknown;
3644 break;
3645 }
3646 cu_language_defn = language_def (cu_language);
3647 }
3648
3649 /* Return the named attribute or NULL if not there. */
3650
3651 static struct attribute *
3652 dwarf_attr (die, name)
3653 struct die_info *die;
3654 unsigned int name;
3655 {
3656 unsigned int i;
3657 struct attribute *spec = NULL;
3658
3659 for (i = 0; i < die->num_attrs; ++i)
3660 {
3661 if (die->attrs[i].name == name)
3662 {
3663 return &die->attrs[i];
3664 }
3665 if (die->attrs[i].name == DW_AT_specification
3666 || die->attrs[i].name == DW_AT_abstract_origin)
3667 spec = &die->attrs[i];
3668 }
3669 if (spec)
3670 {
3671 struct die_info *ref_die =
3672 follow_die_ref (dwarf2_get_ref_die_offset (spec));
3673
3674 if (ref_die)
3675 return dwarf_attr (ref_die, name);
3676 }
3677
3678 return NULL;
3679 }
3680
3681 static int
3682 die_is_declaration (struct die_info *die)
3683 {
3684 return (dwarf_attr (die, DW_AT_declaration)
3685 && ! dwarf_attr (die, DW_AT_specification));
3686 }
3687
3688 /* Decode the line number information for the compilation unit whose
3689 line number info is at OFFSET in the .debug_line section.
3690 The compilation directory of the file is passed in COMP_DIR. */
3691
3692 struct filenames
3693 {
3694 unsigned int num_files;
3695 struct fileinfo
3696 {
3697 char *name;
3698 unsigned int dir;
3699 unsigned int time;
3700 unsigned int size;
3701 }
3702 *files;
3703 };
3704
3705 struct directories
3706 {
3707 unsigned int num_dirs;
3708 char **dirs;
3709 };
3710
3711 static void
3712 dwarf_decode_lines (offset, comp_dir, abfd)
3713 unsigned int offset;
3714 char *comp_dir;
3715 bfd *abfd;
3716 {
3717 char *line_ptr;
3718 char *line_end;
3719 struct line_head lh;
3720 struct cleanup *back_to;
3721 unsigned int i, bytes_read;
3722 char *cur_file, *cur_dir;
3723 unsigned char op_code, extended_op, adj_opcode;
3724
3725 #define FILE_ALLOC_CHUNK 5
3726 #define DIR_ALLOC_CHUNK 5
3727
3728 struct filenames files;
3729 struct directories dirs;
3730
3731 if (dwarf_line_buffer == NULL)
3732 {
3733 complain (&dwarf2_missing_line_number_section);
3734 return;
3735 }
3736
3737 files.num_files = 0;
3738 files.files = NULL;
3739
3740 dirs.num_dirs = 0;
3741 dirs.dirs = NULL;
3742
3743 line_ptr = dwarf_line_buffer + offset;
3744
3745 /* read in the prologue */
3746 lh.total_length = read_4_bytes (abfd, line_ptr);
3747 line_ptr += 4;
3748 line_end = line_ptr + lh.total_length;
3749 lh.version = read_2_bytes (abfd, line_ptr);
3750 line_ptr += 2;
3751 lh.prologue_length = read_4_bytes (abfd, line_ptr);
3752 line_ptr += 4;
3753 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
3754 line_ptr += 1;
3755 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
3756 line_ptr += 1;
3757 lh.line_base = read_1_signed_byte (abfd, line_ptr);
3758 line_ptr += 1;
3759 lh.line_range = read_1_byte (abfd, line_ptr);
3760 line_ptr += 1;
3761 lh.opcode_base = read_1_byte (abfd, line_ptr);
3762 line_ptr += 1;
3763 lh.standard_opcode_lengths = (unsigned char *)
3764 xmalloc (lh.opcode_base * sizeof (unsigned char));
3765 back_to = make_cleanup (free_current_contents, &lh.standard_opcode_lengths);
3766
3767 lh.standard_opcode_lengths[0] = 1;
3768 for (i = 1; i < lh.opcode_base; ++i)
3769 {
3770 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
3771 line_ptr += 1;
3772 }
3773
3774 /* Read directory table */
3775 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
3776 {
3777 line_ptr += bytes_read;
3778 if ((dirs.num_dirs % DIR_ALLOC_CHUNK) == 0)
3779 {
3780 dirs.dirs = (char **)
3781 xrealloc (dirs.dirs,
3782 (dirs.num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
3783 if (dirs.num_dirs == 0)
3784 make_cleanup (free_current_contents, &dirs.dirs);
3785 }
3786 dirs.dirs[dirs.num_dirs++] = cur_dir;
3787 }
3788 line_ptr += bytes_read;
3789
3790 /* Read file name table */
3791 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
3792 {
3793 line_ptr += bytes_read;
3794 if ((files.num_files % FILE_ALLOC_CHUNK) == 0)
3795 {
3796 files.files = (struct fileinfo *)
3797 xrealloc (files.files,
3798 (files.num_files + FILE_ALLOC_CHUNK)
3799 * sizeof (struct fileinfo));
3800 if (files.num_files == 0)
3801 make_cleanup (free_current_contents, &files.files);
3802 }
3803 files.files[files.num_files].name = cur_file;
3804 files.files[files.num_files].dir =
3805 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3806 line_ptr += bytes_read;
3807 files.files[files.num_files].time =
3808 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3809 line_ptr += bytes_read;
3810 files.files[files.num_files].size =
3811 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3812 line_ptr += bytes_read;
3813 files.num_files++;
3814 }
3815 line_ptr += bytes_read;
3816
3817 /* Read the statement sequences until there's nothing left. */
3818 while (line_ptr < line_end)
3819 {
3820 /* state machine registers */
3821 CORE_ADDR address = 0;
3822 unsigned int file = 1;
3823 unsigned int line = 1;
3824 unsigned int column = 0;
3825 int is_stmt = lh.default_is_stmt;
3826 int basic_block = 0;
3827 int end_sequence = 0;
3828
3829 /* Start a subfile for the current file of the state machine. */
3830 if (files.num_files >= file)
3831 {
3832 /* The file and directory tables are 0 based, the references
3833 are 1 based. */
3834 dwarf2_start_subfile (files.files[file - 1].name,
3835 (files.files[file - 1].dir
3836 ? dirs.dirs[files.files[file - 1].dir - 1]
3837 : comp_dir));
3838 }
3839
3840 /* Decode the table. */
3841 while (!end_sequence)
3842 {
3843 op_code = read_1_byte (abfd, line_ptr);
3844 line_ptr += 1;
3845 switch (op_code)
3846 {
3847 case DW_LNS_extended_op:
3848 line_ptr += 1; /* ignore length */
3849 extended_op = read_1_byte (abfd, line_ptr);
3850 line_ptr += 1;
3851 switch (extended_op)
3852 {
3853 case DW_LNE_end_sequence:
3854 end_sequence = 1;
3855 /* Don't call record_line here. The end_sequence
3856 instruction provides the address of the first byte
3857 *after* the last line in the sequence; it's not the
3858 address of any real source line. However, the GDB
3859 linetable structure only records the starts of lines,
3860 not the ends. This is a weakness of GDB. */
3861 break;
3862 case DW_LNE_set_address:
3863 address = read_address (abfd, line_ptr) + baseaddr;
3864 line_ptr += address_size;
3865 break;
3866 case DW_LNE_define_file:
3867 cur_file = read_string (abfd, line_ptr, &bytes_read);
3868 line_ptr += bytes_read;
3869 if ((files.num_files % FILE_ALLOC_CHUNK) == 0)
3870 {
3871 files.files = (struct fileinfo *)
3872 xrealloc (files.files,
3873 (files.num_files + FILE_ALLOC_CHUNK)
3874 * sizeof (struct fileinfo));
3875 if (files.num_files == 0)
3876 make_cleanup (free_current_contents, &files.files);
3877 }
3878 files.files[files.num_files].name = cur_file;
3879 files.files[files.num_files].dir =
3880 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3881 line_ptr += bytes_read;
3882 files.files[files.num_files].time =
3883 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3884 line_ptr += bytes_read;
3885 files.files[files.num_files].size =
3886 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3887 line_ptr += bytes_read;
3888 files.num_files++;
3889 break;
3890 default:
3891 complain (&dwarf2_mangled_line_number_section);
3892 goto done;
3893 }
3894 break;
3895 case DW_LNS_copy:
3896 record_line (current_subfile, line, address);
3897 basic_block = 0;
3898 break;
3899 case DW_LNS_advance_pc:
3900 address += lh.minimum_instruction_length
3901 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3902 line_ptr += bytes_read;
3903 break;
3904 case DW_LNS_advance_line:
3905 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
3906 line_ptr += bytes_read;
3907 break;
3908 case DW_LNS_set_file:
3909 /* The file and directory tables are 0 based, the references
3910 are 1 based. */
3911 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3912 line_ptr += bytes_read;
3913 dwarf2_start_subfile
3914 (files.files[file - 1].name,
3915 (files.files[file - 1].dir
3916 ? dirs.dirs[files.files[file - 1].dir - 1]
3917 : comp_dir));
3918 break;
3919 case DW_LNS_set_column:
3920 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3921 line_ptr += bytes_read;
3922 break;
3923 case DW_LNS_negate_stmt:
3924 is_stmt = (!is_stmt);
3925 break;
3926 case DW_LNS_set_basic_block:
3927 basic_block = 1;
3928 break;
3929 /* Add to the address register of the state machine the
3930 address increment value corresponding to special opcode
3931 255. Ie, this value is scaled by the minimum instruction
3932 length since special opcode 255 would have scaled the
3933 the increment. */
3934 case DW_LNS_const_add_pc:
3935 address += (lh.minimum_instruction_length
3936 * ((255 - lh.opcode_base) / lh.line_range));
3937 break;
3938 case DW_LNS_fixed_advance_pc:
3939 address += read_2_bytes (abfd, line_ptr);
3940 line_ptr += 2;
3941 break;
3942 default: /* special operand */
3943 adj_opcode = op_code - lh.opcode_base;
3944 address += (adj_opcode / lh.line_range)
3945 * lh.minimum_instruction_length;
3946 line += lh.line_base + (adj_opcode % lh.line_range);
3947 /* append row to matrix using current values */
3948 record_line (current_subfile, line, address);
3949 basic_block = 1;
3950 }
3951 }
3952 }
3953 done:
3954 do_cleanups (back_to);
3955 }
3956
3957 /* Start a subfile for DWARF. FILENAME is the name of the file and
3958 DIRNAME the name of the source directory which contains FILENAME
3959 or NULL if not known.
3960 This routine tries to keep line numbers from identical absolute and
3961 relative file names in a common subfile.
3962
3963 Using the `list' example from the GDB testsuite, which resides in
3964 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
3965 of /srcdir/list0.c yields the following debugging information for list0.c:
3966
3967 DW_AT_name: /srcdir/list0.c
3968 DW_AT_comp_dir: /compdir
3969 files.files[0].name: list0.h
3970 files.files[0].dir: /srcdir
3971 files.files[1].name: list0.c
3972 files.files[1].dir: /srcdir
3973
3974 The line number information for list0.c has to end up in a single
3975 subfile, so that `break /srcdir/list0.c:1' works as expected. */
3976
3977 static void
3978 dwarf2_start_subfile (filename, dirname)
3979 char *filename;
3980 char *dirname;
3981 {
3982 /* If the filename isn't absolute, try to match an existing subfile
3983 with the full pathname. */
3984
3985 if (*filename != '/' && dirname != NULL)
3986 {
3987 struct subfile *subfile;
3988 char *fullname = concat (dirname, "/", filename, NULL);
3989
3990 for (subfile = subfiles; subfile; subfile = subfile->next)
3991 {
3992 if (STREQ (subfile->name, fullname))
3993 {
3994 current_subfile = subfile;
3995 free (fullname);
3996 return;
3997 }
3998 }
3999 free (fullname);
4000 }
4001 start_subfile (filename, dirname);
4002 }
4003
4004 /* Given a pointer to a DWARF information entry, figure out if we need
4005 to make a symbol table entry for it, and if so, create a new entry
4006 and return a pointer to it.
4007 If TYPE is NULL, determine symbol type from the die, otherwise
4008 used the passed type. */
4009
4010 static struct symbol *
4011 new_symbol (die, type, objfile)
4012 struct die_info *die;
4013 struct type *type;
4014 struct objfile *objfile;
4015 {
4016 struct symbol *sym = NULL;
4017 char *name;
4018 struct attribute *attr = NULL;
4019 struct attribute *attr2 = NULL;
4020 CORE_ADDR addr;
4021
4022 name = dwarf2_linkage_name (die);
4023 if (name)
4024 {
4025 sym = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
4026 sizeof (struct symbol));
4027 OBJSTAT (objfile, n_syms++);
4028 memset (sym, 0, sizeof (struct symbol));
4029 SYMBOL_NAME (sym) = obsavestring (name, strlen (name),
4030 &objfile->symbol_obstack);
4031
4032 /* Default assumptions.
4033 Use the passed type or decode it from the die. */
4034 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
4035 SYMBOL_CLASS (sym) = LOC_STATIC;
4036 if (type != NULL)
4037 SYMBOL_TYPE (sym) = type;
4038 else
4039 SYMBOL_TYPE (sym) = die_type (die, objfile);
4040 attr = dwarf_attr (die, DW_AT_decl_line);
4041 if (attr)
4042 {
4043 SYMBOL_LINE (sym) = DW_UNSND (attr);
4044 }
4045
4046 /* If this symbol is from a C++ compilation, then attempt to
4047 cache the demangled form for future reference. This is a
4048 typical time versus space tradeoff, that was decided in favor
4049 of time because it sped up C++ symbol lookups by a factor of
4050 about 20. */
4051
4052 SYMBOL_LANGUAGE (sym) = cu_language;
4053 SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
4054 switch (die->tag)
4055 {
4056 case DW_TAG_label:
4057 attr = dwarf_attr (die, DW_AT_low_pc);
4058 if (attr)
4059 {
4060 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
4061 }
4062 SYMBOL_CLASS (sym) = LOC_LABEL;
4063 break;
4064 case DW_TAG_subprogram:
4065 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
4066 finish_block. */
4067 SYMBOL_CLASS (sym) = LOC_BLOCK;
4068 attr2 = dwarf_attr (die, DW_AT_external);
4069 if (attr2 && (DW_UNSND (attr2) != 0))
4070 {
4071 add_symbol_to_list (sym, &global_symbols);
4072 }
4073 else
4074 {
4075 add_symbol_to_list (sym, list_in_scope);
4076 }
4077 break;
4078 case DW_TAG_variable:
4079 /* Compilation with minimal debug info may result in variables
4080 with missing type entries. Change the misleading `void' type
4081 to something sensible. */
4082 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
4083 SYMBOL_TYPE (sym) = init_type (TYPE_CODE_INT,
4084 TARGET_INT_BIT / HOST_CHAR_BIT, 0,
4085 "<variable, no debug info>",
4086 objfile);
4087 attr = dwarf_attr (die, DW_AT_const_value);
4088 if (attr)
4089 {
4090 dwarf2_const_value (attr, sym, objfile);
4091 attr2 = dwarf_attr (die, DW_AT_external);
4092 if (attr2 && (DW_UNSND (attr2) != 0))
4093 add_symbol_to_list (sym, &global_symbols);
4094 else
4095 add_symbol_to_list (sym, list_in_scope);
4096 break;
4097 }
4098 attr = dwarf_attr (die, DW_AT_location);
4099 if (attr)
4100 {
4101 attr2 = dwarf_attr (die, DW_AT_external);
4102 if (attr2 && (DW_UNSND (attr2) != 0))
4103 {
4104 SYMBOL_VALUE_ADDRESS (sym) =
4105 decode_locdesc (DW_BLOCK (attr), objfile);
4106 add_symbol_to_list (sym, &global_symbols);
4107
4108 /* In shared libraries the address of the variable
4109 in the location descriptor might still be relocatable,
4110 so its value could be zero.
4111 Enter the symbol as a LOC_UNRESOLVED symbol, if its
4112 value is zero, the address of the variable will then
4113 be determined from the minimal symbol table whenever
4114 the variable is referenced. */
4115 if (SYMBOL_VALUE_ADDRESS (sym))
4116 {
4117 SYMBOL_VALUE_ADDRESS (sym) += baseaddr;
4118 SYMBOL_CLASS (sym) = LOC_STATIC;
4119 }
4120 else
4121 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
4122 }
4123 else
4124 {
4125 SYMBOL_VALUE (sym) = addr =
4126 decode_locdesc (DW_BLOCK (attr), objfile);
4127 add_symbol_to_list (sym, list_in_scope);
4128 if (optimized_out)
4129 {
4130 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
4131 }
4132 else if (isreg)
4133 {
4134 SYMBOL_CLASS (sym) = LOC_REGISTER;
4135 }
4136 else if (offreg)
4137 {
4138 SYMBOL_CLASS (sym) = LOC_BASEREG;
4139 SYMBOL_BASEREG (sym) = basereg;
4140 }
4141 else if (islocal)
4142 {
4143 SYMBOL_CLASS (sym) = LOC_LOCAL;
4144 }
4145 else
4146 {
4147 SYMBOL_CLASS (sym) = LOC_STATIC;
4148 SYMBOL_VALUE_ADDRESS (sym) = addr + baseaddr;
4149 }
4150 }
4151 }
4152 else
4153 {
4154 /* We do not know the address of this symbol.
4155 If it is an external symbol and we have type information
4156 for it, enter the symbol as a LOC_UNRESOLVED symbol.
4157 The address of the variable will then be determined from
4158 the minimal symbol table whenever the variable is
4159 referenced. */
4160 attr2 = dwarf_attr (die, DW_AT_external);
4161 if (attr2 && (DW_UNSND (attr2) != 0)
4162 && dwarf_attr (die, DW_AT_type) != NULL)
4163 {
4164 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
4165 add_symbol_to_list (sym, &global_symbols);
4166 }
4167 }
4168 break;
4169 case DW_TAG_formal_parameter:
4170 attr = dwarf_attr (die, DW_AT_location);
4171 if (attr)
4172 {
4173 SYMBOL_VALUE (sym) = decode_locdesc (DW_BLOCK (attr), objfile);
4174 if (isreg)
4175 {
4176 SYMBOL_CLASS (sym) = LOC_REGPARM;
4177 }
4178 else if (offreg)
4179 {
4180 if (isderef)
4181 {
4182 if (basereg != frame_base_reg)
4183 complain (&dwarf2_complex_location_expr);
4184 SYMBOL_CLASS (sym) = LOC_REF_ARG;
4185 }
4186 else
4187 {
4188 SYMBOL_CLASS (sym) = LOC_BASEREG_ARG;
4189 SYMBOL_BASEREG (sym) = basereg;
4190 }
4191 }
4192 else
4193 {
4194 SYMBOL_CLASS (sym) = LOC_ARG;
4195 }
4196 }
4197 attr = dwarf_attr (die, DW_AT_const_value);
4198 if (attr)
4199 {
4200 dwarf2_const_value (attr, sym, objfile);
4201 }
4202 add_symbol_to_list (sym, list_in_scope);
4203 break;
4204 case DW_TAG_unspecified_parameters:
4205 /* From varargs functions; gdb doesn't seem to have any
4206 interest in this information, so just ignore it for now.
4207 (FIXME?) */
4208 break;
4209 case DW_TAG_class_type:
4210 case DW_TAG_structure_type:
4211 case DW_TAG_union_type:
4212 case DW_TAG_enumeration_type:
4213 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4214 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
4215 add_symbol_to_list (sym, list_in_scope);
4216
4217 /* The semantics of C++ state that "struct foo { ... }" also
4218 defines a typedef for "foo". Synthesize a typedef symbol so
4219 that "ptype foo" works as expected. */
4220 if (cu_language == language_cplus)
4221 {
4222 struct symbol *typedef_sym = (struct symbol *)
4223 obstack_alloc (&objfile->symbol_obstack,
4224 sizeof (struct symbol));
4225 *typedef_sym = *sym;
4226 SYMBOL_NAMESPACE (typedef_sym) = VAR_NAMESPACE;
4227 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
4228 TYPE_NAME (SYMBOL_TYPE (sym)) =
4229 obsavestring (SYMBOL_NAME (sym),
4230 strlen (SYMBOL_NAME (sym)),
4231 &objfile->type_obstack);
4232 add_symbol_to_list (typedef_sym, list_in_scope);
4233 }
4234 break;
4235 case DW_TAG_typedef:
4236 case DW_TAG_base_type:
4237 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4238 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
4239 add_symbol_to_list (sym, list_in_scope);
4240 break;
4241 case DW_TAG_enumerator:
4242 attr = dwarf_attr (die, DW_AT_const_value);
4243 if (attr)
4244 {
4245 dwarf2_const_value (attr, sym, objfile);
4246 }
4247 add_symbol_to_list (sym, list_in_scope);
4248 break;
4249 default:
4250 /* Not a tag we recognize. Hopefully we aren't processing
4251 trash data, but since we must specifically ignore things
4252 we don't recognize, there is nothing else we should do at
4253 this point. */
4254 complain (&dwarf2_unsupported_tag, dwarf_tag_name (die->tag));
4255 break;
4256 }
4257 }
4258 return (sym);
4259 }
4260
4261 /* Copy constant value from an attribute to a symbol. */
4262
4263 static void
4264 dwarf2_const_value (attr, sym, objfile)
4265 struct attribute *attr;
4266 struct symbol *sym;
4267 struct objfile *objfile;
4268 {
4269 struct dwarf_block *blk;
4270
4271 switch (attr->form)
4272 {
4273 case DW_FORM_addr:
4274 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != (unsigned int) address_size)
4275 complain (&dwarf2_const_value_length_mismatch, SYMBOL_NAME (sym),
4276 address_size, TYPE_LENGTH (SYMBOL_TYPE (sym)));
4277 SYMBOL_VALUE_BYTES (sym) = (char *)
4278 obstack_alloc (&objfile->symbol_obstack, address_size);
4279 store_address (SYMBOL_VALUE_BYTES (sym), address_size, DW_ADDR (attr));
4280 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
4281 break;
4282 case DW_FORM_block1:
4283 case DW_FORM_block2:
4284 case DW_FORM_block4:
4285 case DW_FORM_block:
4286 blk = DW_BLOCK (attr);
4287 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
4288 complain (&dwarf2_const_value_length_mismatch, SYMBOL_NAME (sym),
4289 blk->size, TYPE_LENGTH (SYMBOL_TYPE (sym)));
4290 SYMBOL_VALUE_BYTES (sym) = (char *)
4291 obstack_alloc (&objfile->symbol_obstack, blk->size);
4292 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
4293 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
4294 break;
4295
4296 /* The DW_AT_const_value attributes are supposed to carry the
4297 symbol's value "represented as it would be on the target
4298 architecture." By the time we get here, it's already been
4299 converted to host endianness, so we just need to sign- or
4300 zero-extend it as appropriate. */
4301 case DW_FORM_data1:
4302 dwarf2_const_value_data (attr, sym, 8);
4303 break;
4304 case DW_FORM_data2:
4305 dwarf2_const_value_data (attr, sym, 16);
4306 break;
4307 case DW_FORM_data4:
4308 dwarf2_const_value_data (attr, sym, 32);
4309 break;
4310 case DW_FORM_data8:
4311 dwarf2_const_value_data (attr, sym, 64);
4312 break;
4313
4314 case DW_FORM_sdata:
4315 SYMBOL_VALUE (sym) = DW_SND (attr);
4316 SYMBOL_CLASS (sym) = LOC_CONST;
4317 break;
4318
4319 case DW_FORM_udata:
4320 SYMBOL_VALUE (sym) = DW_UNSND (attr);
4321 SYMBOL_CLASS (sym) = LOC_CONST;
4322 break;
4323
4324 default:
4325 complain (&dwarf2_unsupported_const_value_attr,
4326 dwarf_form_name (attr->form));
4327 SYMBOL_VALUE (sym) = 0;
4328 SYMBOL_CLASS (sym) = LOC_CONST;
4329 break;
4330 }
4331 }
4332
4333
4334 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
4335 or zero-extend it as appropriate for the symbol's type. */
4336 static void
4337 dwarf2_const_value_data (struct attribute *attr,
4338 struct symbol *sym,
4339 int bits)
4340 {
4341 LONGEST l = DW_UNSND (attr);
4342
4343 if (bits < sizeof (l) * 8)
4344 {
4345 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
4346 l &= ((LONGEST) 1 << bits) - 1;
4347 else
4348 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
4349 }
4350
4351 SYMBOL_VALUE (sym) = l;
4352 SYMBOL_CLASS (sym) = LOC_CONST;
4353 }
4354
4355
4356 /* Return the type of the die in question using its DW_AT_type attribute. */
4357
4358 static struct type *
4359 die_type (die, objfile)
4360 struct die_info *die;
4361 struct objfile *objfile;
4362 {
4363 struct type *type;
4364 struct attribute *type_attr;
4365 struct die_info *type_die;
4366 unsigned int ref;
4367
4368 type_attr = dwarf_attr (die, DW_AT_type);
4369 if (!type_attr)
4370 {
4371 /* A missing DW_AT_type represents a void type. */
4372 return dwarf2_fundamental_type (objfile, FT_VOID);
4373 }
4374 else
4375 {
4376 ref = dwarf2_get_ref_die_offset (type_attr);
4377 type_die = follow_die_ref (ref);
4378 if (!type_die)
4379 {
4380 error ("Dwarf Error: Cannot find referent at offset %d.", ref);
4381 return NULL;
4382 }
4383 }
4384 type = tag_type_to_type (type_die, objfile);
4385 if (!type)
4386 {
4387 dump_die (type_die);
4388 error ("Dwarf Error: Problem turning type die at offset into gdb type.");
4389 }
4390 return type;
4391 }
4392
4393 /* Return the containing type of the die in question using its
4394 DW_AT_containing_type attribute. */
4395
4396 static struct type *
4397 die_containing_type (die, objfile)
4398 struct die_info *die;
4399 struct objfile *objfile;
4400 {
4401 struct type *type = NULL;
4402 struct attribute *type_attr;
4403 struct die_info *type_die = NULL;
4404 unsigned int ref;
4405
4406 type_attr = dwarf_attr (die, DW_AT_containing_type);
4407 if (type_attr)
4408 {
4409 ref = dwarf2_get_ref_die_offset (type_attr);
4410 type_die = follow_die_ref (ref);
4411 if (!type_die)
4412 {
4413 error ("Dwarf Error: Cannot find referent at offset %d.", ref);
4414 return NULL;
4415 }
4416 type = tag_type_to_type (type_die, objfile);
4417 }
4418 if (!type)
4419 {
4420 if (type_die)
4421 dump_die (type_die);
4422 error ("Dwarf Error: Problem turning containing type into gdb type.");
4423 }
4424 return type;
4425 }
4426
4427 #if 0
4428 static struct type *
4429 type_at_offset (offset, objfile)
4430 unsigned int offset;
4431 struct objfile *objfile;
4432 {
4433 struct die_info *die;
4434 struct type *type;
4435
4436 die = follow_die_ref (offset);
4437 if (!die)
4438 {
4439 error ("Dwarf Error: Cannot find type referent at offset %d.", offset);
4440 return NULL;
4441 }
4442 type = tag_type_to_type (die, objfile);
4443 return type;
4444 }
4445 #endif
4446
4447 static struct type *
4448 tag_type_to_type (die, objfile)
4449 struct die_info *die;
4450 struct objfile *objfile;
4451 {
4452 if (die->type)
4453 {
4454 return die->type;
4455 }
4456 else
4457 {
4458 struct attribute *attr;
4459 attr = dwarf_attr (die, DW_AT_name);
4460 if (attr && DW_STRING (attr))
4461 {
4462 char *attrname=DW_STRING (attr);
4463 unsigned long hashval=hash(attrname, strlen(attrname)) % TYPE_HASH_SIZE;
4464
4465 if (dwarf2_cached_types[hashval] != NULL)
4466 {
4467 const char *nameoftype;
4468 nameoftype = TYPE_NAME(dwarf2_cached_types[hashval]) == NULL ? TYPE_TAG_NAME(dwarf2_cached_types[hashval]) : TYPE_NAME(dwarf2_cached_types[hashval]);
4469 if (strcmp(attrname, nameoftype) == 0)
4470 {
4471 die->type=dwarf2_cached_types[hashval];
4472 }
4473 else
4474 {
4475 read_type_die (die, objfile);
4476 dwarf2_cached_types[hashval] = die->type;
4477 }
4478 }
4479 else
4480 {
4481 read_type_die (die, objfile);
4482 dwarf2_cached_types[hashval] = die->type;
4483 }
4484 }
4485 else
4486 {
4487 read_type_die (die, objfile);
4488 }
4489
4490 if (!die->type)
4491 {
4492 dump_die (die);
4493 error ("Dwarf Error: Cannot find type of die.");
4494 }
4495 return die->type;
4496 }
4497 }
4498
4499 static void
4500 read_type_die (die, objfile)
4501 struct die_info *die;
4502 struct objfile *objfile;
4503 {
4504 switch (die->tag)
4505 {
4506 case DW_TAG_class_type:
4507 case DW_TAG_structure_type:
4508 case DW_TAG_union_type:
4509 read_structure_scope (die, objfile);
4510 break;
4511 case DW_TAG_enumeration_type:
4512 read_enumeration (die, objfile);
4513 break;
4514 case DW_TAG_subprogram:
4515 case DW_TAG_subroutine_type:
4516 read_subroutine_type (die, objfile);
4517 break;
4518 case DW_TAG_array_type:
4519 read_array_type (die, objfile);
4520 break;
4521 case DW_TAG_pointer_type:
4522 read_tag_pointer_type (die, objfile);
4523 break;
4524 case DW_TAG_ptr_to_member_type:
4525 read_tag_ptr_to_member_type (die, objfile);
4526 break;
4527 case DW_TAG_reference_type:
4528 read_tag_reference_type (die, objfile);
4529 break;
4530 case DW_TAG_const_type:
4531 read_tag_const_type (die, objfile);
4532 break;
4533 case DW_TAG_volatile_type:
4534 read_tag_volatile_type (die, objfile);
4535 break;
4536 case DW_TAG_string_type:
4537 read_tag_string_type (die, objfile);
4538 break;
4539 case DW_TAG_typedef:
4540 read_typedef (die, objfile);
4541 break;
4542 case DW_TAG_base_type:
4543 read_base_type (die, objfile);
4544 break;
4545 default:
4546 complain (&dwarf2_unexpected_tag, dwarf_tag_name (die->tag));
4547 break;
4548 }
4549 }
4550
4551 static struct type *
4552 dwarf_base_type (encoding, size, objfile)
4553 int encoding;
4554 int size;
4555 struct objfile *objfile;
4556 {
4557 /* FIXME - this should not produce a new (struct type *)
4558 every time. It should cache base types. */
4559 struct type *type;
4560 switch (encoding)
4561 {
4562 case DW_ATE_address:
4563 type = dwarf2_fundamental_type (objfile, FT_VOID);
4564 return type;
4565 case DW_ATE_boolean:
4566 type = dwarf2_fundamental_type (objfile, FT_BOOLEAN);
4567 return type;
4568 case DW_ATE_complex_float:
4569 if (size == 16)
4570 {
4571 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_COMPLEX);
4572 }
4573 else
4574 {
4575 type = dwarf2_fundamental_type (objfile, FT_COMPLEX);
4576 }
4577 return type;
4578 case DW_ATE_float:
4579 if (size == 8)
4580 {
4581 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT);
4582 }
4583 else
4584 {
4585 type = dwarf2_fundamental_type (objfile, FT_FLOAT);
4586 }
4587 return type;
4588 case DW_ATE_signed:
4589 switch (size)
4590 {
4591 case 1:
4592 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
4593 break;
4594 case 2:
4595 type = dwarf2_fundamental_type (objfile, FT_SIGNED_SHORT);
4596 break;
4597 default:
4598 case 4:
4599 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
4600 break;
4601 }
4602 return type;
4603 case DW_ATE_signed_char:
4604 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
4605 return type;
4606 case DW_ATE_unsigned:
4607 switch (size)
4608 {
4609 case 1:
4610 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
4611 break;
4612 case 2:
4613 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_SHORT);
4614 break;
4615 default:
4616 case 4:
4617 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_INTEGER);
4618 break;
4619 }
4620 return type;
4621 case DW_ATE_unsigned_char:
4622 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
4623 return type;
4624 default:
4625 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
4626 return type;
4627 }
4628 }
4629
4630 #if 0
4631 struct die_info *
4632 copy_die (old_die)
4633 struct die_info *old_die;
4634 {
4635 struct die_info *new_die;
4636 int i, num_attrs;
4637
4638 new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
4639 memset (new_die, 0, sizeof (struct die_info));
4640
4641 new_die->tag = old_die->tag;
4642 new_die->has_children = old_die->has_children;
4643 new_die->abbrev = old_die->abbrev;
4644 new_die->offset = old_die->offset;
4645 new_die->type = NULL;
4646
4647 num_attrs = old_die->num_attrs;
4648 new_die->num_attrs = num_attrs;
4649 new_die->attrs = (struct attribute *)
4650 xmalloc (num_attrs * sizeof (struct attribute));
4651
4652 for (i = 0; i < old_die->num_attrs; ++i)
4653 {
4654 new_die->attrs[i].name = old_die->attrs[i].name;
4655 new_die->attrs[i].form = old_die->attrs[i].form;
4656 new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
4657 }
4658
4659 new_die->next = NULL;
4660 return new_die;
4661 }
4662 #endif
4663
4664 /* Return sibling of die, NULL if no sibling. */
4665
4666 struct die_info *
4667 sibling_die (die)
4668 struct die_info *die;
4669 {
4670 int nesting_level = 0;
4671
4672 if (!die->has_children)
4673 {
4674 if (die->next && (die->next->tag == 0))
4675 {
4676 return NULL;
4677 }
4678 else
4679 {
4680 return die->next;
4681 }
4682 }
4683 else
4684 {
4685 do
4686 {
4687 if (die->has_children)
4688 {
4689 nesting_level++;
4690 }
4691 if (die->tag == 0)
4692 {
4693 nesting_level--;
4694 }
4695 die = die->next;
4696 }
4697 while (nesting_level);
4698 if (die && (die->tag == 0))
4699 {
4700 return NULL;
4701 }
4702 else
4703 {
4704 return die;
4705 }
4706 }
4707 }
4708
4709 /* Get linkage name of a die, return NULL if not found. */
4710
4711 static char *
4712 dwarf2_linkage_name (die)
4713 struct die_info *die;
4714 {
4715 struct attribute *attr;
4716
4717 attr = dwarf_attr (die, DW_AT_MIPS_linkage_name);
4718 if (attr && DW_STRING (attr))
4719 return DW_STRING (attr);
4720 attr = dwarf_attr (die, DW_AT_name);
4721 if (attr && DW_STRING (attr))
4722 return DW_STRING (attr);
4723 return NULL;
4724 }
4725
4726 /* Convert a DIE tag into its string name. */
4727
4728 static char *
4729 dwarf_tag_name (tag)
4730 register unsigned tag;
4731 {
4732 switch (tag)
4733 {
4734 case DW_TAG_padding:
4735 return "DW_TAG_padding";
4736 case DW_TAG_array_type:
4737 return "DW_TAG_array_type";
4738 case DW_TAG_class_type:
4739 return "DW_TAG_class_type";
4740 case DW_TAG_entry_point:
4741 return "DW_TAG_entry_point";
4742 case DW_TAG_enumeration_type:
4743 return "DW_TAG_enumeration_type";
4744 case DW_TAG_formal_parameter:
4745 return "DW_TAG_formal_parameter";
4746 case DW_TAG_imported_declaration:
4747 return "DW_TAG_imported_declaration";
4748 case DW_TAG_label:
4749 return "DW_TAG_label";
4750 case DW_TAG_lexical_block:
4751 return "DW_TAG_lexical_block";
4752 case DW_TAG_member:
4753 return "DW_TAG_member";
4754 case DW_TAG_pointer_type:
4755 return "DW_TAG_pointer_type";
4756 case DW_TAG_reference_type:
4757 return "DW_TAG_reference_type";
4758 case DW_TAG_compile_unit:
4759 return "DW_TAG_compile_unit";
4760 case DW_TAG_string_type:
4761 return "DW_TAG_string_type";
4762 case DW_TAG_structure_type:
4763 return "DW_TAG_structure_type";
4764 case DW_TAG_subroutine_type:
4765 return "DW_TAG_subroutine_type";
4766 case DW_TAG_typedef:
4767 return "DW_TAG_typedef";
4768 case DW_TAG_union_type:
4769 return "DW_TAG_union_type";
4770 case DW_TAG_unspecified_parameters:
4771 return "DW_TAG_unspecified_parameters";
4772 case DW_TAG_variant:
4773 return "DW_TAG_variant";
4774 case DW_TAG_common_block:
4775 return "DW_TAG_common_block";
4776 case DW_TAG_common_inclusion:
4777 return "DW_TAG_common_inclusion";
4778 case DW_TAG_inheritance:
4779 return "DW_TAG_inheritance";
4780 case DW_TAG_inlined_subroutine:
4781 return "DW_TAG_inlined_subroutine";
4782 case DW_TAG_module:
4783 return "DW_TAG_module";
4784 case DW_TAG_ptr_to_member_type:
4785 return "DW_TAG_ptr_to_member_type";
4786 case DW_TAG_set_type:
4787 return "DW_TAG_set_type";
4788 case DW_TAG_subrange_type:
4789 return "DW_TAG_subrange_type";
4790 case DW_TAG_with_stmt:
4791 return "DW_TAG_with_stmt";
4792 case DW_TAG_access_declaration:
4793 return "DW_TAG_access_declaration";
4794 case DW_TAG_base_type:
4795 return "DW_TAG_base_type";
4796 case DW_TAG_catch_block:
4797 return "DW_TAG_catch_block";
4798 case DW_TAG_const_type:
4799 return "DW_TAG_const_type";
4800 case DW_TAG_constant:
4801 return "DW_TAG_constant";
4802 case DW_TAG_enumerator:
4803 return "DW_TAG_enumerator";
4804 case DW_TAG_file_type:
4805 return "DW_TAG_file_type";
4806 case DW_TAG_friend:
4807 return "DW_TAG_friend";
4808 case DW_TAG_namelist:
4809 return "DW_TAG_namelist";
4810 case DW_TAG_namelist_item:
4811 return "DW_TAG_namelist_item";
4812 case DW_TAG_packed_type:
4813 return "DW_TAG_packed_type";
4814 case DW_TAG_subprogram:
4815 return "DW_TAG_subprogram";
4816 case DW_TAG_template_type_param:
4817 return "DW_TAG_template_type_param";
4818 case DW_TAG_template_value_param:
4819 return "DW_TAG_template_value_param";
4820 case DW_TAG_thrown_type:
4821 return "DW_TAG_thrown_type";
4822 case DW_TAG_try_block:
4823 return "DW_TAG_try_block";
4824 case DW_TAG_variant_part:
4825 return "DW_TAG_variant_part";
4826 case DW_TAG_variable:
4827 return "DW_TAG_variable";
4828 case DW_TAG_volatile_type:
4829 return "DW_TAG_volatile_type";
4830 case DW_TAG_MIPS_loop:
4831 return "DW_TAG_MIPS_loop";
4832 case DW_TAG_format_label:
4833 return "DW_TAG_format_label";
4834 case DW_TAG_function_template:
4835 return "DW_TAG_function_template";
4836 case DW_TAG_class_template:
4837 return "DW_TAG_class_template";
4838 default:
4839 return "DW_TAG_<unknown>";
4840 }
4841 }
4842
4843 /* Convert a DWARF attribute code into its string name. */
4844
4845 static char *
4846 dwarf_attr_name (attr)
4847 register unsigned attr;
4848 {
4849 switch (attr)
4850 {
4851 case DW_AT_sibling:
4852 return "DW_AT_sibling";
4853 case DW_AT_location:
4854 return "DW_AT_location";
4855 case DW_AT_name:
4856 return "DW_AT_name";
4857 case DW_AT_ordering:
4858 return "DW_AT_ordering";
4859 case DW_AT_subscr_data:
4860 return "DW_AT_subscr_data";
4861 case DW_AT_byte_size:
4862 return "DW_AT_byte_size";
4863 case DW_AT_bit_offset:
4864 return "DW_AT_bit_offset";
4865 case DW_AT_bit_size:
4866 return "DW_AT_bit_size";
4867 case DW_AT_element_list:
4868 return "DW_AT_element_list";
4869 case DW_AT_stmt_list:
4870 return "DW_AT_stmt_list";
4871 case DW_AT_low_pc:
4872 return "DW_AT_low_pc";
4873 case DW_AT_high_pc:
4874 return "DW_AT_high_pc";
4875 case DW_AT_language:
4876 return "DW_AT_language";
4877 case DW_AT_member:
4878 return "DW_AT_member";
4879 case DW_AT_discr:
4880 return "DW_AT_discr";
4881 case DW_AT_discr_value:
4882 return "DW_AT_discr_value";
4883 case DW_AT_visibility:
4884 return "DW_AT_visibility";
4885 case DW_AT_import:
4886 return "DW_AT_import";
4887 case DW_AT_string_length:
4888 return "DW_AT_string_length";
4889 case DW_AT_common_reference:
4890 return "DW_AT_common_reference";
4891 case DW_AT_comp_dir:
4892 return "DW_AT_comp_dir";
4893 case DW_AT_const_value:
4894 return "DW_AT_const_value";
4895 case DW_AT_containing_type:
4896 return "DW_AT_containing_type";
4897 case DW_AT_default_value:
4898 return "DW_AT_default_value";
4899 case DW_AT_inline:
4900 return "DW_AT_inline";
4901 case DW_AT_is_optional:
4902 return "DW_AT_is_optional";
4903 case DW_AT_lower_bound:
4904 return "DW_AT_lower_bound";
4905 case DW_AT_producer:
4906 return "DW_AT_producer";
4907 case DW_AT_prototyped:
4908 return "DW_AT_prototyped";
4909 case DW_AT_return_addr:
4910 return "DW_AT_return_addr";
4911 case DW_AT_start_scope:
4912 return "DW_AT_start_scope";
4913 case DW_AT_stride_size:
4914 return "DW_AT_stride_size";
4915 case DW_AT_upper_bound:
4916 return "DW_AT_upper_bound";
4917 case DW_AT_abstract_origin:
4918 return "DW_AT_abstract_origin";
4919 case DW_AT_accessibility:
4920 return "DW_AT_accessibility";
4921 case DW_AT_address_class:
4922 return "DW_AT_address_class";
4923 case DW_AT_artificial:
4924 return "DW_AT_artificial";
4925 case DW_AT_base_types:
4926 return "DW_AT_base_types";
4927 case DW_AT_calling_convention:
4928 return "DW_AT_calling_convention";
4929 case DW_AT_count:
4930 return "DW_AT_count";
4931 case DW_AT_data_member_location:
4932 return "DW_AT_data_member_location";
4933 case DW_AT_decl_column:
4934 return "DW_AT_decl_column";
4935 case DW_AT_decl_file:
4936 return "DW_AT_decl_file";
4937 case DW_AT_decl_line:
4938 return "DW_AT_decl_line";
4939 case DW_AT_declaration:
4940 return "DW_AT_declaration";
4941 case DW_AT_discr_list:
4942 return "DW_AT_discr_list";
4943 case DW_AT_encoding:
4944 return "DW_AT_encoding";
4945 case DW_AT_external:
4946 return "DW_AT_external";
4947 case DW_AT_frame_base:
4948 return "DW_AT_frame_base";
4949 case DW_AT_friend:
4950 return "DW_AT_friend";
4951 case DW_AT_identifier_case:
4952 return "DW_AT_identifier_case";
4953 case DW_AT_macro_info:
4954 return "DW_AT_macro_info";
4955 case DW_AT_namelist_items:
4956 return "DW_AT_namelist_items";
4957 case DW_AT_priority:
4958 return "DW_AT_priority";
4959 case DW_AT_segment:
4960 return "DW_AT_segment";
4961 case DW_AT_specification:
4962 return "DW_AT_specification";
4963 case DW_AT_static_link:
4964 return "DW_AT_static_link";
4965 case DW_AT_type:
4966 return "DW_AT_type";
4967 case DW_AT_use_location:
4968 return "DW_AT_use_location";
4969 case DW_AT_variable_parameter:
4970 return "DW_AT_variable_parameter";
4971 case DW_AT_virtuality:
4972 return "DW_AT_virtuality";
4973 case DW_AT_vtable_elem_location:
4974 return "DW_AT_vtable_elem_location";
4975
4976 #ifdef MIPS
4977 case DW_AT_MIPS_fde:
4978 return "DW_AT_MIPS_fde";
4979 case DW_AT_MIPS_loop_begin:
4980 return "DW_AT_MIPS_loop_begin";
4981 case DW_AT_MIPS_tail_loop_begin:
4982 return "DW_AT_MIPS_tail_loop_begin";
4983 case DW_AT_MIPS_epilog_begin:
4984 return "DW_AT_MIPS_epilog_begin";
4985 case DW_AT_MIPS_loop_unroll_factor:
4986 return "DW_AT_MIPS_loop_unroll_factor";
4987 case DW_AT_MIPS_software_pipeline_depth:
4988 return "DW_AT_MIPS_software_pipeline_depth";
4989 case DW_AT_MIPS_linkage_name:
4990 return "DW_AT_MIPS_linkage_name";
4991 #endif
4992
4993 case DW_AT_sf_names:
4994 return "DW_AT_sf_names";
4995 case DW_AT_src_info:
4996 return "DW_AT_src_info";
4997 case DW_AT_mac_info:
4998 return "DW_AT_mac_info";
4999 case DW_AT_src_coords:
5000 return "DW_AT_src_coords";
5001 case DW_AT_body_begin:
5002 return "DW_AT_body_begin";
5003 case DW_AT_body_end:
5004 return "DW_AT_body_end";
5005 default:
5006 return "DW_AT_<unknown>";
5007 }
5008 }
5009
5010 /* Convert a DWARF value form code into its string name. */
5011
5012 static char *
5013 dwarf_form_name (form)
5014 register unsigned form;
5015 {
5016 switch (form)
5017 {
5018 case DW_FORM_addr:
5019 return "DW_FORM_addr";
5020 case DW_FORM_block2:
5021 return "DW_FORM_block2";
5022 case DW_FORM_block4:
5023 return "DW_FORM_block4";
5024 case DW_FORM_data2:
5025 return "DW_FORM_data2";
5026 case DW_FORM_data4:
5027 return "DW_FORM_data4";
5028 case DW_FORM_data8:
5029 return "DW_FORM_data8";
5030 case DW_FORM_string:
5031 return "DW_FORM_string";
5032 case DW_FORM_block:
5033 return "DW_FORM_block";
5034 case DW_FORM_block1:
5035 return "DW_FORM_block1";
5036 case DW_FORM_data1:
5037 return "DW_FORM_data1";
5038 case DW_FORM_flag:
5039 return "DW_FORM_flag";
5040 case DW_FORM_sdata:
5041 return "DW_FORM_sdata";
5042 case DW_FORM_strp:
5043 return "DW_FORM_strp";
5044 case DW_FORM_udata:
5045 return "DW_FORM_udata";
5046 case DW_FORM_ref_addr:
5047 return "DW_FORM_ref_addr";
5048 case DW_FORM_ref1:
5049 return "DW_FORM_ref1";
5050 case DW_FORM_ref2:
5051 return "DW_FORM_ref2";
5052 case DW_FORM_ref4:
5053 return "DW_FORM_ref4";
5054 case DW_FORM_ref8:
5055 return "DW_FORM_ref8";
5056 case DW_FORM_ref_udata:
5057 return "DW_FORM_ref_udata";
5058 case DW_FORM_indirect:
5059 return "DW_FORM_indirect";
5060 default:
5061 return "DW_FORM_<unknown>";
5062 }
5063 }
5064
5065 /* Convert a DWARF stack opcode into its string name. */
5066
5067 static char *
5068 dwarf_stack_op_name (op)
5069 register unsigned op;
5070 {
5071 switch (op)
5072 {
5073 case DW_OP_addr:
5074 return "DW_OP_addr";
5075 case DW_OP_deref:
5076 return "DW_OP_deref";
5077 case DW_OP_const1u:
5078 return "DW_OP_const1u";
5079 case DW_OP_const1s:
5080 return "DW_OP_const1s";
5081 case DW_OP_const2u:
5082 return "DW_OP_const2u";
5083 case DW_OP_const2s:
5084 return "DW_OP_const2s";
5085 case DW_OP_const4u:
5086 return "DW_OP_const4u";
5087 case DW_OP_const4s:
5088 return "DW_OP_const4s";
5089 case DW_OP_const8u:
5090 return "DW_OP_const8u";
5091 case DW_OP_const8s:
5092 return "DW_OP_const8s";
5093 case DW_OP_constu:
5094 return "DW_OP_constu";
5095 case DW_OP_consts:
5096 return "DW_OP_consts";
5097 case DW_OP_dup:
5098 return "DW_OP_dup";
5099 case DW_OP_drop:
5100 return "DW_OP_drop";
5101 case DW_OP_over:
5102 return "DW_OP_over";
5103 case DW_OP_pick:
5104 return "DW_OP_pick";
5105 case DW_OP_swap:
5106 return "DW_OP_swap";
5107 case DW_OP_rot:
5108 return "DW_OP_rot";
5109 case DW_OP_xderef:
5110 return "DW_OP_xderef";
5111 case DW_OP_abs:
5112 return "DW_OP_abs";
5113 case DW_OP_and:
5114 return "DW_OP_and";
5115 case DW_OP_div:
5116 return "DW_OP_div";
5117 case DW_OP_minus:
5118 return "DW_OP_minus";
5119 case DW_OP_mod:
5120 return "DW_OP_mod";
5121 case DW_OP_mul:
5122 return "DW_OP_mul";
5123 case DW_OP_neg:
5124 return "DW_OP_neg";
5125 case DW_OP_not:
5126 return "DW_OP_not";
5127 case DW_OP_or:
5128 return "DW_OP_or";
5129 case DW_OP_plus:
5130 return "DW_OP_plus";
5131 case DW_OP_plus_uconst:
5132 return "DW_OP_plus_uconst";
5133 case DW_OP_shl:
5134 return "DW_OP_shl";
5135 case DW_OP_shr:
5136 return "DW_OP_shr";
5137 case DW_OP_shra:
5138 return "DW_OP_shra";
5139 case DW_OP_xor:
5140 return "DW_OP_xor";
5141 case DW_OP_bra:
5142 return "DW_OP_bra";
5143 case DW_OP_eq:
5144 return "DW_OP_eq";
5145 case DW_OP_ge:
5146 return "DW_OP_ge";
5147 case DW_OP_gt:
5148 return "DW_OP_gt";
5149 case DW_OP_le:
5150 return "DW_OP_le";
5151 case DW_OP_lt:
5152 return "DW_OP_lt";
5153 case DW_OP_ne:
5154 return "DW_OP_ne";
5155 case DW_OP_skip:
5156 return "DW_OP_skip";
5157 case DW_OP_lit0:
5158 return "DW_OP_lit0";
5159 case DW_OP_lit1:
5160 return "DW_OP_lit1";
5161 case DW_OP_lit2:
5162 return "DW_OP_lit2";
5163 case DW_OP_lit3:
5164 return "DW_OP_lit3";
5165 case DW_OP_lit4:
5166 return "DW_OP_lit4";
5167 case DW_OP_lit5:
5168 return "DW_OP_lit5";
5169 case DW_OP_lit6:
5170 return "DW_OP_lit6";
5171 case DW_OP_lit7:
5172 return "DW_OP_lit7";
5173 case DW_OP_lit8:
5174 return "DW_OP_lit8";
5175 case DW_OP_lit9:
5176 return "DW_OP_lit9";
5177 case DW_OP_lit10:
5178 return "DW_OP_lit10";
5179 case DW_OP_lit11:
5180 return "DW_OP_lit11";
5181 case DW_OP_lit12:
5182 return "DW_OP_lit12";
5183 case DW_OP_lit13:
5184 return "DW_OP_lit13";
5185 case DW_OP_lit14:
5186 return "DW_OP_lit14";
5187 case DW_OP_lit15:
5188 return "DW_OP_lit15";
5189 case DW_OP_lit16:
5190 return "DW_OP_lit16";
5191 case DW_OP_lit17:
5192 return "DW_OP_lit17";
5193 case DW_OP_lit18:
5194 return "DW_OP_lit18";
5195 case DW_OP_lit19:
5196 return "DW_OP_lit19";
5197 case DW_OP_lit20:
5198 return "DW_OP_lit20";
5199 case DW_OP_lit21:
5200 return "DW_OP_lit21";
5201 case DW_OP_lit22:
5202 return "DW_OP_lit22";
5203 case DW_OP_lit23:
5204 return "DW_OP_lit23";
5205 case DW_OP_lit24:
5206 return "DW_OP_lit24";
5207 case DW_OP_lit25:
5208 return "DW_OP_lit25";
5209 case DW_OP_lit26:
5210 return "DW_OP_lit26";
5211 case DW_OP_lit27:
5212 return "DW_OP_lit27";
5213 case DW_OP_lit28:
5214 return "DW_OP_lit28";
5215 case DW_OP_lit29:
5216 return "DW_OP_lit29";
5217 case DW_OP_lit30:
5218 return "DW_OP_lit30";
5219 case DW_OP_lit31:
5220 return "DW_OP_lit31";
5221 case DW_OP_reg0:
5222 return "DW_OP_reg0";
5223 case DW_OP_reg1:
5224 return "DW_OP_reg1";
5225 case DW_OP_reg2:
5226 return "DW_OP_reg2";
5227 case DW_OP_reg3:
5228 return "DW_OP_reg3";
5229 case DW_OP_reg4:
5230 return "DW_OP_reg4";
5231 case DW_OP_reg5:
5232 return "DW_OP_reg5";
5233 case DW_OP_reg6:
5234 return "DW_OP_reg6";
5235 case DW_OP_reg7:
5236 return "DW_OP_reg7";
5237 case DW_OP_reg8:
5238 return "DW_OP_reg8";
5239 case DW_OP_reg9:
5240 return "DW_OP_reg9";
5241 case DW_OP_reg10:
5242 return "DW_OP_reg10";
5243 case DW_OP_reg11:
5244 return "DW_OP_reg11";
5245 case DW_OP_reg12:
5246 return "DW_OP_reg12";
5247 case DW_OP_reg13:
5248 return "DW_OP_reg13";
5249 case DW_OP_reg14:
5250 return "DW_OP_reg14";
5251 case DW_OP_reg15:
5252 return "DW_OP_reg15";
5253 case DW_OP_reg16:
5254 return "DW_OP_reg16";
5255 case DW_OP_reg17:
5256 return "DW_OP_reg17";
5257 case DW_OP_reg18:
5258 return "DW_OP_reg18";
5259 case DW_OP_reg19:
5260 return "DW_OP_reg19";
5261 case DW_OP_reg20:
5262 return "DW_OP_reg20";
5263 case DW_OP_reg21:
5264 return "DW_OP_reg21";
5265 case DW_OP_reg22:
5266 return "DW_OP_reg22";
5267 case DW_OP_reg23:
5268 return "DW_OP_reg23";
5269 case DW_OP_reg24:
5270 return "DW_OP_reg24";
5271 case DW_OP_reg25:
5272 return "DW_OP_reg25";
5273 case DW_OP_reg26:
5274 return "DW_OP_reg26";
5275 case DW_OP_reg27:
5276 return "DW_OP_reg27";
5277 case DW_OP_reg28:
5278 return "DW_OP_reg28";
5279 case DW_OP_reg29:
5280 return "DW_OP_reg29";
5281 case DW_OP_reg30:
5282 return "DW_OP_reg30";
5283 case DW_OP_reg31:
5284 return "DW_OP_reg31";
5285 case DW_OP_breg0:
5286 return "DW_OP_breg0";
5287 case DW_OP_breg1:
5288 return "DW_OP_breg1";
5289 case DW_OP_breg2:
5290 return "DW_OP_breg2";
5291 case DW_OP_breg3:
5292 return "DW_OP_breg3";
5293 case DW_OP_breg4:
5294 return "DW_OP_breg4";
5295 case DW_OP_breg5:
5296 return "DW_OP_breg5";
5297 case DW_OP_breg6:
5298 return "DW_OP_breg6";
5299 case DW_OP_breg7:
5300 return "DW_OP_breg7";
5301 case DW_OP_breg8:
5302 return "DW_OP_breg8";
5303 case DW_OP_breg9:
5304 return "DW_OP_breg9";
5305 case DW_OP_breg10:
5306 return "DW_OP_breg10";
5307 case DW_OP_breg11:
5308 return "DW_OP_breg11";
5309 case DW_OP_breg12:
5310 return "DW_OP_breg12";
5311 case DW_OP_breg13:
5312 return "DW_OP_breg13";
5313 case DW_OP_breg14:
5314 return "DW_OP_breg14";
5315 case DW_OP_breg15:
5316 return "DW_OP_breg15";
5317 case DW_OP_breg16:
5318 return "DW_OP_breg16";
5319 case DW_OP_breg17:
5320 return "DW_OP_breg17";
5321 case DW_OP_breg18:
5322 return "DW_OP_breg18";
5323 case DW_OP_breg19:
5324 return "DW_OP_breg19";
5325 case DW_OP_breg20:
5326 return "DW_OP_breg20";
5327 case DW_OP_breg21:
5328 return "DW_OP_breg21";
5329 case DW_OP_breg22:
5330 return "DW_OP_breg22";
5331 case DW_OP_breg23:
5332 return "DW_OP_breg23";
5333 case DW_OP_breg24:
5334 return "DW_OP_breg24";
5335 case DW_OP_breg25:
5336 return "DW_OP_breg25";
5337 case DW_OP_breg26:
5338 return "DW_OP_breg26";
5339 case DW_OP_breg27:
5340 return "DW_OP_breg27";
5341 case DW_OP_breg28:
5342 return "DW_OP_breg28";
5343 case DW_OP_breg29:
5344 return "DW_OP_breg29";
5345 case DW_OP_breg30:
5346 return "DW_OP_breg30";
5347 case DW_OP_breg31:
5348 return "DW_OP_breg31";
5349 case DW_OP_regx:
5350 return "DW_OP_regx";
5351 case DW_OP_fbreg:
5352 return "DW_OP_fbreg";
5353 case DW_OP_bregx:
5354 return "DW_OP_bregx";
5355 case DW_OP_piece:
5356 return "DW_OP_piece";
5357 case DW_OP_deref_size:
5358 return "DW_OP_deref_size";
5359 case DW_OP_xderef_size:
5360 return "DW_OP_xderef_size";
5361 case DW_OP_nop:
5362 return "DW_OP_nop";
5363 default:
5364 return "OP_<unknown>";
5365 }
5366 }
5367
5368 static char *
5369 dwarf_bool_name (mybool)
5370 unsigned mybool;
5371 {
5372 if (mybool)
5373 return "TRUE";
5374 else
5375 return "FALSE";
5376 }
5377
5378 /* Convert a DWARF type code into its string name. */
5379
5380 static char *
5381 dwarf_type_encoding_name (enc)
5382 register unsigned enc;
5383 {
5384 switch (enc)
5385 {
5386 case DW_ATE_address:
5387 return "DW_ATE_address";
5388 case DW_ATE_boolean:
5389 return "DW_ATE_boolean";
5390 case DW_ATE_complex_float:
5391 return "DW_ATE_complex_float";
5392 case DW_ATE_float:
5393 return "DW_ATE_float";
5394 case DW_ATE_signed:
5395 return "DW_ATE_signed";
5396 case DW_ATE_signed_char:
5397 return "DW_ATE_signed_char";
5398 case DW_ATE_unsigned:
5399 return "DW_ATE_unsigned";
5400 case DW_ATE_unsigned_char:
5401 return "DW_ATE_unsigned_char";
5402 default:
5403 return "DW_ATE_<unknown>";
5404 }
5405 }
5406
5407 /* Convert a DWARF call frame info operation to its string name. */
5408
5409 #if 0
5410 static char *
5411 dwarf_cfi_name (cfi_opc)
5412 register unsigned cfi_opc;
5413 {
5414 switch (cfi_opc)
5415 {
5416 case DW_CFA_advance_loc:
5417 return "DW_CFA_advance_loc";
5418 case DW_CFA_offset:
5419 return "DW_CFA_offset";
5420 case DW_CFA_restore:
5421 return "DW_CFA_restore";
5422 case DW_CFA_nop:
5423 return "DW_CFA_nop";
5424 case DW_CFA_set_loc:
5425 return "DW_CFA_set_loc";
5426 case DW_CFA_advance_loc1:
5427 return "DW_CFA_advance_loc1";
5428 case DW_CFA_advance_loc2:
5429 return "DW_CFA_advance_loc2";
5430 case DW_CFA_advance_loc4:
5431 return "DW_CFA_advance_loc4";
5432 case DW_CFA_offset_extended:
5433 return "DW_CFA_offset_extended";
5434 case DW_CFA_restore_extended:
5435 return "DW_CFA_restore_extended";
5436 case DW_CFA_undefined:
5437 return "DW_CFA_undefined";
5438 case DW_CFA_same_value:
5439 return "DW_CFA_same_value";
5440 case DW_CFA_register:
5441 return "DW_CFA_register";
5442 case DW_CFA_remember_state:
5443 return "DW_CFA_remember_state";
5444 case DW_CFA_restore_state:
5445 return "DW_CFA_restore_state";
5446 case DW_CFA_def_cfa:
5447 return "DW_CFA_def_cfa";
5448 case DW_CFA_def_cfa_register:
5449 return "DW_CFA_def_cfa_register";
5450 case DW_CFA_def_cfa_offset:
5451 return "DW_CFA_def_cfa_offset";
5452 /* SGI/MIPS specific */
5453 case DW_CFA_MIPS_advance_loc8:
5454 return "DW_CFA_MIPS_advance_loc8";
5455 default:
5456 return "DW_CFA_<unknown>";
5457 }
5458 }
5459 #endif
5460
5461 void
5462 dump_die (die)
5463 struct die_info *die;
5464 {
5465 unsigned int i;
5466
5467 fprintf (stderr, "Die: %s (abbrev = %d, offset = %d)\n",
5468 dwarf_tag_name (die->tag), die->abbrev, die->offset);
5469 fprintf (stderr, "\thas children: %s\n",
5470 dwarf_bool_name (die->has_children));
5471
5472 fprintf (stderr, "\tattributes:\n");
5473 for (i = 0; i < die->num_attrs; ++i)
5474 {
5475 fprintf (stderr, "\t\t%s (%s) ",
5476 dwarf_attr_name (die->attrs[i].name),
5477 dwarf_form_name (die->attrs[i].form));
5478 switch (die->attrs[i].form)
5479 {
5480 case DW_FORM_ref_addr:
5481 case DW_FORM_addr:
5482 fprintf (stderr, "address: ");
5483 print_address_numeric (DW_ADDR (&die->attrs[i]), 1, gdb_stderr);
5484 break;
5485 case DW_FORM_block2:
5486 case DW_FORM_block4:
5487 case DW_FORM_block:
5488 case DW_FORM_block1:
5489 fprintf (stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
5490 break;
5491 case DW_FORM_data1:
5492 case DW_FORM_data2:
5493 case DW_FORM_data4:
5494 case DW_FORM_ref1:
5495 case DW_FORM_ref2:
5496 case DW_FORM_ref4:
5497 case DW_FORM_udata:
5498 case DW_FORM_sdata:
5499 fprintf (stderr, "constant: %d", DW_UNSND (&die->attrs[i]));
5500 break;
5501 case DW_FORM_string:
5502 fprintf (stderr, "string: \"%s\"",
5503 DW_STRING (&die->attrs[i])
5504 ? DW_STRING (&die->attrs[i]) : "");
5505 break;
5506 case DW_FORM_flag:
5507 if (DW_UNSND (&die->attrs[i]))
5508 fprintf (stderr, "flag: TRUE");
5509 else
5510 fprintf (stderr, "flag: FALSE");
5511 break;
5512 case DW_FORM_strp: /* we do not support separate string
5513 section yet */
5514 case DW_FORM_indirect: /* we do not handle indirect yet */
5515 case DW_FORM_data8: /* we do not have 64 bit quantities */
5516 default:
5517 fprintf (stderr, "unsupported attribute form: %d.",
5518 die->attrs[i].form);
5519 }
5520 fprintf (stderr, "\n");
5521 }
5522 }
5523
5524 void
5525 dump_die_list (die)
5526 struct die_info *die;
5527 {
5528 while (die)
5529 {
5530 dump_die (die);
5531 die = die->next;
5532 }
5533 }
5534
5535 void
5536 store_in_ref_table (offset, die)
5537 unsigned int offset;
5538 struct die_info *die;
5539 {
5540 int h;
5541 struct die_info *old;
5542
5543 h = (offset % REF_HASH_SIZE);
5544 old = die_ref_table[h];
5545 die->next_ref = old;
5546 die_ref_table[h] = die;
5547 }
5548
5549
5550 static void
5551 dwarf2_empty_die_ref_table ()
5552 {
5553 memset (die_ref_table, 0, sizeof (die_ref_table));
5554 }
5555
5556 static unsigned int
5557 dwarf2_get_ref_die_offset (attr)
5558 struct attribute *attr;
5559 {
5560 unsigned int result = 0;
5561
5562 switch (attr->form)
5563 {
5564 case DW_FORM_ref_addr:
5565 result = DW_ADDR (attr);
5566 break;
5567 case DW_FORM_ref1:
5568 case DW_FORM_ref2:
5569 case DW_FORM_ref4:
5570 case DW_FORM_ref_udata:
5571 result = cu_header_offset + DW_UNSND (attr);
5572 break;
5573 default:
5574 complain (&dwarf2_unsupported_die_ref_attr, dwarf_form_name (attr->form));
5575 }
5576 return result;
5577 }
5578
5579 struct die_info *
5580 follow_die_ref (offset)
5581 unsigned int offset;
5582 {
5583 struct die_info *die;
5584 int h;
5585
5586 h = (offset % REF_HASH_SIZE);
5587 die = die_ref_table[h];
5588 while (die)
5589 {
5590 if (die->offset == offset)
5591 {
5592 return die;
5593 }
5594 die = die->next_ref;
5595 }
5596 return NULL;
5597 }
5598
5599 static struct type *
5600 dwarf2_fundamental_type (objfile, typeid)
5601 struct objfile *objfile;
5602 int typeid;
5603 {
5604 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
5605 {
5606 error ("Dwarf Error: internal error - invalid fundamental type id %d.",
5607 typeid);
5608 }
5609
5610 /* Look for this particular type in the fundamental type vector. If
5611 one is not found, create and install one appropriate for the
5612 current language and the current target machine. */
5613
5614 if (ftypes[typeid] == NULL)
5615 {
5616 ftypes[typeid] = cu_language_defn->la_fund_type (objfile, typeid);
5617 }
5618
5619 return (ftypes[typeid]);
5620 }
5621
5622 /* Decode simple location descriptions.
5623 Given a pointer to a dwarf block that defines a location, compute
5624 the location and return the value.
5625
5626 FIXME: This is a kludge until we figure out a better
5627 way to handle the location descriptions.
5628 Gdb's design does not mesh well with the DWARF2 notion of a location
5629 computing interpreter, which is a shame because the flexibility goes unused.
5630 FIXME: Implement more operations as necessary.
5631
5632 A location description containing no operations indicates that the
5633 object is optimized out. The global optimized_out flag is set for
5634 those, the return value is meaningless.
5635
5636 When the result is a register number, the global isreg flag is set,
5637 otherwise it is cleared.
5638
5639 When the result is a base register offset, the global offreg flag is set
5640 and the register number is returned in basereg, otherwise it is cleared.
5641
5642 When the DW_OP_fbreg operation is encountered without a corresponding
5643 DW_AT_frame_base attribute, the global islocal flag is set.
5644 Hopefully the machine dependent code knows how to set up a virtual
5645 frame pointer for the local references.
5646
5647 Note that stack[0] is unused except as a default error return.
5648 Note that stack overflow is not yet handled. */
5649
5650 static CORE_ADDR
5651 decode_locdesc (blk, objfile)
5652 struct dwarf_block *blk;
5653 struct objfile *objfile;
5654 {
5655 int i;
5656 int size = blk->size;
5657 char *data = blk->data;
5658 CORE_ADDR stack[64];
5659 int stacki;
5660 unsigned int bytes_read, unsnd;
5661 unsigned char op;
5662
5663 i = 0;
5664 stacki = 0;
5665 stack[stacki] = 0;
5666 isreg = 0;
5667 offreg = 0;
5668 isderef = 0;
5669 islocal = 0;
5670 optimized_out = 1;
5671
5672 while (i < size)
5673 {
5674 optimized_out = 0;
5675 op = data[i++];
5676 switch (op)
5677 {
5678 case DW_OP_reg0:
5679 case DW_OP_reg1:
5680 case DW_OP_reg2:
5681 case DW_OP_reg3:
5682 case DW_OP_reg4:
5683 case DW_OP_reg5:
5684 case DW_OP_reg6:
5685 case DW_OP_reg7:
5686 case DW_OP_reg8:
5687 case DW_OP_reg9:
5688 case DW_OP_reg10:
5689 case DW_OP_reg11:
5690 case DW_OP_reg12:
5691 case DW_OP_reg13:
5692 case DW_OP_reg14:
5693 case DW_OP_reg15:
5694 case DW_OP_reg16:
5695 case DW_OP_reg17:
5696 case DW_OP_reg18:
5697 case DW_OP_reg19:
5698 case DW_OP_reg20:
5699 case DW_OP_reg21:
5700 case DW_OP_reg22:
5701 case DW_OP_reg23:
5702 case DW_OP_reg24:
5703 case DW_OP_reg25:
5704 case DW_OP_reg26:
5705 case DW_OP_reg27:
5706 case DW_OP_reg28:
5707 case DW_OP_reg29:
5708 case DW_OP_reg30:
5709 case DW_OP_reg31:
5710 isreg = 1;
5711 stack[++stacki] = op - DW_OP_reg0;
5712 break;
5713
5714 case DW_OP_regx:
5715 isreg = 1;
5716 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5717 i += bytes_read;
5718 #if defined(HARRIS_TARGET) && defined(_M88K)
5719 /* The Harris 88110 gdb ports have long kept their special reg
5720 numbers between their gp-regs and their x-regs. This is
5721 not how our dwarf is generated. Punt. */
5722 unsnd += 6;
5723 #endif
5724 stack[++stacki] = unsnd;
5725 break;
5726
5727 case DW_OP_breg0:
5728 case DW_OP_breg1:
5729 case DW_OP_breg2:
5730 case DW_OP_breg3:
5731 case DW_OP_breg4:
5732 case DW_OP_breg5:
5733 case DW_OP_breg6:
5734 case DW_OP_breg7:
5735 case DW_OP_breg8:
5736 case DW_OP_breg9:
5737 case DW_OP_breg10:
5738 case DW_OP_breg11:
5739 case DW_OP_breg12:
5740 case DW_OP_breg13:
5741 case DW_OP_breg14:
5742 case DW_OP_breg15:
5743 case DW_OP_breg16:
5744 case DW_OP_breg17:
5745 case DW_OP_breg18:
5746 case DW_OP_breg19:
5747 case DW_OP_breg20:
5748 case DW_OP_breg21:
5749 case DW_OP_breg22:
5750 case DW_OP_breg23:
5751 case DW_OP_breg24:
5752 case DW_OP_breg25:
5753 case DW_OP_breg26:
5754 case DW_OP_breg27:
5755 case DW_OP_breg28:
5756 case DW_OP_breg29:
5757 case DW_OP_breg30:
5758 case DW_OP_breg31:
5759 offreg = 1;
5760 basereg = op - DW_OP_breg0;
5761 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5762 i += bytes_read;
5763 break;
5764
5765 case DW_OP_bregx:
5766 offreg = 1;
5767 basereg = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5768 i += bytes_read;
5769 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5770 i += bytes_read;
5771 break;
5772
5773 case DW_OP_fbreg:
5774 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5775 i += bytes_read;
5776 if (frame_base_reg >= 0)
5777 {
5778 offreg = 1;
5779 basereg = frame_base_reg;
5780 stack[stacki] += frame_base_offset;
5781 }
5782 else
5783 {
5784 complain (&dwarf2_missing_at_frame_base);
5785 islocal = 1;
5786 }
5787 break;
5788
5789 case DW_OP_addr:
5790 stack[++stacki] = read_address (objfile->obfd, &data[i]);
5791 i += address_size;
5792 break;
5793
5794 case DW_OP_const1u:
5795 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
5796 i += 1;
5797 break;
5798
5799 case DW_OP_const1s:
5800 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
5801 i += 1;
5802 break;
5803
5804 case DW_OP_const2u:
5805 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
5806 i += 2;
5807 break;
5808
5809 case DW_OP_const2s:
5810 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
5811 i += 2;
5812 break;
5813
5814 case DW_OP_const4u:
5815 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
5816 i += 4;
5817 break;
5818
5819 case DW_OP_const4s:
5820 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
5821 i += 4;
5822 break;
5823
5824 case DW_OP_constu:
5825 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
5826 &bytes_read);
5827 i += bytes_read;
5828 break;
5829
5830 case DW_OP_consts:
5831 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5832 i += bytes_read;
5833 break;
5834
5835 case DW_OP_plus:
5836 stack[stacki - 1] += stack[stacki];
5837 stacki--;
5838 break;
5839
5840 case DW_OP_plus_uconst:
5841 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5842 i += bytes_read;
5843 break;
5844
5845 case DW_OP_minus:
5846 stack[stacki - 1] = stack[stacki] - stack[stacki - 1];
5847 stacki--;
5848 break;
5849
5850 case DW_OP_deref:
5851 isderef = 1;
5852 /* If we're not the last op, then we definitely can't encode
5853 this using GDB's address_class enum. */
5854 if (i < size)
5855 complain (&dwarf2_complex_location_expr);
5856 break;
5857
5858 default:
5859 complain (&dwarf2_unsupported_stack_op, dwarf_stack_op_name (op));
5860 return (stack[stacki]);
5861 }
5862 }
5863 return (stack[stacki]);
5864 }
5865
5866 /* memory allocation interface */
5867
5868 /* ARGSUSED */
5869 static void
5870 dwarf2_free_tmp_obstack (ignore)
5871 PTR ignore;
5872 {
5873 obstack_free (&dwarf2_tmp_obstack, NULL);
5874 }
5875
5876 static struct dwarf_block *
5877 dwarf_alloc_block ()
5878 {
5879 struct dwarf_block *blk;
5880
5881 blk = (struct dwarf_block *)
5882 obstack_alloc (&dwarf2_tmp_obstack, sizeof (struct dwarf_block));
5883 return (blk);
5884 }
5885
5886 static struct abbrev_info *
5887 dwarf_alloc_abbrev ()
5888 {
5889 struct abbrev_info *abbrev;
5890
5891 abbrev = (struct abbrev_info *) xmalloc (sizeof (struct abbrev_info));
5892 memset (abbrev, 0, sizeof (struct abbrev_info));
5893 return (abbrev);
5894 }
5895
5896 static struct die_info *
5897 dwarf_alloc_die ()
5898 {
5899 struct die_info *die;
5900
5901 die = (struct die_info *) xmalloc (sizeof (struct die_info));
5902 memset (die, 0, sizeof (struct die_info));
5903 return (die);
5904 }