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