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