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