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