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