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