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