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