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