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