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