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