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