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