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