* gdb.base/gdb1476.exp: Fix typo.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004
4 Free Software Foundation, Inc.
5
6 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
7 Inc. with support from Florida State University (under contract
8 with the Ada Joint Program Office), and Silicon Graphics, Inc.
9 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
10 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
11 support in dwarfread.c
12
13 This file is part of GDB.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or (at
18 your option) any later version.
19
20 This program is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
29
30 #include "defs.h"
31 #include "bfd.h"
32 #include "symtab.h"
33 #include "gdbtypes.h"
34 #include "symfile.h"
35 #include "objfiles.h"
36 #include "elf/dwarf2.h"
37 #include "buildsym.h"
38 #include "demangle.h"
39 #include "expression.h"
40 #include "filenames.h" /* for DOSish file names */
41 #include "macrotab.h"
42 #include "language.h"
43 #include "complaints.h"
44 #include "bcache.h"
45 #include "dwarf2expr.h"
46 #include "dwarf2loc.h"
47 #include "cp-support.h"
48
49 #include <fcntl.h>
50 #include "gdb_string.h"
51 #include "gdb_assert.h"
52 #include <sys/types.h>
53
54 #ifndef DWARF2_REG_TO_REGNUM
55 #define DWARF2_REG_TO_REGNUM(REG) (REG)
56 #endif
57
58 #if 0
59 /* .debug_info header for a compilation unit
60 Because of alignment constraints, this structure has padding and cannot
61 be mapped directly onto the beginning of the .debug_info section. */
62 typedef struct comp_unit_header
63 {
64 unsigned int length; /* length of the .debug_info
65 contribution */
66 unsigned short version; /* version number -- 2 for DWARF
67 version 2 */
68 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
69 unsigned char addr_size; /* byte size of an address -- 4 */
70 }
71 _COMP_UNIT_HEADER;
72 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
73 #endif
74
75 /* .debug_pubnames header
76 Because of alignment constraints, this structure has padding and cannot
77 be mapped directly onto the beginning of the .debug_info section. */
78 typedef struct pubnames_header
79 {
80 unsigned int length; /* length of the .debug_pubnames
81 contribution */
82 unsigned char version; /* version number -- 2 for DWARF
83 version 2 */
84 unsigned int info_offset; /* offset into .debug_info section */
85 unsigned int info_size; /* byte size of .debug_info section
86 portion */
87 }
88 _PUBNAMES_HEADER;
89 #define _ACTUAL_PUBNAMES_HEADER_SIZE 13
90
91 /* .debug_pubnames header
92 Because of alignment constraints, this structure has padding and cannot
93 be mapped directly onto the beginning of the .debug_info section. */
94 typedef struct aranges_header
95 {
96 unsigned int length; /* byte len of the .debug_aranges
97 contribution */
98 unsigned short version; /* version number -- 2 for DWARF
99 version 2 */
100 unsigned int info_offset; /* offset into .debug_info section */
101 unsigned char addr_size; /* byte size of an address */
102 unsigned char seg_size; /* byte size of segment descriptor */
103 }
104 _ARANGES_HEADER;
105 #define _ACTUAL_ARANGES_HEADER_SIZE 12
106
107 /* .debug_line statement program prologue
108 Because of alignment constraints, this structure has padding and cannot
109 be mapped directly onto the beginning of the .debug_info section. */
110 typedef struct statement_prologue
111 {
112 unsigned int total_length; /* byte length of the statement
113 information */
114 unsigned short version; /* version number -- 2 for DWARF
115 version 2 */
116 unsigned int prologue_length; /* # bytes between prologue &
117 stmt program */
118 unsigned char minimum_instruction_length; /* byte size of
119 smallest instr */
120 unsigned char default_is_stmt; /* initial value of is_stmt
121 register */
122 char line_base;
123 unsigned char line_range;
124 unsigned char opcode_base; /* number assigned to first special
125 opcode */
126 unsigned char *standard_opcode_lengths;
127 }
128 _STATEMENT_PROLOGUE;
129
130 /* offsets and sizes of debugging sections */
131
132 static unsigned int dwarf_info_size;
133 static unsigned int dwarf_abbrev_size;
134 static unsigned int dwarf_line_size;
135 static unsigned int dwarf_pubnames_size;
136 static unsigned int dwarf_aranges_size;
137 static unsigned int dwarf_loc_size;
138 static unsigned int dwarf_macinfo_size;
139 static unsigned int dwarf_str_size;
140 static unsigned int dwarf_ranges_size;
141 unsigned int dwarf_frame_size;
142 unsigned int dwarf_eh_frame_size;
143
144 static asection *dwarf_info_section;
145 static asection *dwarf_abbrev_section;
146 static asection *dwarf_line_section;
147 static asection *dwarf_pubnames_section;
148 static asection *dwarf_aranges_section;
149 static asection *dwarf_loc_section;
150 static asection *dwarf_macinfo_section;
151 static asection *dwarf_str_section;
152 static asection *dwarf_ranges_section;
153 asection *dwarf_frame_section;
154 asection *dwarf_eh_frame_section;
155
156 /* names of the debugging sections */
157
158 #define INFO_SECTION ".debug_info"
159 #define ABBREV_SECTION ".debug_abbrev"
160 #define LINE_SECTION ".debug_line"
161 #define PUBNAMES_SECTION ".debug_pubnames"
162 #define ARANGES_SECTION ".debug_aranges"
163 #define LOC_SECTION ".debug_loc"
164 #define MACINFO_SECTION ".debug_macinfo"
165 #define STR_SECTION ".debug_str"
166 #define RANGES_SECTION ".debug_ranges"
167 #define FRAME_SECTION ".debug_frame"
168 #define EH_FRAME_SECTION ".eh_frame"
169
170 /* local data types */
171
172 /* We hold several abbreviation tables in memory at the same time. */
173 #ifndef ABBREV_HASH_SIZE
174 #define ABBREV_HASH_SIZE 121
175 #endif
176
177 /* The data in a compilation unit header, after target2host
178 translation, looks like this. */
179 struct comp_unit_head
180 {
181 unsigned long length;
182 short version;
183 unsigned int abbrev_offset;
184 unsigned char addr_size;
185 unsigned char signed_addr_p;
186 unsigned int offset_size; /* size of file offsets; either 4 or 8 */
187 unsigned int initial_length_size; /* size of the length field; either
188 4 or 12 */
189
190 /* Offset to the first byte of this compilation unit header in the
191 * .debug_info section, for resolving relative reference dies. */
192
193 unsigned int offset;
194
195 /* Pointer to this compilation unit header in the .debug_info
196 * section */
197
198 char *cu_head_ptr;
199
200 /* Pointer to the first die of this compilatio unit. This will
201 * be the first byte following the compilation unit header. */
202
203 char *first_die_ptr;
204
205 /* Pointer to the next compilation unit header in the program. */
206
207 struct comp_unit_head *next;
208
209 /* DWARF abbreviation table associated with this compilation unit */
210
211 struct abbrev_info *dwarf2_abbrevs[ABBREV_HASH_SIZE];
212
213 /* Base address of this compilation unit. */
214
215 CORE_ADDR base_address;
216
217 /* Non-zero if base_address has been set. */
218
219 int base_known;
220 };
221
222 /* Internal state when decoding a particular compilation unit. */
223 struct dwarf2_cu
224 {
225 /* The objfile containing this compilation unit. */
226 struct objfile *objfile;
227
228 /* The header of the compilation unit.
229
230 FIXME drow/2003-11-10: Some of the things from the comp_unit_head
231 should be moved to the dwarf2_cu structure; for instance the abbrevs
232 hash table. */
233 struct comp_unit_head header;
234 };
235
236 /* The line number information for a compilation unit (found in the
237 .debug_line section) begins with a "statement program header",
238 which contains the following information. */
239 struct line_header
240 {
241 unsigned int total_length;
242 unsigned short version;
243 unsigned int header_length;
244 unsigned char minimum_instruction_length;
245 unsigned char default_is_stmt;
246 int line_base;
247 unsigned char line_range;
248 unsigned char opcode_base;
249
250 /* standard_opcode_lengths[i] is the number of operands for the
251 standard opcode whose value is i. This means that
252 standard_opcode_lengths[0] is unused, and the last meaningful
253 element is standard_opcode_lengths[opcode_base - 1]. */
254 unsigned char *standard_opcode_lengths;
255
256 /* The include_directories table. NOTE! These strings are not
257 allocated with xmalloc; instead, they are pointers into
258 debug_line_buffer. If you try to free them, `free' will get
259 indigestion. */
260 unsigned int num_include_dirs, include_dirs_size;
261 char **include_dirs;
262
263 /* The file_names table. NOTE! These strings are not allocated
264 with xmalloc; instead, they are pointers into debug_line_buffer.
265 Don't try to free them directly. */
266 unsigned int num_file_names, file_names_size;
267 struct file_entry
268 {
269 char *name;
270 unsigned int dir_index;
271 unsigned int mod_time;
272 unsigned int length;
273 } *file_names;
274
275 /* The start and end of the statement program following this
276 header. These point into dwarf_line_buffer. */
277 char *statement_program_start, *statement_program_end;
278 };
279
280 /* When we construct a partial symbol table entry we only
281 need this much information. */
282 struct partial_die_info
283 {
284 enum dwarf_tag tag;
285 unsigned char has_children;
286 unsigned char is_external;
287 unsigned char is_declaration;
288 unsigned char has_type;
289 unsigned int offset;
290 unsigned int abbrev;
291 char *name;
292 int has_pc_info;
293 CORE_ADDR lowpc;
294 CORE_ADDR highpc;
295 struct dwarf_block *locdesc;
296 unsigned int language;
297 char *sibling;
298 };
299
300 /* This data structure holds the information of an abbrev. */
301 struct abbrev_info
302 {
303 unsigned int number; /* number identifying abbrev */
304 enum dwarf_tag tag; /* dwarf tag */
305 int has_children; /* boolean */
306 unsigned int num_attrs; /* number of attributes */
307 struct attr_abbrev *attrs; /* an array of attribute descriptions */
308 struct abbrev_info *next; /* next in chain */
309 };
310
311 struct attr_abbrev
312 {
313 enum dwarf_attribute name;
314 enum dwarf_form form;
315 };
316
317 /* This data structure holds a complete die structure. */
318 struct die_info
319 {
320 enum dwarf_tag tag; /* Tag indicating type of die */
321 unsigned int abbrev; /* Abbrev number */
322 unsigned int offset; /* Offset in .debug_info section */
323 unsigned int num_attrs; /* Number of attributes */
324 struct attribute *attrs; /* An array of attributes */
325 struct die_info *next_ref; /* Next die in ref hash table */
326
327 /* The dies in a compilation unit form an n-ary tree. PARENT
328 points to this die's parent; CHILD points to the first child of
329 this node; and all the children of a given node are chained
330 together via their SIBLING fields, terminated by a die whose
331 tag is zero. */
332 struct die_info *child; /* Its first child, if any. */
333 struct die_info *sibling; /* Its next sibling, if any. */
334 struct die_info *parent; /* Its parent, if any. */
335
336 struct type *type; /* Cached type information */
337 };
338
339 /* Attributes have a name and a value */
340 struct attribute
341 {
342 enum dwarf_attribute name;
343 enum dwarf_form form;
344 union
345 {
346 char *str;
347 struct dwarf_block *blk;
348 unsigned long unsnd;
349 long int snd;
350 CORE_ADDR addr;
351 }
352 u;
353 };
354
355 struct function_range
356 {
357 const char *name;
358 CORE_ADDR lowpc, highpc;
359 int seen_line;
360 struct function_range *next;
361 };
362
363 static struct function_range *cu_first_fn, *cu_last_fn, *cu_cached_fn;
364
365 /* Get at parts of an attribute structure */
366
367 #define DW_STRING(attr) ((attr)->u.str)
368 #define DW_UNSND(attr) ((attr)->u.unsnd)
369 #define DW_BLOCK(attr) ((attr)->u.blk)
370 #define DW_SND(attr) ((attr)->u.snd)
371 #define DW_ADDR(attr) ((attr)->u.addr)
372
373 /* Blocks are a bunch of untyped bytes. */
374 struct dwarf_block
375 {
376 unsigned int size;
377 char *data;
378 };
379
380 #ifndef ATTR_ALLOC_CHUNK
381 #define ATTR_ALLOC_CHUNK 4
382 #endif
383
384 /* A hash table of die offsets for following references. */
385 #ifndef REF_HASH_SIZE
386 #define REF_HASH_SIZE 1021
387 #endif
388
389 static struct die_info *die_ref_table[REF_HASH_SIZE];
390
391 /* Obstack for allocating temporary storage used during symbol reading. */
392 static struct obstack dwarf2_tmp_obstack;
393
394 /* Offset to the first byte of the current compilation unit header,
395 for resolving relative reference dies. */
396 static unsigned int cu_header_offset;
397
398 /* Allocate fields for structs, unions and enums in this size. */
399 #ifndef DW_FIELD_ALLOC_CHUNK
400 #define DW_FIELD_ALLOC_CHUNK 4
401 #endif
402
403 /* The language we are debugging. */
404 static enum language cu_language;
405 static const struct language_defn *cu_language_defn;
406
407 /* Actually data from the sections. */
408 static char *dwarf_info_buffer;
409 static char *dwarf_abbrev_buffer;
410 static char *dwarf_line_buffer;
411 static char *dwarf_str_buffer;
412 static char *dwarf_macinfo_buffer;
413 static char *dwarf_ranges_buffer;
414 static char *dwarf_loc_buffer;
415
416 /* A zeroed version of a partial die for initialization purposes. */
417 static struct partial_die_info zeroed_partial_die;
418
419 /* The generic symbol table building routines have separate lists for
420 file scope symbols and all all other scopes (local scopes). So
421 we need to select the right one to pass to add_symbol_to_list().
422 We do it by keeping a pointer to the correct list in list_in_scope.
423
424 FIXME: The original dwarf code just treated the file scope as the first
425 local scope, and all other local scopes as nested local scopes, and worked
426 fine. Check to see if we really need to distinguish these
427 in buildsym.c. */
428 static struct pending **list_in_scope = &file_symbols;
429
430 /* FIXME: decode_locdesc sets these variables to describe the location
431 to the caller. These ought to be a structure or something. If
432 none of the flags are set, the object lives at the address returned
433 by decode_locdesc. */
434
435 static int isreg; /* Object lives in register.
436 decode_locdesc's return value is
437 the register number. */
438
439 /* This value is added to each symbol value. FIXME: Generalize to
440 the section_offsets structure used by dbxread (once this is done,
441 pass the appropriate section number to end_symtab). */
442 static CORE_ADDR baseaddr; /* Add to each symbol value */
443
444 /* We put a pointer to this structure in the read_symtab_private field
445 of the psymtab.
446 The complete dwarf information for an objfile is kept in the
447 psymbol_obstack, so that absolute die references can be handled.
448 Most of the information in this structure is related to an entire
449 object file and could be passed via the sym_private field of the objfile.
450 It is however conceivable that dwarf2 might not be the only type
451 of symbols read from an object file. */
452
453 struct dwarf2_pinfo
454 {
455 /* Pointer to start of dwarf info buffer for the objfile. */
456
457 char *dwarf_info_buffer;
458
459 /* Offset in dwarf_info_buffer for this compilation unit. */
460
461 unsigned long dwarf_info_offset;
462
463 /* Pointer to start of dwarf abbreviation buffer for the objfile. */
464
465 char *dwarf_abbrev_buffer;
466
467 /* Size of dwarf abbreviation section for the objfile. */
468
469 unsigned int dwarf_abbrev_size;
470
471 /* Pointer to start of dwarf line buffer for the objfile. */
472
473 char *dwarf_line_buffer;
474
475 /* Size of dwarf_line_buffer, in bytes. */
476
477 unsigned int dwarf_line_size;
478
479 /* Pointer to start of dwarf string buffer for the objfile. */
480
481 char *dwarf_str_buffer;
482
483 /* Size of dwarf string section for the objfile. */
484
485 unsigned int dwarf_str_size;
486
487 /* Pointer to start of dwarf macro buffer for the objfile. */
488
489 char *dwarf_macinfo_buffer;
490
491 /* Size of dwarf macinfo section for the objfile. */
492
493 unsigned int dwarf_macinfo_size;
494
495 /* Pointer to start of dwarf ranges buffer for the objfile. */
496
497 char *dwarf_ranges_buffer;
498
499 /* Size of dwarf ranges buffer for the objfile. */
500
501 unsigned int dwarf_ranges_size;
502
503 /* Pointer to start of dwarf locations buffer for the objfile. */
504
505 char *dwarf_loc_buffer;
506
507 /* Size of dwarf locations buffer for the objfile. */
508
509 unsigned int dwarf_loc_size;
510 };
511
512 #define PST_PRIVATE(p) ((struct dwarf2_pinfo *)(p)->read_symtab_private)
513 #define DWARF_INFO_BUFFER(p) (PST_PRIVATE(p)->dwarf_info_buffer)
514 #define DWARF_INFO_OFFSET(p) (PST_PRIVATE(p)->dwarf_info_offset)
515 #define DWARF_ABBREV_BUFFER(p) (PST_PRIVATE(p)->dwarf_abbrev_buffer)
516 #define DWARF_ABBREV_SIZE(p) (PST_PRIVATE(p)->dwarf_abbrev_size)
517 #define DWARF_LINE_BUFFER(p) (PST_PRIVATE(p)->dwarf_line_buffer)
518 #define DWARF_LINE_SIZE(p) (PST_PRIVATE(p)->dwarf_line_size)
519 #define DWARF_STR_BUFFER(p) (PST_PRIVATE(p)->dwarf_str_buffer)
520 #define DWARF_STR_SIZE(p) (PST_PRIVATE(p)->dwarf_str_size)
521 #define DWARF_MACINFO_BUFFER(p) (PST_PRIVATE(p)->dwarf_macinfo_buffer)
522 #define DWARF_MACINFO_SIZE(p) (PST_PRIVATE(p)->dwarf_macinfo_size)
523 #define DWARF_RANGES_BUFFER(p) (PST_PRIVATE(p)->dwarf_ranges_buffer)
524 #define DWARF_RANGES_SIZE(p) (PST_PRIVATE(p)->dwarf_ranges_size)
525 #define DWARF_LOC_BUFFER(p) (PST_PRIVATE(p)->dwarf_loc_buffer)
526 #define DWARF_LOC_SIZE(p) (PST_PRIVATE(p)->dwarf_loc_size)
527
528 /* Maintain an array of referenced fundamental types for the current
529 compilation unit being read. For DWARF version 1, we have to construct
530 the fundamental types on the fly, since no information about the
531 fundamental types is supplied. Each such fundamental type is created by
532 calling a language dependent routine to create the type, and then a
533 pointer to that type is then placed in the array at the index specified
534 by it's FT_<TYPENAME> value. The array has a fixed size set by the
535 FT_NUM_MEMBERS compile time constant, which is the number of predefined
536 fundamental types gdb knows how to construct. */
537 static struct type *ftypes[FT_NUM_MEMBERS]; /* Fundamental types */
538
539 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
540 but this would require a corresponding change in unpack_field_as_long
541 and friends. */
542 static int bits_per_byte = 8;
543
544 /* The routines that read and process dies for a C struct or C++ class
545 pass lists of data member fields and lists of member function fields
546 in an instance of a field_info structure, as defined below. */
547 struct field_info
548 {
549 /* List of data member and baseclasses fields. */
550 struct nextfield
551 {
552 struct nextfield *next;
553 int accessibility;
554 int virtuality;
555 struct field field;
556 }
557 *fields;
558
559 /* Number of fields. */
560 int nfields;
561
562 /* Number of baseclasses. */
563 int nbaseclasses;
564
565 /* Set if the accesibility of one of the fields is not public. */
566 int non_public_fields;
567
568 /* Member function fields array, entries are allocated in the order they
569 are encountered in the object file. */
570 struct nextfnfield
571 {
572 struct nextfnfield *next;
573 struct fn_field fnfield;
574 }
575 *fnfields;
576
577 /* Member function fieldlist array, contains name of possibly overloaded
578 member function, number of overloaded member functions and a pointer
579 to the head of the member function field chain. */
580 struct fnfieldlist
581 {
582 char *name;
583 int length;
584 struct nextfnfield *head;
585 }
586 *fnfieldlists;
587
588 /* Number of entries in the fnfieldlists array. */
589 int nfnfields;
590 };
591
592 /* Various complaints about symbol reading that don't abort the process */
593
594 static void
595 dwarf2_non_const_array_bound_ignored_complaint (const char *arg1)
596 {
597 complaint (&symfile_complaints, "non-constant array bounds form '%s' ignored",
598 arg1);
599 }
600
601 static void
602 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
603 {
604 complaint (&symfile_complaints,
605 "statement list doesn't fit in .debug_line section");
606 }
607
608 static void
609 dwarf2_complex_location_expr_complaint (void)
610 {
611 complaint (&symfile_complaints, "location expression too complex");
612 }
613
614 static void
615 dwarf2_unsupported_at_frame_base_complaint (const char *arg1)
616 {
617 complaint (&symfile_complaints,
618 "unsupported DW_AT_frame_base for function '%s'", arg1);
619 }
620
621 static void
622 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
623 int arg3)
624 {
625 complaint (&symfile_complaints,
626 "const value length mismatch for '%s', got %d, expected %d", arg1,
627 arg2, arg3);
628 }
629
630 static void
631 dwarf2_macros_too_long_complaint (void)
632 {
633 complaint (&symfile_complaints,
634 "macro info runs off end of `.debug_macinfo' section");
635 }
636
637 static void
638 dwarf2_macro_malformed_definition_complaint (const char *arg1)
639 {
640 complaint (&symfile_complaints,
641 "macro debug info contains a malformed macro definition:\n`%s'",
642 arg1);
643 }
644
645 static void
646 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
647 {
648 complaint (&symfile_complaints,
649 "invalid attribute class or form for '%s' in '%s'", arg1, arg2);
650 }
651
652 /* local function prototypes */
653
654 static void dwarf2_locate_sections (bfd *, asection *, void *);
655
656 #if 0
657 static void dwarf2_build_psymtabs_easy (struct objfile *, int);
658 #endif
659
660 static void dwarf2_build_psymtabs_hard (struct objfile *, int);
661
662 static char *scan_partial_symbols (char *, CORE_ADDR *, CORE_ADDR *,
663 struct dwarf2_cu *,
664 const char *namespace);
665
666 static void add_partial_symbol (struct partial_die_info *, struct dwarf2_cu *,
667 const char *namespace);
668
669 static char *add_partial_namespace (struct partial_die_info *pdi,
670 char *info_ptr,
671 CORE_ADDR *lowpc, CORE_ADDR *highpc,
672 struct dwarf2_cu *cu,
673 const char *namespace);
674
675 static char *add_partial_enumeration (struct partial_die_info *enum_pdi,
676 char *info_ptr,
677 struct dwarf2_cu *cu,
678 const char *namespace);
679
680 static char *locate_pdi_sibling (struct partial_die_info *orig_pdi,
681 char *info_ptr,
682 bfd *abfd,
683 struct dwarf2_cu *cu);
684
685 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
686
687 static void psymtab_to_symtab_1 (struct partial_symtab *);
688
689 char *dwarf2_read_section (struct objfile *, asection *);
690
691 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
692
693 static void dwarf2_empty_abbrev_table (void *);
694
695 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
696 struct dwarf2_cu *);
697
698 static char *read_partial_die (struct partial_die_info *,
699 bfd *, char *, struct dwarf2_cu *);
700
701 static char *read_full_die (struct die_info **, bfd *, char *,
702 struct dwarf2_cu *, int *);
703
704 static char *read_attribute (struct attribute *, struct attr_abbrev *,
705 bfd *, char *, struct dwarf2_cu *);
706
707 static char *read_attribute_value (struct attribute *, unsigned,
708 bfd *, char *, struct dwarf2_cu *);
709
710 static unsigned int read_1_byte (bfd *, char *);
711
712 static int read_1_signed_byte (bfd *, char *);
713
714 static unsigned int read_2_bytes (bfd *, char *);
715
716 static unsigned int read_4_bytes (bfd *, char *);
717
718 static unsigned long read_8_bytes (bfd *, char *);
719
720 static CORE_ADDR read_address (bfd *, char *ptr, struct dwarf2_cu *,
721 int *bytes_read);
722
723 static LONGEST read_initial_length (bfd *, char *,
724 struct comp_unit_head *, int *bytes_read);
725
726 static LONGEST read_offset (bfd *, char *, const struct comp_unit_head *,
727 int *bytes_read);
728
729 static char *read_n_bytes (bfd *, char *, unsigned int);
730
731 static char *read_string (bfd *, char *, unsigned int *);
732
733 static char *read_indirect_string (bfd *, char *, const struct comp_unit_head *,
734 unsigned int *);
735
736 static unsigned long read_unsigned_leb128 (bfd *, char *, unsigned int *);
737
738 static long read_signed_leb128 (bfd *, char *, unsigned int *);
739
740 static void set_cu_language (unsigned int);
741
742 static struct attribute *dwarf_attr (struct die_info *, unsigned int);
743
744 static int die_is_declaration (struct die_info *);
745
746 static void free_line_header (struct line_header *lh);
747
748 static struct line_header *(dwarf_decode_line_header
749 (unsigned int offset,
750 bfd *abfd, struct dwarf2_cu *cu));
751
752 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
753 struct dwarf2_cu *);
754
755 static void dwarf2_start_subfile (char *, char *);
756
757 static struct symbol *new_symbol (struct die_info *, struct type *,
758 struct dwarf2_cu *);
759
760 static void dwarf2_const_value (struct attribute *, struct symbol *,
761 struct dwarf2_cu *);
762
763 static void dwarf2_const_value_data (struct attribute *attr,
764 struct symbol *sym,
765 int bits);
766
767 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
768
769 static struct type *die_containing_type (struct die_info *,
770 struct dwarf2_cu *);
771
772 #if 0
773 static struct type *type_at_offset (unsigned int, struct objfile *);
774 #endif
775
776 static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
777
778 static void read_type_die (struct die_info *, struct dwarf2_cu *);
779
780 static void read_typedef (struct die_info *, struct dwarf2_cu *);
781
782 static void read_base_type (struct die_info *, struct dwarf2_cu *);
783
784 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
785
786 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
787
788 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
789
790 static int dwarf2_get_pc_bounds (struct die_info *,
791 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *);
792
793 static void dwarf2_add_field (struct field_info *, struct die_info *,
794 struct dwarf2_cu *);
795
796 static void dwarf2_attach_fields_to_type (struct field_info *,
797 struct type *, struct dwarf2_cu *);
798
799 static void dwarf2_add_member_fn (struct field_info *,
800 struct die_info *, struct type *,
801 struct dwarf2_cu *);
802
803 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
804 struct type *, struct dwarf2_cu *);
805
806 static void read_structure_scope (struct die_info *, struct dwarf2_cu *);
807
808 static void read_common_block (struct die_info *, struct dwarf2_cu *);
809
810 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
811
812 static void read_enumeration (struct die_info *, struct dwarf2_cu *);
813
814 static struct type *dwarf_base_type (int, int, struct dwarf2_cu *);
815
816 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
817
818 static void read_array_type (struct die_info *, struct dwarf2_cu *);
819
820 static void read_tag_pointer_type (struct die_info *, struct dwarf2_cu *);
821
822 static void read_tag_ptr_to_member_type (struct die_info *,
823 struct dwarf2_cu *);
824
825 static void read_tag_reference_type (struct die_info *, struct dwarf2_cu *);
826
827 static void read_tag_const_type (struct die_info *, struct dwarf2_cu *);
828
829 static void read_tag_volatile_type (struct die_info *, struct dwarf2_cu *);
830
831 static void read_tag_string_type (struct die_info *, struct dwarf2_cu *);
832
833 static void read_subroutine_type (struct die_info *, struct dwarf2_cu *);
834
835 static struct die_info *read_comp_unit (char *, bfd *, struct dwarf2_cu *);
836
837 static struct die_info *read_die_and_children (char *info_ptr, bfd *abfd,
838 struct dwarf2_cu *,
839 char **new_info_ptr,
840 struct die_info *parent);
841
842 static struct die_info *read_die_and_siblings (char *info_ptr, bfd *abfd,
843 struct dwarf2_cu *,
844 char **new_info_ptr,
845 struct die_info *parent);
846
847 static void free_die_list (struct die_info *);
848
849 static struct cleanup *make_cleanup_free_die_list (struct die_info *);
850
851 static void process_die (struct die_info *, struct dwarf2_cu *);
852
853 static char *dwarf2_linkage_name (struct die_info *);
854
855 static char *dwarf2_name (struct die_info *die);
856
857 static struct die_info *dwarf2_extension (struct die_info *die);
858
859 static char *dwarf_tag_name (unsigned int);
860
861 static char *dwarf_attr_name (unsigned int);
862
863 static char *dwarf_form_name (unsigned int);
864
865 static char *dwarf_stack_op_name (unsigned int);
866
867 static char *dwarf_bool_name (unsigned int);
868
869 static char *dwarf_type_encoding_name (unsigned int);
870
871 #if 0
872 static char *dwarf_cfi_name (unsigned int);
873
874 struct die_info *copy_die (struct die_info *);
875 #endif
876
877 static struct die_info *sibling_die (struct die_info *);
878
879 static void dump_die (struct die_info *);
880
881 static void dump_die_list (struct die_info *);
882
883 static void store_in_ref_table (unsigned int, struct die_info *);
884
885 static void dwarf2_empty_hash_tables (void);
886
887 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
888
889 static struct die_info *follow_die_ref (unsigned int);
890
891 static struct type *dwarf2_fundamental_type (struct objfile *, int);
892
893 /* memory allocation interface */
894
895 static void dwarf2_free_tmp_obstack (void *);
896
897 static struct dwarf_block *dwarf_alloc_block (void);
898
899 static struct abbrev_info *dwarf_alloc_abbrev (void);
900
901 static struct die_info *dwarf_alloc_die (void);
902
903 static void initialize_cu_func_list (void);
904
905 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR);
906
907 static void dwarf_decode_macros (struct line_header *, unsigned int,
908 char *, bfd *, struct dwarf2_cu *);
909
910 static int attr_form_is_block (struct attribute *);
911
912 static void
913 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
914 struct dwarf2_cu *cu);
915
916 /* Try to locate the sections we need for DWARF 2 debugging
917 information and return true if we have enough to do something. */
918
919 int
920 dwarf2_has_info (bfd *abfd)
921 {
922 dwarf_info_section = 0;
923 dwarf_abbrev_section = 0;
924 dwarf_line_section = 0;
925 dwarf_str_section = 0;
926 dwarf_macinfo_section = 0;
927 dwarf_frame_section = 0;
928 dwarf_eh_frame_section = 0;
929 dwarf_ranges_section = 0;
930 dwarf_loc_section = 0;
931
932 bfd_map_over_sections (abfd, dwarf2_locate_sections, NULL);
933 return (dwarf_info_section != NULL && dwarf_abbrev_section != NULL);
934 }
935
936 /* This function is mapped across the sections and remembers the
937 offset and size of each of the debugging sections we are interested
938 in. */
939
940 static void
941 dwarf2_locate_sections (bfd *ignore_abfd, asection *sectp, void *ignore_ptr)
942 {
943 if (strcmp (sectp->name, INFO_SECTION) == 0)
944 {
945 dwarf_info_size = bfd_get_section_size_before_reloc (sectp);
946 dwarf_info_section = sectp;
947 }
948 else if (strcmp (sectp->name, ABBREV_SECTION) == 0)
949 {
950 dwarf_abbrev_size = bfd_get_section_size_before_reloc (sectp);
951 dwarf_abbrev_section = sectp;
952 }
953 else if (strcmp (sectp->name, LINE_SECTION) == 0)
954 {
955 dwarf_line_size = bfd_get_section_size_before_reloc (sectp);
956 dwarf_line_section = sectp;
957 }
958 else if (strcmp (sectp->name, PUBNAMES_SECTION) == 0)
959 {
960 dwarf_pubnames_size = bfd_get_section_size_before_reloc (sectp);
961 dwarf_pubnames_section = sectp;
962 }
963 else if (strcmp (sectp->name, ARANGES_SECTION) == 0)
964 {
965 dwarf_aranges_size = bfd_get_section_size_before_reloc (sectp);
966 dwarf_aranges_section = sectp;
967 }
968 else if (strcmp (sectp->name, LOC_SECTION) == 0)
969 {
970 dwarf_loc_size = bfd_get_section_size_before_reloc (sectp);
971 dwarf_loc_section = sectp;
972 }
973 else if (strcmp (sectp->name, MACINFO_SECTION) == 0)
974 {
975 dwarf_macinfo_size = bfd_get_section_size_before_reloc (sectp);
976 dwarf_macinfo_section = sectp;
977 }
978 else if (strcmp (sectp->name, STR_SECTION) == 0)
979 {
980 dwarf_str_size = bfd_get_section_size_before_reloc (sectp);
981 dwarf_str_section = sectp;
982 }
983 else if (strcmp (sectp->name, FRAME_SECTION) == 0)
984 {
985 dwarf_frame_size = bfd_get_section_size_before_reloc (sectp);
986 dwarf_frame_section = sectp;
987 }
988 else if (strcmp (sectp->name, EH_FRAME_SECTION) == 0)
989 {
990 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
991 if (aflag & SEC_HAS_CONTENTS)
992 {
993 dwarf_eh_frame_size = bfd_get_section_size_before_reloc (sectp);
994 dwarf_eh_frame_section = sectp;
995 }
996 }
997 else if (strcmp (sectp->name, RANGES_SECTION) == 0)
998 {
999 dwarf_ranges_size = bfd_get_section_size_before_reloc (sectp);
1000 dwarf_ranges_section = sectp;
1001 }
1002 }
1003
1004 /* Build a partial symbol table. */
1005
1006 void
1007 dwarf2_build_psymtabs (struct objfile *objfile, int mainline)
1008 {
1009
1010 /* We definitely need the .debug_info and .debug_abbrev sections */
1011
1012 dwarf_info_buffer = dwarf2_read_section (objfile, dwarf_info_section);
1013 dwarf_abbrev_buffer = dwarf2_read_section (objfile, dwarf_abbrev_section);
1014
1015 if (dwarf_line_section)
1016 dwarf_line_buffer = dwarf2_read_section (objfile, dwarf_line_section);
1017 else
1018 dwarf_line_buffer = NULL;
1019
1020 if (dwarf_str_section)
1021 dwarf_str_buffer = dwarf2_read_section (objfile, dwarf_str_section);
1022 else
1023 dwarf_str_buffer = NULL;
1024
1025 if (dwarf_macinfo_section)
1026 dwarf_macinfo_buffer = dwarf2_read_section (objfile,
1027 dwarf_macinfo_section);
1028 else
1029 dwarf_macinfo_buffer = NULL;
1030
1031 if (dwarf_ranges_section)
1032 dwarf_ranges_buffer = dwarf2_read_section (objfile, dwarf_ranges_section);
1033 else
1034 dwarf_ranges_buffer = NULL;
1035
1036 if (dwarf_loc_section)
1037 dwarf_loc_buffer = dwarf2_read_section (objfile, dwarf_loc_section);
1038 else
1039 dwarf_loc_buffer = NULL;
1040
1041 if (mainline
1042 || (objfile->global_psymbols.size == 0
1043 && objfile->static_psymbols.size == 0))
1044 {
1045 init_psymbol_list (objfile, 1024);
1046 }
1047
1048 #if 0
1049 if (dwarf_aranges_offset && dwarf_pubnames_offset)
1050 {
1051 /* Things are significantly easier if we have .debug_aranges and
1052 .debug_pubnames sections */
1053
1054 dwarf2_build_psymtabs_easy (objfile, mainline);
1055 }
1056 else
1057 #endif
1058 /* only test this case for now */
1059 {
1060 /* In this case we have to work a bit harder */
1061 dwarf2_build_psymtabs_hard (objfile, mainline);
1062 }
1063 }
1064
1065 #if 0
1066 /* Build the partial symbol table from the information in the
1067 .debug_pubnames and .debug_aranges sections. */
1068
1069 static void
1070 dwarf2_build_psymtabs_easy (struct objfile *objfile, int mainline)
1071 {
1072 bfd *abfd = objfile->obfd;
1073 char *aranges_buffer, *pubnames_buffer;
1074 char *aranges_ptr, *pubnames_ptr;
1075 unsigned int entry_length, version, info_offset, info_size;
1076
1077 pubnames_buffer = dwarf2_read_section (objfile,
1078 dwarf_pubnames_section);
1079 pubnames_ptr = pubnames_buffer;
1080 while ((pubnames_ptr - pubnames_buffer) < dwarf_pubnames_size)
1081 {
1082 struct comp_unit_head cu_header;
1083 int bytes_read;
1084
1085 entry_length = read_initial_length (abfd, pubnames_ptr, &cu_header,
1086 &bytes_read);
1087 pubnames_ptr += bytes_read;
1088 version = read_1_byte (abfd, pubnames_ptr);
1089 pubnames_ptr += 1;
1090 info_offset = read_4_bytes (abfd, pubnames_ptr);
1091 pubnames_ptr += 4;
1092 info_size = read_4_bytes (abfd, pubnames_ptr);
1093 pubnames_ptr += 4;
1094 }
1095
1096 aranges_buffer = dwarf2_read_section (objfile,
1097 dwarf_aranges_section);
1098
1099 }
1100 #endif
1101
1102 /* Read in the comp unit header information from the debug_info at
1103 info_ptr. */
1104
1105 static char *
1106 read_comp_unit_head (struct comp_unit_head *cu_header,
1107 char *info_ptr, bfd *abfd)
1108 {
1109 int signed_addr;
1110 int bytes_read;
1111 cu_header->length = read_initial_length (abfd, info_ptr, cu_header,
1112 &bytes_read);
1113 info_ptr += bytes_read;
1114 cu_header->version = read_2_bytes (abfd, info_ptr);
1115 info_ptr += 2;
1116 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
1117 &bytes_read);
1118 info_ptr += bytes_read;
1119 cu_header->addr_size = read_1_byte (abfd, info_ptr);
1120 info_ptr += 1;
1121 signed_addr = bfd_get_sign_extend_vma (abfd);
1122 if (signed_addr < 0)
1123 internal_error (__FILE__, __LINE__,
1124 "read_comp_unit_head: dwarf from non elf file");
1125 cu_header->signed_addr_p = signed_addr;
1126 return info_ptr;
1127 }
1128
1129 /* Build the partial symbol table by doing a quick pass through the
1130 .debug_info and .debug_abbrev sections. */
1131
1132 static void
1133 dwarf2_build_psymtabs_hard (struct objfile *objfile, int mainline)
1134 {
1135 /* Instead of reading this into a big buffer, we should probably use
1136 mmap() on architectures that support it. (FIXME) */
1137 bfd *abfd = objfile->obfd;
1138 char *info_ptr, *abbrev_ptr;
1139 char *beg_of_comp_unit;
1140 struct partial_die_info comp_unit_die;
1141 struct partial_symtab *pst;
1142 struct cleanup *back_to;
1143 CORE_ADDR lowpc, highpc;
1144
1145 info_ptr = dwarf_info_buffer;
1146 abbrev_ptr = dwarf_abbrev_buffer;
1147
1148 /* We use dwarf2_tmp_obstack for objects that don't need to survive
1149 the partial symbol scan, like attribute values.
1150
1151 We could reduce our peak memory consumption during partial symbol
1152 table construction by freeing stuff from this obstack more often
1153 --- say, after processing each compilation unit, or each die ---
1154 but it turns out that this saves almost nothing. For an
1155 executable with 11Mb of Dwarf 2 data, I found about 64k allocated
1156 on dwarf2_tmp_obstack. Some investigation showed:
1157
1158 1) 69% of the attributes used forms DW_FORM_addr, DW_FORM_data*,
1159 DW_FORM_flag, DW_FORM_[su]data, and DW_FORM_ref*. These are
1160 all fixed-length values not requiring dynamic allocation.
1161
1162 2) 30% of the attributes used the form DW_FORM_string. For
1163 DW_FORM_string, read_attribute simply hands back a pointer to
1164 the null-terminated string in dwarf_info_buffer, so no dynamic
1165 allocation is needed there either.
1166
1167 3) The remaining 1% of the attributes all used DW_FORM_block1.
1168 75% of those were DW_AT_frame_base location lists for
1169 functions; the rest were DW_AT_location attributes, probably
1170 for the global variables.
1171
1172 Anyway, what this all means is that the memory the dwarf2
1173 reader uses as temporary space reading partial symbols is about
1174 0.5% as much as we use for dwarf_*_buffer. That's noise. */
1175
1176 obstack_init (&dwarf2_tmp_obstack);
1177 back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
1178
1179 /* Since the objects we're extracting from dwarf_info_buffer vary in
1180 length, only the individual functions to extract them (like
1181 read_comp_unit_head and read_partial_die) can really know whether
1182 the buffer is large enough to hold another complete object.
1183
1184 At the moment, they don't actually check that. If
1185 dwarf_info_buffer holds just one extra byte after the last
1186 compilation unit's dies, then read_comp_unit_head will happily
1187 read off the end of the buffer. read_partial_die is similarly
1188 casual. Those functions should be fixed.
1189
1190 For this loop condition, simply checking whether there's any data
1191 left at all should be sufficient. */
1192 while (info_ptr < dwarf_info_buffer + dwarf_info_size)
1193 {
1194 struct dwarf2_cu cu;
1195 beg_of_comp_unit = info_ptr;
1196
1197 cu.objfile = objfile;
1198 info_ptr = read_comp_unit_head (&cu.header, info_ptr, abfd);
1199
1200 if (cu.header.version != 2)
1201 {
1202 error ("Dwarf Error: wrong version in compilation unit header (is %d, should be %d) [in module %s]", cu.header.version, 2, bfd_get_filename (abfd));
1203 return;
1204 }
1205 if (cu.header.abbrev_offset >= dwarf_abbrev_size)
1206 {
1207 error ("Dwarf Error: bad offset (0x%lx) in compilation unit header (offset 0x%lx + 6) [in module %s]",
1208 (long) cu.header.abbrev_offset,
1209 (long) (beg_of_comp_unit - dwarf_info_buffer),
1210 bfd_get_filename (abfd));
1211 return;
1212 }
1213 if (beg_of_comp_unit + cu.header.length + cu.header.initial_length_size
1214 > dwarf_info_buffer + dwarf_info_size)
1215 {
1216 error ("Dwarf Error: bad length (0x%lx) in compilation unit header (offset 0x%lx + 0) [in module %s]",
1217 (long) cu.header.length,
1218 (long) (beg_of_comp_unit - dwarf_info_buffer),
1219 bfd_get_filename (abfd));
1220 return;
1221 }
1222 /* Complete the cu_header */
1223 cu.header.offset = beg_of_comp_unit - dwarf_info_buffer;
1224 cu.header.first_die_ptr = info_ptr;
1225 cu.header.cu_head_ptr = beg_of_comp_unit;
1226
1227 /* Read the abbrevs for this compilation unit into a table */
1228 dwarf2_read_abbrevs (abfd, &cu);
1229 make_cleanup (dwarf2_empty_abbrev_table, cu.header.dwarf2_abbrevs);
1230
1231 /* Read the compilation unit die */
1232 info_ptr = read_partial_die (&comp_unit_die, abfd, info_ptr,
1233 &cu);
1234
1235 /* Set the language we're debugging */
1236 set_cu_language (comp_unit_die.language);
1237
1238 /* Allocate a new partial symbol table structure */
1239 pst = start_psymtab_common (objfile, objfile->section_offsets,
1240 comp_unit_die.name ? comp_unit_die.name : "",
1241 comp_unit_die.lowpc,
1242 objfile->global_psymbols.next,
1243 objfile->static_psymbols.next);
1244
1245 pst->read_symtab_private = (char *)
1246 obstack_alloc (&objfile->psymbol_obstack, sizeof (struct dwarf2_pinfo));
1247 cu_header_offset = beg_of_comp_unit - dwarf_info_buffer;
1248 DWARF_INFO_BUFFER (pst) = dwarf_info_buffer;
1249 DWARF_INFO_OFFSET (pst) = beg_of_comp_unit - dwarf_info_buffer;
1250 DWARF_ABBREV_BUFFER (pst) = dwarf_abbrev_buffer;
1251 DWARF_ABBREV_SIZE (pst) = dwarf_abbrev_size;
1252 DWARF_LINE_BUFFER (pst) = dwarf_line_buffer;
1253 DWARF_LINE_SIZE (pst) = dwarf_line_size;
1254 DWARF_STR_BUFFER (pst) = dwarf_str_buffer;
1255 DWARF_STR_SIZE (pst) = dwarf_str_size;
1256 DWARF_MACINFO_BUFFER (pst) = dwarf_macinfo_buffer;
1257 DWARF_MACINFO_SIZE (pst) = dwarf_macinfo_size;
1258 DWARF_RANGES_BUFFER (pst) = dwarf_ranges_buffer;
1259 DWARF_RANGES_SIZE (pst) = dwarf_ranges_size;
1260 DWARF_LOC_BUFFER (pst) = dwarf_loc_buffer;
1261 DWARF_LOC_SIZE (pst) = dwarf_loc_size;
1262 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1263
1264 /* Store the function that reads in the rest of the symbol table */
1265 pst->read_symtab = dwarf2_psymtab_to_symtab;
1266
1267 /* Check if comp unit has_children.
1268 If so, read the rest of the partial symbols from this comp unit.
1269 If not, there's no more debug_info for this comp unit. */
1270 if (comp_unit_die.has_children)
1271 {
1272 lowpc = ((CORE_ADDR) -1);
1273 highpc = ((CORE_ADDR) 0);
1274
1275 info_ptr = scan_partial_symbols (info_ptr, &lowpc, &highpc,
1276 &cu, NULL);
1277
1278 /* If we didn't find a lowpc, set it to highpc to avoid
1279 complaints from `maint check'. */
1280 if (lowpc == ((CORE_ADDR) -1))
1281 lowpc = highpc;
1282
1283 /* If the compilation unit didn't have an explicit address range,
1284 then use the information extracted from its child dies. */
1285 if (! comp_unit_die.has_pc_info)
1286 {
1287 comp_unit_die.lowpc = lowpc;
1288 comp_unit_die.highpc = highpc;
1289 }
1290 }
1291 pst->textlow = comp_unit_die.lowpc + baseaddr;
1292 pst->texthigh = comp_unit_die.highpc + baseaddr;
1293
1294 pst->n_global_syms = objfile->global_psymbols.next -
1295 (objfile->global_psymbols.list + pst->globals_offset);
1296 pst->n_static_syms = objfile->static_psymbols.next -
1297 (objfile->static_psymbols.list + pst->statics_offset);
1298 sort_pst_symbols (pst);
1299
1300 /* If there is already a psymtab or symtab for a file of this
1301 name, remove it. (If there is a symtab, more drastic things
1302 also happen.) This happens in VxWorks. */
1303 free_named_symtabs (pst->filename);
1304
1305 info_ptr = beg_of_comp_unit + cu.header.length
1306 + cu.header.initial_length_size;
1307 }
1308 do_cleanups (back_to);
1309 }
1310
1311 /* Read in all interesting dies to the end of the compilation unit or
1312 to the end of the current namespace. NAMESPACE is NULL if we
1313 haven't yet encountered any DW_TAG_namespace entries; otherwise,
1314 it's the name of the current namespace. In particular, it's the
1315 empty string if we're currently in the global namespace but have
1316 previously encountered a DW_TAG_namespace. */
1317
1318 static char *
1319 scan_partial_symbols (char *info_ptr, CORE_ADDR *lowpc,
1320 CORE_ADDR *highpc, struct dwarf2_cu *cu,
1321 const char *namespace)
1322 {
1323 struct objfile *objfile = cu->objfile;
1324 bfd *abfd = objfile->obfd;
1325 struct partial_die_info pdi;
1326
1327 /* Now, march along the PDI's, descending into ones which have
1328 interesting children but skipping the children of the other ones,
1329 until we reach the end of the compilation unit. */
1330
1331 while (1)
1332 {
1333 /* This flag tells whether or not info_ptr has gotten updated
1334 inside the loop. */
1335 int info_ptr_updated = 0;
1336
1337 info_ptr = read_partial_die (&pdi, abfd, info_ptr, cu);
1338
1339 /* Anonymous namespaces have no name but have interesting
1340 children, so we need to look at them. Ditto for anonymous
1341 enums. */
1342
1343 if (pdi.name != NULL || pdi.tag == DW_TAG_namespace
1344 || pdi.tag == DW_TAG_enumeration_type)
1345 {
1346 switch (pdi.tag)
1347 {
1348 case DW_TAG_subprogram:
1349 if (pdi.has_pc_info)
1350 {
1351 if (pdi.lowpc < *lowpc)
1352 {
1353 *lowpc = pdi.lowpc;
1354 }
1355 if (pdi.highpc > *highpc)
1356 {
1357 *highpc = pdi.highpc;
1358 }
1359 if (!pdi.is_declaration)
1360 {
1361 add_partial_symbol (&pdi, cu, namespace);
1362 }
1363 }
1364 break;
1365 case DW_TAG_variable:
1366 case DW_TAG_typedef:
1367 case DW_TAG_union_type:
1368 case DW_TAG_class_type:
1369 case DW_TAG_structure_type:
1370 if (!pdi.is_declaration)
1371 {
1372 add_partial_symbol (&pdi, cu, namespace);
1373 }
1374 break;
1375 case DW_TAG_enumeration_type:
1376 if (!pdi.is_declaration)
1377 {
1378 info_ptr = add_partial_enumeration (&pdi, info_ptr, cu,
1379 namespace);
1380 info_ptr_updated = 1;
1381 }
1382 break;
1383 case DW_TAG_base_type:
1384 /* File scope base type definitions are added to the partial
1385 symbol table. */
1386 add_partial_symbol (&pdi, cu, namespace);
1387 break;
1388 case DW_TAG_namespace:
1389 /* We've hit a DW_TAG_namespace entry, so we know this
1390 file has been compiled using a compiler that
1391 generates them; update NAMESPACE to reflect that. */
1392 if (namespace == NULL)
1393 namespace = "";
1394 info_ptr = add_partial_namespace (&pdi, info_ptr, lowpc, highpc,
1395 cu, namespace);
1396 info_ptr_updated = 1;
1397 break;
1398 default:
1399 break;
1400 }
1401 }
1402
1403 if (pdi.tag == 0)
1404 break;
1405
1406 /* If the die has a sibling, skip to the sibling, unless another
1407 function has already updated info_ptr for us. */
1408
1409 /* NOTE: carlton/2003-06-16: This is a bit hackish, but whether
1410 or not we want to update this depends on enough stuff (not
1411 only pdi.tag but also whether or not pdi.name is NULL) that
1412 this seems like the easiest way to handle the issue. */
1413
1414 if (!info_ptr_updated)
1415 info_ptr = locate_pdi_sibling (&pdi, info_ptr, abfd, cu);
1416 }
1417
1418 return info_ptr;
1419 }
1420
1421 static void
1422 add_partial_symbol (struct partial_die_info *pdi,
1423 struct dwarf2_cu *cu, const char *namespace)
1424 {
1425 struct objfile *objfile = cu->objfile;
1426 CORE_ADDR addr = 0;
1427 const struct partial_symbol *psym = NULL;
1428
1429 switch (pdi->tag)
1430 {
1431 case DW_TAG_subprogram:
1432 if (pdi->is_external)
1433 {
1434 /*prim_record_minimal_symbol (pdi->name, pdi->lowpc + baseaddr,
1435 mst_text, objfile); */
1436 psym = add_psymbol_to_list (pdi->name, strlen (pdi->name),
1437 VAR_DOMAIN, LOC_BLOCK,
1438 &objfile->global_psymbols,
1439 0, pdi->lowpc + baseaddr,
1440 cu_language, objfile);
1441 }
1442 else
1443 {
1444 /*prim_record_minimal_symbol (pdi->name, pdi->lowpc + baseaddr,
1445 mst_file_text, objfile); */
1446 psym = add_psymbol_to_list (pdi->name, strlen (pdi->name),
1447 VAR_DOMAIN, LOC_BLOCK,
1448 &objfile->static_psymbols,
1449 0, pdi->lowpc + baseaddr,
1450 cu_language, objfile);
1451 }
1452 break;
1453 case DW_TAG_variable:
1454 if (pdi->is_external)
1455 {
1456 /* Global Variable.
1457 Don't enter into the minimal symbol tables as there is
1458 a minimal symbol table entry from the ELF symbols already.
1459 Enter into partial symbol table if it has a location
1460 descriptor or a type.
1461 If the location descriptor is missing, new_symbol will create
1462 a LOC_UNRESOLVED symbol, the address of the variable will then
1463 be determined from the minimal symbol table whenever the variable
1464 is referenced.
1465 The address for the partial symbol table entry is not
1466 used by GDB, but it comes in handy for debugging partial symbol
1467 table building. */
1468
1469 if (pdi->locdesc)
1470 addr = decode_locdesc (pdi->locdesc, cu);
1471 if (pdi->locdesc || pdi->has_type)
1472 psym = add_psymbol_to_list (pdi->name, strlen (pdi->name),
1473 VAR_DOMAIN, LOC_STATIC,
1474 &objfile->global_psymbols,
1475 0, addr + baseaddr,
1476 cu_language, objfile);
1477 }
1478 else
1479 {
1480 /* Static Variable. Skip symbols without location descriptors. */
1481 if (pdi->locdesc == NULL)
1482 return;
1483 addr = decode_locdesc (pdi->locdesc, cu);
1484 /*prim_record_minimal_symbol (pdi->name, addr + baseaddr,
1485 mst_file_data, objfile); */
1486 psym = add_psymbol_to_list (pdi->name, strlen (pdi->name),
1487 VAR_DOMAIN, LOC_STATIC,
1488 &objfile->static_psymbols,
1489 0, addr + baseaddr,
1490 cu_language, objfile);
1491 }
1492 break;
1493 case DW_TAG_typedef:
1494 case DW_TAG_base_type:
1495 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1496 VAR_DOMAIN, LOC_TYPEDEF,
1497 &objfile->static_psymbols,
1498 0, (CORE_ADDR) 0, cu_language, objfile);
1499 break;
1500 case DW_TAG_class_type:
1501 case DW_TAG_structure_type:
1502 case DW_TAG_union_type:
1503 case DW_TAG_enumeration_type:
1504 /* Skip aggregate types without children, these are external
1505 references. */
1506 if (pdi->has_children == 0)
1507 return;
1508 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1509 STRUCT_DOMAIN, LOC_TYPEDEF,
1510 &objfile->static_psymbols,
1511 0, (CORE_ADDR) 0, cu_language, objfile);
1512
1513 if (cu_language == language_cplus)
1514 {
1515 /* For C++, these implicitly act as typedefs as well. */
1516 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1517 VAR_DOMAIN, LOC_TYPEDEF,
1518 &objfile->static_psymbols,
1519 0, (CORE_ADDR) 0, cu_language, objfile);
1520 }
1521 break;
1522 case DW_TAG_enumerator:
1523 add_psymbol_to_list (pdi->name, strlen (pdi->name),
1524 VAR_DOMAIN, LOC_CONST,
1525 &objfile->static_psymbols,
1526 0, (CORE_ADDR) 0, cu_language, objfile);
1527 break;
1528 default:
1529 break;
1530 }
1531
1532 /* Check to see if we should scan the name for possible namespace
1533 info. Only do this if this is C++, if we don't have namespace
1534 debugging info in the file, if the psym is of an appropriate type
1535 (otherwise we'll have psym == NULL), and if we actually had a
1536 mangled name to begin with. */
1537
1538 if (cu_language == language_cplus
1539 && namespace == NULL
1540 && psym != NULL
1541 && SYMBOL_CPLUS_DEMANGLED_NAME (psym) != NULL)
1542 cp_check_possible_namespace_symbols (SYMBOL_CPLUS_DEMANGLED_NAME (psym),
1543 objfile);
1544 }
1545
1546 /* Read a partial die corresponding to a namespace; also, add a symbol
1547 corresponding to that namespace to the symbol table. NAMESPACE is
1548 the name of the enclosing namespace. */
1549
1550 static char *
1551 add_partial_namespace (struct partial_die_info *pdi, char *info_ptr,
1552 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1553 struct dwarf2_cu *cu, const char *namespace)
1554 {
1555 struct objfile *objfile = cu->objfile;
1556 const char *new_name = pdi->name;
1557 char *full_name;
1558
1559 /* Calculate the full name of the namespace that we just entered. */
1560
1561 if (new_name == NULL)
1562 new_name = "(anonymous namespace)";
1563 full_name = alloca (strlen (namespace) + 2 + strlen (new_name) + 1);
1564 strcpy (full_name, namespace);
1565 if (*namespace != '\0')
1566 strcat (full_name, "::");
1567 strcat (full_name, new_name);
1568
1569 /* FIXME: carlton/2003-06-27: Once we build qualified names for more
1570 symbols than just namespaces, we should replace this by a call to
1571 add_partial_symbol. */
1572
1573 add_psymbol_to_list (full_name, strlen (full_name),
1574 VAR_DOMAIN, LOC_TYPEDEF,
1575 &objfile->global_psymbols,
1576 0, 0, cu_language, objfile);
1577
1578 /* Now scan partial symbols in that namespace. */
1579
1580 if (pdi->has_children)
1581 info_ptr = scan_partial_symbols (info_ptr, lowpc, highpc, cu, full_name);
1582
1583 return info_ptr;
1584 }
1585
1586 /* Read a partial die corresponding to an enumeration type. */
1587
1588 static char *
1589 add_partial_enumeration (struct partial_die_info *enum_pdi, char *info_ptr,
1590 struct dwarf2_cu *cu, const char *namespace)
1591 {
1592 struct objfile *objfile = cu->objfile;
1593 bfd *abfd = objfile->obfd;
1594 struct partial_die_info pdi;
1595
1596 if (enum_pdi->name != NULL)
1597 add_partial_symbol (enum_pdi, cu, namespace);
1598
1599 while (1)
1600 {
1601 info_ptr = read_partial_die (&pdi, abfd, info_ptr, cu);
1602 if (pdi.tag == 0)
1603 break;
1604 if (pdi.tag != DW_TAG_enumerator || pdi.name == NULL)
1605 complaint (&symfile_complaints, "malformed enumerator DIE ignored");
1606 else
1607 add_partial_symbol (&pdi, cu, namespace);
1608 }
1609
1610 return info_ptr;
1611 }
1612
1613 /* Locate ORIG_PDI's sibling; INFO_PTR should point to the next DIE
1614 after ORIG_PDI. */
1615
1616 static char *
1617 locate_pdi_sibling (struct partial_die_info *orig_pdi, char *info_ptr,
1618 bfd *abfd, struct dwarf2_cu *cu)
1619 {
1620 /* Do we know the sibling already? */
1621
1622 if (orig_pdi->sibling)
1623 return orig_pdi->sibling;
1624
1625 /* Are there any children to deal with? */
1626
1627 if (!orig_pdi->has_children)
1628 return info_ptr;
1629
1630 /* Okay, we don't know the sibling, but we have children that we
1631 want to skip. So read children until we run into one without a
1632 tag; return whatever follows it. */
1633
1634 while (1)
1635 {
1636 struct partial_die_info pdi;
1637
1638 info_ptr = read_partial_die (&pdi, abfd, info_ptr, cu);
1639
1640 if (pdi.tag == 0)
1641 return info_ptr;
1642 else
1643 info_ptr = locate_pdi_sibling (&pdi, info_ptr, abfd, cu);
1644 }
1645 }
1646
1647 /* Expand this partial symbol table into a full symbol table. */
1648
1649 static void
1650 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
1651 {
1652 /* FIXME: This is barely more than a stub. */
1653 if (pst != NULL)
1654 {
1655 if (pst->readin)
1656 {
1657 warning ("bug: psymtab for %s is already read in.", pst->filename);
1658 }
1659 else
1660 {
1661 if (info_verbose)
1662 {
1663 printf_filtered ("Reading in symbols for %s...", pst->filename);
1664 gdb_flush (gdb_stdout);
1665 }
1666
1667 psymtab_to_symtab_1 (pst);
1668
1669 /* Finish up the debug error message. */
1670 if (info_verbose)
1671 printf_filtered ("done.\n");
1672 }
1673 }
1674 }
1675
1676 static void
1677 psymtab_to_symtab_1 (struct partial_symtab *pst)
1678 {
1679 struct objfile *objfile = pst->objfile;
1680 bfd *abfd = objfile->obfd;
1681 struct dwarf2_cu cu;
1682 struct die_info *dies;
1683 unsigned long offset;
1684 CORE_ADDR lowpc, highpc;
1685 struct die_info *child_die;
1686 char *info_ptr;
1687 struct symtab *symtab;
1688 struct cleanup *back_to;
1689 struct attribute *attr;
1690
1691 /* Set local variables from the partial symbol table info. */
1692 offset = DWARF_INFO_OFFSET (pst);
1693 dwarf_info_buffer = DWARF_INFO_BUFFER (pst);
1694 dwarf_abbrev_buffer = DWARF_ABBREV_BUFFER (pst);
1695 dwarf_abbrev_size = DWARF_ABBREV_SIZE (pst);
1696 dwarf_line_buffer = DWARF_LINE_BUFFER (pst);
1697 dwarf_line_size = DWARF_LINE_SIZE (pst);
1698 dwarf_str_buffer = DWARF_STR_BUFFER (pst);
1699 dwarf_str_size = DWARF_STR_SIZE (pst);
1700 dwarf_macinfo_buffer = DWARF_MACINFO_BUFFER (pst);
1701 dwarf_macinfo_size = DWARF_MACINFO_SIZE (pst);
1702 dwarf_ranges_buffer = DWARF_RANGES_BUFFER (pst);
1703 dwarf_ranges_size = DWARF_RANGES_SIZE (pst);
1704 dwarf_loc_buffer = DWARF_LOC_BUFFER (pst);
1705 dwarf_loc_size = DWARF_LOC_SIZE (pst);
1706 baseaddr = ANOFFSET (pst->section_offsets, SECT_OFF_TEXT (objfile));
1707 cu_header_offset = offset;
1708 info_ptr = dwarf_info_buffer + offset;
1709
1710 obstack_init (&dwarf2_tmp_obstack);
1711 back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
1712
1713 buildsym_init ();
1714 make_cleanup (really_free_pendings, NULL);
1715
1716 cu.objfile = objfile;
1717
1718 /* read in the comp_unit header */
1719 info_ptr = read_comp_unit_head (&cu.header, info_ptr, abfd);
1720
1721 /* Read the abbrevs for this compilation unit */
1722 dwarf2_read_abbrevs (abfd, &cu);
1723 make_cleanup (dwarf2_empty_abbrev_table, cu.header.dwarf2_abbrevs);
1724
1725 dies = read_comp_unit (info_ptr, abfd, &cu);
1726
1727 make_cleanup_free_die_list (dies);
1728
1729 /* Find the base address of the compilation unit for range lists and
1730 location lists. It will normally be specified by DW_AT_low_pc.
1731 In DWARF-3 draft 4, the base address could be overridden by
1732 DW_AT_entry_pc. It's been removed, but GCC still uses this for
1733 compilation units with discontinuous ranges. */
1734
1735 cu.header.base_known = 0;
1736 cu.header.base_address = 0;
1737
1738 attr = dwarf_attr (dies, DW_AT_entry_pc);
1739 if (attr)
1740 {
1741 cu.header.base_address = DW_ADDR (attr);
1742 cu.header.base_known = 1;
1743 }
1744 else
1745 {
1746 attr = dwarf_attr (dies, DW_AT_low_pc);
1747 if (attr)
1748 {
1749 cu.header.base_address = DW_ADDR (attr);
1750 cu.header.base_known = 1;
1751 }
1752 }
1753
1754 /* Do line number decoding in read_file_scope () */
1755 process_die (dies, &cu);
1756
1757 if (!dwarf2_get_pc_bounds (dies, &lowpc, &highpc, &cu))
1758 {
1759 /* Some compilers don't define a DW_AT_high_pc attribute for
1760 the compilation unit. If the DW_AT_high_pc is missing,
1761 synthesize it, by scanning the DIE's below the compilation unit. */
1762 highpc = 0;
1763 if (dies->child != NULL)
1764 {
1765 child_die = dies->child;
1766 while (child_die && child_die->tag)
1767 {
1768 if (child_die->tag == DW_TAG_subprogram)
1769 {
1770 CORE_ADDR low, high;
1771
1772 if (dwarf2_get_pc_bounds (child_die, &low, &high, &cu))
1773 {
1774 highpc = max (highpc, high);
1775 }
1776 }
1777 child_die = sibling_die (child_die);
1778 }
1779 }
1780 }
1781 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
1782
1783 /* Set symtab language to language from DW_AT_language.
1784 If the compilation is from a C file generated by language preprocessors,
1785 do not set the language if it was already deduced by start_subfile. */
1786 if (symtab != NULL
1787 && !(cu_language == language_c && symtab->language != language_c))
1788 {
1789 symtab->language = cu_language;
1790 }
1791 pst->symtab = symtab;
1792 pst->readin = 1;
1793
1794 do_cleanups (back_to);
1795 }
1796
1797 /* Process a die and its children. */
1798
1799 static void
1800 process_die (struct die_info *die, struct dwarf2_cu *cu)
1801 {
1802 switch (die->tag)
1803 {
1804 case DW_TAG_padding:
1805 break;
1806 case DW_TAG_compile_unit:
1807 read_file_scope (die, cu);
1808 break;
1809 case DW_TAG_subprogram:
1810 read_subroutine_type (die, cu);
1811 read_func_scope (die, cu);
1812 break;
1813 case DW_TAG_inlined_subroutine:
1814 /* FIXME: These are ignored for now.
1815 They could be used to set breakpoints on all inlined instances
1816 of a function and make GDB `next' properly over inlined functions. */
1817 break;
1818 case DW_TAG_lexical_block:
1819 case DW_TAG_try_block:
1820 case DW_TAG_catch_block:
1821 read_lexical_block_scope (die, cu);
1822 break;
1823 case DW_TAG_class_type:
1824 case DW_TAG_structure_type:
1825 case DW_TAG_union_type:
1826 read_structure_scope (die, cu);
1827 break;
1828 case DW_TAG_enumeration_type:
1829 read_enumeration (die, cu);
1830 break;
1831 case DW_TAG_subroutine_type:
1832 read_subroutine_type (die, cu);
1833 break;
1834 case DW_TAG_array_type:
1835 read_array_type (die, cu);
1836 break;
1837 case DW_TAG_pointer_type:
1838 read_tag_pointer_type (die, cu);
1839 break;
1840 case DW_TAG_ptr_to_member_type:
1841 read_tag_ptr_to_member_type (die, cu);
1842 break;
1843 case DW_TAG_reference_type:
1844 read_tag_reference_type (die, cu);
1845 break;
1846 case DW_TAG_string_type:
1847 read_tag_string_type (die, cu);
1848 break;
1849 case DW_TAG_base_type:
1850 read_base_type (die, cu);
1851 if (dwarf_attr (die, DW_AT_name))
1852 {
1853 /* Add a typedef symbol for the base type definition. */
1854 new_symbol (die, die->type, cu);
1855 }
1856 break;
1857 case DW_TAG_common_block:
1858 read_common_block (die, cu);
1859 break;
1860 case DW_TAG_common_inclusion:
1861 break;
1862 case DW_TAG_namespace:
1863 if (!processing_has_namespace_info)
1864 {
1865 processing_has_namespace_info = 1;
1866 processing_current_namespace = "";
1867 }
1868 read_namespace (die, cu);
1869 break;
1870 case DW_TAG_imported_declaration:
1871 case DW_TAG_imported_module:
1872 /* FIXME: carlton/2002-10-16: Eventually, we should use the
1873 information contained in these. DW_TAG_imported_declaration
1874 dies shouldn't have children; DW_TAG_imported_module dies
1875 shouldn't in the C++ case, but conceivably could in the
1876 Fortran case, so we'll have to replace this gdb_assert if
1877 Fortran compilers start generating that info. */
1878 if (!processing_has_namespace_info)
1879 {
1880 processing_has_namespace_info = 1;
1881 processing_current_namespace = "";
1882 }
1883 gdb_assert (die->child == NULL);
1884 break;
1885 default:
1886 new_symbol (die, NULL, cu);
1887 break;
1888 }
1889 }
1890
1891 static void
1892 initialize_cu_func_list (void)
1893 {
1894 cu_first_fn = cu_last_fn = cu_cached_fn = NULL;
1895 }
1896
1897 static void
1898 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
1899 {
1900 struct objfile *objfile = cu->objfile;
1901 struct comp_unit_head *cu_header = &cu->header;
1902 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
1903 CORE_ADDR lowpc = ((CORE_ADDR) -1);
1904 CORE_ADDR highpc = ((CORE_ADDR) 0);
1905 struct attribute *attr;
1906 char *name = "<unknown>";
1907 char *comp_dir = NULL;
1908 struct die_info *child_die;
1909 bfd *abfd = objfile->obfd;
1910 struct line_header *line_header = 0;
1911
1912 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
1913 {
1914 if (die->child != NULL)
1915 {
1916 child_die = die->child;
1917 while (child_die && child_die->tag)
1918 {
1919 if (child_die->tag == DW_TAG_subprogram)
1920 {
1921 CORE_ADDR low, high;
1922
1923 if (dwarf2_get_pc_bounds (child_die, &low, &high, cu))
1924 {
1925 lowpc = min (lowpc, low);
1926 highpc = max (highpc, high);
1927 }
1928 }
1929 child_die = sibling_die (child_die);
1930 }
1931 }
1932 }
1933
1934 /* If we didn't find a lowpc, set it to highpc to avoid complaints
1935 from finish_block. */
1936 if (lowpc == ((CORE_ADDR) -1))
1937 lowpc = highpc;
1938 lowpc += baseaddr;
1939 highpc += baseaddr;
1940
1941 attr = dwarf_attr (die, DW_AT_name);
1942 if (attr)
1943 {
1944 name = DW_STRING (attr);
1945 }
1946 attr = dwarf_attr (die, DW_AT_comp_dir);
1947 if (attr)
1948 {
1949 comp_dir = DW_STRING (attr);
1950 if (comp_dir)
1951 {
1952 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1953 directory, get rid of it. */
1954 char *cp = strchr (comp_dir, ':');
1955
1956 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1957 comp_dir = cp + 1;
1958 }
1959 }
1960
1961 if (objfile->ei.entry_point >= lowpc &&
1962 objfile->ei.entry_point < highpc)
1963 {
1964 objfile->ei.deprecated_entry_file_lowpc = lowpc;
1965 objfile->ei.deprecated_entry_file_highpc = highpc;
1966 }
1967
1968 attr = dwarf_attr (die, DW_AT_language);
1969 if (attr)
1970 {
1971 set_cu_language (DW_UNSND (attr));
1972 }
1973
1974 /* We assume that we're processing GCC output. */
1975 processing_gcc_compilation = 2;
1976 #if 0
1977 /* FIXME:Do something here. */
1978 if (dip->at_producer != NULL)
1979 {
1980 handle_producer (dip->at_producer);
1981 }
1982 #endif
1983
1984 /* The compilation unit may be in a different language or objfile,
1985 zero out all remembered fundamental types. */
1986 memset (ftypes, 0, FT_NUM_MEMBERS * sizeof (struct type *));
1987
1988 start_symtab (name, comp_dir, lowpc);
1989 record_debugformat ("DWARF 2");
1990
1991 initialize_cu_func_list ();
1992
1993 /* Process all dies in compilation unit. */
1994 if (die->child != NULL)
1995 {
1996 child_die = die->child;
1997 while (child_die && child_die->tag)
1998 {
1999 process_die (child_die, cu);
2000 child_die = sibling_die (child_die);
2001 }
2002 }
2003
2004 /* Decode line number information if present. */
2005 attr = dwarf_attr (die, DW_AT_stmt_list);
2006 if (attr)
2007 {
2008 unsigned int line_offset = DW_UNSND (attr);
2009 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
2010 if (line_header)
2011 {
2012 make_cleanup ((make_cleanup_ftype *) free_line_header,
2013 (void *) line_header);
2014 dwarf_decode_lines (line_header, comp_dir, abfd, cu);
2015 }
2016 }
2017
2018 /* Decode macro information, if present. Dwarf 2 macro information
2019 refers to information in the line number info statement program
2020 header, so we can only read it if we've read the header
2021 successfully. */
2022 attr = dwarf_attr (die, DW_AT_macro_info);
2023 if (attr && line_header)
2024 {
2025 unsigned int macro_offset = DW_UNSND (attr);
2026 dwarf_decode_macros (line_header, macro_offset,
2027 comp_dir, abfd, cu);
2028 }
2029 do_cleanups (back_to);
2030 }
2031
2032 static void
2033 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc)
2034 {
2035 struct function_range *thisfn;
2036
2037 thisfn = (struct function_range *)
2038 obstack_alloc (&dwarf2_tmp_obstack, sizeof (struct function_range));
2039 thisfn->name = name;
2040 thisfn->lowpc = lowpc;
2041 thisfn->highpc = highpc;
2042 thisfn->seen_line = 0;
2043 thisfn->next = NULL;
2044
2045 if (cu_last_fn == NULL)
2046 cu_first_fn = thisfn;
2047 else
2048 cu_last_fn->next = thisfn;
2049
2050 cu_last_fn = thisfn;
2051 }
2052
2053 static void
2054 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
2055 {
2056 struct objfile *objfile = cu->objfile;
2057 struct context_stack *new;
2058 CORE_ADDR lowpc;
2059 CORE_ADDR highpc;
2060 struct die_info *child_die;
2061 struct attribute *attr;
2062 char *name;
2063
2064 name = dwarf2_linkage_name (die);
2065
2066 /* Ignore functions with missing or empty names and functions with
2067 missing or invalid low and high pc attributes. */
2068 if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
2069 return;
2070
2071 lowpc += baseaddr;
2072 highpc += baseaddr;
2073
2074 /* Record the function range for dwarf_decode_lines. */
2075 add_to_cu_func_list (name, lowpc, highpc);
2076
2077 if (objfile->ei.entry_point >= lowpc &&
2078 objfile->ei.entry_point < highpc)
2079 {
2080 objfile->ei.entry_func_lowpc = lowpc;
2081 objfile->ei.entry_func_highpc = highpc;
2082 }
2083
2084 new = push_context (0, lowpc);
2085 new->name = new_symbol (die, die->type, cu);
2086
2087 /* If there is a location expression for DW_AT_frame_base, record
2088 it. */
2089 attr = dwarf_attr (die, DW_AT_frame_base);
2090 if (attr)
2091 dwarf2_symbol_mark_computed (attr, new->name, cu);
2092
2093 list_in_scope = &local_symbols;
2094
2095 if (die->child != NULL)
2096 {
2097 child_die = die->child;
2098 while (child_die && child_die->tag)
2099 {
2100 process_die (child_die, cu);
2101 child_die = sibling_die (child_die);
2102 }
2103 }
2104
2105 new = pop_context ();
2106 /* Make a block for the local symbols within. */
2107 finish_block (new->name, &local_symbols, new->old_blocks,
2108 lowpc, highpc, objfile);
2109
2110 /* In C++, we can have functions nested inside functions (e.g., when
2111 a function declares a class that has methods). This means that
2112 when we finish processing a function scope, we may need to go
2113 back to building a containing block's symbol lists. */
2114 local_symbols = new->locals;
2115 param_symbols = new->params;
2116
2117 /* If we've finished processing a top-level function, subsequent
2118 symbols go in the file symbol list. */
2119 if (outermost_context_p ())
2120 list_in_scope = &file_symbols;
2121 }
2122
2123 /* Process all the DIES contained within a lexical block scope. Start
2124 a new scope, process the dies, and then close the scope. */
2125
2126 static void
2127 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
2128 {
2129 struct objfile *objfile = cu->objfile;
2130 struct context_stack *new;
2131 CORE_ADDR lowpc, highpc;
2132 struct die_info *child_die;
2133
2134 /* Ignore blocks with missing or invalid low and high pc attributes. */
2135 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
2136 as multiple lexical blocks? Handling children in a sane way would
2137 be nasty. Might be easier to properly extend generic blocks to
2138 describe ranges. */
2139 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
2140 return;
2141 lowpc += baseaddr;
2142 highpc += baseaddr;
2143
2144 push_context (0, lowpc);
2145 if (die->child != NULL)
2146 {
2147 child_die = die->child;
2148 while (child_die && child_die->tag)
2149 {
2150 process_die (child_die, cu);
2151 child_die = sibling_die (child_die);
2152 }
2153 }
2154 new = pop_context ();
2155
2156 if (local_symbols != NULL)
2157 {
2158 finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
2159 highpc, objfile);
2160 }
2161 local_symbols = new->locals;
2162 }
2163
2164 /* Get low and high pc attributes from a die. Return 1 if the attributes
2165 are present and valid, otherwise, return 0. Return -1 if the range is
2166 discontinuous, i.e. derived from DW_AT_ranges information. */
2167 static int
2168 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
2169 CORE_ADDR *highpc, struct dwarf2_cu *cu)
2170 {
2171 struct objfile *objfile = cu->objfile;
2172 struct comp_unit_head *cu_header = &cu->header;
2173 struct attribute *attr;
2174 bfd *obfd = objfile->obfd;
2175 CORE_ADDR low = 0;
2176 CORE_ADDR high = 0;
2177 int ret = 0;
2178
2179 attr = dwarf_attr (die, DW_AT_high_pc);
2180 if (attr)
2181 {
2182 high = DW_ADDR (attr);
2183 attr = dwarf_attr (die, DW_AT_low_pc);
2184 if (attr)
2185 low = DW_ADDR (attr);
2186 else
2187 /* Found high w/o low attribute. */
2188 return 0;
2189
2190 /* Found consecutive range of addresses. */
2191 ret = 1;
2192 }
2193 else
2194 {
2195 attr = dwarf_attr (die, DW_AT_ranges);
2196 if (attr != NULL)
2197 {
2198 unsigned int addr_size = cu_header->addr_size;
2199 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
2200 /* Value of the DW_AT_ranges attribute is the offset in the
2201 .debug_ranges section. */
2202 unsigned int offset = DW_UNSND (attr);
2203 /* Base address selection entry. */
2204 CORE_ADDR base;
2205 int found_base;
2206 int dummy;
2207 char *buffer;
2208 CORE_ADDR marker;
2209 int low_set;
2210
2211 found_base = cu_header->base_known;
2212 base = cu_header->base_address;
2213
2214 if (offset >= dwarf_ranges_size)
2215 {
2216 complaint (&symfile_complaints,
2217 "Offset %d out of bounds for DW_AT_ranges attribute",
2218 offset);
2219 return 0;
2220 }
2221 buffer = dwarf_ranges_buffer + offset;
2222
2223 /* Read in the largest possible address. */
2224 marker = read_address (obfd, buffer, cu, &dummy);
2225 if ((marker & mask) == mask)
2226 {
2227 /* If we found the largest possible address, then
2228 read the base address. */
2229 base = read_address (obfd, buffer + addr_size, cu, &dummy);
2230 buffer += 2 * addr_size;
2231 offset += 2 * addr_size;
2232 found_base = 1;
2233 }
2234
2235 low_set = 0;
2236
2237 while (1)
2238 {
2239 CORE_ADDR range_beginning, range_end;
2240
2241 range_beginning = read_address (obfd, buffer, cu, &dummy);
2242 buffer += addr_size;
2243 range_end = read_address (obfd, buffer, cu, &dummy);
2244 buffer += addr_size;
2245 offset += 2 * addr_size;
2246
2247 /* An end of list marker is a pair of zero addresses. */
2248 if (range_beginning == 0 && range_end == 0)
2249 /* Found the end of list entry. */
2250 break;
2251
2252 /* Each base address selection entry is a pair of 2 values.
2253 The first is the largest possible address, the second is
2254 the base address. Check for a base address here. */
2255 if ((range_beginning & mask) == mask)
2256 {
2257 /* If we found the largest possible address, then
2258 read the base address. */
2259 base = read_address (obfd, buffer + addr_size, cu, &dummy);
2260 found_base = 1;
2261 continue;
2262 }
2263
2264 if (!found_base)
2265 {
2266 /* We have no valid base address for the ranges
2267 data. */
2268 complaint (&symfile_complaints,
2269 "Invalid .debug_ranges data (no base address)");
2270 return 0;
2271 }
2272
2273 range_beginning += base;
2274 range_end += base;
2275
2276 /* FIXME: This is recording everything as a low-high
2277 segment of consecutive addresses. We should have a
2278 data structure for discontiguous block ranges
2279 instead. */
2280 if (! low_set)
2281 {
2282 low = range_beginning;
2283 high = range_end;
2284 low_set = 1;
2285 }
2286 else
2287 {
2288 if (range_beginning < low)
2289 low = range_beginning;
2290 if (range_end > high)
2291 high = range_end;
2292 }
2293 }
2294
2295 if (! low_set)
2296 /* If the first entry is an end-of-list marker, the range
2297 describes an empty scope, i.e. no instructions. */
2298 return 0;
2299
2300 ret = -1;
2301 }
2302 }
2303
2304 if (high < low)
2305 return 0;
2306
2307 /* When using the GNU linker, .gnu.linkonce. sections are used to
2308 eliminate duplicate copies of functions and vtables and such.
2309 The linker will arbitrarily choose one and discard the others.
2310 The AT_*_pc values for such functions refer to local labels in
2311 these sections. If the section from that file was discarded, the
2312 labels are not in the output, so the relocs get a value of 0.
2313 If this is a discarded function, mark the pc bounds as invalid,
2314 so that GDB will ignore it. */
2315 if (low == 0 && (bfd_get_file_flags (obfd) & HAS_RELOC) == 0)
2316 return 0;
2317
2318 *lowpc = low;
2319 *highpc = high;
2320 return ret;
2321 }
2322
2323 /* Add an aggregate field to the field list. */
2324
2325 static void
2326 dwarf2_add_field (struct field_info *fip, struct die_info *die,
2327 struct dwarf2_cu *cu)
2328 {
2329 struct objfile *objfile = cu->objfile;
2330 struct nextfield *new_field;
2331 struct attribute *attr;
2332 struct field *fp;
2333 char *fieldname = "";
2334
2335 /* Allocate a new field list entry and link it in. */
2336 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
2337 make_cleanup (xfree, new_field);
2338 memset (new_field, 0, sizeof (struct nextfield));
2339 new_field->next = fip->fields;
2340 fip->fields = new_field;
2341 fip->nfields++;
2342
2343 /* Handle accessibility and virtuality of field.
2344 The default accessibility for members is public, the default
2345 accessibility for inheritance is private. */
2346 if (die->tag != DW_TAG_inheritance)
2347 new_field->accessibility = DW_ACCESS_public;
2348 else
2349 new_field->accessibility = DW_ACCESS_private;
2350 new_field->virtuality = DW_VIRTUALITY_none;
2351
2352 attr = dwarf_attr (die, DW_AT_accessibility);
2353 if (attr)
2354 new_field->accessibility = DW_UNSND (attr);
2355 if (new_field->accessibility != DW_ACCESS_public)
2356 fip->non_public_fields = 1;
2357 attr = dwarf_attr (die, DW_AT_virtuality);
2358 if (attr)
2359 new_field->virtuality = DW_UNSND (attr);
2360
2361 fp = &new_field->field;
2362
2363 if (die->tag == DW_TAG_member && ! die_is_declaration (die))
2364 {
2365 /* Data member other than a C++ static data member. */
2366
2367 /* Get type of field. */
2368 fp->type = die_type (die, cu);
2369
2370 FIELD_STATIC_KIND (*fp) = 0;
2371
2372 /* Get bit size of field (zero if none). */
2373 attr = dwarf_attr (die, DW_AT_bit_size);
2374 if (attr)
2375 {
2376 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
2377 }
2378 else
2379 {
2380 FIELD_BITSIZE (*fp) = 0;
2381 }
2382
2383 /* Get bit offset of field. */
2384 attr = dwarf_attr (die, DW_AT_data_member_location);
2385 if (attr)
2386 {
2387 FIELD_BITPOS (*fp) =
2388 decode_locdesc (DW_BLOCK (attr), cu) * bits_per_byte;
2389 }
2390 else
2391 FIELD_BITPOS (*fp) = 0;
2392 attr = dwarf_attr (die, DW_AT_bit_offset);
2393 if (attr)
2394 {
2395 if (BITS_BIG_ENDIAN)
2396 {
2397 /* For big endian bits, the DW_AT_bit_offset gives the
2398 additional bit offset from the MSB of the containing
2399 anonymous object to the MSB of the field. We don't
2400 have to do anything special since we don't need to
2401 know the size of the anonymous object. */
2402 FIELD_BITPOS (*fp) += DW_UNSND (attr);
2403 }
2404 else
2405 {
2406 /* For little endian bits, compute the bit offset to the
2407 MSB of the anonymous object, subtract off the number of
2408 bits from the MSB of the field to the MSB of the
2409 object, and then subtract off the number of bits of
2410 the field itself. The result is the bit offset of
2411 the LSB of the field. */
2412 int anonymous_size;
2413 int bit_offset = DW_UNSND (attr);
2414
2415 attr = dwarf_attr (die, DW_AT_byte_size);
2416 if (attr)
2417 {
2418 /* The size of the anonymous object containing
2419 the bit field is explicit, so use the
2420 indicated size (in bytes). */
2421 anonymous_size = DW_UNSND (attr);
2422 }
2423 else
2424 {
2425 /* The size of the anonymous object containing
2426 the bit field must be inferred from the type
2427 attribute of the data member containing the
2428 bit field. */
2429 anonymous_size = TYPE_LENGTH (fp->type);
2430 }
2431 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
2432 - bit_offset - FIELD_BITSIZE (*fp);
2433 }
2434 }
2435
2436 /* Get name of field. */
2437 attr = dwarf_attr (die, DW_AT_name);
2438 if (attr && DW_STRING (attr))
2439 fieldname = DW_STRING (attr);
2440 fp->name = obsavestring (fieldname, strlen (fieldname),
2441 &objfile->type_obstack);
2442
2443 /* Change accessibility for artificial fields (e.g. virtual table
2444 pointer or virtual base class pointer) to private. */
2445 if (dwarf_attr (die, DW_AT_artificial))
2446 {
2447 new_field->accessibility = DW_ACCESS_private;
2448 fip->non_public_fields = 1;
2449 }
2450 }
2451 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
2452 {
2453 /* C++ static member. */
2454
2455 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
2456 is a declaration, but all versions of G++ as of this writing
2457 (so through at least 3.2.1) incorrectly generate
2458 DW_TAG_variable tags. */
2459
2460 char *physname;
2461
2462 /* Get name of field. */
2463 attr = dwarf_attr (die, DW_AT_name);
2464 if (attr && DW_STRING (attr))
2465 fieldname = DW_STRING (attr);
2466 else
2467 return;
2468
2469 /* Get physical name. */
2470 physname = dwarf2_linkage_name (die);
2471
2472 SET_FIELD_PHYSNAME (*fp, obsavestring (physname, strlen (physname),
2473 &objfile->type_obstack));
2474 FIELD_TYPE (*fp) = die_type (die, cu);
2475 FIELD_NAME (*fp) = obsavestring (fieldname, strlen (fieldname),
2476 &objfile->type_obstack);
2477 }
2478 else if (die->tag == DW_TAG_inheritance)
2479 {
2480 /* C++ base class field. */
2481 attr = dwarf_attr (die, DW_AT_data_member_location);
2482 if (attr)
2483 FIELD_BITPOS (*fp) = (decode_locdesc (DW_BLOCK (attr), cu)
2484 * bits_per_byte);
2485 FIELD_BITSIZE (*fp) = 0;
2486 FIELD_STATIC_KIND (*fp) = 0;
2487 FIELD_TYPE (*fp) = die_type (die, cu);
2488 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
2489 fip->nbaseclasses++;
2490 }
2491 }
2492
2493 /* Create the vector of fields, and attach it to the type. */
2494
2495 static void
2496 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
2497 struct dwarf2_cu *cu)
2498 {
2499 int nfields = fip->nfields;
2500
2501 /* Record the field count, allocate space for the array of fields,
2502 and create blank accessibility bitfields if necessary. */
2503 TYPE_NFIELDS (type) = nfields;
2504 TYPE_FIELDS (type) = (struct field *)
2505 TYPE_ALLOC (type, sizeof (struct field) * nfields);
2506 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
2507
2508 if (fip->non_public_fields)
2509 {
2510 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2511
2512 TYPE_FIELD_PRIVATE_BITS (type) =
2513 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
2514 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
2515
2516 TYPE_FIELD_PROTECTED_BITS (type) =
2517 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
2518 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
2519
2520 TYPE_FIELD_IGNORE_BITS (type) =
2521 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
2522 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
2523 }
2524
2525 /* If the type has baseclasses, allocate and clear a bit vector for
2526 TYPE_FIELD_VIRTUAL_BITS. */
2527 if (fip->nbaseclasses)
2528 {
2529 int num_bytes = B_BYTES (fip->nbaseclasses);
2530 char *pointer;
2531
2532 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2533 pointer = (char *) TYPE_ALLOC (type, num_bytes);
2534 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
2535 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
2536 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
2537 }
2538
2539 /* Copy the saved-up fields into the field vector. Start from the head
2540 of the list, adding to the tail of the field array, so that they end
2541 up in the same order in the array in which they were added to the list. */
2542 while (nfields-- > 0)
2543 {
2544 TYPE_FIELD (type, nfields) = fip->fields->field;
2545 switch (fip->fields->accessibility)
2546 {
2547 case DW_ACCESS_private:
2548 SET_TYPE_FIELD_PRIVATE (type, nfields);
2549 break;
2550
2551 case DW_ACCESS_protected:
2552 SET_TYPE_FIELD_PROTECTED (type, nfields);
2553 break;
2554
2555 case DW_ACCESS_public:
2556 break;
2557
2558 default:
2559 /* Unknown accessibility. Complain and treat it as public. */
2560 {
2561 complaint (&symfile_complaints, "unsupported accessibility %d",
2562 fip->fields->accessibility);
2563 }
2564 break;
2565 }
2566 if (nfields < fip->nbaseclasses)
2567 {
2568 switch (fip->fields->virtuality)
2569 {
2570 case DW_VIRTUALITY_virtual:
2571 case DW_VIRTUALITY_pure_virtual:
2572 SET_TYPE_FIELD_VIRTUAL (type, nfields);
2573 break;
2574 }
2575 }
2576 fip->fields = fip->fields->next;
2577 }
2578 }
2579
2580 /* Add a member function to the proper fieldlist. */
2581
2582 static void
2583 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
2584 struct type *type, struct dwarf2_cu *cu)
2585 {
2586 struct objfile *objfile = cu->objfile;
2587 struct attribute *attr;
2588 struct fnfieldlist *flp;
2589 int i;
2590 struct fn_field *fnp;
2591 char *fieldname;
2592 char *physname;
2593 struct nextfnfield *new_fnfield;
2594
2595 /* Get name of member function. */
2596 attr = dwarf_attr (die, DW_AT_name);
2597 if (attr && DW_STRING (attr))
2598 fieldname = DW_STRING (attr);
2599 else
2600 return;
2601
2602 /* Get the mangled name. */
2603 physname = dwarf2_linkage_name (die);
2604
2605 /* Look up member function name in fieldlist. */
2606 for (i = 0; i < fip->nfnfields; i++)
2607 {
2608 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
2609 break;
2610 }
2611
2612 /* Create new list element if necessary. */
2613 if (i < fip->nfnfields)
2614 flp = &fip->fnfieldlists[i];
2615 else
2616 {
2617 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
2618 {
2619 fip->fnfieldlists = (struct fnfieldlist *)
2620 xrealloc (fip->fnfieldlists,
2621 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
2622 * sizeof (struct fnfieldlist));
2623 if (fip->nfnfields == 0)
2624 make_cleanup (free_current_contents, &fip->fnfieldlists);
2625 }
2626 flp = &fip->fnfieldlists[fip->nfnfields];
2627 flp->name = fieldname;
2628 flp->length = 0;
2629 flp->head = NULL;
2630 fip->nfnfields++;
2631 }
2632
2633 /* Create a new member function field and chain it to the field list
2634 entry. */
2635 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
2636 make_cleanup (xfree, new_fnfield);
2637 memset (new_fnfield, 0, sizeof (struct nextfnfield));
2638 new_fnfield->next = flp->head;
2639 flp->head = new_fnfield;
2640 flp->length++;
2641
2642 /* Fill in the member function field info. */
2643 fnp = &new_fnfield->fnfield;
2644 fnp->physname = obsavestring (physname, strlen (physname),
2645 &objfile->type_obstack);
2646 fnp->type = alloc_type (objfile);
2647 if (die->type && TYPE_CODE (die->type) == TYPE_CODE_FUNC)
2648 {
2649 int nparams = TYPE_NFIELDS (die->type);
2650
2651 /* TYPE is the domain of this method, and DIE->TYPE is the type
2652 of the method itself (TYPE_CODE_METHOD). */
2653 smash_to_method_type (fnp->type, type,
2654 TYPE_TARGET_TYPE (die->type),
2655 TYPE_FIELDS (die->type),
2656 TYPE_NFIELDS (die->type),
2657 TYPE_VARARGS (die->type));
2658
2659 /* Handle static member functions.
2660 Dwarf2 has no clean way to discern C++ static and non-static
2661 member functions. G++ helps GDB by marking the first
2662 parameter for non-static member functions (which is the
2663 this pointer) as artificial. We obtain this information
2664 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
2665 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (die->type, 0) == 0)
2666 fnp->voffset = VOFFSET_STATIC;
2667 }
2668 else
2669 complaint (&symfile_complaints, "member function type missing for '%s'",
2670 physname);
2671
2672 /* Get fcontext from DW_AT_containing_type if present. */
2673 if (dwarf_attr (die, DW_AT_containing_type) != NULL)
2674 fnp->fcontext = die_containing_type (die, cu);
2675
2676 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
2677 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
2678
2679 /* Get accessibility. */
2680 attr = dwarf_attr (die, DW_AT_accessibility);
2681 if (attr)
2682 {
2683 switch (DW_UNSND (attr))
2684 {
2685 case DW_ACCESS_private:
2686 fnp->is_private = 1;
2687 break;
2688 case DW_ACCESS_protected:
2689 fnp->is_protected = 1;
2690 break;
2691 }
2692 }
2693
2694 /* Check for artificial methods. */
2695 attr = dwarf_attr (die, DW_AT_artificial);
2696 if (attr && DW_UNSND (attr) != 0)
2697 fnp->is_artificial = 1;
2698
2699 /* Get index in virtual function table if it is a virtual member function. */
2700 attr = dwarf_attr (die, DW_AT_vtable_elem_location);
2701 if (attr)
2702 {
2703 /* Support the .debug_loc offsets */
2704 if (attr_form_is_block (attr))
2705 {
2706 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
2707 }
2708 else if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
2709 {
2710 dwarf2_complex_location_expr_complaint ();
2711 }
2712 else
2713 {
2714 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
2715 fieldname);
2716 }
2717 }
2718 }
2719
2720 /* Create the vector of member function fields, and attach it to the type. */
2721
2722 static void
2723 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
2724 struct dwarf2_cu *cu)
2725 {
2726 struct fnfieldlist *flp;
2727 int total_length = 0;
2728 int i;
2729
2730 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2731 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
2732 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
2733
2734 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
2735 {
2736 struct nextfnfield *nfp = flp->head;
2737 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
2738 int k;
2739
2740 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
2741 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
2742 fn_flp->fn_fields = (struct fn_field *)
2743 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
2744 for (k = flp->length; (k--, nfp); nfp = nfp->next)
2745 fn_flp->fn_fields[k] = nfp->fnfield;
2746
2747 total_length += flp->length;
2748 }
2749
2750 TYPE_NFN_FIELDS (type) = fip->nfnfields;
2751 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
2752 }
2753
2754 /* Called when we find the DIE that starts a structure or union scope
2755 (definition) to process all dies that define the members of the
2756 structure or union.
2757
2758 NOTE: we need to call struct_type regardless of whether or not the
2759 DIE has an at_name attribute, since it might be an anonymous
2760 structure or union. This gets the type entered into our set of
2761 user defined types.
2762
2763 However, if the structure is incomplete (an opaque struct/union)
2764 then suppress creating a symbol table entry for it since gdb only
2765 wants to find the one with the complete definition. Note that if
2766 it is complete, we just call new_symbol, which does it's own
2767 checking about whether the struct/union is anonymous or not (and
2768 suppresses creating a symbol table entry itself). */
2769
2770 static void
2771 read_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
2772 {
2773 struct objfile *objfile = cu->objfile;
2774 struct type *type;
2775 struct attribute *attr;
2776
2777 type = alloc_type (objfile);
2778
2779 INIT_CPLUS_SPECIFIC (type);
2780 attr = dwarf_attr (die, DW_AT_name);
2781 if (attr && DW_STRING (attr))
2782 {
2783 TYPE_TAG_NAME (type) = obsavestring (DW_STRING (attr),
2784 strlen (DW_STRING (attr)),
2785 &objfile->type_obstack);
2786 }
2787
2788 if (die->tag == DW_TAG_structure_type)
2789 {
2790 TYPE_CODE (type) = TYPE_CODE_STRUCT;
2791 }
2792 else if (die->tag == DW_TAG_union_type)
2793 {
2794 TYPE_CODE (type) = TYPE_CODE_UNION;
2795 }
2796 else
2797 {
2798 /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
2799 in gdbtypes.h. */
2800 TYPE_CODE (type) = TYPE_CODE_CLASS;
2801 }
2802
2803 attr = dwarf_attr (die, DW_AT_byte_size);
2804 if (attr)
2805 {
2806 TYPE_LENGTH (type) = DW_UNSND (attr);
2807 }
2808 else
2809 {
2810 TYPE_LENGTH (type) = 0;
2811 }
2812
2813 /* We need to add the type field to the die immediately so we don't
2814 infinitely recurse when dealing with pointers to the structure
2815 type within the structure itself. */
2816 die->type = type;
2817
2818 if (die->child != NULL && ! die_is_declaration (die))
2819 {
2820 struct field_info fi;
2821 struct die_info *child_die;
2822 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
2823
2824 memset (&fi, 0, sizeof (struct field_info));
2825
2826 child_die = die->child;
2827
2828 while (child_die && child_die->tag)
2829 {
2830 if (child_die->tag == DW_TAG_member
2831 || child_die->tag == DW_TAG_variable)
2832 {
2833 /* NOTE: carlton/2002-11-05: A C++ static data member
2834 should be a DW_TAG_member that is a declaration, but
2835 all versions of G++ as of this writing (so through at
2836 least 3.2.1) incorrectly generate DW_TAG_variable
2837 tags for them instead. */
2838 dwarf2_add_field (&fi, child_die, cu);
2839 }
2840 else if (child_die->tag == DW_TAG_subprogram)
2841 {
2842 /* C++ member function. */
2843 process_die (child_die, cu);
2844 dwarf2_add_member_fn (&fi, child_die, type, cu);
2845 }
2846 else if (child_die->tag == DW_TAG_inheritance)
2847 {
2848 /* C++ base class field. */
2849 dwarf2_add_field (&fi, child_die, cu);
2850 }
2851 else
2852 {
2853 process_die (child_die, cu);
2854 }
2855 child_die = sibling_die (child_die);
2856 }
2857
2858 /* Attach fields and member functions to the type. */
2859 if (fi.nfields)
2860 dwarf2_attach_fields_to_type (&fi, type, cu);
2861 if (fi.nfnfields)
2862 {
2863 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
2864
2865 /* Get the type which refers to the base class (possibly this
2866 class itself) which contains the vtable pointer for the current
2867 class from the DW_AT_containing_type attribute. */
2868
2869 if (dwarf_attr (die, DW_AT_containing_type) != NULL)
2870 {
2871 struct type *t = die_containing_type (die, cu);
2872
2873 TYPE_VPTR_BASETYPE (type) = t;
2874 if (type == t)
2875 {
2876 static const char vptr_name[] =
2877 {'_', 'v', 'p', 't', 'r', '\0'};
2878 int i;
2879
2880 /* Our own class provides vtbl ptr. */
2881 for (i = TYPE_NFIELDS (t) - 1;
2882 i >= TYPE_N_BASECLASSES (t);
2883 --i)
2884 {
2885 char *fieldname = TYPE_FIELD_NAME (t, i);
2886
2887 if ((strncmp (fieldname, vptr_name,
2888 strlen (vptr_name) - 1)
2889 == 0)
2890 && is_cplus_marker (fieldname[strlen (vptr_name)]))
2891 {
2892 TYPE_VPTR_FIELDNO (type) = i;
2893 break;
2894 }
2895 }
2896
2897 /* Complain if virtual function table field not found. */
2898 if (i < TYPE_N_BASECLASSES (t))
2899 complaint (&symfile_complaints,
2900 "virtual function table pointer not found when defining class '%s'",
2901 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
2902 "");
2903 }
2904 else
2905 {
2906 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
2907 }
2908 }
2909 }
2910
2911 new_symbol (die, type, cu);
2912
2913 do_cleanups (back_to);
2914 }
2915 else
2916 {
2917 /* No children, must be stub. */
2918 TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
2919 }
2920 }
2921
2922 /* Given a pointer to a die which begins an enumeration, process all
2923 the dies that define the members of the enumeration.
2924
2925 This will be much nicer in draft 6 of the DWARF spec when our
2926 members will be dies instead squished into the DW_AT_element_list
2927 attribute.
2928
2929 NOTE: We reverse the order of the element list. */
2930
2931 static void
2932 read_enumeration (struct die_info *die, struct dwarf2_cu *cu)
2933 {
2934 struct objfile *objfile = cu->objfile;
2935 struct die_info *child_die;
2936 struct type *type;
2937 struct field *fields;
2938 struct attribute *attr;
2939 struct symbol *sym;
2940 int num_fields;
2941 int unsigned_enum = 1;
2942
2943 type = alloc_type (objfile);
2944
2945 TYPE_CODE (type) = TYPE_CODE_ENUM;
2946 attr = dwarf_attr (die, DW_AT_name);
2947 if (attr && DW_STRING (attr))
2948 {
2949 TYPE_TAG_NAME (type) = obsavestring (DW_STRING (attr),
2950 strlen (DW_STRING (attr)),
2951 &objfile->type_obstack);
2952 }
2953
2954 attr = dwarf_attr (die, DW_AT_byte_size);
2955 if (attr)
2956 {
2957 TYPE_LENGTH (type) = DW_UNSND (attr);
2958 }
2959 else
2960 {
2961 TYPE_LENGTH (type) = 0;
2962 }
2963
2964 num_fields = 0;
2965 fields = NULL;
2966 if (die->child != NULL)
2967 {
2968 child_die = die->child;
2969 while (child_die && child_die->tag)
2970 {
2971 if (child_die->tag != DW_TAG_enumerator)
2972 {
2973 process_die (child_die, cu);
2974 }
2975 else
2976 {
2977 attr = dwarf_attr (child_die, DW_AT_name);
2978 if (attr)
2979 {
2980 sym = new_symbol (child_die, type, cu);
2981 if (SYMBOL_VALUE (sym) < 0)
2982 unsigned_enum = 0;
2983
2984 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
2985 {
2986 fields = (struct field *)
2987 xrealloc (fields,
2988 (num_fields + DW_FIELD_ALLOC_CHUNK)
2989 * sizeof (struct field));
2990 }
2991
2992 FIELD_NAME (fields[num_fields]) = DEPRECATED_SYMBOL_NAME (sym);
2993 FIELD_TYPE (fields[num_fields]) = NULL;
2994 FIELD_BITPOS (fields[num_fields]) = SYMBOL_VALUE (sym);
2995 FIELD_BITSIZE (fields[num_fields]) = 0;
2996 FIELD_STATIC_KIND (fields[num_fields]) = 0;
2997
2998 num_fields++;
2999 }
3000 }
3001
3002 child_die = sibling_die (child_die);
3003 }
3004
3005 if (num_fields)
3006 {
3007 TYPE_NFIELDS (type) = num_fields;
3008 TYPE_FIELDS (type) = (struct field *)
3009 TYPE_ALLOC (type, sizeof (struct field) * num_fields);
3010 memcpy (TYPE_FIELDS (type), fields,
3011 sizeof (struct field) * num_fields);
3012 xfree (fields);
3013 }
3014 if (unsigned_enum)
3015 TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
3016 }
3017 die->type = type;
3018 new_symbol (die, type, cu);
3019 }
3020
3021 /* Extract all information from a DW_TAG_array_type DIE and put it in
3022 the DIE's type field. For now, this only handles one dimensional
3023 arrays. */
3024
3025 static void
3026 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
3027 {
3028 struct objfile *objfile = cu->objfile;
3029 struct die_info *child_die;
3030 struct type *type = NULL;
3031 struct type *element_type, *range_type, *index_type;
3032 struct type **range_types = NULL;
3033 struct attribute *attr;
3034 int ndim = 0;
3035 struct cleanup *back_to;
3036
3037 /* Return if we've already decoded this type. */
3038 if (die->type)
3039 {
3040 return;
3041 }
3042
3043 element_type = die_type (die, cu);
3044
3045 /* Irix 6.2 native cc creates array types without children for
3046 arrays with unspecified length. */
3047 if (die->child == NULL)
3048 {
3049 index_type = dwarf2_fundamental_type (objfile, FT_INTEGER);
3050 range_type = create_range_type (NULL, index_type, 0, -1);
3051 die->type = create_array_type (NULL, element_type, range_type);
3052 return;
3053 }
3054
3055 back_to = make_cleanup (null_cleanup, NULL);
3056 child_die = die->child;
3057 while (child_die && child_die->tag)
3058 {
3059 if (child_die->tag == DW_TAG_subrange_type)
3060 {
3061 unsigned int low, high;
3062
3063 /* Default bounds to an array with unspecified length. */
3064 low = 0;
3065 high = -1;
3066 if (cu_language == language_fortran)
3067 {
3068 /* FORTRAN implies a lower bound of 1, if not given. */
3069 low = 1;
3070 }
3071
3072 index_type = die_type (child_die, cu);
3073 attr = dwarf_attr (child_die, DW_AT_lower_bound);
3074 if (attr)
3075 {
3076 if (attr->form == DW_FORM_sdata)
3077 {
3078 low = DW_SND (attr);
3079 }
3080 else if (attr->form == DW_FORM_udata
3081 || attr->form == DW_FORM_data1
3082 || attr->form == DW_FORM_data2
3083 || attr->form == DW_FORM_data4
3084 || attr->form == DW_FORM_data8)
3085 {
3086 low = DW_UNSND (attr);
3087 }
3088 else
3089 {
3090 dwarf2_non_const_array_bound_ignored_complaint
3091 (dwarf_form_name (attr->form));
3092 low = 0;
3093 }
3094 }
3095 attr = dwarf_attr (child_die, DW_AT_upper_bound);
3096 if (attr)
3097 {
3098 if (attr->form == DW_FORM_sdata)
3099 {
3100 high = DW_SND (attr);
3101 }
3102 else if (attr->form == DW_FORM_udata
3103 || attr->form == DW_FORM_data1
3104 || attr->form == DW_FORM_data2
3105 || attr->form == DW_FORM_data4
3106 || attr->form == DW_FORM_data8)
3107 {
3108 high = DW_UNSND (attr);
3109 }
3110 else if (attr->form == DW_FORM_block1)
3111 {
3112 /* GCC encodes arrays with unspecified or dynamic length
3113 with a DW_FORM_block1 attribute.
3114 FIXME: GDB does not yet know how to handle dynamic
3115 arrays properly, treat them as arrays with unspecified
3116 length for now.
3117
3118 FIXME: jimb/2003-09-22: GDB does not really know
3119 how to handle arrays of unspecified length
3120 either; we just represent them as zero-length
3121 arrays. Choose an appropriate upper bound given
3122 the lower bound we've computed above. */
3123 high = low - 1;
3124 }
3125 else
3126 {
3127 dwarf2_non_const_array_bound_ignored_complaint
3128 (dwarf_form_name (attr->form));
3129 high = 1;
3130 }
3131 }
3132
3133 /* Create a range type and save it for array type creation. */
3134 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
3135 {
3136 range_types = (struct type **)
3137 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
3138 * sizeof (struct type *));
3139 if (ndim == 0)
3140 make_cleanup (free_current_contents, &range_types);
3141 }
3142 range_types[ndim++] = create_range_type (NULL, index_type, low, high);
3143 }
3144 child_die = sibling_die (child_die);
3145 }
3146
3147 /* Dwarf2 dimensions are output from left to right, create the
3148 necessary array types in backwards order. */
3149 type = element_type;
3150 while (ndim-- > 0)
3151 type = create_array_type (NULL, type, range_types[ndim]);
3152
3153 /* Understand Dwarf2 support for vector types (like they occur on
3154 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
3155 array type. This is not part of the Dwarf2/3 standard yet, but a
3156 custom vendor extension. The main difference between a regular
3157 array and the vector variant is that vectors are passed by value
3158 to functions. */
3159 attr = dwarf_attr (die, DW_AT_GNU_vector);
3160 if (attr)
3161 TYPE_FLAGS (type) |= TYPE_FLAG_VECTOR;
3162
3163 do_cleanups (back_to);
3164
3165 /* Install the type in the die. */
3166 die->type = type;
3167 }
3168
3169 /* First cut: install each common block member as a global variable. */
3170
3171 static void
3172 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
3173 {
3174 struct die_info *child_die;
3175 struct attribute *attr;
3176 struct symbol *sym;
3177 CORE_ADDR base = (CORE_ADDR) 0;
3178
3179 attr = dwarf_attr (die, DW_AT_location);
3180 if (attr)
3181 {
3182 /* Support the .debug_loc offsets */
3183 if (attr_form_is_block (attr))
3184 {
3185 base = decode_locdesc (DW_BLOCK (attr), cu);
3186 }
3187 else if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
3188 {
3189 dwarf2_complex_location_expr_complaint ();
3190 }
3191 else
3192 {
3193 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
3194 "common block member");
3195 }
3196 }
3197 if (die->child != NULL)
3198 {
3199 child_die = die->child;
3200 while (child_die && child_die->tag)
3201 {
3202 sym = new_symbol (child_die, NULL, cu);
3203 attr = dwarf_attr (child_die, DW_AT_data_member_location);
3204 if (attr)
3205 {
3206 SYMBOL_VALUE_ADDRESS (sym) =
3207 base + decode_locdesc (DW_BLOCK (attr), cu);
3208 add_symbol_to_list (sym, &global_symbols);
3209 }
3210 child_die = sibling_die (child_die);
3211 }
3212 }
3213 }
3214
3215 /* Read a C++ namespace. */
3216
3217 static void
3218 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
3219 {
3220 struct objfile *objfile = cu->objfile;
3221 const char *previous_namespace = processing_current_namespace;
3222 const char *name = NULL;
3223 int is_anonymous;
3224 struct die_info *current_die;
3225
3226 /* Loop through the extensions until we find a name. */
3227
3228 for (current_die = die;
3229 current_die != NULL;
3230 current_die = dwarf2_extension (die))
3231 {
3232 name = dwarf2_name (current_die);
3233 if (name != NULL)
3234 break;
3235 }
3236
3237 /* Is it an anonymous namespace? */
3238
3239 is_anonymous = (name == NULL);
3240 if (is_anonymous)
3241 name = "(anonymous namespace)";
3242
3243 /* Now build the name of the current namespace. */
3244
3245 if (previous_namespace[0] == '\0')
3246 {
3247 processing_current_namespace = name;
3248 }
3249 else
3250 {
3251 /* We need temp_name around because processing_current_namespace
3252 is a const char *. */
3253 char *temp_name = alloca (strlen (previous_namespace)
3254 + 2 + strlen(name) + 1);
3255 strcpy (temp_name, previous_namespace);
3256 strcat (temp_name, "::");
3257 strcat (temp_name, name);
3258
3259 processing_current_namespace = temp_name;
3260 }
3261
3262 /* Add a symbol associated to this if we haven't seen the namespace
3263 before. Also, add a using directive if it's an anonymous
3264 namespace. */
3265
3266 if (dwarf2_extension (die) == NULL)
3267 {
3268 struct type *type;
3269
3270 /* FIXME: carlton/2003-06-27: Once GDB is more const-correct,
3271 this cast will hopefully become unnecessary. */
3272 type = init_type (TYPE_CODE_NAMESPACE, 0, 0,
3273 (char *) processing_current_namespace,
3274 objfile);
3275 TYPE_TAG_NAME (type) = TYPE_NAME (type);
3276
3277 new_symbol (die, type, cu);
3278
3279 if (is_anonymous)
3280 cp_add_using_directive (processing_current_namespace,
3281 strlen (previous_namespace),
3282 strlen (processing_current_namespace));
3283 }
3284
3285 if (die->child != NULL)
3286 {
3287 struct die_info *child_die = die->child;
3288
3289 while (child_die && child_die->tag)
3290 {
3291 process_die (child_die, cu);
3292 child_die = sibling_die (child_die);
3293 }
3294 }
3295
3296 processing_current_namespace = previous_namespace;
3297 }
3298
3299 /* Extract all information from a DW_TAG_pointer_type DIE and add to
3300 the user defined type vector. */
3301
3302 static void
3303 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
3304 {
3305 struct comp_unit_head *cu_header = &cu->header;
3306 struct type *type;
3307 struct attribute *attr_byte_size;
3308 struct attribute *attr_address_class;
3309 int byte_size, addr_class;
3310
3311 if (die->type)
3312 {
3313 return;
3314 }
3315
3316 type = lookup_pointer_type (die_type (die, cu));
3317
3318 attr_byte_size = dwarf_attr (die, DW_AT_byte_size);
3319 if (attr_byte_size)
3320 byte_size = DW_UNSND (attr_byte_size);
3321 else
3322 byte_size = cu_header->addr_size;
3323
3324 attr_address_class = dwarf_attr (die, DW_AT_address_class);
3325 if (attr_address_class)
3326 addr_class = DW_UNSND (attr_address_class);
3327 else
3328 addr_class = DW_ADDR_none;
3329
3330 /* If the pointer size or address class is different than the
3331 default, create a type variant marked as such and set the
3332 length accordingly. */
3333 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
3334 {
3335 if (ADDRESS_CLASS_TYPE_FLAGS_P ())
3336 {
3337 int type_flags;
3338
3339 type_flags = ADDRESS_CLASS_TYPE_FLAGS (byte_size, addr_class);
3340 gdb_assert ((type_flags & ~TYPE_FLAG_ADDRESS_CLASS_ALL) == 0);
3341 type = make_type_with_address_space (type, type_flags);
3342 }
3343 else if (TYPE_LENGTH (type) != byte_size)
3344 {
3345 complaint (&symfile_complaints, "invalid pointer size %d", byte_size);
3346 }
3347 else {
3348 /* Should we also complain about unhandled address classes? */
3349 }
3350 }
3351
3352 TYPE_LENGTH (type) = byte_size;
3353 die->type = type;
3354 }
3355
3356 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
3357 the user defined type vector. */
3358
3359 static void
3360 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
3361 {
3362 struct objfile *objfile = cu->objfile;
3363 struct type *type;
3364 struct type *to_type;
3365 struct type *domain;
3366
3367 if (die->type)
3368 {
3369 return;
3370 }
3371
3372 type = alloc_type (objfile);
3373 to_type = die_type (die, cu);
3374 domain = die_containing_type (die, cu);
3375 smash_to_member_type (type, domain, to_type);
3376
3377 die->type = type;
3378 }
3379
3380 /* Extract all information from a DW_TAG_reference_type DIE and add to
3381 the user defined type vector. */
3382
3383 static void
3384 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
3385 {
3386 struct comp_unit_head *cu_header = &cu->header;
3387 struct type *type;
3388 struct attribute *attr;
3389
3390 if (die->type)
3391 {
3392 return;
3393 }
3394
3395 type = lookup_reference_type (die_type (die, cu));
3396 attr = dwarf_attr (die, DW_AT_byte_size);
3397 if (attr)
3398 {
3399 TYPE_LENGTH (type) = DW_UNSND (attr);
3400 }
3401 else
3402 {
3403 TYPE_LENGTH (type) = cu_header->addr_size;
3404 }
3405 die->type = type;
3406 }
3407
3408 static void
3409 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
3410 {
3411 struct type *base_type;
3412
3413 if (die->type)
3414 {
3415 return;
3416 }
3417
3418 base_type = die_type (die, cu);
3419 die->type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
3420 }
3421
3422 static void
3423 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
3424 {
3425 struct type *base_type;
3426
3427 if (die->type)
3428 {
3429 return;
3430 }
3431
3432 base_type = die_type (die, cu);
3433 die->type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
3434 }
3435
3436 /* Extract all information from a DW_TAG_string_type DIE and add to
3437 the user defined type vector. It isn't really a user defined type,
3438 but it behaves like one, with other DIE's using an AT_user_def_type
3439 attribute to reference it. */
3440
3441 static void
3442 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
3443 {
3444 struct objfile *objfile = cu->objfile;
3445 struct type *type, *range_type, *index_type, *char_type;
3446 struct attribute *attr;
3447 unsigned int length;
3448
3449 if (die->type)
3450 {
3451 return;
3452 }
3453
3454 attr = dwarf_attr (die, DW_AT_string_length);
3455 if (attr)
3456 {
3457 length = DW_UNSND (attr);
3458 }
3459 else
3460 {
3461 /* check for the DW_AT_byte_size attribute */
3462 attr = dwarf_attr (die, DW_AT_byte_size);
3463 if (attr)
3464 {
3465 length = DW_UNSND (attr);
3466 }
3467 else
3468 {
3469 length = 1;
3470 }
3471 }
3472 index_type = dwarf2_fundamental_type (objfile, FT_INTEGER);
3473 range_type = create_range_type (NULL, index_type, 1, length);
3474 if (cu_language == language_fortran)
3475 {
3476 /* Need to create a unique string type for bounds
3477 information */
3478 type = create_string_type (0, range_type);
3479 }
3480 else
3481 {
3482 char_type = dwarf2_fundamental_type (objfile, FT_CHAR);
3483 type = create_string_type (char_type, range_type);
3484 }
3485 die->type = type;
3486 }
3487
3488 /* Handle DIES due to C code like:
3489
3490 struct foo
3491 {
3492 int (*funcp)(int a, long l);
3493 int b;
3494 };
3495
3496 ('funcp' generates a DW_TAG_subroutine_type DIE)
3497 */
3498
3499 static void
3500 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
3501 {
3502 struct type *type; /* Type that this function returns */
3503 struct type *ftype; /* Function that returns above type */
3504 struct attribute *attr;
3505
3506 /* Decode the type that this subroutine returns */
3507 if (die->type)
3508 {
3509 return;
3510 }
3511 type = die_type (die, cu);
3512 ftype = lookup_function_type (type);
3513
3514 /* All functions in C++ have prototypes. */
3515 attr = dwarf_attr (die, DW_AT_prototyped);
3516 if ((attr && (DW_UNSND (attr) != 0))
3517 || cu_language == language_cplus)
3518 TYPE_FLAGS (ftype) |= TYPE_FLAG_PROTOTYPED;
3519
3520 if (die->child != NULL)
3521 {
3522 struct die_info *child_die;
3523 int nparams = 0;
3524 int iparams = 0;
3525
3526 /* Count the number of parameters.
3527 FIXME: GDB currently ignores vararg functions, but knows about
3528 vararg member functions. */
3529 child_die = die->child;
3530 while (child_die && child_die->tag)
3531 {
3532 if (child_die->tag == DW_TAG_formal_parameter)
3533 nparams++;
3534 else if (child_die->tag == DW_TAG_unspecified_parameters)
3535 TYPE_FLAGS (ftype) |= TYPE_FLAG_VARARGS;
3536 child_die = sibling_die (child_die);
3537 }
3538
3539 /* Allocate storage for parameters and fill them in. */
3540 TYPE_NFIELDS (ftype) = nparams;
3541 TYPE_FIELDS (ftype) = (struct field *)
3542 TYPE_ALLOC (ftype, nparams * sizeof (struct field));
3543
3544 child_die = die->child;
3545 while (child_die && child_die->tag)
3546 {
3547 if (child_die->tag == DW_TAG_formal_parameter)
3548 {
3549 /* Dwarf2 has no clean way to discern C++ static and non-static
3550 member functions. G++ helps GDB by marking the first
3551 parameter for non-static member functions (which is the
3552 this pointer) as artificial. We pass this information
3553 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL. */
3554 attr = dwarf_attr (child_die, DW_AT_artificial);
3555 if (attr)
3556 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
3557 else
3558 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3559 TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
3560 iparams++;
3561 }
3562 child_die = sibling_die (child_die);
3563 }
3564 }
3565
3566 die->type = ftype;
3567 }
3568
3569 static void
3570 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
3571 {
3572 struct objfile *objfile = cu->objfile;
3573 struct attribute *attr;
3574 char *name = NULL;
3575
3576 if (!die->type)
3577 {
3578 attr = dwarf_attr (die, DW_AT_name);
3579 if (attr && DW_STRING (attr))
3580 {
3581 name = DW_STRING (attr);
3582 }
3583 die->type = init_type (TYPE_CODE_TYPEDEF, 0, TYPE_FLAG_TARGET_STUB, name, objfile);
3584 TYPE_TARGET_TYPE (die->type) = die_type (die, cu);
3585 }
3586 }
3587
3588 /* Find a representation of a given base type and install
3589 it in the TYPE field of the die. */
3590
3591 static void
3592 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
3593 {
3594 struct objfile *objfile = cu->objfile;
3595 struct type *type;
3596 struct attribute *attr;
3597 int encoding = 0, size = 0;
3598
3599 /* If we've already decoded this die, this is a no-op. */
3600 if (die->type)
3601 {
3602 return;
3603 }
3604
3605 attr = dwarf_attr (die, DW_AT_encoding);
3606 if (attr)
3607 {
3608 encoding = DW_UNSND (attr);
3609 }
3610 attr = dwarf_attr (die, DW_AT_byte_size);
3611 if (attr)
3612 {
3613 size = DW_UNSND (attr);
3614 }
3615 attr = dwarf_attr (die, DW_AT_name);
3616 if (attr && DW_STRING (attr))
3617 {
3618 enum type_code code = TYPE_CODE_INT;
3619 int type_flags = 0;
3620
3621 switch (encoding)
3622 {
3623 case DW_ATE_address:
3624 /* Turn DW_ATE_address into a void * pointer. */
3625 code = TYPE_CODE_PTR;
3626 type_flags |= TYPE_FLAG_UNSIGNED;
3627 break;
3628 case DW_ATE_boolean:
3629 code = TYPE_CODE_BOOL;
3630 type_flags |= TYPE_FLAG_UNSIGNED;
3631 break;
3632 case DW_ATE_complex_float:
3633 code = TYPE_CODE_COMPLEX;
3634 break;
3635 case DW_ATE_float:
3636 code = TYPE_CODE_FLT;
3637 break;
3638 case DW_ATE_signed:
3639 case DW_ATE_signed_char:
3640 break;
3641 case DW_ATE_unsigned:
3642 case DW_ATE_unsigned_char:
3643 type_flags |= TYPE_FLAG_UNSIGNED;
3644 break;
3645 default:
3646 complaint (&symfile_complaints, "unsupported DW_AT_encoding: '%s'",
3647 dwarf_type_encoding_name (encoding));
3648 break;
3649 }
3650 type = init_type (code, size, type_flags, DW_STRING (attr), objfile);
3651 if (encoding == DW_ATE_address)
3652 TYPE_TARGET_TYPE (type) = dwarf2_fundamental_type (objfile, FT_VOID);
3653 else if (encoding == DW_ATE_complex_float)
3654 {
3655 if (size == 32)
3656 TYPE_TARGET_TYPE (type)
3657 = dwarf2_fundamental_type (objfile, FT_EXT_PREC_FLOAT);
3658 else if (size == 16)
3659 TYPE_TARGET_TYPE (type)
3660 = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT);
3661 else if (size == 8)
3662 TYPE_TARGET_TYPE (type)
3663 = dwarf2_fundamental_type (objfile, FT_FLOAT);
3664 }
3665 }
3666 else
3667 {
3668 type = dwarf_base_type (encoding, size, cu);
3669 }
3670 die->type = type;
3671 }
3672
3673 /* Read a whole compilation unit into a linked list of dies. */
3674
3675 static struct die_info *
3676 read_comp_unit (char *info_ptr, bfd *abfd, struct dwarf2_cu *cu)
3677 {
3678 /* Reset die reference table; we are
3679 building new ones now. */
3680 dwarf2_empty_hash_tables ();
3681
3682 return read_die_and_children (info_ptr, abfd, cu, &info_ptr, NULL);
3683 }
3684
3685 /* Read a single die and all its descendents. Set the die's sibling
3686 field to NULL; set other fields in the die correctly, and set all
3687 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
3688 location of the info_ptr after reading all of those dies. PARENT
3689 is the parent of the die in question. */
3690
3691 static struct die_info *
3692 read_die_and_children (char *info_ptr, bfd *abfd,
3693 struct dwarf2_cu *cu,
3694 char **new_info_ptr,
3695 struct die_info *parent)
3696 {
3697 struct die_info *die;
3698 char *cur_ptr;
3699 int has_children;
3700
3701 cur_ptr = read_full_die (&die, abfd, info_ptr, cu, &has_children);
3702 store_in_ref_table (die->offset, die);
3703
3704 if (has_children)
3705 {
3706 die->child = read_die_and_siblings (cur_ptr, abfd, cu,
3707 new_info_ptr, die);
3708 }
3709 else
3710 {
3711 die->child = NULL;
3712 *new_info_ptr = cur_ptr;
3713 }
3714
3715 die->sibling = NULL;
3716 die->parent = parent;
3717 return die;
3718 }
3719
3720 /* Read a die, all of its descendents, and all of its siblings; set
3721 all of the fields of all of the dies correctly. Arguments are as
3722 in read_die_and_children. */
3723
3724 static struct die_info *
3725 read_die_and_siblings (char *info_ptr, bfd *abfd,
3726 struct dwarf2_cu *cu,
3727 char **new_info_ptr,
3728 struct die_info *parent)
3729 {
3730 struct die_info *first_die, *last_sibling;
3731 char *cur_ptr;
3732
3733 cur_ptr = info_ptr;
3734 first_die = last_sibling = NULL;
3735
3736 while (1)
3737 {
3738 struct die_info *die
3739 = read_die_and_children (cur_ptr, abfd, cu, &cur_ptr, parent);
3740
3741 if (!first_die)
3742 {
3743 first_die = die;
3744 }
3745 else
3746 {
3747 last_sibling->sibling = die;
3748 }
3749
3750 if (die->tag == 0)
3751 {
3752 *new_info_ptr = cur_ptr;
3753 return first_die;
3754 }
3755 else
3756 {
3757 last_sibling = die;
3758 }
3759 }
3760 }
3761
3762 /* Free a linked list of dies. */
3763
3764 static void
3765 free_die_list (struct die_info *dies)
3766 {
3767 struct die_info *die, *next;
3768
3769 die = dies;
3770 while (die)
3771 {
3772 if (die->child != NULL)
3773 free_die_list (die->child);
3774 next = die->sibling;
3775 xfree (die->attrs);
3776 xfree (die);
3777 die = next;
3778 }
3779 }
3780
3781 static void
3782 do_free_die_list_cleanup (void *dies)
3783 {
3784 free_die_list (dies);
3785 }
3786
3787 static struct cleanup *
3788 make_cleanup_free_die_list (struct die_info *dies)
3789 {
3790 return make_cleanup (do_free_die_list_cleanup, dies);
3791 }
3792
3793
3794 /* Read the contents of the section at OFFSET and of size SIZE from the
3795 object file specified by OBJFILE into the psymbol_obstack and return it. */
3796
3797 char *
3798 dwarf2_read_section (struct objfile *objfile, asection *sectp)
3799 {
3800 bfd *abfd = objfile->obfd;
3801 char *buf, *retbuf;
3802 bfd_size_type size = bfd_get_section_size_before_reloc (sectp);
3803
3804 if (size == 0)
3805 return NULL;
3806
3807 buf = (char *) obstack_alloc (&objfile->psymbol_obstack, size);
3808 retbuf
3809 = (char *) symfile_relocate_debug_section (abfd, sectp, (bfd_byte *) buf);
3810 if (retbuf != NULL)
3811 return retbuf;
3812
3813 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3814 || bfd_bread (buf, size, abfd) != size)
3815 error ("Dwarf Error: Can't read DWARF data from '%s'",
3816 bfd_get_filename (abfd));
3817
3818 return buf;
3819 }
3820
3821 /* In DWARF version 2, the description of the debugging information is
3822 stored in a separate .debug_abbrev section. Before we read any
3823 dies from a section we read in all abbreviations and install them
3824 in a hash table. */
3825
3826 static void
3827 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
3828 {
3829 struct comp_unit_head *cu_header = &cu->header;
3830 char *abbrev_ptr;
3831 struct abbrev_info *cur_abbrev;
3832 unsigned int abbrev_number, bytes_read, abbrev_name;
3833 unsigned int abbrev_form, hash_number;
3834
3835 /* Initialize dwarf2 abbrevs */
3836 memset (cu_header->dwarf2_abbrevs, 0,
3837 ABBREV_HASH_SIZE*sizeof (struct abbrev_info *));
3838
3839 abbrev_ptr = dwarf_abbrev_buffer + cu_header->abbrev_offset;
3840 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3841 abbrev_ptr += bytes_read;
3842
3843 /* loop until we reach an abbrev number of 0 */
3844 while (abbrev_number)
3845 {
3846 cur_abbrev = dwarf_alloc_abbrev ();
3847
3848 /* read in abbrev header */
3849 cur_abbrev->number = abbrev_number;
3850 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3851 abbrev_ptr += bytes_read;
3852 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
3853 abbrev_ptr += 1;
3854
3855 /* now read in declarations */
3856 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3857 abbrev_ptr += bytes_read;
3858 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3859 abbrev_ptr += bytes_read;
3860 while (abbrev_name)
3861 {
3862 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
3863 {
3864 cur_abbrev->attrs = (struct attr_abbrev *)
3865 xrealloc (cur_abbrev->attrs,
3866 (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
3867 * sizeof (struct attr_abbrev));
3868 }
3869 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
3870 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
3871 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3872 abbrev_ptr += bytes_read;
3873 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3874 abbrev_ptr += bytes_read;
3875 }
3876
3877 hash_number = abbrev_number % ABBREV_HASH_SIZE;
3878 cur_abbrev->next = cu_header->dwarf2_abbrevs[hash_number];
3879 cu_header->dwarf2_abbrevs[hash_number] = cur_abbrev;
3880
3881 /* Get next abbreviation.
3882 Under Irix6 the abbreviations for a compilation unit are not
3883 always properly terminated with an abbrev number of 0.
3884 Exit loop if we encounter an abbreviation which we have
3885 already read (which means we are about to read the abbreviations
3886 for the next compile unit) or if the end of the abbreviation
3887 table is reached. */
3888 if ((unsigned int) (abbrev_ptr - dwarf_abbrev_buffer)
3889 >= dwarf_abbrev_size)
3890 break;
3891 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3892 abbrev_ptr += bytes_read;
3893 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
3894 break;
3895 }
3896 }
3897
3898 /* Empty the abbrev table for a new compilation unit. */
3899
3900 static void
3901 dwarf2_empty_abbrev_table (void *ptr_to_abbrevs_table)
3902 {
3903 int i;
3904 struct abbrev_info *abbrev, *next;
3905 struct abbrev_info **abbrevs;
3906
3907 abbrevs = (struct abbrev_info **)ptr_to_abbrevs_table;
3908
3909 for (i = 0; i < ABBREV_HASH_SIZE; ++i)
3910 {
3911 next = NULL;
3912 abbrev = abbrevs[i];
3913 while (abbrev)
3914 {
3915 next = abbrev->next;
3916 xfree (abbrev->attrs);
3917 xfree (abbrev);
3918 abbrev = next;
3919 }
3920 abbrevs[i] = NULL;
3921 }
3922 }
3923
3924 /* Lookup an abbrev_info structure in the abbrev hash table. */
3925
3926 static struct abbrev_info *
3927 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
3928 {
3929 struct comp_unit_head *cu_header = &cu->header;
3930 unsigned int hash_number;
3931 struct abbrev_info *abbrev;
3932
3933 hash_number = number % ABBREV_HASH_SIZE;
3934 abbrev = cu_header->dwarf2_abbrevs[hash_number];
3935
3936 while (abbrev)
3937 {
3938 if (abbrev->number == number)
3939 return abbrev;
3940 else
3941 abbrev = abbrev->next;
3942 }
3943 return NULL;
3944 }
3945
3946 /* Read a minimal amount of information into the minimal die structure. */
3947
3948 static char *
3949 read_partial_die (struct partial_die_info *part_die, bfd *abfd,
3950 char *info_ptr, struct dwarf2_cu *cu)
3951 {
3952 unsigned int abbrev_number, bytes_read, i;
3953 struct abbrev_info *abbrev;
3954 struct attribute attr;
3955 struct attribute spec_attr;
3956 int found_spec_attr = 0;
3957 int has_low_pc_attr = 0;
3958 int has_high_pc_attr = 0;
3959
3960 *part_die = zeroed_partial_die;
3961 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3962 info_ptr += bytes_read;
3963 if (!abbrev_number)
3964 return info_ptr;
3965
3966 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3967 if (!abbrev)
3968 {
3969 error ("Dwarf Error: Could not find abbrev number %d [in module %s]", abbrev_number,
3970 bfd_get_filename (abfd));
3971 }
3972 part_die->offset = info_ptr - dwarf_info_buffer;
3973 part_die->tag = abbrev->tag;
3974 part_die->has_children = abbrev->has_children;
3975 part_die->abbrev = abbrev_number;
3976
3977 for (i = 0; i < abbrev->num_attrs; ++i)
3978 {
3979 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
3980
3981 /* Store the data if it is of an attribute we want to keep in a
3982 partial symbol table. */
3983 switch (attr.name)
3984 {
3985 case DW_AT_name:
3986
3987 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
3988 if (part_die->name == NULL)
3989 part_die->name = DW_STRING (&attr);
3990 break;
3991 case DW_AT_MIPS_linkage_name:
3992 part_die->name = DW_STRING (&attr);
3993 break;
3994 case DW_AT_low_pc:
3995 has_low_pc_attr = 1;
3996 part_die->lowpc = DW_ADDR (&attr);
3997 break;
3998 case DW_AT_high_pc:
3999 has_high_pc_attr = 1;
4000 part_die->highpc = DW_ADDR (&attr);
4001 break;
4002 case DW_AT_location:
4003 /* Support the .debug_loc offsets */
4004 if (attr_form_is_block (&attr))
4005 {
4006 part_die->locdesc = DW_BLOCK (&attr);
4007 }
4008 else if (attr.form == DW_FORM_data4 || attr.form == DW_FORM_data8)
4009 {
4010 dwarf2_complex_location_expr_complaint ();
4011 }
4012 else
4013 {
4014 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
4015 "partial symbol information");
4016 }
4017 break;
4018 case DW_AT_language:
4019 part_die->language = DW_UNSND (&attr);
4020 break;
4021 case DW_AT_external:
4022 part_die->is_external = DW_UNSND (&attr);
4023 break;
4024 case DW_AT_declaration:
4025 part_die->is_declaration = DW_UNSND (&attr);
4026 break;
4027 case DW_AT_type:
4028 part_die->has_type = 1;
4029 break;
4030 case DW_AT_abstract_origin:
4031 case DW_AT_specification:
4032 found_spec_attr = 1;
4033 spec_attr = attr;
4034 break;
4035 case DW_AT_sibling:
4036 /* Ignore absolute siblings, they might point outside of
4037 the current compile unit. */
4038 if (attr.form == DW_FORM_ref_addr)
4039 complaint (&symfile_complaints, "ignoring absolute DW_AT_sibling");
4040 else
4041 part_die->sibling =
4042 dwarf_info_buffer + dwarf2_get_ref_die_offset (&attr);
4043 break;
4044 default:
4045 break;
4046 }
4047 }
4048
4049 /* If we found a reference attribute and the die has no name, try
4050 to find a name in the referred to die. */
4051
4052 if (found_spec_attr && part_die->name == NULL)
4053 {
4054 struct partial_die_info spec_die;
4055 char *spec_ptr;
4056
4057 spec_ptr = dwarf_info_buffer + dwarf2_get_ref_die_offset (&spec_attr);
4058 read_partial_die (&spec_die, abfd, spec_ptr, cu);
4059 if (spec_die.name)
4060 {
4061 part_die->name = spec_die.name;
4062
4063 /* Copy DW_AT_external attribute if it is set. */
4064 if (spec_die.is_external)
4065 part_die->is_external = spec_die.is_external;
4066 }
4067 }
4068
4069 /* When using the GNU linker, .gnu.linkonce. sections are used to
4070 eliminate duplicate copies of functions and vtables and such.
4071 The linker will arbitrarily choose one and discard the others.
4072 The AT_*_pc values for such functions refer to local labels in
4073 these sections. If the section from that file was discarded, the
4074 labels are not in the output, so the relocs get a value of 0.
4075 If this is a discarded function, mark the pc bounds as invalid,
4076 so that GDB will ignore it. */
4077 if (has_low_pc_attr && has_high_pc_attr
4078 && part_die->lowpc < part_die->highpc
4079 && (part_die->lowpc != 0
4080 || (bfd_get_file_flags (abfd) & HAS_RELOC)))
4081 part_die->has_pc_info = 1;
4082 return info_ptr;
4083 }
4084
4085 /* Read the die from the .debug_info section buffer. Set DIEP to
4086 point to a newly allocated die with its information, except for its
4087 child, sibling, and parent fields. Set HAS_CHILDREN to tell
4088 whether the die has children or not. */
4089
4090 static char *
4091 read_full_die (struct die_info **diep, bfd *abfd, char *info_ptr,
4092 struct dwarf2_cu *cu, int *has_children)
4093 {
4094 unsigned int abbrev_number, bytes_read, i, offset;
4095 struct abbrev_info *abbrev;
4096 struct die_info *die;
4097
4098 offset = info_ptr - dwarf_info_buffer;
4099 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4100 info_ptr += bytes_read;
4101 if (!abbrev_number)
4102 {
4103 die = dwarf_alloc_die ();
4104 die->tag = 0;
4105 die->abbrev = abbrev_number;
4106 die->type = NULL;
4107 *diep = die;
4108 *has_children = 0;
4109 return info_ptr;
4110 }
4111
4112 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4113 if (!abbrev)
4114 {
4115 error ("Dwarf Error: could not find abbrev number %d [in module %s]",
4116 abbrev_number,
4117 bfd_get_filename (abfd));
4118 }
4119 die = dwarf_alloc_die ();
4120 die->offset = offset;
4121 die->tag = abbrev->tag;
4122 die->abbrev = abbrev_number;
4123 die->type = NULL;
4124
4125 die->num_attrs = abbrev->num_attrs;
4126 die->attrs = (struct attribute *)
4127 xmalloc (die->num_attrs * sizeof (struct attribute));
4128
4129 for (i = 0; i < abbrev->num_attrs; ++i)
4130 {
4131 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
4132 abfd, info_ptr, cu);
4133 }
4134
4135 *diep = die;
4136 *has_children = abbrev->has_children;
4137 return info_ptr;
4138 }
4139
4140 /* Read an attribute value described by an attribute form. */
4141
4142 static char *
4143 read_attribute_value (struct attribute *attr, unsigned form,
4144 bfd *abfd, char *info_ptr,
4145 struct dwarf2_cu *cu)
4146 {
4147 struct comp_unit_head *cu_header = &cu->header;
4148 unsigned int bytes_read;
4149 struct dwarf_block *blk;
4150
4151 attr->form = form;
4152 switch (form)
4153 {
4154 case DW_FORM_addr:
4155 case DW_FORM_ref_addr:
4156 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
4157 info_ptr += bytes_read;
4158 break;
4159 case DW_FORM_block2:
4160 blk = dwarf_alloc_block ();
4161 blk->size = read_2_bytes (abfd, info_ptr);
4162 info_ptr += 2;
4163 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4164 info_ptr += blk->size;
4165 DW_BLOCK (attr) = blk;
4166 break;
4167 case DW_FORM_block4:
4168 blk = dwarf_alloc_block ();
4169 blk->size = read_4_bytes (abfd, info_ptr);
4170 info_ptr += 4;
4171 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4172 info_ptr += blk->size;
4173 DW_BLOCK (attr) = blk;
4174 break;
4175 case DW_FORM_data2:
4176 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
4177 info_ptr += 2;
4178 break;
4179 case DW_FORM_data4:
4180 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
4181 info_ptr += 4;
4182 break;
4183 case DW_FORM_data8:
4184 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
4185 info_ptr += 8;
4186 break;
4187 case DW_FORM_string:
4188 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
4189 info_ptr += bytes_read;
4190 break;
4191 case DW_FORM_strp:
4192 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
4193 &bytes_read);
4194 info_ptr += bytes_read;
4195 break;
4196 case DW_FORM_block:
4197 blk = dwarf_alloc_block ();
4198 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4199 info_ptr += bytes_read;
4200 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4201 info_ptr += blk->size;
4202 DW_BLOCK (attr) = blk;
4203 break;
4204 case DW_FORM_block1:
4205 blk = dwarf_alloc_block ();
4206 blk->size = read_1_byte (abfd, info_ptr);
4207 info_ptr += 1;
4208 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
4209 info_ptr += blk->size;
4210 DW_BLOCK (attr) = blk;
4211 break;
4212 case DW_FORM_data1:
4213 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
4214 info_ptr += 1;
4215 break;
4216 case DW_FORM_flag:
4217 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
4218 info_ptr += 1;
4219 break;
4220 case DW_FORM_sdata:
4221 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
4222 info_ptr += bytes_read;
4223 break;
4224 case DW_FORM_udata:
4225 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4226 info_ptr += bytes_read;
4227 break;
4228 case DW_FORM_ref1:
4229 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
4230 info_ptr += 1;
4231 break;
4232 case DW_FORM_ref2:
4233 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
4234 info_ptr += 2;
4235 break;
4236 case DW_FORM_ref4:
4237 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
4238 info_ptr += 4;
4239 break;
4240 case DW_FORM_ref8:
4241 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
4242 info_ptr += 8;
4243 break;
4244 case DW_FORM_ref_udata:
4245 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4246 info_ptr += bytes_read;
4247 break;
4248 case DW_FORM_indirect:
4249 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4250 info_ptr += bytes_read;
4251 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
4252 break;
4253 default:
4254 error ("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]",
4255 dwarf_form_name (form),
4256 bfd_get_filename (abfd));
4257 }
4258 return info_ptr;
4259 }
4260
4261 /* Read an attribute described by an abbreviated attribute. */
4262
4263 static char *
4264 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
4265 bfd *abfd, char *info_ptr, struct dwarf2_cu *cu)
4266 {
4267 attr->name = abbrev->name;
4268 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
4269 }
4270
4271 /* read dwarf information from a buffer */
4272
4273 static unsigned int
4274 read_1_byte (bfd *abfd, char *buf)
4275 {
4276 return bfd_get_8 (abfd, (bfd_byte *) buf);
4277 }
4278
4279 static int
4280 read_1_signed_byte (bfd *abfd, char *buf)
4281 {
4282 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
4283 }
4284
4285 static unsigned int
4286 read_2_bytes (bfd *abfd, char *buf)
4287 {
4288 return bfd_get_16 (abfd, (bfd_byte *) buf);
4289 }
4290
4291 static int
4292 read_2_signed_bytes (bfd *abfd, char *buf)
4293 {
4294 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
4295 }
4296
4297 static unsigned int
4298 read_4_bytes (bfd *abfd, char *buf)
4299 {
4300 return bfd_get_32 (abfd, (bfd_byte *) buf);
4301 }
4302
4303 static int
4304 read_4_signed_bytes (bfd *abfd, char *buf)
4305 {
4306 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
4307 }
4308
4309 static unsigned long
4310 read_8_bytes (bfd *abfd, char *buf)
4311 {
4312 return bfd_get_64 (abfd, (bfd_byte *) buf);
4313 }
4314
4315 static CORE_ADDR
4316 read_address (bfd *abfd, char *buf, struct dwarf2_cu *cu, int *bytes_read)
4317 {
4318 struct comp_unit_head *cu_header = &cu->header;
4319 CORE_ADDR retval = 0;
4320
4321 if (cu_header->signed_addr_p)
4322 {
4323 switch (cu_header->addr_size)
4324 {
4325 case 2:
4326 retval = bfd_get_signed_16 (abfd, (bfd_byte *) buf);
4327 break;
4328 case 4:
4329 retval = bfd_get_signed_32 (abfd, (bfd_byte *) buf);
4330 break;
4331 case 8:
4332 retval = bfd_get_signed_64 (abfd, (bfd_byte *) buf);
4333 break;
4334 default:
4335 internal_error (__FILE__, __LINE__,
4336 "read_address: bad switch, signed [in module %s]",
4337 bfd_get_filename (abfd));
4338 }
4339 }
4340 else
4341 {
4342 switch (cu_header->addr_size)
4343 {
4344 case 2:
4345 retval = bfd_get_16 (abfd, (bfd_byte *) buf);
4346 break;
4347 case 4:
4348 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
4349 break;
4350 case 8:
4351 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
4352 break;
4353 default:
4354 internal_error (__FILE__, __LINE__,
4355 "read_address: bad switch, unsigned [in module %s]",
4356 bfd_get_filename (abfd));
4357 }
4358 }
4359
4360 *bytes_read = cu_header->addr_size;
4361 return retval;
4362 }
4363
4364 /* Read the initial length from a section. The (draft) DWARF 3
4365 specification allows the initial length to take up either 4 bytes
4366 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
4367 bytes describe the length and all offsets will be 8 bytes in length
4368 instead of 4.
4369
4370 An older, non-standard 64-bit format is also handled by this
4371 function. The older format in question stores the initial length
4372 as an 8-byte quantity without an escape value. Lengths greater
4373 than 2^32 aren't very common which means that the initial 4 bytes
4374 is almost always zero. Since a length value of zero doesn't make
4375 sense for the 32-bit format, this initial zero can be considered to
4376 be an escape value which indicates the presence of the older 64-bit
4377 format. As written, the code can't detect (old format) lengths
4378 greater than 4GB. If it becomes necessary to handle lengths somewhat
4379 larger than 4GB, we could allow other small values (such as the
4380 non-sensical values of 1, 2, and 3) to also be used as escape values
4381 indicating the presence of the old format.
4382
4383 The value returned via bytes_read should be used to increment
4384 the relevant pointer after calling read_initial_length().
4385
4386 As a side effect, this function sets the fields initial_length_size
4387 and offset_size in cu_header to the values appropriate for the
4388 length field. (The format of the initial length field determines
4389 the width of file offsets to be fetched later with fetch_offset().)
4390
4391 [ Note: read_initial_length() and read_offset() are based on the
4392 document entitled "DWARF Debugging Information Format", revision
4393 3, draft 8, dated November 19, 2001. This document was obtained
4394 from:
4395
4396 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
4397
4398 This document is only a draft and is subject to change. (So beware.)
4399
4400 Details regarding the older, non-standard 64-bit format were
4401 determined empirically by examining 64-bit ELF files produced
4402 by the SGI toolchain on an IRIX 6.5 machine.
4403
4404 - Kevin, July 16, 2002
4405 ] */
4406
4407 static LONGEST
4408 read_initial_length (bfd *abfd, char *buf, struct comp_unit_head *cu_header,
4409 int *bytes_read)
4410 {
4411 LONGEST retval = 0;
4412
4413 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
4414
4415 if (retval == 0xffffffff)
4416 {
4417 retval = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
4418 *bytes_read = 12;
4419 if (cu_header != NULL)
4420 {
4421 cu_header->initial_length_size = 12;
4422 cu_header->offset_size = 8;
4423 }
4424 }
4425 else if (retval == 0)
4426 {
4427 /* Handle (non-standard) 64-bit DWARF2 formats such as that used
4428 by IRIX. */
4429 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
4430 *bytes_read = 8;
4431 if (cu_header != NULL)
4432 {
4433 cu_header->initial_length_size = 8;
4434 cu_header->offset_size = 8;
4435 }
4436 }
4437 else
4438 {
4439 *bytes_read = 4;
4440 if (cu_header != NULL)
4441 {
4442 cu_header->initial_length_size = 4;
4443 cu_header->offset_size = 4;
4444 }
4445 }
4446
4447 return retval;
4448 }
4449
4450 /* Read an offset from the data stream. The size of the offset is
4451 given by cu_header->offset_size. */
4452
4453 static LONGEST
4454 read_offset (bfd *abfd, char *buf, const struct comp_unit_head *cu_header,
4455 int *bytes_read)
4456 {
4457 LONGEST retval = 0;
4458
4459 switch (cu_header->offset_size)
4460 {
4461 case 4:
4462 retval = bfd_get_32 (abfd, (bfd_byte *) buf);
4463 *bytes_read = 4;
4464 break;
4465 case 8:
4466 retval = bfd_get_64 (abfd, (bfd_byte *) buf);
4467 *bytes_read = 8;
4468 break;
4469 default:
4470 internal_error (__FILE__, __LINE__,
4471 "read_offset: bad switch [in module %s]",
4472 bfd_get_filename (abfd));
4473 }
4474
4475 return retval;
4476 }
4477
4478 static char *
4479 read_n_bytes (bfd *abfd, char *buf, unsigned int size)
4480 {
4481 /* If the size of a host char is 8 bits, we can return a pointer
4482 to the buffer, otherwise we have to copy the data to a buffer
4483 allocated on the temporary obstack. */
4484 gdb_assert (HOST_CHAR_BIT == 8);
4485 return buf;
4486 }
4487
4488 static char *
4489 read_string (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
4490 {
4491 /* If the size of a host char is 8 bits, we can return a pointer
4492 to the string, otherwise we have to copy the string to a buffer
4493 allocated on the temporary obstack. */
4494 gdb_assert (HOST_CHAR_BIT == 8);
4495 if (*buf == '\0')
4496 {
4497 *bytes_read_ptr = 1;
4498 return NULL;
4499 }
4500 *bytes_read_ptr = strlen (buf) + 1;
4501 return buf;
4502 }
4503
4504 static char *
4505 read_indirect_string (bfd *abfd, char *buf,
4506 const struct comp_unit_head *cu_header,
4507 unsigned int *bytes_read_ptr)
4508 {
4509 LONGEST str_offset = read_offset (abfd, buf, cu_header,
4510 (int *) bytes_read_ptr);
4511
4512 if (dwarf_str_buffer == NULL)
4513 {
4514 error ("DW_FORM_strp used without .debug_str section [in module %s]",
4515 bfd_get_filename (abfd));
4516 return NULL;
4517 }
4518 if (str_offset >= dwarf_str_size)
4519 {
4520 error ("DW_FORM_strp pointing outside of .debug_str section [in module %s]",
4521 bfd_get_filename (abfd));
4522 return NULL;
4523 }
4524 gdb_assert (HOST_CHAR_BIT == 8);
4525 if (dwarf_str_buffer[str_offset] == '\0')
4526 return NULL;
4527 return dwarf_str_buffer + str_offset;
4528 }
4529
4530 static unsigned long
4531 read_unsigned_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
4532 {
4533 unsigned long result;
4534 unsigned int num_read;
4535 int i, shift;
4536 unsigned char byte;
4537
4538 result = 0;
4539 shift = 0;
4540 num_read = 0;
4541 i = 0;
4542 while (1)
4543 {
4544 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
4545 buf++;
4546 num_read++;
4547 result |= ((unsigned long)(byte & 127) << shift);
4548 if ((byte & 128) == 0)
4549 {
4550 break;
4551 }
4552 shift += 7;
4553 }
4554 *bytes_read_ptr = num_read;
4555 return result;
4556 }
4557
4558 static long
4559 read_signed_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
4560 {
4561 long result;
4562 int i, shift, size, num_read;
4563 unsigned char byte;
4564
4565 result = 0;
4566 shift = 0;
4567 size = 32;
4568 num_read = 0;
4569 i = 0;
4570 while (1)
4571 {
4572 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
4573 buf++;
4574 num_read++;
4575 result |= ((long)(byte & 127) << shift);
4576 shift += 7;
4577 if ((byte & 128) == 0)
4578 {
4579 break;
4580 }
4581 }
4582 if ((shift < size) && (byte & 0x40))
4583 {
4584 result |= -(1 << shift);
4585 }
4586 *bytes_read_ptr = num_read;
4587 return result;
4588 }
4589
4590 static void
4591 set_cu_language (unsigned int lang)
4592 {
4593 switch (lang)
4594 {
4595 case DW_LANG_C89:
4596 case DW_LANG_C:
4597 cu_language = language_c;
4598 break;
4599 case DW_LANG_C_plus_plus:
4600 cu_language = language_cplus;
4601 break;
4602 case DW_LANG_Fortran77:
4603 case DW_LANG_Fortran90:
4604 case DW_LANG_Fortran95:
4605 cu_language = language_fortran;
4606 break;
4607 case DW_LANG_Mips_Assembler:
4608 cu_language = language_asm;
4609 break;
4610 case DW_LANG_Java:
4611 cu_language = language_java;
4612 break;
4613 case DW_LANG_Ada83:
4614 case DW_LANG_Ada95:
4615 case DW_LANG_Cobol74:
4616 case DW_LANG_Cobol85:
4617 case DW_LANG_Pascal83:
4618 case DW_LANG_Modula2:
4619 default:
4620 cu_language = language_minimal;
4621 break;
4622 }
4623 cu_language_defn = language_def (cu_language);
4624 }
4625
4626 /* Return the named attribute or NULL if not there. */
4627
4628 static struct attribute *
4629 dwarf_attr (struct die_info *die, unsigned int name)
4630 {
4631 unsigned int i;
4632 struct attribute *spec = NULL;
4633
4634 for (i = 0; i < die->num_attrs; ++i)
4635 {
4636 if (die->attrs[i].name == name)
4637 {
4638 return &die->attrs[i];
4639 }
4640 if (die->attrs[i].name == DW_AT_specification
4641 || die->attrs[i].name == DW_AT_abstract_origin)
4642 spec = &die->attrs[i];
4643 }
4644 if (spec)
4645 {
4646 struct die_info *ref_die =
4647 follow_die_ref (dwarf2_get_ref_die_offset (spec));
4648
4649 if (ref_die)
4650 return dwarf_attr (ref_die, name);
4651 }
4652
4653 return NULL;
4654 }
4655
4656 static int
4657 die_is_declaration (struct die_info *die)
4658 {
4659 return (dwarf_attr (die, DW_AT_declaration)
4660 && ! dwarf_attr (die, DW_AT_specification));
4661 }
4662
4663
4664 /* Free the line_header structure *LH, and any arrays and strings it
4665 refers to. */
4666 static void
4667 free_line_header (struct line_header *lh)
4668 {
4669 if (lh->standard_opcode_lengths)
4670 xfree (lh->standard_opcode_lengths);
4671
4672 /* Remember that all the lh->file_names[i].name pointers are
4673 pointers into debug_line_buffer, and don't need to be freed. */
4674 if (lh->file_names)
4675 xfree (lh->file_names);
4676
4677 /* Similarly for the include directory names. */
4678 if (lh->include_dirs)
4679 xfree (lh->include_dirs);
4680
4681 xfree (lh);
4682 }
4683
4684
4685 /* Add an entry to LH's include directory table. */
4686 static void
4687 add_include_dir (struct line_header *lh, char *include_dir)
4688 {
4689 /* Grow the array if necessary. */
4690 if (lh->include_dirs_size == 0)
4691 {
4692 lh->include_dirs_size = 1; /* for testing */
4693 lh->include_dirs = xmalloc (lh->include_dirs_size
4694 * sizeof (*lh->include_dirs));
4695 }
4696 else if (lh->num_include_dirs >= lh->include_dirs_size)
4697 {
4698 lh->include_dirs_size *= 2;
4699 lh->include_dirs = xrealloc (lh->include_dirs,
4700 (lh->include_dirs_size
4701 * sizeof (*lh->include_dirs)));
4702 }
4703
4704 lh->include_dirs[lh->num_include_dirs++] = include_dir;
4705 }
4706
4707
4708 /* Add an entry to LH's file name table. */
4709 static void
4710 add_file_name (struct line_header *lh,
4711 char *name,
4712 unsigned int dir_index,
4713 unsigned int mod_time,
4714 unsigned int length)
4715 {
4716 struct file_entry *fe;
4717
4718 /* Grow the array if necessary. */
4719 if (lh->file_names_size == 0)
4720 {
4721 lh->file_names_size = 1; /* for testing */
4722 lh->file_names = xmalloc (lh->file_names_size
4723 * sizeof (*lh->file_names));
4724 }
4725 else if (lh->num_file_names >= lh->file_names_size)
4726 {
4727 lh->file_names_size *= 2;
4728 lh->file_names = xrealloc (lh->file_names,
4729 (lh->file_names_size
4730 * sizeof (*lh->file_names)));
4731 }
4732
4733 fe = &lh->file_names[lh->num_file_names++];
4734 fe->name = name;
4735 fe->dir_index = dir_index;
4736 fe->mod_time = mod_time;
4737 fe->length = length;
4738 }
4739
4740
4741 /* Read the statement program header starting at OFFSET in
4742 dwarf_line_buffer, according to the endianness of ABFD. Return a
4743 pointer to a struct line_header, allocated using xmalloc.
4744
4745 NOTE: the strings in the include directory and file name tables of
4746 the returned object point into debug_line_buffer, and must not be
4747 freed. */
4748 static struct line_header *
4749 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
4750 struct dwarf2_cu *cu)
4751 {
4752 struct cleanup *back_to;
4753 struct line_header *lh;
4754 char *line_ptr;
4755 int bytes_read;
4756 int i;
4757 char *cur_dir, *cur_file;
4758
4759 if (dwarf_line_buffer == NULL)
4760 {
4761 complaint (&symfile_complaints, "missing .debug_line section");
4762 return 0;
4763 }
4764
4765 /* Make sure that at least there's room for the total_length field. That
4766 could be 12 bytes long, but we're just going to fudge that. */
4767 if (offset + 4 >= dwarf_line_size)
4768 {
4769 dwarf2_statement_list_fits_in_line_number_section_complaint ();
4770 return 0;
4771 }
4772
4773 lh = xmalloc (sizeof (*lh));
4774 memset (lh, 0, sizeof (*lh));
4775 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
4776 (void *) lh);
4777
4778 line_ptr = dwarf_line_buffer + offset;
4779
4780 /* read in the header */
4781 lh->total_length = read_initial_length (abfd, line_ptr, NULL, &bytes_read);
4782 line_ptr += bytes_read;
4783 if (line_ptr + lh->total_length > dwarf_line_buffer + dwarf_line_size)
4784 {
4785 dwarf2_statement_list_fits_in_line_number_section_complaint ();
4786 return 0;
4787 }
4788 lh->statement_program_end = line_ptr + lh->total_length;
4789 lh->version = read_2_bytes (abfd, line_ptr);
4790 line_ptr += 2;
4791 lh->header_length = read_offset (abfd, line_ptr, &cu->header, &bytes_read);
4792 line_ptr += bytes_read;
4793 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
4794 line_ptr += 1;
4795 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
4796 line_ptr += 1;
4797 lh->line_base = read_1_signed_byte (abfd, line_ptr);
4798 line_ptr += 1;
4799 lh->line_range = read_1_byte (abfd, line_ptr);
4800 line_ptr += 1;
4801 lh->opcode_base = read_1_byte (abfd, line_ptr);
4802 line_ptr += 1;
4803 lh->standard_opcode_lengths
4804 = (unsigned char *) xmalloc (lh->opcode_base * sizeof (unsigned char));
4805
4806 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
4807 for (i = 1; i < lh->opcode_base; ++i)
4808 {
4809 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
4810 line_ptr += 1;
4811 }
4812
4813 /* Read directory table */
4814 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
4815 {
4816 line_ptr += bytes_read;
4817 add_include_dir (lh, cur_dir);
4818 }
4819 line_ptr += bytes_read;
4820
4821 /* Read file name table */
4822 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
4823 {
4824 unsigned int dir_index, mod_time, length;
4825
4826 line_ptr += bytes_read;
4827 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
4828 line_ptr += bytes_read;
4829 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
4830 line_ptr += bytes_read;
4831 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
4832 line_ptr += bytes_read;
4833
4834 add_file_name (lh, cur_file, dir_index, mod_time, length);
4835 }
4836 line_ptr += bytes_read;
4837 lh->statement_program_start = line_ptr;
4838
4839 if (line_ptr > dwarf_line_buffer + dwarf_line_size)
4840 complaint (&symfile_complaints,
4841 "line number info header doesn't fit in `.debug_line' section");
4842
4843 discard_cleanups (back_to);
4844 return lh;
4845 }
4846
4847 /* This function exists to work around a bug in certain compilers
4848 (particularly GCC 2.95), in which the first line number marker of a
4849 function does not show up until after the prologue, right before
4850 the second line number marker. This function shifts ADDRESS down
4851 to the beginning of the function if necessary, and is called on
4852 addresses passed to record_line. */
4853
4854 static CORE_ADDR
4855 check_cu_functions (CORE_ADDR address)
4856 {
4857 struct function_range *fn;
4858
4859 /* Find the function_range containing address. */
4860 if (!cu_first_fn)
4861 return address;
4862
4863 if (!cu_cached_fn)
4864 cu_cached_fn = cu_first_fn;
4865
4866 fn = cu_cached_fn;
4867 while (fn)
4868 if (fn->lowpc <= address && fn->highpc > address)
4869 goto found;
4870 else
4871 fn = fn->next;
4872
4873 fn = cu_first_fn;
4874 while (fn && fn != cu_cached_fn)
4875 if (fn->lowpc <= address && fn->highpc > address)
4876 goto found;
4877 else
4878 fn = fn->next;
4879
4880 return address;
4881
4882 found:
4883 if (fn->seen_line)
4884 return address;
4885 if (address != fn->lowpc)
4886 complaint (&symfile_complaints,
4887 "misplaced first line number at 0x%lx for '%s'",
4888 (unsigned long) address, fn->name);
4889 fn->seen_line = 1;
4890 return fn->lowpc;
4891 }
4892
4893 /* Decode the line number information for the compilation unit whose
4894 line number info is at OFFSET in the .debug_line section.
4895 The compilation directory of the file is passed in COMP_DIR. */
4896
4897 static void
4898 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
4899 struct dwarf2_cu *cu)
4900 {
4901 char *line_ptr;
4902 char *line_end;
4903 unsigned int bytes_read;
4904 unsigned char op_code, extended_op, adj_opcode;
4905
4906 line_ptr = lh->statement_program_start;
4907 line_end = lh->statement_program_end;
4908
4909 /* Read the statement sequences until there's nothing left. */
4910 while (line_ptr < line_end)
4911 {
4912 /* state machine registers */
4913 CORE_ADDR address = 0;
4914 unsigned int file = 1;
4915 unsigned int line = 1;
4916 unsigned int column = 0;
4917 int is_stmt = lh->default_is_stmt;
4918 int basic_block = 0;
4919 int end_sequence = 0;
4920
4921 /* Start a subfile for the current file of the state machine. */
4922 if (lh->num_file_names >= file)
4923 {
4924 /* lh->include_dirs and lh->file_names are 0-based, but the
4925 directory and file name numbers in the statement program
4926 are 1-based. */
4927 struct file_entry *fe = &lh->file_names[file - 1];
4928 char *dir;
4929 if (fe->dir_index)
4930 dir = lh->include_dirs[fe->dir_index - 1];
4931 else
4932 dir = comp_dir;
4933 dwarf2_start_subfile (fe->name, dir);
4934 }
4935
4936 /* Decode the table. */
4937 while (!end_sequence)
4938 {
4939 op_code = read_1_byte (abfd, line_ptr);
4940 line_ptr += 1;
4941
4942 if (op_code >= lh->opcode_base)
4943 { /* Special operand. */
4944 adj_opcode = op_code - lh->opcode_base;
4945 address += (adj_opcode / lh->line_range)
4946 * lh->minimum_instruction_length;
4947 line += lh->line_base + (adj_opcode % lh->line_range);
4948 /* append row to matrix using current values */
4949 record_line (current_subfile, line,
4950 check_cu_functions (address));
4951 basic_block = 1;
4952 }
4953 else switch (op_code)
4954 {
4955 case DW_LNS_extended_op:
4956 line_ptr += 1; /* ignore length */
4957 extended_op = read_1_byte (abfd, line_ptr);
4958 line_ptr += 1;
4959 switch (extended_op)
4960 {
4961 case DW_LNE_end_sequence:
4962 end_sequence = 1;
4963 record_line (current_subfile, 0, address);
4964 break;
4965 case DW_LNE_set_address:
4966 address = read_address (abfd, line_ptr, cu, &bytes_read);
4967 line_ptr += bytes_read;
4968 address += baseaddr;
4969 break;
4970 case DW_LNE_define_file:
4971 {
4972 char *cur_file;
4973 unsigned int dir_index, mod_time, length;
4974
4975 cur_file = read_string (abfd, line_ptr, &bytes_read);
4976 line_ptr += bytes_read;
4977 dir_index =
4978 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
4979 line_ptr += bytes_read;
4980 mod_time =
4981 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
4982 line_ptr += bytes_read;
4983 length =
4984 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
4985 line_ptr += bytes_read;
4986 add_file_name (lh, cur_file, dir_index, mod_time, length);
4987 }
4988 break;
4989 default:
4990 complaint (&symfile_complaints,
4991 "mangled .debug_line section");
4992 return;
4993 }
4994 break;
4995 case DW_LNS_copy:
4996 record_line (current_subfile, line,
4997 check_cu_functions (address));
4998 basic_block = 0;
4999 break;
5000 case DW_LNS_advance_pc:
5001 address += lh->minimum_instruction_length
5002 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5003 line_ptr += bytes_read;
5004 break;
5005 case DW_LNS_advance_line:
5006 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
5007 line_ptr += bytes_read;
5008 break;
5009 case DW_LNS_set_file:
5010 {
5011 /* lh->include_dirs and lh->file_names are 0-based,
5012 but the directory and file name numbers in the
5013 statement program are 1-based. */
5014 struct file_entry *fe;
5015 char *dir;
5016 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5017 line_ptr += bytes_read;
5018 fe = &lh->file_names[file - 1];
5019 if (fe->dir_index)
5020 dir = lh->include_dirs[fe->dir_index - 1];
5021 else
5022 dir = comp_dir;
5023 dwarf2_start_subfile (fe->name, dir);
5024 }
5025 break;
5026 case DW_LNS_set_column:
5027 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5028 line_ptr += bytes_read;
5029 break;
5030 case DW_LNS_negate_stmt:
5031 is_stmt = (!is_stmt);
5032 break;
5033 case DW_LNS_set_basic_block:
5034 basic_block = 1;
5035 break;
5036 /* Add to the address register of the state machine the
5037 address increment value corresponding to special opcode
5038 255. Ie, this value is scaled by the minimum instruction
5039 length since special opcode 255 would have scaled the
5040 the increment. */
5041 case DW_LNS_const_add_pc:
5042 address += (lh->minimum_instruction_length
5043 * ((255 - lh->opcode_base) / lh->line_range));
5044 break;
5045 case DW_LNS_fixed_advance_pc:
5046 address += read_2_bytes (abfd, line_ptr);
5047 line_ptr += 2;
5048 break;
5049 default:
5050 { /* Unknown standard opcode, ignore it. */
5051 int i;
5052 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
5053 {
5054 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
5055 line_ptr += bytes_read;
5056 }
5057 }
5058 }
5059 }
5060 }
5061 }
5062
5063 /* Start a subfile for DWARF. FILENAME is the name of the file and
5064 DIRNAME the name of the source directory which contains FILENAME
5065 or NULL if not known.
5066 This routine tries to keep line numbers from identical absolute and
5067 relative file names in a common subfile.
5068
5069 Using the `list' example from the GDB testsuite, which resides in
5070 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
5071 of /srcdir/list0.c yields the following debugging information for list0.c:
5072
5073 DW_AT_name: /srcdir/list0.c
5074 DW_AT_comp_dir: /compdir
5075 files.files[0].name: list0.h
5076 files.files[0].dir: /srcdir
5077 files.files[1].name: list0.c
5078 files.files[1].dir: /srcdir
5079
5080 The line number information for list0.c has to end up in a single
5081 subfile, so that `break /srcdir/list0.c:1' works as expected. */
5082
5083 static void
5084 dwarf2_start_subfile (char *filename, char *dirname)
5085 {
5086 /* If the filename isn't absolute, try to match an existing subfile
5087 with the full pathname. */
5088
5089 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
5090 {
5091 struct subfile *subfile;
5092 char *fullname = concat (dirname, "/", filename, NULL);
5093
5094 for (subfile = subfiles; subfile; subfile = subfile->next)
5095 {
5096 if (FILENAME_CMP (subfile->name, fullname) == 0)
5097 {
5098 current_subfile = subfile;
5099 xfree (fullname);
5100 return;
5101 }
5102 }
5103 xfree (fullname);
5104 }
5105 start_subfile (filename, dirname);
5106 }
5107
5108 static void
5109 var_decode_location (struct attribute *attr, struct symbol *sym,
5110 struct dwarf2_cu *cu)
5111 {
5112 struct objfile *objfile = cu->objfile;
5113 struct comp_unit_head *cu_header = &cu->header;
5114
5115 /* NOTE drow/2003-01-30: There used to be a comment and some special
5116 code here to turn a symbol with DW_AT_external and a
5117 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
5118 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
5119 with some versions of binutils) where shared libraries could have
5120 relocations against symbols in their debug information - the
5121 minimal symbol would have the right address, but the debug info
5122 would not. It's no longer necessary, because we will explicitly
5123 apply relocations when we read in the debug information now. */
5124
5125 /* A DW_AT_location attribute with no contents indicates that a
5126 variable has been optimized away. */
5127 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
5128 {
5129 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
5130 return;
5131 }
5132
5133 /* Handle one degenerate form of location expression specially, to
5134 preserve GDB's previous behavior when section offsets are
5135 specified. If this is just a DW_OP_addr then mark this symbol
5136 as LOC_STATIC. */
5137
5138 if (attr_form_is_block (attr)
5139 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
5140 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
5141 {
5142 int dummy;
5143
5144 SYMBOL_VALUE_ADDRESS (sym) =
5145 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
5146 fixup_symbol_section (sym, objfile);
5147 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
5148 SYMBOL_SECTION (sym));
5149 SYMBOL_CLASS (sym) = LOC_STATIC;
5150 return;
5151 }
5152
5153 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
5154 expression evaluator, and use LOC_COMPUTED only when necessary
5155 (i.e. when the value of a register or memory location is
5156 referenced, or a thread-local block, etc.). Then again, it might
5157 not be worthwhile. I'm assuming that it isn't unless performance
5158 or memory numbers show me otherwise. */
5159
5160 dwarf2_symbol_mark_computed (attr, sym, cu);
5161 SYMBOL_CLASS (sym) = LOC_COMPUTED;
5162 }
5163
5164 /* Given a pointer to a DWARF information entry, figure out if we need
5165 to make a symbol table entry for it, and if so, create a new entry
5166 and return a pointer to it.
5167 If TYPE is NULL, determine symbol type from the die, otherwise
5168 used the passed type. */
5169
5170 static struct symbol *
5171 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
5172 {
5173 struct objfile *objfile = cu->objfile;
5174 struct symbol *sym = NULL;
5175 char *name;
5176 struct attribute *attr = NULL;
5177 struct attribute *attr2 = NULL;
5178
5179 if (die->tag != DW_TAG_namespace)
5180 name = dwarf2_linkage_name (die);
5181 else
5182 name = TYPE_NAME (type);
5183
5184 if (name)
5185 {
5186 sym = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
5187 sizeof (struct symbol));
5188 OBJSTAT (objfile, n_syms++);
5189 memset (sym, 0, sizeof (struct symbol));
5190
5191 /* Cache this symbol's name and the name's demangled form (if any). */
5192 SYMBOL_LANGUAGE (sym) = cu_language;
5193 SYMBOL_SET_NAMES (sym, name, strlen (name), objfile);
5194
5195 /* Default assumptions.
5196 Use the passed type or decode it from the die. */
5197 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
5198 SYMBOL_CLASS (sym) = LOC_STATIC;
5199 if (type != NULL)
5200 SYMBOL_TYPE (sym) = type;
5201 else
5202 SYMBOL_TYPE (sym) = die_type (die, cu);
5203 attr = dwarf_attr (die, DW_AT_decl_line);
5204 if (attr)
5205 {
5206 SYMBOL_LINE (sym) = DW_UNSND (attr);
5207 }
5208 switch (die->tag)
5209 {
5210 case DW_TAG_label:
5211 attr = dwarf_attr (die, DW_AT_low_pc);
5212 if (attr)
5213 {
5214 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
5215 }
5216 SYMBOL_CLASS (sym) = LOC_LABEL;
5217 break;
5218 case DW_TAG_subprogram:
5219 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
5220 finish_block. */
5221 SYMBOL_CLASS (sym) = LOC_BLOCK;
5222 attr2 = dwarf_attr (die, DW_AT_external);
5223 if (attr2 && (DW_UNSND (attr2) != 0))
5224 {
5225 add_symbol_to_list (sym, &global_symbols);
5226 }
5227 else
5228 {
5229 add_symbol_to_list (sym, list_in_scope);
5230 }
5231 break;
5232 case DW_TAG_variable:
5233 /* Compilation with minimal debug info may result in variables
5234 with missing type entries. Change the misleading `void' type
5235 to something sensible. */
5236 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
5237 SYMBOL_TYPE (sym) = init_type (TYPE_CODE_INT,
5238 TARGET_INT_BIT / HOST_CHAR_BIT, 0,
5239 "<variable, no debug info>",
5240 objfile);
5241 attr = dwarf_attr (die, DW_AT_const_value);
5242 if (attr)
5243 {
5244 dwarf2_const_value (attr, sym, cu);
5245 attr2 = dwarf_attr (die, DW_AT_external);
5246 if (attr2 && (DW_UNSND (attr2) != 0))
5247 add_symbol_to_list (sym, &global_symbols);
5248 else
5249 add_symbol_to_list (sym, list_in_scope);
5250 break;
5251 }
5252 attr = dwarf_attr (die, DW_AT_location);
5253 if (attr)
5254 {
5255 var_decode_location (attr, sym, cu);
5256 attr2 = dwarf_attr (die, DW_AT_external);
5257 if (attr2 && (DW_UNSND (attr2) != 0))
5258 add_symbol_to_list (sym, &global_symbols);
5259 else
5260 add_symbol_to_list (sym, list_in_scope);
5261 }
5262 else
5263 {
5264 /* We do not know the address of this symbol.
5265 If it is an external symbol and we have type information
5266 for it, enter the symbol as a LOC_UNRESOLVED symbol.
5267 The address of the variable will then be determined from
5268 the minimal symbol table whenever the variable is
5269 referenced. */
5270 attr2 = dwarf_attr (die, DW_AT_external);
5271 if (attr2 && (DW_UNSND (attr2) != 0)
5272 && dwarf_attr (die, DW_AT_type) != NULL)
5273 {
5274 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
5275 add_symbol_to_list (sym, &global_symbols);
5276 }
5277 }
5278 break;
5279 case DW_TAG_formal_parameter:
5280 attr = dwarf_attr (die, DW_AT_location);
5281 if (attr)
5282 {
5283 var_decode_location (attr, sym, cu);
5284 /* FIXME drow/2003-07-31: Is LOC_COMPUTED_ARG necessary? */
5285 if (SYMBOL_CLASS (sym) == LOC_COMPUTED)
5286 SYMBOL_CLASS (sym) = LOC_COMPUTED_ARG;
5287 }
5288 attr = dwarf_attr (die, DW_AT_const_value);
5289 if (attr)
5290 {
5291 dwarf2_const_value (attr, sym, cu);
5292 }
5293 add_symbol_to_list (sym, list_in_scope);
5294 break;
5295 case DW_TAG_unspecified_parameters:
5296 /* From varargs functions; gdb doesn't seem to have any
5297 interest in this information, so just ignore it for now.
5298 (FIXME?) */
5299 break;
5300 case DW_TAG_class_type:
5301 case DW_TAG_structure_type:
5302 case DW_TAG_union_type:
5303 case DW_TAG_enumeration_type:
5304 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5305 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5306 add_symbol_to_list (sym, list_in_scope);
5307
5308 /* The semantics of C++ state that "struct foo { ... }" also
5309 defines a typedef for "foo". Synthesize a typedef symbol so
5310 that "ptype foo" works as expected. */
5311 if (cu_language == language_cplus)
5312 {
5313 struct symbol *typedef_sym = (struct symbol *)
5314 obstack_alloc (&objfile->symbol_obstack,
5315 sizeof (struct symbol));
5316 *typedef_sym = *sym;
5317 SYMBOL_DOMAIN (typedef_sym) = VAR_DOMAIN;
5318 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
5319 TYPE_NAME (SYMBOL_TYPE (sym)) =
5320 obsavestring (DEPRECATED_SYMBOL_NAME (sym),
5321 strlen (DEPRECATED_SYMBOL_NAME (sym)),
5322 &objfile->type_obstack);
5323 add_symbol_to_list (typedef_sym, list_in_scope);
5324 }
5325 break;
5326 case DW_TAG_typedef:
5327 case DW_TAG_base_type:
5328 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5329 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
5330 add_symbol_to_list (sym, list_in_scope);
5331 break;
5332 case DW_TAG_enumerator:
5333 attr = dwarf_attr (die, DW_AT_const_value);
5334 if (attr)
5335 {
5336 dwarf2_const_value (attr, sym, cu);
5337 }
5338 add_symbol_to_list (sym, list_in_scope);
5339 break;
5340 case DW_TAG_namespace:
5341 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5342 add_symbol_to_list (sym, &global_symbols);
5343 break;
5344 default:
5345 /* Not a tag we recognize. Hopefully we aren't processing
5346 trash data, but since we must specifically ignore things
5347 we don't recognize, there is nothing else we should do at
5348 this point. */
5349 complaint (&symfile_complaints, "unsupported tag: '%s'",
5350 dwarf_tag_name (die->tag));
5351 break;
5352 }
5353 }
5354 return (sym);
5355 }
5356
5357 /* Copy constant value from an attribute to a symbol. */
5358
5359 static void
5360 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
5361 struct dwarf2_cu *cu)
5362 {
5363 struct objfile *objfile = cu->objfile;
5364 struct comp_unit_head *cu_header = &cu->header;
5365 struct dwarf_block *blk;
5366
5367 switch (attr->form)
5368 {
5369 case DW_FORM_addr:
5370 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
5371 dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
5372 cu_header->addr_size,
5373 TYPE_LENGTH (SYMBOL_TYPE
5374 (sym)));
5375 SYMBOL_VALUE_BYTES (sym) = (char *)
5376 obstack_alloc (&objfile->symbol_obstack, cu_header->addr_size);
5377 /* NOTE: cagney/2003-05-09: In-lined store_address call with
5378 it's body - store_unsigned_integer. */
5379 store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
5380 DW_ADDR (attr));
5381 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
5382 break;
5383 case DW_FORM_block1:
5384 case DW_FORM_block2:
5385 case DW_FORM_block4:
5386 case DW_FORM_block:
5387 blk = DW_BLOCK (attr);
5388 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
5389 dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
5390 blk->size,
5391 TYPE_LENGTH (SYMBOL_TYPE
5392 (sym)));
5393 SYMBOL_VALUE_BYTES (sym) = (char *)
5394 obstack_alloc (&objfile->symbol_obstack, blk->size);
5395 memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
5396 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
5397 break;
5398
5399 /* The DW_AT_const_value attributes are supposed to carry the
5400 symbol's value "represented as it would be on the target
5401 architecture." By the time we get here, it's already been
5402 converted to host endianness, so we just need to sign- or
5403 zero-extend it as appropriate. */
5404 case DW_FORM_data1:
5405 dwarf2_const_value_data (attr, sym, 8);
5406 break;
5407 case DW_FORM_data2:
5408 dwarf2_const_value_data (attr, sym, 16);
5409 break;
5410 case DW_FORM_data4:
5411 dwarf2_const_value_data (attr, sym, 32);
5412 break;
5413 case DW_FORM_data8:
5414 dwarf2_const_value_data (attr, sym, 64);
5415 break;
5416
5417 case DW_FORM_sdata:
5418 SYMBOL_VALUE (sym) = DW_SND (attr);
5419 SYMBOL_CLASS (sym) = LOC_CONST;
5420 break;
5421
5422 case DW_FORM_udata:
5423 SYMBOL_VALUE (sym) = DW_UNSND (attr);
5424 SYMBOL_CLASS (sym) = LOC_CONST;
5425 break;
5426
5427 default:
5428 complaint (&symfile_complaints,
5429 "unsupported const value attribute form: '%s'",
5430 dwarf_form_name (attr->form));
5431 SYMBOL_VALUE (sym) = 0;
5432 SYMBOL_CLASS (sym) = LOC_CONST;
5433 break;
5434 }
5435 }
5436
5437
5438 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
5439 or zero-extend it as appropriate for the symbol's type. */
5440 static void
5441 dwarf2_const_value_data (struct attribute *attr,
5442 struct symbol *sym,
5443 int bits)
5444 {
5445 LONGEST l = DW_UNSND (attr);
5446
5447 if (bits < sizeof (l) * 8)
5448 {
5449 if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
5450 l &= ((LONGEST) 1 << bits) - 1;
5451 else
5452 l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
5453 }
5454
5455 SYMBOL_VALUE (sym) = l;
5456 SYMBOL_CLASS (sym) = LOC_CONST;
5457 }
5458
5459
5460 /* Return the type of the die in question using its DW_AT_type attribute. */
5461
5462 static struct type *
5463 die_type (struct die_info *die, struct dwarf2_cu *cu)
5464 {
5465 struct type *type;
5466 struct attribute *type_attr;
5467 struct die_info *type_die;
5468 unsigned int ref;
5469
5470 type_attr = dwarf_attr (die, DW_AT_type);
5471 if (!type_attr)
5472 {
5473 /* A missing DW_AT_type represents a void type. */
5474 return dwarf2_fundamental_type (cu->objfile, FT_VOID);
5475 }
5476 else
5477 {
5478 ref = dwarf2_get_ref_die_offset (type_attr);
5479 type_die = follow_die_ref (ref);
5480 if (!type_die)
5481 {
5482 error ("Dwarf Error: Cannot find referent at offset %d [in module %s]",
5483 ref, cu->objfile->name);
5484 return NULL;
5485 }
5486 }
5487 type = tag_type_to_type (type_die, cu);
5488 if (!type)
5489 {
5490 dump_die (type_die);
5491 error ("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]",
5492 cu->objfile->name);
5493 }
5494 return type;
5495 }
5496
5497 /* Return the containing type of the die in question using its
5498 DW_AT_containing_type attribute. */
5499
5500 static struct type *
5501 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
5502 {
5503 struct type *type = NULL;
5504 struct attribute *type_attr;
5505 struct die_info *type_die = NULL;
5506 unsigned int ref;
5507
5508 type_attr = dwarf_attr (die, DW_AT_containing_type);
5509 if (type_attr)
5510 {
5511 ref = dwarf2_get_ref_die_offset (type_attr);
5512 type_die = follow_die_ref (ref);
5513 if (!type_die)
5514 {
5515 error ("Dwarf Error: Cannot find referent at offset %d [in module %s]", ref,
5516 cu->objfile->name);
5517 return NULL;
5518 }
5519 type = tag_type_to_type (type_die, cu);
5520 }
5521 if (!type)
5522 {
5523 if (type_die)
5524 dump_die (type_die);
5525 error ("Dwarf Error: Problem turning containing type into gdb type [in module %s]",
5526 cu->objfile->name);
5527 }
5528 return type;
5529 }
5530
5531 #if 0
5532 static struct type *
5533 type_at_offset (unsigned int offset, struct dwarf2_cu *cu)
5534 {
5535 struct die_info *die;
5536 struct type *type;
5537
5538 die = follow_die_ref (offset);
5539 if (!die)
5540 {
5541 error ("Dwarf Error: Cannot find type referent at offset %d.", offset);
5542 return NULL;
5543 }
5544 type = tag_type_to_type (die, cu);
5545 return type;
5546 }
5547 #endif
5548
5549 static struct type *
5550 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
5551 {
5552 if (die->type)
5553 {
5554 return die->type;
5555 }
5556 else
5557 {
5558 read_type_die (die, cu);
5559 if (!die->type)
5560 {
5561 dump_die (die);
5562 error ("Dwarf Error: Cannot find type of die [in module %s]",
5563 cu->objfile->name);
5564 }
5565 return die->type;
5566 }
5567 }
5568
5569 static void
5570 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
5571 {
5572 switch (die->tag)
5573 {
5574 case DW_TAG_class_type:
5575 case DW_TAG_structure_type:
5576 case DW_TAG_union_type:
5577 read_structure_scope (die, cu);
5578 break;
5579 case DW_TAG_enumeration_type:
5580 read_enumeration (die, cu);
5581 break;
5582 case DW_TAG_subprogram:
5583 case DW_TAG_subroutine_type:
5584 read_subroutine_type (die, cu);
5585 break;
5586 case DW_TAG_array_type:
5587 read_array_type (die, cu);
5588 break;
5589 case DW_TAG_pointer_type:
5590 read_tag_pointer_type (die, cu);
5591 break;
5592 case DW_TAG_ptr_to_member_type:
5593 read_tag_ptr_to_member_type (die, cu);
5594 break;
5595 case DW_TAG_reference_type:
5596 read_tag_reference_type (die, cu);
5597 break;
5598 case DW_TAG_const_type:
5599 read_tag_const_type (die, cu);
5600 break;
5601 case DW_TAG_volatile_type:
5602 read_tag_volatile_type (die, cu);
5603 break;
5604 case DW_TAG_string_type:
5605 read_tag_string_type (die, cu);
5606 break;
5607 case DW_TAG_typedef:
5608 read_typedef (die, cu);
5609 break;
5610 case DW_TAG_base_type:
5611 read_base_type (die, cu);
5612 break;
5613 default:
5614 complaint (&symfile_complaints, "unexepected tag in read_type_die: '%s'",
5615 dwarf_tag_name (die->tag));
5616 break;
5617 }
5618 }
5619
5620 static struct type *
5621 dwarf_base_type (int encoding, int size, struct dwarf2_cu *cu)
5622 {
5623 struct objfile *objfile = cu->objfile;
5624
5625 /* FIXME - this should not produce a new (struct type *)
5626 every time. It should cache base types. */
5627 struct type *type;
5628 switch (encoding)
5629 {
5630 case DW_ATE_address:
5631 type = dwarf2_fundamental_type (objfile, FT_VOID);
5632 return type;
5633 case DW_ATE_boolean:
5634 type = dwarf2_fundamental_type (objfile, FT_BOOLEAN);
5635 return type;
5636 case DW_ATE_complex_float:
5637 if (size == 16)
5638 {
5639 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_COMPLEX);
5640 }
5641 else
5642 {
5643 type = dwarf2_fundamental_type (objfile, FT_COMPLEX);
5644 }
5645 return type;
5646 case DW_ATE_float:
5647 if (size == 8)
5648 {
5649 type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT);
5650 }
5651 else
5652 {
5653 type = dwarf2_fundamental_type (objfile, FT_FLOAT);
5654 }
5655 return type;
5656 case DW_ATE_signed:
5657 switch (size)
5658 {
5659 case 1:
5660 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
5661 break;
5662 case 2:
5663 type = dwarf2_fundamental_type (objfile, FT_SIGNED_SHORT);
5664 break;
5665 default:
5666 case 4:
5667 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
5668 break;
5669 }
5670 return type;
5671 case DW_ATE_signed_char:
5672 type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
5673 return type;
5674 case DW_ATE_unsigned:
5675 switch (size)
5676 {
5677 case 1:
5678 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
5679 break;
5680 case 2:
5681 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_SHORT);
5682 break;
5683 default:
5684 case 4:
5685 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_INTEGER);
5686 break;
5687 }
5688 return type;
5689 case DW_ATE_unsigned_char:
5690 type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
5691 return type;
5692 default:
5693 type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
5694 return type;
5695 }
5696 }
5697
5698 #if 0
5699 struct die_info *
5700 copy_die (struct die_info *old_die)
5701 {
5702 struct die_info *new_die;
5703 int i, num_attrs;
5704
5705 new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
5706 memset (new_die, 0, sizeof (struct die_info));
5707
5708 new_die->tag = old_die->tag;
5709 new_die->has_children = old_die->has_children;
5710 new_die->abbrev = old_die->abbrev;
5711 new_die->offset = old_die->offset;
5712 new_die->type = NULL;
5713
5714 num_attrs = old_die->num_attrs;
5715 new_die->num_attrs = num_attrs;
5716 new_die->attrs = (struct attribute *)
5717 xmalloc (num_attrs * sizeof (struct attribute));
5718
5719 for (i = 0; i < old_die->num_attrs; ++i)
5720 {
5721 new_die->attrs[i].name = old_die->attrs[i].name;
5722 new_die->attrs[i].form = old_die->attrs[i].form;
5723 new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
5724 }
5725
5726 new_die->next = NULL;
5727 return new_die;
5728 }
5729 #endif
5730
5731 /* Return sibling of die, NULL if no sibling. */
5732
5733 static struct die_info *
5734 sibling_die (struct die_info *die)
5735 {
5736 return die->sibling;
5737 }
5738
5739 /* Get linkage name of a die, return NULL if not found. */
5740
5741 static char *
5742 dwarf2_linkage_name (struct die_info *die)
5743 {
5744 struct attribute *attr;
5745
5746 attr = dwarf_attr (die, DW_AT_MIPS_linkage_name);
5747 if (attr && DW_STRING (attr))
5748 return DW_STRING (attr);
5749 attr = dwarf_attr (die, DW_AT_name);
5750 if (attr && DW_STRING (attr))
5751 return DW_STRING (attr);
5752 return NULL;
5753 }
5754
5755 /* Get name of a die, return NULL if not found. */
5756
5757 static char *
5758 dwarf2_name (struct die_info *die)
5759 {
5760 struct attribute *attr;
5761
5762 attr = dwarf_attr (die, DW_AT_name);
5763 if (attr && DW_STRING (attr))
5764 return DW_STRING (attr);
5765 return NULL;
5766 }
5767
5768 /* Return the die that this die in an extension of, or NULL if there
5769 is none. */
5770
5771 static struct die_info *
5772 dwarf2_extension (struct die_info *die)
5773 {
5774 struct attribute *attr;
5775 struct die_info *extension_die;
5776 unsigned int ref;
5777
5778 attr = dwarf_attr (die, DW_AT_extension);
5779 if (attr == NULL)
5780 return NULL;
5781
5782 ref = dwarf2_get_ref_die_offset (attr);
5783 extension_die = follow_die_ref (ref);
5784 if (!extension_die)
5785 {
5786 error ("Dwarf Error: Cannot find referent at offset %d.", ref);
5787 }
5788
5789 return extension_die;
5790 }
5791
5792 /* Convert a DIE tag into its string name. */
5793
5794 static char *
5795 dwarf_tag_name (unsigned tag)
5796 {
5797 switch (tag)
5798 {
5799 case DW_TAG_padding:
5800 return "DW_TAG_padding";
5801 case DW_TAG_array_type:
5802 return "DW_TAG_array_type";
5803 case DW_TAG_class_type:
5804 return "DW_TAG_class_type";
5805 case DW_TAG_entry_point:
5806 return "DW_TAG_entry_point";
5807 case DW_TAG_enumeration_type:
5808 return "DW_TAG_enumeration_type";
5809 case DW_TAG_formal_parameter:
5810 return "DW_TAG_formal_parameter";
5811 case DW_TAG_imported_declaration:
5812 return "DW_TAG_imported_declaration";
5813 case DW_TAG_label:
5814 return "DW_TAG_label";
5815 case DW_TAG_lexical_block:
5816 return "DW_TAG_lexical_block";
5817 case DW_TAG_member:
5818 return "DW_TAG_member";
5819 case DW_TAG_pointer_type:
5820 return "DW_TAG_pointer_type";
5821 case DW_TAG_reference_type:
5822 return "DW_TAG_reference_type";
5823 case DW_TAG_compile_unit:
5824 return "DW_TAG_compile_unit";
5825 case DW_TAG_string_type:
5826 return "DW_TAG_string_type";
5827 case DW_TAG_structure_type:
5828 return "DW_TAG_structure_type";
5829 case DW_TAG_subroutine_type:
5830 return "DW_TAG_subroutine_type";
5831 case DW_TAG_typedef:
5832 return "DW_TAG_typedef";
5833 case DW_TAG_union_type:
5834 return "DW_TAG_union_type";
5835 case DW_TAG_unspecified_parameters:
5836 return "DW_TAG_unspecified_parameters";
5837 case DW_TAG_variant:
5838 return "DW_TAG_variant";
5839 case DW_TAG_common_block:
5840 return "DW_TAG_common_block";
5841 case DW_TAG_common_inclusion:
5842 return "DW_TAG_common_inclusion";
5843 case DW_TAG_inheritance:
5844 return "DW_TAG_inheritance";
5845 case DW_TAG_inlined_subroutine:
5846 return "DW_TAG_inlined_subroutine";
5847 case DW_TAG_module:
5848 return "DW_TAG_module";
5849 case DW_TAG_ptr_to_member_type:
5850 return "DW_TAG_ptr_to_member_type";
5851 case DW_TAG_set_type:
5852 return "DW_TAG_set_type";
5853 case DW_TAG_subrange_type:
5854 return "DW_TAG_subrange_type";
5855 case DW_TAG_with_stmt:
5856 return "DW_TAG_with_stmt";
5857 case DW_TAG_access_declaration:
5858 return "DW_TAG_access_declaration";
5859 case DW_TAG_base_type:
5860 return "DW_TAG_base_type";
5861 case DW_TAG_catch_block:
5862 return "DW_TAG_catch_block";
5863 case DW_TAG_const_type:
5864 return "DW_TAG_const_type";
5865 case DW_TAG_constant:
5866 return "DW_TAG_constant";
5867 case DW_TAG_enumerator:
5868 return "DW_TAG_enumerator";
5869 case DW_TAG_file_type:
5870 return "DW_TAG_file_type";
5871 case DW_TAG_friend:
5872 return "DW_TAG_friend";
5873 case DW_TAG_namelist:
5874 return "DW_TAG_namelist";
5875 case DW_TAG_namelist_item:
5876 return "DW_TAG_namelist_item";
5877 case DW_TAG_packed_type:
5878 return "DW_TAG_packed_type";
5879 case DW_TAG_subprogram:
5880 return "DW_TAG_subprogram";
5881 case DW_TAG_template_type_param:
5882 return "DW_TAG_template_type_param";
5883 case DW_TAG_template_value_param:
5884 return "DW_TAG_template_value_param";
5885 case DW_TAG_thrown_type:
5886 return "DW_TAG_thrown_type";
5887 case DW_TAG_try_block:
5888 return "DW_TAG_try_block";
5889 case DW_TAG_variant_part:
5890 return "DW_TAG_variant_part";
5891 case DW_TAG_variable:
5892 return "DW_TAG_variable";
5893 case DW_TAG_volatile_type:
5894 return "DW_TAG_volatile_type";
5895 case DW_TAG_dwarf_procedure:
5896 return "DW_TAG_dwarf_procedure";
5897 case DW_TAG_restrict_type:
5898 return "DW_TAG_restrict_type";
5899 case DW_TAG_interface_type:
5900 return "DW_TAG_interface_type";
5901 case DW_TAG_namespace:
5902 return "DW_TAG_namespace";
5903 case DW_TAG_imported_module:
5904 return "DW_TAG_imported_module";
5905 case DW_TAG_unspecified_type:
5906 return "DW_TAG_unspecified_type";
5907 case DW_TAG_partial_unit:
5908 return "DW_TAG_partial_unit";
5909 case DW_TAG_imported_unit:
5910 return "DW_TAG_imported_unit";
5911 case DW_TAG_MIPS_loop:
5912 return "DW_TAG_MIPS_loop";
5913 case DW_TAG_format_label:
5914 return "DW_TAG_format_label";
5915 case DW_TAG_function_template:
5916 return "DW_TAG_function_template";
5917 case DW_TAG_class_template:
5918 return "DW_TAG_class_template";
5919 default:
5920 return "DW_TAG_<unknown>";
5921 }
5922 }
5923
5924 /* Convert a DWARF attribute code into its string name. */
5925
5926 static char *
5927 dwarf_attr_name (unsigned attr)
5928 {
5929 switch (attr)
5930 {
5931 case DW_AT_sibling:
5932 return "DW_AT_sibling";
5933 case DW_AT_location:
5934 return "DW_AT_location";
5935 case DW_AT_name:
5936 return "DW_AT_name";
5937 case DW_AT_ordering:
5938 return "DW_AT_ordering";
5939 case DW_AT_subscr_data:
5940 return "DW_AT_subscr_data";
5941 case DW_AT_byte_size:
5942 return "DW_AT_byte_size";
5943 case DW_AT_bit_offset:
5944 return "DW_AT_bit_offset";
5945 case DW_AT_bit_size:
5946 return "DW_AT_bit_size";
5947 case DW_AT_element_list:
5948 return "DW_AT_element_list";
5949 case DW_AT_stmt_list:
5950 return "DW_AT_stmt_list";
5951 case DW_AT_low_pc:
5952 return "DW_AT_low_pc";
5953 case DW_AT_high_pc:
5954 return "DW_AT_high_pc";
5955 case DW_AT_language:
5956 return "DW_AT_language";
5957 case DW_AT_member:
5958 return "DW_AT_member";
5959 case DW_AT_discr:
5960 return "DW_AT_discr";
5961 case DW_AT_discr_value:
5962 return "DW_AT_discr_value";
5963 case DW_AT_visibility:
5964 return "DW_AT_visibility";
5965 case DW_AT_import:
5966 return "DW_AT_import";
5967 case DW_AT_string_length:
5968 return "DW_AT_string_length";
5969 case DW_AT_common_reference:
5970 return "DW_AT_common_reference";
5971 case DW_AT_comp_dir:
5972 return "DW_AT_comp_dir";
5973 case DW_AT_const_value:
5974 return "DW_AT_const_value";
5975 case DW_AT_containing_type:
5976 return "DW_AT_containing_type";
5977 case DW_AT_default_value:
5978 return "DW_AT_default_value";
5979 case DW_AT_inline:
5980 return "DW_AT_inline";
5981 case DW_AT_is_optional:
5982 return "DW_AT_is_optional";
5983 case DW_AT_lower_bound:
5984 return "DW_AT_lower_bound";
5985 case DW_AT_producer:
5986 return "DW_AT_producer";
5987 case DW_AT_prototyped:
5988 return "DW_AT_prototyped";
5989 case DW_AT_return_addr:
5990 return "DW_AT_return_addr";
5991 case DW_AT_start_scope:
5992 return "DW_AT_start_scope";
5993 case DW_AT_stride_size:
5994 return "DW_AT_stride_size";
5995 case DW_AT_upper_bound:
5996 return "DW_AT_upper_bound";
5997 case DW_AT_abstract_origin:
5998 return "DW_AT_abstract_origin";
5999 case DW_AT_accessibility:
6000 return "DW_AT_accessibility";
6001 case DW_AT_address_class:
6002 return "DW_AT_address_class";
6003 case DW_AT_artificial:
6004 return "DW_AT_artificial";
6005 case DW_AT_base_types:
6006 return "DW_AT_base_types";
6007 case DW_AT_calling_convention:
6008 return "DW_AT_calling_convention";
6009 case DW_AT_count:
6010 return "DW_AT_count";
6011 case DW_AT_data_member_location:
6012 return "DW_AT_data_member_location";
6013 case DW_AT_decl_column:
6014 return "DW_AT_decl_column";
6015 case DW_AT_decl_file:
6016 return "DW_AT_decl_file";
6017 case DW_AT_decl_line:
6018 return "DW_AT_decl_line";
6019 case DW_AT_declaration:
6020 return "DW_AT_declaration";
6021 case DW_AT_discr_list:
6022 return "DW_AT_discr_list";
6023 case DW_AT_encoding:
6024 return "DW_AT_encoding";
6025 case DW_AT_external:
6026 return "DW_AT_external";
6027 case DW_AT_frame_base:
6028 return "DW_AT_frame_base";
6029 case DW_AT_friend:
6030 return "DW_AT_friend";
6031 case DW_AT_identifier_case:
6032 return "DW_AT_identifier_case";
6033 case DW_AT_macro_info:
6034 return "DW_AT_macro_info";
6035 case DW_AT_namelist_items:
6036 return "DW_AT_namelist_items";
6037 case DW_AT_priority:
6038 return "DW_AT_priority";
6039 case DW_AT_segment:
6040 return "DW_AT_segment";
6041 case DW_AT_specification:
6042 return "DW_AT_specification";
6043 case DW_AT_static_link:
6044 return "DW_AT_static_link";
6045 case DW_AT_type:
6046 return "DW_AT_type";
6047 case DW_AT_use_location:
6048 return "DW_AT_use_location";
6049 case DW_AT_variable_parameter:
6050 return "DW_AT_variable_parameter";
6051 case DW_AT_virtuality:
6052 return "DW_AT_virtuality";
6053 case DW_AT_vtable_elem_location:
6054 return "DW_AT_vtable_elem_location";
6055 case DW_AT_allocated:
6056 return "DW_AT_allocated";
6057 case DW_AT_associated:
6058 return "DW_AT_associated";
6059 case DW_AT_data_location:
6060 return "DW_AT_data_location";
6061 case DW_AT_stride:
6062 return "DW_AT_stride";
6063 case DW_AT_entry_pc:
6064 return "DW_AT_entry_pc";
6065 case DW_AT_use_UTF8:
6066 return "DW_AT_use_UTF8";
6067 case DW_AT_extension:
6068 return "DW_AT_extension";
6069 case DW_AT_ranges:
6070 return "DW_AT_ranges";
6071 case DW_AT_trampoline:
6072 return "DW_AT_trampoline";
6073 case DW_AT_call_column:
6074 return "DW_AT_call_column";
6075 case DW_AT_call_file:
6076 return "DW_AT_call_file";
6077 case DW_AT_call_line:
6078 return "DW_AT_call_line";
6079 #ifdef MIPS
6080 case DW_AT_MIPS_fde:
6081 return "DW_AT_MIPS_fde";
6082 case DW_AT_MIPS_loop_begin:
6083 return "DW_AT_MIPS_loop_begin";
6084 case DW_AT_MIPS_tail_loop_begin:
6085 return "DW_AT_MIPS_tail_loop_begin";
6086 case DW_AT_MIPS_epilog_begin:
6087 return "DW_AT_MIPS_epilog_begin";
6088 case DW_AT_MIPS_loop_unroll_factor:
6089 return "DW_AT_MIPS_loop_unroll_factor";
6090 case DW_AT_MIPS_software_pipeline_depth:
6091 return "DW_AT_MIPS_software_pipeline_depth";
6092 #endif
6093 case DW_AT_MIPS_linkage_name:
6094 return "DW_AT_MIPS_linkage_name";
6095
6096 case DW_AT_sf_names:
6097 return "DW_AT_sf_names";
6098 case DW_AT_src_info:
6099 return "DW_AT_src_info";
6100 case DW_AT_mac_info:
6101 return "DW_AT_mac_info";
6102 case DW_AT_src_coords:
6103 return "DW_AT_src_coords";
6104 case DW_AT_body_begin:
6105 return "DW_AT_body_begin";
6106 case DW_AT_body_end:
6107 return "DW_AT_body_end";
6108 case DW_AT_GNU_vector:
6109 return "DW_AT_GNU_vector";
6110 default:
6111 return "DW_AT_<unknown>";
6112 }
6113 }
6114
6115 /* Convert a DWARF value form code into its string name. */
6116
6117 static char *
6118 dwarf_form_name (unsigned form)
6119 {
6120 switch (form)
6121 {
6122 case DW_FORM_addr:
6123 return "DW_FORM_addr";
6124 case DW_FORM_block2:
6125 return "DW_FORM_block2";
6126 case DW_FORM_block4:
6127 return "DW_FORM_block4";
6128 case DW_FORM_data2:
6129 return "DW_FORM_data2";
6130 case DW_FORM_data4:
6131 return "DW_FORM_data4";
6132 case DW_FORM_data8:
6133 return "DW_FORM_data8";
6134 case DW_FORM_string:
6135 return "DW_FORM_string";
6136 case DW_FORM_block:
6137 return "DW_FORM_block";
6138 case DW_FORM_block1:
6139 return "DW_FORM_block1";
6140 case DW_FORM_data1:
6141 return "DW_FORM_data1";
6142 case DW_FORM_flag:
6143 return "DW_FORM_flag";
6144 case DW_FORM_sdata:
6145 return "DW_FORM_sdata";
6146 case DW_FORM_strp:
6147 return "DW_FORM_strp";
6148 case DW_FORM_udata:
6149 return "DW_FORM_udata";
6150 case DW_FORM_ref_addr:
6151 return "DW_FORM_ref_addr";
6152 case DW_FORM_ref1:
6153 return "DW_FORM_ref1";
6154 case DW_FORM_ref2:
6155 return "DW_FORM_ref2";
6156 case DW_FORM_ref4:
6157 return "DW_FORM_ref4";
6158 case DW_FORM_ref8:
6159 return "DW_FORM_ref8";
6160 case DW_FORM_ref_udata:
6161 return "DW_FORM_ref_udata";
6162 case DW_FORM_indirect:
6163 return "DW_FORM_indirect";
6164 default:
6165 return "DW_FORM_<unknown>";
6166 }
6167 }
6168
6169 /* Convert a DWARF stack opcode into its string name. */
6170
6171 static char *
6172 dwarf_stack_op_name (unsigned op)
6173 {
6174 switch (op)
6175 {
6176 case DW_OP_addr:
6177 return "DW_OP_addr";
6178 case DW_OP_deref:
6179 return "DW_OP_deref";
6180 case DW_OP_const1u:
6181 return "DW_OP_const1u";
6182 case DW_OP_const1s:
6183 return "DW_OP_const1s";
6184 case DW_OP_const2u:
6185 return "DW_OP_const2u";
6186 case DW_OP_const2s:
6187 return "DW_OP_const2s";
6188 case DW_OP_const4u:
6189 return "DW_OP_const4u";
6190 case DW_OP_const4s:
6191 return "DW_OP_const4s";
6192 case DW_OP_const8u:
6193 return "DW_OP_const8u";
6194 case DW_OP_const8s:
6195 return "DW_OP_const8s";
6196 case DW_OP_constu:
6197 return "DW_OP_constu";
6198 case DW_OP_consts:
6199 return "DW_OP_consts";
6200 case DW_OP_dup:
6201 return "DW_OP_dup";
6202 case DW_OP_drop:
6203 return "DW_OP_drop";
6204 case DW_OP_over:
6205 return "DW_OP_over";
6206 case DW_OP_pick:
6207 return "DW_OP_pick";
6208 case DW_OP_swap:
6209 return "DW_OP_swap";
6210 case DW_OP_rot:
6211 return "DW_OP_rot";
6212 case DW_OP_xderef:
6213 return "DW_OP_xderef";
6214 case DW_OP_abs:
6215 return "DW_OP_abs";
6216 case DW_OP_and:
6217 return "DW_OP_and";
6218 case DW_OP_div:
6219 return "DW_OP_div";
6220 case DW_OP_minus:
6221 return "DW_OP_minus";
6222 case DW_OP_mod:
6223 return "DW_OP_mod";
6224 case DW_OP_mul:
6225 return "DW_OP_mul";
6226 case DW_OP_neg:
6227 return "DW_OP_neg";
6228 case DW_OP_not:
6229 return "DW_OP_not";
6230 case DW_OP_or:
6231 return "DW_OP_or";
6232 case DW_OP_plus:
6233 return "DW_OP_plus";
6234 case DW_OP_plus_uconst:
6235 return "DW_OP_plus_uconst";
6236 case DW_OP_shl:
6237 return "DW_OP_shl";
6238 case DW_OP_shr:
6239 return "DW_OP_shr";
6240 case DW_OP_shra:
6241 return "DW_OP_shra";
6242 case DW_OP_xor:
6243 return "DW_OP_xor";
6244 case DW_OP_bra:
6245 return "DW_OP_bra";
6246 case DW_OP_eq:
6247 return "DW_OP_eq";
6248 case DW_OP_ge:
6249 return "DW_OP_ge";
6250 case DW_OP_gt:
6251 return "DW_OP_gt";
6252 case DW_OP_le:
6253 return "DW_OP_le";
6254 case DW_OP_lt:
6255 return "DW_OP_lt";
6256 case DW_OP_ne:
6257 return "DW_OP_ne";
6258 case DW_OP_skip:
6259 return "DW_OP_skip";
6260 case DW_OP_lit0:
6261 return "DW_OP_lit0";
6262 case DW_OP_lit1:
6263 return "DW_OP_lit1";
6264 case DW_OP_lit2:
6265 return "DW_OP_lit2";
6266 case DW_OP_lit3:
6267 return "DW_OP_lit3";
6268 case DW_OP_lit4:
6269 return "DW_OP_lit4";
6270 case DW_OP_lit5:
6271 return "DW_OP_lit5";
6272 case DW_OP_lit6:
6273 return "DW_OP_lit6";
6274 case DW_OP_lit7:
6275 return "DW_OP_lit7";
6276 case DW_OP_lit8:
6277 return "DW_OP_lit8";
6278 case DW_OP_lit9:
6279 return "DW_OP_lit9";
6280 case DW_OP_lit10:
6281 return "DW_OP_lit10";
6282 case DW_OP_lit11:
6283 return "DW_OP_lit11";
6284 case DW_OP_lit12:
6285 return "DW_OP_lit12";
6286 case DW_OP_lit13:
6287 return "DW_OP_lit13";
6288 case DW_OP_lit14:
6289 return "DW_OP_lit14";
6290 case DW_OP_lit15:
6291 return "DW_OP_lit15";
6292 case DW_OP_lit16:
6293 return "DW_OP_lit16";
6294 case DW_OP_lit17:
6295 return "DW_OP_lit17";
6296 case DW_OP_lit18:
6297 return "DW_OP_lit18";
6298 case DW_OP_lit19:
6299 return "DW_OP_lit19";
6300 case DW_OP_lit20:
6301 return "DW_OP_lit20";
6302 case DW_OP_lit21:
6303 return "DW_OP_lit21";
6304 case DW_OP_lit22:
6305 return "DW_OP_lit22";
6306 case DW_OP_lit23:
6307 return "DW_OP_lit23";
6308 case DW_OP_lit24:
6309 return "DW_OP_lit24";
6310 case DW_OP_lit25:
6311 return "DW_OP_lit25";
6312 case DW_OP_lit26:
6313 return "DW_OP_lit26";
6314 case DW_OP_lit27:
6315 return "DW_OP_lit27";
6316 case DW_OP_lit28:
6317 return "DW_OP_lit28";
6318 case DW_OP_lit29:
6319 return "DW_OP_lit29";
6320 case DW_OP_lit30:
6321 return "DW_OP_lit30";
6322 case DW_OP_lit31:
6323 return "DW_OP_lit31";
6324 case DW_OP_reg0:
6325 return "DW_OP_reg0";
6326 case DW_OP_reg1:
6327 return "DW_OP_reg1";
6328 case DW_OP_reg2:
6329 return "DW_OP_reg2";
6330 case DW_OP_reg3:
6331 return "DW_OP_reg3";
6332 case DW_OP_reg4:
6333 return "DW_OP_reg4";
6334 case DW_OP_reg5:
6335 return "DW_OP_reg5";
6336 case DW_OP_reg6:
6337 return "DW_OP_reg6";
6338 case DW_OP_reg7:
6339 return "DW_OP_reg7";
6340 case DW_OP_reg8:
6341 return "DW_OP_reg8";
6342 case DW_OP_reg9:
6343 return "DW_OP_reg9";
6344 case DW_OP_reg10:
6345 return "DW_OP_reg10";
6346 case DW_OP_reg11:
6347 return "DW_OP_reg11";
6348 case DW_OP_reg12:
6349 return "DW_OP_reg12";
6350 case DW_OP_reg13:
6351 return "DW_OP_reg13";
6352 case DW_OP_reg14:
6353 return "DW_OP_reg14";
6354 case DW_OP_reg15:
6355 return "DW_OP_reg15";
6356 case DW_OP_reg16:
6357 return "DW_OP_reg16";
6358 case DW_OP_reg17:
6359 return "DW_OP_reg17";
6360 case DW_OP_reg18:
6361 return "DW_OP_reg18";
6362 case DW_OP_reg19:
6363 return "DW_OP_reg19";
6364 case DW_OP_reg20:
6365 return "DW_OP_reg20";
6366 case DW_OP_reg21:
6367 return "DW_OP_reg21";
6368 case DW_OP_reg22:
6369 return "DW_OP_reg22";
6370 case DW_OP_reg23:
6371 return "DW_OP_reg23";
6372 case DW_OP_reg24:
6373 return "DW_OP_reg24";
6374 case DW_OP_reg25:
6375 return "DW_OP_reg25";
6376 case DW_OP_reg26:
6377 return "DW_OP_reg26";
6378 case DW_OP_reg27:
6379 return "DW_OP_reg27";
6380 case DW_OP_reg28:
6381 return "DW_OP_reg28";
6382 case DW_OP_reg29:
6383 return "DW_OP_reg29";
6384 case DW_OP_reg30:
6385 return "DW_OP_reg30";
6386 case DW_OP_reg31:
6387 return "DW_OP_reg31";
6388 case DW_OP_breg0:
6389 return "DW_OP_breg0";
6390 case DW_OP_breg1:
6391 return "DW_OP_breg1";
6392 case DW_OP_breg2:
6393 return "DW_OP_breg2";
6394 case DW_OP_breg3:
6395 return "DW_OP_breg3";
6396 case DW_OP_breg4:
6397 return "DW_OP_breg4";
6398 case DW_OP_breg5:
6399 return "DW_OP_breg5";
6400 case DW_OP_breg6:
6401 return "DW_OP_breg6";
6402 case DW_OP_breg7:
6403 return "DW_OP_breg7";
6404 case DW_OP_breg8:
6405 return "DW_OP_breg8";
6406 case DW_OP_breg9:
6407 return "DW_OP_breg9";
6408 case DW_OP_breg10:
6409 return "DW_OP_breg10";
6410 case DW_OP_breg11:
6411 return "DW_OP_breg11";
6412 case DW_OP_breg12:
6413 return "DW_OP_breg12";
6414 case DW_OP_breg13:
6415 return "DW_OP_breg13";
6416 case DW_OP_breg14:
6417 return "DW_OP_breg14";
6418 case DW_OP_breg15:
6419 return "DW_OP_breg15";
6420 case DW_OP_breg16:
6421 return "DW_OP_breg16";
6422 case DW_OP_breg17:
6423 return "DW_OP_breg17";
6424 case DW_OP_breg18:
6425 return "DW_OP_breg18";
6426 case DW_OP_breg19:
6427 return "DW_OP_breg19";
6428 case DW_OP_breg20:
6429 return "DW_OP_breg20";
6430 case DW_OP_breg21:
6431 return "DW_OP_breg21";
6432 case DW_OP_breg22:
6433 return "DW_OP_breg22";
6434 case DW_OP_breg23:
6435 return "DW_OP_breg23";
6436 case DW_OP_breg24:
6437 return "DW_OP_breg24";
6438 case DW_OP_breg25:
6439 return "DW_OP_breg25";
6440 case DW_OP_breg26:
6441 return "DW_OP_breg26";
6442 case DW_OP_breg27:
6443 return "DW_OP_breg27";
6444 case DW_OP_breg28:
6445 return "DW_OP_breg28";
6446 case DW_OP_breg29:
6447 return "DW_OP_breg29";
6448 case DW_OP_breg30:
6449 return "DW_OP_breg30";
6450 case DW_OP_breg31:
6451 return "DW_OP_breg31";
6452 case DW_OP_regx:
6453 return "DW_OP_regx";
6454 case DW_OP_fbreg:
6455 return "DW_OP_fbreg";
6456 case DW_OP_bregx:
6457 return "DW_OP_bregx";
6458 case DW_OP_piece:
6459 return "DW_OP_piece";
6460 case DW_OP_deref_size:
6461 return "DW_OP_deref_size";
6462 case DW_OP_xderef_size:
6463 return "DW_OP_xderef_size";
6464 case DW_OP_nop:
6465 return "DW_OP_nop";
6466 /* DWARF 3 extensions. */
6467 case DW_OP_push_object_address:
6468 return "DW_OP_push_object_address";
6469 case DW_OP_call2:
6470 return "DW_OP_call2";
6471 case DW_OP_call4:
6472 return "DW_OP_call4";
6473 case DW_OP_call_ref:
6474 return "DW_OP_call_ref";
6475 /* GNU extensions. */
6476 case DW_OP_GNU_push_tls_address:
6477 return "DW_OP_GNU_push_tls_address";
6478 default:
6479 return "OP_<unknown>";
6480 }
6481 }
6482
6483 static char *
6484 dwarf_bool_name (unsigned mybool)
6485 {
6486 if (mybool)
6487 return "TRUE";
6488 else
6489 return "FALSE";
6490 }
6491
6492 /* Convert a DWARF type code into its string name. */
6493
6494 static char *
6495 dwarf_type_encoding_name (unsigned enc)
6496 {
6497 switch (enc)
6498 {
6499 case DW_ATE_address:
6500 return "DW_ATE_address";
6501 case DW_ATE_boolean:
6502 return "DW_ATE_boolean";
6503 case DW_ATE_complex_float:
6504 return "DW_ATE_complex_float";
6505 case DW_ATE_float:
6506 return "DW_ATE_float";
6507 case DW_ATE_signed:
6508 return "DW_ATE_signed";
6509 case DW_ATE_signed_char:
6510 return "DW_ATE_signed_char";
6511 case DW_ATE_unsigned:
6512 return "DW_ATE_unsigned";
6513 case DW_ATE_unsigned_char:
6514 return "DW_ATE_unsigned_char";
6515 case DW_ATE_imaginary_float:
6516 return "DW_ATE_imaginary_float";
6517 default:
6518 return "DW_ATE_<unknown>";
6519 }
6520 }
6521
6522 /* Convert a DWARF call frame info operation to its string name. */
6523
6524 #if 0
6525 static char *
6526 dwarf_cfi_name (unsigned cfi_opc)
6527 {
6528 switch (cfi_opc)
6529 {
6530 case DW_CFA_advance_loc:
6531 return "DW_CFA_advance_loc";
6532 case DW_CFA_offset:
6533 return "DW_CFA_offset";
6534 case DW_CFA_restore:
6535 return "DW_CFA_restore";
6536 case DW_CFA_nop:
6537 return "DW_CFA_nop";
6538 case DW_CFA_set_loc:
6539 return "DW_CFA_set_loc";
6540 case DW_CFA_advance_loc1:
6541 return "DW_CFA_advance_loc1";
6542 case DW_CFA_advance_loc2:
6543 return "DW_CFA_advance_loc2";
6544 case DW_CFA_advance_loc4:
6545 return "DW_CFA_advance_loc4";
6546 case DW_CFA_offset_extended:
6547 return "DW_CFA_offset_extended";
6548 case DW_CFA_restore_extended:
6549 return "DW_CFA_restore_extended";
6550 case DW_CFA_undefined:
6551 return "DW_CFA_undefined";
6552 case DW_CFA_same_value:
6553 return "DW_CFA_same_value";
6554 case DW_CFA_register:
6555 return "DW_CFA_register";
6556 case DW_CFA_remember_state:
6557 return "DW_CFA_remember_state";
6558 case DW_CFA_restore_state:
6559 return "DW_CFA_restore_state";
6560 case DW_CFA_def_cfa:
6561 return "DW_CFA_def_cfa";
6562 case DW_CFA_def_cfa_register:
6563 return "DW_CFA_def_cfa_register";
6564 case DW_CFA_def_cfa_offset:
6565 return "DW_CFA_def_cfa_offset";
6566
6567 /* DWARF 3 */
6568 case DW_CFA_def_cfa_expression:
6569 return "DW_CFA_def_cfa_expression";
6570 case DW_CFA_expression:
6571 return "DW_CFA_expression";
6572 case DW_CFA_offset_extended_sf:
6573 return "DW_CFA_offset_extended_sf";
6574 case DW_CFA_def_cfa_sf:
6575 return "DW_CFA_def_cfa_sf";
6576 case DW_CFA_def_cfa_offset_sf:
6577 return "DW_CFA_def_cfa_offset_sf";
6578
6579 /* SGI/MIPS specific */
6580 case DW_CFA_MIPS_advance_loc8:
6581 return "DW_CFA_MIPS_advance_loc8";
6582
6583 /* GNU extensions */
6584 case DW_CFA_GNU_window_save:
6585 return "DW_CFA_GNU_window_save";
6586 case DW_CFA_GNU_args_size:
6587 return "DW_CFA_GNU_args_size";
6588 case DW_CFA_GNU_negative_offset_extended:
6589 return "DW_CFA_GNU_negative_offset_extended";
6590
6591 default:
6592 return "DW_CFA_<unknown>";
6593 }
6594 }
6595 #endif
6596
6597 static void
6598 dump_die (struct die_info *die)
6599 {
6600 unsigned int i;
6601
6602 fprintf_unfiltered (gdb_stderr, "Die: %s (abbrev = %d, offset = %d)\n",
6603 dwarf_tag_name (die->tag), die->abbrev, die->offset);
6604 fprintf_unfiltered (gdb_stderr, "\thas children: %s\n",
6605 dwarf_bool_name (die->child != NULL));
6606
6607 fprintf_unfiltered (gdb_stderr, "\tattributes:\n");
6608 for (i = 0; i < die->num_attrs; ++i)
6609 {
6610 fprintf_unfiltered (gdb_stderr, "\t\t%s (%s) ",
6611 dwarf_attr_name (die->attrs[i].name),
6612 dwarf_form_name (die->attrs[i].form));
6613 switch (die->attrs[i].form)
6614 {
6615 case DW_FORM_ref_addr:
6616 case DW_FORM_addr:
6617 fprintf_unfiltered (gdb_stderr, "address: ");
6618 print_address_numeric (DW_ADDR (&die->attrs[i]), 1, gdb_stderr);
6619 break;
6620 case DW_FORM_block2:
6621 case DW_FORM_block4:
6622 case DW_FORM_block:
6623 case DW_FORM_block1:
6624 fprintf_unfiltered (gdb_stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
6625 break;
6626 case DW_FORM_data1:
6627 case DW_FORM_data2:
6628 case DW_FORM_data4:
6629 case DW_FORM_data8:
6630 case DW_FORM_ref1:
6631 case DW_FORM_ref2:
6632 case DW_FORM_ref4:
6633 case DW_FORM_udata:
6634 case DW_FORM_sdata:
6635 fprintf_unfiltered (gdb_stderr, "constant: %ld", DW_UNSND (&die->attrs[i]));
6636 break;
6637 case DW_FORM_string:
6638 case DW_FORM_strp:
6639 fprintf_unfiltered (gdb_stderr, "string: \"%s\"",
6640 DW_STRING (&die->attrs[i])
6641 ? DW_STRING (&die->attrs[i]) : "");
6642 break;
6643 case DW_FORM_flag:
6644 if (DW_UNSND (&die->attrs[i]))
6645 fprintf_unfiltered (gdb_stderr, "flag: TRUE");
6646 else
6647 fprintf_unfiltered (gdb_stderr, "flag: FALSE");
6648 break;
6649 case DW_FORM_indirect:
6650 /* the reader will have reduced the indirect form to
6651 the "base form" so this form should not occur */
6652 fprintf_unfiltered (gdb_stderr, "unexpected attribute form: DW_FORM_indirect");
6653 break;
6654 default:
6655 fprintf_unfiltered (gdb_stderr, "unsupported attribute form: %d.",
6656 die->attrs[i].form);
6657 }
6658 fprintf_unfiltered (gdb_stderr, "\n");
6659 }
6660 }
6661
6662 static void
6663 dump_die_list (struct die_info *die)
6664 {
6665 while (die)
6666 {
6667 dump_die (die);
6668 if (die->child != NULL)
6669 dump_die_list (die->child);
6670 if (die->sibling != NULL)
6671 dump_die_list (die->sibling);
6672 }
6673 }
6674
6675 static void
6676 store_in_ref_table (unsigned int offset, struct die_info *die)
6677 {
6678 int h;
6679 struct die_info *old;
6680
6681 h = (offset % REF_HASH_SIZE);
6682 old = die_ref_table[h];
6683 die->next_ref = old;
6684 die_ref_table[h] = die;
6685 }
6686
6687
6688 static void
6689 dwarf2_empty_hash_tables (void)
6690 {
6691 memset (die_ref_table, 0, sizeof (die_ref_table));
6692 }
6693
6694 static unsigned int
6695 dwarf2_get_ref_die_offset (struct attribute *attr)
6696 {
6697 unsigned int result = 0;
6698
6699 switch (attr->form)
6700 {
6701 case DW_FORM_ref_addr:
6702 result = DW_ADDR (attr);
6703 break;
6704 case DW_FORM_ref1:
6705 case DW_FORM_ref2:
6706 case DW_FORM_ref4:
6707 case DW_FORM_ref8:
6708 case DW_FORM_ref_udata:
6709 result = cu_header_offset + DW_UNSND (attr);
6710 break;
6711 default:
6712 complaint (&symfile_complaints,
6713 "unsupported die ref attribute form: '%s'",
6714 dwarf_form_name (attr->form));
6715 }
6716 return result;
6717 }
6718
6719 static struct die_info *
6720 follow_die_ref (unsigned int offset)
6721 {
6722 struct die_info *die;
6723 int h;
6724
6725 h = (offset % REF_HASH_SIZE);
6726 die = die_ref_table[h];
6727 while (die)
6728 {
6729 if (die->offset == offset)
6730 {
6731 return die;
6732 }
6733 die = die->next_ref;
6734 }
6735 return NULL;
6736 }
6737
6738 static struct type *
6739 dwarf2_fundamental_type (struct objfile *objfile, int typeid)
6740 {
6741 if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
6742 {
6743 error ("Dwarf Error: internal error - invalid fundamental type id %d [in module %s]",
6744 typeid, objfile->name);
6745 }
6746
6747 /* Look for this particular type in the fundamental type vector. If
6748 one is not found, create and install one appropriate for the
6749 current language and the current target machine. */
6750
6751 if (ftypes[typeid] == NULL)
6752 {
6753 ftypes[typeid] = cu_language_defn->la_fund_type (objfile, typeid);
6754 }
6755
6756 return (ftypes[typeid]);
6757 }
6758
6759 /* Decode simple location descriptions.
6760 Given a pointer to a dwarf block that defines a location, compute
6761 the location and return the value.
6762
6763 NOTE drow/2003-11-18: This function is called in two situations
6764 now: for the address of static or global variables (partial symbols
6765 only) and for offsets into structures which are expected to be
6766 (more or less) constant. The partial symbol case should go away,
6767 and only the constant case should remain. That will let this
6768 function complain more accurately. A few special modes are allowed
6769 without complaint for global variables (for instance, global
6770 register values and thread-local values).
6771
6772 A location description containing no operations indicates that the
6773 object is optimized out. The return value is 0 for that case.
6774 FIXME drow/2003-11-16: No callers check for this case any more; soon all
6775 callers will only want a very basic result and this can become a
6776 complaint.
6777
6778 When the result is a register number, the global isreg flag is set,
6779 otherwise it is cleared.
6780
6781 Note that stack[0] is unused except as a default error return.
6782 Note that stack overflow is not yet handled. */
6783
6784 static CORE_ADDR
6785 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
6786 {
6787 struct objfile *objfile = cu->objfile;
6788 struct comp_unit_head *cu_header = &cu->header;
6789 int i;
6790 int size = blk->size;
6791 char *data = blk->data;
6792 CORE_ADDR stack[64];
6793 int stacki;
6794 unsigned int bytes_read, unsnd;
6795 unsigned char op;
6796
6797 i = 0;
6798 stacki = 0;
6799 stack[stacki] = 0;
6800 isreg = 0;
6801
6802 while (i < size)
6803 {
6804 op = data[i++];
6805 switch (op)
6806 {
6807 case DW_OP_lit0:
6808 case DW_OP_lit1:
6809 case DW_OP_lit2:
6810 case DW_OP_lit3:
6811 case DW_OP_lit4:
6812 case DW_OP_lit5:
6813 case DW_OP_lit6:
6814 case DW_OP_lit7:
6815 case DW_OP_lit8:
6816 case DW_OP_lit9:
6817 case DW_OP_lit10:
6818 case DW_OP_lit11:
6819 case DW_OP_lit12:
6820 case DW_OP_lit13:
6821 case DW_OP_lit14:
6822 case DW_OP_lit15:
6823 case DW_OP_lit16:
6824 case DW_OP_lit17:
6825 case DW_OP_lit18:
6826 case DW_OP_lit19:
6827 case DW_OP_lit20:
6828 case DW_OP_lit21:
6829 case DW_OP_lit22:
6830 case DW_OP_lit23:
6831 case DW_OP_lit24:
6832 case DW_OP_lit25:
6833 case DW_OP_lit26:
6834 case DW_OP_lit27:
6835 case DW_OP_lit28:
6836 case DW_OP_lit29:
6837 case DW_OP_lit30:
6838 case DW_OP_lit31:
6839 stack[++stacki] = op - DW_OP_lit0;
6840 break;
6841
6842 case DW_OP_reg0:
6843 case DW_OP_reg1:
6844 case DW_OP_reg2:
6845 case DW_OP_reg3:
6846 case DW_OP_reg4:
6847 case DW_OP_reg5:
6848 case DW_OP_reg6:
6849 case DW_OP_reg7:
6850 case DW_OP_reg8:
6851 case DW_OP_reg9:
6852 case DW_OP_reg10:
6853 case DW_OP_reg11:
6854 case DW_OP_reg12:
6855 case DW_OP_reg13:
6856 case DW_OP_reg14:
6857 case DW_OP_reg15:
6858 case DW_OP_reg16:
6859 case DW_OP_reg17:
6860 case DW_OP_reg18:
6861 case DW_OP_reg19:
6862 case DW_OP_reg20:
6863 case DW_OP_reg21:
6864 case DW_OP_reg22:
6865 case DW_OP_reg23:
6866 case DW_OP_reg24:
6867 case DW_OP_reg25:
6868 case DW_OP_reg26:
6869 case DW_OP_reg27:
6870 case DW_OP_reg28:
6871 case DW_OP_reg29:
6872 case DW_OP_reg30:
6873 case DW_OP_reg31:
6874 isreg = 1;
6875 stack[++stacki] = op - DW_OP_reg0;
6876 if (i < size)
6877 dwarf2_complex_location_expr_complaint ();
6878 break;
6879
6880 case DW_OP_regx:
6881 isreg = 1;
6882 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
6883 i += bytes_read;
6884 stack[++stacki] = unsnd;
6885 if (i < size)
6886 dwarf2_complex_location_expr_complaint ();
6887 break;
6888
6889 case DW_OP_addr:
6890 stack[++stacki] = read_address (objfile->obfd, &data[i],
6891 cu, &bytes_read);
6892 i += bytes_read;
6893 break;
6894
6895 case DW_OP_const1u:
6896 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
6897 i += 1;
6898 break;
6899
6900 case DW_OP_const1s:
6901 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
6902 i += 1;
6903 break;
6904
6905 case DW_OP_const2u:
6906 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
6907 i += 2;
6908 break;
6909
6910 case DW_OP_const2s:
6911 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
6912 i += 2;
6913 break;
6914
6915 case DW_OP_const4u:
6916 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
6917 i += 4;
6918 break;
6919
6920 case DW_OP_const4s:
6921 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
6922 i += 4;
6923 break;
6924
6925 case DW_OP_constu:
6926 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
6927 &bytes_read);
6928 i += bytes_read;
6929 break;
6930
6931 case DW_OP_consts:
6932 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
6933 i += bytes_read;
6934 break;
6935
6936 case DW_OP_dup:
6937 stack[stacki + 1] = stack[stacki];
6938 stacki++;
6939 break;
6940
6941 case DW_OP_plus:
6942 stack[stacki - 1] += stack[stacki];
6943 stacki--;
6944 break;
6945
6946 case DW_OP_plus_uconst:
6947 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
6948 i += bytes_read;
6949 break;
6950
6951 case DW_OP_minus:
6952 stack[stacki - 1] -= stack[stacki];
6953 stacki--;
6954 break;
6955
6956 case DW_OP_deref:
6957 /* If we're not the last op, then we definitely can't encode
6958 this using GDB's address_class enum. This is valid for partial
6959 global symbols, although the variable's address will be bogus
6960 in the psymtab. */
6961 if (i < size)
6962 dwarf2_complex_location_expr_complaint ();
6963 break;
6964
6965 case DW_OP_GNU_push_tls_address:
6966 /* The top of the stack has the offset from the beginning
6967 of the thread control block at which the variable is located. */
6968 /* Nothing should follow this operator, so the top of stack would
6969 be returned. */
6970 /* This is valid for partial global symbols, but the variable's
6971 address will be bogus in the psymtab. */
6972 if (i < size)
6973 dwarf2_complex_location_expr_complaint ();
6974 break;
6975
6976 default:
6977 complaint (&symfile_complaints, "unsupported stack op: '%s'",
6978 dwarf_stack_op_name (op));
6979 return (stack[stacki]);
6980 }
6981 }
6982 return (stack[stacki]);
6983 }
6984
6985 /* memory allocation interface */
6986
6987 static void
6988 dwarf2_free_tmp_obstack (void *ignore)
6989 {
6990 obstack_free (&dwarf2_tmp_obstack, NULL);
6991 }
6992
6993 static struct dwarf_block *
6994 dwarf_alloc_block (void)
6995 {
6996 struct dwarf_block *blk;
6997
6998 blk = (struct dwarf_block *)
6999 obstack_alloc (&dwarf2_tmp_obstack, sizeof (struct dwarf_block));
7000 return (blk);
7001 }
7002
7003 static struct abbrev_info *
7004 dwarf_alloc_abbrev (void)
7005 {
7006 struct abbrev_info *abbrev;
7007
7008 abbrev = (struct abbrev_info *) xmalloc (sizeof (struct abbrev_info));
7009 memset (abbrev, 0, sizeof (struct abbrev_info));
7010 return (abbrev);
7011 }
7012
7013 static struct die_info *
7014 dwarf_alloc_die (void)
7015 {
7016 struct die_info *die;
7017
7018 die = (struct die_info *) xmalloc (sizeof (struct die_info));
7019 memset (die, 0, sizeof (struct die_info));
7020 return (die);
7021 }
7022
7023 \f
7024 /* Macro support. */
7025
7026
7027 /* Return the full name of file number I in *LH's file name table.
7028 Use COMP_DIR as the name of the current directory of the
7029 compilation. The result is allocated using xmalloc; the caller is
7030 responsible for freeing it. */
7031 static char *
7032 file_full_name (int file, struct line_header *lh, const char *comp_dir)
7033 {
7034 struct file_entry *fe = &lh->file_names[file - 1];
7035
7036 if (IS_ABSOLUTE_PATH (fe->name))
7037 return xstrdup (fe->name);
7038 else
7039 {
7040 const char *dir;
7041 int dir_len;
7042 char *full_name;
7043
7044 if (fe->dir_index)
7045 dir = lh->include_dirs[fe->dir_index - 1];
7046 else
7047 dir = comp_dir;
7048
7049 if (dir)
7050 {
7051 dir_len = strlen (dir);
7052 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
7053 strcpy (full_name, dir);
7054 full_name[dir_len] = '/';
7055 strcpy (full_name + dir_len + 1, fe->name);
7056 return full_name;
7057 }
7058 else
7059 return xstrdup (fe->name);
7060 }
7061 }
7062
7063
7064 static struct macro_source_file *
7065 macro_start_file (int file, int line,
7066 struct macro_source_file *current_file,
7067 const char *comp_dir,
7068 struct line_header *lh, struct objfile *objfile)
7069 {
7070 /* The full name of this source file. */
7071 char *full_name = file_full_name (file, lh, comp_dir);
7072
7073 /* We don't create a macro table for this compilation unit
7074 at all until we actually get a filename. */
7075 if (! pending_macros)
7076 pending_macros = new_macro_table (&objfile->symbol_obstack,
7077 objfile->macro_cache);
7078
7079 if (! current_file)
7080 /* If we have no current file, then this must be the start_file
7081 directive for the compilation unit's main source file. */
7082 current_file = macro_set_main (pending_macros, full_name);
7083 else
7084 current_file = macro_include (current_file, line, full_name);
7085
7086 xfree (full_name);
7087
7088 return current_file;
7089 }
7090
7091
7092 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
7093 followed by a null byte. */
7094 static char *
7095 copy_string (const char *buf, int len)
7096 {
7097 char *s = xmalloc (len + 1);
7098 memcpy (s, buf, len);
7099 s[len] = '\0';
7100
7101 return s;
7102 }
7103
7104
7105 static const char *
7106 consume_improper_spaces (const char *p, const char *body)
7107 {
7108 if (*p == ' ')
7109 {
7110 complaint (&symfile_complaints,
7111 "macro definition contains spaces in formal argument list:\n`%s'",
7112 body);
7113
7114 while (*p == ' ')
7115 p++;
7116 }
7117
7118 return p;
7119 }
7120
7121
7122 static void
7123 parse_macro_definition (struct macro_source_file *file, int line,
7124 const char *body)
7125 {
7126 const char *p;
7127
7128 /* The body string takes one of two forms. For object-like macro
7129 definitions, it should be:
7130
7131 <macro name> " " <definition>
7132
7133 For function-like macro definitions, it should be:
7134
7135 <macro name> "() " <definition>
7136 or
7137 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
7138
7139 Spaces may appear only where explicitly indicated, and in the
7140 <definition>.
7141
7142 The Dwarf 2 spec says that an object-like macro's name is always
7143 followed by a space, but versions of GCC around March 2002 omit
7144 the space when the macro's definition is the empty string.
7145
7146 The Dwarf 2 spec says that there should be no spaces between the
7147 formal arguments in a function-like macro's formal argument list,
7148 but versions of GCC around March 2002 include spaces after the
7149 commas. */
7150
7151
7152 /* Find the extent of the macro name. The macro name is terminated
7153 by either a space or null character (for an object-like macro) or
7154 an opening paren (for a function-like macro). */
7155 for (p = body; *p; p++)
7156 if (*p == ' ' || *p == '(')
7157 break;
7158
7159 if (*p == ' ' || *p == '\0')
7160 {
7161 /* It's an object-like macro. */
7162 int name_len = p - body;
7163 char *name = copy_string (body, name_len);
7164 const char *replacement;
7165
7166 if (*p == ' ')
7167 replacement = body + name_len + 1;
7168 else
7169 {
7170 dwarf2_macro_malformed_definition_complaint (body);
7171 replacement = body + name_len;
7172 }
7173
7174 macro_define_object (file, line, name, replacement);
7175
7176 xfree (name);
7177 }
7178 else if (*p == '(')
7179 {
7180 /* It's a function-like macro. */
7181 char *name = copy_string (body, p - body);
7182 int argc = 0;
7183 int argv_size = 1;
7184 char **argv = xmalloc (argv_size * sizeof (*argv));
7185
7186 p++;
7187
7188 p = consume_improper_spaces (p, body);
7189
7190 /* Parse the formal argument list. */
7191 while (*p && *p != ')')
7192 {
7193 /* Find the extent of the current argument name. */
7194 const char *arg_start = p;
7195
7196 while (*p && *p != ',' && *p != ')' && *p != ' ')
7197 p++;
7198
7199 if (! *p || p == arg_start)
7200 dwarf2_macro_malformed_definition_complaint (body);
7201 else
7202 {
7203 /* Make sure argv has room for the new argument. */
7204 if (argc >= argv_size)
7205 {
7206 argv_size *= 2;
7207 argv = xrealloc (argv, argv_size * sizeof (*argv));
7208 }
7209
7210 argv[argc++] = copy_string (arg_start, p - arg_start);
7211 }
7212
7213 p = consume_improper_spaces (p, body);
7214
7215 /* Consume the comma, if present. */
7216 if (*p == ',')
7217 {
7218 p++;
7219
7220 p = consume_improper_spaces (p, body);
7221 }
7222 }
7223
7224 if (*p == ')')
7225 {
7226 p++;
7227
7228 if (*p == ' ')
7229 /* Perfectly formed definition, no complaints. */
7230 macro_define_function (file, line, name,
7231 argc, (const char **) argv,
7232 p + 1);
7233 else if (*p == '\0')
7234 {
7235 /* Complain, but do define it. */
7236 dwarf2_macro_malformed_definition_complaint (body);
7237 macro_define_function (file, line, name,
7238 argc, (const char **) argv,
7239 p);
7240 }
7241 else
7242 /* Just complain. */
7243 dwarf2_macro_malformed_definition_complaint (body);
7244 }
7245 else
7246 /* Just complain. */
7247 dwarf2_macro_malformed_definition_complaint (body);
7248
7249 xfree (name);
7250 {
7251 int i;
7252
7253 for (i = 0; i < argc; i++)
7254 xfree (argv[i]);
7255 }
7256 xfree (argv);
7257 }
7258 else
7259 dwarf2_macro_malformed_definition_complaint (body);
7260 }
7261
7262
7263 static void
7264 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
7265 char *comp_dir, bfd *abfd,
7266 struct dwarf2_cu *cu)
7267 {
7268 char *mac_ptr, *mac_end;
7269 struct macro_source_file *current_file = 0;
7270
7271 if (dwarf_macinfo_buffer == NULL)
7272 {
7273 complaint (&symfile_complaints, "missing .debug_macinfo section");
7274 return;
7275 }
7276
7277 mac_ptr = dwarf_macinfo_buffer + offset;
7278 mac_end = dwarf_macinfo_buffer + dwarf_macinfo_size;
7279
7280 for (;;)
7281 {
7282 enum dwarf_macinfo_record_type macinfo_type;
7283
7284 /* Do we at least have room for a macinfo type byte? */
7285 if (mac_ptr >= mac_end)
7286 {
7287 dwarf2_macros_too_long_complaint ();
7288 return;
7289 }
7290
7291 macinfo_type = read_1_byte (abfd, mac_ptr);
7292 mac_ptr++;
7293
7294 switch (macinfo_type)
7295 {
7296 /* A zero macinfo type indicates the end of the macro
7297 information. */
7298 case 0:
7299 return;
7300
7301 case DW_MACINFO_define:
7302 case DW_MACINFO_undef:
7303 {
7304 int bytes_read;
7305 int line;
7306 char *body;
7307
7308 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
7309 mac_ptr += bytes_read;
7310 body = read_string (abfd, mac_ptr, &bytes_read);
7311 mac_ptr += bytes_read;
7312
7313 if (! current_file)
7314 complaint (&symfile_complaints,
7315 "debug info gives macro %s outside of any file: %s",
7316 macinfo_type ==
7317 DW_MACINFO_define ? "definition" : macinfo_type ==
7318 DW_MACINFO_undef ? "undefinition" :
7319 "something-or-other", body);
7320 else
7321 {
7322 if (macinfo_type == DW_MACINFO_define)
7323 parse_macro_definition (current_file, line, body);
7324 else if (macinfo_type == DW_MACINFO_undef)
7325 macro_undef (current_file, line, body);
7326 }
7327 }
7328 break;
7329
7330 case DW_MACINFO_start_file:
7331 {
7332 int bytes_read;
7333 int line, file;
7334
7335 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
7336 mac_ptr += bytes_read;
7337 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
7338 mac_ptr += bytes_read;
7339
7340 current_file = macro_start_file (file, line,
7341 current_file, comp_dir,
7342 lh, cu->objfile);
7343 }
7344 break;
7345
7346 case DW_MACINFO_end_file:
7347 if (! current_file)
7348 complaint (&symfile_complaints,
7349 "macro debug info has an unmatched `close_file' directive");
7350 else
7351 {
7352 current_file = current_file->included_by;
7353 if (! current_file)
7354 {
7355 enum dwarf_macinfo_record_type next_type;
7356
7357 /* GCC circa March 2002 doesn't produce the zero
7358 type byte marking the end of the compilation
7359 unit. Complain if it's not there, but exit no
7360 matter what. */
7361
7362 /* Do we at least have room for a macinfo type byte? */
7363 if (mac_ptr >= mac_end)
7364 {
7365 dwarf2_macros_too_long_complaint ();
7366 return;
7367 }
7368
7369 /* We don't increment mac_ptr here, so this is just
7370 a look-ahead. */
7371 next_type = read_1_byte (abfd, mac_ptr);
7372 if (next_type != 0)
7373 complaint (&symfile_complaints,
7374 "no terminating 0-type entry for macros in `.debug_macinfo' section");
7375
7376 return;
7377 }
7378 }
7379 break;
7380
7381 case DW_MACINFO_vendor_ext:
7382 {
7383 int bytes_read;
7384 int constant;
7385 char *string;
7386
7387 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
7388 mac_ptr += bytes_read;
7389 string = read_string (abfd, mac_ptr, &bytes_read);
7390 mac_ptr += bytes_read;
7391
7392 /* We don't recognize any vendor extensions. */
7393 }
7394 break;
7395 }
7396 }
7397 }
7398
7399 /* Check if the attribute's form is a DW_FORM_block*
7400 if so return true else false. */
7401 static int
7402 attr_form_is_block (struct attribute *attr)
7403 {
7404 return (attr == NULL ? 0 :
7405 attr->form == DW_FORM_block1
7406 || attr->form == DW_FORM_block2
7407 || attr->form == DW_FORM_block4
7408 || attr->form == DW_FORM_block);
7409 }
7410
7411 static void
7412 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
7413 struct dwarf2_cu *cu)
7414 {
7415 if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
7416 {
7417 struct dwarf2_loclist_baton *baton;
7418
7419 baton = obstack_alloc (&cu->objfile->symbol_obstack,
7420 sizeof (struct dwarf2_loclist_baton));
7421 baton->objfile = cu->objfile;
7422
7423 /* We don't know how long the location list is, but make sure we
7424 don't run off the edge of the section. */
7425 baton->size = dwarf_loc_size - DW_UNSND (attr);
7426 baton->data = dwarf_loc_buffer + DW_UNSND (attr);
7427 baton->base_address = cu->header.base_address;
7428 if (cu->header.base_known == 0)
7429 complaint (&symfile_complaints,
7430 "Location list used without specifying the CU base address.");
7431
7432 SYMBOL_LOCATION_FUNCS (sym) = &dwarf2_loclist_funcs;
7433 SYMBOL_LOCATION_BATON (sym) = baton;
7434 }
7435 else
7436 {
7437 struct dwarf2_locexpr_baton *baton;
7438
7439 baton = obstack_alloc (&cu->objfile->symbol_obstack,
7440 sizeof (struct dwarf2_locexpr_baton));
7441 baton->objfile = cu->objfile;
7442
7443 if (attr_form_is_block (attr))
7444 {
7445 /* Note that we're just copying the block's data pointer
7446 here, not the actual data. We're still pointing into the
7447 dwarf_info_buffer for SYM's objfile; right now we never
7448 release that buffer, but when we do clean up properly
7449 this may need to change. */
7450 baton->size = DW_BLOCK (attr)->size;
7451 baton->data = DW_BLOCK (attr)->data;
7452 }
7453 else
7454 {
7455 dwarf2_invalid_attrib_class_complaint ("location description",
7456 SYMBOL_NATURAL_NAME (sym));
7457 baton->size = 0;
7458 baton->data = NULL;
7459 }
7460
7461 SYMBOL_LOCATION_FUNCS (sym) = &dwarf2_locexpr_funcs;
7462 SYMBOL_LOCATION_BATON (sym) = baton;
7463 }
7464 }
This page took 0.191068 seconds and 4 git commands to generate.