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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-7.1/] [gdb/] [dwarf2read.c] - Blame information for rev 227

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 227 jeremybenn
/* DWARF 2 debugging format support for GDB.
2
 
3
   Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4
                 2004, 2005, 2006, 2007, 2008, 2009, 2010
5
                 Free Software Foundation, Inc.
6
 
7
   Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8
   Inc.  with support from Florida State University (under contract
9
   with the Ada Joint Program Office), and Silicon Graphics, Inc.
10
   Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11
   based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12
   support.
13
 
14
   This file is part of GDB.
15
 
16
   This program is free software; you can redistribute it and/or modify
17
   it under the terms of the GNU General Public License as published by
18
   the Free Software Foundation; either version 3 of the License, or
19
   (at your option) any later version.
20
 
21
   This program is distributed in the hope that it will be useful,
22
   but WITHOUT ANY WARRANTY; without even the implied warranty of
23
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
   GNU General Public License for more details.
25
 
26
   You should have received a copy of the GNU General Public License
27
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
28
 
29
#include "defs.h"
30
#include "bfd.h"
31
#include "symtab.h"
32
#include "gdbtypes.h"
33
#include "objfiles.h"
34
#include "dwarf2.h"
35
#include "buildsym.h"
36
#include "demangle.h"
37
#include "expression.h"
38
#include "filenames.h"  /* for DOSish file names */
39
#include "macrotab.h"
40
#include "language.h"
41
#include "complaints.h"
42
#include "bcache.h"
43
#include "dwarf2expr.h"
44
#include "dwarf2loc.h"
45
#include "cp-support.h"
46
#include "hashtab.h"
47
#include "command.h"
48
#include "gdbcmd.h"
49
#include "block.h"
50
#include "addrmap.h"
51
 
52
#include <fcntl.h>
53
#include "gdb_string.h"
54
#include "gdb_assert.h"
55
#include <sys/types.h>
56
#ifdef HAVE_ZLIB_H
57
#include <zlib.h>
58
#endif
59
#ifdef HAVE_MMAP
60
#include <sys/mman.h>
61
#ifndef MAP_FAILED
62
#define MAP_FAILED ((void *) -1)
63
#endif
64
#endif
65
 
66
#if 0
67
/* .debug_info header for a compilation unit
68
   Because of alignment constraints, this structure has padding and cannot
69
   be mapped directly onto the beginning of the .debug_info section.  */
70
typedef struct comp_unit_header
71
  {
72
    unsigned int length;        /* length of the .debug_info
73
                                   contribution */
74
    unsigned short version;     /* version number -- 2 for DWARF
75
                                   version 2 */
76
    unsigned int abbrev_offset; /* offset into .debug_abbrev section */
77
    unsigned char addr_size;    /* byte size of an address -- 4 */
78
  }
79
_COMP_UNIT_HEADER;
80
#define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
81
#endif
82
 
83
/* .debug_pubnames header
84
   Because of alignment constraints, this structure has padding and cannot
85
   be mapped directly onto the beginning of the .debug_info section.  */
86
typedef struct pubnames_header
87
  {
88
    unsigned int length;        /* length of the .debug_pubnames
89
                                   contribution  */
90
    unsigned char version;      /* version number -- 2 for DWARF
91
                                   version 2 */
92
    unsigned int info_offset;   /* offset into .debug_info section */
93
    unsigned int info_size;     /* byte size of .debug_info section
94
                                   portion */
95
  }
96
_PUBNAMES_HEADER;
97
#define _ACTUAL_PUBNAMES_HEADER_SIZE 13
98
 
99
/* .debug_pubnames header
100
   Because of alignment constraints, this structure has padding and cannot
101
   be mapped directly onto the beginning of the .debug_info section.  */
102
typedef struct aranges_header
103
  {
104
    unsigned int length;        /* byte len of the .debug_aranges
105
                                   contribution */
106
    unsigned short version;     /* version number -- 2 for DWARF
107
                                   version 2 */
108
    unsigned int info_offset;   /* offset into .debug_info section */
109
    unsigned char addr_size;    /* byte size of an address */
110
    unsigned char seg_size;     /* byte size of segment descriptor */
111
  }
112
_ARANGES_HEADER;
113
#define _ACTUAL_ARANGES_HEADER_SIZE 12
114
 
115
/* .debug_line statement program prologue
116
   Because of alignment constraints, this structure has padding and cannot
117
   be mapped directly onto the beginning of the .debug_info section.  */
118
typedef struct statement_prologue
119
  {
120
    unsigned int total_length;  /* byte length of the statement
121
                                   information */
122
    unsigned short version;     /* version number -- 2 for DWARF
123
                                   version 2 */
124
    unsigned int prologue_length;       /* # bytes between prologue &
125
                                           stmt program */
126
    unsigned char minimum_instruction_length;   /* byte size of
127
                                                   smallest instr */
128
    unsigned char default_is_stmt;      /* initial value of is_stmt
129
                                           register */
130
    char line_base;
131
    unsigned char line_range;
132
    unsigned char opcode_base;  /* number assigned to first special
133
                                   opcode */
134
    unsigned char *standard_opcode_lengths;
135
  }
136
_STATEMENT_PROLOGUE;
137
 
138
/* When non-zero, dump DIEs after they are read in.  */
139
static int dwarf2_die_debug = 0;
140
 
141
static int pagesize;
142
 
143
/* When set, the file that we're processing is known to have debugging
144
   info for C++ namespaces.  GCC 3.3.x did not produce this information,
145
   but later versions do.  */
146
 
147
static int processing_has_namespace_info;
148
 
149
static const struct objfile_data *dwarf2_objfile_data_key;
150
 
151
struct dwarf2_section_info
152
{
153
  asection *asection;
154
  gdb_byte *buffer;
155
  bfd_size_type size;
156
  int was_mmapped;
157
};
158
 
159
struct dwarf2_per_objfile
160
{
161
  struct dwarf2_section_info info;
162
  struct dwarf2_section_info abbrev;
163
  struct dwarf2_section_info line;
164
  struct dwarf2_section_info pubnames;
165
  struct dwarf2_section_info aranges;
166
  struct dwarf2_section_info loc;
167
  struct dwarf2_section_info macinfo;
168
  struct dwarf2_section_info str;
169
  struct dwarf2_section_info ranges;
170
  struct dwarf2_section_info types;
171
  struct dwarf2_section_info frame;
172
  struct dwarf2_section_info eh_frame;
173
 
174
  /* A list of all the compilation units.  This is used to locate
175
     the target compilation unit of a particular reference.  */
176
  struct dwarf2_per_cu_data **all_comp_units;
177
 
178
  /* The number of compilation units in ALL_COMP_UNITS.  */
179
  int n_comp_units;
180
 
181
  /* A chain of compilation units that are currently read in, so that
182
     they can be freed later.  */
183
  struct dwarf2_per_cu_data *read_in_chain;
184
 
185
  /* A table mapping .debug_types signatures to its signatured_type entry.
186
     This is NULL if the .debug_types section hasn't been read in yet.  */
187
  htab_t signatured_types;
188
 
189
  /* A flag indicating wether this objfile has a section loaded at a
190
     VMA of 0.  */
191
  int has_section_at_zero;
192
};
193
 
194
static struct dwarf2_per_objfile *dwarf2_per_objfile;
195
 
196
/* names of the debugging sections */
197
 
198
/* Note that if the debugging section has been compressed, it might
199
   have a name like .zdebug_info.  */
200
 
201
#define INFO_SECTION     "debug_info"
202
#define ABBREV_SECTION   "debug_abbrev"
203
#define LINE_SECTION     "debug_line"
204
#define PUBNAMES_SECTION "debug_pubnames"
205
#define ARANGES_SECTION  "debug_aranges"
206
#define LOC_SECTION      "debug_loc"
207
#define MACINFO_SECTION  "debug_macinfo"
208
#define STR_SECTION      "debug_str"
209
#define RANGES_SECTION   "debug_ranges"
210
#define TYPES_SECTION    "debug_types"
211
#define FRAME_SECTION    "debug_frame"
212
#define EH_FRAME_SECTION "eh_frame"
213
 
214
/* local data types */
215
 
216
/* We hold several abbreviation tables in memory at the same time. */
217
#ifndef ABBREV_HASH_SIZE
218
#define ABBREV_HASH_SIZE 121
219
#endif
220
 
221
/* The data in a compilation unit header, after target2host
222
   translation, looks like this.  */
223
struct comp_unit_head
224
{
225
  unsigned int length;
226
  short version;
227
  unsigned char addr_size;
228
  unsigned char signed_addr_p;
229
  unsigned int abbrev_offset;
230
 
231
  /* Size of file offsets; either 4 or 8.  */
232
  unsigned int offset_size;
233
 
234
  /* Size of the length field; either 4 or 12.  */
235
  unsigned int initial_length_size;
236
 
237
  /* Offset to the first byte of this compilation unit header in the
238
     .debug_info section, for resolving relative reference dies.  */
239
  unsigned int offset;
240
 
241
  /* Offset to first die in this cu from the start of the cu.
242
     This will be the first byte following the compilation unit header.  */
243
  unsigned int first_die_offset;
244
};
245
 
246
/* Internal state when decoding a particular compilation unit.  */
247
struct dwarf2_cu
248
{
249
  /* The objfile containing this compilation unit.  */
250
  struct objfile *objfile;
251
 
252
  /* The header of the compilation unit.  */
253
  struct comp_unit_head header;
254
 
255
  /* Base address of this compilation unit.  */
256
  CORE_ADDR base_address;
257
 
258
  /* Non-zero if base_address has been set.  */
259
  int base_known;
260
 
261
  struct function_range *first_fn, *last_fn, *cached_fn;
262
 
263
  /* The language we are debugging.  */
264
  enum language language;
265
  const struct language_defn *language_defn;
266
 
267
  const char *producer;
268
 
269
  /* The generic symbol table building routines have separate lists for
270
     file scope symbols and all all other scopes (local scopes).  So
271
     we need to select the right one to pass to add_symbol_to_list().
272
     We do it by keeping a pointer to the correct list in list_in_scope.
273
 
274
     FIXME: The original dwarf code just treated the file scope as the
275
     first local scope, and all other local scopes as nested local
276
     scopes, and worked fine.  Check to see if we really need to
277
     distinguish these in buildsym.c.  */
278
  struct pending **list_in_scope;
279
 
280
  /* DWARF abbreviation table associated with this compilation unit.  */
281
  struct abbrev_info **dwarf2_abbrevs;
282
 
283
  /* Storage for the abbrev table.  */
284
  struct obstack abbrev_obstack;
285
 
286
  /* Hash table holding all the loaded partial DIEs.  */
287
  htab_t partial_dies;
288
 
289
  /* Storage for things with the same lifetime as this read-in compilation
290
     unit, including partial DIEs.  */
291
  struct obstack comp_unit_obstack;
292
 
293
  /* When multiple dwarf2_cu structures are living in memory, this field
294
     chains them all together, so that they can be released efficiently.
295
     We will probably also want a generation counter so that most-recently-used
296
     compilation units are cached...  */
297
  struct dwarf2_per_cu_data *read_in_chain;
298
 
299
  /* Backchain to our per_cu entry if the tree has been built.  */
300
  struct dwarf2_per_cu_data *per_cu;
301
 
302
  /* Pointer to the die -> type map.  Although it is stored
303
     permanently in per_cu, we copy it here to avoid double
304
     indirection.  */
305
  htab_t type_hash;
306
 
307
  /* How many compilation units ago was this CU last referenced?  */
308
  int last_used;
309
 
310
  /* A hash table of die offsets for following references.  */
311
  htab_t die_hash;
312
 
313
  /* Full DIEs if read in.  */
314
  struct die_info *dies;
315
 
316
  /* A set of pointers to dwarf2_per_cu_data objects for compilation
317
     units referenced by this one.  Only set during full symbol processing;
318
     partial symbol tables do not have dependencies.  */
319
  htab_t dependencies;
320
 
321
  /* Header data from the line table, during full symbol processing.  */
322
  struct line_header *line_header;
323
 
324
  /* Mark used when releasing cached dies.  */
325
  unsigned int mark : 1;
326
 
327
  /* This flag will be set if this compilation unit might include
328
     inter-compilation-unit references.  */
329
  unsigned int has_form_ref_addr : 1;
330
 
331
  /* This flag will be set if this compilation unit includes any
332
     DW_TAG_namespace DIEs.  If we know that there are explicit
333
     DIEs for namespaces, we don't need to try to infer them
334
     from mangled names.  */
335
  unsigned int has_namespace_info : 1;
336
};
337
 
338
/* Persistent data held for a compilation unit, even when not
339
   processing it.  We put a pointer to this structure in the
340
   read_symtab_private field of the psymtab.  If we encounter
341
   inter-compilation-unit references, we also maintain a sorted
342
   list of all compilation units.  */
343
 
344
struct dwarf2_per_cu_data
345
{
346
  /* The start offset and length of this compilation unit.  2**29-1
347
     bytes should suffice to store the length of any compilation unit
348
     - if it doesn't, GDB will fall over anyway.
349
     NOTE: Unlike comp_unit_head.length, this length includes
350
     initial_length_size.  */
351
  unsigned int offset;
352
  unsigned int length : 29;
353
 
354
  /* Flag indicating this compilation unit will be read in before
355
     any of the current compilation units are processed.  */
356
  unsigned int queued : 1;
357
 
358
  /* This flag will be set if we need to load absolutely all DIEs
359
     for this compilation unit, instead of just the ones we think
360
     are interesting.  It gets set if we look for a DIE in the
361
     hash table and don't find it.  */
362
  unsigned int load_all_dies : 1;
363
 
364
  /* Non-zero if this CU is from .debug_types.
365
     Otherwise it's from .debug_info.  */
366
  unsigned int from_debug_types : 1;
367
 
368
  /* Set iff currently read in.  */
369
  struct dwarf2_cu *cu;
370
 
371
  /* If full symbols for this CU have been read in, then this field
372
     holds a map of DIE offsets to types.  It isn't always possible
373
     to reconstruct this information later, so we have to preserve
374
     it.  */
375
  htab_t type_hash;
376
 
377
  /* The partial symbol table associated with this compilation unit,
378
     or NULL for partial units (which do not have an associated
379
     symtab).  */
380
  struct partial_symtab *psymtab;
381
};
382
 
383
/* Entry in the signatured_types hash table.  */
384
 
385
struct signatured_type
386
{
387
  ULONGEST signature;
388
 
389
  /* Offset in .debug_types of the TU (type_unit) for this type.  */
390
  unsigned int offset;
391
 
392
  /* Offset in .debug_types of the type defined by this TU.  */
393
  unsigned int type_offset;
394
 
395
  /* The CU(/TU) of this type.  */
396
  struct dwarf2_per_cu_data per_cu;
397
};
398
 
399
/* Struct used to pass misc. parameters to read_die_and_children, et. al.
400
   which are used for both .debug_info and .debug_types dies.
401
   All parameters here are unchanging for the life of the call.
402
   This struct exists to abstract away the constant parameters of
403
   die reading.  */
404
 
405
struct die_reader_specs
406
{
407
  /* The bfd of this objfile.  */
408
  bfd* abfd;
409
 
410
  /* The CU of the DIE we are parsing.  */
411
  struct dwarf2_cu *cu;
412
 
413
  /* Pointer to start of section buffer.
414
     This is either the start of .debug_info or .debug_types.  */
415
  const gdb_byte *buffer;
416
};
417
 
418
/* The line number information for a compilation unit (found in the
419
   .debug_line section) begins with a "statement program header",
420
   which contains the following information.  */
421
struct line_header
422
{
423
  unsigned int total_length;
424
  unsigned short version;
425
  unsigned int header_length;
426
  unsigned char minimum_instruction_length;
427
  unsigned char default_is_stmt;
428
  int line_base;
429
  unsigned char line_range;
430
  unsigned char opcode_base;
431
 
432
  /* standard_opcode_lengths[i] is the number of operands for the
433
     standard opcode whose value is i.  This means that
434
     standard_opcode_lengths[0] is unused, and the last meaningful
435
     element is standard_opcode_lengths[opcode_base - 1].  */
436
  unsigned char *standard_opcode_lengths;
437
 
438
  /* The include_directories table.  NOTE!  These strings are not
439
     allocated with xmalloc; instead, they are pointers into
440
     debug_line_buffer.  If you try to free them, `free' will get
441
     indigestion.  */
442
  unsigned int num_include_dirs, include_dirs_size;
443
  char **include_dirs;
444
 
445
  /* The file_names table.  NOTE!  These strings are not allocated
446
     with xmalloc; instead, they are pointers into debug_line_buffer.
447
     Don't try to free them directly.  */
448
  unsigned int num_file_names, file_names_size;
449
  struct file_entry
450
  {
451
    char *name;
452
    unsigned int dir_index;
453
    unsigned int mod_time;
454
    unsigned int length;
455
    int included_p; /* Non-zero if referenced by the Line Number Program.  */
456
    struct symtab *symtab; /* The associated symbol table, if any.  */
457
  } *file_names;
458
 
459
  /* The start and end of the statement program following this
460
     header.  These point into dwarf2_per_objfile->line_buffer.  */
461
  gdb_byte *statement_program_start, *statement_program_end;
462
};
463
 
464
/* When we construct a partial symbol table entry we only
465
   need this much information. */
466
struct partial_die_info
467
  {
468
    /* Offset of this DIE.  */
469
    unsigned int offset;
470
 
471
    /* DWARF-2 tag for this DIE.  */
472
    ENUM_BITFIELD(dwarf_tag) tag : 16;
473
 
474
    /* Assorted flags describing the data found in this DIE.  */
475
    unsigned int has_children : 1;
476
    unsigned int is_external : 1;
477
    unsigned int is_declaration : 1;
478
    unsigned int has_type : 1;
479
    unsigned int has_specification : 1;
480
    unsigned int has_pc_info : 1;
481
 
482
    /* Flag set if the SCOPE field of this structure has been
483
       computed.  */
484
    unsigned int scope_set : 1;
485
 
486
    /* Flag set if the DIE has a byte_size attribute.  */
487
    unsigned int has_byte_size : 1;
488
 
489
    /* The name of this DIE.  Normally the value of DW_AT_name, but
490
       sometimes DW_TAG_MIPS_linkage_name or a string computed in some
491
       other fashion.  */
492
    char *name;
493
 
494
    /* The scope to prepend to our children.  This is generally
495
       allocated on the comp_unit_obstack, so will disappear
496
       when this compilation unit leaves the cache.  */
497
    char *scope;
498
 
499
    /* The location description associated with this DIE, if any.  */
500
    struct dwarf_block *locdesc;
501
 
502
    /* If HAS_PC_INFO, the PC range associated with this DIE.  */
503
    CORE_ADDR lowpc;
504
    CORE_ADDR highpc;
505
 
506
    /* Pointer into the info_buffer (or types_buffer) pointing at the target of
507
       DW_AT_sibling, if any.  */
508
    gdb_byte *sibling;
509
 
510
    /* If HAS_SPECIFICATION, the offset of the DIE referred to by
511
       DW_AT_specification (or DW_AT_abstract_origin or
512
       DW_AT_extension).  */
513
    unsigned int spec_offset;
514
 
515
    /* Pointers to this DIE's parent, first child, and next sibling,
516
       if any.  */
517
    struct partial_die_info *die_parent, *die_child, *die_sibling;
518
  };
519
 
520
/* This data structure holds the information of an abbrev. */
521
struct abbrev_info
522
  {
523
    unsigned int number;        /* number identifying abbrev */
524
    enum dwarf_tag tag;         /* dwarf tag */
525
    unsigned short has_children;                /* boolean */
526
    unsigned short num_attrs;   /* number of attributes */
527
    struct attr_abbrev *attrs;  /* an array of attribute descriptions */
528
    struct abbrev_info *next;   /* next in chain */
529
  };
530
 
531
struct attr_abbrev
532
  {
533
    ENUM_BITFIELD(dwarf_attribute) name : 16;
534
    ENUM_BITFIELD(dwarf_form) form : 16;
535
  };
536
 
537
/* Attributes have a name and a value */
538
struct attribute
539
  {
540
    ENUM_BITFIELD(dwarf_attribute) name : 16;
541
    ENUM_BITFIELD(dwarf_form) form : 15;
542
 
543
    /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
544
       field should be in u.str (existing only for DW_STRING) but it is kept
545
       here for better struct attribute alignment.  */
546
    unsigned int string_is_canonical : 1;
547
 
548
    union
549
      {
550
        char *str;
551
        struct dwarf_block *blk;
552
        ULONGEST unsnd;
553
        LONGEST snd;
554
        CORE_ADDR addr;
555
        struct signatured_type *signatured_type;
556
      }
557
    u;
558
  };
559
 
560
/* This data structure holds a complete die structure. */
561
struct die_info
562
  {
563
    /* DWARF-2 tag for this DIE.  */
564
    ENUM_BITFIELD(dwarf_tag) tag : 16;
565
 
566
    /* Number of attributes */
567
    unsigned short num_attrs;
568
 
569
    /* Abbrev number */
570
    unsigned int abbrev;
571
 
572
    /* Offset in .debug_info or .debug_types section.  */
573
    unsigned int offset;
574
 
575
    /* The dies in a compilation unit form an n-ary tree.  PARENT
576
       points to this die's parent; CHILD points to the first child of
577
       this node; and all the children of a given node are chained
578
       together via their SIBLING fields, terminated by a die whose
579
       tag is zero.  */
580
    struct die_info *child;     /* Its first child, if any.  */
581
    struct die_info *sibling;   /* Its next sibling, if any.  */
582
    struct die_info *parent;    /* Its parent, if any.  */
583
 
584
    /* An array of attributes, with NUM_ATTRS elements.  There may be
585
       zero, but it's not common and zero-sized arrays are not
586
       sufficiently portable C.  */
587
    struct attribute attrs[1];
588
  };
589
 
590
struct function_range
591
{
592
  const char *name;
593
  CORE_ADDR lowpc, highpc;
594
  int seen_line;
595
  struct function_range *next;
596
};
597
 
598
/* Get at parts of an attribute structure */
599
 
600
#define DW_STRING(attr)    ((attr)->u.str)
601
#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
602
#define DW_UNSND(attr)     ((attr)->u.unsnd)
603
#define DW_BLOCK(attr)     ((attr)->u.blk)
604
#define DW_SND(attr)       ((attr)->u.snd)
605
#define DW_ADDR(attr)      ((attr)->u.addr)
606
#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
607
 
608
/* Blocks are a bunch of untyped bytes. */
609
struct dwarf_block
610
  {
611
    unsigned int size;
612
    gdb_byte *data;
613
  };
614
 
615
#ifndef ATTR_ALLOC_CHUNK
616
#define ATTR_ALLOC_CHUNK 4
617
#endif
618
 
619
/* Allocate fields for structs, unions and enums in this size.  */
620
#ifndef DW_FIELD_ALLOC_CHUNK
621
#define DW_FIELD_ALLOC_CHUNK 4
622
#endif
623
 
624
/* A zeroed version of a partial die for initialization purposes.  */
625
static struct partial_die_info zeroed_partial_die;
626
 
627
/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
628
   but this would require a corresponding change in unpack_field_as_long
629
   and friends.  */
630
static int bits_per_byte = 8;
631
 
632
/* The routines that read and process dies for a C struct or C++ class
633
   pass lists of data member fields and lists of member function fields
634
   in an instance of a field_info structure, as defined below.  */
635
struct field_info
636
  {
637
    /* List of data member and baseclasses fields. */
638
    struct nextfield
639
      {
640
        struct nextfield *next;
641
        int accessibility;
642
        int virtuality;
643
        struct field field;
644
      }
645
     *fields, *baseclasses;
646
 
647
    /* Number of fields (including baseclasses).  */
648
    int nfields;
649
 
650
    /* Number of baseclasses.  */
651
    int nbaseclasses;
652
 
653
    /* Set if the accesibility of one of the fields is not public.  */
654
    int non_public_fields;
655
 
656
    /* Member function fields array, entries are allocated in the order they
657
       are encountered in the object file.  */
658
    struct nextfnfield
659
      {
660
        struct nextfnfield *next;
661
        struct fn_field fnfield;
662
      }
663
     *fnfields;
664
 
665
    /* Member function fieldlist array, contains name of possibly overloaded
666
       member function, number of overloaded member functions and a pointer
667
       to the head of the member function field chain.  */
668
    struct fnfieldlist
669
      {
670
        char *name;
671
        int length;
672
        struct nextfnfield *head;
673
      }
674
     *fnfieldlists;
675
 
676
    /* Number of entries in the fnfieldlists array.  */
677
    int nfnfields;
678
  };
679
 
680
/* One item on the queue of compilation units to read in full symbols
681
   for.  */
682
struct dwarf2_queue_item
683
{
684
  struct dwarf2_per_cu_data *per_cu;
685
  struct dwarf2_queue_item *next;
686
};
687
 
688
/* The current queue.  */
689
static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
690
 
691
/* Loaded secondary compilation units are kept in memory until they
692
   have not been referenced for the processing of this many
693
   compilation units.  Set this to zero to disable caching.  Cache
694
   sizes of up to at least twenty will improve startup time for
695
   typical inter-CU-reference binaries, at an obvious memory cost.  */
696
static int dwarf2_max_cache_age = 5;
697
static void
698
show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
699
                           struct cmd_list_element *c, const char *value)
700
{
701
  fprintf_filtered (file, _("\
702
The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
703
                    value);
704
}
705
 
706
 
707
/* Various complaints about symbol reading that don't abort the process */
708
 
709
static void
710
dwarf2_statement_list_fits_in_line_number_section_complaint (void)
711
{
712
  complaint (&symfile_complaints,
713
             _("statement list doesn't fit in .debug_line section"));
714
}
715
 
716
static void
717
dwarf2_debug_line_missing_file_complaint (void)
718
{
719
  complaint (&symfile_complaints,
720
             _(".debug_line section has line data without a file"));
721
}
722
 
723
static void
724
dwarf2_debug_line_missing_end_sequence_complaint (void)
725
{
726
  complaint (&symfile_complaints,
727
             _(".debug_line section has line program sequence without an end"));
728
}
729
 
730
static void
731
dwarf2_complex_location_expr_complaint (void)
732
{
733
  complaint (&symfile_complaints, _("location expression too complex"));
734
}
735
 
736
static void
737
dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
738
                                              int arg3)
739
{
740
  complaint (&symfile_complaints,
741
             _("const value length mismatch for '%s', got %d, expected %d"), arg1,
742
             arg2, arg3);
743
}
744
 
745
static void
746
dwarf2_macros_too_long_complaint (void)
747
{
748
  complaint (&symfile_complaints,
749
             _("macro info runs off end of `.debug_macinfo' section"));
750
}
751
 
752
static void
753
dwarf2_macro_malformed_definition_complaint (const char *arg1)
754
{
755
  complaint (&symfile_complaints,
756
             _("macro debug info contains a malformed macro definition:\n`%s'"),
757
             arg1);
758
}
759
 
760
static void
761
dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
762
{
763
  complaint (&symfile_complaints,
764
             _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
765
}
766
 
767
/* local function prototypes */
768
 
769
static void dwarf2_locate_sections (bfd *, asection *, void *);
770
 
771
#if 0
772
static void dwarf2_build_psymtabs_easy (struct objfile *);
773
#endif
774
 
775
static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
776
                                           struct objfile *);
777
 
778
static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
779
                                           struct die_info *,
780
                                           struct partial_symtab *);
781
 
782
static void dwarf2_build_psymtabs_hard (struct objfile *);
783
 
784
static void scan_partial_symbols (struct partial_die_info *,
785
                                  CORE_ADDR *, CORE_ADDR *,
786
                                  int, struct dwarf2_cu *);
787
 
788
static void add_partial_symbol (struct partial_die_info *,
789
                                struct dwarf2_cu *);
790
 
791
static int pdi_needs_namespace (enum dwarf_tag tag);
792
 
793
static void add_partial_namespace (struct partial_die_info *pdi,
794
                                   CORE_ADDR *lowpc, CORE_ADDR *highpc,
795
                                   int need_pc, struct dwarf2_cu *cu);
796
 
797
static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
798
                                CORE_ADDR *highpc, int need_pc,
799
                                struct dwarf2_cu *cu);
800
 
801
static void add_partial_enumeration (struct partial_die_info *enum_pdi,
802
                                     struct dwarf2_cu *cu);
803
 
804
static void add_partial_subprogram (struct partial_die_info *pdi,
805
                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
806
                                    int need_pc, struct dwarf2_cu *cu);
807
 
808
static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
809
                                     gdb_byte *buffer, gdb_byte *info_ptr,
810
                                     bfd *abfd, struct dwarf2_cu *cu);
811
 
812
static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
813
 
814
static void psymtab_to_symtab_1 (struct partial_symtab *);
815
 
816
static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
817
 
818
static void dwarf2_free_abbrev_table (void *);
819
 
820
static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
821
                                            struct dwarf2_cu *);
822
 
823
static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
824
                                                 struct dwarf2_cu *);
825
 
826
static struct partial_die_info *load_partial_dies (bfd *,
827
                                                   gdb_byte *, gdb_byte *,
828
                                                   int, struct dwarf2_cu *);
829
 
830
static gdb_byte *read_partial_die (struct partial_die_info *,
831
                                   struct abbrev_info *abbrev,
832
                                   unsigned int, bfd *,
833
                                   gdb_byte *, gdb_byte *,
834
                                   struct dwarf2_cu *);
835
 
836
static struct partial_die_info *find_partial_die (unsigned int,
837
                                                  struct dwarf2_cu *);
838
 
839
static void fixup_partial_die (struct partial_die_info *,
840
                               struct dwarf2_cu *);
841
 
842
static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
843
                                 bfd *, gdb_byte *, struct dwarf2_cu *);
844
 
845
static gdb_byte *read_attribute_value (struct attribute *, unsigned,
846
                                       bfd *, gdb_byte *, struct dwarf2_cu *);
847
 
848
static unsigned int read_1_byte (bfd *, gdb_byte *);
849
 
850
static int read_1_signed_byte (bfd *, gdb_byte *);
851
 
852
static unsigned int read_2_bytes (bfd *, gdb_byte *);
853
 
854
static unsigned int read_4_bytes (bfd *, gdb_byte *);
855
 
856
static ULONGEST read_8_bytes (bfd *, gdb_byte *);
857
 
858
static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
859
                               unsigned int *);
860
 
861
static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
862
 
863
static LONGEST read_checked_initial_length_and_offset
864
  (bfd *, gdb_byte *, const struct comp_unit_head *,
865
   unsigned int *, unsigned int *);
866
 
867
static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
868
                            unsigned int *);
869
 
870
static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
871
 
872
static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
873
 
874
static char *read_string (bfd *, gdb_byte *, unsigned int *);
875
 
876
static char *read_indirect_string (bfd *, gdb_byte *,
877
                                   const struct comp_unit_head *,
878
                                   unsigned int *);
879
 
880
static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
881
 
882
static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
883
 
884
static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
885
 
886
static void set_cu_language (unsigned int, struct dwarf2_cu *);
887
 
888
static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
889
                                      struct dwarf2_cu *);
890
 
891
static struct attribute *dwarf2_attr_no_follow (struct die_info *,
892
                                                unsigned int,
893
                                                struct dwarf2_cu *);
894
 
895
static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
896
                               struct dwarf2_cu *cu);
897
 
898
static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
899
 
900
static struct die_info *die_specification (struct die_info *die,
901
                                           struct dwarf2_cu **);
902
 
903
static void free_line_header (struct line_header *lh);
904
 
905
static void add_file_name (struct line_header *, char *, unsigned int,
906
                           unsigned int, unsigned int);
907
 
908
static struct line_header *(dwarf_decode_line_header
909
                            (unsigned int offset,
910
                             bfd *abfd, struct dwarf2_cu *cu));
911
 
912
static void dwarf_decode_lines (struct line_header *, char *, bfd *,
913
                                struct dwarf2_cu *, struct partial_symtab *);
914
 
915
static void dwarf2_start_subfile (char *, char *, char *);
916
 
917
static struct symbol *new_symbol (struct die_info *, struct type *,
918
                                  struct dwarf2_cu *);
919
 
920
static void dwarf2_const_value (struct attribute *, struct symbol *,
921
                                struct dwarf2_cu *);
922
 
923
static void dwarf2_const_value_data (struct attribute *attr,
924
                                     struct symbol *sym,
925
                                     int bits);
926
 
927
static struct type *die_type (struct die_info *, struct dwarf2_cu *);
928
 
929
static int need_gnat_info (struct dwarf2_cu *);
930
 
931
static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
932
 
933
static void set_descriptive_type (struct type *, struct die_info *,
934
                                  struct dwarf2_cu *);
935
 
936
static struct type *die_containing_type (struct die_info *,
937
                                         struct dwarf2_cu *);
938
 
939
static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
940
 
941
static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
942
 
943
static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
944
 
945
static char *typename_concat (struct obstack *,
946
                              const char *prefix,
947
                              const char *suffix,
948
                              struct dwarf2_cu *);
949
 
950
static void read_file_scope (struct die_info *, struct dwarf2_cu *);
951
 
952
static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
953
 
954
static void read_func_scope (struct die_info *, struct dwarf2_cu *);
955
 
956
static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
957
 
958
static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
959
                               struct dwarf2_cu *, struct partial_symtab *);
960
 
961
static int dwarf2_get_pc_bounds (struct die_info *,
962
                                 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
963
                                 struct partial_symtab *);
964
 
965
static void get_scope_pc_bounds (struct die_info *,
966
                                 CORE_ADDR *, CORE_ADDR *,
967
                                 struct dwarf2_cu *);
968
 
969
static void dwarf2_record_block_ranges (struct die_info *, struct block *,
970
                                        CORE_ADDR, struct dwarf2_cu *);
971
 
972
static void dwarf2_add_field (struct field_info *, struct die_info *,
973
                              struct dwarf2_cu *);
974
 
975
static void dwarf2_attach_fields_to_type (struct field_info *,
976
                                          struct type *, struct dwarf2_cu *);
977
 
978
static void dwarf2_add_member_fn (struct field_info *,
979
                                  struct die_info *, struct type *,
980
                                  struct dwarf2_cu *);
981
 
982
static void dwarf2_attach_fn_fields_to_type (struct field_info *,
983
                                             struct type *, struct dwarf2_cu *);
984
 
985
static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
986
 
987
static const char *determine_class_name (struct die_info *die,
988
                                         struct dwarf2_cu *cu);
989
 
990
static void read_common_block (struct die_info *, struct dwarf2_cu *);
991
 
992
static void read_namespace (struct die_info *die, struct dwarf2_cu *);
993
 
994
static void read_module (struct die_info *die, struct dwarf2_cu *cu);
995
 
996
static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
997
 
998
static const char *namespace_name (struct die_info *die,
999
                                   int *is_anonymous, struct dwarf2_cu *);
1000
 
1001
static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1002
 
1003
static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1004
 
1005
static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1006
                                                       struct dwarf2_cu *);
1007
 
1008
static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1009
 
1010
static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1011
                                                 gdb_byte *info_ptr,
1012
                                                 gdb_byte **new_info_ptr,
1013
                                                 struct die_info *parent);
1014
 
1015
static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1016
                                               gdb_byte *info_ptr,
1017
                                               gdb_byte **new_info_ptr,
1018
                                               struct die_info *parent);
1019
 
1020
static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1021
                                               gdb_byte *info_ptr,
1022
                                               gdb_byte **new_info_ptr,
1023
                                               struct die_info *parent);
1024
 
1025
static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1026
                                struct die_info **, gdb_byte *,
1027
                                int *);
1028
 
1029
static void process_die (struct die_info *, struct dwarf2_cu *);
1030
 
1031
static char *dwarf2_linkage_name (struct die_info *, struct dwarf2_cu *);
1032
 
1033
static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1034
                                       struct obstack *);
1035
 
1036
static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1037
 
1038
static struct die_info *dwarf2_extension (struct die_info *die,
1039
                                          struct dwarf2_cu **);
1040
 
1041
static char *dwarf_tag_name (unsigned int);
1042
 
1043
static char *dwarf_attr_name (unsigned int);
1044
 
1045
static char *dwarf_form_name (unsigned int);
1046
 
1047
static char *dwarf_stack_op_name (unsigned int);
1048
 
1049
static char *dwarf_bool_name (unsigned int);
1050
 
1051
static char *dwarf_type_encoding_name (unsigned int);
1052
 
1053
#if 0
1054
static char *dwarf_cfi_name (unsigned int);
1055
#endif
1056
 
1057
static struct die_info *sibling_die (struct die_info *);
1058
 
1059
static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1060
 
1061
static void dump_die_for_error (struct die_info *);
1062
 
1063
static void dump_die_1 (struct ui_file *, int level, int max_level,
1064
                        struct die_info *);
1065
 
1066
/*static*/ void dump_die (struct die_info *, int max_level);
1067
 
1068
static void store_in_ref_table (struct die_info *,
1069
                                struct dwarf2_cu *);
1070
 
1071
static int is_ref_attr (struct attribute *);
1072
 
1073
static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1074
 
1075
static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1076
 
1077
static struct die_info *follow_die_ref_or_sig (struct die_info *,
1078
                                               struct attribute *,
1079
                                               struct dwarf2_cu **);
1080
 
1081
static struct die_info *follow_die_ref (struct die_info *,
1082
                                        struct attribute *,
1083
                                        struct dwarf2_cu **);
1084
 
1085
static struct die_info *follow_die_sig (struct die_info *,
1086
                                        struct attribute *,
1087
                                        struct dwarf2_cu **);
1088
 
1089
static void read_signatured_type_at_offset (struct objfile *objfile,
1090
                                            unsigned int offset);
1091
 
1092
static void read_signatured_type (struct objfile *,
1093
                                  struct signatured_type *type_sig);
1094
 
1095
/* memory allocation interface */
1096
 
1097
static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1098
 
1099
static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1100
 
1101
static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1102
 
1103
static void initialize_cu_func_list (struct dwarf2_cu *);
1104
 
1105
static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1106
                                 struct dwarf2_cu *);
1107
 
1108
static void dwarf_decode_macros (struct line_header *, unsigned int,
1109
                                 char *, bfd *, struct dwarf2_cu *);
1110
 
1111
static int attr_form_is_block (struct attribute *);
1112
 
1113
static int attr_form_is_section_offset (struct attribute *);
1114
 
1115
static int attr_form_is_constant (struct attribute *);
1116
 
1117
static void dwarf2_symbol_mark_computed (struct attribute *attr,
1118
                                         struct symbol *sym,
1119
                                         struct dwarf2_cu *cu);
1120
 
1121
static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1122
                               struct abbrev_info *abbrev,
1123
                               struct dwarf2_cu *cu);
1124
 
1125
static void free_stack_comp_unit (void *);
1126
 
1127
static hashval_t partial_die_hash (const void *item);
1128
 
1129
static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1130
 
1131
static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1132
  (unsigned int offset, struct objfile *objfile);
1133
 
1134
static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1135
  (unsigned int offset, struct objfile *objfile);
1136
 
1137
static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1138
 
1139
static void free_one_comp_unit (void *);
1140
 
1141
static void free_cached_comp_units (void *);
1142
 
1143
static void age_cached_comp_units (void);
1144
 
1145
static void free_one_cached_comp_unit (void *);
1146
 
1147
static struct type *set_die_type (struct die_info *, struct type *,
1148
                                  struct dwarf2_cu *);
1149
 
1150
static void create_all_comp_units (struct objfile *);
1151
 
1152
static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1153
                                 struct objfile *);
1154
 
1155
static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1156
 
1157
static void dwarf2_add_dependence (struct dwarf2_cu *,
1158
                                   struct dwarf2_per_cu_data *);
1159
 
1160
static void dwarf2_mark (struct dwarf2_cu *);
1161
 
1162
static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1163
 
1164
static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1165
 
1166
/* Try to locate the sections we need for DWARF 2 debugging
1167
   information and return true if we have enough to do something.  */
1168
 
1169
int
1170
dwarf2_has_info (struct objfile *objfile)
1171
{
1172
  struct dwarf2_per_objfile *data;
1173
 
1174
  /* Initialize per-objfile state.  */
1175
  data = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1176
  memset (data, 0, sizeof (*data));
1177
  set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1178
  dwarf2_per_objfile = data;
1179
 
1180
  bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1181
  return (data->info.asection != NULL && data->abbrev.asection != NULL);
1182
}
1183
 
1184
/* When loading sections, we can either look for ".<name>", or for
1185
 * ".z<name>", which indicates a compressed section.  */
1186
 
1187
static int
1188
section_is_p (const char *section_name, const char *name)
1189
{
1190
  return (section_name[0] == '.'
1191
          && (strcmp (section_name + 1, name) == 0
1192
              || (section_name[1] == 'z'
1193
                  && strcmp (section_name + 2, name) == 0)));
1194
}
1195
 
1196
/* This function is mapped across the sections and remembers the
1197
   offset and size of each of the debugging sections we are interested
1198
   in.  */
1199
 
1200
static void
1201
dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1202
{
1203
  if (section_is_p (sectp->name, INFO_SECTION))
1204
    {
1205
      dwarf2_per_objfile->info.asection = sectp;
1206
      dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1207
    }
1208
  else if (section_is_p (sectp->name, ABBREV_SECTION))
1209
    {
1210
      dwarf2_per_objfile->abbrev.asection = sectp;
1211
      dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1212
    }
1213
  else if (section_is_p (sectp->name, LINE_SECTION))
1214
    {
1215
      dwarf2_per_objfile->line.asection = sectp;
1216
      dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1217
    }
1218
  else if (section_is_p (sectp->name, PUBNAMES_SECTION))
1219
    {
1220
      dwarf2_per_objfile->pubnames.asection = sectp;
1221
      dwarf2_per_objfile->pubnames.size = bfd_get_section_size (sectp);
1222
    }
1223
  else if (section_is_p (sectp->name, ARANGES_SECTION))
1224
    {
1225
      dwarf2_per_objfile->aranges.asection = sectp;
1226
      dwarf2_per_objfile->aranges.size = bfd_get_section_size (sectp);
1227
    }
1228
  else if (section_is_p (sectp->name, LOC_SECTION))
1229
    {
1230
      dwarf2_per_objfile->loc.asection = sectp;
1231
      dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1232
    }
1233
  else if (section_is_p (sectp->name, MACINFO_SECTION))
1234
    {
1235
      dwarf2_per_objfile->macinfo.asection = sectp;
1236
      dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1237
    }
1238
  else if (section_is_p (sectp->name, STR_SECTION))
1239
    {
1240
      dwarf2_per_objfile->str.asection = sectp;
1241
      dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1242
    }
1243
  else if (section_is_p (sectp->name, FRAME_SECTION))
1244
    {
1245
      dwarf2_per_objfile->frame.asection = sectp;
1246
      dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1247
    }
1248
  else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1249
    {
1250
      flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1251
      if (aflag & SEC_HAS_CONTENTS)
1252
        {
1253
          dwarf2_per_objfile->eh_frame.asection = sectp;
1254
          dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1255
        }
1256
    }
1257
  else if (section_is_p (sectp->name, RANGES_SECTION))
1258
    {
1259
      dwarf2_per_objfile->ranges.asection = sectp;
1260
      dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1261
    }
1262
  else if (section_is_p (sectp->name, TYPES_SECTION))
1263
    {
1264
      dwarf2_per_objfile->types.asection = sectp;
1265
      dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1266
    }
1267
 
1268
  if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1269
      && bfd_section_vma (abfd, sectp) == 0)
1270
    dwarf2_per_objfile->has_section_at_zero = 1;
1271
}
1272
 
1273
/* Decompress a section that was compressed using zlib.  Store the
1274
   decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1275
 
1276
static void
1277
zlib_decompress_section (struct objfile *objfile, asection *sectp,
1278
                         gdb_byte **outbuf, bfd_size_type *outsize)
1279
{
1280
  bfd *abfd = objfile->obfd;
1281
#ifndef HAVE_ZLIB_H
1282
  error (_("Support for zlib-compressed DWARF data (from '%s') "
1283
           "is disabled in this copy of GDB"),
1284
         bfd_get_filename (abfd));
1285
#else
1286
  bfd_size_type compressed_size = bfd_get_section_size (sectp);
1287
  gdb_byte *compressed_buffer = xmalloc (compressed_size);
1288
  struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1289
  bfd_size_type uncompressed_size;
1290
  gdb_byte *uncompressed_buffer;
1291
  z_stream strm;
1292
  int rc;
1293
  int header_size = 12;
1294
 
1295
  if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1296
      || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1297
    error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1298
           bfd_get_filename (abfd));
1299
 
1300
  /* Read the zlib header.  In this case, it should be "ZLIB" followed
1301
     by the uncompressed section size, 8 bytes in big-endian order.  */
1302
  if (compressed_size < header_size
1303
      || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1304
    error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1305
           bfd_get_filename (abfd));
1306
  uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1307
  uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1308
  uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1309
  uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1310
  uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1311
  uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1312
  uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1313
  uncompressed_size += compressed_buffer[11];
1314
 
1315
  /* It is possible the section consists of several compressed
1316
     buffers concatenated together, so we uncompress in a loop.  */
1317
  strm.zalloc = NULL;
1318
  strm.zfree = NULL;
1319
  strm.opaque = NULL;
1320
  strm.avail_in = compressed_size - header_size;
1321
  strm.next_in = (Bytef*) compressed_buffer + header_size;
1322
  strm.avail_out = uncompressed_size;
1323
  uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1324
                                       uncompressed_size);
1325
  rc = inflateInit (&strm);
1326
  while (strm.avail_in > 0)
1327
    {
1328
      if (rc != Z_OK)
1329
        error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1330
               bfd_get_filename (abfd), rc);
1331
      strm.next_out = ((Bytef*) uncompressed_buffer
1332
                       + (uncompressed_size - strm.avail_out));
1333
      rc = inflate (&strm, Z_FINISH);
1334
      if (rc != Z_STREAM_END)
1335
        error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1336
               bfd_get_filename (abfd), rc);
1337
      rc = inflateReset (&strm);
1338
    }
1339
  rc = inflateEnd (&strm);
1340
  if (rc != Z_OK
1341
      || strm.avail_out != 0)
1342
    error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1343
           bfd_get_filename (abfd), rc);
1344
 
1345
  do_cleanups (cleanup);
1346
  *outbuf = uncompressed_buffer;
1347
  *outsize = uncompressed_size;
1348
#endif
1349
}
1350
 
1351
/* Read the contents of the section SECTP from object file specified by
1352
   OBJFILE, store info about the section into INFO.
1353
   If the section is compressed, uncompress it before returning.  */
1354
 
1355
static void
1356
dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1357
{
1358
  bfd *abfd = objfile->obfd;
1359
  asection *sectp = info->asection;
1360
  gdb_byte *buf, *retbuf;
1361
  unsigned char header[4];
1362
 
1363
  info->buffer = NULL;
1364
  info->was_mmapped = 0;
1365
 
1366
  if (info->asection == NULL || info->size == 0)
1367
    return;
1368
 
1369
  /* Check if the file has a 4-byte header indicating compression.  */
1370
  if (info->size > sizeof (header)
1371
      && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1372
      && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1373
    {
1374
      /* Upon decompression, update the buffer and its size.  */
1375
      if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1376
        {
1377
          zlib_decompress_section (objfile, sectp, &info->buffer,
1378
                                   &info->size);
1379
          return;
1380
        }
1381
    }
1382
 
1383
#ifdef HAVE_MMAP
1384
  if (pagesize == 0)
1385
    pagesize = getpagesize ();
1386
 
1387
  /* Only try to mmap sections which are large enough: we don't want to
1388
     waste space due to fragmentation.  Also, only try mmap for sections
1389
     without relocations.  */
1390
 
1391
  if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1392
    {
1393
      off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1394
      size_t map_length = info->size + sectp->filepos - pg_offset;
1395
      caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1396
                                 MAP_PRIVATE, pg_offset);
1397
 
1398
      if (retbuf != MAP_FAILED)
1399
        {
1400
          info->was_mmapped = 1;
1401
          info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1402
          return;
1403
        }
1404
    }
1405
#endif
1406
 
1407
  /* If we get here, we are a normal, not-compressed section.  */
1408
  info->buffer = buf
1409
    = obstack_alloc (&objfile->objfile_obstack, info->size);
1410
 
1411
  /* When debugging .o files, we may need to apply relocations; see
1412
     http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1413
     We never compress sections in .o files, so we only need to
1414
     try this when the section is not compressed.  */
1415
  retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1416
  if (retbuf != NULL)
1417
    {
1418
      info->buffer = retbuf;
1419
      return;
1420
    }
1421
 
1422
  if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1423
      || bfd_bread (buf, info->size, abfd) != info->size)
1424
    error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1425
           bfd_get_filename (abfd));
1426
}
1427
 
1428
/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1429
   SECTION_NAME. */
1430
 
1431
void
1432
dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1433
                         asection **sectp, gdb_byte **bufp,
1434
                         bfd_size_type *sizep)
1435
{
1436
  struct dwarf2_per_objfile *data
1437
    = objfile_data (objfile, dwarf2_objfile_data_key);
1438
  struct dwarf2_section_info *info;
1439
  if (section_is_p (section_name, EH_FRAME_SECTION))
1440
    info = &data->eh_frame;
1441
  else if (section_is_p (section_name, FRAME_SECTION))
1442
    info = &data->frame;
1443
  else
1444
    gdb_assert (0);
1445
 
1446
  if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1447
    /* We haven't read this section in yet.  Do it now.  */
1448
    dwarf2_read_section (objfile, info);
1449
 
1450
  *sectp = info->asection;
1451
  *bufp = info->buffer;
1452
  *sizep = info->size;
1453
}
1454
 
1455
/* Build a partial symbol table.  */
1456
 
1457
void
1458
dwarf2_build_psymtabs (struct objfile *objfile)
1459
{
1460
  dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
1461
  dwarf2_read_section (objfile, &dwarf2_per_objfile->abbrev);
1462
  dwarf2_read_section (objfile, &dwarf2_per_objfile->line);
1463
  dwarf2_read_section (objfile, &dwarf2_per_objfile->str);
1464
  dwarf2_read_section (objfile, &dwarf2_per_objfile->macinfo);
1465
  dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
1466
  dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
1467
  dwarf2_read_section (objfile, &dwarf2_per_objfile->loc);
1468
  dwarf2_read_section (objfile, &dwarf2_per_objfile->eh_frame);
1469
  dwarf2_read_section (objfile, &dwarf2_per_objfile->frame);
1470
 
1471
  if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
1472
    {
1473
      init_psymbol_list (objfile, 1024);
1474
    }
1475
 
1476
#if 0
1477
  if (dwarf_aranges_offset && dwarf_pubnames_offset)
1478
    {
1479
      /* Things are significantly easier if we have .debug_aranges and
1480
         .debug_pubnames sections */
1481
 
1482
      dwarf2_build_psymtabs_easy (objfile);
1483
    }
1484
  else
1485
#endif
1486
    /* only test this case for now */
1487
    {
1488
      /* In this case we have to work a bit harder */
1489
      dwarf2_build_psymtabs_hard (objfile);
1490
    }
1491
}
1492
 
1493
#if 0
1494
/* Build the partial symbol table from the information in the
1495
   .debug_pubnames and .debug_aranges sections.  */
1496
 
1497
static void
1498
dwarf2_build_psymtabs_easy (struct objfile *objfile)
1499
{
1500
  bfd *abfd = objfile->obfd;
1501
  char *aranges_buffer, *pubnames_buffer;
1502
  char *aranges_ptr, *pubnames_ptr;
1503
  unsigned int entry_length, version, info_offset, info_size;
1504
 
1505
  pubnames_buffer = dwarf2_read_section (objfile,
1506
                                         dwarf_pubnames_section);
1507
  pubnames_ptr = pubnames_buffer;
1508
  while ((pubnames_ptr - pubnames_buffer) < dwarf2_per_objfile->pubnames.size)
1509
    {
1510
      unsigned int bytes_read;
1511
 
1512
      entry_length = read_initial_length (abfd, pubnames_ptr, &bytes_read);
1513
      pubnames_ptr += bytes_read;
1514
      version = read_1_byte (abfd, pubnames_ptr);
1515
      pubnames_ptr += 1;
1516
      info_offset = read_4_bytes (abfd, pubnames_ptr);
1517
      pubnames_ptr += 4;
1518
      info_size = read_4_bytes (abfd, pubnames_ptr);
1519
      pubnames_ptr += 4;
1520
    }
1521
 
1522
  aranges_buffer = dwarf2_read_section (objfile,
1523
                                        dwarf_aranges_section);
1524
 
1525
}
1526
#endif
1527
 
1528
/* Return TRUE if OFFSET is within CU_HEADER.  */
1529
 
1530
static inline int
1531
offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
1532
{
1533
  unsigned int bottom = cu_header->offset;
1534
  unsigned int top = (cu_header->offset
1535
                      + cu_header->length
1536
                      + cu_header->initial_length_size);
1537
  return (offset >= bottom && offset < top);
1538
}
1539
 
1540
/* Read in the comp unit header information from the debug_info at info_ptr.
1541
   NOTE: This leaves members offset, first_die_offset to be filled in
1542
   by the caller.  */
1543
 
1544
static gdb_byte *
1545
read_comp_unit_head (struct comp_unit_head *cu_header,
1546
                     gdb_byte *info_ptr, bfd *abfd)
1547
{
1548
  int signed_addr;
1549
  unsigned int bytes_read;
1550
 
1551
  cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
1552
  cu_header->initial_length_size = bytes_read;
1553
  cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
1554
  info_ptr += bytes_read;
1555
  cu_header->version = read_2_bytes (abfd, info_ptr);
1556
  info_ptr += 2;
1557
  cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
1558
                                          &bytes_read);
1559
  info_ptr += bytes_read;
1560
  cu_header->addr_size = read_1_byte (abfd, info_ptr);
1561
  info_ptr += 1;
1562
  signed_addr = bfd_get_sign_extend_vma (abfd);
1563
  if (signed_addr < 0)
1564
    internal_error (__FILE__, __LINE__,
1565
                    _("read_comp_unit_head: dwarf from non elf file"));
1566
  cu_header->signed_addr_p = signed_addr;
1567
 
1568
  return info_ptr;
1569
}
1570
 
1571
static gdb_byte *
1572
partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
1573
                             gdb_byte *buffer, unsigned int buffer_size,
1574
                             bfd *abfd)
1575
{
1576
  gdb_byte *beg_of_comp_unit = info_ptr;
1577
 
1578
  info_ptr = read_comp_unit_head (header, info_ptr, abfd);
1579
 
1580
  if (header->version != 2 && header->version != 3)
1581
    error (_("Dwarf Error: wrong version in compilation unit header "
1582
           "(is %d, should be %d) [in module %s]"), header->version,
1583
           2, bfd_get_filename (abfd));
1584
 
1585
  if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
1586
    error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
1587
           "(offset 0x%lx + 6) [in module %s]"),
1588
           (long) header->abbrev_offset,
1589
           (long) (beg_of_comp_unit - buffer),
1590
           bfd_get_filename (abfd));
1591
 
1592
  if (beg_of_comp_unit + header->length + header->initial_length_size
1593
      > buffer + buffer_size)
1594
    error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
1595
           "(offset 0x%lx + 0) [in module %s]"),
1596
           (long) header->length,
1597
           (long) (beg_of_comp_unit - buffer),
1598
           bfd_get_filename (abfd));
1599
 
1600
  return info_ptr;
1601
}
1602
 
1603
/* Read in the types comp unit header information from .debug_types entry at
1604
   types_ptr.  The result is a pointer to one past the end of the header.  */
1605
 
1606
static gdb_byte *
1607
read_type_comp_unit_head (struct comp_unit_head *cu_header,
1608
                          ULONGEST *signature,
1609
                          gdb_byte *types_ptr, bfd *abfd)
1610
{
1611
  unsigned int bytes_read;
1612
  gdb_byte *initial_types_ptr = types_ptr;
1613
 
1614
  cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
1615
 
1616
  types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
1617
 
1618
  *signature = read_8_bytes (abfd, types_ptr);
1619
  types_ptr += 8;
1620
  types_ptr += cu_header->offset_size;
1621
  cu_header->first_die_offset = types_ptr - initial_types_ptr;
1622
 
1623
  return types_ptr;
1624
}
1625
 
1626
/* Allocate a new partial symtab for file named NAME and mark this new
1627
   partial symtab as being an include of PST.  */
1628
 
1629
static void
1630
dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
1631
                               struct objfile *objfile)
1632
{
1633
  struct partial_symtab *subpst = allocate_psymtab (name, objfile);
1634
 
1635
  subpst->section_offsets = pst->section_offsets;
1636
  subpst->textlow = 0;
1637
  subpst->texthigh = 0;
1638
 
1639
  subpst->dependencies = (struct partial_symtab **)
1640
    obstack_alloc (&objfile->objfile_obstack,
1641
                   sizeof (struct partial_symtab *));
1642
  subpst->dependencies[0] = pst;
1643
  subpst->number_of_dependencies = 1;
1644
 
1645
  subpst->globals_offset = 0;
1646
  subpst->n_global_syms = 0;
1647
  subpst->statics_offset = 0;
1648
  subpst->n_static_syms = 0;
1649
  subpst->symtab = NULL;
1650
  subpst->read_symtab = pst->read_symtab;
1651
  subpst->readin = 0;
1652
 
1653
  /* No private part is necessary for include psymtabs.  This property
1654
     can be used to differentiate between such include psymtabs and
1655
     the regular ones.  */
1656
  subpst->read_symtab_private = NULL;
1657
}
1658
 
1659
/* Read the Line Number Program data and extract the list of files
1660
   included by the source file represented by PST.  Build an include
1661
   partial symtab for each of these included files.  */
1662
 
1663
static void
1664
dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
1665
                               struct die_info *die,
1666
                               struct partial_symtab *pst)
1667
{
1668
  struct objfile *objfile = cu->objfile;
1669
  bfd *abfd = objfile->obfd;
1670
  struct line_header *lh = NULL;
1671
  struct attribute *attr;
1672
 
1673
  attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
1674
  if (attr)
1675
    {
1676
      unsigned int line_offset = DW_UNSND (attr);
1677
      lh = dwarf_decode_line_header (line_offset, abfd, cu);
1678
    }
1679
  if (lh == NULL)
1680
    return;  /* No linetable, so no includes.  */
1681
 
1682
  dwarf_decode_lines (lh, NULL, abfd, cu, pst);
1683
 
1684
  free_line_header (lh);
1685
}
1686
 
1687
static hashval_t
1688
hash_type_signature (const void *item)
1689
{
1690
  const struct signatured_type *type_sig = item;
1691
  /* This drops the top 32 bits of the signature, but is ok for a hash.  */
1692
  return type_sig->signature;
1693
}
1694
 
1695
static int
1696
eq_type_signature (const void *item_lhs, const void *item_rhs)
1697
{
1698
  const struct signatured_type *lhs = item_lhs;
1699
  const struct signatured_type *rhs = item_rhs;
1700
  return lhs->signature == rhs->signature;
1701
}
1702
 
1703
/* Create the hash table of all entries in the .debug_types section.
1704
   The result is zero if there is an error (e.g. missing .debug_types section),
1705
   otherwise non-zero.  */
1706
 
1707
static int
1708
create_debug_types_hash_table (struct objfile *objfile)
1709
{
1710
  gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer;
1711
  htab_t types_htab;
1712
 
1713
  if (info_ptr == NULL)
1714
    {
1715
      dwarf2_per_objfile->signatured_types = NULL;
1716
      return 0;
1717
    }
1718
 
1719
  types_htab = htab_create_alloc_ex (41,
1720
                                     hash_type_signature,
1721
                                     eq_type_signature,
1722
                                     NULL,
1723
                                     &objfile->objfile_obstack,
1724
                                     hashtab_obstack_allocate,
1725
                                     dummy_obstack_deallocate);
1726
 
1727
  if (dwarf2_die_debug)
1728
    fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
1729
 
1730
  while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
1731
    {
1732
      unsigned int offset;
1733
      unsigned int offset_size;
1734
      unsigned int type_offset;
1735
      unsigned int length, initial_length_size;
1736
      unsigned short version;
1737
      ULONGEST signature;
1738
      struct signatured_type *type_sig;
1739
      void **slot;
1740
      gdb_byte *ptr = info_ptr;
1741
 
1742
      offset = ptr - dwarf2_per_objfile->types.buffer;
1743
 
1744
      /* We need to read the type's signature in order to build the hash
1745
         table, but we don't need to read anything else just yet.  */
1746
 
1747
      /* Sanity check to ensure entire cu is present.  */
1748
      length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
1749
      if (ptr + length + initial_length_size
1750
          > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
1751
        {
1752
          complaint (&symfile_complaints,
1753
                     _("debug type entry runs off end of `.debug_types' section, ignored"));
1754
          break;
1755
        }
1756
 
1757
      offset_size = initial_length_size == 4 ? 4 : 8;
1758
      ptr += initial_length_size;
1759
      version = bfd_get_16 (objfile->obfd, ptr);
1760
      ptr += 2;
1761
      ptr += offset_size; /* abbrev offset */
1762
      ptr += 1; /* address size */
1763
      signature = bfd_get_64 (objfile->obfd, ptr);
1764
      ptr += 8;
1765
      type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
1766
 
1767
      type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
1768
      memset (type_sig, 0, sizeof (*type_sig));
1769
      type_sig->signature = signature;
1770
      type_sig->offset = offset;
1771
      type_sig->type_offset = type_offset;
1772
 
1773
      slot = htab_find_slot (types_htab, type_sig, INSERT);
1774
      gdb_assert (slot != NULL);
1775
      *slot = type_sig;
1776
 
1777
      if (dwarf2_die_debug)
1778
        fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
1779
                            offset, phex (signature, sizeof (signature)));
1780
 
1781
      info_ptr = info_ptr + initial_length_size + length;
1782
    }
1783
 
1784
  dwarf2_per_objfile->signatured_types = types_htab;
1785
 
1786
  return 1;
1787
}
1788
 
1789
/* Lookup a signature based type.
1790
   Returns NULL if SIG is not present in the table.  */
1791
 
1792
static struct signatured_type *
1793
lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
1794
{
1795
  struct signatured_type find_entry, *entry;
1796
 
1797
  if (dwarf2_per_objfile->signatured_types == NULL)
1798
    {
1799
      complaint (&symfile_complaints,
1800
                 _("missing `.debug_types' section for DW_FORM_sig8 die"));
1801
      return 0;
1802
    }
1803
 
1804
  find_entry.signature = sig;
1805
  entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
1806
  return entry;
1807
}
1808
 
1809
/* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
1810
 
1811
static void
1812
init_cu_die_reader (struct die_reader_specs *reader,
1813
                    struct dwarf2_cu *cu)
1814
{
1815
  reader->abfd = cu->objfile->obfd;
1816
  reader->cu = cu;
1817
  if (cu->per_cu->from_debug_types)
1818
    reader->buffer = dwarf2_per_objfile->types.buffer;
1819
  else
1820
    reader->buffer = dwarf2_per_objfile->info.buffer;
1821
}
1822
 
1823
/* Find the base address of the compilation unit for range lists and
1824
   location lists.  It will normally be specified by DW_AT_low_pc.
1825
   In DWARF-3 draft 4, the base address could be overridden by
1826
   DW_AT_entry_pc.  It's been removed, but GCC still uses this for
1827
   compilation units with discontinuous ranges.  */
1828
 
1829
static void
1830
dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
1831
{
1832
  struct attribute *attr;
1833
 
1834
  cu->base_known = 0;
1835
  cu->base_address = 0;
1836
 
1837
  attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
1838
  if (attr)
1839
    {
1840
      cu->base_address = DW_ADDR (attr);
1841
      cu->base_known = 1;
1842
    }
1843
  else
1844
    {
1845
      attr = dwarf2_attr (die, DW_AT_low_pc, cu);
1846
      if (attr)
1847
        {
1848
          cu->base_address = DW_ADDR (attr);
1849
          cu->base_known = 1;
1850
        }
1851
    }
1852
}
1853
 
1854
/* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
1855
   to combine the common parts.
1856
   Process a compilation unit for a psymtab.
1857
   BUFFER is a pointer to the beginning of the dwarf section buffer,
1858
   either .debug_info or debug_types.
1859
   INFO_PTR is a pointer to the start of the CU.
1860
   Returns a pointer to the next CU.  */
1861
 
1862
static gdb_byte *
1863
process_psymtab_comp_unit (struct objfile *objfile,
1864
                           struct dwarf2_per_cu_data *this_cu,
1865
                           gdb_byte *buffer, gdb_byte *info_ptr,
1866
                           unsigned int buffer_size)
1867
{
1868
  bfd *abfd = objfile->obfd;
1869
  gdb_byte *beg_of_comp_unit = info_ptr;
1870
  struct die_info *comp_unit_die;
1871
  struct partial_symtab *pst;
1872
  CORE_ADDR baseaddr;
1873
  struct cleanup *back_to_inner;
1874
  struct dwarf2_cu cu;
1875
  unsigned int bytes_read;
1876
  int has_children, has_pc_info;
1877
  struct attribute *attr;
1878
  const char *name;
1879
  CORE_ADDR best_lowpc = 0, best_highpc = 0;
1880
  struct die_reader_specs reader_specs;
1881
 
1882
  memset (&cu, 0, sizeof (cu));
1883
  cu.objfile = objfile;
1884
  obstack_init (&cu.comp_unit_obstack);
1885
 
1886
  back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
1887
 
1888
  info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
1889
                                          buffer, buffer_size,
1890
                                          abfd);
1891
 
1892
  /* Complete the cu_header.  */
1893
  cu.header.offset = beg_of_comp_unit - buffer;
1894
  cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
1895
 
1896
  cu.list_in_scope = &file_symbols;
1897
 
1898
  /* If this compilation unit was already read in, free the
1899
     cached copy in order to read it in again.  This is
1900
     necessary because we skipped some symbols when we first
1901
     read in the compilation unit (see load_partial_dies).
1902
     This problem could be avoided, but the benefit is
1903
     unclear.  */
1904
  if (this_cu->cu != NULL)
1905
    free_one_cached_comp_unit (this_cu->cu);
1906
 
1907
  /* Note that this is a pointer to our stack frame, being
1908
     added to a global data structure.  It will be cleaned up
1909
     in free_stack_comp_unit when we finish with this
1910
     compilation unit.  */
1911
  this_cu->cu = &cu;
1912
  cu.per_cu = this_cu;
1913
 
1914
  /* Read the abbrevs for this compilation unit into a table.  */
1915
  dwarf2_read_abbrevs (abfd, &cu);
1916
  make_cleanup (dwarf2_free_abbrev_table, &cu);
1917
 
1918
  /* Read the compilation unit die.  */
1919
  if (this_cu->from_debug_types)
1920
    info_ptr += 8 /*signature*/ + cu.header.offset_size;
1921
  init_cu_die_reader (&reader_specs, &cu);
1922
  info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
1923
                            &has_children);
1924
 
1925
  if (this_cu->from_debug_types)
1926
    {
1927
      /* offset,length haven't been set yet for type units.  */
1928
      this_cu->offset = cu.header.offset;
1929
      this_cu->length = cu.header.length + cu.header.initial_length_size;
1930
    }
1931
  else if (comp_unit_die->tag == DW_TAG_partial_unit)
1932
    {
1933
      info_ptr = (beg_of_comp_unit + cu.header.length
1934
                  + cu.header.initial_length_size);
1935
      do_cleanups (back_to_inner);
1936
      return info_ptr;
1937
    }
1938
 
1939
  /* Set the language we're debugging.  */
1940
  attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
1941
  if (attr)
1942
    set_cu_language (DW_UNSND (attr), &cu);
1943
  else
1944
    set_cu_language (language_minimal, &cu);
1945
 
1946
  /* Allocate a new partial symbol table structure.  */
1947
  attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
1948
  pst = start_psymtab_common (objfile, objfile->section_offsets,
1949
                              (attr != NULL) ? DW_STRING (attr) : "",
1950
                              /* TEXTLOW and TEXTHIGH are set below.  */
1951
                              0,
1952
                              objfile->global_psymbols.next,
1953
                              objfile->static_psymbols.next);
1954
 
1955
  attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
1956
  if (attr != NULL)
1957
    pst->dirname = DW_STRING (attr);
1958
 
1959
  pst->read_symtab_private = (char *) this_cu;
1960
 
1961
  baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1962
 
1963
  /* Store the function that reads in the rest of the symbol table */
1964
  pst->read_symtab = dwarf2_psymtab_to_symtab;
1965
 
1966
  this_cu->psymtab = pst;
1967
 
1968
  dwarf2_find_base_address (comp_unit_die, &cu);
1969
 
1970
  /* Possibly set the default values of LOWPC and HIGHPC from
1971
     `DW_AT_ranges'.  */
1972
  has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
1973
                                      &best_highpc, &cu, pst);
1974
  if (has_pc_info == 1 && best_lowpc < best_highpc)
1975
    /* Store the contiguous range if it is not empty; it can be empty for
1976
       CUs with no code.  */
1977
    addrmap_set_empty (objfile->psymtabs_addrmap,
1978
                       best_lowpc + baseaddr,
1979
                       best_highpc + baseaddr - 1, pst);
1980
 
1981
  /* Check if comp unit has_children.
1982
     If so, read the rest of the partial symbols from this comp unit.
1983
     If not, there's no more debug_info for this comp unit. */
1984
  if (has_children)
1985
    {
1986
      struct partial_die_info *first_die;
1987
      CORE_ADDR lowpc, highpc;
1988
 
1989
      lowpc = ((CORE_ADDR) -1);
1990
      highpc = ((CORE_ADDR) 0);
1991
 
1992
      first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
1993
 
1994
      scan_partial_symbols (first_die, &lowpc, &highpc,
1995
                            ! has_pc_info, &cu);
1996
 
1997
      /* If we didn't find a lowpc, set it to highpc to avoid
1998
         complaints from `maint check'.  */
1999
      if (lowpc == ((CORE_ADDR) -1))
2000
        lowpc = highpc;
2001
 
2002
      /* If the compilation unit didn't have an explicit address range,
2003
         then use the information extracted from its child dies.  */
2004
      if (! has_pc_info)
2005
        {
2006
          best_lowpc = lowpc;
2007
          best_highpc = highpc;
2008
        }
2009
    }
2010
  pst->textlow = best_lowpc + baseaddr;
2011
  pst->texthigh = best_highpc + baseaddr;
2012
 
2013
  pst->n_global_syms = objfile->global_psymbols.next -
2014
    (objfile->global_psymbols.list + pst->globals_offset);
2015
  pst->n_static_syms = objfile->static_psymbols.next -
2016
    (objfile->static_psymbols.list + pst->statics_offset);
2017
  sort_pst_symbols (pst);
2018
 
2019
  info_ptr = (beg_of_comp_unit + cu.header.length
2020
              + cu.header.initial_length_size);
2021
 
2022
  if (this_cu->from_debug_types)
2023
    {
2024
      /* It's not clear we want to do anything with stmt lists here.
2025
         Waiting to see what gcc ultimately does.  */
2026
    }
2027
  else
2028
    {
2029
      /* Get the list of files included in the current compilation unit,
2030
         and build a psymtab for each of them.  */
2031
      dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
2032
    }
2033
 
2034
  do_cleanups (back_to_inner);
2035
 
2036
  return info_ptr;
2037
}
2038
 
2039
/* Traversal function for htab_traverse_noresize.
2040
   Process one .debug_types comp-unit.  */
2041
 
2042
static int
2043
process_type_comp_unit (void **slot, void *info)
2044
{
2045
  struct signatured_type *entry = (struct signatured_type *) *slot;
2046
  struct objfile *objfile = (struct objfile *) info;
2047
  struct dwarf2_per_cu_data *this_cu;
2048
 
2049
  this_cu = &entry->per_cu;
2050
  this_cu->from_debug_types = 1;
2051
 
2052
  process_psymtab_comp_unit (objfile, this_cu,
2053
                             dwarf2_per_objfile->types.buffer,
2054
                             dwarf2_per_objfile->types.buffer + entry->offset,
2055
                             dwarf2_per_objfile->types.size);
2056
 
2057
  return 1;
2058
}
2059
 
2060
/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
2061
   Build partial symbol tables for the .debug_types comp-units.  */
2062
 
2063
static void
2064
build_type_psymtabs (struct objfile *objfile)
2065
{
2066
  if (! create_debug_types_hash_table (objfile))
2067
    return;
2068
 
2069
  htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
2070
                          process_type_comp_unit, objfile);
2071
}
2072
 
2073
/* Build the partial symbol table by doing a quick pass through the
2074
   .debug_info and .debug_abbrev sections.  */
2075
 
2076
static void
2077
dwarf2_build_psymtabs_hard (struct objfile *objfile)
2078
{
2079
  /* Instead of reading this into a big buffer, we should probably use
2080
     mmap()  on architectures that support it. (FIXME) */
2081
  bfd *abfd = objfile->obfd;
2082
  gdb_byte *info_ptr;
2083
  struct cleanup *back_to;
2084
 
2085
  info_ptr = dwarf2_per_objfile->info.buffer;
2086
 
2087
  /* Any cached compilation units will be linked by the per-objfile
2088
     read_in_chain.  Make sure to free them when we're done.  */
2089
  back_to = make_cleanup (free_cached_comp_units, NULL);
2090
 
2091
  build_type_psymtabs (objfile);
2092
 
2093
  create_all_comp_units (objfile);
2094
 
2095
  objfile->psymtabs_addrmap =
2096
    addrmap_create_mutable (&objfile->objfile_obstack);
2097
 
2098
  /* Since the objects we're extracting from .debug_info vary in
2099
     length, only the individual functions to extract them (like
2100
     read_comp_unit_head and load_partial_die) can really know whether
2101
     the buffer is large enough to hold another complete object.
2102
 
2103
     At the moment, they don't actually check that.  If .debug_info
2104
     holds just one extra byte after the last compilation unit's dies,
2105
     then read_comp_unit_head will happily read off the end of the
2106
     buffer.  read_partial_die is similarly casual.  Those functions
2107
     should be fixed.
2108
 
2109
     For this loop condition, simply checking whether there's any data
2110
     left at all should be sufficient.  */
2111
 
2112
  while (info_ptr < (dwarf2_per_objfile->info.buffer
2113
                     + dwarf2_per_objfile->info.size))
2114
    {
2115
      struct dwarf2_per_cu_data *this_cu;
2116
 
2117
      this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
2118
                                       objfile);
2119
 
2120
      info_ptr = process_psymtab_comp_unit (objfile, this_cu,
2121
                                            dwarf2_per_objfile->info.buffer,
2122
                                            info_ptr,
2123
                                            dwarf2_per_objfile->info.size);
2124
    }
2125
 
2126
  objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
2127
                                                    &objfile->objfile_obstack);
2128
 
2129
  do_cleanups (back_to);
2130
}
2131
 
2132
/* Load the partial DIEs for a secondary CU into memory.  */
2133
 
2134
static void
2135
load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
2136
                        struct objfile *objfile)
2137
{
2138
  bfd *abfd = objfile->obfd;
2139
  gdb_byte *info_ptr, *beg_of_comp_unit;
2140
  struct die_info *comp_unit_die;
2141
  struct dwarf2_cu *cu;
2142
  unsigned int bytes_read;
2143
  struct cleanup *back_to;
2144
  struct attribute *attr;
2145
  int has_children;
2146
  struct die_reader_specs reader_specs;
2147
 
2148
  gdb_assert (! this_cu->from_debug_types);
2149
 
2150
  info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
2151
  beg_of_comp_unit = info_ptr;
2152
 
2153
  cu = alloc_one_comp_unit (objfile);
2154
 
2155
  /* ??? Missing cleanup for CU?  */
2156
 
2157
  /* Link this compilation unit into the compilation unit tree.  */
2158
  this_cu->cu = cu;
2159
  cu->per_cu = this_cu;
2160
  cu->type_hash = this_cu->type_hash;
2161
 
2162
  info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
2163
                                          dwarf2_per_objfile->info.buffer,
2164
                                          dwarf2_per_objfile->info.size,
2165
                                          abfd);
2166
 
2167
  /* Complete the cu_header.  */
2168
  cu->header.offset = this_cu->offset;
2169
  cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
2170
 
2171
  /* Read the abbrevs for this compilation unit into a table.  */
2172
  dwarf2_read_abbrevs (abfd, cu);
2173
  back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
2174
 
2175
  /* Read the compilation unit die.  */
2176
  init_cu_die_reader (&reader_specs, cu);
2177
  info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2178
                            &has_children);
2179
 
2180
  /* Set the language we're debugging.  */
2181
  attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
2182
  if (attr)
2183
    set_cu_language (DW_UNSND (attr), cu);
2184
  else
2185
    set_cu_language (language_minimal, cu);
2186
 
2187
  /* Check if comp unit has_children.
2188
     If so, read the rest of the partial symbols from this comp unit.
2189
     If not, there's no more debug_info for this comp unit. */
2190
  if (has_children)
2191
    load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
2192
 
2193
  do_cleanups (back_to);
2194
}
2195
 
2196
/* Create a list of all compilation units in OBJFILE.  We do this only
2197
   if an inter-comp-unit reference is found; presumably if there is one,
2198
   there will be many, and one will occur early in the .debug_info section.
2199
   So there's no point in building this list incrementally.  */
2200
 
2201
static void
2202
create_all_comp_units (struct objfile *objfile)
2203
{
2204
  int n_allocated;
2205
  int n_comp_units;
2206
  struct dwarf2_per_cu_data **all_comp_units;
2207
  gdb_byte *info_ptr = dwarf2_per_objfile->info.buffer;
2208
 
2209
  n_comp_units = 0;
2210
  n_allocated = 10;
2211
  all_comp_units = xmalloc (n_allocated
2212
                            * sizeof (struct dwarf2_per_cu_data *));
2213
 
2214
  while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
2215
    {
2216
      unsigned int length, initial_length_size;
2217
      gdb_byte *beg_of_comp_unit;
2218
      struct dwarf2_per_cu_data *this_cu;
2219
      unsigned int offset;
2220
 
2221
      offset = info_ptr - dwarf2_per_objfile->info.buffer;
2222
 
2223
      /* Read just enough information to find out where the next
2224
         compilation unit is.  */
2225
      length = read_initial_length (objfile->obfd, info_ptr,
2226
                                    &initial_length_size);
2227
 
2228
      /* Save the compilation unit for later lookup.  */
2229
      this_cu = obstack_alloc (&objfile->objfile_obstack,
2230
                               sizeof (struct dwarf2_per_cu_data));
2231
      memset (this_cu, 0, sizeof (*this_cu));
2232
      this_cu->offset = offset;
2233
      this_cu->length = length + initial_length_size;
2234
 
2235
      if (n_comp_units == n_allocated)
2236
        {
2237
          n_allocated *= 2;
2238
          all_comp_units = xrealloc (all_comp_units,
2239
                                     n_allocated
2240
                                     * sizeof (struct dwarf2_per_cu_data *));
2241
        }
2242
      all_comp_units[n_comp_units++] = this_cu;
2243
 
2244
      info_ptr = info_ptr + this_cu->length;
2245
    }
2246
 
2247
  dwarf2_per_objfile->all_comp_units
2248
    = obstack_alloc (&objfile->objfile_obstack,
2249
                     n_comp_units * sizeof (struct dwarf2_per_cu_data *));
2250
  memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
2251
          n_comp_units * sizeof (struct dwarf2_per_cu_data *));
2252
  xfree (all_comp_units);
2253
  dwarf2_per_objfile->n_comp_units = n_comp_units;
2254
}
2255
 
2256
/* Process all loaded DIEs for compilation unit CU, starting at
2257
   FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
2258
   unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
2259
   DW_AT_ranges).  If NEED_PC is set, then this function will set
2260
   *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
2261
   and record the covered ranges in the addrmap.  */
2262
 
2263
static void
2264
scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
2265
                      CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
2266
{
2267
  struct objfile *objfile = cu->objfile;
2268
  bfd *abfd = objfile->obfd;
2269
  struct partial_die_info *pdi;
2270
 
2271
  /* Now, march along the PDI's, descending into ones which have
2272
     interesting children but skipping the children of the other ones,
2273
     until we reach the end of the compilation unit.  */
2274
 
2275
  pdi = first_die;
2276
 
2277
  while (pdi != NULL)
2278
    {
2279
      fixup_partial_die (pdi, cu);
2280
 
2281
      /* Anonymous namespaces have no name but have interesting
2282
         children, so we need to look at them.  Ditto for anonymous
2283
         enums.  */
2284
 
2285
      if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
2286
          || pdi->tag == DW_TAG_enumeration_type)
2287
        {
2288
          switch (pdi->tag)
2289
            {
2290
            case DW_TAG_subprogram:
2291
              add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
2292
              break;
2293
            case DW_TAG_variable:
2294
            case DW_TAG_typedef:
2295
            case DW_TAG_union_type:
2296
              if (!pdi->is_declaration)
2297
                {
2298
                  add_partial_symbol (pdi, cu);
2299
                }
2300
              break;
2301
            case DW_TAG_class_type:
2302
            case DW_TAG_interface_type:
2303
            case DW_TAG_structure_type:
2304
              if (!pdi->is_declaration)
2305
                {
2306
                  add_partial_symbol (pdi, cu);
2307
                }
2308
              break;
2309
            case DW_TAG_enumeration_type:
2310
              if (!pdi->is_declaration)
2311
                add_partial_enumeration (pdi, cu);
2312
              break;
2313
            case DW_TAG_base_type:
2314
            case DW_TAG_subrange_type:
2315
              /* File scope base type definitions are added to the partial
2316
                 symbol table.  */
2317
              add_partial_symbol (pdi, cu);
2318
              break;
2319
            case DW_TAG_namespace:
2320
              add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
2321
              break;
2322
            case DW_TAG_module:
2323
              add_partial_module (pdi, lowpc, highpc, need_pc, cu);
2324
              break;
2325
            default:
2326
              break;
2327
            }
2328
        }
2329
 
2330
      /* If the die has a sibling, skip to the sibling.  */
2331
 
2332
      pdi = pdi->die_sibling;
2333
    }
2334
}
2335
 
2336
/* Functions used to compute the fully scoped name of a partial DIE.
2337
 
2338
   Normally, this is simple.  For C++, the parent DIE's fully scoped
2339
   name is concatenated with "::" and the partial DIE's name.  For
2340
   Java, the same thing occurs except that "." is used instead of "::".
2341
   Enumerators are an exception; they use the scope of their parent
2342
   enumeration type, i.e. the name of the enumeration type is not
2343
   prepended to the enumerator.
2344
 
2345
   There are two complexities.  One is DW_AT_specification; in this
2346
   case "parent" means the parent of the target of the specification,
2347
   instead of the direct parent of the DIE.  The other is compilers
2348
   which do not emit DW_TAG_namespace; in this case we try to guess
2349
   the fully qualified name of structure types from their members'
2350
   linkage names.  This must be done using the DIE's children rather
2351
   than the children of any DW_AT_specification target.  We only need
2352
   to do this for structures at the top level, i.e. if the target of
2353
   any DW_AT_specification (if any; otherwise the DIE itself) does not
2354
   have a parent.  */
2355
 
2356
/* Compute the scope prefix associated with PDI's parent, in
2357
   compilation unit CU.  The result will be allocated on CU's
2358
   comp_unit_obstack, or a copy of the already allocated PDI->NAME
2359
   field.  NULL is returned if no prefix is necessary.  */
2360
static char *
2361
partial_die_parent_scope (struct partial_die_info *pdi,
2362
                          struct dwarf2_cu *cu)
2363
{
2364
  char *grandparent_scope;
2365
  struct partial_die_info *parent, *real_pdi;
2366
 
2367
  /* We need to look at our parent DIE; if we have a DW_AT_specification,
2368
     then this means the parent of the specification DIE.  */
2369
 
2370
  real_pdi = pdi;
2371
  while (real_pdi->has_specification)
2372
    real_pdi = find_partial_die (real_pdi->spec_offset, cu);
2373
 
2374
  parent = real_pdi->die_parent;
2375
  if (parent == NULL)
2376
    return NULL;
2377
 
2378
  if (parent->scope_set)
2379
    return parent->scope;
2380
 
2381
  fixup_partial_die (parent, cu);
2382
 
2383
  grandparent_scope = partial_die_parent_scope (parent, cu);
2384
 
2385
  if (parent->tag == DW_TAG_namespace
2386
      || parent->tag == DW_TAG_structure_type
2387
      || parent->tag == DW_TAG_class_type
2388
      || parent->tag == DW_TAG_interface_type
2389
      || parent->tag == DW_TAG_union_type
2390
      || parent->tag == DW_TAG_enumeration_type)
2391
    {
2392
      if (grandparent_scope == NULL)
2393
        parent->scope = parent->name;
2394
      else
2395
        parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
2396
                                         parent->name, cu);
2397
    }
2398
  else if (parent->tag == DW_TAG_enumerator)
2399
    /* Enumerators should not get the name of the enumeration as a prefix.  */
2400
    parent->scope = grandparent_scope;
2401
  else
2402
    {
2403
      /* FIXME drow/2004-04-01: What should we be doing with
2404
         function-local names?  For partial symbols, we should probably be
2405
         ignoring them.  */
2406
      complaint (&symfile_complaints,
2407
                 _("unhandled containing DIE tag %d for DIE at %d"),
2408
                 parent->tag, pdi->offset);
2409
      parent->scope = grandparent_scope;
2410
    }
2411
 
2412
  parent->scope_set = 1;
2413
  return parent->scope;
2414
}
2415
 
2416
/* Return the fully scoped name associated with PDI, from compilation unit
2417
   CU.  The result will be allocated with malloc.  */
2418
static char *
2419
partial_die_full_name (struct partial_die_info *pdi,
2420
                       struct dwarf2_cu *cu)
2421
{
2422
  char *parent_scope;
2423
 
2424
  parent_scope = partial_die_parent_scope (pdi, cu);
2425
  if (parent_scope == NULL)
2426
    return NULL;
2427
  else
2428
    return typename_concat (NULL, parent_scope, pdi->name, cu);
2429
}
2430
 
2431
static void
2432
add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
2433
{
2434
  struct objfile *objfile = cu->objfile;
2435
  CORE_ADDR addr = 0;
2436
  char *actual_name = NULL;
2437
  const char *my_prefix;
2438
  const struct partial_symbol *psym = NULL;
2439
  CORE_ADDR baseaddr;
2440
  int built_actual_name = 0;
2441
 
2442
  baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2443
 
2444
  if (pdi_needs_namespace (pdi->tag))
2445
    {
2446
      actual_name = partial_die_full_name (pdi, cu);
2447
      if (actual_name)
2448
        built_actual_name = 1;
2449
    }
2450
 
2451
  if (actual_name == NULL)
2452
    actual_name = pdi->name;
2453
 
2454
  switch (pdi->tag)
2455
    {
2456
    case DW_TAG_subprogram:
2457
      if (pdi->is_external || cu->language == language_ada)
2458
        {
2459
          /* brobecker/2007-12-26: Normally, only "external" DIEs are part
2460
             of the global scope.  But in Ada, we want to be able to access
2461
             nested procedures globally.  So all Ada subprograms are stored
2462
             in the global scope.  */
2463
          /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
2464
             mst_text, objfile); */
2465
          psym = add_psymbol_to_list (actual_name, strlen (actual_name),
2466
                                      built_actual_name,
2467
                                      VAR_DOMAIN, LOC_BLOCK,
2468
                                      &objfile->global_psymbols,
2469
                                      0, pdi->lowpc + baseaddr,
2470
                                      cu->language, objfile);
2471
        }
2472
      else
2473
        {
2474
          /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
2475
             mst_file_text, objfile); */
2476
          psym = add_psymbol_to_list (actual_name, strlen (actual_name),
2477
                                      built_actual_name,
2478
                                      VAR_DOMAIN, LOC_BLOCK,
2479
                                      &objfile->static_psymbols,
2480
                                      0, pdi->lowpc + baseaddr,
2481
                                      cu->language, objfile);
2482
        }
2483
      break;
2484
    case DW_TAG_variable:
2485
      if (pdi->is_external)
2486
        {
2487
          /* Global Variable.
2488
             Don't enter into the minimal symbol tables as there is
2489
             a minimal symbol table entry from the ELF symbols already.
2490
             Enter into partial symbol table if it has a location
2491
             descriptor or a type.
2492
             If the location descriptor is missing, new_symbol will create
2493
             a LOC_UNRESOLVED symbol, the address of the variable will then
2494
             be determined from the minimal symbol table whenever the variable
2495
             is referenced.
2496
             The address for the partial symbol table entry is not
2497
             used by GDB, but it comes in handy for debugging partial symbol
2498
             table building.  */
2499
 
2500
          if (pdi->locdesc)
2501
            addr = decode_locdesc (pdi->locdesc, cu);
2502
          if (pdi->locdesc || pdi->has_type)
2503
            psym = add_psymbol_to_list (actual_name, strlen (actual_name),
2504
                                        built_actual_name,
2505
                                        VAR_DOMAIN, LOC_STATIC,
2506
                                        &objfile->global_psymbols,
2507
                                        0, addr + baseaddr,
2508
                                        cu->language, objfile);
2509
        }
2510
      else
2511
        {
2512
          /* Static Variable. Skip symbols without location descriptors.  */
2513
          if (pdi->locdesc == NULL)
2514
            {
2515
              if (built_actual_name)
2516
                xfree (actual_name);
2517
              return;
2518
            }
2519
          addr = decode_locdesc (pdi->locdesc, cu);
2520
          /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
2521
             mst_file_data, objfile); */
2522
          psym = add_psymbol_to_list (actual_name, strlen (actual_name),
2523
                                      built_actual_name,
2524
                                      VAR_DOMAIN, LOC_STATIC,
2525
                                      &objfile->static_psymbols,
2526
                                      0, addr + baseaddr,
2527
                                      cu->language, objfile);
2528
        }
2529
      break;
2530
    case DW_TAG_typedef:
2531
    case DW_TAG_base_type:
2532
    case DW_TAG_subrange_type:
2533
      add_psymbol_to_list (actual_name, strlen (actual_name),
2534
                           built_actual_name,
2535
                           VAR_DOMAIN, LOC_TYPEDEF,
2536
                           &objfile->static_psymbols,
2537
                           0, (CORE_ADDR) 0, cu->language, objfile);
2538
      break;
2539
    case DW_TAG_namespace:
2540
      add_psymbol_to_list (actual_name, strlen (actual_name),
2541
                           built_actual_name,
2542
                           VAR_DOMAIN, LOC_TYPEDEF,
2543
                           &objfile->global_psymbols,
2544
                           0, (CORE_ADDR) 0, cu->language, objfile);
2545
      break;
2546
    case DW_TAG_class_type:
2547
    case DW_TAG_interface_type:
2548
    case DW_TAG_structure_type:
2549
    case DW_TAG_union_type:
2550
    case DW_TAG_enumeration_type:
2551
      /* Skip external references.  The DWARF standard says in the section
2552
         about "Structure, Union, and Class Type Entries": "An incomplete
2553
         structure, union or class type is represented by a structure,
2554
         union or class entry that does not have a byte size attribute
2555
         and that has a DW_AT_declaration attribute."  */
2556
      if (!pdi->has_byte_size && pdi->is_declaration)
2557
        {
2558
          if (built_actual_name)
2559
            xfree (actual_name);
2560
          return;
2561
        }
2562
 
2563
      /* NOTE: carlton/2003-10-07: See comment in new_symbol about
2564
         static vs. global.  */
2565
      add_psymbol_to_list (actual_name, strlen (actual_name),
2566
                           built_actual_name,
2567
                           STRUCT_DOMAIN, LOC_TYPEDEF,
2568
                           (cu->language == language_cplus
2569
                            || cu->language == language_java)
2570
                           ? &objfile->global_psymbols
2571
                           : &objfile->static_psymbols,
2572
                           0, (CORE_ADDR) 0, cu->language, objfile);
2573
 
2574
      break;
2575
    case DW_TAG_enumerator:
2576
      add_psymbol_to_list (actual_name, strlen (actual_name),
2577
                           built_actual_name,
2578
                           VAR_DOMAIN, LOC_CONST,
2579
                           (cu->language == language_cplus
2580
                            || cu->language == language_java)
2581
                           ? &objfile->global_psymbols
2582
                           : &objfile->static_psymbols,
2583
                           0, (CORE_ADDR) 0, cu->language, objfile);
2584
      break;
2585
    default:
2586
      break;
2587
    }
2588
 
2589
  /* Check to see if we should scan the name for possible namespace
2590
     info.  Only do this if this is C++, if we don't have namespace
2591
     debugging info in the file, if the psym is of an appropriate type
2592
     (otherwise we'll have psym == NULL), and if we actually had a
2593
     mangled name to begin with.  */
2594
 
2595
  /* FIXME drow/2004-02-22: Why don't we do this for classes, i.e. the
2596
     cases which do not set PSYM above?  */
2597
 
2598
  if (cu->language == language_cplus
2599
      && cu->has_namespace_info == 0
2600
      && psym != NULL
2601
      && SYMBOL_CPLUS_DEMANGLED_NAME (psym) != NULL)
2602
    cp_check_possible_namespace_symbols (SYMBOL_CPLUS_DEMANGLED_NAME (psym),
2603
                                         objfile);
2604
 
2605
  if (built_actual_name)
2606
    xfree (actual_name);
2607
}
2608
 
2609
/* Determine whether a die of type TAG living in a C++ class or
2610
   namespace needs to have the name of the scope prepended to the
2611
   name listed in the die.  */
2612
 
2613
static int
2614
pdi_needs_namespace (enum dwarf_tag tag)
2615
{
2616
  switch (tag)
2617
    {
2618
    case DW_TAG_namespace:
2619
    case DW_TAG_typedef:
2620
    case DW_TAG_class_type:
2621
    case DW_TAG_interface_type:
2622
    case DW_TAG_structure_type:
2623
    case DW_TAG_union_type:
2624
    case DW_TAG_enumeration_type:
2625
    case DW_TAG_enumerator:
2626
      return 1;
2627
    default:
2628
      return 0;
2629
    }
2630
}
2631
 
2632
/* Read a partial die corresponding to a namespace; also, add a symbol
2633
   corresponding to that namespace to the symbol table.  NAMESPACE is
2634
   the name of the enclosing namespace.  */
2635
 
2636
static void
2637
add_partial_namespace (struct partial_die_info *pdi,
2638
                       CORE_ADDR *lowpc, CORE_ADDR *highpc,
2639
                       int need_pc, struct dwarf2_cu *cu)
2640
{
2641
  struct objfile *objfile = cu->objfile;
2642
 
2643
  /* Add a symbol for the namespace.  */
2644
 
2645
  add_partial_symbol (pdi, cu);
2646
 
2647
  /* Now scan partial symbols in that namespace.  */
2648
 
2649
  if (pdi->has_children)
2650
    scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
2651
}
2652
 
2653
/* Read a partial die corresponding to a Fortran module.  */
2654
 
2655
static void
2656
add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
2657
                    CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
2658
{
2659
  /* Now scan partial symbols in that module.
2660
 
2661
     FIXME: Support the separate Fortran module namespaces.  */
2662
 
2663
  if (pdi->has_children)
2664
    scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
2665
}
2666
 
2667
/* Read a partial die corresponding to a subprogram and create a partial
2668
   symbol for that subprogram.  When the CU language allows it, this
2669
   routine also defines a partial symbol for each nested subprogram
2670
   that this subprogram contains.
2671
 
2672
   DIE my also be a lexical block, in which case we simply search
2673
   recursively for suprograms defined inside that lexical block.
2674
   Again, this is only performed when the CU language allows this
2675
   type of definitions.  */
2676
 
2677
static void
2678
add_partial_subprogram (struct partial_die_info *pdi,
2679
                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
2680
                        int need_pc, struct dwarf2_cu *cu)
2681
{
2682
  if (pdi->tag == DW_TAG_subprogram)
2683
    {
2684
      if (pdi->has_pc_info)
2685
        {
2686
          if (pdi->lowpc < *lowpc)
2687
            *lowpc = pdi->lowpc;
2688
          if (pdi->highpc > *highpc)
2689
            *highpc = pdi->highpc;
2690
          if (need_pc)
2691
            {
2692
              CORE_ADDR baseaddr;
2693
              struct objfile *objfile = cu->objfile;
2694
 
2695
              baseaddr = ANOFFSET (objfile->section_offsets,
2696
                                   SECT_OFF_TEXT (objfile));
2697
              addrmap_set_empty (objfile->psymtabs_addrmap,
2698
                                 pdi->lowpc + baseaddr,
2699
                                 pdi->highpc - 1 + baseaddr,
2700
                                 cu->per_cu->psymtab);
2701
            }
2702
          if (!pdi->is_declaration)
2703
            add_partial_symbol (pdi, cu);
2704
        }
2705
    }
2706
 
2707
  if (! pdi->has_children)
2708
    return;
2709
 
2710
  if (cu->language == language_ada)
2711
    {
2712
      pdi = pdi->die_child;
2713
      while (pdi != NULL)
2714
        {
2715
          fixup_partial_die (pdi, cu);
2716
          if (pdi->tag == DW_TAG_subprogram
2717
              || pdi->tag == DW_TAG_lexical_block)
2718
            add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
2719
          pdi = pdi->die_sibling;
2720
        }
2721
    }
2722
}
2723
 
2724
/* See if we can figure out if the class lives in a namespace.  We do
2725
   this by looking for a member function; its demangled name will
2726
   contain namespace info, if there is any.  */
2727
 
2728
static void
2729
guess_structure_name (struct partial_die_info *struct_pdi,
2730
                      struct dwarf2_cu *cu)
2731
{
2732
  if ((cu->language == language_cplus
2733
       || cu->language == language_java)
2734
      && cu->has_namespace_info == 0
2735
      && struct_pdi->has_children)
2736
    {
2737
      /* NOTE: carlton/2003-10-07: Getting the info this way changes
2738
         what template types look like, because the demangler
2739
         frequently doesn't give the same name as the debug info.  We
2740
         could fix this by only using the demangled name to get the
2741
         prefix (but see comment in read_structure_type).  */
2742
 
2743
      struct partial_die_info *child_pdi = struct_pdi->die_child;
2744
      struct partial_die_info *real_pdi;
2745
 
2746
      /* If this DIE (this DIE's specification, if any) has a parent, then
2747
         we should not do this.  We'll prepend the parent's fully qualified
2748
         name when we create the partial symbol.  */
2749
 
2750
      real_pdi = struct_pdi;
2751
      while (real_pdi->has_specification)
2752
        real_pdi = find_partial_die (real_pdi->spec_offset, cu);
2753
 
2754
      if (real_pdi->die_parent != NULL)
2755
        return;
2756
 
2757
      while (child_pdi != NULL)
2758
        {
2759
          if (child_pdi->tag == DW_TAG_subprogram)
2760
            {
2761
              char *actual_class_name
2762
                = language_class_name_from_physname (cu->language_defn,
2763
                                                     child_pdi->name);
2764
              if (actual_class_name != NULL)
2765
                {
2766
                  struct_pdi->name
2767
                    = obsavestring (actual_class_name,
2768
                                    strlen (actual_class_name),
2769
                                    &cu->objfile->objfile_obstack);
2770
                  xfree (actual_class_name);
2771
                }
2772
              break;
2773
            }
2774
 
2775
          child_pdi = child_pdi->die_sibling;
2776
        }
2777
    }
2778
}
2779
 
2780
/* Read a partial die corresponding to an enumeration type.  */
2781
 
2782
static void
2783
add_partial_enumeration (struct partial_die_info *enum_pdi,
2784
                         struct dwarf2_cu *cu)
2785
{
2786
  struct objfile *objfile = cu->objfile;
2787
  bfd *abfd = objfile->obfd;
2788
  struct partial_die_info *pdi;
2789
 
2790
  if (enum_pdi->name != NULL)
2791
    add_partial_symbol (enum_pdi, cu);
2792
 
2793
  pdi = enum_pdi->die_child;
2794
  while (pdi)
2795
    {
2796
      if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
2797
        complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
2798
      else
2799
        add_partial_symbol (pdi, cu);
2800
      pdi = pdi->die_sibling;
2801
    }
2802
}
2803
 
2804
/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
2805
   Return the corresponding abbrev, or NULL if the number is zero (indicating
2806
   an empty DIE).  In either case *BYTES_READ will be set to the length of
2807
   the initial number.  */
2808
 
2809
static struct abbrev_info *
2810
peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
2811
                 struct dwarf2_cu *cu)
2812
{
2813
  bfd *abfd = cu->objfile->obfd;
2814
  unsigned int abbrev_number;
2815
  struct abbrev_info *abbrev;
2816
 
2817
  abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
2818
 
2819
  if (abbrev_number == 0)
2820
    return NULL;
2821
 
2822
  abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
2823
  if (!abbrev)
2824
    {
2825
      error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
2826
                      bfd_get_filename (abfd));
2827
    }
2828
 
2829
  return abbrev;
2830
}
2831
 
2832
/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
2833
   Returns a pointer to the end of a series of DIEs, terminated by an empty
2834
   DIE.  Any children of the skipped DIEs will also be skipped.  */
2835
 
2836
static gdb_byte *
2837
skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
2838
{
2839
  struct abbrev_info *abbrev;
2840
  unsigned int bytes_read;
2841
 
2842
  while (1)
2843
    {
2844
      abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
2845
      if (abbrev == NULL)
2846
        return info_ptr + bytes_read;
2847
      else
2848
        info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
2849
    }
2850
}
2851
 
2852
/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
2853
   INFO_PTR should point just after the initial uleb128 of a DIE, and the
2854
   abbrev corresponding to that skipped uleb128 should be passed in
2855
   ABBREV.  Returns a pointer to this DIE's sibling, skipping any
2856
   children.  */
2857
 
2858
static gdb_byte *
2859
skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
2860
              struct abbrev_info *abbrev, struct dwarf2_cu *cu)
2861
{
2862
  unsigned int bytes_read;
2863
  struct attribute attr;
2864
  bfd *abfd = cu->objfile->obfd;
2865
  unsigned int form, i;
2866
 
2867
  for (i = 0; i < abbrev->num_attrs; i++)
2868
    {
2869
      /* The only abbrev we care about is DW_AT_sibling.  */
2870
      if (abbrev->attrs[i].name == DW_AT_sibling)
2871
        {
2872
          read_attribute (&attr, &abbrev->attrs[i],
2873
                          abfd, info_ptr, cu);
2874
          if (attr.form == DW_FORM_ref_addr)
2875
            complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
2876
          else
2877
            return buffer + dwarf2_get_ref_die_offset (&attr);
2878
        }
2879
 
2880
      /* If it isn't DW_AT_sibling, skip this attribute.  */
2881
      form = abbrev->attrs[i].form;
2882
    skip_attribute:
2883
      switch (form)
2884
        {
2885
        case DW_FORM_ref_addr:
2886
          /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
2887
             and later it is offset sized.  */
2888
          if (cu->header.version == 2)
2889
            info_ptr += cu->header.addr_size;
2890
          else
2891
            info_ptr += cu->header.offset_size;
2892
          break;
2893
        case DW_FORM_addr:
2894
          info_ptr += cu->header.addr_size;
2895
          break;
2896
        case DW_FORM_data1:
2897
        case DW_FORM_ref1:
2898
        case DW_FORM_flag:
2899
          info_ptr += 1;
2900
          break;
2901
        case DW_FORM_data2:
2902
        case DW_FORM_ref2:
2903
          info_ptr += 2;
2904
          break;
2905
        case DW_FORM_data4:
2906
        case DW_FORM_ref4:
2907
          info_ptr += 4;
2908
          break;
2909
        case DW_FORM_data8:
2910
        case DW_FORM_ref8:
2911
        case DW_FORM_sig8:
2912
          info_ptr += 8;
2913
          break;
2914
        case DW_FORM_string:
2915
          read_string (abfd, info_ptr, &bytes_read);
2916
          info_ptr += bytes_read;
2917
          break;
2918
        case DW_FORM_strp:
2919
          info_ptr += cu->header.offset_size;
2920
          break;
2921
        case DW_FORM_block:
2922
          info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2923
          info_ptr += bytes_read;
2924
          break;
2925
        case DW_FORM_block1:
2926
          info_ptr += 1 + read_1_byte (abfd, info_ptr);
2927
          break;
2928
        case DW_FORM_block2:
2929
          info_ptr += 2 + read_2_bytes (abfd, info_ptr);
2930
          break;
2931
        case DW_FORM_block4:
2932
          info_ptr += 4 + read_4_bytes (abfd, info_ptr);
2933
          break;
2934
        case DW_FORM_sdata:
2935
        case DW_FORM_udata:
2936
        case DW_FORM_ref_udata:
2937
          info_ptr = skip_leb128 (abfd, info_ptr);
2938
          break;
2939
        case DW_FORM_indirect:
2940
          form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2941
          info_ptr += bytes_read;
2942
          /* We need to continue parsing from here, so just go back to
2943
             the top.  */
2944
          goto skip_attribute;
2945
 
2946
        default:
2947
          error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
2948
                 dwarf_form_name (form),
2949
                 bfd_get_filename (abfd));
2950
        }
2951
    }
2952
 
2953
  if (abbrev->has_children)
2954
    return skip_children (buffer, info_ptr, cu);
2955
  else
2956
    return info_ptr;
2957
}
2958
 
2959
/* Locate ORIG_PDI's sibling.
2960
   INFO_PTR should point to the start of the next DIE after ORIG_PDI
2961
   in BUFFER.  */
2962
 
2963
static gdb_byte *
2964
locate_pdi_sibling (struct partial_die_info *orig_pdi,
2965
                    gdb_byte *buffer, gdb_byte *info_ptr,
2966
                    bfd *abfd, struct dwarf2_cu *cu)
2967
{
2968
  /* Do we know the sibling already?  */
2969
 
2970
  if (orig_pdi->sibling)
2971
    return orig_pdi->sibling;
2972
 
2973
  /* Are there any children to deal with?  */
2974
 
2975
  if (!orig_pdi->has_children)
2976
    return info_ptr;
2977
 
2978
  /* Skip the children the long way.  */
2979
 
2980
  return skip_children (buffer, info_ptr, cu);
2981
}
2982
 
2983
/* Expand this partial symbol table into a full symbol table.  */
2984
 
2985
static void
2986
dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
2987
{
2988
  /* FIXME: This is barely more than a stub.  */
2989
  if (pst != NULL)
2990
    {
2991
      if (pst->readin)
2992
        {
2993
          warning (_("bug: psymtab for %s is already read in."), pst->filename);
2994
        }
2995
      else
2996
        {
2997
          if (info_verbose)
2998
            {
2999
              printf_filtered (_("Reading in symbols for %s..."), pst->filename);
3000
              gdb_flush (gdb_stdout);
3001
            }
3002
 
3003
          /* Restore our global data.  */
3004
          dwarf2_per_objfile = objfile_data (pst->objfile,
3005
                                             dwarf2_objfile_data_key);
3006
 
3007
          /* If this psymtab is constructed from a debug-only objfile, the
3008
             has_section_at_zero flag will not necessarily be correct.  We
3009
             can get the correct value for this flag by looking at the data
3010
             associated with the (presumably stripped) associated objfile.  */
3011
          if (pst->objfile->separate_debug_objfile_backlink)
3012
            {
3013
              struct dwarf2_per_objfile *dpo_backlink
3014
                = objfile_data (pst->objfile->separate_debug_objfile_backlink,
3015
                                dwarf2_objfile_data_key);
3016
              dwarf2_per_objfile->has_section_at_zero
3017
                = dpo_backlink->has_section_at_zero;
3018
            }
3019
 
3020
          psymtab_to_symtab_1 (pst);
3021
 
3022
          /* Finish up the debug error message.  */
3023
          if (info_verbose)
3024
            printf_filtered (_("done.\n"));
3025
        }
3026
    }
3027
}
3028
 
3029
/* Add PER_CU to the queue.  */
3030
 
3031
static void
3032
queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
3033
{
3034
  struct dwarf2_queue_item *item;
3035
 
3036
  per_cu->queued = 1;
3037
  item = xmalloc (sizeof (*item));
3038
  item->per_cu = per_cu;
3039
  item->next = NULL;
3040
 
3041
  if (dwarf2_queue == NULL)
3042
    dwarf2_queue = item;
3043
  else
3044
    dwarf2_queue_tail->next = item;
3045
 
3046
  dwarf2_queue_tail = item;
3047
}
3048
 
3049
/* Process the queue.  */
3050
 
3051
static void
3052
process_queue (struct objfile *objfile)
3053
{
3054
  struct dwarf2_queue_item *item, *next_item;
3055
 
3056
  /* The queue starts out with one item, but following a DIE reference
3057
     may load a new CU, adding it to the end of the queue.  */
3058
  for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
3059
    {
3060
      if (item->per_cu->psymtab && !item->per_cu->psymtab->readin)
3061
        process_full_comp_unit (item->per_cu);
3062
 
3063
      item->per_cu->queued = 0;
3064
      next_item = item->next;
3065
      xfree (item);
3066
    }
3067
 
3068
  dwarf2_queue_tail = NULL;
3069
}
3070
 
3071
/* Free all allocated queue entries.  This function only releases anything if
3072
   an error was thrown; if the queue was processed then it would have been
3073
   freed as we went along.  */
3074
 
3075
static void
3076
dwarf2_release_queue (void *dummy)
3077
{
3078
  struct dwarf2_queue_item *item, *last;
3079
 
3080
  item = dwarf2_queue;
3081
  while (item)
3082
    {
3083
      /* Anything still marked queued is likely to be in an
3084
         inconsistent state, so discard it.  */
3085
      if (item->per_cu->queued)
3086
        {
3087
          if (item->per_cu->cu != NULL)
3088
            free_one_cached_comp_unit (item->per_cu->cu);
3089
          item->per_cu->queued = 0;
3090
        }
3091
 
3092
      last = item;
3093
      item = item->next;
3094
      xfree (last);
3095
    }
3096
 
3097
  dwarf2_queue = dwarf2_queue_tail = NULL;
3098
}
3099
 
3100
/* Read in full symbols for PST, and anything it depends on.  */
3101
 
3102
static void
3103
psymtab_to_symtab_1 (struct partial_symtab *pst)
3104
{
3105
  struct dwarf2_per_cu_data *per_cu;
3106
  struct cleanup *back_to;
3107
  int i;
3108
 
3109
  for (i = 0; i < pst->number_of_dependencies; i++)
3110
    if (!pst->dependencies[i]->readin)
3111
      {
3112
        /* Inform about additional files that need to be read in.  */
3113
        if (info_verbose)
3114
          {
3115
            /* FIXME: i18n: Need to make this a single string.  */
3116
            fputs_filtered (" ", gdb_stdout);
3117
            wrap_here ("");
3118
            fputs_filtered ("and ", gdb_stdout);
3119
            wrap_here ("");
3120
            printf_filtered ("%s...", pst->dependencies[i]->filename);
3121
            wrap_here ("");     /* Flush output */
3122
            gdb_flush (gdb_stdout);
3123
          }
3124
        psymtab_to_symtab_1 (pst->dependencies[i]);
3125
      }
3126
 
3127
  per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
3128
 
3129
  if (per_cu == NULL)
3130
    {
3131
      /* It's an include file, no symbols to read for it.
3132
         Everything is in the parent symtab.  */
3133
      pst->readin = 1;
3134
      return;
3135
    }
3136
 
3137
  back_to = make_cleanup (dwarf2_release_queue, NULL);
3138
 
3139
  queue_comp_unit (per_cu, pst->objfile);
3140
 
3141
  if (per_cu->from_debug_types)
3142
    read_signatured_type_at_offset (pst->objfile, per_cu->offset);
3143
  else
3144
    load_full_comp_unit (per_cu, pst->objfile);
3145
 
3146
  process_queue (pst->objfile);
3147
 
3148
  /* Age the cache, releasing compilation units that have not
3149
     been used recently.  */
3150
  age_cached_comp_units ();
3151
 
3152
  do_cleanups (back_to);
3153
}
3154
 
3155
/* Load the DIEs associated with PER_CU into memory.  */
3156
 
3157
static void
3158
load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
3159
{
3160
  bfd *abfd = objfile->obfd;
3161
  struct dwarf2_cu *cu;
3162
  unsigned int offset;
3163
  gdb_byte *info_ptr, *beg_of_comp_unit;
3164
  struct cleanup *back_to, *free_cu_cleanup;
3165
  struct attribute *attr;
3166
  CORE_ADDR baseaddr;
3167
 
3168
  gdb_assert (! per_cu->from_debug_types);
3169
 
3170
  /* Set local variables from the partial symbol table info.  */
3171
  offset = per_cu->offset;
3172
 
3173
  info_ptr = dwarf2_per_objfile->info.buffer + offset;
3174
  beg_of_comp_unit = info_ptr;
3175
 
3176
  cu = alloc_one_comp_unit (objfile);
3177
 
3178
  /* If an error occurs while loading, release our storage.  */
3179
  free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3180
 
3181
  /* Read in the comp_unit header.  */
3182
  info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
3183
 
3184
  /* Complete the cu_header.  */
3185
  cu->header.offset = offset;
3186
  cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3187
 
3188
  /* Read the abbrevs for this compilation unit.  */
3189
  dwarf2_read_abbrevs (abfd, cu);
3190
  back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
3191
 
3192
  /* Link this compilation unit into the compilation unit tree.  */
3193
  per_cu->cu = cu;
3194
  cu->per_cu = per_cu;
3195
  cu->type_hash = per_cu->type_hash;
3196
 
3197
  cu->dies = read_comp_unit (info_ptr, cu);
3198
 
3199
  /* We try not to read any attributes in this function, because not
3200
     all objfiles needed for references have been loaded yet, and symbol
3201
     table processing isn't initialized.  But we have to set the CU language,
3202
     or we won't be able to build types correctly.  */
3203
  attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
3204
  if (attr)
3205
    set_cu_language (DW_UNSND (attr), cu);
3206
  else
3207
    set_cu_language (language_minimal, cu);
3208
 
3209
  /* Link this CU into read_in_chain.  */
3210
  per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3211
  dwarf2_per_objfile->read_in_chain = per_cu;
3212
 
3213
  do_cleanups (back_to);
3214
 
3215
  /* We've successfully allocated this compilation unit.  Let our caller
3216
     clean it up when finished with it.  */
3217
  discard_cleanups (free_cu_cleanup);
3218
}
3219
 
3220
/* Generate full symbol information for PST and CU, whose DIEs have
3221
   already been loaded into memory.  */
3222
 
3223
static void
3224
process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
3225
{
3226
  struct partial_symtab *pst = per_cu->psymtab;
3227
  struct dwarf2_cu *cu = per_cu->cu;
3228
  struct objfile *objfile = pst->objfile;
3229
  bfd *abfd = objfile->obfd;
3230
  CORE_ADDR lowpc, highpc;
3231
  struct symtab *symtab;
3232
  struct cleanup *back_to;
3233
  CORE_ADDR baseaddr;
3234
 
3235
  baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3236
 
3237
  buildsym_init ();
3238
  back_to = make_cleanup (really_free_pendings, NULL);
3239
 
3240
  cu->list_in_scope = &file_symbols;
3241
 
3242
  dwarf2_find_base_address (cu->dies, cu);
3243
 
3244
  /* Do line number decoding in read_file_scope () */
3245
  process_die (cu->dies, cu);
3246
 
3247
  /* Some compilers don't define a DW_AT_high_pc attribute for the
3248
     compilation unit.  If the DW_AT_high_pc is missing, synthesize
3249
     it, by scanning the DIE's below the compilation unit.  */
3250
  get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
3251
 
3252
  symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
3253
 
3254
  /* Set symtab language to language from DW_AT_language.
3255
     If the compilation is from a C file generated by language preprocessors,
3256
     do not set the language if it was already deduced by start_subfile.  */
3257
  if (symtab != NULL
3258
      && !(cu->language == language_c && symtab->language != language_c))
3259
    {
3260
      symtab->language = cu->language;
3261
    }
3262
  pst->symtab = symtab;
3263
  pst->readin = 1;
3264
 
3265
  do_cleanups (back_to);
3266
}
3267
 
3268
/* Process a die and its children.  */
3269
 
3270
static void
3271
process_die (struct die_info *die, struct dwarf2_cu *cu)
3272
{
3273
  switch (die->tag)
3274
    {
3275
    case DW_TAG_padding:
3276
      break;
3277
    case DW_TAG_compile_unit:
3278
      read_file_scope (die, cu);
3279
      break;
3280
    case DW_TAG_type_unit:
3281
      read_type_unit_scope (die, cu);
3282
      break;
3283
    case DW_TAG_subprogram:
3284
    case DW_TAG_inlined_subroutine:
3285
      read_func_scope (die, cu);
3286
      break;
3287
    case DW_TAG_lexical_block:
3288
    case DW_TAG_try_block:
3289
    case DW_TAG_catch_block:
3290
      read_lexical_block_scope (die, cu);
3291
      break;
3292
    case DW_TAG_class_type:
3293
    case DW_TAG_interface_type:
3294
    case DW_TAG_structure_type:
3295
    case DW_TAG_union_type:
3296
      process_structure_scope (die, cu);
3297
      break;
3298
    case DW_TAG_enumeration_type:
3299
      process_enumeration_scope (die, cu);
3300
      break;
3301
 
3302
    /* These dies have a type, but processing them does not create
3303
       a symbol or recurse to process the children.  Therefore we can
3304
       read them on-demand through read_type_die.  */
3305
    case DW_TAG_subroutine_type:
3306
    case DW_TAG_set_type:
3307
    case DW_TAG_array_type:
3308
    case DW_TAG_pointer_type:
3309
    case DW_TAG_ptr_to_member_type:
3310
    case DW_TAG_reference_type:
3311
    case DW_TAG_string_type:
3312
      break;
3313
 
3314
    case DW_TAG_base_type:
3315
    case DW_TAG_subrange_type:
3316
    case DW_TAG_typedef:
3317
      /* Add a typedef symbol for the type definition, if it has a
3318
         DW_AT_name.  */
3319
      new_symbol (die, read_type_die (die, cu), cu);
3320
      break;
3321
    case DW_TAG_common_block:
3322
      read_common_block (die, cu);
3323
      break;
3324
    case DW_TAG_common_inclusion:
3325
      break;
3326
    case DW_TAG_namespace:
3327
      processing_has_namespace_info = 1;
3328
      read_namespace (die, cu);
3329
      break;
3330
    case DW_TAG_module:
3331
      read_module (die, cu);
3332
      break;
3333
    case DW_TAG_imported_declaration:
3334
    case DW_TAG_imported_module:
3335
      processing_has_namespace_info = 1;
3336
      if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
3337
                                 || cu->language != language_fortran))
3338
        complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
3339
                   dwarf_tag_name (die->tag));
3340
      read_import_statement (die, cu);
3341
      break;
3342
    default:
3343
      new_symbol (die, NULL, cu);
3344
      break;
3345
    }
3346
}
3347
 
3348
/* Return the fully qualified name of DIE, based on its DW_AT_name.
3349
   If scope qualifiers are appropriate they will be added.  The result
3350
   will be allocated on the objfile_obstack, or NULL if the DIE does
3351
   not have a name.  */
3352
 
3353
static const char *
3354
dwarf2_full_name (struct die_info *die, struct dwarf2_cu *cu)
3355
{
3356
  struct attribute *attr;
3357
  char *prefix, *name;
3358
  struct ui_file *buf = NULL;
3359
 
3360
  name = dwarf2_name (die, cu);
3361
  if (!name)
3362
    return NULL;
3363
 
3364
  /* These are the only languages we know how to qualify names in.  */
3365
  if (cu->language != language_cplus
3366
      && cu->language != language_java)
3367
    return name;
3368
 
3369
  /* If no prefix is necessary for this type of DIE, return the
3370
     unqualified name.  The other three tags listed could be handled
3371
     in pdi_needs_namespace, but that requires broader changes.  */
3372
  if (!pdi_needs_namespace (die->tag)
3373
      && die->tag != DW_TAG_subprogram
3374
      && die->tag != DW_TAG_variable
3375
      && die->tag != DW_TAG_member)
3376
    return name;
3377
 
3378
  prefix = determine_prefix (die, cu);
3379
  if (*prefix != '\0')
3380
    name = typename_concat (&cu->objfile->objfile_obstack, prefix,
3381
                            name, cu);
3382
 
3383
  return name;
3384
}
3385
 
3386
/* Read the import statement specified by the given die and record it.  */
3387
 
3388
static void
3389
read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
3390
{
3391
  struct attribute *import_attr;
3392
  struct die_info *imported_die;
3393
  struct dwarf2_cu *imported_cu;
3394
  const char *imported_name;
3395
  const char *imported_name_prefix;
3396
  char *import_alias;
3397
 
3398
  const char *import_prefix;
3399
  char *canonical_name;
3400
 
3401
  import_attr = dwarf2_attr (die, DW_AT_import, cu);
3402
  if (import_attr == NULL)
3403
    {
3404
      complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
3405
                 dwarf_tag_name (die->tag));
3406
      return;
3407
    }
3408
 
3409
  imported_cu = cu;
3410
  imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
3411
  imported_name = dwarf2_name (imported_die, imported_cu);
3412
  if (imported_name == NULL)
3413
    {
3414
      /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
3415
 
3416
        The import in the following code:
3417
        namespace A
3418
          {
3419
            typedef int B;
3420
          }
3421
 
3422
        int main ()
3423
          {
3424
            using A::B;
3425
            B b;
3426
            return b;
3427
          }
3428
 
3429
        ...
3430
         <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
3431
            <52>   DW_AT_decl_file   : 1
3432
            <53>   DW_AT_decl_line   : 6
3433
            <54>   DW_AT_import      : <0x75>
3434
         <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
3435
            <59>   DW_AT_name        : B
3436
            <5b>   DW_AT_decl_file   : 1
3437
            <5c>   DW_AT_decl_line   : 2
3438
            <5d>   DW_AT_type        : <0x6e>
3439
        ...
3440
         <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
3441
            <76>   DW_AT_byte_size   : 4
3442
            <77>   DW_AT_encoding    : 5        (signed)
3443
 
3444
        imports the wrong die ( 0x75 instead of 0x58 ).
3445
        This case will be ignored until the gcc bug is fixed.  */
3446
      return;
3447
    }
3448
 
3449
  /* Figure out the local name after import.  */
3450
  import_alias = dwarf2_name (die, cu);
3451
 
3452
  /* Figure out where the statement is being imported to.  */
3453
  import_prefix = determine_prefix (die, cu);
3454
 
3455
  /* Figure out what the scope of the imported die is and prepend it
3456
     to the name of the imported die.  */
3457
  imported_name_prefix = determine_prefix (imported_die, imported_cu);
3458
 
3459
  if (strlen (imported_name_prefix) > 0)
3460
    {
3461
      canonical_name = alloca (strlen (imported_name_prefix)
3462
                               + 2 + strlen (imported_name) + 1);
3463
      strcpy (canonical_name, imported_name_prefix);
3464
      strcat (canonical_name, "::");
3465
      strcat (canonical_name, imported_name);
3466
    }
3467
  else
3468
    {
3469
      canonical_name = alloca (strlen (imported_name) + 1);
3470
      strcpy (canonical_name, imported_name);
3471
    }
3472
 
3473
  using_directives = cp_add_using (import_prefix,
3474
                                   canonical_name,
3475
                                   import_alias,
3476
                                   using_directives);
3477
}
3478
 
3479
static void
3480
initialize_cu_func_list (struct dwarf2_cu *cu)
3481
{
3482
  cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
3483
}
3484
 
3485
static void
3486
free_cu_line_header (void *arg)
3487
{
3488
  struct dwarf2_cu *cu = arg;
3489
 
3490
  free_line_header (cu->line_header);
3491
  cu->line_header = NULL;
3492
}
3493
 
3494
static void
3495
read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
3496
{
3497
  struct objfile *objfile = cu->objfile;
3498
  struct comp_unit_head *cu_header = &cu->header;
3499
  struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3500
  CORE_ADDR lowpc = ((CORE_ADDR) -1);
3501
  CORE_ADDR highpc = ((CORE_ADDR) 0);
3502
  struct attribute *attr;
3503
  char *name = NULL;
3504
  char *comp_dir = NULL;
3505
  struct die_info *child_die;
3506
  bfd *abfd = objfile->obfd;
3507
  struct line_header *line_header = 0;
3508
  CORE_ADDR baseaddr;
3509
 
3510
  baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3511
 
3512
  get_scope_pc_bounds (die, &lowpc, &highpc, cu);
3513
 
3514
  /* If we didn't find a lowpc, set it to highpc to avoid complaints
3515
     from finish_block.  */
3516
  if (lowpc == ((CORE_ADDR) -1))
3517
    lowpc = highpc;
3518
  lowpc += baseaddr;
3519
  highpc += baseaddr;
3520
 
3521
  /* Find the filename.  Do not use dwarf2_name here, since the filename
3522
     is not a source language identifier.  */
3523
  attr = dwarf2_attr (die, DW_AT_name, cu);
3524
  if (attr)
3525
    {
3526
      name = DW_STRING (attr);
3527
    }
3528
 
3529
  attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
3530
  if (attr)
3531
    comp_dir = DW_STRING (attr);
3532
  else if (name != NULL && IS_ABSOLUTE_PATH (name))
3533
    {
3534
      comp_dir = ldirname (name);
3535
      if (comp_dir != NULL)
3536
        make_cleanup (xfree, comp_dir);
3537
    }
3538
  if (comp_dir != NULL)
3539
    {
3540
      /* Irix 6.2 native cc prepends <machine>.: to the compilation
3541
         directory, get rid of it.  */
3542
      char *cp = strchr (comp_dir, ':');
3543
 
3544
      if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
3545
        comp_dir = cp + 1;
3546
    }
3547
 
3548
  if (name == NULL)
3549
    name = "<unknown>";
3550
 
3551
  attr = dwarf2_attr (die, DW_AT_language, cu);
3552
  if (attr)
3553
    {
3554
      set_cu_language (DW_UNSND (attr), cu);
3555
    }
3556
 
3557
  attr = dwarf2_attr (die, DW_AT_producer, cu);
3558
  if (attr)
3559
    cu->producer = DW_STRING (attr);
3560
 
3561
  /* We assume that we're processing GCC output. */
3562
  processing_gcc_compilation = 2;
3563
 
3564
  processing_has_namespace_info = 0;
3565
 
3566
  start_symtab (name, comp_dir, lowpc);
3567
  record_debugformat ("DWARF 2");
3568
  record_producer (cu->producer);
3569
 
3570
  initialize_cu_func_list (cu);
3571
 
3572
  /* Decode line number information if present.  We do this before
3573
     processing child DIEs, so that the line header table is available
3574
     for DW_AT_decl_file.  */
3575
  attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3576
  if (attr)
3577
    {
3578
      unsigned int line_offset = DW_UNSND (attr);
3579
      line_header = dwarf_decode_line_header (line_offset, abfd, cu);
3580
      if (line_header)
3581
        {
3582
          cu->line_header = line_header;
3583
          make_cleanup (free_cu_line_header, cu);
3584
          dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
3585
        }
3586
    }
3587
 
3588
  /* Process all dies in compilation unit.  */
3589
  if (die->child != NULL)
3590
    {
3591
      child_die = die->child;
3592
      while (child_die && child_die->tag)
3593
        {
3594
          process_die (child_die, cu);
3595
          child_die = sibling_die (child_die);
3596
        }
3597
    }
3598
 
3599
  /* Decode macro information, if present.  Dwarf 2 macro information
3600
     refers to information in the line number info statement program
3601
     header, so we can only read it if we've read the header
3602
     successfully.  */
3603
  attr = dwarf2_attr (die, DW_AT_macro_info, cu);
3604
  if (attr && line_header)
3605
    {
3606
      unsigned int macro_offset = DW_UNSND (attr);
3607
      dwarf_decode_macros (line_header, macro_offset,
3608
                           comp_dir, abfd, cu);
3609
    }
3610
  do_cleanups (back_to);
3611
}
3612
 
3613
/* For TUs we want to skip the first top level sibling if it's not the
3614
   actual type being defined by this TU.  In this case the first top
3615
   level sibling is there to provide context only.  */
3616
 
3617
static void
3618
read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
3619
{
3620
  struct objfile *objfile = cu->objfile;
3621
  struct cleanup *back_to = make_cleanup (null_cleanup, 0);
3622
  CORE_ADDR lowpc;
3623
  struct attribute *attr;
3624
  char *name = NULL;
3625
  char *comp_dir = NULL;
3626
  struct die_info *child_die;
3627
  bfd *abfd = objfile->obfd;
3628
  struct line_header *line_header = 0;
3629
 
3630
  /* start_symtab needs a low pc, but we don't really have one.
3631
     Do what read_file_scope would do in the absence of such info.  */
3632
  lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3633
 
3634
  /* Find the filename.  Do not use dwarf2_name here, since the filename
3635
     is not a source language identifier.  */
3636
  attr = dwarf2_attr (die, DW_AT_name, cu);
3637
  if (attr)
3638
    name = DW_STRING (attr);
3639
 
3640
  attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
3641
  if (attr)
3642
    comp_dir = DW_STRING (attr);
3643
  else if (name != NULL && IS_ABSOLUTE_PATH (name))
3644
    {
3645
      comp_dir = ldirname (name);
3646
      if (comp_dir != NULL)
3647
        make_cleanup (xfree, comp_dir);
3648
    }
3649
 
3650
  if (name == NULL)
3651
    name = "<unknown>";
3652
 
3653
  attr = dwarf2_attr (die, DW_AT_language, cu);
3654
  if (attr)
3655
    set_cu_language (DW_UNSND (attr), cu);
3656
 
3657
  /* This isn't technically needed today.  It is done for symmetry
3658
     with read_file_scope.  */
3659
  attr = dwarf2_attr (die, DW_AT_producer, cu);
3660
  if (attr)
3661
    cu->producer = DW_STRING (attr);
3662
 
3663
  /* We assume that we're processing GCC output. */
3664
  processing_gcc_compilation = 2;
3665
 
3666
  processing_has_namespace_info = 0;
3667
 
3668
  start_symtab (name, comp_dir, lowpc);
3669
  record_debugformat ("DWARF 2");
3670
  record_producer (cu->producer);
3671
 
3672
  /* Process the dies in the type unit.  */
3673
  if (die->child == NULL)
3674
    {
3675
      dump_die_for_error (die);
3676
      error (_("Dwarf Error: Missing children for type unit [in module %s]"),
3677
             bfd_get_filename (abfd));
3678
    }
3679
 
3680
  child_die = die->child;
3681
 
3682
  while (child_die && child_die->tag)
3683
    {
3684
      process_die (child_die, cu);
3685
 
3686
      child_die = sibling_die (child_die);
3687
    }
3688
 
3689
  do_cleanups (back_to);
3690
}
3691
 
3692
static void
3693
add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
3694
                     struct dwarf2_cu *cu)
3695
{
3696
  struct function_range *thisfn;
3697
 
3698
  thisfn = (struct function_range *)
3699
    obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
3700
  thisfn->name = name;
3701
  thisfn->lowpc = lowpc;
3702
  thisfn->highpc = highpc;
3703
  thisfn->seen_line = 0;
3704
  thisfn->next = NULL;
3705
 
3706
  if (cu->last_fn == NULL)
3707
      cu->first_fn = thisfn;
3708
  else
3709
      cu->last_fn->next = thisfn;
3710
 
3711
  cu->last_fn = thisfn;
3712
}
3713
 
3714
/* qsort helper for inherit_abstract_dies.  */
3715
 
3716
static int
3717
unsigned_int_compar (const void *ap, const void *bp)
3718
{
3719
  unsigned int a = *(unsigned int *) ap;
3720
  unsigned int b = *(unsigned int *) bp;
3721
 
3722
  return (a > b) - (b > a);
3723
}
3724
 
3725
/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3726
   Inherit only the children of the DW_AT_abstract_origin DIE not being already
3727
   referenced by DW_AT_abstract_origin from the children of the current DIE.  */
3728
 
3729
static void
3730
inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
3731
{
3732
  struct die_info *child_die;
3733
  unsigned die_children_count;
3734
  /* CU offsets which were referenced by children of the current DIE.  */
3735
  unsigned *offsets;
3736
  unsigned *offsets_end, *offsetp;
3737
  /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
3738
  struct die_info *origin_die;
3739
  /* Iterator of the ORIGIN_DIE children.  */
3740
  struct die_info *origin_child_die;
3741
  struct cleanup *cleanups;
3742
  struct attribute *attr;
3743
 
3744
  attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
3745
  if (!attr)
3746
    return;
3747
 
3748
  origin_die = follow_die_ref (die, attr, &cu);
3749
  if (die->tag != origin_die->tag
3750
      && !(die->tag == DW_TAG_inlined_subroutine
3751
           && origin_die->tag == DW_TAG_subprogram))
3752
    complaint (&symfile_complaints,
3753
               _("DIE 0x%x and its abstract origin 0x%x have different tags"),
3754
               die->offset, origin_die->offset);
3755
 
3756
  child_die = die->child;
3757
  die_children_count = 0;
3758
  while (child_die && child_die->tag)
3759
    {
3760
      child_die = sibling_die (child_die);
3761
      die_children_count++;
3762
    }
3763
  offsets = xmalloc (sizeof (*offsets) * die_children_count);
3764
  cleanups = make_cleanup (xfree, offsets);
3765
 
3766
  offsets_end = offsets;
3767
  child_die = die->child;
3768
  while (child_die && child_die->tag)
3769
    {
3770
      /* For each CHILD_DIE, find the corresponding child of
3771
         ORIGIN_DIE.  If there is more than one layer of
3772
         DW_AT_abstract_origin, follow them all; there shouldn't be,
3773
         but GCC versions at least through 4.4 generate this (GCC PR
3774
         40573).  */
3775
      struct die_info *child_origin_die = child_die;
3776
      while (1)
3777
        {
3778
          attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
3779
          if (attr == NULL)
3780
            break;
3781
          child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
3782
        }
3783
 
3784
      /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
3785
         counterpart may exist.  */
3786
      if (child_origin_die != child_die)
3787
        {
3788
          if (child_die->tag != child_origin_die->tag
3789
              && !(child_die->tag == DW_TAG_inlined_subroutine
3790
                   && child_origin_die->tag == DW_TAG_subprogram))
3791
            complaint (&symfile_complaints,
3792
                       _("Child DIE 0x%x and its abstract origin 0x%x have "
3793
                         "different tags"), child_die->offset,
3794
                       child_origin_die->offset);
3795
          if (child_origin_die->parent != origin_die)
3796
            complaint (&symfile_complaints,
3797
                       _("Child DIE 0x%x and its abstract origin 0x%x have "
3798
                         "different parents"), child_die->offset,
3799
                       child_origin_die->offset);
3800
          else
3801
            *offsets_end++ = child_origin_die->offset;
3802
        }
3803
      child_die = sibling_die (child_die);
3804
    }
3805
  qsort (offsets, offsets_end - offsets, sizeof (*offsets),
3806
         unsigned_int_compar);
3807
  for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
3808
    if (offsetp[-1] == *offsetp)
3809
      complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
3810
                                        "to DIE 0x%x as their abstract origin"),
3811
                 die->offset, *offsetp);
3812
 
3813
  offsetp = offsets;
3814
  origin_child_die = origin_die->child;
3815
  while (origin_child_die && origin_child_die->tag)
3816
    {
3817
      /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
3818
      while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
3819
        offsetp++;
3820
      if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
3821
        {
3822
          /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
3823
          process_die (origin_child_die, cu);
3824
        }
3825
      origin_child_die = sibling_die (origin_child_die);
3826
    }
3827
 
3828
  do_cleanups (cleanups);
3829
}
3830
 
3831
static void
3832
read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
3833
{
3834
  struct objfile *objfile = cu->objfile;
3835
  struct context_stack *new;
3836
  CORE_ADDR lowpc;
3837
  CORE_ADDR highpc;
3838
  struct die_info *child_die;
3839
  struct attribute *attr, *call_line, *call_file;
3840
  char *name;
3841
  CORE_ADDR baseaddr;
3842
  struct block *block;
3843
  int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
3844
 
3845
  if (inlined_func)
3846
    {
3847
      /* If we do not have call site information, we can't show the
3848
         caller of this inlined function.  That's too confusing, so
3849
         only use the scope for local variables.  */
3850
      call_line = dwarf2_attr (die, DW_AT_call_line, cu);
3851
      call_file = dwarf2_attr (die, DW_AT_call_file, cu);
3852
      if (call_line == NULL || call_file == NULL)
3853
        {
3854
          read_lexical_block_scope (die, cu);
3855
          return;
3856
        }
3857
    }
3858
 
3859
  baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3860
 
3861
  name = dwarf2_linkage_name (die, cu);
3862
 
3863
  /* Ignore functions with missing or empty names and functions with
3864
     missing or invalid low and high pc attributes.  */
3865
  if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
3866
    return;
3867
 
3868
  lowpc += baseaddr;
3869
  highpc += baseaddr;
3870
 
3871
  /* Record the function range for dwarf_decode_lines.  */
3872
  add_to_cu_func_list (name, lowpc, highpc, cu);
3873
 
3874
  new = push_context (0, lowpc);
3875
  new->name = new_symbol (die, read_type_die (die, cu), cu);
3876
 
3877
  /* If there is a location expression for DW_AT_frame_base, record
3878
     it.  */
3879
  attr = dwarf2_attr (die, DW_AT_frame_base, cu);
3880
  if (attr)
3881
    /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
3882
       expression is being recorded directly in the function's symbol
3883
       and not in a separate frame-base object.  I guess this hack is
3884
       to avoid adding some sort of frame-base adjunct/annex to the
3885
       function's symbol :-(.  The problem with doing this is that it
3886
       results in a function symbol with a location expression that
3887
       has nothing to do with the location of the function, ouch!  The
3888
       relationship should be: a function's symbol has-a frame base; a
3889
       frame-base has-a location expression.  */
3890
    dwarf2_symbol_mark_computed (attr, new->name, cu);
3891
 
3892
  cu->list_in_scope = &local_symbols;
3893
 
3894
  if (die->child != NULL)
3895
    {
3896
      child_die = die->child;
3897
      while (child_die && child_die->tag)
3898
        {
3899
          process_die (child_die, cu);
3900
          child_die = sibling_die (child_die);
3901
        }
3902
    }
3903
 
3904
  inherit_abstract_dies (die, cu);
3905
 
3906
  new = pop_context ();
3907
  /* Make a block for the local symbols within.  */
3908
  block = finish_block (new->name, &local_symbols, new->old_blocks,
3909
                        lowpc, highpc, objfile);
3910
 
3911
  /* For C++, set the block's scope.  */
3912
  if (cu->language == language_cplus)
3913
    cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
3914
                        determine_prefix (die, cu),
3915
                        processing_has_namespace_info);
3916
 
3917
  /* If we have address ranges, record them.  */
3918
  dwarf2_record_block_ranges (die, block, baseaddr, cu);
3919
 
3920
  /* In C++, we can have functions nested inside functions (e.g., when
3921
     a function declares a class that has methods).  This means that
3922
     when we finish processing a function scope, we may need to go
3923
     back to building a containing block's symbol lists.  */
3924
  local_symbols = new->locals;
3925
  param_symbols = new->params;
3926
  using_directives = new->using_directives;
3927
 
3928
  /* If we've finished processing a top-level function, subsequent
3929
     symbols go in the file symbol list.  */
3930
  if (outermost_context_p ())
3931
    cu->list_in_scope = &file_symbols;
3932
}
3933
 
3934
/* Process all the DIES contained within a lexical block scope.  Start
3935
   a new scope, process the dies, and then close the scope.  */
3936
 
3937
static void
3938
read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
3939
{
3940
  struct objfile *objfile = cu->objfile;
3941
  struct context_stack *new;
3942
  CORE_ADDR lowpc, highpc;
3943
  struct die_info *child_die;
3944
  CORE_ADDR baseaddr;
3945
 
3946
  baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3947
 
3948
  /* Ignore blocks with missing or invalid low and high pc attributes.  */
3949
  /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
3950
     as multiple lexical blocks?  Handling children in a sane way would
3951
     be nasty.  Might be easier to properly extend generic blocks to
3952
     describe ranges.  */
3953
  if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
3954
    return;
3955
  lowpc += baseaddr;
3956
  highpc += baseaddr;
3957
 
3958
  push_context (0, lowpc);
3959
  if (die->child != NULL)
3960
    {
3961
      child_die = die->child;
3962
      while (child_die && child_die->tag)
3963
        {
3964
          process_die (child_die, cu);
3965
          child_die = sibling_die (child_die);
3966
        }
3967
    }
3968
  new = pop_context ();
3969
 
3970
  if (local_symbols != NULL || using_directives != NULL)
3971
    {
3972
      struct block *block
3973
        = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
3974
                        highpc, objfile);
3975
 
3976
      /* Note that recording ranges after traversing children, as we
3977
         do here, means that recording a parent's ranges entails
3978
         walking across all its children's ranges as they appear in
3979
         the address map, which is quadratic behavior.
3980
 
3981
         It would be nicer to record the parent's ranges before
3982
         traversing its children, simply overriding whatever you find
3983
         there.  But since we don't even decide whether to create a
3984
         block until after we've traversed its children, that's hard
3985
         to do.  */
3986
      dwarf2_record_block_ranges (die, block, baseaddr, cu);
3987
    }
3988
  local_symbols = new->locals;
3989
  using_directives = new->using_directives;
3990
}
3991
 
3992
/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
3993
   Return 1 if the attributes are present and valid, otherwise, return 0.
3994
   If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
3995
 
3996
static int
3997
dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
3998
                    CORE_ADDR *high_return, struct dwarf2_cu *cu,
3999
                    struct partial_symtab *ranges_pst)
4000
{
4001
  struct objfile *objfile = cu->objfile;
4002
  struct comp_unit_head *cu_header = &cu->header;
4003
  bfd *obfd = objfile->obfd;
4004
  unsigned int addr_size = cu_header->addr_size;
4005
  CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
4006
  /* Base address selection entry.  */
4007
  CORE_ADDR base;
4008
  int found_base;
4009
  unsigned int dummy;
4010
  gdb_byte *buffer;
4011
  CORE_ADDR marker;
4012
  int low_set;
4013
  CORE_ADDR low = 0;
4014
  CORE_ADDR high = 0;
4015
  CORE_ADDR baseaddr;
4016
 
4017
  found_base = cu->base_known;
4018
  base = cu->base_address;
4019
 
4020
  if (offset >= dwarf2_per_objfile->ranges.size)
4021
    {
4022
      complaint (&symfile_complaints,
4023
                 _("Offset %d out of bounds for DW_AT_ranges attribute"),
4024
                 offset);
4025
      return 0;
4026
    }
4027
  buffer = dwarf2_per_objfile->ranges.buffer + offset;
4028
 
4029
  /* Read in the largest possible address.  */
4030
  marker = read_address (obfd, buffer, cu, &dummy);
4031
  if ((marker & mask) == mask)
4032
    {
4033
      /* If we found the largest possible address, then
4034
         read the base address.  */
4035
      base = read_address (obfd, buffer + addr_size, cu, &dummy);
4036
      buffer += 2 * addr_size;
4037
      offset += 2 * addr_size;
4038
      found_base = 1;
4039
    }
4040
 
4041
  low_set = 0;
4042
 
4043
  baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4044
 
4045
  while (1)
4046
    {
4047
      CORE_ADDR range_beginning, range_end;
4048
 
4049
      range_beginning = read_address (obfd, buffer, cu, &dummy);
4050
      buffer += addr_size;
4051
      range_end = read_address (obfd, buffer, cu, &dummy);
4052
      buffer += addr_size;
4053
      offset += 2 * addr_size;
4054
 
4055
      /* An end of list marker is a pair of zero addresses.  */
4056
      if (range_beginning == 0 && range_end == 0)
4057
        /* Found the end of list entry.  */
4058
        break;
4059
 
4060
      /* Each base address selection entry is a pair of 2 values.
4061
         The first is the largest possible address, the second is
4062
         the base address.  Check for a base address here.  */
4063
      if ((range_beginning & mask) == mask)
4064
        {
4065
          /* If we found the largest possible address, then
4066
             read the base address.  */
4067
          base = read_address (obfd, buffer + addr_size, cu, &dummy);
4068
          found_base = 1;
4069
          continue;
4070
        }
4071
 
4072
      if (!found_base)
4073
        {
4074
          /* We have no valid base address for the ranges
4075
             data.  */
4076
          complaint (&symfile_complaints,
4077
                     _("Invalid .debug_ranges data (no base address)"));
4078
          return 0;
4079
        }
4080
 
4081
      range_beginning += base;
4082
      range_end += base;
4083
 
4084
      if (ranges_pst != NULL && range_beginning < range_end)
4085
        addrmap_set_empty (objfile->psymtabs_addrmap,
4086
                           range_beginning + baseaddr, range_end - 1 + baseaddr,
4087
                           ranges_pst);
4088
 
4089
      /* FIXME: This is recording everything as a low-high
4090
         segment of consecutive addresses.  We should have a
4091
         data structure for discontiguous block ranges
4092
         instead.  */
4093
      if (! low_set)
4094
        {
4095
          low = range_beginning;
4096
          high = range_end;
4097
          low_set = 1;
4098
        }
4099
      else
4100
        {
4101
          if (range_beginning < low)
4102
            low = range_beginning;
4103
          if (range_end > high)
4104
            high = range_end;
4105
        }
4106
    }
4107
 
4108
  if (! low_set)
4109
    /* If the first entry is an end-of-list marker, the range
4110
       describes an empty scope, i.e. no instructions.  */
4111
    return 0;
4112
 
4113
  if (low_return)
4114
    *low_return = low;
4115
  if (high_return)
4116
    *high_return = high;
4117
  return 1;
4118
}
4119
 
4120
/* Get low and high pc attributes from a die.  Return 1 if the attributes
4121
   are present and valid, otherwise, return 0.  Return -1 if the range is
4122
   discontinuous, i.e. derived from DW_AT_ranges information.  */
4123
static int
4124
dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
4125
                      CORE_ADDR *highpc, struct dwarf2_cu *cu,
4126
                      struct partial_symtab *pst)
4127
{
4128
  struct attribute *attr;
4129
  CORE_ADDR low = 0;
4130
  CORE_ADDR high = 0;
4131
  int ret = 0;
4132
 
4133
  attr = dwarf2_attr (die, DW_AT_high_pc, cu);
4134
  if (attr)
4135
    {
4136
      high = DW_ADDR (attr);
4137
      attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4138
      if (attr)
4139
        low = DW_ADDR (attr);
4140
      else
4141
        /* Found high w/o low attribute.  */
4142
        return 0;
4143
 
4144
      /* Found consecutive range of addresses.  */
4145
      ret = 1;
4146
    }
4147
  else
4148
    {
4149
      attr = dwarf2_attr (die, DW_AT_ranges, cu);
4150
      if (attr != NULL)
4151
        {
4152
          /* Value of the DW_AT_ranges attribute is the offset in the
4153
             .debug_ranges section.  */
4154
          if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
4155
            return 0;
4156
          /* Found discontinuous range of addresses.  */
4157
          ret = -1;
4158
        }
4159
    }
4160
 
4161
  if (high < low)
4162
    return 0;
4163
 
4164
  /* When using the GNU linker, .gnu.linkonce. sections are used to
4165
     eliminate duplicate copies of functions and vtables and such.
4166
     The linker will arbitrarily choose one and discard the others.
4167
     The AT_*_pc values for such functions refer to local labels in
4168
     these sections.  If the section from that file was discarded, the
4169
     labels are not in the output, so the relocs get a value of 0.
4170
     If this is a discarded function, mark the pc bounds as invalid,
4171
     so that GDB will ignore it.  */
4172
  if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
4173
    return 0;
4174
 
4175
  *lowpc = low;
4176
  *highpc = high;
4177
  return ret;
4178
}
4179
 
4180
/* Assuming that DIE represents a subprogram DIE or a lexical block, get
4181
   its low and high PC addresses.  Do nothing if these addresses could not
4182
   be determined.  Otherwise, set LOWPC to the low address if it is smaller,
4183
   and HIGHPC to the high address if greater than HIGHPC.  */
4184
 
4185
static void
4186
dwarf2_get_subprogram_pc_bounds (struct die_info *die,
4187
                                 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4188
                                 struct dwarf2_cu *cu)
4189
{
4190
  CORE_ADDR low, high;
4191
  struct die_info *child = die->child;
4192
 
4193
  if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
4194
    {
4195
      *lowpc = min (*lowpc, low);
4196
      *highpc = max (*highpc, high);
4197
    }
4198
 
4199
  /* If the language does not allow nested subprograms (either inside
4200
     subprograms or lexical blocks), we're done.  */
4201
  if (cu->language != language_ada)
4202
    return;
4203
 
4204
  /* Check all the children of the given DIE.  If it contains nested
4205
     subprograms, then check their pc bounds.  Likewise, we need to
4206
     check lexical blocks as well, as they may also contain subprogram
4207
     definitions.  */
4208
  while (child && child->tag)
4209
    {
4210
      if (child->tag == DW_TAG_subprogram
4211
          || child->tag == DW_TAG_lexical_block)
4212
        dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
4213
      child = sibling_die (child);
4214
    }
4215
}
4216
 
4217
/* Get the low and high pc's represented by the scope DIE, and store
4218
   them in *LOWPC and *HIGHPC.  If the correct values can't be
4219
   determined, set *LOWPC to -1 and *HIGHPC to 0.  */
4220
 
4221
static void
4222
get_scope_pc_bounds (struct die_info *die,
4223
                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
4224
                     struct dwarf2_cu *cu)
4225
{
4226
  CORE_ADDR best_low = (CORE_ADDR) -1;
4227
  CORE_ADDR best_high = (CORE_ADDR) 0;
4228
  CORE_ADDR current_low, current_high;
4229
 
4230
  if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
4231
    {
4232
      best_low = current_low;
4233
      best_high = current_high;
4234
    }
4235
  else
4236
    {
4237
      struct die_info *child = die->child;
4238
 
4239
      while (child && child->tag)
4240
        {
4241
          switch (child->tag) {
4242
          case DW_TAG_subprogram:
4243
            dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
4244
            break;
4245
          case DW_TAG_namespace:
4246
            /* FIXME: carlton/2004-01-16: Should we do this for
4247
               DW_TAG_class_type/DW_TAG_structure_type, too?  I think
4248
               that current GCC's always emit the DIEs corresponding
4249
               to definitions of methods of classes as children of a
4250
               DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
4251
               the DIEs giving the declarations, which could be
4252
               anywhere).  But I don't see any reason why the
4253
               standards says that they have to be there.  */
4254
            get_scope_pc_bounds (child, &current_low, &current_high, cu);
4255
 
4256
            if (current_low != ((CORE_ADDR) -1))
4257
              {
4258
                best_low = min (best_low, current_low);
4259
                best_high = max (best_high, current_high);
4260
              }
4261
            break;
4262
          default:
4263
            /* Ignore. */
4264
            break;
4265
          }
4266
 
4267
          child = sibling_die (child);
4268
        }
4269
    }
4270
 
4271
  *lowpc = best_low;
4272
  *highpc = best_high;
4273
}
4274
 
4275
/* Record the address ranges for BLOCK, offset by BASEADDR, as given
4276
   in DIE.  */
4277
static void
4278
dwarf2_record_block_ranges (struct die_info *die, struct block *block,
4279
                            CORE_ADDR baseaddr, struct dwarf2_cu *cu)
4280
{
4281
  struct attribute *attr;
4282
 
4283
  attr = dwarf2_attr (die, DW_AT_high_pc, cu);
4284
  if (attr)
4285
    {
4286
      CORE_ADDR high = DW_ADDR (attr);
4287
      attr = dwarf2_attr (die, DW_AT_low_pc, cu);
4288
      if (attr)
4289
        {
4290
          CORE_ADDR low = DW_ADDR (attr);
4291
          record_block_range (block, baseaddr + low, baseaddr + high - 1);
4292
        }
4293
    }
4294
 
4295
  attr = dwarf2_attr (die, DW_AT_ranges, cu);
4296
  if (attr)
4297
    {
4298
      bfd *obfd = cu->objfile->obfd;
4299
 
4300
      /* The value of the DW_AT_ranges attribute is the offset of the
4301
         address range list in the .debug_ranges section.  */
4302
      unsigned long offset = DW_UNSND (attr);
4303
      gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
4304
 
4305
      /* For some target architectures, but not others, the
4306
         read_address function sign-extends the addresses it returns.
4307
         To recognize base address selection entries, we need a
4308
         mask.  */
4309
      unsigned int addr_size = cu->header.addr_size;
4310
      CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
4311
 
4312
      /* The base address, to which the next pair is relative.  Note
4313
         that this 'base' is a DWARF concept: most entries in a range
4314
         list are relative, to reduce the number of relocs against the
4315
         debugging information.  This is separate from this function's
4316
         'baseaddr' argument, which GDB uses to relocate debugging
4317
         information from a shared library based on the address at
4318
         which the library was loaded.  */
4319
      CORE_ADDR base = cu->base_address;
4320
      int base_known = cu->base_known;
4321
 
4322
      if (offset >= dwarf2_per_objfile->ranges.size)
4323
        {
4324
          complaint (&symfile_complaints,
4325
                     _("Offset %lu out of bounds for DW_AT_ranges attribute"),
4326
                     offset);
4327
          return;
4328
        }
4329
 
4330
      for (;;)
4331
        {
4332
          unsigned int bytes_read;
4333
          CORE_ADDR start, end;
4334
 
4335
          start = read_address (obfd, buffer, cu, &bytes_read);
4336
          buffer += bytes_read;
4337
          end = read_address (obfd, buffer, cu, &bytes_read);
4338
          buffer += bytes_read;
4339
 
4340
          /* Did we find the end of the range list?  */
4341
          if (start == 0 && end == 0)
4342
            break;
4343
 
4344
          /* Did we find a base address selection entry?  */
4345
          else if ((start & base_select_mask) == base_select_mask)
4346
            {
4347
              base = end;
4348
              base_known = 1;
4349
            }
4350
 
4351
          /* We found an ordinary address range.  */
4352
          else
4353
            {
4354
              if (!base_known)
4355
                {
4356
                  complaint (&symfile_complaints,
4357
                             _("Invalid .debug_ranges data (no base address)"));
4358
                  return;
4359
                }
4360
 
4361
              record_block_range (block,
4362
                                  baseaddr + base + start,
4363
                                  baseaddr + base + end - 1);
4364
            }
4365
        }
4366
    }
4367
}
4368
 
4369
/* Add an aggregate field to the field list.  */
4370
 
4371
static void
4372
dwarf2_add_field (struct field_info *fip, struct die_info *die,
4373
                  struct dwarf2_cu *cu)
4374
{
4375
  struct objfile *objfile = cu->objfile;
4376
  struct gdbarch *gdbarch = get_objfile_arch (objfile);
4377
  struct nextfield *new_field;
4378
  struct attribute *attr;
4379
  struct field *fp;
4380
  char *fieldname = "";
4381
 
4382
  /* Allocate a new field list entry and link it in.  */
4383
  new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
4384
  make_cleanup (xfree, new_field);
4385
  memset (new_field, 0, sizeof (struct nextfield));
4386
 
4387
  if (die->tag == DW_TAG_inheritance)
4388
    {
4389
      new_field->next = fip->baseclasses;
4390
      fip->baseclasses = new_field;
4391
    }
4392
  else
4393
    {
4394
      new_field->next = fip->fields;
4395
      fip->fields = new_field;
4396
    }
4397
  fip->nfields++;
4398
 
4399
  /* Handle accessibility and virtuality of field.
4400
     The default accessibility for members is public, the default
4401
     accessibility for inheritance is private.  */
4402
  if (die->tag != DW_TAG_inheritance)
4403
    new_field->accessibility = DW_ACCESS_public;
4404
  else
4405
    new_field->accessibility = DW_ACCESS_private;
4406
  new_field->virtuality = DW_VIRTUALITY_none;
4407
 
4408
  attr = dwarf2_attr (die, DW_AT_accessibility, cu);
4409
  if (attr)
4410
    new_field->accessibility = DW_UNSND (attr);
4411
  if (new_field->accessibility != DW_ACCESS_public)
4412
    fip->non_public_fields = 1;
4413
  attr = dwarf2_attr (die, DW_AT_virtuality, cu);
4414
  if (attr)
4415
    new_field->virtuality = DW_UNSND (attr);
4416
 
4417
  fp = &new_field->field;
4418
 
4419
  if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
4420
    {
4421
      /* Data member other than a C++ static data member.  */
4422
 
4423
      /* Get type of field.  */
4424
      fp->type = die_type (die, cu);
4425
 
4426
      SET_FIELD_BITPOS (*fp, 0);
4427
 
4428
      /* Get bit size of field (zero if none).  */
4429
      attr = dwarf2_attr (die, DW_AT_bit_size, cu);
4430
      if (attr)
4431
        {
4432
          FIELD_BITSIZE (*fp) = DW_UNSND (attr);
4433
        }
4434
      else
4435
        {
4436
          FIELD_BITSIZE (*fp) = 0;
4437
        }
4438
 
4439
      /* Get bit offset of field.  */
4440
      attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
4441
      if (attr)
4442
        {
4443
          int byte_offset = 0;
4444
 
4445
          if (attr_form_is_section_offset (attr))
4446
            dwarf2_complex_location_expr_complaint ();
4447
          else if (attr_form_is_constant (attr))
4448
            byte_offset = dwarf2_get_attr_constant_value (attr, 0);
4449
          else if (attr_form_is_block (attr))
4450
            byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
4451
          else
4452
            dwarf2_complex_location_expr_complaint ();
4453
 
4454
          SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
4455
        }
4456
      attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
4457
      if (attr)
4458
        {
4459
          if (gdbarch_bits_big_endian (gdbarch))
4460
            {
4461
              /* For big endian bits, the DW_AT_bit_offset gives the
4462
                 additional bit offset from the MSB of the containing
4463
                 anonymous object to the MSB of the field.  We don't
4464
                 have to do anything special since we don't need to
4465
                 know the size of the anonymous object.  */
4466
              FIELD_BITPOS (*fp) += DW_UNSND (attr);
4467
            }
4468
          else
4469
            {
4470
              /* For little endian bits, compute the bit offset to the
4471
                 MSB of the anonymous object, subtract off the number of
4472
                 bits from the MSB of the field to the MSB of the
4473
                 object, and then subtract off the number of bits of
4474
                 the field itself.  The result is the bit offset of
4475
                 the LSB of the field.  */
4476
              int anonymous_size;
4477
              int bit_offset = DW_UNSND (attr);
4478
 
4479
              attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4480
              if (attr)
4481
                {
4482
                  /* The size of the anonymous object containing
4483
                     the bit field is explicit, so use the
4484
                     indicated size (in bytes).  */
4485
                  anonymous_size = DW_UNSND (attr);
4486
                }
4487
              else
4488
                {
4489
                  /* The size of the anonymous object containing
4490
                     the bit field must be inferred from the type
4491
                     attribute of the data member containing the
4492
                     bit field.  */
4493
                  anonymous_size = TYPE_LENGTH (fp->type);
4494
                }
4495
              FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
4496
                - bit_offset - FIELD_BITSIZE (*fp);
4497
            }
4498
        }
4499
 
4500
      /* Get name of field.  */
4501
      fieldname = dwarf2_name (die, cu);
4502
      if (fieldname == NULL)
4503
        fieldname = "";
4504
 
4505
      /* The name is already allocated along with this objfile, so we don't
4506
         need to duplicate it for the type.  */
4507
      fp->name = fieldname;
4508
 
4509
      /* Change accessibility for artificial fields (e.g. virtual table
4510
         pointer or virtual base class pointer) to private.  */
4511
      if (dwarf2_attr (die, DW_AT_artificial, cu))
4512
        {
4513
          FIELD_ARTIFICIAL (*fp) = 1;
4514
          new_field->accessibility = DW_ACCESS_private;
4515
          fip->non_public_fields = 1;
4516
        }
4517
    }
4518
  else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
4519
    {
4520
      /* C++ static member.  */
4521
 
4522
      /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
4523
         is a declaration, but all versions of G++ as of this writing
4524
         (so through at least 3.2.1) incorrectly generate
4525
         DW_TAG_variable tags.  */
4526
 
4527
      char *physname;
4528
 
4529
      /* Get name of field.  */
4530
      fieldname = dwarf2_name (die, cu);
4531
      if (fieldname == NULL)
4532
        return;
4533
 
4534
      /* Get physical name.  */
4535
      physname = dwarf2_linkage_name (die, cu);
4536
 
4537
      /* The name is already allocated along with this objfile, so we don't
4538
         need to duplicate it for the type.  */
4539
      SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
4540
      FIELD_TYPE (*fp) = die_type (die, cu);
4541
      FIELD_NAME (*fp) = fieldname;
4542
    }
4543
  else if (die->tag == DW_TAG_inheritance)
4544
    {
4545
      /* C++ base class field.  */
4546
      attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
4547
      if (attr)
4548
        {
4549
          int byte_offset = 0;
4550
 
4551
          if (attr_form_is_section_offset (attr))
4552
            dwarf2_complex_location_expr_complaint ();
4553
          else if (attr_form_is_constant (attr))
4554
            byte_offset = dwarf2_get_attr_constant_value (attr, 0);
4555
          else if (attr_form_is_block (attr))
4556
            byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
4557
          else
4558
            dwarf2_complex_location_expr_complaint ();
4559
 
4560
          SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
4561
        }
4562
      FIELD_BITSIZE (*fp) = 0;
4563
      FIELD_TYPE (*fp) = die_type (die, cu);
4564
      FIELD_NAME (*fp) = type_name_no_tag (fp->type);
4565
      fip->nbaseclasses++;
4566
    }
4567
}
4568
 
4569
/* Create the vector of fields, and attach it to the type.  */
4570
 
4571
static void
4572
dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
4573
                              struct dwarf2_cu *cu)
4574
{
4575
  int nfields = fip->nfields;
4576
 
4577
  /* Record the field count, allocate space for the array of fields,
4578
     and create blank accessibility bitfields if necessary.  */
4579
  TYPE_NFIELDS (type) = nfields;
4580
  TYPE_FIELDS (type) = (struct field *)
4581
    TYPE_ALLOC (type, sizeof (struct field) * nfields);
4582
  memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
4583
 
4584
  if (fip->non_public_fields && cu->language != language_ada)
4585
    {
4586
      ALLOCATE_CPLUS_STRUCT_TYPE (type);
4587
 
4588
      TYPE_FIELD_PRIVATE_BITS (type) =
4589
        (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4590
      B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
4591
 
4592
      TYPE_FIELD_PROTECTED_BITS (type) =
4593
        (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4594
      B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
4595
 
4596
      TYPE_FIELD_IGNORE_BITS (type) =
4597
        (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
4598
      B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
4599
    }
4600
 
4601
  /* If the type has baseclasses, allocate and clear a bit vector for
4602
     TYPE_FIELD_VIRTUAL_BITS.  */
4603
  if (fip->nbaseclasses && cu->language != language_ada)
4604
    {
4605
      int num_bytes = B_BYTES (fip->nbaseclasses);
4606
      unsigned char *pointer;
4607
 
4608
      ALLOCATE_CPLUS_STRUCT_TYPE (type);
4609
      pointer = TYPE_ALLOC (type, num_bytes);
4610
      TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
4611
      B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
4612
      TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
4613
    }
4614
 
4615
  /* Copy the saved-up fields into the field vector.  Start from the head
4616
     of the list, adding to the tail of the field array, so that they end
4617
     up in the same order in the array in which they were added to the list.  */
4618
  while (nfields-- > 0)
4619
    {
4620
      struct nextfield *fieldp;
4621
 
4622
      if (fip->fields)
4623
        {
4624
          fieldp = fip->fields;
4625
          fip->fields = fieldp->next;
4626
        }
4627
      else
4628
        {
4629
          fieldp = fip->baseclasses;
4630
          fip->baseclasses = fieldp->next;
4631
        }
4632
 
4633
      TYPE_FIELD (type, nfields) = fieldp->field;
4634
      switch (fieldp->accessibility)
4635
        {
4636
        case DW_ACCESS_private:
4637
          if (cu->language != language_ada)
4638
            SET_TYPE_FIELD_PRIVATE (type, nfields);
4639
          break;
4640
 
4641
        case DW_ACCESS_protected:
4642
          if (cu->language != language_ada)
4643
            SET_TYPE_FIELD_PROTECTED (type, nfields);
4644
          break;
4645
 
4646
        case DW_ACCESS_public:
4647
          break;
4648
 
4649
        default:
4650
          /* Unknown accessibility.  Complain and treat it as public.  */
4651
          {
4652
            complaint (&symfile_complaints, _("unsupported accessibility %d"),
4653
                       fieldp->accessibility);
4654
          }
4655
          break;
4656
        }
4657
      if (nfields < fip->nbaseclasses)
4658
        {
4659
          switch (fieldp->virtuality)
4660
            {
4661
            case DW_VIRTUALITY_virtual:
4662
            case DW_VIRTUALITY_pure_virtual:
4663
              if (cu->language == language_ada)
4664
                error ("unexpected virtuality in component of Ada type");
4665
              SET_TYPE_FIELD_VIRTUAL (type, nfields);
4666
              break;
4667
            }
4668
        }
4669
    }
4670
}
4671
 
4672
/* Add a member function to the proper fieldlist.  */
4673
 
4674
static void
4675
dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
4676
                      struct type *type, struct dwarf2_cu *cu)
4677
{
4678
  struct objfile *objfile = cu->objfile;
4679
  struct attribute *attr;
4680
  struct fnfieldlist *flp;
4681
  int i;
4682
  struct fn_field *fnp;
4683
  char *fieldname;
4684
  char *physname;
4685
  struct nextfnfield *new_fnfield;
4686
  struct type *this_type;
4687
 
4688
  if (cu->language == language_ada)
4689
    error ("unexpected member function in Ada type");
4690
 
4691
  /* Get name of member function.  */
4692
  fieldname = dwarf2_name (die, cu);
4693
  if (fieldname == NULL)
4694
    return;
4695
 
4696
  /* Get the mangled name.  */
4697
  physname = dwarf2_linkage_name (die, cu);
4698
 
4699
  /* Look up member function name in fieldlist.  */
4700
  for (i = 0; i < fip->nfnfields; i++)
4701
    {
4702
      if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
4703
        break;
4704
    }
4705
 
4706
  /* Create new list element if necessary.  */
4707
  if (i < fip->nfnfields)
4708
    flp = &fip->fnfieldlists[i];
4709
  else
4710
    {
4711
      if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
4712
        {
4713
          fip->fnfieldlists = (struct fnfieldlist *)
4714
            xrealloc (fip->fnfieldlists,
4715
                      (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
4716
                      * sizeof (struct fnfieldlist));
4717
          if (fip->nfnfields == 0)
4718
            make_cleanup (free_current_contents, &fip->fnfieldlists);
4719
        }
4720
      flp = &fip->fnfieldlists[fip->nfnfields];
4721
      flp->name = fieldname;
4722
      flp->length = 0;
4723
      flp->head = NULL;
4724
      fip->nfnfields++;
4725
    }
4726
 
4727
  /* Create a new member function field and chain it to the field list
4728
     entry. */
4729
  new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
4730
  make_cleanup (xfree, new_fnfield);
4731
  memset (new_fnfield, 0, sizeof (struct nextfnfield));
4732
  new_fnfield->next = flp->head;
4733
  flp->head = new_fnfield;
4734
  flp->length++;
4735
 
4736
  /* Fill in the member function field info.  */
4737
  fnp = &new_fnfield->fnfield;
4738
  /* The name is already allocated along with this objfile, so we don't
4739
     need to duplicate it for the type.  */
4740
  fnp->physname = physname ? physname : "";
4741
  fnp->type = alloc_type (objfile);
4742
  this_type = read_type_die (die, cu);
4743
  if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
4744
    {
4745
      int nparams = TYPE_NFIELDS (this_type);
4746
 
4747
      /* TYPE is the domain of this method, and THIS_TYPE is the type
4748
           of the method itself (TYPE_CODE_METHOD).  */
4749
      smash_to_method_type (fnp->type, type,
4750
                            TYPE_TARGET_TYPE (this_type),
4751
                            TYPE_FIELDS (this_type),
4752
                            TYPE_NFIELDS (this_type),
4753
                            TYPE_VARARGS (this_type));
4754
 
4755
      /* Handle static member functions.
4756
         Dwarf2 has no clean way to discern C++ static and non-static
4757
         member functions. G++ helps GDB by marking the first
4758
         parameter for non-static member functions (which is the
4759
         this pointer) as artificial. We obtain this information
4760
         from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
4761
      if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
4762
        fnp->voffset = VOFFSET_STATIC;
4763
    }
4764
  else
4765
    complaint (&symfile_complaints, _("member function type missing for '%s'"),
4766
               physname);
4767
 
4768
  /* Get fcontext from DW_AT_containing_type if present.  */
4769
  if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
4770
    fnp->fcontext = die_containing_type (die, cu);
4771
 
4772
  /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
4773
     and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
4774
 
4775
  /* Get accessibility.  */
4776
  attr = dwarf2_attr (die, DW_AT_accessibility, cu);
4777
  if (attr)
4778
    {
4779
      switch (DW_UNSND (attr))
4780
        {
4781
        case DW_ACCESS_private:
4782
          fnp->is_private = 1;
4783
          break;
4784
        case DW_ACCESS_protected:
4785
          fnp->is_protected = 1;
4786
          break;
4787
        }
4788
    }
4789
 
4790
  /* Check for artificial methods.  */
4791
  attr = dwarf2_attr (die, DW_AT_artificial, cu);
4792
  if (attr && DW_UNSND (attr) != 0)
4793
    fnp->is_artificial = 1;
4794
 
4795
  /* Get index in virtual function table if it is a virtual member
4796
     function.  For GCC, this is an offset in the appropriate
4797
     virtual table, as specified by DW_AT_containing_type.  For
4798
     everyone else, it is an expression to be evaluated relative
4799
     to the object address.  */
4800
 
4801
  attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
4802
  if (attr && fnp->fcontext)
4803
    {
4804
      /* Support the .debug_loc offsets */
4805
      if (attr_form_is_block (attr))
4806
        {
4807
          fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
4808
        }
4809
      else if (attr_form_is_section_offset (attr))
4810
        {
4811
          dwarf2_complex_location_expr_complaint ();
4812
        }
4813
      else
4814
        {
4815
          dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
4816
                                                 fieldname);
4817
        }
4818
    }
4819
  else if (attr)
4820
    {
4821
      /* We only support trivial expressions here.  This hack will work
4822
         for v3 classes, which always start with the vtable pointer.  */
4823
      if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0
4824
          && DW_BLOCK (attr)->data[0] == DW_OP_deref)
4825
        {
4826
          struct dwarf_block blk;
4827
          blk.size = DW_BLOCK (attr)->size - 1;
4828
          blk.data = DW_BLOCK (attr)->data + 1;
4829
          fnp->voffset = decode_locdesc (&blk, cu);
4830
          if ((fnp->voffset % cu->header.addr_size) != 0)
4831
            dwarf2_complex_location_expr_complaint ();
4832
          else
4833
            fnp->voffset /= cu->header.addr_size;
4834
          fnp->voffset += 2;
4835
          fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
4836
        }
4837
      else
4838
        dwarf2_complex_location_expr_complaint ();
4839
    }
4840
  else
4841
    {
4842
      attr = dwarf2_attr (die, DW_AT_virtuality, cu);
4843
      if (attr && DW_UNSND (attr))
4844
        {
4845
          /* GCC does this, as of 2008-08-25; PR debug/37237.  */
4846
          complaint (&symfile_complaints,
4847
                     _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
4848
                     fieldname, die->offset);
4849
          TYPE_CPLUS_DYNAMIC (type) = 1;
4850
        }
4851
    }
4852
}
4853
 
4854
/* Create the vector of member function fields, and attach it to the type.  */
4855
 
4856
static void
4857
dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
4858
                                 struct dwarf2_cu *cu)
4859
{
4860
  struct fnfieldlist *flp;
4861
  int total_length = 0;
4862
  int i;
4863
 
4864
  if (cu->language == language_ada)
4865
    error ("unexpected member functions in Ada type");
4866
 
4867
  ALLOCATE_CPLUS_STRUCT_TYPE (type);
4868
  TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
4869
    TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
4870
 
4871
  for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
4872
    {
4873
      struct nextfnfield *nfp = flp->head;
4874
      struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
4875
      int k;
4876
 
4877
      TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
4878
      TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
4879
      fn_flp->fn_fields = (struct fn_field *)
4880
        TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
4881
      for (k = flp->length; (k--, nfp); nfp = nfp->next)
4882
        fn_flp->fn_fields[k] = nfp->fnfield;
4883
 
4884
      total_length += flp->length;
4885
    }
4886
 
4887
  TYPE_NFN_FIELDS (type) = fip->nfnfields;
4888
  TYPE_NFN_FIELDS_TOTAL (type) = total_length;
4889
}
4890
 
4891
/* Returns non-zero if NAME is the name of a vtable member in CU's
4892
   language, zero otherwise.  */
4893
static int
4894
is_vtable_name (const char *name, struct dwarf2_cu *cu)
4895
{
4896
  static const char vptr[] = "_vptr";
4897
  static const char vtable[] = "vtable";
4898
 
4899
  /* Look for the C++ and Java forms of the vtable.  */
4900
  if ((cu->language == language_java
4901
       && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
4902
       || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
4903
       && is_cplus_marker (name[sizeof (vptr) - 1])))
4904
    return 1;
4905
 
4906
  return 0;
4907
}
4908
 
4909
/* GCC outputs unnamed structures that are really pointers to member
4910
   functions, with the ABI-specified layout.  If TYPE describes
4911
   such a structure, smash it into a member function type.
4912
 
4913
   GCC shouldn't do this; it should just output pointer to member DIEs.
4914
   This is GCC PR debug/28767.  */
4915
 
4916
static void
4917
quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
4918
{
4919
  struct type *pfn_type, *domain_type, *new_type;
4920
 
4921
  /* Check for a structure with no name and two children.  */
4922
  if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
4923
    return;
4924
 
4925
  /* Check for __pfn and __delta members.  */
4926
  if (TYPE_FIELD_NAME (type, 0) == NULL
4927
      || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
4928
      || TYPE_FIELD_NAME (type, 1) == NULL
4929
      || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
4930
    return;
4931
 
4932
  /* Find the type of the method.  */
4933
  pfn_type = TYPE_FIELD_TYPE (type, 0);
4934
  if (pfn_type == NULL
4935
      || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
4936
      || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
4937
    return;
4938
 
4939
  /* Look for the "this" argument.  */
4940
  pfn_type = TYPE_TARGET_TYPE (pfn_type);
4941
  if (TYPE_NFIELDS (pfn_type) == 0
4942
      /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
4943
      || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
4944
    return;
4945
 
4946
  domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
4947
  new_type = alloc_type (objfile);
4948
  smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
4949
                        TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
4950
                        TYPE_VARARGS (pfn_type));
4951
  smash_to_methodptr_type (type, new_type);
4952
}
4953
 
4954
/* Called when we find the DIE that starts a structure or union scope
4955
   (definition) to process all dies that define the members of the
4956
   structure or union.
4957
 
4958
   NOTE: we need to call struct_type regardless of whether or not the
4959
   DIE has an at_name attribute, since it might be an anonymous
4960
   structure or union.  This gets the type entered into our set of
4961
   user defined types.
4962
 
4963
   However, if the structure is incomplete (an opaque struct/union)
4964
   then suppress creating a symbol table entry for it since gdb only
4965
   wants to find the one with the complete definition.  Note that if
4966
   it is complete, we just call new_symbol, which does it's own
4967
   checking about whether the struct/union is anonymous or not (and
4968
   suppresses creating a symbol table entry itself).  */
4969
 
4970
static struct type *
4971
read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
4972
{
4973
  struct objfile *objfile = cu->objfile;
4974
  struct type *type;
4975
  struct attribute *attr;
4976
  char *name;
4977
  struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4978
 
4979
  /* If the definition of this type lives in .debug_types, read that type.
4980
     Don't follow DW_AT_specification though, that will take us back up
4981
     the chain and we want to go down.  */
4982
  attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
4983
  if (attr)
4984
    {
4985
      struct dwarf2_cu *type_cu = cu;
4986
      struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
4987
      /* We could just recurse on read_structure_type, but we need to call
4988
         get_die_type to ensure only one type for this DIE is created.
4989
         This is important, for example, because for c++ classes we need
4990
         TYPE_NAME set which is only done by new_symbol.  Blech.  */
4991
      type = read_type_die (type_die, type_cu);
4992
      return set_die_type (die, type, cu);
4993
    }
4994
 
4995
  type = alloc_type (objfile);
4996
  INIT_CPLUS_SPECIFIC (type);
4997
 
4998
  name = dwarf2_name (die, cu);
4999
  if (name != NULL)
5000
    {
5001
      if (cu->language == language_cplus
5002
          || cu->language == language_java)
5003
        {
5004
          const char *new_prefix = determine_class_name (die, cu);
5005
          TYPE_TAG_NAME (type) = (char *) new_prefix;
5006
        }
5007
      else
5008
        {
5009
          /* The name is already allocated along with this objfile, so
5010
             we don't need to duplicate it for the type.  */
5011
          TYPE_TAG_NAME (type) = name;
5012
        }
5013
    }
5014
 
5015
  if (die->tag == DW_TAG_structure_type)
5016
    {
5017
      TYPE_CODE (type) = TYPE_CODE_STRUCT;
5018
    }
5019
  else if (die->tag == DW_TAG_union_type)
5020
    {
5021
      TYPE_CODE (type) = TYPE_CODE_UNION;
5022
    }
5023
  else
5024
    {
5025
      TYPE_CODE (type) = TYPE_CODE_CLASS;
5026
    }
5027
 
5028
  if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
5029
    TYPE_DECLARED_CLASS (type) = 1;
5030
 
5031
  attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5032
  if (attr)
5033
    {
5034
      TYPE_LENGTH (type) = DW_UNSND (attr);
5035
    }
5036
  else
5037
    {
5038
      TYPE_LENGTH (type) = 0;
5039
    }
5040
 
5041
  TYPE_STUB_SUPPORTED (type) = 1;
5042
  if (die_is_declaration (die, cu))
5043
    TYPE_STUB (type) = 1;
5044
 
5045
  set_descriptive_type (type, die, cu);
5046
 
5047
  /* We need to add the type field to the die immediately so we don't
5048
     infinitely recurse when dealing with pointers to the structure
5049
     type within the structure itself. */
5050
  set_die_type (die, type, cu);
5051
 
5052
  if (die->child != NULL && ! die_is_declaration (die, cu))
5053
    {
5054
      struct field_info fi;
5055
      struct die_info *child_die;
5056
 
5057
      memset (&fi, 0, sizeof (struct field_info));
5058
 
5059
      child_die = die->child;
5060
 
5061
      while (child_die && child_die->tag)
5062
        {
5063
          if (child_die->tag == DW_TAG_member
5064
              || child_die->tag == DW_TAG_variable)
5065
            {
5066
              /* NOTE: carlton/2002-11-05: A C++ static data member
5067
                 should be a DW_TAG_member that is a declaration, but
5068
                 all versions of G++ as of this writing (so through at
5069
                 least 3.2.1) incorrectly generate DW_TAG_variable
5070
                 tags for them instead.  */
5071
              dwarf2_add_field (&fi, child_die, cu);
5072
            }
5073
          else if (child_die->tag == DW_TAG_subprogram)
5074
            {
5075
              /* C++ member function. */
5076
              dwarf2_add_member_fn (&fi, child_die, type, cu);
5077
            }
5078
          else if (child_die->tag == DW_TAG_inheritance)
5079
            {
5080
              /* C++ base class field.  */
5081
              dwarf2_add_field (&fi, child_die, cu);
5082
            }
5083
          child_die = sibling_die (child_die);
5084
        }
5085
 
5086
      /* Attach fields and member functions to the type.  */
5087
      if (fi.nfields)
5088
        dwarf2_attach_fields_to_type (&fi, type, cu);
5089
      if (fi.nfnfields)
5090
        {
5091
          dwarf2_attach_fn_fields_to_type (&fi, type, cu);
5092
 
5093
          /* Get the type which refers to the base class (possibly this
5094
             class itself) which contains the vtable pointer for the current
5095
             class from the DW_AT_containing_type attribute.  This use of
5096
             DW_AT_containing_type is a GNU extension.  */
5097
 
5098
          if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
5099
            {
5100
              struct type *t = die_containing_type (die, cu);
5101
 
5102
              TYPE_VPTR_BASETYPE (type) = t;
5103
              if (type == t)
5104
                {
5105
                  int i;
5106
 
5107
                  /* Our own class provides vtbl ptr.  */
5108
                  for (i = TYPE_NFIELDS (t) - 1;
5109
                       i >= TYPE_N_BASECLASSES (t);
5110
                       --i)
5111
                    {
5112
                      char *fieldname = TYPE_FIELD_NAME (t, i);
5113
 
5114
                      if (is_vtable_name (fieldname, cu))
5115
                        {
5116
                          TYPE_VPTR_FIELDNO (type) = i;
5117
                          break;
5118
                        }
5119
                    }
5120
 
5121
                  /* Complain if virtual function table field not found.  */
5122
                  if (i < TYPE_N_BASECLASSES (t))
5123
                    complaint (&symfile_complaints,
5124
                               _("virtual function table pointer not found when defining class '%s'"),
5125
                               TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
5126
                               "");
5127
                }
5128
              else
5129
                {
5130
                  TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
5131
                }
5132
            }
5133
          else if (cu->producer
5134
                   && strncmp (cu->producer,
5135
                               "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
5136
            {
5137
              /* The IBM XLC compiler does not provide direct indication
5138
                 of the containing type, but the vtable pointer is
5139
                 always named __vfp.  */
5140
 
5141
              int i;
5142
 
5143
              for (i = TYPE_NFIELDS (type) - 1;
5144
                   i >= TYPE_N_BASECLASSES (type);
5145
                   --i)
5146
                {
5147
                  if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
5148
                    {
5149
                      TYPE_VPTR_FIELDNO (type) = i;
5150
                      TYPE_VPTR_BASETYPE (type) = type;
5151
                      break;
5152
                    }
5153
                }
5154
            }
5155
        }
5156
    }
5157
 
5158
  quirk_gcc_member_function_pointer (type, cu->objfile);
5159
 
5160
  do_cleanups (back_to);
5161
  return type;
5162
}
5163
 
5164
static void
5165
process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
5166
{
5167
  struct objfile *objfile = cu->objfile;
5168
  struct die_info *child_die = die->child;
5169
  struct type *this_type;
5170
 
5171
  this_type = get_die_type (die, cu);
5172
  if (this_type == NULL)
5173
    this_type = read_structure_type (die, cu);
5174
 
5175
  /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
5176
     snapshots) has been known to create a die giving a declaration
5177
     for a class that has, as a child, a die giving a definition for a
5178
     nested class.  So we have to process our children even if the
5179
     current die is a declaration.  Normally, of course, a declaration
5180
     won't have any children at all.  */
5181
 
5182
  while (child_die != NULL && child_die->tag)
5183
    {
5184
      if (child_die->tag == DW_TAG_member
5185
          || child_die->tag == DW_TAG_variable
5186
          || child_die->tag == DW_TAG_inheritance)
5187
        {
5188
          /* Do nothing.  */
5189
        }
5190
      else
5191
        process_die (child_die, cu);
5192
 
5193
      child_die = sibling_die (child_die);
5194
    }
5195
 
5196
  /* Do not consider external references.  According to the DWARF standard,
5197
     these DIEs are identified by the fact that they have no byte_size
5198
     attribute, and a declaration attribute.  */
5199
  if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
5200
      || !die_is_declaration (die, cu))
5201
    new_symbol (die, this_type, cu);
5202
}
5203
 
5204
/* Given a DW_AT_enumeration_type die, set its type.  We do not
5205
   complete the type's fields yet, or create any symbols.  */
5206
 
5207
static struct type *
5208
read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
5209
{
5210
  struct objfile *objfile = cu->objfile;
5211
  struct type *type;
5212
  struct attribute *attr;
5213
  const char *name;
5214
 
5215
  /* If the definition of this type lives in .debug_types, read that type.
5216
     Don't follow DW_AT_specification though, that will take us back up
5217
     the chain and we want to go down.  */
5218
  attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
5219
  if (attr)
5220
    {
5221
      struct dwarf2_cu *type_cu = cu;
5222
      struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
5223
      type = read_type_die (type_die, type_cu);
5224
      return set_die_type (die, type, cu);
5225
    }
5226
 
5227
  type = alloc_type (objfile);
5228
 
5229
  TYPE_CODE (type) = TYPE_CODE_ENUM;
5230
  name = dwarf2_full_name (die, cu);
5231
  if (name != NULL)
5232
    TYPE_TAG_NAME (type) = (char *) name;
5233
 
5234
  attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5235
  if (attr)
5236
    {
5237
      TYPE_LENGTH (type) = DW_UNSND (attr);
5238
    }
5239
  else
5240
    {
5241
      TYPE_LENGTH (type) = 0;
5242
    }
5243
 
5244
  /* The enumeration DIE can be incomplete.  In Ada, any type can be
5245
     declared as private in the package spec, and then defined only
5246
     inside the package body.  Such types are known as Taft Amendment
5247
     Types.  When another package uses such a type, an incomplete DIE
5248
     may be generated by the compiler.  */
5249
  if (die_is_declaration (die, cu))
5250
    TYPE_STUB (type) = 1;
5251
 
5252
  return set_die_type (die, type, cu);
5253
}
5254
 
5255
/* Determine the name of the type represented by DIE, which should be
5256
   a named C++ or Java compound type.  Return the name in question,
5257
   allocated on the objfile obstack.  */
5258
 
5259
static const char *
5260
determine_class_name (struct die_info *die, struct dwarf2_cu *cu)
5261
{
5262
  const char *new_prefix = NULL;
5263
 
5264
  /* If we don't have namespace debug info, guess the name by trying
5265
     to demangle the names of members, just like we did in
5266
     guess_structure_name.  */
5267
  if (!processing_has_namespace_info)
5268
    {
5269
      struct die_info *child;
5270
 
5271
      for (child = die->child;
5272
           child != NULL && child->tag != 0;
5273
           child = sibling_die (child))
5274
        {
5275
          if (child->tag == DW_TAG_subprogram)
5276
            {
5277
              char *phys_prefix
5278
                = language_class_name_from_physname (cu->language_defn,
5279
                                                     dwarf2_linkage_name
5280
                                                     (child, cu));
5281
 
5282
              if (phys_prefix != NULL)
5283
                {
5284
                  new_prefix
5285
                    = obsavestring (phys_prefix, strlen (phys_prefix),
5286
                                    &cu->objfile->objfile_obstack);
5287
                  xfree (phys_prefix);
5288
                  break;
5289
                }
5290
            }
5291
        }
5292
    }
5293
 
5294
  if (new_prefix == NULL)
5295
    new_prefix = dwarf2_full_name (die, cu);
5296
 
5297
  return new_prefix;
5298
}
5299
 
5300
/* Given a pointer to a die which begins an enumeration, process all
5301
   the dies that define the members of the enumeration, and create the
5302
   symbol for the enumeration type.
5303
 
5304
   NOTE: We reverse the order of the element list.  */
5305
 
5306
static void
5307
process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
5308
{
5309
  struct objfile *objfile = cu->objfile;
5310
  struct die_info *child_die;
5311
  struct field *fields;
5312
  struct symbol *sym;
5313
  int num_fields;
5314
  int unsigned_enum = 1;
5315
  char *name;
5316
  struct type *this_type;
5317
 
5318
  num_fields = 0;
5319
  fields = NULL;
5320
  this_type = get_die_type (die, cu);
5321
  if (this_type == NULL)
5322
    this_type = read_enumeration_type (die, cu);
5323
  if (die->child != NULL)
5324
    {
5325
      child_die = die->child;
5326
      while (child_die && child_die->tag)
5327
        {
5328
          if (child_die->tag != DW_TAG_enumerator)
5329
            {
5330
              process_die (child_die, cu);
5331
            }
5332
          else
5333
            {
5334
              name = dwarf2_name (child_die, cu);
5335
              if (name)
5336
                {
5337
                  sym = new_symbol (child_die, this_type, cu);
5338
                  if (SYMBOL_VALUE (sym) < 0)
5339
                    unsigned_enum = 0;
5340
 
5341
                  if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
5342
                    {
5343
                      fields = (struct field *)
5344
                        xrealloc (fields,
5345
                                  (num_fields + DW_FIELD_ALLOC_CHUNK)
5346
                                  * sizeof (struct field));
5347
                    }
5348
 
5349
                  FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
5350
                  FIELD_TYPE (fields[num_fields]) = NULL;
5351
                  SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
5352
                  FIELD_BITSIZE (fields[num_fields]) = 0;
5353
 
5354
                  num_fields++;
5355
                }
5356
            }
5357
 
5358
          child_die = sibling_die (child_die);
5359
        }
5360
 
5361
      if (num_fields)
5362
        {
5363
          TYPE_NFIELDS (this_type) = num_fields;
5364
          TYPE_FIELDS (this_type) = (struct field *)
5365
            TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
5366
          memcpy (TYPE_FIELDS (this_type), fields,
5367
                  sizeof (struct field) * num_fields);
5368
          xfree (fields);
5369
        }
5370
      if (unsigned_enum)
5371
        TYPE_UNSIGNED (this_type) = 1;
5372
    }
5373
 
5374
  new_symbol (die, this_type, cu);
5375
}
5376
 
5377
/* Extract all information from a DW_TAG_array_type DIE and put it in
5378
   the DIE's type field.  For now, this only handles one dimensional
5379
   arrays.  */
5380
 
5381
static struct type *
5382
read_array_type (struct die_info *die, struct dwarf2_cu *cu)
5383
{
5384
  struct objfile *objfile = cu->objfile;
5385
  struct die_info *child_die;
5386
  struct type *type = NULL;
5387
  struct type *element_type, *range_type, *index_type;
5388
  struct type **range_types = NULL;
5389
  struct attribute *attr;
5390
  int ndim = 0;
5391
  struct cleanup *back_to;
5392
  char *name;
5393
 
5394
  element_type = die_type (die, cu);
5395
 
5396
  /* Irix 6.2 native cc creates array types without children for
5397
     arrays with unspecified length.  */
5398
  if (die->child == NULL)
5399
    {
5400
      index_type = objfile_type (objfile)->builtin_int;
5401
      range_type = create_range_type (NULL, index_type, 0, -1);
5402
      type = create_array_type (NULL, element_type, range_type);
5403
      return set_die_type (die, type, cu);
5404
    }
5405
 
5406
  back_to = make_cleanup (null_cleanup, NULL);
5407
  child_die = die->child;
5408
  while (child_die && child_die->tag)
5409
    {
5410
      if (child_die->tag == DW_TAG_subrange_type)
5411
        {
5412
          struct type *child_type = read_type_die (child_die, cu);
5413
          if (child_type != NULL)
5414
            {
5415
              /* The range type was succesfully read. Save it for
5416
                 the array type creation.  */
5417
              if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
5418
                {
5419
                  range_types = (struct type **)
5420
                    xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
5421
                              * sizeof (struct type *));
5422
                  if (ndim == 0)
5423
                    make_cleanup (free_current_contents, &range_types);
5424
                }
5425
              range_types[ndim++] = child_type;
5426
            }
5427
        }
5428
      child_die = sibling_die (child_die);
5429
    }
5430
 
5431
  /* Dwarf2 dimensions are output from left to right, create the
5432
     necessary array types in backwards order.  */
5433
 
5434
  type = element_type;
5435
 
5436
  if (read_array_order (die, cu) == DW_ORD_col_major)
5437
    {
5438
      int i = 0;
5439
      while (i < ndim)
5440
        type = create_array_type (NULL, type, range_types[i++]);
5441
    }
5442
  else
5443
    {
5444
      while (ndim-- > 0)
5445
        type = create_array_type (NULL, type, range_types[ndim]);
5446
    }
5447
 
5448
  /* Understand Dwarf2 support for vector types (like they occur on
5449
     the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
5450
     array type.  This is not part of the Dwarf2/3 standard yet, but a
5451
     custom vendor extension.  The main difference between a regular
5452
     array and the vector variant is that vectors are passed by value
5453
     to functions.  */
5454
  attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
5455
  if (attr)
5456
    make_vector_type (type);
5457
 
5458
  name = dwarf2_name (die, cu);
5459
  if (name)
5460
    TYPE_NAME (type) = name;
5461
 
5462
  set_descriptive_type (type, die, cu);
5463
 
5464
  do_cleanups (back_to);
5465
 
5466
  /* Install the type in the die. */
5467
  return set_die_type (die, type, cu);
5468
}
5469
 
5470
static enum dwarf_array_dim_ordering
5471
read_array_order (struct die_info *die, struct dwarf2_cu *cu)
5472
{
5473
  struct attribute *attr;
5474
 
5475
  attr = dwarf2_attr (die, DW_AT_ordering, cu);
5476
 
5477
  if (attr) return DW_SND (attr);
5478
 
5479
  /*
5480
    GNU F77 is a special case, as at 08/2004 array type info is the
5481
    opposite order to the dwarf2 specification, but data is still
5482
    laid out as per normal fortran.
5483
 
5484
    FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
5485
    version checking.
5486
  */
5487
 
5488
  if (cu->language == language_fortran
5489
      && cu->producer && strstr (cu->producer, "GNU F77"))
5490
    {
5491
      return DW_ORD_row_major;
5492
    }
5493
 
5494
  switch (cu->language_defn->la_array_ordering)
5495
    {
5496
    case array_column_major:
5497
      return DW_ORD_col_major;
5498
    case array_row_major:
5499
    default:
5500
      return DW_ORD_row_major;
5501
    };
5502
}
5503
 
5504
/* Extract all information from a DW_TAG_set_type DIE and put it in
5505
   the DIE's type field. */
5506
 
5507
static struct type *
5508
read_set_type (struct die_info *die, struct dwarf2_cu *cu)
5509
{
5510
  struct type *set_type = create_set_type (NULL, die_type (die, cu));
5511
 
5512
  return set_die_type (die, set_type, cu);
5513
}
5514
 
5515
/* First cut: install each common block member as a global variable.  */
5516
 
5517
static void
5518
read_common_block (struct die_info *die, struct dwarf2_cu *cu)
5519
{
5520
  struct die_info *child_die;
5521
  struct attribute *attr;
5522
  struct symbol *sym;
5523
  CORE_ADDR base = (CORE_ADDR) 0;
5524
 
5525
  attr = dwarf2_attr (die, DW_AT_location, cu);
5526
  if (attr)
5527
    {
5528
      /* Support the .debug_loc offsets */
5529
      if (attr_form_is_block (attr))
5530
        {
5531
          base = decode_locdesc (DW_BLOCK (attr), cu);
5532
        }
5533
      else if (attr_form_is_section_offset (attr))
5534
        {
5535
          dwarf2_complex_location_expr_complaint ();
5536
        }
5537
      else
5538
        {
5539
          dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
5540
                                                 "common block member");
5541
        }
5542
    }
5543
  if (die->child != NULL)
5544
    {
5545
      child_die = die->child;
5546
      while (child_die && child_die->tag)
5547
        {
5548
          sym = new_symbol (child_die, NULL, cu);
5549
          attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
5550
          if (attr)
5551
            {
5552
              CORE_ADDR byte_offset = 0;
5553
 
5554
              if (attr_form_is_section_offset (attr))
5555
                dwarf2_complex_location_expr_complaint ();
5556
              else if (attr_form_is_constant (attr))
5557
                byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5558
              else if (attr_form_is_block (attr))
5559
                byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5560
              else
5561
                dwarf2_complex_location_expr_complaint ();
5562
 
5563
              SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
5564
              add_symbol_to_list (sym, &global_symbols);
5565
            }
5566
          child_die = sibling_die (child_die);
5567
        }
5568
    }
5569
}
5570
 
5571
/* Create a type for a C++ namespace.  */
5572
 
5573
static struct type *
5574
read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
5575
{
5576
  struct objfile *objfile = cu->objfile;
5577
  const char *previous_prefix, *name;
5578
  int is_anonymous;
5579
  struct type *type;
5580
 
5581
  /* For extensions, reuse the type of the original namespace.  */
5582
  if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
5583
    {
5584
      struct die_info *ext_die;
5585
      struct dwarf2_cu *ext_cu = cu;
5586
      ext_die = dwarf2_extension (die, &ext_cu);
5587
      type = read_type_die (ext_die, ext_cu);
5588
      return set_die_type (die, type, cu);
5589
    }
5590
 
5591
  name = namespace_name (die, &is_anonymous, cu);
5592
 
5593
  /* Now build the name of the current namespace.  */
5594
 
5595
  previous_prefix = determine_prefix (die, cu);
5596
  if (previous_prefix[0] != '\0')
5597
    name = typename_concat (&objfile->objfile_obstack,
5598
                            previous_prefix, name, cu);
5599
 
5600
  /* Create the type.  */
5601
  type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
5602
                    objfile);
5603
  TYPE_NAME (type) = (char *) name;
5604
  TYPE_TAG_NAME (type) = TYPE_NAME (type);
5605
 
5606
  set_die_type (die, type, cu);
5607
 
5608
  return type;
5609
}
5610
 
5611
/* Read a C++ namespace.  */
5612
 
5613
static void
5614
read_namespace (struct die_info *die, struct dwarf2_cu *cu)
5615
{
5616
  struct objfile *objfile = cu->objfile;
5617
  const char *name;
5618
  int is_anonymous;
5619
 
5620
  /* Add a symbol associated to this if we haven't seen the namespace
5621
     before.  Also, add a using directive if it's an anonymous
5622
     namespace.  */
5623
 
5624
  if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5625
    {
5626
      struct type *type;
5627
 
5628
      type = read_type_die (die, cu);
5629
      new_symbol (die, type, cu);
5630
 
5631
      name = namespace_name (die, &is_anonymous, cu);
5632
      if (is_anonymous)
5633
        {
5634
          const char *previous_prefix = determine_prefix (die, cu);
5635
          cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL);
5636
        }
5637
    }
5638
 
5639
  if (die->child != NULL)
5640
    {
5641
      struct die_info *child_die = die->child;
5642
 
5643
      while (child_die && child_die->tag)
5644
        {
5645
          process_die (child_die, cu);
5646
          child_die = sibling_die (child_die);
5647
        }
5648
    }
5649
}
5650
 
5651
/* Read a Fortran module.  */
5652
 
5653
static void
5654
read_module (struct die_info *die, struct dwarf2_cu *cu)
5655
{
5656
  struct die_info *child_die = die->child;
5657
 
5658
  /* FIXME: Support the separate Fortran module namespaces.  */
5659
 
5660
  while (child_die && child_die->tag)
5661
    {
5662
      process_die (child_die, cu);
5663
      child_die = sibling_die (child_die);
5664
    }
5665
}
5666
 
5667
/* Return the name of the namespace represented by DIE.  Set
5668
   *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
5669
   namespace.  */
5670
 
5671
static const char *
5672
namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
5673
{
5674
  struct die_info *current_die;
5675
  const char *name = NULL;
5676
 
5677
  /* Loop through the extensions until we find a name.  */
5678
 
5679
  for (current_die = die;
5680
       current_die != NULL;
5681
       current_die = dwarf2_extension (die, &cu))
5682
    {
5683
      name = dwarf2_name (current_die, cu);
5684
      if (name != NULL)
5685
        break;
5686
    }
5687
 
5688
  /* Is it an anonymous namespace?  */
5689
 
5690
  *is_anonymous = (name == NULL);
5691
  if (*is_anonymous)
5692
    name = "(anonymous namespace)";
5693
 
5694
  return name;
5695
}
5696
 
5697
/* Extract all information from a DW_TAG_pointer_type DIE and add to
5698
   the user defined type vector.  */
5699
 
5700
static struct type *
5701
read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
5702
{
5703
  struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
5704
  struct comp_unit_head *cu_header = &cu->header;
5705
  struct type *type;
5706
  struct attribute *attr_byte_size;
5707
  struct attribute *attr_address_class;
5708
  int byte_size, addr_class;
5709
 
5710
  type = lookup_pointer_type (die_type (die, cu));
5711
 
5712
  attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
5713
  if (attr_byte_size)
5714
    byte_size = DW_UNSND (attr_byte_size);
5715
  else
5716
    byte_size = cu_header->addr_size;
5717
 
5718
  attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
5719
  if (attr_address_class)
5720
    addr_class = DW_UNSND (attr_address_class);
5721
  else
5722
    addr_class = DW_ADDR_none;
5723
 
5724
  /* If the pointer size or address class is different than the
5725
     default, create a type variant marked as such and set the
5726
     length accordingly.  */
5727
  if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
5728
    {
5729
      if (gdbarch_address_class_type_flags_p (gdbarch))
5730
        {
5731
          int type_flags;
5732
 
5733
          type_flags = gdbarch_address_class_type_flags
5734
                         (gdbarch, byte_size, addr_class);
5735
          gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
5736
                      == 0);
5737
          type = make_type_with_address_space (type, type_flags);
5738
        }
5739
      else if (TYPE_LENGTH (type) != byte_size)
5740
        {
5741
          complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
5742
        }
5743
      else {
5744
        /* Should we also complain about unhandled address classes?  */
5745
      }
5746
    }
5747
 
5748
  TYPE_LENGTH (type) = byte_size;
5749
  return set_die_type (die, type, cu);
5750
}
5751
 
5752
/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
5753
   the user defined type vector.  */
5754
 
5755
static struct type *
5756
read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
5757
{
5758
  struct objfile *objfile = cu->objfile;
5759
  struct type *type;
5760
  struct type *to_type;
5761
  struct type *domain;
5762
 
5763
  to_type = die_type (die, cu);
5764
  domain = die_containing_type (die, cu);
5765
 
5766
  if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
5767
    type = lookup_methodptr_type (to_type);
5768
  else
5769
    type = lookup_memberptr_type (to_type, domain);
5770
 
5771
  return set_die_type (die, type, cu);
5772
}
5773
 
5774
/* Extract all information from a DW_TAG_reference_type DIE and add to
5775
   the user defined type vector.  */
5776
 
5777
static struct type *
5778
read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
5779
{
5780
  struct comp_unit_head *cu_header = &cu->header;
5781
  struct type *type;
5782
  struct attribute *attr;
5783
 
5784
  type = lookup_reference_type (die_type (die, cu));
5785
  attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5786
  if (attr)
5787
    {
5788
      TYPE_LENGTH (type) = DW_UNSND (attr);
5789
    }
5790
  else
5791
    {
5792
      TYPE_LENGTH (type) = cu_header->addr_size;
5793
    }
5794
  return set_die_type (die, type, cu);
5795
}
5796
 
5797
static struct type *
5798
read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
5799
{
5800
  struct type *base_type, *cv_type;
5801
 
5802
  base_type = die_type (die, cu);
5803
  cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
5804
  return set_die_type (die, cv_type, cu);
5805
}
5806
 
5807
static struct type *
5808
read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
5809
{
5810
  struct type *base_type, *cv_type;
5811
 
5812
  base_type = die_type (die, cu);
5813
  cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
5814
  return set_die_type (die, cv_type, cu);
5815
}
5816
 
5817
/* Extract all information from a DW_TAG_string_type DIE and add to
5818
   the user defined type vector.  It isn't really a user defined type,
5819
   but it behaves like one, with other DIE's using an AT_user_def_type
5820
   attribute to reference it.  */
5821
 
5822
static struct type *
5823
read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
5824
{
5825
  struct objfile *objfile = cu->objfile;
5826
  struct gdbarch *gdbarch = get_objfile_arch (objfile);
5827
  struct type *type, *range_type, *index_type, *char_type;
5828
  struct attribute *attr;
5829
  unsigned int length;
5830
 
5831
  attr = dwarf2_attr (die, DW_AT_string_length, cu);
5832
  if (attr)
5833
    {
5834
      length = DW_UNSND (attr);
5835
    }
5836
  else
5837
    {
5838
      /* check for the DW_AT_byte_size attribute */
5839
      attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5840
      if (attr)
5841
        {
5842
          length = DW_UNSND (attr);
5843
        }
5844
      else
5845
        {
5846
          length = 1;
5847
        }
5848
    }
5849
 
5850
  index_type = objfile_type (objfile)->builtin_int;
5851
  range_type = create_range_type (NULL, index_type, 1, length);
5852
  char_type = language_string_char_type (cu->language_defn, gdbarch);
5853
  type = create_string_type (NULL, char_type, range_type);
5854
 
5855
  return set_die_type (die, type, cu);
5856
}
5857
 
5858
/* Handle DIES due to C code like:
5859
 
5860
   struct foo
5861
   {
5862
   int (*funcp)(int a, long l);
5863
   int b;
5864
   };
5865
 
5866
   ('funcp' generates a DW_TAG_subroutine_type DIE)
5867
 */
5868
 
5869
static struct type *
5870
read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
5871
{
5872
  struct type *type;            /* Type that this function returns */
5873
  struct type *ftype;           /* Function that returns above type */
5874
  struct attribute *attr;
5875
 
5876
  type = die_type (die, cu);
5877
  ftype = lookup_function_type (type);
5878
 
5879
  /* All functions in C++, Pascal and Java have prototypes.  */
5880
  attr = dwarf2_attr (die, DW_AT_prototyped, cu);
5881
  if ((attr && (DW_UNSND (attr) != 0))
5882
      || cu->language == language_cplus
5883
      || cu->language == language_java
5884
      || cu->language == language_pascal)
5885
    TYPE_PROTOTYPED (ftype) = 1;
5886
 
5887
  /* Store the calling convention in the type if it's available in
5888
     the subroutine die.  Otherwise set the calling convention to
5889
     the default value DW_CC_normal.  */
5890
  attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
5891
  TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
5892
 
5893
  /* We need to add the subroutine type to the die immediately so
5894
     we don't infinitely recurse when dealing with parameters
5895
     declared as the same subroutine type. */
5896
  set_die_type (die, ftype, cu);
5897
 
5898
  if (die->child != NULL)
5899
    {
5900
      struct die_info *child_die;
5901
      int nparams = 0;
5902
      int iparams = 0;
5903
 
5904
      /* Count the number of parameters.
5905
         FIXME: GDB currently ignores vararg functions, but knows about
5906
         vararg member functions.  */
5907
      child_die = die->child;
5908
      while (child_die && child_die->tag)
5909
        {
5910
          if (child_die->tag == DW_TAG_formal_parameter)
5911
            nparams++;
5912
          else if (child_die->tag == DW_TAG_unspecified_parameters)
5913
            TYPE_VARARGS (ftype) = 1;
5914
          child_die = sibling_die (child_die);
5915
        }
5916
 
5917
      /* Allocate storage for parameters and fill them in.  */
5918
      TYPE_NFIELDS (ftype) = nparams;
5919
      TYPE_FIELDS (ftype) = (struct field *)
5920
        TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
5921
 
5922
      child_die = die->child;
5923
      while (child_die && child_die->tag)
5924
        {
5925
          if (child_die->tag == DW_TAG_formal_parameter)
5926
            {
5927
              /* Dwarf2 has no clean way to discern C++ static and non-static
5928
                 member functions. G++ helps GDB by marking the first
5929
                 parameter for non-static member functions (which is the
5930
                 this pointer) as artificial. We pass this information
5931
                 to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.  */
5932
              attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
5933
              if (attr)
5934
                TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
5935
              else
5936
                TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
5937
              TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
5938
              iparams++;
5939
            }
5940
          child_die = sibling_die (child_die);
5941
        }
5942
    }
5943
 
5944
  return ftype;
5945
}
5946
 
5947
static struct type *
5948
read_typedef (struct die_info *die, struct dwarf2_cu *cu)
5949
{
5950
  struct objfile *objfile = cu->objfile;
5951
  struct attribute *attr;
5952
  const char *name = NULL;
5953
  struct type *this_type;
5954
 
5955
  name = dwarf2_full_name (die, cu);
5956
  this_type = init_type (TYPE_CODE_TYPEDEF, 0,
5957
                         TYPE_FLAG_TARGET_STUB, NULL, objfile);
5958
  TYPE_NAME (this_type) = (char *) name;
5959
  set_die_type (die, this_type, cu);
5960
  TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
5961
  return this_type;
5962
}
5963
 
5964
/* Find a representation of a given base type and install
5965
   it in the TYPE field of the die.  */
5966
 
5967
static struct type *
5968
read_base_type (struct die_info *die, struct dwarf2_cu *cu)
5969
{
5970
  struct objfile *objfile = cu->objfile;
5971
  struct type *type;
5972
  struct attribute *attr;
5973
  int encoding = 0, size = 0;
5974
  char *name;
5975
  enum type_code code = TYPE_CODE_INT;
5976
  int type_flags = 0;
5977
  struct type *target_type = NULL;
5978
 
5979
  attr = dwarf2_attr (die, DW_AT_encoding, cu);
5980
  if (attr)
5981
    {
5982
      encoding = DW_UNSND (attr);
5983
    }
5984
  attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5985
  if (attr)
5986
    {
5987
      size = DW_UNSND (attr);
5988
    }
5989
  name = dwarf2_name (die, cu);
5990
  if (!name)
5991
    {
5992
      complaint (&symfile_complaints,
5993
                 _("DW_AT_name missing from DW_TAG_base_type"));
5994
    }
5995
 
5996
  switch (encoding)
5997
    {
5998
      case DW_ATE_address:
5999
        /* Turn DW_ATE_address into a void * pointer.  */
6000
        code = TYPE_CODE_PTR;
6001
        type_flags |= TYPE_FLAG_UNSIGNED;
6002
        target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
6003
        break;
6004
      case DW_ATE_boolean:
6005
        code = TYPE_CODE_BOOL;
6006
        type_flags |= TYPE_FLAG_UNSIGNED;
6007
        break;
6008
      case DW_ATE_complex_float:
6009
        code = TYPE_CODE_COMPLEX;
6010
        target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
6011
        break;
6012
      case DW_ATE_decimal_float:
6013
        code = TYPE_CODE_DECFLOAT;
6014
        break;
6015
      case DW_ATE_float:
6016
        code = TYPE_CODE_FLT;
6017
        break;
6018
      case DW_ATE_signed:
6019
        break;
6020
      case DW_ATE_unsigned:
6021
        type_flags |= TYPE_FLAG_UNSIGNED;
6022
        break;
6023
      case DW_ATE_signed_char:
6024
        if (cu->language == language_ada || cu->language == language_m2
6025
            || cu->language == language_pascal)
6026
          code = TYPE_CODE_CHAR;
6027
        break;
6028
      case DW_ATE_unsigned_char:
6029
        if (cu->language == language_ada || cu->language == language_m2
6030
            || cu->language == language_pascal)
6031
          code = TYPE_CODE_CHAR;
6032
        type_flags |= TYPE_FLAG_UNSIGNED;
6033
        break;
6034
      default:
6035
        complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
6036
                   dwarf_type_encoding_name (encoding));
6037
        break;
6038
    }
6039
 
6040
  type = init_type (code, size, type_flags, NULL, objfile);
6041
  TYPE_NAME (type) = name;
6042
  TYPE_TARGET_TYPE (type) = target_type;
6043
 
6044
  if (name && strcmp (name, "char") == 0)
6045
    TYPE_NOSIGN (type) = 1;
6046
 
6047
  return set_die_type (die, type, cu);
6048
}
6049
 
6050
/* Read the given DW_AT_subrange DIE.  */
6051
 
6052
static struct type *
6053
read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
6054
{
6055
  struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
6056
  struct type *base_type;
6057
  struct type *range_type;
6058
  struct attribute *attr;
6059
  LONGEST low = 0;
6060
  LONGEST high = -1;
6061
  char *name;
6062
  LONGEST negative_mask;
6063
 
6064
  base_type = die_type (die, cu);
6065
  if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
6066
    {
6067
      complaint (&symfile_complaints,
6068
                _("DW_AT_type missing from DW_TAG_subrange_type"));
6069
      base_type
6070
        = init_type (TYPE_CODE_INT, gdbarch_addr_bit (gdbarch) / 8,
6071
                     0, NULL, cu->objfile);
6072
    }
6073
 
6074
  if (cu->language == language_fortran)
6075
    {
6076
      /* FORTRAN implies a lower bound of 1, if not given.  */
6077
      low = 1;
6078
    }
6079
 
6080
  /* FIXME: For variable sized arrays either of these could be
6081
     a variable rather than a constant value.  We'll allow it,
6082
     but we don't know how to handle it.  */
6083
  attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
6084
  if (attr)
6085
    low = dwarf2_get_attr_constant_value (attr, 0);
6086
 
6087
  attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
6088
  if (attr)
6089
    {
6090
      if (attr->form == DW_FORM_block1)
6091
        {
6092
          /* GCC encodes arrays with unspecified or dynamic length
6093
             with a DW_FORM_block1 attribute.
6094
             FIXME: GDB does not yet know how to handle dynamic
6095
             arrays properly, treat them as arrays with unspecified
6096
             length for now.
6097
 
6098
             FIXME: jimb/2003-09-22: GDB does not really know
6099
             how to handle arrays of unspecified length
6100
             either; we just represent them as zero-length
6101
             arrays.  Choose an appropriate upper bound given
6102
             the lower bound we've computed above.  */
6103
          high = low - 1;
6104
        }
6105
      else
6106
        high = dwarf2_get_attr_constant_value (attr, 1);
6107
    }
6108
 
6109
  negative_mask =
6110
    (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
6111
  if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
6112
    low |= negative_mask;
6113
  if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
6114
    high |= negative_mask;
6115
 
6116
  range_type = create_range_type (NULL, base_type, low, high);
6117
 
6118
  name = dwarf2_name (die, cu);
6119
  if (name)
6120
    TYPE_NAME (range_type) = name;
6121
 
6122
  attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6123
  if (attr)
6124
    TYPE_LENGTH (range_type) = DW_UNSND (attr);
6125
 
6126
  set_descriptive_type (range_type, die, cu);
6127
 
6128
  return set_die_type (die, range_type, cu);
6129
}
6130
 
6131
static struct type *
6132
read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
6133
{
6134
  struct type *type;
6135
 
6136
  /* For now, we only support the C meaning of an unspecified type: void.  */
6137
 
6138
  type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
6139
  TYPE_NAME (type) = dwarf2_name (die, cu);
6140
 
6141
  return set_die_type (die, type, cu);
6142
}
6143
 
6144
/* Trivial hash function for die_info: the hash value of a DIE
6145
   is its offset in .debug_info for this objfile.  */
6146
 
6147
static hashval_t
6148
die_hash (const void *item)
6149
{
6150
  const struct die_info *die = item;
6151
  return die->offset;
6152
}
6153
 
6154
/* Trivial comparison function for die_info structures: two DIEs
6155
   are equal if they have the same offset.  */
6156
 
6157
static int
6158
die_eq (const void *item_lhs, const void *item_rhs)
6159
{
6160
  const struct die_info *die_lhs = item_lhs;
6161
  const struct die_info *die_rhs = item_rhs;
6162
  return die_lhs->offset == die_rhs->offset;
6163
}
6164
 
6165
/* Read a whole compilation unit into a linked list of dies.  */
6166
 
6167
static struct die_info *
6168
read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
6169
{
6170
  struct die_reader_specs reader_specs;
6171
 
6172
  gdb_assert (cu->die_hash == NULL);
6173
  cu->die_hash
6174
    = htab_create_alloc_ex (cu->header.length / 12,
6175
                            die_hash,
6176
                            die_eq,
6177
                            NULL,
6178
                            &cu->comp_unit_obstack,
6179
                            hashtab_obstack_allocate,
6180
                            dummy_obstack_deallocate);
6181
 
6182
  init_cu_die_reader (&reader_specs, cu);
6183
 
6184
  return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
6185
}
6186
 
6187
/* Main entry point for reading a DIE and all children.
6188
   Read the DIE and dump it if requested.  */
6189
 
6190
static struct die_info *
6191
read_die_and_children (const struct die_reader_specs *reader,
6192
                       gdb_byte *info_ptr,
6193
                       gdb_byte **new_info_ptr,
6194
                       struct die_info *parent)
6195
{
6196
  struct die_info *result = read_die_and_children_1 (reader, info_ptr,
6197
                                                     new_info_ptr, parent);
6198
 
6199
  if (dwarf2_die_debug)
6200
    {
6201
      fprintf_unfiltered (gdb_stdlog,
6202
                          "\nRead die from %s of %s:\n",
6203
                          reader->buffer == dwarf2_per_objfile->info.buffer
6204
                          ? ".debug_info"
6205
                          : reader->buffer == dwarf2_per_objfile->types.buffer
6206
                          ? ".debug_types"
6207
                          : "unknown section",
6208
                          reader->abfd->filename);
6209
      dump_die (result, dwarf2_die_debug);
6210
    }
6211
 
6212
  return result;
6213
}
6214
 
6215
/* Read a single die and all its descendents.  Set the die's sibling
6216
   field to NULL; set other fields in the die correctly, and set all
6217
   of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
6218
   location of the info_ptr after reading all of those dies.  PARENT
6219
   is the parent of the die in question.  */
6220
 
6221
static struct die_info *
6222
read_die_and_children_1 (const struct die_reader_specs *reader,
6223
                         gdb_byte *info_ptr,
6224
                         gdb_byte **new_info_ptr,
6225
                         struct die_info *parent)
6226
{
6227
  struct die_info *die;
6228
  gdb_byte *cur_ptr;
6229
  int has_children;
6230
 
6231
  cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
6232
  if (die == NULL)
6233
    {
6234
      *new_info_ptr = cur_ptr;
6235
      return NULL;
6236
    }
6237
  store_in_ref_table (die, reader->cu);
6238
 
6239
  if (has_children)
6240
    die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
6241
  else
6242
    {
6243
      die->child = NULL;
6244
      *new_info_ptr = cur_ptr;
6245
    }
6246
 
6247
  die->sibling = NULL;
6248
  die->parent = parent;
6249
  return die;
6250
}
6251
 
6252
/* Read a die, all of its descendents, and all of its siblings; set
6253
   all of the fields of all of the dies correctly.  Arguments are as
6254
   in read_die_and_children.  */
6255
 
6256
static struct die_info *
6257
read_die_and_siblings (const struct die_reader_specs *reader,
6258
                       gdb_byte *info_ptr,
6259
                       gdb_byte **new_info_ptr,
6260
                       struct die_info *parent)
6261
{
6262
  struct die_info *first_die, *last_sibling;
6263
  gdb_byte *cur_ptr;
6264
 
6265
  cur_ptr = info_ptr;
6266
  first_die = last_sibling = NULL;
6267
 
6268
  while (1)
6269
    {
6270
      struct die_info *die
6271
        = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
6272
 
6273
      if (die == NULL)
6274
        {
6275
          *new_info_ptr = cur_ptr;
6276
          return first_die;
6277
        }
6278
 
6279
      if (!first_die)
6280
        first_die = die;
6281
      else
6282
        last_sibling->sibling = die;
6283
 
6284
      last_sibling = die;
6285
    }
6286
}
6287
 
6288
/* Read the die from the .debug_info section buffer.  Set DIEP to
6289
   point to a newly allocated die with its information, except for its
6290
   child, sibling, and parent fields.  Set HAS_CHILDREN to tell
6291
   whether the die has children or not.  */
6292
 
6293
static gdb_byte *
6294
read_full_die (const struct die_reader_specs *reader,
6295
               struct die_info **diep, gdb_byte *info_ptr,
6296
               int *has_children)
6297
{
6298
  unsigned int abbrev_number, bytes_read, i, offset;
6299
  struct abbrev_info *abbrev;
6300
  struct die_info *die;
6301
  struct dwarf2_cu *cu = reader->cu;
6302
  bfd *abfd = reader->abfd;
6303
 
6304
  offset = info_ptr - reader->buffer;
6305
  abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6306
  info_ptr += bytes_read;
6307
  if (!abbrev_number)
6308
    {
6309
      *diep = NULL;
6310
      *has_children = 0;
6311
      return info_ptr;
6312
    }
6313
 
6314
  abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
6315
  if (!abbrev)
6316
    error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
6317
           abbrev_number,
6318
           bfd_get_filename (abfd));
6319
 
6320
  die = dwarf_alloc_die (cu, abbrev->num_attrs);
6321
  die->offset = offset;
6322
  die->tag = abbrev->tag;
6323
  die->abbrev = abbrev_number;
6324
 
6325
  die->num_attrs = abbrev->num_attrs;
6326
 
6327
  for (i = 0; i < abbrev->num_attrs; ++i)
6328
    info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
6329
                               abfd, info_ptr, cu);
6330
 
6331
  *diep = die;
6332
  *has_children = abbrev->has_children;
6333
  return info_ptr;
6334
}
6335
 
6336
/* In DWARF version 2, the description of the debugging information is
6337
   stored in a separate .debug_abbrev section.  Before we read any
6338
   dies from a section we read in all abbreviations and install them
6339
   in a hash table.  This function also sets flags in CU describing
6340
   the data found in the abbrev table.  */
6341
 
6342
static void
6343
dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
6344
{
6345
  struct comp_unit_head *cu_header = &cu->header;
6346
  gdb_byte *abbrev_ptr;
6347
  struct abbrev_info *cur_abbrev;
6348
  unsigned int abbrev_number, bytes_read, abbrev_name;
6349
  unsigned int abbrev_form, hash_number;
6350
  struct attr_abbrev *cur_attrs;
6351
  unsigned int allocated_attrs;
6352
 
6353
  /* Initialize dwarf2 abbrevs */
6354
  obstack_init (&cu->abbrev_obstack);
6355
  cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
6356
                                      (ABBREV_HASH_SIZE
6357
                                       * sizeof (struct abbrev_info *)));
6358
  memset (cu->dwarf2_abbrevs, 0,
6359
          ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
6360
 
6361
  abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
6362
  abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6363
  abbrev_ptr += bytes_read;
6364
 
6365
  allocated_attrs = ATTR_ALLOC_CHUNK;
6366
  cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6367
 
6368
  /* loop until we reach an abbrev number of 0 */
6369
  while (abbrev_number)
6370
    {
6371
      cur_abbrev = dwarf_alloc_abbrev (cu);
6372
 
6373
      /* read in abbrev header */
6374
      cur_abbrev->number = abbrev_number;
6375
      cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6376
      abbrev_ptr += bytes_read;
6377
      cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
6378
      abbrev_ptr += 1;
6379
 
6380
      if (cur_abbrev->tag == DW_TAG_namespace)
6381
        cu->has_namespace_info = 1;
6382
 
6383
      /* now read in declarations */
6384
      abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6385
      abbrev_ptr += bytes_read;
6386
      abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6387
      abbrev_ptr += bytes_read;
6388
      while (abbrev_name)
6389
        {
6390
          if (cur_abbrev->num_attrs == allocated_attrs)
6391
            {
6392
              allocated_attrs += ATTR_ALLOC_CHUNK;
6393
              cur_attrs
6394
                = xrealloc (cur_attrs, (allocated_attrs
6395
                                        * sizeof (struct attr_abbrev)));
6396
            }
6397
 
6398
          /* Record whether this compilation unit might have
6399
             inter-compilation-unit references.  If we don't know what form
6400
             this attribute will have, then it might potentially be a
6401
             DW_FORM_ref_addr, so we conservatively expect inter-CU
6402
             references.  */
6403
 
6404
          if (abbrev_form == DW_FORM_ref_addr
6405
              || abbrev_form == DW_FORM_indirect)
6406
            cu->has_form_ref_addr = 1;
6407
 
6408
          cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
6409
          cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
6410
          abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6411
          abbrev_ptr += bytes_read;
6412
          abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6413
          abbrev_ptr += bytes_read;
6414
        }
6415
 
6416
      cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
6417
                                         (cur_abbrev->num_attrs
6418
                                          * sizeof (struct attr_abbrev)));
6419
      memcpy (cur_abbrev->attrs, cur_attrs,
6420
              cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
6421
 
6422
      hash_number = abbrev_number % ABBREV_HASH_SIZE;
6423
      cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
6424
      cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
6425
 
6426
      /* Get next abbreviation.
6427
         Under Irix6 the abbreviations for a compilation unit are not
6428
         always properly terminated with an abbrev number of 0.
6429
         Exit loop if we encounter an abbreviation which we have
6430
         already read (which means we are about to read the abbreviations
6431
         for the next compile unit) or if the end of the abbreviation
6432
         table is reached.  */
6433
      if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
6434
          >= dwarf2_per_objfile->abbrev.size)
6435
        break;
6436
      abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
6437
      abbrev_ptr += bytes_read;
6438
      if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
6439
        break;
6440
    }
6441
 
6442
  xfree (cur_attrs);
6443
}
6444
 
6445
/* Release the memory used by the abbrev table for a compilation unit.  */
6446
 
6447
static void
6448
dwarf2_free_abbrev_table (void *ptr_to_cu)
6449
{
6450
  struct dwarf2_cu *cu = ptr_to_cu;
6451
 
6452
  obstack_free (&cu->abbrev_obstack, NULL);
6453
  cu->dwarf2_abbrevs = NULL;
6454
}
6455
 
6456
/* Lookup an abbrev_info structure in the abbrev hash table.  */
6457
 
6458
static struct abbrev_info *
6459
dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
6460
{
6461
  unsigned int hash_number;
6462
  struct abbrev_info *abbrev;
6463
 
6464
  hash_number = number % ABBREV_HASH_SIZE;
6465
  abbrev = cu->dwarf2_abbrevs[hash_number];
6466
 
6467
  while (abbrev)
6468
    {
6469
      if (abbrev->number == number)
6470
        return abbrev;
6471
      else
6472
        abbrev = abbrev->next;
6473
    }
6474
  return NULL;
6475
}
6476
 
6477
/* Returns nonzero if TAG represents a type that we might generate a partial
6478
   symbol for.  */
6479
 
6480
static int
6481
is_type_tag_for_partial (int tag)
6482
{
6483
  switch (tag)
6484
    {
6485
#if 0
6486
    /* Some types that would be reasonable to generate partial symbols for,
6487
       that we don't at present.  */
6488
    case DW_TAG_array_type:
6489
    case DW_TAG_file_type:
6490
    case DW_TAG_ptr_to_member_type:
6491
    case DW_TAG_set_type:
6492
    case DW_TAG_string_type:
6493
    case DW_TAG_subroutine_type:
6494
#endif
6495
    case DW_TAG_base_type:
6496
    case DW_TAG_class_type:
6497
    case DW_TAG_interface_type:
6498
    case DW_TAG_enumeration_type:
6499
    case DW_TAG_structure_type:
6500
    case DW_TAG_subrange_type:
6501
    case DW_TAG_typedef:
6502
    case DW_TAG_union_type:
6503
      return 1;
6504
    default:
6505
      return 0;
6506
    }
6507
}
6508
 
6509
/* Load all DIEs that are interesting for partial symbols into memory.  */
6510
 
6511
static struct partial_die_info *
6512
load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
6513
                   int building_psymtab, struct dwarf2_cu *cu)
6514
{
6515
  struct partial_die_info *part_die;
6516
  struct partial_die_info *parent_die, *last_die, *first_die = NULL;
6517
  struct abbrev_info *abbrev;
6518
  unsigned int bytes_read;
6519
  unsigned int load_all = 0;
6520
 
6521
  int nesting_level = 1;
6522
 
6523
  parent_die = NULL;
6524
  last_die = NULL;
6525
 
6526
  if (cu->per_cu && cu->per_cu->load_all_dies)
6527
    load_all = 1;
6528
 
6529
  cu->partial_dies
6530
    = htab_create_alloc_ex (cu->header.length / 12,
6531
                            partial_die_hash,
6532
                            partial_die_eq,
6533
                            NULL,
6534
                            &cu->comp_unit_obstack,
6535
                            hashtab_obstack_allocate,
6536
                            dummy_obstack_deallocate);
6537
 
6538
  part_die = obstack_alloc (&cu->comp_unit_obstack,
6539
                            sizeof (struct partial_die_info));
6540
 
6541
  while (1)
6542
    {
6543
      abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6544
 
6545
      /* A NULL abbrev means the end of a series of children.  */
6546
      if (abbrev == NULL)
6547
        {
6548
          if (--nesting_level == 0)
6549
            {
6550
              /* PART_DIE was probably the last thing allocated on the
6551
                 comp_unit_obstack, so we could call obstack_free
6552
                 here.  We don't do that because the waste is small,
6553
                 and will be cleaned up when we're done with this
6554
                 compilation unit.  This way, we're also more robust
6555
                 against other users of the comp_unit_obstack.  */
6556
              return first_die;
6557
            }
6558
          info_ptr += bytes_read;
6559
          last_die = parent_die;
6560
          parent_die = parent_die->die_parent;
6561
          continue;
6562
        }
6563
 
6564
      /* Check whether this DIE is interesting enough to save.  Normally
6565
         we would not be interested in members here, but there may be
6566
         later variables referencing them via DW_AT_specification (for
6567
         static members).  */
6568
      if (!load_all
6569
          && !is_type_tag_for_partial (abbrev->tag)
6570
          && abbrev->tag != DW_TAG_enumerator
6571
          && abbrev->tag != DW_TAG_subprogram
6572
          && abbrev->tag != DW_TAG_lexical_block
6573
          && abbrev->tag != DW_TAG_variable
6574
          && abbrev->tag != DW_TAG_namespace
6575
          && abbrev->tag != DW_TAG_member)
6576
        {
6577
          /* Otherwise we skip to the next sibling, if any.  */
6578
          info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
6579
          continue;
6580
        }
6581
 
6582
      info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
6583
                                   buffer, info_ptr, cu);
6584
 
6585
      /* This two-pass algorithm for processing partial symbols has a
6586
         high cost in cache pressure.  Thus, handle some simple cases
6587
         here which cover the majority of C partial symbols.  DIEs
6588
         which neither have specification tags in them, nor could have
6589
         specification tags elsewhere pointing at them, can simply be
6590
         processed and discarded.
6591
 
6592
         This segment is also optional; scan_partial_symbols and
6593
         add_partial_symbol will handle these DIEs if we chain
6594
         them in normally.  When compilers which do not emit large
6595
         quantities of duplicate debug information are more common,
6596
         this code can probably be removed.  */
6597
 
6598
      /* Any complete simple types at the top level (pretty much all
6599
         of them, for a language without namespaces), can be processed
6600
         directly.  */
6601
      if (parent_die == NULL
6602
          && part_die->has_specification == 0
6603
          && part_die->is_declaration == 0
6604
          && (part_die->tag == DW_TAG_typedef
6605
              || part_die->tag == DW_TAG_base_type
6606
              || part_die->tag == DW_TAG_subrange_type))
6607
        {
6608
          if (building_psymtab && part_die->name != NULL)
6609
            add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
6610
                                 VAR_DOMAIN, LOC_TYPEDEF,
6611
                                 &cu->objfile->static_psymbols,
6612
                                 0, (CORE_ADDR) 0, cu->language, cu->objfile);
6613
          info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
6614
          continue;
6615
        }
6616
 
6617
      /* If we're at the second level, and we're an enumerator, and
6618
         our parent has no specification (meaning possibly lives in a
6619
         namespace elsewhere), then we can add the partial symbol now
6620
         instead of queueing it.  */
6621
      if (part_die->tag == DW_TAG_enumerator
6622
          && parent_die != NULL
6623
          && parent_die->die_parent == NULL
6624
          && parent_die->tag == DW_TAG_enumeration_type
6625
          && parent_die->has_specification == 0)
6626
        {
6627
          if (part_die->name == NULL)
6628
            complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6629
          else if (building_psymtab)
6630
            add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
6631
                                 VAR_DOMAIN, LOC_CONST,
6632
                                 (cu->language == language_cplus
6633
                                  || cu->language == language_java)
6634
                                 ? &cu->objfile->global_psymbols
6635
                                 : &cu->objfile->static_psymbols,
6636
                                 0, (CORE_ADDR) 0, cu->language, cu->objfile);
6637
 
6638
          info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
6639
          continue;
6640
        }
6641
 
6642
      /* We'll save this DIE so link it in.  */
6643
      part_die->die_parent = parent_die;
6644
      part_die->die_sibling = NULL;
6645
      part_die->die_child = NULL;
6646
 
6647
      if (last_die && last_die == parent_die)
6648
        last_die->die_child = part_die;
6649
      else if (last_die)
6650
        last_die->die_sibling = part_die;
6651
 
6652
      last_die = part_die;
6653
 
6654
      if (first_die == NULL)
6655
        first_die = part_die;
6656
 
6657
      /* Maybe add the DIE to the hash table.  Not all DIEs that we
6658
         find interesting need to be in the hash table, because we
6659
         also have the parent/sibling/child chains; only those that we
6660
         might refer to by offset later during partial symbol reading.
6661
 
6662
         For now this means things that might have be the target of a
6663
         DW_AT_specification, DW_AT_abstract_origin, or
6664
         DW_AT_extension.  DW_AT_extension will refer only to
6665
         namespaces; DW_AT_abstract_origin refers to functions (and
6666
         many things under the function DIE, but we do not recurse
6667
         into function DIEs during partial symbol reading) and
6668
         possibly variables as well; DW_AT_specification refers to
6669
         declarations.  Declarations ought to have the DW_AT_declaration
6670
         flag.  It happens that GCC forgets to put it in sometimes, but
6671
         only for functions, not for types.
6672
 
6673
         Adding more things than necessary to the hash table is harmless
6674
         except for the performance cost.  Adding too few will result in
6675
         wasted time in find_partial_die, when we reread the compilation
6676
         unit with load_all_dies set.  */
6677
 
6678
      if (load_all
6679
          || abbrev->tag == DW_TAG_subprogram
6680
          || abbrev->tag == DW_TAG_variable
6681
          || abbrev->tag == DW_TAG_namespace
6682
          || part_die->is_declaration)
6683
        {
6684
          void **slot;
6685
 
6686
          slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
6687
                                           part_die->offset, INSERT);
6688
          *slot = part_die;
6689
        }
6690
 
6691
      part_die = obstack_alloc (&cu->comp_unit_obstack,
6692
                                sizeof (struct partial_die_info));
6693
 
6694
      /* For some DIEs we want to follow their children (if any).  For C
6695
         we have no reason to follow the children of structures; for other
6696
         languages we have to, both so that we can get at method physnames
6697
         to infer fully qualified class names, and for DW_AT_specification.
6698
 
6699
         For Ada, we need to scan the children of subprograms and lexical
6700
         blocks as well because Ada allows the definition of nested
6701
         entities that could be interesting for the debugger, such as
6702
         nested subprograms for instance.  */
6703
      if (last_die->has_children
6704
          && (load_all
6705
              || last_die->tag == DW_TAG_namespace
6706
              || last_die->tag == DW_TAG_enumeration_type
6707
              || (cu->language != language_c
6708
                  && (last_die->tag == DW_TAG_class_type
6709
                      || last_die->tag == DW_TAG_interface_type
6710
                      || last_die->tag == DW_TAG_structure_type
6711
                      || last_die->tag == DW_TAG_union_type))
6712
              || (cu->language == language_ada
6713
                  && (last_die->tag == DW_TAG_subprogram
6714
                      || last_die->tag == DW_TAG_lexical_block))))
6715
        {
6716
          nesting_level++;
6717
          parent_die = last_die;
6718
          continue;
6719
        }
6720
 
6721
      /* Otherwise we skip to the next sibling, if any.  */
6722
      info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
6723
 
6724
      /* Back to the top, do it again.  */
6725
    }
6726
}
6727
 
6728
/* Read a minimal amount of information into the minimal die structure.  */
6729
 
6730
static gdb_byte *
6731
read_partial_die (struct partial_die_info *part_die,
6732
                  struct abbrev_info *abbrev,
6733
                  unsigned int abbrev_len, bfd *abfd,
6734
                  gdb_byte *buffer, gdb_byte *info_ptr,
6735
                  struct dwarf2_cu *cu)
6736
{
6737
  unsigned int bytes_read, i;
6738
  struct attribute attr;
6739
  int has_low_pc_attr = 0;
6740
  int has_high_pc_attr = 0;
6741
 
6742
  memset (part_die, 0, sizeof (struct partial_die_info));
6743
 
6744
  part_die->offset = info_ptr - buffer;
6745
 
6746
  info_ptr += abbrev_len;
6747
 
6748
  if (abbrev == NULL)
6749
    return info_ptr;
6750
 
6751
  part_die->tag = abbrev->tag;
6752
  part_die->has_children = abbrev->has_children;
6753
 
6754
  for (i = 0; i < abbrev->num_attrs; ++i)
6755
    {
6756
      info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
6757
 
6758
      /* Store the data if it is of an attribute we want to keep in a
6759
         partial symbol table.  */
6760
      switch (attr.name)
6761
        {
6762
        case DW_AT_name:
6763
          switch (part_die->tag)
6764
            {
6765
            case DW_TAG_compile_unit:
6766
            case DW_TAG_type_unit:
6767
              /* Compilation units have a DW_AT_name that is a filename, not
6768
                 a source language identifier.  */
6769
            case DW_TAG_enumeration_type:
6770
            case DW_TAG_enumerator:
6771
              /* These tags always have simple identifiers already; no need
6772
                 to canonicalize them.  */
6773
              part_die->name = DW_STRING (&attr);
6774
              break;
6775
            default:
6776
              part_die->name
6777
                = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
6778
                                            &cu->objfile->objfile_obstack);
6779
              break;
6780
            }
6781
          break;
6782
        case DW_AT_MIPS_linkage_name:
6783
          part_die->name = DW_STRING (&attr);
6784
          break;
6785
        case DW_AT_low_pc:
6786
          has_low_pc_attr = 1;
6787
          part_die->lowpc = DW_ADDR (&attr);
6788
          break;
6789
        case DW_AT_high_pc:
6790
          has_high_pc_attr = 1;
6791
          part_die->highpc = DW_ADDR (&attr);
6792
          break;
6793
        case DW_AT_location:
6794
          /* Support the .debug_loc offsets */
6795
          if (attr_form_is_block (&attr))
6796
            {
6797
               part_die->locdesc = DW_BLOCK (&attr);
6798
            }
6799
          else if (attr_form_is_section_offset (&attr))
6800
            {
6801
              dwarf2_complex_location_expr_complaint ();
6802
            }
6803
          else
6804
            {
6805
              dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6806
                                                     "partial symbol information");
6807
            }
6808
          break;
6809
        case DW_AT_external:
6810
          part_die->is_external = DW_UNSND (&attr);
6811
          break;
6812
        case DW_AT_declaration:
6813
          part_die->is_declaration = DW_UNSND (&attr);
6814
          break;
6815
        case DW_AT_type:
6816
          part_die->has_type = 1;
6817
          break;
6818
        case DW_AT_abstract_origin:
6819
        case DW_AT_specification:
6820
        case DW_AT_extension:
6821
          part_die->has_specification = 1;
6822
          part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
6823
          break;
6824
        case DW_AT_sibling:
6825
          /* Ignore absolute siblings, they might point outside of
6826
             the current compile unit.  */
6827
          if (attr.form == DW_FORM_ref_addr)
6828
            complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
6829
          else
6830
            part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
6831
          break;
6832
        case DW_AT_byte_size:
6833
          part_die->has_byte_size = 1;
6834
          break;
6835
        case DW_AT_calling_convention:
6836
          /* DWARF doesn't provide a way to identify a program's source-level
6837
             entry point.  DW_AT_calling_convention attributes are only meant
6838
             to describe functions' calling conventions.
6839
 
6840
             However, because it's a necessary piece of information in
6841
             Fortran, and because DW_CC_program is the only piece of debugging
6842
             information whose definition refers to a 'main program' at all,
6843
             several compilers have begun marking Fortran main programs with
6844
             DW_CC_program --- even when those functions use the standard
6845
             calling conventions.
6846
 
6847
             So until DWARF specifies a way to provide this information and
6848
             compilers pick up the new representation, we'll support this
6849
             practice.  */
6850
          if (DW_UNSND (&attr) == DW_CC_program
6851
              && cu->language == language_fortran)
6852
            set_main_name (part_die->name);
6853
          break;
6854
        default:
6855
          break;
6856
        }
6857
    }
6858
 
6859
  /* When using the GNU linker, .gnu.linkonce. sections are used to
6860
     eliminate duplicate copies of functions and vtables and such.
6861
     The linker will arbitrarily choose one and discard the others.
6862
     The AT_*_pc values for such functions refer to local labels in
6863
     these sections.  If the section from that file was discarded, the
6864
     labels are not in the output, so the relocs get a value of 0.
6865
     If this is a discarded function, mark the pc bounds as invalid,
6866
     so that GDB will ignore it.  */
6867
  if (has_low_pc_attr && has_high_pc_attr
6868
      && part_die->lowpc < part_die->highpc
6869
      && (part_die->lowpc != 0
6870
          || dwarf2_per_objfile->has_section_at_zero))
6871
    part_die->has_pc_info = 1;
6872
 
6873
  return info_ptr;
6874
}
6875
 
6876
/* Find a cached partial DIE at OFFSET in CU.  */
6877
 
6878
static struct partial_die_info *
6879
find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
6880
{
6881
  struct partial_die_info *lookup_die = NULL;
6882
  struct partial_die_info part_die;
6883
 
6884
  part_die.offset = offset;
6885
  lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
6886
 
6887
  return lookup_die;
6888
}
6889
 
6890
/* Find a partial DIE at OFFSET, which may or may not be in CU,
6891
   except in the case of .debug_types DIEs which do not reference
6892
   outside their CU (they do however referencing other types via
6893
   DW_FORM_sig8).  */
6894
 
6895
static struct partial_die_info *
6896
find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
6897
{
6898
  struct dwarf2_per_cu_data *per_cu = NULL;
6899
  struct partial_die_info *pd = NULL;
6900
 
6901
  if (cu->per_cu->from_debug_types)
6902
    {
6903
      pd = find_partial_die_in_comp_unit (offset, cu);
6904
      if (pd != NULL)
6905
        return pd;
6906
      goto not_found;
6907
    }
6908
 
6909
  if (offset_in_cu_p (&cu->header, offset))
6910
    {
6911
      pd = find_partial_die_in_comp_unit (offset, cu);
6912
      if (pd != NULL)
6913
        return pd;
6914
    }
6915
 
6916
  per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
6917
 
6918
  if (per_cu->cu == NULL)
6919
    {
6920
      load_partial_comp_unit (per_cu, cu->objfile);
6921
      per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6922
      dwarf2_per_objfile->read_in_chain = per_cu;
6923
    }
6924
 
6925
  per_cu->cu->last_used = 0;
6926
  pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6927
 
6928
  if (pd == NULL && per_cu->load_all_dies == 0)
6929
    {
6930
      struct cleanup *back_to;
6931
      struct partial_die_info comp_unit_die;
6932
      struct abbrev_info *abbrev;
6933
      unsigned int bytes_read;
6934
      char *info_ptr;
6935
 
6936
      per_cu->load_all_dies = 1;
6937
 
6938
      /* Re-read the DIEs.  */
6939
      back_to = make_cleanup (null_cleanup, 0);
6940
      if (per_cu->cu->dwarf2_abbrevs == NULL)
6941
        {
6942
          dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
6943
          make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
6944
        }
6945
      info_ptr = (dwarf2_per_objfile->info.buffer
6946
                  + per_cu->cu->header.offset
6947
                  + per_cu->cu->header.first_die_offset);
6948
      abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
6949
      info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
6950
                                   per_cu->cu->objfile->obfd,
6951
                                   dwarf2_per_objfile->info.buffer, info_ptr,
6952
                                   per_cu->cu);
6953
      if (comp_unit_die.has_children)
6954
        load_partial_dies (per_cu->cu->objfile->obfd,
6955
                           dwarf2_per_objfile->info.buffer, info_ptr,
6956
                           0, per_cu->cu);
6957
      do_cleanups (back_to);
6958
 
6959
      pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
6960
    }
6961
 
6962
 not_found:
6963
 
6964
  if (pd == NULL)
6965
    internal_error (__FILE__, __LINE__,
6966
                    _("could not find partial DIE 0x%x in cache [from module %s]\n"),
6967
                    offset, bfd_get_filename (cu->objfile->obfd));
6968
  return pd;
6969
}
6970
 
6971
/* Adjust PART_DIE before generating a symbol for it.  This function
6972
   may set the is_external flag or change the DIE's name.  */
6973
 
6974
static void
6975
fixup_partial_die (struct partial_die_info *part_die,
6976
                   struct dwarf2_cu *cu)
6977
{
6978
  /* If we found a reference attribute and the DIE has no name, try
6979
     to find a name in the referred to DIE.  */
6980
 
6981
  if (part_die->name == NULL && part_die->has_specification)
6982
    {
6983
      struct partial_die_info *spec_die;
6984
 
6985
      spec_die = find_partial_die (part_die->spec_offset, cu);
6986
 
6987
      fixup_partial_die (spec_die, cu);
6988
 
6989
      if (spec_die->name)
6990
        {
6991
          part_die->name = spec_die->name;
6992
 
6993
          /* Copy DW_AT_external attribute if it is set.  */
6994
          if (spec_die->is_external)
6995
            part_die->is_external = spec_die->is_external;
6996
        }
6997
    }
6998
 
6999
  /* Set default names for some unnamed DIEs.  */
7000
  if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
7001
                                 || part_die->tag == DW_TAG_class_type))
7002
    part_die->name = "(anonymous class)";
7003
 
7004
  if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
7005
    part_die->name = "(anonymous namespace)";
7006
 
7007
  if (part_die->tag == DW_TAG_structure_type
7008
      || part_die->tag == DW_TAG_class_type
7009
      || part_die->tag == DW_TAG_union_type)
7010
    guess_structure_name (part_die, cu);
7011
}
7012
 
7013
/* Read an attribute value described by an attribute form.  */
7014
 
7015
static gdb_byte *
7016
read_attribute_value (struct attribute *attr, unsigned form,
7017
                      bfd *abfd, gdb_byte *info_ptr,
7018
                      struct dwarf2_cu *cu)
7019
{
7020
  struct comp_unit_head *cu_header = &cu->header;
7021
  unsigned int bytes_read;
7022
  struct dwarf_block *blk;
7023
 
7024
  attr->form = form;
7025
  switch (form)
7026
    {
7027
    case DW_FORM_ref_addr:
7028
      if (cu->header.version == 2)
7029
        DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
7030
      else
7031
        DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
7032
      info_ptr += bytes_read;
7033
      break;
7034
    case DW_FORM_addr:
7035
      DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
7036
      info_ptr += bytes_read;
7037
      break;
7038
    case DW_FORM_block2:
7039
      blk = dwarf_alloc_block (cu);
7040
      blk->size = read_2_bytes (abfd, info_ptr);
7041
      info_ptr += 2;
7042
      blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7043
      info_ptr += blk->size;
7044
      DW_BLOCK (attr) = blk;
7045
      break;
7046
    case DW_FORM_block4:
7047
      blk = dwarf_alloc_block (cu);
7048
      blk->size = read_4_bytes (abfd, info_ptr);
7049
      info_ptr += 4;
7050
      blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7051
      info_ptr += blk->size;
7052
      DW_BLOCK (attr) = blk;
7053
      break;
7054
    case DW_FORM_data2:
7055
      DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
7056
      info_ptr += 2;
7057
      break;
7058
    case DW_FORM_data4:
7059
      DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
7060
      info_ptr += 4;
7061
      break;
7062
    case DW_FORM_data8:
7063
      DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
7064
      info_ptr += 8;
7065
      break;
7066
    case DW_FORM_string:
7067
      DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
7068
      DW_STRING_IS_CANONICAL (attr) = 0;
7069
      info_ptr += bytes_read;
7070
      break;
7071
    case DW_FORM_strp:
7072
      DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
7073
                                               &bytes_read);
7074
      DW_STRING_IS_CANONICAL (attr) = 0;
7075
      info_ptr += bytes_read;
7076
      break;
7077
    case DW_FORM_block:
7078
      blk = dwarf_alloc_block (cu);
7079
      blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7080
      info_ptr += bytes_read;
7081
      blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7082
      info_ptr += blk->size;
7083
      DW_BLOCK (attr) = blk;
7084
      break;
7085
    case DW_FORM_block1:
7086
      blk = dwarf_alloc_block (cu);
7087
      blk->size = read_1_byte (abfd, info_ptr);
7088
      info_ptr += 1;
7089
      blk->data = read_n_bytes (abfd, info_ptr, blk->size);
7090
      info_ptr += blk->size;
7091
      DW_BLOCK (attr) = blk;
7092
      break;
7093
    case DW_FORM_data1:
7094
      DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
7095
      info_ptr += 1;
7096
      break;
7097
    case DW_FORM_flag:
7098
      DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
7099
      info_ptr += 1;
7100
      break;
7101
    case DW_FORM_sdata:
7102
      DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
7103
      info_ptr += bytes_read;
7104
      break;
7105
    case DW_FORM_udata:
7106
      DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7107
      info_ptr += bytes_read;
7108
      break;
7109
    case DW_FORM_ref1:
7110
      DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
7111
      info_ptr += 1;
7112
      break;
7113
    case DW_FORM_ref2:
7114
      DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
7115
      info_ptr += 2;
7116
      break;
7117
    case DW_FORM_ref4:
7118
      DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
7119
      info_ptr += 4;
7120
      break;
7121
    case DW_FORM_ref8:
7122
      DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
7123
      info_ptr += 8;
7124
      break;
7125
    case DW_FORM_sig8:
7126
      /* Convert the signature to something we can record in DW_UNSND
7127
         for later lookup.
7128
         NOTE: This is NULL if the type wasn't found.  */
7129
      DW_SIGNATURED_TYPE (attr) =
7130
        lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
7131
      info_ptr += 8;
7132
      break;
7133
    case DW_FORM_ref_udata:
7134
      DW_ADDR (attr) = (cu->header.offset
7135
                        + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
7136
      info_ptr += bytes_read;
7137
      break;
7138
    case DW_FORM_indirect:
7139
      form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7140
      info_ptr += bytes_read;
7141
      info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
7142
      break;
7143
    default:
7144
      error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
7145
             dwarf_form_name (form),
7146
             bfd_get_filename (abfd));
7147
    }
7148
 
7149
  /* We have seen instances where the compiler tried to emit a byte
7150
     size attribute of -1 which ended up being encoded as an unsigned
7151
     0xffffffff.  Although 0xffffffff is technically a valid size value,
7152
     an object of this size seems pretty unlikely so we can relatively
7153
     safely treat these cases as if the size attribute was invalid and
7154
     treat them as zero by default.  */
7155
  if (attr->name == DW_AT_byte_size
7156
      && form == DW_FORM_data4
7157
      && DW_UNSND (attr) >= 0xffffffff)
7158
    {
7159
      complaint
7160
        (&symfile_complaints,
7161
         _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
7162
         hex_string (DW_UNSND (attr)));
7163
      DW_UNSND (attr) = 0;
7164
    }
7165
 
7166
  return info_ptr;
7167
}
7168
 
7169
/* Read an attribute described by an abbreviated attribute.  */
7170
 
7171
static gdb_byte *
7172
read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
7173
                bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
7174
{
7175
  attr->name = abbrev->name;
7176
  return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
7177
}
7178
 
7179
/* read dwarf information from a buffer */
7180
 
7181
static unsigned int
7182
read_1_byte (bfd *abfd, gdb_byte *buf)
7183
{
7184
  return bfd_get_8 (abfd, buf);
7185
}
7186
 
7187
static int
7188
read_1_signed_byte (bfd *abfd, gdb_byte *buf)
7189
{
7190
  return bfd_get_signed_8 (abfd, buf);
7191
}
7192
 
7193
static unsigned int
7194
read_2_bytes (bfd *abfd, gdb_byte *buf)
7195
{
7196
  return bfd_get_16 (abfd, buf);
7197
}
7198
 
7199
static int
7200
read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
7201
{
7202
  return bfd_get_signed_16 (abfd, buf);
7203
}
7204
 
7205
static unsigned int
7206
read_4_bytes (bfd *abfd, gdb_byte *buf)
7207
{
7208
  return bfd_get_32 (abfd, buf);
7209
}
7210
 
7211
static int
7212
read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
7213
{
7214
  return bfd_get_signed_32 (abfd, buf);
7215
}
7216
 
7217
static ULONGEST
7218
read_8_bytes (bfd *abfd, gdb_byte *buf)
7219
{
7220
  return bfd_get_64 (abfd, buf);
7221
}
7222
 
7223
static CORE_ADDR
7224
read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
7225
              unsigned int *bytes_read)
7226
{
7227
  struct comp_unit_head *cu_header = &cu->header;
7228
  CORE_ADDR retval = 0;
7229
 
7230
  if (cu_header->signed_addr_p)
7231
    {
7232
      switch (cu_header->addr_size)
7233
        {
7234
        case 2:
7235
          retval = bfd_get_signed_16 (abfd, buf);
7236
          break;
7237
        case 4:
7238
          retval = bfd_get_signed_32 (abfd, buf);
7239
          break;
7240
        case 8:
7241
          retval = bfd_get_signed_64 (abfd, buf);
7242
          break;
7243
        default:
7244
          internal_error (__FILE__, __LINE__,
7245
                          _("read_address: bad switch, signed [in module %s]"),
7246
                          bfd_get_filename (abfd));
7247
        }
7248
    }
7249
  else
7250
    {
7251
      switch (cu_header->addr_size)
7252
        {
7253
        case 2:
7254
          retval = bfd_get_16 (abfd, buf);
7255
          break;
7256
        case 4:
7257
          retval = bfd_get_32 (abfd, buf);
7258
          break;
7259
        case 8:
7260
          retval = bfd_get_64 (abfd, buf);
7261
          break;
7262
        default:
7263
          internal_error (__FILE__, __LINE__,
7264
                          _("read_address: bad switch, unsigned [in module %s]"),
7265
                          bfd_get_filename (abfd));
7266
        }
7267
    }
7268
 
7269
  *bytes_read = cu_header->addr_size;
7270
  return retval;
7271
}
7272
 
7273
/* Read the initial length from a section.  The (draft) DWARF 3
7274
   specification allows the initial length to take up either 4 bytes
7275
   or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
7276
   bytes describe the length and all offsets will be 8 bytes in length
7277
   instead of 4.
7278
 
7279
   An older, non-standard 64-bit format is also handled by this
7280
   function.  The older format in question stores the initial length
7281
   as an 8-byte quantity without an escape value.  Lengths greater
7282
   than 2^32 aren't very common which means that the initial 4 bytes
7283
   is almost always zero.  Since a length value of zero doesn't make
7284
   sense for the 32-bit format, this initial zero can be considered to
7285
   be an escape value which indicates the presence of the older 64-bit
7286
   format.  As written, the code can't detect (old format) lengths
7287
   greater than 4GB.  If it becomes necessary to handle lengths
7288
   somewhat larger than 4GB, we could allow other small values (such
7289
   as the non-sensical values of 1, 2, and 3) to also be used as
7290
   escape values indicating the presence of the old format.
7291
 
7292
   The value returned via bytes_read should be used to increment the
7293
   relevant pointer after calling read_initial_length().
7294
 
7295
   [ Note:  read_initial_length() and read_offset() are based on the
7296
     document entitled "DWARF Debugging Information Format", revision
7297
     3, draft 8, dated November 19, 2001.  This document was obtained
7298
     from:
7299
 
7300
        http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
7301
 
7302
     This document is only a draft and is subject to change.  (So beware.)
7303
 
7304
     Details regarding the older, non-standard 64-bit format were
7305
     determined empirically by examining 64-bit ELF files produced by
7306
     the SGI toolchain on an IRIX 6.5 machine.
7307
 
7308
     - Kevin, July 16, 2002
7309
   ] */
7310
 
7311
static LONGEST
7312
read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
7313
{
7314
  LONGEST length = bfd_get_32 (abfd, buf);
7315
 
7316
  if (length == 0xffffffff)
7317
    {
7318
      length = bfd_get_64 (abfd, buf + 4);
7319
      *bytes_read = 12;
7320
    }
7321
  else if (length == 0)
7322
    {
7323
      /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
7324
      length = bfd_get_64 (abfd, buf);
7325
      *bytes_read = 8;
7326
    }
7327
  else
7328
    {
7329
      *bytes_read = 4;
7330
    }
7331
 
7332
  return length;
7333
}
7334
 
7335
/* Cover function for read_initial_length.
7336
   Returns the length of the object at BUF, and stores the size of the
7337
   initial length in *BYTES_READ and stores the size that offsets will be in
7338
   *OFFSET_SIZE.
7339
   If the initial length size is not equivalent to that specified in
7340
   CU_HEADER then issue a complaint.
7341
   This is useful when reading non-comp-unit headers.  */
7342
 
7343
static LONGEST
7344
read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
7345
                                        const struct comp_unit_head *cu_header,
7346
                                        unsigned int *bytes_read,
7347
                                        unsigned int *offset_size)
7348
{
7349
  LONGEST length = read_initial_length (abfd, buf, bytes_read);
7350
 
7351
  gdb_assert (cu_header->initial_length_size == 4
7352
              || cu_header->initial_length_size == 8
7353
              || cu_header->initial_length_size == 12);
7354
 
7355
  if (cu_header->initial_length_size != *bytes_read)
7356
    complaint (&symfile_complaints,
7357
               _("intermixed 32-bit and 64-bit DWARF sections"));
7358
 
7359
  *offset_size = (*bytes_read == 4) ? 4 : 8;
7360
  return length;
7361
}
7362
 
7363
/* Read an offset from the data stream.  The size of the offset is
7364
   given by cu_header->offset_size.  */
7365
 
7366
static LONGEST
7367
read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
7368
             unsigned int *bytes_read)
7369
{
7370
  LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
7371
  *bytes_read = cu_header->offset_size;
7372
  return offset;
7373
}
7374
 
7375
/* Read an offset from the data stream.  */
7376
 
7377
static LONGEST
7378
read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
7379
{
7380
  LONGEST retval = 0;
7381
 
7382
  switch (offset_size)
7383
    {
7384
    case 4:
7385
      retval = bfd_get_32 (abfd, buf);
7386
      break;
7387
    case 8:
7388
      retval = bfd_get_64 (abfd, buf);
7389
      break;
7390
    default:
7391
      internal_error (__FILE__, __LINE__,
7392
                      _("read_offset_1: bad switch [in module %s]"),
7393
                      bfd_get_filename (abfd));
7394
    }
7395
 
7396
  return retval;
7397
}
7398
 
7399
static gdb_byte *
7400
read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
7401
{
7402
  /* If the size of a host char is 8 bits, we can return a pointer
7403
     to the buffer, otherwise we have to copy the data to a buffer
7404
     allocated on the temporary obstack.  */
7405
  gdb_assert (HOST_CHAR_BIT == 8);
7406
  return buf;
7407
}
7408
 
7409
static char *
7410
read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
7411
{
7412
  /* If the size of a host char is 8 bits, we can return a pointer
7413
     to the string, otherwise we have to copy the string to a buffer
7414
     allocated on the temporary obstack.  */
7415
  gdb_assert (HOST_CHAR_BIT == 8);
7416
  if (*buf == '\0')
7417
    {
7418
      *bytes_read_ptr = 1;
7419
      return NULL;
7420
    }
7421
  *bytes_read_ptr = strlen ((char *) buf) + 1;
7422
  return (char *) buf;
7423
}
7424
 
7425
static char *
7426
read_indirect_string (bfd *abfd, gdb_byte *buf,
7427
                      const struct comp_unit_head *cu_header,
7428
                      unsigned int *bytes_read_ptr)
7429
{
7430
  LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
7431
 
7432
  if (dwarf2_per_objfile->str.buffer == NULL)
7433
    {
7434
      error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
7435
                      bfd_get_filename (abfd));
7436
      return NULL;
7437
    }
7438
  if (str_offset >= dwarf2_per_objfile->str.size)
7439
    {
7440
      error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
7441
                      bfd_get_filename (abfd));
7442
      return NULL;
7443
    }
7444
  gdb_assert (HOST_CHAR_BIT == 8);
7445
  if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
7446
    return NULL;
7447
  return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
7448
}
7449
 
7450
static unsigned long
7451
read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
7452
{
7453
  unsigned long result;
7454
  unsigned int num_read;
7455
  int i, shift;
7456
  unsigned char byte;
7457
 
7458
  result = 0;
7459
  shift = 0;
7460
  num_read = 0;
7461
  i = 0;
7462
  while (1)
7463
    {
7464
      byte = bfd_get_8 (abfd, buf);
7465
      buf++;
7466
      num_read++;
7467
      result |= ((unsigned long)(byte & 127) << shift);
7468
      if ((byte & 128) == 0)
7469
        {
7470
          break;
7471
        }
7472
      shift += 7;
7473
    }
7474
  *bytes_read_ptr = num_read;
7475
  return result;
7476
}
7477
 
7478
static long
7479
read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
7480
{
7481
  long result;
7482
  int i, shift, num_read;
7483
  unsigned char byte;
7484
 
7485
  result = 0;
7486
  shift = 0;
7487
  num_read = 0;
7488
  i = 0;
7489
  while (1)
7490
    {
7491
      byte = bfd_get_8 (abfd, buf);
7492
      buf++;
7493
      num_read++;
7494
      result |= ((long)(byte & 127) << shift);
7495
      shift += 7;
7496
      if ((byte & 128) == 0)
7497
        {
7498
          break;
7499
        }
7500
    }
7501
  if ((shift < 8 * sizeof (result)) && (byte & 0x40))
7502
    result |= -(((long)1) << shift);
7503
  *bytes_read_ptr = num_read;
7504
  return result;
7505
}
7506
 
7507
/* Return a pointer to just past the end of an LEB128 number in BUF.  */
7508
 
7509
static gdb_byte *
7510
skip_leb128 (bfd *abfd, gdb_byte *buf)
7511
{
7512
  int byte;
7513
 
7514
  while (1)
7515
    {
7516
      byte = bfd_get_8 (abfd, buf);
7517
      buf++;
7518
      if ((byte & 128) == 0)
7519
        return buf;
7520
    }
7521
}
7522
 
7523
static void
7524
set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
7525
{
7526
  switch (lang)
7527
    {
7528
    case DW_LANG_C89:
7529
    case DW_LANG_C99:
7530
    case DW_LANG_C:
7531
      cu->language = language_c;
7532
      break;
7533
    case DW_LANG_C_plus_plus:
7534
      cu->language = language_cplus;
7535
      break;
7536
    case DW_LANG_Fortran77:
7537
    case DW_LANG_Fortran90:
7538
    case DW_LANG_Fortran95:
7539
      cu->language = language_fortran;
7540
      break;
7541
    case DW_LANG_Mips_Assembler:
7542
      cu->language = language_asm;
7543
      break;
7544
    case DW_LANG_Java:
7545
      cu->language = language_java;
7546
      break;
7547
    case DW_LANG_Ada83:
7548
    case DW_LANG_Ada95:
7549
      cu->language = language_ada;
7550
      break;
7551
    case DW_LANG_Modula2:
7552
      cu->language = language_m2;
7553
      break;
7554
    case DW_LANG_Pascal83:
7555
      cu->language = language_pascal;
7556
      break;
7557
    case DW_LANG_ObjC:
7558
      cu->language = language_objc;
7559
      break;
7560
    case DW_LANG_Cobol74:
7561
    case DW_LANG_Cobol85:
7562
    default:
7563
      cu->language = language_minimal;
7564
      break;
7565
    }
7566
  cu->language_defn = language_def (cu->language);
7567
}
7568
 
7569
/* Return the named attribute or NULL if not there.  */
7570
 
7571
static struct attribute *
7572
dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
7573
{
7574
  unsigned int i;
7575
  struct attribute *spec = NULL;
7576
 
7577
  for (i = 0; i < die->num_attrs; ++i)
7578
    {
7579
      if (die->attrs[i].name == name)
7580
        return &die->attrs[i];
7581
      if (die->attrs[i].name == DW_AT_specification
7582
          || die->attrs[i].name == DW_AT_abstract_origin)
7583
        spec = &die->attrs[i];
7584
    }
7585
 
7586
  if (spec)
7587
    {
7588
      die = follow_die_ref (die, spec, &cu);
7589
      return dwarf2_attr (die, name, cu);
7590
    }
7591
 
7592
  return NULL;
7593
}
7594
 
7595
/* Return the named attribute or NULL if not there,
7596
   but do not follow DW_AT_specification, etc.
7597
   This is for use in contexts where we're reading .debug_types dies.
7598
   Following DW_AT_specification, DW_AT_abstract_origin will take us
7599
   back up the chain, and we want to go down.  */
7600
 
7601
static struct attribute *
7602
dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
7603
                       struct dwarf2_cu *cu)
7604
{
7605
  unsigned int i;
7606
 
7607
  for (i = 0; i < die->num_attrs; ++i)
7608
    if (die->attrs[i].name == name)
7609
      return &die->attrs[i];
7610
 
7611
  return NULL;
7612
}
7613
 
7614
/* Return non-zero iff the attribute NAME is defined for the given DIE,
7615
   and holds a non-zero value.  This function should only be used for
7616
   DW_FORM_flag attributes.  */
7617
 
7618
static int
7619
dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
7620
{
7621
  struct attribute *attr = dwarf2_attr (die, name, cu);
7622
 
7623
  return (attr && DW_UNSND (attr));
7624
}
7625
 
7626
static int
7627
die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
7628
{
7629
  /* A DIE is a declaration if it has a DW_AT_declaration attribute
7630
     which value is non-zero.  However, we have to be careful with
7631
     DIEs having a DW_AT_specification attribute, because dwarf2_attr()
7632
     (via dwarf2_flag_true_p) follows this attribute.  So we may
7633
     end up accidently finding a declaration attribute that belongs
7634
     to a different DIE referenced by the specification attribute,
7635
     even though the given DIE does not have a declaration attribute.  */
7636
  return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
7637
          && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
7638
}
7639
 
7640
/* Return the die giving the specification for DIE, if there is
7641
   one.  *SPEC_CU is the CU containing DIE on input, and the CU
7642
   containing the return value on output.  If there is no
7643
   specification, but there is an abstract origin, that is
7644
   returned.  */
7645
 
7646
static struct die_info *
7647
die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
7648
{
7649
  struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
7650
                                             *spec_cu);
7651
 
7652
  if (spec_attr == NULL)
7653
    spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
7654
 
7655
  if (spec_attr == NULL)
7656
    return NULL;
7657
  else
7658
    return follow_die_ref (die, spec_attr, spec_cu);
7659
}
7660
 
7661
/* Free the line_header structure *LH, and any arrays and strings it
7662
   refers to.  */
7663
static void
7664
free_line_header (struct line_header *lh)
7665
{
7666
  if (lh->standard_opcode_lengths)
7667
    xfree (lh->standard_opcode_lengths);
7668
 
7669
  /* Remember that all the lh->file_names[i].name pointers are
7670
     pointers into debug_line_buffer, and don't need to be freed.  */
7671
  if (lh->file_names)
7672
    xfree (lh->file_names);
7673
 
7674
  /* Similarly for the include directory names.  */
7675
  if (lh->include_dirs)
7676
    xfree (lh->include_dirs);
7677
 
7678
  xfree (lh);
7679
}
7680
 
7681
 
7682
/* Add an entry to LH's include directory table.  */
7683
static void
7684
add_include_dir (struct line_header *lh, char *include_dir)
7685
{
7686
  /* Grow the array if necessary.  */
7687
  if (lh->include_dirs_size == 0)
7688
    {
7689
      lh->include_dirs_size = 1; /* for testing */
7690
      lh->include_dirs = xmalloc (lh->include_dirs_size
7691
                                  * sizeof (*lh->include_dirs));
7692
    }
7693
  else if (lh->num_include_dirs >= lh->include_dirs_size)
7694
    {
7695
      lh->include_dirs_size *= 2;
7696
      lh->include_dirs = xrealloc (lh->include_dirs,
7697
                                   (lh->include_dirs_size
7698
                                    * sizeof (*lh->include_dirs)));
7699
    }
7700
 
7701
  lh->include_dirs[lh->num_include_dirs++] = include_dir;
7702
}
7703
 
7704
 
7705
/* Add an entry to LH's file name table.  */
7706
static void
7707
add_file_name (struct line_header *lh,
7708
               char *name,
7709
               unsigned int dir_index,
7710
               unsigned int mod_time,
7711
               unsigned int length)
7712
{
7713
  struct file_entry *fe;
7714
 
7715
  /* Grow the array if necessary.  */
7716
  if (lh->file_names_size == 0)
7717
    {
7718
      lh->file_names_size = 1; /* for testing */
7719
      lh->file_names = xmalloc (lh->file_names_size
7720
                                * sizeof (*lh->file_names));
7721
    }
7722
  else if (lh->num_file_names >= lh->file_names_size)
7723
    {
7724
      lh->file_names_size *= 2;
7725
      lh->file_names = xrealloc (lh->file_names,
7726
                                 (lh->file_names_size
7727
                                  * sizeof (*lh->file_names)));
7728
    }
7729
 
7730
  fe = &lh->file_names[lh->num_file_names++];
7731
  fe->name = name;
7732
  fe->dir_index = dir_index;
7733
  fe->mod_time = mod_time;
7734
  fe->length = length;
7735
  fe->included_p = 0;
7736
  fe->symtab = NULL;
7737
}
7738
 
7739
 
7740
/* Read the statement program header starting at OFFSET in
7741
   .debug_line, according to the endianness of ABFD.  Return a pointer
7742
   to a struct line_header, allocated using xmalloc.
7743
 
7744
   NOTE: the strings in the include directory and file name tables of
7745
   the returned object point into debug_line_buffer, and must not be
7746
   freed.  */
7747
static struct line_header *
7748
dwarf_decode_line_header (unsigned int offset, bfd *abfd,
7749
                          struct dwarf2_cu *cu)
7750
{
7751
  struct cleanup *back_to;
7752
  struct line_header *lh;
7753
  gdb_byte *line_ptr;
7754
  unsigned int bytes_read, offset_size;
7755
  int i;
7756
  char *cur_dir, *cur_file;
7757
 
7758
  if (dwarf2_per_objfile->line.buffer == NULL)
7759
    {
7760
      complaint (&symfile_complaints, _("missing .debug_line section"));
7761
      return 0;
7762
    }
7763
 
7764
  /* Make sure that at least there's room for the total_length field.
7765
     That could be 12 bytes long, but we're just going to fudge that.  */
7766
  if (offset + 4 >= dwarf2_per_objfile->line.size)
7767
    {
7768
      dwarf2_statement_list_fits_in_line_number_section_complaint ();
7769
      return 0;
7770
    }
7771
 
7772
  lh = xmalloc (sizeof (*lh));
7773
  memset (lh, 0, sizeof (*lh));
7774
  back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
7775
                          (void *) lh);
7776
 
7777
  line_ptr = dwarf2_per_objfile->line.buffer + offset;
7778
 
7779
  /* Read in the header.  */
7780
  lh->total_length =
7781
    read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
7782
                                            &bytes_read, &offset_size);
7783
  line_ptr += bytes_read;
7784
  if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
7785
                                     + dwarf2_per_objfile->line.size))
7786
    {
7787
      dwarf2_statement_list_fits_in_line_number_section_complaint ();
7788
      return 0;
7789
    }
7790
  lh->statement_program_end = line_ptr + lh->total_length;
7791
  lh->version = read_2_bytes (abfd, line_ptr);
7792
  line_ptr += 2;
7793
  lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
7794
  line_ptr += offset_size;
7795
  lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
7796
  line_ptr += 1;
7797
  lh->default_is_stmt = read_1_byte (abfd, line_ptr);
7798
  line_ptr += 1;
7799
  lh->line_base = read_1_signed_byte (abfd, line_ptr);
7800
  line_ptr += 1;
7801
  lh->line_range = read_1_byte (abfd, line_ptr);
7802
  line_ptr += 1;
7803
  lh->opcode_base = read_1_byte (abfd, line_ptr);
7804
  line_ptr += 1;
7805
  lh->standard_opcode_lengths
7806
    = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
7807
 
7808
  lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
7809
  for (i = 1; i < lh->opcode_base; ++i)
7810
    {
7811
      lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
7812
      line_ptr += 1;
7813
    }
7814
 
7815
  /* Read directory table.  */
7816
  while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7817
    {
7818
      line_ptr += bytes_read;
7819
      add_include_dir (lh, cur_dir);
7820
    }
7821
  line_ptr += bytes_read;
7822
 
7823
  /* Read file name table.  */
7824
  while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
7825
    {
7826
      unsigned int dir_index, mod_time, length;
7827
 
7828
      line_ptr += bytes_read;
7829
      dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7830
      line_ptr += bytes_read;
7831
      mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7832
      line_ptr += bytes_read;
7833
      length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
7834
      line_ptr += bytes_read;
7835
 
7836
      add_file_name (lh, cur_file, dir_index, mod_time, length);
7837
    }
7838
  line_ptr += bytes_read;
7839
  lh->statement_program_start = line_ptr;
7840
 
7841
  if (line_ptr > (dwarf2_per_objfile->line.buffer
7842
                  + dwarf2_per_objfile->line.size))
7843
    complaint (&symfile_complaints,
7844
               _("line number info header doesn't fit in `.debug_line' section"));
7845
 
7846
  discard_cleanups (back_to);
7847
  return lh;
7848
}
7849
 
7850
/* This function exists to work around a bug in certain compilers
7851
   (particularly GCC 2.95), in which the first line number marker of a
7852
   function does not show up until after the prologue, right before
7853
   the second line number marker.  This function shifts ADDRESS down
7854
   to the beginning of the function if necessary, and is called on
7855
   addresses passed to record_line.  */
7856
 
7857
static CORE_ADDR
7858
check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
7859
{
7860
  struct function_range *fn;
7861
 
7862
  /* Find the function_range containing address.  */
7863
  if (!cu->first_fn)
7864
    return address;
7865
 
7866
  if (!cu->cached_fn)
7867
    cu->cached_fn = cu->first_fn;
7868
 
7869
  fn = cu->cached_fn;
7870
  while (fn)
7871
    if (fn->lowpc <= address && fn->highpc > address)
7872
      goto found;
7873
    else
7874
      fn = fn->next;
7875
 
7876
  fn = cu->first_fn;
7877
  while (fn && fn != cu->cached_fn)
7878
    if (fn->lowpc <= address && fn->highpc > address)
7879
      goto found;
7880
    else
7881
      fn = fn->next;
7882
 
7883
  return address;
7884
 
7885
 found:
7886
  if (fn->seen_line)
7887
    return address;
7888
  if (address != fn->lowpc)
7889
    complaint (&symfile_complaints,
7890
               _("misplaced first line number at 0x%lx for '%s'"),
7891
               (unsigned long) address, fn->name);
7892
  fn->seen_line = 1;
7893
  return fn->lowpc;
7894
}
7895
 
7896
/* Decode the Line Number Program (LNP) for the given line_header
7897
   structure and CU.  The actual information extracted and the type
7898
   of structures created from the LNP depends on the value of PST.
7899
 
7900
   1. If PST is NULL, then this procedure uses the data from the program
7901
      to create all necessary symbol tables, and their linetables.
7902
      The compilation directory of the file is passed in COMP_DIR,
7903
      and must not be NULL.
7904
 
7905
   2. If PST is not NULL, this procedure reads the program to determine
7906
      the list of files included by the unit represented by PST, and
7907
      builds all the associated partial symbol tables.  In this case,
7908
      the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
7909
      is not used to compute the full name of the symtab, and therefore
7910
      omitting it when building the partial symtab does not introduce
7911
      the potential for inconsistency - a partial symtab and its associated
7912
      symbtab having a different fullname -).  */
7913
 
7914
static void
7915
dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
7916
                    struct dwarf2_cu *cu, struct partial_symtab *pst)
7917
{
7918
  gdb_byte *line_ptr, *extended_end;
7919
  gdb_byte *line_end;
7920
  unsigned int bytes_read, extended_len;
7921
  unsigned char op_code, extended_op, adj_opcode;
7922
  CORE_ADDR baseaddr;
7923
  struct objfile *objfile = cu->objfile;
7924
  struct gdbarch *gdbarch = get_objfile_arch (objfile);
7925
  const int decode_for_pst_p = (pst != NULL);
7926
  struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
7927
 
7928
  baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7929
 
7930
  line_ptr = lh->statement_program_start;
7931
  line_end = lh->statement_program_end;
7932
 
7933
  /* Read the statement sequences until there's nothing left.  */
7934
  while (line_ptr < line_end)
7935
    {
7936
      /* state machine registers  */
7937
      CORE_ADDR address = 0;
7938
      unsigned int file = 1;
7939
      unsigned int line = 1;
7940
      unsigned int column = 0;
7941
      int is_stmt = lh->default_is_stmt;
7942
      int basic_block = 0;
7943
      int end_sequence = 0;
7944
      CORE_ADDR addr;
7945
 
7946
      if (!decode_for_pst_p && lh->num_file_names >= file)
7947
        {
7948
          /* Start a subfile for the current file of the state machine.  */
7949
          /* lh->include_dirs and lh->file_names are 0-based, but the
7950
             directory and file name numbers in the statement program
7951
             are 1-based.  */
7952
          struct file_entry *fe = &lh->file_names[file - 1];
7953
          char *dir = NULL;
7954
 
7955
          if (fe->dir_index)
7956
            dir = lh->include_dirs[fe->dir_index - 1];
7957
 
7958
          dwarf2_start_subfile (fe->name, dir, comp_dir);
7959
        }
7960
 
7961
      /* Decode the table.  */
7962
      while (!end_sequence)
7963
        {
7964
          op_code = read_1_byte (abfd, line_ptr);
7965
          line_ptr += 1;
7966
          if (line_ptr > line_end)
7967
            {
7968
              dwarf2_debug_line_missing_end_sequence_complaint ();
7969
              break;
7970
            }
7971
 
7972
          if (op_code >= lh->opcode_base)
7973
            {
7974
              /* Special operand.  */
7975
              adj_opcode = op_code - lh->opcode_base;
7976
              address += (adj_opcode / lh->line_range)
7977
                * lh->minimum_instruction_length;
7978
              line += lh->line_base + (adj_opcode % lh->line_range);
7979
              if (lh->num_file_names < file || file == 0)
7980
                dwarf2_debug_line_missing_file_complaint ();
7981
              else
7982
                {
7983
                  lh->file_names[file - 1].included_p = 1;
7984
                  if (!decode_for_pst_p && is_stmt)
7985
                    {
7986
                      if (last_subfile != current_subfile)
7987
                        {
7988
                          addr = gdbarch_addr_bits_remove (gdbarch, address);
7989
                          if (last_subfile)
7990
                            record_line (last_subfile, 0, addr);
7991
                          last_subfile = current_subfile;
7992
                        }
7993
                      /* Append row to matrix using current values.  */
7994
                      addr = check_cu_functions (address, cu);
7995
                      addr = gdbarch_addr_bits_remove (gdbarch, addr);
7996
                      record_line (current_subfile, line, addr);
7997
                    }
7998
                }
7999
              basic_block = 0;
8000
            }
8001
          else switch (op_code)
8002
            {
8003
            case DW_LNS_extended_op:
8004
              extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8005
              line_ptr += bytes_read;
8006
              extended_end = line_ptr + extended_len;
8007
              extended_op = read_1_byte (abfd, line_ptr);
8008
              line_ptr += 1;
8009
              switch (extended_op)
8010
                {
8011
                case DW_LNE_end_sequence:
8012
                  end_sequence = 1;
8013
                  break;
8014
                case DW_LNE_set_address:
8015
                  address = read_address (abfd, line_ptr, cu, &bytes_read);
8016
                  line_ptr += bytes_read;
8017
                  address += baseaddr;
8018
                  break;
8019
                case DW_LNE_define_file:
8020
                  {
8021
                    char *cur_file;
8022
                    unsigned int dir_index, mod_time, length;
8023
 
8024
                    cur_file = read_string (abfd, line_ptr, &bytes_read);
8025
                    line_ptr += bytes_read;
8026
                    dir_index =
8027
                      read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8028
                    line_ptr += bytes_read;
8029
                    mod_time =
8030
                      read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8031
                    line_ptr += bytes_read;
8032
                    length =
8033
                      read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8034
                    line_ptr += bytes_read;
8035
                    add_file_name (lh, cur_file, dir_index, mod_time, length);
8036
                  }
8037
                  break;
8038
                case DW_LNE_set_discriminator:
8039
                  /* The discriminator is not interesting to the debugger;
8040
                     just ignore it.  */
8041
                  line_ptr = extended_end;
8042
                  break;
8043
                default:
8044
                  complaint (&symfile_complaints,
8045
                             _("mangled .debug_line section"));
8046
                  return;
8047
                }
8048
              /* Make sure that we parsed the extended op correctly.  If e.g.
8049
                 we expected a different address size than the producer used,
8050
                 we may have read the wrong number of bytes.  */
8051
              if (line_ptr != extended_end)
8052
                {
8053
                  complaint (&symfile_complaints,
8054
                             _("mangled .debug_line section"));
8055
                  return;
8056
                }
8057
              break;
8058
            case DW_LNS_copy:
8059
              if (lh->num_file_names < file || file == 0)
8060
                dwarf2_debug_line_missing_file_complaint ();
8061
              else
8062
                {
8063
                  lh->file_names[file - 1].included_p = 1;
8064
                  if (!decode_for_pst_p && is_stmt)
8065
                    {
8066
                      if (last_subfile != current_subfile)
8067
                        {
8068
                          addr = gdbarch_addr_bits_remove (gdbarch, address);
8069
                          if (last_subfile)
8070
                            record_line (last_subfile, 0, addr);
8071
                          last_subfile = current_subfile;
8072
                        }
8073
                      addr = check_cu_functions (address, cu);
8074
                      addr = gdbarch_addr_bits_remove (gdbarch, addr);
8075
                      record_line (current_subfile, line, addr);
8076
                    }
8077
                }
8078
              basic_block = 0;
8079
              break;
8080
            case DW_LNS_advance_pc:
8081
              address += lh->minimum_instruction_length
8082
                * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8083
              line_ptr += bytes_read;
8084
              break;
8085
            case DW_LNS_advance_line:
8086
              line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
8087
              line_ptr += bytes_read;
8088
              break;
8089
            case DW_LNS_set_file:
8090
              {
8091
                /* The arrays lh->include_dirs and lh->file_names are
8092
                   0-based, but the directory and file name numbers in
8093
                   the statement program are 1-based.  */
8094
                struct file_entry *fe;
8095
                char *dir = NULL;
8096
 
8097
                file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8098
                line_ptr += bytes_read;
8099
                if (lh->num_file_names < file || file == 0)
8100
                  dwarf2_debug_line_missing_file_complaint ();
8101
                else
8102
                  {
8103
                    fe = &lh->file_names[file - 1];
8104
                    if (fe->dir_index)
8105
                      dir = lh->include_dirs[fe->dir_index - 1];
8106
                    if (!decode_for_pst_p)
8107
                      {
8108
                        last_subfile = current_subfile;
8109
                        dwarf2_start_subfile (fe->name, dir, comp_dir);
8110
                      }
8111
                  }
8112
              }
8113
              break;
8114
            case DW_LNS_set_column:
8115
              column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8116
              line_ptr += bytes_read;
8117
              break;
8118
            case DW_LNS_negate_stmt:
8119
              is_stmt = (!is_stmt);
8120
              break;
8121
            case DW_LNS_set_basic_block:
8122
              basic_block = 1;
8123
              break;
8124
            /* Add to the address register of the state machine the
8125
               address increment value corresponding to special opcode
8126
               255.  I.e., this value is scaled by the minimum
8127
               instruction length since special opcode 255 would have
8128
               scaled the the increment.  */
8129
            case DW_LNS_const_add_pc:
8130
              address += (lh->minimum_instruction_length
8131
                          * ((255 - lh->opcode_base) / lh->line_range));
8132
              break;
8133
            case DW_LNS_fixed_advance_pc:
8134
              address += read_2_bytes (abfd, line_ptr);
8135
              line_ptr += 2;
8136
              break;
8137
            default:
8138
              {
8139
                /* Unknown standard opcode, ignore it.  */
8140
                int i;
8141
 
8142
                for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
8143
                  {
8144
                    (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
8145
                    line_ptr += bytes_read;
8146
                  }
8147
              }
8148
            }
8149
        }
8150
      if (lh->num_file_names < file || file == 0)
8151
        dwarf2_debug_line_missing_file_complaint ();
8152
      else
8153
        {
8154
          lh->file_names[file - 1].included_p = 1;
8155
          if (!decode_for_pst_p)
8156
            {
8157
              addr = gdbarch_addr_bits_remove (gdbarch, address);
8158
              record_line (current_subfile, 0, addr);
8159
            }
8160
        }
8161
    }
8162
 
8163
  if (decode_for_pst_p)
8164
    {
8165
      int file_index;
8166
 
8167
      /* Now that we're done scanning the Line Header Program, we can
8168
         create the psymtab of each included file.  */
8169
      for (file_index = 0; file_index < lh->num_file_names; file_index++)
8170
        if (lh->file_names[file_index].included_p == 1)
8171
          {
8172
            const struct file_entry fe = lh->file_names [file_index];
8173
            char *include_name = fe.name;
8174
            char *dir_name = NULL;
8175
            char *pst_filename = pst->filename;
8176
 
8177
            if (fe.dir_index)
8178
              dir_name = lh->include_dirs[fe.dir_index - 1];
8179
 
8180
            if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
8181
              {
8182
                include_name = concat (dir_name, SLASH_STRING,
8183
                                       include_name, (char *)NULL);
8184
                make_cleanup (xfree, include_name);
8185
              }
8186
 
8187
            if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
8188
              {
8189
                pst_filename = concat (pst->dirname, SLASH_STRING,
8190
                                       pst_filename, (char *)NULL);
8191
                make_cleanup (xfree, pst_filename);
8192
              }
8193
 
8194
            if (strcmp (include_name, pst_filename) != 0)
8195
              dwarf2_create_include_psymtab (include_name, pst, objfile);
8196
          }
8197
    }
8198
  else
8199
    {
8200
      /* Make sure a symtab is created for every file, even files
8201
         which contain only variables (i.e. no code with associated
8202
         line numbers).  */
8203
 
8204
      int i;
8205
      struct file_entry *fe;
8206
 
8207
      for (i = 0; i < lh->num_file_names; i++)
8208
        {
8209
          char *dir = NULL;
8210
          fe = &lh->file_names[i];
8211
          if (fe->dir_index)
8212
            dir = lh->include_dirs[fe->dir_index - 1];
8213
          dwarf2_start_subfile (fe->name, dir, comp_dir);
8214
 
8215
          /* Skip the main file; we don't need it, and it must be
8216
             allocated last, so that it will show up before the
8217
             non-primary symtabs in the objfile's symtab list.  */
8218
          if (current_subfile == first_subfile)
8219
            continue;
8220
 
8221
          if (current_subfile->symtab == NULL)
8222
            current_subfile->symtab = allocate_symtab (current_subfile->name,
8223
                                                       cu->objfile);
8224
          fe->symtab = current_subfile->symtab;
8225
        }
8226
    }
8227
}
8228
 
8229
/* Start a subfile for DWARF.  FILENAME is the name of the file and
8230
   DIRNAME the name of the source directory which contains FILENAME
8231
   or NULL if not known.  COMP_DIR is the compilation directory for the
8232
   linetable's compilation unit or NULL if not known.
8233
   This routine tries to keep line numbers from identical absolute and
8234
   relative file names in a common subfile.
8235
 
8236
   Using the `list' example from the GDB testsuite, which resides in
8237
   /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
8238
   of /srcdir/list0.c yields the following debugging information for list0.c:
8239
 
8240
   DW_AT_name:          /srcdir/list0.c
8241
   DW_AT_comp_dir:              /compdir
8242
   files.files[0].name: list0.h
8243
   files.files[0].dir:  /srcdir
8244
   files.files[1].name: list0.c
8245
   files.files[1].dir:  /srcdir
8246
 
8247
   The line number information for list0.c has to end up in a single
8248
   subfile, so that `break /srcdir/list0.c:1' works as expected.
8249
   start_subfile will ensure that this happens provided that we pass the
8250
   concatenation of files.files[1].dir and files.files[1].name as the
8251
   subfile's name.  */
8252
 
8253
static void
8254
dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
8255
{
8256
  char *fullname;
8257
 
8258
  /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
8259
     `start_symtab' will always pass the contents of DW_AT_comp_dir as
8260
     second argument to start_subfile.  To be consistent, we do the
8261
     same here.  In order not to lose the line information directory,
8262
     we concatenate it to the filename when it makes sense.
8263
     Note that the Dwarf3 standard says (speaking of filenames in line
8264
     information): ``The directory index is ignored for file names
8265
     that represent full path names''.  Thus ignoring dirname in the
8266
     `else' branch below isn't an issue.  */
8267
 
8268
  if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
8269
    fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
8270
  else
8271
    fullname = filename;
8272
 
8273
  start_subfile (fullname, comp_dir);
8274
 
8275
  if (fullname != filename)
8276
    xfree (fullname);
8277
}
8278
 
8279
static void
8280
var_decode_location (struct attribute *attr, struct symbol *sym,
8281
                     struct dwarf2_cu *cu)
8282
{
8283
  struct objfile *objfile = cu->objfile;
8284
  struct comp_unit_head *cu_header = &cu->header;
8285
 
8286
  /* NOTE drow/2003-01-30: There used to be a comment and some special
8287
     code here to turn a symbol with DW_AT_external and a
8288
     SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
8289
     necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
8290
     with some versions of binutils) where shared libraries could have
8291
     relocations against symbols in their debug information - the
8292
     minimal symbol would have the right address, but the debug info
8293
     would not.  It's no longer necessary, because we will explicitly
8294
     apply relocations when we read in the debug information now.  */
8295
 
8296
  /* A DW_AT_location attribute with no contents indicates that a
8297
     variable has been optimized away.  */
8298
  if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
8299
    {
8300
      SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
8301
      return;
8302
    }
8303
 
8304
  /* Handle one degenerate form of location expression specially, to
8305
     preserve GDB's previous behavior when section offsets are
8306
     specified.  If this is just a DW_OP_addr then mark this symbol
8307
     as LOC_STATIC.  */
8308
 
8309
  if (attr_form_is_block (attr)
8310
      && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
8311
      && DW_BLOCK (attr)->data[0] == DW_OP_addr)
8312
    {
8313
      unsigned int dummy;
8314
 
8315
      SYMBOL_VALUE_ADDRESS (sym) =
8316
        read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
8317
      SYMBOL_CLASS (sym) = LOC_STATIC;
8318
      fixup_symbol_section (sym, objfile);
8319
      SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
8320
                                              SYMBOL_SECTION (sym));
8321
      return;
8322
    }
8323
 
8324
  /* NOTE drow/2002-01-30: It might be worthwhile to have a static
8325
     expression evaluator, and use LOC_COMPUTED only when necessary
8326
     (i.e. when the value of a register or memory location is
8327
     referenced, or a thread-local block, etc.).  Then again, it might
8328
     not be worthwhile.  I'm assuming that it isn't unless performance
8329
     or memory numbers show me otherwise.  */
8330
 
8331
  dwarf2_symbol_mark_computed (attr, sym, cu);
8332
  SYMBOL_CLASS (sym) = LOC_COMPUTED;
8333
}
8334
 
8335
/* Given a pointer to a DWARF information entry, figure out if we need
8336
   to make a symbol table entry for it, and if so, create a new entry
8337
   and return a pointer to it.
8338
   If TYPE is NULL, determine symbol type from the die, otherwise
8339
   used the passed type.  */
8340
 
8341
static struct symbol *
8342
new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
8343
{
8344
  struct objfile *objfile = cu->objfile;
8345
  struct symbol *sym = NULL;
8346
  char *name;
8347
  struct attribute *attr = NULL;
8348
  struct attribute *attr2 = NULL;
8349
  CORE_ADDR baseaddr;
8350
  int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
8351
 
8352
  baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8353
 
8354
  if (die->tag != DW_TAG_namespace)
8355
    name = dwarf2_linkage_name (die, cu);
8356
  else
8357
    name = TYPE_NAME (type);
8358
 
8359
  if (name)
8360
    {
8361
      sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
8362
                                             sizeof (struct symbol));
8363
      OBJSTAT (objfile, n_syms++);
8364
      memset (sym, 0, sizeof (struct symbol));
8365
 
8366
      /* Cache this symbol's name and the name's demangled form (if any).  */
8367
      SYMBOL_LANGUAGE (sym) = cu->language;
8368
      SYMBOL_SET_NAMES (sym, name, strlen (name), 0, objfile);
8369
 
8370
      /* Default assumptions.
8371
         Use the passed type or decode it from the die.  */
8372
      SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
8373
      SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
8374
      if (type != NULL)
8375
        SYMBOL_TYPE (sym) = type;
8376
      else
8377
        SYMBOL_TYPE (sym) = die_type (die, cu);
8378
      attr = dwarf2_attr (die,
8379
                          inlined_func ? DW_AT_call_line : DW_AT_decl_line,
8380
                          cu);
8381
      if (attr)
8382
        {
8383
          SYMBOL_LINE (sym) = DW_UNSND (attr);
8384
        }
8385
 
8386
      attr = dwarf2_attr (die,
8387
                          inlined_func ? DW_AT_call_file : DW_AT_decl_file,
8388
                          cu);
8389
      if (attr)
8390
        {
8391
          int file_index = DW_UNSND (attr);
8392
          if (cu->line_header == NULL
8393
              || file_index > cu->line_header->num_file_names)
8394
            complaint (&symfile_complaints,
8395
                       _("file index out of range"));
8396
          else if (file_index > 0)
8397
            {
8398
              struct file_entry *fe;
8399
              fe = &cu->line_header->file_names[file_index - 1];
8400
              SYMBOL_SYMTAB (sym) = fe->symtab;
8401
            }
8402
        }
8403
 
8404
      switch (die->tag)
8405
        {
8406
        case DW_TAG_label:
8407
          attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8408
          if (attr)
8409
            {
8410
              SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
8411
            }
8412
          SYMBOL_CLASS (sym) = LOC_LABEL;
8413
          break;
8414
        case DW_TAG_subprogram:
8415
          /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
8416
             finish_block.  */
8417
          SYMBOL_CLASS (sym) = LOC_BLOCK;
8418
          attr2 = dwarf2_attr (die, DW_AT_external, cu);
8419
          if ((attr2 && (DW_UNSND (attr2) != 0))
8420
              || cu->language == language_ada)
8421
            {
8422
              /* Subprograms marked external are stored as a global symbol.
8423
                 Ada subprograms, whether marked external or not, are always
8424
                 stored as a global symbol, because we want to be able to
8425
                 access them globally.  For instance, we want to be able
8426
                 to break on a nested subprogram without having to
8427
                 specify the context.  */
8428
              add_symbol_to_list (sym, &global_symbols);
8429
            }
8430
          else
8431
            {
8432
              add_symbol_to_list (sym, cu->list_in_scope);
8433
            }
8434
          break;
8435
        case DW_TAG_inlined_subroutine:
8436
          /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
8437
             finish_block.  */
8438
          SYMBOL_CLASS (sym) = LOC_BLOCK;
8439
          SYMBOL_INLINED (sym) = 1;
8440
          /* Do not add the symbol to any lists.  It will be found via
8441
             BLOCK_FUNCTION from the blockvector.  */
8442
          break;
8443
        case DW_TAG_variable:
8444
          /* Compilation with minimal debug info may result in variables
8445
             with missing type entries. Change the misleading `void' type
8446
             to something sensible.  */
8447
          if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
8448
            SYMBOL_TYPE (sym)
8449
              = objfile_type (objfile)->nodebug_data_symbol;
8450
 
8451
          attr = dwarf2_attr (die, DW_AT_const_value, cu);
8452
          if (attr)
8453
            {
8454
              dwarf2_const_value (attr, sym, cu);
8455
              attr2 = dwarf2_attr (die, DW_AT_external, cu);
8456
              if (attr2 && (DW_UNSND (attr2) != 0))
8457
                add_symbol_to_list (sym, &global_symbols);
8458
              else
8459
                add_symbol_to_list (sym, cu->list_in_scope);
8460
              break;
8461
            }
8462
          attr = dwarf2_attr (die, DW_AT_location, cu);
8463
          if (attr)
8464
            {
8465
              var_decode_location (attr, sym, cu);
8466
              attr2 = dwarf2_attr (die, DW_AT_external, cu);
8467
              if (attr2 && (DW_UNSND (attr2) != 0))
8468
                add_symbol_to_list (sym, &global_symbols);
8469
              else
8470
                add_symbol_to_list (sym, cu->list_in_scope);
8471
            }
8472
          else
8473
            {
8474
              /* We do not know the address of this symbol.
8475
                 If it is an external symbol and we have type information
8476
                 for it, enter the symbol as a LOC_UNRESOLVED symbol.
8477
                 The address of the variable will then be determined from
8478
                 the minimal symbol table whenever the variable is
8479
                 referenced.  */
8480
              attr2 = dwarf2_attr (die, DW_AT_external, cu);
8481
              if (attr2 && (DW_UNSND (attr2) != 0)
8482
                  && dwarf2_attr (die, DW_AT_type, cu) != NULL)
8483
                {
8484
                  struct pending **list_to_add;
8485
 
8486
                  /* A variable with DW_AT_external is never static, but it
8487
                     may be block-scoped.  */
8488
                  list_to_add = (cu->list_in_scope == &file_symbols
8489
                                 ? &global_symbols : cu->list_in_scope);
8490
 
8491
                  SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
8492
                  add_symbol_to_list (sym, list_to_add);
8493
                }
8494
              else if (!die_is_declaration (die, cu))
8495
                {
8496
                  /* Use the default LOC_OPTIMIZED_OUT class.  */
8497
                  gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
8498
                  add_symbol_to_list (sym, cu->list_in_scope);
8499
                }
8500
            }
8501
          break;
8502
        case DW_TAG_formal_parameter:
8503
          /* If we are inside a function, mark this as an argument.  If
8504
             not, we might be looking at an argument to an inlined function
8505
             when we do not have enough information to show inlined frames;
8506
             pretend it's a local variable in that case so that the user can
8507
             still see it.  */
8508
          if (context_stack_depth > 0
8509
              && context_stack[context_stack_depth - 1].name != NULL)
8510
            SYMBOL_IS_ARGUMENT (sym) = 1;
8511
          attr = dwarf2_attr (die, DW_AT_location, cu);
8512
          if (attr)
8513
            {
8514
              var_decode_location (attr, sym, cu);
8515
            }
8516
          attr = dwarf2_attr (die, DW_AT_const_value, cu);
8517
          if (attr)
8518
            {
8519
              dwarf2_const_value (attr, sym, cu);
8520
            }
8521
          add_symbol_to_list (sym, cu->list_in_scope);
8522
          break;
8523
        case DW_TAG_unspecified_parameters:
8524
          /* From varargs functions; gdb doesn't seem to have any
8525
             interest in this information, so just ignore it for now.
8526
             (FIXME?) */
8527
          break;
8528
        case DW_TAG_class_type:
8529
        case DW_TAG_interface_type:
8530
        case DW_TAG_structure_type:
8531
        case DW_TAG_union_type:
8532
        case DW_TAG_set_type:
8533
        case DW_TAG_enumeration_type:
8534
          SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8535
          SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
8536
 
8537
          /* Make sure that the symbol includes appropriate enclosing
8538
             classes/namespaces in its name.  These are calculated in
8539
             read_structure_type, and the correct name is saved in
8540
             the type.  */
8541
 
8542
          if (cu->language == language_cplus
8543
              || cu->language == language_java)
8544
            {
8545
              struct type *type = SYMBOL_TYPE (sym);
8546
 
8547
              if (TYPE_TAG_NAME (type) != NULL)
8548
                {
8549
                  /* FIXME: carlton/2003-11-10: Should this use
8550
                     SYMBOL_SET_NAMES instead?  (The same problem also
8551
                     arises further down in this function.)  */
8552
                  /* The type's name is already allocated along with
8553
                     this objfile, so we don't need to duplicate it
8554
                     for the symbol.  */
8555
                  SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
8556
                }
8557
            }
8558
 
8559
          {
8560
            /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
8561
               really ever be static objects: otherwise, if you try
8562
               to, say, break of a class's method and you're in a file
8563
               which doesn't mention that class, it won't work unless
8564
               the check for all static symbols in lookup_symbol_aux
8565
               saves you.  See the OtherFileClass tests in
8566
               gdb.c++/namespace.exp.  */
8567
 
8568
            struct pending **list_to_add;
8569
 
8570
            list_to_add = (cu->list_in_scope == &file_symbols
8571
                           && (cu->language == language_cplus
8572
                               || cu->language == language_java)
8573
                           ? &global_symbols : cu->list_in_scope);
8574
 
8575
            add_symbol_to_list (sym, list_to_add);
8576
 
8577
            /* The semantics of C++ state that "struct foo { ... }" also
8578
               defines a typedef for "foo".  A Java class declaration also
8579
               defines a typedef for the class.  */
8580
            if (cu->language == language_cplus
8581
                || cu->language == language_java
8582
                || cu->language == language_ada)
8583
              {
8584
                /* The symbol's name is already allocated along with
8585
                   this objfile, so we don't need to duplicate it for
8586
                   the type.  */
8587
                if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
8588
                  TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
8589
              }
8590
          }
8591
          break;
8592
        case DW_TAG_typedef:
8593
          SYMBOL_LINKAGE_NAME (sym) = (char *) dwarf2_full_name (die, cu);
8594
          SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8595
          SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
8596
          add_symbol_to_list (sym, cu->list_in_scope);
8597
          break;
8598
        case DW_TAG_base_type:
8599
        case DW_TAG_subrange_type:
8600
          SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8601
          SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
8602
          add_symbol_to_list (sym, cu->list_in_scope);
8603
          break;
8604
        case DW_TAG_enumerator:
8605
          SYMBOL_LINKAGE_NAME (sym) = (char *) dwarf2_full_name (die, cu);
8606
          attr = dwarf2_attr (die, DW_AT_const_value, cu);
8607
          if (attr)
8608
            {
8609
              dwarf2_const_value (attr, sym, cu);
8610
            }
8611
          {
8612
            /* NOTE: carlton/2003-11-10: See comment above in the
8613
               DW_TAG_class_type, etc. block.  */
8614
 
8615
            struct pending **list_to_add;
8616
 
8617
            list_to_add = (cu->list_in_scope == &file_symbols
8618
                           && (cu->language == language_cplus
8619
                               || cu->language == language_java)
8620
                           ? &global_symbols : cu->list_in_scope);
8621
 
8622
            add_symbol_to_list (sym, list_to_add);
8623
          }
8624
          break;
8625
        case DW_TAG_namespace:
8626
          SYMBOL_CLASS (sym) = LOC_TYPEDEF;
8627
          add_symbol_to_list (sym, &global_symbols);
8628
          break;
8629
        default:
8630
          /* Not a tag we recognize.  Hopefully we aren't processing
8631
             trash data, but since we must specifically ignore things
8632
             we don't recognize, there is nothing else we should do at
8633
             this point. */
8634
          complaint (&symfile_complaints, _("unsupported tag: '%s'"),
8635
                     dwarf_tag_name (die->tag));
8636
          break;
8637
        }
8638
 
8639
      /* For the benefit of old versions of GCC, check for anonymous
8640
         namespaces based on the demangled name.  */
8641
      if (!processing_has_namespace_info
8642
          && cu->language == language_cplus
8643
          && dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu) != NULL)
8644
        cp_scan_for_anonymous_namespaces (sym);
8645
    }
8646
  return (sym);
8647
}
8648
 
8649
/* Copy constant value from an attribute to a symbol.  */
8650
 
8651
static void
8652
dwarf2_const_value (struct attribute *attr, struct symbol *sym,
8653
                    struct dwarf2_cu *cu)
8654
{
8655
  struct objfile *objfile = cu->objfile;
8656
  struct comp_unit_head *cu_header = &cu->header;
8657
  enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
8658
                                BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
8659
  struct dwarf_block *blk;
8660
 
8661
  switch (attr->form)
8662
    {
8663
    case DW_FORM_addr:
8664
      if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
8665
        dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
8666
                                                      cu_header->addr_size,
8667
                                                      TYPE_LENGTH (SYMBOL_TYPE
8668
                                                                   (sym)));
8669
      SYMBOL_VALUE_BYTES (sym) =
8670
        obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
8671
      /* NOTE: cagney/2003-05-09: In-lined store_address call with
8672
         it's body - store_unsigned_integer.  */
8673
      store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
8674
                              byte_order, DW_ADDR (attr));
8675
      SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8676
      break;
8677
    case DW_FORM_string:
8678
    case DW_FORM_strp:
8679
      /* DW_STRING is already allocated on the obstack, point directly
8680
         to it.  */
8681
      SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
8682
      SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8683
      break;
8684
    case DW_FORM_block1:
8685
    case DW_FORM_block2:
8686
    case DW_FORM_block4:
8687
    case DW_FORM_block:
8688
      blk = DW_BLOCK (attr);
8689
      if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
8690
        dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
8691
                                                      blk->size,
8692
                                                      TYPE_LENGTH (SYMBOL_TYPE
8693
                                                                   (sym)));
8694
      SYMBOL_VALUE_BYTES (sym) =
8695
        obstack_alloc (&objfile->objfile_obstack, blk->size);
8696
      memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
8697
      SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
8698
      break;
8699
 
8700
      /* The DW_AT_const_value attributes are supposed to carry the
8701
         symbol's value "represented as it would be on the target
8702
         architecture."  By the time we get here, it's already been
8703
         converted to host endianness, so we just need to sign- or
8704
         zero-extend it as appropriate.  */
8705
    case DW_FORM_data1:
8706
      dwarf2_const_value_data (attr, sym, 8);
8707
      break;
8708
    case DW_FORM_data2:
8709
      dwarf2_const_value_data (attr, sym, 16);
8710
      break;
8711
    case DW_FORM_data4:
8712
      dwarf2_const_value_data (attr, sym, 32);
8713
      break;
8714
    case DW_FORM_data8:
8715
      dwarf2_const_value_data (attr, sym, 64);
8716
      break;
8717
 
8718
    case DW_FORM_sdata:
8719
      SYMBOL_VALUE (sym) = DW_SND (attr);
8720
      SYMBOL_CLASS (sym) = LOC_CONST;
8721
      break;
8722
 
8723
    case DW_FORM_udata:
8724
      SYMBOL_VALUE (sym) = DW_UNSND (attr);
8725
      SYMBOL_CLASS (sym) = LOC_CONST;
8726
      break;
8727
 
8728
    default:
8729
      complaint (&symfile_complaints,
8730
                 _("unsupported const value attribute form: '%s'"),
8731
                 dwarf_form_name (attr->form));
8732
      SYMBOL_VALUE (sym) = 0;
8733
      SYMBOL_CLASS (sym) = LOC_CONST;
8734
      break;
8735
    }
8736
}
8737
 
8738
 
8739
/* Given an attr with a DW_FORM_dataN value in host byte order, sign-
8740
   or zero-extend it as appropriate for the symbol's type.  */
8741
static void
8742
dwarf2_const_value_data (struct attribute *attr,
8743
                         struct symbol *sym,
8744
                         int bits)
8745
{
8746
  LONGEST l = DW_UNSND (attr);
8747
 
8748
  if (bits < sizeof (l) * 8)
8749
    {
8750
      if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
8751
        l &= ((LONGEST) 1 << bits) - 1;
8752
      else
8753
        l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
8754
    }
8755
 
8756
  SYMBOL_VALUE (sym) = l;
8757
  SYMBOL_CLASS (sym) = LOC_CONST;
8758
}
8759
 
8760
 
8761
/* Return the type of the die in question using its DW_AT_type attribute.  */
8762
 
8763
static struct type *
8764
die_type (struct die_info *die, struct dwarf2_cu *cu)
8765
{
8766
  struct type *type;
8767
  struct attribute *type_attr;
8768
  struct die_info *type_die;
8769
 
8770
  type_attr = dwarf2_attr (die, DW_AT_type, cu);
8771
  if (!type_attr)
8772
    {
8773
      /* A missing DW_AT_type represents a void type.  */
8774
      return objfile_type (cu->objfile)->builtin_void;
8775
    }
8776
 
8777
  type_die = follow_die_ref_or_sig (die, type_attr, &cu);
8778
 
8779
  type = tag_type_to_type (type_die, cu);
8780
  if (!type)
8781
    {
8782
      dump_die_for_error (type_die);
8783
      error (_("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]"),
8784
                      cu->objfile->name);
8785
    }
8786
  return type;
8787
}
8788
 
8789
/* True iff CU's producer generates GNAT Ada auxiliary information
8790
   that allows to find parallel types through that information instead
8791
   of having to do expensive parallel lookups by type name.  */
8792
 
8793
static int
8794
need_gnat_info (struct dwarf2_cu *cu)
8795
{
8796
  /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
8797
     of GNAT produces this auxiliary information, without any indication
8798
     that it is produced.  Part of enhancing the FSF version of GNAT
8799
     to produce that information will be to put in place an indicator
8800
     that we can use in order to determine whether the descriptive type
8801
     info is available or not.  One suggestion that has been made is
8802
     to use a new attribute, attached to the CU die.  For now, assume
8803
     that the descriptive type info is not available.  */
8804
  return 0;
8805
}
8806
 
8807
 
8808
/* Return the auxiliary type of the die in question using its
8809
   DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
8810
   attribute is not present.  */
8811
 
8812
static struct type *
8813
die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
8814
{
8815
  struct type *type;
8816
  struct attribute *type_attr;
8817
  struct die_info *type_die;
8818
 
8819
  type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
8820
  if (!type_attr)
8821
    return NULL;
8822
 
8823
  type_die = follow_die_ref (die, type_attr, &cu);
8824
  type = tag_type_to_type (type_die, cu);
8825
  if (!type)
8826
    {
8827
      dump_die_for_error (type_die);
8828
      error (_("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]"),
8829
                      cu->objfile->name);
8830
    }
8831
  return type;
8832
}
8833
 
8834
/* If DIE has a descriptive_type attribute, then set the TYPE's
8835
   descriptive type accordingly.  */
8836
 
8837
static void
8838
set_descriptive_type (struct type *type, struct die_info *die,
8839
                      struct dwarf2_cu *cu)
8840
{
8841
  struct type *descriptive_type = die_descriptive_type (die, cu);
8842
 
8843
  if (descriptive_type)
8844
    {
8845
      ALLOCATE_GNAT_AUX_TYPE (type);
8846
      TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
8847
    }
8848
}
8849
 
8850
/* Return the containing type of the die in question using its
8851
   DW_AT_containing_type attribute.  */
8852
 
8853
static struct type *
8854
die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
8855
{
8856
  struct type *type = NULL;
8857
  struct attribute *type_attr;
8858
  struct die_info *type_die = NULL;
8859
 
8860
  type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
8861
  if (type_attr)
8862
    {
8863
      type_die = follow_die_ref_or_sig (die, type_attr, &cu);
8864
      type = tag_type_to_type (type_die, cu);
8865
    }
8866
  if (!type)
8867
    {
8868
      if (type_die)
8869
        dump_die_for_error (type_die);
8870
      error (_("Dwarf Error: Problem turning containing type into gdb type [in module %s]"),
8871
                      cu->objfile->name);
8872
    }
8873
  return type;
8874
}
8875
 
8876
static struct type *
8877
tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
8878
{
8879
  struct type *this_type;
8880
 
8881
  this_type = read_type_die (die, cu);
8882
  if (!this_type)
8883
    {
8884
      dump_die_for_error (die);
8885
      error (_("Dwarf Error: Cannot find type of die [in module %s]"),
8886
             cu->objfile->name);
8887
    }
8888
  return this_type;
8889
}
8890
 
8891
static struct type *
8892
read_type_die (struct die_info *die, struct dwarf2_cu *cu)
8893
{
8894
  struct type *this_type;
8895
 
8896
  this_type = get_die_type (die, cu);
8897
  if (this_type)
8898
    return this_type;
8899
 
8900
  switch (die->tag)
8901
    {
8902
    case DW_TAG_class_type:
8903
    case DW_TAG_interface_type:
8904
    case DW_TAG_structure_type:
8905
    case DW_TAG_union_type:
8906
      this_type = read_structure_type (die, cu);
8907
      break;
8908
    case DW_TAG_enumeration_type:
8909
      this_type = read_enumeration_type (die, cu);
8910
      break;
8911
    case DW_TAG_subprogram:
8912
    case DW_TAG_subroutine_type:
8913
    case DW_TAG_inlined_subroutine:
8914
      this_type = read_subroutine_type (die, cu);
8915
      break;
8916
    case DW_TAG_array_type:
8917
      this_type = read_array_type (die, cu);
8918
      break;
8919
    case DW_TAG_set_type:
8920
      this_type = read_set_type (die, cu);
8921
      break;
8922
    case DW_TAG_pointer_type:
8923
      this_type = read_tag_pointer_type (die, cu);
8924
      break;
8925
    case DW_TAG_ptr_to_member_type:
8926
      this_type = read_tag_ptr_to_member_type (die, cu);
8927
      break;
8928
    case DW_TAG_reference_type:
8929
      this_type = read_tag_reference_type (die, cu);
8930
      break;
8931
    case DW_TAG_const_type:
8932
      this_type = read_tag_const_type (die, cu);
8933
      break;
8934
    case DW_TAG_volatile_type:
8935
      this_type = read_tag_volatile_type (die, cu);
8936
      break;
8937
    case DW_TAG_string_type:
8938
      this_type = read_tag_string_type (die, cu);
8939
      break;
8940
    case DW_TAG_typedef:
8941
      this_type = read_typedef (die, cu);
8942
      break;
8943
    case DW_TAG_subrange_type:
8944
      this_type = read_subrange_type (die, cu);
8945
      break;
8946
    case DW_TAG_base_type:
8947
      this_type = read_base_type (die, cu);
8948
      break;
8949
    case DW_TAG_unspecified_type:
8950
      this_type = read_unspecified_type (die, cu);
8951
      break;
8952
    case DW_TAG_namespace:
8953
      this_type = read_namespace_type (die, cu);
8954
      break;
8955
    default:
8956
      complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
8957
                 dwarf_tag_name (die->tag));
8958
      break;
8959
    }
8960
 
8961
  return this_type;
8962
}
8963
 
8964
/* Return the name of the namespace/class that DIE is defined within,
8965
   or "" if we can't tell.  The caller should not xfree the result.
8966
 
8967
   For example, if we're within the method foo() in the following
8968
   code:
8969
 
8970
   namespace N {
8971
     class C {
8972
       void foo () {
8973
       }
8974
     };
8975
   }
8976
 
8977
   then determine_prefix on foo's die will return "N::C".  */
8978
 
8979
static char *
8980
determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
8981
{
8982
  struct die_info *parent, *spec_die;
8983
  struct dwarf2_cu *spec_cu;
8984
  struct type *parent_type;
8985
 
8986
  if (cu->language != language_cplus
8987
      && cu->language != language_java)
8988
    return "";
8989
 
8990
  /* We have to be careful in the presence of DW_AT_specification.
8991
     For example, with GCC 3.4, given the code
8992
 
8993
     namespace N {
8994
       void foo() {
8995
         // Definition of N::foo.
8996
       }
8997
     }
8998
 
8999
     then we'll have a tree of DIEs like this:
9000
 
9001
     1: DW_TAG_compile_unit
9002
       2: DW_TAG_namespace        // N
9003
         3: DW_TAG_subprogram     // declaration of N::foo
9004
       4: DW_TAG_subprogram       // definition of N::foo
9005
            DW_AT_specification   // refers to die #3
9006
 
9007
     Thus, when processing die #4, we have to pretend that we're in
9008
     the context of its DW_AT_specification, namely the contex of die
9009
     #3.  */
9010
  spec_cu = cu;
9011
  spec_die = die_specification (die, &spec_cu);
9012
  if (spec_die == NULL)
9013
    parent = die->parent;
9014
  else
9015
    {
9016
      parent = spec_die->parent;
9017
      cu = spec_cu;
9018
    }
9019
 
9020
  if (parent == NULL)
9021
    return "";
9022
  else
9023
    switch (parent->tag)
9024
      {
9025
      case DW_TAG_namespace:
9026
        parent_type = read_type_die (parent, cu);
9027
        /* We give a name to even anonymous namespaces.  */
9028
        return TYPE_TAG_NAME (parent_type);
9029
      case DW_TAG_class_type:
9030
      case DW_TAG_interface_type:
9031
      case DW_TAG_structure_type:
9032
      case DW_TAG_union_type:
9033
        parent_type = read_type_die (parent, cu);
9034
        if (TYPE_TAG_NAME (parent_type) != NULL)
9035
          return TYPE_TAG_NAME (parent_type);
9036
        else
9037
          /* An anonymous structure is only allowed non-static data
9038
             members; no typedefs, no member functions, et cetera.
9039
             So it does not need a prefix.  */
9040
          return "";
9041
      default:
9042
        return determine_prefix (parent, cu);
9043
      }
9044
}
9045
 
9046
/* Return a newly-allocated string formed by concatenating PREFIX and
9047
   SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
9048
   simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
9049
   perform an obconcat, otherwise allocate storage for the result.  The CU argument
9050
   is used to determine the language and hence, the appropriate separator.  */
9051
 
9052
#define MAX_SEP_LEN 2  /* sizeof ("::")  */
9053
 
9054
static char *
9055
typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
9056
                 struct dwarf2_cu *cu)
9057
{
9058
  char *sep;
9059
 
9060
  if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
9061
    sep = "";
9062
  else if (cu->language == language_java)
9063
    sep = ".";
9064
  else
9065
    sep = "::";
9066
 
9067
  if (prefix == NULL)
9068
    prefix = "";
9069
  if (suffix == NULL)
9070
    suffix = "";
9071
 
9072
  if (obs == NULL)
9073
    {
9074
      char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9075
      strcpy (retval, prefix);
9076
      strcat (retval, sep);
9077
      strcat (retval, suffix);
9078
      return retval;
9079
    }
9080
  else
9081
    {
9082
      /* We have an obstack.  */
9083
      return obconcat (obs, prefix, sep, suffix);
9084
    }
9085
}
9086
 
9087
/* Return sibling of die, NULL if no sibling.  */
9088
 
9089
static struct die_info *
9090
sibling_die (struct die_info *die)
9091
{
9092
  return die->sibling;
9093
}
9094
 
9095
/* Get linkage name of a die, return NULL if not found.  */
9096
 
9097
static char *
9098
dwarf2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9099
{
9100
  struct attribute *attr;
9101
 
9102
  attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9103
  if (attr && DW_STRING (attr))
9104
    return DW_STRING (attr);
9105
  return dwarf2_name (die, cu);
9106
}
9107
 
9108
/* Get name of a die, return NULL if not found.  */
9109
 
9110
static char *
9111
dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
9112
                          struct obstack *obstack)
9113
{
9114
  if (name && cu->language == language_cplus)
9115
    {
9116
      char *canon_name = cp_canonicalize_string (name);
9117
 
9118
      if (canon_name != NULL)
9119
        {
9120
          if (strcmp (canon_name, name) != 0)
9121
            name = obsavestring (canon_name, strlen (canon_name),
9122
                                 obstack);
9123
          xfree (canon_name);
9124
        }
9125
    }
9126
 
9127
  return name;
9128
}
9129
 
9130
/* Get name of a die, return NULL if not found.  */
9131
 
9132
static char *
9133
dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9134
{
9135
  struct attribute *attr;
9136
 
9137
  attr = dwarf2_attr (die, DW_AT_name, cu);
9138
  if (!attr || !DW_STRING (attr))
9139
    return NULL;
9140
 
9141
  switch (die->tag)
9142
    {
9143
    case DW_TAG_compile_unit:
9144
      /* Compilation units have a DW_AT_name that is a filename, not
9145
         a source language identifier.  */
9146
    case DW_TAG_enumeration_type:
9147
    case DW_TAG_enumerator:
9148
      /* These tags always have simple identifiers already; no need
9149
         to canonicalize them.  */
9150
      return DW_STRING (attr);
9151
    default:
9152
      if (!DW_STRING_IS_CANONICAL (attr))
9153
        {
9154
          DW_STRING (attr)
9155
            = dwarf2_canonicalize_name (DW_STRING (attr), cu,
9156
                                        &cu->objfile->objfile_obstack);
9157
          DW_STRING_IS_CANONICAL (attr) = 1;
9158
        }
9159
      return DW_STRING (attr);
9160
    }
9161
}
9162
 
9163
/* Return the die that this die in an extension of, or NULL if there
9164
   is none.  *EXT_CU is the CU containing DIE on input, and the CU
9165
   containing the return value on output.  */
9166
 
9167
static struct die_info *
9168
dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9169
{
9170
  struct attribute *attr;
9171
 
9172
  attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9173
  if (attr == NULL)
9174
    return NULL;
9175
 
9176
  return follow_die_ref (die, attr, ext_cu);
9177
}
9178
 
9179
/* Convert a DIE tag into its string name.  */
9180
 
9181
static char *
9182
dwarf_tag_name (unsigned tag)
9183
{
9184
  switch (tag)
9185
    {
9186
    case DW_TAG_padding:
9187
      return "DW_TAG_padding";
9188
    case DW_TAG_array_type:
9189
      return "DW_TAG_array_type";
9190
    case DW_TAG_class_type:
9191
      return "DW_TAG_class_type";
9192
    case DW_TAG_entry_point:
9193
      return "DW_TAG_entry_point";
9194
    case DW_TAG_enumeration_type:
9195
      return "DW_TAG_enumeration_type";
9196
    case DW_TAG_formal_parameter:
9197
      return "DW_TAG_formal_parameter";
9198
    case DW_TAG_imported_declaration:
9199
      return "DW_TAG_imported_declaration";
9200
    case DW_TAG_label:
9201
      return "DW_TAG_label";
9202
    case DW_TAG_lexical_block:
9203
      return "DW_TAG_lexical_block";
9204
    case DW_TAG_member:
9205
      return "DW_TAG_member";
9206
    case DW_TAG_pointer_type:
9207
      return "DW_TAG_pointer_type";
9208
    case DW_TAG_reference_type:
9209
      return "DW_TAG_reference_type";
9210
    case DW_TAG_compile_unit:
9211
      return "DW_TAG_compile_unit";
9212
    case DW_TAG_string_type:
9213
      return "DW_TAG_string_type";
9214
    case DW_TAG_structure_type:
9215
      return "DW_TAG_structure_type";
9216
    case DW_TAG_subroutine_type:
9217
      return "DW_TAG_subroutine_type";
9218
    case DW_TAG_typedef:
9219
      return "DW_TAG_typedef";
9220
    case DW_TAG_union_type:
9221
      return "DW_TAG_union_type";
9222
    case DW_TAG_unspecified_parameters:
9223
      return "DW_TAG_unspecified_parameters";
9224
    case DW_TAG_variant:
9225
      return "DW_TAG_variant";
9226
    case DW_TAG_common_block:
9227
      return "DW_TAG_common_block";
9228
    case DW_TAG_common_inclusion:
9229
      return "DW_TAG_common_inclusion";
9230
    case DW_TAG_inheritance:
9231
      return "DW_TAG_inheritance";
9232
    case DW_TAG_inlined_subroutine:
9233
      return "DW_TAG_inlined_subroutine";
9234
    case DW_TAG_module:
9235
      return "DW_TAG_module";
9236
    case DW_TAG_ptr_to_member_type:
9237
      return "DW_TAG_ptr_to_member_type";
9238
    case DW_TAG_set_type:
9239
      return "DW_TAG_set_type";
9240
    case DW_TAG_subrange_type:
9241
      return "DW_TAG_subrange_type";
9242
    case DW_TAG_with_stmt:
9243
      return "DW_TAG_with_stmt";
9244
    case DW_TAG_access_declaration:
9245
      return "DW_TAG_access_declaration";
9246
    case DW_TAG_base_type:
9247
      return "DW_TAG_base_type";
9248
    case DW_TAG_catch_block:
9249
      return "DW_TAG_catch_block";
9250
    case DW_TAG_const_type:
9251
      return "DW_TAG_const_type";
9252
    case DW_TAG_constant:
9253
      return "DW_TAG_constant";
9254
    case DW_TAG_enumerator:
9255
      return "DW_TAG_enumerator";
9256
    case DW_TAG_file_type:
9257
      return "DW_TAG_file_type";
9258
    case DW_TAG_friend:
9259
      return "DW_TAG_friend";
9260
    case DW_TAG_namelist:
9261
      return "DW_TAG_namelist";
9262
    case DW_TAG_namelist_item:
9263
      return "DW_TAG_namelist_item";
9264
    case DW_TAG_packed_type:
9265
      return "DW_TAG_packed_type";
9266
    case DW_TAG_subprogram:
9267
      return "DW_TAG_subprogram";
9268
    case DW_TAG_template_type_param:
9269
      return "DW_TAG_template_type_param";
9270
    case DW_TAG_template_value_param:
9271
      return "DW_TAG_template_value_param";
9272
    case DW_TAG_thrown_type:
9273
      return "DW_TAG_thrown_type";
9274
    case DW_TAG_try_block:
9275
      return "DW_TAG_try_block";
9276
    case DW_TAG_variant_part:
9277
      return "DW_TAG_variant_part";
9278
    case DW_TAG_variable:
9279
      return "DW_TAG_variable";
9280
    case DW_TAG_volatile_type:
9281
      return "DW_TAG_volatile_type";
9282
    case DW_TAG_dwarf_procedure:
9283
      return "DW_TAG_dwarf_procedure";
9284
    case DW_TAG_restrict_type:
9285
      return "DW_TAG_restrict_type";
9286
    case DW_TAG_interface_type:
9287
      return "DW_TAG_interface_type";
9288
    case DW_TAG_namespace:
9289
      return "DW_TAG_namespace";
9290
    case DW_TAG_imported_module:
9291
      return "DW_TAG_imported_module";
9292
    case DW_TAG_unspecified_type:
9293
      return "DW_TAG_unspecified_type";
9294
    case DW_TAG_partial_unit:
9295
      return "DW_TAG_partial_unit";
9296
    case DW_TAG_imported_unit:
9297
      return "DW_TAG_imported_unit";
9298
    case DW_TAG_condition:
9299
      return "DW_TAG_condition";
9300
    case DW_TAG_shared_type:
9301
      return "DW_TAG_shared_type";
9302
    case DW_TAG_type_unit:
9303
      return "DW_TAG_type_unit";
9304
    case DW_TAG_MIPS_loop:
9305
      return "DW_TAG_MIPS_loop";
9306
    case DW_TAG_HP_array_descriptor:
9307
      return "DW_TAG_HP_array_descriptor";
9308
    case DW_TAG_format_label:
9309
      return "DW_TAG_format_label";
9310
    case DW_TAG_function_template:
9311
      return "DW_TAG_function_template";
9312
    case DW_TAG_class_template:
9313
      return "DW_TAG_class_template";
9314
    case DW_TAG_GNU_BINCL:
9315
      return "DW_TAG_GNU_BINCL";
9316
    case DW_TAG_GNU_EINCL:
9317
      return "DW_TAG_GNU_EINCL";
9318
    case DW_TAG_upc_shared_type:
9319
      return "DW_TAG_upc_shared_type";
9320
    case DW_TAG_upc_strict_type:
9321
      return "DW_TAG_upc_strict_type";
9322
    case DW_TAG_upc_relaxed_type:
9323
      return "DW_TAG_upc_relaxed_type";
9324
    case DW_TAG_PGI_kanji_type:
9325
      return "DW_TAG_PGI_kanji_type";
9326
    case DW_TAG_PGI_interface_block:
9327
      return "DW_TAG_PGI_interface_block";
9328
    default:
9329
      return "DW_TAG_<unknown>";
9330
    }
9331
}
9332
 
9333
/* Convert a DWARF attribute code into its string name.  */
9334
 
9335
static char *
9336
dwarf_attr_name (unsigned attr)
9337
{
9338
  switch (attr)
9339
    {
9340
    case DW_AT_sibling:
9341
      return "DW_AT_sibling";
9342
    case DW_AT_location:
9343
      return "DW_AT_location";
9344
    case DW_AT_name:
9345
      return "DW_AT_name";
9346
    case DW_AT_ordering:
9347
      return "DW_AT_ordering";
9348
    case DW_AT_subscr_data:
9349
      return "DW_AT_subscr_data";
9350
    case DW_AT_byte_size:
9351
      return "DW_AT_byte_size";
9352
    case DW_AT_bit_offset:
9353
      return "DW_AT_bit_offset";
9354
    case DW_AT_bit_size:
9355
      return "DW_AT_bit_size";
9356
    case DW_AT_element_list:
9357
      return "DW_AT_element_list";
9358
    case DW_AT_stmt_list:
9359
      return "DW_AT_stmt_list";
9360
    case DW_AT_low_pc:
9361
      return "DW_AT_low_pc";
9362
    case DW_AT_high_pc:
9363
      return "DW_AT_high_pc";
9364
    case DW_AT_language:
9365
      return "DW_AT_language";
9366
    case DW_AT_member:
9367
      return "DW_AT_member";
9368
    case DW_AT_discr:
9369
      return "DW_AT_discr";
9370
    case DW_AT_discr_value:
9371
      return "DW_AT_discr_value";
9372
    case DW_AT_visibility:
9373
      return "DW_AT_visibility";
9374
    case DW_AT_import:
9375
      return "DW_AT_import";
9376
    case DW_AT_string_length:
9377
      return "DW_AT_string_length";
9378
    case DW_AT_common_reference:
9379
      return "DW_AT_common_reference";
9380
    case DW_AT_comp_dir:
9381
      return "DW_AT_comp_dir";
9382
    case DW_AT_const_value:
9383
      return "DW_AT_const_value";
9384
    case DW_AT_containing_type:
9385
      return "DW_AT_containing_type";
9386
    case DW_AT_default_value:
9387
      return "DW_AT_default_value";
9388
    case DW_AT_inline:
9389
      return "DW_AT_inline";
9390
    case DW_AT_is_optional:
9391
      return "DW_AT_is_optional";
9392
    case DW_AT_lower_bound:
9393
      return "DW_AT_lower_bound";
9394
    case DW_AT_producer:
9395
      return "DW_AT_producer";
9396
    case DW_AT_prototyped:
9397
      return "DW_AT_prototyped";
9398
    case DW_AT_return_addr:
9399
      return "DW_AT_return_addr";
9400
    case DW_AT_start_scope:
9401
      return "DW_AT_start_scope";
9402
    case DW_AT_bit_stride:
9403
      return "DW_AT_bit_stride";
9404
    case DW_AT_upper_bound:
9405
      return "DW_AT_upper_bound";
9406
    case DW_AT_abstract_origin:
9407
      return "DW_AT_abstract_origin";
9408
    case DW_AT_accessibility:
9409
      return "DW_AT_accessibility";
9410
    case DW_AT_address_class:
9411
      return "DW_AT_address_class";
9412
    case DW_AT_artificial:
9413
      return "DW_AT_artificial";
9414
    case DW_AT_base_types:
9415
      return "DW_AT_base_types";
9416
    case DW_AT_calling_convention:
9417
      return "DW_AT_calling_convention";
9418
    case DW_AT_count:
9419
      return "DW_AT_count";
9420
    case DW_AT_data_member_location:
9421
      return "DW_AT_data_member_location";
9422
    case DW_AT_decl_column:
9423
      return "DW_AT_decl_column";
9424
    case DW_AT_decl_file:
9425
      return "DW_AT_decl_file";
9426
    case DW_AT_decl_line:
9427
      return "DW_AT_decl_line";
9428
    case DW_AT_declaration:
9429
      return "DW_AT_declaration";
9430
    case DW_AT_discr_list:
9431
      return "DW_AT_discr_list";
9432
    case DW_AT_encoding:
9433
      return "DW_AT_encoding";
9434
    case DW_AT_external:
9435
      return "DW_AT_external";
9436
    case DW_AT_frame_base:
9437
      return "DW_AT_frame_base";
9438
    case DW_AT_friend:
9439
      return "DW_AT_friend";
9440
    case DW_AT_identifier_case:
9441
      return "DW_AT_identifier_case";
9442
    case DW_AT_macro_info:
9443
      return "DW_AT_macro_info";
9444
    case DW_AT_namelist_items:
9445
      return "DW_AT_namelist_items";
9446
    case DW_AT_priority:
9447
      return "DW_AT_priority";
9448
    case DW_AT_segment:
9449
      return "DW_AT_segment";
9450
    case DW_AT_specification:
9451
      return "DW_AT_specification";
9452
    case DW_AT_static_link:
9453
      return "DW_AT_static_link";
9454
    case DW_AT_type:
9455
      return "DW_AT_type";
9456
    case DW_AT_use_location:
9457
      return "DW_AT_use_location";
9458
    case DW_AT_variable_parameter:
9459
      return "DW_AT_variable_parameter";
9460
    case DW_AT_virtuality:
9461
      return "DW_AT_virtuality";
9462
    case DW_AT_vtable_elem_location:
9463
      return "DW_AT_vtable_elem_location";
9464
    /* DWARF 3 values.  */
9465
    case DW_AT_allocated:
9466
      return "DW_AT_allocated";
9467
    case DW_AT_associated:
9468
      return "DW_AT_associated";
9469
    case DW_AT_data_location:
9470
      return "DW_AT_data_location";
9471
    case DW_AT_byte_stride:
9472
      return "DW_AT_byte_stride";
9473
    case DW_AT_entry_pc:
9474
      return "DW_AT_entry_pc";
9475
    case DW_AT_use_UTF8:
9476
      return "DW_AT_use_UTF8";
9477
    case DW_AT_extension:
9478
      return "DW_AT_extension";
9479
    case DW_AT_ranges:
9480
      return "DW_AT_ranges";
9481
    case DW_AT_trampoline:
9482
      return "DW_AT_trampoline";
9483
    case DW_AT_call_column:
9484
      return "DW_AT_call_column";
9485
    case DW_AT_call_file:
9486
      return "DW_AT_call_file";
9487
    case DW_AT_call_line:
9488
      return "DW_AT_call_line";
9489
    case DW_AT_description:
9490
      return "DW_AT_description";
9491
    case DW_AT_binary_scale:
9492
      return "DW_AT_binary_scale";
9493
    case DW_AT_decimal_scale:
9494
      return "DW_AT_decimal_scale";
9495
    case DW_AT_small:
9496
      return "DW_AT_small";
9497
    case DW_AT_decimal_sign:
9498
      return "DW_AT_decimal_sign";
9499
    case DW_AT_digit_count:
9500
      return "DW_AT_digit_count";
9501
    case DW_AT_picture_string:
9502
      return "DW_AT_picture_string";
9503
    case DW_AT_mutable:
9504
      return "DW_AT_mutable";
9505
    case DW_AT_threads_scaled:
9506
      return "DW_AT_threads_scaled";
9507
    case DW_AT_explicit:
9508
      return "DW_AT_explicit";
9509
    case DW_AT_object_pointer:
9510
      return "DW_AT_object_pointer";
9511
    case DW_AT_endianity:
9512
      return "DW_AT_endianity";
9513
    case DW_AT_elemental:
9514
      return "DW_AT_elemental";
9515
    case DW_AT_pure:
9516
      return "DW_AT_pure";
9517
    case DW_AT_recursive:
9518
      return "DW_AT_recursive";
9519
    /* DWARF 4 values.  */
9520
    case DW_AT_signature:
9521
      return "DW_AT_signature";
9522
    /* SGI/MIPS extensions.  */
9523
#ifdef MIPS /* collides with DW_AT_HP_block_index */
9524
    case DW_AT_MIPS_fde:
9525
      return "DW_AT_MIPS_fde";
9526
#endif
9527
    case DW_AT_MIPS_loop_begin:
9528
      return "DW_AT_MIPS_loop_begin";
9529
    case DW_AT_MIPS_tail_loop_begin:
9530
      return "DW_AT_MIPS_tail_loop_begin";
9531
    case DW_AT_MIPS_epilog_begin:
9532
      return "DW_AT_MIPS_epilog_begin";
9533
    case DW_AT_MIPS_loop_unroll_factor:
9534
      return "DW_AT_MIPS_loop_unroll_factor";
9535
    case DW_AT_MIPS_software_pipeline_depth:
9536
      return "DW_AT_MIPS_software_pipeline_depth";
9537
    case DW_AT_MIPS_linkage_name:
9538
      return "DW_AT_MIPS_linkage_name";
9539
    case DW_AT_MIPS_stride:
9540
      return "DW_AT_MIPS_stride";
9541
    case DW_AT_MIPS_abstract_name:
9542
      return "DW_AT_MIPS_abstract_name";
9543
    case DW_AT_MIPS_clone_origin:
9544
      return "DW_AT_MIPS_clone_origin";
9545
    case DW_AT_MIPS_has_inlines:
9546
      return "DW_AT_MIPS_has_inlines";
9547
    /* HP extensions.  */
9548
#ifndef MIPS /* collides with DW_AT_MIPS_fde */
9549
    case DW_AT_HP_block_index:
9550
      return "DW_AT_HP_block_index";
9551
#endif
9552
    case DW_AT_HP_unmodifiable:
9553
      return "DW_AT_HP_unmodifiable";
9554
    case DW_AT_HP_actuals_stmt_list:
9555
      return "DW_AT_HP_actuals_stmt_list";
9556
    case DW_AT_HP_proc_per_section:
9557
      return "DW_AT_HP_proc_per_section";
9558
    case DW_AT_HP_raw_data_ptr:
9559
      return "DW_AT_HP_raw_data_ptr";
9560
    case DW_AT_HP_pass_by_reference:
9561
      return "DW_AT_HP_pass_by_reference";
9562
    case DW_AT_HP_opt_level:
9563
      return "DW_AT_HP_opt_level";
9564
    case DW_AT_HP_prof_version_id:
9565
      return "DW_AT_HP_prof_version_id";
9566
    case DW_AT_HP_opt_flags:
9567
      return "DW_AT_HP_opt_flags";
9568
    case DW_AT_HP_cold_region_low_pc:
9569
      return "DW_AT_HP_cold_region_low_pc";
9570
    case DW_AT_HP_cold_region_high_pc:
9571
      return "DW_AT_HP_cold_region_high_pc";
9572
    case DW_AT_HP_all_variables_modifiable:
9573
      return "DW_AT_HP_all_variables_modifiable";
9574
    case DW_AT_HP_linkage_name:
9575
      return "DW_AT_HP_linkage_name";
9576
    case DW_AT_HP_prof_flags:
9577
      return "DW_AT_HP_prof_flags";
9578
    /* GNU extensions.  */
9579
    case DW_AT_sf_names:
9580
      return "DW_AT_sf_names";
9581
    case DW_AT_src_info:
9582
      return "DW_AT_src_info";
9583
    case DW_AT_mac_info:
9584
      return "DW_AT_mac_info";
9585
    case DW_AT_src_coords:
9586
      return "DW_AT_src_coords";
9587
    case DW_AT_body_begin:
9588
      return "DW_AT_body_begin";
9589
    case DW_AT_body_end:
9590
      return "DW_AT_body_end";
9591
    case DW_AT_GNU_vector:
9592
      return "DW_AT_GNU_vector";
9593
    /* VMS extensions.  */
9594
    case DW_AT_VMS_rtnbeg_pd_address:
9595
      return "DW_AT_VMS_rtnbeg_pd_address";
9596
    /* UPC extension.  */
9597
    case DW_AT_upc_threads_scaled:
9598
      return "DW_AT_upc_threads_scaled";
9599
    /* PGI (STMicroelectronics) extensions.  */
9600
    case DW_AT_PGI_lbase:
9601
      return "DW_AT_PGI_lbase";
9602
    case DW_AT_PGI_soffset:
9603
      return "DW_AT_PGI_soffset";
9604
    case DW_AT_PGI_lstride:
9605
      return "DW_AT_PGI_lstride";
9606
    default:
9607
      return "DW_AT_<unknown>";
9608
    }
9609
}
9610
 
9611
/* Convert a DWARF value form code into its string name.  */
9612
 
9613
static char *
9614
dwarf_form_name (unsigned form)
9615
{
9616
  switch (form)
9617
    {
9618
    case DW_FORM_addr:
9619
      return "DW_FORM_addr";
9620
    case DW_FORM_block2:
9621
      return "DW_FORM_block2";
9622
    case DW_FORM_block4:
9623
      return "DW_FORM_block4";
9624
    case DW_FORM_data2:
9625
      return "DW_FORM_data2";
9626
    case DW_FORM_data4:
9627
      return "DW_FORM_data4";
9628
    case DW_FORM_data8:
9629
      return "DW_FORM_data8";
9630
    case DW_FORM_string:
9631
      return "DW_FORM_string";
9632
    case DW_FORM_block:
9633
      return "DW_FORM_block";
9634
    case DW_FORM_block1:
9635
      return "DW_FORM_block1";
9636
    case DW_FORM_data1:
9637
      return "DW_FORM_data1";
9638
    case DW_FORM_flag:
9639
      return "DW_FORM_flag";
9640
    case DW_FORM_sdata:
9641
      return "DW_FORM_sdata";
9642
    case DW_FORM_strp:
9643
      return "DW_FORM_strp";
9644
    case DW_FORM_udata:
9645
      return "DW_FORM_udata";
9646
    case DW_FORM_ref_addr:
9647
      return "DW_FORM_ref_addr";
9648
    case DW_FORM_ref1:
9649
      return "DW_FORM_ref1";
9650
    case DW_FORM_ref2:
9651
      return "DW_FORM_ref2";
9652
    case DW_FORM_ref4:
9653
      return "DW_FORM_ref4";
9654
    case DW_FORM_ref8:
9655
      return "DW_FORM_ref8";
9656
    case DW_FORM_ref_udata:
9657
      return "DW_FORM_ref_udata";
9658
    case DW_FORM_indirect:
9659
      return "DW_FORM_indirect";
9660
    case DW_FORM_sec_offset:
9661
      return "DW_FORM_sec_offset";
9662
    case DW_FORM_exprloc:
9663
      return "DW_FORM_exprloc";
9664
    case DW_FORM_flag_present:
9665
      return "DW_FORM_flag_present";
9666
    case DW_FORM_sig8:
9667
      return "DW_FORM_sig8";
9668
    default:
9669
      return "DW_FORM_<unknown>";
9670
    }
9671
}
9672
 
9673
/* Convert a DWARF stack opcode into its string name.  */
9674
 
9675
static char *
9676
dwarf_stack_op_name (unsigned op)
9677
{
9678
  switch (op)
9679
    {
9680
    case DW_OP_addr:
9681
      return "DW_OP_addr";
9682
    case DW_OP_deref:
9683
      return "DW_OP_deref";
9684
    case DW_OP_const1u:
9685
      return "DW_OP_const1u";
9686
    case DW_OP_const1s:
9687
      return "DW_OP_const1s";
9688
    case DW_OP_const2u:
9689
      return "DW_OP_const2u";
9690
    case DW_OP_const2s:
9691
      return "DW_OP_const2s";
9692
    case DW_OP_const4u:
9693
      return "DW_OP_const4u";
9694
    case DW_OP_const4s:
9695
      return "DW_OP_const4s";
9696
    case DW_OP_const8u:
9697
      return "DW_OP_const8u";
9698
    case DW_OP_const8s:
9699
      return "DW_OP_const8s";
9700
    case DW_OP_constu:
9701
      return "DW_OP_constu";
9702
    case DW_OP_consts:
9703
      return "DW_OP_consts";
9704
    case DW_OP_dup:
9705
      return "DW_OP_dup";
9706
    case DW_OP_drop:
9707
      return "DW_OP_drop";
9708
    case DW_OP_over:
9709
      return "DW_OP_over";
9710
    case DW_OP_pick:
9711
      return "DW_OP_pick";
9712
    case DW_OP_swap:
9713
      return "DW_OP_swap";
9714
    case DW_OP_rot:
9715
      return "DW_OP_rot";
9716
    case DW_OP_xderef:
9717
      return "DW_OP_xderef";
9718
    case DW_OP_abs:
9719
      return "DW_OP_abs";
9720
    case DW_OP_and:
9721
      return "DW_OP_and";
9722
    case DW_OP_div:
9723
      return "DW_OP_div";
9724
    case DW_OP_minus:
9725
      return "DW_OP_minus";
9726
    case DW_OP_mod:
9727
      return "DW_OP_mod";
9728
    case DW_OP_mul:
9729
      return "DW_OP_mul";
9730
    case DW_OP_neg:
9731
      return "DW_OP_neg";
9732
    case DW_OP_not:
9733
      return "DW_OP_not";
9734
    case DW_OP_or:
9735
      return "DW_OP_or";
9736
    case DW_OP_plus:
9737
      return "DW_OP_plus";
9738
    case DW_OP_plus_uconst:
9739
      return "DW_OP_plus_uconst";
9740
    case DW_OP_shl:
9741
      return "DW_OP_shl";
9742
    case DW_OP_shr:
9743
      return "DW_OP_shr";
9744
    case DW_OP_shra:
9745
      return "DW_OP_shra";
9746
    case DW_OP_xor:
9747
      return "DW_OP_xor";
9748
    case DW_OP_bra:
9749
      return "DW_OP_bra";
9750
    case DW_OP_eq:
9751
      return "DW_OP_eq";
9752
    case DW_OP_ge:
9753
      return "DW_OP_ge";
9754
    case DW_OP_gt:
9755
      return "DW_OP_gt";
9756
    case DW_OP_le:
9757
      return "DW_OP_le";
9758
    case DW_OP_lt:
9759
      return "DW_OP_lt";
9760
    case DW_OP_ne:
9761
      return "DW_OP_ne";
9762
    case DW_OP_skip:
9763
      return "DW_OP_skip";
9764
    case DW_OP_lit0:
9765
      return "DW_OP_lit0";
9766
    case DW_OP_lit1:
9767
      return "DW_OP_lit1";
9768
    case DW_OP_lit2:
9769
      return "DW_OP_lit2";
9770
    case DW_OP_lit3:
9771
      return "DW_OP_lit3";
9772
    case DW_OP_lit4:
9773
      return "DW_OP_lit4";
9774
    case DW_OP_lit5:
9775
      return "DW_OP_lit5";
9776
    case DW_OP_lit6:
9777
      return "DW_OP_lit6";
9778
    case DW_OP_lit7:
9779
      return "DW_OP_lit7";
9780
    case DW_OP_lit8:
9781
      return "DW_OP_lit8";
9782
    case DW_OP_lit9:
9783
      return "DW_OP_lit9";
9784
    case DW_OP_lit10:
9785
      return "DW_OP_lit10";
9786
    case DW_OP_lit11:
9787
      return "DW_OP_lit11";
9788
    case DW_OP_lit12:
9789
      return "DW_OP_lit12";
9790
    case DW_OP_lit13:
9791
      return "DW_OP_lit13";
9792
    case DW_OP_lit14:
9793
      return "DW_OP_lit14";
9794
    case DW_OP_lit15:
9795
      return "DW_OP_lit15";
9796
    case DW_OP_lit16:
9797
      return "DW_OP_lit16";
9798
    case DW_OP_lit17:
9799
      return "DW_OP_lit17";
9800
    case DW_OP_lit18:
9801
      return "DW_OP_lit18";
9802
    case DW_OP_lit19:
9803
      return "DW_OP_lit19";
9804
    case DW_OP_lit20:
9805
      return "DW_OP_lit20";
9806
    case DW_OP_lit21:
9807
      return "DW_OP_lit21";
9808
    case DW_OP_lit22:
9809
      return "DW_OP_lit22";
9810
    case DW_OP_lit23:
9811
      return "DW_OP_lit23";
9812
    case DW_OP_lit24:
9813
      return "DW_OP_lit24";
9814
    case DW_OP_lit25:
9815
      return "DW_OP_lit25";
9816
    case DW_OP_lit26:
9817
      return "DW_OP_lit26";
9818
    case DW_OP_lit27:
9819
      return "DW_OP_lit27";
9820
    case DW_OP_lit28:
9821
      return "DW_OP_lit28";
9822
    case DW_OP_lit29:
9823
      return "DW_OP_lit29";
9824
    case DW_OP_lit30:
9825
      return "DW_OP_lit30";
9826
    case DW_OP_lit31:
9827
      return "DW_OP_lit31";
9828
    case DW_OP_reg0:
9829
      return "DW_OP_reg0";
9830
    case DW_OP_reg1:
9831
      return "DW_OP_reg1";
9832
    case DW_OP_reg2:
9833
      return "DW_OP_reg2";
9834
    case DW_OP_reg3:
9835
      return "DW_OP_reg3";
9836
    case DW_OP_reg4:
9837
      return "DW_OP_reg4";
9838
    case DW_OP_reg5:
9839
      return "DW_OP_reg5";
9840
    case DW_OP_reg6:
9841
      return "DW_OP_reg6";
9842
    case DW_OP_reg7:
9843
      return "DW_OP_reg7";
9844
    case DW_OP_reg8:
9845
      return "DW_OP_reg8";
9846
    case DW_OP_reg9:
9847
      return "DW_OP_reg9";
9848
    case DW_OP_reg10:
9849
      return "DW_OP_reg10";
9850
    case DW_OP_reg11:
9851
      return "DW_OP_reg11";
9852
    case DW_OP_reg12:
9853
      return "DW_OP_reg12";
9854
    case DW_OP_reg13:
9855
      return "DW_OP_reg13";
9856
    case DW_OP_reg14:
9857
      return "DW_OP_reg14";
9858
    case DW_OP_reg15:
9859
      return "DW_OP_reg15";
9860
    case DW_OP_reg16:
9861
      return "DW_OP_reg16";
9862
    case DW_OP_reg17:
9863
      return "DW_OP_reg17";
9864
    case DW_OP_reg18:
9865
      return "DW_OP_reg18";
9866
    case DW_OP_reg19:
9867
      return "DW_OP_reg19";
9868
    case DW_OP_reg20:
9869
      return "DW_OP_reg20";
9870
    case DW_OP_reg21:
9871
      return "DW_OP_reg21";
9872
    case DW_OP_reg22:
9873
      return "DW_OP_reg22";
9874
    case DW_OP_reg23:
9875
      return "DW_OP_reg23";
9876
    case DW_OP_reg24:
9877
      return "DW_OP_reg24";
9878
    case DW_OP_reg25:
9879
      return "DW_OP_reg25";
9880
    case DW_OP_reg26:
9881
      return "DW_OP_reg26";
9882
    case DW_OP_reg27:
9883
      return "DW_OP_reg27";
9884
    case DW_OP_reg28:
9885
      return "DW_OP_reg28";
9886
    case DW_OP_reg29:
9887
      return "DW_OP_reg29";
9888
    case DW_OP_reg30:
9889
      return "DW_OP_reg30";
9890
    case DW_OP_reg31:
9891
      return "DW_OP_reg31";
9892
    case DW_OP_breg0:
9893
      return "DW_OP_breg0";
9894
    case DW_OP_breg1:
9895
      return "DW_OP_breg1";
9896
    case DW_OP_breg2:
9897
      return "DW_OP_breg2";
9898
    case DW_OP_breg3:
9899
      return "DW_OP_breg3";
9900
    case DW_OP_breg4:
9901
      return "DW_OP_breg4";
9902
    case DW_OP_breg5:
9903
      return "DW_OP_breg5";
9904
    case DW_OP_breg6:
9905
      return "DW_OP_breg6";
9906
    case DW_OP_breg7:
9907
      return "DW_OP_breg7";
9908
    case DW_OP_breg8:
9909
      return "DW_OP_breg8";
9910
    case DW_OP_breg9:
9911
      return "DW_OP_breg9";
9912
    case DW_OP_breg10:
9913
      return "DW_OP_breg10";
9914
    case DW_OP_breg11:
9915
      return "DW_OP_breg11";
9916
    case DW_OP_breg12:
9917
      return "DW_OP_breg12";
9918
    case DW_OP_breg13:
9919
      return "DW_OP_breg13";
9920
    case DW_OP_breg14:
9921
      return "DW_OP_breg14";
9922
    case DW_OP_breg15:
9923
      return "DW_OP_breg15";
9924
    case DW_OP_breg16:
9925
      return "DW_OP_breg16";
9926
    case DW_OP_breg17:
9927
      return "DW_OP_breg17";
9928
    case DW_OP_breg18:
9929
      return "DW_OP_breg18";
9930
    case DW_OP_breg19:
9931
      return "DW_OP_breg19";
9932
    case DW_OP_breg20:
9933
      return "DW_OP_breg20";
9934
    case DW_OP_breg21:
9935
      return "DW_OP_breg21";
9936
    case DW_OP_breg22:
9937
      return "DW_OP_breg22";
9938
    case DW_OP_breg23:
9939
      return "DW_OP_breg23";
9940
    case DW_OP_breg24:
9941
      return "DW_OP_breg24";
9942
    case DW_OP_breg25:
9943
      return "DW_OP_breg25";
9944
    case DW_OP_breg26:
9945
      return "DW_OP_breg26";
9946
    case DW_OP_breg27:
9947
      return "DW_OP_breg27";
9948
    case DW_OP_breg28:
9949
      return "DW_OP_breg28";
9950
    case DW_OP_breg29:
9951
      return "DW_OP_breg29";
9952
    case DW_OP_breg30:
9953
      return "DW_OP_breg30";
9954
    case DW_OP_breg31:
9955
      return "DW_OP_breg31";
9956
    case DW_OP_regx:
9957
      return "DW_OP_regx";
9958
    case DW_OP_fbreg:
9959
      return "DW_OP_fbreg";
9960
    case DW_OP_bregx:
9961
      return "DW_OP_bregx";
9962
    case DW_OP_piece:
9963
      return "DW_OP_piece";
9964
    case DW_OP_deref_size:
9965
      return "DW_OP_deref_size";
9966
    case DW_OP_xderef_size:
9967
      return "DW_OP_xderef_size";
9968
    case DW_OP_nop:
9969
      return "DW_OP_nop";
9970
    /* DWARF 3 extensions.  */
9971
    case DW_OP_push_object_address:
9972
      return "DW_OP_push_object_address";
9973
    case DW_OP_call2:
9974
      return "DW_OP_call2";
9975
    case DW_OP_call4:
9976
      return "DW_OP_call4";
9977
    case DW_OP_call_ref:
9978
      return "DW_OP_call_ref";
9979
    /* GNU extensions.  */
9980
    case DW_OP_form_tls_address:
9981
      return "DW_OP_form_tls_address";
9982
    case DW_OP_call_frame_cfa:
9983
      return "DW_OP_call_frame_cfa";
9984
    case DW_OP_bit_piece:
9985
      return "DW_OP_bit_piece";
9986
    case DW_OP_GNU_push_tls_address:
9987
      return "DW_OP_GNU_push_tls_address";
9988
    case DW_OP_GNU_uninit:
9989
      return "DW_OP_GNU_uninit";
9990
    /* HP extensions. */
9991
    case DW_OP_HP_is_value:
9992
      return "DW_OP_HP_is_value";
9993
    case DW_OP_HP_fltconst4:
9994
      return "DW_OP_HP_fltconst4";
9995
    case DW_OP_HP_fltconst8:
9996
      return "DW_OP_HP_fltconst8";
9997
    case DW_OP_HP_mod_range:
9998
      return "DW_OP_HP_mod_range";
9999
    case DW_OP_HP_unmod_range:
10000
      return "DW_OP_HP_unmod_range";
10001
    case DW_OP_HP_tls:
10002
      return "DW_OP_HP_tls";
10003
    default:
10004
      return "OP_<unknown>";
10005
    }
10006
}
10007
 
10008
static char *
10009
dwarf_bool_name (unsigned mybool)
10010
{
10011
  if (mybool)
10012
    return "TRUE";
10013
  else
10014
    return "FALSE";
10015
}
10016
 
10017
/* Convert a DWARF type code into its string name.  */
10018
 
10019
static char *
10020
dwarf_type_encoding_name (unsigned enc)
10021
{
10022
  switch (enc)
10023
    {
10024
    case DW_ATE_void:
10025
      return "DW_ATE_void";
10026
    case DW_ATE_address:
10027
      return "DW_ATE_address";
10028
    case DW_ATE_boolean:
10029
      return "DW_ATE_boolean";
10030
    case DW_ATE_complex_float:
10031
      return "DW_ATE_complex_float";
10032
    case DW_ATE_float:
10033
      return "DW_ATE_float";
10034
    case DW_ATE_signed:
10035
      return "DW_ATE_signed";
10036
    case DW_ATE_signed_char:
10037
      return "DW_ATE_signed_char";
10038
    case DW_ATE_unsigned:
10039
      return "DW_ATE_unsigned";
10040
    case DW_ATE_unsigned_char:
10041
      return "DW_ATE_unsigned_char";
10042
    /* DWARF 3.  */
10043
    case DW_ATE_imaginary_float:
10044
      return "DW_ATE_imaginary_float";
10045
    case DW_ATE_packed_decimal:
10046
      return "DW_ATE_packed_decimal";
10047
    case DW_ATE_numeric_string:
10048
      return "DW_ATE_numeric_string";
10049
    case DW_ATE_edited:
10050
      return "DW_ATE_edited";
10051
    case DW_ATE_signed_fixed:
10052
      return "DW_ATE_signed_fixed";
10053
    case DW_ATE_unsigned_fixed:
10054
      return "DW_ATE_unsigned_fixed";
10055
    case DW_ATE_decimal_float:
10056
      return "DW_ATE_decimal_float";
10057
    /* HP extensions.  */
10058
    case DW_ATE_HP_float80:
10059
      return "DW_ATE_HP_float80";
10060
    case DW_ATE_HP_complex_float80:
10061
      return "DW_ATE_HP_complex_float80";
10062
    case DW_ATE_HP_float128:
10063
      return "DW_ATE_HP_float128";
10064
    case DW_ATE_HP_complex_float128:
10065
      return "DW_ATE_HP_complex_float128";
10066
    case DW_ATE_HP_floathpintel:
10067
      return "DW_ATE_HP_floathpintel";
10068
    case DW_ATE_HP_imaginary_float80:
10069
      return "DW_ATE_HP_imaginary_float80";
10070
    case DW_ATE_HP_imaginary_float128:
10071
      return "DW_ATE_HP_imaginary_float128";
10072
    default:
10073
      return "DW_ATE_<unknown>";
10074
    }
10075
}
10076
 
10077
/* Convert a DWARF call frame info operation to its string name. */
10078
 
10079
#if 0
10080
static char *
10081
dwarf_cfi_name (unsigned cfi_opc)
10082
{
10083
  switch (cfi_opc)
10084
    {
10085
    case DW_CFA_advance_loc:
10086
      return "DW_CFA_advance_loc";
10087
    case DW_CFA_offset:
10088
      return "DW_CFA_offset";
10089
    case DW_CFA_restore:
10090
      return "DW_CFA_restore";
10091
    case DW_CFA_nop:
10092
      return "DW_CFA_nop";
10093
    case DW_CFA_set_loc:
10094
      return "DW_CFA_set_loc";
10095
    case DW_CFA_advance_loc1:
10096
      return "DW_CFA_advance_loc1";
10097
    case DW_CFA_advance_loc2:
10098
      return "DW_CFA_advance_loc2";
10099
    case DW_CFA_advance_loc4:
10100
      return "DW_CFA_advance_loc4";
10101
    case DW_CFA_offset_extended:
10102
      return "DW_CFA_offset_extended";
10103
    case DW_CFA_restore_extended:
10104
      return "DW_CFA_restore_extended";
10105
    case DW_CFA_undefined:
10106
      return "DW_CFA_undefined";
10107
    case DW_CFA_same_value:
10108
      return "DW_CFA_same_value";
10109
    case DW_CFA_register:
10110
      return "DW_CFA_register";
10111
    case DW_CFA_remember_state:
10112
      return "DW_CFA_remember_state";
10113
    case DW_CFA_restore_state:
10114
      return "DW_CFA_restore_state";
10115
    case DW_CFA_def_cfa:
10116
      return "DW_CFA_def_cfa";
10117
    case DW_CFA_def_cfa_register:
10118
      return "DW_CFA_def_cfa_register";
10119
    case DW_CFA_def_cfa_offset:
10120
      return "DW_CFA_def_cfa_offset";
10121
    /* DWARF 3.  */
10122
    case DW_CFA_def_cfa_expression:
10123
      return "DW_CFA_def_cfa_expression";
10124
    case DW_CFA_expression:
10125
      return "DW_CFA_expression";
10126
    case DW_CFA_offset_extended_sf:
10127
      return "DW_CFA_offset_extended_sf";
10128
    case DW_CFA_def_cfa_sf:
10129
      return "DW_CFA_def_cfa_sf";
10130
    case DW_CFA_def_cfa_offset_sf:
10131
      return "DW_CFA_def_cfa_offset_sf";
10132
    case DW_CFA_val_offset:
10133
      return "DW_CFA_val_offset";
10134
    case DW_CFA_val_offset_sf:
10135
      return "DW_CFA_val_offset_sf";
10136
    case DW_CFA_val_expression:
10137
      return "DW_CFA_val_expression";
10138
    /* SGI/MIPS specific.  */
10139
    case DW_CFA_MIPS_advance_loc8:
10140
      return "DW_CFA_MIPS_advance_loc8";
10141
    /* GNU extensions.  */
10142
    case DW_CFA_GNU_window_save:
10143
      return "DW_CFA_GNU_window_save";
10144
    case DW_CFA_GNU_args_size:
10145
      return "DW_CFA_GNU_args_size";
10146
    case DW_CFA_GNU_negative_offset_extended:
10147
      return "DW_CFA_GNU_negative_offset_extended";
10148
    default:
10149
      return "DW_CFA_<unknown>";
10150
    }
10151
}
10152
#endif
10153
 
10154
static void
10155
dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
10156
{
10157
  unsigned int i;
10158
 
10159
  print_spaces (indent, f);
10160
  fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
10161
           dwarf_tag_name (die->tag), die->abbrev, die->offset);
10162
 
10163
  if (die->parent != NULL)
10164
    {
10165
      print_spaces (indent, f);
10166
      fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
10167
                          die->parent->offset);
10168
    }
10169
 
10170
  print_spaces (indent, f);
10171
  fprintf_unfiltered (f, "  has children: %s\n",
10172
           dwarf_bool_name (die->child != NULL));
10173
 
10174
  print_spaces (indent, f);
10175
  fprintf_unfiltered (f, "  attributes:\n");
10176
 
10177
  for (i = 0; i < die->num_attrs; ++i)
10178
    {
10179
      print_spaces (indent, f);
10180
      fprintf_unfiltered (f, "    %s (%s) ",
10181
               dwarf_attr_name (die->attrs[i].name),
10182
               dwarf_form_name (die->attrs[i].form));
10183
 
10184
      switch (die->attrs[i].form)
10185
        {
10186
        case DW_FORM_ref_addr:
10187
        case DW_FORM_addr:
10188
          fprintf_unfiltered (f, "address: ");
10189
          fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
10190
          break;
10191
        case DW_FORM_block2:
10192
        case DW_FORM_block4:
10193
        case DW_FORM_block:
10194
        case DW_FORM_block1:
10195
          fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
10196
          break;
10197
        case DW_FORM_ref1:
10198
        case DW_FORM_ref2:
10199
        case DW_FORM_ref4:
10200
          fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
10201
                              (long) (DW_ADDR (&die->attrs[i])));
10202
          break;
10203
        case DW_FORM_data1:
10204
        case DW_FORM_data2:
10205
        case DW_FORM_data4:
10206
        case DW_FORM_data8:
10207
        case DW_FORM_udata:
10208
        case DW_FORM_sdata:
10209
          fprintf_unfiltered (f, "constant: %s",
10210
                              pulongest (DW_UNSND (&die->attrs[i])));
10211
          break;
10212
        case DW_FORM_sig8:
10213
          if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
10214
            fprintf_unfiltered (f, "signatured type, offset: 0x%x",
10215
                                DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
10216
          else
10217
            fprintf_unfiltered (f, "signatured type, offset: unknown");
10218
          break;
10219
        case DW_FORM_string:
10220
        case DW_FORM_strp:
10221
          fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
10222
                   DW_STRING (&die->attrs[i])
10223
                   ? DW_STRING (&die->attrs[i]) : "",
10224
                   DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
10225
          break;
10226
        case DW_FORM_flag:
10227
          if (DW_UNSND (&die->attrs[i]))
10228
            fprintf_unfiltered (f, "flag: TRUE");
10229
          else
10230
            fprintf_unfiltered (f, "flag: FALSE");
10231
          break;
10232
        case DW_FORM_indirect:
10233
          /* the reader will have reduced the indirect form to
10234
             the "base form" so this form should not occur */
10235
          fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
10236
          break;
10237
        default:
10238
          fprintf_unfiltered (f, "unsupported attribute form: %d.",
10239
                   die->attrs[i].form);
10240
          break;
10241
        }
10242
      fprintf_unfiltered (f, "\n");
10243
    }
10244
}
10245
 
10246
static void
10247
dump_die_for_error (struct die_info *die)
10248
{
10249
  dump_die_shallow (gdb_stderr, 0, die);
10250
}
10251
 
10252
static void
10253
dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
10254
{
10255
  int indent = level * 4;
10256
 
10257
  gdb_assert (die != NULL);
10258
 
10259
  if (level >= max_level)
10260
    return;
10261
 
10262
  dump_die_shallow (f, indent, die);
10263
 
10264
  if (die->child != NULL)
10265
    {
10266
      print_spaces (indent, f);
10267
      fprintf_unfiltered (f, "  Children:");
10268
      if (level + 1 < max_level)
10269
        {
10270
          fprintf_unfiltered (f, "\n");
10271
          dump_die_1 (f, level + 1, max_level, die->child);
10272
        }
10273
      else
10274
        {
10275
          fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
10276
        }
10277
    }
10278
 
10279
  if (die->sibling != NULL && level > 0)
10280
    {
10281
      dump_die_1 (f, level, max_level, die->sibling);
10282
    }
10283
}
10284
 
10285
/* This is called from the pdie macro in gdbinit.in.
10286
   It's not static so gcc will keep a copy callable from gdb.  */
10287
 
10288
void
10289
dump_die (struct die_info *die, int max_level)
10290
{
10291
  dump_die_1 (gdb_stdlog, 0, max_level, die);
10292
}
10293
 
10294
static void
10295
store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
10296
{
10297
  void **slot;
10298
 
10299
  slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
10300
 
10301
  *slot = die;
10302
}
10303
 
10304
static int
10305
is_ref_attr (struct attribute *attr)
10306
{
10307
  switch (attr->form)
10308
    {
10309
    case DW_FORM_ref_addr:
10310
    case DW_FORM_ref1:
10311
    case DW_FORM_ref2:
10312
    case DW_FORM_ref4:
10313
    case DW_FORM_ref8:
10314
    case DW_FORM_ref_udata:
10315
      return 1;
10316
    default:
10317
      return 0;
10318
    }
10319
}
10320
 
10321
static unsigned int
10322
dwarf2_get_ref_die_offset (struct attribute *attr)
10323
{
10324
  if (is_ref_attr (attr))
10325
    return DW_ADDR (attr);
10326
 
10327
  complaint (&symfile_complaints,
10328
             _("unsupported die ref attribute form: '%s'"),
10329
             dwarf_form_name (attr->form));
10330
  return 0;
10331
}
10332
 
10333
/* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
10334
 * the value held by the attribute is not constant.  */
10335
 
10336
static LONGEST
10337
dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
10338
{
10339
  if (attr->form == DW_FORM_sdata)
10340
    return DW_SND (attr);
10341
  else if (attr->form == DW_FORM_udata
10342
           || attr->form == DW_FORM_data1
10343
           || attr->form == DW_FORM_data2
10344
           || attr->form == DW_FORM_data4
10345
           || attr->form == DW_FORM_data8)
10346
    return DW_UNSND (attr);
10347
  else
10348
    {
10349
      complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
10350
                 dwarf_form_name (attr->form));
10351
      return default_value;
10352
    }
10353
}
10354
 
10355
/* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
10356
   unit and add it to our queue.
10357
   The result is non-zero if PER_CU was queued, otherwise the result is zero
10358
   meaning either PER_CU is already queued or it is already loaded.  */
10359
 
10360
static int
10361
maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
10362
                       struct dwarf2_per_cu_data *per_cu)
10363
{
10364
  /* Mark the dependence relation so that we don't flush PER_CU
10365
     too early.  */
10366
  dwarf2_add_dependence (this_cu, per_cu);
10367
 
10368
  /* If it's already on the queue, we have nothing to do.  */
10369
  if (per_cu->queued)
10370
    return 0;
10371
 
10372
  /* If the compilation unit is already loaded, just mark it as
10373
     used.  */
10374
  if (per_cu->cu != NULL)
10375
    {
10376
      per_cu->cu->last_used = 0;
10377
      return 0;
10378
    }
10379
 
10380
  /* Add it to the queue.  */
10381
  queue_comp_unit (per_cu, this_cu->objfile);
10382
 
10383
  return 1;
10384
}
10385
 
10386
/* Follow reference or signature attribute ATTR of SRC_DIE.
10387
   On entry *REF_CU is the CU of SRC_DIE.
10388
   On exit *REF_CU is the CU of the result.  */
10389
 
10390
static struct die_info *
10391
follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
10392
                       struct dwarf2_cu **ref_cu)
10393
{
10394
  struct die_info *die;
10395
 
10396
  if (is_ref_attr (attr))
10397
    die = follow_die_ref (src_die, attr, ref_cu);
10398
  else if (attr->form == DW_FORM_sig8)
10399
    die = follow_die_sig (src_die, attr, ref_cu);
10400
  else
10401
    {
10402
      dump_die_for_error (src_die);
10403
      error (_("Dwarf Error: Expected reference attribute [in module %s]"),
10404
             (*ref_cu)->objfile->name);
10405
    }
10406
 
10407
  return die;
10408
}
10409
 
10410
/* Follow reference attribute ATTR of SRC_DIE.
10411
   On entry *REF_CU is the CU of SRC_DIE.
10412
   On exit *REF_CU is the CU of the result.  */
10413
 
10414
static struct die_info *
10415
follow_die_ref (struct die_info *src_die, struct attribute *attr,
10416
                struct dwarf2_cu **ref_cu)
10417
{
10418
  struct die_info *die;
10419
  unsigned int offset;
10420
  struct die_info temp_die;
10421
  struct dwarf2_cu *target_cu, *cu = *ref_cu;
10422
 
10423
  gdb_assert (cu->per_cu != NULL);
10424
 
10425
  offset = dwarf2_get_ref_die_offset (attr);
10426
 
10427
  if (cu->per_cu->from_debug_types)
10428
    {
10429
      /* .debug_types CUs cannot reference anything outside their CU.
10430
         If they need to, they have to reference a signatured type via
10431
         DW_FORM_sig8.  */
10432
      if (! offset_in_cu_p (&cu->header, offset))
10433
        goto not_found;
10434
      target_cu = cu;
10435
    }
10436
  else if (! offset_in_cu_p (&cu->header, offset))
10437
    {
10438
      struct dwarf2_per_cu_data *per_cu;
10439
      per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
10440
 
10441
      /* If necessary, add it to the queue and load its DIEs.  */
10442
      if (maybe_queue_comp_unit (cu, per_cu))
10443
        load_full_comp_unit (per_cu, cu->objfile);
10444
 
10445
      target_cu = per_cu->cu;
10446
    }
10447
  else
10448
    target_cu = cu;
10449
 
10450
  *ref_cu = target_cu;
10451
  temp_die.offset = offset;
10452
  die = htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
10453
  if (die)
10454
    return die;
10455
 
10456
 not_found:
10457
 
10458
  error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
10459
         "at 0x%x [in module %s]"),
10460
         offset, src_die->offset, cu->objfile->name);
10461
}
10462
 
10463
/* Follow the signature attribute ATTR in SRC_DIE.
10464
   On entry *REF_CU is the CU of SRC_DIE.
10465
   On exit *REF_CU is the CU of the result.  */
10466
 
10467
static struct die_info *
10468
follow_die_sig (struct die_info *src_die, struct attribute *attr,
10469
                struct dwarf2_cu **ref_cu)
10470
{
10471
  struct objfile *objfile = (*ref_cu)->objfile;
10472
  struct die_info temp_die;
10473
  struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
10474
  struct dwarf2_cu *sig_cu;
10475
  struct die_info *die;
10476
 
10477
  /* sig_type will be NULL if the signatured type is missing from
10478
     the debug info.  */
10479
  if (sig_type == NULL)
10480
    error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
10481
             "at 0x%x [in module %s]"),
10482
           src_die->offset, objfile->name);
10483
 
10484
  /* If necessary, add it to the queue and load its DIEs.  */
10485
 
10486
  if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
10487
    read_signatured_type (objfile, sig_type);
10488
 
10489
  gdb_assert (sig_type->per_cu.cu != NULL);
10490
 
10491
  sig_cu = sig_type->per_cu.cu;
10492
  temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
10493
  die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
10494
  if (die)
10495
    {
10496
      *ref_cu = sig_cu;
10497
      return die;
10498
    }
10499
 
10500
  error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
10501
         "at 0x%x [in module %s]"),
10502
         sig_type->type_offset, src_die->offset, objfile->name);
10503
}
10504
 
10505
/* Given an offset of a signatured type, return its signatured_type.  */
10506
 
10507
static struct signatured_type *
10508
lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
10509
{
10510
  gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
10511
  unsigned int length, initial_length_size;
10512
  unsigned int sig_offset;
10513
  struct signatured_type find_entry, *type_sig;
10514
 
10515
  length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
10516
  sig_offset = (initial_length_size
10517
                + 2 /*version*/
10518
                + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
10519
                + 1 /*address_size*/);
10520
  find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
10521
  type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
10522
 
10523
  /* This is only used to lookup previously recorded types.
10524
     If we didn't find it, it's our bug.  */
10525
  gdb_assert (type_sig != NULL);
10526
  gdb_assert (offset == type_sig->offset);
10527
 
10528
  return type_sig;
10529
}
10530
 
10531
/* Read in signatured type at OFFSET and build its CU and die(s).  */
10532
 
10533
static void
10534
read_signatured_type_at_offset (struct objfile *objfile,
10535
                                unsigned int offset)
10536
{
10537
  struct signatured_type *type_sig;
10538
 
10539
  /* We have the section offset, but we need the signature to do the
10540
     hash table lookup.  */
10541
  type_sig = lookup_signatured_type_at_offset (objfile, offset);
10542
 
10543
  gdb_assert (type_sig->per_cu.cu == NULL);
10544
 
10545
  read_signatured_type (objfile, type_sig);
10546
 
10547
  gdb_assert (type_sig->per_cu.cu != NULL);
10548
}
10549
 
10550
/* Read in a signatured type and build its CU and DIEs.  */
10551
 
10552
static void
10553
read_signatured_type (struct objfile *objfile,
10554
                      struct signatured_type *type_sig)
10555
{
10556
  gdb_byte *types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
10557
  struct die_reader_specs reader_specs;
10558
  struct dwarf2_cu *cu;
10559
  ULONGEST signature;
10560
  struct cleanup *back_to, *free_cu_cleanup;
10561
  struct attribute *attr;
10562
 
10563
  gdb_assert (type_sig->per_cu.cu == NULL);
10564
 
10565
  cu = xmalloc (sizeof (struct dwarf2_cu));
10566
  memset (cu, 0, sizeof (struct dwarf2_cu));
10567
  obstack_init (&cu->comp_unit_obstack);
10568
  cu->objfile = objfile;
10569
  type_sig->per_cu.cu = cu;
10570
  cu->per_cu = &type_sig->per_cu;
10571
 
10572
  /* If an error occurs while loading, release our storage.  */
10573
  free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
10574
 
10575
  types_ptr = read_type_comp_unit_head (&cu->header, &signature,
10576
                                        types_ptr, objfile->obfd);
10577
  gdb_assert (signature == type_sig->signature);
10578
 
10579
  cu->die_hash
10580
    = htab_create_alloc_ex (cu->header.length / 12,
10581
                            die_hash,
10582
                            die_eq,
10583
                            NULL,
10584
                            &cu->comp_unit_obstack,
10585
                            hashtab_obstack_allocate,
10586
                            dummy_obstack_deallocate);
10587
 
10588
  dwarf2_read_abbrevs (cu->objfile->obfd, cu);
10589
  back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
10590
 
10591
  init_cu_die_reader (&reader_specs, cu);
10592
 
10593
  cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
10594
                                    NULL /*parent*/);
10595
 
10596
  /* We try not to read any attributes in this function, because not
10597
     all objfiles needed for references have been loaded yet, and symbol
10598
     table processing isn't initialized.  But we have to set the CU language,
10599
     or we won't be able to build types correctly.  */
10600
  attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
10601
  if (attr)
10602
    set_cu_language (DW_UNSND (attr), cu);
10603
  else
10604
    set_cu_language (language_minimal, cu);
10605
 
10606
  do_cleanups (back_to);
10607
 
10608
  /* We've successfully allocated this compilation unit.  Let our caller
10609
     clean it up when finished with it.  */
10610
  discard_cleanups (free_cu_cleanup);
10611
 
10612
  type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
10613
  dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
10614
}
10615
 
10616
/* Decode simple location descriptions.
10617
   Given a pointer to a dwarf block that defines a location, compute
10618
   the location and return the value.
10619
 
10620
   NOTE drow/2003-11-18: This function is called in two situations
10621
   now: for the address of static or global variables (partial symbols
10622
   only) and for offsets into structures which are expected to be
10623
   (more or less) constant.  The partial symbol case should go away,
10624
   and only the constant case should remain.  That will let this
10625
   function complain more accurately.  A few special modes are allowed
10626
   without complaint for global variables (for instance, global
10627
   register values and thread-local values).
10628
 
10629
   A location description containing no operations indicates that the
10630
   object is optimized out.  The return value is 0 for that case.
10631
   FIXME drow/2003-11-16: No callers check for this case any more; soon all
10632
   callers will only want a very basic result and this can become a
10633
   complaint.
10634
 
10635
   Note that stack[0] is unused except as a default error return.
10636
   Note that stack overflow is not yet handled.  */
10637
 
10638
static CORE_ADDR
10639
decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
10640
{
10641
  struct objfile *objfile = cu->objfile;
10642
  struct comp_unit_head *cu_header = &cu->header;
10643
  int i;
10644
  int size = blk->size;
10645
  gdb_byte *data = blk->data;
10646
  CORE_ADDR stack[64];
10647
  int stacki;
10648
  unsigned int bytes_read, unsnd;
10649
  gdb_byte op;
10650
 
10651
  i = 0;
10652
  stacki = 0;
10653
  stack[stacki] = 0;
10654
 
10655
  while (i < size)
10656
    {
10657
      op = data[i++];
10658
      switch (op)
10659
        {
10660
        case DW_OP_lit0:
10661
        case DW_OP_lit1:
10662
        case DW_OP_lit2:
10663
        case DW_OP_lit3:
10664
        case DW_OP_lit4:
10665
        case DW_OP_lit5:
10666
        case DW_OP_lit6:
10667
        case DW_OP_lit7:
10668
        case DW_OP_lit8:
10669
        case DW_OP_lit9:
10670
        case DW_OP_lit10:
10671
        case DW_OP_lit11:
10672
        case DW_OP_lit12:
10673
        case DW_OP_lit13:
10674
        case DW_OP_lit14:
10675
        case DW_OP_lit15:
10676
        case DW_OP_lit16:
10677
        case DW_OP_lit17:
10678
        case DW_OP_lit18:
10679
        case DW_OP_lit19:
10680
        case DW_OP_lit20:
10681
        case DW_OP_lit21:
10682
        case DW_OP_lit22:
10683
        case DW_OP_lit23:
10684
        case DW_OP_lit24:
10685
        case DW_OP_lit25:
10686
        case DW_OP_lit26:
10687
        case DW_OP_lit27:
10688
        case DW_OP_lit28:
10689
        case DW_OP_lit29:
10690
        case DW_OP_lit30:
10691
        case DW_OP_lit31:
10692
          stack[++stacki] = op - DW_OP_lit0;
10693
          break;
10694
 
10695
        case DW_OP_reg0:
10696
        case DW_OP_reg1:
10697
        case DW_OP_reg2:
10698
        case DW_OP_reg3:
10699
        case DW_OP_reg4:
10700
        case DW_OP_reg5:
10701
        case DW_OP_reg6:
10702
        case DW_OP_reg7:
10703
        case DW_OP_reg8:
10704
        case DW_OP_reg9:
10705
        case DW_OP_reg10:
10706
        case DW_OP_reg11:
10707
        case DW_OP_reg12:
10708
        case DW_OP_reg13:
10709
        case DW_OP_reg14:
10710
        case DW_OP_reg15:
10711
        case DW_OP_reg16:
10712
        case DW_OP_reg17:
10713
        case DW_OP_reg18:
10714
        case DW_OP_reg19:
10715
        case DW_OP_reg20:
10716
        case DW_OP_reg21:
10717
        case DW_OP_reg22:
10718
        case DW_OP_reg23:
10719
        case DW_OP_reg24:
10720
        case DW_OP_reg25:
10721
        case DW_OP_reg26:
10722
        case DW_OP_reg27:
10723
        case DW_OP_reg28:
10724
        case DW_OP_reg29:
10725
        case DW_OP_reg30:
10726
        case DW_OP_reg31:
10727
          stack[++stacki] = op - DW_OP_reg0;
10728
          if (i < size)
10729
            dwarf2_complex_location_expr_complaint ();
10730
          break;
10731
 
10732
        case DW_OP_regx:
10733
          unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
10734
          i += bytes_read;
10735
          stack[++stacki] = unsnd;
10736
          if (i < size)
10737
            dwarf2_complex_location_expr_complaint ();
10738
          break;
10739
 
10740
        case DW_OP_addr:
10741
          stack[++stacki] = read_address (objfile->obfd, &data[i],
10742
                                          cu, &bytes_read);
10743
          i += bytes_read;
10744
          break;
10745
 
10746
        case DW_OP_const1u:
10747
          stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
10748
          i += 1;
10749
          break;
10750
 
10751
        case DW_OP_const1s:
10752
          stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
10753
          i += 1;
10754
          break;
10755
 
10756
        case DW_OP_const2u:
10757
          stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
10758
          i += 2;
10759
          break;
10760
 
10761
        case DW_OP_const2s:
10762
          stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
10763
          i += 2;
10764
          break;
10765
 
10766
        case DW_OP_const4u:
10767
          stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
10768
          i += 4;
10769
          break;
10770
 
10771
        case DW_OP_const4s:
10772
          stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
10773
          i += 4;
10774
          break;
10775
 
10776
        case DW_OP_constu:
10777
          stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
10778
                                                  &bytes_read);
10779
          i += bytes_read;
10780
          break;
10781
 
10782
        case DW_OP_consts:
10783
          stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
10784
          i += bytes_read;
10785
          break;
10786
 
10787
        case DW_OP_dup:
10788
          stack[stacki + 1] = stack[stacki];
10789
          stacki++;
10790
          break;
10791
 
10792
        case DW_OP_plus:
10793
          stack[stacki - 1] += stack[stacki];
10794
          stacki--;
10795
          break;
10796
 
10797
        case DW_OP_plus_uconst:
10798
          stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
10799
          i += bytes_read;
10800
          break;
10801
 
10802
        case DW_OP_minus:
10803
          stack[stacki - 1] -= stack[stacki];
10804
          stacki--;
10805
          break;
10806
 
10807
        case DW_OP_deref:
10808
          /* If we're not the last op, then we definitely can't encode
10809
             this using GDB's address_class enum.  This is valid for partial
10810
             global symbols, although the variable's address will be bogus
10811
             in the psymtab.  */
10812
          if (i < size)
10813
            dwarf2_complex_location_expr_complaint ();
10814
          break;
10815
 
10816
        case DW_OP_GNU_push_tls_address:
10817
          /* The top of the stack has the offset from the beginning
10818
             of the thread control block at which the variable is located.  */
10819
          /* Nothing should follow this operator, so the top of stack would
10820
             be returned.  */
10821
          /* This is valid for partial global symbols, but the variable's
10822
             address will be bogus in the psymtab.  */
10823
          if (i < size)
10824
            dwarf2_complex_location_expr_complaint ();
10825
          break;
10826
 
10827
        case DW_OP_GNU_uninit:
10828
          break;
10829
 
10830
        default:
10831
          complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
10832
                     dwarf_stack_op_name (op));
10833
          return (stack[stacki]);
10834
        }
10835
    }
10836
  return (stack[stacki]);
10837
}
10838
 
10839
/* memory allocation interface */
10840
 
10841
static struct dwarf_block *
10842
dwarf_alloc_block (struct dwarf2_cu *cu)
10843
{
10844
  struct dwarf_block *blk;
10845
 
10846
  blk = (struct dwarf_block *)
10847
    obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
10848
  return (blk);
10849
}
10850
 
10851
static struct abbrev_info *
10852
dwarf_alloc_abbrev (struct dwarf2_cu *cu)
10853
{
10854
  struct abbrev_info *abbrev;
10855
 
10856
  abbrev = (struct abbrev_info *)
10857
    obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
10858
  memset (abbrev, 0, sizeof (struct abbrev_info));
10859
  return (abbrev);
10860
}
10861
 
10862
static struct die_info *
10863
dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
10864
{
10865
  struct die_info *die;
10866
  size_t size = sizeof (struct die_info);
10867
 
10868
  if (num_attrs > 1)
10869
    size += (num_attrs - 1) * sizeof (struct attribute);
10870
 
10871
  die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
10872
  memset (die, 0, sizeof (struct die_info));
10873
  return (die);
10874
}
10875
 
10876
 
10877
/* Macro support.  */
10878
 
10879
 
10880
/* Return the full name of file number I in *LH's file name table.
10881
   Use COMP_DIR as the name of the current directory of the
10882
   compilation.  The result is allocated using xmalloc; the caller is
10883
   responsible for freeing it.  */
10884
static char *
10885
file_full_name (int file, struct line_header *lh, const char *comp_dir)
10886
{
10887
  /* Is the file number a valid index into the line header's file name
10888
     table?  Remember that file numbers start with one, not zero.  */
10889
  if (1 <= file && file <= lh->num_file_names)
10890
    {
10891
      struct file_entry *fe = &lh->file_names[file - 1];
10892
 
10893
      if (IS_ABSOLUTE_PATH (fe->name))
10894
        return xstrdup (fe->name);
10895
      else
10896
        {
10897
          const char *dir;
10898
          int dir_len;
10899
          char *full_name;
10900
 
10901
          if (fe->dir_index)
10902
            dir = lh->include_dirs[fe->dir_index - 1];
10903
          else
10904
            dir = comp_dir;
10905
 
10906
          if (dir)
10907
            {
10908
              dir_len = strlen (dir);
10909
              full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
10910
              strcpy (full_name, dir);
10911
              full_name[dir_len] = '/';
10912
              strcpy (full_name + dir_len + 1, fe->name);
10913
              return full_name;
10914
            }
10915
          else
10916
            return xstrdup (fe->name);
10917
        }
10918
    }
10919
  else
10920
    {
10921
      /* The compiler produced a bogus file number.  We can at least
10922
         record the macro definitions made in the file, even if we
10923
         won't be able to find the file by name.  */
10924
      char fake_name[80];
10925
      sprintf (fake_name, "<bad macro file number %d>", file);
10926
 
10927
      complaint (&symfile_complaints,
10928
                 _("bad file number in macro information (%d)"),
10929
                 file);
10930
 
10931
      return xstrdup (fake_name);
10932
    }
10933
}
10934
 
10935
 
10936
static struct macro_source_file *
10937
macro_start_file (int file, int line,
10938
                  struct macro_source_file *current_file,
10939
                  const char *comp_dir,
10940
                  struct line_header *lh, struct objfile *objfile)
10941
{
10942
  /* The full name of this source file.  */
10943
  char *full_name = file_full_name (file, lh, comp_dir);
10944
 
10945
  /* We don't create a macro table for this compilation unit
10946
     at all until we actually get a filename.  */
10947
  if (! pending_macros)
10948
    pending_macros = new_macro_table (&objfile->objfile_obstack,
10949
                                      objfile->macro_cache);
10950
 
10951
  if (! current_file)
10952
    /* If we have no current file, then this must be the start_file
10953
       directive for the compilation unit's main source file.  */
10954
    current_file = macro_set_main (pending_macros, full_name);
10955
  else
10956
    current_file = macro_include (current_file, line, full_name);
10957
 
10958
  xfree (full_name);
10959
 
10960
  return current_file;
10961
}
10962
 
10963
 
10964
/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
10965
   followed by a null byte.  */
10966
static char *
10967
copy_string (const char *buf, int len)
10968
{
10969
  char *s = xmalloc (len + 1);
10970
  memcpy (s, buf, len);
10971
  s[len] = '\0';
10972
 
10973
  return s;
10974
}
10975
 
10976
 
10977
static const char *
10978
consume_improper_spaces (const char *p, const char *body)
10979
{
10980
  if (*p == ' ')
10981
    {
10982
      complaint (&symfile_complaints,
10983
                 _("macro definition contains spaces in formal argument list:\n`%s'"),
10984
                 body);
10985
 
10986
      while (*p == ' ')
10987
        p++;
10988
    }
10989
 
10990
  return p;
10991
}
10992
 
10993
 
10994
static void
10995
parse_macro_definition (struct macro_source_file *file, int line,
10996
                        const char *body)
10997
{
10998
  const char *p;
10999
 
11000
  /* The body string takes one of two forms.  For object-like macro
11001
     definitions, it should be:
11002
 
11003
        <macro name> " " <definition>
11004
 
11005
     For function-like macro definitions, it should be:
11006
 
11007
        <macro name> "() " <definition>
11008
     or
11009
        <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
11010
 
11011
     Spaces may appear only where explicitly indicated, and in the
11012
     <definition>.
11013
 
11014
     The Dwarf 2 spec says that an object-like macro's name is always
11015
     followed by a space, but versions of GCC around March 2002 omit
11016
     the space when the macro's definition is the empty string.
11017
 
11018
     The Dwarf 2 spec says that there should be no spaces between the
11019
     formal arguments in a function-like macro's formal argument list,
11020
     but versions of GCC around March 2002 include spaces after the
11021
     commas.  */
11022
 
11023
 
11024
  /* Find the extent of the macro name.  The macro name is terminated
11025
     by either a space or null character (for an object-like macro) or
11026
     an opening paren (for a function-like macro).  */
11027
  for (p = body; *p; p++)
11028
    if (*p == ' ' || *p == '(')
11029
      break;
11030
 
11031
  if (*p == ' ' || *p == '\0')
11032
    {
11033
      /* It's an object-like macro.  */
11034
      int name_len = p - body;
11035
      char *name = copy_string (body, name_len);
11036
      const char *replacement;
11037
 
11038
      if (*p == ' ')
11039
        replacement = body + name_len + 1;
11040
      else
11041
        {
11042
          dwarf2_macro_malformed_definition_complaint (body);
11043
          replacement = body + name_len;
11044
        }
11045
 
11046
      macro_define_object (file, line, name, replacement);
11047
 
11048
      xfree (name);
11049
    }
11050
  else if (*p == '(')
11051
    {
11052
      /* It's a function-like macro.  */
11053
      char *name = copy_string (body, p - body);
11054
      int argc = 0;
11055
      int argv_size = 1;
11056
      char **argv = xmalloc (argv_size * sizeof (*argv));
11057
 
11058
      p++;
11059
 
11060
      p = consume_improper_spaces (p, body);
11061
 
11062
      /* Parse the formal argument list.  */
11063
      while (*p && *p != ')')
11064
        {
11065
          /* Find the extent of the current argument name.  */
11066
          const char *arg_start = p;
11067
 
11068
          while (*p && *p != ',' && *p != ')' && *p != ' ')
11069
            p++;
11070
 
11071
          if (! *p || p == arg_start)
11072
            dwarf2_macro_malformed_definition_complaint (body);
11073
          else
11074
            {
11075
              /* Make sure argv has room for the new argument.  */
11076
              if (argc >= argv_size)
11077
                {
11078
                  argv_size *= 2;
11079
                  argv = xrealloc (argv, argv_size * sizeof (*argv));
11080
                }
11081
 
11082
              argv[argc++] = copy_string (arg_start, p - arg_start);
11083
            }
11084
 
11085
          p = consume_improper_spaces (p, body);
11086
 
11087
          /* Consume the comma, if present.  */
11088
          if (*p == ',')
11089
            {
11090
              p++;
11091
 
11092
              p = consume_improper_spaces (p, body);
11093
            }
11094
        }
11095
 
11096
      if (*p == ')')
11097
        {
11098
          p++;
11099
 
11100
          if (*p == ' ')
11101
            /* Perfectly formed definition, no complaints.  */
11102
            macro_define_function (file, line, name,
11103
                                   argc, (const char **) argv,
11104
                                   p + 1);
11105
          else if (*p == '\0')
11106
            {
11107
              /* Complain, but do define it.  */
11108
              dwarf2_macro_malformed_definition_complaint (body);
11109
              macro_define_function (file, line, name,
11110
                                     argc, (const char **) argv,
11111
                                     p);
11112
            }
11113
          else
11114
            /* Just complain.  */
11115
            dwarf2_macro_malformed_definition_complaint (body);
11116
        }
11117
      else
11118
        /* Just complain.  */
11119
        dwarf2_macro_malformed_definition_complaint (body);
11120
 
11121
      xfree (name);
11122
      {
11123
        int i;
11124
 
11125
        for (i = 0; i < argc; i++)
11126
          xfree (argv[i]);
11127
      }
11128
      xfree (argv);
11129
    }
11130
  else
11131
    dwarf2_macro_malformed_definition_complaint (body);
11132
}
11133
 
11134
 
11135
static void
11136
dwarf_decode_macros (struct line_header *lh, unsigned int offset,
11137
                     char *comp_dir, bfd *abfd,
11138
                     struct dwarf2_cu *cu)
11139
{
11140
  gdb_byte *mac_ptr, *mac_end;
11141
  struct macro_source_file *current_file = 0;
11142
  enum dwarf_macinfo_record_type macinfo_type;
11143
  int at_commandline;
11144
 
11145
  if (dwarf2_per_objfile->macinfo.buffer == NULL)
11146
    {
11147
      complaint (&symfile_complaints, _("missing .debug_macinfo section"));
11148
      return;
11149
    }
11150
 
11151
  /* First pass: Find the name of the base filename.
11152
     This filename is needed in order to process all macros whose definition
11153
     (or undefinition) comes from the command line.  These macros are defined
11154
     before the first DW_MACINFO_start_file entry, and yet still need to be
11155
     associated to the base file.
11156
 
11157
     To determine the base file name, we scan the macro definitions until we
11158
     reach the first DW_MACINFO_start_file entry.  We then initialize
11159
     CURRENT_FILE accordingly so that any macro definition found before the
11160
     first DW_MACINFO_start_file can still be associated to the base file.  */
11161
 
11162
  mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
11163
  mac_end = dwarf2_per_objfile->macinfo.buffer
11164
    + dwarf2_per_objfile->macinfo.size;
11165
 
11166
  do
11167
    {
11168
      /* Do we at least have room for a macinfo type byte?  */
11169
      if (mac_ptr >= mac_end)
11170
        {
11171
          /* Complaint is printed during the second pass as GDB will probably
11172
             stop the first pass earlier upon finding DW_MACINFO_start_file.  */
11173
          break;
11174
        }
11175
 
11176
      macinfo_type = read_1_byte (abfd, mac_ptr);
11177
      mac_ptr++;
11178
 
11179
      switch (macinfo_type)
11180
        {
11181
          /* A zero macinfo type indicates the end of the macro
11182
             information.  */
11183
        case 0:
11184
          break;
11185
 
11186
        case DW_MACINFO_define:
11187
        case DW_MACINFO_undef:
11188
          /* Only skip the data by MAC_PTR.  */
11189
          {
11190
            unsigned int bytes_read;
11191
 
11192
            read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11193
            mac_ptr += bytes_read;
11194
            read_string (abfd, mac_ptr, &bytes_read);
11195
            mac_ptr += bytes_read;
11196
          }
11197
          break;
11198
 
11199
        case DW_MACINFO_start_file:
11200
          {
11201
            unsigned int bytes_read;
11202
            int line, file;
11203
 
11204
            line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11205
            mac_ptr += bytes_read;
11206
            file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11207
            mac_ptr += bytes_read;
11208
 
11209
            current_file = macro_start_file (file, line, current_file, comp_dir,
11210
                                             lh, cu->objfile);
11211
          }
11212
          break;
11213
 
11214
        case DW_MACINFO_end_file:
11215
          /* No data to skip by MAC_PTR.  */
11216
          break;
11217
 
11218
        case DW_MACINFO_vendor_ext:
11219
          /* Only skip the data by MAC_PTR.  */
11220
          {
11221
            unsigned int bytes_read;
11222
 
11223
            read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11224
            mac_ptr += bytes_read;
11225
            read_string (abfd, mac_ptr, &bytes_read);
11226
            mac_ptr += bytes_read;
11227
          }
11228
          break;
11229
 
11230
        default:
11231
          break;
11232
        }
11233
    } while (macinfo_type != 0 && current_file == NULL);
11234
 
11235
  /* Second pass: Process all entries.
11236
 
11237
     Use the AT_COMMAND_LINE flag to determine whether we are still processing
11238
     command-line macro definitions/undefinitions.  This flag is unset when we
11239
     reach the first DW_MACINFO_start_file entry.  */
11240
 
11241
  mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
11242
 
11243
  /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
11244
     GDB is still reading the definitions from command line.  First
11245
     DW_MACINFO_start_file will need to be ignored as it was already executed
11246
     to create CURRENT_FILE for the main source holding also the command line
11247
     definitions.  On first met DW_MACINFO_start_file this flag is reset to
11248
     normally execute all the remaining DW_MACINFO_start_file macinfos.  */
11249
 
11250
  at_commandline = 1;
11251
 
11252
  do
11253
    {
11254
      /* Do we at least have room for a macinfo type byte?  */
11255
      if (mac_ptr >= mac_end)
11256
        {
11257
          dwarf2_macros_too_long_complaint ();
11258
          break;
11259
        }
11260
 
11261
      macinfo_type = read_1_byte (abfd, mac_ptr);
11262
      mac_ptr++;
11263
 
11264
      switch (macinfo_type)
11265
        {
11266
          /* A zero macinfo type indicates the end of the macro
11267
             information.  */
11268
        case 0:
11269
          break;
11270
 
11271
        case DW_MACINFO_define:
11272
        case DW_MACINFO_undef:
11273
          {
11274
            unsigned int bytes_read;
11275
            int line;
11276
            char *body;
11277
 
11278
            line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11279
            mac_ptr += bytes_read;
11280
            body = read_string (abfd, mac_ptr, &bytes_read);
11281
            mac_ptr += bytes_read;
11282
 
11283
            if (! current_file)
11284
              {
11285
                /* DWARF violation as no main source is present.  */
11286
                complaint (&symfile_complaints,
11287
                           _("debug info with no main source gives macro %s "
11288
                             "on line %d: %s"),
11289
                           macinfo_type == DW_MACINFO_define ?
11290
                             _("definition") :
11291
                               macinfo_type == DW_MACINFO_undef ?
11292
                                 _("undefinition") :
11293
                                 _("something-or-other"), line, body);
11294
                break;
11295
              }
11296
            if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
11297
              complaint (&symfile_complaints,
11298
                         _("debug info gives %s macro %s with %s line %d: %s"),
11299
                         at_commandline ? _("command-line") : _("in-file"),
11300
                         macinfo_type == DW_MACINFO_define ?
11301
                           _("definition") :
11302
                             macinfo_type == DW_MACINFO_undef ?
11303
                               _("undefinition") :
11304
                               _("something-or-other"),
11305
                         line == 0 ? _("zero") : _("non-zero"), line, body);
11306
 
11307
            if (macinfo_type == DW_MACINFO_define)
11308
              parse_macro_definition (current_file, line, body);
11309
            else if (macinfo_type == DW_MACINFO_undef)
11310
              macro_undef (current_file, line, body);
11311
          }
11312
          break;
11313
 
11314
        case DW_MACINFO_start_file:
11315
          {
11316
            unsigned int bytes_read;
11317
            int line, file;
11318
 
11319
            line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11320
            mac_ptr += bytes_read;
11321
            file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11322
            mac_ptr += bytes_read;
11323
 
11324
            if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
11325
              complaint (&symfile_complaints,
11326
                         _("debug info gives source %d included "
11327
                           "from %s at %s line %d"),
11328
                         file, at_commandline ? _("command-line") : _("file"),
11329
                         line == 0 ? _("zero") : _("non-zero"), line);
11330
 
11331
            if (at_commandline)
11332
              {
11333
                /* This DW_MACINFO_start_file was executed in the pass one.  */
11334
                at_commandline = 0;
11335
              }
11336
            else
11337
              current_file = macro_start_file (file, line,
11338
                                               current_file, comp_dir,
11339
                                               lh, cu->objfile);
11340
          }
11341
          break;
11342
 
11343
        case DW_MACINFO_end_file:
11344
          if (! current_file)
11345
            complaint (&symfile_complaints,
11346
                       _("macro debug info has an unmatched `close_file' directive"));
11347
          else
11348
            {
11349
              current_file = current_file->included_by;
11350
              if (! current_file)
11351
                {
11352
                  enum dwarf_macinfo_record_type next_type;
11353
 
11354
                  /* GCC circa March 2002 doesn't produce the zero
11355
                     type byte marking the end of the compilation
11356
                     unit.  Complain if it's not there, but exit no
11357
                     matter what.  */
11358
 
11359
                  /* Do we at least have room for a macinfo type byte?  */
11360
                  if (mac_ptr >= mac_end)
11361
                    {
11362
                      dwarf2_macros_too_long_complaint ();
11363
                      return;
11364
                    }
11365
 
11366
                  /* We don't increment mac_ptr here, so this is just
11367
                     a look-ahead.  */
11368
                  next_type = read_1_byte (abfd, mac_ptr);
11369
                  if (next_type != 0)
11370
                    complaint (&symfile_complaints,
11371
                               _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
11372
 
11373
                  return;
11374
                }
11375
            }
11376
          break;
11377
 
11378
        case DW_MACINFO_vendor_ext:
11379
          {
11380
            unsigned int bytes_read;
11381
            int constant;
11382
            char *string;
11383
 
11384
            constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
11385
            mac_ptr += bytes_read;
11386
            string = read_string (abfd, mac_ptr, &bytes_read);
11387
            mac_ptr += bytes_read;
11388
 
11389
            /* We don't recognize any vendor extensions.  */
11390
          }
11391
          break;
11392
        }
11393
    } while (macinfo_type != 0);
11394
}
11395
 
11396
/* Check if the attribute's form is a DW_FORM_block*
11397
   if so return true else false. */
11398
static int
11399
attr_form_is_block (struct attribute *attr)
11400
{
11401
  return (attr == NULL ? 0 :
11402
      attr->form == DW_FORM_block1
11403
      || attr->form == DW_FORM_block2
11404
      || attr->form == DW_FORM_block4
11405
      || attr->form == DW_FORM_block);
11406
}
11407
 
11408
/* Return non-zero if ATTR's value is a section offset --- classes
11409
   lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
11410
   You may use DW_UNSND (attr) to retrieve such offsets.
11411
 
11412
   Section 7.5.4, "Attribute Encodings", explains that no attribute
11413
   may have a value that belongs to more than one of these classes; it
11414
   would be ambiguous if we did, because we use the same forms for all
11415
   of them.  */
11416
static int
11417
attr_form_is_section_offset (struct attribute *attr)
11418
{
11419
  return (attr->form == DW_FORM_data4
11420
          || attr->form == DW_FORM_data8);
11421
}
11422
 
11423
 
11424
/* Return non-zero if ATTR's value falls in the 'constant' class, or
11425
   zero otherwise.  When this function returns true, you can apply
11426
   dwarf2_get_attr_constant_value to it.
11427
 
11428
   However, note that for some attributes you must check
11429
   attr_form_is_section_offset before using this test.  DW_FORM_data4
11430
   and DW_FORM_data8 are members of both the constant class, and of
11431
   the classes that contain offsets into other debug sections
11432
   (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
11433
   that, if an attribute's can be either a constant or one of the
11434
   section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
11435
   taken as section offsets, not constants.  */
11436
static int
11437
attr_form_is_constant (struct attribute *attr)
11438
{
11439
  switch (attr->form)
11440
    {
11441
    case DW_FORM_sdata:
11442
    case DW_FORM_udata:
11443
    case DW_FORM_data1:
11444
    case DW_FORM_data2:
11445
    case DW_FORM_data4:
11446
    case DW_FORM_data8:
11447
      return 1;
11448
    default:
11449
      return 0;
11450
    }
11451
}
11452
 
11453
static void
11454
dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
11455
                             struct dwarf2_cu *cu)
11456
{
11457
  if (attr_form_is_section_offset (attr)
11458
      /* ".debug_loc" may not exist at all, or the offset may be outside
11459
         the section.  If so, fall through to the complaint in the
11460
         other branch.  */
11461
      && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
11462
    {
11463
      struct dwarf2_loclist_baton *baton;
11464
 
11465
      baton = obstack_alloc (&cu->objfile->objfile_obstack,
11466
                             sizeof (struct dwarf2_loclist_baton));
11467
      baton->per_cu = cu->per_cu;
11468
      gdb_assert (baton->per_cu);
11469
 
11470
      /* We don't know how long the location list is, but make sure we
11471
         don't run off the edge of the section.  */
11472
      baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
11473
      baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
11474
      baton->base_address = cu->base_address;
11475
      if (cu->base_known == 0)
11476
        complaint (&symfile_complaints,
11477
                   _("Location list used without specifying the CU base address."));
11478
 
11479
      SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
11480
      SYMBOL_LOCATION_BATON (sym) = baton;
11481
    }
11482
  else
11483
    {
11484
      struct dwarf2_locexpr_baton *baton;
11485
 
11486
      baton = obstack_alloc (&cu->objfile->objfile_obstack,
11487
                             sizeof (struct dwarf2_locexpr_baton));
11488
      baton->per_cu = cu->per_cu;
11489
      gdb_assert (baton->per_cu);
11490
 
11491
      if (attr_form_is_block (attr))
11492
        {
11493
          /* Note that we're just copying the block's data pointer
11494
             here, not the actual data.  We're still pointing into the
11495
             info_buffer for SYM's objfile; right now we never release
11496
             that buffer, but when we do clean up properly this may
11497
             need to change.  */
11498
          baton->size = DW_BLOCK (attr)->size;
11499
          baton->data = DW_BLOCK (attr)->data;
11500
        }
11501
      else
11502
        {
11503
          dwarf2_invalid_attrib_class_complaint ("location description",
11504
                                                 SYMBOL_NATURAL_NAME (sym));
11505
          baton->size = 0;
11506
          baton->data = NULL;
11507
        }
11508
 
11509
      SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11510
      SYMBOL_LOCATION_BATON (sym) = baton;
11511
    }
11512
}
11513
 
11514
/* Return the OBJFILE associated with the compilation unit CU.  */
11515
 
11516
struct objfile *
11517
dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
11518
{
11519
  struct objfile *objfile = per_cu->psymtab->objfile;
11520
 
11521
  /* Return the master objfile, so that we can report and look up the
11522
     correct file containing this variable.  */
11523
  if (objfile->separate_debug_objfile_backlink)
11524
    objfile = objfile->separate_debug_objfile_backlink;
11525
 
11526
  return objfile;
11527
}
11528
 
11529
/* Return the address size given in the compilation unit header for CU.  */
11530
 
11531
CORE_ADDR
11532
dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
11533
{
11534
  if (per_cu->cu)
11535
    return per_cu->cu->header.addr_size;
11536
  else
11537
    {
11538
      /* If the CU is not currently read in, we re-read its header.  */
11539
      struct objfile *objfile = per_cu->psymtab->objfile;
11540
      struct dwarf2_per_objfile *per_objfile
11541
        = objfile_data (objfile, dwarf2_objfile_data_key);
11542
      gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
11543
 
11544
      struct comp_unit_head cu_header;
11545
      memset (&cu_header, 0, sizeof cu_header);
11546
      read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
11547
      return cu_header.addr_size;
11548
    }
11549
}
11550
 
11551
/* Locate the .debug_info compilation unit from CU's objfile which contains
11552
   the DIE at OFFSET.  Raises an error on failure.  */
11553
 
11554
static struct dwarf2_per_cu_data *
11555
dwarf2_find_containing_comp_unit (unsigned int offset,
11556
                                  struct objfile *objfile)
11557
{
11558
  struct dwarf2_per_cu_data *this_cu;
11559
  int low, high;
11560
 
11561
  low = 0;
11562
  high = dwarf2_per_objfile->n_comp_units - 1;
11563
  while (high > low)
11564
    {
11565
      int mid = low + (high - low) / 2;
11566
      if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
11567
        high = mid;
11568
      else
11569
        low = mid + 1;
11570
    }
11571
  gdb_assert (low == high);
11572
  if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
11573
    {
11574
      if (low == 0)
11575
        error (_("Dwarf Error: could not find partial DIE containing "
11576
               "offset 0x%lx [in module %s]"),
11577
               (long) offset, bfd_get_filename (objfile->obfd));
11578
 
11579
      gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
11580
      return dwarf2_per_objfile->all_comp_units[low-1];
11581
    }
11582
  else
11583
    {
11584
      this_cu = dwarf2_per_objfile->all_comp_units[low];
11585
      if (low == dwarf2_per_objfile->n_comp_units - 1
11586
          && offset >= this_cu->offset + this_cu->length)
11587
        error (_("invalid dwarf2 offset %u"), offset);
11588
      gdb_assert (offset < this_cu->offset + this_cu->length);
11589
      return this_cu;
11590
    }
11591
}
11592
 
11593
/* Locate the compilation unit from OBJFILE which is located at exactly
11594
   OFFSET.  Raises an error on failure.  */
11595
 
11596
static struct dwarf2_per_cu_data *
11597
dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
11598
{
11599
  struct dwarf2_per_cu_data *this_cu;
11600
  this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
11601
  if (this_cu->offset != offset)
11602
    error (_("no compilation unit with offset %u."), offset);
11603
  return this_cu;
11604
}
11605
 
11606
/* Malloc space for a dwarf2_cu for OBJFILE and initialize it.  */
11607
 
11608
static struct dwarf2_cu *
11609
alloc_one_comp_unit (struct objfile *objfile)
11610
{
11611
  struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
11612
  cu->objfile = objfile;
11613
  obstack_init (&cu->comp_unit_obstack);
11614
  return cu;
11615
}
11616
 
11617
/* Release one cached compilation unit, CU.  We unlink it from the tree
11618
   of compilation units, but we don't remove it from the read_in_chain;
11619
   the caller is responsible for that.
11620
   NOTE: DATA is a void * because this function is also used as a
11621
   cleanup routine.  */
11622
 
11623
static void
11624
free_one_comp_unit (void *data)
11625
{
11626
  struct dwarf2_cu *cu = data;
11627
 
11628
  if (cu->per_cu != NULL)
11629
    cu->per_cu->cu = NULL;
11630
  cu->per_cu = NULL;
11631
 
11632
  obstack_free (&cu->comp_unit_obstack, NULL);
11633
 
11634
  xfree (cu);
11635
}
11636
 
11637
/* This cleanup function is passed the address of a dwarf2_cu on the stack
11638
   when we're finished with it.  We can't free the pointer itself, but be
11639
   sure to unlink it from the cache.  Also release any associated storage
11640
   and perform cache maintenance.
11641
 
11642
   Only used during partial symbol parsing.  */
11643
 
11644
static void
11645
free_stack_comp_unit (void *data)
11646
{
11647
  struct dwarf2_cu *cu = data;
11648
 
11649
  obstack_free (&cu->comp_unit_obstack, NULL);
11650
  cu->partial_dies = NULL;
11651
 
11652
  if (cu->per_cu != NULL)
11653
    {
11654
      /* This compilation unit is on the stack in our caller, so we
11655
         should not xfree it.  Just unlink it.  */
11656
      cu->per_cu->cu = NULL;
11657
      cu->per_cu = NULL;
11658
 
11659
      /* If we had a per-cu pointer, then we may have other compilation
11660
         units loaded, so age them now.  */
11661
      age_cached_comp_units ();
11662
    }
11663
}
11664
 
11665
/* Free all cached compilation units.  */
11666
 
11667
static void
11668
free_cached_comp_units (void *data)
11669
{
11670
  struct dwarf2_per_cu_data *per_cu, **last_chain;
11671
 
11672
  per_cu = dwarf2_per_objfile->read_in_chain;
11673
  last_chain = &dwarf2_per_objfile->read_in_chain;
11674
  while (per_cu != NULL)
11675
    {
11676
      struct dwarf2_per_cu_data *next_cu;
11677
 
11678
      next_cu = per_cu->cu->read_in_chain;
11679
 
11680
      free_one_comp_unit (per_cu->cu);
11681
      *last_chain = next_cu;
11682
 
11683
      per_cu = next_cu;
11684
    }
11685
}
11686
 
11687
/* Increase the age counter on each cached compilation unit, and free
11688
   any that are too old.  */
11689
 
11690
static void
11691
age_cached_comp_units (void)
11692
{
11693
  struct dwarf2_per_cu_data *per_cu, **last_chain;
11694
 
11695
  dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
11696
  per_cu = dwarf2_per_objfile->read_in_chain;
11697
  while (per_cu != NULL)
11698
    {
11699
      per_cu->cu->last_used ++;
11700
      if (per_cu->cu->last_used <= dwarf2_max_cache_age)
11701
        dwarf2_mark (per_cu->cu);
11702
      per_cu = per_cu->cu->read_in_chain;
11703
    }
11704
 
11705
  per_cu = dwarf2_per_objfile->read_in_chain;
11706
  last_chain = &dwarf2_per_objfile->read_in_chain;
11707
  while (per_cu != NULL)
11708
    {
11709
      struct dwarf2_per_cu_data *next_cu;
11710
 
11711
      next_cu = per_cu->cu->read_in_chain;
11712
 
11713
      if (!per_cu->cu->mark)
11714
        {
11715
          free_one_comp_unit (per_cu->cu);
11716
          *last_chain = next_cu;
11717
        }
11718
      else
11719
        last_chain = &per_cu->cu->read_in_chain;
11720
 
11721
      per_cu = next_cu;
11722
    }
11723
}
11724
 
11725
/* Remove a single compilation unit from the cache.  */
11726
 
11727
static void
11728
free_one_cached_comp_unit (void *target_cu)
11729
{
11730
  struct dwarf2_per_cu_data *per_cu, **last_chain;
11731
 
11732
  per_cu = dwarf2_per_objfile->read_in_chain;
11733
  last_chain = &dwarf2_per_objfile->read_in_chain;
11734
  while (per_cu != NULL)
11735
    {
11736
      struct dwarf2_per_cu_data *next_cu;
11737
 
11738
      next_cu = per_cu->cu->read_in_chain;
11739
 
11740
      if (per_cu->cu == target_cu)
11741
        {
11742
          free_one_comp_unit (per_cu->cu);
11743
          *last_chain = next_cu;
11744
          break;
11745
        }
11746
      else
11747
        last_chain = &per_cu->cu->read_in_chain;
11748
 
11749
      per_cu = next_cu;
11750
    }
11751
}
11752
 
11753
/* Release all extra memory associated with OBJFILE.  */
11754
 
11755
void
11756
dwarf2_free_objfile (struct objfile *objfile)
11757
{
11758
  dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
11759
 
11760
  if (dwarf2_per_objfile == NULL)
11761
    return;
11762
 
11763
  /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
11764
  free_cached_comp_units (NULL);
11765
 
11766
  /* Everything else should be on the objfile obstack.  */
11767
}
11768
 
11769
/* A pair of DIE offset and GDB type pointer.  We store these
11770
   in a hash table separate from the DIEs, and preserve them
11771
   when the DIEs are flushed out of cache.  */
11772
 
11773
struct dwarf2_offset_and_type
11774
{
11775
  unsigned int offset;
11776
  struct type *type;
11777
};
11778
 
11779
/* Hash function for a dwarf2_offset_and_type.  */
11780
 
11781
static hashval_t
11782
offset_and_type_hash (const void *item)
11783
{
11784
  const struct dwarf2_offset_and_type *ofs = item;
11785
  return ofs->offset;
11786
}
11787
 
11788
/* Equality function for a dwarf2_offset_and_type.  */
11789
 
11790
static int
11791
offset_and_type_eq (const void *item_lhs, const void *item_rhs)
11792
{
11793
  const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
11794
  const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
11795
  return ofs_lhs->offset == ofs_rhs->offset;
11796
}
11797
 
11798
/* Set the type associated with DIE to TYPE.  Save it in CU's hash
11799
   table if necessary.  For convenience, return TYPE.  */
11800
 
11801
static struct type *
11802
set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11803
{
11804
  struct dwarf2_offset_and_type **slot, ofs;
11805
 
11806
  /* For Ada types, make sure that the gnat-specific data is always
11807
     initialized (if not already set).  There are a few types where
11808
     we should not be doing so, because the type-specific area is
11809
     already used to hold some other piece of info (eg: TYPE_CODE_FLT
11810
     where the type-specific area is used to store the floatformat).
11811
     But this is not a problem, because the gnat-specific information
11812
     is actually not needed for these types.  */
11813
  if (need_gnat_info (cu)
11814
      && TYPE_CODE (type) != TYPE_CODE_FUNC
11815
      && TYPE_CODE (type) != TYPE_CODE_FLT
11816
      && !HAVE_GNAT_AUX_INFO (type))
11817
    INIT_GNAT_SPECIFIC (type);
11818
 
11819
  if (cu->type_hash == NULL)
11820
    {
11821
      gdb_assert (cu->per_cu != NULL);
11822
      cu->per_cu->type_hash
11823
        = htab_create_alloc_ex (cu->header.length / 24,
11824
                                offset_and_type_hash,
11825
                                offset_and_type_eq,
11826
                                NULL,
11827
                                &cu->objfile->objfile_obstack,
11828
                                hashtab_obstack_allocate,
11829
                                dummy_obstack_deallocate);
11830
      cu->type_hash = cu->per_cu->type_hash;
11831
    }
11832
 
11833
  ofs.offset = die->offset;
11834
  ofs.type = type;
11835
  slot = (struct dwarf2_offset_and_type **)
11836
    htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
11837
  *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
11838
  **slot = ofs;
11839
  return type;
11840
}
11841
 
11842
/* Find the type for DIE in CU's type_hash, or return NULL if DIE does
11843
   not have a saved type.  */
11844
 
11845
static struct type *
11846
get_die_type (struct die_info *die, struct dwarf2_cu *cu)
11847
{
11848
  struct dwarf2_offset_and_type *slot, ofs;
11849
  htab_t type_hash = cu->type_hash;
11850
 
11851
  if (type_hash == NULL)
11852
    return NULL;
11853
 
11854
  ofs.offset = die->offset;
11855
  slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
11856
  if (slot)
11857
    return slot->type;
11858
  else
11859
    return NULL;
11860
}
11861
 
11862
/* Add a dependence relationship from CU to REF_PER_CU.  */
11863
 
11864
static void
11865
dwarf2_add_dependence (struct dwarf2_cu *cu,
11866
                       struct dwarf2_per_cu_data *ref_per_cu)
11867
{
11868
  void **slot;
11869
 
11870
  if (cu->dependencies == NULL)
11871
    cu->dependencies
11872
      = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
11873
                              NULL, &cu->comp_unit_obstack,
11874
                              hashtab_obstack_allocate,
11875
                              dummy_obstack_deallocate);
11876
 
11877
  slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
11878
  if (*slot == NULL)
11879
    *slot = ref_per_cu;
11880
}
11881
 
11882
/* Subroutine of dwarf2_mark to pass to htab_traverse.
11883
   Set the mark field in every compilation unit in the
11884
   cache that we must keep because we are keeping CU.  */
11885
 
11886
static int
11887
dwarf2_mark_helper (void **slot, void *data)
11888
{
11889
  struct dwarf2_per_cu_data *per_cu;
11890
 
11891
  per_cu = (struct dwarf2_per_cu_data *) *slot;
11892
  if (per_cu->cu->mark)
11893
    return 1;
11894
  per_cu->cu->mark = 1;
11895
 
11896
  if (per_cu->cu->dependencies != NULL)
11897
    htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
11898
 
11899
  return 1;
11900
}
11901
 
11902
/* Set the mark field in CU and in every other compilation unit in the
11903
   cache that we must keep because we are keeping CU.  */
11904
 
11905
static void
11906
dwarf2_mark (struct dwarf2_cu *cu)
11907
{
11908
  if (cu->mark)
11909
    return;
11910
  cu->mark = 1;
11911
  if (cu->dependencies != NULL)
11912
    htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
11913
}
11914
 
11915
static void
11916
dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
11917
{
11918
  while (per_cu)
11919
    {
11920
      per_cu->cu->mark = 0;
11921
      per_cu = per_cu->cu->read_in_chain;
11922
    }
11923
}
11924
 
11925
/* Trivial hash function for partial_die_info: the hash value of a DIE
11926
   is its offset in .debug_info for this objfile.  */
11927
 
11928
static hashval_t
11929
partial_die_hash (const void *item)
11930
{
11931
  const struct partial_die_info *part_die = item;
11932
  return part_die->offset;
11933
}
11934
 
11935
/* Trivial comparison function for partial_die_info structures: two DIEs
11936
   are equal if they have the same offset.  */
11937
 
11938
static int
11939
partial_die_eq (const void *item_lhs, const void *item_rhs)
11940
{
11941
  const struct partial_die_info *part_die_lhs = item_lhs;
11942
  const struct partial_die_info *part_die_rhs = item_rhs;
11943
  return part_die_lhs->offset == part_die_rhs->offset;
11944
}
11945
 
11946
static struct cmd_list_element *set_dwarf2_cmdlist;
11947
static struct cmd_list_element *show_dwarf2_cmdlist;
11948
 
11949
static void
11950
set_dwarf2_cmd (char *args, int from_tty)
11951
{
11952
  help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
11953
}
11954
 
11955
static void
11956
show_dwarf2_cmd (char *args, int from_tty)
11957
{
11958
  cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
11959
}
11960
 
11961
/* If section described by INFO was mmapped, munmap it now.  */
11962
 
11963
static void
11964
munmap_section_buffer (struct dwarf2_section_info *info)
11965
{
11966
  if (info->was_mmapped)
11967
    {
11968
#ifdef HAVE_MMAP
11969
      intptr_t begin = (intptr_t) info->buffer;
11970
      intptr_t map_begin = begin & ~(pagesize - 1);
11971
      size_t map_length = info->size + begin - map_begin;
11972
      gdb_assert (munmap ((void *) map_begin, map_length) == 0);
11973
#else
11974
      /* Without HAVE_MMAP, we should never be here to begin with.  */
11975
      gdb_assert (0);
11976
#endif
11977
    }
11978
}
11979
 
11980
/* munmap debug sections for OBJFILE, if necessary.  */
11981
 
11982
static void
11983
dwarf2_per_objfile_free (struct objfile *objfile, void *d)
11984
{
11985
  struct dwarf2_per_objfile *data = d;
11986
  munmap_section_buffer (&data->info);
11987
  munmap_section_buffer (&data->abbrev);
11988
  munmap_section_buffer (&data->line);
11989
  munmap_section_buffer (&data->str);
11990
  munmap_section_buffer (&data->macinfo);
11991
  munmap_section_buffer (&data->ranges);
11992
  munmap_section_buffer (&data->loc);
11993
  munmap_section_buffer (&data->frame);
11994
  munmap_section_buffer (&data->eh_frame);
11995
}
11996
 
11997
void _initialize_dwarf2_read (void);
11998
 
11999
void
12000
_initialize_dwarf2_read (void)
12001
{
12002
  dwarf2_objfile_data_key
12003
    = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
12004
 
12005
  add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
12006
Set DWARF 2 specific variables.\n\
12007
Configure DWARF 2 variables such as the cache size"),
12008
                  &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
12009
                  0/*allow-unknown*/, &maintenance_set_cmdlist);
12010
 
12011
  add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
12012
Show DWARF 2 specific variables\n\
12013
Show DWARF 2 variables such as the cache size"),
12014
                  &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
12015
                  0/*allow-unknown*/, &maintenance_show_cmdlist);
12016
 
12017
  add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
12018
                            &dwarf2_max_cache_age, _("\
12019
Set the upper bound on the age of cached dwarf2 compilation units."), _("\
12020
Show the upper bound on the age of cached dwarf2 compilation units."), _("\
12021
A higher limit means that cached compilation units will be stored\n\
12022
in memory longer, and more total memory will be used.  Zero disables\n\
12023
caching, which can slow down startup."),
12024
                            NULL,
12025
                            show_dwarf2_max_cache_age,
12026
                            &set_dwarf2_cmdlist,
12027
                            &show_dwarf2_cmdlist);
12028
 
12029
  add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
12030
Set debugging of the dwarf2 DIE reader."), _("\
12031
Show debugging of the dwarf2 DIE reader."), _("\
12032
When enabled (non-zero), DIEs are dumped after they are read in.\n\
12033
The value is the maximum depth to print."),
12034
                            NULL,
12035
                            NULL,
12036
                            &setdebuglist, &showdebuglist);
12037
}

powered by: WebSVN 2.1.0

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