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