OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [gdb/] [dwarf2read.c] - Blame information for rev 1767

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.