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