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