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