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