* dwarf2read.c (create_addrmap_from_index): Complain about bad CU
[external/binutils.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994-2013 Free Software Foundation, Inc.
4
5    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6    Inc.  with support from Florida State University (under contract
7    with the Ada Joint Program Office), and Silicon Graphics, Inc.
8    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10    support.
11
12    This file is part of GDB.
13
14    This program is free software; you can redistribute it and/or modify
15    it under the terms of the GNU General Public License as published by
16    the Free Software Foundation; either version 3 of the License, or
17    (at your option) any later version.
18
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License for more details.
23
24    You should have received a copy of the GNU General Public License
25    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28    reading off the end of the section.
29    E.g., load_partial_dies, read_partial_die.  */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h"  /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "jv-lang.h"
57 #include "psympriv.h"
58 #include "exceptions.h"
59 #include "gdb_stat.h"
60 #include "completer.h"
61 #include "vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71
72 #include <fcntl.h>
73 #include "gdb_string.h"
74 #include "gdb_assert.h"
75 #include <sys/types.h>
76
77 typedef struct symbol *symbolp;
78 DEF_VEC_P (symbolp);
79
80 /* When non-zero, print basic high level tracing messages.
81    This is in contrast to the low level DIE reading of dwarf2_die_debug.  */
82 static int dwarf2_read_debug = 0;
83
84 /* When non-zero, dump DIEs after they are read in.  */
85 static unsigned int dwarf2_die_debug = 0;
86
87 /* When non-zero, cross-check physname against demangler.  */
88 static int check_physname = 0;
89
90 /* When non-zero, do not reject deprecated .gdb_index sections.  */
91 static int use_deprecated_index_sections = 0;
92
93 static const struct objfile_data *dwarf2_objfile_data_key;
94
95 /* The "aclass" indices for various kinds of computed DWARF symbols.  */
96
97 static int dwarf2_locexpr_index;
98 static int dwarf2_loclist_index;
99 static int dwarf2_locexpr_block_index;
100 static int dwarf2_loclist_block_index;
101
102 struct dwarf2_section_info
103 {
104   asection *asection;
105   gdb_byte *buffer;
106   bfd_size_type size;
107   /* True if we have tried to read this section.  */
108   int readin;
109 };
110
111 typedef struct dwarf2_section_info dwarf2_section_info_def;
112 DEF_VEC_O (dwarf2_section_info_def);
113
114 /* All offsets in the index are of this type.  It must be
115    architecture-independent.  */
116 typedef uint32_t offset_type;
117
118 DEF_VEC_I (offset_type);
119
120 /* Ensure only legit values are used.  */
121 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
122   do { \
123     gdb_assert ((unsigned int) (value) <= 1); \
124     GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
125   } while (0)
126
127 /* Ensure only legit values are used.  */
128 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
129   do { \
130     gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
131                 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
132     GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
133   } while (0)
134
135 /* Ensure we don't use more than the alloted nuber of bits for the CU.  */
136 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
137   do { \
138     gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
139     GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
140   } while (0)
141
142 /* A description of the mapped index.  The file format is described in
143    a comment by the code that writes the index.  */
144 struct mapped_index
145 {
146   /* Index data format version.  */
147   int version;
148
149   /* The total length of the buffer.  */
150   off_t total_size;
151
152   /* A pointer to the address table data.  */
153   const gdb_byte *address_table;
154
155   /* Size of the address table data in bytes.  */
156   offset_type address_table_size;
157
158   /* The symbol table, implemented as a hash table.  */
159   const offset_type *symbol_table;
160
161   /* Size in slots, each slot is 2 offset_types.  */
162   offset_type symbol_table_slots;
163
164   /* A pointer to the constant pool.  */
165   const char *constant_pool;
166 };
167
168 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
169 DEF_VEC_P (dwarf2_per_cu_ptr);
170
171 /* Collection of data recorded per objfile.
172    This hangs off of dwarf2_objfile_data_key.  */
173
174 struct dwarf2_per_objfile
175 {
176   struct dwarf2_section_info info;
177   struct dwarf2_section_info abbrev;
178   struct dwarf2_section_info line;
179   struct dwarf2_section_info loc;
180   struct dwarf2_section_info macinfo;
181   struct dwarf2_section_info macro;
182   struct dwarf2_section_info str;
183   struct dwarf2_section_info ranges;
184   struct dwarf2_section_info addr;
185   struct dwarf2_section_info frame;
186   struct dwarf2_section_info eh_frame;
187   struct dwarf2_section_info gdb_index;
188
189   VEC (dwarf2_section_info_def) *types;
190
191   /* Back link.  */
192   struct objfile *objfile;
193
194   /* Table of all the compilation units.  This is used to locate
195      the target compilation unit of a particular reference.  */
196   struct dwarf2_per_cu_data **all_comp_units;
197
198   /* The number of compilation units in ALL_COMP_UNITS.  */
199   int n_comp_units;
200
201   /* The number of .debug_types-related CUs.  */
202   int n_type_units;
203
204   /* The .debug_types-related CUs (TUs).  */
205   struct signatured_type **all_type_units;
206
207   /* The number of entries in all_type_unit_groups.  */
208   int n_type_unit_groups;
209
210   /* Table of type unit groups.
211      This exists to make it easy to iterate over all CUs and TU groups.  */
212   struct type_unit_group **all_type_unit_groups;
213
214   /* Table of struct type_unit_group objects.
215      The hash key is the DW_AT_stmt_list value.  */
216   htab_t type_unit_groups;
217
218   /* A table mapping .debug_types signatures to its signatured_type entry.
219      This is NULL if the .debug_types section hasn't been read in yet.  */
220   htab_t signatured_types;
221
222   /* Type unit statistics, to see how well the scaling improvements
223      are doing.  */
224   struct tu_stats
225   {
226     int nr_uniq_abbrev_tables;
227     int nr_symtabs;
228     int nr_symtab_sharers;
229     int nr_stmt_less_type_units;
230   } tu_stats;
231
232   /* A chain of compilation units that are currently read in, so that
233      they can be freed later.  */
234   struct dwarf2_per_cu_data *read_in_chain;
235
236   /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
237      This is NULL if the table hasn't been allocated yet.  */
238   htab_t dwo_files;
239
240   /* Non-zero if we've check for whether there is a DWP file.  */
241   int dwp_checked;
242
243   /* The DWP file if there is one, or NULL.  */
244   struct dwp_file *dwp_file;
245
246   /* The shared '.dwz' file, if one exists.  This is used when the
247      original data was compressed using 'dwz -m'.  */
248   struct dwz_file *dwz_file;
249
250   /* A flag indicating wether this objfile has a section loaded at a
251      VMA of 0.  */
252   int has_section_at_zero;
253
254   /* True if we are using the mapped index,
255      or we are faking it for OBJF_READNOW's sake.  */
256   unsigned char using_index;
257
258   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
259   struct mapped_index *index_table;
260
261   /* When using index_table, this keeps track of all quick_file_names entries.
262      TUs typically share line table entries with a CU, so we maintain a
263      separate table of all line table entries to support the sharing.
264      Note that while there can be way more TUs than CUs, we've already
265      sorted all the TUs into "type unit groups", grouped by their
266      DW_AT_stmt_list value.  Therefore the only sharing done here is with a
267      CU and its associated TU group if there is one.  */
268   htab_t quick_file_names_table;
269
270   /* Set during partial symbol reading, to prevent queueing of full
271      symbols.  */
272   int reading_partial_symbols;
273
274   /* Table mapping type DIEs to their struct type *.
275      This is NULL if not allocated yet.
276      The mapping is done via (CU/TU signature + DIE offset) -> type.  */
277   htab_t die_type_hash;
278
279   /* The CUs we recently read.  */
280   VEC (dwarf2_per_cu_ptr) *just_read_cus;
281 };
282
283 static struct dwarf2_per_objfile *dwarf2_per_objfile;
284
285 /* Default names of the debugging sections.  */
286
287 /* Note that if the debugging section has been compressed, it might
288    have a name like .zdebug_info.  */
289
290 static const struct dwarf2_debug_sections dwarf2_elf_names =
291 {
292   { ".debug_info", ".zdebug_info" },
293   { ".debug_abbrev", ".zdebug_abbrev" },
294   { ".debug_line", ".zdebug_line" },
295   { ".debug_loc", ".zdebug_loc" },
296   { ".debug_macinfo", ".zdebug_macinfo" },
297   { ".debug_macro", ".zdebug_macro" },
298   { ".debug_str", ".zdebug_str" },
299   { ".debug_ranges", ".zdebug_ranges" },
300   { ".debug_types", ".zdebug_types" },
301   { ".debug_addr", ".zdebug_addr" },
302   { ".debug_frame", ".zdebug_frame" },
303   { ".eh_frame", NULL },
304   { ".gdb_index", ".zgdb_index" },
305   23
306 };
307
308 /* List of DWO/DWP sections.  */
309
310 static const struct dwop_section_names
311 {
312   struct dwarf2_section_names abbrev_dwo;
313   struct dwarf2_section_names info_dwo;
314   struct dwarf2_section_names line_dwo;
315   struct dwarf2_section_names loc_dwo;
316   struct dwarf2_section_names macinfo_dwo;
317   struct dwarf2_section_names macro_dwo;
318   struct dwarf2_section_names str_dwo;
319   struct dwarf2_section_names str_offsets_dwo;
320   struct dwarf2_section_names types_dwo;
321   struct dwarf2_section_names cu_index;
322   struct dwarf2_section_names tu_index;
323 }
324 dwop_section_names =
325 {
326   { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327   { ".debug_info.dwo", ".zdebug_info.dwo" },
328   { ".debug_line.dwo", ".zdebug_line.dwo" },
329   { ".debug_loc.dwo", ".zdebug_loc.dwo" },
330   { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
331   { ".debug_macro.dwo", ".zdebug_macro.dwo" },
332   { ".debug_str.dwo", ".zdebug_str.dwo" },
333   { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
334   { ".debug_types.dwo", ".zdebug_types.dwo" },
335   { ".debug_cu_index", ".zdebug_cu_index" },
336   { ".debug_tu_index", ".zdebug_tu_index" },
337 };
338
339 /* local data types */
340
341 /* The data in a compilation unit header, after target2host
342    translation, looks like this.  */
343 struct comp_unit_head
344 {
345   unsigned int length;
346   short version;
347   unsigned char addr_size;
348   unsigned char signed_addr_p;
349   sect_offset abbrev_offset;
350
351   /* Size of file offsets; either 4 or 8.  */
352   unsigned int offset_size;
353
354   /* Size of the length field; either 4 or 12.  */
355   unsigned int initial_length_size;
356
357   /* Offset to the first byte of this compilation unit header in the
358      .debug_info section, for resolving relative reference dies.  */
359   sect_offset offset;
360
361   /* Offset to first die in this cu from the start of the cu.
362      This will be the first byte following the compilation unit header.  */
363   cu_offset first_die_offset;
364 };
365
366 /* Type used for delaying computation of method physnames.
367    See comments for compute_delayed_physnames.  */
368 struct delayed_method_info
369 {
370   /* The type to which the method is attached, i.e., its parent class.  */
371   struct type *type;
372
373   /* The index of the method in the type's function fieldlists.  */
374   int fnfield_index;
375
376   /* The index of the method in the fieldlist.  */
377   int index;
378
379   /* The name of the DIE.  */
380   const char *name;
381
382   /*  The DIE associated with this method.  */
383   struct die_info *die;
384 };
385
386 typedef struct delayed_method_info delayed_method_info;
387 DEF_VEC_O (delayed_method_info);
388
389 /* Internal state when decoding a particular compilation unit.  */
390 struct dwarf2_cu
391 {
392   /* The objfile containing this compilation unit.  */
393   struct objfile *objfile;
394
395   /* The header of the compilation unit.  */
396   struct comp_unit_head header;
397
398   /* Base address of this compilation unit.  */
399   CORE_ADDR base_address;
400
401   /* Non-zero if base_address has been set.  */
402   int base_known;
403
404   /* The language we are debugging.  */
405   enum language language;
406   const struct language_defn *language_defn;
407
408   const char *producer;
409
410   /* The generic symbol table building routines have separate lists for
411      file scope symbols and all all other scopes (local scopes).  So
412      we need to select the right one to pass to add_symbol_to_list().
413      We do it by keeping a pointer to the correct list in list_in_scope.
414
415      FIXME: The original dwarf code just treated the file scope as the
416      first local scope, and all other local scopes as nested local
417      scopes, and worked fine.  Check to see if we really need to
418      distinguish these in buildsym.c.  */
419   struct pending **list_in_scope;
420
421   /* The abbrev table for this CU.
422      Normally this points to the abbrev table in the objfile.
423      But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file.  */
424   struct abbrev_table *abbrev_table;
425
426   /* Hash table holding all the loaded partial DIEs
427      with partial_die->offset.SECT_OFF as hash.  */
428   htab_t partial_dies;
429
430   /* Storage for things with the same lifetime as this read-in compilation
431      unit, including partial DIEs.  */
432   struct obstack comp_unit_obstack;
433
434   /* When multiple dwarf2_cu structures are living in memory, this field
435      chains them all together, so that they can be released efficiently.
436      We will probably also want a generation counter so that most-recently-used
437      compilation units are cached...  */
438   struct dwarf2_per_cu_data *read_in_chain;
439
440   /* Backchain to our per_cu entry if the tree has been built.  */
441   struct dwarf2_per_cu_data *per_cu;
442
443   /* How many compilation units ago was this CU last referenced?  */
444   int last_used;
445
446   /* A hash table of DIE cu_offset for following references with
447      die_info->offset.sect_off as hash.  */
448   htab_t die_hash;
449
450   /* Full DIEs if read in.  */
451   struct die_info *dies;
452
453   /* A set of pointers to dwarf2_per_cu_data objects for compilation
454      units referenced by this one.  Only set during full symbol processing;
455      partial symbol tables do not have dependencies.  */
456   htab_t dependencies;
457
458   /* Header data from the line table, during full symbol processing.  */
459   struct line_header *line_header;
460
461   /* A list of methods which need to have physnames computed
462      after all type information has been read.  */
463   VEC (delayed_method_info) *method_list;
464
465   /* To be copied to symtab->call_site_htab.  */
466   htab_t call_site_htab;
467
468   /* Non-NULL if this CU came from a DWO file.
469      There is an invariant here that is important to remember:
470      Except for attributes copied from the top level DIE in the "main"
471      (or "stub") file in preparation for reading the DWO file
472      (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
473      Either there isn't a DWO file (in which case this is NULL and the point
474      is moot), or there is and either we're not going to read it (in which
475      case this is NULL) or there is and we are reading it (in which case this
476      is non-NULL).  */
477   struct dwo_unit *dwo_unit;
478
479   /* The DW_AT_addr_base attribute if present, zero otherwise
480      (zero is a valid value though).
481      Note this value comes from the stub CU/TU's DIE.  */
482   ULONGEST addr_base;
483
484   /* The DW_AT_ranges_base attribute if present, zero otherwise
485      (zero is a valid value though).
486      Note this value comes from the stub CU/TU's DIE.
487      Also note that the value is zero in the non-DWO case so this value can
488      be used without needing to know whether DWO files are in use or not.
489      N.B. This does not apply to DW_AT_ranges appearing in
490      DW_TAG_compile_unit dies.  This is a bit of a wart, consider if ever
491      DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492      DW_AT_ranges_base *would* have to be applied, and we'd have to care
493      whether the DW_AT_ranges attribute came from the skeleton or DWO.  */
494   ULONGEST ranges_base;
495
496   /* Mark used when releasing cached dies.  */
497   unsigned int mark : 1;
498
499   /* This CU references .debug_loc.  See the symtab->locations_valid field.
500      This test is imperfect as there may exist optimized debug code not using
501      any location list and still facing inlining issues if handled as
502      unoptimized code.  For a future better test see GCC PR other/32998.  */
503   unsigned int has_loclist : 1;
504
505   /* These cache the results for producer_is_* fields.  CHECKED_PRODUCER is set
506      if all the producer_is_* fields are valid.  This information is cached
507      because profiling CU expansion showed excessive time spent in
508      producer_is_gxx_lt_4_6.  */
509   unsigned int checked_producer : 1;
510   unsigned int producer_is_gxx_lt_4_6 : 1;
511   unsigned int producer_is_gcc_lt_4_3 : 1;
512   unsigned int producer_is_icc : 1;
513
514   /* When set, the file that we're processing is known to have
515      debugging info for C++ namespaces.  GCC 3.3.x did not produce
516      this information, but later versions do.  */
517
518   unsigned int processing_has_namespace_info : 1;
519 };
520
521 /* Persistent data held for a compilation unit, even when not
522    processing it.  We put a pointer to this structure in the
523    read_symtab_private field of the psymtab.  */
524
525 struct dwarf2_per_cu_data
526 {
527   /* The start offset and length of this compilation unit.
528      NOTE: Unlike comp_unit_head.length, this length includes
529      initial_length_size.
530      If the DIE refers to a DWO file, this is always of the original die,
531      not the DWO file.  */
532   sect_offset offset;
533   unsigned int length;
534
535   /* Flag indicating this compilation unit will be read in before
536      any of the current compilation units are processed.  */
537   unsigned int queued : 1;
538
539   /* This flag will be set when reading partial DIEs if we need to load
540      absolutely all DIEs for this compilation unit, instead of just the ones
541      we think are interesting.  It gets set if we look for a DIE in the
542      hash table and don't find it.  */
543   unsigned int load_all_dies : 1;
544
545   /* Non-zero if this CU is from .debug_types.  */
546   unsigned int is_debug_types : 1;
547
548   /* Non-zero if this CU is from the .dwz file.  */
549   unsigned int is_dwz : 1;
550
551   /* The section this CU/TU lives in.
552      If the DIE refers to a DWO file, this is always the original die,
553      not the DWO file.  */
554   struct dwarf2_section_info *section;
555
556   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
557      of the CU cache it gets reset to NULL again.  */
558   struct dwarf2_cu *cu;
559
560   /* The corresponding objfile.
561      Normally we can get the objfile from dwarf2_per_objfile.
562      However we can enter this file with just a "per_cu" handle.  */
563   struct objfile *objfile;
564
565   /* When using partial symbol tables, the 'psymtab' field is active.
566      Otherwise the 'quick' field is active.  */
567   union
568   {
569     /* The partial symbol table associated with this compilation unit,
570        or NULL for unread partial units.  */
571     struct partial_symtab *psymtab;
572
573     /* Data needed by the "quick" functions.  */
574     struct dwarf2_per_cu_quick_data *quick;
575   } v;
576
577   /* The CUs we import using DW_TAG_imported_unit.  This is filled in
578      while reading psymtabs, used to compute the psymtab dependencies,
579      and then cleared.  Then it is filled in again while reading full
580      symbols, and only deleted when the objfile is destroyed.
581
582      This is also used to work around a difference between the way gold
583      generates .gdb_index version <=7 and the way gdb does.  Arguably this
584      is a gold bug.  For symbols coming from TUs, gold records in the index
585      the CU that includes the TU instead of the TU itself.  This breaks
586      dw2_lookup_symbol: It assumes that if the index says symbol X lives
587      in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
588      will find X.  Alas TUs live in their own symtab, so after expanding CU Y
589      we need to look in TU Z to find X.  Fortunately, this is akin to
590      DW_TAG_imported_unit, so we just use the same mechanism: For
591      .gdb_index version <=7 this also records the TUs that the CU referred
592      to.  Concurrently with this change gdb was modified to emit version 8
593      indices so we only pay a price for gold generated indices.  */
594   VEC (dwarf2_per_cu_ptr) *imported_symtabs;
595
596   /* Type units are grouped by their DW_AT_stmt_list entry so that they
597      can share them.  If this is a TU, this points to the containing
598      symtab.  */
599   struct type_unit_group *type_unit_group;
600 };
601
602 /* Entry in the signatured_types hash table.  */
603
604 struct signatured_type
605 {
606   /* The "per_cu" object of this type.
607      N.B.: This is the first member so that it's easy to convert pointers
608      between them.  */
609   struct dwarf2_per_cu_data per_cu;
610
611   /* The type's signature.  */
612   ULONGEST signature;
613
614   /* Offset in the TU of the type's DIE, as read from the TU header.
615      If the definition lives in a DWO file, this value is unusable.  */
616   cu_offset type_offset_in_tu;
617
618   /* Offset in the section of the type's DIE.
619      If the definition lives in a DWO file, this is the offset in the
620      .debug_types.dwo section.
621      The value is zero until the actual value is known.
622      Zero is otherwise not a valid section offset.  */
623   sect_offset type_offset_in_section;
624 };
625
626 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
627    This includes type_unit_group and quick_file_names.  */
628
629 struct stmt_list_hash
630 {
631   /* The DWO unit this table is from or NULL if there is none.  */
632   struct dwo_unit *dwo_unit;
633
634   /* Offset in .debug_line or .debug_line.dwo.  */
635   sect_offset line_offset;
636 };
637
638 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
639    an object of this type.  */
640
641 struct type_unit_group
642 {
643   /* dwarf2read.c's main "handle" on the symtab.
644      To simplify things we create an artificial CU that "includes" all the
645      type units using this stmt_list so that the rest of the code still has
646      a "per_cu" handle on the symtab.
647      This PER_CU is recognized by having no section.  */
648 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
649   struct dwarf2_per_cu_data per_cu;
650
651   union
652   {
653     /* The TUs that share this DW_AT_stmt_list entry.
654        This is added to while parsing type units to build partial symtabs,
655        and is deleted afterwards and not used again.  */
656     VEC (dwarf2_per_cu_ptr) *tus;
657
658     /* When reading the line table in "quick" functions, we need a real TU.
659        Any will do, we know they all share the same DW_AT_stmt_list entry.
660        For simplicity's sake, we pick the first one.  */
661     struct dwarf2_per_cu_data *first_tu;
662   } t;
663
664   /* The primary symtab.
665      Type units in a group needn't all be defined in the same source file,
666      so we create an essentially anonymous symtab as the primary symtab.  */
667   struct symtab *primary_symtab;
668
669   /* The data used to construct the hash key.  */
670   struct stmt_list_hash hash;
671
672   /* The number of symtabs from the line header.
673      The value here must match line_header.num_file_names.  */
674   unsigned int num_symtabs;
675
676   /* The symbol tables for this TU (obtained from the files listed in
677      DW_AT_stmt_list).
678      WARNING: The order of entries here must match the order of entries
679      in the line header.  After the first TU using this type_unit_group, the
680      line header for the subsequent TUs is recreated from this.  This is done
681      because we need to use the same symtabs for each TU using the same
682      DW_AT_stmt_list value.  Also note that symtabs may be repeated here,
683      there's no guarantee the line header doesn't have duplicate entries.  */
684   struct symtab **symtabs;
685 };
686
687 /* These sections are what may appear in a DWO file.  */
688
689 struct dwo_sections
690 {
691   struct dwarf2_section_info abbrev;
692   struct dwarf2_section_info line;
693   struct dwarf2_section_info loc;
694   struct dwarf2_section_info macinfo;
695   struct dwarf2_section_info macro;
696   struct dwarf2_section_info str;
697   struct dwarf2_section_info str_offsets;
698   /* In the case of a virtual DWO file, these two are unused.  */
699   struct dwarf2_section_info info;
700   VEC (dwarf2_section_info_def) *types;
701 };
702
703 /* Common bits of DWO CUs/TUs.  */
704
705 struct dwo_unit
706 {
707   /* Backlink to the containing struct dwo_file.  */
708   struct dwo_file *dwo_file;
709
710   /* The "id" that distinguishes this CU/TU.
711      .debug_info calls this "dwo_id", .debug_types calls this "signature".
712      Since signatures came first, we stick with it for consistency.  */
713   ULONGEST signature;
714
715   /* The section this CU/TU lives in, in the DWO file.  */
716   struct dwarf2_section_info *section;
717
718   /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section.  */
719   sect_offset offset;
720   unsigned int length;
721
722   /* For types, offset in the type's DIE of the type defined by this TU.  */
723   cu_offset type_offset_in_tu;
724 };
725
726 /* Data for one DWO file.
727    This includes virtual DWO files that have been packaged into a
728    DWP file.  */
729
730 struct dwo_file
731 {
732   /* The DW_AT_GNU_dwo_name attribute.  This is the hash key.
733      For virtual DWO files the name is constructed from the section offsets
734      of abbrev,line,loc,str_offsets so that we combine virtual DWO files
735      from related CU+TUs.  */
736   const char *name;
737
738   /* The bfd, when the file is open.  Otherwise this is NULL.
739      This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd.  */
740   bfd *dbfd;
741
742   /* Section info for this file.  */
743   struct dwo_sections sections;
744
745   /* Table of CUs in the file.
746      Each element is a struct dwo_unit.  */
747   htab_t cus;
748
749   /* Table of TUs in the file.
750      Each element is a struct dwo_unit.  */
751   htab_t tus;
752 };
753
754 /* These sections are what may appear in a DWP file.  */
755
756 struct dwp_sections
757 {
758   struct dwarf2_section_info str;
759   struct dwarf2_section_info cu_index;
760   struct dwarf2_section_info tu_index;
761   /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
762      by section number.  We don't need to record them here.  */
763 };
764
765 /* These sections are what may appear in a virtual DWO file.  */
766
767 struct virtual_dwo_sections
768 {
769   struct dwarf2_section_info abbrev;
770   struct dwarf2_section_info line;
771   struct dwarf2_section_info loc;
772   struct dwarf2_section_info macinfo;
773   struct dwarf2_section_info macro;
774   struct dwarf2_section_info str_offsets;
775   /* Each DWP hash table entry records one CU or one TU.
776      That is recorded here, and copied to dwo_unit.section.  */
777   struct dwarf2_section_info info_or_types;
778 };
779
780 /* Contents of DWP hash tables.  */
781
782 struct dwp_hash_table
783 {
784   uint32_t nr_units, nr_slots;
785   const gdb_byte *hash_table, *unit_table, *section_pool;
786 };
787
788 /* Data for one DWP file.  */
789
790 struct dwp_file
791 {
792   /* Name of the file.  */
793   const char *name;
794
795   /* The bfd, when the file is open.  Otherwise this is NULL.  */
796   bfd *dbfd;
797
798   /* Section info for this file.  */
799   struct dwp_sections sections;
800
801   /* Table of CUs in the file. */
802   const struct dwp_hash_table *cus;
803
804   /* Table of TUs in the file.  */
805   const struct dwp_hash_table *tus;
806
807   /* Table of loaded CUs/TUs.  Each entry is a struct dwo_unit *.  */
808   htab_t loaded_cutus;
809
810   /* Table to map ELF section numbers to their sections.  */
811   unsigned int num_sections;
812   asection **elf_sections;
813 };
814
815 /* This represents a '.dwz' file.  */
816
817 struct dwz_file
818 {
819   /* A dwz file can only contain a few sections.  */
820   struct dwarf2_section_info abbrev;
821   struct dwarf2_section_info info;
822   struct dwarf2_section_info str;
823   struct dwarf2_section_info line;
824   struct dwarf2_section_info macro;
825   struct dwarf2_section_info gdb_index;
826
827   /* The dwz's BFD.  */
828   bfd *dwz_bfd;
829 };
830
831 /* Struct used to pass misc. parameters to read_die_and_children, et
832    al.  which are used for both .debug_info and .debug_types dies.
833    All parameters here are unchanging for the life of the call.  This
834    struct exists to abstract away the constant parameters of die reading.  */
835
836 struct die_reader_specs
837 {
838   /* die_section->asection->owner.  */
839   bfd* abfd;
840
841   /* The CU of the DIE we are parsing.  */
842   struct dwarf2_cu *cu;
843
844   /* Non-NULL if reading a DWO file (including one packaged into a DWP).  */
845   struct dwo_file *dwo_file;
846
847   /* The section the die comes from.
848      This is either .debug_info or .debug_types, or the .dwo variants.  */
849   struct dwarf2_section_info *die_section;
850
851   /* die_section->buffer.  */
852   gdb_byte *buffer;
853
854   /* The end of the buffer.  */
855   const gdb_byte *buffer_end;
856 };
857
858 /* Type of function passed to init_cutu_and_read_dies, et.al.  */
859 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
860                                       gdb_byte *info_ptr,
861                                       struct die_info *comp_unit_die,
862                                       int has_children,
863                                       void *data);
864
865 /* The line number information for a compilation unit (found in the
866    .debug_line section) begins with a "statement program header",
867    which contains the following information.  */
868 struct line_header
869 {
870   unsigned int total_length;
871   unsigned short version;
872   unsigned int header_length;
873   unsigned char minimum_instruction_length;
874   unsigned char maximum_ops_per_instruction;
875   unsigned char default_is_stmt;
876   int line_base;
877   unsigned char line_range;
878   unsigned char opcode_base;
879
880   /* standard_opcode_lengths[i] is the number of operands for the
881      standard opcode whose value is i.  This means that
882      standard_opcode_lengths[0] is unused, and the last meaningful
883      element is standard_opcode_lengths[opcode_base - 1].  */
884   unsigned char *standard_opcode_lengths;
885
886   /* The include_directories table.  NOTE!  These strings are not
887      allocated with xmalloc; instead, they are pointers into
888      debug_line_buffer.  If you try to free them, `free' will get
889      indigestion.  */
890   unsigned int num_include_dirs, include_dirs_size;
891   char **include_dirs;
892
893   /* The file_names table.  NOTE!  These strings are not allocated
894      with xmalloc; instead, they are pointers into debug_line_buffer.
895      Don't try to free them directly.  */
896   unsigned int num_file_names, file_names_size;
897   struct file_entry
898   {
899     char *name;
900     unsigned int dir_index;
901     unsigned int mod_time;
902     unsigned int length;
903     int included_p; /* Non-zero if referenced by the Line Number Program.  */
904     struct symtab *symtab; /* The associated symbol table, if any.  */
905   } *file_names;
906
907   /* The start and end of the statement program following this
908      header.  These point into dwarf2_per_objfile->line_buffer.  */
909   gdb_byte *statement_program_start, *statement_program_end;
910 };
911
912 /* When we construct a partial symbol table entry we only
913    need this much information.  */
914 struct partial_die_info
915   {
916     /* Offset of this DIE.  */
917     sect_offset offset;
918
919     /* DWARF-2 tag for this DIE.  */
920     ENUM_BITFIELD(dwarf_tag) tag : 16;
921
922     /* Assorted flags describing the data found in this DIE.  */
923     unsigned int has_children : 1;
924     unsigned int is_external : 1;
925     unsigned int is_declaration : 1;
926     unsigned int has_type : 1;
927     unsigned int has_specification : 1;
928     unsigned int has_pc_info : 1;
929     unsigned int may_be_inlined : 1;
930
931     /* Flag set if the SCOPE field of this structure has been
932        computed.  */
933     unsigned int scope_set : 1;
934
935     /* Flag set if the DIE has a byte_size attribute.  */
936     unsigned int has_byte_size : 1;
937
938     /* Flag set if any of the DIE's children are template arguments.  */
939     unsigned int has_template_arguments : 1;
940
941     /* Flag set if fixup_partial_die has been called on this die.  */
942     unsigned int fixup_called : 1;
943
944     /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt.  */
945     unsigned int is_dwz : 1;
946
947     /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt.  */
948     unsigned int spec_is_dwz : 1;
949
950     /* The name of this DIE.  Normally the value of DW_AT_name, but
951        sometimes a default name for unnamed DIEs.  */
952     const char *name;
953
954     /* The linkage name, if present.  */
955     const char *linkage_name;
956
957     /* The scope to prepend to our children.  This is generally
958        allocated on the comp_unit_obstack, so will disappear
959        when this compilation unit leaves the cache.  */
960     const char *scope;
961
962     /* Some data associated with the partial DIE.  The tag determines
963        which field is live.  */
964     union
965     {
966       /* The location description associated with this DIE, if any.  */
967       struct dwarf_block *locdesc;
968       /* The offset of an import, for DW_TAG_imported_unit.  */
969       sect_offset offset;
970     } d;
971
972     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
973     CORE_ADDR lowpc;
974     CORE_ADDR highpc;
975
976     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
977        DW_AT_sibling, if any.  */
978     /* NOTE: This member isn't strictly necessary, read_partial_die could
979        return DW_AT_sibling values to its caller load_partial_dies.  */
980     gdb_byte *sibling;
981
982     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
983        DW_AT_specification (or DW_AT_abstract_origin or
984        DW_AT_extension).  */
985     sect_offset spec_offset;
986
987     /* Pointers to this DIE's parent, first child, and next sibling,
988        if any.  */
989     struct partial_die_info *die_parent, *die_child, *die_sibling;
990   };
991
992 /* This data structure holds the information of an abbrev.  */
993 struct abbrev_info
994   {
995     unsigned int number;        /* number identifying abbrev */
996     enum dwarf_tag tag;         /* dwarf tag */
997     unsigned short has_children;                /* boolean */
998     unsigned short num_attrs;   /* number of attributes */
999     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
1000     struct abbrev_info *next;   /* next in chain */
1001   };
1002
1003 struct attr_abbrev
1004   {
1005     ENUM_BITFIELD(dwarf_attribute) name : 16;
1006     ENUM_BITFIELD(dwarf_form) form : 16;
1007   };
1008
1009 /* Size of abbrev_table.abbrev_hash_table.  */
1010 #define ABBREV_HASH_SIZE 121
1011
1012 /* Top level data structure to contain an abbreviation table.  */
1013
1014 struct abbrev_table
1015 {
1016   /* Where the abbrev table came from.
1017      This is used as a sanity check when the table is used.  */
1018   sect_offset offset;
1019
1020   /* Storage for the abbrev table.  */
1021   struct obstack abbrev_obstack;
1022
1023   /* Hash table of abbrevs.
1024      This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1025      It could be statically allocated, but the previous code didn't so we
1026      don't either.  */
1027   struct abbrev_info **abbrevs;
1028 };
1029
1030 /* Attributes have a name and a value.  */
1031 struct attribute
1032   {
1033     ENUM_BITFIELD(dwarf_attribute) name : 16;
1034     ENUM_BITFIELD(dwarf_form) form : 15;
1035
1036     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
1037        field should be in u.str (existing only for DW_STRING) but it is kept
1038        here for better struct attribute alignment.  */
1039     unsigned int string_is_canonical : 1;
1040
1041     union
1042       {
1043         const char *str;
1044         struct dwarf_block *blk;
1045         ULONGEST unsnd;
1046         LONGEST snd;
1047         CORE_ADDR addr;
1048         struct signatured_type *signatured_type;
1049       }
1050     u;
1051   };
1052
1053 /* This data structure holds a complete die structure.  */
1054 struct die_info
1055   {
1056     /* DWARF-2 tag for this DIE.  */
1057     ENUM_BITFIELD(dwarf_tag) tag : 16;
1058
1059     /* Number of attributes */
1060     unsigned char num_attrs;
1061
1062     /* True if we're presently building the full type name for the
1063        type derived from this DIE.  */
1064     unsigned char building_fullname : 1;
1065
1066     /* Abbrev number */
1067     unsigned int abbrev;
1068
1069     /* Offset in .debug_info or .debug_types section.  */
1070     sect_offset offset;
1071
1072     /* The dies in a compilation unit form an n-ary tree.  PARENT
1073        points to this die's parent; CHILD points to the first child of
1074        this node; and all the children of a given node are chained
1075        together via their SIBLING fields.  */
1076     struct die_info *child;     /* Its first child, if any.  */
1077     struct die_info *sibling;   /* Its next sibling, if any.  */
1078     struct die_info *parent;    /* Its parent, if any.  */
1079
1080     /* An array of attributes, with NUM_ATTRS elements.  There may be
1081        zero, but it's not common and zero-sized arrays are not
1082        sufficiently portable C.  */
1083     struct attribute attrs[1];
1084   };
1085
1086 /* Get at parts of an attribute structure.  */
1087
1088 #define DW_STRING(attr)    ((attr)->u.str)
1089 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1090 #define DW_UNSND(attr)     ((attr)->u.unsnd)
1091 #define DW_BLOCK(attr)     ((attr)->u.blk)
1092 #define DW_SND(attr)       ((attr)->u.snd)
1093 #define DW_ADDR(attr)      ((attr)->u.addr)
1094 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1095
1096 /* Blocks are a bunch of untyped bytes.  */
1097 struct dwarf_block
1098   {
1099     size_t size;
1100
1101     /* Valid only if SIZE is not zero.  */
1102     gdb_byte *data;
1103   };
1104
1105 #ifndef ATTR_ALLOC_CHUNK
1106 #define ATTR_ALLOC_CHUNK 4
1107 #endif
1108
1109 /* Allocate fields for structs, unions and enums in this size.  */
1110 #ifndef DW_FIELD_ALLOC_CHUNK
1111 #define DW_FIELD_ALLOC_CHUNK 4
1112 #endif
1113
1114 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1115    but this would require a corresponding change in unpack_field_as_long
1116    and friends.  */
1117 static int bits_per_byte = 8;
1118
1119 /* The routines that read and process dies for a C struct or C++ class
1120    pass lists of data member fields and lists of member function fields
1121    in an instance of a field_info structure, as defined below.  */
1122 struct field_info
1123   {
1124     /* List of data member and baseclasses fields.  */
1125     struct nextfield
1126       {
1127         struct nextfield *next;
1128         int accessibility;
1129         int virtuality;
1130         struct field field;
1131       }
1132      *fields, *baseclasses;
1133
1134     /* Number of fields (including baseclasses).  */
1135     int nfields;
1136
1137     /* Number of baseclasses.  */
1138     int nbaseclasses;
1139
1140     /* Set if the accesibility of one of the fields is not public.  */
1141     int non_public_fields;
1142
1143     /* Member function fields array, entries are allocated in the order they
1144        are encountered in the object file.  */
1145     struct nextfnfield
1146       {
1147         struct nextfnfield *next;
1148         struct fn_field fnfield;
1149       }
1150      *fnfields;
1151
1152     /* Member function fieldlist array, contains name of possibly overloaded
1153        member function, number of overloaded member functions and a pointer
1154        to the head of the member function field chain.  */
1155     struct fnfieldlist
1156       {
1157         const char *name;
1158         int length;
1159         struct nextfnfield *head;
1160       }
1161      *fnfieldlists;
1162
1163     /* Number of entries in the fnfieldlists array.  */
1164     int nfnfields;
1165
1166     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
1167        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
1168     struct typedef_field_list
1169       {
1170         struct typedef_field field;
1171         struct typedef_field_list *next;
1172       }
1173     *typedef_field_list;
1174     unsigned typedef_field_list_count;
1175   };
1176
1177 /* One item on the queue of compilation units to read in full symbols
1178    for.  */
1179 struct dwarf2_queue_item
1180 {
1181   struct dwarf2_per_cu_data *per_cu;
1182   enum language pretend_language;
1183   struct dwarf2_queue_item *next;
1184 };
1185
1186 /* The current queue.  */
1187 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1188
1189 /* Loaded secondary compilation units are kept in memory until they
1190    have not been referenced for the processing of this many
1191    compilation units.  Set this to zero to disable caching.  Cache
1192    sizes of up to at least twenty will improve startup time for
1193    typical inter-CU-reference binaries, at an obvious memory cost.  */
1194 static int dwarf2_max_cache_age = 5;
1195 static void
1196 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1197                            struct cmd_list_element *c, const char *value)
1198 {
1199   fprintf_filtered (file, _("The upper bound on the age of cached "
1200                             "dwarf2 compilation units is %s.\n"),
1201                     value);
1202 }
1203
1204
1205 /* Various complaints about symbol reading that don't abort the process.  */
1206
1207 static void
1208 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1209 {
1210   complaint (&symfile_complaints,
1211              _("statement list doesn't fit in .debug_line section"));
1212 }
1213
1214 static void
1215 dwarf2_debug_line_missing_file_complaint (void)
1216 {
1217   complaint (&symfile_complaints,
1218              _(".debug_line section has line data without a file"));
1219 }
1220
1221 static void
1222 dwarf2_debug_line_missing_end_sequence_complaint (void)
1223 {
1224   complaint (&symfile_complaints,
1225              _(".debug_line section has line "
1226                "program sequence without an end"));
1227 }
1228
1229 static void
1230 dwarf2_complex_location_expr_complaint (void)
1231 {
1232   complaint (&symfile_complaints, _("location expression too complex"));
1233 }
1234
1235 static void
1236 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1237                                               int arg3)
1238 {
1239   complaint (&symfile_complaints,
1240              _("const value length mismatch for '%s', got %d, expected %d"),
1241              arg1, arg2, arg3);
1242 }
1243
1244 static void
1245 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1246 {
1247   complaint (&symfile_complaints,
1248              _("debug info runs off end of %s section"
1249                " [in module %s]"),
1250              section->asection->name,
1251              bfd_get_filename (section->asection->owner));
1252 }
1253
1254 static void
1255 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1256 {
1257   complaint (&symfile_complaints,
1258              _("macro debug info contains a "
1259                "malformed macro definition:\n`%s'"),
1260              arg1);
1261 }
1262
1263 static void
1264 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1265 {
1266   complaint (&symfile_complaints,
1267              _("invalid attribute class or form for '%s' in '%s'"),
1268              arg1, arg2);
1269 }
1270
1271 /* local function prototypes */
1272
1273 static void dwarf2_locate_sections (bfd *, asection *, void *);
1274
1275 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1276                                            struct objfile *);
1277
1278 static void dwarf2_find_base_address (struct die_info *die,
1279                                       struct dwarf2_cu *cu);
1280
1281 static struct partial_symtab *create_partial_symtab
1282   (struct dwarf2_per_cu_data *per_cu, const char *name);
1283
1284 static void dwarf2_build_psymtabs_hard (struct objfile *);
1285
1286 static void scan_partial_symbols (struct partial_die_info *,
1287                                   CORE_ADDR *, CORE_ADDR *,
1288                                   int, struct dwarf2_cu *);
1289
1290 static void add_partial_symbol (struct partial_die_info *,
1291                                 struct dwarf2_cu *);
1292
1293 static void add_partial_namespace (struct partial_die_info *pdi,
1294                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
1295                                    int need_pc, struct dwarf2_cu *cu);
1296
1297 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1298                                 CORE_ADDR *highpc, int need_pc,
1299                                 struct dwarf2_cu *cu);
1300
1301 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1302                                      struct dwarf2_cu *cu);
1303
1304 static void add_partial_subprogram (struct partial_die_info *pdi,
1305                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
1306                                     int need_pc, struct dwarf2_cu *cu);
1307
1308 static void dwarf2_read_symtab (struct partial_symtab *,
1309                                 struct objfile *);
1310
1311 static void psymtab_to_symtab_1 (struct partial_symtab *);
1312
1313 static struct abbrev_info *abbrev_table_lookup_abbrev
1314   (const struct abbrev_table *, unsigned int);
1315
1316 static struct abbrev_table *abbrev_table_read_table
1317   (struct dwarf2_section_info *, sect_offset);
1318
1319 static void abbrev_table_free (struct abbrev_table *);
1320
1321 static void abbrev_table_free_cleanup (void *);
1322
1323 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1324                                  struct dwarf2_section_info *);
1325
1326 static void dwarf2_free_abbrev_table (void *);
1327
1328 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1329
1330 static struct partial_die_info *load_partial_dies
1331   (const struct die_reader_specs *, gdb_byte *, int);
1332
1333 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1334                                    struct partial_die_info *,
1335                                    struct abbrev_info *,
1336                                    unsigned int,
1337                                    gdb_byte *);
1338
1339 static struct partial_die_info *find_partial_die (sect_offset, int,
1340                                                   struct dwarf2_cu *);
1341
1342 static void fixup_partial_die (struct partial_die_info *,
1343                                struct dwarf2_cu *);
1344
1345 static gdb_byte *read_attribute (const struct die_reader_specs *,
1346                                  struct attribute *, struct attr_abbrev *,
1347                                  gdb_byte *);
1348
1349 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1350
1351 static int read_1_signed_byte (bfd *, const gdb_byte *);
1352
1353 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1354
1355 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1356
1357 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1358
1359 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1360                                unsigned int *);
1361
1362 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1363
1364 static LONGEST read_checked_initial_length_and_offset
1365   (bfd *, gdb_byte *, const struct comp_unit_head *,
1366    unsigned int *, unsigned int *);
1367
1368 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1369                             unsigned int *);
1370
1371 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1372
1373 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1374                                        sect_offset);
1375
1376 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1377
1378 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1379
1380 static char *read_indirect_string (bfd *, gdb_byte *,
1381                                    const struct comp_unit_head *,
1382                                    unsigned int *);
1383
1384 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1385
1386 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1387
1388 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1389
1390 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1391                                               unsigned int *);
1392
1393 static char *read_str_index (const struct die_reader_specs *reader,
1394                              struct dwarf2_cu *cu, ULONGEST str_index);
1395
1396 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1397
1398 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1399                                       struct dwarf2_cu *);
1400
1401 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1402                                                 unsigned int);
1403
1404 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1405                                struct dwarf2_cu *cu);
1406
1407 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1408
1409 static struct die_info *die_specification (struct die_info *die,
1410                                            struct dwarf2_cu **);
1411
1412 static void free_line_header (struct line_header *lh);
1413
1414 static void add_file_name (struct line_header *, char *, unsigned int,
1415                            unsigned int, unsigned int);
1416
1417 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1418                                                      struct dwarf2_cu *cu);
1419
1420 static void dwarf_decode_lines (struct line_header *, const char *,
1421                                 struct dwarf2_cu *, struct partial_symtab *,
1422                                 int);
1423
1424 static void dwarf2_start_subfile (char *, const char *, const char *);
1425
1426 static void dwarf2_start_symtab (struct dwarf2_cu *,
1427                                  const char *, const char *, CORE_ADDR);
1428
1429 static struct symbol *new_symbol (struct die_info *, struct type *,
1430                                   struct dwarf2_cu *);
1431
1432 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1433                                        struct dwarf2_cu *, struct symbol *);
1434
1435 static void dwarf2_const_value (struct attribute *, struct symbol *,
1436                                 struct dwarf2_cu *);
1437
1438 static void dwarf2_const_value_attr (struct attribute *attr,
1439                                      struct type *type,
1440                                      const char *name,
1441                                      struct obstack *obstack,
1442                                      struct dwarf2_cu *cu, LONGEST *value,
1443                                      gdb_byte **bytes,
1444                                      struct dwarf2_locexpr_baton **baton);
1445
1446 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1447
1448 static int need_gnat_info (struct dwarf2_cu *);
1449
1450 static struct type *die_descriptive_type (struct die_info *,
1451                                           struct dwarf2_cu *);
1452
1453 static void set_descriptive_type (struct type *, struct die_info *,
1454                                   struct dwarf2_cu *);
1455
1456 static struct type *die_containing_type (struct die_info *,
1457                                          struct dwarf2_cu *);
1458
1459 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1460                                      struct dwarf2_cu *);
1461
1462 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1463
1464 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1465
1466 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1467
1468 static char *typename_concat (struct obstack *obs, const char *prefix,
1469                               const char *suffix, int physname,
1470                               struct dwarf2_cu *cu);
1471
1472 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1473
1474 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1475
1476 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1477
1478 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1479
1480 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1481
1482 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1483                                struct dwarf2_cu *, struct partial_symtab *);
1484
1485 static int dwarf2_get_pc_bounds (struct die_info *,
1486                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1487                                  struct partial_symtab *);
1488
1489 static void get_scope_pc_bounds (struct die_info *,
1490                                  CORE_ADDR *, CORE_ADDR *,
1491                                  struct dwarf2_cu *);
1492
1493 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1494                                         CORE_ADDR, struct dwarf2_cu *);
1495
1496 static void dwarf2_add_field (struct field_info *, struct die_info *,
1497                               struct dwarf2_cu *);
1498
1499 static void dwarf2_attach_fields_to_type (struct field_info *,
1500                                           struct type *, struct dwarf2_cu *);
1501
1502 static void dwarf2_add_member_fn (struct field_info *,
1503                                   struct die_info *, struct type *,
1504                                   struct dwarf2_cu *);
1505
1506 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1507                                              struct type *,
1508                                              struct dwarf2_cu *);
1509
1510 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1511
1512 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1513
1514 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1515
1516 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1517
1518 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1519
1520 static struct type *read_module_type (struct die_info *die,
1521                                       struct dwarf2_cu *cu);
1522
1523 static const char *namespace_name (struct die_info *die,
1524                                    int *is_anonymous, struct dwarf2_cu *);
1525
1526 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1527
1528 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1529
1530 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1531                                                        struct dwarf2_cu *);
1532
1533 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1534                                                gdb_byte *info_ptr,
1535                                                gdb_byte **new_info_ptr,
1536                                                struct die_info *parent);
1537
1538 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1539                                                gdb_byte *info_ptr,
1540                                                gdb_byte **new_info_ptr,
1541                                                struct die_info *parent);
1542
1543 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1544                                   struct die_info **, gdb_byte *, int *, int);
1545
1546 static gdb_byte *read_full_die (const struct die_reader_specs *,
1547                                 struct die_info **, gdb_byte *, int *);
1548
1549 static void process_die (struct die_info *, struct dwarf2_cu *);
1550
1551 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1552                                              struct obstack *);
1553
1554 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1555
1556 static const char *dwarf2_full_name (const char *name,
1557                                      struct die_info *die,
1558                                      struct dwarf2_cu *cu);
1559
1560 static const char *dwarf2_physname (const char *name, struct die_info *die,
1561                                     struct dwarf2_cu *cu);
1562
1563 static struct die_info *dwarf2_extension (struct die_info *die,
1564                                           struct dwarf2_cu **);
1565
1566 static const char *dwarf_tag_name (unsigned int);
1567
1568 static const char *dwarf_attr_name (unsigned int);
1569
1570 static const char *dwarf_form_name (unsigned int);
1571
1572 static char *dwarf_bool_name (unsigned int);
1573
1574 static const char *dwarf_type_encoding_name (unsigned int);
1575
1576 static struct die_info *sibling_die (struct die_info *);
1577
1578 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1579
1580 static void dump_die_for_error (struct die_info *);
1581
1582 static void dump_die_1 (struct ui_file *, int level, int max_level,
1583                         struct die_info *);
1584
1585 /*static*/ void dump_die (struct die_info *, int max_level);
1586
1587 static void store_in_ref_table (struct die_info *,
1588                                 struct dwarf2_cu *);
1589
1590 static int is_ref_attr (struct attribute *);
1591
1592 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1593
1594 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1595
1596 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1597                                                struct attribute *,
1598                                                struct dwarf2_cu **);
1599
1600 static struct die_info *follow_die_ref (struct die_info *,
1601                                         struct attribute *,
1602                                         struct dwarf2_cu **);
1603
1604 static struct die_info *follow_die_sig (struct die_info *,
1605                                         struct attribute *,
1606                                         struct dwarf2_cu **);
1607
1608 static struct signatured_type *lookup_signatured_type_at_offset
1609     (struct objfile *objfile,
1610      struct dwarf2_section_info *section, sect_offset offset);
1611
1612 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1613
1614 static void read_signatured_type (struct signatured_type *);
1615
1616 static struct type_unit_group *get_type_unit_group
1617     (struct dwarf2_cu *, struct attribute *);
1618
1619 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1620
1621 /* memory allocation interface */
1622
1623 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1624
1625 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1626
1627 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1628                                  const char *, int);
1629
1630 static int attr_form_is_block (struct attribute *);
1631
1632 static int attr_form_is_section_offset (struct attribute *);
1633
1634 static int attr_form_is_constant (struct attribute *);
1635
1636 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1637                                    struct dwarf2_loclist_baton *baton,
1638                                    struct attribute *attr);
1639
1640 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1641                                          struct symbol *sym,
1642                                          struct dwarf2_cu *cu,
1643                                          int is_block);
1644
1645 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1646                                gdb_byte *info_ptr,
1647                                struct abbrev_info *abbrev);
1648
1649 static void free_stack_comp_unit (void *);
1650
1651 static hashval_t partial_die_hash (const void *item);
1652
1653 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1654
1655 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1656   (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1657
1658 static void init_one_comp_unit (struct dwarf2_cu *cu,
1659                                 struct dwarf2_per_cu_data *per_cu);
1660
1661 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1662                                    struct die_info *comp_unit_die,
1663                                    enum language pretend_language);
1664
1665 static void free_heap_comp_unit (void *);
1666
1667 static void free_cached_comp_units (void *);
1668
1669 static void age_cached_comp_units (void);
1670
1671 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1672
1673 static struct type *set_die_type (struct die_info *, struct type *,
1674                                   struct dwarf2_cu *);
1675
1676 static void create_all_comp_units (struct objfile *);
1677
1678 static int create_all_type_units (struct objfile *);
1679
1680 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1681                                  enum language);
1682
1683 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1684                                     enum language);
1685
1686 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1687                                     enum language);
1688
1689 static void dwarf2_add_dependence (struct dwarf2_cu *,
1690                                    struct dwarf2_per_cu_data *);
1691
1692 static void dwarf2_mark (struct dwarf2_cu *);
1693
1694 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1695
1696 static struct type *get_die_type_at_offset (sect_offset,
1697                                             struct dwarf2_per_cu_data *per_cu);
1698
1699 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1700
1701 static void dwarf2_release_queue (void *dummy);
1702
1703 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1704                              enum language pretend_language);
1705
1706 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1707                                   struct dwarf2_per_cu_data *per_cu,
1708                                   enum language pretend_language);
1709
1710 static void process_queue (void);
1711
1712 static void find_file_and_directory (struct die_info *die,
1713                                      struct dwarf2_cu *cu,
1714                                      const char **name, const char **comp_dir);
1715
1716 static char *file_full_name (int file, struct line_header *lh,
1717                              const char *comp_dir);
1718
1719 static gdb_byte *read_and_check_comp_unit_head
1720   (struct comp_unit_head *header,
1721    struct dwarf2_section_info *section,
1722    struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1723    int is_debug_types_section);
1724
1725 static void init_cutu_and_read_dies
1726   (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1727    int use_existing_cu, int keep,
1728    die_reader_func_ftype *die_reader_func, void *data);
1729
1730 static void init_cutu_and_read_dies_simple
1731   (struct dwarf2_per_cu_data *this_cu,
1732    die_reader_func_ftype *die_reader_func, void *data);
1733
1734 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1735
1736 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1737
1738 static struct dwo_unit *lookup_dwo_comp_unit
1739   (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1740
1741 static struct dwo_unit *lookup_dwo_type_unit
1742   (struct signatured_type *, const char *, const char *);
1743
1744 static void free_dwo_file_cleanup (void *);
1745
1746 static void process_cu_includes (void);
1747
1748 static void check_producer (struct dwarf2_cu *cu);
1749
1750 #if WORDS_BIGENDIAN
1751
1752 /* Convert VALUE between big- and little-endian.  */
1753 static offset_type
1754 byte_swap (offset_type value)
1755 {
1756   offset_type result;
1757
1758   result = (value & 0xff) << 24;
1759   result |= (value & 0xff00) << 8;
1760   result |= (value & 0xff0000) >> 8;
1761   result |= (value & 0xff000000) >> 24;
1762   return result;
1763 }
1764
1765 #define MAYBE_SWAP(V)  byte_swap (V)
1766
1767 #else
1768 #define MAYBE_SWAP(V) (V)
1769 #endif /* WORDS_BIGENDIAN */
1770
1771 /* The suffix for an index file.  */
1772 #define INDEX_SUFFIX ".gdb-index"
1773
1774 /* Try to locate the sections we need for DWARF 2 debugging
1775    information and return true if we have enough to do something.
1776    NAMES points to the dwarf2 section names, or is NULL if the standard
1777    ELF names are used.  */
1778
1779 int
1780 dwarf2_has_info (struct objfile *objfile,
1781                  const struct dwarf2_debug_sections *names)
1782 {
1783   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1784   if (!dwarf2_per_objfile)
1785     {
1786       /* Initialize per-objfile state.  */
1787       struct dwarf2_per_objfile *data
1788         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1789
1790       memset (data, 0, sizeof (*data));
1791       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1792       dwarf2_per_objfile = data;
1793
1794       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1795                              (void *) names);
1796       dwarf2_per_objfile->objfile = objfile;
1797     }
1798   return (dwarf2_per_objfile->info.asection != NULL
1799           && dwarf2_per_objfile->abbrev.asection != NULL);
1800 }
1801
1802 /* When loading sections, we look either for uncompressed section or for
1803    compressed section names.  */
1804
1805 static int
1806 section_is_p (const char *section_name,
1807               const struct dwarf2_section_names *names)
1808 {
1809   if (names->normal != NULL
1810       && strcmp (section_name, names->normal) == 0)
1811     return 1;
1812   if (names->compressed != NULL
1813       && strcmp (section_name, names->compressed) == 0)
1814     return 1;
1815   return 0;
1816 }
1817
1818 /* This function is mapped across the sections and remembers the
1819    offset and size of each of the debugging sections we are interested
1820    in.  */
1821
1822 static void
1823 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1824 {
1825   const struct dwarf2_debug_sections *names;
1826   flagword aflag = bfd_get_section_flags (abfd, sectp);
1827
1828   if (vnames == NULL)
1829     names = &dwarf2_elf_names;
1830   else
1831     names = (const struct dwarf2_debug_sections *) vnames;
1832
1833   if ((aflag & SEC_HAS_CONTENTS) == 0)
1834     {
1835     }
1836   else if (section_is_p (sectp->name, &names->info))
1837     {
1838       dwarf2_per_objfile->info.asection = sectp;
1839       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1840     }
1841   else if (section_is_p (sectp->name, &names->abbrev))
1842     {
1843       dwarf2_per_objfile->abbrev.asection = sectp;
1844       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1845     }
1846   else if (section_is_p (sectp->name, &names->line))
1847     {
1848       dwarf2_per_objfile->line.asection = sectp;
1849       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1850     }
1851   else if (section_is_p (sectp->name, &names->loc))
1852     {
1853       dwarf2_per_objfile->loc.asection = sectp;
1854       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1855     }
1856   else if (section_is_p (sectp->name, &names->macinfo))
1857     {
1858       dwarf2_per_objfile->macinfo.asection = sectp;
1859       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1860     }
1861   else if (section_is_p (sectp->name, &names->macro))
1862     {
1863       dwarf2_per_objfile->macro.asection = sectp;
1864       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1865     }
1866   else if (section_is_p (sectp->name, &names->str))
1867     {
1868       dwarf2_per_objfile->str.asection = sectp;
1869       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1870     }
1871   else if (section_is_p (sectp->name, &names->addr))
1872     {
1873       dwarf2_per_objfile->addr.asection = sectp;
1874       dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1875     }
1876   else if (section_is_p (sectp->name, &names->frame))
1877     {
1878       dwarf2_per_objfile->frame.asection = sectp;
1879       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1880     }
1881   else if (section_is_p (sectp->name, &names->eh_frame))
1882     {
1883       dwarf2_per_objfile->eh_frame.asection = sectp;
1884       dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1885     }
1886   else if (section_is_p (sectp->name, &names->ranges))
1887     {
1888       dwarf2_per_objfile->ranges.asection = sectp;
1889       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1890     }
1891   else if (section_is_p (sectp->name, &names->types))
1892     {
1893       struct dwarf2_section_info type_section;
1894
1895       memset (&type_section, 0, sizeof (type_section));
1896       type_section.asection = sectp;
1897       type_section.size = bfd_get_section_size (sectp);
1898
1899       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1900                      &type_section);
1901     }
1902   else if (section_is_p (sectp->name, &names->gdb_index))
1903     {
1904       dwarf2_per_objfile->gdb_index.asection = sectp;
1905       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1906     }
1907
1908   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1909       && bfd_section_vma (abfd, sectp) == 0)
1910     dwarf2_per_objfile->has_section_at_zero = 1;
1911 }
1912
1913 /* A helper function that decides whether a section is empty,
1914    or not present.  */
1915
1916 static int
1917 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1918 {
1919   return info->asection == NULL || info->size == 0;
1920 }
1921
1922 /* Read the contents of the section INFO.
1923    OBJFILE is the main object file, but not necessarily the file where
1924    the section comes from.  E.g., for DWO files INFO->asection->owner
1925    is the bfd of the DWO file.
1926    If the section is compressed, uncompress it before returning.  */
1927
1928 static void
1929 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1930 {
1931   asection *sectp = info->asection;
1932   bfd *abfd;
1933   gdb_byte *buf, *retbuf;
1934   unsigned char header[4];
1935
1936   if (info->readin)
1937     return;
1938   info->buffer = NULL;
1939   info->readin = 1;
1940
1941   if (dwarf2_section_empty_p (info))
1942     return;
1943
1944   abfd = sectp->owner;
1945
1946   /* If the section has relocations, we must read it ourselves.
1947      Otherwise we attach it to the BFD.  */
1948   if ((sectp->flags & SEC_RELOC) == 0)
1949     {
1950       const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1951
1952       /* We have to cast away const here for historical reasons.
1953          Fixing dwarf2read to be const-correct would be quite nice.  */
1954       info->buffer = (gdb_byte *) bytes;
1955       return;
1956     }
1957
1958   buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1959   info->buffer = buf;
1960
1961   /* When debugging .o files, we may need to apply relocations; see
1962      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1963      We never compress sections in .o files, so we only need to
1964      try this when the section is not compressed.  */
1965   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1966   if (retbuf != NULL)
1967     {
1968       info->buffer = retbuf;
1969       return;
1970     }
1971
1972   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1973       || bfd_bread (buf, info->size, abfd) != info->size)
1974     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1975            bfd_get_filename (abfd));
1976 }
1977
1978 /* A helper function that returns the size of a section in a safe way.
1979    If you are positive that the section has been read before using the
1980    size, then it is safe to refer to the dwarf2_section_info object's
1981    "size" field directly.  In other cases, you must call this
1982    function, because for compressed sections the size field is not set
1983    correctly until the section has been read.  */
1984
1985 static bfd_size_type
1986 dwarf2_section_size (struct objfile *objfile,
1987                      struct dwarf2_section_info *info)
1988 {
1989   if (!info->readin)
1990     dwarf2_read_section (objfile, info);
1991   return info->size;
1992 }
1993
1994 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1995    SECTION_NAME.  */
1996
1997 void
1998 dwarf2_get_section_info (struct objfile *objfile,
1999                          enum dwarf2_section_enum sect,
2000                          asection **sectp, gdb_byte **bufp,
2001                          bfd_size_type *sizep)
2002 {
2003   struct dwarf2_per_objfile *data
2004     = objfile_data (objfile, dwarf2_objfile_data_key);
2005   struct dwarf2_section_info *info;
2006
2007   /* We may see an objfile without any DWARF, in which case we just
2008      return nothing.  */
2009   if (data == NULL)
2010     {
2011       *sectp = NULL;
2012       *bufp = NULL;
2013       *sizep = 0;
2014       return;
2015     }
2016   switch (sect)
2017     {
2018     case DWARF2_DEBUG_FRAME:
2019       info = &data->frame;
2020       break;
2021     case DWARF2_EH_FRAME:
2022       info = &data->eh_frame;
2023       break;
2024     default:
2025       gdb_assert_not_reached ("unexpected section");
2026     }
2027
2028   dwarf2_read_section (objfile, info);
2029
2030   *sectp = info->asection;
2031   *bufp = info->buffer;
2032   *sizep = info->size;
2033 }
2034
2035 /* A helper function to find the sections for a .dwz file.  */
2036
2037 static void
2038 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2039 {
2040   struct dwz_file *dwz_file = arg;
2041
2042   /* Note that we only support the standard ELF names, because .dwz
2043      is ELF-only (at the time of writing).  */
2044   if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2045     {
2046       dwz_file->abbrev.asection = sectp;
2047       dwz_file->abbrev.size = bfd_get_section_size (sectp);
2048     }
2049   else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2050     {
2051       dwz_file->info.asection = sectp;
2052       dwz_file->info.size = bfd_get_section_size (sectp);
2053     }
2054   else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2055     {
2056       dwz_file->str.asection = sectp;
2057       dwz_file->str.size = bfd_get_section_size (sectp);
2058     }
2059   else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2060     {
2061       dwz_file->line.asection = sectp;
2062       dwz_file->line.size = bfd_get_section_size (sectp);
2063     }
2064   else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2065     {
2066       dwz_file->macro.asection = sectp;
2067       dwz_file->macro.size = bfd_get_section_size (sectp);
2068     }
2069   else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2070     {
2071       dwz_file->gdb_index.asection = sectp;
2072       dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2073     }
2074 }
2075
2076 /* Open the separate '.dwz' debug file, if needed.  Error if the file
2077    cannot be found.  */
2078
2079 static struct dwz_file *
2080 dwarf2_get_dwz_file (void)
2081 {
2082   bfd *abfd, *dwz_bfd;
2083   asection *section;
2084   gdb_byte *data;
2085   struct cleanup *cleanup;
2086   const char *filename;
2087   struct dwz_file *result;
2088
2089   if (dwarf2_per_objfile->dwz_file != NULL)
2090     return dwarf2_per_objfile->dwz_file;
2091
2092   abfd = dwarf2_per_objfile->objfile->obfd;
2093   section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2094   if (section == NULL)
2095     error (_("could not find '.gnu_debugaltlink' section"));
2096   if (!bfd_malloc_and_get_section (abfd, section, &data))
2097     error (_("could not read '.gnu_debugaltlink' section: %s"),
2098            bfd_errmsg (bfd_get_error ()));
2099   cleanup = make_cleanup (xfree, data);
2100
2101   filename = data;
2102   if (!IS_ABSOLUTE_PATH (filename))
2103     {
2104       char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2105       char *rel;
2106
2107       make_cleanup (xfree, abs);
2108       abs = ldirname (abs);
2109       make_cleanup (xfree, abs);
2110
2111       rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2112       make_cleanup (xfree, rel);
2113       filename = rel;
2114     }
2115
2116   /* The format is just a NUL-terminated file name, followed by the
2117      build-id.  For now, though, we ignore the build-id.  */
2118   dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2119   if (dwz_bfd == NULL)
2120     error (_("could not read '%s': %s"), filename,
2121            bfd_errmsg (bfd_get_error ()));
2122
2123   if (!bfd_check_format (dwz_bfd, bfd_object))
2124     {
2125       gdb_bfd_unref (dwz_bfd);
2126       error (_("file '%s' was not usable: %s"), filename,
2127              bfd_errmsg (bfd_get_error ()));
2128     }
2129
2130   result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2131                            struct dwz_file);
2132   result->dwz_bfd = dwz_bfd;
2133
2134   bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2135
2136   do_cleanups (cleanup);
2137
2138   dwarf2_per_objfile->dwz_file = result;
2139   return result;
2140 }
2141 \f
2142 /* DWARF quick_symbols_functions support.  */
2143
2144 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2145    unique line tables, so we maintain a separate table of all .debug_line
2146    derived entries to support the sharing.
2147    All the quick functions need is the list of file names.  We discard the
2148    line_header when we're done and don't need to record it here.  */
2149 struct quick_file_names
2150 {
2151   /* The data used to construct the hash key.  */
2152   struct stmt_list_hash hash;
2153
2154   /* The number of entries in file_names, real_names.  */
2155   unsigned int num_file_names;
2156
2157   /* The file names from the line table, after being run through
2158      file_full_name.  */
2159   const char **file_names;
2160
2161   /* The file names from the line table after being run through
2162      gdb_realpath.  These are computed lazily.  */
2163   const char **real_names;
2164 };
2165
2166 /* When using the index (and thus not using psymtabs), each CU has an
2167    object of this type.  This is used to hold information needed by
2168    the various "quick" methods.  */
2169 struct dwarf2_per_cu_quick_data
2170 {
2171   /* The file table.  This can be NULL if there was no file table
2172      or it's currently not read in.
2173      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
2174   struct quick_file_names *file_names;
2175
2176   /* The corresponding symbol table.  This is NULL if symbols for this
2177      CU have not yet been read.  */
2178   struct symtab *symtab;
2179
2180   /* A temporary mark bit used when iterating over all CUs in
2181      expand_symtabs_matching.  */
2182   unsigned int mark : 1;
2183
2184   /* True if we've tried to read the file table and found there isn't one.
2185      There will be no point in trying to read it again next time.  */
2186   unsigned int no_file_data : 1;
2187 };
2188
2189 /* Utility hash function for a stmt_list_hash.  */
2190
2191 static hashval_t
2192 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2193 {
2194   hashval_t v = 0;
2195
2196   if (stmt_list_hash->dwo_unit != NULL)
2197     v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2198   v += stmt_list_hash->line_offset.sect_off;
2199   return v;
2200 }
2201
2202 /* Utility equality function for a stmt_list_hash.  */
2203
2204 static int
2205 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2206                     const struct stmt_list_hash *rhs)
2207 {
2208   if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2209     return 0;
2210   if (lhs->dwo_unit != NULL
2211       && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2212     return 0;
2213
2214   return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2215 }
2216
2217 /* Hash function for a quick_file_names.  */
2218
2219 static hashval_t
2220 hash_file_name_entry (const void *e)
2221 {
2222   const struct quick_file_names *file_data = e;
2223
2224   return hash_stmt_list_entry (&file_data->hash);
2225 }
2226
2227 /* Equality function for a quick_file_names.  */
2228
2229 static int
2230 eq_file_name_entry (const void *a, const void *b)
2231 {
2232   const struct quick_file_names *ea = a;
2233   const struct quick_file_names *eb = b;
2234
2235   return eq_stmt_list_entry (&ea->hash, &eb->hash);
2236 }
2237
2238 /* Delete function for a quick_file_names.  */
2239
2240 static void
2241 delete_file_name_entry (void *e)
2242 {
2243   struct quick_file_names *file_data = e;
2244   int i;
2245
2246   for (i = 0; i < file_data->num_file_names; ++i)
2247     {
2248       xfree ((void*) file_data->file_names[i]);
2249       if (file_data->real_names)
2250         xfree ((void*) file_data->real_names[i]);
2251     }
2252
2253   /* The space for the struct itself lives on objfile_obstack,
2254      so we don't free it here.  */
2255 }
2256
2257 /* Create a quick_file_names hash table.  */
2258
2259 static htab_t
2260 create_quick_file_names_table (unsigned int nr_initial_entries)
2261 {
2262   return htab_create_alloc (nr_initial_entries,
2263                             hash_file_name_entry, eq_file_name_entry,
2264                             delete_file_name_entry, xcalloc, xfree);
2265 }
2266
2267 /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
2268    have to be created afterwards.  You should call age_cached_comp_units after
2269    processing PER_CU->CU.  dw2_setup must have been already called.  */
2270
2271 static void
2272 load_cu (struct dwarf2_per_cu_data *per_cu)
2273 {
2274   if (per_cu->is_debug_types)
2275     load_full_type_unit (per_cu);
2276   else
2277     load_full_comp_unit (per_cu, language_minimal);
2278
2279   gdb_assert (per_cu->cu != NULL);
2280
2281   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2282 }
2283
2284 /* Read in the symbols for PER_CU.  */
2285
2286 static void
2287 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2288 {
2289   struct cleanup *back_to;
2290
2291   /* Skip type_unit_groups, reading the type units they contain
2292      is handled elsewhere.  */
2293   if (IS_TYPE_UNIT_GROUP (per_cu))
2294     return;
2295
2296   back_to = make_cleanup (dwarf2_release_queue, NULL);
2297
2298   if (dwarf2_per_objfile->using_index
2299       ? per_cu->v.quick->symtab == NULL
2300       : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2301     {
2302       queue_comp_unit (per_cu, language_minimal);
2303       load_cu (per_cu);
2304     }
2305
2306   process_queue ();
2307
2308   /* Age the cache, releasing compilation units that have not
2309      been used recently.  */
2310   age_cached_comp_units ();
2311
2312   do_cleanups (back_to);
2313 }
2314
2315 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
2316    the objfile from which this CU came.  Returns the resulting symbol
2317    table.  */
2318
2319 static struct symtab *
2320 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2321 {
2322   gdb_assert (dwarf2_per_objfile->using_index);
2323   if (!per_cu->v.quick->symtab)
2324     {
2325       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2326       increment_reading_symtab ();
2327       dw2_do_instantiate_symtab (per_cu);
2328       process_cu_includes ();
2329       do_cleanups (back_to);
2330     }
2331   return per_cu->v.quick->symtab;
2332 }
2333
2334 /* Return the CU given its index.
2335
2336    This is intended for loops like:
2337
2338    for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2339                     + dwarf2_per_objfile->n_type_units); ++i)
2340      {
2341        struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2342
2343        ...;
2344      }
2345 */
2346
2347 static struct dwarf2_per_cu_data *
2348 dw2_get_cu (int index)
2349 {
2350   if (index >= dwarf2_per_objfile->n_comp_units)
2351     {
2352       index -= dwarf2_per_objfile->n_comp_units;
2353       gdb_assert (index < dwarf2_per_objfile->n_type_units);
2354       return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2355     }
2356
2357   return dwarf2_per_objfile->all_comp_units[index];
2358 }
2359
2360 /* Return the primary CU given its index.
2361    The difference between this function and dw2_get_cu is in the handling
2362    of type units (TUs).  Here we return the type_unit_group object.
2363
2364    This is intended for loops like:
2365
2366    for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2367                     + dwarf2_per_objfile->n_type_unit_groups); ++i)
2368      {
2369        struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2370
2371        ...;
2372      }
2373 */
2374
2375 static struct dwarf2_per_cu_data *
2376 dw2_get_primary_cu (int index)
2377 {
2378   if (index >= dwarf2_per_objfile->n_comp_units)
2379     {
2380       index -= dwarf2_per_objfile->n_comp_units;
2381       gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2382       return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2383     }
2384
2385   return dwarf2_per_objfile->all_comp_units[index];
2386 }
2387
2388 /* A helper for create_cus_from_index that handles a given list of
2389    CUs.  */
2390
2391 static void
2392 create_cus_from_index_list (struct objfile *objfile,
2393                             const gdb_byte *cu_list, offset_type n_elements,
2394                             struct dwarf2_section_info *section,
2395                             int is_dwz,
2396                             int base_offset)
2397 {
2398   offset_type i;
2399
2400   for (i = 0; i < n_elements; i += 2)
2401     {
2402       struct dwarf2_per_cu_data *the_cu;
2403       ULONGEST offset, length;
2404
2405       gdb_static_assert (sizeof (ULONGEST) >= 8);
2406       offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2407       length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2408       cu_list += 2 * 8;
2409
2410       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2411                                struct dwarf2_per_cu_data);
2412       the_cu->offset.sect_off = offset;
2413       the_cu->length = length;
2414       the_cu->objfile = objfile;
2415       the_cu->section = section;
2416       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2417                                         struct dwarf2_per_cu_quick_data);
2418       the_cu->is_dwz = is_dwz;
2419       dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2420     }
2421 }
2422
2423 /* Read the CU list from the mapped index, and use it to create all
2424    the CU objects for this objfile.  */
2425
2426 static void
2427 create_cus_from_index (struct objfile *objfile,
2428                        const gdb_byte *cu_list, offset_type cu_list_elements,
2429                        const gdb_byte *dwz_list, offset_type dwz_elements)
2430 {
2431   struct dwz_file *dwz;
2432
2433   dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2434   dwarf2_per_objfile->all_comp_units
2435     = obstack_alloc (&objfile->objfile_obstack,
2436                      dwarf2_per_objfile->n_comp_units
2437                      * sizeof (struct dwarf2_per_cu_data *));
2438
2439   create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2440                               &dwarf2_per_objfile->info, 0, 0);
2441
2442   if (dwz_elements == 0)
2443     return;
2444
2445   dwz = dwarf2_get_dwz_file ();
2446   create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2447                               cu_list_elements / 2);
2448 }
2449
2450 /* Create the signatured type hash table from the index.  */
2451
2452 static void
2453 create_signatured_type_table_from_index (struct objfile *objfile,
2454                                          struct dwarf2_section_info *section,
2455                                          const gdb_byte *bytes,
2456                                          offset_type elements)
2457 {
2458   offset_type i;
2459   htab_t sig_types_hash;
2460
2461   dwarf2_per_objfile->n_type_units = elements / 3;
2462   dwarf2_per_objfile->all_type_units
2463     = obstack_alloc (&objfile->objfile_obstack,
2464                      dwarf2_per_objfile->n_type_units
2465                      * sizeof (struct signatured_type *));
2466
2467   sig_types_hash = allocate_signatured_type_table (objfile);
2468
2469   for (i = 0; i < elements; i += 3)
2470     {
2471       struct signatured_type *sig_type;
2472       ULONGEST offset, type_offset_in_tu, signature;
2473       void **slot;
2474
2475       gdb_static_assert (sizeof (ULONGEST) >= 8);
2476       offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2477       type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2478                                                     BFD_ENDIAN_LITTLE);
2479       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2480       bytes += 3 * 8;
2481
2482       sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2483                                  struct signatured_type);
2484       sig_type->signature = signature;
2485       sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2486       sig_type->per_cu.is_debug_types = 1;
2487       sig_type->per_cu.section = section;
2488       sig_type->per_cu.offset.sect_off = offset;
2489       sig_type->per_cu.objfile = objfile;
2490       sig_type->per_cu.v.quick
2491         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2492                           struct dwarf2_per_cu_quick_data);
2493
2494       slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2495       *slot = sig_type;
2496
2497       dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2498     }
2499
2500   dwarf2_per_objfile->signatured_types = sig_types_hash;
2501 }
2502
2503 /* Read the address map data from the mapped index, and use it to
2504    populate the objfile's psymtabs_addrmap.  */
2505
2506 static void
2507 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2508 {
2509   const gdb_byte *iter, *end;
2510   struct obstack temp_obstack;
2511   struct addrmap *mutable_map;
2512   struct cleanup *cleanup;
2513   CORE_ADDR baseaddr;
2514
2515   obstack_init (&temp_obstack);
2516   cleanup = make_cleanup_obstack_free (&temp_obstack);
2517   mutable_map = addrmap_create_mutable (&temp_obstack);
2518
2519   iter = index->address_table;
2520   end = iter + index->address_table_size;
2521
2522   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2523
2524   while (iter < end)
2525     {
2526       ULONGEST hi, lo, cu_index;
2527       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2528       iter += 8;
2529       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2530       iter += 8;
2531       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2532       iter += 4;
2533
2534       if (cu_index < dwarf2_per_objfile->n_comp_units)
2535         {
2536           addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2537                              dw2_get_cu (cu_index));
2538         }
2539       else
2540         {
2541           complaint (&symfile_complaints,
2542                      _(".gdb_index address table has invalid CU number %u"),
2543                      (unsigned) cu_index);
2544         }
2545     }
2546
2547   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2548                                                     &objfile->objfile_obstack);
2549   do_cleanups (cleanup);
2550 }
2551
2552 /* The hash function for strings in the mapped index.  This is the same as
2553    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2554    implementation.  This is necessary because the hash function is tied to the
2555    format of the mapped index file.  The hash values do not have to match with
2556    SYMBOL_HASH_NEXT.
2557    
2558    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2559
2560 static hashval_t
2561 mapped_index_string_hash (int index_version, const void *p)
2562 {
2563   const unsigned char *str = (const unsigned char *) p;
2564   hashval_t r = 0;
2565   unsigned char c;
2566
2567   while ((c = *str++) != 0)
2568     {
2569       if (index_version >= 5)
2570         c = tolower (c);
2571       r = r * 67 + c - 113;
2572     }
2573
2574   return r;
2575 }
2576
2577 /* Find a slot in the mapped index INDEX for the object named NAME.
2578    If NAME is found, set *VEC_OUT to point to the CU vector in the
2579    constant pool and return 1.  If NAME cannot be found, return 0.  */
2580
2581 static int
2582 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2583                           offset_type **vec_out)
2584 {
2585   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2586   offset_type hash;
2587   offset_type slot, step;
2588   int (*cmp) (const char *, const char *);
2589
2590   if (current_language->la_language == language_cplus
2591       || current_language->la_language == language_java
2592       || current_language->la_language == language_fortran)
2593     {
2594       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2595          not contain any.  */
2596       const char *paren = strchr (name, '(');
2597
2598       if (paren)
2599         {
2600           char *dup;
2601
2602           dup = xmalloc (paren - name + 1);
2603           memcpy (dup, name, paren - name);
2604           dup[paren - name] = 0;
2605
2606           make_cleanup (xfree, dup);
2607           name = dup;
2608         }
2609     }
2610
2611   /* Index version 4 did not support case insensitive searches.  But the
2612      indices for case insensitive languages are built in lowercase, therefore
2613      simulate our NAME being searched is also lowercased.  */
2614   hash = mapped_index_string_hash ((index->version == 4
2615                                     && case_sensitivity == case_sensitive_off
2616                                     ? 5 : index->version),
2617                                    name);
2618
2619   slot = hash & (index->symbol_table_slots - 1);
2620   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2621   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2622
2623   for (;;)
2624     {
2625       /* Convert a slot number to an offset into the table.  */
2626       offset_type i = 2 * slot;
2627       const char *str;
2628       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2629         {
2630           do_cleanups (back_to);
2631           return 0;
2632         }
2633
2634       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2635       if (!cmp (name, str))
2636         {
2637           *vec_out = (offset_type *) (index->constant_pool
2638                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2639           do_cleanups (back_to);
2640           return 1;
2641         }
2642
2643       slot = (slot + step) & (index->symbol_table_slots - 1);
2644     }
2645 }
2646
2647 /* A helper function that reads the .gdb_index from SECTION and fills
2648    in MAP.  FILENAME is the name of the file containing the section;
2649    it is used for error reporting.  DEPRECATED_OK is nonzero if it is
2650    ok to use deprecated sections.
2651
2652    CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2653    out parameters that are filled in with information about the CU and
2654    TU lists in the section.
2655
2656    Returns 1 if all went well, 0 otherwise.  */
2657
2658 static int
2659 read_index_from_section (struct objfile *objfile,
2660                          const char *filename,
2661                          int deprecated_ok,
2662                          struct dwarf2_section_info *section,
2663                          struct mapped_index *map,
2664                          const gdb_byte **cu_list,
2665                          offset_type *cu_list_elements,
2666                          const gdb_byte **types_list,
2667                          offset_type *types_list_elements)
2668 {
2669   char *addr;
2670   offset_type version;
2671   offset_type *metadata;
2672   int i;
2673
2674   if (dwarf2_section_empty_p (section))
2675     return 0;
2676
2677   /* Older elfutils strip versions could keep the section in the main
2678      executable while splitting it for the separate debug info file.  */
2679   if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2680     return 0;
2681
2682   dwarf2_read_section (objfile, section);
2683
2684   addr = section->buffer;
2685   /* Version check.  */
2686   version = MAYBE_SWAP (*(offset_type *) addr);
2687   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2688      causes the index to behave very poorly for certain requests.  Version 3
2689      contained incomplete addrmap.  So, it seems better to just ignore such
2690      indices.  */
2691   if (version < 4)
2692     {
2693       static int warning_printed = 0;
2694       if (!warning_printed)
2695         {
2696           warning (_("Skipping obsolete .gdb_index section in %s."),
2697                    filename);
2698           warning_printed = 1;
2699         }
2700       return 0;
2701     }
2702   /* Index version 4 uses a different hash function than index version
2703      5 and later.
2704
2705      Versions earlier than 6 did not emit psymbols for inlined
2706      functions.  Using these files will cause GDB not to be able to
2707      set breakpoints on inlined functions by name, so we ignore these
2708      indices unless the user has done
2709      "set use-deprecated-index-sections on".  */
2710   if (version < 6 && !deprecated_ok)
2711     {
2712       static int warning_printed = 0;
2713       if (!warning_printed)
2714         {
2715           warning (_("\
2716 Skipping deprecated .gdb_index section in %s.\n\
2717 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2718 to use the section anyway."),
2719                    filename);
2720           warning_printed = 1;
2721         }
2722       return 0;
2723     }
2724   /* Version 7 indices generated by gold refer to the CU for a symbol instead
2725      of the TU (for symbols coming from TUs).  It's just a performance bug, and
2726      we can't distinguish gdb-generated indices from gold-generated ones, so
2727      nothing to do here.  */
2728
2729   /* Indexes with higher version than the one supported by GDB may be no
2730      longer backward compatible.  */
2731   if (version > 8)
2732     return 0;
2733
2734   map->version = version;
2735   map->total_size = section->size;
2736
2737   metadata = (offset_type *) (addr + sizeof (offset_type));
2738
2739   i = 0;
2740   *cu_list = addr + MAYBE_SWAP (metadata[i]);
2741   *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2742                        / 8);
2743   ++i;
2744
2745   *types_list = addr + MAYBE_SWAP (metadata[i]);
2746   *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2747                            - MAYBE_SWAP (metadata[i]))
2748                           / 8);
2749   ++i;
2750
2751   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2752   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2753                              - MAYBE_SWAP (metadata[i]));
2754   ++i;
2755
2756   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2757   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2758                               - MAYBE_SWAP (metadata[i]))
2759                              / (2 * sizeof (offset_type)));
2760   ++i;
2761
2762   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2763
2764   return 1;
2765 }
2766
2767
2768 /* Read the index file.  If everything went ok, initialize the "quick"
2769    elements of all the CUs and return 1.  Otherwise, return 0.  */
2770
2771 static int
2772 dwarf2_read_index (struct objfile *objfile)
2773 {
2774   struct mapped_index local_map, *map;
2775   const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2776   offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2777
2778   if (!read_index_from_section (objfile, objfile->name,
2779                                 use_deprecated_index_sections,
2780                                 &dwarf2_per_objfile->gdb_index, &local_map,
2781                                 &cu_list, &cu_list_elements,
2782                                 &types_list, &types_list_elements))
2783     return 0;
2784
2785   /* Don't use the index if it's empty.  */
2786   if (local_map.symbol_table_slots == 0)
2787     return 0;
2788
2789   /* If there is a .dwz file, read it so we can get its CU list as
2790      well.  */
2791   if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2792     {
2793       struct dwz_file *dwz = dwarf2_get_dwz_file ();
2794       struct mapped_index dwz_map;
2795       const gdb_byte *dwz_types_ignore;
2796       offset_type dwz_types_elements_ignore;
2797
2798       if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2799                                     1,
2800                                     &dwz->gdb_index, &dwz_map,
2801                                     &dwz_list, &dwz_list_elements,
2802                                     &dwz_types_ignore,
2803                                     &dwz_types_elements_ignore))
2804         {
2805           warning (_("could not read '.gdb_index' section from %s; skipping"),
2806                    bfd_get_filename (dwz->dwz_bfd));
2807           return 0;
2808         }
2809     }
2810
2811   create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2812                          dwz_list_elements);
2813
2814   if (types_list_elements)
2815     {
2816       struct dwarf2_section_info *section;
2817
2818       /* We can only handle a single .debug_types when we have an
2819          index.  */
2820       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2821         return 0;
2822
2823       section = VEC_index (dwarf2_section_info_def,
2824                            dwarf2_per_objfile->types, 0);
2825
2826       create_signatured_type_table_from_index (objfile, section, types_list,
2827                                                types_list_elements);
2828     }
2829
2830   create_addrmap_from_index (objfile, &local_map);
2831
2832   map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2833   *map = local_map;
2834
2835   dwarf2_per_objfile->index_table = map;
2836   dwarf2_per_objfile->using_index = 1;
2837   dwarf2_per_objfile->quick_file_names_table =
2838     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2839
2840   return 1;
2841 }
2842
2843 /* A helper for the "quick" functions which sets the global
2844    dwarf2_per_objfile according to OBJFILE.  */
2845
2846 static void
2847 dw2_setup (struct objfile *objfile)
2848 {
2849   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2850   gdb_assert (dwarf2_per_objfile);
2851 }
2852
2853 /* die_reader_func for dw2_get_file_names.  */
2854
2855 static void
2856 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2857                            gdb_byte *info_ptr,
2858                            struct die_info *comp_unit_die,
2859                            int has_children,
2860                            void *data)
2861 {
2862   struct dwarf2_cu *cu = reader->cu;
2863   struct dwarf2_per_cu_data *this_cu = cu->per_cu;  
2864   struct objfile *objfile = dwarf2_per_objfile->objfile;
2865   struct dwarf2_per_cu_data *lh_cu;
2866   struct line_header *lh;
2867   struct attribute *attr;
2868   int i;
2869   const char *name, *comp_dir;
2870   void **slot;
2871   struct quick_file_names *qfn;
2872   unsigned int line_offset;
2873
2874   /* Our callers never want to match partial units -- instead they
2875      will match the enclosing full CU.  */
2876   if (comp_unit_die->tag == DW_TAG_partial_unit)
2877     {
2878       this_cu->v.quick->no_file_data = 1;
2879       return;
2880     }
2881
2882   /* If we're reading the line header for TUs, store it in the "per_cu"
2883      for tu_group.  */
2884   if (this_cu->is_debug_types)
2885     {
2886       struct type_unit_group *tu_group = data;
2887
2888       gdb_assert (tu_group != NULL);
2889       lh_cu = &tu_group->per_cu;
2890     }
2891   else
2892     lh_cu = this_cu;
2893
2894   lh = NULL;
2895   slot = NULL;
2896   line_offset = 0;
2897
2898   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2899   if (attr)
2900     {
2901       struct quick_file_names find_entry;
2902
2903       line_offset = DW_UNSND (attr);
2904
2905       /* We may have already read in this line header (TU line header sharing).
2906          If we have we're done.  */
2907       find_entry.hash.dwo_unit = cu->dwo_unit;
2908       find_entry.hash.line_offset.sect_off = line_offset;
2909       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2910                              &find_entry, INSERT);
2911       if (*slot != NULL)
2912         {
2913           lh_cu->v.quick->file_names = *slot;
2914           return;
2915         }
2916
2917       lh = dwarf_decode_line_header (line_offset, cu);
2918     }
2919   if (lh == NULL)
2920     {
2921       lh_cu->v.quick->no_file_data = 1;
2922       return;
2923     }
2924
2925   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2926   qfn->hash.dwo_unit = cu->dwo_unit;
2927   qfn->hash.line_offset.sect_off = line_offset;
2928   gdb_assert (slot != NULL);
2929   *slot = qfn;
2930
2931   find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2932
2933   qfn->num_file_names = lh->num_file_names;
2934   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2935                                    lh->num_file_names * sizeof (char *));
2936   for (i = 0; i < lh->num_file_names; ++i)
2937     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2938   qfn->real_names = NULL;
2939
2940   free_line_header (lh);
2941
2942   lh_cu->v.quick->file_names = qfn;
2943 }
2944
2945 /* A helper for the "quick" functions which attempts to read the line
2946    table for THIS_CU.  */
2947
2948 static struct quick_file_names *
2949 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
2950 {
2951   /* For TUs this should only be called on the parent group.  */
2952   if (this_cu->is_debug_types)
2953     gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2954
2955   if (this_cu->v.quick->file_names != NULL)
2956     return this_cu->v.quick->file_names;
2957   /* If we know there is no line data, no point in looking again.  */
2958   if (this_cu->v.quick->no_file_data)
2959     return NULL;
2960
2961   /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2962      in the stub for CUs, there's is no need to lookup the DWO file.
2963      However, that's not the case for TUs where DW_AT_stmt_list lives in the
2964      DWO file.  */
2965   if (this_cu->is_debug_types)
2966     {
2967       struct type_unit_group *tu_group = this_cu->type_unit_group;
2968
2969       init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2970                                dw2_get_file_names_reader, tu_group);
2971     }
2972   else
2973     init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2974
2975   if (this_cu->v.quick->no_file_data)
2976     return NULL;
2977   return this_cu->v.quick->file_names;
2978 }
2979
2980 /* A helper for the "quick" functions which computes and caches the
2981    real path for a given file name from the line table.  */
2982
2983 static const char *
2984 dw2_get_real_path (struct objfile *objfile,
2985                    struct quick_file_names *qfn, int index)
2986 {
2987   if (qfn->real_names == NULL)
2988     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2989                                       qfn->num_file_names, sizeof (char *));
2990
2991   if (qfn->real_names[index] == NULL)
2992     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2993
2994   return qfn->real_names[index];
2995 }
2996
2997 static struct symtab *
2998 dw2_find_last_source_symtab (struct objfile *objfile)
2999 {
3000   int index;
3001
3002   dw2_setup (objfile);
3003   index = dwarf2_per_objfile->n_comp_units - 1;
3004   return dw2_instantiate_symtab (dw2_get_cu (index));
3005 }
3006
3007 /* Traversal function for dw2_forget_cached_source_info.  */
3008
3009 static int
3010 dw2_free_cached_file_names (void **slot, void *info)
3011 {
3012   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3013
3014   if (file_data->real_names)
3015     {
3016       int i;
3017
3018       for (i = 0; i < file_data->num_file_names; ++i)
3019         {
3020           xfree ((void*) file_data->real_names[i]);
3021           file_data->real_names[i] = NULL;
3022         }
3023     }
3024
3025   return 1;
3026 }
3027
3028 static void
3029 dw2_forget_cached_source_info (struct objfile *objfile)
3030 {
3031   dw2_setup (objfile);
3032
3033   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3034                           dw2_free_cached_file_names, NULL);
3035 }
3036
3037 /* Helper function for dw2_map_symtabs_matching_filename that expands
3038    the symtabs and calls the iterator.  */
3039
3040 static int
3041 dw2_map_expand_apply (struct objfile *objfile,
3042                       struct dwarf2_per_cu_data *per_cu,
3043                       const char *name, const char *real_path,
3044                       int (*callback) (struct symtab *, void *),
3045                       void *data)
3046 {
3047   struct symtab *last_made = objfile->symtabs;
3048
3049   /* Don't visit already-expanded CUs.  */
3050   if (per_cu->v.quick->symtab)
3051     return 0;
3052
3053   /* This may expand more than one symtab, and we want to iterate over
3054      all of them.  */
3055   dw2_instantiate_symtab (per_cu);
3056
3057   return iterate_over_some_symtabs (name, real_path, callback, data,
3058                                     objfile->symtabs, last_made);
3059 }
3060
3061 /* Implementation of the map_symtabs_matching_filename method.  */
3062
3063 static int
3064 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3065                                    const char *real_path,
3066                                    int (*callback) (struct symtab *, void *),
3067                                    void *data)
3068 {
3069   int i;
3070   const char *name_basename = lbasename (name);
3071
3072   dw2_setup (objfile);
3073
3074   /* The rule is CUs specify all the files, including those used by
3075      any TU, so there's no need to scan TUs here.  */
3076
3077   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3078     {
3079       int j;
3080       struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3081       struct quick_file_names *file_data;
3082
3083       /* We only need to look at symtabs not already expanded.  */
3084       if (per_cu->v.quick->symtab)
3085         continue;
3086
3087       file_data = dw2_get_file_names (per_cu);
3088       if (file_data == NULL)
3089         continue;
3090
3091       for (j = 0; j < file_data->num_file_names; ++j)
3092         {
3093           const char *this_name = file_data->file_names[j];
3094           const char *this_real_name;
3095
3096           if (compare_filenames_for_search (this_name, name))
3097             {
3098               if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3099                                         callback, data))
3100                 return 1;
3101               continue;
3102             }
3103
3104           /* Before we invoke realpath, which can get expensive when many
3105              files are involved, do a quick comparison of the basenames.  */
3106           if (! basenames_may_differ
3107               && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3108             continue;
3109
3110           this_real_name = dw2_get_real_path (objfile, file_data, j);
3111           if (compare_filenames_for_search (this_real_name, name))
3112             {
3113               if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3114                                         callback, data))
3115                 return 1;
3116               continue;
3117             }
3118
3119           if (real_path != NULL)
3120             {
3121               gdb_assert (IS_ABSOLUTE_PATH (real_path));
3122               gdb_assert (IS_ABSOLUTE_PATH (name));
3123               if (this_real_name != NULL
3124                   && FILENAME_CMP (real_path, this_real_name) == 0)
3125                 {
3126                   if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3127                                             callback, data))
3128                     return 1;
3129                   continue;
3130                 }
3131             }
3132         }
3133     }
3134
3135   return 0;
3136 }
3137
3138 /* Struct used to manage iterating over all CUs looking for a symbol.  */
3139
3140 struct dw2_symtab_iterator
3141 {
3142   /* The internalized form of .gdb_index.  */
3143   struct mapped_index *index;
3144   /* If non-zero, only look for symbols that match BLOCK_INDEX.  */
3145   int want_specific_block;
3146   /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3147      Unused if !WANT_SPECIFIC_BLOCK.  */
3148   int block_index;
3149   /* The kind of symbol we're looking for.  */
3150   domain_enum domain;
3151   /* The list of CUs from the index entry of the symbol,
3152      or NULL if not found.  */
3153   offset_type *vec;
3154   /* The next element in VEC to look at.  */
3155   int next;
3156   /* The number of elements in VEC, or zero if there is no match.  */
3157   int length;
3158 };
3159
3160 /* Initialize the index symtab iterator ITER.
3161    If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3162    in block BLOCK_INDEX.  Otherwise BLOCK_INDEX is ignored.  */
3163
3164 static void
3165 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3166                       struct mapped_index *index,
3167                       int want_specific_block,
3168                       int block_index,
3169                       domain_enum domain,
3170                       const char *name)
3171 {
3172   iter->index = index;
3173   iter->want_specific_block = want_specific_block;
3174   iter->block_index = block_index;
3175   iter->domain = domain;
3176   iter->next = 0;
3177
3178   if (find_slot_in_mapped_hash (index, name, &iter->vec))
3179     iter->length = MAYBE_SWAP (*iter->vec);
3180   else
3181     {
3182       iter->vec = NULL;
3183       iter->length = 0;
3184     }
3185 }
3186
3187 /* Return the next matching CU or NULL if there are no more.  */
3188
3189 static struct dwarf2_per_cu_data *
3190 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3191 {
3192   for ( ; iter->next < iter->length; ++iter->next)
3193     {
3194       offset_type cu_index_and_attrs =
3195         MAYBE_SWAP (iter->vec[iter->next + 1]);
3196       offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3197       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3198       int want_static = iter->block_index != GLOBAL_BLOCK;
3199       /* This value is only valid for index versions >= 7.  */
3200       int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3201       gdb_index_symbol_kind symbol_kind =
3202         GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3203       /* Only check the symbol attributes if they're present.
3204          Indices prior to version 7 don't record them,
3205          and indices >= 7 may elide them for certain symbols
3206          (gold does this).  */
3207       int attrs_valid =
3208         (iter->index->version >= 7
3209          && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3210
3211       /* Skip if already read in.  */
3212       if (per_cu->v.quick->symtab)
3213         continue;
3214
3215       if (attrs_valid
3216           && iter->want_specific_block
3217           && want_static != is_static)
3218         continue;
3219
3220       /* Only check the symbol's kind if it has one.  */
3221       if (attrs_valid)
3222         {
3223           switch (iter->domain)
3224             {
3225             case VAR_DOMAIN:
3226               if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3227                   && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3228                   /* Some types are also in VAR_DOMAIN.  */
3229                   && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3230                 continue;
3231               break;
3232             case STRUCT_DOMAIN:
3233               if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3234                 continue;
3235               break;
3236             case LABEL_DOMAIN:
3237               if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3238                 continue;
3239               break;
3240             default:
3241               break;
3242             }
3243         }
3244
3245       ++iter->next;
3246       return per_cu;
3247     }
3248
3249   return NULL;
3250 }
3251
3252 static struct symtab *
3253 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3254                    const char *name, domain_enum domain)
3255 {
3256   struct symtab *stab_best = NULL;
3257   struct mapped_index *index;
3258
3259   dw2_setup (objfile);
3260
3261   index = dwarf2_per_objfile->index_table;
3262
3263   /* index is NULL if OBJF_READNOW.  */
3264   if (index)
3265     {
3266       struct dw2_symtab_iterator iter;
3267       struct dwarf2_per_cu_data *per_cu;
3268
3269       dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3270
3271       while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3272         {
3273           struct symbol *sym = NULL;
3274           struct symtab *stab = dw2_instantiate_symtab (per_cu);
3275
3276           /* Some caution must be observed with overloaded functions
3277              and methods, since the index will not contain any overload
3278              information (but NAME might contain it).  */
3279           if (stab->primary)
3280             {
3281               struct blockvector *bv = BLOCKVECTOR (stab);
3282               struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3283
3284               sym = lookup_block_symbol (block, name, domain);
3285             }
3286
3287           if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3288             {
3289               if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3290                 return stab;
3291
3292               stab_best = stab;
3293             }
3294
3295           /* Keep looking through other CUs.  */
3296         }
3297     }
3298
3299   return stab_best;
3300 }
3301
3302 static void
3303 dw2_print_stats (struct objfile *objfile)
3304 {
3305   int i, total, count;
3306
3307   dw2_setup (objfile);
3308   total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3309   count = 0;
3310   for (i = 0; i < total; ++i)
3311     {
3312       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3313
3314       if (!per_cu->v.quick->symtab)
3315         ++count;
3316     }
3317   printf_filtered (_("  Number of read CUs: %d\n"), total - count);
3318   printf_filtered (_("  Number of unread CUs: %d\n"), count);
3319 }
3320
3321 static void
3322 dw2_dump (struct objfile *objfile)
3323 {
3324   /* Nothing worth printing.  */
3325 }
3326
3327 static void
3328 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3329               struct section_offsets *delta)
3330 {
3331   /* There's nothing to relocate here.  */
3332 }
3333
3334 static void
3335 dw2_expand_symtabs_for_function (struct objfile *objfile,
3336                                  const char *func_name)
3337 {
3338   struct mapped_index *index;
3339
3340   dw2_setup (objfile);
3341
3342   index = dwarf2_per_objfile->index_table;
3343
3344   /* index is NULL if OBJF_READNOW.  */
3345   if (index)
3346     {
3347       struct dw2_symtab_iterator iter;
3348       struct dwarf2_per_cu_data *per_cu;
3349
3350       /* Note: It doesn't matter what we pass for block_index here.  */
3351       dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3352                             func_name);
3353
3354       while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3355         dw2_instantiate_symtab (per_cu);
3356     }
3357 }
3358
3359 static void
3360 dw2_expand_all_symtabs (struct objfile *objfile)
3361 {
3362   int i;
3363
3364   dw2_setup (objfile);
3365
3366   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3367                    + dwarf2_per_objfile->n_type_units); ++i)
3368     {
3369       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3370
3371       dw2_instantiate_symtab (per_cu);
3372     }
3373 }
3374
3375 static void
3376 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3377                                   const char *fullname)
3378 {
3379   int i;
3380
3381   dw2_setup (objfile);
3382
3383   /* We don't need to consider type units here.
3384      This is only called for examining code, e.g. expand_line_sal.
3385      There can be an order of magnitude (or more) more type units
3386      than comp units, and we avoid them if we can.  */
3387
3388   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3389     {
3390       int j;
3391       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3392       struct quick_file_names *file_data;
3393
3394       /* We only need to look at symtabs not already expanded.  */
3395       if (per_cu->v.quick->symtab)
3396         continue;
3397
3398       file_data = dw2_get_file_names (per_cu);
3399       if (file_data == NULL)
3400         continue;
3401
3402       for (j = 0; j < file_data->num_file_names; ++j)
3403         {
3404           const char *this_fullname = file_data->file_names[j];
3405
3406           if (filename_cmp (this_fullname, fullname) == 0)
3407             {
3408               dw2_instantiate_symtab (per_cu);
3409               break;
3410             }
3411         }
3412     }
3413 }
3414
3415 /* A helper function for dw2_find_symbol_file that finds the primary
3416    file name for a given CU.  This is a die_reader_func.  */
3417
3418 static void
3419 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3420                                  gdb_byte *info_ptr,
3421                                  struct die_info *comp_unit_die,
3422                                  int has_children,
3423                                  void *data)
3424 {
3425   const char **result_ptr = data;
3426   struct dwarf2_cu *cu = reader->cu;
3427   struct attribute *attr;
3428
3429   attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3430   if (attr == NULL)
3431     *result_ptr = NULL;
3432   else
3433     *result_ptr = DW_STRING (attr);
3434 }
3435
3436 static const char *
3437 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3438 {
3439   struct dwarf2_per_cu_data *per_cu;
3440   offset_type *vec;
3441   const char *filename;
3442
3443   dw2_setup (objfile);
3444
3445   /* index_table is NULL if OBJF_READNOW.  */
3446   if (!dwarf2_per_objfile->index_table)
3447     {
3448       struct symtab *s;
3449
3450       ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3451         {
3452           struct blockvector *bv = BLOCKVECTOR (s);
3453           const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3454           struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3455
3456           if (sym)
3457             {
3458               /* Only file extension of returned filename is recognized.  */
3459               return SYMBOL_SYMTAB (sym)->filename;
3460             }
3461         }
3462       return NULL;
3463     }
3464
3465   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3466                                  name, &vec))
3467     return NULL;
3468
3469   /* Note that this just looks at the very first one named NAME -- but
3470      actually we are looking for a function.  find_main_filename
3471      should be rewritten so that it doesn't require a custom hook.  It
3472      could just use the ordinary symbol tables.  */
3473   /* vec[0] is the length, which must always be >0.  */
3474   per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3475
3476   if (per_cu->v.quick->symtab != NULL)
3477     {
3478       /* Only file extension of returned filename is recognized.  */
3479       return per_cu->v.quick->symtab->filename;
3480     }
3481
3482   init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3483                            dw2_get_primary_filename_reader, &filename);
3484
3485   /* Only file extension of returned filename is recognized.  */
3486   return filename;
3487 }
3488
3489 static void
3490 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3491                           struct objfile *objfile, int global,
3492                           int (*callback) (struct block *,
3493                                            struct symbol *, void *),
3494                           void *data, symbol_compare_ftype *match,
3495                           symbol_compare_ftype *ordered_compare)
3496 {
3497   /* Currently unimplemented; used for Ada.  The function can be called if the
3498      current language is Ada for a non-Ada objfile using GNU index.  As Ada
3499      does not look for non-Ada symbols this function should just return.  */
3500 }
3501
3502 static void
3503 dw2_expand_symtabs_matching
3504   (struct objfile *objfile,
3505    int (*file_matcher) (const char *, void *, int basenames),
3506    int (*name_matcher) (const char *, void *),
3507    enum search_domain kind,
3508    void *data)
3509 {
3510   int i;
3511   offset_type iter;
3512   struct mapped_index *index;
3513
3514   dw2_setup (objfile);
3515
3516   /* index_table is NULL if OBJF_READNOW.  */
3517   if (!dwarf2_per_objfile->index_table)
3518     return;
3519   index = dwarf2_per_objfile->index_table;
3520
3521   if (file_matcher != NULL)
3522     {
3523       struct cleanup *cleanup;
3524       htab_t visited_found, visited_not_found;
3525
3526       visited_found = htab_create_alloc (10,
3527                                          htab_hash_pointer, htab_eq_pointer,
3528                                          NULL, xcalloc, xfree);
3529       cleanup = make_cleanup_htab_delete (visited_found);
3530       visited_not_found = htab_create_alloc (10,
3531                                              htab_hash_pointer, htab_eq_pointer,
3532                                              NULL, xcalloc, xfree);
3533       make_cleanup_htab_delete (visited_not_found);
3534
3535       /* The rule is CUs specify all the files, including those used by
3536          any TU, so there's no need to scan TUs here.  */
3537
3538       for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3539         {
3540           int j;
3541           struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3542           struct quick_file_names *file_data;
3543           void **slot;
3544
3545           per_cu->v.quick->mark = 0;
3546
3547           /* We only need to look at symtabs not already expanded.  */
3548           if (per_cu->v.quick->symtab)
3549             continue;
3550
3551           file_data = dw2_get_file_names (per_cu);
3552           if (file_data == NULL)
3553             continue;
3554
3555           if (htab_find (visited_not_found, file_data) != NULL)
3556             continue;
3557           else if (htab_find (visited_found, file_data) != NULL)
3558             {
3559               per_cu->v.quick->mark = 1;
3560               continue;
3561             }
3562
3563           for (j = 0; j < file_data->num_file_names; ++j)
3564             {
3565               const char *this_real_name;
3566
3567               if (file_matcher (file_data->file_names[j], data, 0))
3568                 {
3569                   per_cu->v.quick->mark = 1;
3570                   break;
3571                 }
3572
3573               /* Before we invoke realpath, which can get expensive when many
3574                  files are involved, do a quick comparison of the basenames.  */
3575               if (!basenames_may_differ
3576                   && !file_matcher (lbasename (file_data->file_names[j]),
3577                                     data, 1))
3578                 continue;
3579
3580               this_real_name = dw2_get_real_path (objfile, file_data, j);
3581               if (file_matcher (this_real_name, data, 0))
3582                 {
3583                   per_cu->v.quick->mark = 1;
3584                   break;
3585                 }
3586             }
3587
3588           slot = htab_find_slot (per_cu->v.quick->mark
3589                                  ? visited_found
3590                                  : visited_not_found,
3591                                  file_data, INSERT);
3592           *slot = file_data;
3593         }
3594
3595       do_cleanups (cleanup);
3596     }
3597
3598   for (iter = 0; iter < index->symbol_table_slots; ++iter)
3599     {
3600       offset_type idx = 2 * iter;
3601       const char *name;
3602       offset_type *vec, vec_len, vec_idx;
3603
3604       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3605         continue;
3606
3607       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3608
3609       if (! (*name_matcher) (name, data))
3610         continue;
3611
3612       /* The name was matched, now expand corresponding CUs that were
3613          marked.  */
3614       vec = (offset_type *) (index->constant_pool
3615                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
3616       vec_len = MAYBE_SWAP (vec[0]);
3617       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3618         {
3619           struct dwarf2_per_cu_data *per_cu;
3620           offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3621           gdb_index_symbol_kind symbol_kind =
3622             GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3623           int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3624
3625           /* Don't crash on bad data.  */
3626           if (cu_index >= (dwarf2_per_objfile->n_comp_units
3627                            + dwarf2_per_objfile->n_type_units))
3628             continue;
3629
3630           /* Only check the symbol's kind if it has one.
3631              Indices prior to version 7 don't record it.  */
3632           if (index->version >= 7)
3633             {
3634               switch (kind)
3635                 {
3636                 case VARIABLES_DOMAIN:
3637                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3638                     continue;
3639                   break;
3640                 case FUNCTIONS_DOMAIN:
3641                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3642                     continue;
3643                   break;
3644                 case TYPES_DOMAIN:
3645                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3646                     continue;
3647                   break;
3648                 default:
3649                   break;
3650                 }
3651             }
3652
3653           per_cu = dw2_get_cu (cu_index);
3654           if (file_matcher == NULL || per_cu->v.quick->mark)
3655             dw2_instantiate_symtab (per_cu);
3656         }
3657     }
3658 }
3659
3660 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3661    symtab.  */
3662
3663 static struct symtab *
3664 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3665 {
3666   int i;
3667
3668   if (BLOCKVECTOR (symtab) != NULL
3669       && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3670     return symtab;
3671
3672   if (symtab->includes == NULL)
3673     return NULL;
3674
3675   for (i = 0; symtab->includes[i]; ++i)
3676     {
3677       struct symtab *s = symtab->includes[i];
3678
3679       s = recursively_find_pc_sect_symtab (s, pc);
3680       if (s != NULL)
3681         return s;
3682     }
3683
3684   return NULL;
3685 }
3686
3687 static struct symtab *
3688 dw2_find_pc_sect_symtab (struct objfile *objfile,
3689                          struct minimal_symbol *msymbol,
3690                          CORE_ADDR pc,
3691                          struct obj_section *section,
3692                          int warn_if_readin)
3693 {
3694   struct dwarf2_per_cu_data *data;
3695   struct symtab *result;
3696
3697   dw2_setup (objfile);
3698
3699   if (!objfile->psymtabs_addrmap)
3700     return NULL;
3701
3702   data = addrmap_find (objfile->psymtabs_addrmap, pc);
3703   if (!data)
3704     return NULL;
3705
3706   if (warn_if_readin && data->v.quick->symtab)
3707     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3708              paddress (get_objfile_arch (objfile), pc));
3709
3710   result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3711   gdb_assert (result != NULL);
3712   return result;
3713 }
3714
3715 static void
3716 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3717                           void *data, int need_fullname)
3718 {
3719   int i;
3720   struct cleanup *cleanup;
3721   htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3722                                       NULL, xcalloc, xfree);
3723
3724   cleanup = make_cleanup_htab_delete (visited);
3725   dw2_setup (objfile);
3726
3727   /* The rule is CUs specify all the files, including those used by
3728      any TU, so there's no need to scan TUs here.
3729      We can ignore file names coming from already-expanded CUs.  */
3730
3731   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3732     {
3733       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3734
3735       if (per_cu->v.quick->symtab)
3736         {
3737           void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3738                                         INSERT);
3739
3740           *slot = per_cu->v.quick->file_names;
3741         }
3742     }
3743
3744   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3745     {
3746       int j;
3747       struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3748       struct quick_file_names *file_data;
3749       void **slot;
3750
3751       /* We only need to look at symtabs not already expanded.  */
3752       if (per_cu->v.quick->symtab)
3753         continue;
3754
3755       file_data = dw2_get_file_names (per_cu);
3756       if (file_data == NULL)
3757         continue;
3758
3759       slot = htab_find_slot (visited, file_data, INSERT);
3760       if (*slot)
3761         {
3762           /* Already visited.  */
3763           continue;
3764         }
3765       *slot = file_data;
3766
3767       for (j = 0; j < file_data->num_file_names; ++j)
3768         {
3769           const char *this_real_name;
3770
3771           if (need_fullname)
3772             this_real_name = dw2_get_real_path (objfile, file_data, j);
3773           else
3774             this_real_name = NULL;
3775           (*fun) (file_data->file_names[j], this_real_name, data);
3776         }
3777     }
3778
3779   do_cleanups (cleanup);
3780 }
3781
3782 static int
3783 dw2_has_symbols (struct objfile *objfile)
3784 {
3785   return 1;
3786 }
3787
3788 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3789 {
3790   dw2_has_symbols,
3791   dw2_find_last_source_symtab,
3792   dw2_forget_cached_source_info,
3793   dw2_map_symtabs_matching_filename,
3794   dw2_lookup_symbol,
3795   dw2_print_stats,
3796   dw2_dump,
3797   dw2_relocate,
3798   dw2_expand_symtabs_for_function,
3799   dw2_expand_all_symtabs,
3800   dw2_expand_symtabs_with_fullname,
3801   dw2_find_symbol_file,
3802   dw2_map_matching_symbols,
3803   dw2_expand_symtabs_matching,
3804   dw2_find_pc_sect_symtab,
3805   dw2_map_symbol_filenames
3806 };
3807
3808 /* Initialize for reading DWARF for this objfile.  Return 0 if this
3809    file will use psymtabs, or 1 if using the GNU index.  */
3810
3811 int
3812 dwarf2_initialize_objfile (struct objfile *objfile)
3813 {
3814   /* If we're about to read full symbols, don't bother with the
3815      indices.  In this case we also don't care if some other debug
3816      format is making psymtabs, because they are all about to be
3817      expanded anyway.  */
3818   if ((objfile->flags & OBJF_READNOW))
3819     {
3820       int i;
3821
3822       dwarf2_per_objfile->using_index = 1;
3823       create_all_comp_units (objfile);
3824       create_all_type_units (objfile);
3825       dwarf2_per_objfile->quick_file_names_table =
3826         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3827
3828       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3829                        + dwarf2_per_objfile->n_type_units); ++i)
3830         {
3831           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3832
3833           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3834                                             struct dwarf2_per_cu_quick_data);
3835         }
3836
3837       /* Return 1 so that gdb sees the "quick" functions.  However,
3838          these functions will be no-ops because we will have expanded
3839          all symtabs.  */
3840       return 1;
3841     }
3842
3843   if (dwarf2_read_index (objfile))
3844     return 1;
3845
3846   return 0;
3847 }
3848
3849 \f
3850
3851 /* Build a partial symbol table.  */
3852
3853 void
3854 dwarf2_build_psymtabs (struct objfile *objfile)
3855 {
3856   volatile struct gdb_exception except;
3857
3858   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3859     {
3860       init_psymbol_list (objfile, 1024);
3861     }
3862
3863   TRY_CATCH (except, RETURN_MASK_ERROR)
3864     {
3865       /* This isn't really ideal: all the data we allocate on the
3866          objfile's obstack is still uselessly kept around.  However,
3867          freeing it seems unsafe.  */
3868       struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3869
3870       dwarf2_build_psymtabs_hard (objfile);
3871       discard_cleanups (cleanups);
3872     }
3873   if (except.reason < 0)
3874     exception_print (gdb_stderr, except);
3875 }
3876
3877 /* Return the total length of the CU described by HEADER.  */
3878
3879 static unsigned int
3880 get_cu_length (const struct comp_unit_head *header)
3881 {
3882   return header->initial_length_size + header->length;
3883 }
3884
3885 /* Return TRUE if OFFSET is within CU_HEADER.  */
3886
3887 static inline int
3888 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3889 {
3890   sect_offset bottom = { cu_header->offset.sect_off };
3891   sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3892
3893   return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3894 }
3895
3896 /* Find the base address of the compilation unit for range lists and
3897    location lists.  It will normally be specified by DW_AT_low_pc.
3898    In DWARF-3 draft 4, the base address could be overridden by
3899    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3900    compilation units with discontinuous ranges.  */
3901
3902 static void
3903 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3904 {
3905   struct attribute *attr;
3906
3907   cu->base_known = 0;
3908   cu->base_address = 0;
3909
3910   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3911   if (attr)
3912     {
3913       cu->base_address = DW_ADDR (attr);
3914       cu->base_known = 1;
3915     }
3916   else
3917     {
3918       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3919       if (attr)
3920         {
3921           cu->base_address = DW_ADDR (attr);
3922           cu->base_known = 1;
3923         }
3924     }
3925 }
3926
3927 /* Read in the comp unit header information from the debug_info at info_ptr.
3928    NOTE: This leaves members offset, first_die_offset to be filled in
3929    by the caller.  */
3930
3931 static gdb_byte *
3932 read_comp_unit_head (struct comp_unit_head *cu_header,
3933                      gdb_byte *info_ptr, bfd *abfd)
3934 {
3935   int signed_addr;
3936   unsigned int bytes_read;
3937
3938   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3939   cu_header->initial_length_size = bytes_read;
3940   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3941   info_ptr += bytes_read;
3942   cu_header->version = read_2_bytes (abfd, info_ptr);
3943   info_ptr += 2;
3944   cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3945                                              &bytes_read);
3946   info_ptr += bytes_read;
3947   cu_header->addr_size = read_1_byte (abfd, info_ptr);
3948   info_ptr += 1;
3949   signed_addr = bfd_get_sign_extend_vma (abfd);
3950   if (signed_addr < 0)
3951     internal_error (__FILE__, __LINE__,
3952                     _("read_comp_unit_head: dwarf from non elf file"));
3953   cu_header->signed_addr_p = signed_addr;
3954
3955   return info_ptr;
3956 }
3957
3958 /* Helper function that returns the proper abbrev section for
3959    THIS_CU.  */
3960
3961 static struct dwarf2_section_info *
3962 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3963 {
3964   struct dwarf2_section_info *abbrev;
3965
3966   if (this_cu->is_dwz)
3967     abbrev = &dwarf2_get_dwz_file ()->abbrev;
3968   else
3969     abbrev = &dwarf2_per_objfile->abbrev;
3970
3971   return abbrev;
3972 }
3973
3974 /* Subroutine of read_and_check_comp_unit_head and
3975    read_and_check_type_unit_head to simplify them.
3976    Perform various error checking on the header.  */
3977
3978 static void
3979 error_check_comp_unit_head (struct comp_unit_head *header,
3980                             struct dwarf2_section_info *section,
3981                             struct dwarf2_section_info *abbrev_section)
3982 {
3983   bfd *abfd = section->asection->owner;
3984   const char *filename = bfd_get_filename (abfd);
3985
3986   if (header->version != 2 && header->version != 3 && header->version != 4)
3987     error (_("Dwarf Error: wrong version in compilation unit header "
3988            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3989            filename);
3990
3991   if (header->abbrev_offset.sect_off
3992       >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3993     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3994            "(offset 0x%lx + 6) [in module %s]"),
3995            (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3996            filename);
3997
3998   /* Cast to unsigned long to use 64-bit arithmetic when possible to
3999      avoid potential 32-bit overflow.  */
4000   if (((unsigned long) header->offset.sect_off + get_cu_length (header))
4001       > section->size)
4002     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4003            "(offset 0x%lx + 0) [in module %s]"),
4004            (long) header->length, (long) header->offset.sect_off,
4005            filename);
4006 }
4007
4008 /* Read in a CU/TU header and perform some basic error checking.
4009    The contents of the header are stored in HEADER.
4010    The result is a pointer to the start of the first DIE.  */
4011
4012 static gdb_byte *
4013 read_and_check_comp_unit_head (struct comp_unit_head *header,
4014                                struct dwarf2_section_info *section,
4015                                struct dwarf2_section_info *abbrev_section,
4016                                gdb_byte *info_ptr,
4017                                int is_debug_types_section)
4018 {
4019   gdb_byte *beg_of_comp_unit = info_ptr;
4020   bfd *abfd = section->asection->owner;
4021
4022   header->offset.sect_off = beg_of_comp_unit - section->buffer;
4023
4024   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4025
4026   /* If we're reading a type unit, skip over the signature and
4027      type_offset fields.  */
4028   if (is_debug_types_section)
4029     info_ptr += 8 /*signature*/ + header->offset_size;
4030
4031   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4032
4033   error_check_comp_unit_head (header, section, abbrev_section);
4034
4035   return info_ptr;
4036 }
4037
4038 /* Read in the types comp unit header information from .debug_types entry at
4039    types_ptr.  The result is a pointer to one past the end of the header.  */
4040
4041 static gdb_byte *
4042 read_and_check_type_unit_head (struct comp_unit_head *header,
4043                                struct dwarf2_section_info *section,
4044                                struct dwarf2_section_info *abbrev_section,
4045                                gdb_byte *info_ptr,
4046                                ULONGEST *signature,
4047                                cu_offset *type_offset_in_tu)
4048 {
4049   gdb_byte *beg_of_comp_unit = info_ptr;
4050   bfd *abfd = section->asection->owner;
4051
4052   header->offset.sect_off = beg_of_comp_unit - section->buffer;
4053
4054   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4055
4056   /* If we're reading a type unit, skip over the signature and
4057      type_offset fields.  */
4058   if (signature != NULL)
4059     *signature = read_8_bytes (abfd, info_ptr);
4060   info_ptr += 8;
4061   if (type_offset_in_tu != NULL)
4062     type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4063                                                header->offset_size);
4064   info_ptr += header->offset_size;
4065
4066   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4067
4068   error_check_comp_unit_head (header, section, abbrev_section);
4069
4070   return info_ptr;
4071 }
4072
4073 /* Fetch the abbreviation table offset from a comp or type unit header.  */
4074
4075 static sect_offset
4076 read_abbrev_offset (struct dwarf2_section_info *section,
4077                     sect_offset offset)
4078 {
4079   bfd *abfd = section->asection->owner;
4080   gdb_byte *info_ptr;
4081   unsigned int length, initial_length_size, offset_size;
4082   sect_offset abbrev_offset;
4083
4084   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4085   info_ptr = section->buffer + offset.sect_off;
4086   length = read_initial_length (abfd, info_ptr, &initial_length_size);
4087   offset_size = initial_length_size == 4 ? 4 : 8;
4088   info_ptr += initial_length_size + 2 /*version*/;
4089   abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4090   return abbrev_offset;
4091 }
4092
4093 /* Allocate a new partial symtab for file named NAME and mark this new
4094    partial symtab as being an include of PST.  */
4095
4096 static void
4097 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4098                                struct objfile *objfile)
4099 {
4100   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4101
4102   if (!IS_ABSOLUTE_PATH (subpst->filename))
4103     {
4104       /* It shares objfile->objfile_obstack.  */
4105       subpst->dirname = pst->dirname;
4106     }
4107
4108   subpst->section_offsets = pst->section_offsets;
4109   subpst->textlow = 0;
4110   subpst->texthigh = 0;
4111
4112   subpst->dependencies = (struct partial_symtab **)
4113     obstack_alloc (&objfile->objfile_obstack,
4114                    sizeof (struct partial_symtab *));
4115   subpst->dependencies[0] = pst;
4116   subpst->number_of_dependencies = 1;
4117
4118   subpst->globals_offset = 0;
4119   subpst->n_global_syms = 0;
4120   subpst->statics_offset = 0;
4121   subpst->n_static_syms = 0;
4122   subpst->symtab = NULL;
4123   subpst->read_symtab = pst->read_symtab;
4124   subpst->readin = 0;
4125
4126   /* No private part is necessary for include psymtabs.  This property
4127      can be used to differentiate between such include psymtabs and
4128      the regular ones.  */
4129   subpst->read_symtab_private = NULL;
4130 }
4131
4132 /* Read the Line Number Program data and extract the list of files
4133    included by the source file represented by PST.  Build an include
4134    partial symtab for each of these included files.  */
4135
4136 static void
4137 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4138                                struct die_info *die,
4139                                struct partial_symtab *pst)
4140 {
4141   struct line_header *lh = NULL;
4142   struct attribute *attr;
4143
4144   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4145   if (attr)
4146     lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4147   if (lh == NULL)
4148     return;  /* No linetable, so no includes.  */
4149
4150   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
4151   dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4152
4153   free_line_header (lh);
4154 }
4155
4156 static hashval_t
4157 hash_signatured_type (const void *item)
4158 {
4159   const struct signatured_type *sig_type = item;
4160
4161   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
4162   return sig_type->signature;
4163 }
4164
4165 static int
4166 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4167 {
4168   const struct signatured_type *lhs = item_lhs;
4169   const struct signatured_type *rhs = item_rhs;
4170
4171   return lhs->signature == rhs->signature;
4172 }
4173
4174 /* Allocate a hash table for signatured types.  */
4175
4176 static htab_t
4177 allocate_signatured_type_table (struct objfile *objfile)
4178 {
4179   return htab_create_alloc_ex (41,
4180                                hash_signatured_type,
4181                                eq_signatured_type,
4182                                NULL,
4183                                &objfile->objfile_obstack,
4184                                hashtab_obstack_allocate,
4185                                dummy_obstack_deallocate);
4186 }
4187
4188 /* A helper function to add a signatured type CU to a table.  */
4189
4190 static int
4191 add_signatured_type_cu_to_table (void **slot, void *datum)
4192 {
4193   struct signatured_type *sigt = *slot;
4194   struct signatured_type ***datap = datum;
4195
4196   **datap = sigt;
4197   ++*datap;
4198
4199   return 1;
4200 }
4201
4202 /* Create the hash table of all entries in the .debug_types section.
4203    DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4204    NULL otherwise.
4205    Note: This function processes DWO files only, not DWP files.
4206    The result is a pointer to the hash table or NULL if there are
4207    no types.  */
4208
4209 static htab_t
4210 create_debug_types_hash_table (struct dwo_file *dwo_file,
4211                                VEC (dwarf2_section_info_def) *types)
4212 {
4213   struct objfile *objfile = dwarf2_per_objfile->objfile;
4214   htab_t types_htab = NULL;
4215   int ix;
4216   struct dwarf2_section_info *section;
4217   struct dwarf2_section_info *abbrev_section;
4218
4219   if (VEC_empty (dwarf2_section_info_def, types))
4220     return NULL;
4221
4222   abbrev_section = (dwo_file != NULL
4223                     ? &dwo_file->sections.abbrev
4224                     : &dwarf2_per_objfile->abbrev);
4225
4226   if (dwarf2_read_debug)
4227     fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4228                         dwo_file ? ".dwo" : "",
4229                         bfd_get_filename (abbrev_section->asection->owner));
4230
4231   for (ix = 0;
4232        VEC_iterate (dwarf2_section_info_def, types, ix, section);
4233        ++ix)
4234     {
4235       bfd *abfd;
4236       gdb_byte *info_ptr, *end_ptr;
4237       struct dwarf2_section_info *abbrev_section;
4238
4239       dwarf2_read_section (objfile, section);
4240       info_ptr = section->buffer;
4241
4242       if (info_ptr == NULL)
4243         continue;
4244
4245       /* We can't set abfd until now because the section may be empty or
4246          not present, in which case section->asection will be NULL.  */
4247       abfd = section->asection->owner;
4248
4249       if (dwo_file)
4250         abbrev_section = &dwo_file->sections.abbrev;
4251       else
4252         abbrev_section = &dwarf2_per_objfile->abbrev;
4253
4254       if (types_htab == NULL)
4255         {
4256           if (dwo_file)
4257             types_htab = allocate_dwo_unit_table (objfile);
4258           else
4259             types_htab = allocate_signatured_type_table (objfile);
4260         }
4261
4262       /* We don't use init_cutu_and_read_dies_simple, or some such, here
4263          because we don't need to read any dies: the signature is in the
4264          header.  */
4265
4266       end_ptr = info_ptr + section->size;
4267       while (info_ptr < end_ptr)
4268         {
4269           sect_offset offset;
4270           cu_offset type_offset_in_tu;
4271           ULONGEST signature;
4272           struct signatured_type *sig_type;
4273           struct dwo_unit *dwo_tu;
4274           void **slot;
4275           gdb_byte *ptr = info_ptr;
4276           struct comp_unit_head header;
4277           unsigned int length;
4278
4279           offset.sect_off = ptr - section->buffer;
4280
4281           /* We need to read the type's signature in order to build the hash
4282              table, but we don't need anything else just yet.  */
4283
4284           ptr = read_and_check_type_unit_head (&header, section,
4285                                                abbrev_section, ptr,
4286                                                &signature, &type_offset_in_tu);
4287
4288           length = get_cu_length (&header);
4289
4290           /* Skip dummy type units.  */
4291           if (ptr >= info_ptr + length
4292               || peek_abbrev_code (abfd, ptr) == 0)
4293             {
4294               info_ptr += length;
4295               continue;
4296             }
4297
4298           if (dwo_file)
4299             {
4300               sig_type = NULL;
4301               dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4302                                        struct dwo_unit);
4303               dwo_tu->dwo_file = dwo_file;
4304               dwo_tu->signature = signature;
4305               dwo_tu->type_offset_in_tu = type_offset_in_tu;
4306               dwo_tu->section = section;
4307               dwo_tu->offset = offset;
4308               dwo_tu->length = length;
4309             }
4310           else
4311             {
4312               /* N.B.: type_offset is not usable if this type uses a DWO file.
4313                  The real type_offset is in the DWO file.  */
4314               dwo_tu = NULL;
4315               sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4316                                          struct signatured_type);
4317               sig_type->signature = signature;
4318               sig_type->type_offset_in_tu = type_offset_in_tu;
4319               sig_type->per_cu.objfile = objfile;
4320               sig_type->per_cu.is_debug_types = 1;
4321               sig_type->per_cu.section = section;
4322               sig_type->per_cu.offset = offset;
4323               sig_type->per_cu.length = length;
4324             }
4325
4326           slot = htab_find_slot (types_htab,
4327                                  dwo_file ? (void*) dwo_tu : (void *) sig_type,
4328                                  INSERT);
4329           gdb_assert (slot != NULL);
4330           if (*slot != NULL)
4331             {
4332               sect_offset dup_offset;
4333
4334               if (dwo_file)
4335                 {
4336                   const struct dwo_unit *dup_tu = *slot;
4337
4338                   dup_offset = dup_tu->offset;
4339                 }
4340               else
4341                 {
4342                   const struct signatured_type *dup_tu = *slot;
4343
4344                   dup_offset = dup_tu->per_cu.offset;
4345                 }
4346
4347               complaint (&symfile_complaints,
4348                          _("debug type entry at offset 0x%x is duplicate to the "
4349                            "entry at offset 0x%x, signature 0x%s"),
4350                          offset.sect_off, dup_offset.sect_off,
4351                          phex (signature, sizeof (signature)));
4352             }
4353           *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4354
4355           if (dwarf2_read_debug)
4356             fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
4357                                 offset.sect_off,
4358                                 phex (signature, sizeof (signature)));
4359
4360           info_ptr += length;
4361         }
4362     }
4363
4364   return types_htab;
4365 }
4366
4367 /* Create the hash table of all entries in the .debug_types section,
4368    and initialize all_type_units.
4369    The result is zero if there is an error (e.g. missing .debug_types section),
4370    otherwise non-zero.  */
4371
4372 static int
4373 create_all_type_units (struct objfile *objfile)
4374 {
4375   htab_t types_htab;
4376   struct signatured_type **iter;
4377
4378   types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4379   if (types_htab == NULL)
4380     {
4381       dwarf2_per_objfile->signatured_types = NULL;
4382       return 0;
4383     }
4384
4385   dwarf2_per_objfile->signatured_types = types_htab;
4386
4387   dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4388   dwarf2_per_objfile->all_type_units
4389     = obstack_alloc (&objfile->objfile_obstack,
4390                      dwarf2_per_objfile->n_type_units
4391                      * sizeof (struct signatured_type *));
4392   iter = &dwarf2_per_objfile->all_type_units[0];
4393   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4394   gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4395               == dwarf2_per_objfile->n_type_units);
4396
4397   return 1;
4398 }
4399
4400 /* Lookup a signature based type for DW_FORM_ref_sig8.
4401    Returns NULL if signature SIG is not present in the table.  */
4402
4403 static struct signatured_type *
4404 lookup_signatured_type (ULONGEST sig)
4405 {
4406   struct signatured_type find_entry, *entry;
4407
4408   if (dwarf2_per_objfile->signatured_types == NULL)
4409     {
4410       complaint (&symfile_complaints,
4411                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4412       return NULL;
4413     }
4414
4415   find_entry.signature = sig;
4416   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4417   return entry;
4418 }
4419 \f
4420 /* Low level DIE reading support.  */
4421
4422 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
4423
4424 static void
4425 init_cu_die_reader (struct die_reader_specs *reader,
4426                     struct dwarf2_cu *cu,
4427                     struct dwarf2_section_info *section,
4428                     struct dwo_file *dwo_file)
4429 {
4430   gdb_assert (section->readin && section->buffer != NULL);
4431   reader->abfd = section->asection->owner;
4432   reader->cu = cu;
4433   reader->dwo_file = dwo_file;
4434   reader->die_section = section;
4435   reader->buffer = section->buffer;
4436   reader->buffer_end = section->buffer + section->size;
4437 }
4438
4439 /* Initialize a CU (or TU) and read its DIEs.
4440    If the CU defers to a DWO file, read the DWO file as well.
4441
4442    ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4443    Otherwise the table specified in the comp unit header is read in and used.
4444    This is an optimization for when we already have the abbrev table.
4445
4446    If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4447    Otherwise, a new CU is allocated with xmalloc.
4448
4449    If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4450    read_in_chain.  Otherwise the dwarf2_cu data is freed at the end.
4451
4452    WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4453    linker) then DIE_READER_FUNC will not get called.  */
4454
4455 static void
4456 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4457                          struct abbrev_table *abbrev_table,
4458                          int use_existing_cu, int keep,
4459                          die_reader_func_ftype *die_reader_func,
4460                          void *data)
4461 {
4462   struct objfile *objfile = dwarf2_per_objfile->objfile;
4463   struct dwarf2_section_info *section = this_cu->section;
4464   bfd *abfd = section->asection->owner;
4465   struct dwarf2_cu *cu;
4466   gdb_byte *begin_info_ptr, *info_ptr;
4467   struct die_reader_specs reader;
4468   struct die_info *comp_unit_die;
4469   int has_children;
4470   struct attribute *attr;
4471   struct cleanup *cleanups, *free_cu_cleanup = NULL;
4472   struct signatured_type *sig_type = NULL;
4473   struct dwarf2_section_info *abbrev_section;
4474   /* Non-zero if CU currently points to a DWO file and we need to
4475      reread it.  When this happens we need to reread the skeleton die
4476      before we can reread the DWO file.  */
4477   int rereading_dwo_cu = 0;
4478
4479   if (dwarf2_die_debug)
4480     fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4481                         this_cu->is_debug_types ? "type" : "comp",
4482                         this_cu->offset.sect_off);
4483
4484   if (use_existing_cu)
4485     gdb_assert (keep);
4486
4487   cleanups = make_cleanup (null_cleanup, NULL);
4488
4489   /* This is cheap if the section is already read in.  */
4490   dwarf2_read_section (objfile, section);
4491
4492   begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4493
4494   abbrev_section = get_abbrev_section_for_cu (this_cu);
4495
4496   if (use_existing_cu && this_cu->cu != NULL)
4497     {
4498       cu = this_cu->cu;
4499
4500       /* If this CU is from a DWO file we need to start over, we need to
4501          refetch the attributes from the skeleton CU.
4502          This could be optimized by retrieving those attributes from when we
4503          were here the first time: the previous comp_unit_die was stored in
4504          comp_unit_obstack.  But there's no data yet that we need this
4505          optimization.  */
4506       if (cu->dwo_unit != NULL)
4507         rereading_dwo_cu = 1;
4508     }
4509   else
4510     {
4511       /* If !use_existing_cu, this_cu->cu must be NULL.  */
4512       gdb_assert (this_cu->cu == NULL);
4513
4514       cu = xmalloc (sizeof (*cu));
4515       init_one_comp_unit (cu, this_cu);
4516
4517       /* If an error occurs while loading, release our storage.  */
4518       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4519     }
4520
4521   if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4522     {
4523       /* We already have the header, there's no need to read it in again.  */
4524       info_ptr += cu->header.first_die_offset.cu_off;
4525     }
4526   else
4527     {
4528       if (this_cu->is_debug_types)
4529         {
4530           ULONGEST signature;
4531           cu_offset type_offset_in_tu;
4532
4533           info_ptr = read_and_check_type_unit_head (&cu->header, section,
4534                                                     abbrev_section, info_ptr,
4535                                                     &signature,
4536                                                     &type_offset_in_tu);
4537
4538           /* Since per_cu is the first member of struct signatured_type,
4539              we can go from a pointer to one to a pointer to the other.  */
4540           sig_type = (struct signatured_type *) this_cu;
4541           gdb_assert (sig_type->signature == signature);
4542           gdb_assert (sig_type->type_offset_in_tu.cu_off
4543                       == type_offset_in_tu.cu_off);
4544           gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4545
4546           /* LENGTH has not been set yet for type units if we're
4547              using .gdb_index.  */
4548           this_cu->length = get_cu_length (&cu->header);
4549
4550           /* Establish the type offset that can be used to lookup the type.  */
4551           sig_type->type_offset_in_section.sect_off =
4552             this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4553         }
4554       else
4555         {
4556           info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4557                                                     abbrev_section,
4558                                                     info_ptr, 0);
4559
4560           gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4561           gdb_assert (this_cu->length == get_cu_length (&cu->header));
4562         }
4563     }
4564
4565   /* Skip dummy compilation units.  */
4566   if (info_ptr >= begin_info_ptr + this_cu->length
4567       || peek_abbrev_code (abfd, info_ptr) == 0)
4568     {
4569       do_cleanups (cleanups);
4570       return;
4571     }
4572
4573   /* If we don't have them yet, read the abbrevs for this compilation unit.
4574      And if we need to read them now, make sure they're freed when we're
4575      done.  Note that it's important that if the CU had an abbrev table
4576      on entry we don't free it when we're done: Somewhere up the call stack
4577      it may be in use.  */
4578   if (abbrev_table != NULL)
4579     {
4580       gdb_assert (cu->abbrev_table == NULL);
4581       gdb_assert (cu->header.abbrev_offset.sect_off
4582                   == abbrev_table->offset.sect_off);
4583       cu->abbrev_table = abbrev_table;
4584     }
4585   else if (cu->abbrev_table == NULL)
4586     {
4587       dwarf2_read_abbrevs (cu, abbrev_section);
4588       make_cleanup (dwarf2_free_abbrev_table, cu);
4589     }
4590   else if (rereading_dwo_cu)
4591     {
4592       dwarf2_free_abbrev_table (cu);
4593       dwarf2_read_abbrevs (cu, abbrev_section);
4594     }
4595
4596   /* Read the top level CU/TU die.  */
4597   init_cu_die_reader (&reader, cu, section, NULL);
4598   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4599
4600   /* If we have a DWO stub, process it and then read in the DWO file.
4601      Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4602      a DWO CU, that this test will fail.  */
4603   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4604   if (attr)
4605     {
4606       const char *dwo_name = DW_STRING (attr);
4607       const char *comp_dir_string;
4608       struct dwo_unit *dwo_unit;
4609       ULONGEST signature; /* Or dwo_id.  */
4610       struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4611       int i,num_extra_attrs;
4612       struct dwarf2_section_info *dwo_abbrev_section;
4613
4614       if (has_children)
4615         error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4616                  " has children (offset 0x%x) [in module %s]"),
4617                this_cu->offset.sect_off, bfd_get_filename (abfd));
4618
4619       /* These attributes aren't processed until later:
4620          DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4621          However, the attribute is found in the stub which we won't have later.
4622          In order to not impose this complication on the rest of the code,
4623          we read them here and copy them to the DWO CU/TU die.  */
4624
4625       /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4626          DWO file.  */
4627       stmt_list = NULL;
4628       if (! this_cu->is_debug_types)
4629         stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4630       low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4631       high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4632       ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4633       comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4634
4635       /* There should be a DW_AT_addr_base attribute here (if needed).
4636          We need the value before we can process DW_FORM_GNU_addr_index.  */
4637       cu->addr_base = 0;
4638       attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4639       if (attr)
4640         cu->addr_base = DW_UNSND (attr);
4641
4642       /* There should be a DW_AT_ranges_base attribute here (if needed).
4643          We need the value before we can process DW_AT_ranges.  */
4644       cu->ranges_base = 0;
4645       attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4646       if (attr)
4647         cu->ranges_base = DW_UNSND (attr);
4648
4649       if (this_cu->is_debug_types)
4650         {
4651           gdb_assert (sig_type != NULL);
4652           signature = sig_type->signature;
4653         }
4654       else
4655         {
4656           attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4657           if (! attr)
4658             error (_("Dwarf Error: missing dwo_id [in module %s]"),
4659                    dwo_name);
4660           signature = DW_UNSND (attr);
4661         }
4662
4663       /* We may need the comp_dir in order to find the DWO file.  */
4664       comp_dir_string = NULL;
4665       if (comp_dir)
4666         comp_dir_string = DW_STRING (comp_dir);
4667
4668       if (this_cu->is_debug_types)
4669         dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4670       else
4671         dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4672                                          signature);
4673
4674       if (dwo_unit == NULL)
4675         {
4676           error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4677                    " with ID %s [in module %s]"),
4678                  this_cu->offset.sect_off,
4679                  phex (signature, sizeof (signature)),
4680                  objfile->name);
4681         }
4682
4683       /* Set up for reading the DWO CU/TU.  */
4684       cu->dwo_unit = dwo_unit;
4685       section = dwo_unit->section;
4686       dwarf2_read_section (objfile, section);
4687       begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4688       dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4689       init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4690
4691       if (this_cu->is_debug_types)
4692         {
4693           ULONGEST signature;
4694           cu_offset type_offset_in_tu;
4695
4696           info_ptr = read_and_check_type_unit_head (&cu->header, section,
4697                                                     dwo_abbrev_section,
4698                                                     info_ptr,
4699                                                     &signature,
4700                                                     &type_offset_in_tu);
4701           gdb_assert (sig_type->signature == signature);
4702           gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4703           /* For DWOs coming from DWP files, we don't know the CU length
4704              nor the type's offset in the TU until now.  */
4705           dwo_unit->length = get_cu_length (&cu->header);
4706           dwo_unit->type_offset_in_tu = type_offset_in_tu;
4707
4708           /* Establish the type offset that can be used to lookup the type.
4709              For DWO files, we don't know it until now.  */
4710           sig_type->type_offset_in_section.sect_off =
4711             dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4712         }
4713       else
4714         {
4715           info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4716                                                     dwo_abbrev_section,
4717                                                     info_ptr, 0);
4718           gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4719           /* For DWOs coming from DWP files, we don't know the CU length
4720              until now.  */
4721           dwo_unit->length = get_cu_length (&cu->header);
4722         }
4723
4724       /* Discard the original CU's abbrev table, and read the DWO's.  */
4725       if (abbrev_table == NULL)
4726         {
4727           dwarf2_free_abbrev_table (cu);
4728           dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4729         }
4730       else
4731         {
4732           dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4733           make_cleanup (dwarf2_free_abbrev_table, cu);
4734         }
4735
4736       /* Read in the die, but leave space to copy over the attributes
4737          from the stub.  This has the benefit of simplifying the rest of
4738          the code - all the real work is done here.  */
4739       num_extra_attrs = ((stmt_list != NULL)
4740                          + (low_pc != NULL)
4741                          + (high_pc != NULL)
4742                          + (ranges != NULL)
4743                          + (comp_dir != NULL));
4744       info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4745                                   &has_children, num_extra_attrs);
4746
4747       /* Copy over the attributes from the stub to the DWO die.  */
4748       i = comp_unit_die->num_attrs;
4749       if (stmt_list != NULL)
4750         comp_unit_die->attrs[i++] = *stmt_list;
4751       if (low_pc != NULL)
4752         comp_unit_die->attrs[i++] = *low_pc;
4753       if (high_pc != NULL)
4754         comp_unit_die->attrs[i++] = *high_pc;
4755       if (ranges != NULL)
4756         comp_unit_die->attrs[i++] = *ranges;
4757       if (comp_dir != NULL)
4758         comp_unit_die->attrs[i++] = *comp_dir;
4759       comp_unit_die->num_attrs += num_extra_attrs;
4760
4761       /* Skip dummy compilation units.  */
4762       if (info_ptr >= begin_info_ptr + dwo_unit->length
4763           || peek_abbrev_code (abfd, info_ptr) == 0)
4764         {
4765           do_cleanups (cleanups);
4766           return;
4767         }
4768     }
4769
4770   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4771
4772   if (free_cu_cleanup != NULL)
4773     {
4774       if (keep)
4775         {
4776           /* We've successfully allocated this compilation unit.  Let our
4777              caller clean it up when finished with it.  */
4778           discard_cleanups (free_cu_cleanup);
4779
4780           /* We can only discard free_cu_cleanup and all subsequent cleanups.
4781              So we have to manually free the abbrev table.  */
4782           dwarf2_free_abbrev_table (cu);
4783
4784           /* Link this CU into read_in_chain.  */
4785           this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4786           dwarf2_per_objfile->read_in_chain = this_cu;
4787         }
4788       else
4789         do_cleanups (free_cu_cleanup);
4790     }
4791
4792   do_cleanups (cleanups);
4793 }
4794
4795 /* Read CU/TU THIS_CU in section SECTION,
4796    but do not follow DW_AT_GNU_dwo_name if present.
4797    DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4798    to have already done the lookup to find the DWO/DWP file).
4799
4800    The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4801    THIS_CU->is_debug_types, but nothing else.
4802
4803    We fill in THIS_CU->length.
4804
4805    WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4806    linker) then DIE_READER_FUNC will not get called.
4807
4808    THIS_CU->cu is always freed when done.
4809    This is done in order to not leave THIS_CU->cu in a state where we have
4810    to care whether it refers to the "main" CU or the DWO CU.  */
4811
4812 static void
4813 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4814                                    struct dwarf2_section_info *abbrev_section,
4815                                    struct dwo_file *dwo_file,
4816                                    die_reader_func_ftype *die_reader_func,
4817                                    void *data)
4818 {
4819   struct objfile *objfile = dwarf2_per_objfile->objfile;
4820   struct dwarf2_section_info *section = this_cu->section;
4821   bfd *abfd = section->asection->owner;
4822   struct dwarf2_cu cu;
4823   gdb_byte *begin_info_ptr, *info_ptr;
4824   struct die_reader_specs reader;
4825   struct cleanup *cleanups;
4826   struct die_info *comp_unit_die;
4827   int has_children;
4828
4829   if (dwarf2_die_debug)
4830     fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4831                         this_cu->is_debug_types ? "type" : "comp",
4832                         this_cu->offset.sect_off);
4833
4834   gdb_assert (this_cu->cu == NULL);
4835
4836   /* This is cheap if the section is already read in.  */
4837   dwarf2_read_section (objfile, section);
4838
4839   init_one_comp_unit (&cu, this_cu);
4840
4841   cleanups = make_cleanup (free_stack_comp_unit, &cu);
4842
4843   begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4844   info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4845                                             abbrev_section, info_ptr,
4846                                             this_cu->is_debug_types);
4847
4848   this_cu->length = get_cu_length (&cu.header);
4849
4850   /* Skip dummy compilation units.  */
4851   if (info_ptr >= begin_info_ptr + this_cu->length
4852       || peek_abbrev_code (abfd, info_ptr) == 0)
4853     {
4854       do_cleanups (cleanups);
4855       return;
4856     }
4857
4858   dwarf2_read_abbrevs (&cu, abbrev_section);
4859   make_cleanup (dwarf2_free_abbrev_table, &cu);
4860
4861   init_cu_die_reader (&reader, &cu, section, dwo_file);
4862   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4863
4864   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4865
4866   do_cleanups (cleanups);
4867 }
4868
4869 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4870    does not lookup the specified DWO file.
4871    This cannot be used to read DWO files.
4872
4873    THIS_CU->cu is always freed when done.
4874    This is done in order to not leave THIS_CU->cu in a state where we have
4875    to care whether it refers to the "main" CU or the DWO CU.
4876    We can revisit this if the data shows there's a performance issue.  */
4877
4878 static void
4879 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4880                                 die_reader_func_ftype *die_reader_func,
4881                                 void *data)
4882 {
4883   init_cutu_and_read_dies_no_follow (this_cu,
4884                                      get_abbrev_section_for_cu (this_cu),
4885                                      NULL,
4886                                      die_reader_func, data);
4887 }
4888 \f
4889 /* Type Unit Groups.
4890
4891    Type Unit Groups are a way to collapse the set of all TUs (type units) into
4892    a more manageable set.  The grouping is done by DW_AT_stmt_list entry
4893    so that all types coming from the same compilation (.o file) are grouped
4894    together.  A future step could be to put the types in the same symtab as
4895    the CU the types ultimately came from.  */
4896
4897 static hashval_t
4898 hash_type_unit_group (const void *item)
4899 {
4900   const struct type_unit_group *tu_group = item;
4901
4902   return hash_stmt_list_entry (&tu_group->hash);
4903 }
4904
4905 static int
4906 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4907 {
4908   const struct type_unit_group *lhs = item_lhs;
4909   const struct type_unit_group *rhs = item_rhs;
4910
4911   return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4912 }
4913
4914 /* Allocate a hash table for type unit groups.  */
4915
4916 static htab_t
4917 allocate_type_unit_groups_table (void)
4918 {
4919   return htab_create_alloc_ex (3,
4920                                hash_type_unit_group,
4921                                eq_type_unit_group,
4922                                NULL,
4923                                &dwarf2_per_objfile->objfile->objfile_obstack,
4924                                hashtab_obstack_allocate,
4925                                dummy_obstack_deallocate);
4926 }
4927
4928 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4929    partial symtabs.  We combine several TUs per psymtab to not let the size
4930    of any one psymtab grow too big.  */
4931 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4932 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4933
4934 /* Helper routine for get_type_unit_group.
4935    Create the type_unit_group object used to hold one or more TUs.  */
4936
4937 static struct type_unit_group *
4938 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4939 {
4940   struct objfile *objfile = dwarf2_per_objfile->objfile;
4941   struct dwarf2_per_cu_data *per_cu;
4942   struct type_unit_group *tu_group;
4943
4944   tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4945                              struct type_unit_group);
4946   per_cu = &tu_group->per_cu;
4947   per_cu->objfile = objfile;
4948   per_cu->is_debug_types = 1;
4949   per_cu->type_unit_group = tu_group;
4950
4951   if (dwarf2_per_objfile->using_index)
4952     {
4953       per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4954                                         struct dwarf2_per_cu_quick_data);
4955       tu_group->t.first_tu = cu->per_cu;
4956     }
4957   else
4958     {
4959       unsigned int line_offset = line_offset_struct.sect_off;
4960       struct partial_symtab *pst;
4961       char *name;
4962
4963       /* Give the symtab a useful name for debug purposes.  */
4964       if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4965         name = xstrprintf ("<type_units_%d>",
4966                            (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4967       else
4968         name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4969
4970       pst = create_partial_symtab (per_cu, name);
4971       pst->anonymous = 1;
4972
4973       xfree (name);
4974     }
4975
4976   tu_group->hash.dwo_unit = cu->dwo_unit;
4977   tu_group->hash.line_offset = line_offset_struct;
4978
4979   return tu_group;
4980 }
4981
4982 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4983    STMT_LIST is a DW_AT_stmt_list attribute.  */
4984
4985 static struct type_unit_group *
4986 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
4987 {
4988   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4989   struct type_unit_group *tu_group;
4990   void **slot;
4991   unsigned int line_offset;
4992   struct type_unit_group type_unit_group_for_lookup;
4993
4994   if (dwarf2_per_objfile->type_unit_groups == NULL)
4995     {
4996       dwarf2_per_objfile->type_unit_groups =
4997         allocate_type_unit_groups_table ();
4998     }
4999
5000   /* Do we need to create a new group, or can we use an existing one?  */
5001
5002   if (stmt_list)
5003     {
5004       line_offset = DW_UNSND (stmt_list);
5005       ++tu_stats->nr_symtab_sharers;
5006     }
5007   else
5008     {
5009       /* Ugh, no stmt_list.  Rare, but we have to handle it.
5010          We can do various things here like create one group per TU or
5011          spread them over multiple groups to split up the expansion work.
5012          To avoid worst case scenarios (too many groups or too large groups)
5013          we, umm, group them in bunches.  */
5014       line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5015                      | (tu_stats->nr_stmt_less_type_units
5016                         / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5017       ++tu_stats->nr_stmt_less_type_units;
5018     }
5019
5020   type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5021   type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5022   slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5023                          &type_unit_group_for_lookup, INSERT);
5024   if (*slot != NULL)
5025     {
5026       tu_group = *slot;
5027       gdb_assert (tu_group != NULL);
5028     }
5029   else
5030     {
5031       sect_offset line_offset_struct;
5032
5033       line_offset_struct.sect_off = line_offset;
5034       tu_group = create_type_unit_group (cu, line_offset_struct);
5035       *slot = tu_group;
5036       ++tu_stats->nr_symtabs;
5037     }
5038
5039   return tu_group;
5040 }
5041
5042 /* Struct used to sort TUs by their abbreviation table offset.  */
5043
5044 struct tu_abbrev_offset
5045 {
5046   struct signatured_type *sig_type;
5047   sect_offset abbrev_offset;
5048 };
5049
5050 /* Helper routine for build_type_unit_groups, passed to qsort.  */
5051
5052 static int
5053 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5054 {
5055   const struct tu_abbrev_offset * const *a = ap;
5056   const struct tu_abbrev_offset * const *b = bp;
5057   unsigned int aoff = (*a)->abbrev_offset.sect_off;
5058   unsigned int boff = (*b)->abbrev_offset.sect_off;
5059
5060   return (aoff > boff) - (aoff < boff);
5061 }
5062
5063 /* A helper function to add a type_unit_group to a table.  */
5064
5065 static int
5066 add_type_unit_group_to_table (void **slot, void *datum)
5067 {
5068   struct type_unit_group *tu_group = *slot;
5069   struct type_unit_group ***datap = datum;
5070
5071   **datap = tu_group;
5072   ++*datap;
5073
5074   return 1;
5075 }
5076
5077 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5078    each one passing FUNC,DATA.
5079
5080    The efficiency is because we sort TUs by the abbrev table they use and
5081    only read each abbrev table once.  In one program there are 200K TUs
5082    sharing 8K abbrev tables.
5083
5084    The main purpose of this function is to support building the
5085    dwarf2_per_objfile->type_unit_groups table.
5086    TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5087    can collapse the search space by grouping them by stmt_list.
5088    The savings can be significant, in the same program from above the 200K TUs
5089    share 8K stmt_list tables.
5090
5091    FUNC is expected to call get_type_unit_group, which will create the
5092    struct type_unit_group if necessary and add it to
5093    dwarf2_per_objfile->type_unit_groups.  */
5094
5095 static void
5096 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5097 {
5098   struct objfile *objfile = dwarf2_per_objfile->objfile;
5099   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5100   struct cleanup *cleanups;
5101   struct abbrev_table *abbrev_table;
5102   sect_offset abbrev_offset;
5103   struct tu_abbrev_offset *sorted_by_abbrev;
5104   struct type_unit_group **iter;
5105   int i;
5106
5107   /* It's up to the caller to not call us multiple times.  */
5108   gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5109
5110   if (dwarf2_per_objfile->n_type_units == 0)
5111     return;
5112
5113   /* TUs typically share abbrev tables, and there can be way more TUs than
5114      abbrev tables.  Sort by abbrev table to reduce the number of times we
5115      read each abbrev table in.
5116      Alternatives are to punt or to maintain a cache of abbrev tables.
5117      This is simpler and efficient enough for now.
5118
5119      Later we group TUs by their DW_AT_stmt_list value (as this defines the
5120      symtab to use).  Typically TUs with the same abbrev offset have the same
5121      stmt_list value too so in practice this should work well.
5122
5123      The basic algorithm here is:
5124
5125       sort TUs by abbrev table
5126       for each TU with same abbrev table:
5127         read abbrev table if first user
5128         read TU top level DIE
5129           [IWBN if DWO skeletons had DW_AT_stmt_list]
5130         call FUNC  */
5131
5132   if (dwarf2_read_debug)
5133     fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5134
5135   /* Sort in a separate table to maintain the order of all_type_units
5136      for .gdb_index: TU indices directly index all_type_units.  */
5137   sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5138                               dwarf2_per_objfile->n_type_units);
5139   for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5140     {
5141       struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5142
5143       sorted_by_abbrev[i].sig_type = sig_type;
5144       sorted_by_abbrev[i].abbrev_offset =
5145         read_abbrev_offset (sig_type->per_cu.section,
5146                             sig_type->per_cu.offset);
5147     }
5148   cleanups = make_cleanup (xfree, sorted_by_abbrev);
5149   qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5150          sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5151
5152   /* Note: In the .gdb_index case, get_type_unit_group may have already been
5153      called any number of times, so we don't reset tu_stats here.  */
5154
5155   abbrev_offset.sect_off = ~(unsigned) 0;
5156   abbrev_table = NULL;
5157   make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5158
5159   for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5160     {
5161       const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5162
5163       /* Switch to the next abbrev table if necessary.  */
5164       if (abbrev_table == NULL
5165           || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5166         {
5167           if (abbrev_table != NULL)
5168             {
5169               abbrev_table_free (abbrev_table);
5170               /* Reset to NULL in case abbrev_table_read_table throws
5171                  an error: abbrev_table_free_cleanup will get called.  */
5172               abbrev_table = NULL;
5173             }
5174           abbrev_offset = tu->abbrev_offset;
5175           abbrev_table =
5176             abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5177                                      abbrev_offset);
5178           ++tu_stats->nr_uniq_abbrev_tables;
5179         }
5180
5181       init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5182                                func, data);
5183     }
5184
5185   /* Create a vector of pointers to primary type units to make it easy to
5186      iterate over them and CUs.  See dw2_get_primary_cu.  */
5187   dwarf2_per_objfile->n_type_unit_groups =
5188     htab_elements (dwarf2_per_objfile->type_unit_groups);
5189   dwarf2_per_objfile->all_type_unit_groups =
5190     obstack_alloc (&objfile->objfile_obstack,
5191                    dwarf2_per_objfile->n_type_unit_groups
5192                    * sizeof (struct type_unit_group *));
5193   iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5194   htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5195                           add_type_unit_group_to_table, &iter);
5196   gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5197               == dwarf2_per_objfile->n_type_unit_groups);
5198
5199   do_cleanups (cleanups);
5200
5201   if (dwarf2_read_debug)
5202     {
5203       fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5204       fprintf_unfiltered (gdb_stdlog, "  %d TUs\n",
5205                           dwarf2_per_objfile->n_type_units);
5206       fprintf_unfiltered (gdb_stdlog, "  %d uniq abbrev tables\n",
5207                           tu_stats->nr_uniq_abbrev_tables);
5208       fprintf_unfiltered (gdb_stdlog, "  %d symtabs from stmt_list entries\n",
5209                           tu_stats->nr_symtabs);
5210       fprintf_unfiltered (gdb_stdlog, "  %d symtab sharers\n",
5211                           tu_stats->nr_symtab_sharers);
5212       fprintf_unfiltered (gdb_stdlog, "  %d type units without a stmt_list\n",
5213                           tu_stats->nr_stmt_less_type_units);
5214     }
5215 }
5216 \f
5217 /* Partial symbol tables.  */
5218
5219 /* Create a psymtab named NAME and assign it to PER_CU.
5220
5221    The caller must fill in the following details:
5222    dirname, textlow, texthigh.  */
5223
5224 static struct partial_symtab *
5225 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5226 {
5227   struct objfile *objfile = per_cu->objfile;
5228   struct partial_symtab *pst;
5229
5230   pst = start_psymtab_common (objfile, objfile->section_offsets,
5231                               name, 0,
5232                               objfile->global_psymbols.next,
5233                               objfile->static_psymbols.next);
5234
5235   pst->psymtabs_addrmap_supported = 1;
5236
5237   /* This is the glue that links PST into GDB's symbol API.  */
5238   pst->read_symtab_private = per_cu;
5239   pst->read_symtab = dwarf2_read_symtab;
5240   per_cu->v.psymtab = pst;
5241
5242   return pst;
5243 }
5244
5245 /* die_reader_func for process_psymtab_comp_unit.  */
5246
5247 static void
5248 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5249                                   gdb_byte *info_ptr,
5250                                   struct die_info *comp_unit_die,
5251                                   int has_children,
5252                                   void *data)
5253 {
5254   struct dwarf2_cu *cu = reader->cu;
5255   struct objfile *objfile = cu->objfile;
5256   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5257   struct attribute *attr;
5258   CORE_ADDR baseaddr;
5259   CORE_ADDR best_lowpc = 0, best_highpc = 0;
5260   struct partial_symtab *pst;
5261   int has_pc_info;
5262   const char *filename;
5263   int *want_partial_unit_ptr = data;
5264
5265   if (comp_unit_die->tag == DW_TAG_partial_unit
5266       && (want_partial_unit_ptr == NULL
5267           || !*want_partial_unit_ptr))
5268     return;
5269
5270   gdb_assert (! per_cu->is_debug_types);
5271
5272   prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5273
5274   cu->list_in_scope = &file_symbols;
5275
5276   /* Allocate a new partial symbol table structure.  */
5277   attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5278   if (attr == NULL || !DW_STRING (attr))
5279     filename = "";
5280   else
5281     filename = DW_STRING (attr);
5282
5283   pst = create_partial_symtab (per_cu, filename);
5284
5285   /* This must be done before calling dwarf2_build_include_psymtabs.  */
5286   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5287   if (attr != NULL)
5288     pst->dirname = DW_STRING (attr);
5289
5290   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5291
5292   dwarf2_find_base_address (comp_unit_die, cu);
5293
5294   /* Possibly set the default values of LOWPC and HIGHPC from
5295      `DW_AT_ranges'.  */
5296   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5297                                       &best_highpc, cu, pst);
5298   if (has_pc_info == 1 && best_lowpc < best_highpc)
5299     /* Store the contiguous range if it is not empty; it can be empty for
5300        CUs with no code.  */
5301     addrmap_set_empty (objfile->psymtabs_addrmap,
5302                        best_lowpc + baseaddr,
5303                        best_highpc + baseaddr - 1, pst);
5304
5305   /* Check if comp unit has_children.
5306      If so, read the rest of the partial symbols from this comp unit.
5307      If not, there's no more debug_info for this comp unit.  */
5308   if (has_children)
5309     {
5310       struct partial_die_info *first_die;
5311       CORE_ADDR lowpc, highpc;
5312
5313       lowpc = ((CORE_ADDR) -1);
5314       highpc = ((CORE_ADDR) 0);
5315
5316       first_die = load_partial_dies (reader, info_ptr, 1);
5317
5318       scan_partial_symbols (first_die, &lowpc, &highpc,
5319                             ! has_pc_info, cu);
5320
5321       /* If we didn't find a lowpc, set it to highpc to avoid
5322          complaints from `maint check'.  */
5323       if (lowpc == ((CORE_ADDR) -1))
5324         lowpc = highpc;
5325
5326       /* If the compilation unit didn't have an explicit address range,
5327          then use the information extracted from its child dies.  */
5328       if (! has_pc_info)
5329         {
5330           best_lowpc = lowpc;
5331           best_highpc = highpc;
5332         }
5333     }
5334   pst->textlow = best_lowpc + baseaddr;
5335   pst->texthigh = best_highpc + baseaddr;
5336
5337   pst->n_global_syms = objfile->global_psymbols.next -
5338     (objfile->global_psymbols.list + pst->globals_offset);
5339   pst->n_static_syms = objfile->static_psymbols.next -
5340     (objfile->static_psymbols.list + pst->statics_offset);
5341   sort_pst_symbols (objfile, pst);
5342
5343   if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5344     {
5345       int i;
5346       int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5347       struct dwarf2_per_cu_data *iter;
5348
5349       /* Fill in 'dependencies' here; we fill in 'users' in a
5350          post-pass.  */
5351       pst->number_of_dependencies = len;
5352       pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5353                                          len * sizeof (struct symtab *));
5354       for (i = 0;
5355            VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5356                         i, iter);
5357            ++i)
5358         pst->dependencies[i] = iter->v.psymtab;
5359
5360       VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5361     }
5362
5363   /* Get the list of files included in the current compilation unit,
5364      and build a psymtab for each of them.  */
5365   dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5366
5367   if (dwarf2_read_debug)
5368     {
5369       struct gdbarch *gdbarch = get_objfile_arch (objfile);
5370
5371       fprintf_unfiltered (gdb_stdlog,
5372                           "Psymtab for %s unit @0x%x: %s - %s"
5373                           ", %d global, %d static syms\n",
5374                           per_cu->is_debug_types ? "type" : "comp",
5375                           per_cu->offset.sect_off,
5376                           paddress (gdbarch, pst->textlow),
5377                           paddress (gdbarch, pst->texthigh),
5378                           pst->n_global_syms, pst->n_static_syms);
5379     }
5380 }
5381
5382 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5383    Process compilation unit THIS_CU for a psymtab.  */
5384
5385 static void
5386 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5387                            int want_partial_unit)
5388 {
5389   /* If this compilation unit was already read in, free the
5390      cached copy in order to read it in again.  This is
5391      necessary because we skipped some symbols when we first
5392      read in the compilation unit (see load_partial_dies).
5393      This problem could be avoided, but the benefit is unclear.  */
5394   if (this_cu->cu != NULL)
5395     free_one_cached_comp_unit (this_cu);
5396
5397   gdb_assert (! this_cu->is_debug_types);
5398   init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5399                            process_psymtab_comp_unit_reader,
5400                            &want_partial_unit);
5401
5402   /* Age out any secondary CUs.  */
5403   age_cached_comp_units ();
5404 }
5405
5406 /* Reader function for build_type_psymtabs.  */
5407
5408 static void
5409 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5410                             gdb_byte *info_ptr,
5411                             struct die_info *type_unit_die,
5412                             int has_children,
5413                             void *data)
5414 {
5415   struct objfile *objfile = dwarf2_per_objfile->objfile;
5416   struct dwarf2_cu *cu = reader->cu;
5417   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5418   struct type_unit_group *tu_group;
5419   struct attribute *attr;
5420   struct partial_die_info *first_die;
5421   CORE_ADDR lowpc, highpc;
5422   struct partial_symtab *pst;
5423
5424   gdb_assert (data == NULL);
5425
5426   if (! has_children)
5427     return;
5428
5429   attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5430   tu_group = get_type_unit_group (cu, attr);
5431
5432   VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5433
5434   prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5435   cu->list_in_scope = &file_symbols;
5436   pst = create_partial_symtab (per_cu, "");
5437   pst->anonymous = 1;
5438
5439   first_die = load_partial_dies (reader, info_ptr, 1);
5440
5441   lowpc = (CORE_ADDR) -1;
5442   highpc = (CORE_ADDR) 0;
5443   scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5444
5445   pst->n_global_syms = objfile->global_psymbols.next -
5446     (objfile->global_psymbols.list + pst->globals_offset);
5447   pst->n_static_syms = objfile->static_psymbols.next -
5448     (objfile->static_psymbols.list + pst->statics_offset);
5449   sort_pst_symbols (objfile, pst);
5450 }
5451
5452 /* Traversal function for build_type_psymtabs.  */
5453
5454 static int
5455 build_type_psymtab_dependencies (void **slot, void *info)
5456 {
5457   struct objfile *objfile = dwarf2_per_objfile->objfile;
5458   struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5459   struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5460   struct partial_symtab *pst = per_cu->v.psymtab;
5461   int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5462   struct dwarf2_per_cu_data *iter;
5463   int i;
5464
5465   gdb_assert (len > 0);
5466
5467   pst->number_of_dependencies = len;
5468   pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5469                                      len * sizeof (struct psymtab *));
5470   for (i = 0;
5471        VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5472        ++i)
5473     {
5474       pst->dependencies[i] = iter->v.psymtab;
5475       iter->type_unit_group = tu_group;
5476     }
5477
5478   VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5479
5480   return 1;
5481 }
5482
5483 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5484    Build partial symbol tables for the .debug_types comp-units.  */
5485
5486 static void
5487 build_type_psymtabs (struct objfile *objfile)
5488 {
5489   if (! create_all_type_units (objfile))
5490     return;
5491
5492   build_type_unit_groups (build_type_psymtabs_reader, NULL);
5493
5494   /* Now that all TUs have been processed we can fill in the dependencies.  */
5495   htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5496                           build_type_psymtab_dependencies, NULL);
5497 }
5498
5499 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
5500
5501 static void
5502 psymtabs_addrmap_cleanup (void *o)
5503 {
5504   struct objfile *objfile = o;
5505
5506   objfile->psymtabs_addrmap = NULL;
5507 }
5508
5509 /* Compute the 'user' field for each psymtab in OBJFILE.  */
5510
5511 static void
5512 set_partial_user (struct objfile *objfile)
5513 {
5514   int i;
5515
5516   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5517     {
5518       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5519       struct partial_symtab *pst = per_cu->v.psymtab;
5520       int j;
5521
5522       if (pst == NULL)
5523         continue;
5524
5525       for (j = 0; j < pst->number_of_dependencies; ++j)
5526         {
5527           /* Set the 'user' field only if it is not already set.  */
5528           if (pst->dependencies[j]->user == NULL)
5529             pst->dependencies[j]->user = pst;
5530         }
5531     }
5532 }
5533
5534 /* Build the partial symbol table by doing a quick pass through the
5535    .debug_info and .debug_abbrev sections.  */
5536
5537 static void
5538 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5539 {
5540   struct cleanup *back_to, *addrmap_cleanup;
5541   struct obstack temp_obstack;
5542   int i;
5543
5544   if (dwarf2_read_debug)
5545     {
5546       fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5547                           objfile->name);
5548     }
5549
5550   dwarf2_per_objfile->reading_partial_symbols = 1;
5551
5552   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5553
5554   /* Any cached compilation units will be linked by the per-objfile
5555      read_in_chain.  Make sure to free them when we're done.  */
5556   back_to = make_cleanup (free_cached_comp_units, NULL);
5557
5558   build_type_psymtabs (objfile);
5559
5560   create_all_comp_units (objfile);
5561
5562   /* Create a temporary address map on a temporary obstack.  We later
5563      copy this to the final obstack.  */
5564   obstack_init (&temp_obstack);
5565   make_cleanup_obstack_free (&temp_obstack);
5566   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5567   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5568
5569   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5570     {
5571       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5572
5573       process_psymtab_comp_unit (per_cu, 0);
5574     }
5575
5576   set_partial_user (objfile);
5577
5578   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5579                                                     &objfile->objfile_obstack);
5580   discard_cleanups (addrmap_cleanup);
5581
5582   do_cleanups (back_to);
5583
5584   if (dwarf2_read_debug)
5585     fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5586                         objfile->name);
5587 }
5588
5589 /* die_reader_func for load_partial_comp_unit.  */
5590
5591 static void
5592 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5593                                gdb_byte *info_ptr,
5594                                struct die_info *comp_unit_die,
5595                                int has_children,
5596                                void *data)
5597 {
5598   struct dwarf2_cu *cu = reader->cu;
5599
5600   prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5601
5602   /* Check if comp unit has_children.
5603      If so, read the rest of the partial symbols from this comp unit.
5604      If not, there's no more debug_info for this comp unit.  */
5605   if (has_children)
5606     load_partial_dies (reader, info_ptr, 0);
5607 }
5608
5609 /* Load the partial DIEs for a secondary CU into memory.
5610    This is also used when rereading a primary CU with load_all_dies.  */
5611
5612 static void
5613 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5614 {
5615   init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5616                            load_partial_comp_unit_reader, NULL);
5617 }
5618
5619 static void
5620 read_comp_units_from_section (struct objfile *objfile,
5621                               struct dwarf2_section_info *section,
5622                               unsigned int is_dwz,
5623                               int *n_allocated,
5624                               int *n_comp_units,
5625                               struct dwarf2_per_cu_data ***all_comp_units)
5626 {
5627   gdb_byte *info_ptr;
5628   bfd *abfd = section->asection->owner;
5629
5630   dwarf2_read_section (objfile, section);
5631
5632   info_ptr = section->buffer;
5633
5634   while (info_ptr < section->buffer + section->size)
5635     {
5636       unsigned int length, initial_length_size;
5637       struct dwarf2_per_cu_data *this_cu;
5638       sect_offset offset;
5639
5640       offset.sect_off = info_ptr - section->buffer;
5641
5642       /* Read just enough information to find out where the next
5643          compilation unit is.  */
5644       length = read_initial_length (abfd, info_ptr, &initial_length_size);
5645
5646       /* Save the compilation unit for later lookup.  */
5647       this_cu = obstack_alloc (&objfile->objfile_obstack,
5648                                sizeof (struct dwarf2_per_cu_data));
5649       memset (this_cu, 0, sizeof (*this_cu));
5650       this_cu->offset = offset;
5651       this_cu->length = length + initial_length_size;
5652       this_cu->is_dwz = is_dwz;
5653       this_cu->objfile = objfile;
5654       this_cu->section = section;
5655
5656       if (*n_comp_units == *n_allocated)
5657         {
5658           *n_allocated *= 2;
5659           *all_comp_units = xrealloc (*all_comp_units,
5660                                       *n_allocated
5661                                       * sizeof (struct dwarf2_per_cu_data *));
5662         }
5663       (*all_comp_units)[*n_comp_units] = this_cu;
5664       ++*n_comp_units;
5665
5666       info_ptr = info_ptr + this_cu->length;
5667     }
5668 }
5669
5670 /* Create a list of all compilation units in OBJFILE.
5671    This is only done for -readnow and building partial symtabs.  */
5672
5673 static void
5674 create_all_comp_units (struct objfile *objfile)
5675 {
5676   int n_allocated;
5677   int n_comp_units;
5678   struct dwarf2_per_cu_data **all_comp_units;
5679
5680   n_comp_units = 0;
5681   n_allocated = 10;
5682   all_comp_units = xmalloc (n_allocated
5683                             * sizeof (struct dwarf2_per_cu_data *));
5684
5685   read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5686                                 &n_allocated, &n_comp_units, &all_comp_units);
5687
5688   if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5689     {
5690       struct dwz_file *dwz = dwarf2_get_dwz_file ();
5691
5692       read_comp_units_from_section (objfile, &dwz->info, 1,
5693                                     &n_allocated, &n_comp_units,
5694                                     &all_comp_units);
5695     }
5696
5697   dwarf2_per_objfile->all_comp_units
5698     = obstack_alloc (&objfile->objfile_obstack,
5699                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5700   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5701           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5702   xfree (all_comp_units);
5703   dwarf2_per_objfile->n_comp_units = n_comp_units;
5704 }
5705
5706 /* Process all loaded DIEs for compilation unit CU, starting at
5707    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
5708    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5709    DW_AT_ranges).  If NEED_PC is set, then this function will set
5710    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5711    and record the covered ranges in the addrmap.  */
5712
5713 static void
5714 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5715                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5716 {
5717   struct partial_die_info *pdi;
5718
5719   /* Now, march along the PDI's, descending into ones which have
5720      interesting children but skipping the children of the other ones,
5721      until we reach the end of the compilation unit.  */
5722
5723   pdi = first_die;
5724
5725   while (pdi != NULL)
5726     {
5727       fixup_partial_die (pdi, cu);
5728
5729       /* Anonymous namespaces or modules have no name but have interesting
5730          children, so we need to look at them.  Ditto for anonymous
5731          enums.  */
5732
5733       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5734           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5735           || pdi->tag == DW_TAG_imported_unit)
5736         {
5737           switch (pdi->tag)
5738             {
5739             case DW_TAG_subprogram:
5740               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5741               break;
5742             case DW_TAG_constant:
5743             case DW_TAG_variable:
5744             case DW_TAG_typedef:
5745             case DW_TAG_union_type:
5746               if (!pdi->is_declaration)
5747                 {
5748                   add_partial_symbol (pdi, cu);
5749                 }
5750               break;
5751             case DW_TAG_class_type:
5752             case DW_TAG_interface_type:
5753             case DW_TAG_structure_type:
5754               if (!pdi->is_declaration)
5755                 {
5756                   add_partial_symbol (pdi, cu);
5757                 }
5758               break;
5759             case DW_TAG_enumeration_type:
5760               if (!pdi->is_declaration)
5761                 add_partial_enumeration (pdi, cu);
5762               break;
5763             case DW_TAG_base_type:
5764             case DW_TAG_subrange_type:
5765               /* File scope base type definitions are added to the partial
5766                  symbol table.  */
5767               add_partial_symbol (pdi, cu);
5768               break;
5769             case DW_TAG_namespace:
5770               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5771               break;
5772             case DW_TAG_module:
5773               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5774               break;
5775             case DW_TAG_imported_unit:
5776               {
5777                 struct dwarf2_per_cu_data *per_cu;
5778
5779                 /* For now we don't handle imported units in type units.  */
5780                 if (cu->per_cu->is_debug_types)
5781                   {
5782                     error (_("Dwarf Error: DW_TAG_imported_unit is not"
5783                              " supported in type units [in module %s]"),
5784                            cu->objfile->name);
5785                   }
5786
5787                 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5788                                                            pdi->is_dwz,
5789                                                            cu->objfile);
5790
5791                 /* Go read the partial unit, if needed.  */
5792                 if (per_cu->v.psymtab == NULL)
5793                   process_psymtab_comp_unit (per_cu, 1);
5794
5795                 VEC_safe_push (dwarf2_per_cu_ptr,
5796                                cu->per_cu->imported_symtabs, per_cu);
5797               }
5798               break;
5799             default:
5800               break;
5801             }
5802         }
5803
5804       /* If the die has a sibling, skip to the sibling.  */
5805
5806       pdi = pdi->die_sibling;
5807     }
5808 }
5809
5810 /* Functions used to compute the fully scoped name of a partial DIE.
5811
5812    Normally, this is simple.  For C++, the parent DIE's fully scoped
5813    name is concatenated with "::" and the partial DIE's name.  For
5814    Java, the same thing occurs except that "." is used instead of "::".
5815    Enumerators are an exception; they use the scope of their parent
5816    enumeration type, i.e. the name of the enumeration type is not
5817    prepended to the enumerator.
5818
5819    There are two complexities.  One is DW_AT_specification; in this
5820    case "parent" means the parent of the target of the specification,
5821    instead of the direct parent of the DIE.  The other is compilers
5822    which do not emit DW_TAG_namespace; in this case we try to guess
5823    the fully qualified name of structure types from their members'
5824    linkage names.  This must be done using the DIE's children rather
5825    than the children of any DW_AT_specification target.  We only need
5826    to do this for structures at the top level, i.e. if the target of
5827    any DW_AT_specification (if any; otherwise the DIE itself) does not
5828    have a parent.  */
5829
5830 /* Compute the scope prefix associated with PDI's parent, in
5831    compilation unit CU.  The result will be allocated on CU's
5832    comp_unit_obstack, or a copy of the already allocated PDI->NAME
5833    field.  NULL is returned if no prefix is necessary.  */
5834 static const char *
5835 partial_die_parent_scope (struct partial_die_info *pdi,
5836                           struct dwarf2_cu *cu)
5837 {
5838   const char *grandparent_scope;
5839   struct partial_die_info *parent, *real_pdi;
5840
5841   /* We need to look at our parent DIE; if we have a DW_AT_specification,
5842      then this means the parent of the specification DIE.  */
5843
5844   real_pdi = pdi;
5845   while (real_pdi->has_specification)
5846     real_pdi = find_partial_die (real_pdi->spec_offset,
5847                                  real_pdi->spec_is_dwz, cu);
5848
5849   parent = real_pdi->die_parent;
5850   if (parent == NULL)
5851     return NULL;
5852
5853   if (parent->scope_set)
5854     return parent->scope;
5855
5856   fixup_partial_die (parent, cu);
5857
5858   grandparent_scope = partial_die_parent_scope (parent, cu);
5859
5860   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5861      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5862      Work around this problem here.  */
5863   if (cu->language == language_cplus
5864       && parent->tag == DW_TAG_namespace
5865       && strcmp (parent->name, "::") == 0
5866       && grandparent_scope == NULL)
5867     {
5868       parent->scope = NULL;
5869       parent->scope_set = 1;
5870       return NULL;
5871     }
5872
5873   if (pdi->tag == DW_TAG_enumerator)
5874     /* Enumerators should not get the name of the enumeration as a prefix.  */
5875     parent->scope = grandparent_scope;
5876   else if (parent->tag == DW_TAG_namespace
5877       || parent->tag == DW_TAG_module
5878       || parent->tag == DW_TAG_structure_type
5879       || parent->tag == DW_TAG_class_type
5880       || parent->tag == DW_TAG_interface_type
5881       || parent->tag == DW_TAG_union_type
5882       || parent->tag == DW_TAG_enumeration_type)
5883     {
5884       if (grandparent_scope == NULL)
5885         parent->scope = parent->name;
5886       else
5887         parent->scope = typename_concat (&cu->comp_unit_obstack,
5888                                          grandparent_scope,
5889                                          parent->name, 0, cu);
5890     }
5891   else
5892     {
5893       /* FIXME drow/2004-04-01: What should we be doing with
5894          function-local names?  For partial symbols, we should probably be
5895          ignoring them.  */
5896       complaint (&symfile_complaints,
5897                  _("unhandled containing DIE tag %d for DIE at %d"),
5898                  parent->tag, pdi->offset.sect_off);
5899       parent->scope = grandparent_scope;
5900     }
5901
5902   parent->scope_set = 1;
5903   return parent->scope;
5904 }
5905
5906 /* Return the fully scoped name associated with PDI, from compilation unit
5907    CU.  The result will be allocated with malloc.  */
5908
5909 static char *
5910 partial_die_full_name (struct partial_die_info *pdi,
5911                        struct dwarf2_cu *cu)
5912 {
5913   const char *parent_scope;
5914
5915   /* If this is a template instantiation, we can not work out the
5916      template arguments from partial DIEs.  So, unfortunately, we have
5917      to go through the full DIEs.  At least any work we do building
5918      types here will be reused if full symbols are loaded later.  */
5919   if (pdi->has_template_arguments)
5920     {
5921       fixup_partial_die (pdi, cu);
5922
5923       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5924         {
5925           struct die_info *die;
5926           struct attribute attr;
5927           struct dwarf2_cu *ref_cu = cu;
5928
5929           /* DW_FORM_ref_addr is using section offset.  */
5930           attr.name = 0;
5931           attr.form = DW_FORM_ref_addr;
5932           attr.u.unsnd = pdi->offset.sect_off;
5933           die = follow_die_ref (NULL, &attr, &ref_cu);
5934
5935           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5936         }
5937     }
5938
5939   parent_scope = partial_die_parent_scope (pdi, cu);
5940   if (parent_scope == NULL)
5941     return NULL;
5942   else
5943     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5944 }
5945
5946 static void
5947 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5948 {
5949   struct objfile *objfile = cu->objfile;
5950   CORE_ADDR addr = 0;
5951   const char *actual_name = NULL;
5952   CORE_ADDR baseaddr;
5953   char *built_actual_name;
5954
5955   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5956
5957   built_actual_name = partial_die_full_name (pdi, cu);
5958   if (built_actual_name != NULL)
5959     actual_name = built_actual_name;
5960
5961   if (actual_name == NULL)
5962     actual_name = pdi->name;
5963
5964   switch (pdi->tag)
5965     {
5966     case DW_TAG_subprogram:
5967       if (pdi->is_external || cu->language == language_ada)
5968         {
5969           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5970              of the global scope.  But in Ada, we want to be able to access
5971              nested procedures globally.  So all Ada subprograms are stored
5972              in the global scope.  */
5973           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5974              mst_text, objfile); */
5975           add_psymbol_to_list (actual_name, strlen (actual_name),
5976                                built_actual_name != NULL,
5977                                VAR_DOMAIN, LOC_BLOCK,
5978                                &objfile->global_psymbols,
5979                                0, pdi->lowpc + baseaddr,
5980                                cu->language, objfile);
5981         }
5982       else
5983         {
5984           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5985              mst_file_text, objfile); */
5986           add_psymbol_to_list (actual_name, strlen (actual_name),
5987                                built_actual_name != NULL,
5988                                VAR_DOMAIN, LOC_BLOCK,
5989                                &objfile->static_psymbols,
5990                                0, pdi->lowpc + baseaddr,
5991                                cu->language, objfile);
5992         }
5993       break;
5994     case DW_TAG_constant:
5995       {
5996         struct psymbol_allocation_list *list;
5997
5998         if (pdi->is_external)
5999           list = &objfile->global_psymbols;
6000         else
6001           list = &objfile->static_psymbols;
6002         add_psymbol_to_list (actual_name, strlen (actual_name),
6003                              built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6004                              list, 0, 0, cu->language, objfile);
6005       }
6006       break;
6007     case DW_TAG_variable:
6008       if (pdi->d.locdesc)
6009         addr = decode_locdesc (pdi->d.locdesc, cu);
6010
6011       if (pdi->d.locdesc
6012           && addr == 0
6013           && !dwarf2_per_objfile->has_section_at_zero)
6014         {
6015           /* A global or static variable may also have been stripped
6016              out by the linker if unused, in which case its address
6017              will be nullified; do not add such variables into partial
6018              symbol table then.  */
6019         }
6020       else if (pdi->is_external)
6021         {
6022           /* Global Variable.
6023              Don't enter into the minimal symbol tables as there is
6024              a minimal symbol table entry from the ELF symbols already.
6025              Enter into partial symbol table if it has a location
6026              descriptor or a type.
6027              If the location descriptor is missing, new_symbol will create
6028              a LOC_UNRESOLVED symbol, the address of the variable will then
6029              be determined from the minimal symbol table whenever the variable
6030              is referenced.
6031              The address for the partial symbol table entry is not
6032              used by GDB, but it comes in handy for debugging partial symbol
6033              table building.  */
6034
6035           if (pdi->d.locdesc || pdi->has_type)
6036             add_psymbol_to_list (actual_name, strlen (actual_name),
6037                                  built_actual_name != NULL,
6038                                  VAR_DOMAIN, LOC_STATIC,
6039                                  &objfile->global_psymbols,
6040                                  0, addr + baseaddr,
6041                                  cu->language, objfile);
6042         }
6043       else
6044         {
6045           /* Static Variable.  Skip symbols without location descriptors.  */
6046           if (pdi->d.locdesc == NULL)
6047             {
6048               xfree (built_actual_name);
6049               return;
6050             }
6051           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6052              mst_file_data, objfile); */
6053           add_psymbol_to_list (actual_name, strlen (actual_name),
6054                                built_actual_name != NULL,
6055                                VAR_DOMAIN, LOC_STATIC,
6056                                &objfile->static_psymbols,
6057                                0, addr + baseaddr,
6058                                cu->language, objfile);
6059         }
6060       break;
6061     case DW_TAG_typedef:
6062     case DW_TAG_base_type:
6063     case DW_TAG_subrange_type:
6064       add_psymbol_to_list (actual_name, strlen (actual_name),
6065                            built_actual_name != NULL,
6066                            VAR_DOMAIN, LOC_TYPEDEF,
6067                            &objfile->static_psymbols,
6068                            0, (CORE_ADDR) 0, cu->language, objfile);
6069       break;
6070     case DW_TAG_namespace:
6071       add_psymbol_to_list (actual_name, strlen (actual_name),
6072                            built_actual_name != NULL,
6073                            VAR_DOMAIN, LOC_TYPEDEF,
6074                            &objfile->global_psymbols,
6075                            0, (CORE_ADDR) 0, cu->language, objfile);
6076       break;
6077     case DW_TAG_class_type:
6078     case DW_TAG_interface_type:
6079     case DW_TAG_structure_type:
6080     case DW_TAG_union_type:
6081     case DW_TAG_enumeration_type:
6082       /* Skip external references.  The DWARF standard says in the section
6083          about "Structure, Union, and Class Type Entries": "An incomplete
6084          structure, union or class type is represented by a structure,
6085          union or class entry that does not have a byte size attribute
6086          and that has a DW_AT_declaration attribute."  */
6087       if (!pdi->has_byte_size && pdi->is_declaration)
6088         {
6089           xfree (built_actual_name);
6090           return;
6091         }
6092
6093       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6094          static vs. global.  */
6095       add_psymbol_to_list (actual_name, strlen (actual_name),
6096                            built_actual_name != NULL,
6097                            STRUCT_DOMAIN, LOC_TYPEDEF,
6098                            (cu->language == language_cplus
6099                             || cu->language == language_java)
6100                            ? &objfile->global_psymbols
6101                            : &objfile->static_psymbols,
6102                            0, (CORE_ADDR) 0, cu->language, objfile);
6103
6104       break;
6105     case DW_TAG_enumerator:
6106       add_psymbol_to_list (actual_name, strlen (actual_name),
6107                            built_actual_name != NULL,
6108                            VAR_DOMAIN, LOC_CONST,
6109                            (cu->language == language_cplus
6110                             || cu->language == language_java)
6111                            ? &objfile->global_psymbols
6112                            : &objfile->static_psymbols,
6113                            0, (CORE_ADDR) 0, cu->language, objfile);
6114       break;
6115     default:
6116       break;
6117     }
6118
6119   xfree (built_actual_name);
6120 }
6121
6122 /* Read a partial die corresponding to a namespace; also, add a symbol
6123    corresponding to that namespace to the symbol table.  NAMESPACE is
6124    the name of the enclosing namespace.  */
6125
6126 static void
6127 add_partial_namespace (struct partial_die_info *pdi,
6128                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
6129                        int need_pc, struct dwarf2_cu *cu)
6130 {
6131   /* Add a symbol for the namespace.  */
6132
6133   add_partial_symbol (pdi, cu);
6134
6135   /* Now scan partial symbols in that namespace.  */
6136
6137   if (pdi->has_children)
6138     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6139 }
6140
6141 /* Read a partial die corresponding to a Fortran module.  */
6142
6143 static void
6144 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6145                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6146 {
6147   /* Now scan partial symbols in that module.  */
6148
6149   if (pdi->has_children)
6150     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6151 }
6152
6153 /* Read a partial die corresponding to a subprogram and create a partial
6154    symbol for that subprogram.  When the CU language allows it, this
6155    routine also defines a partial symbol for each nested subprogram
6156    that this subprogram contains.
6157
6158    DIE my also be a lexical block, in which case we simply search
6159    recursively for suprograms defined inside that lexical block.
6160    Again, this is only performed when the CU language allows this
6161    type of definitions.  */
6162
6163 static void
6164 add_partial_subprogram (struct partial_die_info *pdi,
6165                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
6166                         int need_pc, struct dwarf2_cu *cu)
6167 {
6168   if (pdi->tag == DW_TAG_subprogram)
6169     {
6170       if (pdi->has_pc_info)
6171         {
6172           if (pdi->lowpc < *lowpc)
6173             *lowpc = pdi->lowpc;
6174           if (pdi->highpc > *highpc)
6175             *highpc = pdi->highpc;
6176           if (need_pc)
6177             {
6178               CORE_ADDR baseaddr;
6179               struct objfile *objfile = cu->objfile;
6180
6181               baseaddr = ANOFFSET (objfile->section_offsets,
6182                                    SECT_OFF_TEXT (objfile));
6183               addrmap_set_empty (objfile->psymtabs_addrmap,
6184                                  pdi->lowpc + baseaddr,
6185                                  pdi->highpc - 1 + baseaddr,
6186                                  cu->per_cu->v.psymtab);
6187             }
6188         }
6189
6190       if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6191         {
6192           if (!pdi->is_declaration)
6193             /* Ignore subprogram DIEs that do not have a name, they are
6194                illegal.  Do not emit a complaint at this point, we will
6195                do so when we convert this psymtab into a symtab.  */
6196             if (pdi->name)
6197               add_partial_symbol (pdi, cu);
6198         }
6199     }
6200
6201   if (! pdi->has_children)
6202     return;
6203
6204   if (cu->language == language_ada)
6205     {
6206       pdi = pdi->die_child;
6207       while (pdi != NULL)
6208         {
6209           fixup_partial_die (pdi, cu);
6210           if (pdi->tag == DW_TAG_subprogram
6211               || pdi->tag == DW_TAG_lexical_block)
6212             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6213           pdi = pdi->die_sibling;
6214         }
6215     }
6216 }
6217
6218 /* Read a partial die corresponding to an enumeration type.  */
6219
6220 static void
6221 add_partial_enumeration (struct partial_die_info *enum_pdi,
6222                          struct dwarf2_cu *cu)
6223 {
6224   struct partial_die_info *pdi;
6225
6226   if (enum_pdi->name != NULL)
6227     add_partial_symbol (enum_pdi, cu);
6228
6229   pdi = enum_pdi->die_child;
6230   while (pdi)
6231     {
6232       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6233         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6234       else
6235         add_partial_symbol (pdi, cu);
6236       pdi = pdi->die_sibling;
6237     }
6238 }
6239
6240 /* Return the initial uleb128 in the die at INFO_PTR.  */
6241
6242 static unsigned int
6243 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6244 {
6245   unsigned int bytes_read;
6246
6247   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6248 }
6249
6250 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6251    Return the corresponding abbrev, or NULL if the number is zero (indicating
6252    an empty DIE).  In either case *BYTES_READ will be set to the length of
6253    the initial number.  */
6254
6255 static struct abbrev_info *
6256 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6257                  struct dwarf2_cu *cu)
6258 {
6259   bfd *abfd = cu->objfile->obfd;
6260   unsigned int abbrev_number;
6261   struct abbrev_info *abbrev;
6262
6263   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6264
6265   if (abbrev_number == 0)
6266     return NULL;
6267
6268   abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6269   if (!abbrev)
6270     {
6271       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6272              abbrev_number, bfd_get_filename (abfd));
6273     }
6274
6275   return abbrev;
6276 }
6277
6278 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6279    Returns a pointer to the end of a series of DIEs, terminated by an empty
6280    DIE.  Any children of the skipped DIEs will also be skipped.  */
6281
6282 static gdb_byte *
6283 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6284 {
6285   struct dwarf2_cu *cu = reader->cu;
6286   struct abbrev_info *abbrev;
6287   unsigned int bytes_read;
6288
6289   while (1)
6290     {
6291       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6292       if (abbrev == NULL)
6293         return info_ptr + bytes_read;
6294       else
6295         info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6296     }
6297 }
6298
6299 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6300    INFO_PTR should point just after the initial uleb128 of a DIE, and the
6301    abbrev corresponding to that skipped uleb128 should be passed in
6302    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
6303    children.  */
6304
6305 static gdb_byte *
6306 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6307               struct abbrev_info *abbrev)
6308 {
6309   unsigned int bytes_read;
6310   struct attribute attr;
6311   bfd *abfd = reader->abfd;
6312   struct dwarf2_cu *cu = reader->cu;
6313   gdb_byte *buffer = reader->buffer;
6314   const gdb_byte *buffer_end = reader->buffer_end;
6315   gdb_byte *start_info_ptr = info_ptr;
6316   unsigned int form, i;
6317
6318   for (i = 0; i < abbrev->num_attrs; i++)
6319     {
6320       /* The only abbrev we care about is DW_AT_sibling.  */
6321       if (abbrev->attrs[i].name == DW_AT_sibling)
6322         {
6323           read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6324           if (attr.form == DW_FORM_ref_addr)
6325             complaint (&symfile_complaints,
6326                        _("ignoring absolute DW_AT_sibling"));
6327           else
6328             return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6329         }
6330
6331       /* If it isn't DW_AT_sibling, skip this attribute.  */
6332       form = abbrev->attrs[i].form;
6333     skip_attribute:
6334       switch (form)
6335         {
6336         case DW_FORM_ref_addr:
6337           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6338              and later it is offset sized.  */
6339           if (cu->header.version == 2)
6340             info_ptr += cu->header.addr_size;
6341           else
6342             info_ptr += cu->header.offset_size;
6343           break;
6344         case DW_FORM_GNU_ref_alt:
6345           info_ptr += cu->header.offset_size;
6346           break;
6347         case DW_FORM_addr:
6348           info_ptr += cu->header.addr_size;
6349           break;
6350         case DW_FORM_data1:
6351         case DW_FORM_ref1:
6352         case DW_FORM_flag:
6353           info_ptr += 1;
6354           break;
6355         case DW_FORM_flag_present:
6356           break;
6357         case DW_FORM_data2:
6358         case DW_FORM_ref2:
6359           info_ptr += 2;
6360           break;
6361         case DW_FORM_data4:
6362         case DW_FORM_ref4:
6363           info_ptr += 4;
6364           break;
6365         case DW_FORM_data8:
6366         case DW_FORM_ref8:
6367         case DW_FORM_ref_sig8:
6368           info_ptr += 8;
6369           break;
6370         case DW_FORM_string:
6371           read_direct_string (abfd, info_ptr, &bytes_read);
6372           info_ptr += bytes_read;
6373           break;
6374         case DW_FORM_sec_offset:
6375         case DW_FORM_strp:
6376         case DW_FORM_GNU_strp_alt:
6377           info_ptr += cu->header.offset_size;
6378           break;
6379         case DW_FORM_exprloc:
6380         case DW_FORM_block:
6381           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6382           info_ptr += bytes_read;
6383           break;
6384         case DW_FORM_block1:
6385           info_ptr += 1 + read_1_byte (abfd, info_ptr);
6386           break;
6387         case DW_FORM_block2:
6388           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6389           break;
6390         case DW_FORM_block4:
6391           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6392           break;
6393         case DW_FORM_sdata:
6394         case DW_FORM_udata:
6395         case DW_FORM_ref_udata:
6396         case DW_FORM_GNU_addr_index:
6397         case DW_FORM_GNU_str_index:
6398           info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6399           break;
6400         case DW_FORM_indirect:
6401           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6402           info_ptr += bytes_read;
6403           /* We need to continue parsing from here, so just go back to
6404              the top.  */
6405           goto skip_attribute;
6406
6407         default:
6408           error (_("Dwarf Error: Cannot handle %s "
6409                    "in DWARF reader [in module %s]"),
6410                  dwarf_form_name (form),
6411                  bfd_get_filename (abfd));
6412         }
6413     }
6414
6415   if (abbrev->has_children)
6416     return skip_children (reader, info_ptr);
6417   else
6418     return info_ptr;
6419 }
6420
6421 /* Locate ORIG_PDI's sibling.
6422    INFO_PTR should point to the start of the next DIE after ORIG_PDI.  */
6423
6424 static gdb_byte *
6425 locate_pdi_sibling (const struct die_reader_specs *reader,
6426                     struct partial_die_info *orig_pdi,
6427                     gdb_byte *info_ptr)
6428 {
6429   /* Do we know the sibling already?  */
6430
6431   if (orig_pdi->sibling)
6432     return orig_pdi->sibling;
6433
6434   /* Are there any children to deal with?  */
6435
6436   if (!orig_pdi->has_children)
6437     return info_ptr;
6438
6439   /* Skip the children the long way.  */
6440
6441   return skip_children (reader, info_ptr);
6442 }
6443
6444 /* Expand this partial symbol table into a full symbol table.  SELF is
6445    not NULL.  */
6446
6447 static void
6448 dwarf2_read_symtab (struct partial_symtab *self,
6449                     struct objfile *objfile)
6450 {
6451   if (self->readin)
6452     {
6453       warning (_("bug: psymtab for %s is already read in."),
6454                self->filename);
6455     }
6456   else
6457     {
6458       if (info_verbose)
6459         {
6460           printf_filtered (_("Reading in symbols for %s..."),
6461                            self->filename);
6462           gdb_flush (gdb_stdout);
6463         }
6464
6465       /* Restore our global data.  */
6466       dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6467
6468       /* If this psymtab is constructed from a debug-only objfile, the
6469          has_section_at_zero flag will not necessarily be correct.  We
6470          can get the correct value for this flag by looking at the data
6471          associated with the (presumably stripped) associated objfile.  */
6472       if (objfile->separate_debug_objfile_backlink)
6473         {
6474           struct dwarf2_per_objfile *dpo_backlink
6475             = objfile_data (objfile->separate_debug_objfile_backlink,
6476                             dwarf2_objfile_data_key);
6477
6478           dwarf2_per_objfile->has_section_at_zero
6479             = dpo_backlink->has_section_at_zero;
6480         }
6481
6482       dwarf2_per_objfile->reading_partial_symbols = 0;
6483
6484       psymtab_to_symtab_1 (self);
6485
6486       /* Finish up the debug error message.  */
6487       if (info_verbose)
6488         printf_filtered (_("done.\n"));
6489     }
6490
6491   process_cu_includes ();
6492 }
6493 \f
6494 /* Reading in full CUs.  */
6495
6496 /* Add PER_CU to the queue.  */
6497
6498 static void
6499 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6500                  enum language pretend_language)
6501 {
6502   struct dwarf2_queue_item *item;
6503
6504   per_cu->queued = 1;
6505   item = xmalloc (sizeof (*item));
6506   item->per_cu = per_cu;
6507   item->pretend_language = pretend_language;
6508   item->next = NULL;
6509
6510   if (dwarf2_queue == NULL)
6511     dwarf2_queue = item;
6512   else
6513     dwarf2_queue_tail->next = item;
6514
6515   dwarf2_queue_tail = item;
6516 }
6517
6518 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
6519    unit and add it to our queue.
6520    The result is non-zero if PER_CU was queued, otherwise the result is zero
6521    meaning either PER_CU is already queued or it is already loaded.  */
6522
6523 static int
6524 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6525                        struct dwarf2_per_cu_data *per_cu,
6526                        enum language pretend_language)
6527 {
6528   /* We may arrive here during partial symbol reading, if we need full
6529      DIEs to process an unusual case (e.g. template arguments).  Do
6530      not queue PER_CU, just tell our caller to load its DIEs.  */
6531   if (dwarf2_per_objfile->reading_partial_symbols)
6532     {
6533       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6534         return 1;
6535       return 0;
6536     }
6537
6538   /* Mark the dependence relation so that we don't flush PER_CU
6539      too early.  */
6540   dwarf2_add_dependence (this_cu, per_cu);
6541
6542   /* If it's already on the queue, we have nothing to do.  */
6543   if (per_cu->queued)
6544     return 0;
6545
6546   /* If the compilation unit is already loaded, just mark it as
6547      used.  */
6548   if (per_cu->cu != NULL)
6549     {
6550       per_cu->cu->last_used = 0;
6551       return 0;
6552     }
6553
6554   /* Add it to the queue.  */
6555   queue_comp_unit (per_cu, pretend_language);
6556
6557   return 1;
6558 }
6559
6560 /* Process the queue.  */
6561
6562 static void
6563 process_queue (void)
6564 {
6565   struct dwarf2_queue_item *item, *next_item;
6566
6567   if (dwarf2_read_debug)
6568     {
6569       fprintf_unfiltered (gdb_stdlog,
6570                           "Expanding one or more symtabs of objfile %s ...\n",
6571                           dwarf2_per_objfile->objfile->name);
6572     }
6573
6574   /* The queue starts out with one item, but following a DIE reference
6575      may load a new CU, adding it to the end of the queue.  */
6576   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6577     {
6578       if (dwarf2_per_objfile->using_index
6579           ? !item->per_cu->v.quick->symtab
6580           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6581         {
6582           struct dwarf2_per_cu_data *per_cu = item->per_cu;
6583
6584           if (dwarf2_read_debug)
6585             {
6586               fprintf_unfiltered (gdb_stdlog,
6587                                   "Expanding symtab of %s at offset 0x%x\n",
6588                                   per_cu->is_debug_types ? "TU" : "CU",
6589                                   per_cu->offset.sect_off);
6590             }
6591
6592           if (per_cu->is_debug_types)
6593             process_full_type_unit (per_cu, item->pretend_language);
6594           else
6595             process_full_comp_unit (per_cu, item->pretend_language);
6596
6597           if (dwarf2_read_debug)
6598             {
6599               fprintf_unfiltered (gdb_stdlog,
6600                                   "Done expanding %s at offset 0x%x\n",
6601                                   per_cu->is_debug_types ? "TU" : "CU",
6602                                   per_cu->offset.sect_off);
6603             }
6604         }
6605
6606       item->per_cu->queued = 0;
6607       next_item = item->next;
6608       xfree (item);
6609     }
6610
6611   dwarf2_queue_tail = NULL;
6612
6613   if (dwarf2_read_debug)
6614     {
6615       fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6616                           dwarf2_per_objfile->objfile->name);
6617     }
6618 }
6619
6620 /* Free all allocated queue entries.  This function only releases anything if
6621    an error was thrown; if the queue was processed then it would have been
6622    freed as we went along.  */
6623
6624 static void
6625 dwarf2_release_queue (void *dummy)
6626 {
6627   struct dwarf2_queue_item *item, *last;
6628
6629   item = dwarf2_queue;
6630   while (item)
6631     {
6632       /* Anything still marked queued is likely to be in an
6633          inconsistent state, so discard it.  */
6634       if (item->per_cu->queued)
6635         {
6636           if (item->per_cu->cu != NULL)
6637             free_one_cached_comp_unit (item->per_cu);
6638           item->per_cu->queued = 0;
6639         }
6640
6641       last = item;
6642       item = item->next;
6643       xfree (last);
6644     }
6645
6646   dwarf2_queue = dwarf2_queue_tail = NULL;
6647 }
6648
6649 /* Read in full symbols for PST, and anything it depends on.  */
6650
6651 static void
6652 psymtab_to_symtab_1 (struct partial_symtab *pst)
6653 {
6654   struct dwarf2_per_cu_data *per_cu;
6655   int i;
6656
6657   if (pst->readin)
6658     return;
6659
6660   for (i = 0; i < pst->number_of_dependencies; i++)
6661     if (!pst->dependencies[i]->readin
6662         && pst->dependencies[i]->user == NULL)
6663       {
6664         /* Inform about additional files that need to be read in.  */
6665         if (info_verbose)
6666           {
6667             /* FIXME: i18n: Need to make this a single string.  */
6668             fputs_filtered (" ", gdb_stdout);
6669             wrap_here ("");
6670             fputs_filtered ("and ", gdb_stdout);
6671             wrap_here ("");
6672             printf_filtered ("%s...", pst->dependencies[i]->filename);
6673             wrap_here ("");     /* Flush output.  */
6674             gdb_flush (gdb_stdout);
6675           }
6676         psymtab_to_symtab_1 (pst->dependencies[i]);
6677       }
6678
6679   per_cu = pst->read_symtab_private;
6680
6681   if (per_cu == NULL)
6682     {
6683       /* It's an include file, no symbols to read for it.
6684          Everything is in the parent symtab.  */
6685       pst->readin = 1;
6686       return;
6687     }
6688
6689   dw2_do_instantiate_symtab (per_cu);
6690 }
6691
6692 /* Trivial hash function for die_info: the hash value of a DIE
6693    is its offset in .debug_info for this objfile.  */
6694
6695 static hashval_t
6696 die_hash (const void *item)
6697 {
6698   const struct die_info *die = item;
6699
6700   return die->offset.sect_off;
6701 }
6702
6703 /* Trivial comparison function for die_info structures: two DIEs
6704    are equal if they have the same offset.  */
6705
6706 static int
6707 die_eq (const void *item_lhs, const void *item_rhs)
6708 {
6709   const struct die_info *die_lhs = item_lhs;
6710   const struct die_info *die_rhs = item_rhs;
6711
6712   return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6713 }
6714
6715 /* die_reader_func for load_full_comp_unit.
6716    This is identical to read_signatured_type_reader,
6717    but is kept separate for now.  */
6718
6719 static void
6720 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6721                             gdb_byte *info_ptr,
6722                             struct die_info *comp_unit_die,
6723                             int has_children,
6724                             void *data)
6725 {
6726   struct dwarf2_cu *cu = reader->cu;
6727   enum language *language_ptr = data;
6728
6729   gdb_assert (cu->die_hash == NULL);
6730   cu->die_hash =
6731     htab_create_alloc_ex (cu->header.length / 12,
6732                           die_hash,
6733                           die_eq,
6734                           NULL,
6735                           &cu->comp_unit_obstack,
6736                           hashtab_obstack_allocate,
6737                           dummy_obstack_deallocate);
6738
6739   if (has_children)
6740     comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6741                                                   &info_ptr, comp_unit_die);
6742   cu->dies = comp_unit_die;
6743   /* comp_unit_die is not stored in die_hash, no need.  */
6744
6745   /* We try not to read any attributes in this function, because not
6746      all CUs needed for references have been loaded yet, and symbol
6747      table processing isn't initialized.  But we have to set the CU language,
6748      or we won't be able to build types correctly.
6749      Similarly, if we do not read the producer, we can not apply
6750      producer-specific interpretation.  */
6751   prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6752 }
6753
6754 /* Load the DIEs associated with PER_CU into memory.  */
6755
6756 static void
6757 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6758                      enum language pretend_language)
6759 {
6760   gdb_assert (! this_cu->is_debug_types);
6761
6762   init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6763                            load_full_comp_unit_reader, &pretend_language);
6764 }
6765
6766 /* Add a DIE to the delayed physname list.  */
6767
6768 static void
6769 add_to_method_list (struct type *type, int fnfield_index, int index,
6770                     const char *name, struct die_info *die,
6771                     struct dwarf2_cu *cu)
6772 {
6773   struct delayed_method_info mi;
6774   mi.type = type;
6775   mi.fnfield_index = fnfield_index;
6776   mi.index = index;
6777   mi.name = name;
6778   mi.die = die;
6779   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6780 }
6781
6782 /* A cleanup for freeing the delayed method list.  */
6783
6784 static void
6785 free_delayed_list (void *ptr)
6786 {
6787   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6788   if (cu->method_list != NULL)
6789     {
6790       VEC_free (delayed_method_info, cu->method_list);
6791       cu->method_list = NULL;
6792     }
6793 }
6794
6795 /* Compute the physnames of any methods on the CU's method list.
6796
6797    The computation of method physnames is delayed in order to avoid the
6798    (bad) condition that one of the method's formal parameters is of an as yet
6799    incomplete type.  */
6800
6801 static void
6802 compute_delayed_physnames (struct dwarf2_cu *cu)
6803 {
6804   int i;
6805   struct delayed_method_info *mi;
6806   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6807     {
6808       const char *physname;
6809       struct fn_fieldlist *fn_flp
6810         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6811       physname = dwarf2_physname (mi->name, mi->die, cu);
6812       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6813     }
6814 }
6815
6816 /* Go objects should be embedded in a DW_TAG_module DIE,
6817    and it's not clear if/how imported objects will appear.
6818    To keep Go support simple until that's worked out,
6819    go back through what we've read and create something usable.
6820    We could do this while processing each DIE, and feels kinda cleaner,
6821    but that way is more invasive.
6822    This is to, for example, allow the user to type "p var" or "b main"
6823    without having to specify the package name, and allow lookups
6824    of module.object to work in contexts that use the expression
6825    parser.  */
6826
6827 static void
6828 fixup_go_packaging (struct dwarf2_cu *cu)
6829 {
6830   char *package_name = NULL;
6831   struct pending *list;
6832   int i;
6833
6834   for (list = global_symbols; list != NULL; list = list->next)
6835     {
6836       for (i = 0; i < list->nsyms; ++i)
6837         {
6838           struct symbol *sym = list->symbol[i];
6839
6840           if (SYMBOL_LANGUAGE (sym) == language_go
6841               && SYMBOL_CLASS (sym) == LOC_BLOCK)
6842             {
6843               char *this_package_name = go_symbol_package_name (sym);
6844
6845               if (this_package_name == NULL)
6846                 continue;
6847               if (package_name == NULL)
6848                 package_name = this_package_name;
6849               else
6850                 {
6851                   if (strcmp (package_name, this_package_name) != 0)
6852                     complaint (&symfile_complaints,
6853                                _("Symtab %s has objects from two different Go packages: %s and %s"),
6854                                (SYMBOL_SYMTAB (sym)
6855                           ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
6856                                 : cu->objfile->name),
6857                                this_package_name, package_name);
6858                   xfree (this_package_name);
6859                 }
6860             }
6861         }
6862     }
6863
6864   if (package_name != NULL)
6865     {
6866       struct objfile *objfile = cu->objfile;
6867       const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6868                                                       package_name,
6869                                                       strlen (package_name));
6870       struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6871                                      saved_package_name, objfile);
6872       struct symbol *sym;
6873
6874       TYPE_TAG_NAME (type) = TYPE_NAME (type);
6875
6876       sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6877       SYMBOL_SET_LANGUAGE (sym, language_go);
6878       SYMBOL_SET_NAMES (sym, saved_package_name,
6879                         strlen (saved_package_name), 0, objfile);
6880       /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6881          e.g., "main" finds the "main" module and not C's main().  */
6882       SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6883       SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
6884       SYMBOL_TYPE (sym) = type;
6885
6886       add_symbol_to_list (sym, &global_symbols);
6887
6888       xfree (package_name);
6889     }
6890 }
6891
6892 /* Return the symtab for PER_CU.  This works properly regardless of
6893    whether we're using the index or psymtabs.  */
6894
6895 static struct symtab *
6896 get_symtab (struct dwarf2_per_cu_data *per_cu)
6897 {
6898   return (dwarf2_per_objfile->using_index
6899           ? per_cu->v.quick->symtab
6900           : per_cu->v.psymtab->symtab);
6901 }
6902
6903 /* A helper function for computing the list of all symbol tables
6904    included by PER_CU.  */
6905
6906 static void
6907 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6908                                 htab_t all_children,
6909                                 struct dwarf2_per_cu_data *per_cu)
6910 {
6911   void **slot;
6912   int ix;
6913   struct dwarf2_per_cu_data *iter;
6914
6915   slot = htab_find_slot (all_children, per_cu, INSERT);
6916   if (*slot != NULL)
6917     {
6918       /* This inclusion and its children have been processed.  */
6919       return;
6920     }
6921
6922   *slot = per_cu;
6923   /* Only add a CU if it has a symbol table.  */
6924   if (get_symtab (per_cu) != NULL)
6925     VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6926
6927   for (ix = 0;
6928        VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
6929        ++ix)
6930     recursively_compute_inclusions (result, all_children, iter);
6931 }
6932
6933 /* Compute the symtab 'includes' fields for the symtab related to
6934    PER_CU.  */
6935
6936 static void
6937 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6938 {
6939   gdb_assert (! per_cu->is_debug_types);
6940
6941   if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
6942     {
6943       int ix, len;
6944       struct dwarf2_per_cu_data *iter;
6945       VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6946       htab_t all_children;
6947       struct symtab *symtab = get_symtab (per_cu);
6948
6949       /* If we don't have a symtab, we can just skip this case.  */
6950       if (symtab == NULL)
6951         return;
6952
6953       all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6954                                         NULL, xcalloc, xfree);
6955
6956       for (ix = 0;
6957            VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
6958                         ix, iter);
6959            ++ix)
6960         recursively_compute_inclusions (&result_children, all_children, iter);
6961
6962       /* Now we have a transitive closure of all the included CUs, and
6963          for .gdb_index version 7 the included TUs, so we can convert it
6964          to a list of symtabs.  */
6965       len = VEC_length (dwarf2_per_cu_ptr, result_children);
6966       symtab->includes
6967         = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6968                          (len + 1) * sizeof (struct symtab *));
6969       for (ix = 0;
6970            VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6971            ++ix)
6972         symtab->includes[ix] = get_symtab (iter);
6973       symtab->includes[len] = NULL;
6974
6975       VEC_free (dwarf2_per_cu_ptr, result_children);
6976       htab_delete (all_children);
6977     }
6978 }
6979
6980 /* Compute the 'includes' field for the symtabs of all the CUs we just
6981    read.  */
6982
6983 static void
6984 process_cu_includes (void)
6985 {
6986   int ix;
6987   struct dwarf2_per_cu_data *iter;
6988
6989   for (ix = 0;
6990        VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6991                     ix, iter);
6992        ++ix)
6993     {
6994       if (! iter->is_debug_types)
6995         compute_symtab_includes (iter);
6996     }
6997
6998   VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6999 }
7000
7001 /* Generate full symbol information for PER_CU, whose DIEs have
7002    already been loaded into memory.  */
7003
7004 static void
7005 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7006                         enum language pretend_language)
7007 {
7008   struct dwarf2_cu *cu = per_cu->cu;
7009   struct objfile *objfile = per_cu->objfile;
7010   CORE_ADDR lowpc, highpc;
7011   struct symtab *symtab;
7012   struct cleanup *back_to, *delayed_list_cleanup;
7013   CORE_ADDR baseaddr;
7014   struct block *static_block;
7015
7016   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7017
7018   buildsym_init ();
7019   back_to = make_cleanup (really_free_pendings, NULL);
7020   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7021
7022   cu->list_in_scope = &file_symbols;
7023
7024   cu->language = pretend_language;
7025   cu->language_defn = language_def (cu->language);
7026
7027   /* Do line number decoding in read_file_scope () */
7028   process_die (cu->dies, cu);
7029
7030   /* For now fudge the Go package.  */
7031   if (cu->language == language_go)
7032     fixup_go_packaging (cu);
7033
7034   /* Now that we have processed all the DIEs in the CU, all the types 
7035      should be complete, and it should now be safe to compute all of the
7036      physnames.  */
7037   compute_delayed_physnames (cu);
7038   do_cleanups (delayed_list_cleanup);
7039
7040   /* Some compilers don't define a DW_AT_high_pc attribute for the
7041      compilation unit.  If the DW_AT_high_pc is missing, synthesize
7042      it, by scanning the DIE's below the compilation unit.  */
7043   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7044
7045   static_block
7046     = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
7047                                    per_cu->imported_symtabs != NULL);
7048
7049   /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7050      Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7051      (such as virtual method tables).  Record the ranges in STATIC_BLOCK's
7052      addrmap to help ensure it has an accurate map of pc values belonging to
7053      this comp unit.  */
7054   dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7055
7056   symtab = end_symtab_from_static_block (static_block, objfile,
7057                                          SECT_OFF_TEXT (objfile), 0);
7058
7059   if (symtab != NULL)
7060     {
7061       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7062
7063       /* Set symtab language to language from DW_AT_language.  If the
7064          compilation is from a C file generated by language preprocessors, do
7065          not set the language if it was already deduced by start_subfile.  */
7066       if (!(cu->language == language_c && symtab->language != language_c))
7067         symtab->language = cu->language;
7068
7069       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
7070          produce DW_AT_location with location lists but it can be possibly
7071          invalid without -fvar-tracking.  Still up to GCC-4.4.x incl. 4.4.0
7072          there were bugs in prologue debug info, fixed later in GCC-4.5
7073          by "unwind info for epilogues" patch (which is not directly related).
7074
7075          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7076          needed, it would be wrong due to missing DW_AT_producer there.
7077
7078          Still one can confuse GDB by using non-standard GCC compilation
7079          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7080          */ 
7081       if (cu->has_loclist && gcc_4_minor >= 5)
7082         symtab->locations_valid = 1;
7083
7084       if (gcc_4_minor >= 5)
7085         symtab->epilogue_unwind_valid = 1;
7086
7087       symtab->call_site_htab = cu->call_site_htab;
7088     }
7089
7090   if (dwarf2_per_objfile->using_index)
7091     per_cu->v.quick->symtab = symtab;
7092   else
7093     {
7094       struct partial_symtab *pst = per_cu->v.psymtab;
7095       pst->symtab = symtab;
7096       pst->readin = 1;
7097     }
7098
7099   /* Push it for inclusion processing later.  */
7100   VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7101
7102   do_cleanups (back_to);
7103 }
7104
7105 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7106    already been loaded into memory.  */
7107
7108 static void
7109 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7110                         enum language pretend_language)
7111 {
7112   struct dwarf2_cu *cu = per_cu->cu;
7113   struct objfile *objfile = per_cu->objfile;
7114   struct symtab *symtab;
7115   struct cleanup *back_to, *delayed_list_cleanup;
7116
7117   buildsym_init ();
7118   back_to = make_cleanup (really_free_pendings, NULL);
7119   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7120
7121   cu->list_in_scope = &file_symbols;
7122
7123   cu->language = pretend_language;
7124   cu->language_defn = language_def (cu->language);
7125
7126   /* The symbol tables are set up in read_type_unit_scope.  */
7127   process_die (cu->dies, cu);
7128
7129   /* For now fudge the Go package.  */
7130   if (cu->language == language_go)
7131     fixup_go_packaging (cu);
7132
7133   /* Now that we have processed all the DIEs in the CU, all the types 
7134      should be complete, and it should now be safe to compute all of the
7135      physnames.  */
7136   compute_delayed_physnames (cu);
7137   do_cleanups (delayed_list_cleanup);
7138
7139   /* TUs share symbol tables.
7140      If this is the first TU to use this symtab, complete the construction
7141      of it with end_expandable_symtab.  Otherwise, complete the addition of
7142      this TU's symbols to the existing symtab.  */
7143   if (per_cu->type_unit_group->primary_symtab == NULL)
7144     {
7145       symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7146       per_cu->type_unit_group->primary_symtab = symtab;
7147
7148       if (symtab != NULL)
7149         {
7150           /* Set symtab language to language from DW_AT_language.  If the
7151              compilation is from a C file generated by language preprocessors,
7152              do not set the language if it was already deduced by
7153              start_subfile.  */
7154           if (!(cu->language == language_c && symtab->language != language_c))
7155             symtab->language = cu->language;
7156         }
7157     }
7158   else
7159     {
7160       augment_type_symtab (objfile,
7161                            per_cu->type_unit_group->primary_symtab);
7162       symtab = per_cu->type_unit_group->primary_symtab;
7163     }
7164
7165   if (dwarf2_per_objfile->using_index)
7166     per_cu->v.quick->symtab = symtab;
7167   else
7168     {
7169       struct partial_symtab *pst = per_cu->v.psymtab;
7170       pst->symtab = symtab;
7171       pst->readin = 1;
7172     }
7173
7174   do_cleanups (back_to);
7175 }
7176
7177 /* Process an imported unit DIE.  */
7178
7179 static void
7180 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7181 {
7182   struct attribute *attr;
7183
7184   /* For now we don't handle imported units in type units.  */
7185   if (cu->per_cu->is_debug_types)
7186     {
7187       error (_("Dwarf Error: DW_TAG_imported_unit is not"
7188                " supported in type units [in module %s]"),
7189              cu->objfile->name);
7190     }
7191
7192   attr = dwarf2_attr (die, DW_AT_import, cu);
7193   if (attr != NULL)
7194     {
7195       struct dwarf2_per_cu_data *per_cu;
7196       struct symtab *imported_symtab;
7197       sect_offset offset;
7198       int is_dwz;
7199
7200       offset = dwarf2_get_ref_die_offset (attr);
7201       is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7202       per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7203
7204       /* Queue the unit, if needed.  */
7205       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7206         load_full_comp_unit (per_cu, cu->language);
7207
7208       VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7209                      per_cu);
7210     }
7211 }
7212
7213 /* Process a die and its children.  */
7214
7215 static void
7216 process_die (struct die_info *die, struct dwarf2_cu *cu)
7217 {
7218   switch (die->tag)
7219     {
7220     case DW_TAG_padding:
7221       break;
7222     case DW_TAG_compile_unit:
7223     case DW_TAG_partial_unit:
7224       read_file_scope (die, cu);
7225       break;
7226     case DW_TAG_type_unit:
7227       read_type_unit_scope (die, cu);
7228       break;
7229     case DW_TAG_subprogram:
7230     case DW_TAG_inlined_subroutine:
7231       read_func_scope (die, cu);
7232       break;
7233     case DW_TAG_lexical_block:
7234     case DW_TAG_try_block:
7235     case DW_TAG_catch_block:
7236       read_lexical_block_scope (die, cu);
7237       break;
7238     case DW_TAG_GNU_call_site:
7239       read_call_site_scope (die, cu);
7240       break;
7241     case DW_TAG_class_type:
7242     case DW_TAG_interface_type:
7243     case DW_TAG_structure_type:
7244     case DW_TAG_union_type:
7245       process_structure_scope (die, cu);
7246       break;
7247     case DW_TAG_enumeration_type:
7248       process_enumeration_scope (die, cu);
7249       break;
7250
7251     /* These dies have a type, but processing them does not create
7252        a symbol or recurse to process the children.  Therefore we can
7253        read them on-demand through read_type_die.  */
7254     case DW_TAG_subroutine_type:
7255     case DW_TAG_set_type:
7256     case DW_TAG_array_type:
7257     case DW_TAG_pointer_type:
7258     case DW_TAG_ptr_to_member_type:
7259     case DW_TAG_reference_type:
7260     case DW_TAG_string_type:
7261       break;
7262
7263     case DW_TAG_base_type:
7264     case DW_TAG_subrange_type:
7265     case DW_TAG_typedef:
7266       /* Add a typedef symbol for the type definition, if it has a
7267          DW_AT_name.  */
7268       new_symbol (die, read_type_die (die, cu), cu);
7269       break;
7270     case DW_TAG_common_block:
7271       read_common_block (die, cu);
7272       break;
7273     case DW_TAG_common_inclusion:
7274       break;
7275     case DW_TAG_namespace:
7276       cu->processing_has_namespace_info = 1;
7277       read_namespace (die, cu);
7278       break;
7279     case DW_TAG_module:
7280       cu->processing_has_namespace_info = 1;
7281       read_module (die, cu);
7282       break;
7283     case DW_TAG_imported_declaration:
7284     case DW_TAG_imported_module:
7285       cu->processing_has_namespace_info = 1;
7286       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7287                                  || cu->language != language_fortran))
7288         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7289                    dwarf_tag_name (die->tag));
7290       read_import_statement (die, cu);
7291       break;
7292
7293     case DW_TAG_imported_unit:
7294       process_imported_unit_die (die, cu);
7295       break;
7296
7297     default:
7298       new_symbol (die, NULL, cu);
7299       break;
7300     }
7301 }
7302 \f
7303 /* DWARF name computation.  */
7304
7305 /* A helper function for dwarf2_compute_name which determines whether DIE
7306    needs to have the name of the scope prepended to the name listed in the
7307    die.  */
7308
7309 static int
7310 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7311 {
7312   struct attribute *attr;
7313
7314   switch (die->tag)
7315     {
7316     case DW_TAG_namespace:
7317     case DW_TAG_typedef:
7318     case DW_TAG_class_type:
7319     case DW_TAG_interface_type:
7320     case DW_TAG_structure_type:
7321     case DW_TAG_union_type:
7322     case DW_TAG_enumeration_type:
7323     case DW_TAG_enumerator:
7324     case DW_TAG_subprogram:
7325     case DW_TAG_member:
7326       return 1;
7327
7328     case DW_TAG_variable:
7329     case DW_TAG_constant:
7330       /* We only need to prefix "globally" visible variables.  These include
7331          any variable marked with DW_AT_external or any variable that
7332          lives in a namespace.  [Variables in anonymous namespaces
7333          require prefixing, but they are not DW_AT_external.]  */
7334
7335       if (dwarf2_attr (die, DW_AT_specification, cu))
7336         {
7337           struct dwarf2_cu *spec_cu = cu;
7338
7339           return die_needs_namespace (die_specification (die, &spec_cu),
7340                                       spec_cu);
7341         }
7342
7343       attr = dwarf2_attr (die, DW_AT_external, cu);
7344       if (attr == NULL && die->parent->tag != DW_TAG_namespace
7345           && die->parent->tag != DW_TAG_module)
7346         return 0;
7347       /* A variable in a lexical block of some kind does not need a
7348          namespace, even though in C++ such variables may be external
7349          and have a mangled name.  */
7350       if (die->parent->tag ==  DW_TAG_lexical_block
7351           || die->parent->tag ==  DW_TAG_try_block
7352           || die->parent->tag ==  DW_TAG_catch_block
7353           || die->parent->tag == DW_TAG_subprogram)
7354         return 0;
7355       return 1;
7356
7357     default:
7358       return 0;
7359     }
7360 }
7361
7362 /* Retrieve the last character from a mem_file.  */
7363
7364 static void
7365 do_ui_file_peek_last (void *object, const char *buffer, long length)
7366 {
7367   char *last_char_p = (char *) object;
7368
7369   if (length > 0)
7370     *last_char_p = buffer[length - 1];
7371 }
7372
7373 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
7374    compute the physname for the object, which include a method's:
7375    - formal parameters (C++/Java),
7376    - receiver type (Go),
7377    - return type (Java).
7378
7379    The term "physname" is a bit confusing.
7380    For C++, for example, it is the demangled name.
7381    For Go, for example, it's the mangled name.
7382
7383    For Ada, return the DIE's linkage name rather than the fully qualified
7384    name.  PHYSNAME is ignored..
7385
7386    The result is allocated on the objfile_obstack and canonicalized.  */
7387
7388 static const char *
7389 dwarf2_compute_name (const char *name,
7390                      struct die_info *die, struct dwarf2_cu *cu,
7391                      int physname)
7392 {
7393   struct objfile *objfile = cu->objfile;
7394
7395   if (name == NULL)
7396     name = dwarf2_name (die, cu);
7397
7398   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7399      compute it by typename_concat inside GDB.  */
7400   if (cu->language == language_ada
7401       || (cu->language == language_fortran && physname))
7402     {
7403       /* For Ada unit, we prefer the linkage name over the name, as
7404          the former contains the exported name, which the user expects
7405          to be able to reference.  Ideally, we want the user to be able
7406          to reference this entity using either natural or linkage name,
7407          but we haven't started looking at this enhancement yet.  */
7408       struct attribute *attr;
7409
7410       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7411       if (attr == NULL)
7412         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7413       if (attr && DW_STRING (attr))
7414         return DW_STRING (attr);
7415     }
7416
7417   /* These are the only languages we know how to qualify names in.  */
7418   if (name != NULL
7419       && (cu->language == language_cplus || cu->language == language_java
7420           || cu->language == language_fortran))
7421     {
7422       if (die_needs_namespace (die, cu))
7423         {
7424           long length;
7425           const char *prefix;
7426           struct ui_file *buf;
7427
7428           prefix = determine_prefix (die, cu);
7429           buf = mem_fileopen ();
7430           if (*prefix != '\0')
7431             {
7432               char *prefixed_name = typename_concat (NULL, prefix, name,
7433                                                      physname, cu);
7434
7435               fputs_unfiltered (prefixed_name, buf);
7436               xfree (prefixed_name);
7437             }
7438           else
7439             fputs_unfiltered (name, buf);
7440
7441           /* Template parameters may be specified in the DIE's DW_AT_name, or
7442              as children with DW_TAG_template_type_param or
7443              DW_TAG_value_type_param.  If the latter, add them to the name
7444              here.  If the name already has template parameters, then
7445              skip this step; some versions of GCC emit both, and
7446              it is more efficient to use the pre-computed name.
7447
7448              Something to keep in mind about this process: it is very
7449              unlikely, or in some cases downright impossible, to produce
7450              something that will match the mangled name of a function.
7451              If the definition of the function has the same debug info,
7452              we should be able to match up with it anyway.  But fallbacks
7453              using the minimal symbol, for instance to find a method
7454              implemented in a stripped copy of libstdc++, will not work.
7455              If we do not have debug info for the definition, we will have to
7456              match them up some other way.
7457
7458              When we do name matching there is a related problem with function
7459              templates; two instantiated function templates are allowed to
7460              differ only by their return types, which we do not add here.  */
7461
7462           if (cu->language == language_cplus && strchr (name, '<') == NULL)
7463             {
7464               struct attribute *attr;
7465               struct die_info *child;
7466               int first = 1;
7467
7468               die->building_fullname = 1;
7469
7470               for (child = die->child; child != NULL; child = child->sibling)
7471                 {
7472                   struct type *type;
7473                   LONGEST value;
7474                   gdb_byte *bytes;
7475                   struct dwarf2_locexpr_baton *baton;
7476                   struct value *v;
7477
7478                   if (child->tag != DW_TAG_template_type_param
7479                       && child->tag != DW_TAG_template_value_param)
7480                     continue;
7481
7482                   if (first)
7483                     {
7484                       fputs_unfiltered ("<", buf);
7485                       first = 0;
7486                     }
7487                   else
7488                     fputs_unfiltered (", ", buf);
7489
7490                   attr = dwarf2_attr (child, DW_AT_type, cu);
7491                   if (attr == NULL)
7492                     {
7493                       complaint (&symfile_complaints,
7494                                  _("template parameter missing DW_AT_type"));
7495                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
7496                       continue;
7497                     }
7498                   type = die_type (child, cu);
7499
7500                   if (child->tag == DW_TAG_template_type_param)
7501                     {
7502                       c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7503                       continue;
7504                     }
7505
7506                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
7507                   if (attr == NULL)
7508                     {
7509                       complaint (&symfile_complaints,
7510                                  _("template parameter missing "
7511                                    "DW_AT_const_value"));
7512                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
7513                       continue;
7514                     }
7515
7516                   dwarf2_const_value_attr (attr, type, name,
7517                                            &cu->comp_unit_obstack, cu,
7518                                            &value, &bytes, &baton);
7519
7520                   if (TYPE_NOSIGN (type))
7521                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
7522                        changed, this can use value_print instead.  */
7523                     c_printchar (value, type, buf);
7524                   else
7525                     {
7526                       struct value_print_options opts;
7527
7528                       if (baton != NULL)
7529                         v = dwarf2_evaluate_loc_desc (type, NULL,
7530                                                       baton->data,
7531                                                       baton->size,
7532                                                       baton->per_cu);
7533                       else if (bytes != NULL)
7534                         {
7535                           v = allocate_value (type);
7536                           memcpy (value_contents_writeable (v), bytes,
7537                                   TYPE_LENGTH (type));
7538                         }
7539                       else
7540                         v = value_from_longest (type, value);
7541
7542                       /* Specify decimal so that we do not depend on
7543                          the radix.  */
7544                       get_formatted_print_options (&opts, 'd');
7545                       opts.raw = 1;
7546                       value_print (v, buf, &opts);
7547                       release_value (v);
7548                       value_free (v);
7549                     }
7550                 }
7551
7552               die->building_fullname = 0;
7553
7554               if (!first)
7555                 {
7556                   /* Close the argument list, with a space if necessary
7557                      (nested templates).  */
7558                   char last_char = '\0';
7559                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
7560                   if (last_char == '>')
7561                     fputs_unfiltered (" >", buf);
7562                   else
7563                     fputs_unfiltered (">", buf);
7564                 }
7565             }
7566
7567           /* For Java and C++ methods, append formal parameter type
7568              information, if PHYSNAME.  */
7569
7570           if (physname && die->tag == DW_TAG_subprogram
7571               && (cu->language == language_cplus
7572                   || cu->language == language_java))
7573             {
7574               struct type *type = read_type_die (die, cu);
7575
7576               c_type_print_args (type, buf, 1, cu->language,
7577                                  &type_print_raw_options);
7578
7579               if (cu->language == language_java)
7580                 {
7581                   /* For java, we must append the return type to method
7582                      names.  */
7583                   if (die->tag == DW_TAG_subprogram)
7584                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7585                                      0, 0, &type_print_raw_options);
7586                 }
7587               else if (cu->language == language_cplus)
7588                 {
7589                   /* Assume that an artificial first parameter is
7590                      "this", but do not crash if it is not.  RealView
7591                      marks unnamed (and thus unused) parameters as
7592                      artificial; there is no way to differentiate
7593                      the two cases.  */
7594                   if (TYPE_NFIELDS (type) > 0
7595                       && TYPE_FIELD_ARTIFICIAL (type, 0)
7596                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7597                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7598                                                                         0))))
7599                     fputs_unfiltered (" const", buf);
7600                 }
7601             }
7602
7603           name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7604                                        &length);
7605           ui_file_delete (buf);
7606
7607           if (cu->language == language_cplus)
7608             {
7609               const char *cname
7610                 = dwarf2_canonicalize_name (name, cu,
7611                                             &objfile->objfile_obstack);
7612
7613               if (cname != NULL)
7614                 name = cname;
7615             }
7616         }
7617     }
7618
7619   return name;
7620 }
7621
7622 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7623    If scope qualifiers are appropriate they will be added.  The result
7624    will be allocated on the objfile_obstack, or NULL if the DIE does
7625    not have a name.  NAME may either be from a previous call to
7626    dwarf2_name or NULL.
7627
7628    The output string will be canonicalized (if C++/Java).  */
7629
7630 static const char *
7631 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7632 {
7633   return dwarf2_compute_name (name, die, cu, 0);
7634 }
7635
7636 /* Construct a physname for the given DIE in CU.  NAME may either be
7637    from a previous call to dwarf2_name or NULL.  The result will be
7638    allocated on the objfile_objstack or NULL if the DIE does not have a
7639    name.
7640
7641    The output string will be canonicalized (if C++/Java).  */
7642
7643 static const char *
7644 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7645 {
7646   struct objfile *objfile = cu->objfile;
7647   struct attribute *attr;
7648   const char *retval, *mangled = NULL, *canon = NULL;
7649   struct cleanup *back_to;
7650   int need_copy = 1;
7651
7652   /* In this case dwarf2_compute_name is just a shortcut not building anything
7653      on its own.  */
7654   if (!die_needs_namespace (die, cu))
7655     return dwarf2_compute_name (name, die, cu, 1);
7656
7657   back_to = make_cleanup (null_cleanup, NULL);
7658
7659   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7660   if (!attr)
7661     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7662
7663   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7664      has computed.  */
7665   if (attr && DW_STRING (attr))
7666     {
7667       char *demangled;
7668
7669       mangled = DW_STRING (attr);
7670
7671       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7672          type.  It is easier for GDB users to search for such functions as
7673          `name(params)' than `long name(params)'.  In such case the minimal
7674          symbol names do not match the full symbol names but for template
7675          functions there is never a need to look up their definition from their
7676          declaration so the only disadvantage remains the minimal symbol
7677          variant `long name(params)' does not have the proper inferior type.
7678          */
7679
7680       if (cu->language == language_go)
7681         {
7682           /* This is a lie, but we already lie to the caller new_symbol_full.
7683              new_symbol_full assumes we return the mangled name.
7684              This just undoes that lie until things are cleaned up.  */
7685           demangled = NULL;
7686         }
7687       else
7688         {
7689           demangled = cplus_demangle (mangled,
7690                                       (DMGL_PARAMS | DMGL_ANSI
7691                                        | (cu->language == language_java
7692                                           ? DMGL_JAVA | DMGL_RET_POSTFIX
7693                                           : DMGL_RET_DROP)));
7694         }
7695       if (demangled)
7696         {
7697           make_cleanup (xfree, demangled);
7698           canon = demangled;
7699         }
7700       else
7701         {
7702           canon = mangled;
7703           need_copy = 0;
7704         }
7705     }
7706
7707   if (canon == NULL || check_physname)
7708     {
7709       const char *physname = dwarf2_compute_name (name, die, cu, 1);
7710
7711       if (canon != NULL && strcmp (physname, canon) != 0)
7712         {
7713           /* It may not mean a bug in GDB.  The compiler could also
7714              compute DW_AT_linkage_name incorrectly.  But in such case
7715              GDB would need to be bug-to-bug compatible.  */
7716
7717           complaint (&symfile_complaints,
7718                      _("Computed physname <%s> does not match demangled <%s> "
7719                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7720                      physname, canon, mangled, die->offset.sect_off, objfile->name);
7721
7722           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7723              is available here - over computed PHYSNAME.  It is safer
7724              against both buggy GDB and buggy compilers.  */
7725
7726           retval = canon;
7727         }
7728       else
7729         {
7730           retval = physname;
7731           need_copy = 0;
7732         }
7733     }
7734   else
7735     retval = canon;
7736
7737   if (need_copy)
7738     retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
7739
7740   do_cleanups (back_to);
7741   return retval;
7742 }
7743
7744 /* Read the import statement specified by the given die and record it.  */
7745
7746 static void
7747 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7748 {
7749   struct objfile *objfile = cu->objfile;
7750   struct attribute *import_attr;
7751   struct die_info *imported_die, *child_die;
7752   struct dwarf2_cu *imported_cu;
7753   const char *imported_name;
7754   const char *imported_name_prefix;
7755   const char *canonical_name;
7756   const char *import_alias;
7757   const char *imported_declaration = NULL;
7758   const char *import_prefix;
7759   VEC (const_char_ptr) *excludes = NULL;
7760   struct cleanup *cleanups;
7761
7762   import_attr = dwarf2_attr (die, DW_AT_import, cu);
7763   if (import_attr == NULL)
7764     {
7765       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7766                  dwarf_tag_name (die->tag));
7767       return;
7768     }
7769
7770   imported_cu = cu;
7771   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7772   imported_name = dwarf2_name (imported_die, imported_cu);
7773   if (imported_name == NULL)
7774     {
7775       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7776
7777         The import in the following code:
7778         namespace A
7779           {
7780             typedef int B;
7781           }
7782
7783         int main ()
7784           {
7785             using A::B;
7786             B b;
7787             return b;
7788           }
7789
7790         ...
7791          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7792             <52>   DW_AT_decl_file   : 1
7793             <53>   DW_AT_decl_line   : 6
7794             <54>   DW_AT_import      : <0x75>
7795          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7796             <59>   DW_AT_name        : B
7797             <5b>   DW_AT_decl_file   : 1
7798             <5c>   DW_AT_decl_line   : 2
7799             <5d>   DW_AT_type        : <0x6e>
7800         ...
7801          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7802             <76>   DW_AT_byte_size   : 4
7803             <77>   DW_AT_encoding    : 5        (signed)
7804
7805         imports the wrong die ( 0x75 instead of 0x58 ).
7806         This case will be ignored until the gcc bug is fixed.  */
7807       return;
7808     }
7809
7810   /* Figure out the local name after import.  */
7811   import_alias = dwarf2_name (die, cu);
7812
7813   /* Figure out where the statement is being imported to.  */
7814   import_prefix = determine_prefix (die, cu);
7815
7816   /* Figure out what the scope of the imported die is and prepend it
7817      to the name of the imported die.  */
7818   imported_name_prefix = determine_prefix (imported_die, imported_cu);
7819
7820   if (imported_die->tag != DW_TAG_namespace
7821       && imported_die->tag != DW_TAG_module)
7822     {
7823       imported_declaration = imported_name;
7824       canonical_name = imported_name_prefix;
7825     }
7826   else if (strlen (imported_name_prefix) > 0)
7827     canonical_name = obconcat (&objfile->objfile_obstack,
7828                                imported_name_prefix, "::", imported_name,
7829                                (char *) NULL);
7830   else
7831     canonical_name = imported_name;
7832
7833   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7834
7835   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7836     for (child_die = die->child; child_die && child_die->tag;
7837          child_die = sibling_die (child_die))
7838       {
7839         /* DWARF-4: A Fortran use statement with a “rename list” may be
7840            represented by an imported module entry with an import attribute
7841            referring to the module and owned entries corresponding to those
7842            entities that are renamed as part of being imported.  */
7843
7844         if (child_die->tag != DW_TAG_imported_declaration)
7845           {
7846             complaint (&symfile_complaints,
7847                        _("child DW_TAG_imported_declaration expected "
7848                          "- DIE at 0x%x [in module %s]"),
7849                        child_die->offset.sect_off, objfile->name);
7850             continue;
7851           }
7852
7853         import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7854         if (import_attr == NULL)
7855           {
7856             complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7857                        dwarf_tag_name (child_die->tag));
7858             continue;
7859           }
7860
7861         imported_cu = cu;
7862         imported_die = follow_die_ref_or_sig (child_die, import_attr,
7863                                               &imported_cu);
7864         imported_name = dwarf2_name (imported_die, imported_cu);
7865         if (imported_name == NULL)
7866           {
7867             complaint (&symfile_complaints,
7868                        _("child DW_TAG_imported_declaration has unknown "
7869                          "imported name - DIE at 0x%x [in module %s]"),
7870                        child_die->offset.sect_off, objfile->name);
7871             continue;
7872           }
7873
7874         VEC_safe_push (const_char_ptr, excludes, imported_name);
7875
7876         process_die (child_die, cu);
7877       }
7878
7879   cp_add_using_directive (import_prefix,
7880                           canonical_name,
7881                           import_alias,
7882                           imported_declaration,
7883                           excludes,
7884                           0,
7885                           &objfile->objfile_obstack);
7886
7887   do_cleanups (cleanups);
7888 }
7889
7890 /* Cleanup function for handle_DW_AT_stmt_list.  */
7891
7892 static void
7893 free_cu_line_header (void *arg)
7894 {
7895   struct dwarf2_cu *cu = arg;
7896
7897   free_line_header (cu->line_header);
7898   cu->line_header = NULL;
7899 }
7900
7901 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7902    directory paths.  GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7903    this, it was first present in GCC release 4.3.0.  */
7904
7905 static int
7906 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7907 {
7908   if (!cu->checked_producer)
7909     check_producer (cu);
7910
7911   return cu->producer_is_gcc_lt_4_3;
7912 }
7913
7914 static void
7915 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7916                          const char **name, const char **comp_dir)
7917 {
7918   struct attribute *attr;
7919
7920   *name = NULL;
7921   *comp_dir = NULL;
7922
7923   /* Find the filename.  Do not use dwarf2_name here, since the filename
7924      is not a source language identifier.  */
7925   attr = dwarf2_attr (die, DW_AT_name, cu);
7926   if (attr)
7927     {
7928       *name = DW_STRING (attr);
7929     }
7930
7931   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7932   if (attr)
7933     *comp_dir = DW_STRING (attr);
7934   else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7935            && IS_ABSOLUTE_PATH (*name))
7936     {
7937       char *d = ldirname (*name);
7938
7939       *comp_dir = d;
7940       if (d != NULL)
7941         make_cleanup (xfree, d);
7942     }
7943   if (*comp_dir != NULL)
7944     {
7945       /* Irix 6.2 native cc prepends <machine>.: to the compilation
7946          directory, get rid of it.  */
7947       char *cp = strchr (*comp_dir, ':');
7948
7949       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7950         *comp_dir = cp + 1;
7951     }
7952
7953   if (*name == NULL)
7954     *name = "<unknown>";
7955 }
7956
7957 /* Handle DW_AT_stmt_list for a compilation unit.
7958    DIE is the DW_TAG_compile_unit die for CU.
7959    COMP_DIR is the compilation directory.
7960    WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed.  */
7961
7962 static void
7963 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7964                         const char *comp_dir)
7965 {
7966   struct attribute *attr;
7967
7968   gdb_assert (! cu->per_cu->is_debug_types);
7969
7970   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7971   if (attr)
7972     {
7973       unsigned int line_offset = DW_UNSND (attr);
7974       struct line_header *line_header
7975         = dwarf_decode_line_header (line_offset, cu);
7976
7977       if (line_header)
7978         {
7979           cu->line_header = line_header;
7980           make_cleanup (free_cu_line_header, cu);
7981           dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7982         }
7983     }
7984 }
7985
7986 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit.  */
7987
7988 static void
7989 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7990 {
7991   struct objfile *objfile = dwarf2_per_objfile->objfile;
7992   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7993   CORE_ADDR lowpc = ((CORE_ADDR) -1);
7994   CORE_ADDR highpc = ((CORE_ADDR) 0);
7995   struct attribute *attr;
7996   const char *name = NULL;
7997   const char *comp_dir = NULL;
7998   struct die_info *child_die;
7999   bfd *abfd = objfile->obfd;
8000   CORE_ADDR baseaddr;
8001
8002   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8003
8004   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
8005
8006   /* If we didn't find a lowpc, set it to highpc to avoid complaints
8007      from finish_block.  */
8008   if (lowpc == ((CORE_ADDR) -1))
8009     lowpc = highpc;
8010   lowpc += baseaddr;
8011   highpc += baseaddr;
8012
8013   find_file_and_directory (die, cu, &name, &comp_dir);
8014
8015   prepare_one_comp_unit (cu, die, cu->language);
8016
8017   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8018      standardised yet.  As a workaround for the language detection we fall
8019      back to the DW_AT_producer string.  */
8020   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8021     cu->language = language_opencl;
8022
8023   /* Similar hack for Go.  */
8024   if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8025     set_cu_language (DW_LANG_Go, cu);
8026
8027   dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8028
8029   /* Decode line number information if present.  We do this before
8030      processing child DIEs, so that the line header table is available
8031      for DW_AT_decl_file.  */
8032   handle_DW_AT_stmt_list (die, cu, comp_dir);
8033
8034   /* Process all dies in compilation unit.  */
8035   if (die->child != NULL)
8036     {
8037       child_die = die->child;
8038       while (child_die && child_die->tag)
8039         {
8040           process_die (child_die, cu);
8041           child_die = sibling_die (child_die);
8042         }
8043     }
8044
8045   /* Decode macro information, if present.  Dwarf 2 macro information
8046      refers to information in the line number info statement program
8047      header, so we can only read it if we've read the header
8048      successfully.  */
8049   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8050   if (attr && cu->line_header)
8051     {
8052       if (dwarf2_attr (die, DW_AT_macro_info, cu))
8053         complaint (&symfile_complaints,
8054                    _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8055
8056       dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8057     }
8058   else
8059     {
8060       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8061       if (attr && cu->line_header)
8062         {
8063           unsigned int macro_offset = DW_UNSND (attr);
8064
8065           dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8066         }
8067     }
8068
8069   do_cleanups (back_to);
8070 }
8071
8072 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8073    Create the set of symtabs used by this TU, or if this TU is sharing
8074    symtabs with another TU and the symtabs have already been created
8075    then restore those symtabs in the line header.
8076    We don't need the pc/line-number mapping for type units.  */
8077
8078 static void
8079 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8080 {
8081   struct objfile *objfile = dwarf2_per_objfile->objfile;
8082   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8083   struct type_unit_group *tu_group;
8084   int first_time;
8085   struct line_header *lh;
8086   struct attribute *attr;
8087   unsigned int i, line_offset;
8088
8089   gdb_assert (per_cu->is_debug_types);
8090
8091   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8092
8093   /* If we're using .gdb_index (includes -readnow) then
8094      per_cu->s.type_unit_group may not have been set up yet.  */
8095   if (per_cu->type_unit_group == NULL)
8096     per_cu->type_unit_group = get_type_unit_group (cu, attr);
8097   tu_group = per_cu->type_unit_group;
8098
8099   /* If we've already processed this stmt_list there's no real need to
8100      do it again, we could fake it and just recreate the part we need
8101      (file name,index -> symtab mapping).  If data shows this optimization
8102      is useful we can do it then.  */
8103   first_time = tu_group->primary_symtab == NULL;
8104
8105   /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8106      debug info.  */
8107   lh = NULL;
8108   if (attr != NULL)
8109     {
8110       line_offset = DW_UNSND (attr);
8111       lh = dwarf_decode_line_header (line_offset, cu);
8112     }
8113   if (lh == NULL)
8114     {
8115       if (first_time)
8116         dwarf2_start_symtab (cu, "", NULL, 0);
8117       else
8118         {
8119           gdb_assert (tu_group->symtabs == NULL);
8120           restart_symtab (0);
8121         }
8122       /* Note: The primary symtab will get allocated at the end.  */
8123       return;
8124     }
8125
8126   cu->line_header = lh;
8127   make_cleanup (free_cu_line_header, cu);
8128
8129   if (first_time)
8130     {
8131       dwarf2_start_symtab (cu, "", NULL, 0);
8132
8133       tu_group->num_symtabs = lh->num_file_names;
8134       tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8135
8136       for (i = 0; i < lh->num_file_names; ++i)
8137         {
8138           char *dir = NULL;
8139           struct file_entry *fe = &lh->file_names[i];
8140
8141           if (fe->dir_index)
8142             dir = lh->include_dirs[fe->dir_index - 1];
8143           dwarf2_start_subfile (fe->name, dir, NULL);
8144
8145           /* Note: We don't have to watch for the main subfile here, type units
8146              don't have DW_AT_name.  */
8147
8148           if (current_subfile->symtab == NULL)
8149             {
8150               /* NOTE: start_subfile will recognize when it's been passed
8151                  a file it has already seen.  So we can't assume there's a
8152                  simple mapping from lh->file_names to subfiles,
8153                  lh->file_names may contain dups.  */
8154               current_subfile->symtab = allocate_symtab (current_subfile->name,
8155                                                          objfile);
8156             }
8157
8158           fe->symtab = current_subfile->symtab;
8159           tu_group->symtabs[i] = fe->symtab;
8160         }
8161     }
8162   else
8163     {
8164       restart_symtab (0);
8165
8166       for (i = 0; i < lh->num_file_names; ++i)
8167         {
8168           struct file_entry *fe = &lh->file_names[i];
8169
8170           fe->symtab = tu_group->symtabs[i];
8171         }
8172     }
8173
8174   /* The main symtab is allocated last.  Type units don't have DW_AT_name
8175      so they don't have a "real" (so to speak) symtab anyway.
8176      There is later code that will assign the main symtab to all symbols
8177      that don't have one.  We need to handle the case of a symbol with a
8178      missing symtab (DW_AT_decl_file) anyway.  */
8179 }
8180
8181 /* Process DW_TAG_type_unit.
8182    For TUs we want to skip the first top level sibling if it's not the
8183    actual type being defined by this TU.  In this case the first top
8184    level sibling is there to provide context only.  */
8185
8186 static void
8187 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8188 {
8189   struct die_info *child_die;
8190
8191   prepare_one_comp_unit (cu, die, language_minimal);
8192
8193   /* Initialize (or reinitialize) the machinery for building symtabs.
8194      We do this before processing child DIEs, so that the line header table
8195      is available for DW_AT_decl_file.  */
8196   setup_type_unit_groups (die, cu);
8197
8198   if (die->child != NULL)
8199     {
8200       child_die = die->child;
8201       while (child_die && child_die->tag)
8202         {
8203           process_die (child_die, cu);
8204           child_die = sibling_die (child_die);
8205         }
8206     }
8207 }
8208 \f
8209 /* DWO/DWP files.
8210
8211    http://gcc.gnu.org/wiki/DebugFission
8212    http://gcc.gnu.org/wiki/DebugFissionDWP
8213
8214    To simplify handling of both DWO files ("object" files with the DWARF info)
8215    and DWP files (a file with the DWOs packaged up into one file), we treat
8216    DWP files as having a collection of virtual DWO files.  */
8217
8218 static hashval_t
8219 hash_dwo_file (const void *item)
8220 {
8221   const struct dwo_file *dwo_file = item;
8222
8223   return htab_hash_string (dwo_file->name);
8224 }
8225
8226 static int
8227 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8228 {
8229   const struct dwo_file *lhs = item_lhs;
8230   const struct dwo_file *rhs = item_rhs;
8231
8232   return strcmp (lhs->name, rhs->name) == 0;
8233 }
8234
8235 /* Allocate a hash table for DWO files.  */
8236
8237 static htab_t
8238 allocate_dwo_file_hash_table (void)
8239 {
8240   struct objfile *objfile = dwarf2_per_objfile->objfile;
8241
8242   return htab_create_alloc_ex (41,
8243                                hash_dwo_file,
8244                                eq_dwo_file,
8245                                NULL,
8246                                &objfile->objfile_obstack,
8247                                hashtab_obstack_allocate,
8248                                dummy_obstack_deallocate);
8249 }
8250
8251 /* Lookup DWO file DWO_NAME.  */
8252
8253 static void **
8254 lookup_dwo_file_slot (const char *dwo_name)
8255 {
8256   struct dwo_file find_entry;
8257   void **slot;
8258
8259   if (dwarf2_per_objfile->dwo_files == NULL)
8260     dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8261
8262   memset (&find_entry, 0, sizeof (find_entry));
8263   find_entry.name = dwo_name;
8264   slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8265
8266   return slot;
8267 }
8268
8269 static hashval_t
8270 hash_dwo_unit (const void *item)
8271 {
8272   const struct dwo_unit *dwo_unit = item;
8273
8274   /* This drops the top 32 bits of the id, but is ok for a hash.  */
8275   return dwo_unit->signature;
8276 }
8277
8278 static int
8279 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8280 {
8281   const struct dwo_unit *lhs = item_lhs;
8282   const struct dwo_unit *rhs = item_rhs;
8283
8284   /* The signature is assumed to be unique within the DWO file.
8285      So while object file CU dwo_id's always have the value zero,
8286      that's OK, assuming each object file DWO file has only one CU,
8287      and that's the rule for now.  */
8288   return lhs->signature == rhs->signature;
8289 }
8290
8291 /* Allocate a hash table for DWO CUs,TUs.
8292    There is one of these tables for each of CUs,TUs for each DWO file.  */
8293
8294 static htab_t
8295 allocate_dwo_unit_table (struct objfile *objfile)
8296 {
8297   /* Start out with a pretty small number.
8298      Generally DWO files contain only one CU and maybe some TUs.  */
8299   return htab_create_alloc_ex (3,
8300                                hash_dwo_unit,
8301                                eq_dwo_unit,
8302                                NULL,
8303                                &objfile->objfile_obstack,
8304                                hashtab_obstack_allocate,
8305                                dummy_obstack_deallocate);
8306 }
8307
8308 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader.  */
8309
8310 struct create_dwo_info_table_data
8311 {
8312   struct dwo_file *dwo_file;
8313   htab_t cu_htab;
8314 };
8315
8316 /* die_reader_func for create_dwo_debug_info_hash_table.  */
8317
8318 static void
8319 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8320                                          gdb_byte *info_ptr,
8321                                          struct die_info *comp_unit_die,
8322                                          int has_children,
8323                                          void *datap)
8324 {
8325   struct dwarf2_cu *cu = reader->cu;
8326   struct objfile *objfile = dwarf2_per_objfile->objfile;
8327   sect_offset offset = cu->per_cu->offset;
8328   struct dwarf2_section_info *section = cu->per_cu->section;
8329   struct create_dwo_info_table_data *data = datap;
8330   struct dwo_file *dwo_file = data->dwo_file;
8331   htab_t cu_htab = data->cu_htab;
8332   void **slot;
8333   struct attribute *attr;
8334   struct dwo_unit *dwo_unit;
8335
8336   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8337   if (attr == NULL)
8338     {
8339       error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8340                " its dwo_id [in module %s]"),
8341              offset.sect_off, dwo_file->name);
8342       return;
8343     }
8344
8345   dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8346   dwo_unit->dwo_file = dwo_file;
8347   dwo_unit->signature = DW_UNSND (attr);
8348   dwo_unit->section = section;
8349   dwo_unit->offset = offset;
8350   dwo_unit->length = cu->per_cu->length;
8351
8352   slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8353   gdb_assert (slot != NULL);
8354   if (*slot != NULL)
8355     {
8356       const struct dwo_unit *dup_dwo_unit = *slot;
8357
8358       complaint (&symfile_complaints,
8359                  _("debug entry at offset 0x%x is duplicate to the entry at"
8360                    " offset 0x%x, dwo_id 0x%s [in module %s]"),
8361                  offset.sect_off, dup_dwo_unit->offset.sect_off,
8362                  phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8363                  dwo_file->name);
8364     }
8365   else
8366     *slot = dwo_unit;
8367
8368   if (dwarf2_read_debug)
8369     fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, dwo_id 0x%s\n",
8370                         offset.sect_off,
8371                         phex (dwo_unit->signature,
8372                               sizeof (dwo_unit->signature)));
8373 }
8374
8375 /* Create a hash table to map DWO IDs to their CU entry in
8376    .debug_info.dwo in DWO_FILE.
8377    Note: This function processes DWO files only, not DWP files.  */
8378
8379 static htab_t
8380 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8381 {
8382   struct objfile *objfile = dwarf2_per_objfile->objfile;
8383   struct dwarf2_section_info *section = &dwo_file->sections.info;
8384   bfd *abfd;
8385   htab_t cu_htab;
8386   gdb_byte *info_ptr, *end_ptr;
8387   struct create_dwo_info_table_data create_dwo_info_table_data;
8388
8389   dwarf2_read_section (objfile, section);
8390   info_ptr = section->buffer;
8391
8392   if (info_ptr == NULL)
8393     return NULL;
8394
8395   /* We can't set abfd until now because the section may be empty or
8396      not present, in which case section->asection will be NULL.  */
8397   abfd = section->asection->owner;
8398
8399   if (dwarf2_read_debug)
8400     fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8401                         bfd_get_filename (abfd));
8402
8403   cu_htab = allocate_dwo_unit_table (objfile);
8404
8405   create_dwo_info_table_data.dwo_file = dwo_file;
8406   create_dwo_info_table_data.cu_htab = cu_htab;
8407
8408   end_ptr = info_ptr + section->size;
8409   while (info_ptr < end_ptr)
8410     {
8411       struct dwarf2_per_cu_data per_cu;
8412
8413       memset (&per_cu, 0, sizeof (per_cu));
8414       per_cu.objfile = objfile;
8415       per_cu.is_debug_types = 0;
8416       per_cu.offset.sect_off = info_ptr - section->buffer;
8417       per_cu.section = section;
8418
8419       init_cutu_and_read_dies_no_follow (&per_cu,
8420                                          &dwo_file->sections.abbrev,
8421                                          dwo_file,
8422                                          create_dwo_debug_info_hash_table_reader,
8423                                          &create_dwo_info_table_data);
8424
8425       info_ptr += per_cu.length;
8426     }
8427
8428   return cu_htab;
8429 }
8430
8431 /* DWP file .debug_{cu,tu}_index section format:
8432    [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8433
8434    Both index sections have the same format, and serve to map a 64-bit
8435    signature to a set of section numbers.  Each section begins with a header,
8436    followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8437    indexes, and a pool of 32-bit section numbers.  The index sections will be
8438    aligned at 8-byte boundaries in the file.
8439
8440    The index section header contains two unsigned 32-bit values (using the
8441    byte order of the application binary):
8442
8443     N, the number of compilation units or type units in the index
8444     M, the number of slots in the hash table
8445
8446   (We assume that N and M will not exceed 2^32 - 1.)
8447
8448   The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8449
8450   The hash table begins at offset 8 in the section, and consists of an array
8451   of M 64-bit slots.  Each slot contains a 64-bit signature (using the byte
8452   order of the application binary).  Unused slots in the hash table are 0.
8453   (We rely on the extreme unlikeliness of a signature being exactly 0.)
8454
8455   The parallel table begins immediately after the hash table
8456   (at offset 8 + 8 * M from the beginning of the section), and consists of an
8457   array of 32-bit indexes (using the byte order of the application binary),
8458   corresponding 1-1 with slots in the hash table.  Each entry in the parallel
8459   table contains a 32-bit index into the pool of section numbers.  For unused
8460   hash table slots, the corresponding entry in the parallel table will be 0.
8461
8462   Given a 64-bit compilation unit signature or a type signature S, an entry
8463   in the hash table is located as follows:
8464
8465   1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8466      the low-order k bits all set to 1.
8467
8468   2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8469
8470   3) If the hash table entry at index H matches the signature, use that
8471      entry.  If the hash table entry at index H is unused (all zeroes),
8472      terminate the search: the signature is not present in the table.
8473
8474   4) Let H = (H + H') modulo M. Repeat at Step 3.
8475
8476   Because M > N and H' and M are relatively prime, the search is guaranteed
8477   to stop at an unused slot or find the match.
8478
8479   The pool of section numbers begins immediately following the hash table
8480   (at offset 8 + 12 * M from the beginning of the section).  The pool of
8481   section numbers consists of an array of 32-bit words (using the byte order
8482   of the application binary).  Each item in the array is indexed starting
8483   from 0.  The hash table entry provides the index of the first section
8484   number in the set.  Additional section numbers in the set follow, and the
8485   set is terminated by a 0 entry (section number 0 is not used in ELF).
8486
8487   In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8488   section must be the first entry in the set, and the .debug_abbrev.dwo must
8489   be the second entry. Other members of the set may follow in any order.  */
8490
8491 /* Create a hash table to map DWO IDs to their CU/TU entry in
8492    .debug_{info,types}.dwo in DWP_FILE.
8493    Returns NULL if there isn't one.
8494    Note: This function processes DWP files only, not DWO files.  */
8495
8496 static struct dwp_hash_table *
8497 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8498 {
8499   struct objfile *objfile = dwarf2_per_objfile->objfile;
8500   bfd *dbfd = dwp_file->dbfd;
8501   char *index_ptr, *index_end;
8502   struct dwarf2_section_info *index;
8503   uint32_t version, nr_units, nr_slots;
8504   struct dwp_hash_table *htab;
8505
8506   if (is_debug_types)
8507     index = &dwp_file->sections.tu_index;
8508   else
8509     index = &dwp_file->sections.cu_index;
8510
8511   if (dwarf2_section_empty_p (index))
8512     return NULL;
8513   dwarf2_read_section (objfile, index);
8514
8515   index_ptr = index->buffer;
8516   index_end = index_ptr + index->size;
8517
8518   version = read_4_bytes (dbfd, index_ptr);
8519   index_ptr += 8; /* Skip the unused word.  */
8520   nr_units = read_4_bytes (dbfd, index_ptr);
8521   index_ptr += 4;
8522   nr_slots = read_4_bytes (dbfd, index_ptr);
8523   index_ptr += 4;
8524
8525   if (version != 1)
8526     {
8527       error (_("Dwarf Error: unsupported DWP file version (%u)"
8528                " [in module %s]"),
8529              version, dwp_file->name);
8530     }
8531   if (nr_slots != (nr_slots & -nr_slots))
8532     {
8533       error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8534                " is not power of 2 [in module %s]"),
8535              nr_slots, dwp_file->name);
8536     }
8537
8538   htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8539   htab->nr_units = nr_units;
8540   htab->nr_slots = nr_slots;
8541   htab->hash_table = index_ptr;
8542   htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8543   htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8544
8545   return htab;
8546 }
8547
8548 /* Update SECTIONS with the data from SECTP.
8549
8550    This function is like the other "locate" section routines that are
8551    passed to bfd_map_over_sections, but in this context the sections to
8552    read comes from the DWP hash table, not the full ELF section table.
8553
8554    The result is non-zero for success, or zero if an error was found.  */
8555
8556 static int
8557 locate_virtual_dwo_sections (asection *sectp,
8558                              struct virtual_dwo_sections *sections)
8559 {
8560   const struct dwop_section_names *names = &dwop_section_names;
8561
8562   if (section_is_p (sectp->name, &names->abbrev_dwo))
8563     {
8564       /* There can be only one.  */
8565       if (sections->abbrev.asection != NULL)
8566         return 0;
8567       sections->abbrev.asection = sectp;
8568       sections->abbrev.size = bfd_get_section_size (sectp);
8569     }
8570   else if (section_is_p (sectp->name, &names->info_dwo)
8571            || section_is_p (sectp->name, &names->types_dwo))
8572     {
8573       /* There can be only one.  */
8574       if (sections->info_or_types.asection != NULL)
8575         return 0;
8576       sections->info_or_types.asection = sectp;
8577       sections->info_or_types.size = bfd_get_section_size (sectp);
8578     }
8579   else if (section_is_p (sectp->name, &names->line_dwo))
8580     {
8581       /* There can be only one.  */
8582       if (sections->line.asection != NULL)
8583         return 0;
8584       sections->line.asection = sectp;
8585       sections->line.size = bfd_get_section_size (sectp);
8586     }
8587   else if (section_is_p (sectp->name, &names->loc_dwo))
8588     {
8589       /* There can be only one.  */
8590       if (sections->loc.asection != NULL)
8591         return 0;
8592       sections->loc.asection = sectp;
8593       sections->loc.size = bfd_get_section_size (sectp);
8594     }
8595   else if (section_is_p (sectp->name, &names->macinfo_dwo))
8596     {
8597       /* There can be only one.  */
8598       if (sections->macinfo.asection != NULL)
8599         return 0;
8600       sections->macinfo.asection = sectp;
8601       sections->macinfo.size = bfd_get_section_size (sectp);
8602     }
8603   else if (section_is_p (sectp->name, &names->macro_dwo))
8604     {
8605       /* There can be only one.  */
8606       if (sections->macro.asection != NULL)
8607         return 0;
8608       sections->macro.asection = sectp;
8609       sections->macro.size = bfd_get_section_size (sectp);
8610     }
8611   else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8612     {
8613       /* There can be only one.  */
8614       if (sections->str_offsets.asection != NULL)
8615         return 0;
8616       sections->str_offsets.asection = sectp;
8617       sections->str_offsets.size = bfd_get_section_size (sectp);
8618     }
8619   else
8620     {
8621       /* No other kind of section is valid.  */
8622       return 0;
8623     }
8624
8625   return 1;
8626 }
8627
8628 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8629    HTAB is the hash table from the DWP file.
8630    SECTION_INDEX is the index of the DWO in HTAB.  */
8631
8632 static struct dwo_unit *
8633 create_dwo_in_dwp (struct dwp_file *dwp_file,
8634                    const struct dwp_hash_table *htab,
8635                    uint32_t section_index,
8636                    ULONGEST signature, int is_debug_types)
8637 {
8638   struct objfile *objfile = dwarf2_per_objfile->objfile;
8639   bfd *dbfd = dwp_file->dbfd;
8640   const char *kind = is_debug_types ? "TU" : "CU";
8641   struct dwo_file *dwo_file;
8642   struct dwo_unit *dwo_unit;
8643   struct virtual_dwo_sections sections;
8644   void **dwo_file_slot;
8645   char *virtual_dwo_name;
8646   struct dwarf2_section_info *cutu;
8647   struct cleanup *cleanups;
8648   int i;
8649
8650   if (dwarf2_read_debug)
8651     {
8652       fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8653                           kind,
8654                           section_index, phex (signature, sizeof (signature)),
8655                           dwp_file->name);
8656     }
8657
8658   /* Fetch the sections of this DWO.
8659      Put a limit on the number of sections we look for so that bad data
8660      doesn't cause us to loop forever.  */
8661
8662 #define MAX_NR_DWO_SECTIONS \
8663   (1 /* .debug_info or .debug_types */ \
8664    + 1 /* .debug_abbrev */ \
8665    + 1 /* .debug_line */ \
8666    + 1 /* .debug_loc */ \
8667    + 1 /* .debug_str_offsets */ \
8668    + 1 /* .debug_macro */ \
8669    + 1 /* .debug_macinfo */ \
8670    + 1 /* trailing zero */)
8671
8672   memset (&sections, 0, sizeof (sections));
8673   cleanups = make_cleanup (null_cleanup, 0);
8674
8675   for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8676     {
8677       asection *sectp;
8678       uint32_t section_nr =
8679         read_4_bytes (dbfd,
8680                       htab->section_pool
8681                       + (section_index + i) * sizeof (uint32_t));
8682
8683       if (section_nr == 0)
8684         break;
8685       if (section_nr >= dwp_file->num_sections)
8686         {
8687           error (_("Dwarf Error: bad DWP hash table, section number too large"
8688                    " [in module %s]"),
8689                  dwp_file->name);
8690         }
8691
8692       sectp = dwp_file->elf_sections[section_nr];
8693       if (! locate_virtual_dwo_sections (sectp, &sections))
8694         {
8695           error (_("Dwarf Error: bad DWP hash table, invalid section found"
8696                    " [in module %s]"),
8697                  dwp_file->name);
8698         }
8699     }
8700
8701   if (i < 2
8702       || sections.info_or_types.asection == NULL
8703       || sections.abbrev.asection == NULL)
8704     {
8705       error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8706                " [in module %s]"),
8707              dwp_file->name);
8708     }
8709   if (i == MAX_NR_DWO_SECTIONS)
8710     {
8711       error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8712                " [in module %s]"),
8713              dwp_file->name);
8714     }
8715
8716   /* It's easier for the rest of the code if we fake a struct dwo_file and
8717      have dwo_unit "live" in that.  At least for now.
8718
8719      The DWP file can be made up of a random collection of CUs and TUs.
8720      However, for each CU + set of TUs that came from the same original DWO
8721      file, we want to combine them back into a virtual DWO file to save space
8722      (fewer struct dwo_file objects to allocated).  Remember that for really
8723      large apps there can be on the order of 8K CUs and 200K TUs, or more.  */
8724
8725   virtual_dwo_name =
8726     xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8727                 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8728                 sections.line.asection ? sections.line.asection->id : 0,
8729                 sections.loc.asection ? sections.loc.asection->id : 0,
8730                 (sections.str_offsets.asection
8731                 ? sections.str_offsets.asection->id
8732                 : 0));
8733   make_cleanup (xfree, virtual_dwo_name);
8734   /* Can we use an existing virtual DWO file?  */
8735   dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8736   /* Create one if necessary.  */
8737   if (*dwo_file_slot == NULL)
8738     {
8739       if (dwarf2_read_debug)
8740         {
8741           fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8742                               virtual_dwo_name);
8743         }
8744       dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8745       dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8746                                       virtual_dwo_name,
8747                                       strlen (virtual_dwo_name));
8748       dwo_file->sections.abbrev = sections.abbrev;
8749       dwo_file->sections.line = sections.line;
8750       dwo_file->sections.loc = sections.loc;
8751       dwo_file->sections.macinfo = sections.macinfo;
8752       dwo_file->sections.macro = sections.macro;
8753       dwo_file->sections.str_offsets = sections.str_offsets;
8754       /* The "str" section is global to the entire DWP file.  */
8755       dwo_file->sections.str = dwp_file->sections.str;
8756       /* The info or types section is assigned later to dwo_unit,
8757          there's no need to record it in dwo_file.
8758          Also, we can't simply record type sections in dwo_file because
8759          we record a pointer into the vector in dwo_unit.  As we collect more
8760          types we'll grow the vector and eventually have to reallocate space
8761          for it, invalidating all the pointers into the current copy.  */
8762       *dwo_file_slot = dwo_file;
8763     }
8764   else
8765     {
8766       if (dwarf2_read_debug)
8767         {
8768           fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8769                               virtual_dwo_name);
8770         }
8771       dwo_file = *dwo_file_slot;
8772     }
8773   do_cleanups (cleanups);
8774
8775   dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8776   dwo_unit->dwo_file = dwo_file;
8777   dwo_unit->signature = signature;
8778   dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8779                                      sizeof (struct dwarf2_section_info));
8780   *dwo_unit->section = sections.info_or_types;
8781   /* offset, length, type_offset_in_tu are set later.  */
8782
8783   return dwo_unit;
8784 }
8785
8786 /* Lookup the DWO with SIGNATURE in DWP_FILE.  */
8787
8788 static struct dwo_unit *
8789 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8790                    const struct dwp_hash_table *htab,
8791                    ULONGEST signature, int is_debug_types)
8792 {
8793   bfd *dbfd = dwp_file->dbfd;
8794   uint32_t mask = htab->nr_slots - 1;
8795   uint32_t hash = signature & mask;
8796   uint32_t hash2 = ((signature >> 32) & mask) | 1;
8797   unsigned int i;
8798   void **slot;
8799   struct dwo_unit find_dwo_cu, *dwo_cu;
8800
8801   memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8802   find_dwo_cu.signature = signature;
8803   slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8804
8805   if (*slot != NULL)
8806     return *slot;
8807
8808   /* Use a for loop so that we don't loop forever on bad debug info.  */
8809   for (i = 0; i < htab->nr_slots; ++i)
8810     {
8811       ULONGEST signature_in_table;
8812
8813       signature_in_table =
8814         read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8815       if (signature_in_table == signature)
8816         {
8817           uint32_t section_index =
8818             read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8819
8820           *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8821                                      signature, is_debug_types);
8822           return *slot;
8823         }
8824       if (signature_in_table == 0)
8825         return NULL;
8826       hash = (hash + hash2) & mask;
8827     }
8828
8829   error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8830            " [in module %s]"),
8831          dwp_file->name);
8832 }
8833
8834 /* Subroutine of open_dwop_file to simplify it.
8835    Open the file specified by FILE_NAME and hand it off to BFD for
8836    preliminary analysis.  Return a newly initialized bfd *, which
8837    includes a canonicalized copy of FILE_NAME.
8838    If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8839    In case of trouble, return NULL.
8840    NOTE: This function is derived from symfile_bfd_open.  */
8841
8842 static bfd *
8843 try_open_dwop_file (const char *file_name, int is_dwp)
8844 {
8845   bfd *sym_bfd;
8846   int desc, flags;
8847   char *absolute_name;
8848
8849   flags = OPF_TRY_CWD_FIRST;
8850   if (is_dwp)
8851     flags |= OPF_SEARCH_IN_PATH;
8852   desc = openp (debug_file_directory, flags, file_name,
8853                 O_RDONLY | O_BINARY, &absolute_name);
8854   if (desc < 0)
8855     return NULL;
8856
8857   sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8858   if (!sym_bfd)
8859     {
8860       xfree (absolute_name);
8861       return NULL;
8862     }
8863   xfree (absolute_name);
8864   bfd_set_cacheable (sym_bfd, 1);
8865
8866   if (!bfd_check_format (sym_bfd, bfd_object))
8867     {
8868       gdb_bfd_unref (sym_bfd); /* This also closes desc.  */
8869       return NULL;
8870     }
8871
8872   return sym_bfd;
8873 }
8874
8875 /* Try to open DWO/DWP file FILE_NAME.
8876    COMP_DIR is the DW_AT_comp_dir attribute.
8877    If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8878    The result is the bfd handle of the file.
8879    If there is a problem finding or opening the file, return NULL.
8880    Upon success, the canonicalized path of the file is stored in the bfd,
8881    same as symfile_bfd_open.  */
8882
8883 static bfd *
8884 open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
8885 {
8886   bfd *abfd;
8887
8888   if (IS_ABSOLUTE_PATH (file_name))
8889     return try_open_dwop_file (file_name, is_dwp);
8890
8891   /* Before trying the search path, try DWO_NAME in COMP_DIR.  */
8892
8893   if (comp_dir != NULL)
8894     {
8895       char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8896
8897       /* NOTE: If comp_dir is a relative path, this will also try the
8898          search path, which seems useful.  */
8899       abfd = try_open_dwop_file (path_to_try, is_dwp);
8900       xfree (path_to_try);
8901       if (abfd != NULL)
8902         return abfd;
8903     }
8904
8905   /* That didn't work, try debug-file-directory, which, despite its name,
8906      is a list of paths.  */
8907
8908   if (*debug_file_directory == '\0')
8909     return NULL;
8910
8911   return try_open_dwop_file (file_name, is_dwp);
8912 }
8913
8914 /* This function is mapped across the sections and remembers the offset and
8915    size of each of the DWO debugging sections we are interested in.  */
8916
8917 static void
8918 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8919 {
8920   struct dwo_sections *dwo_sections = dwo_sections_ptr;
8921   const struct dwop_section_names *names = &dwop_section_names;
8922
8923   if (section_is_p (sectp->name, &names->abbrev_dwo))
8924     {
8925       dwo_sections->abbrev.asection = sectp;
8926       dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8927     }
8928   else if (section_is_p (sectp->name, &names->info_dwo))
8929     {
8930       dwo_sections->info.asection = sectp;
8931       dwo_sections->info.size = bfd_get_section_size (sectp);
8932     }
8933   else if (section_is_p (sectp->name, &names->line_dwo))
8934     {
8935       dwo_sections->line.asection = sectp;
8936       dwo_sections->line.size = bfd_get_section_size (sectp);
8937     }
8938   else if (section_is_p (sectp->name, &names->loc_dwo))
8939     {
8940       dwo_sections->loc.asection = sectp;
8941       dwo_sections->loc.size = bfd_get_section_size (sectp);
8942     }
8943   else if (section_is_p (sectp->name, &names->macinfo_dwo))
8944     {
8945       dwo_sections->macinfo.asection = sectp;
8946       dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8947     }
8948   else if (section_is_p (sectp->name, &names->macro_dwo))
8949     {
8950       dwo_sections->macro.asection = sectp;
8951       dwo_sections->macro.size = bfd_get_section_size (sectp);
8952     }
8953   else if (section_is_p (sectp->name, &names->str_dwo))
8954     {
8955       dwo_sections->str.asection = sectp;
8956       dwo_sections->str.size = bfd_get_section_size (sectp);
8957     }
8958   else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8959     {
8960       dwo_sections->str_offsets.asection = sectp;
8961       dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8962     }
8963   else if (section_is_p (sectp->name, &names->types_dwo))
8964     {
8965       struct dwarf2_section_info type_section;
8966
8967       memset (&type_section, 0, sizeof (type_section));
8968       type_section.asection = sectp;
8969       type_section.size = bfd_get_section_size (sectp);
8970       VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8971                      &type_section);
8972     }
8973 }
8974
8975 /* Initialize the use of the DWO file specified by DWO_NAME.
8976    The result is NULL if DWO_NAME can't be found.  */
8977
8978 static struct dwo_file *
8979 open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
8980 {
8981   struct objfile *objfile = dwarf2_per_objfile->objfile;
8982   struct dwo_file *dwo_file;
8983   bfd *dbfd;
8984   struct cleanup *cleanups;
8985
8986   dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8987   if (dbfd == NULL)
8988     {
8989       if (dwarf2_read_debug)
8990         fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8991       return NULL;
8992     }
8993   dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8994   dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8995                                   dwo_name, strlen (dwo_name));
8996   dwo_file->dbfd = dbfd;
8997
8998   cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8999
9000   bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
9001
9002   dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
9003
9004   dwo_file->tus = create_debug_types_hash_table (dwo_file,
9005                                                  dwo_file->sections.types);
9006
9007   discard_cleanups (cleanups);
9008
9009   if (dwarf2_read_debug)
9010     fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9011
9012   return dwo_file;
9013 }
9014
9015 /* This function is mapped across the sections and remembers the offset and
9016    size of each of the DWP debugging sections we are interested in.  */
9017
9018 static void
9019 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9020 {
9021   struct dwp_file *dwp_file = dwp_file_ptr;
9022   const struct dwop_section_names *names = &dwop_section_names;
9023   unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9024
9025   /* Record the ELF section number for later lookup: this is what the
9026      .debug_cu_index,.debug_tu_index tables use.  */
9027   gdb_assert (elf_section_nr < dwp_file->num_sections);
9028   dwp_file->elf_sections[elf_section_nr] = sectp;
9029
9030   /* Look for specific sections that we need.  */
9031   if (section_is_p (sectp->name, &names->str_dwo))
9032     {
9033       dwp_file->sections.str.asection = sectp;
9034       dwp_file->sections.str.size = bfd_get_section_size (sectp);
9035     }
9036   else if (section_is_p (sectp->name, &names->cu_index))
9037     {
9038       dwp_file->sections.cu_index.asection = sectp;
9039       dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9040     }
9041   else if (section_is_p (sectp->name, &names->tu_index))
9042     {
9043       dwp_file->sections.tu_index.asection = sectp;
9044       dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9045     }
9046 }
9047
9048 /* Hash function for dwp_file loaded CUs/TUs.  */
9049
9050 static hashval_t
9051 hash_dwp_loaded_cutus (const void *item)
9052 {
9053   const struct dwo_unit *dwo_unit = item;
9054
9055   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
9056   return dwo_unit->signature;
9057 }
9058
9059 /* Equality function for dwp_file loaded CUs/TUs.  */
9060
9061 static int
9062 eq_dwp_loaded_cutus (const void *a, const void *b)
9063 {
9064   const struct dwo_unit *dua = a;
9065   const struct dwo_unit *dub = b;
9066
9067   return dua->signature == dub->signature;
9068 }
9069
9070 /* Allocate a hash table for dwp_file loaded CUs/TUs.  */
9071
9072 static htab_t
9073 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9074 {
9075   return htab_create_alloc_ex (3,
9076                                hash_dwp_loaded_cutus,
9077                                eq_dwp_loaded_cutus,
9078                                NULL,
9079                                &objfile->objfile_obstack,
9080                                hashtab_obstack_allocate,
9081                                dummy_obstack_deallocate);
9082 }
9083
9084 /* Initialize the use of the DWP file for the current objfile.
9085    By convention the name of the DWP file is ${objfile}.dwp.
9086    The result is NULL if it can't be found.  */
9087
9088 static struct dwp_file *
9089 open_and_init_dwp_file (const char *comp_dir)
9090 {
9091   struct objfile *objfile = dwarf2_per_objfile->objfile;
9092   struct dwp_file *dwp_file;
9093   char *dwp_name;
9094   bfd *dbfd;
9095   struct cleanup *cleanups;
9096
9097   dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9098   cleanups = make_cleanup (xfree, dwp_name);
9099
9100   dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9101   if (dbfd == NULL)
9102     {
9103       if (dwarf2_read_debug)
9104         fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9105       do_cleanups (cleanups);
9106       return NULL;
9107     }
9108   dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9109   dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9110                                   dwp_name, strlen (dwp_name));
9111   dwp_file->dbfd = dbfd;
9112   do_cleanups (cleanups);
9113
9114   cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
9115
9116   /* +1: section 0 is unused */
9117   dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9118   dwp_file->elf_sections =
9119     OBSTACK_CALLOC (&objfile->objfile_obstack,
9120                     dwp_file->num_sections, asection *);
9121
9122   bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9123
9124   dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9125
9126   dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9127
9128   dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9129
9130   discard_cleanups (cleanups);
9131
9132   if (dwarf2_read_debug)
9133     {
9134       fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9135       fprintf_unfiltered (gdb_stdlog,
9136                           "    %u CUs, %u TUs\n",
9137                           dwp_file->cus ? dwp_file->cus->nr_units : 0,
9138                           dwp_file->tus ? dwp_file->tus->nr_units : 0);
9139     }
9140
9141   return dwp_file;
9142 }
9143
9144 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9145    Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9146    or in the DWP file for the objfile, referenced by THIS_UNIT.
9147    If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9148    IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9149
9150    This is called, for example, when wanting to read a variable with a
9151    complex location.  Therefore we don't want to do file i/o for every call.
9152    Therefore we don't want to look for a DWO file on every call.
9153    Therefore we first see if we've already seen SIGNATURE in a DWP file,
9154    then we check if we've already seen DWO_NAME, and only THEN do we check
9155    for a DWO file.
9156
9157    The result is a pointer to the dwo_unit object or NULL if we didn't find it
9158    (dwo_id mismatch or couldn't find the DWO/DWP file).  */
9159
9160 static struct dwo_unit *
9161 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9162                  const char *dwo_name, const char *comp_dir,
9163                  ULONGEST signature, int is_debug_types)
9164 {
9165   struct objfile *objfile = dwarf2_per_objfile->objfile;
9166   const char *kind = is_debug_types ? "TU" : "CU";
9167   void **dwo_file_slot;
9168   struct dwo_file *dwo_file;
9169   struct dwp_file *dwp_file;
9170
9171   /* Have we already read SIGNATURE from a DWP file?  */
9172
9173   if (! dwarf2_per_objfile->dwp_checked)
9174     {
9175       dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9176       dwarf2_per_objfile->dwp_checked = 1;
9177     }
9178   dwp_file = dwarf2_per_objfile->dwp_file;
9179
9180   if (dwp_file != NULL)
9181     {
9182       const struct dwp_hash_table *dwp_htab =
9183         is_debug_types ? dwp_file->tus : dwp_file->cus;
9184
9185       if (dwp_htab != NULL)
9186         {
9187           struct dwo_unit *dwo_cutu =
9188             lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9189
9190           if (dwo_cutu != NULL)
9191             {
9192               if (dwarf2_read_debug)
9193                 {
9194                   fprintf_unfiltered (gdb_stdlog,
9195                                       "Virtual DWO %s %s found: @%s\n",
9196                                       kind, hex_string (signature),
9197                                       host_address_to_string (dwo_cutu));
9198                 }
9199               return dwo_cutu;
9200             }
9201         }
9202     }
9203
9204   /* Have we already seen DWO_NAME?  */
9205
9206   dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9207   if (*dwo_file_slot == NULL)
9208     {
9209       /* Read in the file and build a table of the DWOs it contains.  */
9210       *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9211     }
9212   /* NOTE: This will be NULL if unable to open the file.  */
9213   dwo_file = *dwo_file_slot;
9214
9215   if (dwo_file != NULL)
9216     {
9217       htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9218
9219       if (htab != NULL)
9220         {
9221           struct dwo_unit find_dwo_cutu, *dwo_cutu;
9222
9223           memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9224           find_dwo_cutu.signature = signature;
9225           dwo_cutu = htab_find (htab, &find_dwo_cutu);
9226
9227           if (dwo_cutu != NULL)
9228             {
9229               if (dwarf2_read_debug)
9230                 {
9231                   fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9232                                       kind, dwo_name, hex_string (signature),
9233                                       host_address_to_string (dwo_cutu));
9234                 }
9235               return dwo_cutu;
9236             }
9237         }
9238     }
9239
9240   /* We didn't find it.  This could mean a dwo_id mismatch, or
9241      someone deleted the DWO/DWP file, or the search path isn't set up
9242      correctly to find the file.  */
9243
9244   if (dwarf2_read_debug)
9245     {
9246       fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9247                           kind, dwo_name, hex_string (signature));
9248     }
9249
9250   complaint (&symfile_complaints,
9251              _("Could not find DWO CU referenced by CU at offset 0x%x"
9252                " [in module %s]"),
9253              this_unit->offset.sect_off, objfile->name);
9254   return NULL;
9255 }
9256
9257 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9258    See lookup_dwo_cutu_unit for details.  */
9259
9260 static struct dwo_unit *
9261 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9262                       const char *dwo_name, const char *comp_dir,
9263                       ULONGEST signature)
9264 {
9265   return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9266 }
9267
9268 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9269    See lookup_dwo_cutu_unit for details.  */
9270
9271 static struct dwo_unit *
9272 lookup_dwo_type_unit (struct signatured_type *this_tu,
9273                       const char *dwo_name, const char *comp_dir)
9274 {
9275   return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9276 }
9277
9278 /* Free all resources associated with DWO_FILE.
9279    Close the DWO file and munmap the sections.
9280    All memory should be on the objfile obstack.  */
9281
9282 static void
9283 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9284 {
9285   int ix;
9286   struct dwarf2_section_info *section;
9287
9288   gdb_bfd_unref (dwo_file->dbfd);
9289
9290   VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9291 }
9292
9293 /* Wrapper for free_dwo_file for use in cleanups.  */
9294
9295 static void
9296 free_dwo_file_cleanup (void *arg)
9297 {
9298   struct dwo_file *dwo_file = (struct dwo_file *) arg;
9299   struct objfile *objfile = dwarf2_per_objfile->objfile;
9300
9301   free_dwo_file (dwo_file, objfile);
9302 }
9303
9304 /* Traversal function for free_dwo_files.  */
9305
9306 static int
9307 free_dwo_file_from_slot (void **slot, void *info)
9308 {
9309   struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9310   struct objfile *objfile = (struct objfile *) info;
9311
9312   free_dwo_file (dwo_file, objfile);
9313
9314   return 1;
9315 }
9316
9317 /* Free all resources associated with DWO_FILES.  */
9318
9319 static void
9320 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9321 {
9322   htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9323 }
9324 \f
9325 /* Read in various DIEs.  */
9326
9327 /* qsort helper for inherit_abstract_dies.  */
9328
9329 static int
9330 unsigned_int_compar (const void *ap, const void *bp)
9331 {
9332   unsigned int a = *(unsigned int *) ap;
9333   unsigned int b = *(unsigned int *) bp;
9334
9335   return (a > b) - (b > a);
9336 }
9337
9338 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9339    Inherit only the children of the DW_AT_abstract_origin DIE not being
9340    already referenced by DW_AT_abstract_origin from the children of the
9341    current DIE.  */
9342
9343 static void
9344 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9345 {
9346   struct die_info *child_die;
9347   unsigned die_children_count;
9348   /* CU offsets which were referenced by children of the current DIE.  */
9349   sect_offset *offsets;
9350   sect_offset *offsets_end, *offsetp;
9351   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
9352   struct die_info *origin_die;
9353   /* Iterator of the ORIGIN_DIE children.  */
9354   struct die_info *origin_child_die;
9355   struct cleanup *cleanups;
9356   struct attribute *attr;
9357   struct dwarf2_cu *origin_cu;
9358   struct pending **origin_previous_list_in_scope;
9359
9360   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9361   if (!attr)
9362     return;
9363
9364   /* Note that following die references may follow to a die in a
9365      different cu.  */
9366
9367   origin_cu = cu;
9368   origin_die = follow_die_ref (die, attr, &origin_cu);
9369
9370   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9371      symbols in.  */
9372   origin_previous_list_in_scope = origin_cu->list_in_scope;
9373   origin_cu->list_in_scope = cu->list_in_scope;
9374
9375   if (die->tag != origin_die->tag
9376       && !(die->tag == DW_TAG_inlined_subroutine
9377            && origin_die->tag == DW_TAG_subprogram))
9378     complaint (&symfile_complaints,
9379                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9380                die->offset.sect_off, origin_die->offset.sect_off);
9381
9382   child_die = die->child;
9383   die_children_count = 0;
9384   while (child_die && child_die->tag)
9385     {
9386       child_die = sibling_die (child_die);
9387       die_children_count++;
9388     }
9389   offsets = xmalloc (sizeof (*offsets) * die_children_count);
9390   cleanups = make_cleanup (xfree, offsets);
9391
9392   offsets_end = offsets;
9393   child_die = die->child;
9394   while (child_die && child_die->tag)
9395     {
9396       /* For each CHILD_DIE, find the corresponding child of
9397          ORIGIN_DIE.  If there is more than one layer of
9398          DW_AT_abstract_origin, follow them all; there shouldn't be,
9399          but GCC versions at least through 4.4 generate this (GCC PR
9400          40573).  */
9401       struct die_info *child_origin_die = child_die;
9402       struct dwarf2_cu *child_origin_cu = cu;
9403
9404       while (1)
9405         {
9406           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9407                               child_origin_cu);
9408           if (attr == NULL)
9409             break;
9410           child_origin_die = follow_die_ref (child_origin_die, attr,
9411                                              &child_origin_cu);
9412         }
9413
9414       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9415          counterpart may exist.  */
9416       if (child_origin_die != child_die)
9417         {
9418           if (child_die->tag != child_origin_die->tag
9419               && !(child_die->tag == DW_TAG_inlined_subroutine
9420                    && child_origin_die->tag == DW_TAG_subprogram))
9421             complaint (&symfile_complaints,
9422                        _("Child DIE 0x%x and its abstract origin 0x%x have "
9423                          "different tags"), child_die->offset.sect_off,
9424                        child_origin_die->offset.sect_off);
9425           if (child_origin_die->parent != origin_die)
9426             complaint (&symfile_complaints,
9427                        _("Child DIE 0x%x and its abstract origin 0x%x have "
9428                          "different parents"), child_die->offset.sect_off,
9429                        child_origin_die->offset.sect_off);
9430           else
9431             *offsets_end++ = child_origin_die->offset;
9432         }
9433       child_die = sibling_die (child_die);
9434     }
9435   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9436          unsigned_int_compar);
9437   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9438     if (offsetp[-1].sect_off == offsetp->sect_off)
9439       complaint (&symfile_complaints,
9440                  _("Multiple children of DIE 0x%x refer "
9441                    "to DIE 0x%x as their abstract origin"),
9442                  die->offset.sect_off, offsetp->sect_off);
9443
9444   offsetp = offsets;
9445   origin_child_die = origin_die->child;
9446   while (origin_child_die && origin_child_die->tag)
9447     {
9448       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
9449       while (offsetp < offsets_end
9450              && offsetp->sect_off < origin_child_die->offset.sect_off)
9451         offsetp++;
9452       if (offsetp >= offsets_end
9453           || offsetp->sect_off > origin_child_die->offset.sect_off)
9454         {
9455           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
9456           process_die (origin_child_die, origin_cu);
9457         }
9458       origin_child_die = sibling_die (origin_child_die);
9459     }
9460   origin_cu->list_in_scope = origin_previous_list_in_scope;
9461
9462   do_cleanups (cleanups);
9463 }
9464
9465 static void
9466 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9467 {
9468   struct objfile *objfile = cu->objfile;
9469   struct context_stack *new;
9470   CORE_ADDR lowpc;
9471   CORE_ADDR highpc;
9472   struct die_info *child_die;
9473   struct attribute *attr, *call_line, *call_file;
9474   const char *name;
9475   CORE_ADDR baseaddr;
9476   struct block *block;
9477   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9478   VEC (symbolp) *template_args = NULL;
9479   struct template_symbol *templ_func = NULL;
9480
9481   if (inlined_func)
9482     {
9483       /* If we do not have call site information, we can't show the
9484          caller of this inlined function.  That's too confusing, so
9485          only use the scope for local variables.  */
9486       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9487       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9488       if (call_line == NULL || call_file == NULL)
9489         {
9490           read_lexical_block_scope (die, cu);
9491           return;
9492         }
9493     }
9494
9495   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9496
9497   name = dwarf2_name (die, cu);
9498
9499   /* Ignore functions with missing or empty names.  These are actually
9500      illegal according to the DWARF standard.  */
9501   if (name == NULL)
9502     {
9503       complaint (&symfile_complaints,
9504                  _("missing name for subprogram DIE at %d"),
9505                  die->offset.sect_off);
9506       return;
9507     }
9508
9509   /* Ignore functions with missing or invalid low and high pc attributes.  */
9510   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9511     {
9512       attr = dwarf2_attr (die, DW_AT_external, cu);
9513       if (!attr || !DW_UNSND (attr))
9514         complaint (&symfile_complaints,
9515                    _("cannot get low and high bounds "
9516                      "for subprogram DIE at %d"),
9517                    die->offset.sect_off);
9518       return;
9519     }
9520
9521   lowpc += baseaddr;
9522   highpc += baseaddr;
9523
9524   /* If we have any template arguments, then we must allocate a
9525      different sort of symbol.  */
9526   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9527     {
9528       if (child_die->tag == DW_TAG_template_type_param
9529           || child_die->tag == DW_TAG_template_value_param)
9530         {
9531           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9532                                        struct template_symbol);
9533           templ_func->base.is_cplus_template_function = 1;
9534           break;
9535         }
9536     }
9537
9538   new = push_context (0, lowpc);
9539   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9540                                (struct symbol *) templ_func);
9541
9542   /* If there is a location expression for DW_AT_frame_base, record
9543      it.  */
9544   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9545   if (attr)
9546     dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
9547
9548   cu->list_in_scope = &local_symbols;
9549
9550   if (die->child != NULL)
9551     {
9552       child_die = die->child;
9553       while (child_die && child_die->tag)
9554         {
9555           if (child_die->tag == DW_TAG_template_type_param
9556               || child_die->tag == DW_TAG_template_value_param)
9557             {
9558               struct symbol *arg = new_symbol (child_die, NULL, cu);
9559
9560               if (arg != NULL)
9561                 VEC_safe_push (symbolp, template_args, arg);
9562             }
9563           else
9564             process_die (child_die, cu);
9565           child_die = sibling_die (child_die);
9566         }
9567     }
9568
9569   inherit_abstract_dies (die, cu);
9570
9571   /* If we have a DW_AT_specification, we might need to import using
9572      directives from the context of the specification DIE.  See the
9573      comment in determine_prefix.  */
9574   if (cu->language == language_cplus
9575       && dwarf2_attr (die, DW_AT_specification, cu))
9576     {
9577       struct dwarf2_cu *spec_cu = cu;
9578       struct die_info *spec_die = die_specification (die, &spec_cu);
9579
9580       while (spec_die)
9581         {
9582           child_die = spec_die->child;
9583           while (child_die && child_die->tag)
9584             {
9585               if (child_die->tag == DW_TAG_imported_module)
9586                 process_die (child_die, spec_cu);
9587               child_die = sibling_die (child_die);
9588             }
9589
9590           /* In some cases, GCC generates specification DIEs that
9591              themselves contain DW_AT_specification attributes.  */
9592           spec_die = die_specification (spec_die, &spec_cu);
9593         }
9594     }
9595
9596   new = pop_context ();
9597   /* Make a block for the local symbols within.  */
9598   block = finish_block (new->name, &local_symbols, new->old_blocks,
9599                         lowpc, highpc, objfile);
9600
9601   /* For C++, set the block's scope.  */
9602   if ((cu->language == language_cplus || cu->language == language_fortran)
9603       && cu->processing_has_namespace_info)
9604     block_set_scope (block, determine_prefix (die, cu),
9605                      &objfile->objfile_obstack);
9606
9607   /* If we have address ranges, record them.  */
9608   dwarf2_record_block_ranges (die, block, baseaddr, cu);
9609
9610   /* Attach template arguments to function.  */
9611   if (! VEC_empty (symbolp, template_args))
9612     {
9613       gdb_assert (templ_func != NULL);
9614
9615       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9616       templ_func->template_arguments
9617         = obstack_alloc (&objfile->objfile_obstack,
9618                          (templ_func->n_template_arguments
9619                           * sizeof (struct symbol *)));
9620       memcpy (templ_func->template_arguments,
9621               VEC_address (symbolp, template_args),
9622               (templ_func->n_template_arguments * sizeof (struct symbol *)));
9623       VEC_free (symbolp, template_args);
9624     }
9625
9626   /* In C++, we can have functions nested inside functions (e.g., when
9627      a function declares a class that has methods).  This means that
9628      when we finish processing a function scope, we may need to go
9629      back to building a containing block's symbol lists.  */
9630   local_symbols = new->locals;
9631   using_directives = new->using_directives;
9632
9633   /* If we've finished processing a top-level function, subsequent
9634      symbols go in the file symbol list.  */
9635   if (outermost_context_p ())
9636     cu->list_in_scope = &file_symbols;
9637 }
9638
9639 /* Process all the DIES contained within a lexical block scope.  Start
9640    a new scope, process the dies, and then close the scope.  */
9641
9642 static void
9643 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9644 {
9645   struct objfile *objfile = cu->objfile;
9646   struct context_stack *new;
9647   CORE_ADDR lowpc, highpc;
9648   struct die_info *child_die;
9649   CORE_ADDR baseaddr;
9650
9651   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9652
9653   /* Ignore blocks with missing or invalid low and high pc attributes.  */
9654   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9655      as multiple lexical blocks?  Handling children in a sane way would
9656      be nasty.  Might be easier to properly extend generic blocks to
9657      describe ranges.  */
9658   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9659     return;
9660   lowpc += baseaddr;
9661   highpc += baseaddr;
9662
9663   push_context (0, lowpc);
9664   if (die->child != NULL)
9665     {
9666       child_die = die->child;
9667       while (child_die && child_die->tag)
9668         {
9669           process_die (child_die, cu);
9670           child_die = sibling_die (child_die);
9671         }
9672     }
9673   new = pop_context ();
9674
9675   if (local_symbols != NULL || using_directives != NULL)
9676     {
9677       struct block *block
9678         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9679                         highpc, objfile);
9680
9681       /* Note that recording ranges after traversing children, as we
9682          do here, means that recording a parent's ranges entails
9683          walking across all its children's ranges as they appear in
9684          the address map, which is quadratic behavior.
9685
9686          It would be nicer to record the parent's ranges before
9687          traversing its children, simply overriding whatever you find
9688          there.  But since we don't even decide whether to create a
9689          block until after we've traversed its children, that's hard
9690          to do.  */
9691       dwarf2_record_block_ranges (die, block, baseaddr, cu);
9692     }
9693   local_symbols = new->locals;
9694   using_directives = new->using_directives;
9695 }
9696
9697 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */
9698
9699 static void
9700 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9701 {
9702   struct objfile *objfile = cu->objfile;
9703   struct gdbarch *gdbarch = get_objfile_arch (objfile);
9704   CORE_ADDR pc, baseaddr;
9705   struct attribute *attr;
9706   struct call_site *call_site, call_site_local;
9707   void **slot;
9708   int nparams;
9709   struct die_info *child_die;
9710
9711   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9712
9713   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9714   if (!attr)
9715     {
9716       complaint (&symfile_complaints,
9717                  _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9718                    "DIE 0x%x [in module %s]"),
9719                  die->offset.sect_off, objfile->name);
9720       return;
9721     }
9722   pc = DW_ADDR (attr) + baseaddr;
9723
9724   if (cu->call_site_htab == NULL)
9725     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9726                                                NULL, &objfile->objfile_obstack,
9727                                                hashtab_obstack_allocate, NULL);
9728   call_site_local.pc = pc;
9729   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9730   if (*slot != NULL)
9731     {
9732       complaint (&symfile_complaints,
9733                  _("Duplicate PC %s for DW_TAG_GNU_call_site "
9734                    "DIE 0x%x [in module %s]"),
9735                  paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9736       return;
9737     }
9738
9739   /* Count parameters at the caller.  */
9740
9741   nparams = 0;
9742   for (child_die = die->child; child_die && child_die->tag;
9743        child_die = sibling_die (child_die))
9744     {
9745       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9746         {
9747           complaint (&symfile_complaints,
9748                      _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9749                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9750                      child_die->tag, child_die->offset.sect_off, objfile->name);
9751           continue;
9752         }
9753
9754       nparams++;
9755     }
9756
9757   call_site = obstack_alloc (&objfile->objfile_obstack,
9758                              (sizeof (*call_site)
9759                               + (sizeof (*call_site->parameter)
9760                                  * (nparams - 1))));
9761   *slot = call_site;
9762   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9763   call_site->pc = pc;
9764
9765   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9766     {
9767       struct die_info *func_die;
9768
9769       /* Skip also over DW_TAG_inlined_subroutine.  */
9770       for (func_die = die->parent;
9771            func_die && func_die->tag != DW_TAG_subprogram
9772            && func_die->tag != DW_TAG_subroutine_type;
9773            func_die = func_die->parent);
9774
9775       /* DW_AT_GNU_all_call_sites is a superset
9776          of DW_AT_GNU_all_tail_call_sites.  */
9777       if (func_die
9778           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9779           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9780         {
9781           /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9782              not complete.  But keep CALL_SITE for look ups via call_site_htab,
9783              both the initial caller containing the real return address PC and
9784              the final callee containing the current PC of a chain of tail
9785              calls do not need to have the tail call list complete.  But any
9786              function candidate for a virtual tail call frame searched via
9787              TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9788              determined unambiguously.  */
9789         }
9790       else
9791         {
9792           struct type *func_type = NULL;
9793
9794           if (func_die)
9795             func_type = get_die_type (func_die, cu);
9796           if (func_type != NULL)
9797             {
9798               gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9799
9800               /* Enlist this call site to the function.  */
9801               call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9802               TYPE_TAIL_CALL_LIST (func_type) = call_site;
9803             }
9804           else
9805             complaint (&symfile_complaints,
9806                        _("Cannot find function owning DW_TAG_GNU_call_site "
9807                          "DIE 0x%x [in module %s]"),
9808                        die->offset.sect_off, objfile->name);
9809         }
9810     }
9811
9812   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9813   if (attr == NULL)
9814     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9815   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9816   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9817     /* Keep NULL DWARF_BLOCK.  */;
9818   else if (attr_form_is_block (attr))
9819     {
9820       struct dwarf2_locexpr_baton *dlbaton;
9821
9822       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9823       dlbaton->data = DW_BLOCK (attr)->data;
9824       dlbaton->size = DW_BLOCK (attr)->size;
9825       dlbaton->per_cu = cu->per_cu;
9826
9827       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9828     }
9829   else if (is_ref_attr (attr))
9830     {
9831       struct dwarf2_cu *target_cu = cu;
9832       struct die_info *target_die;
9833
9834       target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9835       gdb_assert (target_cu->objfile == objfile);
9836       if (die_is_declaration (target_die, target_cu))
9837         {
9838           const char *target_physname = NULL;
9839           struct attribute *target_attr;
9840
9841           /* Prefer the mangled name; otherwise compute the demangled one.  */
9842           target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9843           if (target_attr == NULL)
9844             target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9845                                        target_cu);
9846           if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9847             target_physname = DW_STRING (target_attr);
9848           else
9849             target_physname = dwarf2_physname (NULL, target_die, target_cu);
9850           if (target_physname == NULL)
9851             complaint (&symfile_complaints,
9852                        _("DW_AT_GNU_call_site_target target DIE has invalid "
9853                          "physname, for referencing DIE 0x%x [in module %s]"),
9854                        die->offset.sect_off, objfile->name);
9855           else
9856             SET_FIELD_PHYSNAME (call_site->target, target_physname);
9857         }
9858       else
9859         {
9860           CORE_ADDR lowpc;
9861
9862           /* DW_AT_entry_pc should be preferred.  */
9863           if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9864             complaint (&symfile_complaints,
9865                        _("DW_AT_GNU_call_site_target target DIE has invalid "
9866                          "low pc, for referencing DIE 0x%x [in module %s]"),
9867                        die->offset.sect_off, objfile->name);
9868           else
9869             SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9870         }
9871     }
9872   else
9873     complaint (&symfile_complaints,
9874                _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9875                  "block nor reference, for DIE 0x%x [in module %s]"),
9876                die->offset.sect_off, objfile->name);
9877
9878   call_site->per_cu = cu->per_cu;
9879
9880   for (child_die = die->child;
9881        child_die && child_die->tag;
9882        child_die = sibling_die (child_die))
9883     {
9884       struct call_site_parameter *parameter;
9885       struct attribute *loc, *origin;
9886
9887       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9888         {
9889           /* Already printed the complaint above.  */
9890           continue;
9891         }
9892
9893       gdb_assert (call_site->parameter_count < nparams);
9894       parameter = &call_site->parameter[call_site->parameter_count];
9895
9896       /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9897          specifies DW_TAG_formal_parameter.  Value of the data assumed for the
9898          register is contained in DW_AT_GNU_call_site_value.  */
9899
9900       loc = dwarf2_attr (child_die, DW_AT_location, cu);
9901       origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9902       if (loc == NULL && origin != NULL && is_ref_attr (origin))
9903         {
9904           sect_offset offset;
9905
9906           parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9907           offset = dwarf2_get_ref_die_offset (origin);
9908           if (!offset_in_cu_p (&cu->header, offset))
9909             {
9910               /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9911                  binding can be done only inside one CU.  Such referenced DIE
9912                  therefore cannot be even moved to DW_TAG_partial_unit.  */
9913               complaint (&symfile_complaints,
9914                          _("DW_AT_abstract_origin offset is not in CU for "
9915                            "DW_TAG_GNU_call_site child DIE 0x%x "
9916                            "[in module %s]"),
9917                          child_die->offset.sect_off, objfile->name);
9918               continue;
9919             }
9920           parameter->u.param_offset.cu_off = (offset.sect_off
9921                                               - cu->header.offset.sect_off);
9922         }
9923       else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9924         {
9925           complaint (&symfile_complaints,
9926                      _("No DW_FORM_block* DW_AT_location for "
9927                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9928                      child_die->offset.sect_off, objfile->name);
9929           continue;
9930         }
9931       else
9932         {
9933           parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9934             (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9935           if (parameter->u.dwarf_reg != -1)
9936             parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9937           else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9938                                     &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9939                                              &parameter->u.fb_offset))
9940             parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9941           else
9942             {
9943               complaint (&symfile_complaints,
9944                          _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9945                            "for DW_FORM_block* DW_AT_location is supported for "
9946                            "DW_TAG_GNU_call_site child DIE 0x%x "
9947                            "[in module %s]"),
9948                          child_die->offset.sect_off, objfile->name);
9949               continue;
9950             }
9951         }
9952
9953       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9954       if (!attr_form_is_block (attr))
9955         {
9956           complaint (&symfile_complaints,
9957                      _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9958                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9959                      child_die->offset.sect_off, objfile->name);
9960           continue;
9961         }
9962       parameter->value = DW_BLOCK (attr)->data;
9963       parameter->value_size = DW_BLOCK (attr)->size;
9964
9965       /* Parameters are not pre-cleared by memset above.  */
9966       parameter->data_value = NULL;
9967       parameter->data_value_size = 0;
9968       call_site->parameter_count++;
9969
9970       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9971       if (attr)
9972         {
9973           if (!attr_form_is_block (attr))
9974             complaint (&symfile_complaints,
9975                        _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9976                          "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9977                        child_die->offset.sect_off, objfile->name);
9978           else
9979             {
9980               parameter->data_value = DW_BLOCK (attr)->data;
9981               parameter->data_value_size = DW_BLOCK (attr)->size;
9982             }
9983         }
9984     }
9985 }
9986
9987 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9988    Return 1 if the attributes are present and valid, otherwise, return 0.
9989    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
9990
9991 static int
9992 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9993                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
9994                     struct partial_symtab *ranges_pst)
9995 {
9996   struct objfile *objfile = cu->objfile;
9997   struct comp_unit_head *cu_header = &cu->header;
9998   bfd *obfd = objfile->obfd;
9999   unsigned int addr_size = cu_header->addr_size;
10000   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10001   /* Base address selection entry.  */
10002   CORE_ADDR base;
10003   int found_base;
10004   unsigned int dummy;
10005   gdb_byte *buffer;
10006   CORE_ADDR marker;
10007   int low_set;
10008   CORE_ADDR low = 0;
10009   CORE_ADDR high = 0;
10010   CORE_ADDR baseaddr;
10011
10012   found_base = cu->base_known;
10013   base = cu->base_address;
10014
10015   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10016   if (offset >= dwarf2_per_objfile->ranges.size)
10017     {
10018       complaint (&symfile_complaints,
10019                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
10020                  offset);
10021       return 0;
10022     }
10023   buffer = dwarf2_per_objfile->ranges.buffer + offset;
10024
10025   /* Read in the largest possible address.  */
10026   marker = read_address (obfd, buffer, cu, &dummy);
10027   if ((marker & mask) == mask)
10028     {
10029       /* If we found the largest possible address, then
10030          read the base address.  */
10031       base = read_address (obfd, buffer + addr_size, cu, &dummy);
10032       buffer += 2 * addr_size;
10033       offset += 2 * addr_size;
10034       found_base = 1;
10035     }
10036
10037   low_set = 0;
10038
10039   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10040
10041   while (1)
10042     {
10043       CORE_ADDR range_beginning, range_end;
10044
10045       range_beginning = read_address (obfd, buffer, cu, &dummy);
10046       buffer += addr_size;
10047       range_end = read_address (obfd, buffer, cu, &dummy);
10048       buffer += addr_size;
10049       offset += 2 * addr_size;
10050
10051       /* An end of list marker is a pair of zero addresses.  */
10052       if (range_beginning == 0 && range_end == 0)
10053         /* Found the end of list entry.  */
10054         break;
10055
10056       /* Each base address selection entry is a pair of 2 values.
10057          The first is the largest possible address, the second is
10058          the base address.  Check for a base address here.  */
10059       if ((range_beginning & mask) == mask)
10060         {
10061           /* If we found the largest possible address, then
10062              read the base address.  */
10063           base = read_address (obfd, buffer + addr_size, cu, &dummy);
10064           found_base = 1;
10065           continue;
10066         }
10067
10068       if (!found_base)
10069         {
10070           /* We have no valid base address for the ranges
10071              data.  */
10072           complaint (&symfile_complaints,
10073                      _("Invalid .debug_ranges data (no base address)"));
10074           return 0;
10075         }
10076
10077       if (range_beginning > range_end)
10078         {
10079           /* Inverted range entries are invalid.  */
10080           complaint (&symfile_complaints,
10081                      _("Invalid .debug_ranges data (inverted range)"));
10082           return 0;
10083         }
10084
10085       /* Empty range entries have no effect.  */
10086       if (range_beginning == range_end)
10087         continue;
10088
10089       range_beginning += base;
10090       range_end += base;
10091
10092       /* A not-uncommon case of bad debug info.
10093          Don't pollute the addrmap with bad data.  */
10094       if (range_beginning + baseaddr == 0
10095           && !dwarf2_per_objfile->has_section_at_zero)
10096         {
10097           complaint (&symfile_complaints,
10098                      _(".debug_ranges entry has start address of zero"
10099                        " [in module %s]"), objfile->name);
10100           continue;
10101         }
10102
10103       if (ranges_pst != NULL)
10104         addrmap_set_empty (objfile->psymtabs_addrmap,
10105                            range_beginning + baseaddr,
10106                            range_end - 1 + baseaddr,
10107                            ranges_pst);
10108
10109       /* FIXME: This is recording everything as a low-high
10110          segment of consecutive addresses.  We should have a
10111          data structure for discontiguous block ranges
10112          instead.  */
10113       if (! low_set)
10114         {
10115           low = range_beginning;
10116           high = range_end;
10117           low_set = 1;
10118         }
10119       else
10120         {
10121           if (range_beginning < low)
10122             low = range_beginning;
10123           if (range_end > high)
10124             high = range_end;
10125         }
10126     }
10127
10128   if (! low_set)
10129     /* If the first entry is an end-of-list marker, the range
10130        describes an empty scope, i.e. no instructions.  */
10131     return 0;
10132
10133   if (low_return)
10134     *low_return = low;
10135   if (high_return)
10136     *high_return = high;
10137   return 1;
10138 }
10139
10140 /* Get low and high pc attributes from a die.  Return 1 if the attributes
10141    are present and valid, otherwise, return 0.  Return -1 if the range is
10142    discontinuous, i.e. derived from DW_AT_ranges information.  */
10143
10144 static int
10145 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10146                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
10147                       struct partial_symtab *pst)
10148 {
10149   struct attribute *attr;
10150   struct attribute *attr_high;
10151   CORE_ADDR low = 0;
10152   CORE_ADDR high = 0;
10153   int ret = 0;
10154
10155   attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10156   if (attr_high)
10157     {
10158       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10159       if (attr)
10160         {
10161           low = DW_ADDR (attr);
10162           if (attr_high->form == DW_FORM_addr
10163               || attr_high->form == DW_FORM_GNU_addr_index)
10164             high = DW_ADDR (attr_high);
10165           else
10166             high = low + DW_UNSND (attr_high);
10167         }
10168       else
10169         /* Found high w/o low attribute.  */
10170         return 0;
10171
10172       /* Found consecutive range of addresses.  */
10173       ret = 1;
10174     }
10175   else
10176     {
10177       attr = dwarf2_attr (die, DW_AT_ranges, cu);
10178       if (attr != NULL)
10179         {
10180           /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10181              We take advantage of the fact that DW_AT_ranges does not appear
10182              in DW_TAG_compile_unit of DWO files.  */
10183           int need_ranges_base = die->tag != DW_TAG_compile_unit;
10184           unsigned int ranges_offset = (DW_UNSND (attr)
10185                                         + (need_ranges_base
10186                                            ? cu->ranges_base
10187                                            : 0));
10188
10189           /* Value of the DW_AT_ranges attribute is the offset in the
10190              .debug_ranges section.  */
10191           if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10192             return 0;
10193           /* Found discontinuous range of addresses.  */
10194           ret = -1;
10195         }
10196     }
10197
10198   /* read_partial_die has also the strict LOW < HIGH requirement.  */
10199   if (high <= low)
10200     return 0;
10201
10202   /* When using the GNU linker, .gnu.linkonce. sections are used to
10203      eliminate duplicate copies of functions and vtables and such.
10204      The linker will arbitrarily choose one and discard the others.
10205      The AT_*_pc values for such functions refer to local labels in
10206      these sections.  If the section from that file was discarded, the
10207      labels are not in the output, so the relocs get a value of 0.
10208      If this is a discarded function, mark the pc bounds as invalid,
10209      so that GDB will ignore it.  */
10210   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10211     return 0;
10212
10213   *lowpc = low;
10214   if (highpc)
10215     *highpc = high;
10216   return ret;
10217 }
10218
10219 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10220    its low and high PC addresses.  Do nothing if these addresses could not
10221    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
10222    and HIGHPC to the high address if greater than HIGHPC.  */
10223
10224 static void
10225 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10226                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
10227                                  struct dwarf2_cu *cu)
10228 {
10229   CORE_ADDR low, high;
10230   struct die_info *child = die->child;
10231
10232   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10233     {
10234       *lowpc = min (*lowpc, low);
10235       *highpc = max (*highpc, high);
10236     }
10237
10238   /* If the language does not allow nested subprograms (either inside
10239      subprograms or lexical blocks), we're done.  */
10240   if (cu->language != language_ada)
10241     return;
10242
10243   /* Check all the children of the given DIE.  If it contains nested
10244      subprograms, then check their pc bounds.  Likewise, we need to
10245      check lexical blocks as well, as they may also contain subprogram
10246      definitions.  */
10247   while (child && child->tag)
10248     {
10249       if (child->tag == DW_TAG_subprogram
10250           || child->tag == DW_TAG_lexical_block)
10251         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10252       child = sibling_die (child);
10253     }
10254 }
10255
10256 /* Get the low and high pc's represented by the scope DIE, and store
10257    them in *LOWPC and *HIGHPC.  If the correct values can't be
10258    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
10259
10260 static void
10261 get_scope_pc_bounds (struct die_info *die,
10262                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
10263                      struct dwarf2_cu *cu)
10264 {
10265   CORE_ADDR best_low = (CORE_ADDR) -1;
10266   CORE_ADDR best_high = (CORE_ADDR) 0;
10267   CORE_ADDR current_low, current_high;
10268
10269   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10270     {
10271       best_low = current_low;
10272       best_high = current_high;
10273     }
10274   else
10275     {
10276       struct die_info *child = die->child;
10277
10278       while (child && child->tag)
10279         {
10280           switch (child->tag) {
10281           case DW_TAG_subprogram:
10282             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10283             break;
10284           case DW_TAG_namespace:
10285           case DW_TAG_module:
10286             /* FIXME: carlton/2004-01-16: Should we do this for
10287                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
10288                that current GCC's always emit the DIEs corresponding
10289                to definitions of methods of classes as children of a
10290                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10291                the DIEs giving the declarations, which could be
10292                anywhere).  But I don't see any reason why the
10293                standards says that they have to be there.  */
10294             get_scope_pc_bounds (child, &current_low, &current_high, cu);
10295
10296             if (current_low != ((CORE_ADDR) -1))
10297               {
10298                 best_low = min (best_low, current_low);
10299                 best_high = max (best_high, current_high);
10300               }
10301             break;
10302           default:
10303             /* Ignore.  */
10304             break;
10305           }
10306
10307           child = sibling_die (child);
10308         }
10309     }
10310
10311   *lowpc = best_low;
10312   *highpc = best_high;
10313 }
10314
10315 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10316    in DIE.  */
10317
10318 static void
10319 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10320                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10321 {
10322   struct objfile *objfile = cu->objfile;
10323   struct attribute *attr;
10324   struct attribute *attr_high;
10325
10326   attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10327   if (attr_high)
10328     {
10329       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10330       if (attr)
10331         {
10332           CORE_ADDR low = DW_ADDR (attr);
10333           CORE_ADDR high;
10334           if (attr_high->form == DW_FORM_addr
10335               || attr_high->form == DW_FORM_GNU_addr_index)
10336             high = DW_ADDR (attr_high);
10337           else
10338             high = low + DW_UNSND (attr_high);
10339
10340           record_block_range (block, baseaddr + low, baseaddr + high - 1);
10341         }
10342     }
10343
10344   attr = dwarf2_attr (die, DW_AT_ranges, cu);
10345   if (attr)
10346     {
10347       bfd *obfd = objfile->obfd;
10348       /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10349          We take advantage of the fact that DW_AT_ranges does not appear
10350          in DW_TAG_compile_unit of DWO files.  */
10351       int need_ranges_base = die->tag != DW_TAG_compile_unit;
10352
10353       /* The value of the DW_AT_ranges attribute is the offset of the
10354          address range list in the .debug_ranges section.  */
10355       unsigned long offset = (DW_UNSND (attr)
10356                               + (need_ranges_base ? cu->ranges_base : 0));
10357       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10358
10359       /* For some target architectures, but not others, the
10360          read_address function sign-extends the addresses it returns.
10361          To recognize base address selection entries, we need a
10362          mask.  */
10363       unsigned int addr_size = cu->header.addr_size;
10364       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10365
10366       /* The base address, to which the next pair is relative.  Note
10367          that this 'base' is a DWARF concept: most entries in a range
10368          list are relative, to reduce the number of relocs against the
10369          debugging information.  This is separate from this function's
10370          'baseaddr' argument, which GDB uses to relocate debugging
10371          information from a shared library based on the address at
10372          which the library was loaded.  */
10373       CORE_ADDR base = cu->base_address;
10374       int base_known = cu->base_known;
10375
10376       gdb_assert (dwarf2_per_objfile->ranges.readin);
10377       if (offset >= dwarf2_per_objfile->ranges.size)
10378         {
10379           complaint (&symfile_complaints,
10380                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10381                      offset);
10382           return;
10383         }
10384
10385       for (;;)
10386         {
10387           unsigned int bytes_read;
10388           CORE_ADDR start, end;
10389
10390           start = read_address (obfd, buffer, cu, &bytes_read);
10391           buffer += bytes_read;
10392           end = read_address (obfd, buffer, cu, &bytes_read);
10393           buffer += bytes_read;
10394
10395           /* Did we find the end of the range list?  */
10396           if (start == 0 && end == 0)
10397             break;
10398
10399           /* Did we find a base address selection entry?  */
10400           else if ((start & base_select_mask) == base_select_mask)
10401             {
10402               base = end;
10403               base_known = 1;
10404             }
10405
10406           /* We found an ordinary address range.  */
10407           else
10408             {
10409               if (!base_known)
10410                 {
10411                   complaint (&symfile_complaints,
10412                              _("Invalid .debug_ranges data "
10413                                "(no base address)"));
10414                   return;
10415                 }
10416
10417               if (start > end)
10418                 {
10419                   /* Inverted range entries are invalid.  */
10420                   complaint (&symfile_complaints,
10421                              _("Invalid .debug_ranges data "
10422                                "(inverted range)"));
10423                   return;
10424                 }
10425
10426               /* Empty range entries have no effect.  */
10427               if (start == end)
10428                 continue;
10429
10430               start += base + baseaddr;
10431               end += base + baseaddr;
10432
10433               /* A not-uncommon case of bad debug info.
10434                  Don't pollute the addrmap with bad data.  */
10435               if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10436                 {
10437                   complaint (&symfile_complaints,
10438                              _(".debug_ranges entry has start address of zero"
10439                                " [in module %s]"), objfile->name);
10440                   continue;
10441                 }
10442
10443               record_block_range (block, start, end - 1);
10444             }
10445         }
10446     }
10447 }
10448
10449 /* Check whether the producer field indicates either of GCC < 4.6, or the
10450    Intel C/C++ compiler, and cache the result in CU.  */
10451
10452 static void
10453 check_producer (struct dwarf2_cu *cu)
10454 {
10455   const char *cs;
10456   int major, minor, release;
10457
10458   if (cu->producer == NULL)
10459     {
10460       /* For unknown compilers expect their behavior is DWARF version
10461          compliant.
10462
10463          GCC started to support .debug_types sections by -gdwarf-4 since
10464          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
10465          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10466          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10467          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
10468     }
10469   else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10470     {
10471       /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
10472
10473       cs = &cu->producer[strlen ("GNU ")];
10474       while (*cs && !isdigit (*cs))
10475         cs++;
10476       if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10477         {
10478           /* Not recognized as GCC.  */
10479         }
10480       else
10481         {
10482           cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10483           cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10484         }
10485     }
10486   else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10487     cu->producer_is_icc = 1;
10488   else
10489     {
10490       /* For other non-GCC compilers, expect their behavior is DWARF version
10491          compliant.  */
10492     }
10493
10494   cu->checked_producer = 1;
10495 }
10496
10497 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10498    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10499    during 4.6.0 experimental.  */
10500
10501 static int
10502 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10503 {
10504   if (!cu->checked_producer)
10505     check_producer (cu);
10506
10507   return cu->producer_is_gxx_lt_4_6;
10508 }
10509
10510 /* Return the default accessibility type if it is not overriden by
10511    DW_AT_accessibility.  */
10512
10513 static enum dwarf_access_attribute
10514 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10515 {
10516   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10517     {
10518       /* The default DWARF 2 accessibility for members is public, the default
10519          accessibility for inheritance is private.  */
10520
10521       if (die->tag != DW_TAG_inheritance)
10522         return DW_ACCESS_public;
10523       else
10524         return DW_ACCESS_private;
10525     }
10526   else
10527     {
10528       /* DWARF 3+ defines the default accessibility a different way.  The same
10529          rules apply now for DW_TAG_inheritance as for the members and it only
10530          depends on the container kind.  */
10531
10532       if (die->parent->tag == DW_TAG_class_type)
10533         return DW_ACCESS_private;
10534       else
10535         return DW_ACCESS_public;
10536     }
10537 }
10538
10539 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
10540    offset.  If the attribute was not found return 0, otherwise return
10541    1.  If it was found but could not properly be handled, set *OFFSET
10542    to 0.  */
10543
10544 static int
10545 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10546                              LONGEST *offset)
10547 {
10548   struct attribute *attr;
10549
10550   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10551   if (attr != NULL)
10552     {
10553       *offset = 0;
10554
10555       /* Note that we do not check for a section offset first here.
10556          This is because DW_AT_data_member_location is new in DWARF 4,
10557          so if we see it, we can assume that a constant form is really
10558          a constant and not a section offset.  */
10559       if (attr_form_is_constant (attr))
10560         *offset = dwarf2_get_attr_constant_value (attr, 0);
10561       else if (attr_form_is_section_offset (attr))
10562         dwarf2_complex_location_expr_complaint ();
10563       else if (attr_form_is_block (attr))
10564         *offset = decode_locdesc (DW_BLOCK (attr), cu);
10565       else
10566         dwarf2_complex_location_expr_complaint ();
10567
10568       return 1;
10569     }
10570
10571   return 0;
10572 }
10573
10574 /* Add an aggregate field to the field list.  */
10575
10576 static void
10577 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10578                   struct dwarf2_cu *cu)
10579 {
10580   struct objfile *objfile = cu->objfile;
10581   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10582   struct nextfield *new_field;
10583   struct attribute *attr;
10584   struct field *fp;
10585   const char *fieldname = "";
10586
10587   /* Allocate a new field list entry and link it in.  */
10588   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10589   make_cleanup (xfree, new_field);
10590   memset (new_field, 0, sizeof (struct nextfield));
10591
10592   if (die->tag == DW_TAG_inheritance)
10593     {
10594       new_field->next = fip->baseclasses;
10595       fip->baseclasses = new_field;
10596     }
10597   else
10598     {
10599       new_field->next = fip->fields;
10600       fip->fields = new_field;
10601     }
10602   fip->nfields++;
10603
10604   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10605   if (attr)
10606     new_field->accessibility = DW_UNSND (attr);
10607   else
10608     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10609   if (new_field->accessibility != DW_ACCESS_public)
10610     fip->non_public_fields = 1;
10611
10612   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10613   if (attr)
10614     new_field->virtuality = DW_UNSND (attr);
10615   else
10616     new_field->virtuality = DW_VIRTUALITY_none;
10617
10618   fp = &new_field->field;
10619
10620   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10621     {
10622       LONGEST offset;
10623
10624       /* Data member other than a C++ static data member.  */
10625
10626       /* Get type of field.  */
10627       fp->type = die_type (die, cu);
10628
10629       SET_FIELD_BITPOS (*fp, 0);
10630
10631       /* Get bit size of field (zero if none).  */
10632       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10633       if (attr)
10634         {
10635           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10636         }
10637       else
10638         {
10639           FIELD_BITSIZE (*fp) = 0;
10640         }
10641
10642       /* Get bit offset of field.  */
10643       if (handle_data_member_location (die, cu, &offset))
10644         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10645       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10646       if (attr)
10647         {
10648           if (gdbarch_bits_big_endian (gdbarch))
10649             {
10650               /* For big endian bits, the DW_AT_bit_offset gives the
10651                  additional bit offset from the MSB of the containing
10652                  anonymous object to the MSB of the field.  We don't
10653                  have to do anything special since we don't need to
10654                  know the size of the anonymous object.  */
10655               SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10656             }
10657           else
10658             {
10659               /* For little endian bits, compute the bit offset to the
10660                  MSB of the anonymous object, subtract off the number of
10661                  bits from the MSB of the field to the MSB of the
10662                  object, and then subtract off the number of bits of
10663                  the field itself.  The result is the bit offset of
10664                  the LSB of the field.  */
10665               int anonymous_size;
10666               int bit_offset = DW_UNSND (attr);
10667
10668               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10669               if (attr)
10670                 {
10671                   /* The size of the anonymous object containing
10672                      the bit field is explicit, so use the
10673                      indicated size (in bytes).  */
10674                   anonymous_size = DW_UNSND (attr);
10675                 }
10676               else
10677                 {
10678                   /* The size of the anonymous object containing
10679                      the bit field must be inferred from the type
10680                      attribute of the data member containing the
10681                      bit field.  */
10682                   anonymous_size = TYPE_LENGTH (fp->type);
10683                 }
10684               SET_FIELD_BITPOS (*fp,
10685                                 (FIELD_BITPOS (*fp)
10686                                  + anonymous_size * bits_per_byte
10687                                  - bit_offset - FIELD_BITSIZE (*fp)));
10688             }
10689         }
10690
10691       /* Get name of field.  */
10692       fieldname = dwarf2_name (die, cu);
10693       if (fieldname == NULL)
10694         fieldname = "";
10695
10696       /* The name is already allocated along with this objfile, so we don't
10697          need to duplicate it for the type.  */
10698       fp->name = fieldname;
10699
10700       /* Change accessibility for artificial fields (e.g. virtual table
10701          pointer or virtual base class pointer) to private.  */
10702       if (dwarf2_attr (die, DW_AT_artificial, cu))
10703         {
10704           FIELD_ARTIFICIAL (*fp) = 1;
10705           new_field->accessibility = DW_ACCESS_private;
10706           fip->non_public_fields = 1;
10707         }
10708     }
10709   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10710     {
10711       /* C++ static member.  */
10712
10713       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10714          is a declaration, but all versions of G++ as of this writing
10715          (so through at least 3.2.1) incorrectly generate
10716          DW_TAG_variable tags.  */
10717
10718       const char *physname;
10719
10720       /* Get name of field.  */
10721       fieldname = dwarf2_name (die, cu);
10722       if (fieldname == NULL)
10723         return;
10724
10725       attr = dwarf2_attr (die, DW_AT_const_value, cu);
10726       if (attr
10727           /* Only create a symbol if this is an external value.
10728              new_symbol checks this and puts the value in the global symbol
10729              table, which we want.  If it is not external, new_symbol
10730              will try to put the value in cu->list_in_scope which is wrong.  */
10731           && dwarf2_flag_true_p (die, DW_AT_external, cu))
10732         {
10733           /* A static const member, not much different than an enum as far as
10734              we're concerned, except that we can support more types.  */
10735           new_symbol (die, NULL, cu);
10736         }
10737
10738       /* Get physical name.  */
10739       physname = dwarf2_physname (fieldname, die, cu);
10740
10741       /* The name is already allocated along with this objfile, so we don't
10742          need to duplicate it for the type.  */
10743       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10744       FIELD_TYPE (*fp) = die_type (die, cu);
10745       FIELD_NAME (*fp) = fieldname;
10746     }
10747   else if (die->tag == DW_TAG_inheritance)
10748     {
10749       LONGEST offset;
10750
10751       /* C++ base class field.  */
10752       if (handle_data_member_location (die, cu, &offset))
10753         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10754       FIELD_BITSIZE (*fp) = 0;
10755       FIELD_TYPE (*fp) = die_type (die, cu);
10756       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10757       fip->nbaseclasses++;
10758     }
10759 }
10760
10761 /* Add a typedef defined in the scope of the FIP's class.  */
10762
10763 static void
10764 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10765                     struct dwarf2_cu *cu)
10766 {
10767   struct objfile *objfile = cu->objfile;
10768   struct typedef_field_list *new_field;
10769   struct attribute *attr;
10770   struct typedef_field *fp;
10771   char *fieldname = "";
10772
10773   /* Allocate a new field list entry and link it in.  */
10774   new_field = xzalloc (sizeof (*new_field));
10775   make_cleanup (xfree, new_field);
10776
10777   gdb_assert (die->tag == DW_TAG_typedef);
10778
10779   fp = &new_field->field;
10780
10781   /* Get name of field.  */
10782   fp->name = dwarf2_name (die, cu);
10783   if (fp->name == NULL)
10784     return;
10785
10786   fp->type = read_type_die (die, cu);
10787
10788   new_field->next = fip->typedef_field_list;
10789   fip->typedef_field_list = new_field;
10790   fip->typedef_field_list_count++;
10791 }
10792
10793 /* Create the vector of fields, and attach it to the type.  */
10794
10795 static void
10796 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10797                               struct dwarf2_cu *cu)
10798 {
10799   int nfields = fip->nfields;
10800
10801   /* Record the field count, allocate space for the array of fields,
10802      and create blank accessibility bitfields if necessary.  */
10803   TYPE_NFIELDS (type) = nfields;
10804   TYPE_FIELDS (type) = (struct field *)
10805     TYPE_ALLOC (type, sizeof (struct field) * nfields);
10806   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10807
10808   if (fip->non_public_fields && cu->language != language_ada)
10809     {
10810       ALLOCATE_CPLUS_STRUCT_TYPE (type);
10811
10812       TYPE_FIELD_PRIVATE_BITS (type) =
10813         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10814       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10815
10816       TYPE_FIELD_PROTECTED_BITS (type) =
10817         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10818       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10819
10820       TYPE_FIELD_IGNORE_BITS (type) =
10821         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10822       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10823     }
10824
10825   /* If the type has baseclasses, allocate and clear a bit vector for
10826      TYPE_FIELD_VIRTUAL_BITS.  */
10827   if (fip->nbaseclasses && cu->language != language_ada)
10828     {
10829       int num_bytes = B_BYTES (fip->nbaseclasses);
10830       unsigned char *pointer;
10831
10832       ALLOCATE_CPLUS_STRUCT_TYPE (type);
10833       pointer = TYPE_ALLOC (type, num_bytes);
10834       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10835       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10836       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10837     }
10838
10839   /* Copy the saved-up fields into the field vector.  Start from the head of
10840      the list, adding to the tail of the field array, so that they end up in
10841      the same order in the array in which they were added to the list.  */
10842   while (nfields-- > 0)
10843     {
10844       struct nextfield *fieldp;
10845
10846       if (fip->fields)
10847         {
10848           fieldp = fip->fields;
10849           fip->fields = fieldp->next;
10850         }
10851       else
10852         {
10853           fieldp = fip->baseclasses;
10854           fip->baseclasses = fieldp->next;
10855         }
10856
10857       TYPE_FIELD (type, nfields) = fieldp->field;
10858       switch (fieldp->accessibility)
10859         {
10860         case DW_ACCESS_private:
10861           if (cu->language != language_ada)
10862             SET_TYPE_FIELD_PRIVATE (type, nfields);
10863           break;
10864
10865         case DW_ACCESS_protected:
10866           if (cu->language != language_ada)
10867             SET_TYPE_FIELD_PROTECTED (type, nfields);
10868           break;
10869
10870         case DW_ACCESS_public:
10871           break;
10872
10873         default:
10874           /* Unknown accessibility.  Complain and treat it as public.  */
10875           {
10876             complaint (&symfile_complaints, _("unsupported accessibility %d"),
10877                        fieldp->accessibility);
10878           }
10879           break;
10880         }
10881       if (nfields < fip->nbaseclasses)
10882         {
10883           switch (fieldp->virtuality)
10884             {
10885             case DW_VIRTUALITY_virtual:
10886             case DW_VIRTUALITY_pure_virtual:
10887               if (cu->language == language_ada)
10888                 error (_("unexpected virtuality in component of Ada type"));
10889               SET_TYPE_FIELD_VIRTUAL (type, nfields);
10890               break;
10891             }
10892         }
10893     }
10894 }
10895
10896 /* Return true if this member function is a constructor, false
10897    otherwise.  */
10898
10899 static int
10900 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10901 {
10902   const char *fieldname;
10903   const char *typename;
10904   int len;
10905
10906   if (die->parent == NULL)
10907     return 0;
10908
10909   if (die->parent->tag != DW_TAG_structure_type
10910       && die->parent->tag != DW_TAG_union_type
10911       && die->parent->tag != DW_TAG_class_type)
10912     return 0;
10913
10914   fieldname = dwarf2_name (die, cu);
10915   typename = dwarf2_name (die->parent, cu);
10916   if (fieldname == NULL || typename == NULL)
10917     return 0;
10918
10919   len = strlen (fieldname);
10920   return (strncmp (fieldname, typename, len) == 0
10921           && (typename[len] == '\0' || typename[len] == '<'));
10922 }
10923
10924 /* Add a member function to the proper fieldlist.  */
10925
10926 static void
10927 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10928                       struct type *type, struct dwarf2_cu *cu)
10929 {
10930   struct objfile *objfile = cu->objfile;
10931   struct attribute *attr;
10932   struct fnfieldlist *flp;
10933   int i;
10934   struct fn_field *fnp;
10935   const char *fieldname;
10936   struct nextfnfield *new_fnfield;
10937   struct type *this_type;
10938   enum dwarf_access_attribute accessibility;
10939
10940   if (cu->language == language_ada)
10941     error (_("unexpected member function in Ada type"));
10942
10943   /* Get name of member function.  */
10944   fieldname = dwarf2_name (die, cu);
10945   if (fieldname == NULL)
10946     return;
10947
10948   /* Look up member function name in fieldlist.  */
10949   for (i = 0; i < fip->nfnfields; i++)
10950     {
10951       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10952         break;
10953     }
10954
10955   /* Create new list element if necessary.  */
10956   if (i < fip->nfnfields)
10957     flp = &fip->fnfieldlists[i];
10958   else
10959     {
10960       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10961         {
10962           fip->fnfieldlists = (struct fnfieldlist *)
10963             xrealloc (fip->fnfieldlists,
10964                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10965                       * sizeof (struct fnfieldlist));
10966           if (fip->nfnfields == 0)
10967             make_cleanup (free_current_contents, &fip->fnfieldlists);
10968         }
10969       flp = &fip->fnfieldlists[fip->nfnfields];
10970       flp->name = fieldname;
10971       flp->length = 0;
10972       flp->head = NULL;
10973       i = fip->nfnfields++;
10974     }
10975
10976   /* Create a new member function field and chain it to the field list
10977      entry.  */
10978   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10979   make_cleanup (xfree, new_fnfield);
10980   memset (new_fnfield, 0, sizeof (struct nextfnfield));
10981   new_fnfield->next = flp->head;
10982   flp->head = new_fnfield;
10983   flp->length++;
10984
10985   /* Fill in the member function field info.  */
10986   fnp = &new_fnfield->fnfield;
10987
10988   /* Delay processing of the physname until later.  */
10989   if (cu->language == language_cplus || cu->language == language_java)
10990     {
10991       add_to_method_list (type, i, flp->length - 1, fieldname,
10992                           die, cu);
10993     }
10994   else
10995     {
10996       const char *physname = dwarf2_physname (fieldname, die, cu);
10997       fnp->physname = physname ? physname : "";
10998     }
10999
11000   fnp->type = alloc_type (objfile);
11001   this_type = read_type_die (die, cu);
11002   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
11003     {
11004       int nparams = TYPE_NFIELDS (this_type);
11005
11006       /* TYPE is the domain of this method, and THIS_TYPE is the type
11007            of the method itself (TYPE_CODE_METHOD).  */
11008       smash_to_method_type (fnp->type, type,
11009                             TYPE_TARGET_TYPE (this_type),
11010                             TYPE_FIELDS (this_type),
11011                             TYPE_NFIELDS (this_type),
11012                             TYPE_VARARGS (this_type));
11013
11014       /* Handle static member functions.
11015          Dwarf2 has no clean way to discern C++ static and non-static
11016          member functions.  G++ helps GDB by marking the first
11017          parameter for non-static member functions (which is the this
11018          pointer) as artificial.  We obtain this information from
11019          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
11020       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11021         fnp->voffset = VOFFSET_STATIC;
11022     }
11023   else
11024     complaint (&symfile_complaints, _("member function type missing for '%s'"),
11025                dwarf2_full_name (fieldname, die, cu));
11026
11027   /* Get fcontext from DW_AT_containing_type if present.  */
11028   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11029     fnp->fcontext = die_containing_type (die, cu);
11030
11031   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11032      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
11033
11034   /* Get accessibility.  */
11035   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11036   if (attr)
11037     accessibility = DW_UNSND (attr);
11038   else
11039     accessibility = dwarf2_default_access_attribute (die, cu);
11040   switch (accessibility)
11041     {
11042     case DW_ACCESS_private:
11043       fnp->is_private = 1;
11044       break;
11045     case DW_ACCESS_protected:
11046       fnp->is_protected = 1;
11047       break;
11048     }
11049
11050   /* Check for artificial methods.  */
11051   attr = dwarf2_attr (die, DW_AT_artificial, cu);
11052   if (attr && DW_UNSND (attr) != 0)
11053     fnp->is_artificial = 1;
11054
11055   fnp->is_constructor = dwarf2_is_constructor (die, cu);
11056
11057   /* Get index in virtual function table if it is a virtual member
11058      function.  For older versions of GCC, this is an offset in the
11059      appropriate virtual table, as specified by DW_AT_containing_type.
11060      For everyone else, it is an expression to be evaluated relative
11061      to the object address.  */
11062
11063   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11064   if (attr)
11065     {
11066       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11067         {
11068           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11069             {
11070               /* Old-style GCC.  */
11071               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11072             }
11073           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11074                    || (DW_BLOCK (attr)->size > 1
11075                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11076                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11077             {
11078               struct dwarf_block blk;
11079               int offset;
11080
11081               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11082                         ? 1 : 2);
11083               blk.size = DW_BLOCK (attr)->size - offset;
11084               blk.data = DW_BLOCK (attr)->data + offset;
11085               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11086               if ((fnp->voffset % cu->header.addr_size) != 0)
11087                 dwarf2_complex_location_expr_complaint ();
11088               else
11089                 fnp->voffset /= cu->header.addr_size;
11090               fnp->voffset += 2;
11091             }
11092           else
11093             dwarf2_complex_location_expr_complaint ();
11094
11095           if (!fnp->fcontext)
11096             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11097         }
11098       else if (attr_form_is_section_offset (attr))
11099         {
11100           dwarf2_complex_location_expr_complaint ();
11101         }
11102       else
11103         {
11104           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11105                                                  fieldname);
11106         }
11107     }
11108   else
11109     {
11110       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11111       if (attr && DW_UNSND (attr))
11112         {
11113           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
11114           complaint (&symfile_complaints,
11115                      _("Member function \"%s\" (offset %d) is virtual "
11116                        "but the vtable offset is not specified"),
11117                      fieldname, die->offset.sect_off);
11118           ALLOCATE_CPLUS_STRUCT_TYPE (type);
11119           TYPE_CPLUS_DYNAMIC (type) = 1;
11120         }
11121     }
11122 }
11123
11124 /* Create the vector of member function fields, and attach it to the type.  */
11125
11126 static void
11127 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11128                                  struct dwarf2_cu *cu)
11129 {
11130   struct fnfieldlist *flp;
11131   int i;
11132
11133   if (cu->language == language_ada)
11134     error (_("unexpected member functions in Ada type"));
11135
11136   ALLOCATE_CPLUS_STRUCT_TYPE (type);
11137   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11138     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11139
11140   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11141     {
11142       struct nextfnfield *nfp = flp->head;
11143       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11144       int k;
11145
11146       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11147       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11148       fn_flp->fn_fields = (struct fn_field *)
11149         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11150       for (k = flp->length; (k--, nfp); nfp = nfp->next)
11151         fn_flp->fn_fields[k] = nfp->fnfield;
11152     }
11153
11154   TYPE_NFN_FIELDS (type) = fip->nfnfields;
11155 }
11156
11157 /* Returns non-zero if NAME is the name of a vtable member in CU's
11158    language, zero otherwise.  */
11159 static int
11160 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11161 {
11162   static const char vptr[] = "_vptr";
11163   static const char vtable[] = "vtable";
11164
11165   /* Look for the C++ and Java forms of the vtable.  */
11166   if ((cu->language == language_java
11167        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11168        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11169        && is_cplus_marker (name[sizeof (vptr) - 1])))
11170     return 1;
11171
11172   return 0;
11173 }
11174
11175 /* GCC outputs unnamed structures that are really pointers to member
11176    functions, with the ABI-specified layout.  If TYPE describes
11177    such a structure, smash it into a member function type.
11178
11179    GCC shouldn't do this; it should just output pointer to member DIEs.
11180    This is GCC PR debug/28767.  */
11181
11182 static void
11183 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11184 {
11185   struct type *pfn_type, *domain_type, *new_type;
11186
11187   /* Check for a structure with no name and two children.  */
11188   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11189     return;
11190
11191   /* Check for __pfn and __delta members.  */
11192   if (TYPE_FIELD_NAME (type, 0) == NULL
11193       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11194       || TYPE_FIELD_NAME (type, 1) == NULL
11195       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11196     return;
11197
11198   /* Find the type of the method.  */
11199   pfn_type = TYPE_FIELD_TYPE (type, 0);
11200   if (pfn_type == NULL
11201       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11202       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11203     return;
11204
11205   /* Look for the "this" argument.  */
11206   pfn_type = TYPE_TARGET_TYPE (pfn_type);
11207   if (TYPE_NFIELDS (pfn_type) == 0
11208       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11209       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11210     return;
11211
11212   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11213   new_type = alloc_type (objfile);
11214   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11215                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11216                         TYPE_VARARGS (pfn_type));
11217   smash_to_methodptr_type (type, new_type);
11218 }
11219
11220 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11221    (icc).  */
11222
11223 static int
11224 producer_is_icc (struct dwarf2_cu *cu)
11225 {
11226   if (!cu->checked_producer)
11227     check_producer (cu);
11228
11229   return cu->producer_is_icc;
11230 }
11231
11232 /* Called when we find the DIE that starts a structure or union scope
11233    (definition) to create a type for the structure or union.  Fill in
11234    the type's name and general properties; the members will not be
11235    processed until process_structure_type.
11236
11237    NOTE: we need to call these functions regardless of whether or not the
11238    DIE has a DW_AT_name attribute, since it might be an anonymous
11239    structure or union.  This gets the type entered into our set of
11240    user defined types.
11241
11242    However, if the structure is incomplete (an opaque struct/union)
11243    then suppress creating a symbol table entry for it since gdb only
11244    wants to find the one with the complete definition.  Note that if
11245    it is complete, we just call new_symbol, which does it's own
11246    checking about whether the struct/union is anonymous or not (and
11247    suppresses creating a symbol table entry itself).  */
11248
11249 static struct type *
11250 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11251 {
11252   struct objfile *objfile = cu->objfile;
11253   struct type *type;
11254   struct attribute *attr;
11255   const char *name;
11256
11257   /* If the definition of this type lives in .debug_types, read that type.
11258      Don't follow DW_AT_specification though, that will take us back up
11259      the chain and we want to go down.  */
11260   attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11261   if (attr)
11262     {
11263       struct dwarf2_cu *type_cu = cu;
11264       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11265
11266       /* We could just recurse on read_structure_type, but we need to call
11267          get_die_type to ensure only one type for this DIE is created.
11268          This is important, for example, because for c++ classes we need
11269          TYPE_NAME set which is only done by new_symbol.  Blech.  */
11270       type = read_type_die (type_die, type_cu);
11271
11272       /* TYPE_CU may not be the same as CU.
11273          Ensure TYPE is recorded in CU's type_hash table.  */
11274       return set_die_type (die, type, cu);
11275     }
11276
11277   type = alloc_type (objfile);
11278   INIT_CPLUS_SPECIFIC (type);
11279
11280   name = dwarf2_name (die, cu);
11281   if (name != NULL)
11282     {
11283       if (cu->language == language_cplus
11284           || cu->language == language_java)
11285         {
11286           const char *full_name = dwarf2_full_name (name, die, cu);
11287
11288           /* dwarf2_full_name might have already finished building the DIE's
11289              type.  If so, there is no need to continue.  */
11290           if (get_die_type (die, cu) != NULL)
11291             return get_die_type (die, cu);
11292
11293           TYPE_TAG_NAME (type) = full_name;
11294           if (die->tag == DW_TAG_structure_type
11295               || die->tag == DW_TAG_class_type)
11296             TYPE_NAME (type) = TYPE_TAG_NAME (type);
11297         }
11298       else
11299         {
11300           /* The name is already allocated along with this objfile, so
11301              we don't need to duplicate it for the type.  */
11302           TYPE_TAG_NAME (type) = name;
11303           if (die->tag == DW_TAG_class_type)
11304             TYPE_NAME (type) = TYPE_TAG_NAME (type);
11305         }
11306     }
11307
11308   if (die->tag == DW_TAG_structure_type)
11309     {
11310       TYPE_CODE (type) = TYPE_CODE_STRUCT;
11311     }
11312   else if (die->tag == DW_TAG_union_type)
11313     {
11314       TYPE_CODE (type) = TYPE_CODE_UNION;
11315     }
11316   else
11317     {
11318       TYPE_CODE (type) = TYPE_CODE_CLASS;
11319     }
11320
11321   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11322     TYPE_DECLARED_CLASS (type) = 1;
11323
11324   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11325   if (attr)
11326     {
11327       TYPE_LENGTH (type) = DW_UNSND (attr);
11328     }
11329   else
11330     {
11331       TYPE_LENGTH (type) = 0;
11332     }
11333
11334   if (producer_is_icc (cu))
11335     {
11336       /* ICC does not output the required DW_AT_declaration
11337          on incomplete types, but gives them a size of zero.  */
11338     }
11339   else
11340     TYPE_STUB_SUPPORTED (type) = 1;
11341
11342   if (die_is_declaration (die, cu))
11343     TYPE_STUB (type) = 1;
11344   else if (attr == NULL && die->child == NULL
11345            && producer_is_realview (cu->producer))
11346     /* RealView does not output the required DW_AT_declaration
11347        on incomplete types.  */
11348     TYPE_STUB (type) = 1;
11349
11350   /* We need to add the type field to the die immediately so we don't
11351      infinitely recurse when dealing with pointers to the structure
11352      type within the structure itself.  */
11353   set_die_type (die, type, cu);
11354
11355   /* set_die_type should be already done.  */
11356   set_descriptive_type (type, die, cu);
11357
11358   return type;
11359 }
11360
11361 /* Finish creating a structure or union type, including filling in
11362    its members and creating a symbol for it.  */
11363
11364 static void
11365 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11366 {
11367   struct objfile *objfile = cu->objfile;
11368   struct die_info *child_die = die->child;
11369   struct type *type;
11370
11371   type = get_die_type (die, cu);
11372   if (type == NULL)
11373     type = read_structure_type (die, cu);
11374
11375   if (die->child != NULL && ! die_is_declaration (die, cu))
11376     {
11377       struct field_info fi;
11378       struct die_info *child_die;
11379       VEC (symbolp) *template_args = NULL;
11380       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11381
11382       memset (&fi, 0, sizeof (struct field_info));
11383
11384       child_die = die->child;
11385
11386       while (child_die && child_die->tag)
11387         {
11388           if (child_die->tag == DW_TAG_member
11389               || child_die->tag == DW_TAG_variable)
11390             {
11391               /* NOTE: carlton/2002-11-05: A C++ static data member
11392                  should be a DW_TAG_member that is a declaration, but
11393                  all versions of G++ as of this writing (so through at
11394                  least 3.2.1) incorrectly generate DW_TAG_variable
11395                  tags for them instead.  */
11396               dwarf2_add_field (&fi, child_die, cu);
11397             }
11398           else if (child_die->tag == DW_TAG_subprogram)
11399             {
11400               /* C++ member function.  */
11401               dwarf2_add_member_fn (&fi, child_die, type, cu);
11402             }
11403           else if (child_die->tag == DW_TAG_inheritance)
11404             {
11405               /* C++ base class field.  */
11406               dwarf2_add_field (&fi, child_die, cu);
11407             }
11408           else if (child_die->tag == DW_TAG_typedef)
11409             dwarf2_add_typedef (&fi, child_die, cu);
11410           else if (child_die->tag == DW_TAG_template_type_param
11411                    || child_die->tag == DW_TAG_template_value_param)
11412             {
11413               struct symbol *arg = new_symbol (child_die, NULL, cu);
11414
11415               if (arg != NULL)
11416                 VEC_safe_push (symbolp, template_args, arg);
11417             }
11418
11419           child_die = sibling_die (child_die);
11420         }
11421
11422       /* Attach template arguments to type.  */
11423       if (! VEC_empty (symbolp, template_args))
11424         {
11425           ALLOCATE_CPLUS_STRUCT_TYPE (type);
11426           TYPE_N_TEMPLATE_ARGUMENTS (type)
11427             = VEC_length (symbolp, template_args);
11428           TYPE_TEMPLATE_ARGUMENTS (type)
11429             = obstack_alloc (&objfile->objfile_obstack,
11430                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
11431                               * sizeof (struct symbol *)));
11432           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11433                   VEC_address (symbolp, template_args),
11434                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
11435                    * sizeof (struct symbol *)));
11436           VEC_free (symbolp, template_args);
11437         }
11438
11439       /* Attach fields and member functions to the type.  */
11440       if (fi.nfields)
11441         dwarf2_attach_fields_to_type (&fi, type, cu);
11442       if (fi.nfnfields)
11443         {
11444           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11445
11446           /* Get the type which refers to the base class (possibly this
11447              class itself) which contains the vtable pointer for the current
11448              class from the DW_AT_containing_type attribute.  This use of
11449              DW_AT_containing_type is a GNU extension.  */
11450
11451           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11452             {
11453               struct type *t = die_containing_type (die, cu);
11454
11455               TYPE_VPTR_BASETYPE (type) = t;
11456               if (type == t)
11457                 {
11458                   int i;
11459
11460                   /* Our own class provides vtbl ptr.  */
11461                   for (i = TYPE_NFIELDS (t) - 1;
11462                        i >= TYPE_N_BASECLASSES (t);
11463                        --i)
11464                     {
11465                       const char *fieldname = TYPE_FIELD_NAME (t, i);
11466
11467                       if (is_vtable_name (fieldname, cu))
11468                         {
11469                           TYPE_VPTR_FIELDNO (type) = i;
11470                           break;
11471                         }
11472                     }
11473
11474                   /* Complain if virtual function table field not found.  */
11475                   if (i < TYPE_N_BASECLASSES (t))
11476                     complaint (&symfile_complaints,
11477                                _("virtual function table pointer "
11478                                  "not found when defining class '%s'"),
11479                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11480                                "");
11481                 }
11482               else
11483                 {
11484                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11485                 }
11486             }
11487           else if (cu->producer
11488                    && strncmp (cu->producer,
11489                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11490             {
11491               /* The IBM XLC compiler does not provide direct indication
11492                  of the containing type, but the vtable pointer is
11493                  always named __vfp.  */
11494
11495               int i;
11496
11497               for (i = TYPE_NFIELDS (type) - 1;
11498                    i >= TYPE_N_BASECLASSES (type);
11499                    --i)
11500                 {
11501                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11502                     {
11503                       TYPE_VPTR_FIELDNO (type) = i;
11504                       TYPE_VPTR_BASETYPE (type) = type;
11505                       break;
11506                     }
11507                 }
11508             }
11509         }
11510
11511       /* Copy fi.typedef_field_list linked list elements content into the
11512          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
11513       if (fi.typedef_field_list)
11514         {
11515           int i = fi.typedef_field_list_count;
11516
11517           ALLOCATE_CPLUS_STRUCT_TYPE (type);
11518           TYPE_TYPEDEF_FIELD_ARRAY (type)
11519             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11520           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11521
11522           /* Reverse the list order to keep the debug info elements order.  */
11523           while (--i >= 0)
11524             {
11525               struct typedef_field *dest, *src;
11526
11527               dest = &TYPE_TYPEDEF_FIELD (type, i);
11528               src = &fi.typedef_field_list->field;
11529               fi.typedef_field_list = fi.typedef_field_list->next;
11530               *dest = *src;
11531             }
11532         }
11533
11534       do_cleanups (back_to);
11535
11536       if (HAVE_CPLUS_STRUCT (type))
11537         TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11538     }
11539
11540   quirk_gcc_member_function_pointer (type, objfile);
11541
11542   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11543      snapshots) has been known to create a die giving a declaration
11544      for a class that has, as a child, a die giving a definition for a
11545      nested class.  So we have to process our children even if the
11546      current die is a declaration.  Normally, of course, a declaration
11547      won't have any children at all.  */
11548
11549   while (child_die != NULL && child_die->tag)
11550     {
11551       if (child_die->tag == DW_TAG_member
11552           || child_die->tag == DW_TAG_variable
11553           || child_die->tag == DW_TAG_inheritance
11554           || child_die->tag == DW_TAG_template_value_param
11555           || child_die->tag == DW_TAG_template_type_param)
11556         {
11557           /* Do nothing.  */
11558         }
11559       else
11560         process_die (child_die, cu);
11561
11562       child_die = sibling_die (child_die);
11563     }
11564
11565   /* Do not consider external references.  According to the DWARF standard,
11566      these DIEs are identified by the fact that they have no byte_size
11567      attribute, and a declaration attribute.  */
11568   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11569       || !die_is_declaration (die, cu))
11570     new_symbol (die, type, cu);
11571 }
11572
11573 /* Given a DW_AT_enumeration_type die, set its type.  We do not
11574    complete the type's fields yet, or create any symbols.  */
11575
11576 static struct type *
11577 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11578 {
11579   struct objfile *objfile = cu->objfile;
11580   struct type *type;
11581   struct attribute *attr;
11582   const char *name;
11583
11584   /* If the definition of this type lives in .debug_types, read that type.
11585      Don't follow DW_AT_specification though, that will take us back up
11586      the chain and we want to go down.  */
11587   attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11588   if (attr)
11589     {
11590       struct dwarf2_cu *type_cu = cu;
11591       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11592
11593       type = read_type_die (type_die, type_cu);
11594
11595       /* TYPE_CU may not be the same as CU.
11596          Ensure TYPE is recorded in CU's type_hash table.  */
11597       return set_die_type (die, type, cu);
11598     }
11599
11600   type = alloc_type (objfile);
11601
11602   TYPE_CODE (type) = TYPE_CODE_ENUM;
11603   name = dwarf2_full_name (NULL, die, cu);
11604   if (name != NULL)
11605     TYPE_TAG_NAME (type) = name;
11606
11607   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11608   if (attr)
11609     {
11610       TYPE_LENGTH (type) = DW_UNSND (attr);
11611     }
11612   else
11613     {
11614       TYPE_LENGTH (type) = 0;
11615     }
11616
11617   /* The enumeration DIE can be incomplete.  In Ada, any type can be
11618      declared as private in the package spec, and then defined only
11619      inside the package body.  Such types are known as Taft Amendment
11620      Types.  When another package uses such a type, an incomplete DIE
11621      may be generated by the compiler.  */
11622   if (die_is_declaration (die, cu))
11623     TYPE_STUB (type) = 1;
11624
11625   return set_die_type (die, type, cu);
11626 }
11627
11628 /* Given a pointer to a die which begins an enumeration, process all
11629    the dies that define the members of the enumeration, and create the
11630    symbol for the enumeration type.
11631
11632    NOTE: We reverse the order of the element list.  */
11633
11634 static void
11635 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11636 {
11637   struct type *this_type;
11638
11639   this_type = get_die_type (die, cu);
11640   if (this_type == NULL)
11641     this_type = read_enumeration_type (die, cu);
11642
11643   if (die->child != NULL)
11644     {
11645       struct die_info *child_die;
11646       struct symbol *sym;
11647       struct field *fields = NULL;
11648       int num_fields = 0;
11649       int unsigned_enum = 1;
11650       const char *name;
11651       int flag_enum = 1;
11652       ULONGEST mask = 0;
11653
11654       child_die = die->child;
11655       while (child_die && child_die->tag)
11656         {
11657           if (child_die->tag != DW_TAG_enumerator)
11658             {
11659               process_die (child_die, cu);
11660             }
11661           else
11662             {
11663               name = dwarf2_name (child_die, cu);
11664               if (name)
11665                 {
11666                   sym = new_symbol (child_die, this_type, cu);
11667                   if (SYMBOL_VALUE (sym) < 0)
11668                     {
11669                       unsigned_enum = 0;
11670                       flag_enum = 0;
11671                     }
11672                   else if ((mask & SYMBOL_VALUE (sym)) != 0)
11673                     flag_enum = 0;
11674                   else
11675                     mask |= SYMBOL_VALUE (sym);
11676
11677                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11678                     {
11679                       fields = (struct field *)
11680                         xrealloc (fields,
11681                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
11682                                   * sizeof (struct field));
11683                     }
11684
11685                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11686                   FIELD_TYPE (fields[num_fields]) = NULL;
11687                   SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11688                   FIELD_BITSIZE (fields[num_fields]) = 0;
11689
11690                   num_fields++;
11691                 }
11692             }
11693
11694           child_die = sibling_die (child_die);
11695         }
11696
11697       if (num_fields)
11698         {
11699           TYPE_NFIELDS (this_type) = num_fields;
11700           TYPE_FIELDS (this_type) = (struct field *)
11701             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11702           memcpy (TYPE_FIELDS (this_type), fields,
11703                   sizeof (struct field) * num_fields);
11704           xfree (fields);
11705         }
11706       if (unsigned_enum)
11707         TYPE_UNSIGNED (this_type) = 1;
11708       if (flag_enum)
11709         TYPE_FLAG_ENUM (this_type) = 1;
11710     }
11711
11712   /* If we are reading an enum from a .debug_types unit, and the enum
11713      is a declaration, and the enum is not the signatured type in the
11714      unit, then we do not want to add a symbol for it.  Adding a
11715      symbol would in some cases obscure the true definition of the
11716      enum, giving users an incomplete type when the definition is
11717      actually available.  Note that we do not want to do this for all
11718      enums which are just declarations, because C++0x allows forward
11719      enum declarations.  */
11720   if (cu->per_cu->is_debug_types
11721       && die_is_declaration (die, cu))
11722     {
11723       struct signatured_type *sig_type;
11724
11725       sig_type
11726         = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11727                                             cu->per_cu->section,
11728                                             cu->per_cu->offset);
11729       gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11730       if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11731         return;
11732     }
11733
11734   new_symbol (die, this_type, cu);
11735 }
11736
11737 /* Extract all information from a DW_TAG_array_type DIE and put it in
11738    the DIE's type field.  For now, this only handles one dimensional
11739    arrays.  */
11740
11741 static struct type *
11742 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11743 {
11744   struct objfile *objfile = cu->objfile;
11745   struct die_info *child_die;
11746   struct type *type;
11747   struct type *element_type, *range_type, *index_type;
11748   struct type **range_types = NULL;
11749   struct attribute *attr;
11750   int ndim = 0;
11751   struct cleanup *back_to;
11752   const char *name;
11753
11754   element_type = die_type (die, cu);
11755
11756   /* The die_type call above may have already set the type for this DIE.  */
11757   type = get_die_type (die, cu);
11758   if (type)
11759     return type;
11760
11761   /* Irix 6.2 native cc creates array types without children for
11762      arrays with unspecified length.  */
11763   if (die->child == NULL)
11764     {
11765       index_type = objfile_type (objfile)->builtin_int;
11766       range_type = create_range_type (NULL, index_type, 0, -1);
11767       type = create_array_type (NULL, element_type, range_type);
11768       return set_die_type (die, type, cu);
11769     }
11770
11771   back_to = make_cleanup (null_cleanup, NULL);
11772   child_die = die->child;
11773   while (child_die && child_die->tag)
11774     {
11775       if (child_die->tag == DW_TAG_subrange_type)
11776         {
11777           struct type *child_type = read_type_die (child_die, cu);
11778
11779           if (child_type != NULL)
11780             {
11781               /* The range type was succesfully read.  Save it for the
11782                  array type creation.  */
11783               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11784                 {
11785                   range_types = (struct type **)
11786                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11787                               * sizeof (struct type *));
11788                   if (ndim == 0)
11789                     make_cleanup (free_current_contents, &range_types);
11790                 }
11791               range_types[ndim++] = child_type;
11792             }
11793         }
11794       child_die = sibling_die (child_die);
11795     }
11796
11797   /* Dwarf2 dimensions are output from left to right, create the
11798      necessary array types in backwards order.  */
11799
11800   type = element_type;
11801
11802   if (read_array_order (die, cu) == DW_ORD_col_major)
11803     {
11804       int i = 0;
11805
11806       while (i < ndim)
11807         type = create_array_type (NULL, type, range_types[i++]);
11808     }
11809   else
11810     {
11811       while (ndim-- > 0)
11812         type = create_array_type (NULL, type, range_types[ndim]);
11813     }
11814
11815   /* Understand Dwarf2 support for vector types (like they occur on
11816      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
11817      array type.  This is not part of the Dwarf2/3 standard yet, but a
11818      custom vendor extension.  The main difference between a regular
11819      array and the vector variant is that vectors are passed by value
11820      to functions.  */
11821   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11822   if (attr)
11823     make_vector_type (type);
11824
11825   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
11826      implementation may choose to implement triple vectors using this
11827      attribute.  */
11828   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11829   if (attr)
11830     {
11831       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11832         TYPE_LENGTH (type) = DW_UNSND (attr);
11833       else
11834         complaint (&symfile_complaints,
11835                    _("DW_AT_byte_size for array type smaller "
11836                      "than the total size of elements"));
11837     }
11838
11839   name = dwarf2_name (die, cu);
11840   if (name)
11841     TYPE_NAME (type) = name;
11842
11843   /* Install the type in the die.  */
11844   set_die_type (die, type, cu);
11845
11846   /* set_die_type should be already done.  */
11847   set_descriptive_type (type, die, cu);
11848
11849   do_cleanups (back_to);
11850
11851   return type;
11852 }
11853
11854 static enum dwarf_array_dim_ordering
11855 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11856 {
11857   struct attribute *attr;
11858
11859   attr = dwarf2_attr (die, DW_AT_ordering, cu);
11860
11861   if (attr) return DW_SND (attr);
11862
11863   /* GNU F77 is a special case, as at 08/2004 array type info is the
11864      opposite order to the dwarf2 specification, but data is still
11865      laid out as per normal fortran.
11866
11867      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11868      version checking.  */
11869
11870   if (cu->language == language_fortran
11871       && cu->producer && strstr (cu->producer, "GNU F77"))
11872     {
11873       return DW_ORD_row_major;
11874     }
11875
11876   switch (cu->language_defn->la_array_ordering)
11877     {
11878     case array_column_major:
11879       return DW_ORD_col_major;
11880     case array_row_major:
11881     default:
11882       return DW_ORD_row_major;
11883     };
11884 }
11885
11886 /* Extract all information from a DW_TAG_set_type DIE and put it in
11887    the DIE's type field.  */
11888
11889 static struct type *
11890 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11891 {
11892   struct type *domain_type, *set_type;
11893   struct attribute *attr;
11894
11895   domain_type = die_type (die, cu);
11896
11897   /* The die_type call above may have already set the type for this DIE.  */
11898   set_type = get_die_type (die, cu);
11899   if (set_type)
11900     return set_type;
11901
11902   set_type = create_set_type (NULL, domain_type);
11903
11904   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11905   if (attr)
11906     TYPE_LENGTH (set_type) = DW_UNSND (attr);
11907
11908   return set_die_type (die, set_type, cu);
11909 }
11910
11911 /* A helper for read_common_block that creates a locexpr baton.
11912    SYM is the symbol which we are marking as computed.
11913    COMMON_DIE is the DIE for the common block.
11914    COMMON_LOC is the location expression attribute for the common
11915    block itself.
11916    MEMBER_LOC is the location expression attribute for the particular
11917    member of the common block that we are processing.
11918    CU is the CU from which the above come.  */
11919
11920 static void
11921 mark_common_block_symbol_computed (struct symbol *sym,
11922                                    struct die_info *common_die,
11923                                    struct attribute *common_loc,
11924                                    struct attribute *member_loc,
11925                                    struct dwarf2_cu *cu)
11926 {
11927   struct objfile *objfile = dwarf2_per_objfile->objfile;
11928   struct dwarf2_locexpr_baton *baton;
11929   gdb_byte *ptr;
11930   unsigned int cu_off;
11931   enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11932   LONGEST offset = 0;
11933
11934   gdb_assert (common_loc && member_loc);
11935   gdb_assert (attr_form_is_block (common_loc));
11936   gdb_assert (attr_form_is_block (member_loc)
11937               || attr_form_is_constant (member_loc));
11938
11939   baton = obstack_alloc (&objfile->objfile_obstack,
11940                          sizeof (struct dwarf2_locexpr_baton));
11941   baton->per_cu = cu->per_cu;
11942   gdb_assert (baton->per_cu);
11943
11944   baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11945
11946   if (attr_form_is_constant (member_loc))
11947     {
11948       offset = dwarf2_get_attr_constant_value (member_loc, 0);
11949       baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11950     }
11951   else
11952     baton->size += DW_BLOCK (member_loc)->size;
11953
11954   ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11955   baton->data = ptr;
11956
11957   *ptr++ = DW_OP_call4;
11958   cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11959   store_unsigned_integer (ptr, 4, byte_order, cu_off);
11960   ptr += 4;
11961
11962   if (attr_form_is_constant (member_loc))
11963     {
11964       *ptr++ = DW_OP_addr;
11965       store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11966       ptr += cu->header.addr_size;
11967     }
11968   else
11969     {
11970       /* We have to copy the data here, because DW_OP_call4 will only
11971          use a DW_AT_location attribute.  */
11972       memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11973       ptr += DW_BLOCK (member_loc)->size;
11974     }
11975
11976   *ptr++ = DW_OP_plus;
11977   gdb_assert (ptr - baton->data == baton->size);
11978
11979   SYMBOL_LOCATION_BATON (sym) = baton;
11980   SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
11981 }
11982
11983 /* Create appropriate locally-scoped variables for all the
11984    DW_TAG_common_block entries.  Also create a struct common_block
11985    listing all such variables for `info common'.  COMMON_BLOCK_DOMAIN
11986    is used to sepate the common blocks name namespace from regular
11987    variable names.  */
11988
11989 static void
11990 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11991 {
11992   struct attribute *attr;
11993
11994   attr = dwarf2_attr (die, DW_AT_location, cu);
11995   if (attr)
11996     {
11997       /* Support the .debug_loc offsets.  */
11998       if (attr_form_is_block (attr))
11999         {
12000           /* Ok.  */
12001         }
12002       else if (attr_form_is_section_offset (attr))
12003         {
12004           dwarf2_complex_location_expr_complaint ();
12005           attr = NULL;
12006         }
12007       else
12008         {
12009           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12010                                                  "common block member");
12011           attr = NULL;
12012         }
12013     }
12014
12015   if (die->child != NULL)
12016     {
12017       struct objfile *objfile = cu->objfile;
12018       struct die_info *child_die;
12019       size_t n_entries = 0, size;
12020       struct common_block *common_block;
12021       struct symbol *sym;
12022
12023       for (child_die = die->child;
12024            child_die && child_die->tag;
12025            child_die = sibling_die (child_die))
12026         ++n_entries;
12027
12028       size = (sizeof (struct common_block)
12029               + (n_entries - 1) * sizeof (struct symbol *));
12030       common_block = obstack_alloc (&objfile->objfile_obstack, size);
12031       memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12032       common_block->n_entries = 0;
12033
12034       for (child_die = die->child;
12035            child_die && child_die->tag;
12036            child_die = sibling_die (child_die))
12037         {
12038           /* Create the symbol in the DW_TAG_common_block block in the current
12039              symbol scope.  */
12040           sym = new_symbol (child_die, NULL, cu);
12041           if (sym != NULL)
12042             {
12043               struct attribute *member_loc;
12044
12045               common_block->contents[common_block->n_entries++] = sym;
12046
12047               member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12048                                         cu);
12049               if (member_loc)
12050                 {
12051                   /* GDB has handled this for a long time, but it is
12052                      not specified by DWARF.  It seems to have been
12053                      emitted by gfortran at least as recently as:
12054                      http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057.  */
12055                   complaint (&symfile_complaints,
12056                              _("Variable in common block has "
12057                                "DW_AT_data_member_location "
12058                                "- DIE at 0x%x [in module %s]"),
12059                              child_die->offset.sect_off, cu->objfile->name);
12060
12061                   if (attr_form_is_section_offset (member_loc))
12062                     dwarf2_complex_location_expr_complaint ();
12063                   else if (attr_form_is_constant (member_loc)
12064                            || attr_form_is_block (member_loc))
12065                     {
12066                       if (attr)
12067                         mark_common_block_symbol_computed (sym, die, attr,
12068                                                            member_loc, cu);
12069                     }
12070                   else
12071                     dwarf2_complex_location_expr_complaint ();
12072                 }
12073             }
12074         }
12075
12076       sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12077       SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12078     }
12079 }
12080
12081 /* Create a type for a C++ namespace.  */
12082
12083 static struct type *
12084 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12085 {
12086   struct objfile *objfile = cu->objfile;
12087   const char *previous_prefix, *name;
12088   int is_anonymous;
12089   struct type *type;
12090
12091   /* For extensions, reuse the type of the original namespace.  */
12092   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12093     {
12094       struct die_info *ext_die;
12095       struct dwarf2_cu *ext_cu = cu;
12096
12097       ext_die = dwarf2_extension (die, &ext_cu);
12098       type = read_type_die (ext_die, ext_cu);
12099
12100       /* EXT_CU may not be the same as CU.
12101          Ensure TYPE is recorded in CU's type_hash table.  */
12102       return set_die_type (die, type, cu);
12103     }
12104
12105   name = namespace_name (die, &is_anonymous, cu);
12106
12107   /* Now build the name of the current namespace.  */
12108
12109   previous_prefix = determine_prefix (die, cu);
12110   if (previous_prefix[0] != '\0')
12111     name = typename_concat (&objfile->objfile_obstack,
12112                             previous_prefix, name, 0, cu);
12113
12114   /* Create the type.  */
12115   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12116                     objfile);
12117   TYPE_NAME (type) = name;
12118   TYPE_TAG_NAME (type) = TYPE_NAME (type);
12119
12120   return set_die_type (die, type, cu);
12121 }
12122
12123 /* Read a C++ namespace.  */
12124
12125 static void
12126 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12127 {
12128   struct objfile *objfile = cu->objfile;
12129   int is_anonymous;
12130
12131   /* Add a symbol associated to this if we haven't seen the namespace
12132      before.  Also, add a using directive if it's an anonymous
12133      namespace.  */
12134
12135   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12136     {
12137       struct type *type;
12138
12139       type = read_type_die (die, cu);
12140       new_symbol (die, type, cu);
12141
12142       namespace_name (die, &is_anonymous, cu);
12143       if (is_anonymous)
12144         {
12145           const char *previous_prefix = determine_prefix (die, cu);
12146
12147           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12148                                   NULL, NULL, 0, &objfile->objfile_obstack);
12149         }
12150     }
12151
12152   if (die->child != NULL)
12153     {
12154       struct die_info *child_die = die->child;
12155
12156       while (child_die && child_die->tag)
12157         {
12158           process_die (child_die, cu);
12159           child_die = sibling_die (child_die);
12160         }
12161     }
12162 }
12163
12164 /* Read a Fortran module as type.  This DIE can be only a declaration used for
12165    imported module.  Still we need that type as local Fortran "use ... only"
12166    declaration imports depend on the created type in determine_prefix.  */
12167
12168 static struct type *
12169 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12170 {
12171   struct objfile *objfile = cu->objfile;
12172   const char *module_name;
12173   struct type *type;
12174
12175   module_name = dwarf2_name (die, cu);
12176   if (!module_name)
12177     complaint (&symfile_complaints,
12178                _("DW_TAG_module has no name, offset 0x%x"),
12179                die->offset.sect_off);
12180   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12181
12182   /* determine_prefix uses TYPE_TAG_NAME.  */
12183   TYPE_TAG_NAME (type) = TYPE_NAME (type);
12184
12185   return set_die_type (die, type, cu);
12186 }
12187
12188 /* Read a Fortran module.  */
12189
12190 static void
12191 read_module (struct die_info *die, struct dwarf2_cu *cu)
12192 {
12193   struct die_info *child_die = die->child;
12194
12195   while (child_die && child_die->tag)
12196     {
12197       process_die (child_die, cu);
12198       child_die = sibling_die (child_die);
12199     }
12200 }
12201
12202 /* Return the name of the namespace represented by DIE.  Set
12203    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12204    namespace.  */
12205
12206 static const char *
12207 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12208 {
12209   struct die_info *current_die;
12210   const char *name = NULL;
12211
12212   /* Loop through the extensions until we find a name.  */
12213
12214   for (current_die = die;
12215        current_die != NULL;
12216        current_die = dwarf2_extension (die, &cu))
12217     {
12218       name = dwarf2_name (current_die, cu);
12219       if (name != NULL)
12220         break;
12221     }
12222
12223   /* Is it an anonymous namespace?  */
12224
12225   *is_anonymous = (name == NULL);
12226   if (*is_anonymous)
12227     name = CP_ANONYMOUS_NAMESPACE_STR;
12228
12229   return name;
12230 }
12231
12232 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12233    the user defined type vector.  */
12234
12235 static struct type *
12236 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12237 {
12238   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12239   struct comp_unit_head *cu_header = &cu->header;
12240   struct type *type;
12241   struct attribute *attr_byte_size;
12242   struct attribute *attr_address_class;
12243   int byte_size, addr_class;
12244   struct type *target_type;
12245
12246   target_type = die_type (die, cu);
12247
12248   /* The die_type call above may have already set the type for this DIE.  */
12249   type = get_die_type (die, cu);
12250   if (type)
12251     return type;
12252
12253   type = lookup_pointer_type (target_type);
12254
12255   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12256   if (attr_byte_size)
12257     byte_size = DW_UNSND (attr_byte_size);
12258   else
12259     byte_size = cu_header->addr_size;
12260
12261   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12262   if (attr_address_class)
12263     addr_class = DW_UNSND (attr_address_class);
12264   else
12265     addr_class = DW_ADDR_none;
12266
12267   /* If the pointer size or address class is different than the
12268      default, create a type variant marked as such and set the
12269      length accordingly.  */
12270   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12271     {
12272       if (gdbarch_address_class_type_flags_p (gdbarch))
12273         {
12274           int type_flags;
12275
12276           type_flags = gdbarch_address_class_type_flags
12277                          (gdbarch, byte_size, addr_class);
12278           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12279                       == 0);
12280           type = make_type_with_address_space (type, type_flags);
12281         }
12282       else if (TYPE_LENGTH (type) != byte_size)
12283         {
12284           complaint (&symfile_complaints,
12285                      _("invalid pointer size %d"), byte_size);
12286         }
12287       else
12288         {
12289           /* Should we also complain about unhandled address classes?  */
12290         }
12291     }
12292
12293   TYPE_LENGTH (type) = byte_size;
12294   return set_die_type (die, type, cu);
12295 }
12296
12297 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12298    the user defined type vector.  */
12299
12300 static struct type *
12301 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12302 {
12303   struct type *type;
12304   struct type *to_type;
12305   struct type *domain;
12306
12307   to_type = die_type (die, cu);
12308   domain = die_containing_type (die, cu);
12309
12310   /* The calls above may have already set the type for this DIE.  */
12311   type = get_die_type (die, cu);
12312   if (type)
12313     return type;
12314
12315   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12316     type = lookup_methodptr_type (to_type);
12317   else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12318     {
12319       struct type *new_type = alloc_type (cu->objfile);
12320
12321       smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12322                             TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12323                             TYPE_VARARGS (to_type));
12324       type = lookup_methodptr_type (new_type);
12325     }
12326   else
12327     type = lookup_memberptr_type (to_type, domain);
12328
12329   return set_die_type (die, type, cu);
12330 }
12331
12332 /* Extract all information from a DW_TAG_reference_type DIE and add to
12333    the user defined type vector.  */
12334
12335 static struct type *
12336 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12337 {
12338   struct comp_unit_head *cu_header = &cu->header;
12339   struct type *type, *target_type;
12340   struct attribute *attr;
12341
12342   target_type = die_type (die, cu);
12343
12344   /* The die_type call above may have already set the type for this DIE.  */
12345   type = get_die_type (die, cu);
12346   if (type)
12347     return type;
12348
12349   type = lookup_reference_type (target_type);
12350   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12351   if (attr)
12352     {
12353       TYPE_LENGTH (type) = DW_UNSND (attr);
12354     }
12355   else
12356     {
12357       TYPE_LENGTH (type) = cu_header->addr_size;
12358     }
12359   return set_die_type (die, type, cu);
12360 }
12361
12362 static struct type *
12363 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12364 {
12365   struct type *base_type, *cv_type;
12366
12367   base_type = die_type (die, cu);
12368
12369   /* The die_type call above may have already set the type for this DIE.  */
12370   cv_type = get_die_type (die, cu);
12371   if (cv_type)
12372     return cv_type;
12373
12374   /* In case the const qualifier is applied to an array type, the element type
12375      is so qualified, not the array type (section 6.7.3 of C99).  */
12376   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12377     {
12378       struct type *el_type, *inner_array;
12379
12380       base_type = copy_type (base_type);
12381       inner_array = base_type;
12382
12383       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12384         {
12385           TYPE_TARGET_TYPE (inner_array) =
12386             copy_type (TYPE_TARGET_TYPE (inner_array));
12387           inner_array = TYPE_TARGET_TYPE (inner_array);
12388         }
12389
12390       el_type = TYPE_TARGET_TYPE (inner_array);
12391       TYPE_TARGET_TYPE (inner_array) =
12392         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12393
12394       return set_die_type (die, base_type, cu);
12395     }
12396
12397   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12398   return set_die_type (die, cv_type, cu);
12399 }
12400
12401 static struct type *
12402 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12403 {
12404   struct type *base_type, *cv_type;
12405
12406   base_type = die_type (die, cu);
12407
12408   /* The die_type call above may have already set the type for this DIE.  */
12409   cv_type = get_die_type (die, cu);
12410   if (cv_type)
12411     return cv_type;
12412
12413   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12414   return set_die_type (die, cv_type, cu);
12415 }
12416
12417 /* Handle DW_TAG_restrict_type.  */
12418
12419 static struct type *
12420 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12421 {
12422   struct type *base_type, *cv_type;
12423
12424   base_type = die_type (die, cu);
12425
12426   /* The die_type call above may have already set the type for this DIE.  */
12427   cv_type = get_die_type (die, cu);
12428   if (cv_type)
12429     return cv_type;
12430
12431   cv_type = make_restrict_type (base_type);
12432   return set_die_type (die, cv_type, cu);
12433 }
12434
12435 /* Extract all information from a DW_TAG_string_type DIE and add to
12436    the user defined type vector.  It isn't really a user defined type,
12437    but it behaves like one, with other DIE's using an AT_user_def_type
12438    attribute to reference it.  */
12439
12440 static struct type *
12441 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12442 {
12443   struct objfile *objfile = cu->objfile;
12444   struct gdbarch *gdbarch = get_objfile_arch (objfile);
12445   struct type *type, *range_type, *index_type, *char_type;
12446   struct attribute *attr;
12447   unsigned int length;
12448
12449   attr = dwarf2_attr (die, DW_AT_string_length, cu);
12450   if (attr)
12451     {
12452       length = DW_UNSND (attr);
12453     }
12454   else
12455     {
12456       /* Check for the DW_AT_byte_size attribute.  */
12457       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12458       if (attr)
12459         {
12460           length = DW_UNSND (attr);
12461         }
12462       else
12463         {
12464           length = 1;
12465         }
12466     }
12467
12468   index_type = objfile_type (objfile)->builtin_int;
12469   range_type = create_range_type (NULL, index_type, 1, length);
12470   char_type = language_string_char_type (cu->language_defn, gdbarch);
12471   type = create_string_type (NULL, char_type, range_type);
12472
12473   return set_die_type (die, type, cu);
12474 }
12475
12476 /* Handle DIES due to C code like:
12477
12478    struct foo
12479    {
12480    int (*funcp)(int a, long l);
12481    int b;
12482    };
12483
12484    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
12485
12486 static struct type *
12487 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12488 {
12489   struct objfile *objfile = cu->objfile;
12490   struct type *type;            /* Type that this function returns.  */
12491   struct type *ftype;           /* Function that returns above type.  */
12492   struct attribute *attr;
12493
12494   type = die_type (die, cu);
12495
12496   /* The die_type call above may have already set the type for this DIE.  */
12497   ftype = get_die_type (die, cu);
12498   if (ftype)
12499     return ftype;
12500
12501   ftype = lookup_function_type (type);
12502
12503   /* All functions in C++, Pascal and Java have prototypes.  */
12504   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12505   if ((attr && (DW_UNSND (attr) != 0))
12506       || cu->language == language_cplus
12507       || cu->language == language_java
12508       || cu->language == language_pascal)
12509     TYPE_PROTOTYPED (ftype) = 1;
12510   else if (producer_is_realview (cu->producer))
12511     /* RealView does not emit DW_AT_prototyped.  We can not
12512        distinguish prototyped and unprototyped functions; default to
12513        prototyped, since that is more common in modern code (and
12514        RealView warns about unprototyped functions).  */
12515     TYPE_PROTOTYPED (ftype) = 1;
12516
12517   /* Store the calling convention in the type if it's available in
12518      the subroutine die.  Otherwise set the calling convention to
12519      the default value DW_CC_normal.  */
12520   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12521   if (attr)
12522     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12523   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12524     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12525   else
12526     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12527
12528   /* We need to add the subroutine type to the die immediately so
12529      we don't infinitely recurse when dealing with parameters
12530      declared as the same subroutine type.  */
12531   set_die_type (die, ftype, cu);
12532
12533   if (die->child != NULL)
12534     {
12535       struct type *void_type = objfile_type (objfile)->builtin_void;
12536       struct die_info *child_die;
12537       int nparams, iparams;
12538
12539       /* Count the number of parameters.
12540          FIXME: GDB currently ignores vararg functions, but knows about
12541          vararg member functions.  */
12542       nparams = 0;
12543       child_die = die->child;
12544       while (child_die && child_die->tag)
12545         {
12546           if (child_die->tag == DW_TAG_formal_parameter)
12547             nparams++;
12548           else if (child_die->tag == DW_TAG_unspecified_parameters)
12549             TYPE_VARARGS (ftype) = 1;
12550           child_die = sibling_die (child_die);
12551         }
12552
12553       /* Allocate storage for parameters and fill them in.  */
12554       TYPE_NFIELDS (ftype) = nparams;
12555       TYPE_FIELDS (ftype) = (struct field *)
12556         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12557
12558       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
12559          even if we error out during the parameters reading below.  */
12560       for (iparams = 0; iparams < nparams; iparams++)
12561         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12562
12563       iparams = 0;
12564       child_die = die->child;
12565       while (child_die && child_die->tag)
12566         {
12567           if (child_die->tag == DW_TAG_formal_parameter)
12568             {
12569               struct type *arg_type;
12570
12571               /* DWARF version 2 has no clean way to discern C++
12572                  static and non-static member functions.  G++ helps
12573                  GDB by marking the first parameter for non-static
12574                  member functions (which is the this pointer) as
12575                  artificial.  We pass this information to
12576                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12577
12578                  DWARF version 3 added DW_AT_object_pointer, which GCC
12579                  4.5 does not yet generate.  */
12580               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12581               if (attr)
12582                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12583               else
12584                 {
12585                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12586
12587                   /* GCC/43521: In java, the formal parameter
12588                      "this" is sometimes not marked with DW_AT_artificial.  */
12589                   if (cu->language == language_java)
12590                     {
12591                       const char *name = dwarf2_name (child_die, cu);
12592
12593                       if (name && !strcmp (name, "this"))
12594                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12595                     }
12596                 }
12597               arg_type = die_type (child_die, cu);
12598
12599               /* RealView does not mark THIS as const, which the testsuite
12600                  expects.  GCC marks THIS as const in method definitions,
12601                  but not in the class specifications (GCC PR 43053).  */
12602               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12603                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12604                 {
12605                   int is_this = 0;
12606                   struct dwarf2_cu *arg_cu = cu;
12607                   const char *name = dwarf2_name (child_die, cu);
12608
12609                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12610                   if (attr)
12611                     {
12612                       /* If the compiler emits this, use it.  */
12613                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
12614                         is_this = 1;
12615                     }
12616                   else if (name && strcmp (name, "this") == 0)
12617                     /* Function definitions will have the argument names.  */
12618                     is_this = 1;
12619                   else if (name == NULL && iparams == 0)
12620                     /* Declarations may not have the names, so like
12621                        elsewhere in GDB, assume an artificial first
12622                        argument is "this".  */
12623                     is_this = 1;
12624
12625                   if (is_this)
12626                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12627                                              arg_type, 0);
12628                 }
12629
12630               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12631               iparams++;
12632             }
12633           child_die = sibling_die (child_die);
12634         }
12635     }
12636
12637   return ftype;
12638 }
12639
12640 static struct type *
12641 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12642 {
12643   struct objfile *objfile = cu->objfile;
12644   const char *name = NULL;
12645   struct type *this_type, *target_type;
12646
12647   name = dwarf2_full_name (NULL, die, cu);
12648   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12649                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
12650   TYPE_NAME (this_type) = name;
12651   set_die_type (die, this_type, cu);
12652   target_type = die_type (die, cu);
12653   if (target_type != this_type)
12654     TYPE_TARGET_TYPE (this_type) = target_type;
12655   else
12656     {
12657       /* Self-referential typedefs are, it seems, not allowed by the DWARF
12658          spec and cause infinite loops in GDB.  */
12659       complaint (&symfile_complaints,
12660                  _("Self-referential DW_TAG_typedef "
12661                    "- DIE at 0x%x [in module %s]"),
12662                  die->offset.sect_off, objfile->name);
12663       TYPE_TARGET_TYPE (this_type) = NULL;
12664     }
12665   return this_type;
12666 }
12667
12668 /* Find a representation of a given base type and install
12669    it in the TYPE field of the die.  */
12670
12671 static struct type *
12672 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12673 {
12674   struct objfile *objfile = cu->objfile;
12675   struct type *type;
12676   struct attribute *attr;
12677   int encoding = 0, size = 0;
12678   const char *name;
12679   enum type_code code = TYPE_CODE_INT;
12680   int type_flags = 0;
12681   struct type *target_type = NULL;
12682
12683   attr = dwarf2_attr (die, DW_AT_encoding, cu);
12684   if (attr)
12685     {
12686       encoding = DW_UNSND (attr);
12687     }
12688   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12689   if (attr)
12690     {
12691       size = DW_UNSND (attr);
12692     }
12693   name = dwarf2_name (die, cu);
12694   if (!name)
12695     {
12696       complaint (&symfile_complaints,
12697                  _("DW_AT_name missing from DW_TAG_base_type"));
12698     }
12699
12700   switch (encoding)
12701     {
12702       case DW_ATE_address:
12703         /* Turn DW_ATE_address into a void * pointer.  */
12704         code = TYPE_CODE_PTR;
12705         type_flags |= TYPE_FLAG_UNSIGNED;
12706         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12707         break;
12708       case DW_ATE_boolean:
12709         code = TYPE_CODE_BOOL;
12710         type_flags |= TYPE_FLAG_UNSIGNED;
12711         break;
12712       case DW_ATE_complex_float:
12713         code = TYPE_CODE_COMPLEX;
12714         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12715         break;
12716       case DW_ATE_decimal_float:
12717         code = TYPE_CODE_DECFLOAT;
12718         break;
12719       case DW_ATE_float:
12720         code = TYPE_CODE_FLT;
12721         break;
12722       case DW_ATE_signed:
12723         break;
12724       case DW_ATE_unsigned:
12725         type_flags |= TYPE_FLAG_UNSIGNED;
12726         if (cu->language == language_fortran
12727             && name
12728             && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12729           code = TYPE_CODE_CHAR;
12730         break;
12731       case DW_ATE_signed_char:
12732         if (cu->language == language_ada || cu->language == language_m2
12733             || cu->language == language_pascal
12734             || cu->language == language_fortran)
12735           code = TYPE_CODE_CHAR;
12736         break;
12737       case DW_ATE_unsigned_char:
12738         if (cu->language == language_ada || cu->language == language_m2
12739             || cu->language == language_pascal
12740             || cu->language == language_fortran)
12741           code = TYPE_CODE_CHAR;
12742         type_flags |= TYPE_FLAG_UNSIGNED;
12743         break;
12744       case DW_ATE_UTF:
12745         /* We just treat this as an integer and then recognize the
12746            type by name elsewhere.  */
12747         break;
12748
12749       default:
12750         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12751                    dwarf_type_encoding_name (encoding));
12752         break;
12753     }
12754
12755   type = init_type (code, size, type_flags, NULL, objfile);
12756   TYPE_NAME (type) = name;
12757   TYPE_TARGET_TYPE (type) = target_type;
12758
12759   if (name && strcmp (name, "char") == 0)
12760     TYPE_NOSIGN (type) = 1;
12761
12762   return set_die_type (die, type, cu);
12763 }
12764
12765 /* Read the given DW_AT_subrange DIE.  */
12766
12767 static struct type *
12768 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12769 {
12770   struct type *base_type, *orig_base_type;
12771   struct type *range_type;
12772   struct attribute *attr;
12773   LONGEST low, high;
12774   int low_default_is_valid;
12775   const char *name;
12776   LONGEST negative_mask;
12777
12778   orig_base_type = die_type (die, cu);
12779   /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12780      whereas the real type might be.  So, we use ORIG_BASE_TYPE when
12781      creating the range type, but we use the result of check_typedef
12782      when examining properties of the type.  */
12783   base_type = check_typedef (orig_base_type);
12784
12785   /* The die_type call above may have already set the type for this DIE.  */
12786   range_type = get_die_type (die, cu);
12787   if (range_type)
12788     return range_type;
12789
12790   /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12791      omitting DW_AT_lower_bound.  */
12792   switch (cu->language)
12793     {
12794     case language_c:
12795     case language_cplus:
12796       low = 0;
12797       low_default_is_valid = 1;
12798       break;
12799     case language_fortran:
12800       low = 1;
12801       low_default_is_valid = 1;
12802       break;
12803     case language_d:
12804     case language_java:
12805     case language_objc:
12806       low = 0;
12807       low_default_is_valid = (cu->header.version >= 4);
12808       break;
12809     case language_ada:
12810     case language_m2:
12811     case language_pascal:
12812       low = 1;
12813       low_default_is_valid = (cu->header.version >= 4);
12814       break;
12815     default:
12816       low = 0;
12817       low_default_is_valid = 0;
12818       break;
12819     }
12820
12821   /* FIXME: For variable sized arrays either of these could be
12822      a variable rather than a constant value.  We'll allow it,
12823      but we don't know how to handle it.  */
12824   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12825   if (attr)
12826     low = dwarf2_get_attr_constant_value (attr, low);
12827   else if (!low_default_is_valid)
12828     complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12829                                       "- DIE at 0x%x [in module %s]"),
12830                die->offset.sect_off, cu->objfile->name);
12831
12832   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12833   if (attr)
12834     {
12835       if (attr_form_is_block (attr) || is_ref_attr (attr))
12836         {
12837           /* GCC encodes arrays with unspecified or dynamic length
12838              with a DW_FORM_block1 attribute or a reference attribute.
12839              FIXME: GDB does not yet know how to handle dynamic
12840              arrays properly, treat them as arrays with unspecified
12841              length for now.
12842
12843              FIXME: jimb/2003-09-22: GDB does not really know
12844              how to handle arrays of unspecified length
12845              either; we just represent them as zero-length
12846              arrays.  Choose an appropriate upper bound given
12847              the lower bound we've computed above.  */
12848           high = low - 1;
12849         }
12850       else
12851         high = dwarf2_get_attr_constant_value (attr, 1);
12852     }
12853   else
12854     {
12855       attr = dwarf2_attr (die, DW_AT_count, cu);
12856       if (attr)
12857         {
12858           int count = dwarf2_get_attr_constant_value (attr, 1);
12859           high = low + count - 1;
12860         }
12861       else
12862         {
12863           /* Unspecified array length.  */
12864           high = low - 1;
12865         }
12866     }
12867
12868   /* Dwarf-2 specifications explicitly allows to create subrange types
12869      without specifying a base type.
12870      In that case, the base type must be set to the type of
12871      the lower bound, upper bound or count, in that order, if any of these
12872      three attributes references an object that has a type.
12873      If no base type is found, the Dwarf-2 specifications say that
12874      a signed integer type of size equal to the size of an address should
12875      be used.
12876      For the following C code: `extern char gdb_int [];'
12877      GCC produces an empty range DIE.
12878      FIXME: muller/2010-05-28: Possible references to object for low bound,
12879      high bound or count are not yet handled by this code.  */
12880   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12881     {
12882       struct objfile *objfile = cu->objfile;
12883       struct gdbarch *gdbarch = get_objfile_arch (objfile);
12884       int addr_size = gdbarch_addr_bit (gdbarch) /8;
12885       struct type *int_type = objfile_type (objfile)->builtin_int;
12886
12887       /* Test "int", "long int", and "long long int" objfile types,
12888          and select the first one having a size above or equal to the
12889          architecture address size.  */
12890       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12891         base_type = int_type;
12892       else
12893         {
12894           int_type = objfile_type (objfile)->builtin_long;
12895           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12896             base_type = int_type;
12897           else
12898             {
12899               int_type = objfile_type (objfile)->builtin_long_long;
12900               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12901                 base_type = int_type;
12902             }
12903         }
12904     }
12905
12906   negative_mask =
12907     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12908   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12909     low |= negative_mask;
12910   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12911     high |= negative_mask;
12912
12913   range_type = create_range_type (NULL, orig_base_type, low, high);
12914
12915   /* Mark arrays with dynamic length at least as an array of unspecified
12916      length.  GDB could check the boundary but before it gets implemented at
12917      least allow accessing the array elements.  */
12918   if (attr && attr_form_is_block (attr))
12919     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12920
12921   /* Ada expects an empty array on no boundary attributes.  */
12922   if (attr == NULL && cu->language != language_ada)
12923     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12924
12925   name = dwarf2_name (die, cu);
12926   if (name)
12927     TYPE_NAME (range_type) = name;
12928
12929   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12930   if (attr)
12931     TYPE_LENGTH (range_type) = DW_UNSND (attr);
12932
12933   set_die_type (die, range_type, cu);
12934
12935   /* set_die_type should be already done.  */
12936   set_descriptive_type (range_type, die, cu);
12937
12938   return range_type;
12939 }
12940
12941 static struct type *
12942 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12943 {
12944   struct type *type;
12945
12946   /* For now, we only support the C meaning of an unspecified type: void.  */
12947
12948   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12949   TYPE_NAME (type) = dwarf2_name (die, cu);
12950
12951   return set_die_type (die, type, cu);
12952 }
12953
12954 /* Read a single die and all its descendents.  Set the die's sibling
12955    field to NULL; set other fields in the die correctly, and set all
12956    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
12957    location of the info_ptr after reading all of those dies.  PARENT
12958    is the parent of the die in question.  */
12959
12960 static struct die_info *
12961 read_die_and_children (const struct die_reader_specs *reader,
12962                        gdb_byte *info_ptr,
12963                        gdb_byte **new_info_ptr,
12964                        struct die_info *parent)
12965 {
12966   struct die_info *die;
12967   gdb_byte *cur_ptr;
12968   int has_children;
12969
12970   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
12971   if (die == NULL)
12972     {
12973       *new_info_ptr = cur_ptr;
12974       return NULL;
12975     }
12976   store_in_ref_table (die, reader->cu);
12977
12978   if (has_children)
12979     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
12980   else
12981     {
12982       die->child = NULL;
12983       *new_info_ptr = cur_ptr;
12984     }
12985
12986   die->sibling = NULL;
12987   die->parent = parent;
12988   return die;
12989 }
12990
12991 /* Read a die, all of its descendents, and all of its siblings; set
12992    all of the fields of all of the dies correctly.  Arguments are as
12993    in read_die_and_children.  */
12994
12995 static struct die_info *
12996 read_die_and_siblings (const struct die_reader_specs *reader,
12997                        gdb_byte *info_ptr,
12998                        gdb_byte **new_info_ptr,
12999                        struct die_info *parent)
13000 {
13001   struct die_info *first_die, *last_sibling;
13002   gdb_byte *cur_ptr;
13003
13004   cur_ptr = info_ptr;
13005   first_die = last_sibling = NULL;
13006
13007   while (1)
13008     {
13009       struct die_info *die
13010         = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13011
13012       if (die == NULL)
13013         {
13014           *new_info_ptr = cur_ptr;
13015           return first_die;
13016         }
13017
13018       if (!first_die)
13019         first_die = die;
13020       else
13021         last_sibling->sibling = die;
13022
13023       last_sibling = die;
13024     }
13025 }
13026
13027 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13028    attributes.
13029    The caller is responsible for filling in the extra attributes
13030    and updating (*DIEP)->num_attrs.
13031    Set DIEP to point to a newly allocated die with its information,
13032    except for its child, sibling, and parent fields.
13033    Set HAS_CHILDREN to tell whether the die has children or not.  */
13034
13035 static gdb_byte *
13036 read_full_die_1 (const struct die_reader_specs *reader,
13037                  struct die_info **diep, gdb_byte *info_ptr,
13038                  int *has_children, int num_extra_attrs)
13039 {
13040   unsigned int abbrev_number, bytes_read, i;
13041   sect_offset offset;
13042   struct abbrev_info *abbrev;
13043   struct die_info *die;
13044   struct dwarf2_cu *cu = reader->cu;
13045   bfd *abfd = reader->abfd;
13046
13047   offset.sect_off = info_ptr - reader->buffer;
13048   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13049   info_ptr += bytes_read;
13050   if (!abbrev_number)
13051     {
13052       *diep = NULL;
13053       *has_children = 0;
13054       return info_ptr;
13055     }
13056
13057   abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13058   if (!abbrev)
13059     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13060            abbrev_number,
13061            bfd_get_filename (abfd));
13062
13063   die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13064   die->offset = offset;
13065   die->tag = abbrev->tag;
13066   die->abbrev = abbrev_number;
13067
13068   /* Make the result usable.
13069      The caller needs to update num_attrs after adding the extra
13070      attributes.  */
13071   die->num_attrs = abbrev->num_attrs;
13072
13073   for (i = 0; i < abbrev->num_attrs; ++i)
13074     info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13075                                info_ptr);
13076
13077   *diep = die;
13078   *has_children = abbrev->has_children;
13079   return info_ptr;
13080 }
13081
13082 /* Read a die and all its attributes.
13083    Set DIEP to point to a newly allocated die with its information,
13084    except for its child, sibling, and parent fields.
13085    Set HAS_CHILDREN to tell whether the die has children or not.  */
13086
13087 static gdb_byte *
13088 read_full_die (const struct die_reader_specs *reader,
13089                struct die_info **diep, gdb_byte *info_ptr,
13090                int *has_children)
13091 {
13092   return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13093 }
13094 \f
13095 /* Abbreviation tables.
13096
13097    In DWARF version 2, the description of the debugging information is
13098    stored in a separate .debug_abbrev section.  Before we read any
13099    dies from a section we read in all abbreviations and install them
13100    in a hash table.  */
13101
13102 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE.  */
13103
13104 static struct abbrev_info *
13105 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13106 {
13107   struct abbrev_info *abbrev;
13108
13109   abbrev = (struct abbrev_info *)
13110     obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13111   memset (abbrev, 0, sizeof (struct abbrev_info));
13112   return abbrev;
13113 }
13114
13115 /* Add an abbreviation to the table.  */
13116
13117 static void
13118 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13119                          unsigned int abbrev_number,
13120                          struct abbrev_info *abbrev)
13121 {
13122   unsigned int hash_number;
13123
13124   hash_number = abbrev_number % ABBREV_HASH_SIZE;
13125   abbrev->next = abbrev_table->abbrevs[hash_number];
13126   abbrev_table->abbrevs[hash_number] = abbrev;
13127 }
13128
13129 /* Look up an abbrev in the table.
13130    Returns NULL if the abbrev is not found.  */
13131
13132 static struct abbrev_info *
13133 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13134                             unsigned int abbrev_number)
13135 {
13136   unsigned int hash_number;
13137   struct abbrev_info *abbrev;
13138
13139   hash_number = abbrev_number % ABBREV_HASH_SIZE;
13140   abbrev = abbrev_table->abbrevs[hash_number];
13141
13142   while (abbrev)
13143     {
13144       if (abbrev->number == abbrev_number)
13145         return abbrev;
13146       abbrev = abbrev->next;
13147     }
13148   return NULL;
13149 }
13150
13151 /* Read in an abbrev table.  */
13152
13153 static struct abbrev_table *
13154 abbrev_table_read_table (struct dwarf2_section_info *section,
13155                          sect_offset offset)
13156 {
13157   struct objfile *objfile = dwarf2_per_objfile->objfile;
13158   bfd *abfd = section->asection->owner;
13159   struct abbrev_table *abbrev_table;
13160   gdb_byte *abbrev_ptr;
13161   struct abbrev_info *cur_abbrev;
13162   unsigned int abbrev_number, bytes_read, abbrev_name;
13163   unsigned int abbrev_form;
13164   struct attr_abbrev *cur_attrs;
13165   unsigned int allocated_attrs;
13166
13167   abbrev_table = XMALLOC (struct abbrev_table);
13168   abbrev_table->offset = offset;
13169   obstack_init (&abbrev_table->abbrev_obstack);
13170   abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13171                                          (ABBREV_HASH_SIZE
13172                                           * sizeof (struct abbrev_info *)));
13173   memset (abbrev_table->abbrevs, 0,
13174           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13175
13176   dwarf2_read_section (objfile, section);
13177   abbrev_ptr = section->buffer + offset.sect_off;
13178   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13179   abbrev_ptr += bytes_read;
13180
13181   allocated_attrs = ATTR_ALLOC_CHUNK;
13182   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13183
13184   /* Loop until we reach an abbrev number of 0.  */
13185   while (abbrev_number)
13186     {
13187       cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13188
13189       /* read in abbrev header */
13190       cur_abbrev->number = abbrev_number;
13191       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13192       abbrev_ptr += bytes_read;
13193       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13194       abbrev_ptr += 1;
13195
13196       /* now read in declarations */
13197       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13198       abbrev_ptr += bytes_read;
13199       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13200       abbrev_ptr += bytes_read;
13201       while (abbrev_name)
13202         {
13203           if (cur_abbrev->num_attrs == allocated_attrs)
13204             {
13205               allocated_attrs += ATTR_ALLOC_CHUNK;
13206               cur_attrs
13207                 = xrealloc (cur_attrs, (allocated_attrs
13208                                         * sizeof (struct attr_abbrev)));
13209             }
13210
13211           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13212           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13213           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13214           abbrev_ptr += bytes_read;
13215           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13216           abbrev_ptr += bytes_read;
13217         }
13218
13219       cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13220                                          (cur_abbrev->num_attrs
13221                                           * sizeof (struct attr_abbrev)));
13222       memcpy (cur_abbrev->attrs, cur_attrs,
13223               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13224
13225       abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13226
13227       /* Get next abbreviation.
13228          Under Irix6 the abbreviations for a compilation unit are not
13229          always properly terminated with an abbrev number of 0.
13230          Exit loop if we encounter an abbreviation which we have
13231          already read (which means we are about to read the abbreviations
13232          for the next compile unit) or if the end of the abbreviation
13233          table is reached.  */
13234       if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13235         break;
13236       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13237       abbrev_ptr += bytes_read;
13238       if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13239         break;
13240     }
13241
13242   xfree (cur_attrs);
13243   return abbrev_table;
13244 }
13245
13246 /* Free the resources held by ABBREV_TABLE.  */
13247
13248 static void
13249 abbrev_table_free (struct abbrev_table *abbrev_table)
13250 {
13251   obstack_free (&abbrev_table->abbrev_obstack, NULL);
13252   xfree (abbrev_table);
13253 }
13254
13255 /* Same as abbrev_table_free but as a cleanup.
13256    We pass in a pointer to the pointer to the table so that we can
13257    set the pointer to NULL when we're done.  It also simplifies
13258    build_type_unit_groups.  */
13259
13260 static void
13261 abbrev_table_free_cleanup (void *table_ptr)
13262 {
13263   struct abbrev_table **abbrev_table_ptr = table_ptr;
13264
13265   if (*abbrev_table_ptr != NULL)
13266     abbrev_table_free (*abbrev_table_ptr);
13267   *abbrev_table_ptr = NULL;
13268 }
13269
13270 /* Read the abbrev table for CU from ABBREV_SECTION.  */
13271
13272 static void
13273 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13274                      struct dwarf2_section_info *abbrev_section)
13275 {
13276   cu->abbrev_table =
13277     abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13278 }
13279
13280 /* Release the memory used by the abbrev table for a compilation unit.  */
13281
13282 static void
13283 dwarf2_free_abbrev_table (void *ptr_to_cu)
13284 {
13285   struct dwarf2_cu *cu = ptr_to_cu;
13286
13287   abbrev_table_free (cu->abbrev_table);
13288   /* Set this to NULL so that we SEGV if we try to read it later,
13289      and also because free_comp_unit verifies this is NULL.  */
13290   cu->abbrev_table = NULL;
13291 }
13292 \f
13293 /* Returns nonzero if TAG represents a type that we might generate a partial
13294    symbol for.  */
13295
13296 static int
13297 is_type_tag_for_partial (int tag)
13298 {
13299   switch (tag)
13300     {
13301 #if 0
13302     /* Some types that would be reasonable to generate partial symbols for,
13303        that we don't at present.  */
13304     case DW_TAG_array_type:
13305     case DW_TAG_file_type:
13306     case DW_TAG_ptr_to_member_type:
13307     case DW_TAG_set_type:
13308     case DW_TAG_string_type:
13309     case DW_TAG_subroutine_type:
13310 #endif
13311     case DW_TAG_base_type:
13312     case DW_TAG_class_type:
13313     case DW_TAG_interface_type:
13314     case DW_TAG_enumeration_type:
13315     case DW_TAG_structure_type:
13316     case DW_TAG_subrange_type:
13317     case DW_TAG_typedef:
13318     case DW_TAG_union_type:
13319       return 1;
13320     default:
13321       return 0;
13322     }
13323 }
13324
13325 /* Load all DIEs that are interesting for partial symbols into memory.  */
13326
13327 static struct partial_die_info *
13328 load_partial_dies (const struct die_reader_specs *reader,
13329                    gdb_byte *info_ptr, int building_psymtab)
13330 {
13331   struct dwarf2_cu *cu = reader->cu;
13332   struct objfile *objfile = cu->objfile;
13333   struct partial_die_info *part_die;
13334   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13335   struct abbrev_info *abbrev;
13336   unsigned int bytes_read;
13337   unsigned int load_all = 0;
13338   int nesting_level = 1;
13339
13340   parent_die = NULL;
13341   last_die = NULL;
13342
13343   gdb_assert (cu->per_cu != NULL);
13344   if (cu->per_cu->load_all_dies)
13345     load_all = 1;
13346
13347   cu->partial_dies
13348     = htab_create_alloc_ex (cu->header.length / 12,
13349                             partial_die_hash,
13350                             partial_die_eq,
13351                             NULL,
13352                             &cu->comp_unit_obstack,
13353                             hashtab_obstack_allocate,
13354                             dummy_obstack_deallocate);
13355
13356   part_die = obstack_alloc (&cu->comp_unit_obstack,
13357                             sizeof (struct partial_die_info));
13358
13359   while (1)
13360     {
13361       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13362
13363       /* A NULL abbrev means the end of a series of children.  */
13364       if (abbrev == NULL)
13365         {
13366           if (--nesting_level == 0)
13367             {
13368               /* PART_DIE was probably the last thing allocated on the
13369                  comp_unit_obstack, so we could call obstack_free
13370                  here.  We don't do that because the waste is small,
13371                  and will be cleaned up when we're done with this
13372                  compilation unit.  This way, we're also more robust
13373                  against other users of the comp_unit_obstack.  */
13374               return first_die;
13375             }
13376           info_ptr += bytes_read;
13377           last_die = parent_die;
13378           parent_die = parent_die->die_parent;
13379           continue;
13380         }
13381
13382       /* Check for template arguments.  We never save these; if
13383          they're seen, we just mark the parent, and go on our way.  */
13384       if (parent_die != NULL
13385           && cu->language == language_cplus
13386           && (abbrev->tag == DW_TAG_template_type_param
13387               || abbrev->tag == DW_TAG_template_value_param))
13388         {
13389           parent_die->has_template_arguments = 1;
13390
13391           if (!load_all)
13392             {
13393               /* We don't need a partial DIE for the template argument.  */
13394               info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13395               continue;
13396             }
13397         }
13398
13399       /* We only recurse into c++ subprograms looking for template arguments.
13400          Skip their other children.  */
13401       if (!load_all
13402           && cu->language == language_cplus
13403           && parent_die != NULL
13404           && parent_die->tag == DW_TAG_subprogram)
13405         {
13406           info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13407           continue;
13408         }
13409
13410       /* Check whether this DIE is interesting enough to save.  Normally
13411          we would not be interested in members here, but there may be
13412          later variables referencing them via DW_AT_specification (for
13413          static members).  */
13414       if (!load_all
13415           && !is_type_tag_for_partial (abbrev->tag)
13416           && abbrev->tag != DW_TAG_constant
13417           && abbrev->tag != DW_TAG_enumerator
13418           && abbrev->tag != DW_TAG_subprogram
13419           && abbrev->tag != DW_TAG_lexical_block
13420           && abbrev->tag != DW_TAG_variable
13421           && abbrev->tag != DW_TAG_namespace
13422           && abbrev->tag != DW_TAG_module
13423           && abbrev->tag != DW_TAG_member
13424           && abbrev->tag != DW_TAG_imported_unit)
13425         {
13426           /* Otherwise we skip to the next sibling, if any.  */
13427           info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13428           continue;
13429         }
13430
13431       info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13432                                    info_ptr);
13433
13434       /* This two-pass algorithm for processing partial symbols has a
13435          high cost in cache pressure.  Thus, handle some simple cases
13436          here which cover the majority of C partial symbols.  DIEs
13437          which neither have specification tags in them, nor could have
13438          specification tags elsewhere pointing at them, can simply be
13439          processed and discarded.
13440
13441          This segment is also optional; scan_partial_symbols and
13442          add_partial_symbol will handle these DIEs if we chain
13443          them in normally.  When compilers which do not emit large
13444          quantities of duplicate debug information are more common,
13445          this code can probably be removed.  */
13446
13447       /* Any complete simple types at the top level (pretty much all
13448          of them, for a language without namespaces), can be processed
13449          directly.  */
13450       if (parent_die == NULL
13451           && part_die->has_specification == 0
13452           && part_die->is_declaration == 0
13453           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13454               || part_die->tag == DW_TAG_base_type
13455               || part_die->tag == DW_TAG_subrange_type))
13456         {
13457           if (building_psymtab && part_die->name != NULL)
13458             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13459                                  VAR_DOMAIN, LOC_TYPEDEF,
13460                                  &objfile->static_psymbols,
13461                                  0, (CORE_ADDR) 0, cu->language, objfile);
13462           info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13463           continue;
13464         }
13465
13466       /* The exception for DW_TAG_typedef with has_children above is
13467          a workaround of GCC PR debug/47510.  In the case of this complaint
13468          type_name_no_tag_or_error will error on such types later.
13469
13470          GDB skipped children of DW_TAG_typedef by the shortcut above and then
13471          it could not find the child DIEs referenced later, this is checked
13472          above.  In correct DWARF DW_TAG_typedef should have no children.  */
13473
13474       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13475         complaint (&symfile_complaints,
13476                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13477                      "- DIE at 0x%x [in module %s]"),
13478                    part_die->offset.sect_off, objfile->name);
13479
13480       /* If we're at the second level, and we're an enumerator, and
13481          our parent has no specification (meaning possibly lives in a
13482          namespace elsewhere), then we can add the partial symbol now
13483          instead of queueing it.  */
13484       if (part_die->tag == DW_TAG_enumerator
13485           && parent_die != NULL
13486           && parent_die->die_parent == NULL
13487           && parent_die->tag == DW_TAG_enumeration_type
13488           && parent_die->has_specification == 0)
13489         {
13490           if (part_die->name == NULL)
13491             complaint (&symfile_complaints,
13492                        _("malformed enumerator DIE ignored"));
13493           else if (building_psymtab)
13494             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13495                                  VAR_DOMAIN, LOC_CONST,
13496                                  (cu->language == language_cplus
13497                                   || cu->language == language_java)
13498                                  ? &objfile->global_psymbols
13499                                  : &objfile->static_psymbols,
13500                                  0, (CORE_ADDR) 0, cu->language, objfile);
13501
13502           info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13503           continue;
13504         }
13505
13506       /* We'll save this DIE so link it in.  */
13507       part_die->die_parent = parent_die;
13508       part_die->die_sibling = NULL;
13509       part_die->die_child = NULL;
13510
13511       if (last_die && last_die == parent_die)
13512         last_die->die_child = part_die;
13513       else if (last_die)
13514         last_die->die_sibling = part_die;
13515
13516       last_die = part_die;
13517
13518       if (first_die == NULL)
13519         first_die = part_die;
13520
13521       /* Maybe add the DIE to the hash table.  Not all DIEs that we
13522          find interesting need to be in the hash table, because we
13523          also have the parent/sibling/child chains; only those that we
13524          might refer to by offset later during partial symbol reading.
13525
13526          For now this means things that might have be the target of a
13527          DW_AT_specification, DW_AT_abstract_origin, or
13528          DW_AT_extension.  DW_AT_extension will refer only to
13529          namespaces; DW_AT_abstract_origin refers to functions (and
13530          many things under the function DIE, but we do not recurse
13531          into function DIEs during partial symbol reading) and
13532          possibly variables as well; DW_AT_specification refers to
13533          declarations.  Declarations ought to have the DW_AT_declaration
13534          flag.  It happens that GCC forgets to put it in sometimes, but
13535          only for functions, not for types.
13536
13537          Adding more things than necessary to the hash table is harmless
13538          except for the performance cost.  Adding too few will result in
13539          wasted time in find_partial_die, when we reread the compilation
13540          unit with load_all_dies set.  */
13541
13542       if (load_all
13543           || abbrev->tag == DW_TAG_constant
13544           || abbrev->tag == DW_TAG_subprogram
13545           || abbrev->tag == DW_TAG_variable
13546           || abbrev->tag == DW_TAG_namespace
13547           || part_die->is_declaration)
13548         {
13549           void **slot;
13550
13551           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13552                                            part_die->offset.sect_off, INSERT);
13553           *slot = part_die;
13554         }
13555
13556       part_die = obstack_alloc (&cu->comp_unit_obstack,
13557                                 sizeof (struct partial_die_info));
13558
13559       /* For some DIEs we want to follow their children (if any).  For C
13560          we have no reason to follow the children of structures; for other
13561          languages we have to, so that we can get at method physnames
13562          to infer fully qualified class names, for DW_AT_specification,
13563          and for C++ template arguments.  For C++, we also look one level
13564          inside functions to find template arguments (if the name of the
13565          function does not already contain the template arguments).
13566
13567          For Ada, we need to scan the children of subprograms and lexical
13568          blocks as well because Ada allows the definition of nested
13569          entities that could be interesting for the debugger, such as
13570          nested subprograms for instance.  */
13571       if (last_die->has_children
13572           && (load_all
13573               || last_die->tag == DW_TAG_namespace
13574               || last_die->tag == DW_TAG_module
13575               || last_die->tag == DW_TAG_enumeration_type
13576               || (cu->language == language_cplus
13577                   && last_die->tag == DW_TAG_subprogram
13578                   && (last_die->name == NULL
13579                       || strchr (last_die->name, '<') == NULL))
13580               || (cu->language != language_c
13581                   && (last_die->tag == DW_TAG_class_type
13582                       || last_die->tag == DW_TAG_interface_type
13583                       || last_die->tag == DW_TAG_structure_type
13584                       || last_die->tag == DW_TAG_union_type))
13585               || (cu->language == language_ada
13586                   && (last_die->tag == DW_TAG_subprogram
13587                       || last_die->tag == DW_TAG_lexical_block))))
13588         {
13589           nesting_level++;
13590           parent_die = last_die;
13591           continue;
13592         }
13593
13594       /* Otherwise we skip to the next sibling, if any.  */
13595       info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13596
13597       /* Back to the top, do it again.  */
13598     }
13599 }
13600
13601 /* Read a minimal amount of information into the minimal die structure.  */
13602
13603 static gdb_byte *
13604 read_partial_die (const struct die_reader_specs *reader,
13605                   struct partial_die_info *part_die,
13606                   struct abbrev_info *abbrev, unsigned int abbrev_len,
13607                   gdb_byte *info_ptr)
13608 {
13609   struct dwarf2_cu *cu = reader->cu;
13610   struct objfile *objfile = cu->objfile;
13611   gdb_byte *buffer = reader->buffer;
13612   unsigned int i;
13613   struct attribute attr;
13614   int has_low_pc_attr = 0;
13615   int has_high_pc_attr = 0;
13616   int high_pc_relative = 0;
13617
13618   memset (part_die, 0, sizeof (struct partial_die_info));
13619
13620   part_die->offset.sect_off = info_ptr - buffer;
13621
13622   info_ptr += abbrev_len;
13623
13624   if (abbrev == NULL)
13625     return info_ptr;
13626
13627   part_die->tag = abbrev->tag;
13628   part_die->has_children = abbrev->has_children;
13629
13630   for (i = 0; i < abbrev->num_attrs; ++i)
13631     {
13632       info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13633
13634       /* Store the data if it is of an attribute we want to keep in a
13635          partial symbol table.  */
13636       switch (attr.name)
13637         {
13638         case DW_AT_name:
13639           switch (part_die->tag)
13640             {
13641             case DW_TAG_compile_unit:
13642             case DW_TAG_partial_unit:
13643             case DW_TAG_type_unit:
13644               /* Compilation units have a DW_AT_name that is a filename, not
13645                  a source language identifier.  */
13646             case DW_TAG_enumeration_type:
13647             case DW_TAG_enumerator:
13648               /* These tags always have simple identifiers already; no need
13649                  to canonicalize them.  */
13650               part_die->name = DW_STRING (&attr);
13651               break;
13652             default:
13653               part_die->name
13654                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13655                                             &objfile->objfile_obstack);
13656               break;
13657             }
13658           break;
13659         case DW_AT_linkage_name:
13660         case DW_AT_MIPS_linkage_name:
13661           /* Note that both forms of linkage name might appear.  We
13662              assume they will be the same, and we only store the last
13663              one we see.  */
13664           if (cu->language == language_ada)
13665             part_die->name = DW_STRING (&attr);
13666           part_die->linkage_name = DW_STRING (&attr);
13667           break;
13668         case DW_AT_low_pc:
13669           has_low_pc_attr = 1;
13670           part_die->lowpc = DW_ADDR (&attr);
13671           break;
13672         case DW_AT_high_pc:
13673           has_high_pc_attr = 1;
13674           if (attr.form == DW_FORM_addr
13675               || attr.form == DW_FORM_GNU_addr_index)
13676             part_die->highpc = DW_ADDR (&attr);
13677           else
13678             {
13679               high_pc_relative = 1;
13680               part_die->highpc = DW_UNSND (&attr);
13681             }
13682           break;
13683         case DW_AT_location:
13684           /* Support the .debug_loc offsets.  */
13685           if (attr_form_is_block (&attr))
13686             {
13687                part_die->d.locdesc = DW_BLOCK (&attr);
13688             }
13689           else if (attr_form_is_section_offset (&attr))
13690             {
13691               dwarf2_complex_location_expr_complaint ();
13692             }
13693           else
13694             {
13695               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13696                                                      "partial symbol information");
13697             }
13698           break;
13699         case DW_AT_external:
13700           part_die->is_external = DW_UNSND (&attr);
13701           break;
13702         case DW_AT_declaration:
13703           part_die->is_declaration = DW_UNSND (&attr);
13704           break;
13705         case DW_AT_type:
13706           part_die->has_type = 1;
13707           break;
13708         case DW_AT_abstract_origin:
13709         case DW_AT_specification:
13710         case DW_AT_extension:
13711           part_die->has_specification = 1;
13712           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13713           part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13714                                    || cu->per_cu->is_dwz);
13715           break;
13716         case DW_AT_sibling:
13717           /* Ignore absolute siblings, they might point outside of
13718              the current compile unit.  */
13719           if (attr.form == DW_FORM_ref_addr)
13720             complaint (&symfile_complaints,
13721                        _("ignoring absolute DW_AT_sibling"));
13722           else
13723             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13724           break;
13725         case DW_AT_byte_size:
13726           part_die->has_byte_size = 1;
13727           break;
13728         case DW_AT_calling_convention:
13729           /* DWARF doesn't provide a way to identify a program's source-level
13730              entry point.  DW_AT_calling_convention attributes are only meant
13731              to describe functions' calling conventions.
13732
13733              However, because it's a necessary piece of information in
13734              Fortran, and because DW_CC_program is the only piece of debugging
13735              information whose definition refers to a 'main program' at all,
13736              several compilers have begun marking Fortran main programs with
13737              DW_CC_program --- even when those functions use the standard
13738              calling conventions.
13739
13740              So until DWARF specifies a way to provide this information and
13741              compilers pick up the new representation, we'll support this
13742              practice.  */
13743           if (DW_UNSND (&attr) == DW_CC_program
13744               && cu->language == language_fortran)
13745             {
13746               set_main_name (part_die->name);
13747
13748               /* As this DIE has a static linkage the name would be difficult
13749                  to look up later.  */
13750               language_of_main = language_fortran;
13751             }
13752           break;
13753         case DW_AT_inline:
13754           if (DW_UNSND (&attr) == DW_INL_inlined
13755               || DW_UNSND (&attr) == DW_INL_declared_inlined)
13756             part_die->may_be_inlined = 1;
13757           break;
13758
13759         case DW_AT_import:
13760           if (part_die->tag == DW_TAG_imported_unit)
13761             {
13762               part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13763               part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13764                                   || cu->per_cu->is_dwz);
13765             }
13766           break;
13767
13768         default:
13769           break;
13770         }
13771     }
13772
13773   if (high_pc_relative)
13774     part_die->highpc += part_die->lowpc;
13775
13776   if (has_low_pc_attr && has_high_pc_attr)
13777     {
13778       /* When using the GNU linker, .gnu.linkonce. sections are used to
13779          eliminate duplicate copies of functions and vtables and such.
13780          The linker will arbitrarily choose one and discard the others.
13781          The AT_*_pc values for such functions refer to local labels in
13782          these sections.  If the section from that file was discarded, the
13783          labels are not in the output, so the relocs get a value of 0.
13784          If this is a discarded function, mark the pc bounds as invalid,
13785          so that GDB will ignore it.  */
13786       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13787         {
13788           struct gdbarch *gdbarch = get_objfile_arch (objfile);
13789
13790           complaint (&symfile_complaints,
13791                      _("DW_AT_low_pc %s is zero "
13792                        "for DIE at 0x%x [in module %s]"),
13793                      paddress (gdbarch, part_die->lowpc),
13794                      part_die->offset.sect_off, objfile->name);
13795         }
13796       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
13797       else if (part_die->lowpc >= part_die->highpc)
13798         {
13799           struct gdbarch *gdbarch = get_objfile_arch (objfile);
13800
13801           complaint (&symfile_complaints,
13802                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13803                        "for DIE at 0x%x [in module %s]"),
13804                      paddress (gdbarch, part_die->lowpc),
13805                      paddress (gdbarch, part_die->highpc),
13806                      part_die->offset.sect_off, objfile->name);
13807         }
13808       else
13809         part_die->has_pc_info = 1;
13810     }
13811
13812   return info_ptr;
13813 }
13814
13815 /* Find a cached partial DIE at OFFSET in CU.  */
13816
13817 static struct partial_die_info *
13818 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13819 {
13820   struct partial_die_info *lookup_die = NULL;
13821   struct partial_die_info part_die;
13822
13823   part_die.offset = offset;
13824   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13825                                     offset.sect_off);
13826
13827   return lookup_die;
13828 }
13829
13830 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13831    except in the case of .debug_types DIEs which do not reference
13832    outside their CU (they do however referencing other types via
13833    DW_FORM_ref_sig8).  */
13834
13835 static struct partial_die_info *
13836 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13837 {
13838   struct objfile *objfile = cu->objfile;
13839   struct dwarf2_per_cu_data *per_cu = NULL;
13840   struct partial_die_info *pd = NULL;
13841
13842   if (offset_in_dwz == cu->per_cu->is_dwz
13843       && offset_in_cu_p (&cu->header, offset))
13844     {
13845       pd = find_partial_die_in_comp_unit (offset, cu);
13846       if (pd != NULL)
13847         return pd;
13848       /* We missed recording what we needed.
13849          Load all dies and try again.  */
13850       per_cu = cu->per_cu;
13851     }
13852   else
13853     {
13854       /* TUs don't reference other CUs/TUs (except via type signatures).  */
13855       if (cu->per_cu->is_debug_types)
13856         {
13857           error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13858                    " external reference to offset 0x%lx [in module %s].\n"),
13859                  (long) cu->header.offset.sect_off, (long) offset.sect_off,
13860                  bfd_get_filename (objfile->obfd));
13861         }
13862       per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13863                                                  objfile);
13864
13865       if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13866         load_partial_comp_unit (per_cu);
13867
13868       per_cu->cu->last_used = 0;
13869       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13870     }
13871
13872   /* If we didn't find it, and not all dies have been loaded,
13873      load them all and try again.  */
13874
13875   if (pd == NULL && per_cu->load_all_dies == 0)
13876     {
13877       per_cu->load_all_dies = 1;
13878
13879       /* This is nasty.  When we reread the DIEs, somewhere up the call chain
13880          THIS_CU->cu may already be in use.  So we can't just free it and
13881          replace its DIEs with the ones we read in.  Instead, we leave those
13882          DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13883          and clobber THIS_CU->cu->partial_dies with the hash table for the new
13884          set.  */
13885       load_partial_comp_unit (per_cu);
13886
13887       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13888     }
13889
13890   if (pd == NULL)
13891     internal_error (__FILE__, __LINE__,
13892                     _("could not find partial DIE 0x%x "
13893                       "in cache [from module %s]\n"),
13894                     offset.sect_off, bfd_get_filename (objfile->obfd));
13895   return pd;
13896 }
13897
13898 /* See if we can figure out if the class lives in a namespace.  We do
13899    this by looking for a member function; its demangled name will
13900    contain namespace info, if there is any.  */
13901
13902 static void
13903 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13904                                   struct dwarf2_cu *cu)
13905 {
13906   /* NOTE: carlton/2003-10-07: Getting the info this way changes
13907      what template types look like, because the demangler
13908      frequently doesn't give the same name as the debug info.  We
13909      could fix this by only using the demangled name to get the
13910      prefix (but see comment in read_structure_type).  */
13911
13912   struct partial_die_info *real_pdi;
13913   struct partial_die_info *child_pdi;
13914
13915   /* If this DIE (this DIE's specification, if any) has a parent, then
13916      we should not do this.  We'll prepend the parent's fully qualified
13917      name when we create the partial symbol.  */
13918
13919   real_pdi = struct_pdi;
13920   while (real_pdi->has_specification)
13921     real_pdi = find_partial_die (real_pdi->spec_offset,
13922                                  real_pdi->spec_is_dwz, cu);
13923
13924   if (real_pdi->die_parent != NULL)
13925     return;
13926
13927   for (child_pdi = struct_pdi->die_child;
13928        child_pdi != NULL;
13929        child_pdi = child_pdi->die_sibling)
13930     {
13931       if (child_pdi->tag == DW_TAG_subprogram
13932           && child_pdi->linkage_name != NULL)
13933         {
13934           char *actual_class_name
13935             = language_class_name_from_physname (cu->language_defn,
13936                                                  child_pdi->linkage_name);
13937           if (actual_class_name != NULL)
13938             {
13939               struct_pdi->name
13940                 = obstack_copy0 (&cu->objfile->objfile_obstack,
13941                                  actual_class_name,
13942                                  strlen (actual_class_name));
13943               xfree (actual_class_name);
13944             }
13945           break;
13946         }
13947     }
13948 }
13949
13950 /* Adjust PART_DIE before generating a symbol for it.  This function
13951    may set the is_external flag or change the DIE's name.  */
13952
13953 static void
13954 fixup_partial_die (struct partial_die_info *part_die,
13955                    struct dwarf2_cu *cu)
13956 {
13957   /* Once we've fixed up a die, there's no point in doing so again.
13958      This also avoids a memory leak if we were to call
13959      guess_partial_die_structure_name multiple times.  */
13960   if (part_die->fixup_called)
13961     return;
13962
13963   /* If we found a reference attribute and the DIE has no name, try
13964      to find a name in the referred to DIE.  */
13965
13966   if (part_die->name == NULL && part_die->has_specification)
13967     {
13968       struct partial_die_info *spec_die;
13969
13970       spec_die = find_partial_die (part_die->spec_offset,
13971                                    part_die->spec_is_dwz, cu);
13972
13973       fixup_partial_die (spec_die, cu);
13974
13975       if (spec_die->name)
13976         {
13977           part_die->name = spec_die->name;
13978
13979           /* Copy DW_AT_external attribute if it is set.  */
13980           if (spec_die->is_external)
13981             part_die->is_external = spec_die->is_external;
13982         }
13983     }
13984
13985   /* Set default names for some unnamed DIEs.  */
13986
13987   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
13988     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
13989
13990   /* If there is no parent die to provide a namespace, and there are
13991      children, see if we can determine the namespace from their linkage
13992      name.  */
13993   if (cu->language == language_cplus
13994       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
13995       && part_die->die_parent == NULL
13996       && part_die->has_children
13997       && (part_die->tag == DW_TAG_class_type
13998           || part_die->tag == DW_TAG_structure_type
13999           || part_die->tag == DW_TAG_union_type))
14000     guess_partial_die_structure_name (part_die, cu);
14001
14002   /* GCC might emit a nameless struct or union that has a linkage
14003      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
14004   if (part_die->name == NULL
14005       && (part_die->tag == DW_TAG_class_type
14006           || part_die->tag == DW_TAG_interface_type
14007           || part_die->tag == DW_TAG_structure_type
14008           || part_die->tag == DW_TAG_union_type)
14009       && part_die->linkage_name != NULL)
14010     {
14011       char *demangled;
14012
14013       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14014       if (demangled)
14015         {
14016           const char *base;
14017
14018           /* Strip any leading namespaces/classes, keep only the base name.
14019              DW_AT_name for named DIEs does not contain the prefixes.  */
14020           base = strrchr (demangled, ':');
14021           if (base && base > demangled && base[-1] == ':')
14022             base++;
14023           else
14024             base = demangled;
14025
14026           part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14027                                           base, strlen (base));
14028           xfree (demangled);
14029         }
14030     }
14031
14032   part_die->fixup_called = 1;
14033 }
14034
14035 /* Read an attribute value described by an attribute form.  */
14036
14037 static gdb_byte *
14038 read_attribute_value (const struct die_reader_specs *reader,
14039                       struct attribute *attr, unsigned form,
14040                       gdb_byte *info_ptr)
14041 {
14042   struct dwarf2_cu *cu = reader->cu;
14043   bfd *abfd = reader->abfd;
14044   struct comp_unit_head *cu_header = &cu->header;
14045   unsigned int bytes_read;
14046   struct dwarf_block *blk;
14047
14048   attr->form = form;
14049   switch (form)
14050     {
14051     case DW_FORM_ref_addr:
14052       if (cu->header.version == 2)
14053         DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14054       else
14055         DW_UNSND (attr) = read_offset (abfd, info_ptr,
14056                                        &cu->header, &bytes_read);
14057       info_ptr += bytes_read;
14058       break;
14059     case DW_FORM_GNU_ref_alt:
14060       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14061       info_ptr += bytes_read;
14062       break;
14063     case DW_FORM_addr:
14064       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14065       info_ptr += bytes_read;
14066       break;
14067     case DW_FORM_block2:
14068       blk = dwarf_alloc_block (cu);
14069       blk->size = read_2_bytes (abfd, info_ptr);
14070       info_ptr += 2;
14071       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14072       info_ptr += blk->size;
14073       DW_BLOCK (attr) = blk;
14074       break;
14075     case DW_FORM_block4:
14076       blk = dwarf_alloc_block (cu);
14077       blk->size = read_4_bytes (abfd, info_ptr);
14078       info_ptr += 4;
14079       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14080       info_ptr += blk->size;
14081       DW_BLOCK (attr) = blk;
14082       break;
14083     case DW_FORM_data2:
14084       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14085       info_ptr += 2;
14086       break;
14087     case DW_FORM_data4:
14088       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14089       info_ptr += 4;
14090       break;
14091     case DW_FORM_data8:
14092       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14093       info_ptr += 8;
14094       break;
14095     case DW_FORM_sec_offset:
14096       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14097       info_ptr += bytes_read;
14098       break;
14099     case DW_FORM_string:
14100       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14101       DW_STRING_IS_CANONICAL (attr) = 0;
14102       info_ptr += bytes_read;
14103       break;
14104     case DW_FORM_strp:
14105       if (!cu->per_cu->is_dwz)
14106         {
14107           DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14108                                                    &bytes_read);
14109           DW_STRING_IS_CANONICAL (attr) = 0;
14110           info_ptr += bytes_read;
14111           break;
14112         }
14113       /* FALLTHROUGH */
14114     case DW_FORM_GNU_strp_alt:
14115       {
14116         struct dwz_file *dwz = dwarf2_get_dwz_file ();
14117         LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14118                                           &bytes_read);
14119
14120         DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14121         DW_STRING_IS_CANONICAL (attr) = 0;
14122         info_ptr += bytes_read;
14123       }
14124       break;
14125     case DW_FORM_exprloc:
14126     case DW_FORM_block:
14127       blk = dwarf_alloc_block (cu);
14128       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14129       info_ptr += bytes_read;
14130       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14131       info_ptr += blk->size;
14132       DW_BLOCK (attr) = blk;
14133       break;
14134     case DW_FORM_block1:
14135       blk = dwarf_alloc_block (cu);
14136       blk->size = read_1_byte (abfd, info_ptr);
14137       info_ptr += 1;
14138       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14139       info_ptr += blk->size;
14140       DW_BLOCK (attr) = blk;
14141       break;
14142     case DW_FORM_data1:
14143       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14144       info_ptr += 1;
14145       break;
14146     case DW_FORM_flag:
14147       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14148       info_ptr += 1;
14149       break;
14150     case DW_FORM_flag_present:
14151       DW_UNSND (attr) = 1;
14152       break;
14153     case DW_FORM_sdata:
14154       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14155       info_ptr += bytes_read;
14156       break;
14157     case DW_FORM_udata:
14158       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14159       info_ptr += bytes_read;
14160       break;
14161     case DW_FORM_ref1:
14162       DW_UNSND (attr) = (cu->header.offset.sect_off
14163                          + read_1_byte (abfd, info_ptr));
14164       info_ptr += 1;
14165       break;
14166     case DW_FORM_ref2:
14167       DW_UNSND (attr) = (cu->header.offset.sect_off
14168                          + read_2_bytes (abfd, info_ptr));
14169       info_ptr += 2;
14170       break;
14171     case DW_FORM_ref4:
14172       DW_UNSND (attr) = (cu->header.offset.sect_off
14173                          + read_4_bytes (abfd, info_ptr));
14174       info_ptr += 4;
14175       break;
14176     case DW_FORM_ref8:
14177       DW_UNSND (attr) = (cu->header.offset.sect_off
14178                          + read_8_bytes (abfd, info_ptr));
14179       info_ptr += 8;
14180       break;
14181     case DW_FORM_ref_sig8:
14182       /* Convert the signature to something we can record in DW_UNSND
14183          for later lookup.
14184          NOTE: This is NULL if the type wasn't found.  */
14185       DW_SIGNATURED_TYPE (attr) =
14186         lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14187       info_ptr += 8;
14188       break;
14189     case DW_FORM_ref_udata:
14190       DW_UNSND (attr) = (cu->header.offset.sect_off
14191                          + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14192       info_ptr += bytes_read;
14193       break;
14194     case DW_FORM_indirect:
14195       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14196       info_ptr += bytes_read;
14197       info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14198       break;
14199     case DW_FORM_GNU_addr_index:
14200       if (reader->dwo_file == NULL)
14201         {
14202           /* For now flag a hard error.
14203              Later we can turn this into a complaint.  */
14204           error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14205                  dwarf_form_name (form),
14206                  bfd_get_filename (abfd));
14207         }
14208       DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14209       info_ptr += bytes_read;
14210       break;
14211     case DW_FORM_GNU_str_index:
14212       if (reader->dwo_file == NULL)
14213         {
14214           /* For now flag a hard error.
14215              Later we can turn this into a complaint if warranted.  */
14216           error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14217                  dwarf_form_name (form),
14218                  bfd_get_filename (abfd));
14219         }
14220       {
14221         ULONGEST str_index =
14222           read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14223
14224         DW_STRING (attr) = read_str_index (reader, cu, str_index);
14225         DW_STRING_IS_CANONICAL (attr) = 0;
14226         info_ptr += bytes_read;
14227       }
14228       break;
14229     default:
14230       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14231              dwarf_form_name (form),
14232              bfd_get_filename (abfd));
14233     }
14234
14235   /* Super hack.  */
14236   if (cu->per_cu->is_dwz && is_ref_attr (attr))
14237     attr->form = DW_FORM_GNU_ref_alt;
14238
14239   /* We have seen instances where the compiler tried to emit a byte
14240      size attribute of -1 which ended up being encoded as an unsigned
14241      0xffffffff.  Although 0xffffffff is technically a valid size value,
14242      an object of this size seems pretty unlikely so we can relatively
14243      safely treat these cases as if the size attribute was invalid and
14244      treat them as zero by default.  */
14245   if (attr->name == DW_AT_byte_size
14246       && form == DW_FORM_data4
14247       && DW_UNSND (attr) >= 0xffffffff)
14248     {
14249       complaint
14250         (&symfile_complaints,
14251          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14252          hex_string (DW_UNSND (attr)));
14253       DW_UNSND (attr) = 0;
14254     }
14255
14256   return info_ptr;
14257 }
14258
14259 /* Read an attribute described by an abbreviated attribute.  */
14260
14261 static gdb_byte *
14262 read_attribute (const struct die_reader_specs *reader,
14263                 struct attribute *attr, struct attr_abbrev *abbrev,
14264                 gdb_byte *info_ptr)
14265 {
14266   attr->name = abbrev->name;
14267   return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14268 }
14269
14270 /* Read dwarf information from a buffer.  */
14271
14272 static unsigned int
14273 read_1_byte (bfd *abfd, const gdb_byte *buf)
14274 {
14275   return bfd_get_8 (abfd, buf);
14276 }
14277
14278 static int
14279 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14280 {
14281   return bfd_get_signed_8 (abfd, buf);
14282 }
14283
14284 static unsigned int
14285 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14286 {
14287   return bfd_get_16 (abfd, buf);
14288 }
14289
14290 static int
14291 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14292 {
14293   return bfd_get_signed_16 (abfd, buf);
14294 }
14295
14296 static unsigned int
14297 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14298 {
14299   return bfd_get_32 (abfd, buf);
14300 }
14301
14302 static int
14303 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14304 {
14305   return bfd_get_signed_32 (abfd, buf);
14306 }
14307
14308 static ULONGEST
14309 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14310 {
14311   return bfd_get_64 (abfd, buf);
14312 }
14313
14314 static CORE_ADDR
14315 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14316               unsigned int *bytes_read)
14317 {
14318   struct comp_unit_head *cu_header = &cu->header;
14319   CORE_ADDR retval = 0;
14320
14321   if (cu_header->signed_addr_p)
14322     {
14323       switch (cu_header->addr_size)
14324         {
14325         case 2:
14326           retval = bfd_get_signed_16 (abfd, buf);
14327           break;
14328         case 4:
14329           retval = bfd_get_signed_32 (abfd, buf);
14330           break;
14331         case 8:
14332           retval = bfd_get_signed_64 (abfd, buf);
14333           break;
14334         default:
14335           internal_error (__FILE__, __LINE__,
14336                           _("read_address: bad switch, signed [in module %s]"),
14337                           bfd_get_filename (abfd));
14338         }
14339     }
14340   else
14341     {
14342       switch (cu_header->addr_size)
14343         {
14344         case 2:
14345           retval = bfd_get_16 (abfd, buf);
14346           break;
14347         case 4:
14348           retval = bfd_get_32 (abfd, buf);
14349           break;
14350         case 8:
14351           retval = bfd_get_64 (abfd, buf);
14352           break;
14353         default:
14354           internal_error (__FILE__, __LINE__,
14355                           _("read_address: bad switch, "
14356                             "unsigned [in module %s]"),
14357                           bfd_get_filename (abfd));
14358         }
14359     }
14360
14361   *bytes_read = cu_header->addr_size;
14362   return retval;
14363 }
14364
14365 /* Read the initial length from a section.  The (draft) DWARF 3
14366    specification allows the initial length to take up either 4 bytes
14367    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
14368    bytes describe the length and all offsets will be 8 bytes in length
14369    instead of 4.
14370
14371    An older, non-standard 64-bit format is also handled by this
14372    function.  The older format in question stores the initial length
14373    as an 8-byte quantity without an escape value.  Lengths greater
14374    than 2^32 aren't very common which means that the initial 4 bytes
14375    is almost always zero.  Since a length value of zero doesn't make
14376    sense for the 32-bit format, this initial zero can be considered to
14377    be an escape value which indicates the presence of the older 64-bit
14378    format.  As written, the code can't detect (old format) lengths
14379    greater than 4GB.  If it becomes necessary to handle lengths
14380    somewhat larger than 4GB, we could allow other small values (such
14381    as the non-sensical values of 1, 2, and 3) to also be used as
14382    escape values indicating the presence of the old format.
14383
14384    The value returned via bytes_read should be used to increment the
14385    relevant pointer after calling read_initial_length().
14386
14387    [ Note:  read_initial_length() and read_offset() are based on the
14388      document entitled "DWARF Debugging Information Format", revision
14389      3, draft 8, dated November 19, 2001.  This document was obtained
14390      from:
14391
14392         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14393
14394      This document is only a draft and is subject to change.  (So beware.)
14395
14396      Details regarding the older, non-standard 64-bit format were
14397      determined empirically by examining 64-bit ELF files produced by
14398      the SGI toolchain on an IRIX 6.5 machine.
14399
14400      - Kevin, July 16, 2002
14401    ] */
14402
14403 static LONGEST
14404 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14405 {
14406   LONGEST length = bfd_get_32 (abfd, buf);
14407
14408   if (length == 0xffffffff)
14409     {
14410       length = bfd_get_64 (abfd, buf + 4);
14411       *bytes_read = 12;
14412     }
14413   else if (length == 0)
14414     {
14415       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
14416       length = bfd_get_64 (abfd, buf);
14417       *bytes_read = 8;
14418     }
14419   else
14420     {
14421       *bytes_read = 4;
14422     }
14423
14424   return length;
14425 }
14426
14427 /* Cover function for read_initial_length.
14428    Returns the length of the object at BUF, and stores the size of the
14429    initial length in *BYTES_READ and stores the size that offsets will be in
14430    *OFFSET_SIZE.
14431    If the initial length size is not equivalent to that specified in
14432    CU_HEADER then issue a complaint.
14433    This is useful when reading non-comp-unit headers.  */
14434
14435 static LONGEST
14436 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14437                                         const struct comp_unit_head *cu_header,
14438                                         unsigned int *bytes_read,
14439                                         unsigned int *offset_size)
14440 {
14441   LONGEST length = read_initial_length (abfd, buf, bytes_read);
14442
14443   gdb_assert (cu_header->initial_length_size == 4
14444               || cu_header->initial_length_size == 8
14445               || cu_header->initial_length_size == 12);
14446
14447   if (cu_header->initial_length_size != *bytes_read)
14448     complaint (&symfile_complaints,
14449                _("intermixed 32-bit and 64-bit DWARF sections"));
14450
14451   *offset_size = (*bytes_read == 4) ? 4 : 8;
14452   return length;
14453 }
14454
14455 /* Read an offset from the data stream.  The size of the offset is
14456    given by cu_header->offset_size.  */
14457
14458 static LONGEST
14459 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14460              unsigned int *bytes_read)
14461 {
14462   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14463
14464   *bytes_read = cu_header->offset_size;
14465   return offset;
14466 }
14467
14468 /* Read an offset from the data stream.  */
14469
14470 static LONGEST
14471 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14472 {
14473   LONGEST retval = 0;
14474
14475   switch (offset_size)
14476     {
14477     case 4:
14478       retval = bfd_get_32 (abfd, buf);
14479       break;
14480     case 8:
14481       retval = bfd_get_64 (abfd, buf);
14482       break;
14483     default:
14484       internal_error (__FILE__, __LINE__,
14485                       _("read_offset_1: bad switch [in module %s]"),
14486                       bfd_get_filename (abfd));
14487     }
14488
14489   return retval;
14490 }
14491
14492 static gdb_byte *
14493 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14494 {
14495   /* If the size of a host char is 8 bits, we can return a pointer
14496      to the buffer, otherwise we have to copy the data to a buffer
14497      allocated on the temporary obstack.  */
14498   gdb_assert (HOST_CHAR_BIT == 8);
14499   return buf;
14500 }
14501
14502 static char *
14503 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14504 {
14505   /* If the size of a host char is 8 bits, we can return a pointer
14506      to the string, otherwise we have to copy the string to a buffer
14507      allocated on the temporary obstack.  */
14508   gdb_assert (HOST_CHAR_BIT == 8);
14509   if (*buf == '\0')
14510     {
14511       *bytes_read_ptr = 1;
14512       return NULL;
14513     }
14514   *bytes_read_ptr = strlen ((char *) buf) + 1;
14515   return (char *) buf;
14516 }
14517
14518 static char *
14519 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14520 {
14521   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14522   if (dwarf2_per_objfile->str.buffer == NULL)
14523     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14524            bfd_get_filename (abfd));
14525   if (str_offset >= dwarf2_per_objfile->str.size)
14526     error (_("DW_FORM_strp pointing outside of "
14527              ".debug_str section [in module %s]"),
14528            bfd_get_filename (abfd));
14529   gdb_assert (HOST_CHAR_BIT == 8);
14530   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14531     return NULL;
14532   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14533 }
14534
14535 /* Read a string at offset STR_OFFSET in the .debug_str section from
14536    the .dwz file DWZ.  Throw an error if the offset is too large.  If
14537    the string consists of a single NUL byte, return NULL; otherwise
14538    return a pointer to the string.  */
14539
14540 static char *
14541 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14542 {
14543   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14544
14545   if (dwz->str.buffer == NULL)
14546     error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14547              "section [in module %s]"),
14548            bfd_get_filename (dwz->dwz_bfd));
14549   if (str_offset >= dwz->str.size)
14550     error (_("DW_FORM_GNU_strp_alt pointing outside of "
14551              ".debug_str section [in module %s]"),
14552            bfd_get_filename (dwz->dwz_bfd));
14553   gdb_assert (HOST_CHAR_BIT == 8);
14554   if (dwz->str.buffer[str_offset] == '\0')
14555     return NULL;
14556   return (char *) (dwz->str.buffer + str_offset);
14557 }
14558
14559 static char *
14560 read_indirect_string (bfd *abfd, gdb_byte *buf,
14561                       const struct comp_unit_head *cu_header,
14562                       unsigned int *bytes_read_ptr)
14563 {
14564   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14565
14566   return read_indirect_string_at_offset (abfd, str_offset);
14567 }
14568
14569 static ULONGEST
14570 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14571 {
14572   ULONGEST result;
14573   unsigned int num_read;
14574   int i, shift;
14575   unsigned char byte;
14576
14577   result = 0;
14578   shift = 0;
14579   num_read = 0;
14580   i = 0;
14581   while (1)
14582     {
14583       byte = bfd_get_8 (abfd, buf);
14584       buf++;
14585       num_read++;
14586       result |= ((ULONGEST) (byte & 127) << shift);
14587       if ((byte & 128) == 0)
14588         {
14589           break;
14590         }
14591       shift += 7;
14592     }
14593   *bytes_read_ptr = num_read;
14594   return result;
14595 }
14596
14597 static LONGEST
14598 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14599 {
14600   LONGEST result;
14601   int i, shift, num_read;
14602   unsigned char byte;
14603
14604   result = 0;
14605   shift = 0;
14606   num_read = 0;
14607   i = 0;
14608   while (1)
14609     {
14610       byte = bfd_get_8 (abfd, buf);
14611       buf++;
14612       num_read++;
14613       result |= ((LONGEST) (byte & 127) << shift);
14614       shift += 7;
14615       if ((byte & 128) == 0)
14616         {
14617           break;
14618         }
14619     }
14620   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14621     result |= -(((LONGEST) 1) << shift);
14622   *bytes_read_ptr = num_read;
14623   return result;
14624 }
14625
14626 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14627    ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14628    ADDR_SIZE is the size of addresses from the CU header.  */
14629
14630 static CORE_ADDR
14631 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14632 {
14633   struct objfile *objfile = dwarf2_per_objfile->objfile;
14634   bfd *abfd = objfile->obfd;
14635   const gdb_byte *info_ptr;
14636
14637   dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14638   if (dwarf2_per_objfile->addr.buffer == NULL)
14639     error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14640            objfile->name);
14641   if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14642     error (_("DW_FORM_addr_index pointing outside of "
14643              ".debug_addr section [in module %s]"),
14644            objfile->name);
14645   info_ptr = (dwarf2_per_objfile->addr.buffer
14646               + addr_base + addr_index * addr_size);
14647   if (addr_size == 4)
14648     return bfd_get_32 (abfd, info_ptr);
14649   else
14650     return bfd_get_64 (abfd, info_ptr);
14651 }
14652
14653 /* Given index ADDR_INDEX in .debug_addr, fetch the value.  */
14654
14655 static CORE_ADDR
14656 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14657 {
14658   return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14659 }
14660
14661 /* Given a pointer to an leb128 value, fetch the value from .debug_addr.  */
14662
14663 static CORE_ADDR
14664 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14665                              unsigned int *bytes_read)
14666 {
14667   bfd *abfd = cu->objfile->obfd;
14668   unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14669
14670   return read_addr_index (cu, addr_index);
14671 }
14672
14673 /* Data structure to pass results from dwarf2_read_addr_index_reader
14674    back to dwarf2_read_addr_index.  */
14675
14676 struct dwarf2_read_addr_index_data
14677 {
14678   ULONGEST addr_base;
14679   int addr_size;
14680 };
14681
14682 /* die_reader_func for dwarf2_read_addr_index.  */
14683
14684 static void
14685 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14686                                gdb_byte *info_ptr,
14687                                struct die_info *comp_unit_die,
14688                                int has_children,
14689                                void *data)
14690 {
14691   struct dwarf2_cu *cu = reader->cu;
14692   struct dwarf2_read_addr_index_data *aidata =
14693     (struct dwarf2_read_addr_index_data *) data;
14694
14695   aidata->addr_base = cu->addr_base;
14696   aidata->addr_size = cu->header.addr_size;
14697 }
14698
14699 /* Given an index in .debug_addr, fetch the value.
14700    NOTE: This can be called during dwarf expression evaluation,
14701    long after the debug information has been read, and thus per_cu->cu
14702    may no longer exist.  */
14703
14704 CORE_ADDR
14705 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14706                         unsigned int addr_index)
14707 {
14708   struct objfile *objfile = per_cu->objfile;
14709   struct dwarf2_cu *cu = per_cu->cu;
14710   ULONGEST addr_base;
14711   int addr_size;
14712
14713   /* This is intended to be called from outside this file.  */
14714   dw2_setup (objfile);
14715
14716   /* We need addr_base and addr_size.
14717      If we don't have PER_CU->cu, we have to get it.
14718      Nasty, but the alternative is storing the needed info in PER_CU,
14719      which at this point doesn't seem justified: it's not clear how frequently
14720      it would get used and it would increase the size of every PER_CU.
14721      Entry points like dwarf2_per_cu_addr_size do a similar thing
14722      so we're not in uncharted territory here.
14723      Alas we need to be a bit more complicated as addr_base is contained
14724      in the DIE.
14725
14726      We don't need to read the entire CU(/TU).
14727      We just need the header and top level die.
14728
14729      IWBN to use the aging mechanism to let us lazily later discard the CU.
14730      For now we skip this optimization.  */
14731
14732   if (cu != NULL)
14733     {
14734       addr_base = cu->addr_base;
14735       addr_size = cu->header.addr_size;
14736     }
14737   else
14738     {
14739       struct dwarf2_read_addr_index_data aidata;
14740
14741       /* Note: We can't use init_cutu_and_read_dies_simple here,
14742          we need addr_base.  */
14743       init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14744                                dwarf2_read_addr_index_reader, &aidata);
14745       addr_base = aidata.addr_base;
14746       addr_size = aidata.addr_size;
14747     }
14748
14749   return read_addr_index_1 (addr_index, addr_base, addr_size);
14750 }
14751
14752 /* Given a DW_AT_str_index, fetch the string.  */
14753
14754 static char *
14755 read_str_index (const struct die_reader_specs *reader,
14756                 struct dwarf2_cu *cu, ULONGEST str_index)
14757 {
14758   struct objfile *objfile = dwarf2_per_objfile->objfile;
14759   const char *dwo_name = objfile->name;
14760   bfd *abfd = objfile->obfd;
14761   struct dwo_sections *sections = &reader->dwo_file->sections;
14762   gdb_byte *info_ptr;
14763   ULONGEST str_offset;
14764
14765   dwarf2_read_section (objfile, &sections->str);
14766   dwarf2_read_section (objfile, &sections->str_offsets);
14767   if (sections->str.buffer == NULL)
14768     error (_("DW_FORM_str_index used without .debug_str.dwo section"
14769              " in CU at offset 0x%lx [in module %s]"),
14770            (long) cu->header.offset.sect_off, dwo_name);
14771   if (sections->str_offsets.buffer == NULL)
14772     error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14773              " in CU at offset 0x%lx [in module %s]"),
14774            (long) cu->header.offset.sect_off, dwo_name);
14775   if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14776     error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14777              " section in CU at offset 0x%lx [in module %s]"),
14778            (long) cu->header.offset.sect_off, dwo_name);
14779   info_ptr = (sections->str_offsets.buffer
14780               + str_index * cu->header.offset_size);
14781   if (cu->header.offset_size == 4)
14782     str_offset = bfd_get_32 (abfd, info_ptr);
14783   else
14784     str_offset = bfd_get_64 (abfd, info_ptr);
14785   if (str_offset >= sections->str.size)
14786     error (_("Offset from DW_FORM_str_index pointing outside of"
14787              " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14788            (long) cu->header.offset.sect_off, dwo_name);
14789   return (char *) (sections->str.buffer + str_offset);
14790 }
14791
14792 /* Return the length of an LEB128 number in BUF.  */
14793
14794 static int
14795 leb128_size (const gdb_byte *buf)
14796 {
14797   const gdb_byte *begin = buf;
14798   gdb_byte byte;
14799
14800   while (1)
14801     {
14802       byte = *buf++;
14803       if ((byte & 128) == 0)
14804         return buf - begin;
14805     }
14806 }
14807
14808 static void
14809 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14810 {
14811   switch (lang)
14812     {
14813     case DW_LANG_C89:
14814     case DW_LANG_C99:
14815     case DW_LANG_C:
14816       cu->language = language_c;
14817       break;
14818     case DW_LANG_C_plus_plus:
14819       cu->language = language_cplus;
14820       break;
14821     case DW_LANG_D:
14822       cu->language = language_d;
14823       break;
14824     case DW_LANG_Fortran77:
14825     case DW_LANG_Fortran90:
14826     case DW_LANG_Fortran95:
14827       cu->language = language_fortran;
14828       break;
14829     case DW_LANG_Go:
14830       cu->language = language_go;
14831       break;
14832     case DW_LANG_Mips_Assembler:
14833       cu->language = language_asm;
14834       break;
14835     case DW_LANG_Java:
14836       cu->language = language_java;
14837       break;
14838     case DW_LANG_Ada83:
14839     case DW_LANG_Ada95:
14840       cu->language = language_ada;
14841       break;
14842     case DW_LANG_Modula2:
14843       cu->language = language_m2;
14844       break;
14845     case DW_LANG_Pascal83:
14846       cu->language = language_pascal;
14847       break;
14848     case DW_LANG_ObjC:
14849       cu->language = language_objc;
14850       break;
14851     case DW_LANG_Cobol74:
14852     case DW_LANG_Cobol85:
14853     default:
14854       cu->language = language_minimal;
14855       break;
14856     }
14857   cu->language_defn = language_def (cu->language);
14858 }
14859
14860 /* Return the named attribute or NULL if not there.  */
14861
14862 static struct attribute *
14863 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14864 {
14865   for (;;)
14866     {
14867       unsigned int i;
14868       struct attribute *spec = NULL;
14869
14870       for (i = 0; i < die->num_attrs; ++i)
14871         {
14872           if (die->attrs[i].name == name)
14873             return &die->attrs[i];
14874           if (die->attrs[i].name == DW_AT_specification
14875               || die->attrs[i].name == DW_AT_abstract_origin)
14876             spec = &die->attrs[i];
14877         }
14878
14879       if (!spec)
14880         break;
14881
14882       die = follow_die_ref (die, spec, &cu);
14883     }
14884
14885   return NULL;
14886 }
14887
14888 /* Return the named attribute or NULL if not there,
14889    but do not follow DW_AT_specification, etc.
14890    This is for use in contexts where we're reading .debug_types dies.
14891    Following DW_AT_specification, DW_AT_abstract_origin will take us
14892    back up the chain, and we want to go down.  */
14893
14894 static struct attribute *
14895 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14896 {
14897   unsigned int i;
14898
14899   for (i = 0; i < die->num_attrs; ++i)
14900     if (die->attrs[i].name == name)
14901       return &die->attrs[i];
14902
14903   return NULL;
14904 }
14905
14906 /* Return non-zero iff the attribute NAME is defined for the given DIE,
14907    and holds a non-zero value.  This function should only be used for
14908    DW_FORM_flag or DW_FORM_flag_present attributes.  */
14909
14910 static int
14911 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14912 {
14913   struct attribute *attr = dwarf2_attr (die, name, cu);
14914
14915   return (attr && DW_UNSND (attr));
14916 }
14917
14918 static int
14919 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
14920 {
14921   /* A DIE is a declaration if it has a DW_AT_declaration attribute
14922      which value is non-zero.  However, we have to be careful with
14923      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14924      (via dwarf2_flag_true_p) follows this attribute.  So we may
14925      end up accidently finding a declaration attribute that belongs
14926      to a different DIE referenced by the specification attribute,
14927      even though the given DIE does not have a declaration attribute.  */
14928   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14929           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
14930 }
14931
14932 /* Return the die giving the specification for DIE, if there is
14933    one.  *SPEC_CU is the CU containing DIE on input, and the CU
14934    containing the return value on output.  If there is no
14935    specification, but there is an abstract origin, that is
14936    returned.  */
14937
14938 static struct die_info *
14939 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
14940 {
14941   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14942                                              *spec_cu);
14943
14944   if (spec_attr == NULL)
14945     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14946
14947   if (spec_attr == NULL)
14948     return NULL;
14949   else
14950     return follow_die_ref (die, spec_attr, spec_cu);
14951 }
14952
14953 /* Free the line_header structure *LH, and any arrays and strings it
14954    refers to.
14955    NOTE: This is also used as a "cleanup" function.  */
14956
14957 static void
14958 free_line_header (struct line_header *lh)
14959 {
14960   if (lh->standard_opcode_lengths)
14961     xfree (lh->standard_opcode_lengths);
14962
14963   /* Remember that all the lh->file_names[i].name pointers are
14964      pointers into debug_line_buffer, and don't need to be freed.  */
14965   if (lh->file_names)
14966     xfree (lh->file_names);
14967
14968   /* Similarly for the include directory names.  */
14969   if (lh->include_dirs)
14970     xfree (lh->include_dirs);
14971
14972   xfree (lh);
14973 }
14974
14975 /* Add an entry to LH's include directory table.  */
14976
14977 static void
14978 add_include_dir (struct line_header *lh, char *include_dir)
14979 {
14980   /* Grow the array if necessary.  */
14981   if (lh->include_dirs_size == 0)
14982     {
14983       lh->include_dirs_size = 1; /* for testing */
14984       lh->include_dirs = xmalloc (lh->include_dirs_size
14985                                   * sizeof (*lh->include_dirs));
14986     }
14987   else if (lh->num_include_dirs >= lh->include_dirs_size)
14988     {
14989       lh->include_dirs_size *= 2;
14990       lh->include_dirs = xrealloc (lh->include_dirs,
14991                                    (lh->include_dirs_size
14992                                     * sizeof (*lh->include_dirs)));
14993     }
14994
14995   lh->include_dirs[lh->num_include_dirs++] = include_dir;
14996 }
14997
14998 /* Add an entry to LH's file name table.  */
14999
15000 static void
15001 add_file_name (struct line_header *lh,
15002                char *name,
15003                unsigned int dir_index,
15004                unsigned int mod_time,
15005                unsigned int length)
15006 {
15007   struct file_entry *fe;
15008
15009   /* Grow the array if necessary.  */
15010   if (lh->file_names_size == 0)
15011     {
15012       lh->file_names_size = 1; /* for testing */
15013       lh->file_names = xmalloc (lh->file_names_size
15014                                 * sizeof (*lh->file_names));
15015     }
15016   else if (lh->num_file_names >= lh->file_names_size)
15017     {
15018       lh->file_names_size *= 2;
15019       lh->file_names = xrealloc (lh->file_names,
15020                                  (lh->file_names_size
15021                                   * sizeof (*lh->file_names)));
15022     }
15023
15024   fe = &lh->file_names[lh->num_file_names++];
15025   fe->name = name;
15026   fe->dir_index = dir_index;
15027   fe->mod_time = mod_time;
15028   fe->length = length;
15029   fe->included_p = 0;
15030   fe->symtab = NULL;
15031 }
15032
15033 /* A convenience function to find the proper .debug_line section for a
15034    CU.  */
15035
15036 static struct dwarf2_section_info *
15037 get_debug_line_section (struct dwarf2_cu *cu)
15038 {
15039   struct dwarf2_section_info *section;
15040
15041   /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15042      DWO file.  */
15043   if (cu->dwo_unit && cu->per_cu->is_debug_types)
15044     section = &cu->dwo_unit->dwo_file->sections.line;
15045   else if (cu->per_cu->is_dwz)
15046     {
15047       struct dwz_file *dwz = dwarf2_get_dwz_file ();
15048
15049       section = &dwz->line;
15050     }
15051   else
15052     section = &dwarf2_per_objfile->line;
15053
15054   return section;
15055 }
15056
15057 /* Read the statement program header starting at OFFSET in
15058    .debug_line, or .debug_line.dwo.  Return a pointer
15059    to a struct line_header, allocated using xmalloc.
15060
15061    NOTE: the strings in the include directory and file name tables of
15062    the returned object point into the dwarf line section buffer,
15063    and must not be freed.  */
15064
15065 static struct line_header *
15066 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15067 {
15068   struct cleanup *back_to;
15069   struct line_header *lh;
15070   gdb_byte *line_ptr;
15071   unsigned int bytes_read, offset_size;
15072   int i;
15073   char *cur_dir, *cur_file;
15074   struct dwarf2_section_info *section;
15075   bfd *abfd;
15076
15077   section = get_debug_line_section (cu);
15078   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15079   if (section->buffer == NULL)
15080     {
15081       if (cu->dwo_unit && cu->per_cu->is_debug_types)
15082         complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15083       else
15084         complaint (&symfile_complaints, _("missing .debug_line section"));
15085       return 0;
15086     }
15087
15088   /* We can't do this until we know the section is non-empty.
15089      Only then do we know we have such a section.  */
15090   abfd = section->asection->owner;
15091
15092   /* Make sure that at least there's room for the total_length field.
15093      That could be 12 bytes long, but we're just going to fudge that.  */
15094   if (offset + 4 >= section->size)
15095     {
15096       dwarf2_statement_list_fits_in_line_number_section_complaint ();
15097       return 0;
15098     }
15099
15100   lh = xmalloc (sizeof (*lh));
15101   memset (lh, 0, sizeof (*lh));
15102   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15103                           (void *) lh);
15104
15105   line_ptr = section->buffer + offset;
15106
15107   /* Read in the header.  */
15108   lh->total_length =
15109     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15110                                             &bytes_read, &offset_size);
15111   line_ptr += bytes_read;
15112   if (line_ptr + lh->total_length > (section->buffer + section->size))
15113     {
15114       dwarf2_statement_list_fits_in_line_number_section_complaint ();
15115       return 0;
15116     }
15117   lh->statement_program_end = line_ptr + lh->total_length;
15118   lh->version = read_2_bytes (abfd, line_ptr);
15119   line_ptr += 2;
15120   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15121   line_ptr += offset_size;
15122   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15123   line_ptr += 1;
15124   if (lh->version >= 4)
15125     {
15126       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15127       line_ptr += 1;
15128     }
15129   else
15130     lh->maximum_ops_per_instruction = 1;
15131
15132   if (lh->maximum_ops_per_instruction == 0)
15133     {
15134       lh->maximum_ops_per_instruction = 1;
15135       complaint (&symfile_complaints,
15136                  _("invalid maximum_ops_per_instruction "
15137                    "in `.debug_line' section"));
15138     }
15139
15140   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15141   line_ptr += 1;
15142   lh->line_base = read_1_signed_byte (abfd, line_ptr);
15143   line_ptr += 1;
15144   lh->line_range = read_1_byte (abfd, line_ptr);
15145   line_ptr += 1;
15146   lh->opcode_base = read_1_byte (abfd, line_ptr);
15147   line_ptr += 1;
15148   lh->standard_opcode_lengths
15149     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15150
15151   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
15152   for (i = 1; i < lh->opcode_base; ++i)
15153     {
15154       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15155       line_ptr += 1;
15156     }
15157
15158   /* Read directory table.  */
15159   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15160     {
15161       line_ptr += bytes_read;
15162       add_include_dir (lh, cur_dir);
15163     }
15164   line_ptr += bytes_read;
15165
15166   /* Read file name table.  */
15167   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15168     {
15169       unsigned int dir_index, mod_time, length;
15170
15171       line_ptr += bytes_read;
15172       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15173       line_ptr += bytes_read;
15174       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15175       line_ptr += bytes_read;
15176       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15177       line_ptr += bytes_read;
15178
15179       add_file_name (lh, cur_file, dir_index, mod_time, length);
15180     }
15181   line_ptr += bytes_read;
15182   lh->statement_program_start = line_ptr;
15183
15184   if (line_ptr > (section->buffer + section->size))
15185     complaint (&symfile_complaints,
15186                _("line number info header doesn't "
15187                  "fit in `.debug_line' section"));
15188
15189   discard_cleanups (back_to);
15190   return lh;
15191 }
15192
15193 /* Subroutine of dwarf_decode_lines to simplify it.
15194    Return the file name of the psymtab for included file FILE_INDEX
15195    in line header LH of PST.
15196    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15197    If space for the result is malloc'd, it will be freed by a cleanup.
15198    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15199
15200    The function creates dangling cleanup registration.  */
15201
15202 static char *
15203 psymtab_include_file_name (const struct line_header *lh, int file_index,
15204                            const struct partial_symtab *pst,
15205                            const char *comp_dir)
15206 {
15207   const struct file_entry fe = lh->file_names [file_index];
15208   char *include_name = fe.name;
15209   char *include_name_to_compare = include_name;
15210   char *dir_name = NULL;
15211   const char *pst_filename;
15212   char *copied_name = NULL;
15213   int file_is_pst;
15214
15215   if (fe.dir_index)
15216     dir_name = lh->include_dirs[fe.dir_index - 1];
15217
15218   if (!IS_ABSOLUTE_PATH (include_name)
15219       && (dir_name != NULL || comp_dir != NULL))
15220     {
15221       /* Avoid creating a duplicate psymtab for PST.
15222          We do this by comparing INCLUDE_NAME and PST_FILENAME.
15223          Before we do the comparison, however, we need to account
15224          for DIR_NAME and COMP_DIR.
15225          First prepend dir_name (if non-NULL).  If we still don't
15226          have an absolute path prepend comp_dir (if non-NULL).
15227          However, the directory we record in the include-file's
15228          psymtab does not contain COMP_DIR (to match the
15229          corresponding symtab(s)).
15230
15231          Example:
15232
15233          bash$ cd /tmp
15234          bash$ gcc -g ./hello.c
15235          include_name = "hello.c"
15236          dir_name = "."
15237          DW_AT_comp_dir = comp_dir = "/tmp"
15238          DW_AT_name = "./hello.c"  */
15239
15240       if (dir_name != NULL)
15241         {
15242           include_name = concat (dir_name, SLASH_STRING,
15243                                  include_name, (char *)NULL);
15244           include_name_to_compare = include_name;
15245           make_cleanup (xfree, include_name);
15246         }
15247       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15248         {
15249           include_name_to_compare = concat (comp_dir, SLASH_STRING,
15250                                             include_name, (char *)NULL);
15251         }
15252     }
15253
15254   pst_filename = pst->filename;
15255   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15256     {
15257       copied_name = concat (pst->dirname, SLASH_STRING,
15258                             pst_filename, (char *)NULL);
15259       pst_filename = copied_name;
15260     }
15261
15262   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15263
15264   if (include_name_to_compare != include_name)
15265     xfree (include_name_to_compare);
15266   if (copied_name != NULL)
15267     xfree (copied_name);
15268
15269   if (file_is_pst)
15270     return NULL;
15271   return include_name;
15272 }
15273
15274 /* Ignore this record_line request.  */
15275
15276 static void
15277 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15278 {
15279   return;
15280 }
15281
15282 /* Subroutine of dwarf_decode_lines to simplify it.
15283    Process the line number information in LH.  */
15284
15285 static void
15286 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15287                       struct dwarf2_cu *cu, struct partial_symtab *pst)
15288 {
15289   gdb_byte *line_ptr, *extended_end;
15290   gdb_byte *line_end;
15291   unsigned int bytes_read, extended_len;
15292   unsigned char op_code, extended_op, adj_opcode;
15293   CORE_ADDR baseaddr;
15294   struct objfile *objfile = cu->objfile;
15295   bfd *abfd = objfile->obfd;
15296   struct gdbarch *gdbarch = get_objfile_arch (objfile);
15297   const int decode_for_pst_p = (pst != NULL);
15298   struct subfile *last_subfile = NULL;
15299   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15300     = record_line;
15301
15302   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15303
15304   line_ptr = lh->statement_program_start;
15305   line_end = lh->statement_program_end;
15306
15307   /* Read the statement sequences until there's nothing left.  */
15308   while (line_ptr < line_end)
15309     {
15310       /* state machine registers  */
15311       CORE_ADDR address = 0;
15312       unsigned int file = 1;
15313       unsigned int line = 1;
15314       unsigned int column = 0;
15315       int is_stmt = lh->default_is_stmt;
15316       int basic_block = 0;
15317       int end_sequence = 0;
15318       CORE_ADDR addr;
15319       unsigned char op_index = 0;
15320
15321       if (!decode_for_pst_p && lh->num_file_names >= file)
15322         {
15323           /* Start a subfile for the current file of the state machine.  */
15324           /* lh->include_dirs and lh->file_names are 0-based, but the
15325              directory and file name numbers in the statement program
15326              are 1-based.  */
15327           struct file_entry *fe = &lh->file_names[file - 1];
15328           char *dir = NULL;
15329
15330           if (fe->dir_index)
15331             dir = lh->include_dirs[fe->dir_index - 1];
15332
15333           dwarf2_start_subfile (fe->name, dir, comp_dir);
15334         }
15335
15336       /* Decode the table.  */
15337       while (!end_sequence)
15338         {
15339           op_code = read_1_byte (abfd, line_ptr);
15340           line_ptr += 1;
15341           if (line_ptr > line_end)
15342             {
15343               dwarf2_debug_line_missing_end_sequence_complaint ();
15344               break;
15345             }
15346
15347           if (op_code >= lh->opcode_base)
15348             {
15349               /* Special operand.  */
15350               adj_opcode = op_code - lh->opcode_base;
15351               address += (((op_index + (adj_opcode / lh->line_range))
15352                            / lh->maximum_ops_per_instruction)
15353                           * lh->minimum_instruction_length);
15354               op_index = ((op_index + (adj_opcode / lh->line_range))
15355                           % lh->maximum_ops_per_instruction);
15356               line += lh->line_base + (adj_opcode % lh->line_range);
15357               if (lh->num_file_names < file || file == 0)
15358                 dwarf2_debug_line_missing_file_complaint ();
15359               /* For now we ignore lines not starting on an
15360                  instruction boundary.  */
15361               else if (op_index == 0)
15362                 {
15363                   lh->file_names[file - 1].included_p = 1;
15364                   if (!decode_for_pst_p && is_stmt)
15365                     {
15366                       if (last_subfile != current_subfile)
15367                         {
15368                           addr = gdbarch_addr_bits_remove (gdbarch, address);
15369                           if (last_subfile)
15370                             (*p_record_line) (last_subfile, 0, addr);
15371                           last_subfile = current_subfile;
15372                         }
15373                       /* Append row to matrix using current values.  */
15374                       addr = gdbarch_addr_bits_remove (gdbarch, address);
15375                       (*p_record_line) (current_subfile, line, addr);
15376                     }
15377                 }
15378               basic_block = 0;
15379             }
15380           else switch (op_code)
15381             {
15382             case DW_LNS_extended_op:
15383               extended_len = read_unsigned_leb128 (abfd, line_ptr,
15384                                                    &bytes_read);
15385               line_ptr += bytes_read;
15386               extended_end = line_ptr + extended_len;
15387               extended_op = read_1_byte (abfd, line_ptr);
15388               line_ptr += 1;
15389               switch (extended_op)
15390                 {
15391                 case DW_LNE_end_sequence:
15392                   p_record_line = record_line;
15393                   end_sequence = 1;
15394                   break;
15395                 case DW_LNE_set_address:
15396                   address = read_address (abfd, line_ptr, cu, &bytes_read);
15397
15398                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15399                     {
15400                       /* This line table is for a function which has been
15401                          GCd by the linker.  Ignore it.  PR gdb/12528 */
15402
15403                       long line_offset
15404                         = line_ptr - get_debug_line_section (cu)->buffer;
15405
15406                       complaint (&symfile_complaints,
15407                                  _(".debug_line address at offset 0x%lx is 0 "
15408                                    "[in module %s]"),
15409                                  line_offset, objfile->name);
15410                       p_record_line = noop_record_line;
15411                     }
15412
15413                   op_index = 0;
15414                   line_ptr += bytes_read;
15415                   address += baseaddr;
15416                   break;
15417                 case DW_LNE_define_file:
15418                   {
15419                     char *cur_file;
15420                     unsigned int dir_index, mod_time, length;
15421
15422                     cur_file = read_direct_string (abfd, line_ptr,
15423                                                    &bytes_read);
15424                     line_ptr += bytes_read;
15425                     dir_index =
15426                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15427                     line_ptr += bytes_read;
15428                     mod_time =
15429                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15430                     line_ptr += bytes_read;
15431                     length =
15432                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15433                     line_ptr += bytes_read;
15434                     add_file_name (lh, cur_file, dir_index, mod_time, length);
15435                   }
15436                   break;
15437                 case DW_LNE_set_discriminator:
15438                   /* The discriminator is not interesting to the debugger;
15439                      just ignore it.  */
15440                   line_ptr = extended_end;
15441                   break;
15442                 default:
15443                   complaint (&symfile_complaints,
15444                              _("mangled .debug_line section"));
15445                   return;
15446                 }
15447               /* Make sure that we parsed the extended op correctly.  If e.g.
15448                  we expected a different address size than the producer used,
15449                  we may have read the wrong number of bytes.  */
15450               if (line_ptr != extended_end)
15451                 {
15452                   complaint (&symfile_complaints,
15453                              _("mangled .debug_line section"));
15454                   return;
15455                 }
15456               break;
15457             case DW_LNS_copy:
15458               if (lh->num_file_names < file || file == 0)
15459                 dwarf2_debug_line_missing_file_complaint ();
15460               else
15461                 {
15462                   lh->file_names[file - 1].included_p = 1;
15463                   if (!decode_for_pst_p && is_stmt)
15464                     {
15465                       if (last_subfile != current_subfile)
15466                         {
15467                           addr = gdbarch_addr_bits_remove (gdbarch, address);
15468                           if (last_subfile)
15469                             (*p_record_line) (last_subfile, 0, addr);
15470                           last_subfile = current_subfile;
15471                         }
15472                       addr = gdbarch_addr_bits_remove (gdbarch, address);
15473                       (*p_record_line) (current_subfile, line, addr);
15474                     }
15475                 }
15476               basic_block = 0;
15477               break;
15478             case DW_LNS_advance_pc:
15479               {
15480                 CORE_ADDR adjust
15481                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15482
15483                 address += (((op_index + adjust)
15484                              / lh->maximum_ops_per_instruction)
15485                             * lh->minimum_instruction_length);
15486                 op_index = ((op_index + adjust)
15487                             % lh->maximum_ops_per_instruction);
15488                 line_ptr += bytes_read;
15489               }
15490               break;
15491             case DW_LNS_advance_line:
15492               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15493               line_ptr += bytes_read;
15494               break;
15495             case DW_LNS_set_file:
15496               {
15497                 /* The arrays lh->include_dirs and lh->file_names are
15498                    0-based, but the directory and file name numbers in
15499                    the statement program are 1-based.  */
15500                 struct file_entry *fe;
15501                 char *dir = NULL;
15502
15503                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15504                 line_ptr += bytes_read;
15505                 if (lh->num_file_names < file || file == 0)
15506                   dwarf2_debug_line_missing_file_complaint ();
15507                 else
15508                   {
15509                     fe = &lh->file_names[file - 1];
15510                     if (fe->dir_index)
15511                       dir = lh->include_dirs[fe->dir_index - 1];
15512                     if (!decode_for_pst_p)
15513                       {
15514                         last_subfile = current_subfile;
15515                         dwarf2_start_subfile (fe->name, dir, comp_dir);
15516                       }
15517                   }
15518               }
15519               break;
15520             case DW_LNS_set_column:
15521               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15522               line_ptr += bytes_read;
15523               break;
15524             case DW_LNS_negate_stmt:
15525               is_stmt = (!is_stmt);
15526               break;
15527             case DW_LNS_set_basic_block:
15528               basic_block = 1;
15529               break;
15530             /* Add to the address register of the state machine the
15531                address increment value corresponding to special opcode
15532                255.  I.e., this value is scaled by the minimum
15533                instruction length since special opcode 255 would have
15534                scaled the increment.  */
15535             case DW_LNS_const_add_pc:
15536               {
15537                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15538
15539                 address += (((op_index + adjust)
15540                              / lh->maximum_ops_per_instruction)
15541                             * lh->minimum_instruction_length);
15542                 op_index = ((op_index + adjust)
15543                             % lh->maximum_ops_per_instruction);
15544               }
15545               break;
15546             case DW_LNS_fixed_advance_pc:
15547               address += read_2_bytes (abfd, line_ptr);
15548               op_index = 0;
15549               line_ptr += 2;
15550               break;
15551             default:
15552               {
15553                 /* Unknown standard opcode, ignore it.  */
15554                 int i;
15555
15556                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15557                   {
15558                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15559                     line_ptr += bytes_read;
15560                   }
15561               }
15562             }
15563         }
15564       if (lh->num_file_names < file || file == 0)
15565         dwarf2_debug_line_missing_file_complaint ();
15566       else
15567         {
15568           lh->file_names[file - 1].included_p = 1;
15569           if (!decode_for_pst_p)
15570             {
15571               addr = gdbarch_addr_bits_remove (gdbarch, address);
15572               (*p_record_line) (current_subfile, 0, addr);
15573             }
15574         }
15575     }
15576 }
15577
15578 /* Decode the Line Number Program (LNP) for the given line_header
15579    structure and CU.  The actual information extracted and the type
15580    of structures created from the LNP depends on the value of PST.
15581
15582    1. If PST is NULL, then this procedure uses the data from the program
15583       to create all necessary symbol tables, and their linetables.
15584
15585    2. If PST is not NULL, this procedure reads the program to determine
15586       the list of files included by the unit represented by PST, and
15587       builds all the associated partial symbol tables.
15588
15589    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15590    It is used for relative paths in the line table.
15591    NOTE: When processing partial symtabs (pst != NULL),
15592    comp_dir == pst->dirname.
15593
15594    NOTE: It is important that psymtabs have the same file name (via strcmp)
15595    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
15596    symtab we don't use it in the name of the psymtabs we create.
15597    E.g. expand_line_sal requires this when finding psymtabs to expand.
15598    A good testcase for this is mb-inline.exp.  */
15599
15600 static void
15601 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15602                     struct dwarf2_cu *cu, struct partial_symtab *pst,
15603                     int want_line_info)
15604 {
15605   struct objfile *objfile = cu->objfile;
15606   const int decode_for_pst_p = (pst != NULL);
15607   struct subfile *first_subfile = current_subfile;
15608
15609   if (want_line_info)
15610     dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15611
15612   if (decode_for_pst_p)
15613     {
15614       int file_index;
15615
15616       /* Now that we're done scanning the Line Header Program, we can
15617          create the psymtab of each included file.  */
15618       for (file_index = 0; file_index < lh->num_file_names; file_index++)
15619         if (lh->file_names[file_index].included_p == 1)
15620           {
15621             char *include_name =
15622               psymtab_include_file_name (lh, file_index, pst, comp_dir);
15623             if (include_name != NULL)
15624               dwarf2_create_include_psymtab (include_name, pst, objfile);
15625           }
15626     }
15627   else
15628     {
15629       /* Make sure a symtab is created for every file, even files
15630          which contain only variables (i.e. no code with associated
15631          line numbers).  */
15632       int i;
15633
15634       for (i = 0; i < lh->num_file_names; i++)
15635         {
15636           char *dir = NULL;
15637           struct file_entry *fe;
15638
15639           fe = &lh->file_names[i];
15640           if (fe->dir_index)
15641             dir = lh->include_dirs[fe->dir_index - 1];
15642           dwarf2_start_subfile (fe->name, dir, comp_dir);
15643
15644           /* Skip the main file; we don't need it, and it must be
15645              allocated last, so that it will show up before the
15646              non-primary symtabs in the objfile's symtab list.  */
15647           if (current_subfile == first_subfile)
15648             continue;
15649
15650           if (current_subfile->symtab == NULL)
15651             current_subfile->symtab = allocate_symtab (current_subfile->name,
15652                                                        objfile);
15653           fe->symtab = current_subfile->symtab;
15654         }
15655     }
15656 }
15657
15658 /* Start a subfile for DWARF.  FILENAME is the name of the file and
15659    DIRNAME the name of the source directory which contains FILENAME
15660    or NULL if not known.  COMP_DIR is the compilation directory for the
15661    linetable's compilation unit or NULL if not known.
15662    This routine tries to keep line numbers from identical absolute and
15663    relative file names in a common subfile.
15664
15665    Using the `list' example from the GDB testsuite, which resides in
15666    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15667    of /srcdir/list0.c yields the following debugging information for list0.c:
15668
15669    DW_AT_name:          /srcdir/list0.c
15670    DW_AT_comp_dir:              /compdir
15671    files.files[0].name: list0.h
15672    files.files[0].dir:  /srcdir
15673    files.files[1].name: list0.c
15674    files.files[1].dir:  /srcdir
15675
15676    The line number information for list0.c has to end up in a single
15677    subfile, so that `break /srcdir/list0.c:1' works as expected.
15678    start_subfile will ensure that this happens provided that we pass the
15679    concatenation of files.files[1].dir and files.files[1].name as the
15680    subfile's name.  */
15681
15682 static void
15683 dwarf2_start_subfile (char *filename, const char *dirname,
15684                       const char *comp_dir)
15685 {
15686   char *fullname;
15687
15688   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15689      `start_symtab' will always pass the contents of DW_AT_comp_dir as
15690      second argument to start_subfile.  To be consistent, we do the
15691      same here.  In order not to lose the line information directory,
15692      we concatenate it to the filename when it makes sense.
15693      Note that the Dwarf3 standard says (speaking of filenames in line
15694      information): ``The directory index is ignored for file names
15695      that represent full path names''.  Thus ignoring dirname in the
15696      `else' branch below isn't an issue.  */
15697
15698   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15699     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15700   else
15701     fullname = filename;
15702
15703   start_subfile (fullname, comp_dir);
15704
15705   if (fullname != filename)
15706     xfree (fullname);
15707 }
15708
15709 /* Start a symtab for DWARF.
15710    NAME, COMP_DIR, LOW_PC are passed to start_symtab.  */
15711
15712 static void
15713 dwarf2_start_symtab (struct dwarf2_cu *cu,
15714                      const char *name, const char *comp_dir, CORE_ADDR low_pc)
15715 {
15716   start_symtab (name, comp_dir, low_pc);
15717   record_debugformat ("DWARF 2");
15718   record_producer (cu->producer);
15719
15720   /* We assume that we're processing GCC output.  */
15721   processing_gcc_compilation = 2;
15722
15723   cu->processing_has_namespace_info = 0;
15724 }
15725
15726 static void
15727 var_decode_location (struct attribute *attr, struct symbol *sym,
15728                      struct dwarf2_cu *cu)
15729 {
15730   struct objfile *objfile = cu->objfile;
15731   struct comp_unit_head *cu_header = &cu->header;
15732
15733   /* NOTE drow/2003-01-30: There used to be a comment and some special
15734      code here to turn a symbol with DW_AT_external and a
15735      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
15736      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15737      with some versions of binutils) where shared libraries could have
15738      relocations against symbols in their debug information - the
15739      minimal symbol would have the right address, but the debug info
15740      would not.  It's no longer necessary, because we will explicitly
15741      apply relocations when we read in the debug information now.  */
15742
15743   /* A DW_AT_location attribute with no contents indicates that a
15744      variable has been optimized away.  */
15745   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15746     {
15747       SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15748       return;
15749     }
15750
15751   /* Handle one degenerate form of location expression specially, to
15752      preserve GDB's previous behavior when section offsets are
15753      specified.  If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15754      then mark this symbol as LOC_STATIC.  */
15755
15756   if (attr_form_is_block (attr)
15757       && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15758            && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15759           || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15760               && (DW_BLOCK (attr)->size
15761                   == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15762     {
15763       unsigned int dummy;
15764
15765       if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15766         SYMBOL_VALUE_ADDRESS (sym) =
15767           read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15768       else
15769         SYMBOL_VALUE_ADDRESS (sym) =
15770           read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15771       SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
15772       fixup_symbol_section (sym, objfile);
15773       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15774                                               SYMBOL_SECTION (sym));
15775       return;
15776     }
15777
15778   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15779      expression evaluator, and use LOC_COMPUTED only when necessary
15780      (i.e. when the value of a register or memory location is
15781      referenced, or a thread-local block, etc.).  Then again, it might
15782      not be worthwhile.  I'm assuming that it isn't unless performance
15783      or memory numbers show me otherwise.  */
15784
15785   dwarf2_symbol_mark_computed (attr, sym, cu, 0);
15786
15787   if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
15788     cu->has_loclist = 1;
15789 }
15790
15791 /* Given a pointer to a DWARF information entry, figure out if we need
15792    to make a symbol table entry for it, and if so, create a new entry
15793    and return a pointer to it.
15794    If TYPE is NULL, determine symbol type from the die, otherwise
15795    used the passed type.
15796    If SPACE is not NULL, use it to hold the new symbol.  If it is
15797    NULL, allocate a new symbol on the objfile's obstack.  */
15798
15799 static struct symbol *
15800 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15801                  struct symbol *space)
15802 {
15803   struct objfile *objfile = cu->objfile;
15804   struct symbol *sym = NULL;
15805   const char *name;
15806   struct attribute *attr = NULL;
15807   struct attribute *attr2 = NULL;
15808   CORE_ADDR baseaddr;
15809   struct pending **list_to_add = NULL;
15810
15811   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15812
15813   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15814
15815   name = dwarf2_name (die, cu);
15816   if (name)
15817     {
15818       const char *linkagename;
15819       int suppress_add = 0;
15820
15821       if (space)
15822         sym = space;
15823       else
15824         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15825       OBJSTAT (objfile, n_syms++);
15826
15827       /* Cache this symbol's name and the name's demangled form (if any).  */
15828       SYMBOL_SET_LANGUAGE (sym, cu->language);
15829       linkagename = dwarf2_physname (name, die, cu);
15830       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15831
15832       /* Fortran does not have mangling standard and the mangling does differ
15833          between gfortran, iFort etc.  */
15834       if (cu->language == language_fortran
15835           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15836         symbol_set_demangled_name (&(sym->ginfo),
15837                                    dwarf2_full_name (name, die, cu),
15838                                    NULL);
15839
15840       /* Default assumptions.
15841          Use the passed type or decode it from the die.  */
15842       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15843       SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15844       if (type != NULL)
15845         SYMBOL_TYPE (sym) = type;
15846       else
15847         SYMBOL_TYPE (sym) = die_type (die, cu);
15848       attr = dwarf2_attr (die,
15849                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15850                           cu);
15851       if (attr)
15852         {
15853           SYMBOL_LINE (sym) = DW_UNSND (attr);
15854         }
15855
15856       attr = dwarf2_attr (die,
15857                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15858                           cu);
15859       if (attr)
15860         {
15861           int file_index = DW_UNSND (attr);
15862
15863           if (cu->line_header == NULL
15864               || file_index > cu->line_header->num_file_names)
15865             complaint (&symfile_complaints,
15866                        _("file index out of range"));
15867           else if (file_index > 0)
15868             {
15869               struct file_entry *fe;
15870
15871               fe = &cu->line_header->file_names[file_index - 1];
15872               SYMBOL_SYMTAB (sym) = fe->symtab;
15873             }
15874         }
15875
15876       switch (die->tag)
15877         {
15878         case DW_TAG_label:
15879           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15880           if (attr)
15881             {
15882               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15883             }
15884           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15885           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15886           SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
15887           add_symbol_to_list (sym, cu->list_in_scope);
15888           break;
15889         case DW_TAG_subprogram:
15890           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15891              finish_block.  */
15892           SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15893           attr2 = dwarf2_attr (die, DW_AT_external, cu);
15894           if ((attr2 && (DW_UNSND (attr2) != 0))
15895               || cu->language == language_ada)
15896             {
15897               /* Subprograms marked external are stored as a global symbol.
15898                  Ada subprograms, whether marked external or not, are always
15899                  stored as a global symbol, because we want to be able to
15900                  access them globally.  For instance, we want to be able
15901                  to break on a nested subprogram without having to
15902                  specify the context.  */
15903               list_to_add = &global_symbols;
15904             }
15905           else
15906             {
15907               list_to_add = cu->list_in_scope;
15908             }
15909           break;
15910         case DW_TAG_inlined_subroutine:
15911           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15912              finish_block.  */
15913           SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15914           SYMBOL_INLINED (sym) = 1;
15915           list_to_add = cu->list_in_scope;
15916           break;
15917         case DW_TAG_template_value_param:
15918           suppress_add = 1;
15919           /* Fall through.  */
15920         case DW_TAG_constant:
15921         case DW_TAG_variable:
15922         case DW_TAG_member:
15923           /* Compilation with minimal debug info may result in
15924              variables with missing type entries.  Change the
15925              misleading `void' type to something sensible.  */
15926           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
15927             SYMBOL_TYPE (sym)
15928               = objfile_type (objfile)->nodebug_data_symbol;
15929
15930           attr = dwarf2_attr (die, DW_AT_const_value, cu);
15931           /* In the case of DW_TAG_member, we should only be called for
15932              static const members.  */
15933           if (die->tag == DW_TAG_member)
15934             {
15935               /* dwarf2_add_field uses die_is_declaration,
15936                  so we do the same.  */
15937               gdb_assert (die_is_declaration (die, cu));
15938               gdb_assert (attr);
15939             }
15940           if (attr)
15941             {
15942               dwarf2_const_value (attr, sym, cu);
15943               attr2 = dwarf2_attr (die, DW_AT_external, cu);
15944               if (!suppress_add)
15945                 {
15946                   if (attr2 && (DW_UNSND (attr2) != 0))
15947                     list_to_add = &global_symbols;
15948                   else
15949                     list_to_add = cu->list_in_scope;
15950                 }
15951               break;
15952             }
15953           attr = dwarf2_attr (die, DW_AT_location, cu);
15954           if (attr)
15955             {
15956               var_decode_location (attr, sym, cu);
15957               attr2 = dwarf2_attr (die, DW_AT_external, cu);
15958
15959               /* Fortran explicitly imports any global symbols to the local
15960                  scope by DW_TAG_common_block.  */
15961               if (cu->language == language_fortran && die->parent
15962                   && die->parent->tag == DW_TAG_common_block)
15963                 attr2 = NULL;
15964
15965               if (SYMBOL_CLASS (sym) == LOC_STATIC
15966                   && SYMBOL_VALUE_ADDRESS (sym) == 0
15967                   && !dwarf2_per_objfile->has_section_at_zero)
15968                 {
15969                   /* When a static variable is eliminated by the linker,
15970                      the corresponding debug information is not stripped
15971                      out, but the variable address is set to null;
15972                      do not add such variables into symbol table.  */
15973                 }
15974               else if (attr2 && (DW_UNSND (attr2) != 0))
15975                 {
15976                   /* Workaround gfortran PR debug/40040 - it uses
15977                      DW_AT_location for variables in -fPIC libraries which may
15978                      get overriden by other libraries/executable and get
15979                      a different address.  Resolve it by the minimal symbol
15980                      which may come from inferior's executable using copy
15981                      relocation.  Make this workaround only for gfortran as for
15982                      other compilers GDB cannot guess the minimal symbol
15983                      Fortran mangling kind.  */
15984                   if (cu->language == language_fortran && die->parent
15985                       && die->parent->tag == DW_TAG_module
15986                       && cu->producer
15987                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15988                     SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
15989
15990                   /* A variable with DW_AT_external is never static,
15991                      but it may be block-scoped.  */
15992                   list_to_add = (cu->list_in_scope == &file_symbols
15993                                  ? &global_symbols : cu->list_in_scope);
15994                 }
15995               else
15996                 list_to_add = cu->list_in_scope;
15997             }
15998           else
15999             {
16000               /* We do not know the address of this symbol.
16001                  If it is an external symbol and we have type information
16002                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
16003                  The address of the variable will then be determined from
16004                  the minimal symbol table whenever the variable is
16005                  referenced.  */
16006               attr2 = dwarf2_attr (die, DW_AT_external, cu);
16007
16008               /* Fortran explicitly imports any global symbols to the local
16009                  scope by DW_TAG_common_block.  */
16010               if (cu->language == language_fortran && die->parent
16011                   && die->parent->tag == DW_TAG_common_block)
16012                 {
16013                   /* SYMBOL_CLASS doesn't matter here because
16014                      read_common_block is going to reset it.  */
16015                   if (!suppress_add)
16016                     list_to_add = cu->list_in_scope;
16017                 }
16018               else if (attr2 && (DW_UNSND (attr2) != 0)
16019                        && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16020                 {
16021                   /* A variable with DW_AT_external is never static, but it
16022                      may be block-scoped.  */
16023                   list_to_add = (cu->list_in_scope == &file_symbols
16024                                  ? &global_symbols : cu->list_in_scope);
16025
16026                   SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16027                 }
16028               else if (!die_is_declaration (die, cu))
16029                 {
16030                   /* Use the default LOC_OPTIMIZED_OUT class.  */
16031                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16032                   if (!suppress_add)
16033                     list_to_add = cu->list_in_scope;
16034                 }
16035             }
16036           break;
16037         case DW_TAG_formal_parameter:
16038           /* If we are inside a function, mark this as an argument.  If
16039              not, we might be looking at an argument to an inlined function
16040              when we do not have enough information to show inlined frames;
16041              pretend it's a local variable in that case so that the user can
16042              still see it.  */
16043           if (context_stack_depth > 0
16044               && context_stack[context_stack_depth - 1].name != NULL)
16045             SYMBOL_IS_ARGUMENT (sym) = 1;
16046           attr = dwarf2_attr (die, DW_AT_location, cu);
16047           if (attr)
16048             {
16049               var_decode_location (attr, sym, cu);
16050             }
16051           attr = dwarf2_attr (die, DW_AT_const_value, cu);
16052           if (attr)
16053             {
16054               dwarf2_const_value (attr, sym, cu);
16055             }
16056
16057           list_to_add = cu->list_in_scope;
16058           break;
16059         case DW_TAG_unspecified_parameters:
16060           /* From varargs functions; gdb doesn't seem to have any
16061              interest in this information, so just ignore it for now.
16062              (FIXME?) */
16063           break;
16064         case DW_TAG_template_type_param:
16065           suppress_add = 1;
16066           /* Fall through.  */
16067         case DW_TAG_class_type:
16068         case DW_TAG_interface_type:
16069         case DW_TAG_structure_type:
16070         case DW_TAG_union_type:
16071         case DW_TAG_set_type:
16072         case DW_TAG_enumeration_type:
16073           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16074           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16075
16076           {
16077             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16078                really ever be static objects: otherwise, if you try
16079                to, say, break of a class's method and you're in a file
16080                which doesn't mention that class, it won't work unless
16081                the check for all static symbols in lookup_symbol_aux
16082                saves you.  See the OtherFileClass tests in
16083                gdb.c++/namespace.exp.  */
16084
16085             if (!suppress_add)
16086               {
16087                 list_to_add = (cu->list_in_scope == &file_symbols
16088                                && (cu->language == language_cplus
16089                                    || cu->language == language_java)
16090                                ? &global_symbols : cu->list_in_scope);
16091
16092                 /* The semantics of C++ state that "struct foo {
16093                    ... }" also defines a typedef for "foo".  A Java
16094                    class declaration also defines a typedef for the
16095                    class.  */
16096                 if (cu->language == language_cplus
16097                     || cu->language == language_java
16098                     || cu->language == language_ada)
16099                   {
16100                     /* The symbol's name is already allocated along
16101                        with this objfile, so we don't need to
16102                        duplicate it for the type.  */
16103                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16104                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16105                   }
16106               }
16107           }
16108           break;
16109         case DW_TAG_typedef:
16110           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16111           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16112           list_to_add = cu->list_in_scope;
16113           break;
16114         case DW_TAG_base_type:
16115         case DW_TAG_subrange_type:
16116           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16117           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16118           list_to_add = cu->list_in_scope;
16119           break;
16120         case DW_TAG_enumerator:
16121           attr = dwarf2_attr (die, DW_AT_const_value, cu);
16122           if (attr)
16123             {
16124               dwarf2_const_value (attr, sym, cu);
16125             }
16126           {
16127             /* NOTE: carlton/2003-11-10: See comment above in the
16128                DW_TAG_class_type, etc. block.  */
16129
16130             list_to_add = (cu->list_in_scope == &file_symbols
16131                            && (cu->language == language_cplus
16132                                || cu->language == language_java)
16133                            ? &global_symbols : cu->list_in_scope);
16134           }
16135           break;
16136         case DW_TAG_namespace:
16137           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16138           list_to_add = &global_symbols;
16139           break;
16140         case DW_TAG_common_block:
16141           SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16142           SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16143           add_symbol_to_list (sym, cu->list_in_scope);
16144           break;
16145         default:
16146           /* Not a tag we recognize.  Hopefully we aren't processing
16147              trash data, but since we must specifically ignore things
16148              we don't recognize, there is nothing else we should do at
16149              this point.  */
16150           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16151                      dwarf_tag_name (die->tag));
16152           break;
16153         }
16154
16155       if (suppress_add)
16156         {
16157           sym->hash_next = objfile->template_symbols;
16158           objfile->template_symbols = sym;
16159           list_to_add = NULL;
16160         }
16161
16162       if (list_to_add != NULL)
16163         add_symbol_to_list (sym, list_to_add);
16164
16165       /* For the benefit of old versions of GCC, check for anonymous
16166          namespaces based on the demangled name.  */
16167       if (!cu->processing_has_namespace_info
16168           && cu->language == language_cplus)
16169         cp_scan_for_anonymous_namespaces (sym, objfile);
16170     }
16171   return (sym);
16172 }
16173
16174 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
16175
16176 static struct symbol *
16177 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16178 {
16179   return new_symbol_full (die, type, cu, NULL);
16180 }
16181
16182 /* Given an attr with a DW_FORM_dataN value in host byte order,
16183    zero-extend it as appropriate for the symbol's type.  The DWARF
16184    standard (v4) is not entirely clear about the meaning of using
16185    DW_FORM_dataN for a constant with a signed type, where the type is
16186    wider than the data.  The conclusion of a discussion on the DWARF
16187    list was that this is unspecified.  We choose to always zero-extend
16188    because that is the interpretation long in use by GCC.  */
16189
16190 static gdb_byte *
16191 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16192                          const char *name, struct obstack *obstack,
16193                          struct dwarf2_cu *cu, LONGEST *value, int bits)
16194 {
16195   struct objfile *objfile = cu->objfile;
16196   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16197                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16198   LONGEST l = DW_UNSND (attr);
16199
16200   if (bits < sizeof (*value) * 8)
16201     {
16202       l &= ((LONGEST) 1 << bits) - 1;
16203       *value = l;
16204     }
16205   else if (bits == sizeof (*value) * 8)
16206     *value = l;
16207   else
16208     {
16209       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16210       store_unsigned_integer (bytes, bits / 8, byte_order, l);
16211       return bytes;
16212     }
16213
16214   return NULL;
16215 }
16216
16217 /* Read a constant value from an attribute.  Either set *VALUE, or if
16218    the value does not fit in *VALUE, set *BYTES - either already
16219    allocated on the objfile obstack, or newly allocated on OBSTACK,
16220    or, set *BATON, if we translated the constant to a location
16221    expression.  */
16222
16223 static void
16224 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16225                          const char *name, struct obstack *obstack,
16226                          struct dwarf2_cu *cu,
16227                          LONGEST *value, gdb_byte **bytes,
16228                          struct dwarf2_locexpr_baton **baton)
16229 {
16230   struct objfile *objfile = cu->objfile;
16231   struct comp_unit_head *cu_header = &cu->header;
16232   struct dwarf_block *blk;
16233   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16234                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16235
16236   *value = 0;
16237   *bytes = NULL;
16238   *baton = NULL;
16239
16240   switch (attr->form)
16241     {
16242     case DW_FORM_addr:
16243     case DW_FORM_GNU_addr_index:
16244       {
16245         gdb_byte *data;
16246
16247         if (TYPE_LENGTH (type) != cu_header->addr_size)
16248           dwarf2_const_value_length_mismatch_complaint (name,
16249                                                         cu_header->addr_size,
16250                                                         TYPE_LENGTH (type));
16251         /* Symbols of this form are reasonably rare, so we just
16252            piggyback on the existing location code rather than writing
16253            a new implementation of symbol_computed_ops.  */
16254         *baton = obstack_alloc (&objfile->objfile_obstack,
16255                                 sizeof (struct dwarf2_locexpr_baton));
16256         (*baton)->per_cu = cu->per_cu;
16257         gdb_assert ((*baton)->per_cu);
16258
16259         (*baton)->size = 2 + cu_header->addr_size;
16260         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16261         (*baton)->data = data;
16262
16263         data[0] = DW_OP_addr;
16264         store_unsigned_integer (&data[1], cu_header->addr_size,
16265                                 byte_order, DW_ADDR (attr));
16266         data[cu_header->addr_size + 1] = DW_OP_stack_value;
16267       }
16268       break;
16269     case DW_FORM_string:
16270     case DW_FORM_strp:
16271     case DW_FORM_GNU_str_index:
16272     case DW_FORM_GNU_strp_alt:
16273       /* DW_STRING is already allocated on the objfile obstack, point
16274          directly to it.  */
16275       *bytes = (gdb_byte *) DW_STRING (attr);
16276       break;
16277     case DW_FORM_block1:
16278     case DW_FORM_block2:
16279     case DW_FORM_block4:
16280     case DW_FORM_block:
16281     case DW_FORM_exprloc:
16282       blk = DW_BLOCK (attr);
16283       if (TYPE_LENGTH (type) != blk->size)
16284         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16285                                                       TYPE_LENGTH (type));
16286       *bytes = blk->data;
16287       break;
16288
16289       /* The DW_AT_const_value attributes are supposed to carry the
16290          symbol's value "represented as it would be on the target
16291          architecture."  By the time we get here, it's already been
16292          converted to host endianness, so we just need to sign- or
16293          zero-extend it as appropriate.  */
16294     case DW_FORM_data1:
16295       *bytes = dwarf2_const_value_data (attr, type, name,
16296                                         obstack, cu, value, 8);
16297       break;
16298     case DW_FORM_data2:
16299       *bytes = dwarf2_const_value_data (attr, type, name,
16300                                         obstack, cu, value, 16);
16301       break;
16302     case DW_FORM_data4:
16303       *bytes = dwarf2_const_value_data (attr, type, name,
16304                                         obstack, cu, value, 32);
16305       break;
16306     case DW_FORM_data8:
16307       *bytes = dwarf2_const_value_data (attr, type, name,
16308                                         obstack, cu, value, 64);
16309       break;
16310
16311     case DW_FORM_sdata:
16312       *value = DW_SND (attr);
16313       break;
16314
16315     case DW_FORM_udata:
16316       *value = DW_UNSND (attr);
16317       break;
16318
16319     default:
16320       complaint (&symfile_complaints,
16321                  _("unsupported const value attribute form: '%s'"),
16322                  dwarf_form_name (attr->form));
16323       *value = 0;
16324       break;
16325     }
16326 }
16327
16328
16329 /* Copy constant value from an attribute to a symbol.  */
16330
16331 static void
16332 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16333                     struct dwarf2_cu *cu)
16334 {
16335   struct objfile *objfile = cu->objfile;
16336   struct comp_unit_head *cu_header = &cu->header;
16337   LONGEST value;
16338   gdb_byte *bytes;
16339   struct dwarf2_locexpr_baton *baton;
16340
16341   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16342                            SYMBOL_PRINT_NAME (sym),
16343                            &objfile->objfile_obstack, cu,
16344                            &value, &bytes, &baton);
16345
16346   if (baton != NULL)
16347     {
16348       SYMBOL_LOCATION_BATON (sym) = baton;
16349       SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16350     }
16351   else if (bytes != NULL)
16352      {
16353       SYMBOL_VALUE_BYTES (sym) = bytes;
16354       SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
16355     }
16356   else
16357     {
16358       SYMBOL_VALUE (sym) = value;
16359       SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
16360     }
16361 }
16362
16363 /* Return the type of the die in question using its DW_AT_type attribute.  */
16364
16365 static struct type *
16366 die_type (struct die_info *die, struct dwarf2_cu *cu)
16367 {
16368   struct attribute *type_attr;
16369
16370   type_attr = dwarf2_attr (die, DW_AT_type, cu);
16371   if (!type_attr)
16372     {
16373       /* A missing DW_AT_type represents a void type.  */
16374       return objfile_type (cu->objfile)->builtin_void;
16375     }
16376
16377   return lookup_die_type (die, type_attr, cu);
16378 }
16379
16380 /* True iff CU's producer generates GNAT Ada auxiliary information
16381    that allows to find parallel types through that information instead
16382    of having to do expensive parallel lookups by type name.  */
16383
16384 static int
16385 need_gnat_info (struct dwarf2_cu *cu)
16386 {
16387   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16388      of GNAT produces this auxiliary information, without any indication
16389      that it is produced.  Part of enhancing the FSF version of GNAT
16390      to produce that information will be to put in place an indicator
16391      that we can use in order to determine whether the descriptive type
16392      info is available or not.  One suggestion that has been made is
16393      to use a new attribute, attached to the CU die.  For now, assume
16394      that the descriptive type info is not available.  */
16395   return 0;
16396 }
16397
16398 /* Return the auxiliary type of the die in question using its
16399    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
16400    attribute is not present.  */
16401
16402 static struct type *
16403 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16404 {
16405   struct attribute *type_attr;
16406
16407   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16408   if (!type_attr)
16409     return NULL;
16410
16411   return lookup_die_type (die, type_attr, cu);
16412 }
16413
16414 /* If DIE has a descriptive_type attribute, then set the TYPE's
16415    descriptive type accordingly.  */
16416
16417 static void
16418 set_descriptive_type (struct type *type, struct die_info *die,
16419                       struct dwarf2_cu *cu)
16420 {
16421   struct type *descriptive_type = die_descriptive_type (die, cu);
16422
16423   if (descriptive_type)
16424     {
16425       ALLOCATE_GNAT_AUX_TYPE (type);
16426       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16427     }
16428 }
16429
16430 /* Return the containing type of the die in question using its
16431    DW_AT_containing_type attribute.  */
16432
16433 static struct type *
16434 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16435 {
16436   struct attribute *type_attr;
16437
16438   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16439   if (!type_attr)
16440     error (_("Dwarf Error: Problem turning containing type into gdb type "
16441              "[in module %s]"), cu->objfile->name);
16442
16443   return lookup_die_type (die, type_attr, cu);
16444 }
16445
16446 /* Look up the type of DIE in CU using its type attribute ATTR.
16447    If there is no type substitute an error marker.  */
16448
16449 static struct type *
16450 lookup_die_type (struct die_info *die, struct attribute *attr,
16451                  struct dwarf2_cu *cu)
16452 {
16453   struct objfile *objfile = cu->objfile;
16454   struct type *this_type;
16455
16456   /* First see if we have it cached.  */
16457
16458   if (attr->form == DW_FORM_GNU_ref_alt)
16459     {
16460       struct dwarf2_per_cu_data *per_cu;
16461       sect_offset offset = dwarf2_get_ref_die_offset (attr);
16462
16463       per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16464       this_type = get_die_type_at_offset (offset, per_cu);
16465     }
16466   else if (is_ref_attr (attr))
16467     {
16468       sect_offset offset = dwarf2_get_ref_die_offset (attr);
16469
16470       this_type = get_die_type_at_offset (offset, cu->per_cu);
16471     }
16472   else if (attr->form == DW_FORM_ref_sig8)
16473     {
16474       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16475
16476       /* sig_type will be NULL if the signatured type is missing from
16477          the debug info.  */
16478       if (sig_type == NULL)
16479         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16480                  "at 0x%x [in module %s]"),
16481                die->offset.sect_off, objfile->name);
16482
16483       gdb_assert (sig_type->per_cu.is_debug_types);
16484       /* If we haven't filled in type_offset_in_section yet, then we
16485          haven't read the type in yet.  */
16486       this_type = NULL;
16487       if (sig_type->type_offset_in_section.sect_off != 0)
16488         {
16489           this_type =
16490             get_die_type_at_offset (sig_type->type_offset_in_section,
16491                                     &sig_type->per_cu);
16492         }
16493     }
16494   else
16495     {
16496       dump_die_for_error (die);
16497       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16498              dwarf_attr_name (attr->name), objfile->name);
16499     }
16500
16501   /* If not cached we need to read it in.  */
16502
16503   if (this_type == NULL)
16504     {
16505       struct die_info *type_die;
16506       struct dwarf2_cu *type_cu = cu;
16507
16508       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16509       /* If we found the type now, it's probably because the type came
16510          from an inter-CU reference and the type's CU got expanded before
16511          ours.  */
16512       this_type = get_die_type (type_die, type_cu);
16513       if (this_type == NULL)
16514         this_type = read_type_die_1 (type_die, type_cu);
16515     }
16516
16517   /* If we still don't have a type use an error marker.  */
16518
16519   if (this_type == NULL)
16520     {
16521       char *message, *saved;
16522
16523       /* read_type_die already issued a complaint.  */
16524       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16525                             objfile->name,
16526                             cu->header.offset.sect_off,
16527                             die->offset.sect_off);
16528       saved = obstack_copy0 (&objfile->objfile_obstack,
16529                              message, strlen (message));
16530       xfree (message);
16531
16532       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16533     }
16534
16535   return this_type;
16536 }
16537
16538 /* Return the type in DIE, CU.
16539    Returns NULL for invalid types.
16540
16541    This first does a lookup in the appropriate type_hash table,
16542    and only reads the die in if necessary.
16543
16544    NOTE: This can be called when reading in partial or full symbols.  */
16545
16546 static struct type *
16547 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16548 {
16549   struct type *this_type;
16550
16551   this_type = get_die_type (die, cu);
16552   if (this_type)
16553     return this_type;
16554
16555   return read_type_die_1 (die, cu);
16556 }
16557
16558 /* Read the type in DIE, CU.
16559    Returns NULL for invalid types.  */
16560
16561 static struct type *
16562 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16563 {
16564   struct type *this_type = NULL;
16565
16566   switch (die->tag)
16567     {
16568     case DW_TAG_class_type:
16569     case DW_TAG_interface_type:
16570     case DW_TAG_structure_type:
16571     case DW_TAG_union_type:
16572       this_type = read_structure_type (die, cu);
16573       break;
16574     case DW_TAG_enumeration_type:
16575       this_type = read_enumeration_type (die, cu);
16576       break;
16577     case DW_TAG_subprogram:
16578     case DW_TAG_subroutine_type:
16579     case DW_TAG_inlined_subroutine:
16580       this_type = read_subroutine_type (die, cu);
16581       break;
16582     case DW_TAG_array_type:
16583       this_type = read_array_type (die, cu);
16584       break;
16585     case DW_TAG_set_type:
16586       this_type = read_set_type (die, cu);
16587       break;
16588     case DW_TAG_pointer_type:
16589       this_type = read_tag_pointer_type (die, cu);
16590       break;
16591     case DW_TAG_ptr_to_member_type:
16592       this_type = read_tag_ptr_to_member_type (die, cu);
16593       break;
16594     case DW_TAG_reference_type:
16595       this_type = read_tag_reference_type (die, cu);
16596       break;
16597     case DW_TAG_const_type:
16598       this_type = read_tag_const_type (die, cu);
16599       break;
16600     case DW_TAG_volatile_type:
16601       this_type = read_tag_volatile_type (die, cu);
16602       break;
16603     case DW_TAG_restrict_type:
16604       this_type = read_tag_restrict_type (die, cu);
16605       break;
16606     case DW_TAG_string_type:
16607       this_type = read_tag_string_type (die, cu);
16608       break;
16609     case DW_TAG_typedef:
16610       this_type = read_typedef (die, cu);
16611       break;
16612     case DW_TAG_subrange_type:
16613       this_type = read_subrange_type (die, cu);
16614       break;
16615     case DW_TAG_base_type:
16616       this_type = read_base_type (die, cu);
16617       break;
16618     case DW_TAG_unspecified_type:
16619       this_type = read_unspecified_type (die, cu);
16620       break;
16621     case DW_TAG_namespace:
16622       this_type = read_namespace_type (die, cu);
16623       break;
16624     case DW_TAG_module:
16625       this_type = read_module_type (die, cu);
16626       break;
16627     default:
16628       complaint (&symfile_complaints,
16629                  _("unexpected tag in read_type_die: '%s'"),
16630                  dwarf_tag_name (die->tag));
16631       break;
16632     }
16633
16634   return this_type;
16635 }
16636
16637 /* See if we can figure out if the class lives in a namespace.  We do
16638    this by looking for a member function; its demangled name will
16639    contain namespace info, if there is any.
16640    Return the computed name or NULL.
16641    Space for the result is allocated on the objfile's obstack.
16642    This is the full-die version of guess_partial_die_structure_name.
16643    In this case we know DIE has no useful parent.  */
16644
16645 static char *
16646 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16647 {
16648   struct die_info *spec_die;
16649   struct dwarf2_cu *spec_cu;
16650   struct die_info *child;
16651
16652   spec_cu = cu;
16653   spec_die = die_specification (die, &spec_cu);
16654   if (spec_die != NULL)
16655     {
16656       die = spec_die;
16657       cu = spec_cu;
16658     }
16659
16660   for (child = die->child;
16661        child != NULL;
16662        child = child->sibling)
16663     {
16664       if (child->tag == DW_TAG_subprogram)
16665         {
16666           struct attribute *attr;
16667
16668           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16669           if (attr == NULL)
16670             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16671           if (attr != NULL)
16672             {
16673               char *actual_name
16674                 = language_class_name_from_physname (cu->language_defn,
16675                                                      DW_STRING (attr));
16676               char *name = NULL;
16677
16678               if (actual_name != NULL)
16679                 {
16680                   const char *die_name = dwarf2_name (die, cu);
16681
16682                   if (die_name != NULL
16683                       && strcmp (die_name, actual_name) != 0)
16684                     {
16685                       /* Strip off the class name from the full name.
16686                          We want the prefix.  */
16687                       int die_name_len = strlen (die_name);
16688                       int actual_name_len = strlen (actual_name);
16689
16690                       /* Test for '::' as a sanity check.  */
16691                       if (actual_name_len > die_name_len + 2
16692                           && actual_name[actual_name_len
16693                                          - die_name_len - 1] == ':')
16694                         name =
16695                           obstack_copy0 (&cu->objfile->objfile_obstack,
16696                                          actual_name,
16697                                          actual_name_len - die_name_len - 2);
16698                     }
16699                 }
16700               xfree (actual_name);
16701               return name;
16702             }
16703         }
16704     }
16705
16706   return NULL;
16707 }
16708
16709 /* GCC might emit a nameless typedef that has a linkage name.  Determine the
16710    prefix part in such case.  See
16711    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
16712
16713 static char *
16714 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16715 {
16716   struct attribute *attr;
16717   char *base;
16718
16719   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16720       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16721     return NULL;
16722
16723   attr = dwarf2_attr (die, DW_AT_name, cu);
16724   if (attr != NULL && DW_STRING (attr) != NULL)
16725     return NULL;
16726
16727   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16728   if (attr == NULL)
16729     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16730   if (attr == NULL || DW_STRING (attr) == NULL)
16731     return NULL;
16732
16733   /* dwarf2_name had to be already called.  */
16734   gdb_assert (DW_STRING_IS_CANONICAL (attr));
16735
16736   /* Strip the base name, keep any leading namespaces/classes.  */
16737   base = strrchr (DW_STRING (attr), ':');
16738   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16739     return "";
16740
16741   return obstack_copy0 (&cu->objfile->objfile_obstack,
16742                         DW_STRING (attr), &base[-1] - DW_STRING (attr));
16743 }
16744
16745 /* Return the name of the namespace/class that DIE is defined within,
16746    or "" if we can't tell.  The caller should not xfree the result.
16747
16748    For example, if we're within the method foo() in the following
16749    code:
16750
16751    namespace N {
16752      class C {
16753        void foo () {
16754        }
16755      };
16756    }
16757
16758    then determine_prefix on foo's die will return "N::C".  */
16759
16760 static const char *
16761 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16762 {
16763   struct die_info *parent, *spec_die;
16764   struct dwarf2_cu *spec_cu;
16765   struct type *parent_type;
16766   char *retval;
16767
16768   if (cu->language != language_cplus && cu->language != language_java
16769       && cu->language != language_fortran)
16770     return "";
16771
16772   retval = anonymous_struct_prefix (die, cu);
16773   if (retval)
16774     return retval;
16775
16776   /* We have to be careful in the presence of DW_AT_specification.
16777      For example, with GCC 3.4, given the code
16778
16779      namespace N {
16780        void foo() {
16781          // Definition of N::foo.
16782        }
16783      }
16784
16785      then we'll have a tree of DIEs like this:
16786
16787      1: DW_TAG_compile_unit
16788        2: DW_TAG_namespace        // N
16789          3: DW_TAG_subprogram     // declaration of N::foo
16790        4: DW_TAG_subprogram       // definition of N::foo
16791             DW_AT_specification   // refers to die #3
16792
16793      Thus, when processing die #4, we have to pretend that we're in
16794      the context of its DW_AT_specification, namely the contex of die
16795      #3.  */
16796   spec_cu = cu;
16797   spec_die = die_specification (die, &spec_cu);
16798   if (spec_die == NULL)
16799     parent = die->parent;
16800   else
16801     {
16802       parent = spec_die->parent;
16803       cu = spec_cu;
16804     }
16805
16806   if (parent == NULL)
16807     return "";
16808   else if (parent->building_fullname)
16809     {
16810       const char *name;
16811       const char *parent_name;
16812
16813       /* It has been seen on RealView 2.2 built binaries,
16814          DW_TAG_template_type_param types actually _defined_ as
16815          children of the parent class:
16816
16817          enum E {};
16818          template class <class Enum> Class{};
16819          Class<enum E> class_e;
16820
16821          1: DW_TAG_class_type (Class)
16822            2: DW_TAG_enumeration_type (E)
16823              3: DW_TAG_enumerator (enum1:0)
16824              3: DW_TAG_enumerator (enum2:1)
16825              ...
16826            2: DW_TAG_template_type_param
16827               DW_AT_type  DW_FORM_ref_udata (E)
16828
16829          Besides being broken debug info, it can put GDB into an
16830          infinite loop.  Consider:
16831
16832          When we're building the full name for Class<E>, we'll start
16833          at Class, and go look over its template type parameters,
16834          finding E.  We'll then try to build the full name of E, and
16835          reach here.  We're now trying to build the full name of E,
16836          and look over the parent DIE for containing scope.  In the
16837          broken case, if we followed the parent DIE of E, we'd again
16838          find Class, and once again go look at its template type
16839          arguments, etc., etc.  Simply don't consider such parent die
16840          as source-level parent of this die (it can't be, the language
16841          doesn't allow it), and break the loop here.  */
16842       name = dwarf2_name (die, cu);
16843       parent_name = dwarf2_name (parent, cu);
16844       complaint (&symfile_complaints,
16845                  _("template param type '%s' defined within parent '%s'"),
16846                  name ? name : "<unknown>",
16847                  parent_name ? parent_name : "<unknown>");
16848       return "";
16849     }
16850   else
16851     switch (parent->tag)
16852       {
16853       case DW_TAG_namespace:
16854         parent_type = read_type_die (parent, cu);
16855         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16856            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16857            Work around this problem here.  */
16858         if (cu->language == language_cplus
16859             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16860           return "";
16861         /* We give a name to even anonymous namespaces.  */
16862         return TYPE_TAG_NAME (parent_type);
16863       case DW_TAG_class_type:
16864       case DW_TAG_interface_type:
16865       case DW_TAG_structure_type:
16866       case DW_TAG_union_type:
16867       case DW_TAG_module:
16868         parent_type = read_type_die (parent, cu);
16869         if (TYPE_TAG_NAME (parent_type) != NULL)
16870           return TYPE_TAG_NAME (parent_type);
16871         else
16872           /* An anonymous structure is only allowed non-static data
16873              members; no typedefs, no member functions, et cetera.
16874              So it does not need a prefix.  */
16875           return "";
16876       case DW_TAG_compile_unit:
16877       case DW_TAG_partial_unit:
16878         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
16879         if (cu->language == language_cplus
16880             && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16881             && die->child != NULL
16882             && (die->tag == DW_TAG_class_type
16883                 || die->tag == DW_TAG_structure_type
16884                 || die->tag == DW_TAG_union_type))
16885           {
16886             char *name = guess_full_die_structure_name (die, cu);
16887             if (name != NULL)
16888               return name;
16889           }
16890         return "";
16891       default:
16892         return determine_prefix (parent, cu);
16893       }
16894 }
16895
16896 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16897    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
16898    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
16899    an obconcat, otherwise allocate storage for the result.  The CU argument is
16900    used to determine the language and hence, the appropriate separator.  */
16901
16902 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
16903
16904 static char *
16905 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16906                  int physname, struct dwarf2_cu *cu)
16907 {
16908   const char *lead = "";
16909   const char *sep;
16910
16911   if (suffix == NULL || suffix[0] == '\0'
16912       || prefix == NULL || prefix[0] == '\0')
16913     sep = "";
16914   else if (cu->language == language_java)
16915     sep = ".";
16916   else if (cu->language == language_fortran && physname)
16917     {
16918       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
16919          DW_AT_MIPS_linkage_name is preferred and used instead.  */
16920
16921       lead = "__";
16922       sep = "_MOD_";
16923     }
16924   else
16925     sep = "::";
16926
16927   if (prefix == NULL)
16928     prefix = "";
16929   if (suffix == NULL)
16930     suffix = "";
16931
16932   if (obs == NULL)
16933     {
16934       char *retval
16935         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
16936
16937       strcpy (retval, lead);
16938       strcat (retval, prefix);
16939       strcat (retval, sep);
16940       strcat (retval, suffix);
16941       return retval;
16942     }
16943   else
16944     {
16945       /* We have an obstack.  */
16946       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
16947     }
16948 }
16949
16950 /* Return sibling of die, NULL if no sibling.  */
16951
16952 static struct die_info *
16953 sibling_die (struct die_info *die)
16954 {
16955   return die->sibling;
16956 }
16957
16958 /* Get name of a die, return NULL if not found.  */
16959
16960 static const char *
16961 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
16962                           struct obstack *obstack)
16963 {
16964   if (name && cu->language == language_cplus)
16965     {
16966       char *canon_name = cp_canonicalize_string (name);
16967
16968       if (canon_name != NULL)
16969         {
16970           if (strcmp (canon_name, name) != 0)
16971             name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
16972           xfree (canon_name);
16973         }
16974     }
16975
16976   return name;
16977 }
16978
16979 /* Get name of a die, return NULL if not found.  */
16980
16981 static const char *
16982 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
16983 {
16984   struct attribute *attr;
16985
16986   attr = dwarf2_attr (die, DW_AT_name, cu);
16987   if ((!attr || !DW_STRING (attr))
16988       && die->tag != DW_TAG_class_type
16989       && die->tag != DW_TAG_interface_type
16990       && die->tag != DW_TAG_structure_type
16991       && die->tag != DW_TAG_union_type)
16992     return NULL;
16993
16994   switch (die->tag)
16995     {
16996     case DW_TAG_compile_unit:
16997     case DW_TAG_partial_unit:
16998       /* Compilation units have a DW_AT_name that is a filename, not
16999          a source language identifier.  */
17000     case DW_TAG_enumeration_type:
17001     case DW_TAG_enumerator:
17002       /* These tags always have simple identifiers already; no need
17003          to canonicalize them.  */
17004       return DW_STRING (attr);
17005
17006     case DW_TAG_subprogram:
17007       /* Java constructors will all be named "<init>", so return
17008          the class name when we see this special case.  */
17009       if (cu->language == language_java
17010           && DW_STRING (attr) != NULL
17011           && strcmp (DW_STRING (attr), "<init>") == 0)
17012         {
17013           struct dwarf2_cu *spec_cu = cu;
17014           struct die_info *spec_die;
17015
17016           /* GCJ will output '<init>' for Java constructor names.
17017              For this special case, return the name of the parent class.  */
17018
17019           /* GCJ may output suprogram DIEs with AT_specification set.
17020              If so, use the name of the specified DIE.  */
17021           spec_die = die_specification (die, &spec_cu);
17022           if (spec_die != NULL)
17023             return dwarf2_name (spec_die, spec_cu);
17024
17025           do
17026             {
17027               die = die->parent;
17028               if (die->tag == DW_TAG_class_type)
17029                 return dwarf2_name (die, cu);
17030             }
17031           while (die->tag != DW_TAG_compile_unit
17032                  && die->tag != DW_TAG_partial_unit);
17033         }
17034       break;
17035
17036     case DW_TAG_class_type:
17037     case DW_TAG_interface_type:
17038     case DW_TAG_structure_type:
17039     case DW_TAG_union_type:
17040       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17041          structures or unions.  These were of the form "._%d" in GCC 4.1,
17042          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17043          and GCC 4.4.  We work around this problem by ignoring these.  */
17044       if (attr && DW_STRING (attr)
17045           && (strncmp (DW_STRING (attr), "._", 2) == 0
17046               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17047         return NULL;
17048
17049       /* GCC might emit a nameless typedef that has a linkage name.  See
17050          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
17051       if (!attr || DW_STRING (attr) == NULL)
17052         {
17053           char *demangled = NULL;
17054
17055           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17056           if (attr == NULL)
17057             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17058
17059           if (attr == NULL || DW_STRING (attr) == NULL)
17060             return NULL;
17061
17062           /* Avoid demangling DW_STRING (attr) the second time on a second
17063              call for the same DIE.  */
17064           if (!DW_STRING_IS_CANONICAL (attr))
17065             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
17066
17067           if (demangled)
17068             {
17069               char *base;
17070
17071               /* FIXME: we already did this for the partial symbol... */
17072               DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17073                                                 demangled, strlen (demangled));
17074               DW_STRING_IS_CANONICAL (attr) = 1;
17075               xfree (demangled);
17076
17077               /* Strip any leading namespaces/classes, keep only the base name.
17078                  DW_AT_name for named DIEs does not contain the prefixes.  */
17079               base = strrchr (DW_STRING (attr), ':');
17080               if (base && base > DW_STRING (attr) && base[-1] == ':')
17081                 return &base[1];
17082               else
17083                 return DW_STRING (attr);
17084             }
17085         }
17086       break;
17087
17088     default:
17089       break;
17090     }
17091
17092   if (!DW_STRING_IS_CANONICAL (attr))
17093     {
17094       DW_STRING (attr)
17095         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17096                                     &cu->objfile->objfile_obstack);
17097       DW_STRING_IS_CANONICAL (attr) = 1;
17098     }
17099   return DW_STRING (attr);
17100 }
17101
17102 /* Return the die that this die in an extension of, or NULL if there
17103    is none.  *EXT_CU is the CU containing DIE on input, and the CU
17104    containing the return value on output.  */
17105
17106 static struct die_info *
17107 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17108 {
17109   struct attribute *attr;
17110
17111   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17112   if (attr == NULL)
17113     return NULL;
17114
17115   return follow_die_ref (die, attr, ext_cu);
17116 }
17117
17118 /* Convert a DIE tag into its string name.  */
17119
17120 static const char *
17121 dwarf_tag_name (unsigned tag)
17122 {
17123   const char *name = get_DW_TAG_name (tag);
17124
17125   if (name == NULL)
17126     return "DW_TAG_<unknown>";
17127
17128   return name;
17129 }
17130
17131 /* Convert a DWARF attribute code into its string name.  */
17132
17133 static const char *
17134 dwarf_attr_name (unsigned attr)
17135 {
17136   const char *name;
17137
17138 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17139   if (attr == DW_AT_MIPS_fde)
17140     return "DW_AT_MIPS_fde";
17141 #else
17142   if (attr == DW_AT_HP_block_index)
17143     return "DW_AT_HP_block_index";
17144 #endif
17145
17146   name = get_DW_AT_name (attr);
17147
17148   if (name == NULL)
17149     return "DW_AT_<unknown>";
17150
17151   return name;
17152 }
17153
17154 /* Convert a DWARF value form code into its string name.  */
17155
17156 static const char *
17157 dwarf_form_name (unsigned form)
17158 {
17159   const char *name = get_DW_FORM_name (form);
17160
17161   if (name == NULL)
17162     return "DW_FORM_<unknown>";
17163
17164   return name;
17165 }
17166
17167 static char *
17168 dwarf_bool_name (unsigned mybool)
17169 {
17170   if (mybool)
17171     return "TRUE";
17172   else
17173     return "FALSE";
17174 }
17175
17176 /* Convert a DWARF type code into its string name.  */
17177
17178 static const char *
17179 dwarf_type_encoding_name (unsigned enc)
17180 {
17181   const char *name = get_DW_ATE_name (enc);
17182
17183   if (name == NULL)
17184     return "DW_ATE_<unknown>";
17185
17186   return name;
17187 }
17188
17189 static void
17190 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17191 {
17192   unsigned int i;
17193
17194   print_spaces (indent, f);
17195   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17196            dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17197
17198   if (die->parent != NULL)
17199     {
17200       print_spaces (indent, f);
17201       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
17202                           die->parent->offset.sect_off);
17203     }
17204
17205   print_spaces (indent, f);
17206   fprintf_unfiltered (f, "  has children: %s\n",
17207            dwarf_bool_name (die->child != NULL));
17208
17209   print_spaces (indent, f);
17210   fprintf_unfiltered (f, "  attributes:\n");
17211
17212   for (i = 0; i < die->num_attrs; ++i)
17213     {
17214       print_spaces (indent, f);
17215       fprintf_unfiltered (f, "    %s (%s) ",
17216                dwarf_attr_name (die->attrs[i].name),
17217                dwarf_form_name (die->attrs[i].form));
17218
17219       switch (die->attrs[i].form)
17220         {
17221         case DW_FORM_addr:
17222         case DW_FORM_GNU_addr_index:
17223           fprintf_unfiltered (f, "address: ");
17224           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17225           break;
17226         case DW_FORM_block2:
17227         case DW_FORM_block4:
17228         case DW_FORM_block:
17229         case DW_FORM_block1:
17230           fprintf_unfiltered (f, "block: size %s",
17231                               pulongest (DW_BLOCK (&die->attrs[i])->size));
17232           break;
17233         case DW_FORM_exprloc:
17234           fprintf_unfiltered (f, "expression: size %s",
17235                               pulongest (DW_BLOCK (&die->attrs[i])->size));
17236           break;
17237         case DW_FORM_ref_addr:
17238           fprintf_unfiltered (f, "ref address: ");
17239           fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17240           break;
17241         case DW_FORM_GNU_ref_alt:
17242           fprintf_unfiltered (f, "alt ref address: ");
17243           fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17244           break;
17245         case DW_FORM_ref1:
17246         case DW_FORM_ref2:
17247         case DW_FORM_ref4:
17248         case DW_FORM_ref8:
17249         case DW_FORM_ref_udata:
17250           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17251                               (long) (DW_UNSND (&die->attrs[i])));
17252           break;
17253         case DW_FORM_data1:
17254         case DW_FORM_data2:
17255         case DW_FORM_data4:
17256         case DW_FORM_data8:
17257         case DW_FORM_udata:
17258         case DW_FORM_sdata:
17259           fprintf_unfiltered (f, "constant: %s",
17260                               pulongest (DW_UNSND (&die->attrs[i])));
17261           break;
17262         case DW_FORM_sec_offset:
17263           fprintf_unfiltered (f, "section offset: %s",
17264                               pulongest (DW_UNSND (&die->attrs[i])));
17265           break;
17266         case DW_FORM_ref_sig8:
17267           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17268             {
17269               struct signatured_type *sig_type =
17270                 DW_SIGNATURED_TYPE (&die->attrs[i]);
17271
17272               fprintf_unfiltered (f, "signatured type: 0x%s, offset 0x%x",
17273                                   hex_string (sig_type->signature),
17274                                   sig_type->per_cu.offset.sect_off);
17275             }
17276           else
17277             fprintf_unfiltered (f, "signatured type, unknown");
17278           break;
17279         case DW_FORM_string:
17280         case DW_FORM_strp:
17281         case DW_FORM_GNU_str_index:
17282         case DW_FORM_GNU_strp_alt:
17283           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17284                    DW_STRING (&die->attrs[i])
17285                    ? DW_STRING (&die->attrs[i]) : "",
17286                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17287           break;
17288         case DW_FORM_flag:
17289           if (DW_UNSND (&die->attrs[i]))
17290             fprintf_unfiltered (f, "flag: TRUE");
17291           else
17292             fprintf_unfiltered (f, "flag: FALSE");
17293           break;
17294         case DW_FORM_flag_present:
17295           fprintf_unfiltered (f, "flag: TRUE");
17296           break;
17297         case DW_FORM_indirect:
17298           /* The reader will have reduced the indirect form to
17299              the "base form" so this form should not occur.  */
17300           fprintf_unfiltered (f, 
17301                               "unexpected attribute form: DW_FORM_indirect");
17302           break;
17303         default:
17304           fprintf_unfiltered (f, "unsupported attribute form: %d.",
17305                    die->attrs[i].form);
17306           break;
17307         }
17308       fprintf_unfiltered (f, "\n");
17309     }
17310 }
17311
17312 static void
17313 dump_die_for_error (struct die_info *die)
17314 {
17315   dump_die_shallow (gdb_stderr, 0, die);
17316 }
17317
17318 static void
17319 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17320 {
17321   int indent = level * 4;
17322
17323   gdb_assert (die != NULL);
17324
17325   if (level >= max_level)
17326     return;
17327
17328   dump_die_shallow (f, indent, die);
17329
17330   if (die->child != NULL)
17331     {
17332       print_spaces (indent, f);
17333       fprintf_unfiltered (f, "  Children:");
17334       if (level + 1 < max_level)
17335         {
17336           fprintf_unfiltered (f, "\n");
17337           dump_die_1 (f, level + 1, max_level, die->child);
17338         }
17339       else
17340         {
17341           fprintf_unfiltered (f,
17342                               " [not printed, max nesting level reached]\n");
17343         }
17344     }
17345
17346   if (die->sibling != NULL && level > 0)
17347     {
17348       dump_die_1 (f, level, max_level, die->sibling);
17349     }
17350 }
17351
17352 /* This is called from the pdie macro in gdbinit.in.
17353    It's not static so gcc will keep a copy callable from gdb.  */
17354
17355 void
17356 dump_die (struct die_info *die, int max_level)
17357 {
17358   dump_die_1 (gdb_stdlog, 0, max_level, die);
17359 }
17360
17361 static void
17362 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17363 {
17364   void **slot;
17365
17366   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17367                                    INSERT);
17368
17369   *slot = die;
17370 }
17371
17372 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17373    besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file.  */
17374
17375 static int
17376 is_ref_attr (struct attribute *attr)
17377 {
17378   switch (attr->form)
17379     {
17380     case DW_FORM_ref_addr:
17381     case DW_FORM_ref1:
17382     case DW_FORM_ref2:
17383     case DW_FORM_ref4:
17384     case DW_FORM_ref8:
17385     case DW_FORM_ref_udata:
17386     case DW_FORM_GNU_ref_alt:
17387       return 1;
17388     default:
17389       return 0;
17390     }
17391 }
17392
17393 /* Return DIE offset of ATTR.  Return 0 with complaint if ATTR is not of the
17394    required kind.  */
17395
17396 static sect_offset
17397 dwarf2_get_ref_die_offset (struct attribute *attr)
17398 {
17399   sect_offset retval = { DW_UNSND (attr) };
17400
17401   if (is_ref_attr (attr))
17402     return retval;
17403
17404   retval.sect_off = 0;
17405   complaint (&symfile_complaints,
17406              _("unsupported die ref attribute form: '%s'"),
17407              dwarf_form_name (attr->form));
17408   return retval;
17409 }
17410
17411 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
17412  * the value held by the attribute is not constant.  */
17413
17414 static LONGEST
17415 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17416 {
17417   if (attr->form == DW_FORM_sdata)
17418     return DW_SND (attr);
17419   else if (attr->form == DW_FORM_udata
17420            || attr->form == DW_FORM_data1
17421            || attr->form == DW_FORM_data2
17422            || attr->form == DW_FORM_data4
17423            || attr->form == DW_FORM_data8)
17424     return DW_UNSND (attr);
17425   else
17426     {
17427       complaint (&symfile_complaints,
17428                  _("Attribute value is not a constant (%s)"),
17429                  dwarf_form_name (attr->form));
17430       return default_value;
17431     }
17432 }
17433
17434 /* Follow reference or signature attribute ATTR of SRC_DIE.
17435    On entry *REF_CU is the CU of SRC_DIE.
17436    On exit *REF_CU is the CU of the result.  */
17437
17438 static struct die_info *
17439 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17440                        struct dwarf2_cu **ref_cu)
17441 {
17442   struct die_info *die;
17443
17444   if (is_ref_attr (attr))
17445     die = follow_die_ref (src_die, attr, ref_cu);
17446   else if (attr->form == DW_FORM_ref_sig8)
17447     die = follow_die_sig (src_die, attr, ref_cu);
17448   else
17449     {
17450       dump_die_for_error (src_die);
17451       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17452              (*ref_cu)->objfile->name);
17453     }
17454
17455   return die;
17456 }
17457
17458 /* Follow reference OFFSET.
17459    On entry *REF_CU is the CU of the source die referencing OFFSET.
17460    On exit *REF_CU is the CU of the result.
17461    Returns NULL if OFFSET is invalid.  */
17462
17463 static struct die_info *
17464 follow_die_offset (sect_offset offset, int offset_in_dwz,
17465                    struct dwarf2_cu **ref_cu)
17466 {
17467   struct die_info temp_die;
17468   struct dwarf2_cu *target_cu, *cu = *ref_cu;
17469
17470   gdb_assert (cu->per_cu != NULL);
17471
17472   target_cu = cu;
17473
17474   if (cu->per_cu->is_debug_types)
17475     {
17476       /* .debug_types CUs cannot reference anything outside their CU.
17477          If they need to, they have to reference a signatured type via
17478          DW_FORM_ref_sig8.  */
17479       if (! offset_in_cu_p (&cu->header, offset))
17480         return NULL;
17481     }
17482   else if (offset_in_dwz != cu->per_cu->is_dwz
17483            || ! offset_in_cu_p (&cu->header, offset))
17484     {
17485       struct dwarf2_per_cu_data *per_cu;
17486
17487       per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17488                                                  cu->objfile);
17489
17490       /* If necessary, add it to the queue and load its DIEs.  */
17491       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17492         load_full_comp_unit (per_cu, cu->language);
17493
17494       target_cu = per_cu->cu;
17495     }
17496   else if (cu->dies == NULL)
17497     {
17498       /* We're loading full DIEs during partial symbol reading.  */
17499       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17500       load_full_comp_unit (cu->per_cu, language_minimal);
17501     }
17502
17503   *ref_cu = target_cu;
17504   temp_die.offset = offset;
17505   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17506 }
17507
17508 /* Follow reference attribute ATTR of SRC_DIE.
17509    On entry *REF_CU is the CU of SRC_DIE.
17510    On exit *REF_CU is the CU of the result.  */
17511
17512 static struct die_info *
17513 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17514                 struct dwarf2_cu **ref_cu)
17515 {
17516   sect_offset offset = dwarf2_get_ref_die_offset (attr);
17517   struct dwarf2_cu *cu = *ref_cu;
17518   struct die_info *die;
17519
17520   die = follow_die_offset (offset,
17521                            (attr->form == DW_FORM_GNU_ref_alt
17522                             || cu->per_cu->is_dwz),
17523                            ref_cu);
17524   if (!die)
17525     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17526            "at 0x%x [in module %s]"),
17527            offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17528
17529   return die;
17530 }
17531
17532 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17533    Returned value is intended for DW_OP_call*.  Returned
17534    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */
17535
17536 struct dwarf2_locexpr_baton
17537 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17538                                struct dwarf2_per_cu_data *per_cu,
17539                                CORE_ADDR (*get_frame_pc) (void *baton),
17540                                void *baton)
17541 {
17542   struct dwarf2_cu *cu;
17543   struct die_info *die;
17544   struct attribute *attr;
17545   struct dwarf2_locexpr_baton retval;
17546
17547   dw2_setup (per_cu->objfile);
17548
17549   if (per_cu->cu == NULL)
17550     load_cu (per_cu);
17551   cu = per_cu->cu;
17552
17553   die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17554   if (!die)
17555     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17556            offset.sect_off, per_cu->objfile->name);
17557
17558   attr = dwarf2_attr (die, DW_AT_location, cu);
17559   if (!attr)
17560     {
17561       /* DWARF: "If there is no such attribute, then there is no effect.".
17562          DATA is ignored if SIZE is 0.  */
17563
17564       retval.data = NULL;
17565       retval.size = 0;
17566     }
17567   else if (attr_form_is_section_offset (attr))
17568     {
17569       struct dwarf2_loclist_baton loclist_baton;
17570       CORE_ADDR pc = (*get_frame_pc) (baton);
17571       size_t size;
17572
17573       fill_in_loclist_baton (cu, &loclist_baton, attr);
17574
17575       retval.data = dwarf2_find_location_expression (&loclist_baton,
17576                                                      &size, pc);
17577       retval.size = size;
17578     }
17579   else
17580     {
17581       if (!attr_form_is_block (attr))
17582         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17583                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17584                offset.sect_off, per_cu->objfile->name);
17585
17586       retval.data = DW_BLOCK (attr)->data;
17587       retval.size = DW_BLOCK (attr)->size;
17588     }
17589   retval.per_cu = cu->per_cu;
17590
17591   age_cached_comp_units ();
17592
17593   return retval;
17594 }
17595
17596 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17597    offset.  */
17598
17599 struct dwarf2_locexpr_baton
17600 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17601                              struct dwarf2_per_cu_data *per_cu,
17602                              CORE_ADDR (*get_frame_pc) (void *baton),
17603                              void *baton)
17604 {
17605   sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17606
17607   return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17608 }
17609
17610 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17611    PER_CU.  */
17612
17613 struct type *
17614 dwarf2_get_die_type (cu_offset die_offset,
17615                      struct dwarf2_per_cu_data *per_cu)
17616 {
17617   sect_offset die_offset_sect;
17618
17619   dw2_setup (per_cu->objfile);
17620
17621   die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17622   return get_die_type_at_offset (die_offset_sect, per_cu);
17623 }
17624
17625 /* Follow the signature attribute ATTR in SRC_DIE.
17626    On entry *REF_CU is the CU of SRC_DIE.
17627    On exit *REF_CU is the CU of the result.  */
17628
17629 static struct die_info *
17630 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17631                 struct dwarf2_cu **ref_cu)
17632 {
17633   struct objfile *objfile = (*ref_cu)->objfile;
17634   struct die_info temp_die;
17635   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17636   struct dwarf2_cu *sig_cu;
17637   struct die_info *die;
17638
17639   /* sig_type will be NULL if the signatured type is missing from
17640      the debug info.  */
17641   if (sig_type == NULL)
17642     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17643              "at 0x%x [in module %s]"),
17644            src_die->offset.sect_off, objfile->name);
17645
17646   /* If necessary, add it to the queue and load its DIEs.  */
17647
17648   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17649     read_signatured_type (sig_type);
17650
17651   gdb_assert (sig_type->per_cu.cu != NULL);
17652
17653   sig_cu = sig_type->per_cu.cu;
17654   gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17655   temp_die.offset = sig_type->type_offset_in_section;
17656   die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17657                              temp_die.offset.sect_off);
17658   if (die)
17659     {
17660       /* For .gdb_index version 7 keep track of included TUs.
17661          http://sourceware.org/bugzilla/show_bug.cgi?id=15021.  */
17662       if (dwarf2_per_objfile->index_table != NULL
17663           && dwarf2_per_objfile->index_table->version <= 7)
17664         {
17665           VEC_safe_push (dwarf2_per_cu_ptr,
17666                          (*ref_cu)->per_cu->imported_symtabs,
17667                          sig_cu->per_cu);
17668         }
17669
17670       *ref_cu = sig_cu;
17671       return die;
17672     }
17673
17674   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17675          "from DIE at 0x%x [in module %s]"),
17676          temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17677 }
17678
17679 /* Given an offset of a signatured type, return its signatured_type.  */
17680
17681 static struct signatured_type *
17682 lookup_signatured_type_at_offset (struct objfile *objfile,
17683                                   struct dwarf2_section_info *section,
17684                                   sect_offset offset)
17685 {
17686   gdb_byte *info_ptr = section->buffer + offset.sect_off;
17687   unsigned int length, initial_length_size;
17688   unsigned int sig_offset;
17689   struct signatured_type find_entry, *sig_type;
17690
17691   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17692   sig_offset = (initial_length_size
17693                 + 2 /*version*/
17694                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17695                 + 1 /*address_size*/);
17696   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17697   sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17698
17699   /* This is only used to lookup previously recorded types.
17700      If we didn't find it, it's our bug.  */
17701   gdb_assert (sig_type != NULL);
17702   gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17703
17704   return sig_type;
17705 }
17706
17707 /* Load the DIEs associated with type unit PER_CU into memory.  */
17708
17709 static void
17710 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17711 {
17712   struct signatured_type *sig_type;
17713
17714   /* Caller is responsible for ensuring type_unit_groups don't get here.  */
17715   gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17716
17717   /* We have the per_cu, but we need the signatured_type.
17718      Fortunately this is an easy translation.  */
17719   gdb_assert (per_cu->is_debug_types);
17720   sig_type = (struct signatured_type *) per_cu;
17721
17722   gdb_assert (per_cu->cu == NULL);
17723
17724   read_signatured_type (sig_type);
17725
17726   gdb_assert (per_cu->cu != NULL);
17727 }
17728
17729 /* die_reader_func for read_signatured_type.
17730    This is identical to load_full_comp_unit_reader,
17731    but is kept separate for now.  */
17732
17733 static void
17734 read_signatured_type_reader (const struct die_reader_specs *reader,
17735                              gdb_byte *info_ptr,
17736                              struct die_info *comp_unit_die,
17737                              int has_children,
17738                              void *data)
17739 {
17740   struct dwarf2_cu *cu = reader->cu;
17741
17742   gdb_assert (cu->die_hash == NULL);
17743   cu->die_hash =
17744     htab_create_alloc_ex (cu->header.length / 12,
17745                           die_hash,
17746                           die_eq,
17747                           NULL,
17748                           &cu->comp_unit_obstack,
17749                           hashtab_obstack_allocate,
17750                           dummy_obstack_deallocate);
17751
17752   if (has_children)
17753     comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17754                                                   &info_ptr, comp_unit_die);
17755   cu->dies = comp_unit_die;
17756   /* comp_unit_die is not stored in die_hash, no need.  */
17757
17758   /* We try not to read any attributes in this function, because not
17759      all CUs needed for references have been loaded yet, and symbol
17760      table processing isn't initialized.  But we have to set the CU language,
17761      or we won't be able to build types correctly.
17762      Similarly, if we do not read the producer, we can not apply
17763      producer-specific interpretation.  */
17764   prepare_one_comp_unit (cu, cu->dies, language_minimal);
17765 }
17766
17767 /* Read in a signatured type and build its CU and DIEs.
17768    If the type is a stub for the real type in a DWO file,
17769    read in the real type from the DWO file as well.  */
17770
17771 static void
17772 read_signatured_type (struct signatured_type *sig_type)
17773 {
17774   struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17775
17776   gdb_assert (per_cu->is_debug_types);
17777   gdb_assert (per_cu->cu == NULL);
17778
17779   init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17780                            read_signatured_type_reader, NULL);
17781 }
17782
17783 /* Decode simple location descriptions.
17784    Given a pointer to a dwarf block that defines a location, compute
17785    the location and return the value.
17786
17787    NOTE drow/2003-11-18: This function is called in two situations
17788    now: for the address of static or global variables (partial symbols
17789    only) and for offsets into structures which are expected to be
17790    (more or less) constant.  The partial symbol case should go away,
17791    and only the constant case should remain.  That will let this
17792    function complain more accurately.  A few special modes are allowed
17793    without complaint for global variables (for instance, global
17794    register values and thread-local values).
17795
17796    A location description containing no operations indicates that the
17797    object is optimized out.  The return value is 0 for that case.
17798    FIXME drow/2003-11-16: No callers check for this case any more; soon all
17799    callers will only want a very basic result and this can become a
17800    complaint.
17801
17802    Note that stack[0] is unused except as a default error return.  */
17803
17804 static CORE_ADDR
17805 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17806 {
17807   struct objfile *objfile = cu->objfile;
17808   size_t i;
17809   size_t size = blk->size;
17810   gdb_byte *data = blk->data;
17811   CORE_ADDR stack[64];
17812   int stacki;
17813   unsigned int bytes_read, unsnd;
17814   gdb_byte op;
17815
17816   i = 0;
17817   stacki = 0;
17818   stack[stacki] = 0;
17819   stack[++stacki] = 0;
17820
17821   while (i < size)
17822     {
17823       op = data[i++];
17824       switch (op)
17825         {
17826         case DW_OP_lit0:
17827         case DW_OP_lit1:
17828         case DW_OP_lit2:
17829         case DW_OP_lit3:
17830         case DW_OP_lit4:
17831         case DW_OP_lit5:
17832         case DW_OP_lit6:
17833         case DW_OP_lit7:
17834         case DW_OP_lit8:
17835         case DW_OP_lit9:
17836         case DW_OP_lit10:
17837         case DW_OP_lit11:
17838         case DW_OP_lit12:
17839         case DW_OP_lit13:
17840         case DW_OP_lit14:
17841         case DW_OP_lit15:
17842         case DW_OP_lit16:
17843         case DW_OP_lit17:
17844         case DW_OP_lit18:
17845         case DW_OP_lit19:
17846         case DW_OP_lit20:
17847         case DW_OP_lit21:
17848         case DW_OP_lit22:
17849         case DW_OP_lit23:
17850         case DW_OP_lit24:
17851         case DW_OP_lit25:
17852         case DW_OP_lit26:
17853         case DW_OP_lit27:
17854         case DW_OP_lit28:
17855         case DW_OP_lit29:
17856         case DW_OP_lit30:
17857         case DW_OP_lit31:
17858           stack[++stacki] = op - DW_OP_lit0;
17859           break;
17860
17861         case DW_OP_reg0:
17862         case DW_OP_reg1:
17863         case DW_OP_reg2:
17864         case DW_OP_reg3:
17865         case DW_OP_reg4:
17866         case DW_OP_reg5:
17867         case DW_OP_reg6:
17868         case DW_OP_reg7:
17869         case DW_OP_reg8:
17870         case DW_OP_reg9:
17871         case DW_OP_reg10:
17872         case DW_OP_reg11:
17873         case DW_OP_reg12:
17874         case DW_OP_reg13:
17875         case DW_OP_reg14:
17876         case DW_OP_reg15:
17877         case DW_OP_reg16:
17878         case DW_OP_reg17:
17879         case DW_OP_reg18:
17880         case DW_OP_reg19:
17881         case DW_OP_reg20:
17882         case DW_OP_reg21:
17883         case DW_OP_reg22:
17884         case DW_OP_reg23:
17885         case DW_OP_reg24:
17886         case DW_OP_reg25:
17887         case DW_OP_reg26:
17888         case DW_OP_reg27:
17889         case DW_OP_reg28:
17890         case DW_OP_reg29:
17891         case DW_OP_reg30:
17892         case DW_OP_reg31:
17893           stack[++stacki] = op - DW_OP_reg0;
17894           if (i < size)
17895             dwarf2_complex_location_expr_complaint ();
17896           break;
17897
17898         case DW_OP_regx:
17899           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17900           i += bytes_read;
17901           stack[++stacki] = unsnd;
17902           if (i < size)
17903             dwarf2_complex_location_expr_complaint ();
17904           break;
17905
17906         case DW_OP_addr:
17907           stack[++stacki] = read_address (objfile->obfd, &data[i],
17908                                           cu, &bytes_read);
17909           i += bytes_read;
17910           break;
17911
17912         case DW_OP_const1u:
17913           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17914           i += 1;
17915           break;
17916
17917         case DW_OP_const1s:
17918           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17919           i += 1;
17920           break;
17921
17922         case DW_OP_const2u:
17923           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17924           i += 2;
17925           break;
17926
17927         case DW_OP_const2s:
17928           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17929           i += 2;
17930           break;
17931
17932         case DW_OP_const4u:
17933           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17934           i += 4;
17935           break;
17936
17937         case DW_OP_const4s:
17938           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17939           i += 4;
17940           break;
17941
17942         case DW_OP_const8u:
17943           stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17944           i += 8;
17945           break;
17946
17947         case DW_OP_constu:
17948           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17949                                                   &bytes_read);
17950           i += bytes_read;
17951           break;
17952
17953         case DW_OP_consts:
17954           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17955           i += bytes_read;
17956           break;
17957
17958         case DW_OP_dup:
17959           stack[stacki + 1] = stack[stacki];
17960           stacki++;
17961           break;
17962
17963         case DW_OP_plus:
17964           stack[stacki - 1] += stack[stacki];
17965           stacki--;
17966           break;
17967
17968         case DW_OP_plus_uconst:
17969           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17970                                                  &bytes_read);
17971           i += bytes_read;
17972           break;
17973
17974         case DW_OP_minus:
17975           stack[stacki - 1] -= stack[stacki];
17976           stacki--;
17977           break;
17978
17979         case DW_OP_deref:
17980           /* If we're not the last op, then we definitely can't encode
17981              this using GDB's address_class enum.  This is valid for partial
17982              global symbols, although the variable's address will be bogus
17983              in the psymtab.  */
17984           if (i < size)
17985             dwarf2_complex_location_expr_complaint ();
17986           break;
17987
17988         case DW_OP_GNU_push_tls_address:
17989           /* The top of the stack has the offset from the beginning
17990              of the thread control block at which the variable is located.  */
17991           /* Nothing should follow this operator, so the top of stack would
17992              be returned.  */
17993           /* This is valid for partial global symbols, but the variable's
17994              address will be bogus in the psymtab.  Make it always at least
17995              non-zero to not look as a variable garbage collected by linker
17996              which have DW_OP_addr 0.  */
17997           if (i < size)
17998             dwarf2_complex_location_expr_complaint ();
17999           stack[stacki]++;
18000           break;
18001
18002         case DW_OP_GNU_uninit:
18003           break;
18004
18005         case DW_OP_GNU_addr_index:
18006         case DW_OP_GNU_const_index:
18007           stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18008                                                          &bytes_read);
18009           i += bytes_read;
18010           break;
18011
18012         default:
18013           {
18014             const char *name = get_DW_OP_name (op);
18015
18016             if (name)
18017               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18018                          name);
18019             else
18020               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18021                          op);
18022           }
18023
18024           return (stack[stacki]);
18025         }
18026
18027       /* Enforce maximum stack depth of SIZE-1 to avoid writing
18028          outside of the allocated space.  Also enforce minimum>0.  */
18029       if (stacki >= ARRAY_SIZE (stack) - 1)
18030         {
18031           complaint (&symfile_complaints,
18032                      _("location description stack overflow"));
18033           return 0;
18034         }
18035
18036       if (stacki <= 0)
18037         {
18038           complaint (&symfile_complaints,
18039                      _("location description stack underflow"));
18040           return 0;
18041         }
18042     }
18043   return (stack[stacki]);
18044 }
18045
18046 /* memory allocation interface */
18047
18048 static struct dwarf_block *
18049 dwarf_alloc_block (struct dwarf2_cu *cu)
18050 {
18051   struct dwarf_block *blk;
18052
18053   blk = (struct dwarf_block *)
18054     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18055   return (blk);
18056 }
18057
18058 static struct die_info *
18059 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18060 {
18061   struct die_info *die;
18062   size_t size = sizeof (struct die_info);
18063
18064   if (num_attrs > 1)
18065     size += (num_attrs - 1) * sizeof (struct attribute);
18066
18067   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18068   memset (die, 0, sizeof (struct die_info));
18069   return (die);
18070 }
18071
18072 \f
18073 /* Macro support.  */
18074
18075 /* Return file name relative to the compilation directory of file number I in
18076    *LH's file name table.  The result is allocated using xmalloc; the caller is
18077    responsible for freeing it.  */
18078
18079 static char *
18080 file_file_name (int file, struct line_header *lh)
18081 {
18082   /* Is the file number a valid index into the line header's file name
18083      table?  Remember that file numbers start with one, not zero.  */
18084   if (1 <= file && file <= lh->num_file_names)
18085     {
18086       struct file_entry *fe = &lh->file_names[file - 1];
18087
18088       if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18089         return xstrdup (fe->name);
18090       return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18091                      fe->name, NULL);
18092     }
18093   else
18094     {
18095       /* The compiler produced a bogus file number.  We can at least
18096          record the macro definitions made in the file, even if we
18097          won't be able to find the file by name.  */
18098       char fake_name[80];
18099
18100       xsnprintf (fake_name, sizeof (fake_name),
18101                  "<bad macro file number %d>", file);
18102
18103       complaint (&symfile_complaints,
18104                  _("bad file number in macro information (%d)"),
18105                  file);
18106
18107       return xstrdup (fake_name);
18108     }
18109 }
18110
18111 /* Return the full name of file number I in *LH's file name table.
18112    Use COMP_DIR as the name of the current directory of the
18113    compilation.  The result is allocated using xmalloc; the caller is
18114    responsible for freeing it.  */
18115 static char *
18116 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18117 {
18118   /* Is the file number a valid index into the line header's file name
18119      table?  Remember that file numbers start with one, not zero.  */
18120   if (1 <= file && file <= lh->num_file_names)
18121     {
18122       char *relative = file_file_name (file, lh);
18123
18124       if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18125         return relative;
18126       return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18127     }
18128   else
18129     return file_file_name (file, lh);
18130 }
18131
18132
18133 static struct macro_source_file *
18134 macro_start_file (int file, int line,
18135                   struct macro_source_file *current_file,
18136                   const char *comp_dir,
18137                   struct line_header *lh, struct objfile *objfile)
18138 {
18139   /* File name relative to the compilation directory of this source file.  */
18140   char *file_name = file_file_name (file, lh);
18141
18142   /* We don't create a macro table for this compilation unit
18143      at all until we actually get a filename.  */
18144   if (! pending_macros)
18145     pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18146                                       objfile->per_bfd->macro_cache,
18147                                       comp_dir);
18148
18149   if (! current_file)
18150     {
18151       /* If we have no current file, then this must be the start_file
18152          directive for the compilation unit's main source file.  */
18153       current_file = macro_set_main (pending_macros, file_name);
18154       macro_define_special (pending_macros);
18155     }
18156   else
18157     current_file = macro_include (current_file, line, file_name);
18158
18159   xfree (file_name);
18160
18161   return current_file;
18162 }
18163
18164
18165 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18166    followed by a null byte.  */
18167 static char *
18168 copy_string (const char *buf, int len)
18169 {
18170   char *s = xmalloc (len + 1);
18171
18172   memcpy (s, buf, len);
18173   s[len] = '\0';
18174   return s;
18175 }
18176
18177
18178 static const char *
18179 consume_improper_spaces (const char *p, const char *body)
18180 {
18181   if (*p == ' ')
18182     {
18183       complaint (&symfile_complaints,
18184                  _("macro definition contains spaces "
18185                    "in formal argument list:\n`%s'"),
18186                  body);
18187
18188       while (*p == ' ')
18189         p++;
18190     }
18191
18192   return p;
18193 }
18194
18195
18196 static void
18197 parse_macro_definition (struct macro_source_file *file, int line,
18198                         const char *body)
18199 {
18200   const char *p;
18201
18202   /* The body string takes one of two forms.  For object-like macro
18203      definitions, it should be:
18204
18205         <macro name> " " <definition>
18206
18207      For function-like macro definitions, it should be:
18208
18209         <macro name> "() " <definition>
18210      or
18211         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18212
18213      Spaces may appear only where explicitly indicated, and in the
18214      <definition>.
18215
18216      The Dwarf 2 spec says that an object-like macro's name is always
18217      followed by a space, but versions of GCC around March 2002 omit
18218      the space when the macro's definition is the empty string.
18219
18220      The Dwarf 2 spec says that there should be no spaces between the
18221      formal arguments in a function-like macro's formal argument list,
18222      but versions of GCC around March 2002 include spaces after the
18223      commas.  */
18224
18225
18226   /* Find the extent of the macro name.  The macro name is terminated
18227      by either a space or null character (for an object-like macro) or
18228      an opening paren (for a function-like macro).  */
18229   for (p = body; *p; p++)
18230     if (*p == ' ' || *p == '(')
18231       break;
18232
18233   if (*p == ' ' || *p == '\0')
18234     {
18235       /* It's an object-like macro.  */
18236       int name_len = p - body;
18237       char *name = copy_string (body, name_len);
18238       const char *replacement;
18239
18240       if (*p == ' ')
18241         replacement = body + name_len + 1;
18242       else
18243         {
18244           dwarf2_macro_malformed_definition_complaint (body);
18245           replacement = body + name_len;
18246         }
18247
18248       macro_define_object (file, line, name, replacement);
18249
18250       xfree (name);
18251     }
18252   else if (*p == '(')
18253     {
18254       /* It's a function-like macro.  */
18255       char *name = copy_string (body, p - body);
18256       int argc = 0;
18257       int argv_size = 1;
18258       char **argv = xmalloc (argv_size * sizeof (*argv));
18259
18260       p++;
18261
18262       p = consume_improper_spaces (p, body);
18263
18264       /* Parse the formal argument list.  */
18265       while (*p && *p != ')')
18266         {
18267           /* Find the extent of the current argument name.  */
18268           const char *arg_start = p;
18269
18270           while (*p && *p != ',' && *p != ')' && *p != ' ')
18271             p++;
18272
18273           if (! *p || p == arg_start)
18274             dwarf2_macro_malformed_definition_complaint (body);
18275           else
18276             {
18277               /* Make sure argv has room for the new argument.  */
18278               if (argc >= argv_size)
18279                 {
18280                   argv_size *= 2;
18281                   argv = xrealloc (argv, argv_size * sizeof (*argv));
18282                 }
18283
18284               argv[argc++] = copy_string (arg_start, p - arg_start);
18285             }
18286
18287           p = consume_improper_spaces (p, body);
18288
18289           /* Consume the comma, if present.  */
18290           if (*p == ',')
18291             {
18292               p++;
18293
18294               p = consume_improper_spaces (p, body);
18295             }
18296         }
18297
18298       if (*p == ')')
18299         {
18300           p++;
18301
18302           if (*p == ' ')
18303             /* Perfectly formed definition, no complaints.  */
18304             macro_define_function (file, line, name,
18305                                    argc, (const char **) argv,
18306                                    p + 1);
18307           else if (*p == '\0')
18308             {
18309               /* Complain, but do define it.  */
18310               dwarf2_macro_malformed_definition_complaint (body);
18311               macro_define_function (file, line, name,
18312                                      argc, (const char **) argv,
18313                                      p);
18314             }
18315           else
18316             /* Just complain.  */
18317             dwarf2_macro_malformed_definition_complaint (body);
18318         }
18319       else
18320         /* Just complain.  */
18321         dwarf2_macro_malformed_definition_complaint (body);
18322
18323       xfree (name);
18324       {
18325         int i;
18326
18327         for (i = 0; i < argc; i++)
18328           xfree (argv[i]);
18329       }
18330       xfree (argv);
18331     }
18332   else
18333     dwarf2_macro_malformed_definition_complaint (body);
18334 }
18335
18336 /* Skip some bytes from BYTES according to the form given in FORM.
18337    Returns the new pointer.  */
18338
18339 static gdb_byte *
18340 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18341                  enum dwarf_form form,
18342                  unsigned int offset_size,
18343                  struct dwarf2_section_info *section)
18344 {
18345   unsigned int bytes_read;
18346
18347   switch (form)
18348     {
18349     case DW_FORM_data1:
18350     case DW_FORM_flag:
18351       ++bytes;
18352       break;
18353
18354     case DW_FORM_data2:
18355       bytes += 2;
18356       break;
18357
18358     case DW_FORM_data4:
18359       bytes += 4;
18360       break;
18361
18362     case DW_FORM_data8:
18363       bytes += 8;
18364       break;
18365
18366     case DW_FORM_string:
18367       read_direct_string (abfd, bytes, &bytes_read);
18368       bytes += bytes_read;
18369       break;
18370
18371     case DW_FORM_sec_offset:
18372     case DW_FORM_strp:
18373     case DW_FORM_GNU_strp_alt:
18374       bytes += offset_size;
18375       break;
18376
18377     case DW_FORM_block:
18378       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18379       bytes += bytes_read;
18380       break;
18381
18382     case DW_FORM_block1:
18383       bytes += 1 + read_1_byte (abfd, bytes);
18384       break;
18385     case DW_FORM_block2:
18386       bytes += 2 + read_2_bytes (abfd, bytes);
18387       break;
18388     case DW_FORM_block4:
18389       bytes += 4 + read_4_bytes (abfd, bytes);
18390       break;
18391
18392     case DW_FORM_sdata:
18393     case DW_FORM_udata:
18394     case DW_FORM_GNU_addr_index:
18395     case DW_FORM_GNU_str_index:
18396       bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18397       if (bytes == NULL)
18398         {
18399           dwarf2_section_buffer_overflow_complaint (section);
18400           return NULL;
18401         }
18402       break;
18403
18404     default:
18405       {
18406       complain:
18407         complaint (&symfile_complaints,
18408                    _("invalid form 0x%x in `%s'"),
18409                    form,
18410                    section->asection->name);
18411         return NULL;
18412       }
18413     }
18414
18415   return bytes;
18416 }
18417
18418 /* A helper for dwarf_decode_macros that handles skipping an unknown
18419    opcode.  Returns an updated pointer to the macro data buffer; or,
18420    on error, issues a complaint and returns NULL.  */
18421
18422 static gdb_byte *
18423 skip_unknown_opcode (unsigned int opcode,
18424                      gdb_byte **opcode_definitions,
18425                      gdb_byte *mac_ptr, gdb_byte *mac_end,
18426                      bfd *abfd,
18427                      unsigned int offset_size,
18428                      struct dwarf2_section_info *section)
18429 {
18430   unsigned int bytes_read, i;
18431   unsigned long arg;
18432   gdb_byte *defn;
18433
18434   if (opcode_definitions[opcode] == NULL)
18435     {
18436       complaint (&symfile_complaints,
18437                  _("unrecognized DW_MACFINO opcode 0x%x"),
18438                  opcode);
18439       return NULL;
18440     }
18441
18442   defn = opcode_definitions[opcode];
18443   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18444   defn += bytes_read;
18445
18446   for (i = 0; i < arg; ++i)
18447     {
18448       mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18449                                  section);
18450       if (mac_ptr == NULL)
18451         {
18452           /* skip_form_bytes already issued the complaint.  */
18453           return NULL;
18454         }
18455     }
18456
18457   return mac_ptr;
18458 }
18459
18460 /* A helper function which parses the header of a macro section.
18461    If the macro section is the extended (for now called "GNU") type,
18462    then this updates *OFFSET_SIZE.  Returns a pointer to just after
18463    the header, or issues a complaint and returns NULL on error.  */
18464
18465 static gdb_byte *
18466 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18467                           bfd *abfd,
18468                           gdb_byte *mac_ptr,
18469                           unsigned int *offset_size,
18470                           int section_is_gnu)
18471 {
18472   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18473
18474   if (section_is_gnu)
18475     {
18476       unsigned int version, flags;
18477
18478       version = read_2_bytes (abfd, mac_ptr);
18479       if (version != 4)
18480         {
18481           complaint (&symfile_complaints,
18482                      _("unrecognized version `%d' in .debug_macro section"),
18483                      version);
18484           return NULL;
18485         }
18486       mac_ptr += 2;
18487
18488       flags = read_1_byte (abfd, mac_ptr);
18489       ++mac_ptr;
18490       *offset_size = (flags & 1) ? 8 : 4;
18491
18492       if ((flags & 2) != 0)
18493         /* We don't need the line table offset.  */
18494         mac_ptr += *offset_size;
18495
18496       /* Vendor opcode descriptions.  */
18497       if ((flags & 4) != 0)
18498         {
18499           unsigned int i, count;
18500
18501           count = read_1_byte (abfd, mac_ptr);
18502           ++mac_ptr;
18503           for (i = 0; i < count; ++i)
18504             {
18505               unsigned int opcode, bytes_read;
18506               unsigned long arg;
18507
18508               opcode = read_1_byte (abfd, mac_ptr);
18509               ++mac_ptr;
18510               opcode_definitions[opcode] = mac_ptr;
18511               arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18512               mac_ptr += bytes_read;
18513               mac_ptr += arg;
18514             }
18515         }
18516     }
18517
18518   return mac_ptr;
18519 }
18520
18521 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18522    including DW_MACRO_GNU_transparent_include.  */
18523
18524 static void
18525 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18526                           struct macro_source_file *current_file,
18527                           struct line_header *lh, const char *comp_dir,
18528                           struct dwarf2_section_info *section,
18529                           int section_is_gnu, int section_is_dwz,
18530                           unsigned int offset_size,
18531                           struct objfile *objfile,
18532                           htab_t include_hash)
18533 {
18534   enum dwarf_macro_record_type macinfo_type;
18535   int at_commandline;
18536   gdb_byte *opcode_definitions[256];
18537
18538   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18539                                       &offset_size, section_is_gnu);
18540   if (mac_ptr == NULL)
18541     {
18542       /* We already issued a complaint.  */
18543       return;
18544     }
18545
18546   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
18547      GDB is still reading the definitions from command line.  First
18548      DW_MACINFO_start_file will need to be ignored as it was already executed
18549      to create CURRENT_FILE for the main source holding also the command line
18550      definitions.  On first met DW_MACINFO_start_file this flag is reset to
18551      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
18552
18553   at_commandline = 1;
18554
18555   do
18556     {
18557       /* Do we at least have room for a macinfo type byte?  */
18558       if (mac_ptr >= mac_end)
18559         {
18560           dwarf2_section_buffer_overflow_complaint (section);
18561           break;
18562         }
18563
18564       macinfo_type = read_1_byte (abfd, mac_ptr);
18565       mac_ptr++;
18566
18567       /* Note that we rely on the fact that the corresponding GNU and
18568          DWARF constants are the same.  */
18569       switch (macinfo_type)
18570         {
18571           /* A zero macinfo type indicates the end of the macro
18572              information.  */
18573         case 0:
18574           break;
18575
18576         case DW_MACRO_GNU_define:
18577         case DW_MACRO_GNU_undef:
18578         case DW_MACRO_GNU_define_indirect:
18579         case DW_MACRO_GNU_undef_indirect:
18580         case DW_MACRO_GNU_define_indirect_alt:
18581         case DW_MACRO_GNU_undef_indirect_alt:
18582           {
18583             unsigned int bytes_read;
18584             int line;
18585             char *body;
18586             int is_define;
18587
18588             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18589             mac_ptr += bytes_read;
18590
18591             if (macinfo_type == DW_MACRO_GNU_define
18592                 || macinfo_type == DW_MACRO_GNU_undef)
18593               {
18594                 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18595                 mac_ptr += bytes_read;
18596               }
18597             else
18598               {
18599                 LONGEST str_offset;
18600
18601                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18602                 mac_ptr += offset_size;
18603
18604                 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18605                     || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18606                     || section_is_dwz)
18607                   {
18608                     struct dwz_file *dwz = dwarf2_get_dwz_file ();
18609
18610                     body = read_indirect_string_from_dwz (dwz, str_offset);
18611                   }
18612                 else
18613                   body = read_indirect_string_at_offset (abfd, str_offset);
18614               }
18615
18616             is_define = (macinfo_type == DW_MACRO_GNU_define
18617                          || macinfo_type == DW_MACRO_GNU_define_indirect
18618                          || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18619             if (! current_file)
18620               {
18621                 /* DWARF violation as no main source is present.  */
18622                 complaint (&symfile_complaints,
18623                            _("debug info with no main source gives macro %s "
18624                              "on line %d: %s"),
18625                            is_define ? _("definition") : _("undefinition"),
18626                            line, body);
18627                 break;
18628               }
18629             if ((line == 0 && !at_commandline)
18630                 || (line != 0 && at_commandline))
18631               complaint (&symfile_complaints,
18632                          _("debug info gives %s macro %s with %s line %d: %s"),
18633                          at_commandline ? _("command-line") : _("in-file"),
18634                          is_define ? _("definition") : _("undefinition"),
18635                          line == 0 ? _("zero") : _("non-zero"), line, body);
18636
18637             if (is_define)
18638               parse_macro_definition (current_file, line, body);
18639             else
18640               {
18641                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18642                             || macinfo_type == DW_MACRO_GNU_undef_indirect
18643                             || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18644                 macro_undef (current_file, line, body);
18645               }
18646           }
18647           break;
18648
18649         case DW_MACRO_GNU_start_file:
18650           {
18651             unsigned int bytes_read;
18652             int line, file;
18653
18654             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18655             mac_ptr += bytes_read;
18656             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18657             mac_ptr += bytes_read;
18658
18659             if ((line == 0 && !at_commandline)
18660                 || (line != 0 && at_commandline))
18661               complaint (&symfile_complaints,
18662                          _("debug info gives source %d included "
18663                            "from %s at %s line %d"),
18664                          file, at_commandline ? _("command-line") : _("file"),
18665                          line == 0 ? _("zero") : _("non-zero"), line);
18666
18667             if (at_commandline)
18668               {
18669                 /* This DW_MACRO_GNU_start_file was executed in the
18670                    pass one.  */
18671                 at_commandline = 0;
18672               }
18673             else
18674               current_file = macro_start_file (file, line,
18675                                                current_file, comp_dir,
18676                                                lh, objfile);
18677           }
18678           break;
18679
18680         case DW_MACRO_GNU_end_file:
18681           if (! current_file)
18682             complaint (&symfile_complaints,
18683                        _("macro debug info has an unmatched "
18684                          "`close_file' directive"));
18685           else
18686             {
18687               current_file = current_file->included_by;
18688               if (! current_file)
18689                 {
18690                   enum dwarf_macro_record_type next_type;
18691
18692                   /* GCC circa March 2002 doesn't produce the zero
18693                      type byte marking the end of the compilation
18694                      unit.  Complain if it's not there, but exit no
18695                      matter what.  */
18696
18697                   /* Do we at least have room for a macinfo type byte?  */
18698                   if (mac_ptr >= mac_end)
18699                     {
18700                       dwarf2_section_buffer_overflow_complaint (section);
18701                       return;
18702                     }
18703
18704                   /* We don't increment mac_ptr here, so this is just
18705                      a look-ahead.  */
18706                   next_type = read_1_byte (abfd, mac_ptr);
18707                   if (next_type != 0)
18708                     complaint (&symfile_complaints,
18709                                _("no terminating 0-type entry for "
18710                                  "macros in `.debug_macinfo' section"));
18711
18712                   return;
18713                 }
18714             }
18715           break;
18716
18717         case DW_MACRO_GNU_transparent_include:
18718         case DW_MACRO_GNU_transparent_include_alt:
18719           {
18720             LONGEST offset;
18721             void **slot;
18722             bfd *include_bfd = abfd;
18723             struct dwarf2_section_info *include_section = section;
18724             struct dwarf2_section_info alt_section;
18725             gdb_byte *include_mac_end = mac_end;
18726             int is_dwz = section_is_dwz;
18727             gdb_byte *new_mac_ptr;
18728
18729             offset = read_offset_1 (abfd, mac_ptr, offset_size);
18730             mac_ptr += offset_size;
18731
18732             if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18733               {
18734                 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18735
18736                 dwarf2_read_section (dwarf2_per_objfile->objfile,
18737                                      &dwz->macro);
18738
18739                 include_bfd = dwz->macro.asection->owner;
18740                 include_section = &dwz->macro;
18741                 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18742                 is_dwz = 1;
18743               }
18744
18745             new_mac_ptr = include_section->buffer + offset;
18746             slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18747
18748             if (*slot != NULL)
18749               {
18750                 /* This has actually happened; see
18751                    http://sourceware.org/bugzilla/show_bug.cgi?id=13568.  */
18752                 complaint (&symfile_complaints,
18753                            _("recursive DW_MACRO_GNU_transparent_include in "
18754                              ".debug_macro section"));
18755               }
18756             else
18757               {
18758                 *slot = new_mac_ptr;
18759
18760                 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18761                                           include_mac_end, current_file,
18762                                           lh, comp_dir,
18763                                           section, section_is_gnu, is_dwz,
18764                                           offset_size, objfile, include_hash);
18765
18766                 htab_remove_elt (include_hash, new_mac_ptr);
18767               }
18768           }
18769           break;
18770
18771         case DW_MACINFO_vendor_ext:
18772           if (!section_is_gnu)
18773             {
18774               unsigned int bytes_read;
18775               int constant;
18776
18777               constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18778               mac_ptr += bytes_read;
18779               read_direct_string (abfd, mac_ptr, &bytes_read);
18780               mac_ptr += bytes_read;
18781
18782               /* We don't recognize any vendor extensions.  */
18783               break;
18784             }
18785           /* FALLTHROUGH */
18786
18787         default:
18788           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18789                                          mac_ptr, mac_end, abfd, offset_size,
18790                                          section);
18791           if (mac_ptr == NULL)
18792             return;
18793           break;
18794         }
18795     } while (macinfo_type != 0);
18796 }
18797
18798 static void
18799 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18800                      const char *comp_dir, int section_is_gnu)
18801 {
18802   struct objfile *objfile = dwarf2_per_objfile->objfile;
18803   struct line_header *lh = cu->line_header;
18804   bfd *abfd;
18805   gdb_byte *mac_ptr, *mac_end;
18806   struct macro_source_file *current_file = 0;
18807   enum dwarf_macro_record_type macinfo_type;
18808   unsigned int offset_size = cu->header.offset_size;
18809   gdb_byte *opcode_definitions[256];
18810   struct cleanup *cleanup;
18811   htab_t include_hash;
18812   void **slot;
18813   struct dwarf2_section_info *section;
18814   const char *section_name;
18815
18816   if (cu->dwo_unit != NULL)
18817     {
18818       if (section_is_gnu)
18819         {
18820           section = &cu->dwo_unit->dwo_file->sections.macro;
18821           section_name = ".debug_macro.dwo";
18822         }
18823       else
18824         {
18825           section = &cu->dwo_unit->dwo_file->sections.macinfo;
18826           section_name = ".debug_macinfo.dwo";
18827         }
18828     }
18829   else
18830     {
18831       if (section_is_gnu)
18832         {
18833           section = &dwarf2_per_objfile->macro;
18834           section_name = ".debug_macro";
18835         }
18836       else
18837         {
18838           section = &dwarf2_per_objfile->macinfo;
18839           section_name = ".debug_macinfo";
18840         }
18841     }
18842
18843   dwarf2_read_section (objfile, section);
18844   if (section->buffer == NULL)
18845     {
18846       complaint (&symfile_complaints, _("missing %s section"), section_name);
18847       return;
18848     }
18849   abfd = section->asection->owner;
18850
18851   /* First pass: Find the name of the base filename.
18852      This filename is needed in order to process all macros whose definition
18853      (or undefinition) comes from the command line.  These macros are defined
18854      before the first DW_MACINFO_start_file entry, and yet still need to be
18855      associated to the base file.
18856
18857      To determine the base file name, we scan the macro definitions until we
18858      reach the first DW_MACINFO_start_file entry.  We then initialize
18859      CURRENT_FILE accordingly so that any macro definition found before the
18860      first DW_MACINFO_start_file can still be associated to the base file.  */
18861
18862   mac_ptr = section->buffer + offset;
18863   mac_end = section->buffer + section->size;
18864
18865   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18866                                       &offset_size, section_is_gnu);
18867   if (mac_ptr == NULL)
18868     {
18869       /* We already issued a complaint.  */
18870       return;
18871     }
18872
18873   do
18874     {
18875       /* Do we at least have room for a macinfo type byte?  */
18876       if (mac_ptr >= mac_end)
18877         {
18878           /* Complaint is printed during the second pass as GDB will probably
18879              stop the first pass earlier upon finding
18880              DW_MACINFO_start_file.  */
18881           break;
18882         }
18883
18884       macinfo_type = read_1_byte (abfd, mac_ptr);
18885       mac_ptr++;
18886
18887       /* Note that we rely on the fact that the corresponding GNU and
18888          DWARF constants are the same.  */
18889       switch (macinfo_type)
18890         {
18891           /* A zero macinfo type indicates the end of the macro
18892              information.  */
18893         case 0:
18894           break;
18895
18896         case DW_MACRO_GNU_define:
18897         case DW_MACRO_GNU_undef:
18898           /* Only skip the data by MAC_PTR.  */
18899           {
18900             unsigned int bytes_read;
18901
18902             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18903             mac_ptr += bytes_read;
18904             read_direct_string (abfd, mac_ptr, &bytes_read);
18905             mac_ptr += bytes_read;
18906           }
18907           break;
18908
18909         case DW_MACRO_GNU_start_file:
18910           {
18911             unsigned int bytes_read;
18912             int line, file;
18913
18914             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18915             mac_ptr += bytes_read;
18916             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18917             mac_ptr += bytes_read;
18918
18919             current_file = macro_start_file (file, line, current_file,
18920                                              comp_dir, lh, objfile);
18921           }
18922           break;
18923
18924         case DW_MACRO_GNU_end_file:
18925           /* No data to skip by MAC_PTR.  */
18926           break;
18927
18928         case DW_MACRO_GNU_define_indirect:
18929         case DW_MACRO_GNU_undef_indirect:
18930         case DW_MACRO_GNU_define_indirect_alt:
18931         case DW_MACRO_GNU_undef_indirect_alt:
18932           {
18933             unsigned int bytes_read;
18934
18935             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18936             mac_ptr += bytes_read;
18937             mac_ptr += offset_size;
18938           }
18939           break;
18940
18941         case DW_MACRO_GNU_transparent_include:
18942         case DW_MACRO_GNU_transparent_include_alt:
18943           /* Note that, according to the spec, a transparent include
18944              chain cannot call DW_MACRO_GNU_start_file.  So, we can just
18945              skip this opcode.  */
18946           mac_ptr += offset_size;
18947           break;
18948
18949         case DW_MACINFO_vendor_ext:
18950           /* Only skip the data by MAC_PTR.  */
18951           if (!section_is_gnu)
18952             {
18953               unsigned int bytes_read;
18954
18955               read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18956               mac_ptr += bytes_read;
18957               read_direct_string (abfd, mac_ptr, &bytes_read);
18958               mac_ptr += bytes_read;
18959             }
18960           /* FALLTHROUGH */
18961
18962         default:
18963           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18964                                          mac_ptr, mac_end, abfd, offset_size,
18965                                          section);
18966           if (mac_ptr == NULL)
18967             return;
18968           break;
18969         }
18970     } while (macinfo_type != 0 && current_file == NULL);
18971
18972   /* Second pass: Process all entries.
18973
18974      Use the AT_COMMAND_LINE flag to determine whether we are still processing
18975      command-line macro definitions/undefinitions.  This flag is unset when we
18976      reach the first DW_MACINFO_start_file entry.  */
18977
18978   include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18979                                     NULL, xcalloc, xfree);
18980   cleanup = make_cleanup_htab_delete (include_hash);
18981   mac_ptr = section->buffer + offset;
18982   slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18983   *slot = mac_ptr;
18984   dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
18985                             current_file, lh, comp_dir, section,
18986                             section_is_gnu, 0,
18987                             offset_size, objfile, include_hash);
18988   do_cleanups (cleanup);
18989 }
18990
18991 /* Check if the attribute's form is a DW_FORM_block*
18992    if so return true else false.  */
18993
18994 static int
18995 attr_form_is_block (struct attribute *attr)
18996 {
18997   return (attr == NULL ? 0 :
18998       attr->form == DW_FORM_block1
18999       || attr->form == DW_FORM_block2
19000       || attr->form == DW_FORM_block4
19001       || attr->form == DW_FORM_block
19002       || attr->form == DW_FORM_exprloc);
19003 }
19004
19005 /* Return non-zero if ATTR's value is a section offset --- classes
19006    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19007    You may use DW_UNSND (attr) to retrieve such offsets.
19008
19009    Section 7.5.4, "Attribute Encodings", explains that no attribute
19010    may have a value that belongs to more than one of these classes; it
19011    would be ambiguous if we did, because we use the same forms for all
19012    of them.  */
19013
19014 static int
19015 attr_form_is_section_offset (struct attribute *attr)
19016 {
19017   return (attr->form == DW_FORM_data4
19018           || attr->form == DW_FORM_data8
19019           || attr->form == DW_FORM_sec_offset);
19020 }
19021
19022 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19023    zero otherwise.  When this function returns true, you can apply
19024    dwarf2_get_attr_constant_value to it.
19025
19026    However, note that for some attributes you must check
19027    attr_form_is_section_offset before using this test.  DW_FORM_data4
19028    and DW_FORM_data8 are members of both the constant class, and of
19029    the classes that contain offsets into other debug sections
19030    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
19031    that, if an attribute's can be either a constant or one of the
19032    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19033    taken as section offsets, not constants.  */
19034
19035 static int
19036 attr_form_is_constant (struct attribute *attr)
19037 {
19038   switch (attr->form)
19039     {
19040     case DW_FORM_sdata:
19041     case DW_FORM_udata:
19042     case DW_FORM_data1:
19043     case DW_FORM_data2:
19044     case DW_FORM_data4:
19045     case DW_FORM_data8:
19046       return 1;
19047     default:
19048       return 0;
19049     }
19050 }
19051
19052 /* Return the .debug_loc section to use for CU.
19053    For DWO files use .debug_loc.dwo.  */
19054
19055 static struct dwarf2_section_info *
19056 cu_debug_loc_section (struct dwarf2_cu *cu)
19057 {
19058   if (cu->dwo_unit)
19059     return &cu->dwo_unit->dwo_file->sections.loc;
19060   return &dwarf2_per_objfile->loc;
19061 }
19062
19063 /* A helper function that fills in a dwarf2_loclist_baton.  */
19064
19065 static void
19066 fill_in_loclist_baton (struct dwarf2_cu *cu,
19067                        struct dwarf2_loclist_baton *baton,
19068                        struct attribute *attr)
19069 {
19070   struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19071
19072   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19073
19074   baton->per_cu = cu->per_cu;
19075   gdb_assert (baton->per_cu);
19076   /* We don't know how long the location list is, but make sure we
19077      don't run off the edge of the section.  */
19078   baton->size = section->size - DW_UNSND (attr);
19079   baton->data = section->buffer + DW_UNSND (attr);
19080   baton->base_address = cu->base_address;
19081   baton->from_dwo = cu->dwo_unit != NULL;
19082 }
19083
19084 static void
19085 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19086                              struct dwarf2_cu *cu, int is_block)
19087 {
19088   struct objfile *objfile = dwarf2_per_objfile->objfile;
19089   struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19090
19091   if (attr_form_is_section_offset (attr)
19092       /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19093          the section.  If so, fall through to the complaint in the
19094          other branch.  */
19095       && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19096     {
19097       struct dwarf2_loclist_baton *baton;
19098
19099       baton = obstack_alloc (&objfile->objfile_obstack,
19100                              sizeof (struct dwarf2_loclist_baton));
19101
19102       fill_in_loclist_baton (cu, baton, attr);
19103
19104       if (cu->base_known == 0)
19105         complaint (&symfile_complaints,
19106                    _("Location list used without "
19107                      "specifying the CU base address."));
19108
19109       SYMBOL_ACLASS_INDEX (sym) = (is_block
19110                                    ? dwarf2_loclist_block_index
19111                                    : dwarf2_loclist_index);
19112       SYMBOL_LOCATION_BATON (sym) = baton;
19113     }
19114   else
19115     {
19116       struct dwarf2_locexpr_baton *baton;
19117
19118       baton = obstack_alloc (&objfile->objfile_obstack,
19119                              sizeof (struct dwarf2_locexpr_baton));
19120       baton->per_cu = cu->per_cu;
19121       gdb_assert (baton->per_cu);
19122
19123       if (attr_form_is_block (attr))
19124         {
19125           /* Note that we're just copying the block's data pointer
19126              here, not the actual data.  We're still pointing into the
19127              info_buffer for SYM's objfile; right now we never release
19128              that buffer, but when we do clean up properly this may
19129              need to change.  */
19130           baton->size = DW_BLOCK (attr)->size;
19131           baton->data = DW_BLOCK (attr)->data;
19132         }
19133       else
19134         {
19135           dwarf2_invalid_attrib_class_complaint ("location description",
19136                                                  SYMBOL_NATURAL_NAME (sym));
19137           baton->size = 0;
19138         }
19139
19140       SYMBOL_ACLASS_INDEX (sym) = (is_block
19141                                    ? dwarf2_locexpr_block_index
19142                                    : dwarf2_locexpr_index);
19143       SYMBOL_LOCATION_BATON (sym) = baton;
19144     }
19145 }
19146
19147 /* Return the OBJFILE associated with the compilation unit CU.  If CU
19148    came from a separate debuginfo file, then the master objfile is
19149    returned.  */
19150
19151 struct objfile *
19152 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19153 {
19154   struct objfile *objfile = per_cu->objfile;
19155
19156   /* Return the master objfile, so that we can report and look up the
19157      correct file containing this variable.  */
19158   if (objfile->separate_debug_objfile_backlink)
19159     objfile = objfile->separate_debug_objfile_backlink;
19160
19161   return objfile;
19162 }
19163
19164 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19165    (CU_HEADERP is unused in such case) or prepare a temporary copy at
19166    CU_HEADERP first.  */
19167
19168 static const struct comp_unit_head *
19169 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19170                        struct dwarf2_per_cu_data *per_cu)
19171 {
19172   gdb_byte *info_ptr;
19173
19174   if (per_cu->cu)
19175     return &per_cu->cu->header;
19176
19177   info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
19178
19179   memset (cu_headerp, 0, sizeof (*cu_headerp));
19180   read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19181
19182   return cu_headerp;
19183 }
19184
19185 /* Return the address size given in the compilation unit header for CU.  */
19186
19187 int
19188 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19189 {
19190   struct comp_unit_head cu_header_local;
19191   const struct comp_unit_head *cu_headerp;
19192
19193   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19194
19195   return cu_headerp->addr_size;
19196 }
19197
19198 /* Return the offset size given in the compilation unit header for CU.  */
19199
19200 int
19201 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19202 {
19203   struct comp_unit_head cu_header_local;
19204   const struct comp_unit_head *cu_headerp;
19205
19206   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19207
19208   return cu_headerp->offset_size;
19209 }
19210
19211 /* See its dwarf2loc.h declaration.  */
19212
19213 int
19214 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19215 {
19216   struct comp_unit_head cu_header_local;
19217   const struct comp_unit_head *cu_headerp;
19218
19219   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19220
19221   if (cu_headerp->version == 2)
19222     return cu_headerp->addr_size;
19223   else
19224     return cu_headerp->offset_size;
19225 }
19226
19227 /* Return the text offset of the CU.  The returned offset comes from
19228    this CU's objfile.  If this objfile came from a separate debuginfo
19229    file, then the offset may be different from the corresponding
19230    offset in the parent objfile.  */
19231
19232 CORE_ADDR
19233 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19234 {
19235   struct objfile *objfile = per_cu->objfile;
19236
19237   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19238 }
19239
19240 /* Locate the .debug_info compilation unit from CU's objfile which contains
19241    the DIE at OFFSET.  Raises an error on failure.  */
19242
19243 static struct dwarf2_per_cu_data *
19244 dwarf2_find_containing_comp_unit (sect_offset offset,
19245                                   unsigned int offset_in_dwz,
19246                                   struct objfile *objfile)
19247 {
19248   struct dwarf2_per_cu_data *this_cu;
19249   int low, high;
19250   const sect_offset *cu_off;
19251
19252   low = 0;
19253   high = dwarf2_per_objfile->n_comp_units - 1;
19254   while (high > low)
19255     {
19256       struct dwarf2_per_cu_data *mid_cu;
19257       int mid = low + (high - low) / 2;
19258
19259       mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19260       cu_off = &mid_cu->offset;
19261       if (mid_cu->is_dwz > offset_in_dwz
19262           || (mid_cu->is_dwz == offset_in_dwz
19263               && cu_off->sect_off >= offset.sect_off))
19264         high = mid;
19265       else
19266         low = mid + 1;
19267     }
19268   gdb_assert (low == high);
19269   this_cu = dwarf2_per_objfile->all_comp_units[low];
19270   cu_off = &this_cu->offset;
19271   if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19272     {
19273       if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19274         error (_("Dwarf Error: could not find partial DIE containing "
19275                "offset 0x%lx [in module %s]"),
19276                (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19277
19278       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19279                   <= offset.sect_off);
19280       return dwarf2_per_objfile->all_comp_units[low-1];
19281     }
19282   else
19283     {
19284       this_cu = dwarf2_per_objfile->all_comp_units[low];
19285       if (low == dwarf2_per_objfile->n_comp_units - 1
19286           && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19287         error (_("invalid dwarf2 offset %u"), offset.sect_off);
19288       gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19289       return this_cu;
19290     }
19291 }
19292
19293 /* Initialize dwarf2_cu CU, owned by PER_CU.  */
19294
19295 static void
19296 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19297 {
19298   memset (cu, 0, sizeof (*cu));
19299   per_cu->cu = cu;
19300   cu->per_cu = per_cu;
19301   cu->objfile = per_cu->objfile;
19302   obstack_init (&cu->comp_unit_obstack);
19303 }
19304
19305 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
19306
19307 static void
19308 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19309                        enum language pretend_language)
19310 {
19311   struct attribute *attr;
19312
19313   /* Set the language we're debugging.  */
19314   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19315   if (attr)
19316     set_cu_language (DW_UNSND (attr), cu);
19317   else
19318     {
19319       cu->language = pretend_language;
19320       cu->language_defn = language_def (cu->language);
19321     }
19322
19323   attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19324   if (attr)
19325     cu->producer = DW_STRING (attr);
19326 }
19327
19328 /* Release one cached compilation unit, CU.  We unlink it from the tree
19329    of compilation units, but we don't remove it from the read_in_chain;
19330    the caller is responsible for that.
19331    NOTE: DATA is a void * because this function is also used as a
19332    cleanup routine.  */
19333
19334 static void
19335 free_heap_comp_unit (void *data)
19336 {
19337   struct dwarf2_cu *cu = data;
19338
19339   gdb_assert (cu->per_cu != NULL);
19340   cu->per_cu->cu = NULL;
19341   cu->per_cu = NULL;
19342
19343   obstack_free (&cu->comp_unit_obstack, NULL);
19344
19345   xfree (cu);
19346 }
19347
19348 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19349    when we're finished with it.  We can't free the pointer itself, but be
19350    sure to unlink it from the cache.  Also release any associated storage.  */
19351
19352 static void
19353 free_stack_comp_unit (void *data)
19354 {
19355   struct dwarf2_cu *cu = data;
19356
19357   gdb_assert (cu->per_cu != NULL);
19358   cu->per_cu->cu = NULL;
19359   cu->per_cu = NULL;
19360
19361   obstack_free (&cu->comp_unit_obstack, NULL);
19362   cu->partial_dies = NULL;
19363 }
19364
19365 /* Free all cached compilation units.  */
19366
19367 static void
19368 free_cached_comp_units (void *data)
19369 {
19370   struct dwarf2_per_cu_data *per_cu, **last_chain;
19371
19372   per_cu = dwarf2_per_objfile->read_in_chain;
19373   last_chain = &dwarf2_per_objfile->read_in_chain;
19374   while (per_cu != NULL)
19375     {
19376       struct dwarf2_per_cu_data *next_cu;
19377
19378       next_cu = per_cu->cu->read_in_chain;
19379
19380       free_heap_comp_unit (per_cu->cu);
19381       *last_chain = next_cu;
19382
19383       per_cu = next_cu;
19384     }
19385 }
19386
19387 /* Increase the age counter on each cached compilation unit, and free
19388    any that are too old.  */
19389
19390 static void
19391 age_cached_comp_units (void)
19392 {
19393   struct dwarf2_per_cu_data *per_cu, **last_chain;
19394
19395   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19396   per_cu = dwarf2_per_objfile->read_in_chain;
19397   while (per_cu != NULL)
19398     {
19399       per_cu->cu->last_used ++;
19400       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19401         dwarf2_mark (per_cu->cu);
19402       per_cu = per_cu->cu->read_in_chain;
19403     }
19404
19405   per_cu = dwarf2_per_objfile->read_in_chain;
19406   last_chain = &dwarf2_per_objfile->read_in_chain;
19407   while (per_cu != NULL)
19408     {
19409       struct dwarf2_per_cu_data *next_cu;
19410
19411       next_cu = per_cu->cu->read_in_chain;
19412
19413       if (!per_cu->cu->mark)
19414         {
19415           free_heap_comp_unit (per_cu->cu);
19416           *last_chain = next_cu;
19417         }
19418       else
19419         last_chain = &per_cu->cu->read_in_chain;
19420
19421       per_cu = next_cu;
19422     }
19423 }
19424
19425 /* Remove a single compilation unit from the cache.  */
19426
19427 static void
19428 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19429 {
19430   struct dwarf2_per_cu_data *per_cu, **last_chain;
19431
19432   per_cu = dwarf2_per_objfile->read_in_chain;
19433   last_chain = &dwarf2_per_objfile->read_in_chain;
19434   while (per_cu != NULL)
19435     {
19436       struct dwarf2_per_cu_data *next_cu;
19437
19438       next_cu = per_cu->cu->read_in_chain;
19439
19440       if (per_cu == target_per_cu)
19441         {
19442           free_heap_comp_unit (per_cu->cu);
19443           per_cu->cu = NULL;
19444           *last_chain = next_cu;
19445           break;
19446         }
19447       else
19448         last_chain = &per_cu->cu->read_in_chain;
19449
19450       per_cu = next_cu;
19451     }
19452 }
19453
19454 /* Release all extra memory associated with OBJFILE.  */
19455
19456 void
19457 dwarf2_free_objfile (struct objfile *objfile)
19458 {
19459   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19460
19461   if (dwarf2_per_objfile == NULL)
19462     return;
19463
19464   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
19465   free_cached_comp_units (NULL);
19466
19467   if (dwarf2_per_objfile->quick_file_names_table)
19468     htab_delete (dwarf2_per_objfile->quick_file_names_table);
19469
19470   /* Everything else should be on the objfile obstack.  */
19471 }
19472
19473 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19474    We store these in a hash table separate from the DIEs, and preserve them
19475    when the DIEs are flushed out of cache.
19476
19477    The CU "per_cu" pointer is needed because offset alone is not enough to
19478    uniquely identify the type.  A file may have multiple .debug_types sections,
19479    or the type may come from a DWO file.  We have to use something in
19480    dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19481    routine, get_die_type_at_offset, from outside this file, and thus won't
19482    necessarily have PER_CU->cu.  Fortunately, PER_CU is stable for the life
19483    of the objfile.  */
19484
19485 struct dwarf2_per_cu_offset_and_type
19486 {
19487   const struct dwarf2_per_cu_data *per_cu;
19488   sect_offset offset;
19489   struct type *type;
19490 };
19491
19492 /* Hash function for a dwarf2_per_cu_offset_and_type.  */
19493
19494 static hashval_t
19495 per_cu_offset_and_type_hash (const void *item)
19496 {
19497   const struct dwarf2_per_cu_offset_and_type *ofs = item;
19498
19499   return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19500 }
19501
19502 /* Equality function for a dwarf2_per_cu_offset_and_type.  */
19503
19504 static int
19505 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19506 {
19507   const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19508   const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19509
19510   return (ofs_lhs->per_cu == ofs_rhs->per_cu
19511           && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19512 }
19513
19514 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
19515    table if necessary.  For convenience, return TYPE.
19516
19517    The DIEs reading must have careful ordering to:
19518     * Not cause infite loops trying to read in DIEs as a prerequisite for
19519       reading current DIE.
19520     * Not trying to dereference contents of still incompletely read in types
19521       while reading in other DIEs.
19522     * Enable referencing still incompletely read in types just by a pointer to
19523       the type without accessing its fields.
19524
19525    Therefore caller should follow these rules:
19526      * Try to fetch any prerequisite types we may need to build this DIE type
19527        before building the type and calling set_die_type.
19528      * After building type call set_die_type for current DIE as soon as
19529        possible before fetching more types to complete the current type.
19530      * Make the type as complete as possible before fetching more types.  */
19531
19532 static struct type *
19533 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19534 {
19535   struct dwarf2_per_cu_offset_and_type **slot, ofs;
19536   struct objfile *objfile = cu->objfile;
19537
19538   /* For Ada types, make sure that the gnat-specific data is always
19539      initialized (if not already set).  There are a few types where
19540      we should not be doing so, because the type-specific area is
19541      already used to hold some other piece of info (eg: TYPE_CODE_FLT
19542      where the type-specific area is used to store the floatformat).
19543      But this is not a problem, because the gnat-specific information
19544      is actually not needed for these types.  */
19545   if (need_gnat_info (cu)
19546       && TYPE_CODE (type) != TYPE_CODE_FUNC
19547       && TYPE_CODE (type) != TYPE_CODE_FLT
19548       && !HAVE_GNAT_AUX_INFO (type))
19549     INIT_GNAT_SPECIFIC (type);
19550
19551   if (dwarf2_per_objfile->die_type_hash == NULL)
19552     {
19553       dwarf2_per_objfile->die_type_hash =
19554         htab_create_alloc_ex (127,
19555                               per_cu_offset_and_type_hash,
19556                               per_cu_offset_and_type_eq,
19557                               NULL,
19558                               &objfile->objfile_obstack,
19559                               hashtab_obstack_allocate,
19560                               dummy_obstack_deallocate);
19561     }
19562
19563   ofs.per_cu = cu->per_cu;
19564   ofs.offset = die->offset;
19565   ofs.type = type;
19566   slot = (struct dwarf2_per_cu_offset_and_type **)
19567     htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19568   if (*slot)
19569     complaint (&symfile_complaints,
19570                _("A problem internal to GDB: DIE 0x%x has type already set"),
19571                die->offset.sect_off);
19572   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19573   **slot = ofs;
19574   return type;
19575 }
19576
19577 /* Look up the type for the die at OFFSET in the appropriate type_hash
19578    table, or return NULL if the die does not have a saved type.  */
19579
19580 static struct type *
19581 get_die_type_at_offset (sect_offset offset,
19582                         struct dwarf2_per_cu_data *per_cu)
19583 {
19584   struct dwarf2_per_cu_offset_and_type *slot, ofs;
19585
19586   if (dwarf2_per_objfile->die_type_hash == NULL)
19587     return NULL;
19588
19589   ofs.per_cu = per_cu;
19590   ofs.offset = offset;
19591   slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19592   if (slot)
19593     return slot->type;
19594   else
19595     return NULL;
19596 }
19597
19598 /* Look up the type for DIE in the appropriate type_hash table,
19599    or return NULL if DIE does not have a saved type.  */
19600
19601 static struct type *
19602 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19603 {
19604   return get_die_type_at_offset (die->offset, cu->per_cu);
19605 }
19606
19607 /* Add a dependence relationship from CU to REF_PER_CU.  */
19608
19609 static void
19610 dwarf2_add_dependence (struct dwarf2_cu *cu,
19611                        struct dwarf2_per_cu_data *ref_per_cu)
19612 {
19613   void **slot;
19614
19615   if (cu->dependencies == NULL)
19616     cu->dependencies
19617       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19618                               NULL, &cu->comp_unit_obstack,
19619                               hashtab_obstack_allocate,
19620                               dummy_obstack_deallocate);
19621
19622   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19623   if (*slot == NULL)
19624     *slot = ref_per_cu;
19625 }
19626
19627 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19628    Set the mark field in every compilation unit in the
19629    cache that we must keep because we are keeping CU.  */
19630
19631 static int
19632 dwarf2_mark_helper (void **slot, void *data)
19633 {
19634   struct dwarf2_per_cu_data *per_cu;
19635
19636   per_cu = (struct dwarf2_per_cu_data *) *slot;
19637
19638   /* cu->dependencies references may not yet have been ever read if QUIT aborts
19639      reading of the chain.  As such dependencies remain valid it is not much
19640      useful to track and undo them during QUIT cleanups.  */
19641   if (per_cu->cu == NULL)
19642     return 1;
19643
19644   if (per_cu->cu->mark)
19645     return 1;
19646   per_cu->cu->mark = 1;
19647
19648   if (per_cu->cu->dependencies != NULL)
19649     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19650
19651   return 1;
19652 }
19653
19654 /* Set the mark field in CU and in every other compilation unit in the
19655    cache that we must keep because we are keeping CU.  */
19656
19657 static void
19658 dwarf2_mark (struct dwarf2_cu *cu)
19659 {
19660   if (cu->mark)
19661     return;
19662   cu->mark = 1;
19663   if (cu->dependencies != NULL)
19664     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19665 }
19666
19667 static void
19668 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19669 {
19670   while (per_cu)
19671     {
19672       per_cu->cu->mark = 0;
19673       per_cu = per_cu->cu->read_in_chain;
19674     }
19675 }
19676
19677 /* Trivial hash function for partial_die_info: the hash value of a DIE
19678    is its offset in .debug_info for this objfile.  */
19679
19680 static hashval_t
19681 partial_die_hash (const void *item)
19682 {
19683   const struct partial_die_info *part_die = item;
19684
19685   return part_die->offset.sect_off;
19686 }
19687
19688 /* Trivial comparison function for partial_die_info structures: two DIEs
19689    are equal if they have the same offset.  */
19690
19691 static int
19692 partial_die_eq (const void *item_lhs, const void *item_rhs)
19693 {
19694   const struct partial_die_info *part_die_lhs = item_lhs;
19695   const struct partial_die_info *part_die_rhs = item_rhs;
19696
19697   return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19698 }
19699
19700 static struct cmd_list_element *set_dwarf2_cmdlist;
19701 static struct cmd_list_element *show_dwarf2_cmdlist;
19702
19703 static void
19704 set_dwarf2_cmd (char *args, int from_tty)
19705 {
19706   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19707 }
19708
19709 static void
19710 show_dwarf2_cmd (char *args, int from_tty)
19711 {
19712   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19713 }
19714
19715 /* Free data associated with OBJFILE, if necessary.  */
19716
19717 static void
19718 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19719 {
19720   struct dwarf2_per_objfile *data = d;
19721   int ix;
19722
19723   for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19724     VEC_free (dwarf2_per_cu_ptr,
19725               dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19726
19727   for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19728     VEC_free (dwarf2_per_cu_ptr,
19729               dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
19730
19731   VEC_free (dwarf2_section_info_def, data->types);
19732
19733   if (data->dwo_files)
19734     free_dwo_files (data->dwo_files, objfile);
19735
19736   if (data->dwz_file && data->dwz_file->dwz_bfd)
19737     gdb_bfd_unref (data->dwz_file->dwz_bfd);
19738 }
19739
19740 \f
19741 /* The "save gdb-index" command.  */
19742
19743 /* The contents of the hash table we create when building the string
19744    table.  */
19745 struct strtab_entry
19746 {
19747   offset_type offset;
19748   const char *str;
19749 };
19750
19751 /* Hash function for a strtab_entry.
19752
19753    Function is used only during write_hash_table so no index format backward
19754    compatibility is needed.  */
19755
19756 static hashval_t
19757 hash_strtab_entry (const void *e)
19758 {
19759   const struct strtab_entry *entry = e;
19760   return mapped_index_string_hash (INT_MAX, entry->str);
19761 }
19762
19763 /* Equality function for a strtab_entry.  */
19764
19765 static int
19766 eq_strtab_entry (const void *a, const void *b)
19767 {
19768   const struct strtab_entry *ea = a;
19769   const struct strtab_entry *eb = b;
19770   return !strcmp (ea->str, eb->str);
19771 }
19772
19773 /* Create a strtab_entry hash table.  */
19774
19775 static htab_t
19776 create_strtab (void)
19777 {
19778   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19779                             xfree, xcalloc, xfree);
19780 }
19781
19782 /* Add a string to the constant pool.  Return the string's offset in
19783    host order.  */
19784
19785 static offset_type
19786 add_string (htab_t table, struct obstack *cpool, const char *str)
19787 {
19788   void **slot;
19789   struct strtab_entry entry;
19790   struct strtab_entry *result;
19791
19792   entry.str = str;
19793   slot = htab_find_slot (table, &entry, INSERT);
19794   if (*slot)
19795     result = *slot;
19796   else
19797     {
19798       result = XNEW (struct strtab_entry);
19799       result->offset = obstack_object_size (cpool);
19800       result->str = str;
19801       obstack_grow_str0 (cpool, str);
19802       *slot = result;
19803     }
19804   return result->offset;
19805 }
19806
19807 /* An entry in the symbol table.  */
19808 struct symtab_index_entry
19809 {
19810   /* The name of the symbol.  */
19811   const char *name;
19812   /* The offset of the name in the constant pool.  */
19813   offset_type index_offset;
19814   /* A sorted vector of the indices of all the CUs that hold an object
19815      of this name.  */
19816   VEC (offset_type) *cu_indices;
19817 };
19818
19819 /* The symbol table.  This is a power-of-2-sized hash table.  */
19820 struct mapped_symtab
19821 {
19822   offset_type n_elements;
19823   offset_type size;
19824   struct symtab_index_entry **data;
19825 };
19826
19827 /* Hash function for a symtab_index_entry.  */
19828
19829 static hashval_t
19830 hash_symtab_entry (const void *e)
19831 {
19832   const struct symtab_index_entry *entry = e;
19833   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19834                          sizeof (offset_type) * VEC_length (offset_type,
19835                                                             entry->cu_indices),
19836                          0);
19837 }
19838
19839 /* Equality function for a symtab_index_entry.  */
19840
19841 static int
19842 eq_symtab_entry (const void *a, const void *b)
19843 {
19844   const struct symtab_index_entry *ea = a;
19845   const struct symtab_index_entry *eb = b;
19846   int len = VEC_length (offset_type, ea->cu_indices);
19847   if (len != VEC_length (offset_type, eb->cu_indices))
19848     return 0;
19849   return !memcmp (VEC_address (offset_type, ea->cu_indices),
19850                   VEC_address (offset_type, eb->cu_indices),
19851                   sizeof (offset_type) * len);
19852 }
19853
19854 /* Destroy a symtab_index_entry.  */
19855
19856 static void
19857 delete_symtab_entry (void *p)
19858 {
19859   struct symtab_index_entry *entry = p;
19860   VEC_free (offset_type, entry->cu_indices);
19861   xfree (entry);
19862 }
19863
19864 /* Create a hash table holding symtab_index_entry objects.  */
19865
19866 static htab_t
19867 create_symbol_hash_table (void)
19868 {
19869   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19870                             delete_symtab_entry, xcalloc, xfree);
19871 }
19872
19873 /* Create a new mapped symtab object.  */
19874
19875 static struct mapped_symtab *
19876 create_mapped_symtab (void)
19877 {
19878   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19879   symtab->n_elements = 0;
19880   symtab->size = 1024;
19881   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19882   return symtab;
19883 }
19884
19885 /* Destroy a mapped_symtab.  */
19886
19887 static void
19888 cleanup_mapped_symtab (void *p)
19889 {
19890   struct mapped_symtab *symtab = p;
19891   /* The contents of the array are freed when the other hash table is
19892      destroyed.  */
19893   xfree (symtab->data);
19894   xfree (symtab);
19895 }
19896
19897 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
19898    the slot.
19899    
19900    Function is used only during write_hash_table so no index format backward
19901    compatibility is needed.  */
19902
19903 static struct symtab_index_entry **
19904 find_slot (struct mapped_symtab *symtab, const char *name)
19905 {
19906   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19907
19908   index = hash & (symtab->size - 1);
19909   step = ((hash * 17) & (symtab->size - 1)) | 1;
19910
19911   for (;;)
19912     {
19913       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19914         return &symtab->data[index];
19915       index = (index + step) & (symtab->size - 1);
19916     }
19917 }
19918
19919 /* Expand SYMTAB's hash table.  */
19920
19921 static void
19922 hash_expand (struct mapped_symtab *symtab)
19923 {
19924   offset_type old_size = symtab->size;
19925   offset_type i;
19926   struct symtab_index_entry **old_entries = symtab->data;
19927
19928   symtab->size *= 2;
19929   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19930
19931   for (i = 0; i < old_size; ++i)
19932     {
19933       if (old_entries[i])
19934         {
19935           struct symtab_index_entry **slot = find_slot (symtab,
19936                                                         old_entries[i]->name);
19937           *slot = old_entries[i];
19938         }
19939     }
19940
19941   xfree (old_entries);
19942 }
19943
19944 /* Add an entry to SYMTAB.  NAME is the name of the symbol.
19945    CU_INDEX is the index of the CU in which the symbol appears.
19946    IS_STATIC is one if the symbol is static, otherwise zero (global).  */
19947
19948 static void
19949 add_index_entry (struct mapped_symtab *symtab, const char *name,
19950                  int is_static, gdb_index_symbol_kind kind,
19951                  offset_type cu_index)
19952 {
19953   struct symtab_index_entry **slot;
19954   offset_type cu_index_and_attrs;
19955
19956   ++symtab->n_elements;
19957   if (4 * symtab->n_elements / 3 >= symtab->size)
19958     hash_expand (symtab);
19959
19960   slot = find_slot (symtab, name);
19961   if (!*slot)
19962     {
19963       *slot = XNEW (struct symtab_index_entry);
19964       (*slot)->name = name;
19965       /* index_offset is set later.  */
19966       (*slot)->cu_indices = NULL;
19967     }
19968
19969   cu_index_and_attrs = 0;
19970   DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19971   DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19972   DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19973
19974   /* We don't want to record an index value twice as we want to avoid the
19975      duplication.
19976      We process all global symbols and then all static symbols
19977      (which would allow us to avoid the duplication by only having to check
19978      the last entry pushed), but a symbol could have multiple kinds in one CU.
19979      To keep things simple we don't worry about the duplication here and
19980      sort and uniqufy the list after we've processed all symbols.  */
19981   VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19982 }
19983
19984 /* qsort helper routine for uniquify_cu_indices.  */
19985
19986 static int
19987 offset_type_compare (const void *ap, const void *bp)
19988 {
19989   offset_type a = *(offset_type *) ap;
19990   offset_type b = *(offset_type *) bp;
19991
19992   return (a > b) - (b > a);
19993 }
19994
19995 /* Sort and remove duplicates of all symbols' cu_indices lists.  */
19996
19997 static void
19998 uniquify_cu_indices (struct mapped_symtab *symtab)
19999 {
20000   int i;
20001
20002   for (i = 0; i < symtab->size; ++i)
20003     {
20004       struct symtab_index_entry *entry = symtab->data[i];
20005
20006       if (entry
20007           && entry->cu_indices != NULL)
20008         {
20009           unsigned int next_to_insert, next_to_check;
20010           offset_type last_value;
20011
20012           qsort (VEC_address (offset_type, entry->cu_indices),
20013                  VEC_length (offset_type, entry->cu_indices),
20014                  sizeof (offset_type), offset_type_compare);
20015
20016           last_value = VEC_index (offset_type, entry->cu_indices, 0);
20017           next_to_insert = 1;
20018           for (next_to_check = 1;
20019                next_to_check < VEC_length (offset_type, entry->cu_indices);
20020                ++next_to_check)
20021             {
20022               if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20023                   != last_value)
20024                 {
20025                   last_value = VEC_index (offset_type, entry->cu_indices,
20026                                           next_to_check);
20027                   VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20028                                last_value);
20029                   ++next_to_insert;
20030                 }
20031             }
20032           VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20033         }
20034     }
20035 }
20036
20037 /* Add a vector of indices to the constant pool.  */
20038
20039 static offset_type
20040 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20041                       struct symtab_index_entry *entry)
20042 {
20043   void **slot;
20044
20045   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20046   if (!*slot)
20047     {
20048       offset_type len = VEC_length (offset_type, entry->cu_indices);
20049       offset_type val = MAYBE_SWAP (len);
20050       offset_type iter;
20051       int i;
20052
20053       *slot = entry;
20054       entry->index_offset = obstack_object_size (cpool);
20055
20056       obstack_grow (cpool, &val, sizeof (val));
20057       for (i = 0;
20058            VEC_iterate (offset_type, entry->cu_indices, i, iter);
20059            ++i)
20060         {
20061           val = MAYBE_SWAP (iter);
20062           obstack_grow (cpool, &val, sizeof (val));
20063         }
20064     }
20065   else
20066     {
20067       struct symtab_index_entry *old_entry = *slot;
20068       entry->index_offset = old_entry->index_offset;
20069       entry = old_entry;
20070     }
20071   return entry->index_offset;
20072 }
20073
20074 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20075    constant pool entries going into the obstack CPOOL.  */
20076
20077 static void
20078 write_hash_table (struct mapped_symtab *symtab,
20079                   struct obstack *output, struct obstack *cpool)
20080 {
20081   offset_type i;
20082   htab_t symbol_hash_table;
20083   htab_t str_table;
20084
20085   symbol_hash_table = create_symbol_hash_table ();
20086   str_table = create_strtab ();
20087
20088   /* We add all the index vectors to the constant pool first, to
20089      ensure alignment is ok.  */
20090   for (i = 0; i < symtab->size; ++i)
20091     {
20092       if (symtab->data[i])
20093         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20094     }
20095
20096   /* Now write out the hash table.  */
20097   for (i = 0; i < symtab->size; ++i)
20098     {
20099       offset_type str_off, vec_off;
20100
20101       if (symtab->data[i])
20102         {
20103           str_off = add_string (str_table, cpool, symtab->data[i]->name);
20104           vec_off = symtab->data[i]->index_offset;
20105         }
20106       else
20107         {
20108           /* While 0 is a valid constant pool index, it is not valid
20109              to have 0 for both offsets.  */
20110           str_off = 0;
20111           vec_off = 0;
20112         }
20113
20114       str_off = MAYBE_SWAP (str_off);
20115       vec_off = MAYBE_SWAP (vec_off);
20116
20117       obstack_grow (output, &str_off, sizeof (str_off));
20118       obstack_grow (output, &vec_off, sizeof (vec_off));
20119     }
20120
20121   htab_delete (str_table);
20122   htab_delete (symbol_hash_table);
20123 }
20124
20125 /* Struct to map psymtab to CU index in the index file.  */
20126 struct psymtab_cu_index_map
20127 {
20128   struct partial_symtab *psymtab;
20129   unsigned int cu_index;
20130 };
20131
20132 static hashval_t
20133 hash_psymtab_cu_index (const void *item)
20134 {
20135   const struct psymtab_cu_index_map *map = item;
20136
20137   return htab_hash_pointer (map->psymtab);
20138 }
20139
20140 static int
20141 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20142 {
20143   const struct psymtab_cu_index_map *lhs = item_lhs;
20144   const struct psymtab_cu_index_map *rhs = item_rhs;
20145
20146   return lhs->psymtab == rhs->psymtab;
20147 }
20148
20149 /* Helper struct for building the address table.  */
20150 struct addrmap_index_data
20151 {
20152   struct objfile *objfile;
20153   struct obstack *addr_obstack;
20154   htab_t cu_index_htab;
20155
20156   /* Non-zero if the previous_* fields are valid.
20157      We can't write an entry until we see the next entry (since it is only then
20158      that we know the end of the entry).  */
20159   int previous_valid;
20160   /* Index of the CU in the table of all CUs in the index file.  */
20161   unsigned int previous_cu_index;
20162   /* Start address of the CU.  */
20163   CORE_ADDR previous_cu_start;
20164 };
20165
20166 /* Write an address entry to OBSTACK.  */
20167
20168 static void
20169 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20170                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20171 {
20172   offset_type cu_index_to_write;
20173   char addr[8];
20174   CORE_ADDR baseaddr;
20175
20176   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20177
20178   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20179   obstack_grow (obstack, addr, 8);
20180   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20181   obstack_grow (obstack, addr, 8);
20182   cu_index_to_write = MAYBE_SWAP (cu_index);
20183   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20184 }
20185
20186 /* Worker function for traversing an addrmap to build the address table.  */
20187
20188 static int
20189 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20190 {
20191   struct addrmap_index_data *data = datap;
20192   struct partial_symtab *pst = obj;
20193
20194   if (data->previous_valid)
20195     add_address_entry (data->objfile, data->addr_obstack,
20196                        data->previous_cu_start, start_addr,
20197                        data->previous_cu_index);
20198
20199   data->previous_cu_start = start_addr;
20200   if (pst != NULL)
20201     {
20202       struct psymtab_cu_index_map find_map, *map;
20203       find_map.psymtab = pst;
20204       map = htab_find (data->cu_index_htab, &find_map);
20205       gdb_assert (map != NULL);
20206       data->previous_cu_index = map->cu_index;
20207       data->previous_valid = 1;
20208     }
20209   else
20210       data->previous_valid = 0;
20211
20212   return 0;
20213 }
20214
20215 /* Write OBJFILE's address map to OBSTACK.
20216    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20217    in the index file.  */
20218
20219 static void
20220 write_address_map (struct objfile *objfile, struct obstack *obstack,
20221                    htab_t cu_index_htab)
20222 {
20223   struct addrmap_index_data addrmap_index_data;
20224
20225   /* When writing the address table, we have to cope with the fact that
20226      the addrmap iterator only provides the start of a region; we have to
20227      wait until the next invocation to get the start of the next region.  */
20228
20229   addrmap_index_data.objfile = objfile;
20230   addrmap_index_data.addr_obstack = obstack;
20231   addrmap_index_data.cu_index_htab = cu_index_htab;
20232   addrmap_index_data.previous_valid = 0;
20233
20234   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20235                    &addrmap_index_data);
20236
20237   /* It's highly unlikely the last entry (end address = 0xff...ff)
20238      is valid, but we should still handle it.
20239      The end address is recorded as the start of the next region, but that
20240      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
20241      anyway.  */
20242   if (addrmap_index_data.previous_valid)
20243     add_address_entry (objfile, obstack,
20244                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20245                        addrmap_index_data.previous_cu_index);
20246 }
20247
20248 /* Return the symbol kind of PSYM.  */
20249
20250 static gdb_index_symbol_kind
20251 symbol_kind (struct partial_symbol *psym)
20252 {
20253   domain_enum domain = PSYMBOL_DOMAIN (psym);
20254   enum address_class aclass = PSYMBOL_CLASS (psym);
20255
20256   switch (domain)
20257     {
20258     case VAR_DOMAIN:
20259       switch (aclass)
20260         {
20261         case LOC_BLOCK:
20262           return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20263         case LOC_TYPEDEF:
20264           return GDB_INDEX_SYMBOL_KIND_TYPE;
20265         case LOC_COMPUTED:
20266         case LOC_CONST_BYTES:
20267         case LOC_OPTIMIZED_OUT:
20268         case LOC_STATIC:
20269           return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20270         case LOC_CONST:
20271           /* Note: It's currently impossible to recognize psyms as enum values
20272              short of reading the type info.  For now punt.  */
20273           return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20274         default:
20275           /* There are other LOC_FOO values that one might want to classify
20276              as variables, but dwarf2read.c doesn't currently use them.  */
20277           return GDB_INDEX_SYMBOL_KIND_OTHER;
20278         }
20279     case STRUCT_DOMAIN:
20280       return GDB_INDEX_SYMBOL_KIND_TYPE;
20281     default:
20282       return GDB_INDEX_SYMBOL_KIND_OTHER;
20283     }
20284 }
20285
20286 /* Add a list of partial symbols to SYMTAB.  */
20287
20288 static void
20289 write_psymbols (struct mapped_symtab *symtab,
20290                 htab_t psyms_seen,
20291                 struct partial_symbol **psymp,
20292                 int count,
20293                 offset_type cu_index,
20294                 int is_static)
20295 {
20296   for (; count-- > 0; ++psymp)
20297     {
20298       struct partial_symbol *psym = *psymp;
20299       void **slot;
20300
20301       if (SYMBOL_LANGUAGE (psym) == language_ada)
20302         error (_("Ada is not currently supported by the index"));
20303
20304       /* Only add a given psymbol once.  */
20305       slot = htab_find_slot (psyms_seen, psym, INSERT);
20306       if (!*slot)
20307         {
20308           gdb_index_symbol_kind kind = symbol_kind (psym);
20309
20310           *slot = psym;
20311           add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20312                            is_static, kind, cu_index);
20313         }
20314     }
20315 }
20316
20317 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
20318    exception if there is an error.  */
20319
20320 static void
20321 write_obstack (FILE *file, struct obstack *obstack)
20322 {
20323   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20324               file)
20325       != obstack_object_size (obstack))
20326     error (_("couldn't data write to file"));
20327 }
20328
20329 /* Unlink a file if the argument is not NULL.  */
20330
20331 static void
20332 unlink_if_set (void *p)
20333 {
20334   char **filename = p;
20335   if (*filename)
20336     unlink (*filename);
20337 }
20338
20339 /* A helper struct used when iterating over debug_types.  */
20340 struct signatured_type_index_data
20341 {
20342   struct objfile *objfile;
20343   struct mapped_symtab *symtab;
20344   struct obstack *types_list;
20345   htab_t psyms_seen;
20346   int cu_index;
20347 };
20348
20349 /* A helper function that writes a single signatured_type to an
20350    obstack.  */
20351
20352 static int
20353 write_one_signatured_type (void **slot, void *d)
20354 {
20355   struct signatured_type_index_data *info = d;
20356   struct signatured_type *entry = (struct signatured_type *) *slot;
20357   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20358   struct partial_symtab *psymtab = per_cu->v.psymtab;
20359   gdb_byte val[8];
20360
20361   write_psymbols (info->symtab,
20362                   info->psyms_seen,
20363                   info->objfile->global_psymbols.list
20364                   + psymtab->globals_offset,
20365                   psymtab->n_global_syms, info->cu_index,
20366                   0);
20367   write_psymbols (info->symtab,
20368                   info->psyms_seen,
20369                   info->objfile->static_psymbols.list
20370                   + psymtab->statics_offset,
20371                   psymtab->n_static_syms, info->cu_index,
20372                   1);
20373
20374   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20375                           entry->per_cu.offset.sect_off);
20376   obstack_grow (info->types_list, val, 8);
20377   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20378                           entry->type_offset_in_tu.cu_off);
20379   obstack_grow (info->types_list, val, 8);
20380   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20381   obstack_grow (info->types_list, val, 8);
20382
20383   ++info->cu_index;
20384
20385   return 1;
20386 }
20387
20388 /* Recurse into all "included" dependencies and write their symbols as
20389    if they appeared in this psymtab.  */
20390
20391 static void
20392 recursively_write_psymbols (struct objfile *objfile,
20393                             struct partial_symtab *psymtab,
20394                             struct mapped_symtab *symtab,
20395                             htab_t psyms_seen,
20396                             offset_type cu_index)
20397 {
20398   int i;
20399
20400   for (i = 0; i < psymtab->number_of_dependencies; ++i)
20401     if (psymtab->dependencies[i]->user != NULL)
20402       recursively_write_psymbols (objfile, psymtab->dependencies[i],
20403                                   symtab, psyms_seen, cu_index);
20404
20405   write_psymbols (symtab,
20406                   psyms_seen,
20407                   objfile->global_psymbols.list + psymtab->globals_offset,
20408                   psymtab->n_global_syms, cu_index,
20409                   0);
20410   write_psymbols (symtab,
20411                   psyms_seen,
20412                   objfile->static_psymbols.list + psymtab->statics_offset,
20413                   psymtab->n_static_syms, cu_index,
20414                   1);
20415 }
20416
20417 /* Create an index file for OBJFILE in the directory DIR.  */
20418
20419 static void
20420 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20421 {
20422   struct cleanup *cleanup;
20423   char *filename, *cleanup_filename;
20424   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20425   struct obstack cu_list, types_cu_list;
20426   int i;
20427   FILE *out_file;
20428   struct mapped_symtab *symtab;
20429   offset_type val, size_of_contents, total_len;
20430   struct stat st;
20431   htab_t psyms_seen;
20432   htab_t cu_index_htab;
20433   struct psymtab_cu_index_map *psymtab_cu_index_map;
20434
20435   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20436     return;
20437
20438   if (dwarf2_per_objfile->using_index)
20439     error (_("Cannot use an index to create the index"));
20440
20441   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20442     error (_("Cannot make an index when the file has multiple .debug_types sections"));
20443
20444   if (stat (objfile->name, &st) < 0)
20445     perror_with_name (objfile->name);
20446
20447   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20448                      INDEX_SUFFIX, (char *) NULL);
20449   cleanup = make_cleanup (xfree, filename);
20450
20451   out_file = fopen (filename, "wb");
20452   if (!out_file)
20453     error (_("Can't open `%s' for writing"), filename);
20454
20455   cleanup_filename = filename;
20456   make_cleanup (unlink_if_set, &cleanup_filename);
20457
20458   symtab = create_mapped_symtab ();
20459   make_cleanup (cleanup_mapped_symtab, symtab);
20460
20461   obstack_init (&addr_obstack);
20462   make_cleanup_obstack_free (&addr_obstack);
20463
20464   obstack_init (&cu_list);
20465   make_cleanup_obstack_free (&cu_list);
20466
20467   obstack_init (&types_cu_list);
20468   make_cleanup_obstack_free (&types_cu_list);
20469
20470   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20471                                   NULL, xcalloc, xfree);
20472   make_cleanup_htab_delete (psyms_seen);
20473
20474   /* While we're scanning CU's create a table that maps a psymtab pointer
20475      (which is what addrmap records) to its index (which is what is recorded
20476      in the index file).  This will later be needed to write the address
20477      table.  */
20478   cu_index_htab = htab_create_alloc (100,
20479                                      hash_psymtab_cu_index,
20480                                      eq_psymtab_cu_index,
20481                                      NULL, xcalloc, xfree);
20482   make_cleanup_htab_delete (cu_index_htab);
20483   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20484     xmalloc (sizeof (struct psymtab_cu_index_map)
20485              * dwarf2_per_objfile->n_comp_units);
20486   make_cleanup (xfree, psymtab_cu_index_map);
20487
20488   /* The CU list is already sorted, so we don't need to do additional
20489      work here.  Also, the debug_types entries do not appear in
20490      all_comp_units, but only in their own hash table.  */
20491   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20492     {
20493       struct dwarf2_per_cu_data *per_cu
20494         = dwarf2_per_objfile->all_comp_units[i];
20495       struct partial_symtab *psymtab = per_cu->v.psymtab;
20496       gdb_byte val[8];
20497       struct psymtab_cu_index_map *map;
20498       void **slot;
20499
20500       if (psymtab->user == NULL)
20501         recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20502
20503       map = &psymtab_cu_index_map[i];
20504       map->psymtab = psymtab;
20505       map->cu_index = i;
20506       slot = htab_find_slot (cu_index_htab, map, INSERT);
20507       gdb_assert (slot != NULL);
20508       gdb_assert (*slot == NULL);
20509       *slot = map;
20510
20511       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20512                               per_cu->offset.sect_off);
20513       obstack_grow (&cu_list, val, 8);
20514       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20515       obstack_grow (&cu_list, val, 8);
20516     }
20517
20518   /* Dump the address map.  */
20519   write_address_map (objfile, &addr_obstack, cu_index_htab);
20520
20521   /* Write out the .debug_type entries, if any.  */
20522   if (dwarf2_per_objfile->signatured_types)
20523     {
20524       struct signatured_type_index_data sig_data;
20525
20526       sig_data.objfile = objfile;
20527       sig_data.symtab = symtab;
20528       sig_data.types_list = &types_cu_list;
20529       sig_data.psyms_seen = psyms_seen;
20530       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20531       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20532                               write_one_signatured_type, &sig_data);
20533     }
20534
20535   /* Now that we've processed all symbols we can shrink their cu_indices
20536      lists.  */
20537   uniquify_cu_indices (symtab);
20538
20539   obstack_init (&constant_pool);
20540   make_cleanup_obstack_free (&constant_pool);
20541   obstack_init (&symtab_obstack);
20542   make_cleanup_obstack_free (&symtab_obstack);
20543   write_hash_table (symtab, &symtab_obstack, &constant_pool);
20544
20545   obstack_init (&contents);
20546   make_cleanup_obstack_free (&contents);
20547   size_of_contents = 6 * sizeof (offset_type);
20548   total_len = size_of_contents;
20549
20550   /* The version number.  */
20551   val = MAYBE_SWAP (8);
20552   obstack_grow (&contents, &val, sizeof (val));
20553
20554   /* The offset of the CU list from the start of the file.  */
20555   val = MAYBE_SWAP (total_len);
20556   obstack_grow (&contents, &val, sizeof (val));
20557   total_len += obstack_object_size (&cu_list);
20558
20559   /* The offset of the types CU list from the start of the file.  */
20560   val = MAYBE_SWAP (total_len);
20561   obstack_grow (&contents, &val, sizeof (val));
20562   total_len += obstack_object_size (&types_cu_list);
20563
20564   /* The offset of the address table from the start of the file.  */
20565   val = MAYBE_SWAP (total_len);
20566   obstack_grow (&contents, &val, sizeof (val));
20567   total_len += obstack_object_size (&addr_obstack);
20568
20569   /* The offset of the symbol table from the start of the file.  */
20570   val = MAYBE_SWAP (total_len);
20571   obstack_grow (&contents, &val, sizeof (val));
20572   total_len += obstack_object_size (&symtab_obstack);
20573
20574   /* The offset of the constant pool from the start of the file.  */
20575   val = MAYBE_SWAP (total_len);
20576   obstack_grow (&contents, &val, sizeof (val));
20577   total_len += obstack_object_size (&constant_pool);
20578
20579   gdb_assert (obstack_object_size (&contents) == size_of_contents);
20580
20581   write_obstack (out_file, &contents);
20582   write_obstack (out_file, &cu_list);
20583   write_obstack (out_file, &types_cu_list);
20584   write_obstack (out_file, &addr_obstack);
20585   write_obstack (out_file, &symtab_obstack);
20586   write_obstack (out_file, &constant_pool);
20587
20588   fclose (out_file);
20589
20590   /* We want to keep the file, so we set cleanup_filename to NULL
20591      here.  See unlink_if_set.  */
20592   cleanup_filename = NULL;
20593
20594   do_cleanups (cleanup);
20595 }
20596
20597 /* Implementation of the `save gdb-index' command.
20598    
20599    Note that the file format used by this command is documented in the
20600    GDB manual.  Any changes here must be documented there.  */
20601
20602 static void
20603 save_gdb_index_command (char *arg, int from_tty)
20604 {
20605   struct objfile *objfile;
20606
20607   if (!arg || !*arg)
20608     error (_("usage: save gdb-index DIRECTORY"));
20609
20610   ALL_OBJFILES (objfile)
20611   {
20612     struct stat st;
20613
20614     /* If the objfile does not correspond to an actual file, skip it.  */
20615     if (stat (objfile->name, &st) < 0)
20616       continue;
20617
20618     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20619     if (dwarf2_per_objfile)
20620       {
20621         volatile struct gdb_exception except;
20622
20623         TRY_CATCH (except, RETURN_MASK_ERROR)
20624           {
20625             write_psymtabs_to_index (objfile, arg);
20626           }
20627         if (except.reason < 0)
20628           exception_fprintf (gdb_stderr, except,
20629                              _("Error while writing index for `%s': "),
20630                              objfile->name);
20631       }
20632   }
20633 }
20634
20635 \f
20636
20637 int dwarf2_always_disassemble;
20638
20639 static void
20640 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20641                                 struct cmd_list_element *c, const char *value)
20642 {
20643   fprintf_filtered (file,
20644                     _("Whether to always disassemble "
20645                       "DWARF expressions is %s.\n"),
20646                     value);
20647 }
20648
20649 static void
20650 show_check_physname (struct ui_file *file, int from_tty,
20651                      struct cmd_list_element *c, const char *value)
20652 {
20653   fprintf_filtered (file,
20654                     _("Whether to check \"physname\" is %s.\n"),
20655                     value);
20656 }
20657
20658 void _initialize_dwarf2_read (void);
20659
20660 void
20661 _initialize_dwarf2_read (void)
20662 {
20663   struct cmd_list_element *c;
20664
20665   dwarf2_objfile_data_key
20666     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20667
20668   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20669 Set DWARF 2 specific variables.\n\
20670 Configure DWARF 2 variables such as the cache size"),
20671                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20672                   0/*allow-unknown*/, &maintenance_set_cmdlist);
20673
20674   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20675 Show DWARF 2 specific variables\n\
20676 Show DWARF 2 variables such as the cache size"),
20677                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20678                   0/*allow-unknown*/, &maintenance_show_cmdlist);
20679
20680   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20681                             &dwarf2_max_cache_age, _("\
20682 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20683 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20684 A higher limit means that cached compilation units will be stored\n\
20685 in memory longer, and more total memory will be used.  Zero disables\n\
20686 caching, which can slow down startup."),
20687                             NULL,
20688                             show_dwarf2_max_cache_age,
20689                             &set_dwarf2_cmdlist,
20690                             &show_dwarf2_cmdlist);
20691
20692   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20693                            &dwarf2_always_disassemble, _("\
20694 Set whether `info address' always disassembles DWARF expressions."), _("\
20695 Show whether `info address' always disassembles DWARF expressions."), _("\
20696 When enabled, DWARF expressions are always printed in an assembly-like\n\
20697 syntax.  When disabled, expressions will be printed in a more\n\
20698 conversational style, when possible."),
20699                            NULL,
20700                            show_dwarf2_always_disassemble,
20701                            &set_dwarf2_cmdlist,
20702                            &show_dwarf2_cmdlist);
20703
20704   add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20705 Set debugging of the dwarf2 reader."), _("\
20706 Show debugging of the dwarf2 reader."), _("\
20707 When enabled, debugging messages are printed during dwarf2 reading\n\
20708 and symtab expansion."),
20709                             NULL,
20710                             NULL,
20711                             &setdebuglist, &showdebuglist);
20712
20713   add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20714 Set debugging of the dwarf2 DIE reader."), _("\
20715 Show debugging of the dwarf2 DIE reader."), _("\
20716 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20717 The value is the maximum depth to print."),
20718                              NULL,
20719                              NULL,
20720                              &setdebuglist, &showdebuglist);
20721
20722   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20723 Set cross-checking of \"physname\" code against demangler."), _("\
20724 Show cross-checking of \"physname\" code against demangler."), _("\
20725 When enabled, GDB's internal \"physname\" code is checked against\n\
20726 the demangler."),
20727                            NULL, show_check_physname,
20728                            &setdebuglist, &showdebuglist);
20729
20730   add_setshow_boolean_cmd ("use-deprecated-index-sections",
20731                            no_class, &use_deprecated_index_sections, _("\
20732 Set whether to use deprecated gdb_index sections."), _("\
20733 Show whether to use deprecated gdb_index sections."), _("\
20734 When enabled, deprecated .gdb_index sections are used anyway.\n\
20735 Normally they are ignored either because of a missing feature or\n\
20736 performance issue.\n\
20737 Warning: This option must be enabled before gdb reads the file."),
20738                            NULL,
20739                            NULL,
20740                            &setlist, &showlist);
20741
20742   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20743                _("\
20744 Save a gdb-index file.\n\
20745 Usage: save gdb-index DIRECTORY"),
20746                &save_cmdlist);
20747   set_cmd_completer (c, filename_completer);
20748
20749   dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20750                                                         &dwarf2_locexpr_funcs);
20751   dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20752                                                         &dwarf2_loclist_funcs);
20753
20754   dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20755                                         &dwarf2_block_frame_base_locexpr_funcs);
20756   dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20757                                         &dwarf2_block_frame_base_loclist_funcs);
20758 }