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