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