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