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