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